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;
2446 } else if (ret_type->kind == TYPE_ARRAY) {
2447 errorf(declaration->source_position, "'%Y' declared as function returning an array",
2448 declaration->symbol);
2449 declaration->type = type_error_type;
2452 if (! is_valid_array_type(type)) {
2453 errorf(declaration->source_position, "declaration of '%Y' as array of voids",
2454 declaration->symbol);
2455 declaration->type = type_error_type;
2460 static declaration_t *record_function_definition(declaration_t *const declaration)
2462 return internal_record_declaration(declaration, true);
2465 static void parser_error_multiple_definition(declaration_t *declaration,
2466 const source_position_t source_position)
2468 errorf(source_position, "multiple definition of symbol '%Y'",
2469 declaration->symbol);
2470 errorf(declaration->source_position,
2471 "this is the location of the previous definition.");
2474 static bool is_declaration_specifier(const token_t *token,
2475 bool only_type_specifiers)
2477 switch(token->type) {
2481 return is_typedef_symbol(token->v.symbol);
2483 case T___extension__:
2486 return !only_type_specifiers;
2493 static void parse_init_declarator_rest(declaration_t *declaration)
2497 type_t *orig_type = declaration->type;
2498 type_t *type = type = skip_typeref(orig_type);
2500 if(declaration->init.initializer != NULL) {
2501 parser_error_multiple_definition(declaration, token.source_position);
2504 initializer_t *initializer = parse_initializer(type);
2506 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2507 * the array type size */
2508 if(is_type_array(type) && initializer != NULL) {
2509 array_type_t *array_type = &type->array;
2511 if(array_type->size == NULL) {
2512 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2514 cnst->base.datatype = type_size_t;
2516 switch (initializer->kind) {
2517 case INITIALIZER_LIST: {
2518 initializer_list_t *const list = &initializer->list;
2519 cnst->conste.v.int_value = list->len;
2523 case INITIALIZER_STRING: {
2524 initializer_string_t *const string = &initializer->string;
2525 cnst->conste.v.int_value = string->string.size;
2529 case INITIALIZER_WIDE_STRING: {
2530 initializer_wide_string_t *const string = &initializer->wide_string;
2531 cnst->conste.v.int_value = string->string.size;
2536 panic("invalid initializer type");
2539 array_type->size = cnst;
2543 if(is_type_function(type)) {
2544 errorf(declaration->source_position,
2545 "initializers not allowed for function types at declator '%Y' (type '%T')",
2546 declaration->symbol, orig_type);
2548 declaration->init.initializer = initializer;
2552 /* parse rest of a declaration without any declarator */
2553 static void parse_anonymous_declaration_rest(
2554 const declaration_specifiers_t *specifiers,
2555 parsed_declaration_func finished_declaration)
2559 declaration_t *const declaration = allocate_declaration_zero();
2560 declaration->type = specifiers->type;
2561 declaration->storage_class = specifiers->storage_class;
2562 declaration->source_position = specifiers->source_position;
2564 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2565 warningf(declaration->source_position, "useless storage class in empty declaration");
2568 type_t *type = declaration->type;
2569 switch (type->kind) {
2570 case TYPE_COMPOUND_STRUCT:
2571 case TYPE_COMPOUND_UNION: {
2572 const compound_type_t *compound_type = &type->compound;
2573 if (compound_type->declaration->symbol == NULL) {
2574 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2583 warningf(declaration->source_position, "empty declaration");
2587 finished_declaration(declaration);
2590 static void parse_declaration_rest(declaration_t *ndeclaration,
2591 const declaration_specifiers_t *specifiers,
2592 parsed_declaration_func finished_declaration)
2595 declaration_t *declaration = finished_declaration(ndeclaration);
2597 type_t *orig_type = declaration->type;
2598 type_t *type = skip_typeref(orig_type);
2600 if(is_type_valid(type) &&
2601 type->kind != TYPE_FUNCTION && declaration->is_inline) {
2602 warningf(declaration->source_position,
2603 "variable '%Y' declared 'inline'\n", declaration->symbol);
2606 if(token.type == '=') {
2607 parse_init_declarator_rest(declaration);
2610 if(token.type != ',')
2614 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2619 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2621 symbol_t *symbol = declaration->symbol;
2622 if(symbol == NULL) {
2623 errorf(HERE, "anonymous declaration not valid as function parameter");
2626 namespace_t namespc = (namespace_t) declaration->namespc;
2627 if(namespc != NAMESPACE_NORMAL) {
2628 return record_declaration(declaration);
2631 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2632 if(previous_declaration == NULL ||
2633 previous_declaration->parent_context != context) {
2634 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2639 if(previous_declaration->type == NULL) {
2640 previous_declaration->type = declaration->type;
2641 previous_declaration->storage_class = declaration->storage_class;
2642 previous_declaration->parent_context = context;
2643 return previous_declaration;
2645 return record_declaration(declaration);
2649 static void parse_declaration(parsed_declaration_func finished_declaration)
2651 declaration_specifiers_t specifiers;
2652 memset(&specifiers, 0, sizeof(specifiers));
2653 parse_declaration_specifiers(&specifiers);
2655 if(token.type == ';') {
2656 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2658 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2659 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2663 static void parse_kr_declaration_list(declaration_t *declaration)
2665 type_t *type = skip_typeref(declaration->type);
2666 if(!is_type_function(type))
2669 if(!type->function.kr_style_parameters)
2672 /* push function parameters */
2673 int top = environment_top();
2674 context_t *last_context = context;
2675 set_context(&declaration->context);
2677 declaration_t *parameter = declaration->context.declarations;
2678 for( ; parameter != NULL; parameter = parameter->next) {
2679 assert(parameter->parent_context == NULL);
2680 parameter->parent_context = context;
2681 environment_push(parameter);
2684 /* parse declaration list */
2685 while(is_declaration_specifier(&token, false)) {
2686 parse_declaration(finished_kr_declaration);
2689 /* pop function parameters */
2690 assert(context == &declaration->context);
2691 set_context(last_context);
2692 environment_pop_to(top);
2694 /* update function type */
2695 type_t *new_type = duplicate_type(type);
2696 new_type->function.kr_style_parameters = false;
2698 function_parameter_t *parameters = NULL;
2699 function_parameter_t *last_parameter = NULL;
2701 declaration_t *parameter_declaration = declaration->context.declarations;
2702 for( ; parameter_declaration != NULL;
2703 parameter_declaration = parameter_declaration->next) {
2704 type_t *parameter_type = parameter_declaration->type;
2705 if(parameter_type == NULL) {
2707 errorf(HERE, "no type specified for function parameter '%Y'",
2708 parameter_declaration->symbol);
2710 warningf(HERE, "no type specified for function parameter '%Y', using int",
2711 parameter_declaration->symbol);
2712 parameter_type = type_int;
2713 parameter_declaration->type = parameter_type;
2717 semantic_parameter(parameter_declaration);
2718 parameter_type = parameter_declaration->type;
2720 function_parameter_t *function_parameter
2721 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2722 memset(function_parameter, 0, sizeof(function_parameter[0]));
2724 function_parameter->type = parameter_type;
2725 if(last_parameter != NULL) {
2726 last_parameter->next = function_parameter;
2728 parameters = function_parameter;
2730 last_parameter = function_parameter;
2732 new_type->function.parameters = parameters;
2734 type = typehash_insert(new_type);
2735 if(type != new_type) {
2736 obstack_free(type_obst, new_type);
2739 declaration->type = type;
2743 * Check if all labels are defined in the current function.
2745 static void check_for_missing_labels(void)
2747 bool first_err = true;
2748 for (const goto_statement_t *goto_statement = goto_first;
2749 goto_statement != NULL;
2750 goto_statement = goto_statement->next) {
2751 const declaration_t *label = goto_statement->label;
2753 if (label->source_position.input_name == NULL) {
2756 diagnosticf("%s: In function '%Y':\n",
2757 current_function->source_position.input_name,
2758 current_function->symbol);
2760 errorf(goto_statement->statement.source_position,
2761 "label '%Y' used but not defined", label->symbol);
2764 goto_first = goto_last = NULL;
2767 static void parse_external_declaration(void)
2769 /* function-definitions and declarations both start with declaration
2771 declaration_specifiers_t specifiers;
2772 memset(&specifiers, 0, sizeof(specifiers));
2773 parse_declaration_specifiers(&specifiers);
2775 /* must be a declaration */
2776 if(token.type == ';') {
2777 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2781 /* declarator is common to both function-definitions and declarations */
2782 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2784 /* must be a declaration */
2785 if(token.type == ',' || token.type == '=' || token.type == ';') {
2786 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2790 /* must be a function definition */
2791 parse_kr_declaration_list(ndeclaration);
2793 if(token.type != '{') {
2794 parse_error_expected("while parsing function definition", '{', 0);
2799 type_t *type = ndeclaration->type;
2805 /* note that we don't skip typerefs: the standard doesn't allow them here
2806 * (so we can't use is_type_function here) */
2807 if(type->kind != TYPE_FUNCTION) {
2808 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2809 type, ndeclaration->symbol);
2814 /* § 6.7.5.3 (14) a function definition with () means no
2815 * parameters (and not unspecified parameters) */
2816 if(type->function.unspecified_parameters) {
2817 type_t *duplicate = duplicate_type(type);
2818 duplicate->function.unspecified_parameters = false;
2820 type = typehash_insert(duplicate);
2821 if(type != duplicate) {
2822 obstack_free(type_obst, duplicate);
2824 ndeclaration->type = type;
2827 declaration_t *const declaration = record_function_definition(ndeclaration);
2828 if(ndeclaration != declaration) {
2829 declaration->context = ndeclaration->context;
2831 type = skip_typeref(declaration->type);
2833 /* push function parameters and switch context */
2834 int top = environment_top();
2835 context_t *last_context = context;
2836 set_context(&declaration->context);
2838 declaration_t *parameter = declaration->context.declarations;
2839 for( ; parameter != NULL; parameter = parameter->next) {
2840 if(parameter->parent_context == &ndeclaration->context) {
2841 parameter->parent_context = context;
2843 assert(parameter->parent_context == NULL
2844 || parameter->parent_context == context);
2845 parameter->parent_context = context;
2846 environment_push(parameter);
2849 if(declaration->init.statement != NULL) {
2850 parser_error_multiple_definition(declaration, token.source_position);
2852 goto end_of_parse_external_declaration;
2854 /* parse function body */
2855 int label_stack_top = label_top();
2856 declaration_t *old_current_function = current_function;
2857 current_function = declaration;
2859 declaration->init.statement = parse_compound_statement();
2860 check_for_missing_labels();
2862 assert(current_function == declaration);
2863 current_function = old_current_function;
2864 label_pop_to(label_stack_top);
2867 end_of_parse_external_declaration:
2868 assert(context == &declaration->context);
2869 set_context(last_context);
2870 environment_pop_to(top);
2873 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2875 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2876 type->bitfield.base = base;
2877 type->bitfield.size = size;
2882 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2884 /* TODO: check constraints for struct declarations (in specifiers) */
2886 declaration_t *declaration;
2888 if(token.type == ':') {
2891 type_t *base_type = specifiers->type;
2892 expression_t *size = parse_constant_expression();
2894 type_t *type = make_bitfield_type(base_type, size);
2896 declaration = allocate_declaration_zero();
2897 declaration->namespc = NAMESPACE_NORMAL;
2898 declaration->storage_class = STORAGE_CLASS_NONE;
2899 declaration->source_position = token.source_position;
2900 declaration->modifiers = specifiers->decl_modifiers;
2901 declaration->type = type;
2903 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2905 if(token.type == ':') {
2907 expression_t *size = parse_constant_expression();
2909 type_t *type = make_bitfield_type(declaration->type, size);
2910 declaration->type = type;
2913 record_declaration(declaration);
2915 if(token.type != ',')
2922 static void parse_compound_type_entries(void)
2926 while(token.type != '}' && token.type != T_EOF) {
2927 declaration_specifiers_t specifiers;
2928 memset(&specifiers, 0, sizeof(specifiers));
2929 parse_declaration_specifiers(&specifiers);
2931 parse_struct_declarators(&specifiers);
2933 if(token.type == T_EOF) {
2934 errorf(HERE, "EOF while parsing struct");
2939 static type_t *parse_typename(void)
2941 declaration_specifiers_t specifiers;
2942 memset(&specifiers, 0, sizeof(specifiers));
2943 parse_declaration_specifiers(&specifiers);
2944 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2945 /* TODO: improve error message, user does probably not know what a
2946 * storage class is...
2948 errorf(HERE, "typename may not have a storage class");
2951 type_t *result = parse_abstract_declarator(specifiers.type);
2959 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2960 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2961 expression_t *left);
2963 typedef struct expression_parser_function_t expression_parser_function_t;
2964 struct expression_parser_function_t {
2965 unsigned precedence;
2966 parse_expression_function parser;
2967 unsigned infix_precedence;
2968 parse_expression_infix_function infix_parser;
2971 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2974 * Creates a new invalid expression.
2976 static expression_t *create_invalid_expression(void)
2978 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2979 expression->base.source_position = token.source_position;
2984 * Prints an error message if an expression was expected but not read
2986 static expression_t *expected_expression_error(void)
2988 /* skip the error message if the error token was read */
2989 if (token.type != T_ERROR) {
2990 errorf(HERE, "expected expression, got token '%K'", &token);
2994 return create_invalid_expression();
2998 * Parse a string constant.
3000 static expression_t *parse_string_const(void)
3002 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3003 cnst->base.datatype = type_string;
3004 cnst->string.value = parse_string_literals();
3010 * Parse a wide string constant.
3012 static expression_t *parse_wide_string_const(void)
3014 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3015 cnst->base.datatype = type_wchar_t_ptr;
3016 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
3022 * Parse an integer constant.
3024 static expression_t *parse_int_const(void)
3026 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3027 cnst->base.datatype = token.datatype;
3028 cnst->conste.v.int_value = token.v.intvalue;
3036 * Parse a float constant.
3038 static expression_t *parse_float_const(void)
3040 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3041 cnst->base.datatype = token.datatype;
3042 cnst->conste.v.float_value = token.v.floatvalue;
3049 static declaration_t *create_implicit_function(symbol_t *symbol,
3050 const source_position_t source_position)
3052 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
3053 ntype->function.return_type = type_int;
3054 ntype->function.unspecified_parameters = true;
3056 type_t *type = typehash_insert(ntype);
3061 declaration_t *const declaration = allocate_declaration_zero();
3062 declaration->storage_class = STORAGE_CLASS_EXTERN;
3063 declaration->type = type;
3064 declaration->symbol = symbol;
3065 declaration->source_position = source_position;
3066 declaration->parent_context = global_context;
3068 context_t *old_context = context;
3069 set_context(global_context);
3071 environment_push(declaration);
3072 /* prepend the declaration to the global declarations list */
3073 declaration->next = context->declarations;
3074 context->declarations = declaration;
3076 assert(context == global_context);
3077 set_context(old_context);
3083 * Creates a return_type (func)(argument_type) function type if not
3086 * @param return_type the return type
3087 * @param argument_type the argument type
3089 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3091 function_parameter_t *parameter
3092 = obstack_alloc(type_obst, sizeof(parameter[0]));
3093 memset(parameter, 0, sizeof(parameter[0]));
3094 parameter->type = argument_type;
3096 type_t *type = allocate_type_zero(TYPE_FUNCTION);
3097 type->function.return_type = return_type;
3098 type->function.parameters = parameter;
3100 type_t *result = typehash_insert(type);
3101 if(result != type) {
3109 * Creates a function type for some function like builtins.
3111 * @param symbol the symbol describing the builtin
3113 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3115 switch(symbol->ID) {
3116 case T___builtin_alloca:
3117 return make_function_1_type(type_void_ptr, type_size_t);
3118 case T___builtin_nan:
3119 return make_function_1_type(type_double, type_string);
3120 case T___builtin_nanf:
3121 return make_function_1_type(type_float, type_string);
3122 case T___builtin_nand:
3123 return make_function_1_type(type_long_double, type_string);
3124 case T___builtin_va_end:
3125 return make_function_1_type(type_void, type_valist);
3127 panic("not implemented builtin symbol found");
3132 * Performs automatic type cast as described in § 6.3.2.1.
3134 * @param orig_type the original type
3136 static type_t *automatic_type_conversion(type_t *orig_type)
3138 if(orig_type == NULL)
3141 type_t *type = skip_typeref(orig_type);
3142 if(is_type_array(type)) {
3143 array_type_t *array_type = &type->array;
3144 type_t *element_type = array_type->element_type;
3145 unsigned qualifiers = array_type->type.qualifiers;
3147 return make_pointer_type(element_type, qualifiers);
3150 if(is_type_function(type)) {
3151 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3158 * reverts the automatic casts of array to pointer types and function
3159 * to function-pointer types as defined § 6.3.2.1
3161 type_t *revert_automatic_type_conversion(const expression_t *expression)
3163 if(expression->base.datatype == NULL)
3166 switch(expression->kind) {
3167 case EXPR_REFERENCE: {
3168 const reference_expression_t *ref = &expression->reference;
3169 return ref->declaration->type;
3172 const select_expression_t *select = &expression->select;
3173 return select->compound_entry->type;
3175 case EXPR_UNARY_DEREFERENCE: {
3176 expression_t *value = expression->unary.value;
3177 type_t *type = skip_typeref(value->base.datatype);
3178 pointer_type_t *pointer_type = &type->pointer;
3180 return pointer_type->points_to;
3182 case EXPR_BUILTIN_SYMBOL: {
3183 const builtin_symbol_expression_t *builtin
3184 = &expression->builtin_symbol;
3185 return get_builtin_symbol_type(builtin->symbol);
3187 case EXPR_ARRAY_ACCESS: {
3188 const array_access_expression_t *array_access
3189 = &expression->array_access;
3190 const expression_t *array_ref = array_access->array_ref;
3191 type_t *type_left = skip_typeref(array_ref->base.datatype);
3192 assert(is_type_pointer(type_left));
3193 pointer_type_t *pointer_type = &type_left->pointer;
3194 return pointer_type->points_to;
3201 return expression->base.datatype;
3204 static expression_t *parse_reference(void)
3206 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3208 reference_expression_t *ref = &expression->reference;
3209 ref->symbol = token.v.symbol;
3211 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3213 source_position_t source_position = token.source_position;
3216 if(declaration == NULL) {
3217 if (! strict_mode && token.type == '(') {
3218 /* an implicitly defined function */
3219 warningf(HERE, "implicit declaration of function '%Y'",
3222 declaration = create_implicit_function(ref->symbol,
3225 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3230 type_t *type = declaration->type;
3232 /* we always do the auto-type conversions; the & and sizeof parser contains
3233 * code to revert this! */
3234 type = automatic_type_conversion(type);
3236 ref->declaration = declaration;
3237 ref->expression.datatype = type;
3242 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3246 /* TODO check if explicit cast is allowed and issue warnings/errors */
3249 static expression_t *parse_cast(void)
3251 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3253 cast->base.source_position = token.source_position;
3255 type_t *type = parse_typename();
3258 expression_t *value = parse_sub_expression(20);
3260 check_cast_allowed(value, type);
3262 cast->base.datatype = type;
3263 cast->unary.value = value;
3268 static expression_t *parse_statement_expression(void)
3270 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3272 statement_t *statement = parse_compound_statement();
3273 expression->statement.statement = statement;
3274 if(statement == NULL) {
3279 assert(statement->kind == STATEMENT_COMPOUND);
3280 compound_statement_t *compound_statement = &statement->compound;
3282 /* find last statement and use it's type */
3283 const statement_t *last_statement = NULL;
3284 const statement_t *iter = compound_statement->statements;
3285 for( ; iter != NULL; iter = iter->base.next) {
3286 last_statement = iter;
3289 if(last_statement->kind == STATEMENT_EXPRESSION) {
3290 const expression_statement_t *expression_statement
3291 = &last_statement->expression;
3292 expression->base.datatype
3293 = expression_statement->expression->base.datatype;
3295 expression->base.datatype = type_void;
3303 static expression_t *parse_brace_expression(void)
3307 switch(token.type) {
3309 /* gcc extension: a statement expression */
3310 return parse_statement_expression();
3314 return parse_cast();
3316 if(is_typedef_symbol(token.v.symbol)) {
3317 return parse_cast();
3321 expression_t *result = parse_expression();
3327 static expression_t *parse_function_keyword(void)
3332 if (current_function == NULL) {
3333 errorf(HERE, "'__func__' used outside of a function");
3336 string_literal_expression_t *expression
3337 = allocate_ast_zero(sizeof(expression[0]));
3339 expression->expression.kind = EXPR_FUNCTION;
3340 expression->expression.datatype = type_string;
3342 return (expression_t*) expression;
3345 static expression_t *parse_pretty_function_keyword(void)
3347 eat(T___PRETTY_FUNCTION__);
3350 if (current_function == NULL) {
3351 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3354 string_literal_expression_t *expression
3355 = allocate_ast_zero(sizeof(expression[0]));
3357 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3358 expression->expression.datatype = type_string;
3360 return (expression_t*) expression;
3363 static designator_t *parse_designator(void)
3365 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3367 if(token.type != T_IDENTIFIER) {
3368 parse_error_expected("while parsing member designator",
3373 result->symbol = token.v.symbol;
3376 designator_t *last_designator = result;
3378 if(token.type == '.') {
3380 if(token.type != T_IDENTIFIER) {
3381 parse_error_expected("while parsing member designator",
3386 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3387 designator->symbol = token.v.symbol;
3390 last_designator->next = designator;
3391 last_designator = designator;
3394 if(token.type == '[') {
3396 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3397 designator->array_access = parse_expression();
3398 if(designator->array_access == NULL) {
3404 last_designator->next = designator;
3405 last_designator = designator;
3414 static expression_t *parse_offsetof(void)
3416 eat(T___builtin_offsetof);
3418 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3419 expression->base.datatype = type_size_t;
3422 expression->offsetofe.type = parse_typename();
3424 expression->offsetofe.designator = parse_designator();
3430 static expression_t *parse_va_start(void)
3432 eat(T___builtin_va_start);
3434 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3437 expression->va_starte.ap = parse_assignment_expression();
3439 expression_t *const expr = parse_assignment_expression();
3440 if (expr->kind == EXPR_REFERENCE) {
3441 declaration_t *const decl = expr->reference.declaration;
3442 if (decl->parent_context == ¤t_function->context &&
3443 decl->next == NULL) {
3444 expression->va_starte.parameter = decl;
3449 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3451 return create_invalid_expression();
3454 static expression_t *parse_va_arg(void)
3456 eat(T___builtin_va_arg);
3458 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3461 expression->va_arge.ap = parse_assignment_expression();
3463 expression->base.datatype = parse_typename();
3469 static expression_t *parse_builtin_symbol(void)
3471 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3473 symbol_t *symbol = token.v.symbol;
3475 expression->builtin_symbol.symbol = symbol;
3478 type_t *type = get_builtin_symbol_type(symbol);
3479 type = automatic_type_conversion(type);
3481 expression->base.datatype = type;
3485 static expression_t *parse_builtin_constant(void)
3487 eat(T___builtin_constant_p);
3489 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3492 expression->builtin_constant.value = parse_assignment_expression();
3494 expression->base.datatype = type_int;
3499 static expression_t *parse_builtin_prefetch(void)
3501 eat(T___builtin_prefetch);
3503 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3506 expression->builtin_prefetch.adr = parse_assignment_expression();
3507 if (token.type == ',') {
3509 expression->builtin_prefetch.rw = parse_assignment_expression();
3511 if (token.type == ',') {
3513 expression->builtin_prefetch.locality = parse_assignment_expression();
3516 expression->base.datatype = type_void;
3521 static expression_t *parse_compare_builtin(void)
3523 expression_t *expression;
3525 switch(token.type) {
3526 case T___builtin_isgreater:
3527 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3529 case T___builtin_isgreaterequal:
3530 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3532 case T___builtin_isless:
3533 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3535 case T___builtin_islessequal:
3536 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3538 case T___builtin_islessgreater:
3539 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3541 case T___builtin_isunordered:
3542 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3545 panic("invalid compare builtin found");
3551 expression->binary.left = parse_assignment_expression();
3553 expression->binary.right = parse_assignment_expression();
3556 type_t *orig_type_left = expression->binary.left->base.datatype;
3557 type_t *orig_type_right = expression->binary.right->base.datatype;
3558 if(orig_type_left == NULL || orig_type_right == NULL)
3561 type_t *type_left = skip_typeref(orig_type_left);
3562 type_t *type_right = skip_typeref(orig_type_right);
3563 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3564 type_error_incompatible("invalid operands in comparison",
3565 token.source_position, type_left, type_right);
3567 semantic_comparison(&expression->binary);
3573 static expression_t *parse_builtin_expect(void)
3575 eat(T___builtin_expect);
3577 expression_t *expression
3578 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3581 expression->binary.left = parse_assignment_expression();
3583 expression->binary.right = parse_constant_expression();
3586 expression->base.datatype = expression->binary.left->base.datatype;
3591 static expression_t *parse_assume(void) {
3594 expression_t *expression
3595 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3598 expression->unary.value = parse_assignment_expression();
3601 expression->base.datatype = type_void;
3605 static expression_t *parse_alignof(void) {
3608 expression_t *expression
3609 = allocate_expression_zero(EXPR_ALIGNOF);
3612 expression->alignofe.type = parse_typename();
3615 expression->base.datatype = type_size_t;
3619 static expression_t *parse_primary_expression(void)
3621 switch(token.type) {
3623 return parse_int_const();
3624 case T_FLOATINGPOINT:
3625 return parse_float_const();
3626 case T_STRING_LITERAL:
3627 return parse_string_const();
3628 case T_WIDE_STRING_LITERAL:
3629 return parse_wide_string_const();
3631 return parse_reference();
3632 case T___FUNCTION__:
3634 return parse_function_keyword();
3635 case T___PRETTY_FUNCTION__:
3636 return parse_pretty_function_keyword();
3637 case T___builtin_offsetof:
3638 return parse_offsetof();
3639 case T___builtin_va_start:
3640 return parse_va_start();
3641 case T___builtin_va_arg:
3642 return parse_va_arg();
3643 case T___builtin_expect:
3644 return parse_builtin_expect();
3645 case T___builtin_nanf:
3646 case T___builtin_alloca:
3647 case T___builtin_va_end:
3648 return parse_builtin_symbol();
3649 case T___builtin_isgreater:
3650 case T___builtin_isgreaterequal:
3651 case T___builtin_isless:
3652 case T___builtin_islessequal:
3653 case T___builtin_islessgreater:
3654 case T___builtin_isunordered:
3655 return parse_compare_builtin();
3656 case T___builtin_constant_p:
3657 return parse_builtin_constant();
3658 case T___builtin_prefetch:
3659 return parse_builtin_prefetch();
3661 return parse_alignof();
3663 return parse_assume();
3666 return parse_brace_expression();
3669 errorf(HERE, "unexpected token '%K'", &token);
3672 return create_invalid_expression();
3676 * Check if the expression has the character type and issue a warning then.
3678 static void check_for_char_index_type(const expression_t *expression) {
3679 type_t *type = expression->base.datatype;
3680 type_t *base_type = skip_typeref(type);
3682 if (base_type->base.kind == TYPE_ATOMIC) {
3683 switch (base_type->atomic.akind == ATOMIC_TYPE_CHAR) {
3684 warningf(expression->base.source_position,
3685 "array subscript has type '%T'", type);
3690 static expression_t *parse_array_expression(unsigned precedence,
3697 expression_t *inside = parse_expression();
3699 array_access_expression_t *array_access
3700 = allocate_ast_zero(sizeof(array_access[0]));
3702 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3704 type_t *type_left = left->base.datatype;
3705 type_t *type_inside = inside->base.datatype;
3706 type_t *return_type = NULL;
3708 if(type_left != NULL && type_inside != NULL) {
3709 type_left = skip_typeref(type_left);
3710 type_inside = skip_typeref(type_inside);
3712 if(is_type_pointer(type_left)) {
3713 pointer_type_t *pointer = &type_left->pointer;
3714 return_type = pointer->points_to;
3715 array_access->array_ref = left;
3716 array_access->index = inside;
3717 check_for_char_index_type(inside);
3718 } else if(is_type_pointer(type_inside)) {
3719 pointer_type_t *pointer = &type_inside->pointer;
3720 return_type = pointer->points_to;
3721 array_access->array_ref = inside;
3722 array_access->index = left;
3723 array_access->flipped = true;
3724 check_for_char_index_type(left);
3726 errorf(HERE, "array access on object with non-pointer types '%T', '%T'", type_left, type_inside);
3729 array_access->array_ref = left;
3730 array_access->index = inside;
3733 if(token.type != ']') {
3734 parse_error_expected("Problem while parsing array access", ']', 0);
3735 return (expression_t*) array_access;
3739 return_type = automatic_type_conversion(return_type);
3740 array_access->expression.datatype = return_type;
3742 return (expression_t*) array_access;
3745 static expression_t *parse_sizeof(unsigned precedence)
3749 sizeof_expression_t *sizeof_expression
3750 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3751 sizeof_expression->expression.kind = EXPR_SIZEOF;
3752 sizeof_expression->expression.datatype = type_size_t;
3754 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3756 sizeof_expression->type = parse_typename();
3759 expression_t *expression = parse_sub_expression(precedence);
3760 expression->base.datatype = revert_automatic_type_conversion(expression);
3762 sizeof_expression->type = expression->base.datatype;
3763 sizeof_expression->size_expression = expression;
3766 return (expression_t*) sizeof_expression;
3769 static expression_t *parse_select_expression(unsigned precedence,
3770 expression_t *compound)
3773 assert(token.type == '.' || token.type == T_MINUSGREATER);
3775 bool is_pointer = (token.type == T_MINUSGREATER);
3778 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3779 select->select.compound = compound;
3781 if(token.type != T_IDENTIFIER) {
3782 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3785 symbol_t *symbol = token.v.symbol;
3786 select->select.symbol = symbol;
3789 type_t *orig_type = compound->base.datatype;
3790 if(orig_type == NULL)
3791 return create_invalid_expression();
3793 type_t *type = skip_typeref(orig_type);
3795 type_t *type_left = type;
3797 if(type->kind != TYPE_POINTER) {
3798 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3799 return create_invalid_expression();
3801 pointer_type_t *pointer_type = &type->pointer;
3802 type_left = pointer_type->points_to;
3804 type_left = skip_typeref(type_left);
3806 if(type_left->kind != TYPE_COMPOUND_STRUCT
3807 && type_left->kind != TYPE_COMPOUND_UNION) {
3808 errorf(HERE, "request for member '%Y' in something not a struct or "
3809 "union, but '%T'", symbol, type_left);
3810 return create_invalid_expression();
3813 compound_type_t *compound_type = &type_left->compound;
3814 declaration_t *declaration = compound_type->declaration;
3816 if(!declaration->init.is_defined) {
3817 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3819 return create_invalid_expression();
3822 declaration_t *iter = declaration->context.declarations;
3823 for( ; iter != NULL; iter = iter->next) {
3824 if(iter->symbol == symbol) {
3829 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3830 return create_invalid_expression();
3833 /* we always do the auto-type conversions; the & and sizeof parser contains
3834 * code to revert this! */
3835 type_t *expression_type = automatic_type_conversion(iter->type);
3837 select->select.compound_entry = iter;
3838 select->base.datatype = expression_type;
3840 if(expression_type->kind == TYPE_BITFIELD) {
3841 expression_t *extract
3842 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3843 extract->unary.value = select;
3844 extract->base.datatype = expression_type->bitfield.base;
3853 * Parse a call expression, ie. expression '( ... )'.
3855 * @param expression the function address
3857 static expression_t *parse_call_expression(unsigned precedence,
3858 expression_t *expression)
3861 expression_t *result = allocate_expression_zero(EXPR_CALL);
3863 call_expression_t *call = &result->call;
3864 call->function = expression;
3866 function_type_t *function_type = NULL;
3867 type_t *orig_type = expression->base.datatype;
3868 if(is_type_valid(orig_type)) {
3869 type_t *type = skip_typeref(orig_type);
3871 if(is_type_pointer(type)) {
3872 pointer_type_t *pointer_type = &type->pointer;
3874 type = skip_typeref(pointer_type->points_to);
3876 if (is_type_function(type)) {
3877 function_type = &type->function;
3878 call->expression.datatype = function_type->return_type;
3881 if(function_type == NULL) {
3882 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3884 function_type = NULL;
3885 call->expression.datatype = NULL;
3889 /* parse arguments */
3892 if(token.type != ')') {
3893 call_argument_t *last_argument = NULL;
3896 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3898 argument->expression = parse_assignment_expression();
3899 if(last_argument == NULL) {
3900 call->arguments = argument;
3902 last_argument->next = argument;
3904 last_argument = argument;
3906 if(token.type != ',')
3913 if(function_type != NULL) {
3914 function_parameter_t *parameter = function_type->parameters;
3915 call_argument_t *argument = call->arguments;
3916 for( ; parameter != NULL && argument != NULL;
3917 parameter = parameter->next, argument = argument->next) {
3918 type_t *expected_type = parameter->type;
3919 /* TODO report context in error messages */
3920 argument->expression = create_implicit_cast(argument->expression,
3923 /* too few parameters */
3924 if(parameter != NULL) {
3925 errorf(HERE, "too few arguments to function '%E'", expression);
3926 } else if(argument != NULL) {
3927 /* too many parameters */
3928 if(!function_type->variadic
3929 && !function_type->unspecified_parameters) {
3930 errorf(HERE, "too many arguments to function '%E'", expression);
3932 /* do default promotion */
3933 for( ; argument != NULL; argument = argument->next) {
3934 type_t *type = argument->expression->base.datatype;
3939 type = skip_typeref(type);
3940 if(is_type_integer(type)) {
3941 type = promote_integer(type);
3942 } else if(type == type_float) {
3946 argument->expression
3947 = create_implicit_cast(argument->expression, type);
3950 check_format(&result->call);
3953 check_format(&result->call);
3960 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3962 static bool same_compound_type(const type_t *type1, const type_t *type2)
3964 if(!is_type_compound(type1))
3966 if(type1->kind != type2->kind)
3969 const compound_type_t *compound1 = &type1->compound;
3970 const compound_type_t *compound2 = &type2->compound;
3972 return compound1->declaration == compound2->declaration;
3976 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3978 * @param expression the conditional expression
3980 static expression_t *parse_conditional_expression(unsigned precedence,
3981 expression_t *expression)
3985 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3987 conditional_expression_t *conditional = &result->conditional;
3988 conditional->condition = expression;
3991 type_t *condition_type_orig = expression->base.datatype;
3992 if(is_type_valid(condition_type_orig)) {
3993 type_t *condition_type = skip_typeref(condition_type_orig);
3994 if(condition_type->kind != TYPE_ERROR && !is_type_scalar(condition_type)) {
3995 type_error("expected a scalar type in conditional condition",
3996 expression->base.source_position, condition_type_orig);
4000 expression_t *true_expression = parse_expression();
4002 expression_t *false_expression = parse_sub_expression(precedence);
4004 conditional->true_expression = true_expression;
4005 conditional->false_expression = false_expression;
4007 type_t *orig_true_type = true_expression->base.datatype;
4008 type_t *orig_false_type = false_expression->base.datatype;
4009 if(!is_type_valid(orig_true_type) || !is_type_valid(orig_false_type))
4012 type_t *true_type = skip_typeref(orig_true_type);
4013 type_t *false_type = skip_typeref(orig_false_type);
4016 type_t *result_type = NULL;
4017 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4018 result_type = semantic_arithmetic(true_type, false_type);
4020 true_expression = create_implicit_cast(true_expression, result_type);
4021 false_expression = create_implicit_cast(false_expression, result_type);
4023 conditional->true_expression = true_expression;
4024 conditional->false_expression = false_expression;
4025 conditional->expression.datatype = result_type;
4026 } else if (same_compound_type(true_type, false_type)
4027 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4028 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
4029 /* just take 1 of the 2 types */
4030 result_type = true_type;
4031 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4032 && pointers_compatible(true_type, false_type)) {
4034 result_type = true_type;
4037 type_error_incompatible("while parsing conditional",
4038 expression->base.source_position, true_type,
4042 conditional->expression.datatype = result_type;
4047 * Parse an extension expression.
4049 static expression_t *parse_extension(unsigned precedence)
4051 eat(T___extension__);
4053 /* TODO enable extensions */
4054 expression_t *expression = parse_sub_expression(precedence);
4055 /* TODO disable extensions */
4059 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4061 eat(T___builtin_classify_type);
4063 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4064 result->base.datatype = type_int;
4067 expression_t *expression = parse_sub_expression(precedence);
4069 result->classify_type.type_expression = expression;
4074 static void semantic_incdec(unary_expression_t *expression)
4076 type_t *orig_type = expression->value->base.datatype;
4077 if(!is_type_valid(orig_type))
4080 type_t *type = skip_typeref(orig_type);
4081 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4082 /* TODO: improve error message */
4083 errorf(HERE, "operation needs an arithmetic or pointer type");
4087 expression->expression.datatype = orig_type;
4090 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4092 type_t *orig_type = expression->value->base.datatype;
4093 if(!is_type_valid(orig_type))
4096 type_t *type = skip_typeref(orig_type);
4097 if(!is_type_arithmetic(type)) {
4098 /* TODO: improve error message */
4099 errorf(HERE, "operation needs an arithmetic type");
4103 expression->expression.datatype = orig_type;
4106 static void semantic_unexpr_scalar(unary_expression_t *expression)
4108 type_t *orig_type = expression->value->base.datatype;
4109 if(!is_type_valid(orig_type))
4112 type_t *type = skip_typeref(orig_type);
4113 if (!is_type_scalar(type)) {
4114 errorf(HERE, "operand of ! must be of scalar type");
4118 expression->expression.datatype = orig_type;
4121 static void semantic_unexpr_integer(unary_expression_t *expression)
4123 type_t *orig_type = expression->value->base.datatype;
4124 if(!is_type_valid(orig_type))
4127 type_t *type = skip_typeref(orig_type);
4128 if (!is_type_integer(type)) {
4129 errorf(HERE, "operand of ~ must be of integer type");
4133 expression->expression.datatype = orig_type;
4136 static void semantic_dereference(unary_expression_t *expression)
4138 type_t *orig_type = expression->value->base.datatype;
4139 if(!is_type_valid(orig_type))
4142 type_t *type = skip_typeref(orig_type);
4143 if(!is_type_pointer(type)) {
4144 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4148 pointer_type_t *pointer_type = &type->pointer;
4149 type_t *result_type = pointer_type->points_to;
4151 result_type = automatic_type_conversion(result_type);
4152 expression->expression.datatype = result_type;
4156 * Check the semantic of the address taken expression.
4158 static void semantic_take_addr(unary_expression_t *expression)
4160 expression_t *value = expression->value;
4161 value->base.datatype = revert_automatic_type_conversion(value);
4163 type_t *orig_type = value->base.datatype;
4164 if(!is_type_valid(orig_type))
4167 if(value->kind == EXPR_REFERENCE) {
4168 reference_expression_t *reference = (reference_expression_t*) value;
4169 declaration_t *declaration = reference->declaration;
4170 if(declaration != NULL) {
4171 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4172 errorf(expression->expression.source_position,
4173 "address of register variable '%Y' requested",
4174 declaration->symbol);
4176 declaration->address_taken = 1;
4180 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4183 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4184 static expression_t *parse_##unexpression_type(unsigned precedence) \
4188 expression_t *unary_expression \
4189 = allocate_expression_zero(unexpression_type); \
4190 unary_expression->base.source_position = HERE; \
4191 unary_expression->unary.value = parse_sub_expression(precedence); \
4193 sfunc(&unary_expression->unary); \
4195 return unary_expression; \
4198 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4199 semantic_unexpr_arithmetic)
4200 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4201 semantic_unexpr_arithmetic)
4202 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4203 semantic_unexpr_scalar)
4204 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4205 semantic_dereference)
4206 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4208 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4209 semantic_unexpr_integer)
4210 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4212 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4215 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4217 static expression_t *parse_##unexpression_type(unsigned precedence, \
4218 expression_t *left) \
4220 (void) precedence; \
4223 expression_t *unary_expression \
4224 = allocate_expression_zero(unexpression_type); \
4225 unary_expression->unary.value = left; \
4227 sfunc(&unary_expression->unary); \
4229 return unary_expression; \
4232 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4233 EXPR_UNARY_POSTFIX_INCREMENT,
4235 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4236 EXPR_UNARY_POSTFIX_DECREMENT,
4239 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4241 /* TODO: handle complex + imaginary types */
4243 /* § 6.3.1.8 Usual arithmetic conversions */
4244 if(type_left == type_long_double || type_right == type_long_double) {
4245 return type_long_double;
4246 } else if(type_left == type_double || type_right == type_double) {
4248 } else if(type_left == type_float || type_right == type_float) {
4252 type_right = promote_integer(type_right);
4253 type_left = promote_integer(type_left);
4255 if(type_left == type_right)
4258 bool signed_left = is_type_signed(type_left);
4259 bool signed_right = is_type_signed(type_right);
4260 int rank_left = get_rank(type_left);
4261 int rank_right = get_rank(type_right);
4262 if(rank_left < rank_right) {
4263 if(signed_left == signed_right || !signed_right) {
4269 if(signed_left == signed_right || !signed_left) {
4278 * Check the semantic restrictions for a binary expression.
4280 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4282 expression_t *left = expression->left;
4283 expression_t *right = expression->right;
4284 type_t *orig_type_left = left->base.datatype;
4285 type_t *orig_type_right = right->base.datatype;
4287 if(orig_type_left == NULL || orig_type_right == NULL)
4290 type_t *type_left = skip_typeref(orig_type_left);
4291 type_t *type_right = skip_typeref(orig_type_right);
4293 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4294 /* TODO: improve error message */
4295 errorf(HERE, "operation needs arithmetic types");
4299 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4300 expression->left = create_implicit_cast(left, arithmetic_type);
4301 expression->right = create_implicit_cast(right, arithmetic_type);
4302 expression->expression.datatype = arithmetic_type;
4305 static void semantic_shift_op(binary_expression_t *expression)
4307 expression_t *left = expression->left;
4308 expression_t *right = expression->right;
4309 type_t *orig_type_left = left->base.datatype;
4310 type_t *orig_type_right = right->base.datatype;
4312 if(orig_type_left == NULL || orig_type_right == NULL)
4315 type_t *type_left = skip_typeref(orig_type_left);
4316 type_t *type_right = skip_typeref(orig_type_right);
4318 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4319 /* TODO: improve error message */
4320 errorf(HERE, "operation needs integer types");
4324 type_left = promote_integer(type_left);
4325 type_right = promote_integer(type_right);
4327 expression->left = create_implicit_cast(left, type_left);
4328 expression->right = create_implicit_cast(right, type_right);
4329 expression->expression.datatype = type_left;
4332 static void semantic_add(binary_expression_t *expression)
4334 expression_t *left = expression->left;
4335 expression_t *right = expression->right;
4336 type_t *orig_type_left = left->base.datatype;
4337 type_t *orig_type_right = right->base.datatype;
4339 if(orig_type_left == NULL || orig_type_right == NULL)
4342 type_t *type_left = skip_typeref(orig_type_left);
4343 type_t *type_right = skip_typeref(orig_type_right);
4346 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4347 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4348 expression->left = create_implicit_cast(left, arithmetic_type);
4349 expression->right = create_implicit_cast(right, arithmetic_type);
4350 expression->expression.datatype = arithmetic_type;
4352 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4353 expression->expression.datatype = type_left;
4354 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4355 expression->expression.datatype = type_right;
4357 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4361 static void semantic_sub(binary_expression_t *expression)
4363 expression_t *left = expression->left;
4364 expression_t *right = expression->right;
4365 type_t *orig_type_left = left->base.datatype;
4366 type_t *orig_type_right = right->base.datatype;
4368 if(orig_type_left == NULL || orig_type_right == NULL)
4371 type_t *type_left = skip_typeref(orig_type_left);
4372 type_t *type_right = skip_typeref(orig_type_right);
4375 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4376 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4377 expression->left = create_implicit_cast(left, arithmetic_type);
4378 expression->right = create_implicit_cast(right, arithmetic_type);
4379 expression->expression.datatype = arithmetic_type;
4381 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4382 expression->expression.datatype = type_left;
4383 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4384 if(!pointers_compatible(type_left, type_right)) {
4385 errorf(HERE, "pointers to incompatible objects to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4387 expression->expression.datatype = type_ptrdiff_t;
4390 errorf(HERE, "invalid operands to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4394 static void semantic_comparison(binary_expression_t *expression)
4396 expression_t *left = expression->left;
4397 expression_t *right = expression->right;
4398 type_t *orig_type_left = left->base.datatype;
4399 type_t *orig_type_right = right->base.datatype;
4401 if(orig_type_left == NULL || orig_type_right == NULL)
4404 type_t *type_left = skip_typeref(orig_type_left);
4405 type_t *type_right = skip_typeref(orig_type_right);
4407 /* TODO non-arithmetic types */
4408 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4409 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4410 expression->left = create_implicit_cast(left, arithmetic_type);
4411 expression->right = create_implicit_cast(right, arithmetic_type);
4412 expression->expression.datatype = arithmetic_type;
4413 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4414 /* TODO check compatibility */
4415 } else if (is_type_pointer(type_left)) {
4416 expression->right = create_implicit_cast(right, type_left);
4417 } else if (is_type_pointer(type_right)) {
4418 expression->left = create_implicit_cast(left, type_right);
4420 type_error_incompatible("invalid operands in comparison",
4421 token.source_position, type_left, type_right);
4423 expression->expression.datatype = type_int;
4426 static void semantic_arithmetic_assign(binary_expression_t *expression)
4428 expression_t *left = expression->left;
4429 expression_t *right = expression->right;
4430 type_t *orig_type_left = left->base.datatype;
4431 type_t *orig_type_right = right->base.datatype;
4433 if(orig_type_left == NULL || orig_type_right == NULL)
4436 type_t *type_left = skip_typeref(orig_type_left);
4437 type_t *type_right = skip_typeref(orig_type_right);
4439 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4440 /* TODO: improve error message */
4441 errorf(HERE, "operation needs arithmetic types");
4445 /* combined instructions are tricky. We can't create an implicit cast on
4446 * the left side, because we need the uncasted form for the store.
4447 * The ast2firm pass has to know that left_type must be right_type
4448 * for the arithmetic operation and create a cast by itself */
4449 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4450 expression->right = create_implicit_cast(right, arithmetic_type);
4451 expression->expression.datatype = type_left;
4454 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4456 expression_t *left = expression->left;
4457 expression_t *right = expression->right;
4458 type_t *orig_type_left = left->base.datatype;
4459 type_t *orig_type_right = right->base.datatype;
4461 if(orig_type_left == NULL || orig_type_right == NULL)
4464 type_t *type_left = skip_typeref(orig_type_left);
4465 type_t *type_right = skip_typeref(orig_type_right);
4467 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4468 /* combined instructions are tricky. We can't create an implicit cast on
4469 * the left side, because we need the uncasted form for the store.
4470 * The ast2firm pass has to know that left_type must be right_type
4471 * for the arithmetic operation and create a cast by itself */
4472 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4473 expression->right = create_implicit_cast(right, arithmetic_type);
4474 expression->expression.datatype = type_left;
4475 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4476 expression->expression.datatype = type_left;
4478 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4484 * Check the semantic restrictions of a logical expression.
4486 static void semantic_logical_op(binary_expression_t *expression)
4488 expression_t *left = expression->left;
4489 expression_t *right = expression->right;
4490 type_t *orig_type_left = left->base.datatype;
4491 type_t *orig_type_right = right->base.datatype;
4493 if(orig_type_left == NULL || orig_type_right == NULL)
4496 type_t *type_left = skip_typeref(orig_type_left);
4497 type_t *type_right = skip_typeref(orig_type_right);
4499 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4500 /* TODO: improve error message */
4501 errorf(HERE, "operation needs scalar types");
4505 expression->expression.datatype = type_int;
4509 * Checks if a compound type has constant fields.
4511 static bool has_const_fields(const compound_type_t *type)
4513 const context_t *context = &type->declaration->context;
4514 const declaration_t *declaration = context->declarations;
4516 for (; declaration != NULL; declaration = declaration->next) {
4517 if (declaration->namespc != NAMESPACE_NORMAL)
4520 const type_t *decl_type = skip_typeref(declaration->type);
4521 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4529 * Check the semantic restrictions of a binary assign expression.
4531 static void semantic_binexpr_assign(binary_expression_t *expression)
4533 expression_t *left = expression->left;
4534 type_t *orig_type_left = left->base.datatype;
4536 if(orig_type_left == NULL)
4539 type_t *type_left = revert_automatic_type_conversion(left);
4540 type_left = skip_typeref(orig_type_left);
4542 /* must be a modifiable lvalue */
4543 if (is_type_array(type_left)) {
4544 errorf(HERE, "cannot assign to arrays ('%E')", left);
4547 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4548 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4552 if(is_type_incomplete(type_left)) {
4554 "left-hand side of assignment '%E' has incomplete type '%T'",
4555 left, orig_type_left);
4558 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4559 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4560 left, orig_type_left);
4564 semantic_assign(orig_type_left, &expression->right, "assignment");
4566 expression->expression.datatype = orig_type_left;
4569 static void semantic_comma(binary_expression_t *expression)
4571 expression->expression.datatype = expression->right->base.datatype;
4574 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4575 static expression_t *parse_##binexpression_type(unsigned precedence, \
4576 expression_t *left) \
4580 expression_t *right = parse_sub_expression(precedence + lr); \
4582 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4583 binexpr->binary.left = left; \
4584 binexpr->binary.right = right; \
4585 sfunc(&binexpr->binary); \
4590 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4591 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4592 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4593 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4594 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4595 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4596 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4597 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4598 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4600 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4601 semantic_comparison, 1)
4602 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4603 semantic_comparison, 1)
4604 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4605 semantic_comparison, 1)
4606 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4607 semantic_comparison, 1)
4609 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4610 semantic_binexpr_arithmetic, 1)
4611 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4612 semantic_binexpr_arithmetic, 1)
4613 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4614 semantic_binexpr_arithmetic, 1)
4615 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4616 semantic_logical_op, 1)
4617 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4618 semantic_logical_op, 1)
4619 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4620 semantic_shift_op, 1)
4621 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4622 semantic_shift_op, 1)
4623 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4624 semantic_arithmetic_addsubb_assign, 0)
4625 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4626 semantic_arithmetic_addsubb_assign, 0)
4627 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4628 semantic_arithmetic_assign, 0)
4629 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4630 semantic_arithmetic_assign, 0)
4631 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4632 semantic_arithmetic_assign, 0)
4633 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4634 semantic_arithmetic_assign, 0)
4635 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4636 semantic_arithmetic_assign, 0)
4637 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4638 semantic_arithmetic_assign, 0)
4639 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4640 semantic_arithmetic_assign, 0)
4641 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4642 semantic_arithmetic_assign, 0)
4644 static expression_t *parse_sub_expression(unsigned precedence)
4646 if(token.type < 0) {
4647 return expected_expression_error();
4650 expression_parser_function_t *parser
4651 = &expression_parsers[token.type];
4652 source_position_t source_position = token.source_position;
4655 if(parser->parser != NULL) {
4656 left = parser->parser(parser->precedence);
4658 left = parse_primary_expression();
4660 assert(left != NULL);
4661 left->base.source_position = source_position;
4664 if(token.type < 0) {
4665 return expected_expression_error();
4668 parser = &expression_parsers[token.type];
4669 if(parser->infix_parser == NULL)
4671 if(parser->infix_precedence < precedence)
4674 left = parser->infix_parser(parser->infix_precedence, left);
4676 assert(left != NULL);
4677 assert(left->kind != EXPR_UNKNOWN);
4678 left->base.source_position = source_position;
4685 * Parse an expression.
4687 static expression_t *parse_expression(void)
4689 return parse_sub_expression(1);
4693 * Register a parser for a prefix-like operator with given precedence.
4695 * @param parser the parser function
4696 * @param token_type the token type of the prefix token
4697 * @param precedence the precedence of the operator
4699 static void register_expression_parser(parse_expression_function parser,
4700 int token_type, unsigned precedence)
4702 expression_parser_function_t *entry = &expression_parsers[token_type];
4704 if(entry->parser != NULL) {
4705 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4706 panic("trying to register multiple expression parsers for a token");
4708 entry->parser = parser;
4709 entry->precedence = precedence;
4713 * Register a parser for an infix operator with given precedence.
4715 * @param parser the parser function
4716 * @param token_type the token type of the infix operator
4717 * @param precedence the precedence of the operator
4719 static void register_infix_parser(parse_expression_infix_function parser,
4720 int token_type, unsigned precedence)
4722 expression_parser_function_t *entry = &expression_parsers[token_type];
4724 if(entry->infix_parser != NULL) {
4725 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4726 panic("trying to register multiple infix expression parsers for a "
4729 entry->infix_parser = parser;
4730 entry->infix_precedence = precedence;
4734 * Initialize the expression parsers.
4736 static void init_expression_parsers(void)
4738 memset(&expression_parsers, 0, sizeof(expression_parsers));
4740 register_infix_parser(parse_array_expression, '[', 30);
4741 register_infix_parser(parse_call_expression, '(', 30);
4742 register_infix_parser(parse_select_expression, '.', 30);
4743 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4744 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4746 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4749 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4750 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4751 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4752 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4753 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4754 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4755 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4756 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4757 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4758 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4759 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4760 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4761 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4762 T_EXCLAMATIONMARKEQUAL, 13);
4763 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4764 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4765 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4766 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4767 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4768 register_infix_parser(parse_conditional_expression, '?', 7);
4769 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4770 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4771 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4772 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4773 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4774 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4775 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4776 T_LESSLESSEQUAL, 2);
4777 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4778 T_GREATERGREATEREQUAL, 2);
4779 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4781 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4783 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4786 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4788 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4789 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4790 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4791 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4792 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4793 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4794 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4796 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4798 register_expression_parser(parse_sizeof, T_sizeof, 25);
4799 register_expression_parser(parse_extension, T___extension__, 25);
4800 register_expression_parser(parse_builtin_classify_type,
4801 T___builtin_classify_type, 25);
4805 * Parse a asm statement constraints specification.
4807 static asm_constraint_t *parse_asm_constraints(void)
4809 asm_constraint_t *result = NULL;
4810 asm_constraint_t *last = NULL;
4812 while(token.type == T_STRING_LITERAL || token.type == '[') {
4813 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4814 memset(constraint, 0, sizeof(constraint[0]));
4816 if(token.type == '[') {
4818 if(token.type != T_IDENTIFIER) {
4819 parse_error_expected("while parsing asm constraint",
4823 constraint->symbol = token.v.symbol;
4828 constraint->constraints = parse_string_literals();
4830 constraint->expression = parse_expression();
4834 last->next = constraint;
4836 result = constraint;
4840 if(token.type != ',')
4849 * Parse a asm statement clobber specification.
4851 static asm_clobber_t *parse_asm_clobbers(void)
4853 asm_clobber_t *result = NULL;
4854 asm_clobber_t *last = NULL;
4856 while(token.type == T_STRING_LITERAL) {
4857 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4858 clobber->clobber = parse_string_literals();
4861 last->next = clobber;
4867 if(token.type != ',')
4876 * Parse an asm statement.
4878 static statement_t *parse_asm_statement(void)
4882 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4883 statement->base.source_position = token.source_position;
4885 asm_statement_t *asm_statement = &statement->asms;
4887 if(token.type == T_volatile) {
4889 asm_statement->is_volatile = true;
4893 asm_statement->asm_text = parse_string_literals();
4895 if(token.type != ':')
4899 asm_statement->inputs = parse_asm_constraints();
4900 if(token.type != ':')
4904 asm_statement->outputs = parse_asm_constraints();
4905 if(token.type != ':')
4909 asm_statement->clobbers = parse_asm_clobbers();
4918 * Parse a case statement.
4920 static statement_t *parse_case_statement(void)
4924 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4926 statement->base.source_position = token.source_position;
4927 statement->case_label.expression = parse_expression();
4931 if (! is_constant_expression(statement->case_label.expression)) {
4932 errorf(statement->base.source_position,
4933 "case label does not reduce to an integer constant");
4935 /* TODO: check if the case label is already known */
4936 if (current_switch != NULL) {
4937 /* link all cases into the switch statement */
4938 if (current_switch->last_case == NULL) {
4939 current_switch->first_case =
4940 current_switch->last_case = &statement->case_label;
4942 current_switch->last_case->next = &statement->case_label;
4945 errorf(statement->base.source_position,
4946 "case label not within a switch statement");
4949 statement->case_label.label_statement = parse_statement();
4955 * Finds an existing default label of a switch statement.
4957 static case_label_statement_t *
4958 find_default_label(const switch_statement_t *statement)
4960 for (case_label_statement_t *label = statement->first_case;
4962 label = label->next) {
4963 if (label->expression == NULL)
4970 * Parse a default statement.
4972 static statement_t *parse_default_statement(void)
4976 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4978 statement->base.source_position = token.source_position;
4981 if (current_switch != NULL) {
4982 const case_label_statement_t *def_label = find_default_label(current_switch);
4983 if (def_label != NULL) {
4984 errorf(HERE, "multiple default labels in one switch");
4985 errorf(def_label->statement.source_position,
4986 "this is the first default label");
4988 /* link all cases into the switch statement */
4989 if (current_switch->last_case == NULL) {
4990 current_switch->first_case =
4991 current_switch->last_case = &statement->case_label;
4993 current_switch->last_case->next = &statement->case_label;
4997 errorf(statement->base.source_position,
4998 "'default' label not within a switch statement");
5000 statement->label.label_statement = parse_statement();
5006 * Return the declaration for a given label symbol or create a new one.
5008 static declaration_t *get_label(symbol_t *symbol)
5010 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5011 assert(current_function != NULL);
5012 /* if we found a label in the same function, then we already created the
5014 if(candidate != NULL
5015 && candidate->parent_context == ¤t_function->context) {
5019 /* otherwise we need to create a new one */
5020 declaration_t *const declaration = allocate_declaration_zero();
5021 declaration->namespc = NAMESPACE_LABEL;
5022 declaration->symbol = symbol;
5024 label_push(declaration);
5030 * Parse a label statement.
5032 static statement_t *parse_label_statement(void)
5034 assert(token.type == T_IDENTIFIER);
5035 symbol_t *symbol = token.v.symbol;
5038 declaration_t *label = get_label(symbol);
5040 /* if source position is already set then the label is defined twice,
5041 * otherwise it was just mentioned in a goto so far */
5042 if(label->source_position.input_name != NULL) {
5043 errorf(HERE, "duplicate label '%Y'", symbol);
5044 errorf(label->source_position, "previous definition of '%Y' was here",
5047 label->source_position = token.source_position;
5050 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
5052 label_statement->statement.kind = STATEMENT_LABEL;
5053 label_statement->statement.source_position = token.source_position;
5054 label_statement->label = label;
5058 if(token.type == '}') {
5059 /* TODO only warn? */
5060 errorf(HERE, "label at end of compound statement");
5061 return (statement_t*) label_statement;
5063 label_statement->label_statement = parse_statement();
5066 return (statement_t*) label_statement;
5070 * Parse an if statement.
5072 static statement_t *parse_if(void)
5076 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5077 statement->statement.kind = STATEMENT_IF;
5078 statement->statement.source_position = token.source_position;
5081 statement->condition = parse_expression();
5084 statement->true_statement = parse_statement();
5085 if(token.type == T_else) {
5087 statement->false_statement = parse_statement();
5090 return (statement_t*) statement;
5094 * Parse a switch statement.
5096 static statement_t *parse_switch(void)
5100 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5101 statement->statement.kind = STATEMENT_SWITCH;
5102 statement->statement.source_position = token.source_position;
5105 expression_t *const expr = parse_expression();
5106 type_t *const type = promote_integer(skip_typeref(expr->base.datatype));
5107 statement->expression = create_implicit_cast(expr, type);
5110 switch_statement_t *rem = current_switch;
5111 current_switch = statement;
5112 statement->body = parse_statement();
5113 current_switch = rem;
5115 return (statement_t*) statement;
5118 static statement_t *parse_loop_body(statement_t *const loop)
5120 statement_t *const rem = current_loop;
5121 current_loop = loop;
5122 statement_t *const body = parse_statement();
5128 * Parse a while statement.
5130 static statement_t *parse_while(void)
5134 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5135 statement->statement.kind = STATEMENT_WHILE;
5136 statement->statement.source_position = token.source_position;
5139 statement->condition = parse_expression();
5142 statement->body = parse_loop_body((statement_t*)statement);
5144 return (statement_t*) statement;
5148 * Parse a do statement.
5150 static statement_t *parse_do(void)
5154 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5155 statement->statement.kind = STATEMENT_DO_WHILE;
5156 statement->statement.source_position = token.source_position;
5158 statement->body = parse_loop_body((statement_t*)statement);
5161 statement->condition = parse_expression();
5165 return (statement_t*) statement;
5169 * Parse a for statement.
5171 static statement_t *parse_for(void)
5175 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5176 statement->statement.kind = STATEMENT_FOR;
5177 statement->statement.source_position = token.source_position;
5181 int top = environment_top();
5182 context_t *last_context = context;
5183 set_context(&statement->context);
5185 if(token.type != ';') {
5186 if(is_declaration_specifier(&token, false)) {
5187 parse_declaration(record_declaration);
5189 statement->initialisation = parse_expression();
5196 if(token.type != ';') {
5197 statement->condition = parse_expression();
5200 if(token.type != ')') {
5201 statement->step = parse_expression();
5204 statement->body = parse_loop_body((statement_t*)statement);
5206 assert(context == &statement->context);
5207 set_context(last_context);
5208 environment_pop_to(top);
5210 return (statement_t*) statement;
5214 * Parse a goto statement.
5216 static statement_t *parse_goto(void)
5220 if(token.type != T_IDENTIFIER) {
5221 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5225 symbol_t *symbol = token.v.symbol;
5228 declaration_t *label = get_label(symbol);
5230 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5232 statement->statement.kind = STATEMENT_GOTO;
5233 statement->statement.source_position = token.source_position;
5235 statement->label = label;
5237 /* remember the goto's in a list for later checking */
5238 if (goto_last == NULL) {
5239 goto_first = goto_last = statement;
5241 goto_last->next = statement;
5246 return (statement_t*) statement;
5250 * Parse a continue statement.
5252 static statement_t *parse_continue(void)
5254 statement_t *statement;
5255 if (current_loop == NULL) {
5256 errorf(HERE, "continue statement not within loop");
5259 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5261 statement->base.source_position = token.source_position;
5271 * Parse a break statement.
5273 static statement_t *parse_break(void)
5275 statement_t *statement;
5276 if (current_switch == NULL && current_loop == NULL) {
5277 errorf(HERE, "break statement not within loop or switch");
5280 statement = allocate_statement_zero(STATEMENT_BREAK);
5282 statement->base.source_position = token.source_position;
5292 * Check if a given declaration represents a local variable.
5294 static bool is_local_var_declaration(const declaration_t *declaration) {
5295 switch ((storage_class_tag_t) declaration->storage_class) {
5296 case STORAGE_CLASS_NONE:
5297 case STORAGE_CLASS_AUTO:
5298 case STORAGE_CLASS_REGISTER: {
5299 const type_t *type = skip_typeref(declaration->type);
5300 if(is_type_function(type)) {
5312 * Check if a given expression represents a local variable.
5314 static bool is_local_variable(const expression_t *expression)
5316 if (expression->base.kind != EXPR_REFERENCE) {
5319 const declaration_t *declaration = expression->reference.declaration;
5320 return is_local_var_declaration(declaration);
5324 * Parse a return statement.
5326 static statement_t *parse_return(void)
5330 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5332 statement->statement.kind = STATEMENT_RETURN;
5333 statement->statement.source_position = token.source_position;
5335 assert(is_type_function(current_function->type));
5336 function_type_t *function_type = ¤t_function->type->function;
5337 type_t *return_type = function_type->return_type;
5339 expression_t *return_value = NULL;
5340 if(token.type != ';') {
5341 return_value = parse_expression();
5345 if(return_type == NULL)
5346 return (statement_t*) statement;
5347 if(return_value != NULL && return_value->base.datatype == NULL)
5348 return (statement_t*) statement;
5350 return_type = skip_typeref(return_type);
5352 if(return_value != NULL) {
5353 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5355 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5356 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5357 warningf(statement->statement.source_position,
5358 "'return' with a value, in function returning void");
5359 return_value = NULL;
5361 if(is_type_valid(return_type)) {
5362 semantic_assign(return_type, &return_value, "'return'");
5365 /* check for returning address of a local var */
5366 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5367 const expression_t *expression = return_value->unary.value;
5368 if (is_local_variable(expression)) {
5369 warningf(statement->statement.source_position,
5370 "function returns address of local variable");
5374 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5375 warningf(statement->statement.source_position,
5376 "'return' without value, in function returning non-void");
5379 statement->return_value = return_value;
5381 return (statement_t*) statement;
5385 * Parse a declaration statement.
5387 static statement_t *parse_declaration_statement(void)
5389 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5391 statement->base.source_position = token.source_position;
5393 declaration_t *before = last_declaration;
5394 parse_declaration(record_declaration);
5396 if(before == NULL) {
5397 statement->declaration.declarations_begin = context->declarations;
5399 statement->declaration.declarations_begin = before->next;
5401 statement->declaration.declarations_end = last_declaration;
5407 * Parse an expression statement, ie. expr ';'.
5409 static statement_t *parse_expression_statement(void)
5411 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5413 statement->base.source_position = token.source_position;
5414 statement->expression.expression = parse_expression();
5422 * Parse a statement.
5424 static statement_t *parse_statement(void)
5426 statement_t *statement = NULL;
5428 /* declaration or statement */
5429 switch(token.type) {
5431 statement = parse_asm_statement();
5435 statement = parse_case_statement();
5439 statement = parse_default_statement();
5443 statement = parse_compound_statement();
5447 statement = parse_if();
5451 statement = parse_switch();
5455 statement = parse_while();
5459 statement = parse_do();
5463 statement = parse_for();
5467 statement = parse_goto();
5471 statement = parse_continue();
5475 statement = parse_break();
5479 statement = parse_return();
5488 if(look_ahead(1)->type == ':') {
5489 statement = parse_label_statement();
5493 if(is_typedef_symbol(token.v.symbol)) {
5494 statement = parse_declaration_statement();
5498 statement = parse_expression_statement();
5501 case T___extension__:
5502 /* this can be a prefix to a declaration or an expression statement */
5503 /* we simply eat it now and parse the rest with tail recursion */
5506 } while(token.type == T___extension__);
5507 statement = parse_statement();
5511 statement = parse_declaration_statement();
5515 statement = parse_expression_statement();
5519 assert(statement == NULL
5520 || statement->base.source_position.input_name != NULL);
5526 * Parse a compound statement.
5528 static statement_t *parse_compound_statement(void)
5530 compound_statement_t *compound_statement
5531 = allocate_ast_zero(sizeof(compound_statement[0]));
5532 compound_statement->statement.kind = STATEMENT_COMPOUND;
5533 compound_statement->statement.source_position = token.source_position;
5537 int top = environment_top();
5538 context_t *last_context = context;
5539 set_context(&compound_statement->context);
5541 statement_t *last_statement = NULL;
5543 while(token.type != '}' && token.type != T_EOF) {
5544 statement_t *statement = parse_statement();
5545 if(statement == NULL)
5548 if(last_statement != NULL) {
5549 last_statement->base.next = statement;
5551 compound_statement->statements = statement;
5554 while(statement->base.next != NULL)
5555 statement = statement->base.next;
5557 last_statement = statement;
5560 if(token.type == '}') {
5563 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5566 assert(context == &compound_statement->context);
5567 set_context(last_context);
5568 environment_pop_to(top);
5570 return (statement_t*) compound_statement;
5574 * Initialize builtin types.
5576 static void initialize_builtin_types(void)
5578 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5579 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5580 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5581 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5582 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5583 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5584 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5585 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5587 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5588 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5589 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5590 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5594 * Parse a translation unit.
5596 static translation_unit_t *parse_translation_unit(void)
5598 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5600 assert(global_context == NULL);
5601 global_context = &unit->context;
5603 assert(context == NULL);
5604 set_context(&unit->context);
5606 initialize_builtin_types();
5608 while(token.type != T_EOF) {
5609 if (token.type == ';') {
5610 /* TODO error in strict mode */
5611 warningf(HERE, "stray ';' outside of function");
5614 parse_external_declaration();
5618 assert(context == &unit->context);
5620 last_declaration = NULL;
5622 assert(global_context == &unit->context);
5623 global_context = NULL;
5631 * @return the translation unit or NULL if errors occurred.
5633 translation_unit_t *parse(void)
5635 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5636 label_stack = NEW_ARR_F(stack_entry_t, 0);
5637 diagnostic_count = 0;
5641 type_set_output(stderr);
5642 ast_set_output(stderr);
5644 lookahead_bufpos = 0;
5645 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5648 translation_unit_t *unit = parse_translation_unit();
5650 DEL_ARR_F(environment_stack);
5651 DEL_ARR_F(label_stack);
5660 * Initialize the parser.
5662 void init_parser(void)
5664 init_expression_parsers();
5665 obstack_init(&temp_obst);
5667 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5668 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5672 * Terminate the parser.
5674 void exit_parser(void)
5676 obstack_free(&temp_obst, NULL);