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);
988 static initializer_t *parse_sub_initializer_elem(type_t *type)
990 if(token.type == '{') {
991 return parse_sub_initializer(type, NULL);
994 expression_t *expression = parse_assignment_expression();
995 return parse_sub_initializer(type, expression);
998 static bool had_initializer_brace_warning;
1000 static void skip_designator(void)
1003 if(token.type == '.') {
1005 if(token.type == T_IDENTIFIER)
1007 } else if(token.type == '[') {
1009 parse_constant_expression();
1010 if(token.type == ']')
1018 static initializer_t *parse_sub_initializer(type_t *type,
1019 expression_t *expression)
1021 if(is_type_scalar(type)) {
1022 /* there might be extra {} hierarchies */
1023 if(token.type == '{') {
1025 if(!had_initializer_brace_warning) {
1026 warningf(HERE, "braces around scalar initializer");
1027 had_initializer_brace_warning = true;
1029 initializer_t *result = parse_sub_initializer(type, NULL);
1030 if(token.type == ',') {
1032 /* TODO: warn about excessive elements */
1038 if(expression == NULL) {
1039 expression = parse_assignment_expression();
1041 return initializer_from_expression(type, expression);
1044 /* does the expression match the currently looked at object to initialize */
1045 if(expression != NULL) {
1046 initializer_t *result = initializer_from_expression(type, expression);
1051 bool read_paren = false;
1052 if(token.type == '{') {
1057 /* descend into subtype */
1058 initializer_t *result = NULL;
1059 initializer_t **elems;
1060 if(is_type_array(type)) {
1061 if(token.type == '.') {
1063 "compound designator in initializer for array type '%T'",
1068 type_t *const element_type = skip_typeref(type->array.element_type);
1071 had_initializer_brace_warning = false;
1072 if(expression == NULL) {
1073 sub = parse_sub_initializer_elem(element_type);
1075 sub = parse_sub_initializer(element_type, expression);
1078 /* didn't match the subtypes -> try the parent type */
1080 assert(!read_paren);
1084 elems = NEW_ARR_F(initializer_t*, 0);
1085 ARR_APP1(initializer_t*, elems, sub);
1088 if(token.type == '}')
1091 if(token.type == '}')
1094 sub = parse_sub_initializer_elem(element_type);
1096 /* TODO error, do nicer cleanup */
1097 errorf(HERE, "member initializer didn't match");
1101 ARR_APP1(initializer_t*, elems, sub);
1104 assert(is_type_compound(type));
1105 context_t *const context = &type->compound.declaration->context;
1107 if(token.type == '[') {
1109 "array designator in initializer for compound type '%T'",
1114 declaration_t *first = context->declarations;
1117 type_t *first_type = first->type;
1118 first_type = skip_typeref(first_type);
1121 had_initializer_brace_warning = false;
1122 if(expression == NULL) {
1123 sub = parse_sub_initializer_elem(first_type);
1125 sub = parse_sub_initializer(first_type, expression);
1128 /* didn't match the subtypes -> try our parent type */
1130 assert(!read_paren);
1134 elems = NEW_ARR_F(initializer_t*, 0);
1135 ARR_APP1(initializer_t*, elems, sub);
1137 declaration_t *iter = first->next;
1138 for( ; iter != NULL; iter = iter->next) {
1139 if(iter->symbol == NULL)
1141 if(iter->namespc != NAMESPACE_NORMAL)
1144 if(token.type == '}')
1147 if(token.type == '}')
1150 type_t *iter_type = iter->type;
1151 iter_type = skip_typeref(iter_type);
1153 sub = parse_sub_initializer_elem(iter_type);
1155 /* TODO error, do nicer cleanup */
1156 errorf(HERE, "member initializer didn't match");
1160 ARR_APP1(initializer_t*, elems, sub);
1164 int len = ARR_LEN(elems);
1165 size_t elems_size = sizeof(initializer_t*) * len;
1167 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1169 init->initializer.kind = INITIALIZER_LIST;
1171 memcpy(init->initializers, elems, elems_size);
1174 result = (initializer_t*) init;
1177 if(token.type == ',')
1184 static initializer_t *parse_initializer(type_t *const orig_type)
1186 initializer_t *result;
1188 type_t *const type = skip_typeref(orig_type);
1190 if(token.type != '{') {
1191 expression_t *expression = parse_assignment_expression();
1192 initializer_t *initializer = initializer_from_expression(type, expression);
1193 if(initializer == NULL) {
1195 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1196 expression, expression->base.datatype, orig_type);
1201 if(is_type_scalar(type)) {
1205 expression_t *expression = parse_assignment_expression();
1206 result = initializer_from_expression(type, expression);
1208 if(token.type == ',')
1214 result = parse_sub_initializer(type, NULL);
1220 static declaration_t *append_declaration(declaration_t *declaration);
1222 static declaration_t *parse_compound_type_specifier(bool is_struct)
1230 symbol_t *symbol = NULL;
1231 declaration_t *declaration = NULL;
1233 if (token.type == T___attribute__) {
1238 if(token.type == T_IDENTIFIER) {
1239 symbol = token.v.symbol;
1243 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1245 declaration = get_declaration(symbol, NAMESPACE_UNION);
1247 } else if(token.type != '{') {
1249 parse_error_expected("while parsing struct type specifier",
1250 T_IDENTIFIER, '{', 0);
1252 parse_error_expected("while parsing union type specifier",
1253 T_IDENTIFIER, '{', 0);
1259 if(declaration == NULL) {
1260 declaration = allocate_declaration_zero();
1261 declaration->namespc =
1262 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1263 declaration->source_position = token.source_position;
1264 declaration->symbol = symbol;
1265 declaration->parent_context = context;
1266 if (symbol != NULL) {
1267 environment_push(declaration);
1269 append_declaration(declaration);
1272 if(token.type == '{') {
1273 if(declaration->init.is_defined) {
1274 assert(symbol != NULL);
1275 errorf(HERE, "multiple definition of '%s %Y'",
1276 is_struct ? "struct" : "union", symbol);
1277 declaration->context.declarations = NULL;
1279 declaration->init.is_defined = true;
1281 int top = environment_top();
1282 context_t *last_context = context;
1283 set_context(&declaration->context);
1285 parse_compound_type_entries();
1288 assert(context == &declaration->context);
1289 set_context(last_context);
1290 environment_pop_to(top);
1296 static void parse_enum_entries(type_t *const enum_type)
1300 if(token.type == '}') {
1302 errorf(HERE, "empty enum not allowed");
1307 if(token.type != T_IDENTIFIER) {
1308 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1313 declaration_t *const entry = allocate_declaration_zero();
1314 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1315 entry->type = enum_type;
1316 entry->symbol = token.v.symbol;
1317 entry->source_position = token.source_position;
1320 if(token.type == '=') {
1322 entry->init.enum_value = parse_constant_expression();
1327 record_declaration(entry);
1329 if(token.type != ',')
1332 } while(token.type != '}');
1337 static type_t *parse_enum_specifier(void)
1341 declaration_t *declaration;
1344 if(token.type == T_IDENTIFIER) {
1345 symbol = token.v.symbol;
1348 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1349 } else if(token.type != '{') {
1350 parse_error_expected("while parsing enum type specifier",
1351 T_IDENTIFIER, '{', 0);
1358 if(declaration == NULL) {
1359 declaration = allocate_declaration_zero();
1360 declaration->namespc = NAMESPACE_ENUM;
1361 declaration->source_position = token.source_position;
1362 declaration->symbol = symbol;
1363 declaration->parent_context = context;
1366 type_t *const type = allocate_type_zero(TYPE_ENUM);
1367 type->enumt.declaration = declaration;
1369 if(token.type == '{') {
1370 if(declaration->init.is_defined) {
1371 errorf(HERE, "multiple definitions of enum %Y", symbol);
1373 if (symbol != NULL) {
1374 environment_push(declaration);
1376 append_declaration(declaration);
1377 declaration->init.is_defined = 1;
1379 parse_enum_entries(type);
1387 * if a symbol is a typedef to another type, return true
1389 static bool is_typedef_symbol(symbol_t *symbol)
1391 const declaration_t *const declaration =
1392 get_declaration(symbol, NAMESPACE_NORMAL);
1394 declaration != NULL &&
1395 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1398 static type_t *parse_typeof(void)
1406 expression_t *expression = NULL;
1409 switch(token.type) {
1410 case T___extension__:
1411 /* this can be a prefix to a typename or an expression */
1412 /* we simply eat it now. */
1415 } while(token.type == T___extension__);
1419 if(is_typedef_symbol(token.v.symbol)) {
1420 type = parse_typename();
1422 expression = parse_expression();
1423 type = expression->base.datatype;
1428 type = parse_typename();
1432 expression = parse_expression();
1433 type = expression->base.datatype;
1439 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1440 typeof_type->typeoft.expression = expression;
1441 typeof_type->typeoft.typeof_type = type;
1447 SPECIFIER_SIGNED = 1 << 0,
1448 SPECIFIER_UNSIGNED = 1 << 1,
1449 SPECIFIER_LONG = 1 << 2,
1450 SPECIFIER_INT = 1 << 3,
1451 SPECIFIER_DOUBLE = 1 << 4,
1452 SPECIFIER_CHAR = 1 << 5,
1453 SPECIFIER_SHORT = 1 << 6,
1454 SPECIFIER_LONG_LONG = 1 << 7,
1455 SPECIFIER_FLOAT = 1 << 8,
1456 SPECIFIER_BOOL = 1 << 9,
1457 SPECIFIER_VOID = 1 << 10,
1458 #ifdef PROVIDE_COMPLEX
1459 SPECIFIER_COMPLEX = 1 << 11,
1460 SPECIFIER_IMAGINARY = 1 << 12,
1464 static type_t *create_builtin_type(symbol_t *const symbol,
1465 type_t *const real_type)
1467 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1468 type->builtin.symbol = symbol;
1469 type->builtin.real_type = real_type;
1471 type_t *result = typehash_insert(type);
1472 if (type != result) {
1479 static type_t *get_typedef_type(symbol_t *symbol)
1481 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1482 if(declaration == NULL
1483 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1486 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1487 type->typedeft.declaration = declaration;
1492 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1494 type_t *type = NULL;
1495 unsigned type_qualifiers = 0;
1496 unsigned type_specifiers = 0;
1499 specifiers->source_position = token.source_position;
1502 switch(token.type) {
1505 #define MATCH_STORAGE_CLASS(token, class) \
1507 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1508 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1510 specifiers->storage_class = class; \
1514 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1515 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1516 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1517 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1518 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1521 switch (specifiers->storage_class) {
1522 case STORAGE_CLASS_NONE:
1523 specifiers->storage_class = STORAGE_CLASS_THREAD;
1526 case STORAGE_CLASS_EXTERN:
1527 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1530 case STORAGE_CLASS_STATIC:
1531 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1535 errorf(HERE, "multiple storage classes in declaration specifiers");
1541 /* type qualifiers */
1542 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1544 type_qualifiers |= qualifier; \
1548 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1549 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1550 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1552 case T___extension__:
1557 /* type specifiers */
1558 #define MATCH_SPECIFIER(token, specifier, name) \
1561 if(type_specifiers & specifier) { \
1562 errorf(HERE, "multiple " name " type specifiers given"); \
1564 type_specifiers |= specifier; \
1568 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1569 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1570 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1571 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1572 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1573 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1574 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1575 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1576 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1577 #ifdef PROVIDE_COMPLEX
1578 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1579 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1582 /* only in microsoft mode */
1583 specifiers->decl_modifiers |= DM_FORCEINLINE;
1587 specifiers->is_inline = true;
1592 if(type_specifiers & SPECIFIER_LONG_LONG) {
1593 errorf(HERE, "multiple type specifiers given");
1594 } else if(type_specifiers & SPECIFIER_LONG) {
1595 type_specifiers |= SPECIFIER_LONG_LONG;
1597 type_specifiers |= SPECIFIER_LONG;
1601 /* TODO: if is_type_valid(type) for the following rules should issue
1604 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1606 type->compound.declaration = parse_compound_type_specifier(true);
1610 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1612 type->compound.declaration = parse_compound_type_specifier(false);
1616 type = parse_enum_specifier();
1619 type = parse_typeof();
1621 case T___builtin_va_list:
1622 type = duplicate_type(type_valist);
1626 case T___attribute__:
1631 case T_IDENTIFIER: {
1632 type_t *typedef_type = get_typedef_type(token.v.symbol);
1634 if(typedef_type == NULL)
1635 goto finish_specifiers;
1638 type = typedef_type;
1642 /* function specifier */
1644 goto finish_specifiers;
1651 atomic_type_kind_t atomic_type;
1653 /* match valid basic types */
1654 switch(type_specifiers) {
1655 case SPECIFIER_VOID:
1656 atomic_type = ATOMIC_TYPE_VOID;
1658 case SPECIFIER_CHAR:
1659 atomic_type = ATOMIC_TYPE_CHAR;
1661 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1662 atomic_type = ATOMIC_TYPE_SCHAR;
1664 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1665 atomic_type = ATOMIC_TYPE_UCHAR;
1667 case SPECIFIER_SHORT:
1668 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1669 case SPECIFIER_SHORT | SPECIFIER_INT:
1670 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1671 atomic_type = ATOMIC_TYPE_SHORT;
1673 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1674 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1675 atomic_type = ATOMIC_TYPE_USHORT;
1678 case SPECIFIER_SIGNED:
1679 case SPECIFIER_SIGNED | SPECIFIER_INT:
1680 atomic_type = ATOMIC_TYPE_INT;
1682 case SPECIFIER_UNSIGNED:
1683 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1684 atomic_type = ATOMIC_TYPE_UINT;
1686 case SPECIFIER_LONG:
1687 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1688 case SPECIFIER_LONG | SPECIFIER_INT:
1689 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1690 atomic_type = ATOMIC_TYPE_LONG;
1692 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1693 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1694 atomic_type = ATOMIC_TYPE_ULONG;
1696 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1697 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1698 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1699 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1701 atomic_type = ATOMIC_TYPE_LONGLONG;
1703 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1704 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1706 atomic_type = ATOMIC_TYPE_ULONGLONG;
1708 case SPECIFIER_FLOAT:
1709 atomic_type = ATOMIC_TYPE_FLOAT;
1711 case SPECIFIER_DOUBLE:
1712 atomic_type = ATOMIC_TYPE_DOUBLE;
1714 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1715 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1717 case SPECIFIER_BOOL:
1718 atomic_type = ATOMIC_TYPE_BOOL;
1720 #ifdef PROVIDE_COMPLEX
1721 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1722 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1724 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1725 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1727 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1728 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1730 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1731 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1733 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1734 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1736 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1737 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1741 /* invalid specifier combination, give an error message */
1742 if(type_specifiers == 0) {
1743 if (! strict_mode) {
1744 if (warning.implicit_int) {
1745 warningf(HERE, "no type specifiers in declaration, using 'int'");
1747 atomic_type = ATOMIC_TYPE_INT;
1750 errorf(HERE, "no type specifiers given in declaration");
1752 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1753 (type_specifiers & SPECIFIER_UNSIGNED)) {
1754 errorf(HERE, "signed and unsigned specifiers gives");
1755 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1756 errorf(HERE, "only integer types can be signed or unsigned");
1758 errorf(HERE, "multiple datatypes in declaration");
1760 atomic_type = ATOMIC_TYPE_INVALID;
1763 type = allocate_type_zero(TYPE_ATOMIC);
1764 type->atomic.akind = atomic_type;
1767 if(type_specifiers != 0) {
1768 errorf(HERE, "multiple datatypes in declaration");
1772 type->base.qualifiers = type_qualifiers;
1774 type_t *result = typehash_insert(type);
1775 if(newtype && result != type) {
1779 specifiers->type = result;
1782 static type_qualifiers_t parse_type_qualifiers(void)
1784 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1787 switch(token.type) {
1788 /* type qualifiers */
1789 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1790 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1791 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1794 return type_qualifiers;
1799 static declaration_t *parse_identifier_list(void)
1801 declaration_t *declarations = NULL;
1802 declaration_t *last_declaration = NULL;
1804 declaration_t *const declaration = allocate_declaration_zero();
1805 declaration->source_position = token.source_position;
1806 declaration->symbol = token.v.symbol;
1809 if(last_declaration != NULL) {
1810 last_declaration->next = declaration;
1812 declarations = declaration;
1814 last_declaration = declaration;
1816 if(token.type != ',')
1819 } while(token.type == T_IDENTIFIER);
1821 return declarations;
1824 static void semantic_parameter(declaration_t *declaration)
1826 /* TODO: improve error messages */
1828 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1829 errorf(HERE, "typedef not allowed in parameter list");
1830 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1831 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1832 errorf(HERE, "parameter may only have none or register storage class");
1835 type_t *const orig_type = declaration->type;
1836 type_t * type = skip_typeref(orig_type);
1838 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1839 * into a pointer. § 6.7.5.3 (7) */
1840 if (is_type_array(type)) {
1841 type_t *const element_type = type->array.element_type;
1843 type = make_pointer_type(element_type, type->base.qualifiers);
1845 declaration->type = type;
1848 if(is_type_incomplete(type)) {
1849 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1850 orig_type, declaration->symbol);
1854 static declaration_t *parse_parameter(void)
1856 declaration_specifiers_t specifiers;
1857 memset(&specifiers, 0, sizeof(specifiers));
1859 parse_declaration_specifiers(&specifiers);
1861 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1863 semantic_parameter(declaration);
1868 static declaration_t *parse_parameters(function_type_t *type)
1870 if(token.type == T_IDENTIFIER) {
1871 symbol_t *symbol = token.v.symbol;
1872 if(!is_typedef_symbol(symbol)) {
1873 type->kr_style_parameters = true;
1874 return parse_identifier_list();
1878 if(token.type == ')') {
1879 type->unspecified_parameters = 1;
1882 if(token.type == T_void && look_ahead(1)->type == ')') {
1887 declaration_t *declarations = NULL;
1888 declaration_t *declaration;
1889 declaration_t *last_declaration = NULL;
1890 function_parameter_t *parameter;
1891 function_parameter_t *last_parameter = NULL;
1894 switch(token.type) {
1898 return declarations;
1901 case T___extension__:
1903 declaration = parse_parameter();
1905 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1906 memset(parameter, 0, sizeof(parameter[0]));
1907 parameter->type = declaration->type;
1909 if(last_parameter != NULL) {
1910 last_declaration->next = declaration;
1911 last_parameter->next = parameter;
1913 type->parameters = parameter;
1914 declarations = declaration;
1916 last_parameter = parameter;
1917 last_declaration = declaration;
1921 return declarations;
1923 if(token.type != ',')
1924 return declarations;
1934 } construct_type_type_t;
1936 typedef struct construct_type_t construct_type_t;
1937 struct construct_type_t {
1938 construct_type_type_t type;
1939 construct_type_t *next;
1942 typedef struct parsed_pointer_t parsed_pointer_t;
1943 struct parsed_pointer_t {
1944 construct_type_t construct_type;
1945 type_qualifiers_t type_qualifiers;
1948 typedef struct construct_function_type_t construct_function_type_t;
1949 struct construct_function_type_t {
1950 construct_type_t construct_type;
1951 type_t *function_type;
1954 typedef struct parsed_array_t parsed_array_t;
1955 struct parsed_array_t {
1956 construct_type_t construct_type;
1957 type_qualifiers_t type_qualifiers;
1963 typedef struct construct_base_type_t construct_base_type_t;
1964 struct construct_base_type_t {
1965 construct_type_t construct_type;
1969 static construct_type_t *parse_pointer_declarator(void)
1973 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1974 memset(pointer, 0, sizeof(pointer[0]));
1975 pointer->construct_type.type = CONSTRUCT_POINTER;
1976 pointer->type_qualifiers = parse_type_qualifiers();
1978 return (construct_type_t*) pointer;
1981 static construct_type_t *parse_array_declarator(void)
1985 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1986 memset(array, 0, sizeof(array[0]));
1987 array->construct_type.type = CONSTRUCT_ARRAY;
1989 if(token.type == T_static) {
1990 array->is_static = true;
1994 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
1995 if(type_qualifiers != 0) {
1996 if(token.type == T_static) {
1997 array->is_static = true;
2001 array->type_qualifiers = type_qualifiers;
2003 if(token.type == '*' && look_ahead(1)->type == ']') {
2004 array->is_variable = true;
2006 } else if(token.type != ']') {
2007 array->size = parse_assignment_expression();
2012 return (construct_type_t*) array;
2015 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2019 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2021 declaration_t *parameters = parse_parameters(&type->function);
2022 if(declaration != NULL) {
2023 declaration->context.declarations = parameters;
2026 construct_function_type_t *construct_function_type =
2027 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2028 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2029 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2030 construct_function_type->function_type = type;
2034 return (construct_type_t*) construct_function_type;
2037 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2038 bool may_be_abstract)
2040 /* construct a single linked list of construct_type_t's which describe
2041 * how to construct the final declarator type */
2042 construct_type_t *first = NULL;
2043 construct_type_t *last = NULL;
2046 while(token.type == '*') {
2047 construct_type_t *type = parse_pointer_declarator();
2058 /* TODO: find out if this is correct */
2061 construct_type_t *inner_types = NULL;
2063 switch(token.type) {
2065 if(declaration == NULL) {
2066 errorf(HERE, "no identifier expected in typename");
2068 declaration->symbol = token.v.symbol;
2069 declaration->source_position = token.source_position;
2075 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2081 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2082 /* avoid a loop in the outermost scope, because eat_statement doesn't
2084 if(token.type == '}' && current_function == NULL) {
2092 construct_type_t *p = last;
2095 construct_type_t *type;
2096 switch(token.type) {
2098 type = parse_function_declarator(declaration);
2101 type = parse_array_declarator();
2104 goto declarator_finished;
2107 /* insert in the middle of the list (behind p) */
2109 type->next = p->next;
2120 declarator_finished:
2123 /* append inner_types at the end of the list, we don't to set last anymore
2124 * as it's not needed anymore */
2126 assert(first == NULL);
2127 first = inner_types;
2129 last->next = inner_types;
2135 static type_t *construct_declarator_type(construct_type_t *construct_list,
2138 construct_type_t *iter = construct_list;
2139 for( ; iter != NULL; iter = iter->next) {
2140 switch(iter->type) {
2141 case CONSTRUCT_INVALID:
2142 panic("invalid type construction found");
2143 case CONSTRUCT_FUNCTION: {
2144 construct_function_type_t *construct_function_type
2145 = (construct_function_type_t*) iter;
2147 type_t *function_type = construct_function_type->function_type;
2149 function_type->function.return_type = type;
2151 type_t *skipped_return_type = skip_typeref(type);
2152 if (is_type_function(skipped_return_type)) {
2153 errorf(HERE, "function returning function is not allowed");
2154 type = type_error_type;
2155 } else if (is_type_array(skipped_return_type)) {
2156 errorf(HERE, "function returning array is not allowed");
2157 type = type_error_type;
2159 type = function_type;
2164 case CONSTRUCT_POINTER: {
2165 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2166 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2167 pointer_type->pointer.points_to = type;
2168 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2170 type = pointer_type;
2174 case CONSTRUCT_ARRAY: {
2175 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2176 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2178 array_type->base.qualifiers = parsed_array->type_qualifiers;
2179 array_type->array.element_type = type;
2180 array_type->array.is_static = parsed_array->is_static;
2181 array_type->array.is_variable = parsed_array->is_variable;
2182 array_type->array.size = parsed_array->size;
2184 type_t *skipped_type = skip_typeref(type);
2185 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2186 errorf(HERE, "array of void is not allowed");
2187 type = type_error_type;
2195 type_t *hashed_type = typehash_insert(type);
2196 if(hashed_type != type) {
2197 /* the function type was constructed earlier freeing it here will
2198 * destroy other types... */
2199 if(iter->type != CONSTRUCT_FUNCTION) {
2209 static declaration_t *parse_declarator(
2210 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2212 declaration_t *const declaration = allocate_declaration_zero();
2213 declaration->storage_class = specifiers->storage_class;
2214 declaration->modifiers = specifiers->decl_modifiers;
2215 declaration->is_inline = specifiers->is_inline;
2217 construct_type_t *construct_type
2218 = parse_inner_declarator(declaration, may_be_abstract);
2219 type_t *const type = specifiers->type;
2220 declaration->type = construct_declarator_type(construct_type, type);
2222 if(construct_type != NULL) {
2223 obstack_free(&temp_obst, construct_type);
2229 static type_t *parse_abstract_declarator(type_t *base_type)
2231 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2233 type_t *result = construct_declarator_type(construct_type, base_type);
2234 if(construct_type != NULL) {
2235 obstack_free(&temp_obst, construct_type);
2241 static declaration_t *append_declaration(declaration_t* const declaration)
2243 if (last_declaration != NULL) {
2244 last_declaration->next = declaration;
2246 context->declarations = declaration;
2248 last_declaration = declaration;
2252 static bool is_sym_main(const symbol_t *const sym)
2254 return strcmp(sym->string, "main") == 0;
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 (is_function_definition) {
2301 if (warning.missing_prototypes &&
2302 prev_type->function.unspecified_parameters &&
2303 !is_sym_main(symbol)) {
2304 warningf(declaration->source_position, "no previous prototype for '%#T'", type, symbol);
2306 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2307 new_storage_class = STORAGE_CLASS_EXTERN;
2315 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2316 new_storage_class == STORAGE_CLASS_EXTERN) {
2317 warn_redundant_declaration:
2318 if (warning.redundant_decls) {
2319 warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
2320 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2322 } else if (current_function == NULL) {
2323 if (old_storage_class != STORAGE_CLASS_STATIC &&
2324 new_storage_class == STORAGE_CLASS_STATIC) {
2325 errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
2326 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2328 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2329 goto warn_redundant_declaration;
2331 if (new_storage_class == STORAGE_CLASS_NONE) {
2332 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2336 if (old_storage_class == new_storage_class) {
2337 errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
2339 errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
2341 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2344 return previous_declaration;
2346 } else if (is_function_definition &&
2347 declaration->storage_class != STORAGE_CLASS_STATIC) {
2348 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2349 warningf(declaration->source_position, "no previous prototype for '%#T'", type, symbol);
2350 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2351 warningf(declaration->source_position, "no previous declaration for '%#T'", type, symbol);
2353 } else if (warning.missing_declarations &&
2354 declaration->storage_class != STORAGE_CLASS_STATIC &&
2355 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
2356 warningf(declaration->source_position, "no previous declaration for '%#T'", type, symbol);
2359 assert(declaration->parent_context == NULL);
2360 assert(declaration->symbol != NULL);
2361 assert(context != NULL);
2363 declaration->parent_context = context;
2365 environment_push(declaration);
2366 return append_declaration(declaration);
2369 static declaration_t *record_declaration(declaration_t *declaration)
2371 return internal_record_declaration(declaration, false);
2374 static declaration_t *record_function_definition(declaration_t *declaration)
2376 return internal_record_declaration(declaration, true);
2379 static void parser_error_multiple_definition(declaration_t *declaration,
2380 const source_position_t source_position)
2382 errorf(source_position, "multiple definition of symbol '%Y'",
2383 declaration->symbol);
2384 errorf(declaration->source_position,
2385 "this is the location of the previous definition.");
2388 static bool is_declaration_specifier(const token_t *token,
2389 bool only_type_specifiers)
2391 switch(token->type) {
2395 return is_typedef_symbol(token->v.symbol);
2397 case T___extension__:
2400 return !only_type_specifiers;
2407 static void parse_init_declarator_rest(declaration_t *declaration)
2411 type_t *orig_type = declaration->type;
2412 type_t *type = type = skip_typeref(orig_type);
2414 if(declaration->init.initializer != NULL) {
2415 parser_error_multiple_definition(declaration, token.source_position);
2418 initializer_t *initializer = parse_initializer(type);
2420 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2421 * the array type size */
2422 if(is_type_array(type) && initializer != NULL) {
2423 array_type_t *array_type = &type->array;
2425 if(array_type->size == NULL) {
2426 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2428 cnst->base.datatype = type_size_t;
2430 switch (initializer->kind) {
2431 case INITIALIZER_LIST: {
2432 cnst->conste.v.int_value = initializer->list.len;
2436 case INITIALIZER_STRING: {
2437 cnst->conste.v.int_value = initializer->string.string.size;
2441 case INITIALIZER_WIDE_STRING: {
2442 cnst->conste.v.int_value = initializer->wide_string.string.size;
2447 panic("invalid initializer type");
2450 array_type->size = cnst;
2454 if(is_type_function(type)) {
2455 errorf(declaration->source_position,
2456 "initializers not allowed for function types at declator '%Y' (type '%T')",
2457 declaration->symbol, orig_type);
2459 declaration->init.initializer = initializer;
2463 /* parse rest of a declaration without any declarator */
2464 static void parse_anonymous_declaration_rest(
2465 const declaration_specifiers_t *specifiers,
2466 parsed_declaration_func finished_declaration)
2470 declaration_t *const declaration = allocate_declaration_zero();
2471 declaration->type = specifiers->type;
2472 declaration->storage_class = specifiers->storage_class;
2473 declaration->source_position = specifiers->source_position;
2475 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2476 warningf(declaration->source_position, "useless storage class in empty declaration");
2479 type_t *type = declaration->type;
2480 switch (type->kind) {
2481 case TYPE_COMPOUND_STRUCT:
2482 case TYPE_COMPOUND_UNION: {
2483 if (type->compound.declaration->symbol == NULL) {
2484 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2493 warningf(declaration->source_position, "empty declaration");
2497 finished_declaration(declaration);
2500 static void parse_declaration_rest(declaration_t *ndeclaration,
2501 const declaration_specifiers_t *specifiers,
2502 parsed_declaration_func finished_declaration)
2505 declaration_t *declaration = finished_declaration(ndeclaration);
2507 type_t *orig_type = declaration->type;
2508 type_t *type = skip_typeref(orig_type);
2510 if (type->kind != TYPE_FUNCTION &&
2511 declaration->is_inline &&
2512 is_type_valid(type)) {
2513 warningf(declaration->source_position,
2514 "variable '%Y' declared 'inline'\n", declaration->symbol);
2517 if(token.type == '=') {
2518 parse_init_declarator_rest(declaration);
2521 if(token.type != ',')
2525 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2530 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2532 symbol_t *symbol = declaration->symbol;
2533 if(symbol == NULL) {
2534 errorf(HERE, "anonymous declaration not valid as function parameter");
2537 namespace_t namespc = (namespace_t) declaration->namespc;
2538 if(namespc != NAMESPACE_NORMAL) {
2539 return record_declaration(declaration);
2542 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2543 if(previous_declaration == NULL ||
2544 previous_declaration->parent_context != context) {
2545 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2550 if(previous_declaration->type == NULL) {
2551 previous_declaration->type = declaration->type;
2552 previous_declaration->storage_class = declaration->storage_class;
2553 previous_declaration->parent_context = context;
2554 return previous_declaration;
2556 return record_declaration(declaration);
2560 static void parse_declaration(parsed_declaration_func finished_declaration)
2562 declaration_specifiers_t specifiers;
2563 memset(&specifiers, 0, sizeof(specifiers));
2564 parse_declaration_specifiers(&specifiers);
2566 if(token.type == ';') {
2567 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2569 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2570 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2574 static void parse_kr_declaration_list(declaration_t *declaration)
2576 type_t *type = skip_typeref(declaration->type);
2577 if(!is_type_function(type))
2580 if(!type->function.kr_style_parameters)
2583 /* push function parameters */
2584 int top = environment_top();
2585 context_t *last_context = context;
2586 set_context(&declaration->context);
2588 declaration_t *parameter = declaration->context.declarations;
2589 for( ; parameter != NULL; parameter = parameter->next) {
2590 assert(parameter->parent_context == NULL);
2591 parameter->parent_context = context;
2592 environment_push(parameter);
2595 /* parse declaration list */
2596 while(is_declaration_specifier(&token, false)) {
2597 parse_declaration(finished_kr_declaration);
2600 /* pop function parameters */
2601 assert(context == &declaration->context);
2602 set_context(last_context);
2603 environment_pop_to(top);
2605 /* update function type */
2606 type_t *new_type = duplicate_type(type);
2607 new_type->function.kr_style_parameters = false;
2609 function_parameter_t *parameters = NULL;
2610 function_parameter_t *last_parameter = NULL;
2612 declaration_t *parameter_declaration = declaration->context.declarations;
2613 for( ; parameter_declaration != NULL;
2614 parameter_declaration = parameter_declaration->next) {
2615 type_t *parameter_type = parameter_declaration->type;
2616 if(parameter_type == NULL) {
2618 errorf(HERE, "no type specified for function parameter '%Y'",
2619 parameter_declaration->symbol);
2621 if (warning.implicit_int) {
2622 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
2623 parameter_declaration->symbol);
2625 parameter_type = type_int;
2626 parameter_declaration->type = parameter_type;
2630 semantic_parameter(parameter_declaration);
2631 parameter_type = parameter_declaration->type;
2633 function_parameter_t *function_parameter
2634 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2635 memset(function_parameter, 0, sizeof(function_parameter[0]));
2637 function_parameter->type = parameter_type;
2638 if(last_parameter != NULL) {
2639 last_parameter->next = function_parameter;
2641 parameters = function_parameter;
2643 last_parameter = function_parameter;
2645 new_type->function.parameters = parameters;
2647 type = typehash_insert(new_type);
2648 if(type != new_type) {
2649 obstack_free(type_obst, new_type);
2652 declaration->type = type;
2656 * Check if all labels are defined in the current function.
2658 static void check_for_missing_labels(void)
2660 bool first_err = true;
2661 for (const goto_statement_t *goto_statement = goto_first;
2662 goto_statement != NULL;
2663 goto_statement = goto_statement->next) {
2664 const declaration_t *label = goto_statement->label;
2666 if (label->source_position.input_name == NULL) {
2669 diagnosticf("%s: In function '%Y':\n",
2670 current_function->source_position.input_name,
2671 current_function->symbol);
2673 errorf(goto_statement->statement.source_position,
2674 "label '%Y' used but not defined", label->symbol);
2677 goto_first = goto_last = NULL;
2680 static void parse_external_declaration(void)
2682 /* function-definitions and declarations both start with declaration
2684 declaration_specifiers_t specifiers;
2685 memset(&specifiers, 0, sizeof(specifiers));
2686 parse_declaration_specifiers(&specifiers);
2688 /* must be a declaration */
2689 if(token.type == ';') {
2690 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2694 /* declarator is common to both function-definitions and declarations */
2695 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2697 /* must be a declaration */
2698 if(token.type == ',' || token.type == '=' || token.type == ';') {
2699 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2703 /* must be a function definition */
2704 parse_kr_declaration_list(ndeclaration);
2706 if(token.type != '{') {
2707 parse_error_expected("while parsing function definition", '{', 0);
2712 type_t *type = ndeclaration->type;
2714 /* note that we don't skip typerefs: the standard doesn't allow them here
2715 * (so we can't use is_type_function here) */
2716 if(type->kind != TYPE_FUNCTION) {
2717 if (is_type_valid(type)) {
2718 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2719 type, ndeclaration->symbol);
2725 /* § 6.7.5.3 (14) a function definition with () means no
2726 * parameters (and not unspecified parameters) */
2727 if(type->function.unspecified_parameters) {
2728 type_t *duplicate = duplicate_type(type);
2729 duplicate->function.unspecified_parameters = false;
2731 type = typehash_insert(duplicate);
2732 if(type != duplicate) {
2733 obstack_free(type_obst, duplicate);
2735 ndeclaration->type = type;
2738 declaration_t *const declaration = record_function_definition(ndeclaration);
2739 if(ndeclaration != declaration) {
2740 declaration->context = ndeclaration->context;
2742 type = skip_typeref(declaration->type);
2744 /* push function parameters and switch context */
2745 int top = environment_top();
2746 context_t *last_context = context;
2747 set_context(&declaration->context);
2749 declaration_t *parameter = declaration->context.declarations;
2750 for( ; parameter != NULL; parameter = parameter->next) {
2751 if(parameter->parent_context == &ndeclaration->context) {
2752 parameter->parent_context = context;
2754 assert(parameter->parent_context == NULL
2755 || parameter->parent_context == context);
2756 parameter->parent_context = context;
2757 environment_push(parameter);
2760 if(declaration->init.statement != NULL) {
2761 parser_error_multiple_definition(declaration, token.source_position);
2763 goto end_of_parse_external_declaration;
2765 /* parse function body */
2766 int label_stack_top = label_top();
2767 declaration_t *old_current_function = current_function;
2768 current_function = declaration;
2770 declaration->init.statement = parse_compound_statement();
2771 check_for_missing_labels();
2773 assert(current_function == declaration);
2774 current_function = old_current_function;
2775 label_pop_to(label_stack_top);
2778 end_of_parse_external_declaration:
2779 assert(context == &declaration->context);
2780 set_context(last_context);
2781 environment_pop_to(top);
2784 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2786 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2787 type->bitfield.base = base;
2788 type->bitfield.size = size;
2793 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2795 /* TODO: check constraints for struct declarations (in specifiers) */
2797 declaration_t *declaration;
2799 if(token.type == ':') {
2802 type_t *base_type = specifiers->type;
2803 expression_t *size = parse_constant_expression();
2805 type_t *type = make_bitfield_type(base_type, size);
2807 declaration = allocate_declaration_zero();
2808 declaration->namespc = NAMESPACE_NORMAL;
2809 declaration->storage_class = STORAGE_CLASS_NONE;
2810 declaration->source_position = token.source_position;
2811 declaration->modifiers = specifiers->decl_modifiers;
2812 declaration->type = type;
2814 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2816 if(token.type == ':') {
2818 expression_t *size = parse_constant_expression();
2820 type_t *type = make_bitfield_type(declaration->type, size);
2821 declaration->type = type;
2824 record_declaration(declaration);
2826 if(token.type != ',')
2833 static void parse_compound_type_entries(void)
2837 while(token.type != '}' && token.type != T_EOF) {
2838 declaration_specifiers_t specifiers;
2839 memset(&specifiers, 0, sizeof(specifiers));
2840 parse_declaration_specifiers(&specifiers);
2842 parse_struct_declarators(&specifiers);
2844 if(token.type == T_EOF) {
2845 errorf(HERE, "EOF while parsing struct");
2850 static type_t *parse_typename(void)
2852 declaration_specifiers_t specifiers;
2853 memset(&specifiers, 0, sizeof(specifiers));
2854 parse_declaration_specifiers(&specifiers);
2855 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2856 /* TODO: improve error message, user does probably not know what a
2857 * storage class is...
2859 errorf(HERE, "typename may not have a storage class");
2862 type_t *result = parse_abstract_declarator(specifiers.type);
2870 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2871 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2872 expression_t *left);
2874 typedef struct expression_parser_function_t expression_parser_function_t;
2875 struct expression_parser_function_t {
2876 unsigned precedence;
2877 parse_expression_function parser;
2878 unsigned infix_precedence;
2879 parse_expression_infix_function infix_parser;
2882 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2885 * Creates a new invalid expression.
2887 static expression_t *create_invalid_expression(void)
2889 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2890 expression->base.source_position = token.source_position;
2895 * Prints an error message if an expression was expected but not read
2897 static expression_t *expected_expression_error(void)
2899 /* skip the error message if the error token was read */
2900 if (token.type != T_ERROR) {
2901 errorf(HERE, "expected expression, got token '%K'", &token);
2905 return create_invalid_expression();
2909 * Parse a string constant.
2911 static expression_t *parse_string_const(void)
2913 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2914 cnst->base.datatype = type_string;
2915 cnst->string.value = parse_string_literals();
2921 * Parse a wide string constant.
2923 static expression_t *parse_wide_string_const(void)
2925 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2926 cnst->base.datatype = type_wchar_t_ptr;
2927 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2933 * Parse an integer constant.
2935 static expression_t *parse_int_const(void)
2937 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2938 cnst->base.datatype = token.datatype;
2939 cnst->conste.v.int_value = token.v.intvalue;
2947 * Parse a float constant.
2949 static expression_t *parse_float_const(void)
2951 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2952 cnst->base.datatype = token.datatype;
2953 cnst->conste.v.float_value = token.v.floatvalue;
2960 static declaration_t *create_implicit_function(symbol_t *symbol,
2961 const source_position_t source_position)
2963 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2964 ntype->function.return_type = type_int;
2965 ntype->function.unspecified_parameters = true;
2967 type_t *type = typehash_insert(ntype);
2972 declaration_t *const declaration = allocate_declaration_zero();
2973 declaration->storage_class = STORAGE_CLASS_EXTERN;
2974 declaration->type = type;
2975 declaration->symbol = symbol;
2976 declaration->source_position = source_position;
2977 declaration->parent_context = global_context;
2979 context_t *old_context = context;
2980 set_context(global_context);
2982 environment_push(declaration);
2983 /* prepend the declaration to the global declarations list */
2984 declaration->next = context->declarations;
2985 context->declarations = declaration;
2987 assert(context == global_context);
2988 set_context(old_context);
2994 * Creates a return_type (func)(argument_type) function type if not
2997 * @param return_type the return type
2998 * @param argument_type the argument type
3000 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3002 function_parameter_t *parameter
3003 = obstack_alloc(type_obst, sizeof(parameter[0]));
3004 memset(parameter, 0, sizeof(parameter[0]));
3005 parameter->type = argument_type;
3007 type_t *type = allocate_type_zero(TYPE_FUNCTION);
3008 type->function.return_type = return_type;
3009 type->function.parameters = parameter;
3011 type_t *result = typehash_insert(type);
3012 if(result != type) {
3020 * Creates a function type for some function like builtins.
3022 * @param symbol the symbol describing the builtin
3024 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3026 switch(symbol->ID) {
3027 case T___builtin_alloca:
3028 return make_function_1_type(type_void_ptr, type_size_t);
3029 case T___builtin_nan:
3030 return make_function_1_type(type_double, type_string);
3031 case T___builtin_nanf:
3032 return make_function_1_type(type_float, type_string);
3033 case T___builtin_nand:
3034 return make_function_1_type(type_long_double, type_string);
3035 case T___builtin_va_end:
3036 return make_function_1_type(type_void, type_valist);
3038 panic("not implemented builtin symbol found");
3043 * Performs automatic type cast as described in § 6.3.2.1.
3045 * @param orig_type the original type
3047 static type_t *automatic_type_conversion(type_t *orig_type)
3049 type_t *type = skip_typeref(orig_type);
3050 if(is_type_array(type)) {
3051 array_type_t *array_type = &type->array;
3052 type_t *element_type = array_type->element_type;
3053 unsigned qualifiers = array_type->type.qualifiers;
3055 return make_pointer_type(element_type, qualifiers);
3058 if(is_type_function(type)) {
3059 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3066 * reverts the automatic casts of array to pointer types and function
3067 * to function-pointer types as defined § 6.3.2.1
3069 type_t *revert_automatic_type_conversion(const expression_t *expression)
3071 switch (expression->kind) {
3072 case EXPR_REFERENCE: return expression->reference.declaration->type;
3073 case EXPR_SELECT: return expression->select.compound_entry->type;
3075 case EXPR_UNARY_DEREFERENCE: {
3076 const expression_t *const value = expression->unary.value;
3077 type_t *const type = skip_typeref(value->base.datatype);
3078 assert(is_type_pointer(type));
3079 return type->pointer.points_to;
3082 case EXPR_BUILTIN_SYMBOL:
3083 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3085 case EXPR_ARRAY_ACCESS: {
3086 const expression_t *const array_ref = expression->array_access.array_ref;
3087 type_t *const type_left = skip_typeref(array_ref->base.datatype);
3088 if (!is_type_valid(type_left))
3090 assert(is_type_pointer(type_left));
3091 return type_left->pointer.points_to;
3097 return expression->base.datatype;
3100 static expression_t *parse_reference(void)
3102 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3104 reference_expression_t *ref = &expression->reference;
3105 ref->symbol = token.v.symbol;
3107 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3109 source_position_t source_position = token.source_position;
3112 if(declaration == NULL) {
3113 if (! strict_mode && token.type == '(') {
3114 /* an implicitly defined function */
3115 if (warning.implicit_function_declaration) {
3116 warningf(HERE, "implicit declaration of function '%Y'",
3120 declaration = create_implicit_function(ref->symbol,
3123 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3128 type_t *type = declaration->type;
3130 /* we always do the auto-type conversions; the & and sizeof parser contains
3131 * code to revert this! */
3132 type = automatic_type_conversion(type);
3134 ref->declaration = declaration;
3135 ref->expression.datatype = type;
3140 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3144 /* TODO check if explicit cast is allowed and issue warnings/errors */
3147 static expression_t *parse_cast(void)
3149 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3151 cast->base.source_position = token.source_position;
3153 type_t *type = parse_typename();
3156 expression_t *value = parse_sub_expression(20);
3158 check_cast_allowed(value, type);
3160 cast->base.datatype = type;
3161 cast->unary.value = value;
3166 static expression_t *parse_statement_expression(void)
3168 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3170 statement_t *statement = parse_compound_statement();
3171 expression->statement.statement = statement;
3172 expression->base.source_position = statement->base.source_position;
3174 /* find last statement and use its type */
3175 type_t *type = type_void;
3176 const statement_t *stmt = statement->compound.statements;
3178 while (stmt->base.next != NULL)
3179 stmt = stmt->base.next;
3181 if (stmt->kind == STATEMENT_EXPRESSION) {
3182 type = stmt->expression.expression->base.datatype;
3185 warningf(expression->base.source_position, "empty statement expression ({})");
3187 expression->base.datatype = type;
3194 static expression_t *parse_brace_expression(void)
3198 switch(token.type) {
3200 /* gcc extension: a statement expression */
3201 return parse_statement_expression();
3205 return parse_cast();
3207 if(is_typedef_symbol(token.v.symbol)) {
3208 return parse_cast();
3212 expression_t *result = parse_expression();
3218 static expression_t *parse_function_keyword(void)
3223 if (current_function == NULL) {
3224 errorf(HERE, "'__func__' used outside of a function");
3227 string_literal_expression_t *expression
3228 = allocate_ast_zero(sizeof(expression[0]));
3230 expression->expression.kind = EXPR_FUNCTION;
3231 expression->expression.datatype = type_string;
3233 return (expression_t*) expression;
3236 static expression_t *parse_pretty_function_keyword(void)
3238 eat(T___PRETTY_FUNCTION__);
3241 if (current_function == NULL) {
3242 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3245 string_literal_expression_t *expression
3246 = allocate_ast_zero(sizeof(expression[0]));
3248 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3249 expression->expression.datatype = type_string;
3251 return (expression_t*) expression;
3254 static designator_t *parse_designator(void)
3256 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3258 if(token.type != T_IDENTIFIER) {
3259 parse_error_expected("while parsing member designator",
3264 result->symbol = token.v.symbol;
3267 designator_t *last_designator = result;
3269 if(token.type == '.') {
3271 if(token.type != T_IDENTIFIER) {
3272 parse_error_expected("while parsing member designator",
3277 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3278 designator->symbol = token.v.symbol;
3281 last_designator->next = designator;
3282 last_designator = designator;
3285 if(token.type == '[') {
3287 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3288 designator->array_access = parse_expression();
3289 if(designator->array_access == NULL) {
3295 last_designator->next = designator;
3296 last_designator = designator;
3305 static expression_t *parse_offsetof(void)
3307 eat(T___builtin_offsetof);
3309 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3310 expression->base.datatype = type_size_t;
3313 expression->offsetofe.type = parse_typename();
3315 expression->offsetofe.designator = parse_designator();
3321 static expression_t *parse_va_start(void)
3323 eat(T___builtin_va_start);
3325 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3328 expression->va_starte.ap = parse_assignment_expression();
3330 expression_t *const expr = parse_assignment_expression();
3331 if (expr->kind == EXPR_REFERENCE) {
3332 declaration_t *const decl = expr->reference.declaration;
3333 if (decl->parent_context == ¤t_function->context &&
3334 decl->next == NULL) {
3335 expression->va_starte.parameter = decl;
3340 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3342 return create_invalid_expression();
3345 static expression_t *parse_va_arg(void)
3347 eat(T___builtin_va_arg);
3349 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3352 expression->va_arge.ap = parse_assignment_expression();
3354 expression->base.datatype = parse_typename();
3360 static expression_t *parse_builtin_symbol(void)
3362 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3364 symbol_t *symbol = token.v.symbol;
3366 expression->builtin_symbol.symbol = symbol;
3369 type_t *type = get_builtin_symbol_type(symbol);
3370 type = automatic_type_conversion(type);
3372 expression->base.datatype = type;
3376 static expression_t *parse_builtin_constant(void)
3378 eat(T___builtin_constant_p);
3380 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3383 expression->builtin_constant.value = parse_assignment_expression();
3385 expression->base.datatype = type_int;
3390 static expression_t *parse_builtin_prefetch(void)
3392 eat(T___builtin_prefetch);
3394 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3397 expression->builtin_prefetch.adr = parse_assignment_expression();
3398 if (token.type == ',') {
3400 expression->builtin_prefetch.rw = parse_assignment_expression();
3402 if (token.type == ',') {
3404 expression->builtin_prefetch.locality = parse_assignment_expression();
3407 expression->base.datatype = type_void;
3412 static expression_t *parse_compare_builtin(void)
3414 expression_t *expression;
3416 switch(token.type) {
3417 case T___builtin_isgreater:
3418 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3420 case T___builtin_isgreaterequal:
3421 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3423 case T___builtin_isless:
3424 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3426 case T___builtin_islessequal:
3427 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3429 case T___builtin_islessgreater:
3430 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3432 case T___builtin_isunordered:
3433 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3436 panic("invalid compare builtin found");
3442 expression->binary.left = parse_assignment_expression();
3444 expression->binary.right = parse_assignment_expression();
3447 type_t *const orig_type_left = expression->binary.left->base.datatype;
3448 type_t *const orig_type_right = expression->binary.right->base.datatype;
3450 type_t *const type_left = skip_typeref(orig_type_left);
3451 type_t *const type_right = skip_typeref(orig_type_right);
3452 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3453 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3454 type_error_incompatible("invalid operands in comparison",
3455 token.source_position, orig_type_left, orig_type_right);
3458 semantic_comparison(&expression->binary);
3464 static expression_t *parse_builtin_expect(void)
3466 eat(T___builtin_expect);
3468 expression_t *expression
3469 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3472 expression->binary.left = parse_assignment_expression();
3474 expression->binary.right = parse_constant_expression();
3477 expression->base.datatype = expression->binary.left->base.datatype;
3482 static expression_t *parse_assume(void) {
3485 expression_t *expression
3486 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3489 expression->unary.value = parse_assignment_expression();
3492 expression->base.datatype = type_void;
3496 static expression_t *parse_alignof(void) {
3499 expression_t *expression
3500 = allocate_expression_zero(EXPR_ALIGNOF);
3503 expression->alignofe.type = parse_typename();
3506 expression->base.datatype = type_size_t;
3510 static expression_t *parse_primary_expression(void)
3512 switch(token.type) {
3514 return parse_int_const();
3515 case T_FLOATINGPOINT:
3516 return parse_float_const();
3517 case T_STRING_LITERAL:
3518 return parse_string_const();
3519 case T_WIDE_STRING_LITERAL:
3520 return parse_wide_string_const();
3522 return parse_reference();
3523 case T___FUNCTION__:
3525 return parse_function_keyword();
3526 case T___PRETTY_FUNCTION__:
3527 return parse_pretty_function_keyword();
3528 case T___builtin_offsetof:
3529 return parse_offsetof();
3530 case T___builtin_va_start:
3531 return parse_va_start();
3532 case T___builtin_va_arg:
3533 return parse_va_arg();
3534 case T___builtin_expect:
3535 return parse_builtin_expect();
3536 case T___builtin_nanf:
3537 case T___builtin_alloca:
3538 case T___builtin_va_end:
3539 return parse_builtin_symbol();
3540 case T___builtin_isgreater:
3541 case T___builtin_isgreaterequal:
3542 case T___builtin_isless:
3543 case T___builtin_islessequal:
3544 case T___builtin_islessgreater:
3545 case T___builtin_isunordered:
3546 return parse_compare_builtin();
3547 case T___builtin_constant_p:
3548 return parse_builtin_constant();
3549 case T___builtin_prefetch:
3550 return parse_builtin_prefetch();
3552 return parse_alignof();
3554 return parse_assume();
3557 return parse_brace_expression();
3560 errorf(HERE, "unexpected token '%K'", &token);
3563 return create_invalid_expression();
3567 * Check if the expression has the character type and issue a warning then.
3569 static void check_for_char_index_type(const expression_t *expression) {
3570 type_t *const type = expression->base.datatype;
3571 const type_t *const base_type = skip_typeref(type);
3573 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
3574 warning.char_subscripts) {
3575 warningf(expression->base.source_position,
3576 "array subscript has type '%T'", type);
3580 static expression_t *parse_array_expression(unsigned precedence,
3587 expression_t *inside = parse_expression();
3589 array_access_expression_t *array_access
3590 = allocate_ast_zero(sizeof(array_access[0]));
3592 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3594 type_t *const orig_type_left = left->base.datatype;
3595 type_t *const orig_type_inside = inside->base.datatype;
3597 type_t *const type_left = skip_typeref(orig_type_left);
3598 type_t *const type_inside = skip_typeref(orig_type_inside);
3600 type_t *return_type;
3601 if (is_type_pointer(type_left)) {
3602 return_type = type_left->pointer.points_to;
3603 array_access->array_ref = left;
3604 array_access->index = inside;
3605 check_for_char_index_type(inside);
3606 } else if (is_type_pointer(type_inside)) {
3607 return_type = type_inside->pointer.points_to;
3608 array_access->array_ref = inside;
3609 array_access->index = left;
3610 array_access->flipped = true;
3611 check_for_char_index_type(left);
3613 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3615 "array access on object with non-pointer types '%T', '%T'",
3616 orig_type_left, orig_type_inside);
3618 return_type = type_error_type;
3619 array_access->array_ref = create_invalid_expression();
3622 if(token.type != ']') {
3623 parse_error_expected("Problem while parsing array access", ']', 0);
3624 return (expression_t*) array_access;
3628 return_type = automatic_type_conversion(return_type);
3629 array_access->expression.datatype = return_type;
3631 return (expression_t*) array_access;
3634 static expression_t *parse_sizeof(unsigned precedence)
3638 sizeof_expression_t *sizeof_expression
3639 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3640 sizeof_expression->expression.kind = EXPR_SIZEOF;
3641 sizeof_expression->expression.datatype = type_size_t;
3643 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3645 sizeof_expression->type = parse_typename();
3648 expression_t *expression = parse_sub_expression(precedence);
3649 expression->base.datatype = revert_automatic_type_conversion(expression);
3651 sizeof_expression->type = expression->base.datatype;
3652 sizeof_expression->size_expression = expression;
3655 return (expression_t*) sizeof_expression;
3658 static expression_t *parse_select_expression(unsigned precedence,
3659 expression_t *compound)
3662 assert(token.type == '.' || token.type == T_MINUSGREATER);
3664 bool is_pointer = (token.type == T_MINUSGREATER);
3667 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3668 select->select.compound = compound;
3670 if(token.type != T_IDENTIFIER) {
3671 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3674 symbol_t *symbol = token.v.symbol;
3675 select->select.symbol = symbol;
3678 type_t *const orig_type = compound->base.datatype;
3679 type_t *const type = skip_typeref(orig_type);
3681 type_t *type_left = type;
3683 if (!is_type_pointer(type)) {
3684 if (is_type_valid(type)) {
3685 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3687 return create_invalid_expression();
3689 type_left = type->pointer.points_to;
3691 type_left = skip_typeref(type_left);
3693 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3694 type_left->kind != TYPE_COMPOUND_UNION) {
3695 if (is_type_valid(type_left)) {
3696 errorf(HERE, "request for member '%Y' in something not a struct or "
3697 "union, but '%T'", symbol, type_left);
3699 return create_invalid_expression();
3702 declaration_t *const declaration = type_left->compound.declaration;
3704 if(!declaration->init.is_defined) {
3705 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3707 return create_invalid_expression();
3710 declaration_t *iter = declaration->context.declarations;
3711 for( ; iter != NULL; iter = iter->next) {
3712 if(iter->symbol == symbol) {
3717 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3718 return create_invalid_expression();
3721 /* we always do the auto-type conversions; the & and sizeof parser contains
3722 * code to revert this! */
3723 type_t *expression_type = automatic_type_conversion(iter->type);
3725 select->select.compound_entry = iter;
3726 select->base.datatype = expression_type;
3728 if(expression_type->kind == TYPE_BITFIELD) {
3729 expression_t *extract
3730 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3731 extract->unary.value = select;
3732 extract->base.datatype = expression_type->bitfield.base;
3741 * Parse a call expression, ie. expression '( ... )'.
3743 * @param expression the function address
3745 static expression_t *parse_call_expression(unsigned precedence,
3746 expression_t *expression)
3749 expression_t *result = allocate_expression_zero(EXPR_CALL);
3751 call_expression_t *call = &result->call;
3752 call->function = expression;
3754 type_t *const orig_type = expression->base.datatype;
3755 type_t *const type = skip_typeref(orig_type);
3757 function_type_t *function_type = NULL;
3758 if (is_type_pointer(type)) {
3759 type_t *const to_type = skip_typeref(type->pointer.points_to);
3761 if (is_type_function(to_type)) {
3762 function_type = &to_type->function;
3763 call->expression.datatype = function_type->return_type;
3767 if (function_type == NULL && is_type_valid(type)) {
3768 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3771 /* parse arguments */
3774 if(token.type != ')') {
3775 call_argument_t *last_argument = NULL;
3778 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3780 argument->expression = parse_assignment_expression();
3781 if(last_argument == NULL) {
3782 call->arguments = argument;
3784 last_argument->next = argument;
3786 last_argument = argument;
3788 if(token.type != ',')
3795 if(function_type != NULL) {
3796 function_parameter_t *parameter = function_type->parameters;
3797 call_argument_t *argument = call->arguments;
3798 for( ; parameter != NULL && argument != NULL;
3799 parameter = parameter->next, argument = argument->next) {
3800 type_t *expected_type = parameter->type;
3801 /* TODO report context in error messages */
3802 expression_t *const arg_expr = argument->expression;
3803 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
3804 if (res_type == NULL) {
3805 /* TODO improve error message */
3806 errorf(arg_expr->base.source_position,
3807 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
3808 arg_expr, arg_expr->base.datatype, expected_type);
3810 argument->expression = create_implicit_cast(argument->expression, expected_type);
3813 /* too few parameters */
3814 if(parameter != NULL) {
3815 errorf(HERE, "too few arguments to function '%E'", expression);
3816 } else if(argument != NULL) {
3817 /* too many parameters */
3818 if(!function_type->variadic
3819 && !function_type->unspecified_parameters) {
3820 errorf(HERE, "too many arguments to function '%E'", expression);
3822 /* do default promotion */
3823 for( ; argument != NULL; argument = argument->next) {
3824 type_t *type = argument->expression->base.datatype;
3826 type = skip_typeref(type);
3827 if(is_type_integer(type)) {
3828 type = promote_integer(type);
3829 } else if(type == type_float) {
3833 argument->expression
3834 = create_implicit_cast(argument->expression, type);
3837 check_format(&result->call);
3840 check_format(&result->call);
3847 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3849 static bool same_compound_type(const type_t *type1, const type_t *type2)
3852 is_type_compound(type1) &&
3853 type1->kind == type2->kind &&
3854 type1->compound.declaration == type2->compound.declaration;
3858 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3860 * @param expression the conditional expression
3862 static expression_t *parse_conditional_expression(unsigned precedence,
3863 expression_t *expression)
3867 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3869 conditional_expression_t *conditional = &result->conditional;
3870 conditional->condition = expression;
3873 type_t *const condition_type_orig = expression->base.datatype;
3874 type_t *const condition_type = skip_typeref(condition_type_orig);
3875 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
3876 type_error("expected a scalar type in conditional condition",
3877 expression->base.source_position, condition_type_orig);
3880 expression_t *true_expression = parse_expression();
3882 expression_t *false_expression = parse_sub_expression(precedence);
3884 conditional->true_expression = true_expression;
3885 conditional->false_expression = false_expression;
3887 type_t *const orig_true_type = true_expression->base.datatype;
3888 type_t *const orig_false_type = false_expression->base.datatype;
3889 type_t *const true_type = skip_typeref(orig_true_type);
3890 type_t *const false_type = skip_typeref(orig_false_type);
3893 type_t *result_type;
3894 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3895 result_type = semantic_arithmetic(true_type, false_type);
3897 true_expression = create_implicit_cast(true_expression, result_type);
3898 false_expression = create_implicit_cast(false_expression, result_type);
3900 conditional->true_expression = true_expression;
3901 conditional->false_expression = false_expression;
3902 conditional->expression.datatype = result_type;
3903 } else if (same_compound_type(true_type, false_type) || (
3904 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3905 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
3907 /* just take 1 of the 2 types */
3908 result_type = true_type;
3909 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3910 && pointers_compatible(true_type, false_type)) {
3912 result_type = true_type;
3915 if (is_type_valid(true_type) && is_type_valid(false_type)) {
3916 type_error_incompatible("while parsing conditional",
3917 expression->base.source_position, true_type,
3920 result_type = type_error_type;
3923 conditional->expression.datatype = result_type;
3928 * Parse an extension expression.
3930 static expression_t *parse_extension(unsigned precedence)
3932 eat(T___extension__);
3934 /* TODO enable extensions */
3935 expression_t *expression = parse_sub_expression(precedence);
3936 /* TODO disable extensions */
3940 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3942 eat(T___builtin_classify_type);
3944 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3945 result->base.datatype = type_int;
3948 expression_t *expression = parse_sub_expression(precedence);
3950 result->classify_type.type_expression = expression;
3955 static void semantic_incdec(unary_expression_t *expression)
3957 type_t *const orig_type = expression->value->base.datatype;
3958 type_t *const type = skip_typeref(orig_type);
3959 /* TODO !is_type_real && !is_type_pointer */
3960 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3961 if (is_type_valid(type)) {
3962 /* TODO: improve error message */
3963 errorf(HERE, "operation needs an arithmetic or pointer type");
3968 expression->expression.datatype = orig_type;
3971 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3973 type_t *const orig_type = expression->value->base.datatype;
3974 type_t *const type = skip_typeref(orig_type);
3975 if(!is_type_arithmetic(type)) {
3976 if (is_type_valid(type)) {
3977 /* TODO: improve error message */
3978 errorf(HERE, "operation needs an arithmetic type");
3983 expression->expression.datatype = orig_type;
3986 static void semantic_unexpr_scalar(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_scalar(type)) {
3991 if (is_type_valid(type)) {
3992 errorf(HERE, "operand of ! must be of scalar type");
3997 expression->expression.datatype = orig_type;
4000 static void semantic_unexpr_integer(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_integer(type)) {
4005 if (is_type_valid(type)) {
4006 errorf(HERE, "operand of ~ must be of integer type");
4011 expression->expression.datatype = orig_type;
4014 static void semantic_dereference(unary_expression_t *expression)
4016 type_t *const orig_type = expression->value->base.datatype;
4017 type_t *const type = skip_typeref(orig_type);
4018 if(!is_type_pointer(type)) {
4019 if (is_type_valid(type)) {
4020 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4025 type_t *result_type = type->pointer.points_to;
4026 result_type = automatic_type_conversion(result_type);
4027 expression->expression.datatype = result_type;
4031 * Check the semantic of the address taken expression.
4033 static void semantic_take_addr(unary_expression_t *expression)
4035 expression_t *value = expression->value;
4036 value->base.datatype = revert_automatic_type_conversion(value);
4038 type_t *orig_type = value->base.datatype;
4039 if(!is_type_valid(orig_type))
4042 if(value->kind == EXPR_REFERENCE) {
4043 declaration_t *const declaration = value->reference.declaration;
4044 if(declaration != NULL) {
4045 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4046 errorf(expression->expression.source_position,
4047 "address of register variable '%Y' requested",
4048 declaration->symbol);
4050 declaration->address_taken = 1;
4054 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4057 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4058 static expression_t *parse_##unexpression_type(unsigned precedence) \
4062 expression_t *unary_expression \
4063 = allocate_expression_zero(unexpression_type); \
4064 unary_expression->base.source_position = HERE; \
4065 unary_expression->unary.value = parse_sub_expression(precedence); \
4067 sfunc(&unary_expression->unary); \
4069 return unary_expression; \
4072 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4073 semantic_unexpr_arithmetic)
4074 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4075 semantic_unexpr_arithmetic)
4076 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4077 semantic_unexpr_scalar)
4078 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4079 semantic_dereference)
4080 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4082 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4083 semantic_unexpr_integer)
4084 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4086 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4089 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4091 static expression_t *parse_##unexpression_type(unsigned precedence, \
4092 expression_t *left) \
4094 (void) precedence; \
4097 expression_t *unary_expression \
4098 = allocate_expression_zero(unexpression_type); \
4099 unary_expression->unary.value = left; \
4101 sfunc(&unary_expression->unary); \
4103 return unary_expression; \
4106 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4107 EXPR_UNARY_POSTFIX_INCREMENT,
4109 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4110 EXPR_UNARY_POSTFIX_DECREMENT,
4113 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4115 /* TODO: handle complex + imaginary types */
4117 /* § 6.3.1.8 Usual arithmetic conversions */
4118 if(type_left == type_long_double || type_right == type_long_double) {
4119 return type_long_double;
4120 } else if(type_left == type_double || type_right == type_double) {
4122 } else if(type_left == type_float || type_right == type_float) {
4126 type_right = promote_integer(type_right);
4127 type_left = promote_integer(type_left);
4129 if(type_left == type_right)
4132 bool signed_left = is_type_signed(type_left);
4133 bool signed_right = is_type_signed(type_right);
4134 int rank_left = get_rank(type_left);
4135 int rank_right = get_rank(type_right);
4136 if(rank_left < rank_right) {
4137 if(signed_left == signed_right || !signed_right) {
4143 if(signed_left == signed_right || !signed_left) {
4152 * Check the semantic restrictions for a binary expression.
4154 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4156 expression_t *const left = expression->left;
4157 expression_t *const right = expression->right;
4158 type_t *const orig_type_left = left->base.datatype;
4159 type_t *const orig_type_right = right->base.datatype;
4160 type_t *const type_left = skip_typeref(orig_type_left);
4161 type_t *const type_right = skip_typeref(orig_type_right);
4163 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4164 /* TODO: improve error message */
4165 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4166 errorf(HERE, "operation needs arithmetic types");
4171 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4172 expression->left = create_implicit_cast(left, arithmetic_type);
4173 expression->right = create_implicit_cast(right, arithmetic_type);
4174 expression->expression.datatype = arithmetic_type;
4177 static void semantic_shift_op(binary_expression_t *expression)
4179 expression_t *const left = expression->left;
4180 expression_t *const right = expression->right;
4181 type_t *const orig_type_left = left->base.datatype;
4182 type_t *const orig_type_right = right->base.datatype;
4183 type_t * type_left = skip_typeref(orig_type_left);
4184 type_t * type_right = skip_typeref(orig_type_right);
4186 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4187 /* TODO: improve error message */
4188 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4189 errorf(HERE, "operation needs integer types");
4194 type_left = promote_integer(type_left);
4195 type_right = promote_integer(type_right);
4197 expression->left = create_implicit_cast(left, type_left);
4198 expression->right = create_implicit_cast(right, type_right);
4199 expression->expression.datatype = type_left;
4202 static void semantic_add(binary_expression_t *expression)
4204 expression_t *const left = expression->left;
4205 expression_t *const right = expression->right;
4206 type_t *const orig_type_left = left->base.datatype;
4207 type_t *const orig_type_right = right->base.datatype;
4208 type_t *const type_left = skip_typeref(orig_type_left);
4209 type_t *const type_right = skip_typeref(orig_type_right);
4212 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4213 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4214 expression->left = create_implicit_cast(left, arithmetic_type);
4215 expression->right = create_implicit_cast(right, arithmetic_type);
4216 expression->expression.datatype = arithmetic_type;
4218 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4219 expression->expression.datatype = type_left;
4220 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4221 expression->expression.datatype = type_right;
4222 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4223 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4227 static void semantic_sub(binary_expression_t *expression)
4229 expression_t *const left = expression->left;
4230 expression_t *const right = expression->right;
4231 type_t *const orig_type_left = left->base.datatype;
4232 type_t *const orig_type_right = right->base.datatype;
4233 type_t *const type_left = skip_typeref(orig_type_left);
4234 type_t *const type_right = skip_typeref(orig_type_right);
4237 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4238 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4239 expression->left = create_implicit_cast(left, arithmetic_type);
4240 expression->right = create_implicit_cast(right, arithmetic_type);
4241 expression->expression.datatype = arithmetic_type;
4243 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4244 expression->expression.datatype = type_left;
4245 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4246 if(!pointers_compatible(type_left, type_right)) {
4247 errorf(HERE, "pointers to incompatible objects to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4249 expression->expression.datatype = type_ptrdiff_t;
4251 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4252 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4256 static void semantic_comparison(binary_expression_t *expression)
4258 expression_t *left = expression->left;
4259 expression_t *right = expression->right;
4260 type_t *orig_type_left = left->base.datatype;
4261 type_t *orig_type_right = right->base.datatype;
4263 type_t *type_left = skip_typeref(orig_type_left);
4264 type_t *type_right = skip_typeref(orig_type_right);
4266 /* TODO non-arithmetic types */
4267 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4268 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4269 expression->left = create_implicit_cast(left, arithmetic_type);
4270 expression->right = create_implicit_cast(right, arithmetic_type);
4271 expression->expression.datatype = arithmetic_type;
4272 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4273 /* TODO check compatibility */
4274 } else if (is_type_pointer(type_left)) {
4275 expression->right = create_implicit_cast(right, type_left);
4276 } else if (is_type_pointer(type_right)) {
4277 expression->left = create_implicit_cast(left, type_right);
4278 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4279 type_error_incompatible("invalid operands in comparison",
4280 token.source_position, type_left, type_right);
4282 expression->expression.datatype = type_int;
4285 static void semantic_arithmetic_assign(binary_expression_t *expression)
4287 expression_t *left = expression->left;
4288 expression_t *right = expression->right;
4289 type_t *orig_type_left = left->base.datatype;
4290 type_t *orig_type_right = right->base.datatype;
4292 type_t *type_left = skip_typeref(orig_type_left);
4293 type_t *type_right = skip_typeref(orig_type_right);
4295 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4296 /* TODO: improve error message */
4297 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4298 errorf(HERE, "operation needs arithmetic types");
4303 /* combined instructions are tricky. We can't create an implicit cast on
4304 * the left side, because we need the uncasted form for the store.
4305 * The ast2firm pass has to know that left_type must be right_type
4306 * for the arithmetic operation and create a cast by itself */
4307 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4308 expression->right = create_implicit_cast(right, arithmetic_type);
4309 expression->expression.datatype = type_left;
4312 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4314 expression_t *const left = expression->left;
4315 expression_t *const right = expression->right;
4316 type_t *const orig_type_left = left->base.datatype;
4317 type_t *const orig_type_right = right->base.datatype;
4318 type_t *const type_left = skip_typeref(orig_type_left);
4319 type_t *const type_right = skip_typeref(orig_type_right);
4321 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4322 /* combined instructions are tricky. We can't create an implicit cast on
4323 * the left side, because we need the uncasted form for the store.
4324 * The ast2firm pass has to know that left_type must be right_type
4325 * for the arithmetic operation and create a cast by itself */
4326 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4327 expression->right = create_implicit_cast(right, arithmetic_type);
4328 expression->expression.datatype = type_left;
4329 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4330 expression->expression.datatype = type_left;
4331 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4332 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4337 * Check the semantic restrictions of a logical expression.
4339 static void semantic_logical_op(binary_expression_t *expression)
4341 expression_t *const left = expression->left;
4342 expression_t *const right = expression->right;
4343 type_t *const orig_type_left = left->base.datatype;
4344 type_t *const orig_type_right = right->base.datatype;
4345 type_t *const type_left = skip_typeref(orig_type_left);
4346 type_t *const type_right = skip_typeref(orig_type_right);
4348 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4349 /* TODO: improve error message */
4350 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4351 errorf(HERE, "operation needs scalar types");
4356 expression->expression.datatype = type_int;
4360 * Checks if a compound type has constant fields.
4362 static bool has_const_fields(const compound_type_t *type)
4364 const context_t *context = &type->declaration->context;
4365 const declaration_t *declaration = context->declarations;
4367 for (; declaration != NULL; declaration = declaration->next) {
4368 if (declaration->namespc != NAMESPACE_NORMAL)
4371 const type_t *decl_type = skip_typeref(declaration->type);
4372 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4380 * Check the semantic restrictions of a binary assign expression.
4382 static void semantic_binexpr_assign(binary_expression_t *expression)
4384 expression_t *left = expression->left;
4385 type_t *orig_type_left = left->base.datatype;
4387 type_t *type_left = revert_automatic_type_conversion(left);
4388 type_left = skip_typeref(orig_type_left);
4390 /* must be a modifiable lvalue */
4391 if (is_type_array(type_left)) {
4392 errorf(HERE, "cannot assign to arrays ('%E')", left);
4395 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4396 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4400 if(is_type_incomplete(type_left)) {
4402 "left-hand side of assignment '%E' has incomplete type '%T'",
4403 left, orig_type_left);
4406 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4407 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4408 left, orig_type_left);
4412 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4414 if (res_type == NULL) {
4415 errorf(expression->expression.source_position,
4416 "cannot assign to '%T' from '%T'",
4417 orig_type_left, expression->right->base.datatype);
4419 expression->right = create_implicit_cast(expression->right, res_type);
4422 expression->expression.datatype = orig_type_left;
4425 static bool expression_has_effect(const expression_t *const expr)
4427 switch (expr->kind) {
4428 case EXPR_UNKNOWN: break;
4429 case EXPR_INVALID: break;
4430 case EXPR_REFERENCE: return false;
4431 case EXPR_CONST: return false;
4432 case EXPR_STRING_LITERAL: return false;
4433 case EXPR_WIDE_STRING_LITERAL: return false;
4435 const call_expression_t *const call = &expr->call;
4436 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
4439 switch (call->function->builtin_symbol.symbol->ID) {
4440 case T___builtin_va_end: return true;
4441 default: return false;
4444 case EXPR_CONDITIONAL: {
4445 const conditional_expression_t *const cond = &expr->conditional;
4447 expression_has_effect(cond->true_expression) &&
4448 expression_has_effect(cond->false_expression);
4450 case EXPR_SELECT: return false;
4451 case EXPR_ARRAY_ACCESS: return false;
4452 case EXPR_SIZEOF: return false;
4453 case EXPR_CLASSIFY_TYPE: return false;
4454 case EXPR_ALIGNOF: return false;
4456 case EXPR_FUNCTION: return false;
4457 case EXPR_PRETTY_FUNCTION: return false;
4458 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
4459 case EXPR_BUILTIN_CONSTANT_P: return false;
4460 case EXPR_BUILTIN_PREFETCH: return true;
4461 case EXPR_OFFSETOF: return false;
4462 case EXPR_VA_START: return true;
4463 case EXPR_VA_ARG: return true;
4464 case EXPR_STATEMENT: return true; // TODO
4466 case EXPR_UNARY_NEGATE: return false;
4467 case EXPR_UNARY_PLUS: return false;
4468 case EXPR_UNARY_BITWISE_NEGATE: return false;
4469 case EXPR_UNARY_NOT: return false;
4470 case EXPR_UNARY_DEREFERENCE: return false;
4471 case EXPR_UNARY_TAKE_ADDRESS: return false;
4472 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
4473 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
4474 case EXPR_UNARY_PREFIX_INCREMENT: return true;
4475 case EXPR_UNARY_PREFIX_DECREMENT: return true;
4476 case EXPR_UNARY_CAST:
4477 return is_type_atomic(expr->base.datatype, ATOMIC_TYPE_VOID);
4478 case EXPR_UNARY_CAST_IMPLICIT: return true;
4479 case EXPR_UNARY_ASSUME: return true;
4480 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
4482 case EXPR_BINARY_ADD: return false;
4483 case EXPR_BINARY_SUB: return false;
4484 case EXPR_BINARY_MUL: return false;
4485 case EXPR_BINARY_DIV: return false;
4486 case EXPR_BINARY_MOD: return false;
4487 case EXPR_BINARY_EQUAL: return false;
4488 case EXPR_BINARY_NOTEQUAL: return false;
4489 case EXPR_BINARY_LESS: return false;
4490 case EXPR_BINARY_LESSEQUAL: return false;
4491 case EXPR_BINARY_GREATER: return false;
4492 case EXPR_BINARY_GREATEREQUAL: return false;
4493 case EXPR_BINARY_BITWISE_AND: return false;
4494 case EXPR_BINARY_BITWISE_OR: return false;
4495 case EXPR_BINARY_BITWISE_XOR: return false;
4496 case EXPR_BINARY_SHIFTLEFT: return false;
4497 case EXPR_BINARY_SHIFTRIGHT: return false;
4498 case EXPR_BINARY_ASSIGN: return true;
4499 case EXPR_BINARY_MUL_ASSIGN: return true;
4500 case EXPR_BINARY_DIV_ASSIGN: return true;
4501 case EXPR_BINARY_MOD_ASSIGN: return true;
4502 case EXPR_BINARY_ADD_ASSIGN: return true;
4503 case EXPR_BINARY_SUB_ASSIGN: return true;
4504 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
4505 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
4506 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
4507 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
4508 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
4509 case EXPR_BINARY_LOGICAL_AND:
4510 case EXPR_BINARY_LOGICAL_OR:
4511 case EXPR_BINARY_COMMA:
4512 return expression_has_effect(expr->binary.right);
4514 case EXPR_BINARY_BUILTIN_EXPECT: return true;
4515 case EXPR_BINARY_ISGREATER: return false;
4516 case EXPR_BINARY_ISGREATEREQUAL: return false;
4517 case EXPR_BINARY_ISLESS: return false;
4518 case EXPR_BINARY_ISLESSEQUAL: return false;
4519 case EXPR_BINARY_ISLESSGREATER: return false;
4520 case EXPR_BINARY_ISUNORDERED: return false;
4523 panic("unexpected statement");
4526 static void semantic_comma(binary_expression_t *expression)
4528 if (warning.unused_value) {
4529 const expression_t *const left = expression->left;
4530 if (!expression_has_effect(left)) {
4531 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
4534 expression->expression.datatype = expression->right->base.datatype;
4537 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4538 static expression_t *parse_##binexpression_type(unsigned precedence, \
4539 expression_t *left) \
4543 expression_t *right = parse_sub_expression(precedence + lr); \
4545 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4546 binexpr->binary.left = left; \
4547 binexpr->binary.right = right; \
4548 sfunc(&binexpr->binary); \
4553 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4554 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4555 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4556 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4557 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4558 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4559 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4560 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4561 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4563 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4564 semantic_comparison, 1)
4565 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4566 semantic_comparison, 1)
4567 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4568 semantic_comparison, 1)
4569 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4570 semantic_comparison, 1)
4572 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4573 semantic_binexpr_arithmetic, 1)
4574 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4575 semantic_binexpr_arithmetic, 1)
4576 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4577 semantic_binexpr_arithmetic, 1)
4578 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4579 semantic_logical_op, 1)
4580 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4581 semantic_logical_op, 1)
4582 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4583 semantic_shift_op, 1)
4584 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4585 semantic_shift_op, 1)
4586 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4587 semantic_arithmetic_addsubb_assign, 0)
4588 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4589 semantic_arithmetic_addsubb_assign, 0)
4590 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4591 semantic_arithmetic_assign, 0)
4592 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4593 semantic_arithmetic_assign, 0)
4594 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4595 semantic_arithmetic_assign, 0)
4596 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4597 semantic_arithmetic_assign, 0)
4598 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4599 semantic_arithmetic_assign, 0)
4600 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4601 semantic_arithmetic_assign, 0)
4602 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4603 semantic_arithmetic_assign, 0)
4604 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4605 semantic_arithmetic_assign, 0)
4607 static expression_t *parse_sub_expression(unsigned precedence)
4609 if(token.type < 0) {
4610 return expected_expression_error();
4613 expression_parser_function_t *parser
4614 = &expression_parsers[token.type];
4615 source_position_t source_position = token.source_position;
4618 if(parser->parser != NULL) {
4619 left = parser->parser(parser->precedence);
4621 left = parse_primary_expression();
4623 assert(left != NULL);
4624 left->base.source_position = source_position;
4627 if(token.type < 0) {
4628 return expected_expression_error();
4631 parser = &expression_parsers[token.type];
4632 if(parser->infix_parser == NULL)
4634 if(parser->infix_precedence < precedence)
4637 left = parser->infix_parser(parser->infix_precedence, left);
4639 assert(left != NULL);
4640 assert(left->kind != EXPR_UNKNOWN);
4641 left->base.source_position = source_position;
4648 * Parse an expression.
4650 static expression_t *parse_expression(void)
4652 return parse_sub_expression(1);
4656 * Register a parser for a prefix-like operator with given precedence.
4658 * @param parser the parser function
4659 * @param token_type the token type of the prefix token
4660 * @param precedence the precedence of the operator
4662 static void register_expression_parser(parse_expression_function parser,
4663 int token_type, unsigned precedence)
4665 expression_parser_function_t *entry = &expression_parsers[token_type];
4667 if(entry->parser != NULL) {
4668 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4669 panic("trying to register multiple expression parsers for a token");
4671 entry->parser = parser;
4672 entry->precedence = precedence;
4676 * Register a parser for an infix operator with given precedence.
4678 * @param parser the parser function
4679 * @param token_type the token type of the infix operator
4680 * @param precedence the precedence of the operator
4682 static void register_infix_parser(parse_expression_infix_function parser,
4683 int token_type, unsigned precedence)
4685 expression_parser_function_t *entry = &expression_parsers[token_type];
4687 if(entry->infix_parser != NULL) {
4688 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4689 panic("trying to register multiple infix expression parsers for a "
4692 entry->infix_parser = parser;
4693 entry->infix_precedence = precedence;
4697 * Initialize the expression parsers.
4699 static void init_expression_parsers(void)
4701 memset(&expression_parsers, 0, sizeof(expression_parsers));
4703 register_infix_parser(parse_array_expression, '[', 30);
4704 register_infix_parser(parse_call_expression, '(', 30);
4705 register_infix_parser(parse_select_expression, '.', 30);
4706 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4707 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4709 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4712 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4713 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4714 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4715 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4716 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4717 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4718 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4719 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4720 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4721 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4722 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4723 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4724 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4725 T_EXCLAMATIONMARKEQUAL, 13);
4726 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4727 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4728 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4729 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4730 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4731 register_infix_parser(parse_conditional_expression, '?', 7);
4732 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4733 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4734 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4735 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4736 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4737 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4738 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4739 T_LESSLESSEQUAL, 2);
4740 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4741 T_GREATERGREATEREQUAL, 2);
4742 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4744 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4746 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4749 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4751 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4752 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4753 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4754 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4755 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4756 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4757 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4759 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4761 register_expression_parser(parse_sizeof, T_sizeof, 25);
4762 register_expression_parser(parse_extension, T___extension__, 25);
4763 register_expression_parser(parse_builtin_classify_type,
4764 T___builtin_classify_type, 25);
4768 * Parse a asm statement constraints specification.
4770 static asm_constraint_t *parse_asm_constraints(void)
4772 asm_constraint_t *result = NULL;
4773 asm_constraint_t *last = NULL;
4775 while(token.type == T_STRING_LITERAL || token.type == '[') {
4776 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4777 memset(constraint, 0, sizeof(constraint[0]));
4779 if(token.type == '[') {
4781 if(token.type != T_IDENTIFIER) {
4782 parse_error_expected("while parsing asm constraint",
4786 constraint->symbol = token.v.symbol;
4791 constraint->constraints = parse_string_literals();
4793 constraint->expression = parse_expression();
4797 last->next = constraint;
4799 result = constraint;
4803 if(token.type != ',')
4812 * Parse a asm statement clobber specification.
4814 static asm_clobber_t *parse_asm_clobbers(void)
4816 asm_clobber_t *result = NULL;
4817 asm_clobber_t *last = NULL;
4819 while(token.type == T_STRING_LITERAL) {
4820 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4821 clobber->clobber = parse_string_literals();
4824 last->next = clobber;
4830 if(token.type != ',')
4839 * Parse an asm statement.
4841 static statement_t *parse_asm_statement(void)
4845 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4846 statement->base.source_position = token.source_position;
4848 asm_statement_t *asm_statement = &statement->asms;
4850 if(token.type == T_volatile) {
4852 asm_statement->is_volatile = true;
4856 asm_statement->asm_text = parse_string_literals();
4858 if(token.type != ':')
4862 asm_statement->inputs = parse_asm_constraints();
4863 if(token.type != ':')
4867 asm_statement->outputs = parse_asm_constraints();
4868 if(token.type != ':')
4872 asm_statement->clobbers = parse_asm_clobbers();
4881 * Parse a case statement.
4883 static statement_t *parse_case_statement(void)
4887 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4889 statement->base.source_position = token.source_position;
4890 statement->case_label.expression = parse_expression();
4894 if (! is_constant_expression(statement->case_label.expression)) {
4895 errorf(statement->base.source_position,
4896 "case label does not reduce to an integer constant");
4898 /* TODO: check if the case label is already known */
4899 if (current_switch != NULL) {
4900 /* link all cases into the switch statement */
4901 if (current_switch->last_case == NULL) {
4902 current_switch->first_case =
4903 current_switch->last_case = &statement->case_label;
4905 current_switch->last_case->next = &statement->case_label;
4908 errorf(statement->base.source_position,
4909 "case label not within a switch statement");
4912 statement->case_label.label_statement = parse_statement();
4918 * Finds an existing default label of a switch statement.
4920 static case_label_statement_t *
4921 find_default_label(const switch_statement_t *statement)
4923 for (case_label_statement_t *label = statement->first_case;
4925 label = label->next) {
4926 if (label->expression == NULL)
4933 * Parse a default statement.
4935 static statement_t *parse_default_statement(void)
4939 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4941 statement->base.source_position = token.source_position;
4944 if (current_switch != NULL) {
4945 const case_label_statement_t *def_label = find_default_label(current_switch);
4946 if (def_label != NULL) {
4947 errorf(HERE, "multiple default labels in one switch");
4948 errorf(def_label->statement.source_position,
4949 "this is the first default label");
4951 /* link all cases into the switch statement */
4952 if (current_switch->last_case == NULL) {
4953 current_switch->first_case =
4954 current_switch->last_case = &statement->case_label;
4956 current_switch->last_case->next = &statement->case_label;
4960 errorf(statement->base.source_position,
4961 "'default' label not within a switch statement");
4963 statement->label.label_statement = parse_statement();
4969 * Return the declaration for a given label symbol or create a new one.
4971 static declaration_t *get_label(symbol_t *symbol)
4973 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4974 assert(current_function != NULL);
4975 /* if we found a label in the same function, then we already created the
4977 if(candidate != NULL
4978 && candidate->parent_context == ¤t_function->context) {
4982 /* otherwise we need to create a new one */
4983 declaration_t *const declaration = allocate_declaration_zero();
4984 declaration->namespc = NAMESPACE_LABEL;
4985 declaration->symbol = symbol;
4987 label_push(declaration);
4993 * Parse a label statement.
4995 static statement_t *parse_label_statement(void)
4997 assert(token.type == T_IDENTIFIER);
4998 symbol_t *symbol = token.v.symbol;
5001 declaration_t *label = get_label(symbol);
5003 /* if source position is already set then the label is defined twice,
5004 * otherwise it was just mentioned in a goto so far */
5005 if(label->source_position.input_name != NULL) {
5006 errorf(HERE, "duplicate label '%Y'", symbol);
5007 errorf(label->source_position, "previous definition of '%Y' was here",
5010 label->source_position = token.source_position;
5013 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
5015 label_statement->statement.kind = STATEMENT_LABEL;
5016 label_statement->statement.source_position = token.source_position;
5017 label_statement->label = label;
5021 if(token.type == '}') {
5022 /* TODO only warn? */
5023 errorf(HERE, "label at end of compound statement");
5024 return (statement_t*) label_statement;
5026 if (token.type == ';') {
5027 /* eat an empty statement here, to avoid the warning about an empty
5028 * after a label. label:; is commonly used to have a label before
5032 label_statement->label_statement = parse_statement();
5036 return (statement_t*) label_statement;
5040 * Parse an if statement.
5042 static statement_t *parse_if(void)
5046 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5047 statement->statement.kind = STATEMENT_IF;
5048 statement->statement.source_position = token.source_position;
5051 statement->condition = parse_expression();
5054 statement->true_statement = parse_statement();
5055 if(token.type == T_else) {
5057 statement->false_statement = parse_statement();
5060 return (statement_t*) statement;
5064 * Parse a switch statement.
5066 static statement_t *parse_switch(void)
5070 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5071 statement->statement.kind = STATEMENT_SWITCH;
5072 statement->statement.source_position = token.source_position;
5075 expression_t *const expr = parse_expression();
5076 type_t * type = skip_typeref(expr->base.datatype);
5077 if (is_type_integer(type)) {
5078 type = promote_integer(type);
5079 } else if (is_type_valid(type)) {
5080 errorf(expr->base.source_position, "switch quantity is not an integer, but '%T'", type);
5081 type = type_error_type;
5083 statement->expression = create_implicit_cast(expr, type);
5086 switch_statement_t *rem = current_switch;
5087 current_switch = statement;
5088 statement->body = parse_statement();
5089 current_switch = rem;
5091 if (warning.switch_default && find_default_label(statement) == NULL) {
5092 warningf(statement->statement.source_position, "switch has no default case");
5095 return (statement_t*) statement;
5098 static statement_t *parse_loop_body(statement_t *const loop)
5100 statement_t *const rem = current_loop;
5101 current_loop = loop;
5102 statement_t *const body = parse_statement();
5108 * Parse a while statement.
5110 static statement_t *parse_while(void)
5114 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5115 statement->statement.kind = STATEMENT_WHILE;
5116 statement->statement.source_position = token.source_position;
5119 statement->condition = parse_expression();
5122 statement->body = parse_loop_body((statement_t*)statement);
5124 return (statement_t*) statement;
5128 * Parse a do statement.
5130 static statement_t *parse_do(void)
5134 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5135 statement->statement.kind = STATEMENT_DO_WHILE;
5136 statement->statement.source_position = token.source_position;
5138 statement->body = parse_loop_body((statement_t*)statement);
5141 statement->condition = parse_expression();
5145 return (statement_t*) statement;
5149 * Parse a for statement.
5151 static statement_t *parse_for(void)
5155 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5156 statement->statement.kind = STATEMENT_FOR;
5157 statement->statement.source_position = token.source_position;
5161 int top = environment_top();
5162 context_t *last_context = context;
5163 set_context(&statement->context);
5165 if(token.type != ';') {
5166 if(is_declaration_specifier(&token, false)) {
5167 parse_declaration(record_declaration);
5169 statement->initialisation = parse_expression();
5176 if(token.type != ';') {
5177 statement->condition = parse_expression();
5180 if(token.type != ')') {
5181 statement->step = parse_expression();
5184 statement->body = parse_loop_body((statement_t*)statement);
5186 assert(context == &statement->context);
5187 set_context(last_context);
5188 environment_pop_to(top);
5190 return (statement_t*) statement;
5194 * Parse a goto statement.
5196 static statement_t *parse_goto(void)
5200 if(token.type != T_IDENTIFIER) {
5201 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5205 symbol_t *symbol = token.v.symbol;
5208 declaration_t *label = get_label(symbol);
5210 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5212 statement->statement.kind = STATEMENT_GOTO;
5213 statement->statement.source_position = token.source_position;
5215 statement->label = label;
5217 /* remember the goto's in a list for later checking */
5218 if (goto_last == NULL) {
5219 goto_first = goto_last = statement;
5221 goto_last->next = statement;
5226 return (statement_t*) statement;
5230 * Parse a continue statement.
5232 static statement_t *parse_continue(void)
5234 statement_t *statement;
5235 if (current_loop == NULL) {
5236 errorf(HERE, "continue statement not within loop");
5239 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5241 statement->base.source_position = token.source_position;
5251 * Parse a break statement.
5253 static statement_t *parse_break(void)
5255 statement_t *statement;
5256 if (current_switch == NULL && current_loop == NULL) {
5257 errorf(HERE, "break statement not within loop or switch");
5260 statement = allocate_statement_zero(STATEMENT_BREAK);
5262 statement->base.source_position = token.source_position;
5272 * Check if a given declaration represents a local variable.
5274 static bool is_local_var_declaration(const declaration_t *declaration) {
5275 switch ((storage_class_tag_t) declaration->storage_class) {
5276 case STORAGE_CLASS_NONE:
5277 case STORAGE_CLASS_AUTO:
5278 case STORAGE_CLASS_REGISTER: {
5279 const type_t *type = skip_typeref(declaration->type);
5280 if(is_type_function(type)) {
5292 * Check if a given expression represents a local variable.
5294 static bool is_local_variable(const expression_t *expression)
5296 if (expression->base.kind != EXPR_REFERENCE) {
5299 const declaration_t *declaration = expression->reference.declaration;
5300 return is_local_var_declaration(declaration);
5304 * Parse a return statement.
5306 static statement_t *parse_return(void)
5310 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5312 statement->statement.kind = STATEMENT_RETURN;
5313 statement->statement.source_position = token.source_position;
5315 expression_t *return_value = NULL;
5316 if(token.type != ';') {
5317 return_value = parse_expression();
5321 const type_t *const func_type = current_function->type;
5322 assert(is_type_function(func_type));
5323 type_t *const return_type = skip_typeref(func_type->function.return_type);
5325 if(return_value != NULL) {
5326 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5328 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5329 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5330 warningf(statement->statement.source_position,
5331 "'return' with a value, in function returning void");
5332 return_value = NULL;
5334 type_t *const res_type = semantic_assign(return_type,
5335 return_value, "'return'");
5336 if (res_type == NULL) {
5337 errorf(statement->statement.source_position,
5338 "cannot return something of type '%T' in function returning '%T'",
5339 return_value->base.datatype, return_type);
5341 return_value = create_implicit_cast(return_value, res_type);
5344 /* check for returning address of a local var */
5345 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5346 const expression_t *expression = return_value->unary.value;
5347 if (is_local_variable(expression)) {
5348 warningf(statement->statement.source_position,
5349 "function returns address of local variable");
5353 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5354 warningf(statement->statement.source_position,
5355 "'return' without value, in function returning non-void");
5358 statement->return_value = return_value;
5360 return (statement_t*) statement;
5364 * Parse a declaration statement.
5366 static statement_t *parse_declaration_statement(void)
5368 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5370 statement->base.source_position = token.source_position;
5372 declaration_t *before = last_declaration;
5373 parse_declaration(record_declaration);
5375 if(before == NULL) {
5376 statement->declaration.declarations_begin = context->declarations;
5378 statement->declaration.declarations_begin = before->next;
5380 statement->declaration.declarations_end = last_declaration;
5386 * Parse an expression statement, ie. expr ';'.
5388 static statement_t *parse_expression_statement(void)
5390 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5392 statement->base.source_position = token.source_position;
5393 expression_t *const expr = parse_expression();
5394 statement->expression.expression = expr;
5396 if (warning.unused_value && !expression_has_effect(expr)) {
5397 warningf(expr->base.source_position, "statement has no effect");
5406 * Parse a statement.
5408 static statement_t *parse_statement(void)
5410 statement_t *statement = NULL;
5412 /* declaration or statement */
5413 switch(token.type) {
5415 statement = parse_asm_statement();
5419 statement = parse_case_statement();
5423 statement = parse_default_statement();
5427 statement = parse_compound_statement();
5431 statement = parse_if();
5435 statement = parse_switch();
5439 statement = parse_while();
5443 statement = parse_do();
5447 statement = parse_for();
5451 statement = parse_goto();
5455 statement = parse_continue();
5459 statement = parse_break();
5463 statement = parse_return();
5467 if (warning.empty_statement) {
5468 warningf(HERE, "statement is empty");
5475 if(look_ahead(1)->type == ':') {
5476 statement = parse_label_statement();
5480 if(is_typedef_symbol(token.v.symbol)) {
5481 statement = parse_declaration_statement();
5485 statement = parse_expression_statement();
5488 case T___extension__:
5489 /* this can be a prefix to a declaration or an expression statement */
5490 /* we simply eat it now and parse the rest with tail recursion */
5493 } while(token.type == T___extension__);
5494 statement = parse_statement();
5498 statement = parse_declaration_statement();
5502 statement = parse_expression_statement();
5506 assert(statement == NULL
5507 || statement->base.source_position.input_name != NULL);
5513 * Parse a compound statement.
5515 static statement_t *parse_compound_statement(void)
5517 compound_statement_t *const compound_statement
5518 = allocate_ast_zero(sizeof(compound_statement[0]));
5519 compound_statement->statement.kind = STATEMENT_COMPOUND;
5520 compound_statement->statement.source_position = token.source_position;
5524 int top = environment_top();
5525 context_t *last_context = context;
5526 set_context(&compound_statement->context);
5528 statement_t *last_statement = NULL;
5530 while(token.type != '}' && token.type != T_EOF) {
5531 statement_t *statement = parse_statement();
5532 if(statement == NULL)
5535 if(last_statement != NULL) {
5536 last_statement->base.next = statement;
5538 compound_statement->statements = statement;
5541 while(statement->base.next != NULL)
5542 statement = statement->base.next;
5544 last_statement = statement;
5547 if(token.type == '}') {
5550 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5553 assert(context == &compound_statement->context);
5554 set_context(last_context);
5555 environment_pop_to(top);
5557 return (statement_t*) compound_statement;
5561 * Initialize builtin types.
5563 static void initialize_builtin_types(void)
5565 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5566 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5567 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5568 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5569 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5570 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5571 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5572 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5574 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5575 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5576 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5577 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5581 * Parse a translation unit.
5583 static translation_unit_t *parse_translation_unit(void)
5585 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5587 assert(global_context == NULL);
5588 global_context = &unit->context;
5590 assert(context == NULL);
5591 set_context(&unit->context);
5593 initialize_builtin_types();
5595 while(token.type != T_EOF) {
5596 if (token.type == ';') {
5597 /* TODO error in strict mode */
5598 warningf(HERE, "stray ';' outside of function");
5601 parse_external_declaration();
5605 assert(context == &unit->context);
5607 last_declaration = NULL;
5609 assert(global_context == &unit->context);
5610 global_context = NULL;
5618 * @return the translation unit or NULL if errors occurred.
5620 translation_unit_t *parse(void)
5622 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5623 label_stack = NEW_ARR_F(stack_entry_t, 0);
5624 diagnostic_count = 0;
5628 type_set_output(stderr);
5629 ast_set_output(stderr);
5631 lookahead_bufpos = 0;
5632 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5635 translation_unit_t *unit = parse_translation_unit();
5637 DEL_ARR_F(environment_stack);
5638 DEL_ARR_F(label_stack);
5647 * Initialize the parser.
5649 void init_parser(void)
5651 init_expression_parsers();
5652 obstack_init(&temp_obst);
5654 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5655 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5659 * Terminate the parser.
5661 void exit_parser(void)
5663 obstack_free(&temp_obst, NULL);