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 context_t *global_context = NULL;
48 static context_t *context = 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 struct obstack temp_obst;
57 /** The current source position. */
58 #define HERE token.source_position
60 static type_t *type_valist;
62 static statement_t *parse_compound_statement(void);
63 static statement_t *parse_statement(void);
65 static expression_t *parse_sub_expression(unsigned precedence);
66 static expression_t *parse_expression(void);
67 static type_t *parse_typename(void);
69 static void parse_compound_type_entries(void);
70 static declaration_t *parse_declarator(
71 const declaration_specifiers_t *specifiers, bool may_be_abstract);
72 static declaration_t *record_declaration(declaration_t *declaration);
74 static void semantic_comparison(binary_expression_t *expression);
76 #define STORAGE_CLASSES \
83 #define TYPE_QUALIFIERS \
90 #ifdef PROVIDE_COMPLEX
91 #define COMPLEX_SPECIFIERS \
93 #define IMAGINARY_SPECIFIERS \
96 #define COMPLEX_SPECIFIERS
97 #define IMAGINARY_SPECIFIERS
100 #define TYPE_SPECIFIERS \
115 case T___builtin_va_list: \
119 #define DECLARATION_START \
124 #define TYPENAME_START \
129 * Allocate an AST node with given size and
130 * initialize all fields with zero.
132 static void *allocate_ast_zero(size_t size)
134 void *res = allocate_ast(size);
135 memset(res, 0, size);
139 static declaration_t *allocate_declaration_zero(void)
141 declaration_t *declaration = allocate_ast_zero(sizeof(*allocate_declaration_zero()));
142 declaration->type = type_error_type;
147 * Returns the size of a statement node.
149 * @param kind the statement kind
151 static size_t get_statement_struct_size(statement_kind_t kind)
153 static const size_t sizes[] = {
154 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
155 [STATEMENT_RETURN] = sizeof(return_statement_t),
156 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
157 [STATEMENT_IF] = sizeof(if_statement_t),
158 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
159 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
160 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
161 [STATEMENT_BREAK] = sizeof(statement_base_t),
162 [STATEMENT_GOTO] = sizeof(goto_statement_t),
163 [STATEMENT_LABEL] = sizeof(label_statement_t),
164 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
165 [STATEMENT_WHILE] = sizeof(while_statement_t),
166 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
167 [STATEMENT_FOR] = sizeof(for_statement_t),
168 [STATEMENT_ASM] = sizeof(asm_statement_t)
170 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
171 assert(sizes[kind] != 0);
176 * Allocate a statement node of given kind and initialize all
179 static statement_t *allocate_statement_zero(statement_kind_t kind)
181 size_t size = get_statement_struct_size(kind);
182 statement_t *res = allocate_ast_zero(size);
184 res->base.kind = kind;
189 * Returns the size of an expression node.
191 * @param kind the expression kind
193 static size_t get_expression_struct_size(expression_kind_t kind)
195 static const size_t sizes[] = {
196 [EXPR_INVALID] = sizeof(expression_base_t),
197 [EXPR_REFERENCE] = sizeof(reference_expression_t),
198 [EXPR_CONST] = sizeof(const_expression_t),
199 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
200 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
201 [EXPR_CALL] = sizeof(call_expression_t),
202 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
203 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
204 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
205 [EXPR_SELECT] = sizeof(select_expression_t),
206 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
207 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
208 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
209 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
210 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
211 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
212 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
213 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
214 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
215 [EXPR_VA_START] = sizeof(va_start_expression_t),
216 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
217 [EXPR_STATEMENT] = sizeof(statement_expression_t),
219 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
220 return sizes[EXPR_UNARY_FIRST];
222 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
223 return sizes[EXPR_BINARY_FIRST];
225 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
226 assert(sizes[kind] != 0);
231 * Allocate an expression node of given kind and initialize all
234 static expression_t *allocate_expression_zero(expression_kind_t kind)
236 size_t size = get_expression_struct_size(kind);
237 expression_t *res = allocate_ast_zero(size);
239 res->base.kind = kind;
240 res->base.datatype = type_error_type;
245 * Returns the size of a type node.
247 * @param kind the type kind
249 static size_t get_type_struct_size(type_kind_t kind)
251 static const size_t sizes[] = {
252 [TYPE_ATOMIC] = sizeof(atomic_type_t),
253 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
254 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
255 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
256 [TYPE_ENUM] = sizeof(enum_type_t),
257 [TYPE_FUNCTION] = sizeof(function_type_t),
258 [TYPE_POINTER] = sizeof(pointer_type_t),
259 [TYPE_ARRAY] = sizeof(array_type_t),
260 [TYPE_BUILTIN] = sizeof(builtin_type_t),
261 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
262 [TYPE_TYPEOF] = sizeof(typeof_type_t),
264 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
265 assert(kind <= TYPE_TYPEOF);
266 assert(sizes[kind] != 0);
271 * Allocate a type node of given kind and initialize all
274 static type_t *allocate_type_zero(type_kind_t kind)
276 size_t size = get_type_struct_size(kind);
277 type_t *res = obstack_alloc(type_obst, size);
278 memset(res, 0, size);
280 res->base.kind = kind;
285 * Returns the size of an initializer node.
287 * @param kind the initializer kind
289 static size_t get_initializer_size(initializer_kind_t kind)
291 static const size_t sizes[] = {
292 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
293 [INITIALIZER_STRING] = sizeof(initializer_string_t),
294 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
295 [INITIALIZER_LIST] = sizeof(initializer_list_t)
297 assert(kind < sizeof(sizes) / sizeof(*sizes));
298 assert(sizes[kind] != 0);
303 * Allocate an initializer node of given kind and initialize all
306 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
308 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
315 * Free a type from the type obstack.
317 static void free_type(void *type)
319 obstack_free(type_obst, type);
323 * Returns the index of the top element of the environment stack.
325 static size_t environment_top(void)
327 return ARR_LEN(environment_stack);
331 * Returns the index of the top element of the label stack.
333 static size_t label_top(void)
335 return ARR_LEN(label_stack);
340 * Return the next token.
342 static inline void next_token(void)
344 token = lookahead_buffer[lookahead_bufpos];
345 lookahead_buffer[lookahead_bufpos] = lexer_token;
348 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
351 print_token(stderr, &token);
352 fprintf(stderr, "\n");
357 * Return the next token with a given lookahead.
359 static inline const token_t *look_ahead(int num)
361 assert(num > 0 && num <= MAX_LOOKAHEAD);
362 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
363 return &lookahead_buffer[pos];
366 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
369 * Report a parse error because an expected token was not found.
371 static void parse_error_expected(const char *message, ...)
373 if(message != NULL) {
374 errorf(HERE, "%s", message);
377 va_start(ap, message);
378 errorf(HERE, "got '%K', expected %#k", &token, &ap, ", ");
383 * Report a type error.
385 static void type_error(const char *msg, const source_position_t source_position,
388 errorf(source_position, "%s, but found type '%T'", msg, type);
392 * Report an incompatible type.
394 static void type_error_incompatible(const char *msg,
395 const source_position_t source_position, type_t *type1, type_t *type2)
397 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
401 * Eat an complete block, ie. '{ ... }'.
403 static void eat_block(void)
405 if(token.type == '{')
408 while(token.type != '}') {
409 if(token.type == T_EOF)
411 if(token.type == '{') {
421 * Eat a statement until an ';' token.
423 static void eat_statement(void)
425 while(token.type != ';') {
426 if(token.type == T_EOF)
428 if(token.type == '}')
430 if(token.type == '{') {
440 * Eat a parenthesed term, ie. '( ... )'.
442 static void eat_paren(void)
444 if(token.type == '(')
447 while(token.type != ')') {
448 if(token.type == T_EOF)
450 if(token.type == ')' || token.type == ';' || token.type == '}') {
453 if(token.type == '(') {
457 if(token.type == '{') {
466 #define expect(expected) \
467 if(UNLIKELY(token.type != (expected))) { \
468 parse_error_expected(NULL, (expected), 0); \
474 #define expect_block(expected) \
475 if(UNLIKELY(token.type != (expected))) { \
476 parse_error_expected(NULL, (expected), 0); \
482 #define expect_void(expected) \
483 if(UNLIKELY(token.type != (expected))) { \
484 parse_error_expected(NULL, (expected), 0); \
490 static void set_context(context_t *new_context)
492 context = new_context;
494 last_declaration = new_context->declarations;
495 if(last_declaration != NULL) {
496 while(last_declaration->next != NULL) {
497 last_declaration = last_declaration->next;
503 * Search a symbol in a given namespace and returns its declaration or
504 * NULL if this symbol was not found.
506 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
508 declaration_t *declaration = symbol->declaration;
509 for( ; declaration != NULL; declaration = declaration->symbol_next) {
510 if(declaration->namespc == namespc)
518 * pushs an environment_entry on the environment stack and links the
519 * corresponding symbol to the new entry
521 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
523 symbol_t *symbol = declaration->symbol;
524 namespace_t namespc = (namespace_t)declaration->namespc;
526 /* remember old declaration */
528 entry.symbol = symbol;
529 entry.old_declaration = symbol->declaration;
530 entry.namespc = (unsigned short) namespc;
531 ARR_APP1(stack_entry_t, *stack_ptr, entry);
533 /* replace/add declaration into declaration list of the symbol */
534 if(symbol->declaration == NULL) {
535 symbol->declaration = declaration;
537 declaration_t *iter_last = NULL;
538 declaration_t *iter = symbol->declaration;
539 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
540 /* replace an entry? */
541 if(iter->namespc == namespc) {
542 if(iter_last == NULL) {
543 symbol->declaration = declaration;
545 iter_last->symbol_next = declaration;
547 declaration->symbol_next = iter->symbol_next;
552 assert(iter_last->symbol_next == NULL);
553 iter_last->symbol_next = declaration;
558 static void environment_push(declaration_t *declaration)
560 assert(declaration->source_position.input_name != NULL);
561 assert(declaration->parent_context != NULL);
562 stack_push(&environment_stack, declaration);
565 static void label_push(declaration_t *declaration)
567 declaration->parent_context = ¤t_function->context;
568 stack_push(&label_stack, declaration);
572 * pops symbols from the environment stack until @p new_top is the top element
574 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
576 stack_entry_t *stack = *stack_ptr;
577 size_t top = ARR_LEN(stack);
580 assert(new_top <= top);
584 for(i = top; i > new_top; --i) {
585 stack_entry_t *entry = &stack[i - 1];
587 declaration_t *old_declaration = entry->old_declaration;
588 symbol_t *symbol = entry->symbol;
589 namespace_t namespc = (namespace_t)entry->namespc;
591 /* replace/remove declaration */
592 declaration_t *declaration = symbol->declaration;
593 assert(declaration != NULL);
594 if(declaration->namespc == namespc) {
595 if(old_declaration == NULL) {
596 symbol->declaration = declaration->symbol_next;
598 symbol->declaration = old_declaration;
601 declaration_t *iter_last = declaration;
602 declaration_t *iter = declaration->symbol_next;
603 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
604 /* replace an entry? */
605 if(iter->namespc == namespc) {
606 assert(iter_last != NULL);
607 iter_last->symbol_next = old_declaration;
608 old_declaration->symbol_next = iter->symbol_next;
612 assert(iter != NULL);
616 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
619 static void environment_pop_to(size_t new_top)
621 stack_pop_to(&environment_stack, new_top);
624 static void label_pop_to(size_t new_top)
626 stack_pop_to(&label_stack, new_top);
630 static int get_rank(const type_t *type)
632 assert(!is_typeref(type));
633 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
634 * and esp. footnote 108). However we can't fold constants (yet), so we
635 * can't decide whether unsigned int is possible, while int always works.
636 * (unsigned int would be preferable when possible... for stuff like
637 * struct { enum { ... } bla : 4; } ) */
638 if(type->kind == TYPE_ENUM)
639 return ATOMIC_TYPE_INT;
641 assert(type->kind == TYPE_ATOMIC);
642 return type->atomic.akind;
645 static type_t *promote_integer(type_t *type)
647 if(type->kind == TYPE_BITFIELD)
648 type = type->bitfield.base;
650 if(get_rank(type) < ATOMIC_TYPE_INT)
657 * Create a cast expression.
659 * @param expression the expression to cast
660 * @param dest_type the destination type
662 static expression_t *create_cast_expression(expression_t *expression,
665 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
667 cast->unary.value = expression;
668 cast->base.datatype = dest_type;
674 * Check if a given expression represents the 0 pointer constant.
676 static bool is_null_pointer_constant(const expression_t *expression)
678 /* skip void* cast */
679 if(expression->kind == EXPR_UNARY_CAST
680 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
681 expression = expression->unary.value;
684 /* TODO: not correct yet, should be any constant integer expression
685 * which evaluates to 0 */
686 if (expression->kind != EXPR_CONST)
689 type_t *const type = skip_typeref(expression->base.datatype);
690 if (!is_type_integer(type))
693 return expression->conste.v.int_value == 0;
697 * Create an implicit cast expression.
699 * @param expression the expression to cast
700 * @param dest_type the destination type
702 static expression_t *create_implicit_cast(expression_t *expression,
705 type_t *const source_type = expression->base.datatype;
707 if (source_type == dest_type)
710 return create_cast_expression(expression, dest_type);
713 /** Implements the rules from § 6.5.16.1 */
714 static type_t *semantic_assign(type_t *orig_type_left,
715 const expression_t *const right,
718 type_t *const orig_type_right = right->base.datatype;
719 type_t *const type_left = skip_typeref(orig_type_left);
720 type_t *const type_right = skip_typeref(orig_type_right);
722 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
723 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
724 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
725 && is_type_pointer(type_right))) {
726 return orig_type_left;
729 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
730 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
731 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
733 /* the left type has all qualifiers from the right type */
734 unsigned missing_qualifiers
735 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
736 if(missing_qualifiers != 0) {
737 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
738 return orig_type_left;
741 points_to_left = get_unqualified_type(points_to_left);
742 points_to_right = get_unqualified_type(points_to_right);
744 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
745 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
746 && !types_compatible(points_to_left, points_to_right)) {
750 return orig_type_left;
753 if (is_type_compound(type_left) && is_type_compound(type_right)) {
754 type_t *const unqual_type_left = get_unqualified_type(type_left);
755 type_t *const unqual_type_right = get_unqualified_type(type_right);
756 if (types_compatible(unqual_type_left, unqual_type_right)) {
757 return orig_type_left;
761 if (!is_type_valid(type_left))
764 if (!is_type_valid(type_right))
765 return orig_type_right;
770 static expression_t *parse_constant_expression(void)
772 /* start parsing at precedence 7 (conditional expression) */
773 expression_t *result = parse_sub_expression(7);
775 if(!is_constant_expression(result)) {
776 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
782 static expression_t *parse_assignment_expression(void)
784 /* start parsing at precedence 2 (assignment expression) */
785 return parse_sub_expression(2);
788 static type_t *make_global_typedef(const char *name, type_t *type)
790 symbol_t *const symbol = symbol_table_insert(name);
792 declaration_t *const declaration = allocate_declaration_zero();
793 declaration->namespc = NAMESPACE_NORMAL;
794 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
795 declaration->type = type;
796 declaration->symbol = symbol;
797 declaration->source_position = builtin_source_position;
799 record_declaration(declaration);
801 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
802 typedef_type->typedeft.declaration = declaration;
807 static string_t parse_string_literals(void)
809 assert(token.type == T_STRING_LITERAL);
810 string_t result = token.v.string;
814 while (token.type == T_STRING_LITERAL) {
815 result = concat_strings(&result, &token.v.string);
822 static void parse_attributes(void)
826 case T___attribute__: {
834 errorf(HERE, "EOF while parsing attribute");
853 if(token.type != T_STRING_LITERAL) {
854 parse_error_expected("while parsing assembler attribute",
859 parse_string_literals();
864 goto attributes_finished;
873 static designator_t *parse_designation(void)
875 if(token.type != '[' && token.type != '.')
878 designator_t *result = NULL;
879 designator_t *last = NULL;
882 designator_t *designator;
885 designator = allocate_ast_zero(sizeof(designator[0]));
887 designator->array_access = parse_constant_expression();
891 designator = allocate_ast_zero(sizeof(designator[0]));
893 if(token.type != T_IDENTIFIER) {
894 parse_error_expected("while parsing designator",
898 designator->symbol = token.v.symbol;
906 assert(designator != NULL);
908 last->next = designator;
917 static initializer_t *initializer_from_string(array_type_t *type,
918 const string_t *const string)
920 /* TODO: check len vs. size of array type */
923 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
924 initializer->string.string = *string;
929 static initializer_t *initializer_from_wide_string(array_type_t *const type,
930 wide_string_t *const string)
932 /* TODO: check len vs. size of array type */
935 initializer_t *const initializer =
936 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
937 initializer->wide_string.string = *string;
942 static initializer_t *initializer_from_expression(type_t *type,
943 expression_t *expression)
945 /* TODO check that expression is a constant expression */
947 /* § 6.7.8.14/15 char array may be initialized by string literals */
948 type_t *const expr_type = expression->base.datatype;
949 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
950 array_type_t *const array_type = &type->array;
951 type_t *const element_type = skip_typeref(array_type->element_type);
953 if (element_type->kind == TYPE_ATOMIC) {
954 switch (expression->kind) {
955 case EXPR_STRING_LITERAL:
956 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
957 return initializer_from_string(array_type,
958 &expression->string.value);
961 case EXPR_WIDE_STRING_LITERAL: {
962 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
963 if (get_unqualified_type(element_type) == bare_wchar_type) {
964 return initializer_from_wide_string(array_type,
965 &expression->wide_string.value);
975 type_t *const res_type = semantic_assign(type, expression, "initializer");
976 if (res_type == NULL)
979 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
980 result->value.value = create_implicit_cast(expression, res_type);
985 static initializer_t *parse_sub_initializer(type_t *type,
986 expression_t *expression,
987 type_t *expression_type);
989 static initializer_t *parse_sub_initializer_elem(type_t *type)
991 if(token.type == '{') {
992 return parse_sub_initializer(type, NULL, NULL);
995 expression_t *expression = parse_assignment_expression();
996 type_t *expression_type = skip_typeref(expression->base.datatype);
998 return parse_sub_initializer(type, expression, expression_type);
1001 static bool had_initializer_brace_warning;
1003 static void skip_designator(void)
1006 if(token.type == '.') {
1008 if(token.type == T_IDENTIFIER)
1010 } else if(token.type == '[') {
1012 parse_constant_expression();
1013 if(token.type == ']')
1021 static initializer_t *parse_sub_initializer(type_t *type,
1022 expression_t *expression,
1023 type_t *expression_type)
1025 if(is_type_scalar(type)) {
1026 /* there might be extra {} hierarchies */
1027 if(token.type == '{') {
1029 if(!had_initializer_brace_warning) {
1030 warningf(HERE, "braces around scalar initializer");
1031 had_initializer_brace_warning = true;
1033 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1034 if(token.type == ',') {
1036 /* TODO: warn about excessive elements */
1042 if(expression == NULL) {
1043 expression = parse_assignment_expression();
1045 return initializer_from_expression(type, expression);
1048 /* does the expression match the currently looked at object to initialize */
1049 if(expression != NULL) {
1050 initializer_t *result = initializer_from_expression(type, expression);
1055 bool read_paren = false;
1056 if(token.type == '{') {
1061 /* descend into subtype */
1062 initializer_t *result = NULL;
1063 initializer_t **elems;
1064 if(is_type_array(type)) {
1065 if(token.type == '.') {
1067 "compound designator in initializer for array type '%T'",
1072 type_t *const element_type = skip_typeref(type->array.element_type);
1075 had_initializer_brace_warning = false;
1076 if(expression == NULL) {
1077 sub = parse_sub_initializer_elem(element_type);
1079 sub = parse_sub_initializer(element_type, expression,
1083 /* didn't match the subtypes -> try the parent type */
1085 assert(!read_paren);
1089 elems = NEW_ARR_F(initializer_t*, 0);
1090 ARR_APP1(initializer_t*, elems, sub);
1093 if(token.type == '}')
1096 if(token.type == '}')
1099 sub = parse_sub_initializer_elem(element_type);
1101 /* TODO error, do nicer cleanup */
1102 errorf(HERE, "member initializer didn't match");
1106 ARR_APP1(initializer_t*, elems, sub);
1109 assert(is_type_compound(type));
1110 context_t *const context = &type->compound.declaration->context;
1112 if(token.type == '[') {
1114 "array designator in initializer for compound type '%T'",
1119 declaration_t *first = context->declarations;
1122 type_t *first_type = first->type;
1123 first_type = skip_typeref(first_type);
1126 had_initializer_brace_warning = false;
1127 if(expression == NULL) {
1128 sub = parse_sub_initializer_elem(first_type);
1130 sub = parse_sub_initializer(first_type, expression,expression_type);
1133 /* didn't match the subtypes -> try our parent type */
1135 assert(!read_paren);
1139 elems = NEW_ARR_F(initializer_t*, 0);
1140 ARR_APP1(initializer_t*, elems, sub);
1142 declaration_t *iter = first->next;
1143 for( ; iter != NULL; iter = iter->next) {
1144 if(iter->symbol == NULL)
1146 if(iter->namespc != NAMESPACE_NORMAL)
1149 if(token.type == '}')
1152 if(token.type == '}')
1155 type_t *iter_type = iter->type;
1156 iter_type = skip_typeref(iter_type);
1158 sub = parse_sub_initializer_elem(iter_type);
1160 /* TODO error, do nicer cleanup */
1161 errorf(HERE, "member initializer didn't match");
1165 ARR_APP1(initializer_t*, elems, sub);
1169 int len = ARR_LEN(elems);
1170 size_t elems_size = sizeof(initializer_t*) * len;
1172 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1174 init->initializer.kind = INITIALIZER_LIST;
1176 memcpy(init->initializers, elems, elems_size);
1179 result = (initializer_t*) init;
1182 if(token.type == ',')
1189 static initializer_t *parse_initializer(type_t *const orig_type)
1191 initializer_t *result;
1193 type_t *const type = skip_typeref(orig_type);
1195 if(token.type != '{') {
1196 expression_t *expression = parse_assignment_expression();
1197 initializer_t *initializer = initializer_from_expression(type, expression);
1198 if(initializer == NULL) {
1200 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1201 expression, expression->base.datatype, orig_type);
1206 if(is_type_scalar(type)) {
1210 expression_t *expression = parse_assignment_expression();
1211 result = initializer_from_expression(type, expression);
1213 if(token.type == ',')
1219 result = parse_sub_initializer(type, NULL, NULL);
1225 static declaration_t *append_declaration(declaration_t *declaration);
1227 static declaration_t *parse_compound_type_specifier(bool is_struct)
1235 symbol_t *symbol = NULL;
1236 declaration_t *declaration = NULL;
1238 if (token.type == T___attribute__) {
1243 if(token.type == T_IDENTIFIER) {
1244 symbol = token.v.symbol;
1248 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1250 declaration = get_declaration(symbol, NAMESPACE_UNION);
1252 } else if(token.type != '{') {
1254 parse_error_expected("while parsing struct type specifier",
1255 T_IDENTIFIER, '{', 0);
1257 parse_error_expected("while parsing union type specifier",
1258 T_IDENTIFIER, '{', 0);
1264 if(declaration == NULL) {
1265 declaration = allocate_declaration_zero();
1266 declaration->namespc =
1267 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1268 declaration->source_position = token.source_position;
1269 declaration->symbol = symbol;
1270 declaration->parent_context = context;
1271 if (symbol != NULL) {
1272 environment_push(declaration);
1274 append_declaration(declaration);
1277 if(token.type == '{') {
1278 if(declaration->init.is_defined) {
1279 assert(symbol != NULL);
1280 errorf(HERE, "multiple definition of '%s %Y'",
1281 is_struct ? "struct" : "union", symbol);
1282 declaration->context.declarations = NULL;
1284 declaration->init.is_defined = true;
1286 int top = environment_top();
1287 context_t *last_context = context;
1288 set_context(&declaration->context);
1290 parse_compound_type_entries();
1293 assert(context == &declaration->context);
1294 set_context(last_context);
1295 environment_pop_to(top);
1301 static void parse_enum_entries(type_t *const enum_type)
1305 if(token.type == '}') {
1307 errorf(HERE, "empty enum not allowed");
1312 if(token.type != T_IDENTIFIER) {
1313 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1318 declaration_t *const entry = allocate_declaration_zero();
1319 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1320 entry->type = enum_type;
1321 entry->symbol = token.v.symbol;
1322 entry->source_position = token.source_position;
1325 if(token.type == '=') {
1327 entry->init.enum_value = parse_constant_expression();
1332 record_declaration(entry);
1334 if(token.type != ',')
1337 } while(token.type != '}');
1342 static type_t *parse_enum_specifier(void)
1346 declaration_t *declaration;
1349 if(token.type == T_IDENTIFIER) {
1350 symbol = token.v.symbol;
1353 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1354 } else if(token.type != '{') {
1355 parse_error_expected("while parsing enum type specifier",
1356 T_IDENTIFIER, '{', 0);
1363 if(declaration == NULL) {
1364 declaration = allocate_declaration_zero();
1365 declaration->namespc = NAMESPACE_ENUM;
1366 declaration->source_position = token.source_position;
1367 declaration->symbol = symbol;
1368 declaration->parent_context = context;
1371 type_t *const type = allocate_type_zero(TYPE_ENUM);
1372 type->enumt.declaration = declaration;
1374 if(token.type == '{') {
1375 if(declaration->init.is_defined) {
1376 errorf(HERE, "multiple definitions of enum %Y", symbol);
1378 if (symbol != NULL) {
1379 environment_push(declaration);
1381 append_declaration(declaration);
1382 declaration->init.is_defined = 1;
1384 parse_enum_entries(type);
1392 * if a symbol is a typedef to another type, return true
1394 static bool is_typedef_symbol(symbol_t *symbol)
1396 const declaration_t *const declaration =
1397 get_declaration(symbol, NAMESPACE_NORMAL);
1399 declaration != NULL &&
1400 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1403 static type_t *parse_typeof(void)
1411 expression_t *expression = NULL;
1414 switch(token.type) {
1415 case T___extension__:
1416 /* this can be a prefix to a typename or an expression */
1417 /* we simply eat it now. */
1420 } while(token.type == T___extension__);
1424 if(is_typedef_symbol(token.v.symbol)) {
1425 type = parse_typename();
1427 expression = parse_expression();
1428 type = expression->base.datatype;
1433 type = parse_typename();
1437 expression = parse_expression();
1438 type = expression->base.datatype;
1444 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1445 typeof_type->typeoft.expression = expression;
1446 typeof_type->typeoft.typeof_type = type;
1452 SPECIFIER_SIGNED = 1 << 0,
1453 SPECIFIER_UNSIGNED = 1 << 1,
1454 SPECIFIER_LONG = 1 << 2,
1455 SPECIFIER_INT = 1 << 3,
1456 SPECIFIER_DOUBLE = 1 << 4,
1457 SPECIFIER_CHAR = 1 << 5,
1458 SPECIFIER_SHORT = 1 << 6,
1459 SPECIFIER_LONG_LONG = 1 << 7,
1460 SPECIFIER_FLOAT = 1 << 8,
1461 SPECIFIER_BOOL = 1 << 9,
1462 SPECIFIER_VOID = 1 << 10,
1463 #ifdef PROVIDE_COMPLEX
1464 SPECIFIER_COMPLEX = 1 << 11,
1465 SPECIFIER_IMAGINARY = 1 << 12,
1469 static type_t *create_builtin_type(symbol_t *const symbol,
1470 type_t *const real_type)
1472 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1473 type->builtin.symbol = symbol;
1474 type->builtin.real_type = real_type;
1476 type_t *result = typehash_insert(type);
1477 if (type != result) {
1484 static type_t *get_typedef_type(symbol_t *symbol)
1486 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1487 if(declaration == NULL
1488 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1491 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1492 type->typedeft.declaration = declaration;
1497 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1499 type_t *type = NULL;
1500 unsigned type_qualifiers = 0;
1501 unsigned type_specifiers = 0;
1504 specifiers->source_position = token.source_position;
1507 switch(token.type) {
1510 #define MATCH_STORAGE_CLASS(token, class) \
1512 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1513 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1515 specifiers->storage_class = class; \
1519 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1520 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1521 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1522 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1523 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1526 switch (specifiers->storage_class) {
1527 case STORAGE_CLASS_NONE:
1528 specifiers->storage_class = STORAGE_CLASS_THREAD;
1531 case STORAGE_CLASS_EXTERN:
1532 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1535 case STORAGE_CLASS_STATIC:
1536 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1540 errorf(HERE, "multiple storage classes in declaration specifiers");
1546 /* type qualifiers */
1547 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1549 type_qualifiers |= qualifier; \
1553 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1554 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1555 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1557 case T___extension__:
1562 /* type specifiers */
1563 #define MATCH_SPECIFIER(token, specifier, name) \
1566 if(type_specifiers & specifier) { \
1567 errorf(HERE, "multiple " name " type specifiers given"); \
1569 type_specifiers |= specifier; \
1573 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1574 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1575 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1576 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1577 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1578 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1579 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1580 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1581 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1582 #ifdef PROVIDE_COMPLEX
1583 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1584 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1587 /* only in microsoft mode */
1588 specifiers->decl_modifiers |= DM_FORCEINLINE;
1592 specifiers->is_inline = true;
1597 if(type_specifiers & SPECIFIER_LONG_LONG) {
1598 errorf(HERE, "multiple type specifiers given");
1599 } else if(type_specifiers & SPECIFIER_LONG) {
1600 type_specifiers |= SPECIFIER_LONG_LONG;
1602 type_specifiers |= SPECIFIER_LONG;
1606 /* TODO: if is_type_valid(type) for the following rules should issue
1609 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1611 type->compound.declaration = parse_compound_type_specifier(true);
1615 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1617 type->compound.declaration = parse_compound_type_specifier(false);
1621 type = parse_enum_specifier();
1624 type = parse_typeof();
1626 case T___builtin_va_list:
1627 type = duplicate_type(type_valist);
1631 case T___attribute__:
1636 case T_IDENTIFIER: {
1637 type_t *typedef_type = get_typedef_type(token.v.symbol);
1639 if(typedef_type == NULL)
1640 goto finish_specifiers;
1643 type = typedef_type;
1647 /* function specifier */
1649 goto finish_specifiers;
1656 atomic_type_kind_t atomic_type;
1658 /* match valid basic types */
1659 switch(type_specifiers) {
1660 case SPECIFIER_VOID:
1661 atomic_type = ATOMIC_TYPE_VOID;
1663 case SPECIFIER_CHAR:
1664 atomic_type = ATOMIC_TYPE_CHAR;
1666 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1667 atomic_type = ATOMIC_TYPE_SCHAR;
1669 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1670 atomic_type = ATOMIC_TYPE_UCHAR;
1672 case SPECIFIER_SHORT:
1673 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1674 case SPECIFIER_SHORT | SPECIFIER_INT:
1675 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1676 atomic_type = ATOMIC_TYPE_SHORT;
1678 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1679 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1680 atomic_type = ATOMIC_TYPE_USHORT;
1683 case SPECIFIER_SIGNED:
1684 case SPECIFIER_SIGNED | SPECIFIER_INT:
1685 atomic_type = ATOMIC_TYPE_INT;
1687 case SPECIFIER_UNSIGNED:
1688 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1689 atomic_type = ATOMIC_TYPE_UINT;
1691 case SPECIFIER_LONG:
1692 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1693 case SPECIFIER_LONG | SPECIFIER_INT:
1694 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1695 atomic_type = ATOMIC_TYPE_LONG;
1697 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1698 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1699 atomic_type = ATOMIC_TYPE_ULONG;
1701 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1702 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1703 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1704 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1706 atomic_type = ATOMIC_TYPE_LONGLONG;
1708 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1709 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1711 atomic_type = ATOMIC_TYPE_ULONGLONG;
1713 case SPECIFIER_FLOAT:
1714 atomic_type = ATOMIC_TYPE_FLOAT;
1716 case SPECIFIER_DOUBLE:
1717 atomic_type = ATOMIC_TYPE_DOUBLE;
1719 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1720 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1722 case SPECIFIER_BOOL:
1723 atomic_type = ATOMIC_TYPE_BOOL;
1725 #ifdef PROVIDE_COMPLEX
1726 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1727 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1729 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1730 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1732 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1733 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1735 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1736 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1738 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1739 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1741 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1742 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1746 /* invalid specifier combination, give an error message */
1747 if(type_specifiers == 0) {
1748 if (! strict_mode) {
1749 if (warning.implicit_int) {
1750 warningf(HERE, "no type specifiers in declaration, using 'int'");
1752 atomic_type = ATOMIC_TYPE_INT;
1755 errorf(HERE, "no type specifiers given in declaration");
1757 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1758 (type_specifiers & SPECIFIER_UNSIGNED)) {
1759 errorf(HERE, "signed and unsigned specifiers gives");
1760 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1761 errorf(HERE, "only integer types can be signed or unsigned");
1763 errorf(HERE, "multiple datatypes in declaration");
1765 atomic_type = ATOMIC_TYPE_INVALID;
1768 type = allocate_type_zero(TYPE_ATOMIC);
1769 type->atomic.akind = atomic_type;
1772 if(type_specifiers != 0) {
1773 errorf(HERE, "multiple datatypes in declaration");
1777 type->base.qualifiers = type_qualifiers;
1779 type_t *result = typehash_insert(type);
1780 if(newtype && result != type) {
1784 specifiers->type = result;
1787 static type_qualifiers_t parse_type_qualifiers(void)
1789 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1792 switch(token.type) {
1793 /* type qualifiers */
1794 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1795 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1796 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1799 return type_qualifiers;
1804 static declaration_t *parse_identifier_list(void)
1806 declaration_t *declarations = NULL;
1807 declaration_t *last_declaration = NULL;
1809 declaration_t *const declaration = allocate_declaration_zero();
1810 declaration->source_position = token.source_position;
1811 declaration->symbol = token.v.symbol;
1814 if(last_declaration != NULL) {
1815 last_declaration->next = declaration;
1817 declarations = declaration;
1819 last_declaration = declaration;
1821 if(token.type != ',')
1824 } while(token.type == T_IDENTIFIER);
1826 return declarations;
1829 static void semantic_parameter(declaration_t *declaration)
1831 /* TODO: improve error messages */
1833 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1834 errorf(HERE, "typedef not allowed in parameter list");
1835 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1836 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1837 errorf(HERE, "parameter may only have none or register storage class");
1840 type_t *const orig_type = declaration->type;
1841 type_t * type = skip_typeref(orig_type);
1843 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1844 * into a pointer. § 6.7.5.3 (7) */
1845 if (is_type_array(type)) {
1846 type_t *const element_type = type->array.element_type;
1848 type = make_pointer_type(element_type, type->base.qualifiers);
1850 declaration->type = type;
1853 if(is_type_incomplete(type)) {
1854 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1855 orig_type, declaration->symbol);
1859 static declaration_t *parse_parameter(void)
1861 declaration_specifiers_t specifiers;
1862 memset(&specifiers, 0, sizeof(specifiers));
1864 parse_declaration_specifiers(&specifiers);
1866 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1868 semantic_parameter(declaration);
1873 static declaration_t *parse_parameters(function_type_t *type)
1875 if(token.type == T_IDENTIFIER) {
1876 symbol_t *symbol = token.v.symbol;
1877 if(!is_typedef_symbol(symbol)) {
1878 type->kr_style_parameters = true;
1879 return parse_identifier_list();
1883 if(token.type == ')') {
1884 type->unspecified_parameters = 1;
1887 if(token.type == T_void && look_ahead(1)->type == ')') {
1892 declaration_t *declarations = NULL;
1893 declaration_t *declaration;
1894 declaration_t *last_declaration = NULL;
1895 function_parameter_t *parameter;
1896 function_parameter_t *last_parameter = NULL;
1899 switch(token.type) {
1903 return declarations;
1906 case T___extension__:
1908 declaration = parse_parameter();
1910 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1911 memset(parameter, 0, sizeof(parameter[0]));
1912 parameter->type = declaration->type;
1914 if(last_parameter != NULL) {
1915 last_declaration->next = declaration;
1916 last_parameter->next = parameter;
1918 type->parameters = parameter;
1919 declarations = declaration;
1921 last_parameter = parameter;
1922 last_declaration = declaration;
1926 return declarations;
1928 if(token.type != ',')
1929 return declarations;
1939 } construct_type_type_t;
1941 typedef struct construct_type_t construct_type_t;
1942 struct construct_type_t {
1943 construct_type_type_t type;
1944 construct_type_t *next;
1947 typedef struct parsed_pointer_t parsed_pointer_t;
1948 struct parsed_pointer_t {
1949 construct_type_t construct_type;
1950 type_qualifiers_t type_qualifiers;
1953 typedef struct construct_function_type_t construct_function_type_t;
1954 struct construct_function_type_t {
1955 construct_type_t construct_type;
1956 type_t *function_type;
1959 typedef struct parsed_array_t parsed_array_t;
1960 struct parsed_array_t {
1961 construct_type_t construct_type;
1962 type_qualifiers_t type_qualifiers;
1968 typedef struct construct_base_type_t construct_base_type_t;
1969 struct construct_base_type_t {
1970 construct_type_t construct_type;
1974 static construct_type_t *parse_pointer_declarator(void)
1978 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1979 memset(pointer, 0, sizeof(pointer[0]));
1980 pointer->construct_type.type = CONSTRUCT_POINTER;
1981 pointer->type_qualifiers = parse_type_qualifiers();
1983 return (construct_type_t*) pointer;
1986 static construct_type_t *parse_array_declarator(void)
1990 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1991 memset(array, 0, sizeof(array[0]));
1992 array->construct_type.type = CONSTRUCT_ARRAY;
1994 if(token.type == T_static) {
1995 array->is_static = true;
1999 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2000 if(type_qualifiers != 0) {
2001 if(token.type == T_static) {
2002 array->is_static = true;
2006 array->type_qualifiers = type_qualifiers;
2008 if(token.type == '*' && look_ahead(1)->type == ']') {
2009 array->is_variable = true;
2011 } else if(token.type != ']') {
2012 array->size = parse_assignment_expression();
2017 return (construct_type_t*) array;
2020 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2024 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2026 declaration_t *parameters = parse_parameters(&type->function);
2027 if(declaration != NULL) {
2028 declaration->context.declarations = parameters;
2031 construct_function_type_t *construct_function_type =
2032 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2033 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2034 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2035 construct_function_type->function_type = type;
2039 return (construct_type_t*) construct_function_type;
2042 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2043 bool may_be_abstract)
2045 /* construct a single linked list of construct_type_t's which describe
2046 * how to construct the final declarator type */
2047 construct_type_t *first = NULL;
2048 construct_type_t *last = NULL;
2051 while(token.type == '*') {
2052 construct_type_t *type = parse_pointer_declarator();
2063 /* TODO: find out if this is correct */
2066 construct_type_t *inner_types = NULL;
2068 switch(token.type) {
2070 if(declaration == NULL) {
2071 errorf(HERE, "no identifier expected in typename");
2073 declaration->symbol = token.v.symbol;
2074 declaration->source_position = token.source_position;
2080 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2086 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2087 /* avoid a loop in the outermost scope, because eat_statement doesn't
2089 if(token.type == '}' && current_function == NULL) {
2097 construct_type_t *p = last;
2100 construct_type_t *type;
2101 switch(token.type) {
2103 type = parse_function_declarator(declaration);
2106 type = parse_array_declarator();
2109 goto declarator_finished;
2112 /* insert in the middle of the list (behind p) */
2114 type->next = p->next;
2125 declarator_finished:
2128 /* append inner_types at the end of the list, we don't to set last anymore
2129 * as it's not needed anymore */
2131 assert(first == NULL);
2132 first = inner_types;
2134 last->next = inner_types;
2140 static type_t *construct_declarator_type(construct_type_t *construct_list,
2143 construct_type_t *iter = construct_list;
2144 for( ; iter != NULL; iter = iter->next) {
2145 switch(iter->type) {
2146 case CONSTRUCT_INVALID:
2147 panic("invalid type construction found");
2148 case CONSTRUCT_FUNCTION: {
2149 construct_function_type_t *construct_function_type
2150 = (construct_function_type_t*) iter;
2152 type_t *function_type = construct_function_type->function_type;
2154 function_type->function.return_type = type;
2156 type_t *skipped_return_type = skip_typeref(type);
2157 if (is_type_function(skipped_return_type)) {
2158 errorf(HERE, "function returning function is not allowed");
2159 type = type_error_type;
2160 } else if (is_type_array(skipped_return_type)) {
2161 errorf(HERE, "function returning array is not allowed");
2162 type = type_error_type;
2164 type = function_type;
2169 case CONSTRUCT_POINTER: {
2170 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2171 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2172 pointer_type->pointer.points_to = type;
2173 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2175 type = pointer_type;
2179 case CONSTRUCT_ARRAY: {
2180 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2181 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2183 array_type->base.qualifiers = parsed_array->type_qualifiers;
2184 array_type->array.element_type = type;
2185 array_type->array.is_static = parsed_array->is_static;
2186 array_type->array.is_variable = parsed_array->is_variable;
2187 array_type->array.size = parsed_array->size;
2189 type_t *skipped_type = skip_typeref(type);
2190 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2191 errorf(HERE, "array of void is not allowed");
2192 type = type_error_type;
2200 type_t *hashed_type = typehash_insert(type);
2201 if(hashed_type != type) {
2202 /* the function type was constructed earlier freeing it here will
2203 * destroy other types... */
2204 if(iter->type != CONSTRUCT_FUNCTION) {
2214 static declaration_t *parse_declarator(
2215 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2217 declaration_t *const declaration = allocate_declaration_zero();
2218 declaration->storage_class = specifiers->storage_class;
2219 declaration->modifiers = specifiers->decl_modifiers;
2220 declaration->is_inline = specifiers->is_inline;
2222 construct_type_t *construct_type
2223 = parse_inner_declarator(declaration, may_be_abstract);
2224 type_t *const type = specifiers->type;
2225 declaration->type = construct_declarator_type(construct_type, type);
2227 if(construct_type != NULL) {
2228 obstack_free(&temp_obst, construct_type);
2234 static type_t *parse_abstract_declarator(type_t *base_type)
2236 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2238 type_t *result = construct_declarator_type(construct_type, base_type);
2239 if(construct_type != NULL) {
2240 obstack_free(&temp_obst, construct_type);
2246 static declaration_t *append_declaration(declaration_t* const declaration)
2248 if (last_declaration != NULL) {
2249 last_declaration->next = declaration;
2251 context->declarations = declaration;
2253 last_declaration = declaration;
2257 static declaration_t *internal_record_declaration(
2258 declaration_t *const declaration,
2259 const bool is_function_definition)
2261 const symbol_t *const symbol = declaration->symbol;
2262 const namespace_t namespc = (namespace_t)declaration->namespc;
2264 const type_t *const type = skip_typeref(declaration->type);
2265 if (is_type_function(type) &&
2266 type->function.unspecified_parameters &&
2267 warning.strict_prototypes) {
2268 warningf(declaration->source_position,
2269 "function declaration '%#T' is not a prototype",
2270 type, declaration->symbol);
2273 declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2274 assert(declaration != previous_declaration);
2275 if (previous_declaration != NULL) {
2276 if (previous_declaration->parent_context == context) {
2277 /* can happen for K&R style declarations */
2278 if(previous_declaration->type == NULL) {
2279 previous_declaration->type = declaration->type;
2282 const type_t *const prev_type = skip_typeref(previous_declaration->type);
2283 if (!types_compatible(type, prev_type)) {
2284 errorf(declaration->source_position,
2285 "declaration '%#T' is incompatible with previous declaration '%#T'",
2286 type, symbol, previous_declaration->type, symbol);
2287 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2289 unsigned old_storage_class = previous_declaration->storage_class;
2290 unsigned new_storage_class = declaration->storage_class;
2292 /* pretend no storage class means extern for function declarations
2293 * (except if the previous declaration is neither none nor extern) */
2294 if (is_type_function(type)) {
2295 switch (old_storage_class) {
2296 case STORAGE_CLASS_NONE:
2297 old_storage_class = STORAGE_CLASS_EXTERN;
2299 case STORAGE_CLASS_EXTERN:
2300 if (new_storage_class == STORAGE_CLASS_NONE && !is_function_definition) {
2301 new_storage_class = STORAGE_CLASS_EXTERN;
2309 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2310 new_storage_class == STORAGE_CLASS_EXTERN) {
2311 warn_redundant_declaration:
2312 if (warning.redundant_decls) {
2313 warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
2314 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2316 } else if (current_function == NULL) {
2317 if (old_storage_class != STORAGE_CLASS_STATIC &&
2318 new_storage_class == STORAGE_CLASS_STATIC) {
2319 errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
2320 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2322 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2323 goto warn_redundant_declaration;
2325 if (new_storage_class == STORAGE_CLASS_NONE) {
2326 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2330 if (old_storage_class == new_storage_class) {
2331 errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
2333 errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
2335 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2338 return previous_declaration;
2340 } else if (is_function_definition &&
2341 declaration->storage_class != STORAGE_CLASS_STATIC &&
2342 warning.missing_declarations) {
2343 warningf(declaration->source_position, "no previous declaration for '%#T'", type, symbol);
2346 assert(declaration->parent_context == NULL);
2347 assert(declaration->symbol != NULL);
2348 assert(context != NULL);
2350 declaration->parent_context = context;
2352 environment_push(declaration);
2353 return append_declaration(declaration);
2356 static declaration_t *record_declaration(declaration_t *declaration)
2358 return internal_record_declaration(declaration, false);
2361 static declaration_t *record_function_definition(declaration_t *declaration)
2363 return internal_record_declaration(declaration, true);
2366 static void parser_error_multiple_definition(declaration_t *declaration,
2367 const source_position_t source_position)
2369 errorf(source_position, "multiple definition of symbol '%Y'",
2370 declaration->symbol);
2371 errorf(declaration->source_position,
2372 "this is the location of the previous definition.");
2375 static bool is_declaration_specifier(const token_t *token,
2376 bool only_type_specifiers)
2378 switch(token->type) {
2382 return is_typedef_symbol(token->v.symbol);
2384 case T___extension__:
2387 return !only_type_specifiers;
2394 static void parse_init_declarator_rest(declaration_t *declaration)
2398 type_t *orig_type = declaration->type;
2399 type_t *type = type = skip_typeref(orig_type);
2401 if(declaration->init.initializer != NULL) {
2402 parser_error_multiple_definition(declaration, token.source_position);
2405 initializer_t *initializer = parse_initializer(type);
2407 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2408 * the array type size */
2409 if(is_type_array(type) && initializer != NULL) {
2410 array_type_t *array_type = &type->array;
2412 if(array_type->size == NULL) {
2413 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2415 cnst->base.datatype = type_size_t;
2417 switch (initializer->kind) {
2418 case INITIALIZER_LIST: {
2419 cnst->conste.v.int_value = initializer->list.len;
2423 case INITIALIZER_STRING: {
2424 cnst->conste.v.int_value = initializer->string.string.size;
2428 case INITIALIZER_WIDE_STRING: {
2429 cnst->conste.v.int_value = initializer->wide_string.string.size;
2434 panic("invalid initializer type");
2437 array_type->size = cnst;
2441 if(is_type_function(type)) {
2442 errorf(declaration->source_position,
2443 "initializers not allowed for function types at declator '%Y' (type '%T')",
2444 declaration->symbol, orig_type);
2446 declaration->init.initializer = initializer;
2450 /* parse rest of a declaration without any declarator */
2451 static void parse_anonymous_declaration_rest(
2452 const declaration_specifiers_t *specifiers,
2453 parsed_declaration_func finished_declaration)
2457 declaration_t *const declaration = allocate_declaration_zero();
2458 declaration->type = specifiers->type;
2459 declaration->storage_class = specifiers->storage_class;
2460 declaration->source_position = specifiers->source_position;
2462 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2463 warningf(declaration->source_position, "useless storage class in empty declaration");
2466 type_t *type = declaration->type;
2467 switch (type->kind) {
2468 case TYPE_COMPOUND_STRUCT:
2469 case TYPE_COMPOUND_UNION: {
2470 if (type->compound.declaration->symbol == NULL) {
2471 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2480 warningf(declaration->source_position, "empty declaration");
2484 finished_declaration(declaration);
2487 static void parse_declaration_rest(declaration_t *ndeclaration,
2488 const declaration_specifiers_t *specifiers,
2489 parsed_declaration_func finished_declaration)
2492 declaration_t *declaration = finished_declaration(ndeclaration);
2494 type_t *orig_type = declaration->type;
2495 type_t *type = skip_typeref(orig_type);
2497 if (type->kind != TYPE_FUNCTION &&
2498 declaration->is_inline &&
2499 is_type_valid(type)) {
2500 warningf(declaration->source_position,
2501 "variable '%Y' declared 'inline'\n", declaration->symbol);
2504 if(token.type == '=') {
2505 parse_init_declarator_rest(declaration);
2508 if(token.type != ',')
2512 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2517 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2519 symbol_t *symbol = declaration->symbol;
2520 if(symbol == NULL) {
2521 errorf(HERE, "anonymous declaration not valid as function parameter");
2524 namespace_t namespc = (namespace_t) declaration->namespc;
2525 if(namespc != NAMESPACE_NORMAL) {
2526 return record_declaration(declaration);
2529 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2530 if(previous_declaration == NULL ||
2531 previous_declaration->parent_context != context) {
2532 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2537 if(previous_declaration->type == NULL) {
2538 previous_declaration->type = declaration->type;
2539 previous_declaration->storage_class = declaration->storage_class;
2540 previous_declaration->parent_context = context;
2541 return previous_declaration;
2543 return record_declaration(declaration);
2547 static void parse_declaration(parsed_declaration_func finished_declaration)
2549 declaration_specifiers_t specifiers;
2550 memset(&specifiers, 0, sizeof(specifiers));
2551 parse_declaration_specifiers(&specifiers);
2553 if(token.type == ';') {
2554 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2556 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2557 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2561 static void parse_kr_declaration_list(declaration_t *declaration)
2563 type_t *type = skip_typeref(declaration->type);
2564 if(!is_type_function(type))
2567 if(!type->function.kr_style_parameters)
2570 /* push function parameters */
2571 int top = environment_top();
2572 context_t *last_context = context;
2573 set_context(&declaration->context);
2575 declaration_t *parameter = declaration->context.declarations;
2576 for( ; parameter != NULL; parameter = parameter->next) {
2577 assert(parameter->parent_context == NULL);
2578 parameter->parent_context = context;
2579 environment_push(parameter);
2582 /* parse declaration list */
2583 while(is_declaration_specifier(&token, false)) {
2584 parse_declaration(finished_kr_declaration);
2587 /* pop function parameters */
2588 assert(context == &declaration->context);
2589 set_context(last_context);
2590 environment_pop_to(top);
2592 /* update function type */
2593 type_t *new_type = duplicate_type(type);
2594 new_type->function.kr_style_parameters = false;
2596 function_parameter_t *parameters = NULL;
2597 function_parameter_t *last_parameter = NULL;
2599 declaration_t *parameter_declaration = declaration->context.declarations;
2600 for( ; parameter_declaration != NULL;
2601 parameter_declaration = parameter_declaration->next) {
2602 type_t *parameter_type = parameter_declaration->type;
2603 if(parameter_type == NULL) {
2605 errorf(HERE, "no type specified for function parameter '%Y'",
2606 parameter_declaration->symbol);
2608 if (warning.implicit_int) {
2609 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
2610 parameter_declaration->symbol);
2612 parameter_type = type_int;
2613 parameter_declaration->type = parameter_type;
2617 semantic_parameter(parameter_declaration);
2618 parameter_type = parameter_declaration->type;
2620 function_parameter_t *function_parameter
2621 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2622 memset(function_parameter, 0, sizeof(function_parameter[0]));
2624 function_parameter->type = parameter_type;
2625 if(last_parameter != NULL) {
2626 last_parameter->next = function_parameter;
2628 parameters = function_parameter;
2630 last_parameter = function_parameter;
2632 new_type->function.parameters = parameters;
2634 type = typehash_insert(new_type);
2635 if(type != new_type) {
2636 obstack_free(type_obst, new_type);
2639 declaration->type = type;
2643 * Check if all labels are defined in the current function.
2645 static void check_for_missing_labels(void)
2647 bool first_err = true;
2648 for (const goto_statement_t *goto_statement = goto_first;
2649 goto_statement != NULL;
2650 goto_statement = goto_statement->next) {
2651 const declaration_t *label = goto_statement->label;
2653 if (label->source_position.input_name == NULL) {
2656 diagnosticf("%s: In function '%Y':\n",
2657 current_function->source_position.input_name,
2658 current_function->symbol);
2660 errorf(goto_statement->statement.source_position,
2661 "label '%Y' used but not defined", label->symbol);
2664 goto_first = goto_last = NULL;
2667 static void parse_external_declaration(void)
2669 /* function-definitions and declarations both start with declaration
2671 declaration_specifiers_t specifiers;
2672 memset(&specifiers, 0, sizeof(specifiers));
2673 parse_declaration_specifiers(&specifiers);
2675 /* must be a declaration */
2676 if(token.type == ';') {
2677 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2681 /* declarator is common to both function-definitions and declarations */
2682 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2684 /* must be a declaration */
2685 if(token.type == ',' || token.type == '=' || token.type == ';') {
2686 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2690 /* must be a function definition */
2691 parse_kr_declaration_list(ndeclaration);
2693 if(token.type != '{') {
2694 parse_error_expected("while parsing function definition", '{', 0);
2699 type_t *type = ndeclaration->type;
2701 /* note that we don't skip typerefs: the standard doesn't allow them here
2702 * (so we can't use is_type_function here) */
2703 if(type->kind != TYPE_FUNCTION) {
2704 if (is_type_valid(type)) {
2705 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2706 type, ndeclaration->symbol);
2712 /* § 6.7.5.3 (14) a function definition with () means no
2713 * parameters (and not unspecified parameters) */
2714 if(type->function.unspecified_parameters) {
2715 type_t *duplicate = duplicate_type(type);
2716 duplicate->function.unspecified_parameters = false;
2718 type = typehash_insert(duplicate);
2719 if(type != duplicate) {
2720 obstack_free(type_obst, duplicate);
2722 ndeclaration->type = type;
2725 declaration_t *const declaration = record_function_definition(ndeclaration);
2726 if(ndeclaration != declaration) {
2727 declaration->context = ndeclaration->context;
2729 type = skip_typeref(declaration->type);
2731 /* push function parameters and switch context */
2732 int top = environment_top();
2733 context_t *last_context = context;
2734 set_context(&declaration->context);
2736 declaration_t *parameter = declaration->context.declarations;
2737 for( ; parameter != NULL; parameter = parameter->next) {
2738 if(parameter->parent_context == &ndeclaration->context) {
2739 parameter->parent_context = context;
2741 assert(parameter->parent_context == NULL
2742 || parameter->parent_context == context);
2743 parameter->parent_context = context;
2744 environment_push(parameter);
2747 if(declaration->init.statement != NULL) {
2748 parser_error_multiple_definition(declaration, token.source_position);
2750 goto end_of_parse_external_declaration;
2752 /* parse function body */
2753 int label_stack_top = label_top();
2754 declaration_t *old_current_function = current_function;
2755 current_function = declaration;
2757 declaration->init.statement = parse_compound_statement();
2758 check_for_missing_labels();
2760 assert(current_function == declaration);
2761 current_function = old_current_function;
2762 label_pop_to(label_stack_top);
2765 end_of_parse_external_declaration:
2766 assert(context == &declaration->context);
2767 set_context(last_context);
2768 environment_pop_to(top);
2771 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2773 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2774 type->bitfield.base = base;
2775 type->bitfield.size = size;
2780 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2782 /* TODO: check constraints for struct declarations (in specifiers) */
2784 declaration_t *declaration;
2786 if(token.type == ':') {
2789 type_t *base_type = specifiers->type;
2790 expression_t *size = parse_constant_expression();
2792 type_t *type = make_bitfield_type(base_type, size);
2794 declaration = allocate_declaration_zero();
2795 declaration->namespc = NAMESPACE_NORMAL;
2796 declaration->storage_class = STORAGE_CLASS_NONE;
2797 declaration->source_position = token.source_position;
2798 declaration->modifiers = specifiers->decl_modifiers;
2799 declaration->type = type;
2801 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2803 if(token.type == ':') {
2805 expression_t *size = parse_constant_expression();
2807 type_t *type = make_bitfield_type(declaration->type, size);
2808 declaration->type = type;
2811 record_declaration(declaration);
2813 if(token.type != ',')
2820 static void parse_compound_type_entries(void)
2824 while(token.type != '}' && token.type != T_EOF) {
2825 declaration_specifiers_t specifiers;
2826 memset(&specifiers, 0, sizeof(specifiers));
2827 parse_declaration_specifiers(&specifiers);
2829 parse_struct_declarators(&specifiers);
2831 if(token.type == T_EOF) {
2832 errorf(HERE, "EOF while parsing struct");
2837 static type_t *parse_typename(void)
2839 declaration_specifiers_t specifiers;
2840 memset(&specifiers, 0, sizeof(specifiers));
2841 parse_declaration_specifiers(&specifiers);
2842 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2843 /* TODO: improve error message, user does probably not know what a
2844 * storage class is...
2846 errorf(HERE, "typename may not have a storage class");
2849 type_t *result = parse_abstract_declarator(specifiers.type);
2857 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2858 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2859 expression_t *left);
2861 typedef struct expression_parser_function_t expression_parser_function_t;
2862 struct expression_parser_function_t {
2863 unsigned precedence;
2864 parse_expression_function parser;
2865 unsigned infix_precedence;
2866 parse_expression_infix_function infix_parser;
2869 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2872 * Creates a new invalid expression.
2874 static expression_t *create_invalid_expression(void)
2876 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2877 expression->base.source_position = token.source_position;
2882 * Prints an error message if an expression was expected but not read
2884 static expression_t *expected_expression_error(void)
2886 /* skip the error message if the error token was read */
2887 if (token.type != T_ERROR) {
2888 errorf(HERE, "expected expression, got token '%K'", &token);
2892 return create_invalid_expression();
2896 * Parse a string constant.
2898 static expression_t *parse_string_const(void)
2900 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2901 cnst->base.datatype = type_string;
2902 cnst->string.value = parse_string_literals();
2908 * Parse a wide string constant.
2910 static expression_t *parse_wide_string_const(void)
2912 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2913 cnst->base.datatype = type_wchar_t_ptr;
2914 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2920 * Parse an integer constant.
2922 static expression_t *parse_int_const(void)
2924 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2925 cnst->base.datatype = token.datatype;
2926 cnst->conste.v.int_value = token.v.intvalue;
2934 * Parse a float constant.
2936 static expression_t *parse_float_const(void)
2938 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2939 cnst->base.datatype = token.datatype;
2940 cnst->conste.v.float_value = token.v.floatvalue;
2947 static declaration_t *create_implicit_function(symbol_t *symbol,
2948 const source_position_t source_position)
2950 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2951 ntype->function.return_type = type_int;
2952 ntype->function.unspecified_parameters = true;
2954 type_t *type = typehash_insert(ntype);
2959 declaration_t *const declaration = allocate_declaration_zero();
2960 declaration->storage_class = STORAGE_CLASS_EXTERN;
2961 declaration->type = type;
2962 declaration->symbol = symbol;
2963 declaration->source_position = source_position;
2964 declaration->parent_context = global_context;
2966 context_t *old_context = context;
2967 set_context(global_context);
2969 environment_push(declaration);
2970 /* prepend the declaration to the global declarations list */
2971 declaration->next = context->declarations;
2972 context->declarations = declaration;
2974 assert(context == global_context);
2975 set_context(old_context);
2981 * Creates a return_type (func)(argument_type) function type if not
2984 * @param return_type the return type
2985 * @param argument_type the argument type
2987 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2989 function_parameter_t *parameter
2990 = obstack_alloc(type_obst, sizeof(parameter[0]));
2991 memset(parameter, 0, sizeof(parameter[0]));
2992 parameter->type = argument_type;
2994 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2995 type->function.return_type = return_type;
2996 type->function.parameters = parameter;
2998 type_t *result = typehash_insert(type);
2999 if(result != type) {
3007 * Creates a function type for some function like builtins.
3009 * @param symbol the symbol describing the builtin
3011 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3013 switch(symbol->ID) {
3014 case T___builtin_alloca:
3015 return make_function_1_type(type_void_ptr, type_size_t);
3016 case T___builtin_nan:
3017 return make_function_1_type(type_double, type_string);
3018 case T___builtin_nanf:
3019 return make_function_1_type(type_float, type_string);
3020 case T___builtin_nand:
3021 return make_function_1_type(type_long_double, type_string);
3022 case T___builtin_va_end:
3023 return make_function_1_type(type_void, type_valist);
3025 panic("not implemented builtin symbol found");
3030 * Performs automatic type cast as described in § 6.3.2.1.
3032 * @param orig_type the original type
3034 static type_t *automatic_type_conversion(type_t *orig_type)
3036 type_t *type = skip_typeref(orig_type);
3037 if(is_type_array(type)) {
3038 array_type_t *array_type = &type->array;
3039 type_t *element_type = array_type->element_type;
3040 unsigned qualifiers = array_type->type.qualifiers;
3042 return make_pointer_type(element_type, qualifiers);
3045 if(is_type_function(type)) {
3046 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3053 * reverts the automatic casts of array to pointer types and function
3054 * to function-pointer types as defined § 6.3.2.1
3056 type_t *revert_automatic_type_conversion(const expression_t *expression)
3058 switch (expression->kind) {
3059 case EXPR_REFERENCE: return expression->reference.declaration->type;
3060 case EXPR_SELECT: return expression->select.compound_entry->type;
3062 case EXPR_UNARY_DEREFERENCE: {
3063 const expression_t *const value = expression->unary.value;
3064 type_t *const type = skip_typeref(value->base.datatype);
3065 assert(is_type_pointer(type));
3066 return type->pointer.points_to;
3069 case EXPR_BUILTIN_SYMBOL:
3070 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3072 case EXPR_ARRAY_ACCESS: {
3073 const expression_t *const array_ref = expression->array_access.array_ref;
3074 type_t *const type_left = skip_typeref(array_ref->base.datatype);
3075 if (!is_type_valid(type_left))
3077 assert(is_type_pointer(type_left));
3078 return type_left->pointer.points_to;
3084 return expression->base.datatype;
3087 static expression_t *parse_reference(void)
3089 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3091 reference_expression_t *ref = &expression->reference;
3092 ref->symbol = token.v.symbol;
3094 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3096 source_position_t source_position = token.source_position;
3099 if(declaration == NULL) {
3100 if (! strict_mode && token.type == '(') {
3101 /* an implicitly defined function */
3102 if (warning.implicit_function_declaration) {
3103 warningf(HERE, "implicit declaration of function '%Y'",
3107 declaration = create_implicit_function(ref->symbol,
3110 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3115 type_t *type = declaration->type;
3117 /* we always do the auto-type conversions; the & and sizeof parser contains
3118 * code to revert this! */
3119 type = automatic_type_conversion(type);
3121 ref->declaration = declaration;
3122 ref->expression.datatype = type;
3127 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3131 /* TODO check if explicit cast is allowed and issue warnings/errors */
3134 static expression_t *parse_cast(void)
3136 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3138 cast->base.source_position = token.source_position;
3140 type_t *type = parse_typename();
3143 expression_t *value = parse_sub_expression(20);
3145 check_cast_allowed(value, type);
3147 cast->base.datatype = type;
3148 cast->unary.value = value;
3153 static expression_t *parse_statement_expression(void)
3155 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3157 statement_t *statement = parse_compound_statement();
3158 expression->statement.statement = statement;
3159 expression->base.source_position = statement->base.source_position;
3161 /* find last statement and use its type */
3162 type_t *type = type_void;
3163 const statement_t *stmt = statement->compound.statements;
3165 while (stmt->base.next != NULL)
3166 stmt = stmt->base.next;
3168 if (stmt->kind == STATEMENT_EXPRESSION) {
3169 type = stmt->expression.expression->base.datatype;
3172 warningf(expression->base.source_position, "empty statement expression ({})");
3174 expression->base.datatype = type;
3181 static expression_t *parse_brace_expression(void)
3185 switch(token.type) {
3187 /* gcc extension: a statement expression */
3188 return parse_statement_expression();
3192 return parse_cast();
3194 if(is_typedef_symbol(token.v.symbol)) {
3195 return parse_cast();
3199 expression_t *result = parse_expression();
3205 static expression_t *parse_function_keyword(void)
3210 if (current_function == NULL) {
3211 errorf(HERE, "'__func__' used outside of a function");
3214 string_literal_expression_t *expression
3215 = allocate_ast_zero(sizeof(expression[0]));
3217 expression->expression.kind = EXPR_FUNCTION;
3218 expression->expression.datatype = type_string;
3220 return (expression_t*) expression;
3223 static expression_t *parse_pretty_function_keyword(void)
3225 eat(T___PRETTY_FUNCTION__);
3228 if (current_function == NULL) {
3229 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3232 string_literal_expression_t *expression
3233 = allocate_ast_zero(sizeof(expression[0]));
3235 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3236 expression->expression.datatype = type_string;
3238 return (expression_t*) expression;
3241 static designator_t *parse_designator(void)
3243 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3245 if(token.type != T_IDENTIFIER) {
3246 parse_error_expected("while parsing member designator",
3251 result->symbol = token.v.symbol;
3254 designator_t *last_designator = result;
3256 if(token.type == '.') {
3258 if(token.type != T_IDENTIFIER) {
3259 parse_error_expected("while parsing member designator",
3264 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3265 designator->symbol = token.v.symbol;
3268 last_designator->next = designator;
3269 last_designator = designator;
3272 if(token.type == '[') {
3274 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3275 designator->array_access = parse_expression();
3276 if(designator->array_access == NULL) {
3282 last_designator->next = designator;
3283 last_designator = designator;
3292 static expression_t *parse_offsetof(void)
3294 eat(T___builtin_offsetof);
3296 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3297 expression->base.datatype = type_size_t;
3300 expression->offsetofe.type = parse_typename();
3302 expression->offsetofe.designator = parse_designator();
3308 static expression_t *parse_va_start(void)
3310 eat(T___builtin_va_start);
3312 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3315 expression->va_starte.ap = parse_assignment_expression();
3317 expression_t *const expr = parse_assignment_expression();
3318 if (expr->kind == EXPR_REFERENCE) {
3319 declaration_t *const decl = expr->reference.declaration;
3320 if (decl->parent_context == ¤t_function->context &&
3321 decl->next == NULL) {
3322 expression->va_starte.parameter = decl;
3327 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3329 return create_invalid_expression();
3332 static expression_t *parse_va_arg(void)
3334 eat(T___builtin_va_arg);
3336 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3339 expression->va_arge.ap = parse_assignment_expression();
3341 expression->base.datatype = parse_typename();
3347 static expression_t *parse_builtin_symbol(void)
3349 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3351 symbol_t *symbol = token.v.symbol;
3353 expression->builtin_symbol.symbol = symbol;
3356 type_t *type = get_builtin_symbol_type(symbol);
3357 type = automatic_type_conversion(type);
3359 expression->base.datatype = type;
3363 static expression_t *parse_builtin_constant(void)
3365 eat(T___builtin_constant_p);
3367 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3370 expression->builtin_constant.value = parse_assignment_expression();
3372 expression->base.datatype = type_int;
3377 static expression_t *parse_builtin_prefetch(void)
3379 eat(T___builtin_prefetch);
3381 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3384 expression->builtin_prefetch.adr = parse_assignment_expression();
3385 if (token.type == ',') {
3387 expression->builtin_prefetch.rw = parse_assignment_expression();
3389 if (token.type == ',') {
3391 expression->builtin_prefetch.locality = parse_assignment_expression();
3394 expression->base.datatype = type_void;
3399 static expression_t *parse_compare_builtin(void)
3401 expression_t *expression;
3403 switch(token.type) {
3404 case T___builtin_isgreater:
3405 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3407 case T___builtin_isgreaterequal:
3408 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3410 case T___builtin_isless:
3411 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3413 case T___builtin_islessequal:
3414 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3416 case T___builtin_islessgreater:
3417 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3419 case T___builtin_isunordered:
3420 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3423 panic("invalid compare builtin found");
3429 expression->binary.left = parse_assignment_expression();
3431 expression->binary.right = parse_assignment_expression();
3434 type_t *const orig_type_left = expression->binary.left->base.datatype;
3435 type_t *const orig_type_right = expression->binary.right->base.datatype;
3437 type_t *const type_left = skip_typeref(orig_type_left);
3438 type_t *const type_right = skip_typeref(orig_type_right);
3439 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3440 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3441 type_error_incompatible("invalid operands in comparison",
3442 token.source_position, orig_type_left, orig_type_right);
3445 semantic_comparison(&expression->binary);
3451 static expression_t *parse_builtin_expect(void)
3453 eat(T___builtin_expect);
3455 expression_t *expression
3456 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3459 expression->binary.left = parse_assignment_expression();
3461 expression->binary.right = parse_constant_expression();
3464 expression->base.datatype = expression->binary.left->base.datatype;
3469 static expression_t *parse_assume(void) {
3472 expression_t *expression
3473 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3476 expression->unary.value = parse_assignment_expression();
3479 expression->base.datatype = type_void;
3483 static expression_t *parse_alignof(void) {
3486 expression_t *expression
3487 = allocate_expression_zero(EXPR_ALIGNOF);
3490 expression->alignofe.type = parse_typename();
3493 expression->base.datatype = type_size_t;
3497 static expression_t *parse_primary_expression(void)
3499 switch(token.type) {
3501 return parse_int_const();
3502 case T_FLOATINGPOINT:
3503 return parse_float_const();
3504 case T_STRING_LITERAL:
3505 return parse_string_const();
3506 case T_WIDE_STRING_LITERAL:
3507 return parse_wide_string_const();
3509 return parse_reference();
3510 case T___FUNCTION__:
3512 return parse_function_keyword();
3513 case T___PRETTY_FUNCTION__:
3514 return parse_pretty_function_keyword();
3515 case T___builtin_offsetof:
3516 return parse_offsetof();
3517 case T___builtin_va_start:
3518 return parse_va_start();
3519 case T___builtin_va_arg:
3520 return parse_va_arg();
3521 case T___builtin_expect:
3522 return parse_builtin_expect();
3523 case T___builtin_nanf:
3524 case T___builtin_alloca:
3525 case T___builtin_va_end:
3526 return parse_builtin_symbol();
3527 case T___builtin_isgreater:
3528 case T___builtin_isgreaterequal:
3529 case T___builtin_isless:
3530 case T___builtin_islessequal:
3531 case T___builtin_islessgreater:
3532 case T___builtin_isunordered:
3533 return parse_compare_builtin();
3534 case T___builtin_constant_p:
3535 return parse_builtin_constant();
3536 case T___builtin_prefetch:
3537 return parse_builtin_prefetch();
3539 return parse_alignof();
3541 return parse_assume();
3544 return parse_brace_expression();
3547 errorf(HERE, "unexpected token '%K'", &token);
3550 return create_invalid_expression();
3554 * Check if the expression has the character type and issue a warning then.
3556 static void check_for_char_index_type(const expression_t *expression) {
3557 type_t *const type = expression->base.datatype;
3558 const type_t *const base_type = skip_typeref(type);
3560 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
3561 warning.char_subscripts) {
3562 warningf(expression->base.source_position,
3563 "array subscript has type '%T'", type);
3567 static expression_t *parse_array_expression(unsigned precedence,
3574 expression_t *inside = parse_expression();
3576 array_access_expression_t *array_access
3577 = allocate_ast_zero(sizeof(array_access[0]));
3579 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3581 type_t *const orig_type_left = left->base.datatype;
3582 type_t *const orig_type_inside = inside->base.datatype;
3584 type_t *const type_left = skip_typeref(orig_type_left);
3585 type_t *const type_inside = skip_typeref(orig_type_inside);
3587 type_t *return_type;
3588 if (is_type_pointer(type_left)) {
3589 return_type = type_left->pointer.points_to;
3590 array_access->array_ref = left;
3591 array_access->index = inside;
3592 check_for_char_index_type(inside);
3593 } else if (is_type_pointer(type_inside)) {
3594 return_type = type_inside->pointer.points_to;
3595 array_access->array_ref = inside;
3596 array_access->index = left;
3597 array_access->flipped = true;
3598 check_for_char_index_type(left);
3600 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3602 "array access on object with non-pointer types '%T', '%T'",
3603 orig_type_left, orig_type_inside);
3605 return_type = type_error_type;
3606 array_access->array_ref = create_invalid_expression();
3609 if(token.type != ']') {
3610 parse_error_expected("Problem while parsing array access", ']', 0);
3611 return (expression_t*) array_access;
3615 return_type = automatic_type_conversion(return_type);
3616 array_access->expression.datatype = return_type;
3618 return (expression_t*) array_access;
3621 static expression_t *parse_sizeof(unsigned precedence)
3625 sizeof_expression_t *sizeof_expression
3626 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3627 sizeof_expression->expression.kind = EXPR_SIZEOF;
3628 sizeof_expression->expression.datatype = type_size_t;
3630 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3632 sizeof_expression->type = parse_typename();
3635 expression_t *expression = parse_sub_expression(precedence);
3636 expression->base.datatype = revert_automatic_type_conversion(expression);
3638 sizeof_expression->type = expression->base.datatype;
3639 sizeof_expression->size_expression = expression;
3642 return (expression_t*) sizeof_expression;
3645 static expression_t *parse_select_expression(unsigned precedence,
3646 expression_t *compound)
3649 assert(token.type == '.' || token.type == T_MINUSGREATER);
3651 bool is_pointer = (token.type == T_MINUSGREATER);
3654 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3655 select->select.compound = compound;
3657 if(token.type != T_IDENTIFIER) {
3658 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3661 symbol_t *symbol = token.v.symbol;
3662 select->select.symbol = symbol;
3665 type_t *const orig_type = compound->base.datatype;
3666 type_t *const type = skip_typeref(orig_type);
3668 type_t *type_left = type;
3670 if (!is_type_pointer(type)) {
3671 if (is_type_valid(type)) {
3672 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3674 return create_invalid_expression();
3676 type_left = type->pointer.points_to;
3678 type_left = skip_typeref(type_left);
3680 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3681 type_left->kind != TYPE_COMPOUND_UNION) {
3682 if (is_type_valid(type_left)) {
3683 errorf(HERE, "request for member '%Y' in something not a struct or "
3684 "union, but '%T'", symbol, type_left);
3686 return create_invalid_expression();
3689 declaration_t *const declaration = type_left->compound.declaration;
3691 if(!declaration->init.is_defined) {
3692 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3694 return create_invalid_expression();
3697 declaration_t *iter = declaration->context.declarations;
3698 for( ; iter != NULL; iter = iter->next) {
3699 if(iter->symbol == symbol) {
3704 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3705 return create_invalid_expression();
3708 /* we always do the auto-type conversions; the & and sizeof parser contains
3709 * code to revert this! */
3710 type_t *expression_type = automatic_type_conversion(iter->type);
3712 select->select.compound_entry = iter;
3713 select->base.datatype = expression_type;
3715 if(expression_type->kind == TYPE_BITFIELD) {
3716 expression_t *extract
3717 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3718 extract->unary.value = select;
3719 extract->base.datatype = expression_type->bitfield.base;
3728 * Parse a call expression, ie. expression '( ... )'.
3730 * @param expression the function address
3732 static expression_t *parse_call_expression(unsigned precedence,
3733 expression_t *expression)
3736 expression_t *result = allocate_expression_zero(EXPR_CALL);
3738 call_expression_t *call = &result->call;
3739 call->function = expression;
3741 type_t *const orig_type = expression->base.datatype;
3742 type_t *const type = skip_typeref(orig_type);
3744 function_type_t *function_type = NULL;
3745 if (is_type_pointer(type)) {
3746 type_t *const to_type = skip_typeref(type->pointer.points_to);
3748 if (is_type_function(to_type)) {
3749 function_type = &to_type->function;
3750 call->expression.datatype = function_type->return_type;
3754 if (function_type == NULL && is_type_valid(type)) {
3755 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3758 /* parse arguments */
3761 if(token.type != ')') {
3762 call_argument_t *last_argument = NULL;
3765 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3767 argument->expression = parse_assignment_expression();
3768 if(last_argument == NULL) {
3769 call->arguments = argument;
3771 last_argument->next = argument;
3773 last_argument = argument;
3775 if(token.type != ',')
3782 if(function_type != NULL) {
3783 function_parameter_t *parameter = function_type->parameters;
3784 call_argument_t *argument = call->arguments;
3785 for( ; parameter != NULL && argument != NULL;
3786 parameter = parameter->next, argument = argument->next) {
3787 type_t *expected_type = parameter->type;
3788 /* TODO report context in error messages */
3789 expression_t *const arg_expr = argument->expression;
3790 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
3791 if (res_type == NULL) {
3792 /* TODO improve error message */
3793 errorf(arg_expr->base.source_position,
3794 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
3795 arg_expr, arg_expr->base.datatype, expected_type);
3797 argument->expression = create_implicit_cast(argument->expression, expected_type);
3800 /* too few parameters */
3801 if(parameter != NULL) {
3802 errorf(HERE, "too few arguments to function '%E'", expression);
3803 } else if(argument != NULL) {
3804 /* too many parameters */
3805 if(!function_type->variadic
3806 && !function_type->unspecified_parameters) {
3807 errorf(HERE, "too many arguments to function '%E'", expression);
3809 /* do default promotion */
3810 for( ; argument != NULL; argument = argument->next) {
3811 type_t *type = argument->expression->base.datatype;
3813 type = skip_typeref(type);
3814 if(is_type_integer(type)) {
3815 type = promote_integer(type);
3816 } else if(type == type_float) {
3820 argument->expression
3821 = create_implicit_cast(argument->expression, type);
3824 check_format(&result->call);
3827 check_format(&result->call);
3834 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3836 static bool same_compound_type(const type_t *type1, const type_t *type2)
3839 is_type_compound(type1) &&
3840 type1->kind == type2->kind &&
3841 type1->compound.declaration == type2->compound.declaration;
3845 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3847 * @param expression the conditional expression
3849 static expression_t *parse_conditional_expression(unsigned precedence,
3850 expression_t *expression)
3854 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3856 conditional_expression_t *conditional = &result->conditional;
3857 conditional->condition = expression;
3860 type_t *const condition_type_orig = expression->base.datatype;
3861 type_t *const condition_type = skip_typeref(condition_type_orig);
3862 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
3863 type_error("expected a scalar type in conditional condition",
3864 expression->base.source_position, condition_type_orig);
3867 expression_t *true_expression = parse_expression();
3869 expression_t *false_expression = parse_sub_expression(precedence);
3871 conditional->true_expression = true_expression;
3872 conditional->false_expression = false_expression;
3874 type_t *const orig_true_type = true_expression->base.datatype;
3875 type_t *const orig_false_type = false_expression->base.datatype;
3876 type_t *const true_type = skip_typeref(orig_true_type);
3877 type_t *const false_type = skip_typeref(orig_false_type);
3880 type_t *result_type;
3881 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3882 result_type = semantic_arithmetic(true_type, false_type);
3884 true_expression = create_implicit_cast(true_expression, result_type);
3885 false_expression = create_implicit_cast(false_expression, result_type);
3887 conditional->true_expression = true_expression;
3888 conditional->false_expression = false_expression;
3889 conditional->expression.datatype = result_type;
3890 } else if (same_compound_type(true_type, false_type) || (
3891 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3892 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
3894 /* just take 1 of the 2 types */
3895 result_type = true_type;
3896 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3897 && pointers_compatible(true_type, false_type)) {
3899 result_type = true_type;
3902 if (is_type_valid(true_type) && is_type_valid(false_type)) {
3903 type_error_incompatible("while parsing conditional",
3904 expression->base.source_position, true_type,
3907 result_type = type_error_type;
3910 conditional->expression.datatype = result_type;
3915 * Parse an extension expression.
3917 static expression_t *parse_extension(unsigned precedence)
3919 eat(T___extension__);
3921 /* TODO enable extensions */
3922 expression_t *expression = parse_sub_expression(precedence);
3923 /* TODO disable extensions */
3927 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3929 eat(T___builtin_classify_type);
3931 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3932 result->base.datatype = type_int;
3935 expression_t *expression = parse_sub_expression(precedence);
3937 result->classify_type.type_expression = expression;
3942 static void semantic_incdec(unary_expression_t *expression)
3944 type_t *const orig_type = expression->value->base.datatype;
3945 type_t *const type = skip_typeref(orig_type);
3946 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3947 if (is_type_valid(type)) {
3948 /* TODO: improve error message */
3949 errorf(HERE, "operation needs an arithmetic or pointer type");
3954 expression->expression.datatype = orig_type;
3957 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3959 type_t *const orig_type = expression->value->base.datatype;
3960 type_t *const type = skip_typeref(orig_type);
3961 if(!is_type_arithmetic(type)) {
3962 if (is_type_valid(type)) {
3963 /* TODO: improve error message */
3964 errorf(HERE, "operation needs an arithmetic type");
3969 expression->expression.datatype = orig_type;
3972 static void semantic_unexpr_scalar(unary_expression_t *expression)
3974 type_t *const orig_type = expression->value->base.datatype;
3975 type_t *const type = skip_typeref(orig_type);
3976 if (!is_type_scalar(type)) {
3977 if (is_type_valid(type)) {
3978 errorf(HERE, "operand of ! must be of scalar type");
3983 expression->expression.datatype = orig_type;
3986 static void semantic_unexpr_integer(unary_expression_t *expression)
3988 type_t *const orig_type = expression->value->base.datatype;
3989 type_t *const type = skip_typeref(orig_type);
3990 if (!is_type_integer(type)) {
3991 if (is_type_valid(type)) {
3992 errorf(HERE, "operand of ~ must be of integer type");
3997 expression->expression.datatype = orig_type;
4000 static void semantic_dereference(unary_expression_t *expression)
4002 type_t *const orig_type = expression->value->base.datatype;
4003 type_t *const type = skip_typeref(orig_type);
4004 if(!is_type_pointer(type)) {
4005 if (is_type_valid(type)) {
4006 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4011 type_t *result_type = type->pointer.points_to;
4012 result_type = automatic_type_conversion(result_type);
4013 expression->expression.datatype = result_type;
4017 * Check the semantic of the address taken expression.
4019 static void semantic_take_addr(unary_expression_t *expression)
4021 expression_t *value = expression->value;
4022 value->base.datatype = revert_automatic_type_conversion(value);
4024 type_t *orig_type = value->base.datatype;
4025 if(!is_type_valid(orig_type))
4028 if(value->kind == EXPR_REFERENCE) {
4029 declaration_t *const declaration = value->reference.declaration;
4030 if(declaration != NULL) {
4031 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4032 errorf(expression->expression.source_position,
4033 "address of register variable '%Y' requested",
4034 declaration->symbol);
4036 declaration->address_taken = 1;
4040 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4043 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4044 static expression_t *parse_##unexpression_type(unsigned precedence) \
4048 expression_t *unary_expression \
4049 = allocate_expression_zero(unexpression_type); \
4050 unary_expression->base.source_position = HERE; \
4051 unary_expression->unary.value = parse_sub_expression(precedence); \
4053 sfunc(&unary_expression->unary); \
4055 return unary_expression; \
4058 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4059 semantic_unexpr_arithmetic)
4060 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4061 semantic_unexpr_arithmetic)
4062 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4063 semantic_unexpr_scalar)
4064 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4065 semantic_dereference)
4066 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4068 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4069 semantic_unexpr_integer)
4070 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4072 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4075 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4077 static expression_t *parse_##unexpression_type(unsigned precedence, \
4078 expression_t *left) \
4080 (void) precedence; \
4083 expression_t *unary_expression \
4084 = allocate_expression_zero(unexpression_type); \
4085 unary_expression->unary.value = left; \
4087 sfunc(&unary_expression->unary); \
4089 return unary_expression; \
4092 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4093 EXPR_UNARY_POSTFIX_INCREMENT,
4095 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4096 EXPR_UNARY_POSTFIX_DECREMENT,
4099 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4101 /* TODO: handle complex + imaginary types */
4103 /* § 6.3.1.8 Usual arithmetic conversions */
4104 if(type_left == type_long_double || type_right == type_long_double) {
4105 return type_long_double;
4106 } else if(type_left == type_double || type_right == type_double) {
4108 } else if(type_left == type_float || type_right == type_float) {
4112 type_right = promote_integer(type_right);
4113 type_left = promote_integer(type_left);
4115 if(type_left == type_right)
4118 bool signed_left = is_type_signed(type_left);
4119 bool signed_right = is_type_signed(type_right);
4120 int rank_left = get_rank(type_left);
4121 int rank_right = get_rank(type_right);
4122 if(rank_left < rank_right) {
4123 if(signed_left == signed_right || !signed_right) {
4129 if(signed_left == signed_right || !signed_left) {
4138 * Check the semantic restrictions for a binary expression.
4140 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4142 expression_t *const left = expression->left;
4143 expression_t *const right = expression->right;
4144 type_t *const orig_type_left = left->base.datatype;
4145 type_t *const orig_type_right = right->base.datatype;
4146 type_t *const type_left = skip_typeref(orig_type_left);
4147 type_t *const type_right = skip_typeref(orig_type_right);
4149 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4150 /* TODO: improve error message */
4151 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4152 errorf(HERE, "operation needs arithmetic types");
4157 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4158 expression->left = create_implicit_cast(left, arithmetic_type);
4159 expression->right = create_implicit_cast(right, arithmetic_type);
4160 expression->expression.datatype = arithmetic_type;
4163 static void semantic_shift_op(binary_expression_t *expression)
4165 expression_t *const left = expression->left;
4166 expression_t *const right = expression->right;
4167 type_t *const orig_type_left = left->base.datatype;
4168 type_t *const orig_type_right = right->base.datatype;
4169 type_t * type_left = skip_typeref(orig_type_left);
4170 type_t * type_right = skip_typeref(orig_type_right);
4172 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4173 /* TODO: improve error message */
4174 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4175 errorf(HERE, "operation needs integer types");
4180 type_left = promote_integer(type_left);
4181 type_right = promote_integer(type_right);
4183 expression->left = create_implicit_cast(left, type_left);
4184 expression->right = create_implicit_cast(right, type_right);
4185 expression->expression.datatype = type_left;
4188 static void semantic_add(binary_expression_t *expression)
4190 expression_t *const left = expression->left;
4191 expression_t *const right = expression->right;
4192 type_t *const orig_type_left = left->base.datatype;
4193 type_t *const orig_type_right = right->base.datatype;
4194 type_t *const type_left = skip_typeref(orig_type_left);
4195 type_t *const type_right = skip_typeref(orig_type_right);
4198 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4199 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4200 expression->left = create_implicit_cast(left, arithmetic_type);
4201 expression->right = create_implicit_cast(right, arithmetic_type);
4202 expression->expression.datatype = arithmetic_type;
4204 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4205 expression->expression.datatype = type_left;
4206 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4207 expression->expression.datatype = type_right;
4208 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4209 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4213 static void semantic_sub(binary_expression_t *expression)
4215 expression_t *const left = expression->left;
4216 expression_t *const right = expression->right;
4217 type_t *const orig_type_left = left->base.datatype;
4218 type_t *const orig_type_right = right->base.datatype;
4219 type_t *const type_left = skip_typeref(orig_type_left);
4220 type_t *const type_right = skip_typeref(orig_type_right);
4223 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4224 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4225 expression->left = create_implicit_cast(left, arithmetic_type);
4226 expression->right = create_implicit_cast(right, arithmetic_type);
4227 expression->expression.datatype = arithmetic_type;
4229 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4230 expression->expression.datatype = type_left;
4231 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4232 if(!pointers_compatible(type_left, type_right)) {
4233 errorf(HERE, "pointers to incompatible objects to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4235 expression->expression.datatype = type_ptrdiff_t;
4237 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4238 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4242 static void semantic_comparison(binary_expression_t *expression)
4244 expression_t *left = expression->left;
4245 expression_t *right = expression->right;
4246 type_t *orig_type_left = left->base.datatype;
4247 type_t *orig_type_right = right->base.datatype;
4249 type_t *type_left = skip_typeref(orig_type_left);
4250 type_t *type_right = skip_typeref(orig_type_right);
4252 /* TODO non-arithmetic types */
4253 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4254 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4255 expression->left = create_implicit_cast(left, arithmetic_type);
4256 expression->right = create_implicit_cast(right, arithmetic_type);
4257 expression->expression.datatype = arithmetic_type;
4258 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4259 /* TODO check compatibility */
4260 } else if (is_type_pointer(type_left)) {
4261 expression->right = create_implicit_cast(right, type_left);
4262 } else if (is_type_pointer(type_right)) {
4263 expression->left = create_implicit_cast(left, type_right);
4264 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4265 type_error_incompatible("invalid operands in comparison",
4266 token.source_position, type_left, type_right);
4268 expression->expression.datatype = type_int;
4271 static void semantic_arithmetic_assign(binary_expression_t *expression)
4273 expression_t *left = expression->left;
4274 expression_t *right = expression->right;
4275 type_t *orig_type_left = left->base.datatype;
4276 type_t *orig_type_right = right->base.datatype;
4278 type_t *type_left = skip_typeref(orig_type_left);
4279 type_t *type_right = skip_typeref(orig_type_right);
4281 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4282 /* TODO: improve error message */
4283 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4284 errorf(HERE, "operation needs arithmetic types");
4289 /* combined instructions are tricky. We can't create an implicit cast on
4290 * the left side, because we need the uncasted form for the store.
4291 * The ast2firm pass has to know that left_type must be right_type
4292 * for the arithmetic operation and create a cast by itself */
4293 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4294 expression->right = create_implicit_cast(right, arithmetic_type);
4295 expression->expression.datatype = type_left;
4298 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4300 expression_t *const left = expression->left;
4301 expression_t *const right = expression->right;
4302 type_t *const orig_type_left = left->base.datatype;
4303 type_t *const orig_type_right = right->base.datatype;
4304 type_t *const type_left = skip_typeref(orig_type_left);
4305 type_t *const type_right = skip_typeref(orig_type_right);
4307 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4308 /* combined instructions are tricky. We can't create an implicit cast on
4309 * the left side, because we need the uncasted form for the store.
4310 * The ast2firm pass has to know that left_type must be right_type
4311 * for the arithmetic operation and create a cast by itself */
4312 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4313 expression->right = create_implicit_cast(right, arithmetic_type);
4314 expression->expression.datatype = type_left;
4315 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4316 expression->expression.datatype = type_left;
4317 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4318 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4323 * Check the semantic restrictions of a logical expression.
4325 static void semantic_logical_op(binary_expression_t *expression)
4327 expression_t *const left = expression->left;
4328 expression_t *const right = expression->right;
4329 type_t *const orig_type_left = left->base.datatype;
4330 type_t *const orig_type_right = right->base.datatype;
4331 type_t *const type_left = skip_typeref(orig_type_left);
4332 type_t *const type_right = skip_typeref(orig_type_right);
4334 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4335 /* TODO: improve error message */
4336 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4337 errorf(HERE, "operation needs scalar types");
4342 expression->expression.datatype = type_int;
4346 * Checks if a compound type has constant fields.
4348 static bool has_const_fields(const compound_type_t *type)
4350 const context_t *context = &type->declaration->context;
4351 const declaration_t *declaration = context->declarations;
4353 for (; declaration != NULL; declaration = declaration->next) {
4354 if (declaration->namespc != NAMESPACE_NORMAL)
4357 const type_t *decl_type = skip_typeref(declaration->type);
4358 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4366 * Check the semantic restrictions of a binary assign expression.
4368 static void semantic_binexpr_assign(binary_expression_t *expression)
4370 expression_t *left = expression->left;
4371 type_t *orig_type_left = left->base.datatype;
4373 type_t *type_left = revert_automatic_type_conversion(left);
4374 type_left = skip_typeref(orig_type_left);
4376 /* must be a modifiable lvalue */
4377 if (is_type_array(type_left)) {
4378 errorf(HERE, "cannot assign to arrays ('%E')", left);
4381 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4382 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4386 if(is_type_incomplete(type_left)) {
4388 "left-hand side of assignment '%E' has incomplete type '%T'",
4389 left, orig_type_left);
4392 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4393 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4394 left, orig_type_left);
4398 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4400 if (res_type == NULL) {
4401 errorf(expression->expression.source_position,
4402 "cannot assign to '%T' from '%T'",
4403 orig_type_left, expression->right->base.datatype);
4405 expression->right = create_implicit_cast(expression->right, res_type);
4408 expression->expression.datatype = orig_type_left;
4411 static bool expression_has_effect(const expression_t *const expr)
4413 switch (expr->kind) {
4414 case EXPR_UNKNOWN: break;
4415 case EXPR_INVALID: break;
4416 case EXPR_REFERENCE: return false;
4417 case EXPR_CONST: return false;
4418 case EXPR_STRING_LITERAL: return false;
4419 case EXPR_WIDE_STRING_LITERAL: return false;
4421 const call_expression_t *const call = &expr->call;
4422 if (call->function->kind == EXPR_BUILTIN_SYMBOL) {
4423 switch (call->function->builtin_symbol.symbol->ID) {
4424 case T___builtin_va_end: return true;
4425 default: return false;
4429 case EXPR_CONDITIONAL: {
4430 const conditional_expression_t *const cond = &expr->conditional;
4432 expression_has_effect(cond->true_expression) &&
4433 expression_has_effect(cond->false_expression);
4435 case EXPR_SELECT: return false;
4436 case EXPR_ARRAY_ACCESS: return false;
4437 case EXPR_SIZEOF: return false;
4438 case EXPR_CLASSIFY_TYPE: return false;
4439 case EXPR_ALIGNOF: return false;
4441 case EXPR_FUNCTION: return false;
4442 case EXPR_PRETTY_FUNCTION: return false;
4443 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
4444 case EXPR_BUILTIN_CONSTANT_P: return false;
4445 case EXPR_BUILTIN_PREFETCH: return true;
4446 case EXPR_OFFSETOF: return false;
4447 case EXPR_VA_START: return true;
4448 case EXPR_VA_ARG: return true;
4449 case EXPR_STATEMENT: return true; // TODO
4451 case EXPR_UNARY_NEGATE: return false;
4452 case EXPR_UNARY_PLUS: return false;
4453 case EXPR_UNARY_BITWISE_NEGATE: return false;
4454 case EXPR_UNARY_NOT: return false;
4455 case EXPR_UNARY_DEREFERENCE: return false;
4456 case EXPR_UNARY_TAKE_ADDRESS: return false;
4457 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
4458 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
4459 case EXPR_UNARY_PREFIX_INCREMENT: return true;
4460 case EXPR_UNARY_PREFIX_DECREMENT: return true;
4461 case EXPR_UNARY_CAST:
4462 return is_type_atomic(expr->base.datatype, ATOMIC_TYPE_VOID);
4463 case EXPR_UNARY_CAST_IMPLICIT: return true;
4464 case EXPR_UNARY_ASSUME: return true;
4465 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
4467 case EXPR_BINARY_ADD: return false;
4468 case EXPR_BINARY_SUB: return false;
4469 case EXPR_BINARY_MUL: return false;
4470 case EXPR_BINARY_DIV: return false;
4471 case EXPR_BINARY_MOD: return false;
4472 case EXPR_BINARY_EQUAL: return false;
4473 case EXPR_BINARY_NOTEQUAL: return false;
4474 case EXPR_BINARY_LESS: return false;
4475 case EXPR_BINARY_LESSEQUAL: return false;
4476 case EXPR_BINARY_GREATER: return false;
4477 case EXPR_BINARY_GREATEREQUAL: return false;
4478 case EXPR_BINARY_BITWISE_AND: return false;
4479 case EXPR_BINARY_BITWISE_OR: return false;
4480 case EXPR_BINARY_BITWISE_XOR: return false;
4481 case EXPR_BINARY_LOGICAL_AND: return false;
4482 case EXPR_BINARY_LOGICAL_OR: return false;
4483 case EXPR_BINARY_SHIFTLEFT: return false;
4484 case EXPR_BINARY_SHIFTRIGHT: return false;
4485 case EXPR_BINARY_ASSIGN: return true;
4486 case EXPR_BINARY_MUL_ASSIGN: return true;
4487 case EXPR_BINARY_DIV_ASSIGN: return true;
4488 case EXPR_BINARY_MOD_ASSIGN: return true;
4489 case EXPR_BINARY_ADD_ASSIGN: return true;
4490 case EXPR_BINARY_SUB_ASSIGN: return true;
4491 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
4492 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
4493 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
4494 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
4495 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
4496 case EXPR_BINARY_COMMA:
4497 return expression_has_effect(expr->binary.right);
4499 case EXPR_BINARY_BUILTIN_EXPECT: return true;
4500 case EXPR_BINARY_ISGREATER: return false;
4501 case EXPR_BINARY_ISGREATEREQUAL: return false;
4502 case EXPR_BINARY_ISLESS: return false;
4503 case EXPR_BINARY_ISLESSEQUAL: return false;
4504 case EXPR_BINARY_ISLESSGREATER: return false;
4505 case EXPR_BINARY_ISUNORDERED: return false;
4508 panic("unexpected statement");
4511 static void semantic_comma(binary_expression_t *expression)
4513 if (warning.unused_value) {
4514 const expression_t *const left = expression->left;
4515 if (!expression_has_effect(left)) {
4516 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
4519 expression->expression.datatype = expression->right->base.datatype;
4522 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4523 static expression_t *parse_##binexpression_type(unsigned precedence, \
4524 expression_t *left) \
4528 expression_t *right = parse_sub_expression(precedence + lr); \
4530 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4531 binexpr->binary.left = left; \
4532 binexpr->binary.right = right; \
4533 sfunc(&binexpr->binary); \
4538 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4539 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4540 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4541 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4542 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4543 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4544 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4545 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4546 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4548 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4549 semantic_comparison, 1)
4550 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4551 semantic_comparison, 1)
4552 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4553 semantic_comparison, 1)
4554 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4555 semantic_comparison, 1)
4557 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4558 semantic_binexpr_arithmetic, 1)
4559 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4560 semantic_binexpr_arithmetic, 1)
4561 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4562 semantic_binexpr_arithmetic, 1)
4563 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4564 semantic_logical_op, 1)
4565 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4566 semantic_logical_op, 1)
4567 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4568 semantic_shift_op, 1)
4569 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4570 semantic_shift_op, 1)
4571 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4572 semantic_arithmetic_addsubb_assign, 0)
4573 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4574 semantic_arithmetic_addsubb_assign, 0)
4575 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4576 semantic_arithmetic_assign, 0)
4577 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4578 semantic_arithmetic_assign, 0)
4579 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4580 semantic_arithmetic_assign, 0)
4581 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4582 semantic_arithmetic_assign, 0)
4583 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4584 semantic_arithmetic_assign, 0)
4585 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4586 semantic_arithmetic_assign, 0)
4587 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4588 semantic_arithmetic_assign, 0)
4589 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4590 semantic_arithmetic_assign, 0)
4592 static expression_t *parse_sub_expression(unsigned precedence)
4594 if(token.type < 0) {
4595 return expected_expression_error();
4598 expression_parser_function_t *parser
4599 = &expression_parsers[token.type];
4600 source_position_t source_position = token.source_position;
4603 if(parser->parser != NULL) {
4604 left = parser->parser(parser->precedence);
4606 left = parse_primary_expression();
4608 assert(left != NULL);
4609 left->base.source_position = source_position;
4612 if(token.type < 0) {
4613 return expected_expression_error();
4616 parser = &expression_parsers[token.type];
4617 if(parser->infix_parser == NULL)
4619 if(parser->infix_precedence < precedence)
4622 left = parser->infix_parser(parser->infix_precedence, left);
4624 assert(left != NULL);
4625 assert(left->kind != EXPR_UNKNOWN);
4626 left->base.source_position = source_position;
4633 * Parse an expression.
4635 static expression_t *parse_expression(void)
4637 return parse_sub_expression(1);
4641 * Register a parser for a prefix-like operator with given precedence.
4643 * @param parser the parser function
4644 * @param token_type the token type of the prefix token
4645 * @param precedence the precedence of the operator
4647 static void register_expression_parser(parse_expression_function parser,
4648 int token_type, unsigned precedence)
4650 expression_parser_function_t *entry = &expression_parsers[token_type];
4652 if(entry->parser != NULL) {
4653 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4654 panic("trying to register multiple expression parsers for a token");
4656 entry->parser = parser;
4657 entry->precedence = precedence;
4661 * Register a parser for an infix operator with given precedence.
4663 * @param parser the parser function
4664 * @param token_type the token type of the infix operator
4665 * @param precedence the precedence of the operator
4667 static void register_infix_parser(parse_expression_infix_function parser,
4668 int token_type, unsigned precedence)
4670 expression_parser_function_t *entry = &expression_parsers[token_type];
4672 if(entry->infix_parser != NULL) {
4673 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4674 panic("trying to register multiple infix expression parsers for a "
4677 entry->infix_parser = parser;
4678 entry->infix_precedence = precedence;
4682 * Initialize the expression parsers.
4684 static void init_expression_parsers(void)
4686 memset(&expression_parsers, 0, sizeof(expression_parsers));
4688 register_infix_parser(parse_array_expression, '[', 30);
4689 register_infix_parser(parse_call_expression, '(', 30);
4690 register_infix_parser(parse_select_expression, '.', 30);
4691 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4692 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4694 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4697 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4698 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4699 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4700 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4701 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4702 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4703 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4704 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4705 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4706 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4707 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4708 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4709 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4710 T_EXCLAMATIONMARKEQUAL, 13);
4711 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4712 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4713 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4714 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4715 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4716 register_infix_parser(parse_conditional_expression, '?', 7);
4717 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4718 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4719 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4720 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4721 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4722 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4723 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4724 T_LESSLESSEQUAL, 2);
4725 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4726 T_GREATERGREATEREQUAL, 2);
4727 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4729 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4731 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4734 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4736 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4737 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4738 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4739 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4740 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4741 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4742 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4744 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4746 register_expression_parser(parse_sizeof, T_sizeof, 25);
4747 register_expression_parser(parse_extension, T___extension__, 25);
4748 register_expression_parser(parse_builtin_classify_type,
4749 T___builtin_classify_type, 25);
4753 * Parse a asm statement constraints specification.
4755 static asm_constraint_t *parse_asm_constraints(void)
4757 asm_constraint_t *result = NULL;
4758 asm_constraint_t *last = NULL;
4760 while(token.type == T_STRING_LITERAL || token.type == '[') {
4761 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4762 memset(constraint, 0, sizeof(constraint[0]));
4764 if(token.type == '[') {
4766 if(token.type != T_IDENTIFIER) {
4767 parse_error_expected("while parsing asm constraint",
4771 constraint->symbol = token.v.symbol;
4776 constraint->constraints = parse_string_literals();
4778 constraint->expression = parse_expression();
4782 last->next = constraint;
4784 result = constraint;
4788 if(token.type != ',')
4797 * Parse a asm statement clobber specification.
4799 static asm_clobber_t *parse_asm_clobbers(void)
4801 asm_clobber_t *result = NULL;
4802 asm_clobber_t *last = NULL;
4804 while(token.type == T_STRING_LITERAL) {
4805 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4806 clobber->clobber = parse_string_literals();
4809 last->next = clobber;
4815 if(token.type != ',')
4824 * Parse an asm statement.
4826 static statement_t *parse_asm_statement(void)
4830 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4831 statement->base.source_position = token.source_position;
4833 asm_statement_t *asm_statement = &statement->asms;
4835 if(token.type == T_volatile) {
4837 asm_statement->is_volatile = true;
4841 asm_statement->asm_text = parse_string_literals();
4843 if(token.type != ':')
4847 asm_statement->inputs = parse_asm_constraints();
4848 if(token.type != ':')
4852 asm_statement->outputs = parse_asm_constraints();
4853 if(token.type != ':')
4857 asm_statement->clobbers = parse_asm_clobbers();
4866 * Parse a case statement.
4868 static statement_t *parse_case_statement(void)
4872 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4874 statement->base.source_position = token.source_position;
4875 statement->case_label.expression = parse_expression();
4879 if (! is_constant_expression(statement->case_label.expression)) {
4880 errorf(statement->base.source_position,
4881 "case label does not reduce to an integer constant");
4883 /* TODO: check if the case label is already known */
4884 if (current_switch != NULL) {
4885 /* link all cases into the switch statement */
4886 if (current_switch->last_case == NULL) {
4887 current_switch->first_case =
4888 current_switch->last_case = &statement->case_label;
4890 current_switch->last_case->next = &statement->case_label;
4893 errorf(statement->base.source_position,
4894 "case label not within a switch statement");
4897 statement->case_label.label_statement = parse_statement();
4903 * Finds an existing default label of a switch statement.
4905 static case_label_statement_t *
4906 find_default_label(const switch_statement_t *statement)
4908 for (case_label_statement_t *label = statement->first_case;
4910 label = label->next) {
4911 if (label->expression == NULL)
4918 * Parse a default statement.
4920 static statement_t *parse_default_statement(void)
4924 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4926 statement->base.source_position = token.source_position;
4929 if (current_switch != NULL) {
4930 const case_label_statement_t *def_label = find_default_label(current_switch);
4931 if (def_label != NULL) {
4932 errorf(HERE, "multiple default labels in one switch");
4933 errorf(def_label->statement.source_position,
4934 "this is the first default label");
4936 /* link all cases into the switch statement */
4937 if (current_switch->last_case == NULL) {
4938 current_switch->first_case =
4939 current_switch->last_case = &statement->case_label;
4941 current_switch->last_case->next = &statement->case_label;
4945 errorf(statement->base.source_position,
4946 "'default' label not within a switch statement");
4948 statement->label.label_statement = parse_statement();
4954 * Return the declaration for a given label symbol or create a new one.
4956 static declaration_t *get_label(symbol_t *symbol)
4958 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4959 assert(current_function != NULL);
4960 /* if we found a label in the same function, then we already created the
4962 if(candidate != NULL
4963 && candidate->parent_context == ¤t_function->context) {
4967 /* otherwise we need to create a new one */
4968 declaration_t *const declaration = allocate_declaration_zero();
4969 declaration->namespc = NAMESPACE_LABEL;
4970 declaration->symbol = symbol;
4972 label_push(declaration);
4978 * Parse a label statement.
4980 static statement_t *parse_label_statement(void)
4982 assert(token.type == T_IDENTIFIER);
4983 symbol_t *symbol = token.v.symbol;
4986 declaration_t *label = get_label(symbol);
4988 /* if source position is already set then the label is defined twice,
4989 * otherwise it was just mentioned in a goto so far */
4990 if(label->source_position.input_name != NULL) {
4991 errorf(HERE, "duplicate label '%Y'", symbol);
4992 errorf(label->source_position, "previous definition of '%Y' was here",
4995 label->source_position = token.source_position;
4998 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
5000 label_statement->statement.kind = STATEMENT_LABEL;
5001 label_statement->statement.source_position = token.source_position;
5002 label_statement->label = label;
5006 if(token.type == '}') {
5007 /* TODO only warn? */
5008 errorf(HERE, "label at end of compound statement");
5009 return (statement_t*) label_statement;
5011 label_statement->label_statement = parse_statement();
5014 return (statement_t*) label_statement;
5018 * Parse an if statement.
5020 static statement_t *parse_if(void)
5024 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5025 statement->statement.kind = STATEMENT_IF;
5026 statement->statement.source_position = token.source_position;
5029 statement->condition = parse_expression();
5032 statement->true_statement = parse_statement();
5033 if(token.type == T_else) {
5035 statement->false_statement = parse_statement();
5038 return (statement_t*) statement;
5042 * Parse a switch statement.
5044 static statement_t *parse_switch(void)
5048 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5049 statement->statement.kind = STATEMENT_SWITCH;
5050 statement->statement.source_position = token.source_position;
5053 expression_t *const expr = parse_expression();
5054 type_t * type = skip_typeref(expr->base.datatype);
5055 if (is_type_integer(type)) {
5056 type = promote_integer(type);
5057 } else if (is_type_valid(type)) {
5058 errorf(expr->base.source_position, "switch quantity is not an integer, but '%T'", type);
5059 type = type_error_type;
5061 statement->expression = create_implicit_cast(expr, type);
5064 switch_statement_t *rem = current_switch;
5065 current_switch = statement;
5066 statement->body = parse_statement();
5067 current_switch = rem;
5069 return (statement_t*) statement;
5072 static statement_t *parse_loop_body(statement_t *const loop)
5074 statement_t *const rem = current_loop;
5075 current_loop = loop;
5076 statement_t *const body = parse_statement();
5082 * Parse a while statement.
5084 static statement_t *parse_while(void)
5088 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5089 statement->statement.kind = STATEMENT_WHILE;
5090 statement->statement.source_position = token.source_position;
5093 statement->condition = parse_expression();
5096 statement->body = parse_loop_body((statement_t*)statement);
5098 return (statement_t*) statement;
5102 * Parse a do statement.
5104 static statement_t *parse_do(void)
5108 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5109 statement->statement.kind = STATEMENT_DO_WHILE;
5110 statement->statement.source_position = token.source_position;
5112 statement->body = parse_loop_body((statement_t*)statement);
5115 statement->condition = parse_expression();
5119 return (statement_t*) statement;
5123 * Parse a for statement.
5125 static statement_t *parse_for(void)
5129 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5130 statement->statement.kind = STATEMENT_FOR;
5131 statement->statement.source_position = token.source_position;
5135 int top = environment_top();
5136 context_t *last_context = context;
5137 set_context(&statement->context);
5139 if(token.type != ';') {
5140 if(is_declaration_specifier(&token, false)) {
5141 parse_declaration(record_declaration);
5143 statement->initialisation = parse_expression();
5150 if(token.type != ';') {
5151 statement->condition = parse_expression();
5154 if(token.type != ')') {
5155 statement->step = parse_expression();
5158 statement->body = parse_loop_body((statement_t*)statement);
5160 assert(context == &statement->context);
5161 set_context(last_context);
5162 environment_pop_to(top);
5164 return (statement_t*) statement;
5168 * Parse a goto statement.
5170 static statement_t *parse_goto(void)
5174 if(token.type != T_IDENTIFIER) {
5175 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5179 symbol_t *symbol = token.v.symbol;
5182 declaration_t *label = get_label(symbol);
5184 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5186 statement->statement.kind = STATEMENT_GOTO;
5187 statement->statement.source_position = token.source_position;
5189 statement->label = label;
5191 /* remember the goto's in a list for later checking */
5192 if (goto_last == NULL) {
5193 goto_first = goto_last = statement;
5195 goto_last->next = statement;
5200 return (statement_t*) statement;
5204 * Parse a continue statement.
5206 static statement_t *parse_continue(void)
5208 statement_t *statement;
5209 if (current_loop == NULL) {
5210 errorf(HERE, "continue statement not within loop");
5213 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5215 statement->base.source_position = token.source_position;
5225 * Parse a break statement.
5227 static statement_t *parse_break(void)
5229 statement_t *statement;
5230 if (current_switch == NULL && current_loop == NULL) {
5231 errorf(HERE, "break statement not within loop or switch");
5234 statement = allocate_statement_zero(STATEMENT_BREAK);
5236 statement->base.source_position = token.source_position;
5246 * Check if a given declaration represents a local variable.
5248 static bool is_local_var_declaration(const declaration_t *declaration) {
5249 switch ((storage_class_tag_t) declaration->storage_class) {
5250 case STORAGE_CLASS_NONE:
5251 case STORAGE_CLASS_AUTO:
5252 case STORAGE_CLASS_REGISTER: {
5253 const type_t *type = skip_typeref(declaration->type);
5254 if(is_type_function(type)) {
5266 * Check if a given expression represents a local variable.
5268 static bool is_local_variable(const expression_t *expression)
5270 if (expression->base.kind != EXPR_REFERENCE) {
5273 const declaration_t *declaration = expression->reference.declaration;
5274 return is_local_var_declaration(declaration);
5278 * Parse a return statement.
5280 static statement_t *parse_return(void)
5284 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5286 statement->statement.kind = STATEMENT_RETURN;
5287 statement->statement.source_position = token.source_position;
5289 expression_t *return_value = NULL;
5290 if(token.type != ';') {
5291 return_value = parse_expression();
5295 const type_t *const func_type = current_function->type;
5296 assert(is_type_function(func_type));
5297 type_t *const return_type = skip_typeref(func_type->function.return_type);
5299 if(return_value != NULL) {
5300 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5302 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5303 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5304 warningf(statement->statement.source_position,
5305 "'return' with a value, in function returning void");
5306 return_value = NULL;
5308 type_t *const res_type = semantic_assign(return_type,
5309 return_value, "'return'");
5310 if (res_type == NULL) {
5311 errorf(statement->statement.source_position,
5312 "cannot return something of type '%T' in function returning '%T'",
5313 return_value->base.datatype, return_type);
5315 return_value = create_implicit_cast(return_value, res_type);
5318 /* check for returning address of a local var */
5319 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5320 const expression_t *expression = return_value->unary.value;
5321 if (is_local_variable(expression)) {
5322 warningf(statement->statement.source_position,
5323 "function returns address of local variable");
5327 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5328 warningf(statement->statement.source_position,
5329 "'return' without value, in function returning non-void");
5332 statement->return_value = return_value;
5334 return (statement_t*) statement;
5338 * Parse a declaration statement.
5340 static statement_t *parse_declaration_statement(void)
5342 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5344 statement->base.source_position = token.source_position;
5346 declaration_t *before = last_declaration;
5347 parse_declaration(record_declaration);
5349 if(before == NULL) {
5350 statement->declaration.declarations_begin = context->declarations;
5352 statement->declaration.declarations_begin = before->next;
5354 statement->declaration.declarations_end = last_declaration;
5360 * Parse an expression statement, ie. expr ';'.
5362 static statement_t *parse_expression_statement(void)
5364 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5366 statement->base.source_position = token.source_position;
5367 expression_t *const expr = parse_expression();
5368 statement->expression.expression = expr;
5370 if (warning.unused_value && !expression_has_effect(expr)) {
5371 warningf(expr->base.source_position, "statement has no effect");
5380 * Parse a statement.
5382 static statement_t *parse_statement(void)
5384 statement_t *statement = NULL;
5386 /* declaration or statement */
5387 switch(token.type) {
5389 statement = parse_asm_statement();
5393 statement = parse_case_statement();
5397 statement = parse_default_statement();
5401 statement = parse_compound_statement();
5405 statement = parse_if();
5409 statement = parse_switch();
5413 statement = parse_while();
5417 statement = parse_do();
5421 statement = parse_for();
5425 statement = parse_goto();
5429 statement = parse_continue();
5433 statement = parse_break();
5437 statement = parse_return();
5446 if(look_ahead(1)->type == ':') {
5447 statement = parse_label_statement();
5451 if(is_typedef_symbol(token.v.symbol)) {
5452 statement = parse_declaration_statement();
5456 statement = parse_expression_statement();
5459 case T___extension__:
5460 /* this can be a prefix to a declaration or an expression statement */
5461 /* we simply eat it now and parse the rest with tail recursion */
5464 } while(token.type == T___extension__);
5465 statement = parse_statement();
5469 statement = parse_declaration_statement();
5473 statement = parse_expression_statement();
5477 assert(statement == NULL
5478 || statement->base.source_position.input_name != NULL);
5484 * Parse a compound statement.
5486 static statement_t *parse_compound_statement(void)
5488 compound_statement_t *const compound_statement
5489 = allocate_ast_zero(sizeof(compound_statement[0]));
5490 compound_statement->statement.kind = STATEMENT_COMPOUND;
5491 compound_statement->statement.source_position = token.source_position;
5495 int top = environment_top();
5496 context_t *last_context = context;
5497 set_context(&compound_statement->context);
5499 statement_t *last_statement = NULL;
5501 while(token.type != '}' && token.type != T_EOF) {
5502 statement_t *statement = parse_statement();
5503 if(statement == NULL)
5506 if(last_statement != NULL) {
5507 last_statement->base.next = statement;
5509 compound_statement->statements = statement;
5512 while(statement->base.next != NULL)
5513 statement = statement->base.next;
5515 last_statement = statement;
5518 if(token.type == '}') {
5521 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5524 assert(context == &compound_statement->context);
5525 set_context(last_context);
5526 environment_pop_to(top);
5528 return (statement_t*) compound_statement;
5532 * Initialize builtin types.
5534 static void initialize_builtin_types(void)
5536 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5537 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5538 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5539 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5540 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5541 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5542 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5543 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5545 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5546 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5547 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5548 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5552 * Parse a translation unit.
5554 static translation_unit_t *parse_translation_unit(void)
5556 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5558 assert(global_context == NULL);
5559 global_context = &unit->context;
5561 assert(context == NULL);
5562 set_context(&unit->context);
5564 initialize_builtin_types();
5566 while(token.type != T_EOF) {
5567 if (token.type == ';') {
5568 /* TODO error in strict mode */
5569 warningf(HERE, "stray ';' outside of function");
5572 parse_external_declaration();
5576 assert(context == &unit->context);
5578 last_declaration = NULL;
5580 assert(global_context == &unit->context);
5581 global_context = NULL;
5589 * @return the translation unit or NULL if errors occurred.
5591 translation_unit_t *parse(void)
5593 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5594 label_stack = NEW_ARR_F(stack_entry_t, 0);
5595 diagnostic_count = 0;
5599 type_set_output(stderr);
5600 ast_set_output(stderr);
5602 lookahead_bufpos = 0;
5603 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5606 translation_unit_t *unit = parse_translation_unit();
5608 DEL_ARR_F(environment_stack);
5609 DEL_ARR_F(label_stack);
5618 * Initialize the parser.
5620 void init_parser(void)
5622 init_expression_parsers();
5623 obstack_init(&temp_obst);
5625 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5626 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5630 * Terminate the parser.
5632 void exit_parser(void)
5634 obstack_free(&temp_obst, NULL);