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_list = NULL;
54 static struct obstack temp_obst;
56 /** The current source position. */
57 #define HERE token.source_position
59 static type_t *type_valist;
61 static statement_t *parse_compound_statement(void);
62 static statement_t *parse_statement(void);
64 static expression_t *parse_sub_expression(unsigned precedence);
65 static expression_t *parse_expression(void);
66 static type_t *parse_typename(void);
68 static void parse_compound_type_entries(void);
69 static declaration_t *parse_declarator(
70 const declaration_specifiers_t *specifiers, bool may_be_abstract);
71 static declaration_t *record_declaration(declaration_t *declaration);
73 static void semantic_comparison(binary_expression_t *expression);
75 #define STORAGE_CLASSES \
82 #define TYPE_QUALIFIERS \
89 #ifdef PROVIDE_COMPLEX
90 #define COMPLEX_SPECIFIERS \
92 #define IMAGINARY_SPECIFIERS \
95 #define COMPLEX_SPECIFIERS
96 #define IMAGINARY_SPECIFIERS
99 #define TYPE_SPECIFIERS \
114 case T___builtin_va_list: \
118 #define DECLARATION_START \
123 #define TYPENAME_START \
128 * Allocate an AST node with given size and
129 * initialize all fields with zero.
131 static void *allocate_ast_zero(size_t size)
133 void *res = allocate_ast(size);
134 memset(res, 0, size);
139 * Returns the size of a statement node.
141 * @param kind the statement kind
143 static size_t get_statement_struct_size(statement_kind_t kind)
145 static const size_t sizes[] = {
146 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
147 [STATEMENT_RETURN] = sizeof(return_statement_t),
148 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
149 [STATEMENT_IF] = sizeof(if_statement_t),
150 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
151 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
152 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
153 [STATEMENT_BREAK] = sizeof(statement_base_t),
154 [STATEMENT_GOTO] = sizeof(goto_statement_t),
155 [STATEMENT_LABEL] = sizeof(label_statement_t),
156 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
157 [STATEMENT_WHILE] = sizeof(while_statement_t),
158 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
159 [STATEMENT_FOR] = sizeof(for_statement_t),
160 [STATEMENT_ASM] = sizeof(asm_statement_t)
162 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
163 assert(sizes[kind] != 0);
168 * Allocate a statement node of given kind and initialize all
171 static statement_t *allocate_statement_zero(statement_kind_t kind)
173 size_t size = get_statement_struct_size(kind);
174 statement_t *res = allocate_ast_zero(size);
176 res->base.kind = kind;
181 * Returns the size of an expression node.
183 * @param kind the expression kind
185 static size_t get_expression_struct_size(expression_kind_t kind)
187 static const size_t sizes[] = {
188 [EXPR_INVALID] = sizeof(expression_base_t),
189 [EXPR_REFERENCE] = sizeof(reference_expression_t),
190 [EXPR_CONST] = sizeof(const_expression_t),
191 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
192 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
193 [EXPR_CALL] = sizeof(call_expression_t),
194 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
195 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
196 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
197 [EXPR_SELECT] = sizeof(select_expression_t),
198 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
199 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
200 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
201 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
202 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
203 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
204 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
205 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
206 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
207 [EXPR_VA_START] = sizeof(va_start_expression_t),
208 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
209 [EXPR_STATEMENT] = sizeof(statement_expression_t),
211 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
212 return sizes[EXPR_UNARY_FIRST];
214 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
215 return sizes[EXPR_BINARY_FIRST];
217 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
218 assert(sizes[kind] != 0);
223 * Allocate an expression node of given kind and initialize all
226 static expression_t *allocate_expression_zero(expression_kind_t kind)
228 size_t size = get_expression_struct_size(kind);
229 expression_t *res = allocate_ast_zero(size);
231 res->base.kind = kind;
236 * Returns the size of a type node.
238 * @param kind the type kind
240 static size_t get_type_struct_size(type_kind_t kind)
242 static const size_t sizes[] = {
243 [TYPE_ATOMIC] = sizeof(atomic_type_t),
244 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
245 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
246 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
247 [TYPE_ENUM] = sizeof(enum_type_t),
248 [TYPE_FUNCTION] = sizeof(function_type_t),
249 [TYPE_POINTER] = sizeof(pointer_type_t),
250 [TYPE_ARRAY] = sizeof(array_type_t),
251 [TYPE_BUILTIN] = sizeof(builtin_type_t),
252 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
253 [TYPE_TYPEOF] = sizeof(typeof_type_t),
255 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
256 assert(kind <= TYPE_TYPEOF);
257 assert(sizes[kind] != 0);
262 * Allocate a type node of given kind and initialize all
265 static type_t *allocate_type_zero(type_kind_t kind)
267 size_t size = get_type_struct_size(kind);
268 type_t *res = obstack_alloc(type_obst, size);
269 memset(res, 0, size);
271 res->base.kind = kind;
276 * Returns the size of an initializer node.
278 * @param kind the initializer kind
280 static size_t get_initializer_size(initializer_kind_t kind)
282 static const size_t sizes[] = {
283 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
284 [INITIALIZER_STRING] = sizeof(initializer_string_t),
285 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
286 [INITIALIZER_LIST] = sizeof(initializer_list_t)
288 assert(kind < sizeof(sizes) / sizeof(*sizes));
289 assert(sizes[kind] != 0);
294 * Allocate an initializer node of given kind and initialize all
297 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
299 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
306 * Free a type from the type obstack.
308 static void free_type(void *type)
310 obstack_free(type_obst, type);
314 * Returns the index of the top element of the environment stack.
316 static size_t environment_top(void)
318 return ARR_LEN(environment_stack);
322 * Returns the index of the top element of the label stack.
324 static size_t label_top(void)
326 return ARR_LEN(label_stack);
331 * Return the next token.
333 static inline void next_token(void)
335 token = lookahead_buffer[lookahead_bufpos];
336 lookahead_buffer[lookahead_bufpos] = lexer_token;
339 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
342 print_token(stderr, &token);
343 fprintf(stderr, "\n");
348 * Return the next token with a given lookahead.
350 static inline const token_t *look_ahead(int num)
352 assert(num > 0 && num <= MAX_LOOKAHEAD);
353 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
354 return &lookahead_buffer[pos];
357 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
360 * Report a parse error because an expected token was not found.
362 static void parse_error_expected(const char *message, ...)
364 if(message != NULL) {
365 errorf(HERE, "%s", message);
368 va_start(ap, message);
369 errorf(HERE, "got '%K', expected %#k", &token, &ap, ", ");
374 * Report a type error.
376 static void type_error(const char *msg, const source_position_t source_position,
379 errorf(source_position, "%s, but found type '%T'", msg, type);
383 * Report an incompatible type.
385 static void type_error_incompatible(const char *msg,
386 const source_position_t source_position, type_t *type1, type_t *type2)
388 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
392 * Eat an complete block, ie. '{ ... }'.
394 static void eat_block(void)
396 if(token.type == '{')
399 while(token.type != '}') {
400 if(token.type == T_EOF)
402 if(token.type == '{') {
412 * Eat a statement until an ';' token.
414 static void eat_statement(void)
416 while(token.type != ';') {
417 if(token.type == T_EOF)
419 if(token.type == '}')
421 if(token.type == '{') {
431 * Eat a parenthesed term, ie. '( ... )'.
433 static void eat_paren(void)
435 if(token.type == '(')
438 while(token.type != ')') {
439 if(token.type == T_EOF)
441 if(token.type == ')' || token.type == ';' || token.type == '}') {
444 if(token.type == '(') {
448 if(token.type == '{') {
457 #define expect(expected) \
458 if(UNLIKELY(token.type != (expected))) { \
459 parse_error_expected(NULL, (expected), 0); \
465 #define expect_fail(expected) \
466 if(UNLIKELY(token.type != (expected))) { \
467 parse_error_expected(NULL, (expected), 0); \
473 #define expect_block(expected) \
474 if(UNLIKELY(token.type != (expected))) { \
475 parse_error_expected(NULL, (expected), 0); \
481 #define expect_void(expected) \
482 if(UNLIKELY(token.type != (expected))) { \
483 parse_error_expected(NULL, (expected), 0); \
489 static void set_context(context_t *new_context)
491 context = new_context;
493 last_declaration = new_context->declarations;
494 if(last_declaration != NULL) {
495 while(last_declaration->next != NULL) {
496 last_declaration = last_declaration->next;
502 * Search a symbol in a given namespace and returns its declaration or
503 * NULL if this symbol was not found.
505 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
507 declaration_t *declaration = symbol->declaration;
508 for( ; declaration != NULL; declaration = declaration->symbol_next) {
509 if(declaration->namespc == namespc)
517 * pushs an environment_entry on the environment stack and links the
518 * corresponding symbol to the new entry
520 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
522 symbol_t *symbol = declaration->symbol;
523 namespace_t namespc = (namespace_t)declaration->namespc;
525 /* remember old declaration */
527 entry.symbol = symbol;
528 entry.old_declaration = symbol->declaration;
529 entry.namespc = (unsigned short) namespc;
530 ARR_APP1(stack_entry_t, *stack_ptr, entry);
532 /* replace/add declaration into declaration list of the symbol */
533 if(symbol->declaration == NULL) {
534 symbol->declaration = declaration;
536 declaration_t *iter_last = NULL;
537 declaration_t *iter = symbol->declaration;
538 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
539 /* replace an entry? */
540 if(iter->namespc == namespc) {
541 if(iter_last == NULL) {
542 symbol->declaration = declaration;
544 iter_last->symbol_next = declaration;
546 declaration->symbol_next = iter->symbol_next;
551 assert(iter_last->symbol_next == NULL);
552 iter_last->symbol_next = declaration;
557 static void environment_push(declaration_t *declaration)
559 assert(declaration->source_position.input_name != NULL);
560 assert(declaration->parent_context != NULL);
561 stack_push(&environment_stack, declaration);
564 static void label_push(declaration_t *declaration)
566 declaration->parent_context = ¤t_function->context;
567 stack_push(&label_stack, declaration);
571 * pops symbols from the environment stack until @p new_top is the top element
573 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
575 stack_entry_t *stack = *stack_ptr;
576 size_t top = ARR_LEN(stack);
579 assert(new_top <= top);
583 for(i = top; i > new_top; --i) {
584 stack_entry_t *entry = &stack[i - 1];
586 declaration_t *old_declaration = entry->old_declaration;
587 symbol_t *symbol = entry->symbol;
588 namespace_t namespc = (namespace_t)entry->namespc;
590 /* replace/remove declaration */
591 declaration_t *declaration = symbol->declaration;
592 assert(declaration != NULL);
593 if(declaration->namespc == namespc) {
594 if(old_declaration == NULL) {
595 symbol->declaration = declaration->symbol_next;
597 symbol->declaration = old_declaration;
600 declaration_t *iter_last = declaration;
601 declaration_t *iter = declaration->symbol_next;
602 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
603 /* replace an entry? */
604 if(iter->namespc == namespc) {
605 assert(iter_last != NULL);
606 iter_last->symbol_next = old_declaration;
607 old_declaration->symbol_next = iter->symbol_next;
611 assert(iter != NULL);
615 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
618 static void environment_pop_to(size_t new_top)
620 stack_pop_to(&environment_stack, new_top);
623 static void label_pop_to(size_t new_top)
625 stack_pop_to(&label_stack, new_top);
629 static int get_rank(const type_t *type)
631 assert(!is_typeref(type));
632 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
633 * and esp. footnote 108). However we can't fold constants (yet), so we
634 * can't decide whether unsigned int is possible, while int always works.
635 * (unsigned int would be preferable when possible... for stuff like
636 * struct { enum { ... } bla : 4; } ) */
637 if(type->kind == TYPE_ENUM)
638 return ATOMIC_TYPE_INT;
640 assert(type->kind == TYPE_ATOMIC);
641 const atomic_type_t *atomic_type = &type->atomic;
642 atomic_type_kind_t atype = atomic_type->akind;
646 static type_t *promote_integer(type_t *type)
648 if(type->kind == TYPE_BITFIELD)
649 return promote_integer(type->bitfield.base);
651 if(get_rank(type) < ATOMIC_TYPE_INT)
658 * Create a cast expression.
660 * @param expression the expression to cast
661 * @param dest_type the destination type
663 static expression_t *create_cast_expression(expression_t *expression,
666 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
668 cast->unary.value = expression;
669 cast->base.datatype = dest_type;
675 * Check if a given expression represents the 0 pointer constant.
677 static bool is_null_pointer_constant(const expression_t *expression)
679 /* skip void* cast */
680 if(expression->kind == EXPR_UNARY_CAST
681 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
682 expression = expression->unary.value;
685 /* TODO: not correct yet, should be any constant integer expression
686 * which evaluates to 0 */
687 if (expression->kind != EXPR_CONST)
690 type_t *const type = skip_typeref(expression->base.datatype);
691 if (!is_type_integer(type))
694 return expression->conste.v.int_value == 0;
698 * Create an implicit cast expression.
700 * @param expression the expression to cast
701 * @param dest_type the destination type
703 static expression_t *create_implicit_cast(expression_t *expression,
706 type_t *source_type = expression->base.datatype;
708 if(source_type == NULL)
711 source_type = skip_typeref(source_type);
712 dest_type = skip_typeref(dest_type);
714 if(source_type == dest_type)
717 switch (dest_type->kind) {
719 /* TODO warning for implicitly converting to enum */
722 if (source_type->kind != TYPE_ATOMIC &&
723 source_type->kind != TYPE_ENUM &&
724 source_type->kind != TYPE_BITFIELD) {
725 panic("casting of non-atomic types not implemented yet");
728 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
729 type_error_incompatible("can't cast types",
730 expression->base.source_position, source_type,
735 return create_cast_expression(expression, dest_type);
738 switch (source_type->kind) {
740 if (is_null_pointer_constant(expression)) {
741 return create_cast_expression(expression, dest_type);
746 if (pointers_compatible(source_type, dest_type)) {
747 return create_cast_expression(expression, dest_type);
752 array_type_t *array_type = &source_type->array;
753 pointer_type_t *pointer_type = &dest_type->pointer;
754 if (types_compatible(array_type->element_type,
755 pointer_type->points_to)) {
756 return create_cast_expression(expression, dest_type);
762 panic("casting of non-atomic types not implemented yet");
765 type_error_incompatible("can't implicitly cast types",
766 expression->base.source_position, source_type, dest_type);
770 panic("casting of non-atomic types not implemented yet");
774 /** Implements the rules from § 6.5.16.1 */
775 static void semantic_assign(type_t *orig_type_left, expression_t **right,
778 type_t *orig_type_right = (*right)->base.datatype;
780 if(orig_type_right == NULL)
783 type_t *const type_left = skip_typeref(orig_type_left);
784 type_t *const type_right = skip_typeref(orig_type_right);
786 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
787 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
788 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
789 && is_type_pointer(type_right))) {
790 *right = create_implicit_cast(*right, type_left);
794 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
795 pointer_type_t *pointer_type_left = &type_left->pointer;
796 pointer_type_t *pointer_type_right = &type_right->pointer;
797 type_t *points_to_left = pointer_type_left->points_to;
798 type_t *points_to_right = pointer_type_right->points_to;
800 points_to_left = skip_typeref(points_to_left);
801 points_to_right = skip_typeref(points_to_right);
803 /* the left type has all qualifiers from the right type */
804 unsigned missing_qualifiers
805 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
806 if(missing_qualifiers != 0) {
807 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
811 points_to_left = get_unqualified_type(points_to_left);
812 points_to_right = get_unqualified_type(points_to_right);
814 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
815 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
816 && !types_compatible(points_to_left, points_to_right)) {
817 goto incompatible_assign_types;
820 *right = create_implicit_cast(*right, type_left);
824 if (is_type_compound(type_left)
825 && types_compatible(type_left, type_right)) {
826 *right = create_implicit_cast(*right, type_left);
830 incompatible_assign_types:
831 /* TODO: improve error message */
832 errorf(HERE, "incompatible types in %s", context);
833 errorf(HERE, "'%T' <- '%T'", orig_type_left, orig_type_right);
836 static expression_t *parse_constant_expression(void)
838 /* start parsing at precedence 7 (conditional expression) */
839 expression_t *result = parse_sub_expression(7);
841 if(!is_constant_expression(result)) {
842 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
848 static expression_t *parse_assignment_expression(void)
850 /* start parsing at precedence 2 (assignment expression) */
851 return parse_sub_expression(2);
854 static type_t *make_global_typedef(const char *name, type_t *type)
856 symbol_t *const symbol = symbol_table_insert(name);
858 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
859 declaration->namespc = NAMESPACE_NORMAL;
860 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
861 declaration->type = type;
862 declaration->symbol = symbol;
863 declaration->source_position = builtin_source_position;
865 record_declaration(declaration);
867 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
868 typedef_type->typedeft.declaration = declaration;
873 static const char *parse_string_literals(void)
875 assert(token.type == T_STRING_LITERAL);
876 const char *result = token.v.string;
880 while(token.type == T_STRING_LITERAL) {
881 result = concat_strings(result, token.v.string);
888 static void parse_attributes(void)
892 case T___attribute__: {
900 errorf(HERE, "EOF while parsing attribute");
919 if(token.type != T_STRING_LITERAL) {
920 parse_error_expected("while parsing assembler attribute",
925 parse_string_literals();
930 goto attributes_finished;
939 static designator_t *parse_designation(void)
941 if(token.type != '[' && token.type != '.')
944 designator_t *result = NULL;
945 designator_t *last = NULL;
948 designator_t *designator;
951 designator = allocate_ast_zero(sizeof(designator[0]));
953 designator->array_access = parse_constant_expression();
957 designator = allocate_ast_zero(sizeof(designator[0]));
959 if(token.type != T_IDENTIFIER) {
960 parse_error_expected("while parsing designator",
964 designator->symbol = token.v.symbol;
972 assert(designator != NULL);
974 last->next = designator;
983 static initializer_t *initializer_from_string(array_type_t *type,
986 /* TODO: check len vs. size of array type */
989 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
990 initializer->string.string = string;
995 static initializer_t *initializer_from_wide_string(array_type_t *const type,
996 wide_string_t *const string)
998 /* TODO: check len vs. size of array type */
1001 initializer_t *const initializer =
1002 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1003 initializer->wide_string.string = *string;
1008 static initializer_t *initializer_from_expression(type_t *type,
1009 expression_t *expression)
1011 /* TODO check that expression is a constant expression */
1013 /* § 6.7.8.14/15 char array may be initialized by string literals */
1014 type_t *const expr_type = expression->base.datatype;
1015 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1016 array_type_t *const array_type = &type->array;
1017 type_t *const element_type = skip_typeref(array_type->element_type);
1019 if (element_type->kind == TYPE_ATOMIC) {
1020 switch (expression->kind) {
1021 case EXPR_STRING_LITERAL:
1022 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
1023 return initializer_from_string(array_type,
1024 expression->string.value);
1027 case EXPR_WIDE_STRING_LITERAL: {
1028 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1029 if (get_unqualified_type(element_type) == bare_wchar_type) {
1030 return initializer_from_wide_string(array_type,
1031 &expression->wide_string.value);
1041 type_t *expression_type = skip_typeref(expression->base.datatype);
1042 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1043 semantic_assign(type, &expression, "initializer");
1045 initializer_t *result = allocate_initializer_zero(INITIALIZER_VALUE);
1046 result->value.value = expression;
1054 static initializer_t *parse_sub_initializer(type_t *type,
1055 expression_t *expression,
1056 type_t *expression_type);
1058 static initializer_t *parse_sub_initializer_elem(type_t *type)
1060 if(token.type == '{') {
1061 return parse_sub_initializer(type, NULL, NULL);
1064 expression_t *expression = parse_assignment_expression();
1065 type_t *expression_type = skip_typeref(expression->base.datatype);
1067 return parse_sub_initializer(type, expression, expression_type);
1070 static bool had_initializer_brace_warning;
1072 static void skip_designator(void)
1075 if(token.type == '.') {
1077 if(token.type == T_IDENTIFIER)
1079 } else if(token.type == '[') {
1081 parse_constant_expression();
1082 if(token.type == ']')
1090 static initializer_t *parse_sub_initializer(type_t *type,
1091 expression_t *expression,
1092 type_t *expression_type)
1094 if(is_type_scalar(type)) {
1095 /* there might be extra {} hierarchies */
1096 if(token.type == '{') {
1098 if(!had_initializer_brace_warning) {
1099 warningf(HERE, "braces around scalar initializer");
1100 had_initializer_brace_warning = true;
1102 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1103 if(token.type == ',') {
1105 /* TODO: warn about excessive elements */
1111 if(expression == NULL) {
1112 expression = parse_assignment_expression();
1114 return initializer_from_expression(type, expression);
1117 /* does the expression match the currently looked at object to initialize */
1118 if(expression != NULL) {
1119 initializer_t *result = initializer_from_expression(type, expression);
1124 bool read_paren = false;
1125 if(token.type == '{') {
1130 /* descend into subtype */
1131 initializer_t *result = NULL;
1132 initializer_t **elems;
1133 if(is_type_array(type)) {
1134 array_type_t *array_type = &type->array;
1135 type_t *element_type = array_type->element_type;
1136 element_type = skip_typeref(element_type);
1138 if(token.type == '.') {
1140 "compound designator in initializer for array type '%T'",
1146 had_initializer_brace_warning = false;
1147 if(expression == NULL) {
1148 sub = parse_sub_initializer_elem(element_type);
1150 sub = parse_sub_initializer(element_type, expression,
1154 /* didn't match the subtypes -> try the parent type */
1156 assert(!read_paren);
1160 elems = NEW_ARR_F(initializer_t*, 0);
1161 ARR_APP1(initializer_t*, elems, sub);
1164 if(token.type == '}')
1167 if(token.type == '}')
1170 sub = parse_sub_initializer_elem(element_type);
1172 /* TODO error, do nicer cleanup */
1173 errorf(HERE, "member initializer didn't match");
1177 ARR_APP1(initializer_t*, elems, sub);
1180 assert(is_type_compound(type));
1181 compound_type_t *compound_type = &type->compound;
1182 context_t *context = &compound_type->declaration->context;
1184 if(token.type == '[') {
1186 "array designator in initializer for compound type '%T'",
1191 declaration_t *first = context->declarations;
1194 type_t *first_type = first->type;
1195 first_type = skip_typeref(first_type);
1198 had_initializer_brace_warning = false;
1199 if(expression == NULL) {
1200 sub = parse_sub_initializer_elem(first_type);
1202 sub = parse_sub_initializer(first_type, expression,expression_type);
1205 /* didn't match the subtypes -> try our parent type */
1207 assert(!read_paren);
1211 elems = NEW_ARR_F(initializer_t*, 0);
1212 ARR_APP1(initializer_t*, elems, sub);
1214 declaration_t *iter = first->next;
1215 for( ; iter != NULL; iter = iter->next) {
1216 if(iter->symbol == NULL)
1218 if(iter->namespc != NAMESPACE_NORMAL)
1221 if(token.type == '}')
1224 if(token.type == '}')
1227 type_t *iter_type = iter->type;
1228 iter_type = skip_typeref(iter_type);
1230 sub = parse_sub_initializer_elem(iter_type);
1232 /* TODO error, do nicer cleanup */
1233 errorf(HERE, "member initializer didn't match");
1237 ARR_APP1(initializer_t*, elems, sub);
1241 int len = ARR_LEN(elems);
1242 size_t elems_size = sizeof(initializer_t*) * len;
1244 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1246 init->initializer.kind = INITIALIZER_LIST;
1248 memcpy(init->initializers, elems, elems_size);
1251 result = (initializer_t*) init;
1254 if(token.type == ',')
1261 static initializer_t *parse_initializer(type_t *type)
1263 initializer_t *result;
1265 type = skip_typeref(type);
1267 if(token.type != '{') {
1268 expression_t *expression = parse_assignment_expression();
1269 initializer_t *initializer = initializer_from_expression(type, expression);
1270 if(initializer == NULL) {
1271 errorf(HERE, "initializer expression '%E', type '%T' is incompatible with type '%T'", expression, expression->base.datatype, type);
1276 if(is_type_scalar(type)) {
1280 expression_t *expression = parse_assignment_expression();
1281 result = initializer_from_expression(type, expression);
1283 if(token.type == ',')
1289 result = parse_sub_initializer(type, NULL, NULL);
1295 static declaration_t *append_declaration(declaration_t *declaration);
1297 static declaration_t *parse_compound_type_specifier(bool is_struct)
1305 symbol_t *symbol = NULL;
1306 declaration_t *declaration = NULL;
1308 if (token.type == T___attribute__) {
1313 if(token.type == T_IDENTIFIER) {
1314 symbol = token.v.symbol;
1318 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1320 declaration = get_declaration(symbol, NAMESPACE_UNION);
1322 } else if(token.type != '{') {
1324 parse_error_expected("while parsing struct type specifier",
1325 T_IDENTIFIER, '{', 0);
1327 parse_error_expected("while parsing union type specifier",
1328 T_IDENTIFIER, '{', 0);
1334 if(declaration == NULL) {
1335 declaration = allocate_ast_zero(sizeof(declaration[0]));
1338 declaration->namespc = NAMESPACE_STRUCT;
1340 declaration->namespc = NAMESPACE_UNION;
1342 declaration->source_position = token.source_position;
1343 declaration->symbol = symbol;
1344 declaration->parent_context = context;
1345 if (symbol != NULL) {
1346 environment_push(declaration);
1348 append_declaration(declaration);
1351 if(token.type == '{') {
1352 if(declaration->init.is_defined) {
1353 assert(symbol != NULL);
1354 errorf(HERE, "multiple definition of '%s %Y'",
1355 is_struct ? "struct" : "union", symbol);
1356 declaration->context.declarations = NULL;
1358 declaration->init.is_defined = true;
1360 int top = environment_top();
1361 context_t *last_context = context;
1362 set_context(&declaration->context);
1364 parse_compound_type_entries();
1367 assert(context == &declaration->context);
1368 set_context(last_context);
1369 environment_pop_to(top);
1375 static void parse_enum_entries(enum_type_t *const enum_type)
1379 if(token.type == '}') {
1381 errorf(HERE, "empty enum not allowed");
1386 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1388 if(token.type != T_IDENTIFIER) {
1389 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1393 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1394 entry->type = (type_t*) enum_type;
1395 entry->symbol = token.v.symbol;
1396 entry->source_position = token.source_position;
1399 if(token.type == '=') {
1401 entry->init.enum_value = parse_constant_expression();
1406 record_declaration(entry);
1408 if(token.type != ',')
1411 } while(token.type != '}');
1416 static type_t *parse_enum_specifier(void)
1420 declaration_t *declaration;
1423 if(token.type == T_IDENTIFIER) {
1424 symbol = token.v.symbol;
1427 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1428 } else if(token.type != '{') {
1429 parse_error_expected("while parsing enum type specifier",
1430 T_IDENTIFIER, '{', 0);
1437 if(declaration == NULL) {
1438 declaration = allocate_ast_zero(sizeof(declaration[0]));
1440 declaration->namespc = NAMESPACE_ENUM;
1441 declaration->source_position = token.source_position;
1442 declaration->symbol = symbol;
1443 declaration->parent_context = context;
1446 type_t *const type = allocate_type_zero(TYPE_ENUM);
1447 type->enumt.declaration = declaration;
1449 if(token.type == '{') {
1450 if(declaration->init.is_defined) {
1451 errorf(HERE, "multiple definitions of enum %Y", symbol);
1453 if (symbol != NULL) {
1454 environment_push(declaration);
1456 append_declaration(declaration);
1457 declaration->init.is_defined = 1;
1459 parse_enum_entries(&type->enumt);
1467 * if a symbol is a typedef to another type, return true
1469 static bool is_typedef_symbol(symbol_t *symbol)
1471 const declaration_t *const declaration =
1472 get_declaration(symbol, NAMESPACE_NORMAL);
1474 declaration != NULL &&
1475 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1478 static type_t *parse_typeof(void)
1486 expression_t *expression = NULL;
1489 switch(token.type) {
1490 case T___extension__:
1491 /* this can be a prefix to a typename or an expression */
1492 /* we simply eat it now. */
1495 } while(token.type == T___extension__);
1499 if(is_typedef_symbol(token.v.symbol)) {
1500 type = parse_typename();
1502 expression = parse_expression();
1503 type = expression->base.datatype;
1508 type = parse_typename();
1512 expression = parse_expression();
1513 type = expression->base.datatype;
1519 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1520 typeof_type->typeoft.expression = expression;
1521 typeof_type->typeoft.typeof_type = type;
1527 SPECIFIER_SIGNED = 1 << 0,
1528 SPECIFIER_UNSIGNED = 1 << 1,
1529 SPECIFIER_LONG = 1 << 2,
1530 SPECIFIER_INT = 1 << 3,
1531 SPECIFIER_DOUBLE = 1 << 4,
1532 SPECIFIER_CHAR = 1 << 5,
1533 SPECIFIER_SHORT = 1 << 6,
1534 SPECIFIER_LONG_LONG = 1 << 7,
1535 SPECIFIER_FLOAT = 1 << 8,
1536 SPECIFIER_BOOL = 1 << 9,
1537 SPECIFIER_VOID = 1 << 10,
1538 #ifdef PROVIDE_COMPLEX
1539 SPECIFIER_COMPLEX = 1 << 11,
1540 SPECIFIER_IMAGINARY = 1 << 12,
1544 static type_t *create_builtin_type(symbol_t *const symbol,
1545 type_t *const real_type)
1547 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1548 type->builtin.symbol = symbol;
1549 type->builtin.real_type = real_type;
1551 type_t *result = typehash_insert(type);
1552 if (type != result) {
1559 static type_t *get_typedef_type(symbol_t *symbol)
1561 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1562 if(declaration == NULL
1563 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1566 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1567 type->typedeft.declaration = declaration;
1572 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1574 type_t *type = NULL;
1575 unsigned type_qualifiers = 0;
1576 unsigned type_specifiers = 0;
1579 specifiers->source_position = token.source_position;
1582 switch(token.type) {
1585 #define MATCH_STORAGE_CLASS(token, class) \
1587 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1588 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1590 specifiers->storage_class = class; \
1594 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1595 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1596 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1597 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1598 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1601 switch (specifiers->storage_class) {
1602 case STORAGE_CLASS_NONE:
1603 specifiers->storage_class = STORAGE_CLASS_THREAD;
1606 case STORAGE_CLASS_EXTERN:
1607 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1610 case STORAGE_CLASS_STATIC:
1611 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1615 errorf(HERE, "multiple storage classes in declaration specifiers");
1621 /* type qualifiers */
1622 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1624 type_qualifiers |= qualifier; \
1628 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1629 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1630 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1632 case T___extension__:
1637 /* type specifiers */
1638 #define MATCH_SPECIFIER(token, specifier, name) \
1641 if(type_specifiers & specifier) { \
1642 errorf(HERE, "multiple " name " type specifiers given"); \
1644 type_specifiers |= specifier; \
1648 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1649 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1650 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1651 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1652 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1653 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1654 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1655 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1656 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1657 #ifdef PROVIDE_COMPLEX
1658 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1659 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1662 /* only in microsoft mode */
1663 specifiers->decl_modifiers |= DM_FORCEINLINE;
1667 specifiers->is_inline = true;
1672 if(type_specifiers & SPECIFIER_LONG_LONG) {
1673 errorf(HERE, "multiple type specifiers given");
1674 } else if(type_specifiers & SPECIFIER_LONG) {
1675 type_specifiers |= SPECIFIER_LONG_LONG;
1677 type_specifiers |= SPECIFIER_LONG;
1681 /* TODO: if type != NULL for the following rules should issue
1684 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1686 type->compound.declaration = parse_compound_type_specifier(true);
1690 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1692 type->compound.declaration = parse_compound_type_specifier(false);
1696 type = parse_enum_specifier();
1699 type = parse_typeof();
1701 case T___builtin_va_list:
1702 type = duplicate_type(type_valist);
1706 case T___attribute__:
1711 case T_IDENTIFIER: {
1712 type_t *typedef_type = get_typedef_type(token.v.symbol);
1714 if(typedef_type == NULL)
1715 goto finish_specifiers;
1718 type = typedef_type;
1722 /* function specifier */
1724 goto finish_specifiers;
1731 atomic_type_kind_t atomic_type;
1733 /* match valid basic types */
1734 switch(type_specifiers) {
1735 case SPECIFIER_VOID:
1736 atomic_type = ATOMIC_TYPE_VOID;
1738 case SPECIFIER_CHAR:
1739 atomic_type = ATOMIC_TYPE_CHAR;
1741 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1742 atomic_type = ATOMIC_TYPE_SCHAR;
1744 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1745 atomic_type = ATOMIC_TYPE_UCHAR;
1747 case SPECIFIER_SHORT:
1748 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1749 case SPECIFIER_SHORT | SPECIFIER_INT:
1750 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1751 atomic_type = ATOMIC_TYPE_SHORT;
1753 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1754 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1755 atomic_type = ATOMIC_TYPE_USHORT;
1758 case SPECIFIER_SIGNED:
1759 case SPECIFIER_SIGNED | SPECIFIER_INT:
1760 atomic_type = ATOMIC_TYPE_INT;
1762 case SPECIFIER_UNSIGNED:
1763 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1764 atomic_type = ATOMIC_TYPE_UINT;
1766 case SPECIFIER_LONG:
1767 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1768 case SPECIFIER_LONG | SPECIFIER_INT:
1769 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1770 atomic_type = ATOMIC_TYPE_LONG;
1772 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1773 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1774 atomic_type = ATOMIC_TYPE_ULONG;
1776 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1777 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1778 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1779 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1781 atomic_type = ATOMIC_TYPE_LONGLONG;
1783 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1784 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1786 atomic_type = ATOMIC_TYPE_ULONGLONG;
1788 case SPECIFIER_FLOAT:
1789 atomic_type = ATOMIC_TYPE_FLOAT;
1791 case SPECIFIER_DOUBLE:
1792 atomic_type = ATOMIC_TYPE_DOUBLE;
1794 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1795 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1797 case SPECIFIER_BOOL:
1798 atomic_type = ATOMIC_TYPE_BOOL;
1800 #ifdef PROVIDE_COMPLEX
1801 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1802 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1804 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1805 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1807 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1808 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1810 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1811 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1813 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1814 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1816 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1817 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1821 /* invalid specifier combination, give an error message */
1822 if(type_specifiers == 0) {
1823 if (! strict_mode) {
1824 warningf(HERE, "no type specifiers in declaration, using int");
1825 atomic_type = ATOMIC_TYPE_INT;
1828 errorf(HERE, "no type specifiers given in declaration");
1830 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1831 (type_specifiers & SPECIFIER_UNSIGNED)) {
1832 errorf(HERE, "signed and unsigned specifiers gives");
1833 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1834 errorf(HERE, "only integer types can be signed or unsigned");
1836 errorf(HERE, "multiple datatypes in declaration");
1838 atomic_type = ATOMIC_TYPE_INVALID;
1841 type = allocate_type_zero(TYPE_ATOMIC);
1842 type->atomic.akind = atomic_type;
1845 if(type_specifiers != 0) {
1846 errorf(HERE, "multiple datatypes in declaration");
1850 type->base.qualifiers = type_qualifiers;
1852 type_t *result = typehash_insert(type);
1853 if(newtype && result != type) {
1857 specifiers->type = result;
1860 static type_qualifiers_t parse_type_qualifiers(void)
1862 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1865 switch(token.type) {
1866 /* type qualifiers */
1867 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1868 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1869 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1872 return type_qualifiers;
1877 static declaration_t *parse_identifier_list(void)
1879 declaration_t *declarations = NULL;
1880 declaration_t *last_declaration = NULL;
1882 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1884 declaration->source_position = token.source_position;
1885 declaration->symbol = token.v.symbol;
1888 if(last_declaration != NULL) {
1889 last_declaration->next = declaration;
1891 declarations = declaration;
1893 last_declaration = declaration;
1895 if(token.type != ',')
1898 } while(token.type == T_IDENTIFIER);
1900 return declarations;
1903 static void semantic_parameter(declaration_t *declaration)
1905 /* TODO: improve error messages */
1907 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1908 errorf(HERE, "typedef not allowed in parameter list");
1909 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1910 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1911 errorf(HERE, "parameter may only have none or register storage class");
1914 type_t *orig_type = declaration->type;
1915 if(orig_type == NULL)
1917 type_t *type = skip_typeref(orig_type);
1919 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1920 * into a pointer. § 6.7.5.3 (7) */
1921 if (is_type_array(type)) {
1922 const array_type_t *arr_type = &type->array;
1923 type_t *element_type = arr_type->element_type;
1925 type = make_pointer_type(element_type, type->base.qualifiers);
1927 declaration->type = type;
1930 if(is_type_incomplete(type)) {
1931 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1932 orig_type, declaration->symbol);
1936 static declaration_t *parse_parameter(void)
1938 declaration_specifiers_t specifiers;
1939 memset(&specifiers, 0, sizeof(specifiers));
1941 parse_declaration_specifiers(&specifiers);
1943 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1945 semantic_parameter(declaration);
1950 static declaration_t *parse_parameters(function_type_t *type)
1952 if(token.type == T_IDENTIFIER) {
1953 symbol_t *symbol = token.v.symbol;
1954 if(!is_typedef_symbol(symbol)) {
1955 type->kr_style_parameters = true;
1956 return parse_identifier_list();
1960 if(token.type == ')') {
1961 type->unspecified_parameters = 1;
1964 if(token.type == T_void && look_ahead(1)->type == ')') {
1969 declaration_t *declarations = NULL;
1970 declaration_t *declaration;
1971 declaration_t *last_declaration = NULL;
1972 function_parameter_t *parameter;
1973 function_parameter_t *last_parameter = NULL;
1976 switch(token.type) {
1980 return declarations;
1983 case T___extension__:
1985 declaration = parse_parameter();
1987 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1988 memset(parameter, 0, sizeof(parameter[0]));
1989 parameter->type = declaration->type;
1991 if(last_parameter != NULL) {
1992 last_declaration->next = declaration;
1993 last_parameter->next = parameter;
1995 type->parameters = parameter;
1996 declarations = declaration;
1998 last_parameter = parameter;
1999 last_declaration = declaration;
2003 return declarations;
2005 if(token.type != ',')
2006 return declarations;
2016 } construct_type_type_t;
2018 typedef struct construct_type_t construct_type_t;
2019 struct construct_type_t {
2020 construct_type_type_t type;
2021 construct_type_t *next;
2024 typedef struct parsed_pointer_t parsed_pointer_t;
2025 struct parsed_pointer_t {
2026 construct_type_t construct_type;
2027 type_qualifiers_t type_qualifiers;
2030 typedef struct construct_function_type_t construct_function_type_t;
2031 struct construct_function_type_t {
2032 construct_type_t construct_type;
2033 type_t *function_type;
2036 typedef struct parsed_array_t parsed_array_t;
2037 struct parsed_array_t {
2038 construct_type_t construct_type;
2039 type_qualifiers_t type_qualifiers;
2045 typedef struct construct_base_type_t construct_base_type_t;
2046 struct construct_base_type_t {
2047 construct_type_t construct_type;
2051 static construct_type_t *parse_pointer_declarator(void)
2055 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2056 memset(pointer, 0, sizeof(pointer[0]));
2057 pointer->construct_type.type = CONSTRUCT_POINTER;
2058 pointer->type_qualifiers = parse_type_qualifiers();
2060 return (construct_type_t*) pointer;
2063 static construct_type_t *parse_array_declarator(void)
2067 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2068 memset(array, 0, sizeof(array[0]));
2069 array->construct_type.type = CONSTRUCT_ARRAY;
2071 if(token.type == T_static) {
2072 array->is_static = true;
2076 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2077 if(type_qualifiers != 0) {
2078 if(token.type == T_static) {
2079 array->is_static = true;
2083 array->type_qualifiers = type_qualifiers;
2085 if(token.type == '*' && look_ahead(1)->type == ']') {
2086 array->is_variable = true;
2088 } else if(token.type != ']') {
2089 array->size = parse_assignment_expression();
2094 return (construct_type_t*) array;
2097 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2101 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2103 declaration_t *parameters = parse_parameters(&type->function);
2104 if(declaration != NULL) {
2105 declaration->context.declarations = parameters;
2108 construct_function_type_t *construct_function_type =
2109 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2110 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2111 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2112 construct_function_type->function_type = type;
2116 return (construct_type_t*) construct_function_type;
2119 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2120 bool may_be_abstract)
2122 /* construct a single linked list of construct_type_t's which describe
2123 * how to construct the final declarator type */
2124 construct_type_t *first = NULL;
2125 construct_type_t *last = NULL;
2128 while(token.type == '*') {
2129 construct_type_t *type = parse_pointer_declarator();
2140 /* TODO: find out if this is correct */
2143 construct_type_t *inner_types = NULL;
2145 switch(token.type) {
2147 if(declaration == NULL) {
2148 errorf(HERE, "no identifier expected in typename");
2150 declaration->symbol = token.v.symbol;
2151 declaration->source_position = token.source_position;
2157 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2163 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2164 /* avoid a loop in the outermost scope, because eat_statement doesn't
2166 if(token.type == '}' && current_function == NULL) {
2174 construct_type_t *p = last;
2177 construct_type_t *type;
2178 switch(token.type) {
2180 type = parse_function_declarator(declaration);
2183 type = parse_array_declarator();
2186 goto declarator_finished;
2189 /* insert in the middle of the list (behind p) */
2191 type->next = p->next;
2202 declarator_finished:
2205 /* append inner_types at the end of the list, we don't to set last anymore
2206 * as it's not needed anymore */
2208 assert(first == NULL);
2209 first = inner_types;
2211 last->next = inner_types;
2217 static type_t *construct_declarator_type(construct_type_t *construct_list,
2220 construct_type_t *iter = construct_list;
2221 for( ; iter != NULL; iter = iter->next) {
2222 switch(iter->type) {
2223 case CONSTRUCT_INVALID:
2224 panic("invalid type construction found");
2225 case CONSTRUCT_FUNCTION: {
2226 construct_function_type_t *construct_function_type
2227 = (construct_function_type_t*) iter;
2229 type_t *function_type = construct_function_type->function_type;
2231 function_type->function.return_type = type;
2233 type = function_type;
2237 case CONSTRUCT_POINTER: {
2238 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2239 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2240 pointer_type->pointer.points_to = type;
2241 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2243 type = pointer_type;
2247 case CONSTRUCT_ARRAY: {
2248 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2249 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2251 array_type->base.qualifiers = parsed_array->type_qualifiers;
2252 array_type->array.element_type = type;
2253 array_type->array.is_static = parsed_array->is_static;
2254 array_type->array.is_variable = parsed_array->is_variable;
2255 array_type->array.size = parsed_array->size;
2262 type_t *hashed_type = typehash_insert(type);
2263 if(hashed_type != type) {
2264 /* the function type was constructed earlier freeing it here will
2265 * destroy other types... */
2266 if(iter->type != CONSTRUCT_FUNCTION) {
2276 static declaration_t *parse_declarator(
2277 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2279 type_t *type = specifiers->type;
2280 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
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 declaration->type = construct_declarator_type(construct_type, type);
2289 if(construct_type != NULL) {
2290 obstack_free(&temp_obst, construct_type);
2296 static type_t *parse_abstract_declarator(type_t *base_type)
2298 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2300 type_t *result = construct_declarator_type(construct_type, base_type);
2301 if(construct_type != NULL) {
2302 obstack_free(&temp_obst, construct_type);
2308 static declaration_t *append_declaration(declaration_t* const declaration)
2310 if (last_declaration != NULL) {
2311 last_declaration->next = declaration;
2313 context->declarations = declaration;
2315 last_declaration = declaration;
2319 static declaration_t *internal_record_declaration(
2320 declaration_t *const declaration,
2321 const bool is_function_definition)
2323 const symbol_t *const symbol = declaration->symbol;
2324 const namespace_t namespc = (namespace_t)declaration->namespc;
2326 const type_t *const type = skip_typeref(declaration->type);
2327 if (is_type_function(type) && type->function.unspecified_parameters) {
2328 warningf(declaration->source_position,
2329 "function declaration '%#T' is not a prototype",
2330 type, declaration->symbol);
2333 declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2334 assert(declaration != previous_declaration);
2335 if (previous_declaration != NULL
2336 && previous_declaration->parent_context == context) {
2337 /* can happen for K&R style declarations */
2338 if(previous_declaration->type == NULL) {
2339 previous_declaration->type = declaration->type;
2342 const type_t *const prev_type = skip_typeref(previous_declaration->type);
2343 if (!types_compatible(type, prev_type)) {
2344 errorf(declaration->source_position,
2345 "declaration '%#T' is incompatible with previous declaration '%#T'",
2346 type, symbol, previous_declaration->type, symbol);
2347 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2349 unsigned old_storage_class = previous_declaration->storage_class;
2350 unsigned new_storage_class = declaration->storage_class;
2352 /* pretend no storage class means extern for function declarations
2353 * (except if the previous declaration is neither none nor extern) */
2354 if (is_type_function(type)) {
2355 switch (old_storage_class) {
2356 case STORAGE_CLASS_NONE:
2357 old_storage_class = STORAGE_CLASS_EXTERN;
2359 case STORAGE_CLASS_EXTERN:
2360 if (new_storage_class == STORAGE_CLASS_NONE && !is_function_definition) {
2361 new_storage_class = STORAGE_CLASS_EXTERN;
2369 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2370 new_storage_class == STORAGE_CLASS_EXTERN) {
2371 warn_redundant_declaration:
2372 warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
2373 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2374 } else if (current_function == NULL) {
2375 if (old_storage_class != STORAGE_CLASS_STATIC &&
2376 new_storage_class == STORAGE_CLASS_STATIC) {
2377 errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
2378 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2380 if (old_storage_class != STORAGE_CLASS_EXTERN) {
2381 goto warn_redundant_declaration;
2383 if (new_storage_class == STORAGE_CLASS_NONE) {
2384 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2388 if (old_storage_class == new_storage_class) {
2389 errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
2391 errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
2393 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2396 return previous_declaration;
2399 assert(declaration->parent_context == NULL);
2400 assert(declaration->symbol != NULL);
2401 assert(context != NULL);
2403 declaration->parent_context = context;
2405 environment_push(declaration);
2406 return append_declaration(declaration);
2409 static declaration_t *record_declaration(declaration_t *declaration)
2411 return internal_record_declaration(declaration, false);
2414 static declaration_t *record_function_definition(declaration_t *const declaration)
2416 return internal_record_declaration(declaration, true);
2419 static void parser_error_multiple_definition(declaration_t *declaration,
2420 const source_position_t source_position)
2422 errorf(source_position, "multiple definition of symbol '%Y'",
2423 declaration->symbol);
2424 errorf(declaration->source_position,
2425 "this is the location of the previous definition.");
2428 static bool is_declaration_specifier(const token_t *token,
2429 bool only_type_specifiers)
2431 switch(token->type) {
2435 return is_typedef_symbol(token->v.symbol);
2437 case T___extension__:
2440 return !only_type_specifiers;
2447 static void parse_init_declarator_rest(declaration_t *declaration)
2451 type_t *orig_type = declaration->type;
2452 type_t *type = NULL;
2453 if(orig_type != NULL)
2454 type = skip_typeref(orig_type);
2456 if(declaration->init.initializer != NULL) {
2457 parser_error_multiple_definition(declaration, token.source_position);
2460 initializer_t *initializer = parse_initializer(type);
2462 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2463 * the array type size */
2464 if(type != NULL && is_type_array(type) && initializer != NULL) {
2465 array_type_t *array_type = &type->array;
2467 if(array_type->size == NULL) {
2468 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2470 cnst->base.datatype = type_size_t;
2472 switch (initializer->kind) {
2473 case INITIALIZER_LIST: {
2474 initializer_list_t *const list = &initializer->list;
2475 cnst->conste.v.int_value = list->len;
2479 case INITIALIZER_STRING: {
2480 initializer_string_t *const string = &initializer->string;
2481 cnst->conste.v.int_value = strlen(string->string) + 1;
2485 case INITIALIZER_WIDE_STRING: {
2486 initializer_wide_string_t *const string = &initializer->wide_string;
2487 cnst->conste.v.int_value = string->string.size;
2492 panic("invalid initializer type");
2495 array_type->size = cnst;
2499 if(type != NULL && is_type_function(type)) {
2500 errorf(declaration->source_position,
2501 "initializers not allowed for function types at declator '%Y' (type '%T')",
2502 declaration->symbol, orig_type);
2504 declaration->init.initializer = initializer;
2508 /* parse rest of a declaration without any declarator */
2509 static void parse_anonymous_declaration_rest(
2510 const declaration_specifiers_t *specifiers,
2511 parsed_declaration_func finished_declaration)
2515 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2517 declaration->type = specifiers->type;
2518 declaration->storage_class = specifiers->storage_class;
2519 declaration->source_position = specifiers->source_position;
2521 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2522 warningf(declaration->source_position, "useless storage class in empty declaration");
2525 type_t *type = declaration->type;
2526 switch (type->kind) {
2527 case TYPE_COMPOUND_STRUCT:
2528 case TYPE_COMPOUND_UNION: {
2529 const compound_type_t *compound_type = &type->compound;
2530 if (compound_type->declaration->symbol == NULL) {
2531 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2540 warningf(declaration->source_position, "empty declaration");
2544 finished_declaration(declaration);
2547 static void parse_declaration_rest(declaration_t *ndeclaration,
2548 const declaration_specifiers_t *specifiers,
2549 parsed_declaration_func finished_declaration)
2552 declaration_t *declaration = finished_declaration(ndeclaration);
2554 type_t *orig_type = declaration->type;
2555 type_t *type = skip_typeref(orig_type);
2557 if(type->kind != TYPE_FUNCTION && declaration->is_inline) {
2558 warningf(declaration->source_position,
2559 "variable '%Y' declared 'inline'\n", declaration->symbol);
2562 if(token.type == '=') {
2563 parse_init_declarator_rest(declaration);
2566 if(token.type != ',')
2570 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2575 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2577 symbol_t *symbol = declaration->symbol;
2578 if(symbol == NULL) {
2579 errorf(HERE, "anonymous declaration not valid as function parameter");
2582 namespace_t namespc = (namespace_t) declaration->namespc;
2583 if(namespc != NAMESPACE_NORMAL) {
2584 return record_declaration(declaration);
2587 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2588 if(previous_declaration == NULL ||
2589 previous_declaration->parent_context != context) {
2590 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2595 if(previous_declaration->type == NULL) {
2596 previous_declaration->type = declaration->type;
2597 previous_declaration->storage_class = declaration->storage_class;
2598 previous_declaration->parent_context = context;
2599 return previous_declaration;
2601 return record_declaration(declaration);
2605 static void parse_declaration(parsed_declaration_func finished_declaration)
2607 declaration_specifiers_t specifiers;
2608 memset(&specifiers, 0, sizeof(specifiers));
2609 parse_declaration_specifiers(&specifiers);
2611 if(token.type == ';') {
2612 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2614 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2615 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2619 static void parse_kr_declaration_list(declaration_t *declaration)
2621 type_t *type = skip_typeref(declaration->type);
2622 if(!is_type_function(type))
2625 if(!type->function.kr_style_parameters)
2628 /* push function parameters */
2629 int top = environment_top();
2630 context_t *last_context = context;
2631 set_context(&declaration->context);
2633 declaration_t *parameter = declaration->context.declarations;
2634 for( ; parameter != NULL; parameter = parameter->next) {
2635 assert(parameter->parent_context == NULL);
2636 parameter->parent_context = context;
2637 environment_push(parameter);
2640 /* parse declaration list */
2641 while(is_declaration_specifier(&token, false)) {
2642 parse_declaration(finished_kr_declaration);
2645 /* pop function parameters */
2646 assert(context == &declaration->context);
2647 set_context(last_context);
2648 environment_pop_to(top);
2650 /* update function type */
2651 type_t *new_type = duplicate_type(type);
2652 new_type->function.kr_style_parameters = false;
2654 function_parameter_t *parameters = NULL;
2655 function_parameter_t *last_parameter = NULL;
2657 declaration_t *parameter_declaration = declaration->context.declarations;
2658 for( ; parameter_declaration != NULL;
2659 parameter_declaration = parameter_declaration->next) {
2660 type_t *parameter_type = parameter_declaration->type;
2661 if(parameter_type == NULL) {
2663 errorf(HERE, "no type specified for function parameter '%Y'",
2664 parameter_declaration->symbol);
2666 warningf(HERE, "no type specified for function parameter '%Y', using int",
2667 parameter_declaration->symbol);
2668 parameter_type = type_int;
2669 parameter_declaration->type = parameter_type;
2673 semantic_parameter(parameter_declaration);
2674 parameter_type = parameter_declaration->type;
2676 function_parameter_t *function_parameter
2677 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2678 memset(function_parameter, 0, sizeof(function_parameter[0]));
2680 function_parameter->type = parameter_type;
2681 if(last_parameter != NULL) {
2682 last_parameter->next = function_parameter;
2684 parameters = function_parameter;
2686 last_parameter = function_parameter;
2688 new_type->function.parameters = parameters;
2690 type = typehash_insert(new_type);
2691 if(type != new_type) {
2692 obstack_free(type_obst, new_type);
2695 declaration->type = type;
2699 * Check if all labels are defined in the current function.
2701 static void check_for_missing_labels(void)
2703 const goto_statement_t *goto_statement, *next;
2705 for (goto_statement = goto_list;
2706 goto_statement != NULL;
2707 goto_statement = next) {
2708 const declaration_t *label = goto_statement->label;
2710 if (label->source_position.input_name == NULL) {
2711 errorf(goto_statement->statement.source_position,
2712 "label '%Y' used but not defined", label->symbol);
2718 static void parse_external_declaration(void)
2720 /* function-definitions and declarations both start with declaration
2722 declaration_specifiers_t specifiers;
2723 memset(&specifiers, 0, sizeof(specifiers));
2724 parse_declaration_specifiers(&specifiers);
2726 /* must be a declaration */
2727 if(token.type == ';') {
2728 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2732 /* declarator is common to both function-definitions and declarations */
2733 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2735 /* must be a declaration */
2736 if(token.type == ',' || token.type == '=' || token.type == ';') {
2737 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2741 /* must be a function definition */
2742 parse_kr_declaration_list(ndeclaration);
2744 if(token.type != '{') {
2745 parse_error_expected("while parsing function definition", '{', 0);
2750 type_t *type = ndeclaration->type;
2756 /* note that we don't skip typerefs: the standard doesn't allow them here
2757 * (so we can't use is_type_function here) */
2758 if(type->kind != TYPE_FUNCTION) {
2759 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2760 type, ndeclaration->symbol);
2765 /* § 6.7.5.3 (14) a function definition with () means no
2766 * parameters (and not unspecified parameters) */
2767 if(type->function.unspecified_parameters) {
2768 type_t *duplicate = duplicate_type(type);
2769 duplicate->function.unspecified_parameters = false;
2771 type = typehash_insert(duplicate);
2772 if(type != duplicate) {
2773 obstack_free(type_obst, duplicate);
2775 ndeclaration->type = type;
2778 declaration_t *const declaration = record_function_definition(ndeclaration);
2779 if(ndeclaration != declaration) {
2780 declaration->context = ndeclaration->context;
2782 type = skip_typeref(declaration->type);
2784 /* push function parameters and switch context */
2785 int top = environment_top();
2786 context_t *last_context = context;
2787 set_context(&declaration->context);
2789 declaration_t *parameter = declaration->context.declarations;
2790 for( ; parameter != NULL; parameter = parameter->next) {
2791 if(parameter->parent_context == &ndeclaration->context) {
2792 parameter->parent_context = context;
2794 assert(parameter->parent_context == NULL
2795 || parameter->parent_context == context);
2796 parameter->parent_context = context;
2797 environment_push(parameter);
2800 if(declaration->init.statement != NULL) {
2801 parser_error_multiple_definition(declaration, token.source_position);
2803 goto end_of_parse_external_declaration;
2805 /* parse function body */
2806 int label_stack_top = label_top();
2807 declaration_t *old_current_function = current_function;
2808 current_function = declaration;
2810 declaration->init.statement = parse_compound_statement();
2811 check_for_missing_labels();
2813 assert(current_function == declaration);
2814 current_function = old_current_function;
2815 label_pop_to(label_stack_top);
2818 end_of_parse_external_declaration:
2819 assert(context == &declaration->context);
2820 set_context(last_context);
2821 environment_pop_to(top);
2824 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2826 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2827 type->bitfield.base = base;
2828 type->bitfield.size = size;
2833 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2835 /* TODO: check constraints for struct declarations (in specifiers) */
2837 declaration_t *declaration;
2839 if(token.type == ':') {
2842 type_t *base_type = specifiers->type;
2843 expression_t *size = parse_constant_expression();
2845 type_t *type = make_bitfield_type(base_type, size);
2847 declaration = allocate_ast_zero(sizeof(declaration[0]));
2849 declaration->namespc = NAMESPACE_NORMAL;
2850 declaration->storage_class = STORAGE_CLASS_NONE;
2851 declaration->source_position = token.source_position;
2852 declaration->modifiers = specifiers->decl_modifiers;
2853 declaration->type = type;
2855 record_declaration(declaration);
2857 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2859 if(token.type == ':') {
2861 expression_t *size = parse_constant_expression();
2863 type_t *type = make_bitfield_type(declaration->type, size);
2864 declaration->type = type;
2867 record_declaration(declaration);
2869 if(token.type != ',')
2876 static void parse_compound_type_entries(void)
2880 while(token.type != '}' && token.type != T_EOF) {
2881 declaration_specifiers_t specifiers;
2882 memset(&specifiers, 0, sizeof(specifiers));
2883 parse_declaration_specifiers(&specifiers);
2885 parse_struct_declarators(&specifiers);
2887 if(token.type == T_EOF) {
2888 errorf(HERE, "EOF while parsing struct");
2893 static type_t *parse_typename(void)
2895 declaration_specifiers_t specifiers;
2896 memset(&specifiers, 0, sizeof(specifiers));
2897 parse_declaration_specifiers(&specifiers);
2898 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2899 /* TODO: improve error message, user does probably not know what a
2900 * storage class is...
2902 errorf(HERE, "typename may not have a storage class");
2905 type_t *result = parse_abstract_declarator(specifiers.type);
2913 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2914 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2915 expression_t *left);
2917 typedef struct expression_parser_function_t expression_parser_function_t;
2918 struct expression_parser_function_t {
2919 unsigned precedence;
2920 parse_expression_function parser;
2921 unsigned infix_precedence;
2922 parse_expression_infix_function infix_parser;
2925 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2928 * Creates a new invalid expression.
2930 static expression_t *create_invalid_expression(void)
2932 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2933 expression->base.source_position = token.source_position;
2937 static expression_t *expected_expression_error(void)
2939 errorf(HERE, "expected expression, got token '%K'", &token);
2943 return create_invalid_expression();
2947 * Parse a string constant.
2949 static expression_t *parse_string_const(void)
2951 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2952 cnst->base.datatype = type_string;
2953 cnst->string.value = parse_string_literals();
2959 * Parse a wide string constant.
2961 static expression_t *parse_wide_string_const(void)
2963 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2964 cnst->base.datatype = type_wchar_t_ptr;
2965 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2971 * Parse an integer constant.
2973 static expression_t *parse_int_const(void)
2975 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2976 cnst->base.datatype = token.datatype;
2977 cnst->conste.v.int_value = token.v.intvalue;
2985 * Parse a float constant.
2987 static expression_t *parse_float_const(void)
2989 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2990 cnst->base.datatype = token.datatype;
2991 cnst->conste.v.float_value = token.v.floatvalue;
2998 static declaration_t *create_implicit_function(symbol_t *symbol,
2999 const source_position_t source_position)
3001 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
3002 ntype->function.return_type = type_int;
3003 ntype->function.unspecified_parameters = true;
3005 type_t *type = typehash_insert(ntype);
3010 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3012 declaration->storage_class = STORAGE_CLASS_EXTERN;
3013 declaration->type = type;
3014 declaration->symbol = symbol;
3015 declaration->source_position = source_position;
3016 declaration->parent_context = global_context;
3018 context_t *old_context = context;
3019 set_context(global_context);
3021 environment_push(declaration);
3022 /* prepend the declaration to the global declarations list */
3023 declaration->next = context->declarations;
3024 context->declarations = declaration;
3026 assert(context == global_context);
3027 set_context(old_context);
3033 * Creates a return_type (func)(argument_type) function type if not
3036 * @param return_type the return type
3037 * @param argument_type the argument type
3039 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3041 function_parameter_t *parameter
3042 = obstack_alloc(type_obst, sizeof(parameter[0]));
3043 memset(parameter, 0, sizeof(parameter[0]));
3044 parameter->type = argument_type;
3046 type_t *type = allocate_type_zero(TYPE_FUNCTION);
3047 type->function.return_type = return_type;
3048 type->function.parameters = parameter;
3050 type_t *result = typehash_insert(type);
3051 if(result != type) {
3059 * Creates a function type for some function like builtins.
3061 * @param symbol the symbol describing the builtin
3063 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3065 switch(symbol->ID) {
3066 case T___builtin_alloca:
3067 return make_function_1_type(type_void_ptr, type_size_t);
3068 case T___builtin_nan:
3069 return make_function_1_type(type_double, type_string);
3070 case T___builtin_nanf:
3071 return make_function_1_type(type_float, type_string);
3072 case T___builtin_nand:
3073 return make_function_1_type(type_long_double, type_string);
3074 case T___builtin_va_end:
3075 return make_function_1_type(type_void, type_valist);
3077 panic("not implemented builtin symbol found");
3082 * Performs automatic type cast as described in § 6.3.2.1.
3084 * @param orig_type the original type
3086 static type_t *automatic_type_conversion(type_t *orig_type)
3088 if(orig_type == NULL)
3091 type_t *type = skip_typeref(orig_type);
3092 if(is_type_array(type)) {
3093 array_type_t *array_type = &type->array;
3094 type_t *element_type = array_type->element_type;
3095 unsigned qualifiers = array_type->type.qualifiers;
3097 return make_pointer_type(element_type, qualifiers);
3100 if(is_type_function(type)) {
3101 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3108 * reverts the automatic casts of array to pointer types and function
3109 * to function-pointer types as defined § 6.3.2.1
3111 type_t *revert_automatic_type_conversion(const expression_t *expression)
3113 if(expression->base.datatype == NULL)
3116 switch(expression->kind) {
3117 case EXPR_REFERENCE: {
3118 const reference_expression_t *ref = &expression->reference;
3119 return ref->declaration->type;
3122 const select_expression_t *select = &expression->select;
3123 return select->compound_entry->type;
3125 case EXPR_UNARY_DEREFERENCE: {
3126 expression_t *value = expression->unary.value;
3127 type_t *type = skip_typeref(value->base.datatype);
3128 pointer_type_t *pointer_type = &type->pointer;
3130 return pointer_type->points_to;
3132 case EXPR_BUILTIN_SYMBOL: {
3133 const builtin_symbol_expression_t *builtin
3134 = &expression->builtin_symbol;
3135 return get_builtin_symbol_type(builtin->symbol);
3137 case EXPR_ARRAY_ACCESS: {
3138 const array_access_expression_t *array_access
3139 = &expression->array_access;
3140 const expression_t *array_ref = array_access->array_ref;
3141 type_t *type_left = skip_typeref(array_ref->base.datatype);
3142 assert(is_type_pointer(type_left));
3143 pointer_type_t *pointer_type = &type_left->pointer;
3144 return pointer_type->points_to;
3151 return expression->base.datatype;
3154 static expression_t *parse_reference(void)
3156 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3158 reference_expression_t *ref = &expression->reference;
3159 ref->symbol = token.v.symbol;
3161 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3163 source_position_t source_position = token.source_position;
3166 if(declaration == NULL) {
3167 if (! strict_mode && token.type == '(') {
3168 /* an implicitly defined function */
3169 warningf(HERE, "implicit declaration of function '%Y'",
3172 declaration = create_implicit_function(ref->symbol,
3175 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3180 type_t *type = declaration->type;
3182 /* we always do the auto-type conversions; the & and sizeof parser contains
3183 * code to revert this! */
3184 type = automatic_type_conversion(type);
3186 ref->declaration = declaration;
3187 ref->expression.datatype = type;
3192 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3196 /* TODO check if explicit cast is allowed and issue warnings/errors */
3199 static expression_t *parse_cast(void)
3201 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3203 cast->base.source_position = token.source_position;
3205 type_t *type = parse_typename();
3208 expression_t *value = parse_sub_expression(20);
3210 check_cast_allowed(value, type);
3212 cast->base.datatype = type;
3213 cast->unary.value = value;
3218 static expression_t *parse_statement_expression(void)
3220 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3222 statement_t *statement = parse_compound_statement();
3223 expression->statement.statement = statement;
3224 if(statement == NULL) {
3229 assert(statement->kind == STATEMENT_COMPOUND);
3230 compound_statement_t *compound_statement = &statement->compound;
3232 /* find last statement and use it's type */
3233 const statement_t *last_statement = NULL;
3234 const statement_t *iter = compound_statement->statements;
3235 for( ; iter != NULL; iter = iter->base.next) {
3236 last_statement = iter;
3239 if(last_statement->kind == STATEMENT_EXPRESSION) {
3240 const expression_statement_t *expression_statement
3241 = &last_statement->expression;
3242 expression->base.datatype
3243 = expression_statement->expression->base.datatype;
3245 expression->base.datatype = type_void;
3253 static expression_t *parse_brace_expression(void)
3257 switch(token.type) {
3259 /* gcc extension: a statement expression */
3260 return parse_statement_expression();
3264 return parse_cast();
3266 if(is_typedef_symbol(token.v.symbol)) {
3267 return parse_cast();
3271 expression_t *result = parse_expression();
3277 static expression_t *parse_function_keyword(void)
3282 if (current_function == NULL) {
3283 errorf(HERE, "'__func__' used outside of a function");
3286 string_literal_expression_t *expression
3287 = allocate_ast_zero(sizeof(expression[0]));
3289 expression->expression.kind = EXPR_FUNCTION;
3290 expression->expression.datatype = type_string;
3291 expression->value = current_function->symbol->string;
3293 return (expression_t*) expression;
3296 static expression_t *parse_pretty_function_keyword(void)
3298 eat(T___PRETTY_FUNCTION__);
3301 if (current_function == NULL) {
3302 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3305 string_literal_expression_t *expression
3306 = allocate_ast_zero(sizeof(expression[0]));
3308 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3309 expression->expression.datatype = type_string;
3310 expression->value = current_function->symbol->string;
3312 return (expression_t*) expression;
3315 static designator_t *parse_designator(void)
3317 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3319 if(token.type != T_IDENTIFIER) {
3320 parse_error_expected("while parsing member designator",
3325 result->symbol = token.v.symbol;
3328 designator_t *last_designator = result;
3330 if(token.type == '.') {
3332 if(token.type != T_IDENTIFIER) {
3333 parse_error_expected("while parsing member designator",
3338 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3339 designator->symbol = token.v.symbol;
3342 last_designator->next = designator;
3343 last_designator = designator;
3346 if(token.type == '[') {
3348 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3349 designator->array_access = parse_expression();
3350 if(designator->array_access == NULL) {
3356 last_designator->next = designator;
3357 last_designator = designator;
3366 static expression_t *parse_offsetof(void)
3368 eat(T___builtin_offsetof);
3370 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3371 expression->base.datatype = type_size_t;
3374 expression->offsetofe.type = parse_typename();
3376 expression->offsetofe.designator = parse_designator();
3382 static expression_t *parse_va_start(void)
3384 eat(T___builtin_va_start);
3386 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3389 expression->va_starte.ap = parse_assignment_expression();
3391 expression_t *const expr = parse_assignment_expression();
3392 if (expr->kind == EXPR_REFERENCE) {
3393 declaration_t *const decl = expr->reference.declaration;
3394 if (decl->parent_context == ¤t_function->context &&
3395 decl->next == NULL) {
3396 expression->va_starte.parameter = decl;
3401 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3403 return create_invalid_expression();
3406 static expression_t *parse_va_arg(void)
3408 eat(T___builtin_va_arg);
3410 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3413 expression->va_arge.ap = parse_assignment_expression();
3415 expression->base.datatype = parse_typename();
3421 static expression_t *parse_builtin_symbol(void)
3423 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3425 symbol_t *symbol = token.v.symbol;
3427 expression->builtin_symbol.symbol = symbol;
3430 type_t *type = get_builtin_symbol_type(symbol);
3431 type = automatic_type_conversion(type);
3433 expression->base.datatype = type;
3437 static expression_t *parse_builtin_constant(void)
3439 eat(T___builtin_constant_p);
3441 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3444 expression->builtin_constant.value = parse_assignment_expression();
3446 expression->base.datatype = type_int;
3451 static expression_t *parse_builtin_prefetch(void)
3453 eat(T___builtin_prefetch);
3455 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3458 expression->builtin_prefetch.adr = parse_assignment_expression();
3459 if (token.type == ',') {
3461 expression->builtin_prefetch.rw = parse_assignment_expression();
3463 if (token.type == ',') {
3465 expression->builtin_prefetch.locality = parse_assignment_expression();
3468 expression->base.datatype = type_void;
3473 static expression_t *parse_compare_builtin(void)
3475 expression_t *expression;
3477 switch(token.type) {
3478 case T___builtin_isgreater:
3479 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3481 case T___builtin_isgreaterequal:
3482 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3484 case T___builtin_isless:
3485 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3487 case T___builtin_islessequal:
3488 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3490 case T___builtin_islessgreater:
3491 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3493 case T___builtin_isunordered:
3494 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3497 panic("invalid compare builtin found");
3503 expression->binary.left = parse_assignment_expression();
3505 expression->binary.right = parse_assignment_expression();
3508 type_t *orig_type_left = expression->binary.left->base.datatype;
3509 type_t *orig_type_right = expression->binary.right->base.datatype;
3510 if(orig_type_left == NULL || orig_type_right == NULL)
3513 type_t *type_left = skip_typeref(orig_type_left);
3514 type_t *type_right = skip_typeref(orig_type_right);
3515 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3516 type_error_incompatible("invalid operands in comparison",
3517 token.source_position, type_left, type_right);
3519 semantic_comparison(&expression->binary);
3525 static expression_t *parse_builtin_expect(void)
3527 eat(T___builtin_expect);
3529 expression_t *expression
3530 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3533 expression->binary.left = parse_assignment_expression();
3535 expression->binary.right = parse_constant_expression();
3538 expression->base.datatype = expression->binary.left->base.datatype;
3543 static expression_t *parse_assume(void) {
3546 expression_t *expression
3547 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3550 expression->unary.value = parse_assignment_expression();
3553 expression->base.datatype = type_void;
3557 static expression_t *parse_alignof(void) {
3560 expression_t *expression
3561 = allocate_expression_zero(EXPR_ALIGNOF);
3564 expression->alignofe.type = parse_typename();
3567 expression->base.datatype = type_size_t;
3571 static expression_t *parse_primary_expression(void)
3573 switch(token.type) {
3575 return parse_int_const();
3576 case T_FLOATINGPOINT:
3577 return parse_float_const();
3578 case T_STRING_LITERAL:
3579 return parse_string_const();
3580 case T_WIDE_STRING_LITERAL:
3581 return parse_wide_string_const();
3583 return parse_reference();
3584 case T___FUNCTION__:
3586 return parse_function_keyword();
3587 case T___PRETTY_FUNCTION__:
3588 return parse_pretty_function_keyword();
3589 case T___builtin_offsetof:
3590 return parse_offsetof();
3591 case T___builtin_va_start:
3592 return parse_va_start();
3593 case T___builtin_va_arg:
3594 return parse_va_arg();
3595 case T___builtin_expect:
3596 return parse_builtin_expect();
3597 case T___builtin_nanf:
3598 case T___builtin_alloca:
3599 case T___builtin_va_end:
3600 return parse_builtin_symbol();
3601 case T___builtin_isgreater:
3602 case T___builtin_isgreaterequal:
3603 case T___builtin_isless:
3604 case T___builtin_islessequal:
3605 case T___builtin_islessgreater:
3606 case T___builtin_isunordered:
3607 return parse_compare_builtin();
3608 case T___builtin_constant_p:
3609 return parse_builtin_constant();
3610 case T___builtin_prefetch:
3611 return parse_builtin_prefetch();
3613 return parse_alignof();
3615 return parse_assume();
3618 return parse_brace_expression();
3621 errorf(HERE, "unexpected token '%K'", &token);
3624 return create_invalid_expression();
3628 * Check if the expression has the character type and issue a warning then.
3630 static void check_for_char_index_type(const expression_t *expression) {
3631 type_t *type = expression->base.datatype;
3632 type_t *base_type = skip_typeref(type);
3634 if (base_type->base.kind == TYPE_ATOMIC) {
3635 if (base_type->atomic.akind == ATOMIC_TYPE_CHAR) {
3636 warningf(expression->base.source_position,
3637 "array subscript has type '%T'", type);
3642 static expression_t *parse_array_expression(unsigned precedence,
3649 expression_t *inside = parse_expression();
3651 array_access_expression_t *array_access
3652 = allocate_ast_zero(sizeof(array_access[0]));
3654 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3656 type_t *type_left = left->base.datatype;
3657 type_t *type_inside = inside->base.datatype;
3658 type_t *return_type = NULL;
3660 if(type_left != NULL && type_inside != NULL) {
3661 type_left = skip_typeref(type_left);
3662 type_inside = skip_typeref(type_inside);
3664 if(is_type_pointer(type_left)) {
3665 pointer_type_t *pointer = &type_left->pointer;
3666 return_type = pointer->points_to;
3667 array_access->array_ref = left;
3668 array_access->index = inside;
3669 check_for_char_index_type(inside);
3670 } else if(is_type_pointer(type_inside)) {
3671 pointer_type_t *pointer = &type_inside->pointer;
3672 return_type = pointer->points_to;
3673 array_access->array_ref = inside;
3674 array_access->index = left;
3675 array_access->flipped = true;
3676 check_for_char_index_type(left);
3678 errorf(HERE, "array access on object with non-pointer types '%T', '%T'", type_left, type_inside);
3681 array_access->array_ref = left;
3682 array_access->index = inside;
3685 if(token.type != ']') {
3686 parse_error_expected("Problem while parsing array access", ']', 0);
3687 return (expression_t*) array_access;
3691 return_type = automatic_type_conversion(return_type);
3692 array_access->expression.datatype = return_type;
3694 return (expression_t*) array_access;
3697 static expression_t *parse_sizeof(unsigned precedence)
3701 sizeof_expression_t *sizeof_expression
3702 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3703 sizeof_expression->expression.kind = EXPR_SIZEOF;
3704 sizeof_expression->expression.datatype = type_size_t;
3706 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3708 sizeof_expression->type = parse_typename();
3711 expression_t *expression = parse_sub_expression(precedence);
3712 expression->base.datatype = revert_automatic_type_conversion(expression);
3714 sizeof_expression->type = expression->base.datatype;
3715 sizeof_expression->size_expression = expression;
3718 return (expression_t*) sizeof_expression;
3721 static expression_t *parse_select_expression(unsigned precedence,
3722 expression_t *compound)
3725 assert(token.type == '.' || token.type == T_MINUSGREATER);
3727 bool is_pointer = (token.type == T_MINUSGREATER);
3730 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3731 select->select.compound = compound;
3733 if(token.type != T_IDENTIFIER) {
3734 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3737 symbol_t *symbol = token.v.symbol;
3738 select->select.symbol = symbol;
3741 type_t *orig_type = compound->base.datatype;
3742 if(orig_type == NULL)
3743 return create_invalid_expression();
3745 type_t *type = skip_typeref(orig_type);
3747 type_t *type_left = type;
3749 if(type->kind != TYPE_POINTER) {
3750 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3751 return create_invalid_expression();
3753 pointer_type_t *pointer_type = &type->pointer;
3754 type_left = pointer_type->points_to;
3756 type_left = skip_typeref(type_left);
3758 if(type_left->kind != TYPE_COMPOUND_STRUCT
3759 && type_left->kind != TYPE_COMPOUND_UNION) {
3760 errorf(HERE, "request for member '%Y' in something not a struct or "
3761 "union, but '%T'", symbol, type_left);
3762 return create_invalid_expression();
3765 compound_type_t *compound_type = &type_left->compound;
3766 declaration_t *declaration = compound_type->declaration;
3768 if(!declaration->init.is_defined) {
3769 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3771 return create_invalid_expression();
3774 declaration_t *iter = declaration->context.declarations;
3775 for( ; iter != NULL; iter = iter->next) {
3776 if(iter->symbol == symbol) {
3781 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3782 return create_invalid_expression();
3785 /* we always do the auto-type conversions; the & and sizeof parser contains
3786 * code to revert this! */
3787 type_t *expression_type = automatic_type_conversion(iter->type);
3789 select->select.compound_entry = iter;
3790 select->base.datatype = expression_type;
3792 if(expression_type->kind == TYPE_BITFIELD) {
3793 expression_t *extract
3794 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3795 extract->unary.value = select;
3796 extract->base.datatype = expression_type->bitfield.base;
3805 * Parse a call expression, ie. expression '( ... )'.
3807 * @param expression the function address
3809 static expression_t *parse_call_expression(unsigned precedence,
3810 expression_t *expression)
3813 expression_t *result = allocate_expression_zero(EXPR_CALL);
3815 call_expression_t *call = &result->call;
3816 call->function = expression;
3818 function_type_t *function_type = NULL;
3819 type_t *orig_type = expression->base.datatype;
3820 if(orig_type != NULL) {
3821 type_t *type = skip_typeref(orig_type);
3823 if(is_type_pointer(type)) {
3824 pointer_type_t *pointer_type = &type->pointer;
3826 type = skip_typeref(pointer_type->points_to);
3828 if (is_type_function(type)) {
3829 function_type = &type->function;
3830 call->expression.datatype = function_type->return_type;
3833 if(function_type == NULL) {
3834 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3836 function_type = NULL;
3837 call->expression.datatype = NULL;
3841 /* parse arguments */
3844 if(token.type != ')') {
3845 call_argument_t *last_argument = NULL;
3848 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3850 argument->expression = parse_assignment_expression();
3851 if(last_argument == NULL) {
3852 call->arguments = argument;
3854 last_argument->next = argument;
3856 last_argument = argument;
3858 if(token.type != ',')
3865 if(function_type != NULL) {
3866 function_parameter_t *parameter = function_type->parameters;
3867 call_argument_t *argument = call->arguments;
3868 for( ; parameter != NULL && argument != NULL;
3869 parameter = parameter->next, argument = argument->next) {
3870 type_t *expected_type = parameter->type;
3871 /* TODO report context in error messages */
3872 argument->expression = create_implicit_cast(argument->expression,
3875 /* too few parameters */
3876 if(parameter != NULL) {
3877 errorf(HERE, "too few arguments to function '%E'", expression);
3878 } else if(argument != NULL) {
3879 /* too many parameters */
3880 if(!function_type->variadic
3881 && !function_type->unspecified_parameters) {
3882 errorf(HERE, "too many arguments to function '%E'", expression);
3884 /* do default promotion */
3885 for( ; argument != NULL; argument = argument->next) {
3886 type_t *type = argument->expression->base.datatype;
3891 type = skip_typeref(type);
3892 if(is_type_integer(type)) {
3893 type = promote_integer(type);
3894 } else if(type == type_float) {
3898 argument->expression
3899 = create_implicit_cast(argument->expression, type);
3902 check_format(&result->call);
3905 check_format(&result->call);
3912 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3914 static bool same_compound_type(const type_t *type1, const type_t *type2)
3916 if(!is_type_compound(type1))
3918 if(type1->kind != type2->kind)
3921 const compound_type_t *compound1 = &type1->compound;
3922 const compound_type_t *compound2 = &type2->compound;
3924 return compound1->declaration == compound2->declaration;
3928 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3930 * @param expression the conditional expression
3932 static expression_t *parse_conditional_expression(unsigned precedence,
3933 expression_t *expression)
3937 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3939 conditional_expression_t *conditional = &result->conditional;
3940 conditional->condition = expression;
3943 type_t *condition_type_orig = expression->base.datatype;
3944 if(condition_type_orig != NULL) {
3945 type_t *condition_type = skip_typeref(condition_type_orig);
3946 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3947 type_error("expected a scalar type in conditional condition",
3948 expression->base.source_position, condition_type_orig);
3952 expression_t *true_expression = parse_expression();
3954 expression_t *false_expression = parse_sub_expression(precedence);
3956 conditional->true_expression = true_expression;
3957 conditional->false_expression = false_expression;
3959 type_t *orig_true_type = true_expression->base.datatype;
3960 type_t *orig_false_type = false_expression->base.datatype;
3961 if(orig_true_type == NULL || orig_false_type == NULL)
3964 type_t *true_type = skip_typeref(orig_true_type);
3965 type_t *false_type = skip_typeref(orig_false_type);
3968 type_t *result_type = NULL;
3969 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3970 result_type = semantic_arithmetic(true_type, false_type);
3972 true_expression = create_implicit_cast(true_expression, result_type);
3973 false_expression = create_implicit_cast(false_expression, result_type);
3975 conditional->true_expression = true_expression;
3976 conditional->false_expression = false_expression;
3977 conditional->expression.datatype = result_type;
3978 } else if (same_compound_type(true_type, false_type)
3979 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3980 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3981 /* just take 1 of the 2 types */
3982 result_type = true_type;
3983 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3984 && pointers_compatible(true_type, false_type)) {
3986 result_type = true_type;
3989 type_error_incompatible("while parsing conditional",
3990 expression->base.source_position, true_type,
3994 conditional->expression.datatype = result_type;
3999 * Parse an extension expression.
4001 static expression_t *parse_extension(unsigned precedence)
4003 eat(T___extension__);
4005 /* TODO enable extensions */
4006 expression_t *expression = parse_sub_expression(precedence);
4007 /* TODO disable extensions */
4011 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4013 eat(T___builtin_classify_type);
4015 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4016 result->base.datatype = type_int;
4019 expression_t *expression = parse_sub_expression(precedence);
4021 result->classify_type.type_expression = expression;
4026 static void semantic_incdec(unary_expression_t *expression)
4028 type_t *orig_type = expression->value->base.datatype;
4029 if(orig_type == NULL)
4032 type_t *type = skip_typeref(orig_type);
4033 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4034 /* TODO: improve error message */
4035 errorf(HERE, "operation needs an arithmetic or pointer type");
4039 expression->expression.datatype = orig_type;
4042 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4044 type_t *orig_type = expression->value->base.datatype;
4045 if(orig_type == NULL)
4048 type_t *type = skip_typeref(orig_type);
4049 if(!is_type_arithmetic(type)) {
4050 /* TODO: improve error message */
4051 errorf(HERE, "operation needs an arithmetic type");
4055 expression->expression.datatype = orig_type;
4058 static void semantic_unexpr_scalar(unary_expression_t *expression)
4060 type_t *orig_type = expression->value->base.datatype;
4061 if(orig_type == NULL)
4064 type_t *type = skip_typeref(orig_type);
4065 if (!is_type_scalar(type)) {
4066 errorf(HERE, "operand of ! must be of scalar type");
4070 expression->expression.datatype = orig_type;
4073 static void semantic_unexpr_integer(unary_expression_t *expression)
4075 type_t *orig_type = expression->value->base.datatype;
4076 if(orig_type == NULL)
4079 type_t *type = skip_typeref(orig_type);
4080 if (!is_type_integer(type)) {
4081 errorf(HERE, "operand of ~ must be of integer type");
4085 expression->expression.datatype = orig_type;
4088 static void semantic_dereference(unary_expression_t *expression)
4090 type_t *orig_type = expression->value->base.datatype;
4091 if(orig_type == NULL)
4094 type_t *type = skip_typeref(orig_type);
4095 if(!is_type_pointer(type)) {
4096 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4100 pointer_type_t *pointer_type = &type->pointer;
4101 type_t *result_type = pointer_type->points_to;
4103 result_type = automatic_type_conversion(result_type);
4104 expression->expression.datatype = result_type;
4108 * Check the semantic of the address taken expression.
4110 static void semantic_take_addr(unary_expression_t *expression)
4112 expression_t *value = expression->value;
4113 value->base.datatype = revert_automatic_type_conversion(value);
4115 type_t *orig_type = value->base.datatype;
4116 if(orig_type == NULL)
4119 if(value->kind == EXPR_REFERENCE) {
4120 reference_expression_t *reference = (reference_expression_t*) value;
4121 declaration_t *declaration = reference->declaration;
4122 if(declaration != NULL) {
4123 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4124 errorf(expression->expression.source_position,
4125 "address of register variable '%Y' requested",
4126 declaration->symbol);
4128 declaration->address_taken = 1;
4132 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4135 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4136 static expression_t *parse_##unexpression_type(unsigned precedence) \
4140 expression_t *unary_expression \
4141 = allocate_expression_zero(unexpression_type); \
4142 unary_expression->base.source_position = HERE; \
4143 unary_expression->unary.value = parse_sub_expression(precedence); \
4145 sfunc(&unary_expression->unary); \
4147 return unary_expression; \
4150 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4151 semantic_unexpr_arithmetic)
4152 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4153 semantic_unexpr_arithmetic)
4154 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4155 semantic_unexpr_scalar)
4156 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4157 semantic_dereference)
4158 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4160 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4161 semantic_unexpr_integer)
4162 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4164 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4167 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4169 static expression_t *parse_##unexpression_type(unsigned precedence, \
4170 expression_t *left) \
4172 (void) precedence; \
4175 expression_t *unary_expression \
4176 = allocate_expression_zero(unexpression_type); \
4177 unary_expression->unary.value = left; \
4179 sfunc(&unary_expression->unary); \
4181 return unary_expression; \
4184 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4185 EXPR_UNARY_POSTFIX_INCREMENT,
4187 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4188 EXPR_UNARY_POSTFIX_DECREMENT,
4191 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4193 /* TODO: handle complex + imaginary types */
4195 /* § 6.3.1.8 Usual arithmetic conversions */
4196 if(type_left == type_long_double || type_right == type_long_double) {
4197 return type_long_double;
4198 } else if(type_left == type_double || type_right == type_double) {
4200 } else if(type_left == type_float || type_right == type_float) {
4204 type_right = promote_integer(type_right);
4205 type_left = promote_integer(type_left);
4207 if(type_left == type_right)
4210 bool signed_left = is_type_signed(type_left);
4211 bool signed_right = is_type_signed(type_right);
4212 int rank_left = get_rank(type_left);
4213 int rank_right = get_rank(type_right);
4214 if(rank_left < rank_right) {
4215 if(signed_left == signed_right || !signed_right) {
4221 if(signed_left == signed_right || !signed_left) {
4230 * Check the semantic restrictions for a binary expression.
4232 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4234 expression_t *left = expression->left;
4235 expression_t *right = expression->right;
4236 type_t *orig_type_left = left->base.datatype;
4237 type_t *orig_type_right = right->base.datatype;
4239 if(orig_type_left == NULL || orig_type_right == NULL)
4242 type_t *type_left = skip_typeref(orig_type_left);
4243 type_t *type_right = skip_typeref(orig_type_right);
4245 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4246 /* TODO: improve error message */
4247 errorf(HERE, "operation needs arithmetic types");
4251 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4252 expression->left = create_implicit_cast(left, arithmetic_type);
4253 expression->right = create_implicit_cast(right, arithmetic_type);
4254 expression->expression.datatype = arithmetic_type;
4257 static void semantic_shift_op(binary_expression_t *expression)
4259 expression_t *left = expression->left;
4260 expression_t *right = expression->right;
4261 type_t *orig_type_left = left->base.datatype;
4262 type_t *orig_type_right = right->base.datatype;
4264 if(orig_type_left == NULL || orig_type_right == NULL)
4267 type_t *type_left = skip_typeref(orig_type_left);
4268 type_t *type_right = skip_typeref(orig_type_right);
4270 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4271 /* TODO: improve error message */
4272 errorf(HERE, "operation needs integer types");
4276 type_left = promote_integer(type_left);
4277 type_right = promote_integer(type_right);
4279 expression->left = create_implicit_cast(left, type_left);
4280 expression->right = create_implicit_cast(right, type_right);
4281 expression->expression.datatype = type_left;
4284 static void semantic_add(binary_expression_t *expression)
4286 expression_t *left = expression->left;
4287 expression_t *right = expression->right;
4288 type_t *orig_type_left = left->base.datatype;
4289 type_t *orig_type_right = right->base.datatype;
4291 if(orig_type_left == NULL || orig_type_right == NULL)
4294 type_t *type_left = skip_typeref(orig_type_left);
4295 type_t *type_right = skip_typeref(orig_type_right);
4298 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
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;
4304 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4305 expression->expression.datatype = type_left;
4306 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4307 expression->expression.datatype = type_right;
4309 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4313 static void semantic_sub(binary_expression_t *expression)
4315 expression_t *left = expression->left;
4316 expression_t *right = expression->right;
4317 type_t *orig_type_left = left->base.datatype;
4318 type_t *orig_type_right = right->base.datatype;
4320 if(orig_type_left == NULL || orig_type_right == NULL)
4323 type_t *type_left = skip_typeref(orig_type_left);
4324 type_t *type_right = skip_typeref(orig_type_right);
4327 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4328 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4329 expression->left = create_implicit_cast(left, arithmetic_type);
4330 expression->right = create_implicit_cast(right, arithmetic_type);
4331 expression->expression.datatype = arithmetic_type;
4333 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4334 expression->expression.datatype = type_left;
4335 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4336 if(!pointers_compatible(type_left, type_right)) {
4337 errorf(HERE, "pointers to incompatible objects to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4339 expression->expression.datatype = type_ptrdiff_t;
4342 errorf(HERE, "invalid operands to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4346 static void semantic_comparison(binary_expression_t *expression)
4348 expression_t *left = expression->left;
4349 expression_t *right = expression->right;
4350 type_t *orig_type_left = left->base.datatype;
4351 type_t *orig_type_right = right->base.datatype;
4353 if(orig_type_left == NULL || orig_type_right == NULL)
4356 type_t *type_left = skip_typeref(orig_type_left);
4357 type_t *type_right = skip_typeref(orig_type_right);
4359 /* TODO non-arithmetic types */
4360 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4361 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4362 expression->left = create_implicit_cast(left, arithmetic_type);
4363 expression->right = create_implicit_cast(right, arithmetic_type);
4364 expression->expression.datatype = arithmetic_type;
4365 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4366 /* TODO check compatibility */
4367 } else if (is_type_pointer(type_left)) {
4368 expression->right = create_implicit_cast(right, type_left);
4369 } else if (is_type_pointer(type_right)) {
4370 expression->left = create_implicit_cast(left, type_right);
4372 type_error_incompatible("invalid operands in comparison",
4373 token.source_position, type_left, type_right);
4375 expression->expression.datatype = type_int;
4378 static void semantic_arithmetic_assign(binary_expression_t *expression)
4380 expression_t *left = expression->left;
4381 expression_t *right = expression->right;
4382 type_t *orig_type_left = left->base.datatype;
4383 type_t *orig_type_right = right->base.datatype;
4385 if(orig_type_left == NULL || orig_type_right == NULL)
4388 type_t *type_left = skip_typeref(orig_type_left);
4389 type_t *type_right = skip_typeref(orig_type_right);
4391 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4392 /* TODO: improve error message */
4393 errorf(HERE, "operation needs arithmetic types");
4397 /* combined instructions are tricky. We can't create an implicit cast on
4398 * the left side, because we need the uncasted form for the store.
4399 * The ast2firm pass has to know that left_type must be right_type
4400 * for the arithmetic operation and create a cast by itself */
4401 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4402 expression->right = create_implicit_cast(right, arithmetic_type);
4403 expression->expression.datatype = type_left;
4406 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4408 expression_t *left = expression->left;
4409 expression_t *right = expression->right;
4410 type_t *orig_type_left = left->base.datatype;
4411 type_t *orig_type_right = right->base.datatype;
4413 if(orig_type_left == NULL || orig_type_right == NULL)
4416 type_t *type_left = skip_typeref(orig_type_left);
4417 type_t *type_right = skip_typeref(orig_type_right);
4419 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4420 /* combined instructions are tricky. We can't create an implicit cast on
4421 * the left side, because we need the uncasted form for the store.
4422 * The ast2firm pass has to know that left_type must be right_type
4423 * for the arithmetic operation and create a cast by itself */
4424 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4425 expression->right = create_implicit_cast(right, arithmetic_type);
4426 expression->expression.datatype = type_left;
4427 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4428 expression->expression.datatype = type_left;
4430 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4436 * Check the semantic restrictions of a logical expression.
4438 static void semantic_logical_op(binary_expression_t *expression)
4440 expression_t *left = expression->left;
4441 expression_t *right = expression->right;
4442 type_t *orig_type_left = left->base.datatype;
4443 type_t *orig_type_right = right->base.datatype;
4445 if(orig_type_left == NULL || orig_type_right == NULL)
4448 type_t *type_left = skip_typeref(orig_type_left);
4449 type_t *type_right = skip_typeref(orig_type_right);
4451 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4452 /* TODO: improve error message */
4453 errorf(HERE, "operation needs scalar types");
4457 expression->expression.datatype = type_int;
4461 * Checks if a compound type has constant fields.
4463 static bool has_const_fields(const compound_type_t *type)
4465 const context_t *context = &type->declaration->context;
4466 const declaration_t *declaration = context->declarations;
4468 for (; declaration != NULL; declaration = declaration->next) {
4469 if (declaration->namespc != NAMESPACE_NORMAL)
4472 const type_t *decl_type = skip_typeref(declaration->type);
4473 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4481 * Check the semantic restrictions of a binary assign expression.
4483 static void semantic_binexpr_assign(binary_expression_t *expression)
4485 expression_t *left = expression->left;
4486 type_t *orig_type_left = left->base.datatype;
4488 if(orig_type_left == NULL)
4491 type_t *type_left = revert_automatic_type_conversion(left);
4492 type_left = skip_typeref(orig_type_left);
4494 /* must be a modifiable lvalue */
4495 if (is_type_array(type_left)) {
4496 errorf(HERE, "cannot assign to arrays ('%E')", left);
4499 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4500 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4504 if(is_type_incomplete(type_left)) {
4506 "left-hand side of assignment '%E' has incomplete type '%T'",
4507 left, orig_type_left);
4510 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4511 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4512 left, orig_type_left);
4516 semantic_assign(orig_type_left, &expression->right, "assignment");
4518 expression->expression.datatype = orig_type_left;
4521 static void semantic_comma(binary_expression_t *expression)
4523 expression->expression.datatype = expression->right->base.datatype;
4526 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4527 static expression_t *parse_##binexpression_type(unsigned precedence, \
4528 expression_t *left) \
4532 expression_t *right = parse_sub_expression(precedence + lr); \
4534 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4535 binexpr->binary.left = left; \
4536 binexpr->binary.right = right; \
4537 sfunc(&binexpr->binary); \
4542 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4543 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4544 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4545 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4546 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4547 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4548 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4549 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4550 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4552 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4553 semantic_comparison, 1)
4554 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4555 semantic_comparison, 1)
4556 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4557 semantic_comparison, 1)
4558 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4559 semantic_comparison, 1)
4561 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4562 semantic_binexpr_arithmetic, 1)
4563 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4564 semantic_binexpr_arithmetic, 1)
4565 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4566 semantic_binexpr_arithmetic, 1)
4567 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4568 semantic_logical_op, 1)
4569 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4570 semantic_logical_op, 1)
4571 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4572 semantic_shift_op, 1)
4573 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4574 semantic_shift_op, 1)
4575 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4576 semantic_arithmetic_addsubb_assign, 0)
4577 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4578 semantic_arithmetic_addsubb_assign, 0)
4579 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4580 semantic_arithmetic_assign, 0)
4581 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4582 semantic_arithmetic_assign, 0)
4583 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4584 semantic_arithmetic_assign, 0)
4585 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4586 semantic_arithmetic_assign, 0)
4587 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4588 semantic_arithmetic_assign, 0)
4589 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4590 semantic_arithmetic_assign, 0)
4591 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4592 semantic_arithmetic_assign, 0)
4593 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4594 semantic_arithmetic_assign, 0)
4596 static expression_t *parse_sub_expression(unsigned precedence)
4598 if(token.type < 0) {
4599 return expected_expression_error();
4602 expression_parser_function_t *parser
4603 = &expression_parsers[token.type];
4604 source_position_t source_position = token.source_position;
4607 if(parser->parser != NULL) {
4608 left = parser->parser(parser->precedence);
4610 left = parse_primary_expression();
4612 assert(left != NULL);
4613 left->base.source_position = source_position;
4616 if(token.type < 0) {
4617 return expected_expression_error();
4620 parser = &expression_parsers[token.type];
4621 if(parser->infix_parser == NULL)
4623 if(parser->infix_precedence < precedence)
4626 left = parser->infix_parser(parser->infix_precedence, left);
4628 assert(left != NULL);
4629 assert(left->kind != EXPR_UNKNOWN);
4630 left->base.source_position = source_position;
4637 * Parse an expression.
4639 static expression_t *parse_expression(void)
4641 return parse_sub_expression(1);
4645 * Register a parser for a prefix-like operator with given precedence.
4647 * @param parser the parser function
4648 * @param token_type the token type of the prefix token
4649 * @param precedence the precedence of the operator
4651 static void register_expression_parser(parse_expression_function parser,
4652 int token_type, unsigned precedence)
4654 expression_parser_function_t *entry = &expression_parsers[token_type];
4656 if(entry->parser != NULL) {
4657 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4658 panic("trying to register multiple expression parsers for a token");
4660 entry->parser = parser;
4661 entry->precedence = precedence;
4665 * Register a parser for an infix operator with given precedence.
4667 * @param parser the parser function
4668 * @param token_type the token type of the infix operator
4669 * @param precedence the precedence of the operator
4671 static void register_infix_parser(parse_expression_infix_function parser,
4672 int token_type, unsigned precedence)
4674 expression_parser_function_t *entry = &expression_parsers[token_type];
4676 if(entry->infix_parser != NULL) {
4677 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4678 panic("trying to register multiple infix expression parsers for a "
4681 entry->infix_parser = parser;
4682 entry->infix_precedence = precedence;
4686 * Initialize the expression parsers.
4688 static void init_expression_parsers(void)
4690 memset(&expression_parsers, 0, sizeof(expression_parsers));
4692 register_infix_parser(parse_array_expression, '[', 30);
4693 register_infix_parser(parse_call_expression, '(', 30);
4694 register_infix_parser(parse_select_expression, '.', 30);
4695 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4696 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4698 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4701 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4702 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4703 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4704 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4705 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4706 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4707 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4708 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4709 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4710 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4711 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4712 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4713 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4714 T_EXCLAMATIONMARKEQUAL, 13);
4715 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4716 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4717 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4718 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4719 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4720 register_infix_parser(parse_conditional_expression, '?', 7);
4721 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4722 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4723 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4724 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4725 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4726 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4727 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4728 T_LESSLESSEQUAL, 2);
4729 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4730 T_GREATERGREATEREQUAL, 2);
4731 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4733 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4735 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4738 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4740 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4741 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4742 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4743 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4744 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4745 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4746 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4748 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4750 register_expression_parser(parse_sizeof, T_sizeof, 25);
4751 register_expression_parser(parse_extension, T___extension__, 25);
4752 register_expression_parser(parse_builtin_classify_type,
4753 T___builtin_classify_type, 25);
4757 * Parse a asm statement constraints specification.
4759 static asm_constraint_t *parse_asm_constraints(void)
4761 asm_constraint_t *result = NULL;
4762 asm_constraint_t *last = NULL;
4764 while(token.type == T_STRING_LITERAL || token.type == '[') {
4765 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4766 memset(constraint, 0, sizeof(constraint[0]));
4768 if(token.type == '[') {
4770 if(token.type != T_IDENTIFIER) {
4771 parse_error_expected("while parsing asm constraint",
4775 constraint->symbol = token.v.symbol;
4780 constraint->constraints = parse_string_literals();
4782 constraint->expression = parse_expression();
4786 last->next = constraint;
4788 result = constraint;
4792 if(token.type != ',')
4801 * Parse a asm statement clobber specification.
4803 static asm_clobber_t *parse_asm_clobbers(void)
4805 asm_clobber_t *result = NULL;
4806 asm_clobber_t *last = NULL;
4808 while(token.type == T_STRING_LITERAL) {
4809 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4810 clobber->clobber = parse_string_literals();
4813 last->next = clobber;
4819 if(token.type != ',')
4828 * Parse an asm statement.
4830 static statement_t *parse_asm_statement(void)
4834 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4835 statement->base.source_position = token.source_position;
4837 asm_statement_t *asm_statement = &statement->asms;
4839 if(token.type == T_volatile) {
4841 asm_statement->is_volatile = true;
4845 asm_statement->asm_text = parse_string_literals();
4847 if(token.type != ':')
4851 asm_statement->inputs = parse_asm_constraints();
4852 if(token.type != ':')
4856 asm_statement->outputs = parse_asm_constraints();
4857 if(token.type != ':')
4861 asm_statement->clobbers = parse_asm_clobbers();
4870 * Parse a case statement.
4872 static statement_t *parse_case_statement(void)
4876 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4878 statement->base.source_position = token.source_position;
4879 statement->case_label.expression = parse_expression();
4883 if (! is_constant_expression(statement->case_label.expression)) {
4884 errorf(statement->base.source_position,
4885 "case label does not reduce to an integer constant");
4887 /* TODO: check if the case label is already known */
4888 if (current_switch != NULL) {
4889 /* link all cases into the switch statement */
4890 if (current_switch->last_case == NULL) {
4891 current_switch->first_case =
4892 current_switch->last_case = &statement->case_label;
4894 current_switch->last_case->next = &statement->case_label;
4897 errorf(statement->base.source_position,
4898 "case label not within a switch statement");
4901 statement->case_label.label_statement = parse_statement();
4907 * Finds an existing default label of a switch statement.
4909 static case_label_statement_t *
4910 find_default_label(const switch_statement_t *statement)
4912 for (case_label_statement_t *label = statement->first_case;
4914 label = label->next) {
4915 if (label->expression == NULL)
4922 * Parse a default statement.
4924 static statement_t *parse_default_statement(void)
4928 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4930 statement->base.source_position = token.source_position;
4933 if (current_switch != NULL) {
4934 const case_label_statement_t *def_label = find_default_label(current_switch);
4935 if (def_label != NULL) {
4936 errorf(HERE, "multiple default labels in one switch");
4937 errorf(def_label->statement.source_position,
4938 "this is the first default label");
4940 /* link all cases into the switch statement */
4941 if (current_switch->last_case == NULL) {
4942 current_switch->first_case =
4943 current_switch->last_case = &statement->case_label;
4945 current_switch->last_case->next = &statement->case_label;
4949 errorf(statement->base.source_position,
4950 "'default' label not within a switch statement");
4952 statement->label.label_statement = parse_statement();
4958 * Return the declaration for a given label symbol or create a new one.
4960 static declaration_t *get_label(symbol_t *symbol)
4962 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4963 assert(current_function != NULL);
4964 /* if we found a label in the same function, then we already created the
4966 if(candidate != NULL
4967 && candidate->parent_context == ¤t_function->context) {
4971 /* otherwise we need to create a new one */
4972 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4973 declaration->namespc = NAMESPACE_LABEL;
4974 declaration->symbol = symbol;
4976 label_push(declaration);
4982 * Parse a label statement.
4984 static statement_t *parse_label_statement(void)
4986 assert(token.type == T_IDENTIFIER);
4987 symbol_t *symbol = token.v.symbol;
4990 declaration_t *label = get_label(symbol);
4992 /* if source position is already set then the label is defined twice,
4993 * otherwise it was just mentioned in a goto so far */
4994 if(label->source_position.input_name != NULL) {
4995 errorf(HERE, "duplicate label '%Y'", symbol);
4996 errorf(label->source_position, "previous definition of '%Y' was here",
4999 label->source_position = token.source_position;
5002 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
5004 label_statement->statement.kind = STATEMENT_LABEL;
5005 label_statement->statement.source_position = token.source_position;
5006 label_statement->label = label;
5010 if(token.type == '}') {
5011 /* TODO only warn? */
5012 errorf(HERE, "label at end of compound statement");
5013 return (statement_t*) label_statement;
5015 label_statement->label_statement = parse_statement();
5018 return (statement_t*) label_statement;
5022 * Parse an if statement.
5024 static statement_t *parse_if(void)
5028 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5029 statement->statement.kind = STATEMENT_IF;
5030 statement->statement.source_position = token.source_position;
5033 statement->condition = parse_expression();
5036 statement->true_statement = parse_statement();
5037 if(token.type == T_else) {
5039 statement->false_statement = parse_statement();
5042 return (statement_t*) statement;
5046 * Parse a switch statement.
5048 static statement_t *parse_switch(void)
5052 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5053 statement->statement.kind = STATEMENT_SWITCH;
5054 statement->statement.source_position = token.source_position;
5057 expression_t *const expr = parse_expression();
5058 type_t *const type = promote_integer(skip_typeref(expr->base.datatype));
5059 statement->expression = create_implicit_cast(expr, type);
5062 switch_statement_t *rem = current_switch;
5063 current_switch = statement;
5064 statement->body = parse_statement();
5065 current_switch = rem;
5067 return (statement_t*) statement;
5071 * Parse a while statement.
5073 static statement_t *parse_while(void)
5077 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5078 statement->statement.kind = STATEMENT_WHILE;
5079 statement->statement.source_position = token.source_position;
5081 statement_t *rem = current_loop;
5083 statement->condition = parse_expression();
5086 statement->body = parse_statement();
5089 return (statement_t*) statement;
5096 * Parse a do statement.
5098 static statement_t *parse_do(void)
5102 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5103 statement->statement.kind = STATEMENT_DO_WHILE;
5104 statement->statement.source_position = token.source_position;
5106 statement_t *rem = current_loop;
5107 statement->body = parse_statement();
5108 expect_fail(T_while);
5110 statement->condition = parse_expression();
5115 return (statement_t*) statement;
5122 * Parse a for statement.
5124 static statement_t *parse_for(void)
5128 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5129 statement->statement.kind = STATEMENT_FOR;
5130 statement->statement.source_position = token.source_position;
5134 statement_t *rem = current_loop;
5135 int top = environment_top();
5136 context_t *last_context = context;
5137 set_context(&statement->context);
5139 if(token.type != ';') {
5140 if(is_declaration_specifier(&token, false)) {
5141 parse_declaration(record_declaration);
5143 statement->initialisation = parse_expression();
5150 if(token.type != ';') {
5151 statement->condition = parse_expression();
5154 if(token.type != ')') {
5155 statement->step = parse_expression();
5158 statement->body = parse_statement();
5160 assert(context == &statement->context);
5161 set_context(last_context);
5162 environment_pop_to(top);
5165 return (statement_t*) statement;
5172 * Parse a goto statement.
5174 static statement_t *parse_goto(void)
5178 if(token.type != T_IDENTIFIER) {
5179 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5183 symbol_t *symbol = token.v.symbol;
5186 declaration_t *label = get_label(symbol);
5188 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5190 statement->statement.kind = STATEMENT_GOTO;
5191 statement->statement.source_position = token.source_position;
5193 statement->label = label;
5195 /* remember the goto's in a list for later checking */
5196 statement->next = goto_list;
5197 goto_list = statement;
5201 return (statement_t*) statement;
5205 * Parse a continue statement.
5207 static statement_t *parse_continue(void)
5212 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5213 statement->kind = STATEMENT_CONTINUE;
5214 statement->base.source_position = token.source_position;
5216 if (current_loop == NULL) {
5217 errorf(HERE, "continue statement not within loop");
5225 * Parse a break statement.
5227 static statement_t *parse_break(void)
5232 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5233 statement->kind = STATEMENT_BREAK;
5234 statement->base.source_position = token.source_position;
5236 if (current_switch == NULL && current_loop == NULL) {
5237 errorf(HERE, "break statement not within loop or switch");
5244 * Check if a given declaration represents a local variable.
5246 static bool is_local_var_declaration(const declaration_t *declaration) {
5247 switch ((storage_class_tag_t) declaration->storage_class) {
5248 case STORAGE_CLASS_NONE:
5249 case STORAGE_CLASS_AUTO:
5250 case STORAGE_CLASS_REGISTER: {
5251 const type_t *type = skip_typeref(declaration->type);
5252 if(is_type_function(type)) {
5264 * Check if a given expression represents a local variable.
5266 static bool is_local_variable(const expression_t *expression)
5268 if (expression->base.kind != EXPR_REFERENCE) {
5271 const declaration_t *declaration = expression->reference.declaration;
5272 return is_local_var_declaration(declaration);
5276 * Parse a return statement.
5278 static statement_t *parse_return(void)
5282 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5284 statement->statement.kind = STATEMENT_RETURN;
5285 statement->statement.source_position = token.source_position;
5287 assert(is_type_function(current_function->type));
5288 function_type_t *function_type = ¤t_function->type->function;
5289 type_t *return_type = function_type->return_type;
5291 expression_t *return_value = NULL;
5292 if(token.type != ';') {
5293 return_value = parse_expression();
5297 if(return_type == NULL)
5298 return (statement_t*) statement;
5299 if(return_value != NULL && return_value->base.datatype == NULL)
5300 return (statement_t*) statement;
5302 return_type = skip_typeref(return_type);
5304 if(return_value != NULL) {
5305 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5307 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5308 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5309 warningf(statement->statement.source_position,
5310 "'return' with a value, in function returning void");
5311 return_value = NULL;
5313 if(return_type != NULL) {
5314 semantic_assign(return_type, &return_value, "'return'");
5317 /* check for returning address of a local var */
5318 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5319 const expression_t *expression = return_value->unary.value;
5320 if (is_local_variable(expression)) {
5321 warningf(statement->statement.source_position,
5322 "function returns address of local variable");
5326 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5327 warningf(statement->statement.source_position,
5328 "'return' without value, in function returning non-void");
5331 statement->return_value = return_value;
5333 return (statement_t*) statement;
5337 * Parse a declaration statement.
5339 static statement_t *parse_declaration_statement(void)
5341 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5343 statement->base.source_position = token.source_position;
5345 declaration_t *before = last_declaration;
5346 parse_declaration(record_declaration);
5348 if(before == NULL) {
5349 statement->declaration.declarations_begin = context->declarations;
5351 statement->declaration.declarations_begin = before->next;
5353 statement->declaration.declarations_end = last_declaration;
5359 * Parse an expression statement, ie. expr ';'.
5361 static statement_t *parse_expression_statement(void)
5363 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5365 statement->base.source_position = token.source_position;
5366 statement->expression.expression = parse_expression();
5374 * Parse a statement.
5376 static statement_t *parse_statement(void)
5378 statement_t *statement = NULL;
5380 /* declaration or statement */
5381 switch(token.type) {
5383 statement = parse_asm_statement();
5387 statement = parse_case_statement();
5391 statement = parse_default_statement();
5395 statement = parse_compound_statement();
5399 statement = parse_if();
5403 statement = parse_switch();
5407 statement = parse_while();
5411 statement = parse_do();
5415 statement = parse_for();
5419 statement = parse_goto();
5423 statement = parse_continue();
5427 statement = parse_break();
5431 statement = parse_return();
5440 if(look_ahead(1)->type == ':') {
5441 statement = parse_label_statement();
5445 if(is_typedef_symbol(token.v.symbol)) {
5446 statement = parse_declaration_statement();
5450 statement = parse_expression_statement();
5453 case T___extension__:
5454 /* this can be a prefix to a declaration or an expression statement */
5455 /* we simply eat it now and parse the rest with tail recursion */
5458 } while(token.type == T___extension__);
5459 statement = parse_statement();
5463 statement = parse_declaration_statement();
5467 statement = parse_expression_statement();
5471 assert(statement == NULL
5472 || statement->base.source_position.input_name != NULL);
5478 * Parse a compound statement.
5480 static statement_t *parse_compound_statement(void)
5482 compound_statement_t *compound_statement
5483 = allocate_ast_zero(sizeof(compound_statement[0]));
5484 compound_statement->statement.kind = STATEMENT_COMPOUND;
5485 compound_statement->statement.source_position = token.source_position;
5489 int top = environment_top();
5490 context_t *last_context = context;
5491 set_context(&compound_statement->context);
5493 statement_t *last_statement = NULL;
5495 while(token.type != '}' && token.type != T_EOF) {
5496 statement_t *statement = parse_statement();
5497 if(statement == NULL)
5500 if(last_statement != NULL) {
5501 last_statement->base.next = statement;
5503 compound_statement->statements = statement;
5506 while(statement->base.next != NULL)
5507 statement = statement->base.next;
5509 last_statement = statement;
5512 if(token.type == '}') {
5515 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5518 assert(context == &compound_statement->context);
5519 set_context(last_context);
5520 environment_pop_to(top);
5522 return (statement_t*) compound_statement;
5526 * Initialize builtin types.
5528 static void initialize_builtin_types(void)
5530 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5531 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5532 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5533 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5534 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5535 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5536 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5537 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5539 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5540 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5541 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5542 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5546 * Parse a translation unit.
5548 static translation_unit_t *parse_translation_unit(void)
5550 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5552 assert(global_context == NULL);
5553 global_context = &unit->context;
5555 assert(context == NULL);
5556 set_context(&unit->context);
5558 initialize_builtin_types();
5560 while(token.type != T_EOF) {
5561 if (token.type == ';') {
5562 /* TODO error in strict mode */
5563 warningf(HERE, "stray ';' outside of function");
5566 parse_external_declaration();
5570 assert(context == &unit->context);
5572 last_declaration = NULL;
5574 assert(global_context == &unit->context);
5575 global_context = NULL;
5583 * @return the translation unit or NULL if errors occurred.
5585 translation_unit_t *parse(void)
5587 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5588 label_stack = NEW_ARR_F(stack_entry_t, 0);
5589 diagnostic_count = 0;
5593 type_set_output(stderr);
5594 ast_set_output(stderr);
5596 lookahead_bufpos = 0;
5597 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5600 translation_unit_t *unit = parse_translation_unit();
5602 DEL_ARR_F(environment_stack);
5603 DEL_ARR_F(label_stack);
5612 * Initialize the parser.
5614 void init_parser(void)
5616 init_expression_parsers();
5617 obstack_init(&temp_obst);
5619 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5620 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5624 * Terminate the parser.
5626 void exit_parser(void)
5628 obstack_free(&temp_obst, NULL);