7 #include "diagnostic.h"
8 #include "format_check.h"
14 #include "type_hash.h"
16 #include "lang_features.h"
17 #include "adt/bitfiddle.h"
18 #include "adt/error.h"
19 #include "adt/array.h"
21 //#define PRINT_TOKENS
22 //#define ABORT_ON_ERROR
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 const atomic_type_t *atomic_type = &type->atomic;
643 atomic_type_kind_t atype = atomic_type->akind;
647 static type_t *promote_integer(type_t *type)
649 if(type->kind == TYPE_BITFIELD)
650 return promote_integer(type->bitfield.base);
652 if(get_rank(type) < ATOMIC_TYPE_INT)
659 * Create a cast expression.
661 * @param expression the expression to cast
662 * @param dest_type the destination type
664 static expression_t *create_cast_expression(expression_t *expression,
667 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
669 cast->unary.value = expression;
670 cast->base.datatype = dest_type;
676 * Check if a given expression represents the 0 pointer constant.
678 static bool is_null_pointer_constant(const expression_t *expression)
680 /* skip void* cast */
681 if(expression->kind == EXPR_UNARY_CAST
682 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
683 expression = expression->unary.value;
686 /* TODO: not correct yet, should be any constant integer expression
687 * which evaluates to 0 */
688 if (expression->kind != EXPR_CONST)
691 type_t *const type = skip_typeref(expression->base.datatype);
692 if (!is_type_integer(type))
695 return expression->conste.v.int_value == 0;
699 * Create an implicit cast expression.
701 * @param expression the expression to cast
702 * @param dest_type the destination type
704 static expression_t *create_implicit_cast(expression_t *expression,
707 type_t *source_type = expression->base.datatype;
709 if(source_type == NULL)
712 source_type = skip_typeref(source_type);
713 dest_type = skip_typeref(dest_type);
715 if(source_type == dest_type)
718 switch (dest_type->kind) {
720 /* TODO warning for implicitly converting to enum */
723 if (source_type->kind != TYPE_ATOMIC &&
724 source_type->kind != TYPE_ENUM &&
725 source_type->kind != TYPE_BITFIELD) {
726 panic("casting of non-atomic types not implemented yet");
729 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
730 type_error_incompatible("can't cast types",
731 expression->base.source_position, source_type,
736 return create_cast_expression(expression, dest_type);
739 switch (source_type->kind) {
741 if (is_null_pointer_constant(expression)) {
742 return create_cast_expression(expression, dest_type);
747 if (pointers_compatible(source_type, dest_type)) {
748 return create_cast_expression(expression, dest_type);
753 array_type_t *array_type = &source_type->array;
754 pointer_type_t *pointer_type = &dest_type->pointer;
755 if (types_compatible(array_type->element_type,
756 pointer_type->points_to)) {
757 return create_cast_expression(expression, dest_type);
763 panic("casting of non-atomic types not implemented yet");
766 type_error_incompatible("can't implicitly cast types",
767 expression->base.source_position, source_type, dest_type);
771 panic("casting of non-atomic types not implemented yet");
775 /** Implements the rules from § 6.5.16.1 */
776 static void semantic_assign(type_t *orig_type_left, expression_t **right,
779 type_t *orig_type_right = (*right)->base.datatype;
781 if(orig_type_right == NULL)
784 type_t *const type_left = skip_typeref(orig_type_left);
785 type_t *const type_right = skip_typeref(orig_type_right);
787 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
788 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
789 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
790 && is_type_pointer(type_right))) {
791 *right = create_implicit_cast(*right, type_left);
795 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
796 pointer_type_t *pointer_type_left = &type_left->pointer;
797 pointer_type_t *pointer_type_right = &type_right->pointer;
798 type_t *points_to_left = pointer_type_left->points_to;
799 type_t *points_to_right = pointer_type_right->points_to;
801 points_to_left = skip_typeref(points_to_left);
802 points_to_right = skip_typeref(points_to_right);
804 /* the left type has all qualifiers from the right type */
805 unsigned missing_qualifiers
806 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
807 if(missing_qualifiers != 0) {
808 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
812 points_to_left = get_unqualified_type(points_to_left);
813 points_to_right = get_unqualified_type(points_to_right);
815 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
816 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
817 && !types_compatible(points_to_left, points_to_right)) {
818 goto incompatible_assign_types;
821 *right = create_implicit_cast(*right, type_left);
825 if (is_type_compound(type_left)
826 && types_compatible(type_left, type_right)) {
827 *right = create_implicit_cast(*right, type_left);
831 incompatible_assign_types:
832 /* TODO: improve error message */
833 errorf(HERE, "incompatible types in %s: '%T' <- '%T'",
834 context, orig_type_left, orig_type_right);
837 static expression_t *parse_constant_expression(void)
839 /* start parsing at precedence 7 (conditional expression) */
840 expression_t *result = parse_sub_expression(7);
842 if(!is_constant_expression(result)) {
843 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
849 static expression_t *parse_assignment_expression(void)
851 /* start parsing at precedence 2 (assignment expression) */
852 return parse_sub_expression(2);
855 static type_t *make_global_typedef(const char *name, type_t *type)
857 symbol_t *const symbol = symbol_table_insert(name);
859 declaration_t *const declaration = allocate_declaration_zero();
860 declaration->namespc = NAMESPACE_NORMAL;
861 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
862 declaration->type = type;
863 declaration->symbol = symbol;
864 declaration->source_position = builtin_source_position;
866 record_declaration(declaration);
868 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
869 typedef_type->typedeft.declaration = declaration;
874 static string_t parse_string_literals(void)
876 assert(token.type == T_STRING_LITERAL);
877 string_t result = token.v.string;
881 while (token.type == T_STRING_LITERAL) {
882 result = concat_strings(&result, &token.v.string);
889 static void parse_attributes(void)
893 case T___attribute__: {
901 errorf(HERE, "EOF while parsing attribute");
920 if(token.type != T_STRING_LITERAL) {
921 parse_error_expected("while parsing assembler attribute",
926 parse_string_literals();
931 goto attributes_finished;
940 static designator_t *parse_designation(void)
942 if(token.type != '[' && token.type != '.')
945 designator_t *result = NULL;
946 designator_t *last = NULL;
949 designator_t *designator;
952 designator = allocate_ast_zero(sizeof(designator[0]));
954 designator->array_access = parse_constant_expression();
958 designator = allocate_ast_zero(sizeof(designator[0]));
960 if(token.type != T_IDENTIFIER) {
961 parse_error_expected("while parsing designator",
965 designator->symbol = token.v.symbol;
973 assert(designator != NULL);
975 last->next = designator;
984 static initializer_t *initializer_from_string(array_type_t *type,
985 const string_t *const string)
987 /* TODO: check len vs. size of array type */
990 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
991 initializer->string.string = *string;
996 static initializer_t *initializer_from_wide_string(array_type_t *const type,
997 wide_string_t *const string)
999 /* TODO: check len vs. size of array type */
1002 initializer_t *const initializer =
1003 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1004 initializer->wide_string.string = *string;
1009 static initializer_t *initializer_from_expression(type_t *type,
1010 expression_t *expression)
1012 /* TODO check that expression is a constant expression */
1014 /* § 6.7.8.14/15 char array may be initialized by string literals */
1015 type_t *const expr_type = expression->base.datatype;
1016 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1017 array_type_t *const array_type = &type->array;
1018 type_t *const element_type = skip_typeref(array_type->element_type);
1020 if (element_type->kind == TYPE_ATOMIC) {
1021 switch (expression->kind) {
1022 case EXPR_STRING_LITERAL:
1023 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
1024 return initializer_from_string(array_type,
1025 &expression->string.value);
1028 case EXPR_WIDE_STRING_LITERAL: {
1029 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1030 if (get_unqualified_type(element_type) == bare_wchar_type) {
1031 return initializer_from_wide_string(array_type,
1032 &expression->wide_string.value);
1042 type_t *expression_type = skip_typeref(expression->base.datatype);
1043 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1044 semantic_assign(type, &expression, "initializer");
1046 initializer_t *result = allocate_initializer_zero(INITIALIZER_VALUE);
1047 result->value.value = expression;
1055 static initializer_t *parse_sub_initializer(type_t *type,
1056 expression_t *expression,
1057 type_t *expression_type);
1059 static initializer_t *parse_sub_initializer_elem(type_t *type)
1061 if(token.type == '{') {
1062 return parse_sub_initializer(type, NULL, NULL);
1065 expression_t *expression = parse_assignment_expression();
1066 type_t *expression_type = skip_typeref(expression->base.datatype);
1068 return parse_sub_initializer(type, expression, expression_type);
1071 static bool had_initializer_brace_warning;
1073 static void skip_designator(void)
1076 if(token.type == '.') {
1078 if(token.type == T_IDENTIFIER)
1080 } else if(token.type == '[') {
1082 parse_constant_expression();
1083 if(token.type == ']')
1091 static initializer_t *parse_sub_initializer(type_t *type,
1092 expression_t *expression,
1093 type_t *expression_type)
1095 if(is_type_scalar(type)) {
1096 /* there might be extra {} hierarchies */
1097 if(token.type == '{') {
1099 if(!had_initializer_brace_warning) {
1100 warningf(HERE, "braces around scalar initializer");
1101 had_initializer_brace_warning = true;
1103 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1104 if(token.type == ',') {
1106 /* TODO: warn about excessive elements */
1112 if(expression == NULL) {
1113 expression = parse_assignment_expression();
1115 return initializer_from_expression(type, expression);
1118 /* does the expression match the currently looked at object to initialize */
1119 if(expression != NULL) {
1120 initializer_t *result = initializer_from_expression(type, expression);
1125 bool read_paren = false;
1126 if(token.type == '{') {
1131 /* descend into subtype */
1132 initializer_t *result = NULL;
1133 initializer_t **elems;
1134 if(is_type_array(type)) {
1135 array_type_t *array_type = &type->array;
1136 type_t *element_type = array_type->element_type;
1137 element_type = skip_typeref(element_type);
1139 if(token.type == '.') {
1141 "compound designator in initializer for array type '%T'",
1147 had_initializer_brace_warning = false;
1148 if(expression == NULL) {
1149 sub = parse_sub_initializer_elem(element_type);
1151 sub = parse_sub_initializer(element_type, expression,
1155 /* didn't match the subtypes -> try the parent type */
1157 assert(!read_paren);
1161 elems = NEW_ARR_F(initializer_t*, 0);
1162 ARR_APP1(initializer_t*, elems, sub);
1165 if(token.type == '}')
1168 if(token.type == '}')
1171 sub = parse_sub_initializer_elem(element_type);
1173 /* TODO error, do nicer cleanup */
1174 errorf(HERE, "member initializer didn't match");
1178 ARR_APP1(initializer_t*, elems, sub);
1181 assert(is_type_compound(type));
1182 compound_type_t *compound_type = &type->compound;
1183 context_t *context = &compound_type->declaration->context;
1185 if(token.type == '[') {
1187 "array designator in initializer for compound type '%T'",
1192 declaration_t *first = context->declarations;
1195 type_t *first_type = first->type;
1196 first_type = skip_typeref(first_type);
1199 had_initializer_brace_warning = false;
1200 if(expression == NULL) {
1201 sub = parse_sub_initializer_elem(first_type);
1203 sub = parse_sub_initializer(first_type, expression,expression_type);
1206 /* didn't match the subtypes -> try our parent type */
1208 assert(!read_paren);
1212 elems = NEW_ARR_F(initializer_t*, 0);
1213 ARR_APP1(initializer_t*, elems, sub);
1215 declaration_t *iter = first->next;
1216 for( ; iter != NULL; iter = iter->next) {
1217 if(iter->symbol == NULL)
1219 if(iter->namespc != NAMESPACE_NORMAL)
1222 if(token.type == '}')
1225 if(token.type == '}')
1228 type_t *iter_type = iter->type;
1229 iter_type = skip_typeref(iter_type);
1231 sub = parse_sub_initializer_elem(iter_type);
1233 /* TODO error, do nicer cleanup */
1234 errorf(HERE, "member initializer didn't match");
1238 ARR_APP1(initializer_t*, elems, sub);
1242 int len = ARR_LEN(elems);
1243 size_t elems_size = sizeof(initializer_t*) * len;
1245 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1247 init->initializer.kind = INITIALIZER_LIST;
1249 memcpy(init->initializers, elems, elems_size);
1252 result = (initializer_t*) init;
1255 if(token.type == ',')
1262 static initializer_t *parse_initializer(type_t *const orig_type)
1264 initializer_t *result;
1266 type_t *const type = skip_typeref(orig_type);
1268 if(token.type != '{') {
1269 expression_t *expression = parse_assignment_expression();
1270 if (expression->base.datatype == NULL) {
1271 /* something bad happens, don't produce further errors */
1274 initializer_t *initializer = initializer_from_expression(type, expression);
1275 if(initializer == NULL) {
1277 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1278 expression, expression->base.datatype, orig_type);
1283 if(is_type_scalar(type)) {
1287 expression_t *expression = parse_assignment_expression();
1288 result = initializer_from_expression(type, expression);
1290 if(token.type == ',')
1296 result = parse_sub_initializer(type, NULL, NULL);
1302 static declaration_t *append_declaration(declaration_t *declaration);
1304 static declaration_t *parse_compound_type_specifier(bool is_struct)
1312 symbol_t *symbol = NULL;
1313 declaration_t *declaration = NULL;
1315 if (token.type == T___attribute__) {
1320 if(token.type == T_IDENTIFIER) {
1321 symbol = token.v.symbol;
1325 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1327 declaration = get_declaration(symbol, NAMESPACE_UNION);
1329 } else if(token.type != '{') {
1331 parse_error_expected("while parsing struct type specifier",
1332 T_IDENTIFIER, '{', 0);
1334 parse_error_expected("while parsing union type specifier",
1335 T_IDENTIFIER, '{', 0);
1341 if(declaration == NULL) {
1342 declaration = allocate_declaration_zero();
1343 declaration->namespc =
1344 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1345 declaration->source_position = token.source_position;
1346 declaration->symbol = symbol;
1347 declaration->parent_context = context;
1348 if (symbol != NULL) {
1349 environment_push(declaration);
1351 append_declaration(declaration);
1354 if(token.type == '{') {
1355 if(declaration->init.is_defined) {
1356 assert(symbol != NULL);
1357 errorf(HERE, "multiple definition of '%s %Y'",
1358 is_struct ? "struct" : "union", symbol);
1359 declaration->context.declarations = NULL;
1361 declaration->init.is_defined = true;
1363 int top = environment_top();
1364 context_t *last_context = context;
1365 set_context(&declaration->context);
1367 parse_compound_type_entries();
1370 assert(context == &declaration->context);
1371 set_context(last_context);
1372 environment_pop_to(top);
1378 static void parse_enum_entries(type_t *const enum_type)
1382 if(token.type == '}') {
1384 errorf(HERE, "empty enum not allowed");
1389 if(token.type != T_IDENTIFIER) {
1390 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1395 declaration_t *const entry = allocate_declaration_zero();
1396 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1397 entry->type = enum_type;
1398 entry->symbol = token.v.symbol;
1399 entry->source_position = token.source_position;
1402 if(token.type == '=') {
1404 entry->init.enum_value = parse_constant_expression();
1409 record_declaration(entry);
1411 if(token.type != ',')
1414 } while(token.type != '}');
1419 static type_t *parse_enum_specifier(void)
1423 declaration_t *declaration;
1426 if(token.type == T_IDENTIFIER) {
1427 symbol = token.v.symbol;
1430 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1431 } else if(token.type != '{') {
1432 parse_error_expected("while parsing enum type specifier",
1433 T_IDENTIFIER, '{', 0);
1440 if(declaration == NULL) {
1441 declaration = allocate_declaration_zero();
1442 declaration->namespc = NAMESPACE_ENUM;
1443 declaration->source_position = token.source_position;
1444 declaration->symbol = symbol;
1445 declaration->parent_context = context;
1448 type_t *const type = allocate_type_zero(TYPE_ENUM);
1449 type->enumt.declaration = declaration;
1451 if(token.type == '{') {
1452 if(declaration->init.is_defined) {
1453 errorf(HERE, "multiple definitions of enum %Y", symbol);
1455 if (symbol != NULL) {
1456 environment_push(declaration);
1458 append_declaration(declaration);
1459 declaration->init.is_defined = 1;
1461 parse_enum_entries(type);
1469 * if a symbol is a typedef to another type, return true
1471 static bool is_typedef_symbol(symbol_t *symbol)
1473 const declaration_t *const declaration =
1474 get_declaration(symbol, NAMESPACE_NORMAL);
1476 declaration != NULL &&
1477 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1480 static type_t *parse_typeof(void)
1488 expression_t *expression = NULL;
1491 switch(token.type) {
1492 case T___extension__:
1493 /* this can be a prefix to a typename or an expression */
1494 /* we simply eat it now. */
1497 } while(token.type == T___extension__);
1501 if(is_typedef_symbol(token.v.symbol)) {
1502 type = parse_typename();
1504 expression = parse_expression();
1505 type = expression->base.datatype;
1510 type = parse_typename();
1514 expression = parse_expression();
1515 type = expression->base.datatype;
1521 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1522 typeof_type->typeoft.expression = expression;
1523 typeof_type->typeoft.typeof_type = type;
1529 SPECIFIER_SIGNED = 1 << 0,
1530 SPECIFIER_UNSIGNED = 1 << 1,
1531 SPECIFIER_LONG = 1 << 2,
1532 SPECIFIER_INT = 1 << 3,
1533 SPECIFIER_DOUBLE = 1 << 4,
1534 SPECIFIER_CHAR = 1 << 5,
1535 SPECIFIER_SHORT = 1 << 6,
1536 SPECIFIER_LONG_LONG = 1 << 7,
1537 SPECIFIER_FLOAT = 1 << 8,
1538 SPECIFIER_BOOL = 1 << 9,
1539 SPECIFIER_VOID = 1 << 10,
1540 #ifdef PROVIDE_COMPLEX
1541 SPECIFIER_COMPLEX = 1 << 11,
1542 SPECIFIER_IMAGINARY = 1 << 12,
1546 static type_t *create_builtin_type(symbol_t *const symbol,
1547 type_t *const real_type)
1549 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1550 type->builtin.symbol = symbol;
1551 type->builtin.real_type = real_type;
1553 type_t *result = typehash_insert(type);
1554 if (type != result) {
1561 static type_t *get_typedef_type(symbol_t *symbol)
1563 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1564 if(declaration == NULL
1565 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1568 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1569 type->typedeft.declaration = declaration;
1574 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1576 type_t *type = NULL;
1577 unsigned type_qualifiers = 0;
1578 unsigned type_specifiers = 0;
1581 specifiers->source_position = token.source_position;
1584 switch(token.type) {
1587 #define MATCH_STORAGE_CLASS(token, class) \
1589 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1590 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1592 specifiers->storage_class = class; \
1596 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1597 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1598 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1599 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1600 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1603 switch (specifiers->storage_class) {
1604 case STORAGE_CLASS_NONE:
1605 specifiers->storage_class = STORAGE_CLASS_THREAD;
1608 case STORAGE_CLASS_EXTERN:
1609 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1612 case STORAGE_CLASS_STATIC:
1613 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1617 errorf(HERE, "multiple storage classes in declaration specifiers");
1623 /* type qualifiers */
1624 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1626 type_qualifiers |= qualifier; \
1630 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1631 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1632 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1634 case T___extension__:
1639 /* type specifiers */
1640 #define MATCH_SPECIFIER(token, specifier, name) \
1643 if(type_specifiers & specifier) { \
1644 errorf(HERE, "multiple " name " type specifiers given"); \
1646 type_specifiers |= specifier; \
1650 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1651 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1652 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1653 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1654 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1655 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1656 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1657 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1658 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1659 #ifdef PROVIDE_COMPLEX
1660 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1661 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1664 /* only in microsoft mode */
1665 specifiers->decl_modifiers |= DM_FORCEINLINE;
1669 specifiers->is_inline = true;
1674 if(type_specifiers & SPECIFIER_LONG_LONG) {
1675 errorf(HERE, "multiple type specifiers given");
1676 } else if(type_specifiers & SPECIFIER_LONG) {
1677 type_specifiers |= SPECIFIER_LONG_LONG;
1679 type_specifiers |= SPECIFIER_LONG;
1683 /* TODO: if is_type_valid(type) for the following rules should issue
1686 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1688 type->compound.declaration = parse_compound_type_specifier(true);
1692 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1694 type->compound.declaration = parse_compound_type_specifier(false);
1698 type = parse_enum_specifier();
1701 type = parse_typeof();
1703 case T___builtin_va_list:
1704 type = duplicate_type(type_valist);
1708 case T___attribute__:
1713 case T_IDENTIFIER: {
1714 type_t *typedef_type = get_typedef_type(token.v.symbol);
1716 if(typedef_type == NULL)
1717 goto finish_specifiers;
1720 type = typedef_type;
1724 /* function specifier */
1726 goto finish_specifiers;
1733 atomic_type_kind_t atomic_type;
1735 /* match valid basic types */
1736 switch(type_specifiers) {
1737 case SPECIFIER_VOID:
1738 atomic_type = ATOMIC_TYPE_VOID;
1740 case SPECIFIER_CHAR:
1741 atomic_type = ATOMIC_TYPE_CHAR;
1743 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1744 atomic_type = ATOMIC_TYPE_SCHAR;
1746 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1747 atomic_type = ATOMIC_TYPE_UCHAR;
1749 case SPECIFIER_SHORT:
1750 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1751 case SPECIFIER_SHORT | SPECIFIER_INT:
1752 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1753 atomic_type = ATOMIC_TYPE_SHORT;
1755 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1756 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1757 atomic_type = ATOMIC_TYPE_USHORT;
1760 case SPECIFIER_SIGNED:
1761 case SPECIFIER_SIGNED | SPECIFIER_INT:
1762 atomic_type = ATOMIC_TYPE_INT;
1764 case SPECIFIER_UNSIGNED:
1765 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1766 atomic_type = ATOMIC_TYPE_UINT;
1768 case SPECIFIER_LONG:
1769 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1770 case SPECIFIER_LONG | SPECIFIER_INT:
1771 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1772 atomic_type = ATOMIC_TYPE_LONG;
1774 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1775 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1776 atomic_type = ATOMIC_TYPE_ULONG;
1778 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1779 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1780 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1781 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1783 atomic_type = ATOMIC_TYPE_LONGLONG;
1785 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1786 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1788 atomic_type = ATOMIC_TYPE_ULONGLONG;
1790 case SPECIFIER_FLOAT:
1791 atomic_type = ATOMIC_TYPE_FLOAT;
1793 case SPECIFIER_DOUBLE:
1794 atomic_type = ATOMIC_TYPE_DOUBLE;
1796 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1797 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1799 case SPECIFIER_BOOL:
1800 atomic_type = ATOMIC_TYPE_BOOL;
1802 #ifdef PROVIDE_COMPLEX
1803 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1804 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1806 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1807 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1809 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1810 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1812 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1813 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1815 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1816 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1818 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1819 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1823 /* invalid specifier combination, give an error message */
1824 if(type_specifiers == 0) {
1825 if (! strict_mode) {
1826 warningf(HERE, "no type specifiers in declaration, using int");
1827 atomic_type = ATOMIC_TYPE_INT;
1830 errorf(HERE, "no type specifiers given in declaration");
1832 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1833 (type_specifiers & SPECIFIER_UNSIGNED)) {
1834 errorf(HERE, "signed and unsigned specifiers gives");
1835 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1836 errorf(HERE, "only integer types can be signed or unsigned");
1838 errorf(HERE, "multiple datatypes in declaration");
1840 atomic_type = ATOMIC_TYPE_INVALID;
1843 type = allocate_type_zero(TYPE_ATOMIC);
1844 type->atomic.akind = atomic_type;
1847 if(type_specifiers != 0) {
1848 errorf(HERE, "multiple datatypes in declaration");
1852 type->base.qualifiers = type_qualifiers;
1854 type_t *result = typehash_insert(type);
1855 if(newtype && result != type) {
1859 specifiers->type = result;
1862 static type_qualifiers_t parse_type_qualifiers(void)
1864 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1867 switch(token.type) {
1868 /* type qualifiers */
1869 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1870 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1871 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1874 return type_qualifiers;
1879 static declaration_t *parse_identifier_list(void)
1881 declaration_t *declarations = NULL;
1882 declaration_t *last_declaration = NULL;
1884 declaration_t *const declaration = allocate_declaration_zero();
1885 declaration->source_position = token.source_position;
1886 declaration->symbol = token.v.symbol;
1889 if(last_declaration != NULL) {
1890 last_declaration->next = declaration;
1892 declarations = declaration;
1894 last_declaration = declaration;
1896 if(token.type != ',')
1899 } while(token.type == T_IDENTIFIER);
1901 return declarations;
1904 static void semantic_parameter(declaration_t *declaration)
1906 /* TODO: improve error messages */
1908 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1909 errorf(HERE, "typedef not allowed in parameter list");
1910 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1911 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1912 errorf(HERE, "parameter may only have none or register storage class");
1915 type_t *orig_type = declaration->type;
1916 if(orig_type == NULL)
1918 type_t *type = skip_typeref(orig_type);
1920 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1921 * into a pointer. § 6.7.5.3 (7) */
1922 if (is_type_array(type)) {
1923 const array_type_t *arr_type = &type->array;
1924 type_t *element_type = arr_type->element_type;
1926 type = make_pointer_type(element_type, type->base.qualifiers);
1928 declaration->type = type;
1931 if(is_type_incomplete(type)) {
1932 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1933 orig_type, declaration->symbol);
1937 static declaration_t *parse_parameter(void)
1939 declaration_specifiers_t specifiers;
1940 memset(&specifiers, 0, sizeof(specifiers));
1942 parse_declaration_specifiers(&specifiers);
1944 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1946 semantic_parameter(declaration);
1951 static declaration_t *parse_parameters(function_type_t *type)
1953 if(token.type == T_IDENTIFIER) {
1954 symbol_t *symbol = token.v.symbol;
1955 if(!is_typedef_symbol(symbol)) {
1956 type->kr_style_parameters = true;
1957 return parse_identifier_list();
1961 if(token.type == ')') {
1962 type->unspecified_parameters = 1;
1965 if(token.type == T_void && look_ahead(1)->type == ')') {
1970 declaration_t *declarations = NULL;
1971 declaration_t *declaration;
1972 declaration_t *last_declaration = NULL;
1973 function_parameter_t *parameter;
1974 function_parameter_t *last_parameter = NULL;
1977 switch(token.type) {
1981 return declarations;
1984 case T___extension__:
1986 declaration = parse_parameter();
1988 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1989 memset(parameter, 0, sizeof(parameter[0]));
1990 parameter->type = declaration->type;
1992 if(last_parameter != NULL) {
1993 last_declaration->next = declaration;
1994 last_parameter->next = parameter;
1996 type->parameters = parameter;
1997 declarations = declaration;
1999 last_parameter = parameter;
2000 last_declaration = declaration;
2004 return declarations;
2006 if(token.type != ',')
2007 return declarations;
2017 } construct_type_type_t;
2019 typedef struct construct_type_t construct_type_t;
2020 struct construct_type_t {
2021 construct_type_type_t type;
2022 construct_type_t *next;
2025 typedef struct parsed_pointer_t parsed_pointer_t;
2026 struct parsed_pointer_t {
2027 construct_type_t construct_type;
2028 type_qualifiers_t type_qualifiers;
2031 typedef struct construct_function_type_t construct_function_type_t;
2032 struct construct_function_type_t {
2033 construct_type_t construct_type;
2034 type_t *function_type;
2037 typedef struct parsed_array_t parsed_array_t;
2038 struct parsed_array_t {
2039 construct_type_t construct_type;
2040 type_qualifiers_t type_qualifiers;
2046 typedef struct construct_base_type_t construct_base_type_t;
2047 struct construct_base_type_t {
2048 construct_type_t construct_type;
2052 static construct_type_t *parse_pointer_declarator(void)
2056 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2057 memset(pointer, 0, sizeof(pointer[0]));
2058 pointer->construct_type.type = CONSTRUCT_POINTER;
2059 pointer->type_qualifiers = parse_type_qualifiers();
2061 return (construct_type_t*) pointer;
2064 static construct_type_t *parse_array_declarator(void)
2068 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2069 memset(array, 0, sizeof(array[0]));
2070 array->construct_type.type = CONSTRUCT_ARRAY;
2072 if(token.type == T_static) {
2073 array->is_static = true;
2077 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2078 if(type_qualifiers != 0) {
2079 if(token.type == T_static) {
2080 array->is_static = true;
2084 array->type_qualifiers = type_qualifiers;
2086 if(token.type == '*' && look_ahead(1)->type == ']') {
2087 array->is_variable = true;
2089 } else if(token.type != ']') {
2090 array->size = parse_assignment_expression();
2095 return (construct_type_t*) array;
2098 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2102 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2104 declaration_t *parameters = parse_parameters(&type->function);
2105 if(declaration != NULL) {
2106 declaration->context.declarations = parameters;
2109 construct_function_type_t *construct_function_type =
2110 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2111 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2112 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2113 construct_function_type->function_type = type;
2117 return (construct_type_t*) construct_function_type;
2120 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2121 bool may_be_abstract)
2123 /* construct a single linked list of construct_type_t's which describe
2124 * how to construct the final declarator type */
2125 construct_type_t *first = NULL;
2126 construct_type_t *last = NULL;
2129 while(token.type == '*') {
2130 construct_type_t *type = parse_pointer_declarator();
2141 /* TODO: find out if this is correct */
2144 construct_type_t *inner_types = NULL;
2146 switch(token.type) {
2148 if(declaration == NULL) {
2149 errorf(HERE, "no identifier expected in typename");
2151 declaration->symbol = token.v.symbol;
2152 declaration->source_position = token.source_position;
2158 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2164 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2165 /* avoid a loop in the outermost scope, because eat_statement doesn't
2167 if(token.type == '}' && current_function == NULL) {
2175 construct_type_t *p = last;
2178 construct_type_t *type;
2179 switch(token.type) {
2181 type = parse_function_declarator(declaration);
2184 type = parse_array_declarator();
2187 goto declarator_finished;
2190 /* insert in the middle of the list (behind p) */
2192 type->next = p->next;
2203 declarator_finished:
2206 /* append inner_types at the end of the list, we don't to set last anymore
2207 * as it's not needed anymore */
2209 assert(first == NULL);
2210 first = inner_types;
2212 last->next = inner_types;
2218 static type_t *construct_declarator_type(construct_type_t *construct_list,
2221 construct_type_t *iter = construct_list;
2222 for( ; iter != NULL; iter = iter->next) {
2223 switch(iter->type) {
2224 case CONSTRUCT_INVALID:
2225 panic("invalid type construction found");
2226 case CONSTRUCT_FUNCTION: {
2227 construct_function_type_t *construct_function_type
2228 = (construct_function_type_t*) iter;
2230 type_t *function_type = construct_function_type->function_type;
2232 function_type->function.return_type = type;
2234 type = function_type;
2238 case CONSTRUCT_POINTER: {
2239 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2240 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2241 pointer_type->pointer.points_to = type;
2242 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2244 type = pointer_type;
2248 case CONSTRUCT_ARRAY: {
2249 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2250 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2252 array_type->base.qualifiers = parsed_array->type_qualifiers;
2253 array_type->array.element_type = type;
2254 array_type->array.is_static = parsed_array->is_static;
2255 array_type->array.is_variable = parsed_array->is_variable;
2256 array_type->array.size = parsed_array->size;
2263 type_t *hashed_type = typehash_insert(type);
2264 if(hashed_type != type) {
2265 /* the function type was constructed earlier freeing it here will
2266 * destroy other types... */
2267 if(iter->type != CONSTRUCT_FUNCTION) {
2277 static declaration_t *parse_declarator(
2278 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2280 declaration_t *const declaration = allocate_declaration_zero();
2281 declaration->storage_class = specifiers->storage_class;
2282 declaration->modifiers = specifiers->decl_modifiers;
2283 declaration->is_inline = specifiers->is_inline;
2285 construct_type_t *construct_type
2286 = parse_inner_declarator(declaration, may_be_abstract);
2287 type_t *const type = specifiers->type;
2288 declaration->type = construct_declarator_type(construct_type, type);
2290 if(construct_type != NULL) {
2291 obstack_free(&temp_obst, construct_type);
2297 static type_t *parse_abstract_declarator(type_t *base_type)
2299 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2301 type_t *result = construct_declarator_type(construct_type, base_type);
2302 if(construct_type != NULL) {
2303 obstack_free(&temp_obst, construct_type);
2309 static declaration_t *append_declaration(declaration_t* const declaration)
2311 if (last_declaration != NULL) {
2312 last_declaration->next = declaration;
2314 context->declarations = declaration;
2316 last_declaration = declaration;
2320 static declaration_t *internal_record_declaration(
2321 declaration_t *const declaration,
2322 const bool is_function_definition)
2324 const symbol_t *const symbol = declaration->symbol;
2325 const namespace_t namespc = (namespace_t)declaration->namespc;
2327 const type_t *const type = skip_typeref(declaration->type);
2328 if (is_type_function(type) && type->function.unspecified_parameters) {
2329 warningf(declaration->source_position,
2330 "function declaration '%#T' is not a prototype",
2331 type, declaration->symbol);
2334 declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2335 assert(declaration != previous_declaration);
2336 if (previous_declaration != NULL
2337 && previous_declaration->parent_context == context) {
2338 /* can happen for K&R style declarations */
2339 if(previous_declaration->type == NULL) {
2340 previous_declaration->type = declaration->type;
2343 const type_t *const prev_type = skip_typeref(previous_declaration->type);
2344 if (!types_compatible(type, prev_type)) {
2345 errorf(declaration->source_position,
2346 "declaration '%#T' is incompatible with previous declaration '%#T'",
2347 type, symbol, previous_declaration->type, symbol);
2348 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2350 unsigned old_storage_class = previous_declaration->storage_class;
2351 unsigned new_storage_class = declaration->storage_class;
2353 /* pretend no storage class means extern for function declarations
2354 * (except if the previous declaration is neither none nor extern) */
2355 if (is_type_function(type)) {
2356 switch (old_storage_class) {
2357 case STORAGE_CLASS_NONE:
2358 old_storage_class = STORAGE_CLASS_EXTERN;
2360 case STORAGE_CLASS_EXTERN:
2361 if (new_storage_class == STORAGE_CLASS_NONE && !is_function_definition) {
2362 new_storage_class = STORAGE_CLASS_EXTERN;
2370 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2371 new_storage_class == STORAGE_CLASS_EXTERN) {
2372 warn_redundant_declaration:
2373 warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
2374 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2375 } else if (current_function == NULL) {
2376 if (old_storage_class != STORAGE_CLASS_STATIC &&
2377 new_storage_class == STORAGE_CLASS_STATIC) {
2378 errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
2379 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2381 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2382 goto warn_redundant_declaration;
2384 if (new_storage_class == STORAGE_CLASS_NONE) {
2385 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2389 if (old_storage_class == new_storage_class) {
2390 errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
2392 errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
2394 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2397 return previous_declaration;
2400 assert(declaration->parent_context == NULL);
2401 assert(declaration->symbol != NULL);
2402 assert(context != NULL);
2404 declaration->parent_context = context;
2406 environment_push(declaration);
2407 return append_declaration(declaration);
2411 * Check if a given type is a vilid array type.
2413 static bool is_valid_array_type(const type_t *type) {
2414 if (type->kind == TYPE_ARRAY) {
2415 const array_type_t *array = &type->array;
2416 const type_t *etype = skip_typeref(array->element_type);
2418 if (! is_valid_array_type(etype))
2421 if (etype->kind == TYPE_ATOMIC) {
2422 const atomic_type_t *atype = &etype->atomic;
2424 if (atype->akind == ATOMIC_TYPE_VOID) {
2432 static declaration_t *record_declaration(declaration_t *declaration)
2434 declaration = internal_record_declaration(declaration, false);
2435 const type_t *type = skip_typeref(declaration->type);
2437 /* check the type here for several not allowed combinations */
2438 if (type->kind == TYPE_FUNCTION) {
2439 const function_type_t* function_type = &type->function;
2440 const type_t* ret_type = skip_typeref(function_type->return_type);
2442 if (ret_type->kind == TYPE_FUNCTION) {
2443 errorf(declaration->source_position, "'%Y' declared as function returning a function",
2444 declaration->symbol);
2445 declaration->type = type_error_type;
2448 if (! is_valid_array_type(type)) {
2449 errorf(declaration->source_position, "declaration of '%Y' as array of voids",
2450 declaration->symbol);
2451 declaration->type = type_error_type;
2456 static declaration_t *record_function_definition(declaration_t *const declaration)
2458 return internal_record_declaration(declaration, true);
2461 static void parser_error_multiple_definition(declaration_t *declaration,
2462 const source_position_t source_position)
2464 errorf(source_position, "multiple definition of symbol '%Y'",
2465 declaration->symbol);
2466 errorf(declaration->source_position,
2467 "this is the location of the previous definition.");
2470 static bool is_declaration_specifier(const token_t *token,
2471 bool only_type_specifiers)
2473 switch(token->type) {
2477 return is_typedef_symbol(token->v.symbol);
2479 case T___extension__:
2482 return !only_type_specifiers;
2489 static void parse_init_declarator_rest(declaration_t *declaration)
2493 type_t *orig_type = declaration->type;
2494 type_t *type = type = skip_typeref(orig_type);
2496 if(declaration->init.initializer != NULL) {
2497 parser_error_multiple_definition(declaration, token.source_position);
2500 initializer_t *initializer = parse_initializer(type);
2502 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2503 * the array type size */
2504 if(is_type_array(type) && initializer != NULL) {
2505 array_type_t *array_type = &type->array;
2507 if(array_type->size == NULL) {
2508 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2510 cnst->base.datatype = type_size_t;
2512 switch (initializer->kind) {
2513 case INITIALIZER_LIST: {
2514 initializer_list_t *const list = &initializer->list;
2515 cnst->conste.v.int_value = list->len;
2519 case INITIALIZER_STRING: {
2520 initializer_string_t *const string = &initializer->string;
2521 cnst->conste.v.int_value = string->string.size;
2525 case INITIALIZER_WIDE_STRING: {
2526 initializer_wide_string_t *const string = &initializer->wide_string;
2527 cnst->conste.v.int_value = string->string.size;
2532 panic("invalid initializer type");
2535 array_type->size = cnst;
2539 if(is_type_function(type)) {
2540 errorf(declaration->source_position,
2541 "initializers not allowed for function types at declator '%Y' (type '%T')",
2542 declaration->symbol, orig_type);
2544 declaration->init.initializer = initializer;
2548 /* parse rest of a declaration without any declarator */
2549 static void parse_anonymous_declaration_rest(
2550 const declaration_specifiers_t *specifiers,
2551 parsed_declaration_func finished_declaration)
2555 declaration_t *const declaration = allocate_declaration_zero();
2556 declaration->type = specifiers->type;
2557 declaration->storage_class = specifiers->storage_class;
2558 declaration->source_position = specifiers->source_position;
2560 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2561 warningf(declaration->source_position, "useless storage class in empty declaration");
2564 type_t *type = declaration->type;
2565 switch (type->kind) {
2566 case TYPE_COMPOUND_STRUCT:
2567 case TYPE_COMPOUND_UNION: {
2568 const compound_type_t *compound_type = &type->compound;
2569 if (compound_type->declaration->symbol == NULL) {
2570 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2579 warningf(declaration->source_position, "empty declaration");
2583 finished_declaration(declaration);
2586 static void parse_declaration_rest(declaration_t *ndeclaration,
2587 const declaration_specifiers_t *specifiers,
2588 parsed_declaration_func finished_declaration)
2591 declaration_t *declaration = finished_declaration(ndeclaration);
2593 type_t *orig_type = declaration->type;
2594 type_t *type = skip_typeref(orig_type);
2596 if(is_type_valid(type) &&
2597 type->kind != TYPE_FUNCTION && declaration->is_inline) {
2598 warningf(declaration->source_position,
2599 "variable '%Y' declared 'inline'\n", declaration->symbol);
2602 if(token.type == '=') {
2603 parse_init_declarator_rest(declaration);
2606 if(token.type != ',')
2610 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2615 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2617 symbol_t *symbol = declaration->symbol;
2618 if(symbol == NULL) {
2619 errorf(HERE, "anonymous declaration not valid as function parameter");
2622 namespace_t namespc = (namespace_t) declaration->namespc;
2623 if(namespc != NAMESPACE_NORMAL) {
2624 return record_declaration(declaration);
2627 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2628 if(previous_declaration == NULL ||
2629 previous_declaration->parent_context != context) {
2630 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2635 if(previous_declaration->type == NULL) {
2636 previous_declaration->type = declaration->type;
2637 previous_declaration->storage_class = declaration->storage_class;
2638 previous_declaration->parent_context = context;
2639 return previous_declaration;
2641 return record_declaration(declaration);
2645 static void parse_declaration(parsed_declaration_func finished_declaration)
2647 declaration_specifiers_t specifiers;
2648 memset(&specifiers, 0, sizeof(specifiers));
2649 parse_declaration_specifiers(&specifiers);
2651 if(token.type == ';') {
2652 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2654 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2655 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2659 static void parse_kr_declaration_list(declaration_t *declaration)
2661 type_t *type = skip_typeref(declaration->type);
2662 if(!is_type_function(type))
2665 if(!type->function.kr_style_parameters)
2668 /* push function parameters */
2669 int top = environment_top();
2670 context_t *last_context = context;
2671 set_context(&declaration->context);
2673 declaration_t *parameter = declaration->context.declarations;
2674 for( ; parameter != NULL; parameter = parameter->next) {
2675 assert(parameter->parent_context == NULL);
2676 parameter->parent_context = context;
2677 environment_push(parameter);
2680 /* parse declaration list */
2681 while(is_declaration_specifier(&token, false)) {
2682 parse_declaration(finished_kr_declaration);
2685 /* pop function parameters */
2686 assert(context == &declaration->context);
2687 set_context(last_context);
2688 environment_pop_to(top);
2690 /* update function type */
2691 type_t *new_type = duplicate_type(type);
2692 new_type->function.kr_style_parameters = false;
2694 function_parameter_t *parameters = NULL;
2695 function_parameter_t *last_parameter = NULL;
2697 declaration_t *parameter_declaration = declaration->context.declarations;
2698 for( ; parameter_declaration != NULL;
2699 parameter_declaration = parameter_declaration->next) {
2700 type_t *parameter_type = parameter_declaration->type;
2701 if(parameter_type == NULL) {
2703 errorf(HERE, "no type specified for function parameter '%Y'",
2704 parameter_declaration->symbol);
2706 warningf(HERE, "no type specified for function parameter '%Y', using int",
2707 parameter_declaration->symbol);
2708 parameter_type = type_int;
2709 parameter_declaration->type = parameter_type;
2713 semantic_parameter(parameter_declaration);
2714 parameter_type = parameter_declaration->type;
2716 function_parameter_t *function_parameter
2717 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2718 memset(function_parameter, 0, sizeof(function_parameter[0]));
2720 function_parameter->type = parameter_type;
2721 if(last_parameter != NULL) {
2722 last_parameter->next = function_parameter;
2724 parameters = function_parameter;
2726 last_parameter = function_parameter;
2728 new_type->function.parameters = parameters;
2730 type = typehash_insert(new_type);
2731 if(type != new_type) {
2732 obstack_free(type_obst, new_type);
2735 declaration->type = type;
2739 * Check if all labels are defined in the current function.
2741 static void check_for_missing_labels(void)
2743 bool first_err = true;
2744 for (const goto_statement_t *goto_statement = goto_first;
2745 goto_statement != NULL;
2746 goto_statement = goto_statement->next) {
2747 const declaration_t *label = goto_statement->label;
2749 if (label->source_position.input_name == NULL) {
2752 diagnosticf("%s: In function '%Y':\n",
2753 current_function->source_position.input_name,
2754 current_function->symbol);
2756 errorf(goto_statement->statement.source_position,
2757 "label '%Y' used but not defined", label->symbol);
2760 goto_first = goto_last = NULL;
2763 static void parse_external_declaration(void)
2765 /* function-definitions and declarations both start with declaration
2767 declaration_specifiers_t specifiers;
2768 memset(&specifiers, 0, sizeof(specifiers));
2769 parse_declaration_specifiers(&specifiers);
2771 /* must be a declaration */
2772 if(token.type == ';') {
2773 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2777 /* declarator is common to both function-definitions and declarations */
2778 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2780 /* must be a declaration */
2781 if(token.type == ',' || token.type == '=' || token.type == ';') {
2782 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2786 /* must be a function definition */
2787 parse_kr_declaration_list(ndeclaration);
2789 if(token.type != '{') {
2790 parse_error_expected("while parsing function definition", '{', 0);
2795 type_t *type = ndeclaration->type;
2801 /* note that we don't skip typerefs: the standard doesn't allow them here
2802 * (so we can't use is_type_function here) */
2803 if(type->kind != TYPE_FUNCTION) {
2804 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2805 type, ndeclaration->symbol);
2810 /* § 6.7.5.3 (14) a function definition with () means no
2811 * parameters (and not unspecified parameters) */
2812 if(type->function.unspecified_parameters) {
2813 type_t *duplicate = duplicate_type(type);
2814 duplicate->function.unspecified_parameters = false;
2816 type = typehash_insert(duplicate);
2817 if(type != duplicate) {
2818 obstack_free(type_obst, duplicate);
2820 ndeclaration->type = type;
2823 declaration_t *const declaration = record_function_definition(ndeclaration);
2824 if(ndeclaration != declaration) {
2825 declaration->context = ndeclaration->context;
2827 type = skip_typeref(declaration->type);
2829 /* push function parameters and switch context */
2830 int top = environment_top();
2831 context_t *last_context = context;
2832 set_context(&declaration->context);
2834 declaration_t *parameter = declaration->context.declarations;
2835 for( ; parameter != NULL; parameter = parameter->next) {
2836 if(parameter->parent_context == &ndeclaration->context) {
2837 parameter->parent_context = context;
2839 assert(parameter->parent_context == NULL
2840 || parameter->parent_context == context);
2841 parameter->parent_context = context;
2842 environment_push(parameter);
2845 if(declaration->init.statement != NULL) {
2846 parser_error_multiple_definition(declaration, token.source_position);
2848 goto end_of_parse_external_declaration;
2850 /* parse function body */
2851 int label_stack_top = label_top();
2852 declaration_t *old_current_function = current_function;
2853 current_function = declaration;
2855 declaration->init.statement = parse_compound_statement();
2856 check_for_missing_labels();
2858 assert(current_function == declaration);
2859 current_function = old_current_function;
2860 label_pop_to(label_stack_top);
2863 end_of_parse_external_declaration:
2864 assert(context == &declaration->context);
2865 set_context(last_context);
2866 environment_pop_to(top);
2869 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2871 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2872 type->bitfield.base = base;
2873 type->bitfield.size = size;
2878 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2880 /* TODO: check constraints for struct declarations (in specifiers) */
2882 declaration_t *declaration;
2884 if(token.type == ':') {
2887 type_t *base_type = specifiers->type;
2888 expression_t *size = parse_constant_expression();
2890 type_t *type = make_bitfield_type(base_type, size);
2892 declaration = allocate_declaration_zero();
2893 declaration->namespc = NAMESPACE_NORMAL;
2894 declaration->storage_class = STORAGE_CLASS_NONE;
2895 declaration->source_position = token.source_position;
2896 declaration->modifiers = specifiers->decl_modifiers;
2897 declaration->type = type;
2899 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2901 if(token.type == ':') {
2903 expression_t *size = parse_constant_expression();
2905 type_t *type = make_bitfield_type(declaration->type, size);
2906 declaration->type = type;
2909 record_declaration(declaration);
2911 if(token.type != ',')
2918 static void parse_compound_type_entries(void)
2922 while(token.type != '}' && token.type != T_EOF) {
2923 declaration_specifiers_t specifiers;
2924 memset(&specifiers, 0, sizeof(specifiers));
2925 parse_declaration_specifiers(&specifiers);
2927 parse_struct_declarators(&specifiers);
2929 if(token.type == T_EOF) {
2930 errorf(HERE, "EOF while parsing struct");
2935 static type_t *parse_typename(void)
2937 declaration_specifiers_t specifiers;
2938 memset(&specifiers, 0, sizeof(specifiers));
2939 parse_declaration_specifiers(&specifiers);
2940 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2941 /* TODO: improve error message, user does probably not know what a
2942 * storage class is...
2944 errorf(HERE, "typename may not have a storage class");
2947 type_t *result = parse_abstract_declarator(specifiers.type);
2955 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2956 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2957 expression_t *left);
2959 typedef struct expression_parser_function_t expression_parser_function_t;
2960 struct expression_parser_function_t {
2961 unsigned precedence;
2962 parse_expression_function parser;
2963 unsigned infix_precedence;
2964 parse_expression_infix_function infix_parser;
2967 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2970 * Creates a new invalid expression.
2972 static expression_t *create_invalid_expression(void)
2974 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2975 expression->base.source_position = token.source_position;
2980 * Prints an error message if an expression was expected but not read
2982 static expression_t *expected_expression_error(void)
2984 /* skip the error message if the error token was read */
2985 if (token.type != T_ERROR) {
2986 errorf(HERE, "expected expression, got token '%K'", &token);
2990 return create_invalid_expression();
2994 * Parse a string constant.
2996 static expression_t *parse_string_const(void)
2998 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2999 cnst->base.datatype = type_string;
3000 cnst->string.value = parse_string_literals();
3006 * Parse a wide string constant.
3008 static expression_t *parse_wide_string_const(void)
3010 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3011 cnst->base.datatype = type_wchar_t_ptr;
3012 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
3018 * Parse an integer constant.
3020 static expression_t *parse_int_const(void)
3022 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3023 cnst->base.datatype = token.datatype;
3024 cnst->conste.v.int_value = token.v.intvalue;
3032 * Parse a float constant.
3034 static expression_t *parse_float_const(void)
3036 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3037 cnst->base.datatype = token.datatype;
3038 cnst->conste.v.float_value = token.v.floatvalue;
3045 static declaration_t *create_implicit_function(symbol_t *symbol,
3046 const source_position_t source_position)
3048 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
3049 ntype->function.return_type = type_int;
3050 ntype->function.unspecified_parameters = true;
3052 type_t *type = typehash_insert(ntype);
3057 declaration_t *const declaration = allocate_declaration_zero();
3058 declaration->storage_class = STORAGE_CLASS_EXTERN;
3059 declaration->type = type;
3060 declaration->symbol = symbol;
3061 declaration->source_position = source_position;
3062 declaration->parent_context = global_context;
3064 context_t *old_context = context;
3065 set_context(global_context);
3067 environment_push(declaration);
3068 /* prepend the declaration to the global declarations list */
3069 declaration->next = context->declarations;
3070 context->declarations = declaration;
3072 assert(context == global_context);
3073 set_context(old_context);
3079 * Creates a return_type (func)(argument_type) function type if not
3082 * @param return_type the return type
3083 * @param argument_type the argument type
3085 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3087 function_parameter_t *parameter
3088 = obstack_alloc(type_obst, sizeof(parameter[0]));
3089 memset(parameter, 0, sizeof(parameter[0]));
3090 parameter->type = argument_type;
3092 type_t *type = allocate_type_zero(TYPE_FUNCTION);
3093 type->function.return_type = return_type;
3094 type->function.parameters = parameter;
3096 type_t *result = typehash_insert(type);
3097 if(result != type) {
3105 * Creates a function type for some function like builtins.
3107 * @param symbol the symbol describing the builtin
3109 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3111 switch(symbol->ID) {
3112 case T___builtin_alloca:
3113 return make_function_1_type(type_void_ptr, type_size_t);
3114 case T___builtin_nan:
3115 return make_function_1_type(type_double, type_string);
3116 case T___builtin_nanf:
3117 return make_function_1_type(type_float, type_string);
3118 case T___builtin_nand:
3119 return make_function_1_type(type_long_double, type_string);
3120 case T___builtin_va_end:
3121 return make_function_1_type(type_void, type_valist);
3123 panic("not implemented builtin symbol found");
3128 * Performs automatic type cast as described in § 6.3.2.1.
3130 * @param orig_type the original type
3132 static type_t *automatic_type_conversion(type_t *orig_type)
3134 if(orig_type == NULL)
3137 type_t *type = skip_typeref(orig_type);
3138 if(is_type_array(type)) {
3139 array_type_t *array_type = &type->array;
3140 type_t *element_type = array_type->element_type;
3141 unsigned qualifiers = array_type->type.qualifiers;
3143 return make_pointer_type(element_type, qualifiers);
3146 if(is_type_function(type)) {
3147 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3154 * reverts the automatic casts of array to pointer types and function
3155 * to function-pointer types as defined § 6.3.2.1
3157 type_t *revert_automatic_type_conversion(const expression_t *expression)
3159 if(expression->base.datatype == NULL)
3162 switch(expression->kind) {
3163 case EXPR_REFERENCE: {
3164 const reference_expression_t *ref = &expression->reference;
3165 return ref->declaration->type;
3168 const select_expression_t *select = &expression->select;
3169 return select->compound_entry->type;
3171 case EXPR_UNARY_DEREFERENCE: {
3172 expression_t *value = expression->unary.value;
3173 type_t *type = skip_typeref(value->base.datatype);
3174 pointer_type_t *pointer_type = &type->pointer;
3176 return pointer_type->points_to;
3178 case EXPR_BUILTIN_SYMBOL: {
3179 const builtin_symbol_expression_t *builtin
3180 = &expression->builtin_symbol;
3181 return get_builtin_symbol_type(builtin->symbol);
3183 case EXPR_ARRAY_ACCESS: {
3184 const array_access_expression_t *array_access
3185 = &expression->array_access;
3186 const expression_t *array_ref = array_access->array_ref;
3187 type_t *type_left = skip_typeref(array_ref->base.datatype);
3188 assert(is_type_pointer(type_left));
3189 pointer_type_t *pointer_type = &type_left->pointer;
3190 return pointer_type->points_to;
3197 return expression->base.datatype;
3200 static expression_t *parse_reference(void)
3202 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3204 reference_expression_t *ref = &expression->reference;
3205 ref->symbol = token.v.symbol;
3207 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3209 source_position_t source_position = token.source_position;
3212 if(declaration == NULL) {
3213 if (! strict_mode && token.type == '(') {
3214 /* an implicitly defined function */
3215 warningf(HERE, "implicit declaration of function '%Y'",
3218 declaration = create_implicit_function(ref->symbol,
3221 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3226 type_t *type = declaration->type;
3228 /* we always do the auto-type conversions; the & and sizeof parser contains
3229 * code to revert this! */
3230 type = automatic_type_conversion(type);
3232 ref->declaration = declaration;
3233 ref->expression.datatype = type;
3238 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3242 /* TODO check if explicit cast is allowed and issue warnings/errors */
3245 static expression_t *parse_cast(void)
3247 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3249 cast->base.source_position = token.source_position;
3251 type_t *type = parse_typename();
3254 expression_t *value = parse_sub_expression(20);
3256 check_cast_allowed(value, type);
3258 cast->base.datatype = type;
3259 cast->unary.value = value;
3264 static expression_t *parse_statement_expression(void)
3266 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3268 statement_t *statement = parse_compound_statement();
3269 expression->statement.statement = statement;
3270 if(statement == NULL) {
3275 assert(statement->kind == STATEMENT_COMPOUND);
3276 compound_statement_t *compound_statement = &statement->compound;
3278 /* find last statement and use it's type */
3279 const statement_t *last_statement = NULL;
3280 const statement_t *iter = compound_statement->statements;
3281 for( ; iter != NULL; iter = iter->base.next) {
3282 last_statement = iter;
3285 if(last_statement->kind == STATEMENT_EXPRESSION) {
3286 const expression_statement_t *expression_statement
3287 = &last_statement->expression;
3288 expression->base.datatype
3289 = expression_statement->expression->base.datatype;
3291 expression->base.datatype = type_void;
3299 static expression_t *parse_brace_expression(void)
3303 switch(token.type) {
3305 /* gcc extension: a statement expression */
3306 return parse_statement_expression();
3310 return parse_cast();
3312 if(is_typedef_symbol(token.v.symbol)) {
3313 return parse_cast();
3317 expression_t *result = parse_expression();
3323 static expression_t *parse_function_keyword(void)
3328 if (current_function == NULL) {
3329 errorf(HERE, "'__func__' used outside of a function");
3332 string_literal_expression_t *expression
3333 = allocate_ast_zero(sizeof(expression[0]));
3335 expression->expression.kind = EXPR_FUNCTION;
3336 expression->expression.datatype = type_string;
3338 return (expression_t*) expression;
3341 static expression_t *parse_pretty_function_keyword(void)
3343 eat(T___PRETTY_FUNCTION__);
3346 if (current_function == NULL) {
3347 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3350 string_literal_expression_t *expression
3351 = allocate_ast_zero(sizeof(expression[0]));
3353 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3354 expression->expression.datatype = type_string;
3356 return (expression_t*) expression;
3359 static designator_t *parse_designator(void)
3361 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3363 if(token.type != T_IDENTIFIER) {
3364 parse_error_expected("while parsing member designator",
3369 result->symbol = token.v.symbol;
3372 designator_t *last_designator = result;
3374 if(token.type == '.') {
3376 if(token.type != T_IDENTIFIER) {
3377 parse_error_expected("while parsing member designator",
3382 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3383 designator->symbol = token.v.symbol;
3386 last_designator->next = designator;
3387 last_designator = designator;
3390 if(token.type == '[') {
3392 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3393 designator->array_access = parse_expression();
3394 if(designator->array_access == NULL) {
3400 last_designator->next = designator;
3401 last_designator = designator;
3410 static expression_t *parse_offsetof(void)
3412 eat(T___builtin_offsetof);
3414 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3415 expression->base.datatype = type_size_t;
3418 expression->offsetofe.type = parse_typename();
3420 expression->offsetofe.designator = parse_designator();
3426 static expression_t *parse_va_start(void)
3428 eat(T___builtin_va_start);
3430 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3433 expression->va_starte.ap = parse_assignment_expression();
3435 expression_t *const expr = parse_assignment_expression();
3436 if (expr->kind == EXPR_REFERENCE) {
3437 declaration_t *const decl = expr->reference.declaration;
3438 if (decl->parent_context == ¤t_function->context &&
3439 decl->next == NULL) {
3440 expression->va_starte.parameter = decl;
3445 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3447 return create_invalid_expression();
3450 static expression_t *parse_va_arg(void)
3452 eat(T___builtin_va_arg);
3454 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3457 expression->va_arge.ap = parse_assignment_expression();
3459 expression->base.datatype = parse_typename();
3465 static expression_t *parse_builtin_symbol(void)
3467 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3469 symbol_t *symbol = token.v.symbol;
3471 expression->builtin_symbol.symbol = symbol;
3474 type_t *type = get_builtin_symbol_type(symbol);
3475 type = automatic_type_conversion(type);
3477 expression->base.datatype = type;
3481 static expression_t *parse_builtin_constant(void)
3483 eat(T___builtin_constant_p);
3485 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3488 expression->builtin_constant.value = parse_assignment_expression();
3490 expression->base.datatype = type_int;
3495 static expression_t *parse_builtin_prefetch(void)
3497 eat(T___builtin_prefetch);
3499 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3502 expression->builtin_prefetch.adr = parse_assignment_expression();
3503 if (token.type == ',') {
3505 expression->builtin_prefetch.rw = parse_assignment_expression();
3507 if (token.type == ',') {
3509 expression->builtin_prefetch.locality = parse_assignment_expression();
3512 expression->base.datatype = type_void;
3517 static expression_t *parse_compare_builtin(void)
3519 expression_t *expression;
3521 switch(token.type) {
3522 case T___builtin_isgreater:
3523 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3525 case T___builtin_isgreaterequal:
3526 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3528 case T___builtin_isless:
3529 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3531 case T___builtin_islessequal:
3532 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3534 case T___builtin_islessgreater:
3535 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3537 case T___builtin_isunordered:
3538 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3541 panic("invalid compare builtin found");
3547 expression->binary.left = parse_assignment_expression();
3549 expression->binary.right = parse_assignment_expression();
3552 type_t *orig_type_left = expression->binary.left->base.datatype;
3553 type_t *orig_type_right = expression->binary.right->base.datatype;
3554 if(orig_type_left == NULL || orig_type_right == NULL)
3557 type_t *type_left = skip_typeref(orig_type_left);
3558 type_t *type_right = skip_typeref(orig_type_right);
3559 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3560 type_error_incompatible("invalid operands in comparison",
3561 token.source_position, type_left, type_right);
3563 semantic_comparison(&expression->binary);
3569 static expression_t *parse_builtin_expect(void)
3571 eat(T___builtin_expect);
3573 expression_t *expression
3574 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3577 expression->binary.left = parse_assignment_expression();
3579 expression->binary.right = parse_constant_expression();
3582 expression->base.datatype = expression->binary.left->base.datatype;
3587 static expression_t *parse_assume(void) {
3590 expression_t *expression
3591 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3594 expression->unary.value = parse_assignment_expression();
3597 expression->base.datatype = type_void;
3601 static expression_t *parse_alignof(void) {
3604 expression_t *expression
3605 = allocate_expression_zero(EXPR_ALIGNOF);
3608 expression->alignofe.type = parse_typename();
3611 expression->base.datatype = type_size_t;
3615 static expression_t *parse_primary_expression(void)
3617 switch(token.type) {
3619 return parse_int_const();
3620 case T_FLOATINGPOINT:
3621 return parse_float_const();
3622 case T_STRING_LITERAL:
3623 return parse_string_const();
3624 case T_WIDE_STRING_LITERAL:
3625 return parse_wide_string_const();
3627 return parse_reference();
3628 case T___FUNCTION__:
3630 return parse_function_keyword();
3631 case T___PRETTY_FUNCTION__:
3632 return parse_pretty_function_keyword();
3633 case T___builtin_offsetof:
3634 return parse_offsetof();
3635 case T___builtin_va_start:
3636 return parse_va_start();
3637 case T___builtin_va_arg:
3638 return parse_va_arg();
3639 case T___builtin_expect:
3640 return parse_builtin_expect();
3641 case T___builtin_nanf:
3642 case T___builtin_alloca:
3643 case T___builtin_va_end:
3644 return parse_builtin_symbol();
3645 case T___builtin_isgreater:
3646 case T___builtin_isgreaterequal:
3647 case T___builtin_isless:
3648 case T___builtin_islessequal:
3649 case T___builtin_islessgreater:
3650 case T___builtin_isunordered:
3651 return parse_compare_builtin();
3652 case T___builtin_constant_p:
3653 return parse_builtin_constant();
3654 case T___builtin_prefetch:
3655 return parse_builtin_prefetch();
3657 return parse_alignof();
3659 return parse_assume();
3662 return parse_brace_expression();
3665 errorf(HERE, "unexpected token '%K'", &token);
3668 return create_invalid_expression();
3672 * Check if the expression has the character type and issue a warning then.
3674 static void check_for_char_index_type(const expression_t *expression) {
3675 type_t *type = expression->base.datatype;
3676 type_t *base_type = skip_typeref(type);
3678 if (base_type->base.kind == TYPE_ATOMIC) {
3679 switch (base_type->atomic.akind == ATOMIC_TYPE_CHAR) {
3680 warningf(expression->base.source_position,
3681 "array subscript has type '%T'", type);
3686 static expression_t *parse_array_expression(unsigned precedence,
3693 expression_t *inside = parse_expression();
3695 array_access_expression_t *array_access
3696 = allocate_ast_zero(sizeof(array_access[0]));
3698 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3700 type_t *type_left = left->base.datatype;
3701 type_t *type_inside = inside->base.datatype;
3702 type_t *return_type = NULL;
3704 if(type_left != NULL && type_inside != NULL) {
3705 type_left = skip_typeref(type_left);
3706 type_inside = skip_typeref(type_inside);
3708 if(is_type_pointer(type_left)) {
3709 pointer_type_t *pointer = &type_left->pointer;
3710 return_type = pointer->points_to;
3711 array_access->array_ref = left;
3712 array_access->index = inside;
3713 check_for_char_index_type(inside);
3714 } else if(is_type_pointer(type_inside)) {
3715 pointer_type_t *pointer = &type_inside->pointer;
3716 return_type = pointer->points_to;
3717 array_access->array_ref = inside;
3718 array_access->index = left;
3719 array_access->flipped = true;
3720 check_for_char_index_type(left);
3722 errorf(HERE, "array access on object with non-pointer types '%T', '%T'", type_left, type_inside);
3725 array_access->array_ref = left;
3726 array_access->index = inside;
3729 if(token.type != ']') {
3730 parse_error_expected("Problem while parsing array access", ']', 0);
3731 return (expression_t*) array_access;
3735 return_type = automatic_type_conversion(return_type);
3736 array_access->expression.datatype = return_type;
3738 return (expression_t*) array_access;
3741 static expression_t *parse_sizeof(unsigned precedence)
3745 sizeof_expression_t *sizeof_expression
3746 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3747 sizeof_expression->expression.kind = EXPR_SIZEOF;
3748 sizeof_expression->expression.datatype = type_size_t;
3750 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3752 sizeof_expression->type = parse_typename();
3755 expression_t *expression = parse_sub_expression(precedence);
3756 expression->base.datatype = revert_automatic_type_conversion(expression);
3758 sizeof_expression->type = expression->base.datatype;
3759 sizeof_expression->size_expression = expression;
3762 return (expression_t*) sizeof_expression;
3765 static expression_t *parse_select_expression(unsigned precedence,
3766 expression_t *compound)
3769 assert(token.type == '.' || token.type == T_MINUSGREATER);
3771 bool is_pointer = (token.type == T_MINUSGREATER);
3774 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3775 select->select.compound = compound;
3777 if(token.type != T_IDENTIFIER) {
3778 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3781 symbol_t *symbol = token.v.symbol;
3782 select->select.symbol = symbol;
3785 type_t *orig_type = compound->base.datatype;
3786 if(orig_type == NULL)
3787 return create_invalid_expression();
3789 type_t *type = skip_typeref(orig_type);
3791 type_t *type_left = type;
3793 if(type->kind != TYPE_POINTER) {
3794 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3795 return create_invalid_expression();
3797 pointer_type_t *pointer_type = &type->pointer;
3798 type_left = pointer_type->points_to;
3800 type_left = skip_typeref(type_left);
3802 if(type_left->kind != TYPE_COMPOUND_STRUCT
3803 && type_left->kind != TYPE_COMPOUND_UNION) {
3804 errorf(HERE, "request for member '%Y' in something not a struct or "
3805 "union, but '%T'", symbol, type_left);
3806 return create_invalid_expression();
3809 compound_type_t *compound_type = &type_left->compound;
3810 declaration_t *declaration = compound_type->declaration;
3812 if(!declaration->init.is_defined) {
3813 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3815 return create_invalid_expression();
3818 declaration_t *iter = declaration->context.declarations;
3819 for( ; iter != NULL; iter = iter->next) {
3820 if(iter->symbol == symbol) {
3825 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3826 return create_invalid_expression();
3829 /* we always do the auto-type conversions; the & and sizeof parser contains
3830 * code to revert this! */
3831 type_t *expression_type = automatic_type_conversion(iter->type);
3833 select->select.compound_entry = iter;
3834 select->base.datatype = expression_type;
3836 if(expression_type->kind == TYPE_BITFIELD) {
3837 expression_t *extract
3838 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3839 extract->unary.value = select;
3840 extract->base.datatype = expression_type->bitfield.base;
3849 * Parse a call expression, ie. expression '( ... )'.
3851 * @param expression the function address
3853 static expression_t *parse_call_expression(unsigned precedence,
3854 expression_t *expression)
3857 expression_t *result = allocate_expression_zero(EXPR_CALL);
3859 call_expression_t *call = &result->call;
3860 call->function = expression;
3862 function_type_t *function_type = NULL;
3863 type_t *orig_type = expression->base.datatype;
3864 if(is_type_valid(orig_type)) {
3865 type_t *type = skip_typeref(orig_type);
3867 if(is_type_pointer(type)) {
3868 pointer_type_t *pointer_type = &type->pointer;
3870 type = skip_typeref(pointer_type->points_to);
3872 if (is_type_function(type)) {
3873 function_type = &type->function;
3874 call->expression.datatype = function_type->return_type;
3877 if(function_type == NULL) {
3878 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3880 function_type = NULL;
3881 call->expression.datatype = NULL;
3885 /* parse arguments */
3888 if(token.type != ')') {
3889 call_argument_t *last_argument = NULL;
3892 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3894 argument->expression = parse_assignment_expression();
3895 if(last_argument == NULL) {
3896 call->arguments = argument;
3898 last_argument->next = argument;
3900 last_argument = argument;
3902 if(token.type != ',')
3909 if(function_type != NULL) {
3910 function_parameter_t *parameter = function_type->parameters;
3911 call_argument_t *argument = call->arguments;
3912 for( ; parameter != NULL && argument != NULL;
3913 parameter = parameter->next, argument = argument->next) {
3914 type_t *expected_type = parameter->type;
3915 /* TODO report context in error messages */
3916 argument->expression = create_implicit_cast(argument->expression,
3919 /* too few parameters */
3920 if(parameter != NULL) {
3921 errorf(HERE, "too few arguments to function '%E'", expression);
3922 } else if(argument != NULL) {
3923 /* too many parameters */
3924 if(!function_type->variadic
3925 && !function_type->unspecified_parameters) {
3926 errorf(HERE, "too many arguments to function '%E'", expression);
3928 /* do default promotion */
3929 for( ; argument != NULL; argument = argument->next) {
3930 type_t *type = argument->expression->base.datatype;
3935 type = skip_typeref(type);
3936 if(is_type_integer(type)) {
3937 type = promote_integer(type);
3938 } else if(type == type_float) {
3942 argument->expression
3943 = create_implicit_cast(argument->expression, type);
3946 check_format(&result->call);
3949 check_format(&result->call);
3956 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3958 static bool same_compound_type(const type_t *type1, const type_t *type2)
3960 if(!is_type_compound(type1))
3962 if(type1->kind != type2->kind)
3965 const compound_type_t *compound1 = &type1->compound;
3966 const compound_type_t *compound2 = &type2->compound;
3968 return compound1->declaration == compound2->declaration;
3972 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3974 * @param expression the conditional expression
3976 static expression_t *parse_conditional_expression(unsigned precedence,
3977 expression_t *expression)
3981 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3983 conditional_expression_t *conditional = &result->conditional;
3984 conditional->condition = expression;
3987 type_t *condition_type_orig = expression->base.datatype;
3988 if(is_type_valid(condition_type_orig)) {
3989 type_t *condition_type = skip_typeref(condition_type_orig);
3990 if(condition_type->kind != TYPE_ERROR && !is_type_scalar(condition_type)) {
3991 type_error("expected a scalar type in conditional condition",
3992 expression->base.source_position, condition_type_orig);
3996 expression_t *true_expression = parse_expression();
3998 expression_t *false_expression = parse_sub_expression(precedence);
4000 conditional->true_expression = true_expression;
4001 conditional->false_expression = false_expression;
4003 type_t *orig_true_type = true_expression->base.datatype;
4004 type_t *orig_false_type = false_expression->base.datatype;
4005 if(!is_type_valid(orig_true_type) || !is_type_valid(orig_false_type))
4008 type_t *true_type = skip_typeref(orig_true_type);
4009 type_t *false_type = skip_typeref(orig_false_type);
4012 type_t *result_type = NULL;
4013 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4014 result_type = semantic_arithmetic(true_type, false_type);
4016 true_expression = create_implicit_cast(true_expression, result_type);
4017 false_expression = create_implicit_cast(false_expression, result_type);
4019 conditional->true_expression = true_expression;
4020 conditional->false_expression = false_expression;
4021 conditional->expression.datatype = result_type;
4022 } else if (same_compound_type(true_type, false_type)
4023 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4024 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
4025 /* just take 1 of the 2 types */
4026 result_type = true_type;
4027 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4028 && pointers_compatible(true_type, false_type)) {
4030 result_type = true_type;
4033 type_error_incompatible("while parsing conditional",
4034 expression->base.source_position, true_type,
4038 conditional->expression.datatype = result_type;
4043 * Parse an extension expression.
4045 static expression_t *parse_extension(unsigned precedence)
4047 eat(T___extension__);
4049 /* TODO enable extensions */
4050 expression_t *expression = parse_sub_expression(precedence);
4051 /* TODO disable extensions */
4055 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4057 eat(T___builtin_classify_type);
4059 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4060 result->base.datatype = type_int;
4063 expression_t *expression = parse_sub_expression(precedence);
4065 result->classify_type.type_expression = expression;
4070 static void semantic_incdec(unary_expression_t *expression)
4072 type_t *orig_type = expression->value->base.datatype;
4073 if(!is_type_valid(orig_type))
4076 type_t *type = skip_typeref(orig_type);
4077 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4078 /* TODO: improve error message */
4079 errorf(HERE, "operation needs an arithmetic or pointer type");
4083 expression->expression.datatype = orig_type;
4086 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4088 type_t *orig_type = expression->value->base.datatype;
4089 if(!is_type_valid(orig_type))
4092 type_t *type = skip_typeref(orig_type);
4093 if(!is_type_arithmetic(type)) {
4094 /* TODO: improve error message */
4095 errorf(HERE, "operation needs an arithmetic type");
4099 expression->expression.datatype = orig_type;
4102 static void semantic_unexpr_scalar(unary_expression_t *expression)
4104 type_t *orig_type = expression->value->base.datatype;
4105 if(!is_type_valid(orig_type))
4108 type_t *type = skip_typeref(orig_type);
4109 if (!is_type_scalar(type)) {
4110 errorf(HERE, "operand of ! must be of scalar type");
4114 expression->expression.datatype = orig_type;
4117 static void semantic_unexpr_integer(unary_expression_t *expression)
4119 type_t *orig_type = expression->value->base.datatype;
4120 if(!is_type_valid(orig_type))
4123 type_t *type = skip_typeref(orig_type);
4124 if (!is_type_integer(type)) {
4125 errorf(HERE, "operand of ~ must be of integer type");
4129 expression->expression.datatype = orig_type;
4132 static void semantic_dereference(unary_expression_t *expression)
4134 type_t *orig_type = expression->value->base.datatype;
4135 if(!is_type_valid(orig_type))
4138 type_t *type = skip_typeref(orig_type);
4139 if(!is_type_pointer(type)) {
4140 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4144 pointer_type_t *pointer_type = &type->pointer;
4145 type_t *result_type = pointer_type->points_to;
4147 result_type = automatic_type_conversion(result_type);
4148 expression->expression.datatype = result_type;
4152 * Check the semantic of the address taken expression.
4154 static void semantic_take_addr(unary_expression_t *expression)
4156 expression_t *value = expression->value;
4157 value->base.datatype = revert_automatic_type_conversion(value);
4159 type_t *orig_type = value->base.datatype;
4160 if(!is_type_valid(orig_type))
4163 if(value->kind == EXPR_REFERENCE) {
4164 reference_expression_t *reference = (reference_expression_t*) value;
4165 declaration_t *declaration = reference->declaration;
4166 if(declaration != NULL) {
4167 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4168 errorf(expression->expression.source_position,
4169 "address of register variable '%Y' requested",
4170 declaration->symbol);
4172 declaration->address_taken = 1;
4176 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4179 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4180 static expression_t *parse_##unexpression_type(unsigned precedence) \
4184 expression_t *unary_expression \
4185 = allocate_expression_zero(unexpression_type); \
4186 unary_expression->base.source_position = HERE; \
4187 unary_expression->unary.value = parse_sub_expression(precedence); \
4189 sfunc(&unary_expression->unary); \
4191 return unary_expression; \
4194 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4195 semantic_unexpr_arithmetic)
4196 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4197 semantic_unexpr_arithmetic)
4198 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4199 semantic_unexpr_scalar)
4200 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4201 semantic_dereference)
4202 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4204 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4205 semantic_unexpr_integer)
4206 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4208 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4211 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4213 static expression_t *parse_##unexpression_type(unsigned precedence, \
4214 expression_t *left) \
4216 (void) precedence; \
4219 expression_t *unary_expression \
4220 = allocate_expression_zero(unexpression_type); \
4221 unary_expression->unary.value = left; \
4223 sfunc(&unary_expression->unary); \
4225 return unary_expression; \
4228 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4229 EXPR_UNARY_POSTFIX_INCREMENT,
4231 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4232 EXPR_UNARY_POSTFIX_DECREMENT,
4235 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4237 /* TODO: handle complex + imaginary types */
4239 /* § 6.3.1.8 Usual arithmetic conversions */
4240 if(type_left == type_long_double || type_right == type_long_double) {
4241 return type_long_double;
4242 } else if(type_left == type_double || type_right == type_double) {
4244 } else if(type_left == type_float || type_right == type_float) {
4248 type_right = promote_integer(type_right);
4249 type_left = promote_integer(type_left);
4251 if(type_left == type_right)
4254 bool signed_left = is_type_signed(type_left);
4255 bool signed_right = is_type_signed(type_right);
4256 int rank_left = get_rank(type_left);
4257 int rank_right = get_rank(type_right);
4258 if(rank_left < rank_right) {
4259 if(signed_left == signed_right || !signed_right) {
4265 if(signed_left == signed_right || !signed_left) {
4274 * Check the semantic restrictions for a binary expression.
4276 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4278 expression_t *left = expression->left;
4279 expression_t *right = expression->right;
4280 type_t *orig_type_left = left->base.datatype;
4281 type_t *orig_type_right = right->base.datatype;
4283 if(orig_type_left == NULL || orig_type_right == NULL)
4286 type_t *type_left = skip_typeref(orig_type_left);
4287 type_t *type_right = skip_typeref(orig_type_right);
4289 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4290 /* TODO: improve error message */
4291 errorf(HERE, "operation needs arithmetic types");
4295 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4296 expression->left = create_implicit_cast(left, arithmetic_type);
4297 expression->right = create_implicit_cast(right, arithmetic_type);
4298 expression->expression.datatype = arithmetic_type;
4301 static void semantic_shift_op(binary_expression_t *expression)
4303 expression_t *left = expression->left;
4304 expression_t *right = expression->right;
4305 type_t *orig_type_left = left->base.datatype;
4306 type_t *orig_type_right = right->base.datatype;
4308 if(orig_type_left == NULL || orig_type_right == NULL)
4311 type_t *type_left = skip_typeref(orig_type_left);
4312 type_t *type_right = skip_typeref(orig_type_right);
4314 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4315 /* TODO: improve error message */
4316 errorf(HERE, "operation needs integer types");
4320 type_left = promote_integer(type_left);
4321 type_right = promote_integer(type_right);
4323 expression->left = create_implicit_cast(left, type_left);
4324 expression->right = create_implicit_cast(right, type_right);
4325 expression->expression.datatype = type_left;
4328 static void semantic_add(binary_expression_t *expression)
4330 expression_t *left = expression->left;
4331 expression_t *right = expression->right;
4332 type_t *orig_type_left = left->base.datatype;
4333 type_t *orig_type_right = right->base.datatype;
4335 if(orig_type_left == NULL || orig_type_right == NULL)
4338 type_t *type_left = skip_typeref(orig_type_left);
4339 type_t *type_right = skip_typeref(orig_type_right);
4342 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4343 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4344 expression->left = create_implicit_cast(left, arithmetic_type);
4345 expression->right = create_implicit_cast(right, arithmetic_type);
4346 expression->expression.datatype = arithmetic_type;
4348 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4349 expression->expression.datatype = type_left;
4350 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4351 expression->expression.datatype = type_right;
4353 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4357 static void semantic_sub(binary_expression_t *expression)
4359 expression_t *left = expression->left;
4360 expression_t *right = expression->right;
4361 type_t *orig_type_left = left->base.datatype;
4362 type_t *orig_type_right = right->base.datatype;
4364 if(orig_type_left == NULL || orig_type_right == NULL)
4367 type_t *type_left = skip_typeref(orig_type_left);
4368 type_t *type_right = skip_typeref(orig_type_right);
4371 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4372 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4373 expression->left = create_implicit_cast(left, arithmetic_type);
4374 expression->right = create_implicit_cast(right, arithmetic_type);
4375 expression->expression.datatype = arithmetic_type;
4377 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4378 expression->expression.datatype = type_left;
4379 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4380 if(!pointers_compatible(type_left, type_right)) {
4381 errorf(HERE, "pointers to incompatible objects to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4383 expression->expression.datatype = type_ptrdiff_t;
4386 errorf(HERE, "invalid operands to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4390 static void semantic_comparison(binary_expression_t *expression)
4392 expression_t *left = expression->left;
4393 expression_t *right = expression->right;
4394 type_t *orig_type_left = left->base.datatype;
4395 type_t *orig_type_right = right->base.datatype;
4397 if(orig_type_left == NULL || orig_type_right == NULL)
4400 type_t *type_left = skip_typeref(orig_type_left);
4401 type_t *type_right = skip_typeref(orig_type_right);
4403 /* TODO non-arithmetic types */
4404 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4405 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4406 expression->left = create_implicit_cast(left, arithmetic_type);
4407 expression->right = create_implicit_cast(right, arithmetic_type);
4408 expression->expression.datatype = arithmetic_type;
4409 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4410 /* TODO check compatibility */
4411 } else if (is_type_pointer(type_left)) {
4412 expression->right = create_implicit_cast(right, type_left);
4413 } else if (is_type_pointer(type_right)) {
4414 expression->left = create_implicit_cast(left, type_right);
4416 type_error_incompatible("invalid operands in comparison",
4417 token.source_position, type_left, type_right);
4419 expression->expression.datatype = type_int;
4422 static void semantic_arithmetic_assign(binary_expression_t *expression)
4424 expression_t *left = expression->left;
4425 expression_t *right = expression->right;
4426 type_t *orig_type_left = left->base.datatype;
4427 type_t *orig_type_right = right->base.datatype;
4429 if(orig_type_left == NULL || orig_type_right == NULL)
4432 type_t *type_left = skip_typeref(orig_type_left);
4433 type_t *type_right = skip_typeref(orig_type_right);
4435 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4436 /* TODO: improve error message */
4437 errorf(HERE, "operation needs arithmetic types");
4441 /* combined instructions are tricky. We can't create an implicit cast on
4442 * the left side, because we need the uncasted form for the store.
4443 * The ast2firm pass has to know that left_type must be right_type
4444 * for the arithmetic operation and create a cast by itself */
4445 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4446 expression->right = create_implicit_cast(right, arithmetic_type);
4447 expression->expression.datatype = type_left;
4450 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4452 expression_t *left = expression->left;
4453 expression_t *right = expression->right;
4454 type_t *orig_type_left = left->base.datatype;
4455 type_t *orig_type_right = right->base.datatype;
4457 if(orig_type_left == NULL || orig_type_right == NULL)
4460 type_t *type_left = skip_typeref(orig_type_left);
4461 type_t *type_right = skip_typeref(orig_type_right);
4463 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4464 /* combined instructions are tricky. We can't create an implicit cast on
4465 * the left side, because we need the uncasted form for the store.
4466 * The ast2firm pass has to know that left_type must be right_type
4467 * for the arithmetic operation and create a cast by itself */
4468 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4469 expression->right = create_implicit_cast(right, arithmetic_type);
4470 expression->expression.datatype = type_left;
4471 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4472 expression->expression.datatype = type_left;
4474 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4480 * Check the semantic restrictions of a logical expression.
4482 static void semantic_logical_op(binary_expression_t *expression)
4484 expression_t *left = expression->left;
4485 expression_t *right = expression->right;
4486 type_t *orig_type_left = left->base.datatype;
4487 type_t *orig_type_right = right->base.datatype;
4489 if(orig_type_left == NULL || orig_type_right == NULL)
4492 type_t *type_left = skip_typeref(orig_type_left);
4493 type_t *type_right = skip_typeref(orig_type_right);
4495 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4496 /* TODO: improve error message */
4497 errorf(HERE, "operation needs scalar types");
4501 expression->expression.datatype = type_int;
4505 * Checks if a compound type has constant fields.
4507 static bool has_const_fields(const compound_type_t *type)
4509 const context_t *context = &type->declaration->context;
4510 const declaration_t *declaration = context->declarations;
4512 for (; declaration != NULL; declaration = declaration->next) {
4513 if (declaration->namespc != NAMESPACE_NORMAL)
4516 const type_t *decl_type = skip_typeref(declaration->type);
4517 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4525 * Check the semantic restrictions of a binary assign expression.
4527 static void semantic_binexpr_assign(binary_expression_t *expression)
4529 expression_t *left = expression->left;
4530 type_t *orig_type_left = left->base.datatype;
4532 if(orig_type_left == NULL)
4535 type_t *type_left = revert_automatic_type_conversion(left);
4536 type_left = skip_typeref(orig_type_left);
4538 /* must be a modifiable lvalue */
4539 if (is_type_array(type_left)) {
4540 errorf(HERE, "cannot assign to arrays ('%E')", left);
4543 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4544 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4548 if(is_type_incomplete(type_left)) {
4550 "left-hand side of assignment '%E' has incomplete type '%T'",
4551 left, orig_type_left);
4554 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4555 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4556 left, orig_type_left);
4560 semantic_assign(orig_type_left, &expression->right, "assignment");
4562 expression->expression.datatype = orig_type_left;
4565 static void semantic_comma(binary_expression_t *expression)
4567 expression->expression.datatype = expression->right->base.datatype;
4570 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4571 static expression_t *parse_##binexpression_type(unsigned precedence, \
4572 expression_t *left) \
4576 expression_t *right = parse_sub_expression(precedence + lr); \
4578 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4579 binexpr->binary.left = left; \
4580 binexpr->binary.right = right; \
4581 sfunc(&binexpr->binary); \
4586 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4587 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4588 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4589 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4590 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4591 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4592 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4593 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4594 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4596 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4597 semantic_comparison, 1)
4598 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4599 semantic_comparison, 1)
4600 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4601 semantic_comparison, 1)
4602 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4603 semantic_comparison, 1)
4605 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4606 semantic_binexpr_arithmetic, 1)
4607 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4608 semantic_binexpr_arithmetic, 1)
4609 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4610 semantic_binexpr_arithmetic, 1)
4611 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4612 semantic_logical_op, 1)
4613 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4614 semantic_logical_op, 1)
4615 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4616 semantic_shift_op, 1)
4617 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4618 semantic_shift_op, 1)
4619 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4620 semantic_arithmetic_addsubb_assign, 0)
4621 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4622 semantic_arithmetic_addsubb_assign, 0)
4623 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4624 semantic_arithmetic_assign, 0)
4625 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4626 semantic_arithmetic_assign, 0)
4627 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4628 semantic_arithmetic_assign, 0)
4629 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4630 semantic_arithmetic_assign, 0)
4631 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4632 semantic_arithmetic_assign, 0)
4633 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4634 semantic_arithmetic_assign, 0)
4635 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4636 semantic_arithmetic_assign, 0)
4637 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4638 semantic_arithmetic_assign, 0)
4640 static expression_t *parse_sub_expression(unsigned precedence)
4642 if(token.type < 0) {
4643 return expected_expression_error();
4646 expression_parser_function_t *parser
4647 = &expression_parsers[token.type];
4648 source_position_t source_position = token.source_position;
4651 if(parser->parser != NULL) {
4652 left = parser->parser(parser->precedence);
4654 left = parse_primary_expression();
4656 assert(left != NULL);
4657 left->base.source_position = source_position;
4660 if(token.type < 0) {
4661 return expected_expression_error();
4664 parser = &expression_parsers[token.type];
4665 if(parser->infix_parser == NULL)
4667 if(parser->infix_precedence < precedence)
4670 left = parser->infix_parser(parser->infix_precedence, left);
4672 assert(left != NULL);
4673 assert(left->kind != EXPR_UNKNOWN);
4674 left->base.source_position = source_position;
4681 * Parse an expression.
4683 static expression_t *parse_expression(void)
4685 return parse_sub_expression(1);
4689 * Register a parser for a prefix-like operator with given precedence.
4691 * @param parser the parser function
4692 * @param token_type the token type of the prefix token
4693 * @param precedence the precedence of the operator
4695 static void register_expression_parser(parse_expression_function parser,
4696 int token_type, unsigned precedence)
4698 expression_parser_function_t *entry = &expression_parsers[token_type];
4700 if(entry->parser != NULL) {
4701 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4702 panic("trying to register multiple expression parsers for a token");
4704 entry->parser = parser;
4705 entry->precedence = precedence;
4709 * Register a parser for an infix operator with given precedence.
4711 * @param parser the parser function
4712 * @param token_type the token type of the infix operator
4713 * @param precedence the precedence of the operator
4715 static void register_infix_parser(parse_expression_infix_function parser,
4716 int token_type, unsigned precedence)
4718 expression_parser_function_t *entry = &expression_parsers[token_type];
4720 if(entry->infix_parser != NULL) {
4721 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4722 panic("trying to register multiple infix expression parsers for a "
4725 entry->infix_parser = parser;
4726 entry->infix_precedence = precedence;
4730 * Initialize the expression parsers.
4732 static void init_expression_parsers(void)
4734 memset(&expression_parsers, 0, sizeof(expression_parsers));
4736 register_infix_parser(parse_array_expression, '[', 30);
4737 register_infix_parser(parse_call_expression, '(', 30);
4738 register_infix_parser(parse_select_expression, '.', 30);
4739 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4740 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4742 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4745 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4746 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4747 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4748 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4749 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4750 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4751 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4752 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4753 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4754 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4755 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4756 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4757 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4758 T_EXCLAMATIONMARKEQUAL, 13);
4759 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4760 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4761 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4762 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4763 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4764 register_infix_parser(parse_conditional_expression, '?', 7);
4765 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4766 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4767 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4768 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4769 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4770 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4771 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4772 T_LESSLESSEQUAL, 2);
4773 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4774 T_GREATERGREATEREQUAL, 2);
4775 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4777 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4779 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4782 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4784 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4785 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4786 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4787 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4788 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4789 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4790 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4792 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4794 register_expression_parser(parse_sizeof, T_sizeof, 25);
4795 register_expression_parser(parse_extension, T___extension__, 25);
4796 register_expression_parser(parse_builtin_classify_type,
4797 T___builtin_classify_type, 25);
4801 * Parse a asm statement constraints specification.
4803 static asm_constraint_t *parse_asm_constraints(void)
4805 asm_constraint_t *result = NULL;
4806 asm_constraint_t *last = NULL;
4808 while(token.type == T_STRING_LITERAL || token.type == '[') {
4809 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4810 memset(constraint, 0, sizeof(constraint[0]));
4812 if(token.type == '[') {
4814 if(token.type != T_IDENTIFIER) {
4815 parse_error_expected("while parsing asm constraint",
4819 constraint->symbol = token.v.symbol;
4824 constraint->constraints = parse_string_literals();
4826 constraint->expression = parse_expression();
4830 last->next = constraint;
4832 result = constraint;
4836 if(token.type != ',')
4845 * Parse a asm statement clobber specification.
4847 static asm_clobber_t *parse_asm_clobbers(void)
4849 asm_clobber_t *result = NULL;
4850 asm_clobber_t *last = NULL;
4852 while(token.type == T_STRING_LITERAL) {
4853 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4854 clobber->clobber = parse_string_literals();
4857 last->next = clobber;
4863 if(token.type != ',')
4872 * Parse an asm statement.
4874 static statement_t *parse_asm_statement(void)
4878 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4879 statement->base.source_position = token.source_position;
4881 asm_statement_t *asm_statement = &statement->asms;
4883 if(token.type == T_volatile) {
4885 asm_statement->is_volatile = true;
4889 asm_statement->asm_text = parse_string_literals();
4891 if(token.type != ':')
4895 asm_statement->inputs = parse_asm_constraints();
4896 if(token.type != ':')
4900 asm_statement->outputs = parse_asm_constraints();
4901 if(token.type != ':')
4905 asm_statement->clobbers = parse_asm_clobbers();
4914 * Parse a case statement.
4916 static statement_t *parse_case_statement(void)
4920 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4922 statement->base.source_position = token.source_position;
4923 statement->case_label.expression = parse_expression();
4927 if (! is_constant_expression(statement->case_label.expression)) {
4928 errorf(statement->base.source_position,
4929 "case label does not reduce to an integer constant");
4931 /* TODO: check if the case label is already known */
4932 if (current_switch != NULL) {
4933 /* link all cases into the switch statement */
4934 if (current_switch->last_case == NULL) {
4935 current_switch->first_case =
4936 current_switch->last_case = &statement->case_label;
4938 current_switch->last_case->next = &statement->case_label;
4941 errorf(statement->base.source_position,
4942 "case label not within a switch statement");
4945 statement->case_label.label_statement = parse_statement();
4951 * Finds an existing default label of a switch statement.
4953 static case_label_statement_t *
4954 find_default_label(const switch_statement_t *statement)
4956 for (case_label_statement_t *label = statement->first_case;
4958 label = label->next) {
4959 if (label->expression == NULL)
4966 * Parse a default statement.
4968 static statement_t *parse_default_statement(void)
4972 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4974 statement->base.source_position = token.source_position;
4977 if (current_switch != NULL) {
4978 const case_label_statement_t *def_label = find_default_label(current_switch);
4979 if (def_label != NULL) {
4980 errorf(HERE, "multiple default labels in one switch");
4981 errorf(def_label->statement.source_position,
4982 "this is the first default label");
4984 /* link all cases into the switch statement */
4985 if (current_switch->last_case == NULL) {
4986 current_switch->first_case =
4987 current_switch->last_case = &statement->case_label;
4989 current_switch->last_case->next = &statement->case_label;
4993 errorf(statement->base.source_position,
4994 "'default' label not within a switch statement");
4996 statement->label.label_statement = parse_statement();
5002 * Return the declaration for a given label symbol or create a new one.
5004 static declaration_t *get_label(symbol_t *symbol)
5006 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5007 assert(current_function != NULL);
5008 /* if we found a label in the same function, then we already created the
5010 if(candidate != NULL
5011 && candidate->parent_context == ¤t_function->context) {
5015 /* otherwise we need to create a new one */
5016 declaration_t *const declaration = allocate_declaration_zero();
5017 declaration->namespc = NAMESPACE_LABEL;
5018 declaration->symbol = symbol;
5020 label_push(declaration);
5026 * Parse a label statement.
5028 static statement_t *parse_label_statement(void)
5030 assert(token.type == T_IDENTIFIER);
5031 symbol_t *symbol = token.v.symbol;
5034 declaration_t *label = get_label(symbol);
5036 /* if source position is already set then the label is defined twice,
5037 * otherwise it was just mentioned in a goto so far */
5038 if(label->source_position.input_name != NULL) {
5039 errorf(HERE, "duplicate label '%Y'", symbol);
5040 errorf(label->source_position, "previous definition of '%Y' was here",
5043 label->source_position = token.source_position;
5046 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
5048 label_statement->statement.kind = STATEMENT_LABEL;
5049 label_statement->statement.source_position = token.source_position;
5050 label_statement->label = label;
5054 if(token.type == '}') {
5055 /* TODO only warn? */
5056 errorf(HERE, "label at end of compound statement");
5057 return (statement_t*) label_statement;
5059 label_statement->label_statement = parse_statement();
5062 return (statement_t*) label_statement;
5066 * Parse an if statement.
5068 static statement_t *parse_if(void)
5072 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5073 statement->statement.kind = STATEMENT_IF;
5074 statement->statement.source_position = token.source_position;
5077 statement->condition = parse_expression();
5080 statement->true_statement = parse_statement();
5081 if(token.type == T_else) {
5083 statement->false_statement = parse_statement();
5086 return (statement_t*) statement;
5090 * Parse a switch statement.
5092 static statement_t *parse_switch(void)
5096 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5097 statement->statement.kind = STATEMENT_SWITCH;
5098 statement->statement.source_position = token.source_position;
5101 expression_t *const expr = parse_expression();
5102 type_t *const type = promote_integer(skip_typeref(expr->base.datatype));
5103 statement->expression = create_implicit_cast(expr, type);
5106 switch_statement_t *rem = current_switch;
5107 current_switch = statement;
5108 statement->body = parse_statement();
5109 current_switch = rem;
5111 return (statement_t*) statement;
5114 static statement_t *parse_loop_body(statement_t *const loop)
5116 statement_t *const rem = current_loop;
5117 current_loop = loop;
5118 statement_t *const body = parse_statement();
5124 * Parse a while statement.
5126 static statement_t *parse_while(void)
5130 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5131 statement->statement.kind = STATEMENT_WHILE;
5132 statement->statement.source_position = token.source_position;
5135 statement->condition = parse_expression();
5138 statement->body = parse_loop_body((statement_t*)statement);
5140 return (statement_t*) statement;
5144 * Parse a do statement.
5146 static statement_t *parse_do(void)
5150 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5151 statement->statement.kind = STATEMENT_DO_WHILE;
5152 statement->statement.source_position = token.source_position;
5154 statement->body = parse_loop_body((statement_t*)statement);
5157 statement->condition = parse_expression();
5161 return (statement_t*) statement;
5165 * Parse a for statement.
5167 static statement_t *parse_for(void)
5171 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5172 statement->statement.kind = STATEMENT_FOR;
5173 statement->statement.source_position = token.source_position;
5177 int top = environment_top();
5178 context_t *last_context = context;
5179 set_context(&statement->context);
5181 if(token.type != ';') {
5182 if(is_declaration_specifier(&token, false)) {
5183 parse_declaration(record_declaration);
5185 statement->initialisation = parse_expression();
5192 if(token.type != ';') {
5193 statement->condition = parse_expression();
5196 if(token.type != ')') {
5197 statement->step = parse_expression();
5200 statement->body = parse_loop_body((statement_t*)statement);
5202 assert(context == &statement->context);
5203 set_context(last_context);
5204 environment_pop_to(top);
5206 return (statement_t*) statement;
5210 * Parse a goto statement.
5212 static statement_t *parse_goto(void)
5216 if(token.type != T_IDENTIFIER) {
5217 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5221 symbol_t *symbol = token.v.symbol;
5224 declaration_t *label = get_label(symbol);
5226 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5228 statement->statement.kind = STATEMENT_GOTO;
5229 statement->statement.source_position = token.source_position;
5231 statement->label = label;
5233 /* remember the goto's in a list for later checking */
5234 if (goto_last == NULL) {
5235 goto_first = goto_last = statement;
5237 goto_last->next = statement;
5242 return (statement_t*) statement;
5246 * Parse a continue statement.
5248 static statement_t *parse_continue(void)
5250 statement_t *statement;
5251 if (current_loop == NULL) {
5252 errorf(HERE, "continue statement not within loop");
5255 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5257 statement->base.source_position = token.source_position;
5267 * Parse a break statement.
5269 static statement_t *parse_break(void)
5271 statement_t *statement;
5272 if (current_switch == NULL && current_loop == NULL) {
5273 errorf(HERE, "break statement not within loop or switch");
5276 statement = allocate_statement_zero(STATEMENT_BREAK);
5278 statement->base.source_position = token.source_position;
5288 * Check if a given declaration represents a local variable.
5290 static bool is_local_var_declaration(const declaration_t *declaration) {
5291 switch ((storage_class_tag_t) declaration->storage_class) {
5292 case STORAGE_CLASS_NONE:
5293 case STORAGE_CLASS_AUTO:
5294 case STORAGE_CLASS_REGISTER: {
5295 const type_t *type = skip_typeref(declaration->type);
5296 if(is_type_function(type)) {
5308 * Check if a given expression represents a local variable.
5310 static bool is_local_variable(const expression_t *expression)
5312 if (expression->base.kind != EXPR_REFERENCE) {
5315 const declaration_t *declaration = expression->reference.declaration;
5316 return is_local_var_declaration(declaration);
5320 * Parse a return statement.
5322 static statement_t *parse_return(void)
5326 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5328 statement->statement.kind = STATEMENT_RETURN;
5329 statement->statement.source_position = token.source_position;
5331 assert(is_type_function(current_function->type));
5332 function_type_t *function_type = ¤t_function->type->function;
5333 type_t *return_type = function_type->return_type;
5335 expression_t *return_value = NULL;
5336 if(token.type != ';') {
5337 return_value = parse_expression();
5341 if(return_type == NULL)
5342 return (statement_t*) statement;
5343 if(return_value != NULL && return_value->base.datatype == NULL)
5344 return (statement_t*) statement;
5346 return_type = skip_typeref(return_type);
5348 if(return_value != NULL) {
5349 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5351 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5352 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5353 warningf(statement->statement.source_position,
5354 "'return' with a value, in function returning void");
5355 return_value = NULL;
5357 if(is_type_valid(return_type)) {
5358 semantic_assign(return_type, &return_value, "'return'");
5361 /* check for returning address of a local var */
5362 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5363 const expression_t *expression = return_value->unary.value;
5364 if (is_local_variable(expression)) {
5365 warningf(statement->statement.source_position,
5366 "function returns address of local variable");
5370 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5371 warningf(statement->statement.source_position,
5372 "'return' without value, in function returning non-void");
5375 statement->return_value = return_value;
5377 return (statement_t*) statement;
5381 * Parse a declaration statement.
5383 static statement_t *parse_declaration_statement(void)
5385 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5387 statement->base.source_position = token.source_position;
5389 declaration_t *before = last_declaration;
5390 parse_declaration(record_declaration);
5392 if(before == NULL) {
5393 statement->declaration.declarations_begin = context->declarations;
5395 statement->declaration.declarations_begin = before->next;
5397 statement->declaration.declarations_end = last_declaration;
5403 * Parse an expression statement, ie. expr ';'.
5405 static statement_t *parse_expression_statement(void)
5407 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5409 statement->base.source_position = token.source_position;
5410 statement->expression.expression = parse_expression();
5418 * Parse a statement.
5420 static statement_t *parse_statement(void)
5422 statement_t *statement = NULL;
5424 /* declaration or statement */
5425 switch(token.type) {
5427 statement = parse_asm_statement();
5431 statement = parse_case_statement();
5435 statement = parse_default_statement();
5439 statement = parse_compound_statement();
5443 statement = parse_if();
5447 statement = parse_switch();
5451 statement = parse_while();
5455 statement = parse_do();
5459 statement = parse_for();
5463 statement = parse_goto();
5467 statement = parse_continue();
5471 statement = parse_break();
5475 statement = parse_return();
5484 if(look_ahead(1)->type == ':') {
5485 statement = parse_label_statement();
5489 if(is_typedef_symbol(token.v.symbol)) {
5490 statement = parse_declaration_statement();
5494 statement = parse_expression_statement();
5497 case T___extension__:
5498 /* this can be a prefix to a declaration or an expression statement */
5499 /* we simply eat it now and parse the rest with tail recursion */
5502 } while(token.type == T___extension__);
5503 statement = parse_statement();
5507 statement = parse_declaration_statement();
5511 statement = parse_expression_statement();
5515 assert(statement == NULL
5516 || statement->base.source_position.input_name != NULL);
5522 * Parse a compound statement.
5524 static statement_t *parse_compound_statement(void)
5526 compound_statement_t *compound_statement
5527 = allocate_ast_zero(sizeof(compound_statement[0]));
5528 compound_statement->statement.kind = STATEMENT_COMPOUND;
5529 compound_statement->statement.source_position = token.source_position;
5533 int top = environment_top();
5534 context_t *last_context = context;
5535 set_context(&compound_statement->context);
5537 statement_t *last_statement = NULL;
5539 while(token.type != '}' && token.type != T_EOF) {
5540 statement_t *statement = parse_statement();
5541 if(statement == NULL)
5544 if(last_statement != NULL) {
5545 last_statement->base.next = statement;
5547 compound_statement->statements = statement;
5550 while(statement->base.next != NULL)
5551 statement = statement->base.next;
5553 last_statement = statement;
5556 if(token.type == '}') {
5559 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5562 assert(context == &compound_statement->context);
5563 set_context(last_context);
5564 environment_pop_to(top);
5566 return (statement_t*) compound_statement;
5570 * Initialize builtin types.
5572 static void initialize_builtin_types(void)
5574 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5575 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5576 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5577 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5578 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5579 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5580 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5581 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5583 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5584 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5585 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5586 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5590 * Parse a translation unit.
5592 static translation_unit_t *parse_translation_unit(void)
5594 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5596 assert(global_context == NULL);
5597 global_context = &unit->context;
5599 assert(context == NULL);
5600 set_context(&unit->context);
5602 initialize_builtin_types();
5604 while(token.type != T_EOF) {
5605 if (token.type == ';') {
5606 /* TODO error in strict mode */
5607 warningf(HERE, "stray ';' outside of function");
5610 parse_external_declaration();
5614 assert(context == &unit->context);
5616 last_declaration = NULL;
5618 assert(global_context == &unit->context);
5619 global_context = NULL;
5627 * @return the translation unit or NULL if errors occurred.
5629 translation_unit_t *parse(void)
5631 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5632 label_stack = NEW_ARR_F(stack_entry_t, 0);
5633 diagnostic_count = 0;
5637 type_set_output(stderr);
5638 ast_set_output(stderr);
5640 lookahead_bufpos = 0;
5641 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5644 translation_unit_t *unit = parse_translation_unit();
5646 DEL_ARR_F(environment_stack);
5647 DEL_ARR_F(label_stack);
5656 * Initialize the parser.
5658 void init_parser(void)
5660 init_expression_parsers();
5661 obstack_init(&temp_obst);
5663 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5664 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5668 * Terminate the parser.
5670 void exit_parser(void)
5672 obstack_free(&temp_obst, NULL);