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_CALL] = sizeof(call_expression_t),
205 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
206 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
207 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
208 [EXPR_SELECT] = sizeof(select_expression_t),
209 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
210 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
211 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
212 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
213 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
214 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
215 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
216 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
217 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
218 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
219 [EXPR_VA_START] = sizeof(va_start_expression_t),
220 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
221 [EXPR_STATEMENT] = sizeof(statement_expression_t),
223 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
224 return sizes[EXPR_UNARY_FIRST];
226 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
227 return sizes[EXPR_BINARY_FIRST];
229 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
230 assert(sizes[kind] != 0);
235 * Allocate an expression node of given kind and initialize all
238 static expression_t *allocate_expression_zero(expression_kind_t kind)
240 size_t size = get_expression_struct_size(kind);
241 expression_t *res = allocate_ast_zero(size);
243 res->base.kind = kind;
244 res->base.type = type_error_type;
249 * Returns the size of a type node.
251 * @param kind the type kind
253 static size_t get_type_struct_size(type_kind_t kind)
255 static const size_t sizes[] = {
256 [TYPE_ATOMIC] = sizeof(atomic_type_t),
257 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
258 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
259 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
260 [TYPE_ENUM] = sizeof(enum_type_t),
261 [TYPE_FUNCTION] = sizeof(function_type_t),
262 [TYPE_POINTER] = sizeof(pointer_type_t),
263 [TYPE_ARRAY] = sizeof(array_type_t),
264 [TYPE_BUILTIN] = sizeof(builtin_type_t),
265 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
266 [TYPE_TYPEOF] = sizeof(typeof_type_t),
268 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
269 assert(kind <= TYPE_TYPEOF);
270 assert(sizes[kind] != 0);
275 * Allocate a type node of given kind and initialize all
278 static type_t *allocate_type_zero(type_kind_t kind, source_position_t source_position)
280 size_t size = get_type_struct_size(kind);
281 type_t *res = obstack_alloc(type_obst, size);
282 memset(res, 0, size);
284 res->base.kind = kind;
285 res->base.source_position = source_position;
290 * Returns the size of an initializer node.
292 * @param kind the initializer kind
294 static size_t get_initializer_size(initializer_kind_t kind)
296 static const size_t sizes[] = {
297 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
298 [INITIALIZER_STRING] = sizeof(initializer_string_t),
299 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
300 [INITIALIZER_LIST] = sizeof(initializer_list_t)
302 assert(kind < sizeof(sizes) / sizeof(*sizes));
303 assert(sizes[kind] != 0);
308 * Allocate an initializer node of given kind and initialize all
311 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
313 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
320 * Free a type from the type obstack.
322 static void free_type(void *type)
324 obstack_free(type_obst, type);
328 * Returns the index of the top element of the environment stack.
330 static size_t environment_top(void)
332 return ARR_LEN(environment_stack);
336 * Returns the index of the top element of the label stack.
338 static size_t label_top(void)
340 return ARR_LEN(label_stack);
345 * Return the next token.
347 static inline void next_token(void)
349 token = lookahead_buffer[lookahead_bufpos];
350 lookahead_buffer[lookahead_bufpos] = lexer_token;
353 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
356 print_token(stderr, &token);
357 fprintf(stderr, "\n");
362 * Return the next token with a given lookahead.
364 static inline const token_t *look_ahead(int num)
366 assert(num > 0 && num <= MAX_LOOKAHEAD);
367 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
368 return &lookahead_buffer[pos];
371 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
374 * Report a parse error because an expected token was not found.
376 static void parse_error_expected(const char *message, ...)
378 if(message != NULL) {
379 errorf(HERE, "%s", message);
382 va_start(ap, message);
383 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
388 * Report a type error.
390 static void type_error(const char *msg, const source_position_t source_position,
393 errorf(source_position, "%s, but found type '%T'", msg, type);
397 * Report an incompatible type.
399 static void type_error_incompatible(const char *msg,
400 const source_position_t source_position, type_t *type1, type_t *type2)
402 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
406 * Eat an complete block, ie. '{ ... }'.
408 static void eat_block(void)
410 if(token.type == '{')
413 while(token.type != '}') {
414 if(token.type == T_EOF)
416 if(token.type == '{') {
426 * Eat a statement until an ';' token.
428 static void eat_statement(void)
430 while(token.type != ';') {
431 if(token.type == T_EOF)
433 if(token.type == '}')
435 if(token.type == '{') {
445 * Eat a parenthesed term, ie. '( ... )'.
447 static void eat_paren(void)
449 if(token.type == '(')
452 while(token.type != ')') {
453 if(token.type == T_EOF)
455 if(token.type == ')' || token.type == ';' || token.type == '}') {
458 if(token.type == '(') {
462 if(token.type == '{') {
471 #define expect(expected) \
472 if(UNLIKELY(token.type != (expected))) { \
473 parse_error_expected(NULL, (expected), 0); \
479 #define expect_block(expected) \
480 if(UNLIKELY(token.type != (expected))) { \
481 parse_error_expected(NULL, (expected), 0); \
487 #define expect_void(expected) \
488 if(UNLIKELY(token.type != (expected))) { \
489 parse_error_expected(NULL, (expected), 0); \
495 static void set_scope(scope_t *new_scope)
499 last_declaration = new_scope->declarations;
500 if(last_declaration != NULL) {
501 while(last_declaration->next != NULL) {
502 last_declaration = last_declaration->next;
508 * Search a symbol in a given namespace and returns its declaration or
509 * NULL if this symbol was not found.
511 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
513 declaration_t *declaration = symbol->declaration;
514 for( ; declaration != NULL; declaration = declaration->symbol_next) {
515 if(declaration->namespc == namespc)
523 * pushs an environment_entry on the environment stack and links the
524 * corresponding symbol to the new entry
526 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
528 symbol_t *symbol = declaration->symbol;
529 namespace_t namespc = (namespace_t)declaration->namespc;
531 /* replace/add declaration into declaration list of the symbol */
532 declaration_t *iter = symbol->declaration;
534 symbol->declaration = declaration;
536 declaration_t *iter_last = NULL;
537 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
538 /* replace an entry? */
539 if(iter->namespc == namespc) {
540 if(iter_last == NULL) {
541 symbol->declaration = declaration;
543 iter_last->symbol_next = declaration;
545 declaration->symbol_next = iter->symbol_next;
550 assert(iter_last->symbol_next == NULL);
551 iter_last->symbol_next = declaration;
555 /* remember old declaration */
557 entry.symbol = symbol;
558 entry.old_declaration = iter;
559 entry.namespc = (unsigned short) namespc;
560 ARR_APP1(stack_entry_t, *stack_ptr, entry);
563 static void environment_push(declaration_t *declaration)
565 assert(declaration->source_position.input_name != NULL);
566 assert(declaration->parent_scope != NULL);
567 stack_push(&environment_stack, declaration);
570 static void label_push(declaration_t *declaration)
572 declaration->parent_scope = ¤t_function->scope;
573 stack_push(&label_stack, declaration);
577 * pops symbols from the environment stack until @p new_top is the top element
579 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
581 stack_entry_t *stack = *stack_ptr;
582 size_t top = ARR_LEN(stack);
585 assert(new_top <= top);
589 for(i = top; i > new_top; --i) {
590 stack_entry_t *entry = &stack[i - 1];
592 declaration_t *old_declaration = entry->old_declaration;
593 symbol_t *symbol = entry->symbol;
594 namespace_t namespc = (namespace_t)entry->namespc;
596 /* replace/remove declaration */
597 declaration_t *declaration = symbol->declaration;
598 assert(declaration != NULL);
599 if(declaration->namespc == namespc) {
600 if(old_declaration == NULL) {
601 symbol->declaration = declaration->symbol_next;
603 symbol->declaration = old_declaration;
606 declaration_t *iter_last = declaration;
607 declaration_t *iter = declaration->symbol_next;
608 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
609 /* replace an entry? */
610 if(iter->namespc == namespc) {
611 assert(iter_last != NULL);
612 iter_last->symbol_next = old_declaration;
613 old_declaration->symbol_next = iter->symbol_next;
617 assert(iter != NULL);
621 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
624 static void environment_pop_to(size_t new_top)
626 stack_pop_to(&environment_stack, new_top);
629 static void label_pop_to(size_t new_top)
631 stack_pop_to(&label_stack, new_top);
635 static int get_rank(const type_t *type)
637 assert(!is_typeref(type));
638 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
639 * and esp. footnote 108). However we can't fold constants (yet), so we
640 * can't decide whether unsigned int is possible, while int always works.
641 * (unsigned int would be preferable when possible... for stuff like
642 * struct { enum { ... } bla : 4; } ) */
643 if(type->kind == TYPE_ENUM)
644 return ATOMIC_TYPE_INT;
646 assert(type->kind == TYPE_ATOMIC);
647 return type->atomic.akind;
650 static type_t *promote_integer(type_t *type)
652 if(type->kind == TYPE_BITFIELD)
653 type = type->bitfield.base;
655 if(get_rank(type) < ATOMIC_TYPE_INT)
662 * Create a cast expression.
664 * @param expression the expression to cast
665 * @param dest_type the destination type
667 static expression_t *create_cast_expression(expression_t *expression,
670 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
672 cast->unary.value = expression;
673 cast->base.type = dest_type;
679 * Check if a given expression represents the 0 pointer constant.
681 static bool is_null_pointer_constant(const expression_t *expression)
683 /* skip void* cast */
684 if(expression->kind == EXPR_UNARY_CAST
685 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
686 expression = expression->unary.value;
689 /* TODO: not correct yet, should be any constant integer expression
690 * which evaluates to 0 */
691 if (expression->kind != EXPR_CONST)
694 type_t *const type = skip_typeref(expression->base.type);
695 if (!is_type_integer(type))
698 return expression->conste.v.int_value == 0;
702 * Create an implicit cast expression.
704 * @param expression the expression to cast
705 * @param dest_type the destination type
707 static expression_t *create_implicit_cast(expression_t *expression,
710 type_t *const source_type = expression->base.type;
712 if (source_type == dest_type)
715 return create_cast_expression(expression, dest_type);
718 /** Implements the rules from § 6.5.16.1 */
719 static type_t *semantic_assign(type_t *orig_type_left,
720 const expression_t *const right,
723 type_t *const orig_type_right = right->base.type;
724 type_t *const type_left = skip_typeref(orig_type_left);
725 type_t *const type_right = skip_typeref(orig_type_right);
727 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
728 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
729 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
730 && is_type_pointer(type_right))) {
731 return orig_type_left;
734 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
735 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
736 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
738 /* the left type has all qualifiers from the right type */
739 unsigned missing_qualifiers
740 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
741 if(missing_qualifiers != 0) {
742 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
743 return orig_type_left;
746 points_to_left = get_unqualified_type(points_to_left);
747 points_to_right = get_unqualified_type(points_to_right);
749 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
750 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
751 return orig_type_left;
754 if (!types_compatible(points_to_left, points_to_right)) {
755 warningf(right->base.source_position,
756 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
757 orig_type_left, context, right, orig_type_right);
760 return orig_type_left;
763 if (is_type_compound(type_left) && is_type_compound(type_right)) {
764 type_t *const unqual_type_left = get_unqualified_type(type_left);
765 type_t *const unqual_type_right = get_unqualified_type(type_right);
766 if (types_compatible(unqual_type_left, unqual_type_right)) {
767 return orig_type_left;
771 if (!is_type_valid(type_left))
774 if (!is_type_valid(type_right))
775 return orig_type_right;
780 static expression_t *parse_constant_expression(void)
782 /* start parsing at precedence 7 (conditional expression) */
783 expression_t *result = parse_sub_expression(7);
785 if(!is_constant_expression(result)) {
786 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
792 static expression_t *parse_assignment_expression(void)
794 /* start parsing at precedence 2 (assignment expression) */
795 return parse_sub_expression(2);
798 static type_t *make_global_typedef(const char *name, type_t *type)
800 symbol_t *const symbol = symbol_table_insert(name);
802 declaration_t *const declaration = allocate_declaration_zero();
803 declaration->namespc = NAMESPACE_NORMAL;
804 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
805 declaration->type = type;
806 declaration->symbol = symbol;
807 declaration->source_position = builtin_source_position;
809 record_declaration(declaration);
811 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
812 typedef_type->typedeft.declaration = declaration;
817 static string_t parse_string_literals(void)
819 assert(token.type == T_STRING_LITERAL);
820 string_t result = token.v.string;
824 while (token.type == T_STRING_LITERAL) {
825 result = concat_strings(&result, &token.v.string);
832 static void parse_attributes(void)
836 case T___attribute__: {
844 errorf(HERE, "EOF while parsing attribute");
863 if(token.type != T_STRING_LITERAL) {
864 parse_error_expected("while parsing assembler attribute",
869 parse_string_literals();
874 goto attributes_finished;
883 static designator_t *parse_designation(void)
885 if(token.type != '[' && token.type != '.')
888 designator_t *result = NULL;
889 designator_t *last = NULL;
892 designator_t *designator;
895 designator = allocate_ast_zero(sizeof(designator[0]));
897 designator->array_access = parse_constant_expression();
901 designator = allocate_ast_zero(sizeof(designator[0]));
903 if(token.type != T_IDENTIFIER) {
904 parse_error_expected("while parsing designator",
908 designator->symbol = token.v.symbol;
916 assert(designator != NULL);
918 last->next = designator;
927 static initializer_t *initializer_from_string(array_type_t *type,
928 const string_t *const string)
930 /* TODO: check len vs. size of array type */
933 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
934 initializer->string.string = *string;
939 static initializer_t *initializer_from_wide_string(array_type_t *const type,
940 wide_string_t *const string)
942 /* TODO: check len vs. size of array type */
945 initializer_t *const initializer =
946 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
947 initializer->wide_string.string = *string;
952 static initializer_t *initializer_from_expression(type_t *type,
953 expression_t *expression)
955 /* TODO check that expression is a constant expression */
957 /* § 6.7.8.14/15 char array may be initialized by string literals */
958 type_t *const expr_type = expression->base.type;
959 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
960 array_type_t *const array_type = &type->array;
961 type_t *const element_type = skip_typeref(array_type->element_type);
963 if (element_type->kind == TYPE_ATOMIC) {
964 switch (expression->kind) {
965 case EXPR_STRING_LITERAL:
966 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
967 return initializer_from_string(array_type,
968 &expression->string.value);
971 case EXPR_WIDE_STRING_LITERAL: {
972 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
973 if (get_unqualified_type(element_type) == bare_wchar_type) {
974 return initializer_from_wide_string(array_type,
975 &expression->wide_string.value);
985 type_t *const res_type = semantic_assign(type, expression, "initializer");
986 if (res_type == NULL)
989 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
990 result->value.value = create_implicit_cast(expression, res_type);
995 static initializer_t *parse_sub_initializer(type_t *type,
996 expression_t *expression);
998 static initializer_t *parse_sub_initializer_elem(type_t *type)
1000 if(token.type == '{') {
1001 return parse_sub_initializer(type, NULL);
1004 expression_t *expression = parse_assignment_expression();
1005 return parse_sub_initializer(type, expression);
1008 static bool had_initializer_brace_warning;
1010 static void skip_designator(void)
1013 if(token.type == '.') {
1015 if(token.type == T_IDENTIFIER)
1017 } else if(token.type == '[') {
1019 parse_constant_expression();
1020 if(token.type == ']')
1028 static initializer_t *parse_sub_initializer(type_t *type,
1029 expression_t *expression)
1031 if(is_type_scalar(type)) {
1032 /* there might be extra {} hierarchies */
1033 if(token.type == '{') {
1035 if(!had_initializer_brace_warning) {
1036 warningf(HERE, "braces around scalar initializer");
1037 had_initializer_brace_warning = true;
1039 initializer_t *result = parse_sub_initializer(type, NULL);
1040 if(token.type == ',') {
1042 /* TODO: warn about excessive elements */
1048 if(expression == NULL) {
1049 expression = parse_assignment_expression();
1051 return initializer_from_expression(type, expression);
1054 /* does the expression match the currently looked at object to initialize */
1055 if(expression != NULL) {
1056 initializer_t *result = initializer_from_expression(type, expression);
1061 bool read_paren = false;
1062 if(token.type == '{') {
1067 /* descend into subtype */
1068 initializer_t *result = NULL;
1069 initializer_t **elems;
1070 if(is_type_array(type)) {
1071 if(token.type == '.') {
1073 "compound designator in initializer for array type '%T'",
1078 type_t *const element_type = skip_typeref(type->array.element_type);
1081 had_initializer_brace_warning = false;
1083 if(token.type == '{') {
1084 sub = parse_sub_initializer(element_type, NULL);
1086 if(expression == NULL) {
1087 expression = parse_assignment_expression();
1089 /* 6.7.8.14 + 15: we can have an optional {} around the string
1091 if(read_paren && (expression->kind == EXPR_STRING_LITERAL
1092 || expression->kind == EXPR_WIDE_STRING_LITERAL)) {
1093 initializer_t *result
1094 = initializer_from_expression(type, expression);
1095 if(result != NULL) {
1102 sub = parse_sub_initializer(element_type, expression);
1105 /* didn't match the subtypes -> try the parent type */
1107 assert(!read_paren);
1111 elems = NEW_ARR_F(initializer_t*, 0);
1112 ARR_APP1(initializer_t*, elems, sub);
1115 if(token.type == '}')
1118 if(token.type == '}')
1121 sub = parse_sub_initializer_elem(element_type);
1123 /* TODO error, do nicer cleanup */
1124 errorf(HERE, "member initializer didn't match");
1128 ARR_APP1(initializer_t*, elems, sub);
1131 assert(is_type_compound(type));
1132 scope_t *const scope = &type->compound.declaration->scope;
1134 if(token.type == '[') {
1136 "array designator in initializer for compound type '%T'",
1141 declaration_t *first = scope->declarations;
1144 type_t *first_type = first->type;
1145 first_type = skip_typeref(first_type);
1148 had_initializer_brace_warning = false;
1149 if(expression == NULL) {
1150 sub = parse_sub_initializer_elem(first_type);
1152 sub = parse_sub_initializer(first_type, expression);
1155 /* didn't match the subtypes -> try our parent type */
1157 assert(!read_paren);
1161 elems = NEW_ARR_F(initializer_t*, 0);
1162 ARR_APP1(initializer_t*, elems, sub);
1164 declaration_t *iter = first->next;
1165 for( ; iter != NULL; iter = iter->next) {
1166 if(iter->symbol == NULL)
1168 if(iter->namespc != NAMESPACE_NORMAL)
1171 if(token.type == '}')
1174 if(token.type == '}')
1177 type_t *iter_type = iter->type;
1178 iter_type = skip_typeref(iter_type);
1180 sub = parse_sub_initializer_elem(iter_type);
1182 /* TODO error, do nicer cleanup */
1183 errorf(HERE, "member initializer didn't match");
1187 ARR_APP1(initializer_t*, elems, sub);
1191 int len = ARR_LEN(elems);
1192 size_t elems_size = sizeof(initializer_t*) * len;
1194 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1196 init->initializer.kind = INITIALIZER_LIST;
1198 memcpy(init->initializers, elems, elems_size);
1201 result = (initializer_t*) init;
1204 if(token.type == ',')
1211 static initializer_t *parse_initializer(type_t *const orig_type)
1213 initializer_t *result;
1215 type_t *const type = skip_typeref(orig_type);
1217 if(token.type != '{') {
1218 expression_t *expression = parse_assignment_expression();
1219 initializer_t *initializer = initializer_from_expression(type, expression);
1220 if(initializer == NULL) {
1222 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1223 expression, expression->base.type, orig_type);
1228 if(is_type_scalar(type)) {
1232 expression_t *expression = parse_assignment_expression();
1233 result = initializer_from_expression(type, expression);
1235 if(token.type == ',')
1241 result = parse_sub_initializer(type, NULL);
1247 static declaration_t *append_declaration(declaration_t *declaration);
1249 static declaration_t *parse_compound_type_specifier(bool is_struct)
1257 symbol_t *symbol = NULL;
1258 declaration_t *declaration = NULL;
1260 if (token.type == T___attribute__) {
1265 if(token.type == T_IDENTIFIER) {
1266 symbol = token.v.symbol;
1270 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1272 declaration = get_declaration(symbol, NAMESPACE_UNION);
1274 } else if(token.type != '{') {
1276 parse_error_expected("while parsing struct type specifier",
1277 T_IDENTIFIER, '{', 0);
1279 parse_error_expected("while parsing union type specifier",
1280 T_IDENTIFIER, '{', 0);
1286 if(declaration == NULL) {
1287 declaration = allocate_declaration_zero();
1288 declaration->namespc =
1289 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1290 declaration->source_position = token.source_position;
1291 declaration->symbol = symbol;
1292 declaration->parent_scope = scope;
1293 if (symbol != NULL) {
1294 environment_push(declaration);
1296 append_declaration(declaration);
1299 if(token.type == '{') {
1300 if(declaration->init.is_defined) {
1301 assert(symbol != NULL);
1302 errorf(HERE, "multiple definitions of '%s %Y'",
1303 is_struct ? "struct" : "union", symbol);
1304 declaration->scope.declarations = NULL;
1306 declaration->init.is_defined = true;
1308 parse_compound_type_entries(declaration);
1315 static void parse_enum_entries(type_t *const enum_type)
1319 if(token.type == '}') {
1321 errorf(HERE, "empty enum not allowed");
1326 if(token.type != T_IDENTIFIER) {
1327 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1332 declaration_t *const entry = allocate_declaration_zero();
1333 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1334 entry->type = enum_type;
1335 entry->symbol = token.v.symbol;
1336 entry->source_position = token.source_position;
1339 if(token.type == '=') {
1341 entry->init.enum_value = parse_constant_expression();
1346 record_declaration(entry);
1348 if(token.type != ',')
1351 } while(token.type != '}');
1356 static type_t *parse_enum_specifier(void)
1360 declaration_t *declaration;
1363 if(token.type == T_IDENTIFIER) {
1364 symbol = token.v.symbol;
1367 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1368 } else if(token.type != '{') {
1369 parse_error_expected("while parsing enum type specifier",
1370 T_IDENTIFIER, '{', 0);
1377 if(declaration == NULL) {
1378 declaration = allocate_declaration_zero();
1379 declaration->namespc = NAMESPACE_ENUM;
1380 declaration->source_position = token.source_position;
1381 declaration->symbol = symbol;
1382 declaration->parent_scope = scope;
1385 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1386 type->enumt.declaration = declaration;
1388 if(token.type == '{') {
1389 if(declaration->init.is_defined) {
1390 errorf(HERE, "multiple definitions of enum %Y", symbol);
1392 if (symbol != NULL) {
1393 environment_push(declaration);
1395 append_declaration(declaration);
1396 declaration->init.is_defined = 1;
1398 parse_enum_entries(type);
1406 * if a symbol is a typedef to another type, return true
1408 static bool is_typedef_symbol(symbol_t *symbol)
1410 const declaration_t *const declaration =
1411 get_declaration(symbol, NAMESPACE_NORMAL);
1413 declaration != NULL &&
1414 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1417 static type_t *parse_typeof(void)
1425 expression_t *expression = NULL;
1428 switch(token.type) {
1429 case T___extension__:
1430 /* this can be a prefix to a typename or an expression */
1431 /* we simply eat it now. */
1434 } while(token.type == T___extension__);
1438 if(is_typedef_symbol(token.v.symbol)) {
1439 type = parse_typename();
1441 expression = parse_expression();
1442 type = expression->base.type;
1447 type = parse_typename();
1451 expression = parse_expression();
1452 type = expression->base.type;
1458 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1459 typeof_type->typeoft.expression = expression;
1460 typeof_type->typeoft.typeof_type = type;
1466 SPECIFIER_SIGNED = 1 << 0,
1467 SPECIFIER_UNSIGNED = 1 << 1,
1468 SPECIFIER_LONG = 1 << 2,
1469 SPECIFIER_INT = 1 << 3,
1470 SPECIFIER_DOUBLE = 1 << 4,
1471 SPECIFIER_CHAR = 1 << 5,
1472 SPECIFIER_SHORT = 1 << 6,
1473 SPECIFIER_LONG_LONG = 1 << 7,
1474 SPECIFIER_FLOAT = 1 << 8,
1475 SPECIFIER_BOOL = 1 << 9,
1476 SPECIFIER_VOID = 1 << 10,
1477 #ifdef PROVIDE_COMPLEX
1478 SPECIFIER_COMPLEX = 1 << 11,
1479 SPECIFIER_IMAGINARY = 1 << 12,
1483 static type_t *create_builtin_type(symbol_t *const symbol,
1484 type_t *const real_type)
1486 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
1487 type->builtin.symbol = symbol;
1488 type->builtin.real_type = real_type;
1490 type_t *result = typehash_insert(type);
1491 if (type != result) {
1498 static type_t *get_typedef_type(symbol_t *symbol)
1500 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1501 if(declaration == NULL
1502 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1505 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
1506 type->typedeft.declaration = declaration;
1511 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1513 type_t *type = NULL;
1514 unsigned type_qualifiers = 0;
1515 unsigned type_specifiers = 0;
1518 specifiers->source_position = token.source_position;
1521 switch(token.type) {
1524 #define MATCH_STORAGE_CLASS(token, class) \
1526 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1527 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1529 specifiers->storage_class = class; \
1533 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1534 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1535 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1536 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1537 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1540 switch (specifiers->storage_class) {
1541 case STORAGE_CLASS_NONE:
1542 specifiers->storage_class = STORAGE_CLASS_THREAD;
1545 case STORAGE_CLASS_EXTERN:
1546 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1549 case STORAGE_CLASS_STATIC:
1550 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1554 errorf(HERE, "multiple storage classes in declaration specifiers");
1560 /* type qualifiers */
1561 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1563 type_qualifiers |= qualifier; \
1567 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1568 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1569 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1571 case T___extension__:
1576 /* type specifiers */
1577 #define MATCH_SPECIFIER(token, specifier, name) \
1580 if(type_specifiers & specifier) { \
1581 errorf(HERE, "multiple " name " type specifiers given"); \
1583 type_specifiers |= specifier; \
1587 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1588 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1589 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1590 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1591 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1592 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1593 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1594 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1595 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1596 #ifdef PROVIDE_COMPLEX
1597 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1598 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1601 /* only in microsoft mode */
1602 specifiers->decl_modifiers |= DM_FORCEINLINE;
1606 specifiers->is_inline = true;
1611 if(type_specifiers & SPECIFIER_LONG_LONG) {
1612 errorf(HERE, "multiple type specifiers given");
1613 } else if(type_specifiers & SPECIFIER_LONG) {
1614 type_specifiers |= SPECIFIER_LONG_LONG;
1616 type_specifiers |= SPECIFIER_LONG;
1621 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
1623 type->compound.declaration = parse_compound_type_specifier(true);
1627 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
1629 type->compound.declaration = parse_compound_type_specifier(false);
1633 type = parse_enum_specifier();
1636 type = parse_typeof();
1638 case T___builtin_va_list:
1639 type = duplicate_type(type_valist);
1643 case T___attribute__:
1647 case T_IDENTIFIER: {
1648 /* only parse identifier if we haven't found a type yet */
1649 if(type != NULL || type_specifiers != 0)
1650 goto finish_specifiers;
1652 type_t *typedef_type = get_typedef_type(token.v.symbol);
1654 if(typedef_type == NULL)
1655 goto finish_specifiers;
1658 type = typedef_type;
1662 /* function specifier */
1664 goto finish_specifiers;
1671 atomic_type_kind_t atomic_type;
1673 /* match valid basic types */
1674 switch(type_specifiers) {
1675 case SPECIFIER_VOID:
1676 atomic_type = ATOMIC_TYPE_VOID;
1678 case SPECIFIER_CHAR:
1679 atomic_type = ATOMIC_TYPE_CHAR;
1681 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1682 atomic_type = ATOMIC_TYPE_SCHAR;
1684 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1685 atomic_type = ATOMIC_TYPE_UCHAR;
1687 case SPECIFIER_SHORT:
1688 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1689 case SPECIFIER_SHORT | SPECIFIER_INT:
1690 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1691 atomic_type = ATOMIC_TYPE_SHORT;
1693 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1694 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1695 atomic_type = ATOMIC_TYPE_USHORT;
1698 case SPECIFIER_SIGNED:
1699 case SPECIFIER_SIGNED | SPECIFIER_INT:
1700 atomic_type = ATOMIC_TYPE_INT;
1702 case SPECIFIER_UNSIGNED:
1703 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1704 atomic_type = ATOMIC_TYPE_UINT;
1706 case SPECIFIER_LONG:
1707 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1708 case SPECIFIER_LONG | SPECIFIER_INT:
1709 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1710 atomic_type = ATOMIC_TYPE_LONG;
1712 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1713 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1714 atomic_type = ATOMIC_TYPE_ULONG;
1716 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1717 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1718 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1719 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1721 atomic_type = ATOMIC_TYPE_LONGLONG;
1723 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1724 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1726 atomic_type = ATOMIC_TYPE_ULONGLONG;
1728 case SPECIFIER_FLOAT:
1729 atomic_type = ATOMIC_TYPE_FLOAT;
1731 case SPECIFIER_DOUBLE:
1732 atomic_type = ATOMIC_TYPE_DOUBLE;
1734 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1735 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1737 case SPECIFIER_BOOL:
1738 atomic_type = ATOMIC_TYPE_BOOL;
1740 #ifdef PROVIDE_COMPLEX
1741 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1742 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1744 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1745 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1747 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1748 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1750 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1751 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1753 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1754 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1756 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1757 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1761 /* invalid specifier combination, give an error message */
1762 if(type_specifiers == 0) {
1763 if (! strict_mode) {
1764 if (warning.implicit_int) {
1765 warningf(HERE, "no type specifiers in declaration, using 'int'");
1767 atomic_type = ATOMIC_TYPE_INT;
1770 errorf(HERE, "no type specifiers given in declaration");
1772 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1773 (type_specifiers & SPECIFIER_UNSIGNED)) {
1774 errorf(HERE, "signed and unsigned specifiers gives");
1775 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1776 errorf(HERE, "only integer types can be signed or unsigned");
1778 errorf(HERE, "multiple datatypes in declaration");
1780 atomic_type = ATOMIC_TYPE_INVALID;
1783 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
1784 type->atomic.akind = atomic_type;
1787 if(type_specifiers != 0) {
1788 errorf(HERE, "multiple datatypes in declaration");
1792 type->base.qualifiers = type_qualifiers;
1794 type_t *result = typehash_insert(type);
1795 if(newtype && result != type) {
1799 specifiers->type = result;
1802 static type_qualifiers_t parse_type_qualifiers(void)
1804 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1807 switch(token.type) {
1808 /* type qualifiers */
1809 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1810 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1811 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1814 return type_qualifiers;
1819 static declaration_t *parse_identifier_list(void)
1821 declaration_t *declarations = NULL;
1822 declaration_t *last_declaration = NULL;
1824 declaration_t *const declaration = allocate_declaration_zero();
1825 declaration->type = NULL; /* a K&R parameter list has no types, yet */
1826 declaration->source_position = token.source_position;
1827 declaration->symbol = token.v.symbol;
1830 if(last_declaration != NULL) {
1831 last_declaration->next = declaration;
1833 declarations = declaration;
1835 last_declaration = declaration;
1837 if(token.type != ',')
1840 } while(token.type == T_IDENTIFIER);
1842 return declarations;
1845 static void semantic_parameter(declaration_t *declaration)
1847 /* TODO: improve error messages */
1849 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1850 errorf(HERE, "typedef not allowed in parameter list");
1851 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1852 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1853 errorf(HERE, "parameter may only have none or register storage class");
1856 type_t *const orig_type = declaration->type;
1857 type_t * type = skip_typeref(orig_type);
1859 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1860 * into a pointer. § 6.7.5.3 (7) */
1861 if (is_type_array(type)) {
1862 type_t *const element_type = type->array.element_type;
1864 type = make_pointer_type(element_type, type->base.qualifiers);
1866 declaration->type = type;
1869 if(is_type_incomplete(type)) {
1870 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
1871 orig_type, declaration->symbol);
1875 static declaration_t *parse_parameter(void)
1877 declaration_specifiers_t specifiers;
1878 memset(&specifiers, 0, sizeof(specifiers));
1880 parse_declaration_specifiers(&specifiers);
1882 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1884 semantic_parameter(declaration);
1889 static declaration_t *parse_parameters(function_type_t *type)
1891 if(token.type == T_IDENTIFIER) {
1892 symbol_t *symbol = token.v.symbol;
1893 if(!is_typedef_symbol(symbol)) {
1894 type->kr_style_parameters = true;
1895 return parse_identifier_list();
1899 if(token.type == ')') {
1900 type->unspecified_parameters = 1;
1903 if(token.type == T_void && look_ahead(1)->type == ')') {
1908 declaration_t *declarations = NULL;
1909 declaration_t *declaration;
1910 declaration_t *last_declaration = NULL;
1911 function_parameter_t *parameter;
1912 function_parameter_t *last_parameter = NULL;
1915 switch(token.type) {
1919 return declarations;
1922 case T___extension__:
1924 declaration = parse_parameter();
1926 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1927 memset(parameter, 0, sizeof(parameter[0]));
1928 parameter->type = declaration->type;
1930 if(last_parameter != NULL) {
1931 last_declaration->next = declaration;
1932 last_parameter->next = parameter;
1934 type->parameters = parameter;
1935 declarations = declaration;
1937 last_parameter = parameter;
1938 last_declaration = declaration;
1942 return declarations;
1944 if(token.type != ',')
1945 return declarations;
1955 } construct_type_kind_t;
1957 typedef struct construct_type_t construct_type_t;
1958 struct construct_type_t {
1959 construct_type_kind_t kind;
1960 construct_type_t *next;
1963 typedef struct parsed_pointer_t parsed_pointer_t;
1964 struct parsed_pointer_t {
1965 construct_type_t construct_type;
1966 type_qualifiers_t type_qualifiers;
1969 typedef struct construct_function_type_t construct_function_type_t;
1970 struct construct_function_type_t {
1971 construct_type_t construct_type;
1972 type_t *function_type;
1975 typedef struct parsed_array_t parsed_array_t;
1976 struct parsed_array_t {
1977 construct_type_t construct_type;
1978 type_qualifiers_t type_qualifiers;
1984 typedef struct construct_base_type_t construct_base_type_t;
1985 struct construct_base_type_t {
1986 construct_type_t construct_type;
1990 static construct_type_t *parse_pointer_declarator(void)
1994 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1995 memset(pointer, 0, sizeof(pointer[0]));
1996 pointer->construct_type.kind = CONSTRUCT_POINTER;
1997 pointer->type_qualifiers = parse_type_qualifiers();
1999 return (construct_type_t*) pointer;
2002 static construct_type_t *parse_array_declarator(void)
2006 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2007 memset(array, 0, sizeof(array[0]));
2008 array->construct_type.kind = CONSTRUCT_ARRAY;
2010 if(token.type == T_static) {
2011 array->is_static = true;
2015 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2016 if(type_qualifiers != 0) {
2017 if(token.type == T_static) {
2018 array->is_static = true;
2022 array->type_qualifiers = type_qualifiers;
2024 if(token.type == '*' && look_ahead(1)->type == ']') {
2025 array->is_variable = true;
2027 } else if(token.type != ']') {
2028 array->size = parse_assignment_expression();
2033 return (construct_type_t*) array;
2036 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2040 type_t *type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2042 declaration_t *parameters = parse_parameters(&type->function);
2043 if(declaration != NULL) {
2044 declaration->scope.declarations = parameters;
2047 construct_function_type_t *construct_function_type =
2048 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2049 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2050 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2051 construct_function_type->function_type = type;
2055 return (construct_type_t*) construct_function_type;
2058 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2059 bool may_be_abstract)
2061 /* construct a single linked list of construct_type_t's which describe
2062 * how to construct the final declarator type */
2063 construct_type_t *first = NULL;
2064 construct_type_t *last = NULL;
2067 while(token.type == '*') {
2068 construct_type_t *type = parse_pointer_declarator();
2079 /* TODO: find out if this is correct */
2082 construct_type_t *inner_types = NULL;
2084 switch(token.type) {
2086 if(declaration == NULL) {
2087 errorf(HERE, "no identifier expected in typename");
2089 declaration->symbol = token.v.symbol;
2090 declaration->source_position = token.source_position;
2096 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2102 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2103 /* avoid a loop in the outermost scope, because eat_statement doesn't
2105 if(token.type == '}' && current_function == NULL) {
2113 construct_type_t *p = last;
2116 construct_type_t *type;
2117 switch(token.type) {
2119 type = parse_function_declarator(declaration);
2122 type = parse_array_declarator();
2125 goto declarator_finished;
2128 /* insert in the middle of the list (behind p) */
2130 type->next = p->next;
2141 declarator_finished:
2144 /* append inner_types at the end of the list, we don't to set last anymore
2145 * as it's not needed anymore */
2147 assert(first == NULL);
2148 first = inner_types;
2150 last->next = inner_types;
2156 static type_t *construct_declarator_type(construct_type_t *construct_list,
2159 construct_type_t *iter = construct_list;
2160 for( ; iter != NULL; iter = iter->next) {
2161 switch(iter->kind) {
2162 case CONSTRUCT_INVALID:
2163 panic("invalid type construction found");
2164 case CONSTRUCT_FUNCTION: {
2165 construct_function_type_t *construct_function_type
2166 = (construct_function_type_t*) iter;
2168 type_t *function_type = construct_function_type->function_type;
2170 function_type->function.return_type = type;
2172 type_t *skipped_return_type = skip_typeref(type);
2173 if (is_type_function(skipped_return_type)) {
2174 errorf(HERE, "function returning function is not allowed");
2175 type = type_error_type;
2176 } else if (is_type_array(skipped_return_type)) {
2177 errorf(HERE, "function returning array is not allowed");
2178 type = type_error_type;
2180 type = function_type;
2185 case CONSTRUCT_POINTER: {
2186 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2187 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2188 pointer_type->pointer.points_to = type;
2189 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2191 type = pointer_type;
2195 case CONSTRUCT_ARRAY: {
2196 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2197 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2199 array_type->base.qualifiers = parsed_array->type_qualifiers;
2200 array_type->array.element_type = type;
2201 array_type->array.is_static = parsed_array->is_static;
2202 array_type->array.is_variable = parsed_array->is_variable;
2203 array_type->array.size = parsed_array->size;
2205 type_t *skipped_type = skip_typeref(type);
2206 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2207 errorf(HERE, "array of void is not allowed");
2208 type = type_error_type;
2216 type_t *hashed_type = typehash_insert(type);
2217 if(hashed_type != type) {
2218 /* the function type was constructed earlier freeing it here will
2219 * destroy other types... */
2220 if(iter->kind != CONSTRUCT_FUNCTION) {
2230 static declaration_t *parse_declarator(
2231 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2233 declaration_t *const declaration = allocate_declaration_zero();
2234 declaration->storage_class = specifiers->storage_class;
2235 declaration->modifiers = specifiers->decl_modifiers;
2236 declaration->is_inline = specifiers->is_inline;
2238 construct_type_t *construct_type
2239 = parse_inner_declarator(declaration, may_be_abstract);
2240 type_t *const type = specifiers->type;
2241 declaration->type = construct_declarator_type(construct_type, type);
2243 if(construct_type != NULL) {
2244 obstack_free(&temp_obst, construct_type);
2250 static type_t *parse_abstract_declarator(type_t *base_type)
2252 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2254 type_t *result = construct_declarator_type(construct_type, base_type);
2255 if(construct_type != NULL) {
2256 obstack_free(&temp_obst, construct_type);
2262 static declaration_t *append_declaration(declaration_t* const declaration)
2264 if (last_declaration != NULL) {
2265 last_declaration->next = declaration;
2267 scope->declarations = declaration;
2269 last_declaration = declaration;
2274 * Check if the declaration of main is suspicious. main should be a
2275 * function with external linkage, returning int, taking either zero
2276 * arguments, two, or three arguments of appropriate types, ie.
2278 * int main([ int argc, char **argv [, char **env ] ]).
2280 * @param decl the declaration to check
2281 * @param type the function type of the declaration
2283 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2285 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2286 warningf(decl->source_position, "'main' is normally a non-static function");
2288 if (skip_typeref(func_type->return_type) != type_int) {
2289 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2291 const function_parameter_t *parm = func_type->parameters;
2293 type_t *const first_type = parm->type;
2294 if (!types_compatible(skip_typeref(first_type), type_int)) {
2295 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2299 type_t *const second_type = parm->type;
2300 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2301 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2305 type_t *const third_type = parm->type;
2306 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2307 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2311 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2315 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2321 * Check if a symbol is the equal to "main".
2323 static bool is_sym_main(const symbol_t *const sym)
2325 return strcmp(sym->string, "main") == 0;
2328 static declaration_t *internal_record_declaration(
2329 declaration_t *const declaration,
2330 const bool is_function_definition)
2332 const symbol_t *const symbol = declaration->symbol;
2333 const namespace_t namespc = (namespace_t)declaration->namespc;
2335 type_t *const orig_type = declaration->type;
2336 type_t *const type = skip_typeref(orig_type);
2337 if (is_type_function(type) &&
2338 type->function.unspecified_parameters &&
2339 warning.strict_prototypes) {
2340 warningf(declaration->source_position,
2341 "function declaration '%#T' is not a prototype",
2342 orig_type, declaration->symbol);
2345 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2346 check_type_of_main(declaration, &type->function);
2349 assert(declaration->symbol != NULL);
2350 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2352 assert(declaration != previous_declaration);
2353 if (previous_declaration != NULL) {
2354 if (previous_declaration->parent_scope == scope) {
2355 /* can happen for K&R style declarations */
2356 if(previous_declaration->type == NULL) {
2357 previous_declaration->type = declaration->type;
2360 const type_t *prev_type = skip_typeref(previous_declaration->type);
2361 if (!types_compatible(type, prev_type)) {
2362 errorf(declaration->source_position,
2363 "declaration '%#T' is incompatible with "
2364 "previous declaration '%#T'",
2365 orig_type, symbol, previous_declaration->type, symbol);
2366 errorf(previous_declaration->source_position,
2367 "previous declaration of '%Y' was here", symbol);
2369 unsigned old_storage_class
2370 = previous_declaration->storage_class;
2371 unsigned new_storage_class = declaration->storage_class;
2373 if(is_type_incomplete(prev_type)) {
2374 previous_declaration->type = type;
2378 /* pretend no storage class means extern for function
2379 * declarations (except if the previous declaration is neither
2380 * none nor extern) */
2381 if (is_type_function(type)) {
2382 switch (old_storage_class) {
2383 case STORAGE_CLASS_NONE:
2384 old_storage_class = STORAGE_CLASS_EXTERN;
2386 case STORAGE_CLASS_EXTERN:
2387 if (is_function_definition) {
2388 if (warning.missing_prototypes &&
2389 prev_type->function.unspecified_parameters &&
2390 !is_sym_main(symbol)) {
2391 warningf(declaration->source_position,
2392 "no previous prototype for '%#T'",
2395 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2396 new_storage_class = STORAGE_CLASS_EXTERN;
2404 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2405 new_storage_class == STORAGE_CLASS_EXTERN) {
2406 warn_redundant_declaration:
2407 if (warning.redundant_decls) {
2408 warningf(declaration->source_position,
2409 "redundant declaration for '%Y'", symbol);
2410 warningf(previous_declaration->source_position,
2411 "previous declaration of '%Y' was here",
2414 } else if (current_function == NULL) {
2415 if (old_storage_class != STORAGE_CLASS_STATIC &&
2416 new_storage_class == STORAGE_CLASS_STATIC) {
2417 errorf(declaration->source_position,
2418 "static declaration of '%Y' follows non-static declaration",
2420 errorf(previous_declaration->source_position,
2421 "previous declaration of '%Y' was here", symbol);
2423 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2424 goto warn_redundant_declaration;
2426 if (new_storage_class == STORAGE_CLASS_NONE) {
2427 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2431 if (old_storage_class == new_storage_class) {
2432 errorf(declaration->source_position,
2433 "redeclaration of '%Y'", symbol);
2435 errorf(declaration->source_position,
2436 "redeclaration of '%Y' with different linkage",
2439 errorf(previous_declaration->source_position,
2440 "previous declaration of '%Y' was here", symbol);
2443 return previous_declaration;
2445 } else if (is_function_definition) {
2446 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2447 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2448 warningf(declaration->source_position,
2449 "no previous prototype for '%#T'", orig_type, symbol);
2450 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2451 warningf(declaration->source_position,
2452 "no previous declaration for '%#T'", orig_type,
2456 } else if (warning.missing_declarations &&
2457 scope == global_scope &&
2458 !is_type_function(type) && (
2459 declaration->storage_class == STORAGE_CLASS_NONE ||
2460 declaration->storage_class == STORAGE_CLASS_THREAD
2462 warningf(declaration->source_position,
2463 "no previous declaration for '%#T'", orig_type, symbol);
2466 assert(declaration->parent_scope == NULL);
2467 assert(scope != NULL);
2469 declaration->parent_scope = scope;
2471 environment_push(declaration);
2472 return append_declaration(declaration);
2475 static declaration_t *record_declaration(declaration_t *declaration)
2477 return internal_record_declaration(declaration, false);
2480 static declaration_t *record_function_definition(declaration_t *declaration)
2482 return internal_record_declaration(declaration, true);
2485 static void parser_error_multiple_definition(declaration_t *declaration,
2486 const source_position_t source_position)
2488 errorf(source_position, "multiple definition of symbol '%Y'",
2489 declaration->symbol);
2490 errorf(declaration->source_position,
2491 "this is the location of the previous definition.");
2494 static bool is_declaration_specifier(const token_t *token,
2495 bool only_type_specifiers)
2497 switch(token->type) {
2501 return is_typedef_symbol(token->v.symbol);
2503 case T___extension__:
2506 return !only_type_specifiers;
2513 static void parse_init_declarator_rest(declaration_t *declaration)
2517 type_t *orig_type = declaration->type;
2518 type_t *type = type = skip_typeref(orig_type);
2520 if(declaration->init.initializer != NULL) {
2521 parser_error_multiple_definition(declaration, token.source_position);
2524 initializer_t *initializer = parse_initializer(type);
2526 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2527 * the array type size */
2528 if(is_type_array(type) && initializer != NULL) {
2529 array_type_t *array_type = &type->array;
2531 if(array_type->size == NULL) {
2532 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2534 cnst->base.type = type_size_t;
2536 switch (initializer->kind) {
2537 case INITIALIZER_LIST: {
2538 cnst->conste.v.int_value = initializer->list.len;
2542 case INITIALIZER_STRING: {
2543 cnst->conste.v.int_value = initializer->string.string.size;
2547 case INITIALIZER_WIDE_STRING: {
2548 cnst->conste.v.int_value = initializer->wide_string.string.size;
2553 panic("invalid initializer type");
2556 array_type->size = cnst;
2557 array_type->has_implicit_size = true;
2561 if(is_type_function(type)) {
2562 errorf(declaration->source_position,
2563 "initializers not allowed for function types at declator '%Y' (type '%T')",
2564 declaration->symbol, orig_type);
2566 declaration->init.initializer = initializer;
2570 /* parse rest of a declaration without any declarator */
2571 static void parse_anonymous_declaration_rest(
2572 const declaration_specifiers_t *specifiers,
2573 parsed_declaration_func finished_declaration)
2577 declaration_t *const declaration = allocate_declaration_zero();
2578 declaration->type = specifiers->type;
2579 declaration->storage_class = specifiers->storage_class;
2580 declaration->source_position = specifiers->source_position;
2582 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2583 warningf(declaration->source_position, "useless storage class in empty declaration");
2586 type_t *type = declaration->type;
2587 switch (type->kind) {
2588 case TYPE_COMPOUND_STRUCT:
2589 case TYPE_COMPOUND_UNION: {
2590 if (type->compound.declaration->symbol == NULL) {
2591 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2600 warningf(declaration->source_position, "empty declaration");
2604 finished_declaration(declaration);
2607 static void parse_declaration_rest(declaration_t *ndeclaration,
2608 const declaration_specifiers_t *specifiers,
2609 parsed_declaration_func finished_declaration)
2612 declaration_t *declaration = finished_declaration(ndeclaration);
2614 type_t *orig_type = declaration->type;
2615 type_t *type = skip_typeref(orig_type);
2617 if (type->kind != TYPE_FUNCTION &&
2618 declaration->is_inline &&
2619 is_type_valid(type)) {
2620 warningf(declaration->source_position,
2621 "variable '%Y' declared 'inline'\n", declaration->symbol);
2624 if(token.type == '=') {
2625 parse_init_declarator_rest(declaration);
2628 if(token.type != ',')
2632 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2637 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2639 symbol_t *symbol = declaration->symbol;
2640 if(symbol == NULL) {
2641 errorf(HERE, "anonymous declaration not valid as function parameter");
2644 namespace_t namespc = (namespace_t) declaration->namespc;
2645 if(namespc != NAMESPACE_NORMAL) {
2646 return record_declaration(declaration);
2649 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2650 if(previous_declaration == NULL ||
2651 previous_declaration->parent_scope != scope) {
2652 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2657 if(previous_declaration->type == NULL) {
2658 previous_declaration->type = declaration->type;
2659 previous_declaration->storage_class = declaration->storage_class;
2660 previous_declaration->parent_scope = scope;
2661 return previous_declaration;
2663 return record_declaration(declaration);
2667 static void parse_declaration(parsed_declaration_func finished_declaration)
2669 declaration_specifiers_t specifiers;
2670 memset(&specifiers, 0, sizeof(specifiers));
2671 parse_declaration_specifiers(&specifiers);
2673 if(token.type == ';') {
2674 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2676 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2677 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2681 static void parse_kr_declaration_list(declaration_t *declaration)
2683 type_t *type = skip_typeref(declaration->type);
2684 if(!is_type_function(type))
2687 if(!type->function.kr_style_parameters)
2690 /* push function parameters */
2691 int top = environment_top();
2692 scope_t *last_scope = scope;
2693 set_scope(&declaration->scope);
2695 declaration_t *parameter = declaration->scope.declarations;
2696 for( ; parameter != NULL; parameter = parameter->next) {
2697 assert(parameter->parent_scope == NULL);
2698 parameter->parent_scope = scope;
2699 environment_push(parameter);
2702 /* parse declaration list */
2703 while(is_declaration_specifier(&token, false)) {
2704 parse_declaration(finished_kr_declaration);
2707 /* pop function parameters */
2708 assert(scope == &declaration->scope);
2709 set_scope(last_scope);
2710 environment_pop_to(top);
2712 /* update function type */
2713 type_t *new_type = duplicate_type(type);
2714 new_type->function.kr_style_parameters = false;
2716 function_parameter_t *parameters = NULL;
2717 function_parameter_t *last_parameter = NULL;
2719 declaration_t *parameter_declaration = declaration->scope.declarations;
2720 for( ; parameter_declaration != NULL;
2721 parameter_declaration = parameter_declaration->next) {
2722 type_t *parameter_type = parameter_declaration->type;
2723 if(parameter_type == NULL) {
2725 errorf(HERE, "no type specified for function parameter '%Y'",
2726 parameter_declaration->symbol);
2728 if (warning.implicit_int) {
2729 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
2730 parameter_declaration->symbol);
2732 parameter_type = type_int;
2733 parameter_declaration->type = parameter_type;
2737 semantic_parameter(parameter_declaration);
2738 parameter_type = parameter_declaration->type;
2740 function_parameter_t *function_parameter
2741 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2742 memset(function_parameter, 0, sizeof(function_parameter[0]));
2744 function_parameter->type = parameter_type;
2745 if(last_parameter != NULL) {
2746 last_parameter->next = function_parameter;
2748 parameters = function_parameter;
2750 last_parameter = function_parameter;
2752 new_type->function.parameters = parameters;
2754 type = typehash_insert(new_type);
2755 if(type != new_type) {
2756 obstack_free(type_obst, new_type);
2759 declaration->type = type;
2762 static bool first_err = true;
2765 * When called with first_err set, prints the name of the current function,
2768 static void print_in_function(void) {
2771 diagnosticf("%s: In function '%Y':\n",
2772 current_function->source_position.input_name,
2773 current_function->symbol);
2778 * Check if all labels are defined in the current function.
2779 * Check if all labels are used in the current function.
2781 static void check_labels(void)
2783 for (const goto_statement_t *goto_statement = goto_first;
2784 goto_statement != NULL;
2785 goto_statement = goto_statement->next) {
2786 declaration_t *label = goto_statement->label;
2789 if (label->source_position.input_name == NULL) {
2790 print_in_function();
2791 errorf(goto_statement->base.source_position,
2792 "label '%Y' used but not defined", label->symbol);
2795 goto_first = goto_last = NULL;
2797 if (warning.unused_label) {
2798 for (const label_statement_t *label_statement = label_first;
2799 label_statement != NULL;
2800 label_statement = label_statement->next) {
2801 const declaration_t *label = label_statement->label;
2803 if (! label->used) {
2804 print_in_function();
2805 warningf(label_statement->base.source_position,
2806 "label '%Y' defined but not used", label->symbol);
2810 label_first = label_last = NULL;
2814 * Check declarations of current_function for unused entities.
2816 static void check_declarations(void)
2818 if (warning.unused_parameter) {
2819 const scope_t *scope = ¤t_function->scope;
2821 const declaration_t *parameter = scope->declarations;
2822 for (; parameter != NULL; parameter = parameter->next) {
2823 if (! parameter->used) {
2824 print_in_function();
2825 warningf(parameter->source_position,
2826 "unused parameter '%Y'", parameter->symbol);
2830 if (warning.unused_variable) {
2834 static void parse_external_declaration(void)
2836 /* function-definitions and declarations both start with declaration
2838 declaration_specifiers_t specifiers;
2839 memset(&specifiers, 0, sizeof(specifiers));
2840 parse_declaration_specifiers(&specifiers);
2842 /* must be a declaration */
2843 if(token.type == ';') {
2844 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2848 /* declarator is common to both function-definitions and declarations */
2849 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2851 /* must be a declaration */
2852 if(token.type == ',' || token.type == '=' || token.type == ';') {
2853 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2857 /* must be a function definition */
2858 parse_kr_declaration_list(ndeclaration);
2860 if(token.type != '{') {
2861 parse_error_expected("while parsing function definition", '{', 0);
2866 type_t *type = ndeclaration->type;
2868 /* note that we don't skip typerefs: the standard doesn't allow them here
2869 * (so we can't use is_type_function here) */
2870 if(type->kind != TYPE_FUNCTION) {
2871 if (is_type_valid(type)) {
2872 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2873 type, ndeclaration->symbol);
2879 /* § 6.7.5.3 (14) a function definition with () means no
2880 * parameters (and not unspecified parameters) */
2881 if(type->function.unspecified_parameters) {
2882 type_t *duplicate = duplicate_type(type);
2883 duplicate->function.unspecified_parameters = false;
2885 type = typehash_insert(duplicate);
2886 if(type != duplicate) {
2887 obstack_free(type_obst, duplicate);
2889 ndeclaration->type = type;
2892 declaration_t *const declaration = record_function_definition(ndeclaration);
2893 if(ndeclaration != declaration) {
2894 declaration->scope = ndeclaration->scope;
2896 type = skip_typeref(declaration->type);
2898 /* push function parameters and switch scope */
2899 int top = environment_top();
2900 scope_t *last_scope = scope;
2901 set_scope(&declaration->scope);
2903 declaration_t *parameter = declaration->scope.declarations;
2904 for( ; parameter != NULL; parameter = parameter->next) {
2905 if(parameter->parent_scope == &ndeclaration->scope) {
2906 parameter->parent_scope = scope;
2908 assert(parameter->parent_scope == NULL
2909 || parameter->parent_scope == scope);
2910 parameter->parent_scope = scope;
2911 environment_push(parameter);
2914 if(declaration->init.statement != NULL) {
2915 parser_error_multiple_definition(declaration, token.source_position);
2917 goto end_of_parse_external_declaration;
2919 /* parse function body */
2920 int label_stack_top = label_top();
2921 declaration_t *old_current_function = current_function;
2922 current_function = declaration;
2924 declaration->init.statement = parse_compound_statement();
2927 check_declarations();
2929 assert(current_function == declaration);
2930 current_function = old_current_function;
2931 label_pop_to(label_stack_top);
2934 end_of_parse_external_declaration:
2935 assert(scope == &declaration->scope);
2936 set_scope(last_scope);
2937 environment_pop_to(top);
2940 static type_t *make_bitfield_type(type_t *base, expression_t *size,
2941 source_position_t source_position)
2943 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
2944 type->bitfield.base = base;
2945 type->bitfield.size = size;
2950 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
2953 declaration_t *iter = compound_declaration->scope.declarations;
2954 for( ; iter != NULL; iter = iter->next) {
2955 if(iter->namespc != NAMESPACE_NORMAL)
2958 if(iter->symbol == NULL) {
2959 type_t *type = skip_typeref(iter->type);
2960 if(is_type_compound(type)) {
2961 declaration_t *result
2962 = find_compound_entry(type->compound.declaration, symbol);
2969 if(iter->symbol == symbol) {
2977 static void parse_compound_declarators(declaration_t *struct_declaration,
2978 const declaration_specifiers_t *specifiers)
2980 declaration_t *last_declaration = struct_declaration->scope.declarations;
2981 if(last_declaration != NULL) {
2982 while(last_declaration->next != NULL) {
2983 last_declaration = last_declaration->next;
2988 declaration_t *declaration;
2990 if(token.type == ':') {
2991 source_position_t source_position = HERE;
2994 type_t *base_type = specifiers->type;
2995 expression_t *size = parse_constant_expression();
2997 if(!is_type_integer(skip_typeref(base_type))) {
2998 errorf(HERE, "bitfield base type '%T' is not an integer type",
3002 type_t *type = make_bitfield_type(base_type, size, source_position);
3004 declaration = allocate_declaration_zero();
3005 declaration->namespc = NAMESPACE_NORMAL;
3006 declaration->storage_class = STORAGE_CLASS_NONE;
3007 declaration->source_position = source_position;
3008 declaration->modifiers = specifiers->decl_modifiers;
3009 declaration->type = type;
3011 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3013 type_t *orig_type = declaration->type;
3014 type_t *type = skip_typeref(orig_type);
3016 if(token.type == ':') {
3017 source_position_t source_position = HERE;
3019 expression_t *size = parse_constant_expression();
3021 if(!is_type_integer(type)) {
3022 errorf(HERE, "bitfield base type '%T' is not an "
3023 "integer type", orig_type);
3026 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3027 declaration->type = bitfield_type;
3029 /* TODO we ignore arrays for now... what is missing is a check
3030 * that they're at the end of the struct */
3031 if(is_type_incomplete(type) && !is_type_array(type)) {
3033 "compound member '%Y' has incomplete type '%T'",
3034 declaration->symbol, orig_type);
3035 } else if(is_type_function(type)) {
3036 errorf(HERE, "compound member '%Y' must not have function "
3037 "type '%T'", declaration->symbol, orig_type);
3042 /* make sure we don't define a symbol multiple times */
3043 symbol_t *symbol = declaration->symbol;
3044 if(symbol != NULL) {
3045 declaration_t *prev_decl
3046 = find_compound_entry(struct_declaration, symbol);
3048 if(prev_decl != NULL) {
3049 assert(prev_decl->symbol == symbol);
3050 errorf(declaration->source_position,
3051 "multiple declarations of symbol '%Y'", symbol);
3052 errorf(prev_decl->source_position,
3053 "previous declaration of '%Y' was here", symbol);
3057 /* append declaration */
3058 if(last_declaration != NULL) {
3059 last_declaration->next = declaration;
3061 struct_declaration->scope.declarations = declaration;
3063 last_declaration = declaration;
3065 if(token.type != ',')
3072 static void parse_compound_type_entries(declaration_t *compound_declaration)
3076 while(token.type != '}' && token.type != T_EOF) {
3077 declaration_specifiers_t specifiers;
3078 memset(&specifiers, 0, sizeof(specifiers));
3079 parse_declaration_specifiers(&specifiers);
3081 parse_compound_declarators(compound_declaration, &specifiers);
3083 if(token.type == T_EOF) {
3084 errorf(HERE, "EOF while parsing struct");
3089 static type_t *parse_typename(void)
3091 declaration_specifiers_t specifiers;
3092 memset(&specifiers, 0, sizeof(specifiers));
3093 parse_declaration_specifiers(&specifiers);
3094 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
3095 /* TODO: improve error message, user does probably not know what a
3096 * storage class is...
3098 errorf(HERE, "typename may not have a storage class");
3101 type_t *result = parse_abstract_declarator(specifiers.type);
3109 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3110 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3111 expression_t *left);
3113 typedef struct expression_parser_function_t expression_parser_function_t;
3114 struct expression_parser_function_t {
3115 unsigned precedence;
3116 parse_expression_function parser;
3117 unsigned infix_precedence;
3118 parse_expression_infix_function infix_parser;
3121 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3124 * Creates a new invalid expression.
3126 static expression_t *create_invalid_expression(void)
3128 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3129 expression->base.source_position = token.source_position;
3134 * Prints an error message if an expression was expected but not read
3136 static expression_t *expected_expression_error(void)
3138 /* skip the error message if the error token was read */
3139 if (token.type != T_ERROR) {
3140 errorf(HERE, "expected expression, got token '%K'", &token);
3144 return create_invalid_expression();
3148 * Parse a string constant.
3150 static expression_t *parse_string_const(void)
3153 if (token.type == T_STRING_LITERAL) {
3154 string_t res = token.v.string;
3156 while (token.type == T_STRING_LITERAL) {
3157 res = concat_strings(&res, &token.v.string);
3160 if (token.type != T_WIDE_STRING_LITERAL) {
3161 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3162 cnst->base.type = type_char_ptr;
3163 cnst->string.value = res;
3167 wres = concat_string_wide_string(&res, &token.v.wide_string);
3169 wres = token.v.wide_string;
3174 switch (token.type) {
3175 case T_WIDE_STRING_LITERAL:
3176 wres = concat_wide_strings(&wres, &token.v.wide_string);
3179 case T_STRING_LITERAL:
3180 wres = concat_wide_string_string(&wres, &token.v.string);
3184 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3185 cnst->base.type = type_wchar_t_ptr;
3186 cnst->wide_string.value = wres;
3195 * Parse an integer constant.
3197 static expression_t *parse_int_const(void)
3199 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3200 cnst->base.source_position = HERE;
3201 cnst->base.type = token.datatype;
3202 cnst->conste.v.int_value = token.v.intvalue;
3210 * Parse a character constant.
3212 static expression_t *parse_char_const(void)
3214 expression_t *cnst = allocate_expression_zero(EXPR_CHAR_CONST);
3215 cnst->base.source_position = HERE;
3216 cnst->base.type = token.datatype;
3217 cnst->conste.v.chars.begin = token.v.string.begin;
3218 cnst->conste.v.chars.size = token.v.string.size;
3220 if (cnst->conste.v.chars.size != 1) {
3221 if (warning.multichar && (c_mode & _GNUC)) {
3223 warningf(HERE, "multi-character character constant");
3225 errorf(HERE, "more than 1 characters in character constant");
3234 * Parse a float constant.
3236 static expression_t *parse_float_const(void)
3238 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3239 cnst->base.type = token.datatype;
3240 cnst->conste.v.float_value = token.v.floatvalue;
3247 static declaration_t *create_implicit_function(symbol_t *symbol,
3248 const source_position_t source_position)
3250 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
3251 ntype->function.return_type = type_int;
3252 ntype->function.unspecified_parameters = true;
3254 type_t *type = typehash_insert(ntype);
3259 declaration_t *const declaration = allocate_declaration_zero();
3260 declaration->storage_class = STORAGE_CLASS_EXTERN;
3261 declaration->type = type;
3262 declaration->symbol = symbol;
3263 declaration->source_position = source_position;
3264 declaration->parent_scope = global_scope;
3266 scope_t *old_scope = scope;
3267 set_scope(global_scope);
3269 environment_push(declaration);
3270 /* prepends the declaration to the global declarations list */
3271 declaration->next = scope->declarations;
3272 scope->declarations = declaration;
3274 assert(scope == global_scope);
3275 set_scope(old_scope);
3281 * Creates a return_type (func)(argument_type) function type if not
3284 * @param return_type the return type
3285 * @param argument_type the argument type
3287 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3289 function_parameter_t *parameter
3290 = obstack_alloc(type_obst, sizeof(parameter[0]));
3291 memset(parameter, 0, sizeof(parameter[0]));
3292 parameter->type = argument_type;
3294 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
3295 type->function.return_type = return_type;
3296 type->function.parameters = parameter;
3298 type_t *result = typehash_insert(type);
3299 if(result != type) {
3307 * Creates a function type for some function like builtins.
3309 * @param symbol the symbol describing the builtin
3311 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3313 switch(symbol->ID) {
3314 case T___builtin_alloca:
3315 return make_function_1_type(type_void_ptr, type_size_t);
3316 case T___builtin_nan:
3317 return make_function_1_type(type_double, type_char_ptr);
3318 case T___builtin_nanf:
3319 return make_function_1_type(type_float, type_char_ptr);
3320 case T___builtin_nand:
3321 return make_function_1_type(type_long_double, type_char_ptr);
3322 case T___builtin_va_end:
3323 return make_function_1_type(type_void, type_valist);
3325 panic("not implemented builtin symbol found");
3330 * Performs automatic type cast as described in § 6.3.2.1.
3332 * @param orig_type the original type
3334 static type_t *automatic_type_conversion(type_t *orig_type)
3336 type_t *type = skip_typeref(orig_type);
3337 if(is_type_array(type)) {
3338 array_type_t *array_type = &type->array;
3339 type_t *element_type = array_type->element_type;
3340 unsigned qualifiers = array_type->type.qualifiers;
3342 return make_pointer_type(element_type, qualifiers);
3345 if(is_type_function(type)) {
3346 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3353 * reverts the automatic casts of array to pointer types and function
3354 * to function-pointer types as defined § 6.3.2.1
3356 type_t *revert_automatic_type_conversion(const expression_t *expression)
3358 switch (expression->kind) {
3359 case EXPR_REFERENCE: return expression->reference.declaration->type;
3360 case EXPR_SELECT: return expression->select.compound_entry->type;
3362 case EXPR_UNARY_DEREFERENCE: {
3363 const expression_t *const value = expression->unary.value;
3364 type_t *const type = skip_typeref(value->base.type);
3365 assert(is_type_pointer(type));
3366 return type->pointer.points_to;
3369 case EXPR_BUILTIN_SYMBOL:
3370 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3372 case EXPR_ARRAY_ACCESS: {
3373 const expression_t *array_ref = expression->array_access.array_ref;
3374 type_t *type_left = skip_typeref(array_ref->base.type);
3375 if (!is_type_valid(type_left))
3377 assert(is_type_pointer(type_left));
3378 return type_left->pointer.points_to;
3384 return expression->base.type;
3387 static expression_t *parse_reference(void)
3389 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3391 reference_expression_t *ref = &expression->reference;
3392 ref->symbol = token.v.symbol;
3394 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3396 source_position_t source_position = token.source_position;
3399 if(declaration == NULL) {
3400 if (! strict_mode && token.type == '(') {
3401 /* an implicitly defined function */
3402 if (warning.implicit_function_declaration) {
3403 warningf(HERE, "implicit declaration of function '%Y'",
3407 declaration = create_implicit_function(ref->symbol,
3410 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3415 type_t *type = declaration->type;
3417 /* we always do the auto-type conversions; the & and sizeof parser contains
3418 * code to revert this! */
3419 type = automatic_type_conversion(type);
3421 ref->declaration = declaration;
3422 ref->base.type = type;
3424 /* this declaration is used */
3425 declaration->used = true;
3430 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3434 /* TODO check if explicit cast is allowed and issue warnings/errors */
3437 static expression_t *parse_cast(void)
3439 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3441 cast->base.source_position = token.source_position;
3443 type_t *type = parse_typename();
3446 expression_t *value = parse_sub_expression(20);
3448 check_cast_allowed(value, type);
3450 cast->base.type = type;
3451 cast->unary.value = value;
3456 static expression_t *parse_statement_expression(void)
3458 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3460 statement_t *statement = parse_compound_statement();
3461 expression->statement.statement = statement;
3462 expression->base.source_position = statement->base.source_position;
3464 /* find last statement and use its type */
3465 type_t *type = type_void;
3466 const statement_t *stmt = statement->compound.statements;
3468 while (stmt->base.next != NULL)
3469 stmt = stmt->base.next;
3471 if (stmt->kind == STATEMENT_EXPRESSION) {
3472 type = stmt->expression.expression->base.type;
3475 warningf(expression->base.source_position, "empty statement expression ({})");
3477 expression->base.type = type;
3484 static expression_t *parse_brace_expression(void)
3488 switch(token.type) {
3490 /* gcc extension: a statement expression */
3491 return parse_statement_expression();
3495 return parse_cast();
3497 if(is_typedef_symbol(token.v.symbol)) {
3498 return parse_cast();
3502 expression_t *result = parse_expression();
3508 static expression_t *parse_function_keyword(void)
3513 if (current_function == NULL) {
3514 errorf(HERE, "'__func__' used outside of a function");
3517 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
3518 expression->base.type = type_char_ptr;
3523 static expression_t *parse_pretty_function_keyword(void)
3525 eat(T___PRETTY_FUNCTION__);
3528 if (current_function == NULL) {
3529 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3532 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
3533 expression->base.type = type_char_ptr;
3538 static designator_t *parse_designator(void)
3540 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3542 if(token.type != T_IDENTIFIER) {
3543 parse_error_expected("while parsing member designator",
3548 result->symbol = token.v.symbol;
3551 designator_t *last_designator = result;
3553 if(token.type == '.') {
3555 if(token.type != T_IDENTIFIER) {
3556 parse_error_expected("while parsing member designator",
3561 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3562 designator->symbol = token.v.symbol;
3565 last_designator->next = designator;
3566 last_designator = designator;
3569 if(token.type == '[') {
3571 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3572 designator->array_access = parse_expression();
3573 if(designator->array_access == NULL) {
3579 last_designator->next = designator;
3580 last_designator = designator;
3589 static expression_t *parse_offsetof(void)
3591 eat(T___builtin_offsetof);
3593 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3594 expression->base.type = type_size_t;
3597 expression->offsetofe.type = parse_typename();
3599 expression->offsetofe.designator = parse_designator();
3605 static expression_t *parse_va_start(void)
3607 eat(T___builtin_va_start);
3609 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3612 expression->va_starte.ap = parse_assignment_expression();
3614 expression_t *const expr = parse_assignment_expression();
3615 if (expr->kind == EXPR_REFERENCE) {
3616 declaration_t *const decl = expr->reference.declaration;
3618 return create_invalid_expression();
3619 if (decl->parent_scope == ¤t_function->scope &&
3620 decl->next == NULL) {
3621 expression->va_starte.parameter = decl;
3626 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3628 return create_invalid_expression();
3631 static expression_t *parse_va_arg(void)
3633 eat(T___builtin_va_arg);
3635 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3638 expression->va_arge.ap = parse_assignment_expression();
3640 expression->base.type = parse_typename();
3646 static expression_t *parse_builtin_symbol(void)
3648 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3650 symbol_t *symbol = token.v.symbol;
3652 expression->builtin_symbol.symbol = symbol;
3655 type_t *type = get_builtin_symbol_type(symbol);
3656 type = automatic_type_conversion(type);
3658 expression->base.type = type;
3662 static expression_t *parse_builtin_constant(void)
3664 eat(T___builtin_constant_p);
3666 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3669 expression->builtin_constant.value = parse_assignment_expression();
3671 expression->base.type = type_int;
3676 static expression_t *parse_builtin_prefetch(void)
3678 eat(T___builtin_prefetch);
3680 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3683 expression->builtin_prefetch.adr = parse_assignment_expression();
3684 if (token.type == ',') {
3686 expression->builtin_prefetch.rw = parse_assignment_expression();
3688 if (token.type == ',') {
3690 expression->builtin_prefetch.locality = parse_assignment_expression();
3693 expression->base.type = type_void;
3698 static expression_t *parse_compare_builtin(void)
3700 expression_t *expression;
3702 switch(token.type) {
3703 case T___builtin_isgreater:
3704 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3706 case T___builtin_isgreaterequal:
3707 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3709 case T___builtin_isless:
3710 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3712 case T___builtin_islessequal:
3713 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3715 case T___builtin_islessgreater:
3716 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3718 case T___builtin_isunordered:
3719 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3722 panic("invalid compare builtin found");
3725 expression->base.source_position = HERE;
3729 expression->binary.left = parse_assignment_expression();
3731 expression->binary.right = parse_assignment_expression();
3734 type_t *const orig_type_left = expression->binary.left->base.type;
3735 type_t *const orig_type_right = expression->binary.right->base.type;
3737 type_t *const type_left = skip_typeref(orig_type_left);
3738 type_t *const type_right = skip_typeref(orig_type_right);
3739 if(!is_type_float(type_left) && !is_type_float(type_right)) {
3740 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3741 type_error_incompatible("invalid operands in comparison",
3742 expression->base.source_position, orig_type_left, orig_type_right);
3745 semantic_comparison(&expression->binary);
3751 static expression_t *parse_builtin_expect(void)
3753 eat(T___builtin_expect);
3755 expression_t *expression
3756 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3759 expression->binary.left = parse_assignment_expression();
3761 expression->binary.right = parse_constant_expression();
3764 expression->base.type = expression->binary.left->base.type;
3769 static expression_t *parse_assume(void) {
3772 expression_t *expression
3773 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3776 expression->unary.value = parse_assignment_expression();
3779 expression->base.type = type_void;
3783 static expression_t *parse_primary_expression(void)
3785 switch(token.type) {
3787 return parse_int_const();
3789 return parse_char_const();
3790 case T_FLOATINGPOINT:
3791 return parse_float_const();
3792 case T_STRING_LITERAL:
3793 case T_WIDE_STRING_LITERAL:
3794 return parse_string_const();
3796 return parse_reference();
3797 case T___FUNCTION__:
3799 return parse_function_keyword();
3800 case T___PRETTY_FUNCTION__:
3801 return parse_pretty_function_keyword();
3802 case T___builtin_offsetof:
3803 return parse_offsetof();
3804 case T___builtin_va_start:
3805 return parse_va_start();
3806 case T___builtin_va_arg:
3807 return parse_va_arg();
3808 case T___builtin_expect:
3809 return parse_builtin_expect();
3810 case T___builtin_alloca:
3811 case T___builtin_nan:
3812 case T___builtin_nand:
3813 case T___builtin_nanf:
3814 case T___builtin_va_end:
3815 return parse_builtin_symbol();
3816 case T___builtin_isgreater:
3817 case T___builtin_isgreaterequal:
3818 case T___builtin_isless:
3819 case T___builtin_islessequal:
3820 case T___builtin_islessgreater:
3821 case T___builtin_isunordered:
3822 return parse_compare_builtin();
3823 case T___builtin_constant_p:
3824 return parse_builtin_constant();
3825 case T___builtin_prefetch:
3826 return parse_builtin_prefetch();
3828 return parse_assume();
3831 return parse_brace_expression();
3834 errorf(HERE, "unexpected token %K", &token);
3837 return create_invalid_expression();
3841 * Check if the expression has the character type and issue a warning then.
3843 static void check_for_char_index_type(const expression_t *expression) {
3844 type_t *const type = expression->base.type;
3845 const type_t *const base_type = skip_typeref(type);
3847 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
3848 warning.char_subscripts) {
3849 warningf(expression->base.source_position,
3850 "array subscript has type '%T'", type);
3854 static expression_t *parse_array_expression(unsigned precedence,
3861 expression_t *inside = parse_expression();
3863 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
3865 array_access_expression_t *array_access = &expression->array_access;
3867 type_t *const orig_type_left = left->base.type;
3868 type_t *const orig_type_inside = inside->base.type;
3870 type_t *const type_left = skip_typeref(orig_type_left);
3871 type_t *const type_inside = skip_typeref(orig_type_inside);
3873 type_t *return_type;
3874 if (is_type_pointer(type_left)) {
3875 return_type = type_left->pointer.points_to;
3876 array_access->array_ref = left;
3877 array_access->index = inside;
3878 check_for_char_index_type(inside);
3879 } else if (is_type_pointer(type_inside)) {
3880 return_type = type_inside->pointer.points_to;
3881 array_access->array_ref = inside;
3882 array_access->index = left;
3883 array_access->flipped = true;
3884 check_for_char_index_type(left);
3886 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3888 "array access on object with non-pointer types '%T', '%T'",
3889 orig_type_left, orig_type_inside);
3891 return_type = type_error_type;
3892 array_access->array_ref = create_invalid_expression();
3895 if(token.type != ']') {
3896 parse_error_expected("Problem while parsing array access", ']', 0);
3901 return_type = automatic_type_conversion(return_type);
3902 expression->base.type = return_type;
3907 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
3909 expression_t *tp_expression = allocate_expression_zero(kind);
3910 tp_expression->base.type = type_size_t;
3912 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3914 tp_expression->typeprop.type = parse_typename();
3917 expression_t *expression = parse_sub_expression(precedence);
3918 expression->base.type = revert_automatic_type_conversion(expression);
3920 tp_expression->typeprop.type = expression->base.type;
3921 tp_expression->typeprop.tp_expression = expression;
3924 return tp_expression;
3927 static expression_t *parse_sizeof(unsigned precedence)
3930 return parse_typeprop(EXPR_SIZEOF, precedence);
3933 static expression_t *parse_alignof(unsigned precedence)
3936 return parse_typeprop(EXPR_SIZEOF, precedence);
3939 static expression_t *parse_select_expression(unsigned precedence,
3940 expression_t *compound)
3943 assert(token.type == '.' || token.type == T_MINUSGREATER);
3945 bool is_pointer = (token.type == T_MINUSGREATER);
3948 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3949 select->select.compound = compound;
3951 if(token.type != T_IDENTIFIER) {
3952 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3955 symbol_t *symbol = token.v.symbol;
3956 select->select.symbol = symbol;
3959 type_t *const orig_type = compound->base.type;
3960 type_t *const type = skip_typeref(orig_type);
3962 type_t *type_left = type;
3964 if (!is_type_pointer(type)) {
3965 if (is_type_valid(type)) {
3966 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3968 return create_invalid_expression();
3970 type_left = type->pointer.points_to;
3972 type_left = skip_typeref(type_left);
3974 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3975 type_left->kind != TYPE_COMPOUND_UNION) {
3976 if (is_type_valid(type_left)) {
3977 errorf(HERE, "request for member '%Y' in something not a struct or "
3978 "union, but '%T'", symbol, type_left);
3980 return create_invalid_expression();
3983 declaration_t *const declaration = type_left->compound.declaration;
3985 if(!declaration->init.is_defined) {
3986 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3988 return create_invalid_expression();
3991 declaration_t *iter = find_compound_entry(declaration, symbol);
3993 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3994 return create_invalid_expression();
3997 /* we always do the auto-type conversions; the & and sizeof parser contains
3998 * code to revert this! */
3999 type_t *expression_type = automatic_type_conversion(iter->type);
4001 select->select.compound_entry = iter;
4002 select->base.type = expression_type;
4004 if(expression_type->kind == TYPE_BITFIELD) {
4005 expression_t *extract
4006 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
4007 extract->unary.value = select;
4008 extract->base.type = expression_type->bitfield.base;
4017 * Parse a call expression, ie. expression '( ... )'.
4019 * @param expression the function address
4021 static expression_t *parse_call_expression(unsigned precedence,
4022 expression_t *expression)
4025 expression_t *result = allocate_expression_zero(EXPR_CALL);
4027 call_expression_t *call = &result->call;
4028 call->function = expression;
4030 type_t *const orig_type = expression->base.type;
4031 type_t *const type = skip_typeref(orig_type);
4033 function_type_t *function_type = NULL;
4034 if (is_type_pointer(type)) {
4035 type_t *const to_type = skip_typeref(type->pointer.points_to);
4037 if (is_type_function(to_type)) {
4038 function_type = &to_type->function;
4039 call->base.type = function_type->return_type;
4043 if (function_type == NULL && is_type_valid(type)) {
4044 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
4047 /* parse arguments */
4050 if(token.type != ')') {
4051 call_argument_t *last_argument = NULL;
4054 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
4056 argument->expression = parse_assignment_expression();
4057 if(last_argument == NULL) {
4058 call->arguments = argument;
4060 last_argument->next = argument;
4062 last_argument = argument;
4064 if(token.type != ',')
4071 if(function_type != NULL) {
4072 function_parameter_t *parameter = function_type->parameters;
4073 call_argument_t *argument = call->arguments;
4074 for( ; parameter != NULL && argument != NULL;
4075 parameter = parameter->next, argument = argument->next) {
4076 type_t *expected_type = parameter->type;
4077 /* TODO report scope in error messages */
4078 expression_t *const arg_expr = argument->expression;
4079 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
4080 if (res_type == NULL) {
4081 /* TODO improve error message */
4082 errorf(arg_expr->base.source_position,
4083 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
4084 arg_expr, arg_expr->base.type, expected_type);
4086 argument->expression = create_implicit_cast(argument->expression, expected_type);
4089 /* too few parameters */
4090 if(parameter != NULL) {
4091 errorf(HERE, "too few arguments to function '%E'", expression);
4092 } else if(argument != NULL) {
4093 /* too many parameters */
4094 if(!function_type->variadic
4095 && !function_type->unspecified_parameters) {
4096 errorf(HERE, "too many arguments to function '%E'", expression);
4098 /* do default promotion */
4099 for( ; argument != NULL; argument = argument->next) {
4100 type_t *type = argument->expression->base.type;
4102 type = skip_typeref(type);
4103 if(is_type_integer(type)) {
4104 type = promote_integer(type);
4105 } else if(type == type_float) {
4109 argument->expression
4110 = create_implicit_cast(argument->expression, type);
4113 check_format(&result->call);
4116 check_format(&result->call);
4123 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4125 static bool same_compound_type(const type_t *type1, const type_t *type2)
4128 is_type_compound(type1) &&
4129 type1->kind == type2->kind &&
4130 type1->compound.declaration == type2->compound.declaration;
4134 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4136 * @param expression the conditional expression
4138 static expression_t *parse_conditional_expression(unsigned precedence,
4139 expression_t *expression)
4143 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4145 conditional_expression_t *conditional = &result->conditional;
4146 conditional->condition = expression;
4149 type_t *const condition_type_orig = expression->base.type;
4150 type_t *const condition_type = skip_typeref(condition_type_orig);
4151 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4152 type_error("expected a scalar type in conditional condition",
4153 expression->base.source_position, condition_type_orig);
4156 expression_t *true_expression = parse_expression();
4158 expression_t *false_expression = parse_sub_expression(precedence);
4160 conditional->true_expression = true_expression;
4161 conditional->false_expression = false_expression;
4163 type_t *const orig_true_type = true_expression->base.type;
4164 type_t *const orig_false_type = false_expression->base.type;
4165 type_t *const true_type = skip_typeref(orig_true_type);
4166 type_t *const false_type = skip_typeref(orig_false_type);
4169 type_t *result_type;
4170 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4171 result_type = semantic_arithmetic(true_type, false_type);
4173 true_expression = create_implicit_cast(true_expression, result_type);
4174 false_expression = create_implicit_cast(false_expression, result_type);
4176 conditional->true_expression = true_expression;
4177 conditional->false_expression = false_expression;
4178 conditional->base.type = result_type;
4179 } else if (same_compound_type(true_type, false_type) || (
4180 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4181 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4183 /* just take 1 of the 2 types */
4184 result_type = true_type;
4185 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4186 && pointers_compatible(true_type, false_type)) {
4188 result_type = true_type;
4191 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4192 type_error_incompatible("while parsing conditional",
4193 expression->base.source_position, true_type,
4196 result_type = type_error_type;
4199 conditional->base.type = result_type;
4204 * Parse an extension expression.
4206 static expression_t *parse_extension(unsigned precedence)
4208 eat(T___extension__);
4210 /* TODO enable extensions */
4211 expression_t *expression = parse_sub_expression(precedence);
4212 /* TODO disable extensions */
4216 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4218 eat(T___builtin_classify_type);
4220 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4221 result->base.type = type_int;
4224 expression_t *expression = parse_sub_expression(precedence);
4226 result->classify_type.type_expression = expression;
4231 static void semantic_incdec(unary_expression_t *expression)
4233 type_t *const orig_type = expression->value->base.type;
4234 type_t *const type = skip_typeref(orig_type);
4235 /* TODO !is_type_real && !is_type_pointer */
4236 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4237 if (is_type_valid(type)) {
4238 /* TODO: improve error message */
4239 errorf(HERE, "operation needs an arithmetic or pointer type");
4244 expression->base.type = orig_type;
4247 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4249 type_t *const orig_type = expression->value->base.type;
4250 type_t *const type = skip_typeref(orig_type);
4251 if(!is_type_arithmetic(type)) {
4252 if (is_type_valid(type)) {
4253 /* TODO: improve error message */
4254 errorf(HERE, "operation needs an arithmetic type");
4259 expression->base.type = orig_type;
4262 static void semantic_unexpr_scalar(unary_expression_t *expression)
4264 type_t *const orig_type = expression->value->base.type;
4265 type_t *const type = skip_typeref(orig_type);
4266 if (!is_type_scalar(type)) {
4267 if (is_type_valid(type)) {
4268 errorf(HERE, "operand of ! must be of scalar type");
4273 expression->base.type = orig_type;
4276 static void semantic_unexpr_integer(unary_expression_t *expression)
4278 type_t *const orig_type = expression->value->base.type;
4279 type_t *const type = skip_typeref(orig_type);
4280 if (!is_type_integer(type)) {
4281 if (is_type_valid(type)) {
4282 errorf(HERE, "operand of ~ must be of integer type");
4287 expression->base.type = orig_type;
4290 static void semantic_dereference(unary_expression_t *expression)
4292 type_t *const orig_type = expression->value->base.type;
4293 type_t *const type = skip_typeref(orig_type);
4294 if(!is_type_pointer(type)) {
4295 if (is_type_valid(type)) {
4296 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4301 type_t *result_type = type->pointer.points_to;
4302 result_type = automatic_type_conversion(result_type);
4303 expression->base.type = result_type;
4307 * Check the semantic of the address taken expression.
4309 static void semantic_take_addr(unary_expression_t *expression)
4311 expression_t *value = expression->value;
4312 value->base.type = revert_automatic_type_conversion(value);
4314 type_t *orig_type = value->base.type;
4315 if(!is_type_valid(orig_type))
4318 if(value->kind == EXPR_REFERENCE) {
4319 declaration_t *const declaration = value->reference.declaration;
4320 if(declaration != NULL) {
4321 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4322 errorf(expression->base.source_position,
4323 "address of register variable '%Y' requested",
4324 declaration->symbol);
4326 declaration->address_taken = 1;
4330 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4333 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4334 static expression_t *parse_##unexpression_type(unsigned precedence) \
4338 expression_t *unary_expression \
4339 = allocate_expression_zero(unexpression_type); \
4340 unary_expression->base.source_position = HERE; \
4341 unary_expression->unary.value = parse_sub_expression(precedence); \
4343 sfunc(&unary_expression->unary); \
4345 return unary_expression; \
4348 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4349 semantic_unexpr_arithmetic)
4350 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4351 semantic_unexpr_arithmetic)
4352 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4353 semantic_unexpr_scalar)
4354 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4355 semantic_dereference)
4356 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4358 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4359 semantic_unexpr_integer)
4360 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4362 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4365 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4367 static expression_t *parse_##unexpression_type(unsigned precedence, \
4368 expression_t *left) \
4370 (void) precedence; \
4373 expression_t *unary_expression \
4374 = allocate_expression_zero(unexpression_type); \
4375 unary_expression->unary.value = left; \
4377 sfunc(&unary_expression->unary); \
4379 return unary_expression; \
4382 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4383 EXPR_UNARY_POSTFIX_INCREMENT,
4385 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4386 EXPR_UNARY_POSTFIX_DECREMENT,
4389 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4391 /* TODO: handle complex + imaginary types */
4393 /* § 6.3.1.8 Usual arithmetic conversions */
4394 if(type_left == type_long_double || type_right == type_long_double) {
4395 return type_long_double;
4396 } else if(type_left == type_double || type_right == type_double) {
4398 } else if(type_left == type_float || type_right == type_float) {
4402 type_right = promote_integer(type_right);
4403 type_left = promote_integer(type_left);
4405 if(type_left == type_right)
4408 bool signed_left = is_type_signed(type_left);
4409 bool signed_right = is_type_signed(type_right);
4410 int rank_left = get_rank(type_left);
4411 int rank_right = get_rank(type_right);
4412 if(rank_left < rank_right) {
4413 if(signed_left == signed_right || !signed_right) {
4419 if(signed_left == signed_right || !signed_left) {
4428 * Check the semantic restrictions for a binary expression.
4430 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4432 expression_t *const left = expression->left;
4433 expression_t *const right = expression->right;
4434 type_t *const orig_type_left = left->base.type;
4435 type_t *const orig_type_right = right->base.type;
4436 type_t *const type_left = skip_typeref(orig_type_left);
4437 type_t *const type_right = skip_typeref(orig_type_right);
4439 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4440 /* TODO: improve error message */
4441 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4442 errorf(HERE, "operation needs arithmetic types");
4447 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4448 expression->left = create_implicit_cast(left, arithmetic_type);
4449 expression->right = create_implicit_cast(right, arithmetic_type);
4450 expression->base.type = arithmetic_type;
4453 static void semantic_shift_op(binary_expression_t *expression)
4455 expression_t *const left = expression->left;
4456 expression_t *const right = expression->right;
4457 type_t *const orig_type_left = left->base.type;
4458 type_t *const orig_type_right = right->base.type;
4459 type_t * type_left = skip_typeref(orig_type_left);
4460 type_t * type_right = skip_typeref(orig_type_right);
4462 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4463 /* TODO: improve error message */
4464 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4465 errorf(HERE, "operation needs integer types");
4470 type_left = promote_integer(type_left);
4471 type_right = promote_integer(type_right);
4473 expression->left = create_implicit_cast(left, type_left);
4474 expression->right = create_implicit_cast(right, type_right);
4475 expression->base.type = type_left;
4478 static void semantic_add(binary_expression_t *expression)
4480 expression_t *const left = expression->left;
4481 expression_t *const right = expression->right;
4482 type_t *const orig_type_left = left->base.type;
4483 type_t *const orig_type_right = right->base.type;
4484 type_t *const type_left = skip_typeref(orig_type_left);
4485 type_t *const type_right = skip_typeref(orig_type_right);
4488 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4489 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4490 expression->left = create_implicit_cast(left, arithmetic_type);
4491 expression->right = create_implicit_cast(right, arithmetic_type);
4492 expression->base.type = arithmetic_type;
4494 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4495 expression->base.type = type_left;
4496 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4497 expression->base.type = type_right;
4498 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4499 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4503 static void semantic_sub(binary_expression_t *expression)
4505 expression_t *const left = expression->left;
4506 expression_t *const right = expression->right;
4507 type_t *const orig_type_left = left->base.type;
4508 type_t *const orig_type_right = right->base.type;
4509 type_t *const type_left = skip_typeref(orig_type_left);
4510 type_t *const type_right = skip_typeref(orig_type_right);
4513 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4514 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4515 expression->left = create_implicit_cast(left, arithmetic_type);
4516 expression->right = create_implicit_cast(right, arithmetic_type);
4517 expression->base.type = arithmetic_type;
4519 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4520 expression->base.type = type_left;
4521 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4522 if(!pointers_compatible(type_left, type_right)) {
4524 "pointers to incompatible objects to binary '-' ('%T', '%T')",
4525 orig_type_left, orig_type_right);
4527 expression->base.type = type_ptrdiff_t;
4529 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4530 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
4531 orig_type_left, orig_type_right);
4536 * Check the semantics of comparison expressions.
4538 * @param expression The expression to check.
4540 static void semantic_comparison(binary_expression_t *expression)
4542 expression_t *left = expression->left;
4543 expression_t *right = expression->right;
4544 type_t *orig_type_left = left->base.type;
4545 type_t *orig_type_right = right->base.type;
4547 type_t *type_left = skip_typeref(orig_type_left);
4548 type_t *type_right = skip_typeref(orig_type_right);
4550 /* TODO non-arithmetic types */
4551 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4552 if (warning.sign_compare &&
4553 (expression->base.kind != EXPR_BINARY_EQUAL &&
4554 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
4555 (is_type_signed(type_left) != is_type_signed(type_right))) {
4556 warningf(expression->base.source_position,
4557 "comparison between signed and unsigned");
4559 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4560 expression->left = create_implicit_cast(left, arithmetic_type);
4561 expression->right = create_implicit_cast(right, arithmetic_type);
4562 expression->base.type = arithmetic_type;
4563 if (warning.float_equal &&
4564 (expression->base.kind == EXPR_BINARY_EQUAL ||
4565 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
4566 is_type_float(arithmetic_type)) {
4567 warningf(expression->base.source_position,
4568 "comparing floating point with == or != is unsafe");
4570 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4571 /* TODO check compatibility */
4572 } else if (is_type_pointer(type_left)) {
4573 expression->right = create_implicit_cast(right, type_left);
4574 } else if (is_type_pointer(type_right)) {
4575 expression->left = create_implicit_cast(left, type_right);
4576 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4577 type_error_incompatible("invalid operands in comparison",
4578 expression->base.source_position,
4579 type_left, type_right);
4581 expression->base.type = type_int;
4584 static void semantic_arithmetic_assign(binary_expression_t *expression)
4586 expression_t *left = expression->left;
4587 expression_t *right = expression->right;
4588 type_t *orig_type_left = left->base.type;
4589 type_t *orig_type_right = right->base.type;
4591 type_t *type_left = skip_typeref(orig_type_left);
4592 type_t *type_right = skip_typeref(orig_type_right);
4594 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4595 /* TODO: improve error message */
4596 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4597 errorf(HERE, "operation needs arithmetic types");
4602 /* combined instructions are tricky. We can't create an implicit cast on
4603 * the left side, because we need the uncasted form for the store.
4604 * The ast2firm pass has to know that left_type must be right_type
4605 * for the arithmetic operation and create a cast by itself */
4606 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4607 expression->right = create_implicit_cast(right, arithmetic_type);
4608 expression->base.type = type_left;
4611 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4613 expression_t *const left = expression->left;
4614 expression_t *const right = expression->right;
4615 type_t *const orig_type_left = left->base.type;
4616 type_t *const orig_type_right = right->base.type;
4617 type_t *const type_left = skip_typeref(orig_type_left);
4618 type_t *const type_right = skip_typeref(orig_type_right);
4620 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4621 /* combined instructions are tricky. We can't create an implicit cast on
4622 * the left side, because we need the uncasted form for the store.
4623 * The ast2firm pass has to know that left_type must be right_type
4624 * for the arithmetic operation and create a cast by itself */
4625 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4626 expression->right = create_implicit_cast(right, arithmetic_type);
4627 expression->base.type = type_left;
4628 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4629 expression->base.type = type_left;
4630 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4631 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4636 * Check the semantic restrictions of a logical expression.
4638 static void semantic_logical_op(binary_expression_t *expression)
4640 expression_t *const left = expression->left;
4641 expression_t *const right = expression->right;
4642 type_t *const orig_type_left = left->base.type;
4643 type_t *const orig_type_right = right->base.type;
4644 type_t *const type_left = skip_typeref(orig_type_left);
4645 type_t *const type_right = skip_typeref(orig_type_right);
4647 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4648 /* TODO: improve error message */
4649 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4650 errorf(HERE, "operation needs scalar types");
4655 expression->base.type = type_int;
4659 * Checks if a compound type has constant fields.
4661 static bool has_const_fields(const compound_type_t *type)
4663 const scope_t *scope = &type->declaration->scope;
4664 const declaration_t *declaration = scope->declarations;
4666 for (; declaration != NULL; declaration = declaration->next) {
4667 if (declaration->namespc != NAMESPACE_NORMAL)
4670 const type_t *decl_type = skip_typeref(declaration->type);
4671 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4679 * Check the semantic restrictions of a binary assign expression.
4681 static void semantic_binexpr_assign(binary_expression_t *expression)
4683 expression_t *left = expression->left;
4684 type_t *orig_type_left = left->base.type;
4686 type_t *type_left = revert_automatic_type_conversion(left);
4687 type_left = skip_typeref(orig_type_left);
4689 /* must be a modifiable lvalue */
4690 if (is_type_array(type_left)) {
4691 errorf(HERE, "cannot assign to arrays ('%E')", left);
4694 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4695 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4699 if(is_type_incomplete(type_left)) {
4701 "left-hand side of assignment '%E' has incomplete type '%T'",
4702 left, orig_type_left);
4705 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4706 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4707 left, orig_type_left);
4711 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4713 if (res_type == NULL) {
4714 errorf(expression->base.source_position,
4715 "cannot assign to '%T' from '%T'",
4716 orig_type_left, expression->right->base.type);
4718 expression->right = create_implicit_cast(expression->right, res_type);
4721 expression->base.type = orig_type_left;
4724 static bool expression_has_effect(const expression_t *const expr)
4726 switch (expr->kind) {
4727 case EXPR_UNKNOWN: break;
4728 case EXPR_INVALID: break;
4729 case EXPR_REFERENCE: return false;
4730 case EXPR_CONST: return false;
4731 case EXPR_CHAR_CONST: return false;
4732 case EXPR_STRING_LITERAL: return false;
4733 case EXPR_WIDE_STRING_LITERAL: return false;
4735 const call_expression_t *const call = &expr->call;
4736 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
4739 switch (call->function->builtin_symbol.symbol->ID) {
4740 case T___builtin_va_end: return true;
4741 default: return false;
4744 case EXPR_CONDITIONAL: {
4745 const conditional_expression_t *const cond = &expr->conditional;
4747 expression_has_effect(cond->true_expression) &&
4748 expression_has_effect(cond->false_expression);
4750 case EXPR_SELECT: return false;
4751 case EXPR_ARRAY_ACCESS: return false;
4752 case EXPR_SIZEOF: return false;
4753 case EXPR_CLASSIFY_TYPE: return false;
4754 case EXPR_ALIGNOF: return false;
4756 case EXPR_FUNCTION: return false;
4757 case EXPR_PRETTY_FUNCTION: return false;
4758 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
4759 case EXPR_BUILTIN_CONSTANT_P: return false;
4760 case EXPR_BUILTIN_PREFETCH: return true;
4761 case EXPR_OFFSETOF: return false;
4762 case EXPR_VA_START: return true;
4763 case EXPR_VA_ARG: return true;
4764 case EXPR_STATEMENT: return true; // TODO
4766 case EXPR_UNARY_NEGATE: return false;
4767 case EXPR_UNARY_PLUS: return false;
4768 case EXPR_UNARY_BITWISE_NEGATE: return false;
4769 case EXPR_UNARY_NOT: return false;
4770 case EXPR_UNARY_DEREFERENCE: return false;
4771 case EXPR_UNARY_TAKE_ADDRESS: return false;
4772 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
4773 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
4774 case EXPR_UNARY_PREFIX_INCREMENT: return true;
4775 case EXPR_UNARY_PREFIX_DECREMENT: return true;
4776 case EXPR_UNARY_CAST:
4777 return is_type_atomic(expr->base.type, ATOMIC_TYPE_VOID);
4778 case EXPR_UNARY_CAST_IMPLICIT: return true;
4779 case EXPR_UNARY_ASSUME: return true;
4780 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
4782 case EXPR_BINARY_ADD: return false;
4783 case EXPR_BINARY_SUB: return false;
4784 case EXPR_BINARY_MUL: return false;
4785 case EXPR_BINARY_DIV: return false;
4786 case EXPR_BINARY_MOD: return false;
4787 case EXPR_BINARY_EQUAL: return false;
4788 case EXPR_BINARY_NOTEQUAL: return false;
4789 case EXPR_BINARY_LESS: return false;
4790 case EXPR_BINARY_LESSEQUAL: return false;
4791 case EXPR_BINARY_GREATER: return false;
4792 case EXPR_BINARY_GREATEREQUAL: return false;
4793 case EXPR_BINARY_BITWISE_AND: return false;
4794 case EXPR_BINARY_BITWISE_OR: return false;
4795 case EXPR_BINARY_BITWISE_XOR: return false;
4796 case EXPR_BINARY_SHIFTLEFT: return false;
4797 case EXPR_BINARY_SHIFTRIGHT: return false;
4798 case EXPR_BINARY_ASSIGN: return true;
4799 case EXPR_BINARY_MUL_ASSIGN: return true;
4800 case EXPR_BINARY_DIV_ASSIGN: return true;
4801 case EXPR_BINARY_MOD_ASSIGN: return true;
4802 case EXPR_BINARY_ADD_ASSIGN: return true;
4803 case EXPR_BINARY_SUB_ASSIGN: return true;
4804 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
4805 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
4806 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
4807 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
4808 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
4809 case EXPR_BINARY_LOGICAL_AND:
4810 case EXPR_BINARY_LOGICAL_OR:
4811 case EXPR_BINARY_COMMA:
4812 return expression_has_effect(expr->binary.right);
4814 case EXPR_BINARY_BUILTIN_EXPECT: return true;
4815 case EXPR_BINARY_ISGREATER: return false;
4816 case EXPR_BINARY_ISGREATEREQUAL: return false;
4817 case EXPR_BINARY_ISLESS: return false;
4818 case EXPR_BINARY_ISLESSEQUAL: return false;
4819 case EXPR_BINARY_ISLESSGREATER: return false;
4820 case EXPR_BINARY_ISUNORDERED: return false;
4823 panic("unexpected statement");
4826 static void semantic_comma(binary_expression_t *expression)
4828 if (warning.unused_value) {
4829 const expression_t *const left = expression->left;
4830 if (!expression_has_effect(left)) {
4831 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
4834 expression->base.type = expression->right->base.type;
4837 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4838 static expression_t *parse_##binexpression_type(unsigned precedence, \
4839 expression_t *left) \
4842 source_position_t pos = HERE; \
4844 expression_t *right = parse_sub_expression(precedence + lr); \
4846 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4847 binexpr->base.source_position = pos; \
4848 binexpr->binary.left = left; \
4849 binexpr->binary.right = right; \
4850 sfunc(&binexpr->binary); \
4855 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4856 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4857 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4858 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4859 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4860 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4861 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4862 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4863 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4865 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4866 semantic_comparison, 1)
4867 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4868 semantic_comparison, 1)
4869 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4870 semantic_comparison, 1)
4871 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4872 semantic_comparison, 1)
4874 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4875 semantic_binexpr_arithmetic, 1)
4876 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4877 semantic_binexpr_arithmetic, 1)
4878 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4879 semantic_binexpr_arithmetic, 1)
4880 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4881 semantic_logical_op, 1)
4882 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4883 semantic_logical_op, 1)
4884 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4885 semantic_shift_op, 1)
4886 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4887 semantic_shift_op, 1)
4888 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4889 semantic_arithmetic_addsubb_assign, 0)
4890 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4891 semantic_arithmetic_addsubb_assign, 0)
4892 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4893 semantic_arithmetic_assign, 0)
4894 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4895 semantic_arithmetic_assign, 0)
4896 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4897 semantic_arithmetic_assign, 0)
4898 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4899 semantic_arithmetic_assign, 0)
4900 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4901 semantic_arithmetic_assign, 0)
4902 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4903 semantic_arithmetic_assign, 0)
4904 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4905 semantic_arithmetic_assign, 0)
4906 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4907 semantic_arithmetic_assign, 0)
4909 static expression_t *parse_sub_expression(unsigned precedence)
4911 if(token.type < 0) {
4912 return expected_expression_error();
4915 expression_parser_function_t *parser
4916 = &expression_parsers[token.type];
4917 source_position_t source_position = token.source_position;
4920 if(parser->parser != NULL) {
4921 left = parser->parser(parser->precedence);
4923 left = parse_primary_expression();
4925 assert(left != NULL);
4926 left->base.source_position = source_position;
4929 if(token.type < 0) {
4930 return expected_expression_error();
4933 parser = &expression_parsers[token.type];
4934 if(parser->infix_parser == NULL)
4936 if(parser->infix_precedence < precedence)
4939 left = parser->infix_parser(parser->infix_precedence, left);
4941 assert(left != NULL);
4942 assert(left->kind != EXPR_UNKNOWN);
4943 left->base.source_position = source_position;
4950 * Parse an expression.
4952 static expression_t *parse_expression(void)
4954 return parse_sub_expression(1);
4958 * Register a parser for a prefix-like operator with given precedence.
4960 * @param parser the parser function
4961 * @param token_type the token type of the prefix token
4962 * @param precedence the precedence of the operator
4964 static void register_expression_parser(parse_expression_function parser,
4965 int token_type, unsigned precedence)
4967 expression_parser_function_t *entry = &expression_parsers[token_type];
4969 if(entry->parser != NULL) {
4970 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4971 panic("trying to register multiple expression parsers for a token");
4973 entry->parser = parser;
4974 entry->precedence = precedence;
4978 * Register a parser for an infix operator with given precedence.
4980 * @param parser the parser function
4981 * @param token_type the token type of the infix operator
4982 * @param precedence the precedence of the operator
4984 static void register_infix_parser(parse_expression_infix_function parser,
4985 int token_type, unsigned precedence)
4987 expression_parser_function_t *entry = &expression_parsers[token_type];
4989 if(entry->infix_parser != NULL) {
4990 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4991 panic("trying to register multiple infix expression parsers for a "
4994 entry->infix_parser = parser;
4995 entry->infix_precedence = precedence;
4999 * Initialize the expression parsers.
5001 static void init_expression_parsers(void)
5003 memset(&expression_parsers, 0, sizeof(expression_parsers));
5005 register_infix_parser(parse_array_expression, '[', 30);
5006 register_infix_parser(parse_call_expression, '(', 30);
5007 register_infix_parser(parse_select_expression, '.', 30);
5008 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
5009 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
5011 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
5014 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
5015 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
5016 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
5017 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
5018 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
5019 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
5020 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
5021 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
5022 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
5023 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
5024 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
5025 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
5026 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
5027 T_EXCLAMATIONMARKEQUAL, 13);
5028 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
5029 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
5030 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
5031 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
5032 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
5033 register_infix_parser(parse_conditional_expression, '?', 7);
5034 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
5035 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
5036 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
5037 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
5038 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
5039 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
5040 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
5041 T_LESSLESSEQUAL, 2);
5042 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5043 T_GREATERGREATEREQUAL, 2);
5044 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
5046 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
5048 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
5051 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
5053 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
5054 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
5055 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
5056 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
5057 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
5058 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
5059 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
5061 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
5063 register_expression_parser(parse_sizeof, T_sizeof, 25);
5064 register_expression_parser(parse_alignof, T___alignof__, 25);
5065 register_expression_parser(parse_extension, T___extension__, 25);
5066 register_expression_parser(parse_builtin_classify_type,
5067 T___builtin_classify_type, 25);
5071 * Parse a asm statement constraints specification.
5073 static asm_constraint_t *parse_asm_constraints(void)
5075 asm_constraint_t *result = NULL;
5076 asm_constraint_t *last = NULL;
5078 while(token.type == T_STRING_LITERAL || token.type == '[') {
5079 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
5080 memset(constraint, 0, sizeof(constraint[0]));
5082 if(token.type == '[') {
5084 if(token.type != T_IDENTIFIER) {
5085 parse_error_expected("while parsing asm constraint",
5089 constraint->symbol = token.v.symbol;
5094 constraint->constraints = parse_string_literals();
5096 constraint->expression = parse_expression();
5100 last->next = constraint;
5102 result = constraint;
5106 if(token.type != ',')
5115 * Parse a asm statement clobber specification.
5117 static asm_clobber_t *parse_asm_clobbers(void)
5119 asm_clobber_t *result = NULL;
5120 asm_clobber_t *last = NULL;
5122 while(token.type == T_STRING_LITERAL) {
5123 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5124 clobber->clobber = parse_string_literals();
5127 last->next = clobber;
5133 if(token.type != ',')
5142 * Parse an asm statement.
5144 static statement_t *parse_asm_statement(void)
5148 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5149 statement->base.source_position = token.source_position;
5151 asm_statement_t *asm_statement = &statement->asms;
5153 if(token.type == T_volatile) {
5155 asm_statement->is_volatile = true;
5159 asm_statement->asm_text = parse_string_literals();
5161 if(token.type != ':')
5165 asm_statement->inputs = parse_asm_constraints();
5166 if(token.type != ':')
5170 asm_statement->outputs = parse_asm_constraints();
5171 if(token.type != ':')
5175 asm_statement->clobbers = parse_asm_clobbers();
5184 * Parse a case statement.
5186 static statement_t *parse_case_statement(void)
5190 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5192 statement->base.source_position = token.source_position;
5193 statement->case_label.expression = parse_expression();
5195 if (c_mode & _GNUC) {
5196 if (token.type == T_DOTDOTDOT) {
5198 statement->case_label.end_range = parse_expression();
5204 if (! is_constant_expression(statement->case_label.expression)) {
5205 errorf(statement->base.source_position,
5206 "case label does not reduce to an integer constant");
5208 /* TODO: check if the case label is already known */
5209 if (current_switch != NULL) {
5210 /* link all cases into the switch statement */
5211 if (current_switch->last_case == NULL) {
5212 current_switch->first_case =
5213 current_switch->last_case = &statement->case_label;
5215 current_switch->last_case->next = &statement->case_label;
5218 errorf(statement->base.source_position,
5219 "case label not within a switch statement");
5222 statement->case_label.statement = parse_statement();
5228 * Finds an existing default label of a switch statement.
5230 static case_label_statement_t *
5231 find_default_label(const switch_statement_t *statement)
5233 case_label_statement_t *label = statement->first_case;
5234 for ( ; label != NULL; label = label->next) {
5235 if (label->expression == NULL)
5242 * Parse a default statement.
5244 static statement_t *parse_default_statement(void)
5248 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5250 statement->base.source_position = token.source_position;
5253 if (current_switch != NULL) {
5254 const case_label_statement_t *def_label = find_default_label(current_switch);
5255 if (def_label != NULL) {
5256 errorf(HERE, "multiple default labels in one switch");
5257 errorf(def_label->base.source_position,
5258 "this is the first default label");
5260 /* link all cases into the switch statement */
5261 if (current_switch->last_case == NULL) {
5262 current_switch->first_case =
5263 current_switch->last_case = &statement->case_label;
5265 current_switch->last_case->next = &statement->case_label;
5269 errorf(statement->base.source_position,
5270 "'default' label not within a switch statement");
5272 statement->case_label.statement = parse_statement();
5278 * Return the declaration for a given label symbol or create a new one.
5280 static declaration_t *get_label(symbol_t *symbol)
5282 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5283 assert(current_function != NULL);
5284 /* if we found a label in the same function, then we already created the
5286 if(candidate != NULL
5287 && candidate->parent_scope == ¤t_function->scope) {
5291 /* otherwise we need to create a new one */
5292 declaration_t *const declaration = allocate_declaration_zero();
5293 declaration->namespc = NAMESPACE_LABEL;
5294 declaration->symbol = symbol;
5296 label_push(declaration);
5302 * Parse a label statement.
5304 static statement_t *parse_label_statement(void)
5306 assert(token.type == T_IDENTIFIER);
5307 symbol_t *symbol = token.v.symbol;
5310 declaration_t *label = get_label(symbol);
5312 /* if source position is already set then the label is defined twice,
5313 * otherwise it was just mentioned in a goto so far */
5314 if(label->source_position.input_name != NULL) {
5315 errorf(HERE, "duplicate label '%Y'", symbol);
5316 errorf(label->source_position, "previous definition of '%Y' was here",
5319 label->source_position = token.source_position;
5322 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
5324 statement->base.source_position = token.source_position;
5325 statement->label.label = label;
5329 if(token.type == '}') {
5330 /* TODO only warn? */
5331 errorf(HERE, "label at end of compound statement");
5334 if (token.type == ';') {
5335 /* eat an empty statement here, to avoid the warning about an empty
5336 * after a label. label:; is commonly used to have a label before
5340 statement->label.statement = parse_statement();
5344 /* remember the labels's in a list for later checking */
5345 if (label_last == NULL) {
5346 label_first = &statement->label;
5348 label_last->next = &statement->label;
5350 label_last = &statement->label;
5356 * Parse an if statement.
5358 static statement_t *parse_if(void)
5362 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
5363 statement->base.source_position = token.source_position;
5366 statement->ifs.condition = parse_expression();
5369 statement->ifs.true_statement = parse_statement();
5370 if(token.type == T_else) {
5372 statement->ifs.false_statement = parse_statement();
5379 * Parse a switch statement.
5381 static statement_t *parse_switch(void)
5385 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
5386 statement->base.source_position = token.source_position;
5389 expression_t *const expr = parse_expression();
5390 type_t * type = skip_typeref(expr->base.type);
5391 if (is_type_integer(type)) {
5392 type = promote_integer(type);
5393 } else if (is_type_valid(type)) {
5394 errorf(expr->base.source_position,
5395 "switch quantity is not an integer, but '%T'", type);
5396 type = type_error_type;
5398 statement->switchs.expression = create_implicit_cast(expr, type);
5401 switch_statement_t *rem = current_switch;
5402 current_switch = &statement->switchs;
5403 statement->switchs.body = parse_statement();
5404 current_switch = rem;
5406 if (warning.switch_default
5407 && find_default_label(&statement->switchs) == NULL) {
5408 warningf(statement->base.source_position, "switch has no default case");
5414 static statement_t *parse_loop_body(statement_t *const loop)
5416 statement_t *const rem = current_loop;
5417 current_loop = loop;
5419 statement_t *const body = parse_statement();
5426 * Parse a while statement.
5428 static statement_t *parse_while(void)
5432 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
5433 statement->base.source_position = token.source_position;
5436 statement->whiles.condition = parse_expression();
5439 statement->whiles.body = parse_loop_body(statement);
5445 * Parse a do statement.
5447 static statement_t *parse_do(void)
5451 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
5453 statement->base.source_position = token.source_position;
5455 statement->do_while.body = parse_loop_body(statement);
5459 statement->do_while.condition = parse_expression();
5467 * Parse a for statement.
5469 static statement_t *parse_for(void)
5473 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
5474 statement->base.source_position = token.source_position;
5478 int top = environment_top();
5479 scope_t *last_scope = scope;
5480 set_scope(&statement->fors.scope);
5482 if(token.type != ';') {
5483 if(is_declaration_specifier(&token, false)) {
5484 parse_declaration(record_declaration);
5486 expression_t *const init = parse_expression();
5487 statement->fors.initialisation = init;
5488 if (warning.unused_value && !expression_has_effect(init)) {
5489 warningf(init->base.source_position, "initialisation of 'for'-statement has no effect");
5497 if(token.type != ';') {
5498 statement->fors.condition = parse_expression();
5501 if(token.type != ')') {
5502 expression_t *const step = parse_expression();
5503 statement->fors.step = step;
5504 if (warning.unused_value && !expression_has_effect(step)) {
5505 warningf(step->base.source_position, "step of 'for'-statement has no effect");
5509 statement->fors.body = parse_loop_body(statement);
5511 assert(scope == &statement->fors.scope);
5512 set_scope(last_scope);
5513 environment_pop_to(top);
5519 * Parse a goto statement.
5521 static statement_t *parse_goto(void)
5525 if(token.type != T_IDENTIFIER) {
5526 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5530 symbol_t *symbol = token.v.symbol;
5533 declaration_t *label = get_label(symbol);
5535 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
5536 statement->base.source_position = token.source_position;
5538 statement->gotos.label = label;
5540 /* remember the goto's in a list for later checking */
5541 if (goto_last == NULL) {
5542 goto_first = &statement->gotos;
5544 goto_last->next = &statement->gotos;
5546 goto_last = &statement->gotos;
5554 * Parse a continue statement.
5556 static statement_t *parse_continue(void)
5558 statement_t *statement;
5559 if (current_loop == NULL) {
5560 errorf(HERE, "continue statement not within loop");
5563 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5565 statement->base.source_position = token.source_position;
5575 * Parse a break statement.
5577 static statement_t *parse_break(void)
5579 statement_t *statement;
5580 if (current_switch == NULL && current_loop == NULL) {
5581 errorf(HERE, "break statement not within loop or switch");
5584 statement = allocate_statement_zero(STATEMENT_BREAK);
5586 statement->base.source_position = token.source_position;
5596 * Check if a given declaration represents a local variable.
5598 static bool is_local_var_declaration(const declaration_t *declaration) {
5599 switch ((storage_class_tag_t) declaration->storage_class) {
5600 case STORAGE_CLASS_NONE:
5601 case STORAGE_CLASS_AUTO:
5602 case STORAGE_CLASS_REGISTER: {
5603 const type_t *type = skip_typeref(declaration->type);
5604 if(is_type_function(type)) {
5616 * Check if a given declaration represents a variable.
5618 static bool is_var_declaration(const declaration_t *declaration) {
5619 switch ((storage_class_tag_t) declaration->storage_class) {
5620 case STORAGE_CLASS_NONE:
5621 case STORAGE_CLASS_EXTERN:
5622 case STORAGE_CLASS_STATIC:
5623 case STORAGE_CLASS_AUTO:
5624 case STORAGE_CLASS_REGISTER:
5625 case STORAGE_CLASS_THREAD:
5626 case STORAGE_CLASS_THREAD_EXTERN:
5627 case STORAGE_CLASS_THREAD_STATIC: {
5628 const type_t *type = skip_typeref(declaration->type);
5629 if(is_type_function(type)) {
5641 * Check if a given expression represents a local variable.
5643 static bool is_local_variable(const expression_t *expression)
5645 if (expression->base.kind != EXPR_REFERENCE) {
5648 const declaration_t *declaration = expression->reference.declaration;
5649 return is_local_var_declaration(declaration);
5653 * Check if a given expression represents a local variable and
5654 * return its declaration then, else return NULL.
5656 declaration_t *expr_is_variable(const expression_t *expression)
5658 if (expression->base.kind != EXPR_REFERENCE) {
5661 declaration_t *declaration = expression->reference.declaration;
5662 if (is_var_declaration(declaration))
5668 * Parse a return statement.
5670 static statement_t *parse_return(void)
5674 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
5675 statement->base.source_position = token.source_position;
5677 expression_t *return_value = NULL;
5678 if(token.type != ';') {
5679 return_value = parse_expression();
5683 const type_t *const func_type = current_function->type;
5684 assert(is_type_function(func_type));
5685 type_t *const return_type = skip_typeref(func_type->function.return_type);
5687 if(return_value != NULL) {
5688 type_t *return_value_type = skip_typeref(return_value->base.type);
5690 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5691 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5692 warningf(statement->base.source_position,
5693 "'return' with a value, in function returning void");
5694 return_value = NULL;
5696 type_t *const res_type = semantic_assign(return_type,
5697 return_value, "'return'");
5698 if (res_type == NULL) {
5699 errorf(statement->base.source_position,
5700 "cannot return something of type '%T' in function returning '%T'",
5701 return_value->base.type, return_type);
5703 return_value = create_implicit_cast(return_value, res_type);
5706 /* check for returning address of a local var */
5707 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5708 const expression_t *expression = return_value->unary.value;
5709 if (is_local_variable(expression)) {
5710 warningf(statement->base.source_position,
5711 "function returns address of local variable");
5715 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5716 warningf(statement->base.source_position,
5717 "'return' without value, in function returning non-void");
5720 statement->returns.value = return_value;
5726 * Parse a declaration statement.
5728 static statement_t *parse_declaration_statement(void)
5730 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5732 statement->base.source_position = token.source_position;
5734 declaration_t *before = last_declaration;
5735 parse_declaration(record_declaration);
5737 if(before == NULL) {
5738 statement->declaration.declarations_begin = scope->declarations;
5740 statement->declaration.declarations_begin = before->next;
5742 statement->declaration.declarations_end = last_declaration;
5748 * Parse an expression statement, ie. expr ';'.
5750 static statement_t *parse_expression_statement(void)
5752 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5754 statement->base.source_position = token.source_position;
5755 expression_t *const expr = parse_expression();
5756 statement->expression.expression = expr;
5758 if (warning.unused_value && !expression_has_effect(expr)) {
5759 warningf(expr->base.source_position, "statement has no effect");
5768 * Parse a statement.
5770 static statement_t *parse_statement(void)
5772 statement_t *statement = NULL;
5774 /* declaration or statement */
5775 switch(token.type) {
5777 statement = parse_asm_statement();
5781 statement = parse_case_statement();
5785 statement = parse_default_statement();
5789 statement = parse_compound_statement();
5793 statement = parse_if();
5797 statement = parse_switch();
5801 statement = parse_while();
5805 statement = parse_do();
5809 statement = parse_for();
5813 statement = parse_goto();
5817 statement = parse_continue();
5821 statement = parse_break();
5825 statement = parse_return();
5829 if (warning.empty_statement) {
5830 warningf(HERE, "statement is empty");
5837 if(look_ahead(1)->type == ':') {
5838 statement = parse_label_statement();
5842 if(is_typedef_symbol(token.v.symbol)) {
5843 statement = parse_declaration_statement();
5847 statement = parse_expression_statement();
5850 case T___extension__:
5851 /* this can be a prefix to a declaration or an expression statement */
5852 /* we simply eat it now and parse the rest with tail recursion */
5855 } while(token.type == T___extension__);
5856 statement = parse_statement();
5860 statement = parse_declaration_statement();
5864 statement = parse_expression_statement();
5868 assert(statement == NULL
5869 || statement->base.source_position.input_name != NULL);
5875 * Parse a compound statement.
5877 static statement_t *parse_compound_statement(void)
5879 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
5881 statement->base.source_position = token.source_position;
5885 int top = environment_top();
5886 scope_t *last_scope = scope;
5887 set_scope(&statement->compound.scope);
5889 statement_t *last_statement = NULL;
5891 while(token.type != '}' && token.type != T_EOF) {
5892 statement_t *sub_statement = parse_statement();
5893 if(sub_statement == NULL)
5896 if(last_statement != NULL) {
5897 last_statement->base.next = sub_statement;
5899 statement->compound.statements = sub_statement;
5902 while(sub_statement->base.next != NULL)
5903 sub_statement = sub_statement->base.next;
5905 last_statement = sub_statement;
5908 if(token.type == '}') {
5911 errorf(statement->base.source_position,
5912 "end of file while looking for closing '}'");
5915 assert(scope == &statement->compound.scope);
5916 set_scope(last_scope);
5917 environment_pop_to(top);
5923 * Initialize builtin types.
5925 static void initialize_builtin_types(void)
5927 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5928 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5929 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5930 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5931 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5932 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5933 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5934 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5936 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5937 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5938 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5939 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5943 * Check for unused global static functions and variables
5945 static void check_unused_globals(void)
5947 if (!warning.unused_function && !warning.unused_variable)
5950 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
5951 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
5954 type_t *const type = decl->type;
5956 if (is_type_function(skip_typeref(type))) {
5957 if (!warning.unused_function || decl->is_inline)
5960 s = (decl->init.statement != NULL ? "defined" : "declared");
5962 if (!warning.unused_variable)
5968 warningf(decl->source_position, "'%#T' %s but not used",
5969 type, decl->symbol, s);
5974 * Parse a translation unit.
5976 static translation_unit_t *parse_translation_unit(void)
5978 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5980 assert(global_scope == NULL);
5981 global_scope = &unit->scope;
5983 assert(scope == NULL);
5984 set_scope(&unit->scope);
5986 initialize_builtin_types();
5988 while(token.type != T_EOF) {
5989 if (token.type == ';') {
5990 /* TODO error in strict mode */
5991 warningf(HERE, "stray ';' outside of function");
5994 parse_external_declaration();
5998 assert(scope == &unit->scope);
6000 last_declaration = NULL;
6002 assert(global_scope == &unit->scope);
6003 check_unused_globals();
6004 global_scope = NULL;
6012 * @return the translation unit or NULL if errors occurred.
6014 translation_unit_t *parse(void)
6016 environment_stack = NEW_ARR_F(stack_entry_t, 0);
6017 label_stack = NEW_ARR_F(stack_entry_t, 0);
6018 diagnostic_count = 0;
6022 type_set_output(stderr);
6023 ast_set_output(stderr);
6025 lookahead_bufpos = 0;
6026 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
6029 translation_unit_t *unit = parse_translation_unit();
6031 DEL_ARR_F(environment_stack);
6032 DEL_ARR_F(label_stack);
6041 * Initialize the parser.
6043 void init_parser(void)
6045 init_expression_parsers();
6046 obstack_init(&temp_obst);
6048 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
6049 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
6053 * Terminate the parser.
6055 void exit_parser(void)
6057 obstack_free(&temp_obst, NULL);