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 struct obstack temp_obst;
55 /** The current source position. */
56 #define HERE token.source_position
58 static type_t *type_valist;
60 static statement_t *parse_compound_statement(void);
61 static statement_t *parse_statement(void);
63 static expression_t *parse_sub_expression(unsigned precedence);
64 static expression_t *parse_expression(void);
65 static type_t *parse_typename(void);
67 static void parse_compound_type_entries(void);
68 static declaration_t *parse_declarator(
69 const declaration_specifiers_t *specifiers, bool may_be_abstract);
70 static declaration_t *record_declaration(declaration_t *declaration);
72 static void semantic_comparison(binary_expression_t *expression);
74 #define STORAGE_CLASSES \
81 #define TYPE_QUALIFIERS \
88 #ifdef PROVIDE_COMPLEX
89 #define COMPLEX_SPECIFIERS \
91 #define IMAGINARY_SPECIFIERS \
94 #define COMPLEX_SPECIFIERS
95 #define IMAGINARY_SPECIFIERS
98 #define TYPE_SPECIFIERS \
113 case T___builtin_va_list: \
117 #define DECLARATION_START \
122 #define TYPENAME_START \
127 * Allocate an AST node with given size and
128 * initialize all fields with zero.
130 static void *allocate_ast_zero(size_t size)
132 void *res = allocate_ast(size);
133 memset(res, 0, size);
138 * Returns the size of a statement node.
140 * @param kind the statement kind
142 static size_t get_statement_struct_size(statement_kind_t kind)
144 static const size_t sizes[] = {
145 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
146 [STATEMENT_RETURN] = sizeof(return_statement_t),
147 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
148 [STATEMENT_IF] = sizeof(if_statement_t),
149 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
150 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
151 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
152 [STATEMENT_BREAK] = sizeof(statement_base_t),
153 [STATEMENT_GOTO] = sizeof(goto_statement_t),
154 [STATEMENT_LABEL] = sizeof(label_statement_t),
155 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
156 [STATEMENT_WHILE] = sizeof(while_statement_t),
157 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
158 [STATEMENT_FOR] = sizeof(for_statement_t),
159 [STATEMENT_ASM] = sizeof(asm_statement_t)
161 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
162 assert(sizes[kind] != 0);
167 * Allocate a statement node of given kind and initialize all
170 static statement_t *allocate_statement_zero(statement_kind_t kind)
172 size_t size = get_statement_struct_size(kind);
173 statement_t *res = allocate_ast_zero(size);
175 res->base.kind = kind;
180 * Returns the size of an expression node.
182 * @param kind the expression kind
184 static size_t get_expression_struct_size(expression_kind_t kind)
186 static const size_t sizes[] = {
187 [EXPR_INVALID] = sizeof(expression_base_t),
188 [EXPR_REFERENCE] = sizeof(reference_expression_t),
189 [EXPR_CONST] = sizeof(const_expression_t),
190 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
191 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
192 [EXPR_CALL] = sizeof(call_expression_t),
193 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
194 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
195 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
196 [EXPR_SELECT] = sizeof(select_expression_t),
197 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
198 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
199 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
200 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
201 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
202 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
203 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
204 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
205 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
206 [EXPR_VA_START] = sizeof(va_start_expression_t),
207 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
208 [EXPR_STATEMENT] = sizeof(statement_expression_t),
210 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
211 return sizes[EXPR_UNARY_FIRST];
213 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
214 return sizes[EXPR_BINARY_FIRST];
216 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
217 assert(sizes[kind] != 0);
222 * Allocate an expression node of given kind and initialize all
225 static expression_t *allocate_expression_zero(expression_kind_t kind)
227 size_t size = get_expression_struct_size(kind);
228 expression_t *res = allocate_ast_zero(size);
230 res->base.kind = kind;
235 * Returns the size of a type node.
237 * @param kind the type kind
239 static size_t get_type_struct_size(type_kind_t kind)
241 static const size_t sizes[] = {
242 [TYPE_ATOMIC] = sizeof(atomic_type_t),
243 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
244 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
245 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
246 [TYPE_ENUM] = sizeof(enum_type_t),
247 [TYPE_FUNCTION] = sizeof(function_type_t),
248 [TYPE_POINTER] = sizeof(pointer_type_t),
249 [TYPE_ARRAY] = sizeof(array_type_t),
250 [TYPE_BUILTIN] = sizeof(builtin_type_t),
251 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
252 [TYPE_TYPEOF] = sizeof(typeof_type_t),
254 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
255 assert(kind <= TYPE_TYPEOF);
256 assert(sizes[kind] != 0);
261 * Allocate a type node of given kind and initialize all
264 static type_t *allocate_type_zero(type_kind_t kind)
266 size_t size = get_type_struct_size(kind);
267 type_t *res = obstack_alloc(type_obst, size);
268 memset(res, 0, size);
270 res->base.kind = kind;
275 * Returns the size of an initializer node.
277 * @param kind the initializer kind
279 static size_t get_initializer_size(initializer_kind_t kind)
281 static const size_t sizes[] = {
282 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
283 [INITIALIZER_STRING] = sizeof(initializer_string_t),
284 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
285 [INITIALIZER_LIST] = sizeof(initializer_list_t)
287 assert(kind < sizeof(sizes) / sizeof(*sizes));
288 assert(sizes[kind] != 0);
293 * Allocate an initializer node of given kind and initialize all
296 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
298 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
305 * Free a type from the type obstack.
307 static void free_type(void *type)
309 obstack_free(type_obst, type);
313 * Returns the index of the top element of the environment stack.
315 static size_t environment_top(void)
317 return ARR_LEN(environment_stack);
321 * Returns the index of the top element of the label stack.
323 static size_t label_top(void)
325 return ARR_LEN(label_stack);
330 * Return the next token.
332 static inline void next_token(void)
334 token = lookahead_buffer[lookahead_bufpos];
335 lookahead_buffer[lookahead_bufpos] = lexer_token;
338 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
341 print_token(stderr, &token);
342 fprintf(stderr, "\n");
347 * Return the next token with a given lookahead.
349 static inline const token_t *look_ahead(int num)
351 assert(num > 0 && num <= MAX_LOOKAHEAD);
352 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
353 return &lookahead_buffer[pos];
356 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
359 * Report a parse error because an expected token was not found.
361 static void parse_error_expected(const char *message, ...)
363 if(message != NULL) {
364 errorf(HERE, "%s", message);
367 va_start(ap, message);
368 errorf(HERE, "got '%K', expected %#k", &token, &ap, ", ");
373 * Report a type error.
375 static void type_error(const char *msg, const source_position_t source_position,
378 errorf(source_position, "%s, but found type '%T'", msg, type);
382 * Report an incompatible type.
384 static void type_error_incompatible(const char *msg,
385 const source_position_t source_position, type_t *type1, type_t *type2)
387 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
391 * Eat an complete block, ie. '{ ... }'.
393 static void eat_block(void)
395 if(token.type == '{')
398 while(token.type != '}') {
399 if(token.type == T_EOF)
401 if(token.type == '{') {
411 * Eat a statement until an ';' token.
413 static void eat_statement(void)
415 while(token.type != ';') {
416 if(token.type == T_EOF)
418 if(token.type == '}')
420 if(token.type == '{') {
430 * Eat a parenthesed term, ie. '( ... )'.
432 static void eat_paren(void)
434 if(token.type == '(')
437 while(token.type != ')') {
438 if(token.type == T_EOF)
440 if(token.type == ')' || token.type == ';' || token.type == '}') {
443 if(token.type == '(') {
447 if(token.type == '{') {
456 #define expect(expected) \
457 if(UNLIKELY(token.type != (expected))) { \
458 parse_error_expected(NULL, (expected), 0); \
464 #define expect_fail(expected) \
465 if(UNLIKELY(token.type != (expected))) { \
466 parse_error_expected(NULL, (expected), 0); \
472 #define expect_block(expected) \
473 if(UNLIKELY(token.type != (expected))) { \
474 parse_error_expected(NULL, (expected), 0); \
480 #define expect_void(expected) \
481 if(UNLIKELY(token.type != (expected))) { \
482 parse_error_expected(NULL, (expected), 0); \
488 static void set_context(context_t *new_context)
490 context = new_context;
492 last_declaration = new_context->declarations;
493 if(last_declaration != NULL) {
494 while(last_declaration->next != NULL) {
495 last_declaration = last_declaration->next;
501 * Search a symbol in a given namespace and returns its declaration or
502 * NULL if this symbol was not found.
504 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
506 declaration_t *declaration = symbol->declaration;
507 for( ; declaration != NULL; declaration = declaration->symbol_next) {
508 if(declaration->namespc == namespc)
516 * pushs an environment_entry on the environment stack and links the
517 * corresponding symbol to the new entry
519 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
521 symbol_t *symbol = declaration->symbol;
522 namespace_t namespc = (namespace_t)declaration->namespc;
524 /* remember old declaration */
526 entry.symbol = symbol;
527 entry.old_declaration = symbol->declaration;
528 entry.namespc = (unsigned short) namespc;
529 ARR_APP1(stack_entry_t, *stack_ptr, entry);
531 /* replace/add declaration into declaration list of the symbol */
532 if(symbol->declaration == NULL) {
533 symbol->declaration = declaration;
535 declaration_t *iter_last = NULL;
536 declaration_t *iter = symbol->declaration;
537 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
538 /* replace an entry? */
539 if(iter->namespc == namespc) {
540 if(iter_last == NULL) {
541 symbol->declaration = declaration;
543 iter_last->symbol_next = declaration;
545 declaration->symbol_next = iter->symbol_next;
550 assert(iter_last->symbol_next == NULL);
551 iter_last->symbol_next = declaration;
556 static void environment_push(declaration_t *declaration)
558 assert(declaration->source_position.input_name != NULL);
559 assert(declaration->parent_context != NULL);
560 stack_push(&environment_stack, declaration);
563 static void label_push(declaration_t *declaration)
565 declaration->parent_context = ¤t_function->context;
566 stack_push(&label_stack, declaration);
570 * pops symbols from the environment stack until @p new_top is the top element
572 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
574 stack_entry_t *stack = *stack_ptr;
575 size_t top = ARR_LEN(stack);
578 assert(new_top <= top);
582 for(i = top; i > new_top; --i) {
583 stack_entry_t *entry = &stack[i - 1];
585 declaration_t *old_declaration = entry->old_declaration;
586 symbol_t *symbol = entry->symbol;
587 namespace_t namespc = (namespace_t)entry->namespc;
589 /* replace/remove declaration */
590 declaration_t *declaration = symbol->declaration;
591 assert(declaration != NULL);
592 if(declaration->namespc == namespc) {
593 if(old_declaration == NULL) {
594 symbol->declaration = declaration->symbol_next;
596 symbol->declaration = old_declaration;
599 declaration_t *iter_last = declaration;
600 declaration_t *iter = declaration->symbol_next;
601 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
602 /* replace an entry? */
603 if(iter->namespc == namespc) {
604 assert(iter_last != NULL);
605 iter_last->symbol_next = old_declaration;
606 old_declaration->symbol_next = iter->symbol_next;
610 assert(iter != NULL);
614 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
617 static void environment_pop_to(size_t new_top)
619 stack_pop_to(&environment_stack, new_top);
622 static void label_pop_to(size_t new_top)
624 stack_pop_to(&label_stack, new_top);
628 static int get_rank(const type_t *type)
630 assert(!is_typeref(type));
631 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
632 * and esp. footnote 108). However we can't fold constants (yet), so we
633 * can't decide whether unsigned int is possible, while int always works.
634 * (unsigned int would be preferable when possible... for stuff like
635 * struct { enum { ... } bla : 4; } ) */
636 if(type->kind == TYPE_ENUM)
637 return ATOMIC_TYPE_INT;
639 assert(type->kind == TYPE_ATOMIC);
640 const atomic_type_t *atomic_type = &type->atomic;
641 atomic_type_kind_t atype = atomic_type->akind;
645 static type_t *promote_integer(type_t *type)
647 if(type->kind == TYPE_BITFIELD)
648 return promote_integer(type->bitfield.base);
650 if(get_rank(type) < ATOMIC_TYPE_INT)
657 * Create a cast expression.
659 * @param expression the expression to cast
660 * @param dest_type the destination type
662 static expression_t *create_cast_expression(expression_t *expression,
665 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
667 cast->unary.value = expression;
668 cast->base.datatype = dest_type;
674 * Check if a given expression represents the 0 pointer constant.
676 static bool is_null_pointer_constant(const expression_t *expression)
678 /* skip void* cast */
679 if(expression->kind == EXPR_UNARY_CAST
680 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
681 expression = expression->unary.value;
684 /* TODO: not correct yet, should be any constant integer expression
685 * which evaluates to 0 */
686 if (expression->kind != EXPR_CONST)
689 type_t *const type = skip_typeref(expression->base.datatype);
690 if (!is_type_integer(type))
693 return expression->conste.v.int_value == 0;
697 * Create an implicit cast expression.
699 * @param expression the expression to cast
700 * @param dest_type the destination type
702 static expression_t *create_implicit_cast(expression_t *expression,
705 type_t *source_type = expression->base.datatype;
707 if(source_type == NULL)
710 source_type = skip_typeref(source_type);
711 dest_type = skip_typeref(dest_type);
713 if(source_type == dest_type)
716 switch (dest_type->kind) {
718 /* TODO warning for implicitly converting to enum */
721 if (source_type->kind != TYPE_ATOMIC &&
722 source_type->kind != TYPE_ENUM &&
723 source_type->kind != TYPE_BITFIELD) {
724 panic("casting of non-atomic types not implemented yet");
727 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
728 type_error_incompatible("can't cast types",
729 expression->base.source_position, source_type,
734 return create_cast_expression(expression, dest_type);
737 switch (source_type->kind) {
739 if (is_null_pointer_constant(expression)) {
740 return create_cast_expression(expression, dest_type);
745 if (pointers_compatible(source_type, dest_type)) {
746 return create_cast_expression(expression, dest_type);
751 array_type_t *array_type = &source_type->array;
752 pointer_type_t *pointer_type = &dest_type->pointer;
753 if (types_compatible(array_type->element_type,
754 pointer_type->points_to)) {
755 return create_cast_expression(expression, dest_type);
761 panic("casting of non-atomic types not implemented yet");
764 type_error_incompatible("can't implicitly cast types",
765 expression->base.source_position, source_type, dest_type);
769 panic("casting of non-atomic types not implemented yet");
773 /** Implements the rules from § 6.5.16.1 */
774 static void semantic_assign(type_t *orig_type_left, expression_t **right,
777 type_t *orig_type_right = (*right)->base.datatype;
779 if(orig_type_right == NULL)
782 type_t *const type_left = skip_typeref(orig_type_left);
783 type_t *const type_right = skip_typeref(orig_type_right);
785 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
786 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
787 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
788 && is_type_pointer(type_right))) {
789 *right = create_implicit_cast(*right, type_left);
793 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
794 pointer_type_t *pointer_type_left = &type_left->pointer;
795 pointer_type_t *pointer_type_right = &type_right->pointer;
796 type_t *points_to_left = pointer_type_left->points_to;
797 type_t *points_to_right = pointer_type_right->points_to;
799 points_to_left = skip_typeref(points_to_left);
800 points_to_right = skip_typeref(points_to_right);
802 /* the left type has all qualifiers from the right type */
803 unsigned missing_qualifiers
804 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
805 if(missing_qualifiers != 0) {
806 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
810 points_to_left = get_unqualified_type(points_to_left);
811 points_to_right = get_unqualified_type(points_to_right);
813 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
814 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
815 && !types_compatible(points_to_left, points_to_right)) {
816 goto incompatible_assign_types;
819 *right = create_implicit_cast(*right, type_left);
823 if (is_type_compound(type_left)
824 && types_compatible(type_left, type_right)) {
825 *right = create_implicit_cast(*right, type_left);
829 incompatible_assign_types:
830 /* TODO: improve error message */
831 errorf(HERE, "incompatible types in %s", context);
832 errorf(HERE, "'%T' <- '%T'", orig_type_left, orig_type_right);
835 static expression_t *parse_constant_expression(void)
837 /* start parsing at precedence 7 (conditional expression) */
838 expression_t *result = parse_sub_expression(7);
840 if(!is_constant_expression(result)) {
841 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
847 static expression_t *parse_assignment_expression(void)
849 /* start parsing at precedence 2 (assignment expression) */
850 return parse_sub_expression(2);
853 static type_t *make_global_typedef(const char *name, type_t *type)
855 symbol_t *const symbol = symbol_table_insert(name);
857 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
858 declaration->namespc = NAMESPACE_NORMAL;
859 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
860 declaration->type = type;
861 declaration->symbol = symbol;
862 declaration->source_position = builtin_source_position;
864 record_declaration(declaration);
866 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
867 typedef_type->typedeft.declaration = declaration;
872 static const char *parse_string_literals(void)
874 assert(token.type == T_STRING_LITERAL);
875 const char *result = token.v.string;
879 while(token.type == T_STRING_LITERAL) {
880 result = concat_strings(result, token.v.string);
887 static void parse_attributes(void)
891 case T___attribute__: {
899 errorf(HERE, "EOF while parsing attribute");
918 if(token.type != T_STRING_LITERAL) {
919 parse_error_expected("while parsing assembler attribute",
924 parse_string_literals();
929 goto attributes_finished;
938 static designator_t *parse_designation(void)
940 if(token.type != '[' && token.type != '.')
943 designator_t *result = NULL;
944 designator_t *last = NULL;
947 designator_t *designator;
950 designator = allocate_ast_zero(sizeof(designator[0]));
952 designator->array_access = parse_constant_expression();
956 designator = allocate_ast_zero(sizeof(designator[0]));
958 if(token.type != T_IDENTIFIER) {
959 parse_error_expected("while parsing designator",
963 designator->symbol = token.v.symbol;
971 assert(designator != NULL);
973 last->next = designator;
982 static initializer_t *initializer_from_string(array_type_t *type,
985 /* TODO: check len vs. size of array type */
988 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
989 initializer->string.string = string;
994 static initializer_t *initializer_from_wide_string(array_type_t *const type,
995 wide_string_t *const string)
997 /* TODO: check len vs. size of array type */
1000 initializer_t *const initializer =
1001 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1002 initializer->wide_string.string = *string;
1007 static initializer_t *initializer_from_expression(type_t *type,
1008 expression_t *expression)
1010 /* TODO check that expression is a constant expression */
1012 /* § 6.7.8.14/15 char array may be initialized by string literals */
1013 type_t *const expr_type = expression->base.datatype;
1014 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1015 array_type_t *const array_type = &type->array;
1016 type_t *const element_type = skip_typeref(array_type->element_type);
1018 if (element_type->kind == TYPE_ATOMIC) {
1019 switch (expression->kind) {
1020 case EXPR_STRING_LITERAL:
1021 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
1022 return initializer_from_string(array_type,
1023 expression->string.value);
1026 case EXPR_WIDE_STRING_LITERAL: {
1027 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1028 if (get_unqualified_type(element_type) == bare_wchar_type) {
1029 return initializer_from_wide_string(array_type,
1030 &expression->wide_string.value);
1040 type_t *expression_type = skip_typeref(expression->base.datatype);
1041 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1042 semantic_assign(type, &expression, "initializer");
1044 initializer_t *result = allocate_initializer_zero(INITIALIZER_VALUE);
1045 result->value.value = expression;
1053 static initializer_t *parse_sub_initializer(type_t *type,
1054 expression_t *expression,
1055 type_t *expression_type);
1057 static initializer_t *parse_sub_initializer_elem(type_t *type)
1059 if(token.type == '{') {
1060 return parse_sub_initializer(type, NULL, NULL);
1063 expression_t *expression = parse_assignment_expression();
1064 type_t *expression_type = skip_typeref(expression->base.datatype);
1066 return parse_sub_initializer(type, expression, expression_type);
1069 static bool had_initializer_brace_warning;
1071 static void skip_designator(void)
1074 if(token.type == '.') {
1076 if(token.type == T_IDENTIFIER)
1078 } else if(token.type == '[') {
1080 parse_constant_expression();
1081 if(token.type == ']')
1089 static initializer_t *parse_sub_initializer(type_t *type,
1090 expression_t *expression,
1091 type_t *expression_type)
1093 if(is_type_scalar(type)) {
1094 /* there might be extra {} hierarchies */
1095 if(token.type == '{') {
1097 if(!had_initializer_brace_warning) {
1098 warningf(HERE, "braces around scalar initializer");
1099 had_initializer_brace_warning = true;
1101 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1102 if(token.type == ',') {
1104 /* TODO: warn about excessive elements */
1110 if(expression == NULL) {
1111 expression = parse_assignment_expression();
1113 return initializer_from_expression(type, expression);
1116 /* does the expression match the currently looked at object to initialize */
1117 if(expression != NULL) {
1118 initializer_t *result = initializer_from_expression(type, expression);
1123 bool read_paren = false;
1124 if(token.type == '{') {
1129 /* descend into subtype */
1130 initializer_t *result = NULL;
1131 initializer_t **elems;
1132 if(is_type_array(type)) {
1133 array_type_t *array_type = &type->array;
1134 type_t *element_type = array_type->element_type;
1135 element_type = skip_typeref(element_type);
1137 if(token.type == '.') {
1139 "compound designator in initializer for array type '%T'",
1145 had_initializer_brace_warning = false;
1146 if(expression == NULL) {
1147 sub = parse_sub_initializer_elem(element_type);
1149 sub = parse_sub_initializer(element_type, expression,
1153 /* didn't match the subtypes -> try the parent type */
1155 assert(!read_paren);
1159 elems = NEW_ARR_F(initializer_t*, 0);
1160 ARR_APP1(initializer_t*, elems, sub);
1163 if(token.type == '}')
1166 if(token.type == '}')
1169 sub = parse_sub_initializer_elem(element_type);
1171 /* TODO error, do nicer cleanup */
1172 errorf(HERE, "member initializer didn't match");
1176 ARR_APP1(initializer_t*, elems, sub);
1179 assert(is_type_compound(type));
1180 compound_type_t *compound_type = &type->compound;
1181 context_t *context = &compound_type->declaration->context;
1183 if(token.type == '[') {
1185 "array designator in initializer for compound type '%T'",
1190 declaration_t *first = context->declarations;
1193 type_t *first_type = first->type;
1194 first_type = skip_typeref(first_type);
1197 had_initializer_brace_warning = false;
1198 if(expression == NULL) {
1199 sub = parse_sub_initializer_elem(first_type);
1201 sub = parse_sub_initializer(first_type, expression,expression_type);
1204 /* didn't match the subtypes -> try our parent type */
1206 assert(!read_paren);
1210 elems = NEW_ARR_F(initializer_t*, 0);
1211 ARR_APP1(initializer_t*, elems, sub);
1213 declaration_t *iter = first->next;
1214 for( ; iter != NULL; iter = iter->next) {
1215 if(iter->symbol == NULL)
1217 if(iter->namespc != NAMESPACE_NORMAL)
1220 if(token.type == '}')
1223 if(token.type == '}')
1226 type_t *iter_type = iter->type;
1227 iter_type = skip_typeref(iter_type);
1229 sub = parse_sub_initializer_elem(iter_type);
1231 /* TODO error, do nicer cleanup */
1232 errorf(HERE, "member initializer didn't match");
1236 ARR_APP1(initializer_t*, elems, sub);
1240 int len = ARR_LEN(elems);
1241 size_t elems_size = sizeof(initializer_t*) * len;
1243 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1245 init->initializer.kind = INITIALIZER_LIST;
1247 memcpy(init->initializers, elems, elems_size);
1250 result = (initializer_t*) init;
1253 if(token.type == ',')
1260 static initializer_t *parse_initializer(type_t *type)
1262 initializer_t *result;
1264 type = skip_typeref(type);
1266 if(token.type != '{') {
1267 expression_t *expression = parse_assignment_expression();
1268 initializer_t *initializer = initializer_from_expression(type, expression);
1269 if(initializer == NULL) {
1270 errorf(HERE, "initializer expression '%E', type '%T' is incompatible with type '%T'", expression, expression->base.datatype, type);
1275 if(is_type_scalar(type)) {
1279 expression_t *expression = parse_assignment_expression();
1280 result = initializer_from_expression(type, expression);
1282 if(token.type == ',')
1288 result = parse_sub_initializer(type, NULL, NULL);
1294 static declaration_t *append_declaration(declaration_t *declaration);
1296 static declaration_t *parse_compound_type_specifier(bool is_struct)
1304 symbol_t *symbol = NULL;
1305 declaration_t *declaration = NULL;
1307 if (token.type == T___attribute__) {
1312 if(token.type == T_IDENTIFIER) {
1313 symbol = token.v.symbol;
1317 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1319 declaration = get_declaration(symbol, NAMESPACE_UNION);
1321 } else if(token.type != '{') {
1323 parse_error_expected("while parsing struct type specifier",
1324 T_IDENTIFIER, '{', 0);
1326 parse_error_expected("while parsing union type specifier",
1327 T_IDENTIFIER, '{', 0);
1333 if(declaration == NULL) {
1334 declaration = allocate_ast_zero(sizeof(declaration[0]));
1337 declaration->namespc = NAMESPACE_STRUCT;
1339 declaration->namespc = NAMESPACE_UNION;
1341 declaration->source_position = token.source_position;
1342 declaration->symbol = symbol;
1343 declaration->parent_context = context;
1344 if (symbol != NULL) {
1345 environment_push(declaration);
1347 append_declaration(declaration);
1350 if(token.type == '{') {
1351 if(declaration->init.is_defined) {
1352 assert(symbol != NULL);
1353 errorf(HERE, "multiple definition of '%s %Y'",
1354 is_struct ? "struct" : "union", symbol);
1355 declaration->context.declarations = NULL;
1357 declaration->init.is_defined = true;
1359 int top = environment_top();
1360 context_t *last_context = context;
1361 set_context(&declaration->context);
1363 parse_compound_type_entries();
1366 assert(context == &declaration->context);
1367 set_context(last_context);
1368 environment_pop_to(top);
1374 static void parse_enum_entries(enum_type_t *const enum_type)
1378 if(token.type == '}') {
1380 errorf(HERE, "empty enum not allowed");
1385 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1387 if(token.type != T_IDENTIFIER) {
1388 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1392 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1393 entry->type = (type_t*) enum_type;
1394 entry->symbol = token.v.symbol;
1395 entry->source_position = token.source_position;
1398 if(token.type == '=') {
1400 entry->init.enum_value = parse_constant_expression();
1405 record_declaration(entry);
1407 if(token.type != ',')
1410 } while(token.type != '}');
1415 static type_t *parse_enum_specifier(void)
1419 declaration_t *declaration;
1422 if(token.type == T_IDENTIFIER) {
1423 symbol = token.v.symbol;
1426 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1427 } else if(token.type != '{') {
1428 parse_error_expected("while parsing enum type specifier",
1429 T_IDENTIFIER, '{', 0);
1436 if(declaration == NULL) {
1437 declaration = allocate_ast_zero(sizeof(declaration[0]));
1439 declaration->namespc = NAMESPACE_ENUM;
1440 declaration->source_position = token.source_position;
1441 declaration->symbol = symbol;
1442 declaration->parent_context = context;
1445 type_t *const type = allocate_type_zero(TYPE_ENUM);
1446 type->enumt.declaration = declaration;
1448 if(token.type == '{') {
1449 if(declaration->init.is_defined) {
1450 errorf(HERE, "multiple definitions of enum %Y", symbol);
1452 if (symbol != NULL) {
1453 environment_push(declaration);
1455 append_declaration(declaration);
1456 declaration->init.is_defined = 1;
1458 parse_enum_entries(&type->enumt);
1466 * if a symbol is a typedef to another type, return true
1468 static bool is_typedef_symbol(symbol_t *symbol)
1470 const declaration_t *const declaration =
1471 get_declaration(symbol, NAMESPACE_NORMAL);
1473 declaration != NULL &&
1474 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1477 static type_t *parse_typeof(void)
1485 expression_t *expression = NULL;
1488 switch(token.type) {
1489 case T___extension__:
1490 /* this can be a prefix to a typename or an expression */
1491 /* we simply eat it now. */
1494 } while(token.type == T___extension__);
1498 if(is_typedef_symbol(token.v.symbol)) {
1499 type = parse_typename();
1501 expression = parse_expression();
1502 type = expression->base.datatype;
1507 type = parse_typename();
1511 expression = parse_expression();
1512 type = expression->base.datatype;
1518 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1519 typeof_type->typeoft.expression = expression;
1520 typeof_type->typeoft.typeof_type = type;
1526 SPECIFIER_SIGNED = 1 << 0,
1527 SPECIFIER_UNSIGNED = 1 << 1,
1528 SPECIFIER_LONG = 1 << 2,
1529 SPECIFIER_INT = 1 << 3,
1530 SPECIFIER_DOUBLE = 1 << 4,
1531 SPECIFIER_CHAR = 1 << 5,
1532 SPECIFIER_SHORT = 1 << 6,
1533 SPECIFIER_LONG_LONG = 1 << 7,
1534 SPECIFIER_FLOAT = 1 << 8,
1535 SPECIFIER_BOOL = 1 << 9,
1536 SPECIFIER_VOID = 1 << 10,
1537 #ifdef PROVIDE_COMPLEX
1538 SPECIFIER_COMPLEX = 1 << 11,
1539 SPECIFIER_IMAGINARY = 1 << 12,
1543 static type_t *create_builtin_type(symbol_t *const symbol,
1544 type_t *const real_type)
1546 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1547 type->builtin.symbol = symbol;
1548 type->builtin.real_type = real_type;
1550 type_t *result = typehash_insert(type);
1551 if (type != result) {
1558 static type_t *get_typedef_type(symbol_t *symbol)
1560 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1561 if(declaration == NULL
1562 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1565 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1566 type->typedeft.declaration = declaration;
1571 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1573 type_t *type = NULL;
1574 unsigned type_qualifiers = 0;
1575 unsigned type_specifiers = 0;
1578 specifiers->source_position = token.source_position;
1581 switch(token.type) {
1584 #define MATCH_STORAGE_CLASS(token, class) \
1586 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1587 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1589 specifiers->storage_class = class; \
1593 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1594 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1595 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1596 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1597 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1600 switch (specifiers->storage_class) {
1601 case STORAGE_CLASS_NONE:
1602 specifiers->storage_class = STORAGE_CLASS_THREAD;
1605 case STORAGE_CLASS_EXTERN:
1606 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1609 case STORAGE_CLASS_STATIC:
1610 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1614 errorf(HERE, "multiple storage classes in declaration specifiers");
1620 /* type qualifiers */
1621 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1623 type_qualifiers |= qualifier; \
1627 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1628 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1629 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1631 case T___extension__:
1636 /* type specifiers */
1637 #define MATCH_SPECIFIER(token, specifier, name) \
1640 if(type_specifiers & specifier) { \
1641 errorf(HERE, "multiple " name " type specifiers given"); \
1643 type_specifiers |= specifier; \
1647 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1648 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1649 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1650 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1651 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1652 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1653 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1654 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1655 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1656 #ifdef PROVIDE_COMPLEX
1657 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1658 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1661 /* only in microsoft mode */
1662 specifiers->decl_modifiers |= DM_FORCEINLINE;
1666 specifiers->is_inline = true;
1671 if(type_specifiers & SPECIFIER_LONG_LONG) {
1672 errorf(HERE, "multiple type specifiers given");
1673 } else if(type_specifiers & SPECIFIER_LONG) {
1674 type_specifiers |= SPECIFIER_LONG_LONG;
1676 type_specifiers |= SPECIFIER_LONG;
1680 /* TODO: if type != NULL for the following rules should issue
1683 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1685 type->compound.declaration = parse_compound_type_specifier(true);
1689 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1691 type->compound.declaration = parse_compound_type_specifier(false);
1695 type = parse_enum_specifier();
1698 type = parse_typeof();
1700 case T___builtin_va_list:
1701 type = duplicate_type(type_valist);
1705 case T___attribute__:
1710 case T_IDENTIFIER: {
1711 type_t *typedef_type = get_typedef_type(token.v.symbol);
1713 if(typedef_type == NULL)
1714 goto finish_specifiers;
1717 type = typedef_type;
1721 /* function specifier */
1723 goto finish_specifiers;
1730 atomic_type_kind_t atomic_type;
1732 /* match valid basic types */
1733 switch(type_specifiers) {
1734 case SPECIFIER_VOID:
1735 atomic_type = ATOMIC_TYPE_VOID;
1737 case SPECIFIER_CHAR:
1738 atomic_type = ATOMIC_TYPE_CHAR;
1740 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1741 atomic_type = ATOMIC_TYPE_SCHAR;
1743 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1744 atomic_type = ATOMIC_TYPE_UCHAR;
1746 case SPECIFIER_SHORT:
1747 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1748 case SPECIFIER_SHORT | SPECIFIER_INT:
1749 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1750 atomic_type = ATOMIC_TYPE_SHORT;
1752 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1753 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1754 atomic_type = ATOMIC_TYPE_USHORT;
1757 case SPECIFIER_SIGNED:
1758 case SPECIFIER_SIGNED | SPECIFIER_INT:
1759 atomic_type = ATOMIC_TYPE_INT;
1761 case SPECIFIER_UNSIGNED:
1762 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1763 atomic_type = ATOMIC_TYPE_UINT;
1765 case SPECIFIER_LONG:
1766 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1767 case SPECIFIER_LONG | SPECIFIER_INT:
1768 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1769 atomic_type = ATOMIC_TYPE_LONG;
1771 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1772 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1773 atomic_type = ATOMIC_TYPE_ULONG;
1775 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1776 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1777 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1778 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1780 atomic_type = ATOMIC_TYPE_LONGLONG;
1782 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1783 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1785 atomic_type = ATOMIC_TYPE_ULONGLONG;
1787 case SPECIFIER_FLOAT:
1788 atomic_type = ATOMIC_TYPE_FLOAT;
1790 case SPECIFIER_DOUBLE:
1791 atomic_type = ATOMIC_TYPE_DOUBLE;
1793 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1794 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1796 case SPECIFIER_BOOL:
1797 atomic_type = ATOMIC_TYPE_BOOL;
1799 #ifdef PROVIDE_COMPLEX
1800 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1801 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1803 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1804 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1806 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1807 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1809 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1810 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1812 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1813 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1815 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1816 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1820 /* invalid specifier combination, give an error message */
1821 if(type_specifiers == 0) {
1822 if (! strict_mode) {
1823 warningf(HERE, "no type specifiers in declaration, using int");
1824 atomic_type = ATOMIC_TYPE_INT;
1827 errorf(HERE, "no type specifiers given in declaration");
1829 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1830 (type_specifiers & SPECIFIER_UNSIGNED)) {
1831 errorf(HERE, "signed and unsigned specifiers gives");
1832 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1833 errorf(HERE, "only integer types can be signed or unsigned");
1835 errorf(HERE, "multiple datatypes in declaration");
1837 atomic_type = ATOMIC_TYPE_INVALID;
1840 type = allocate_type_zero(TYPE_ATOMIC);
1841 type->atomic.akind = atomic_type;
1844 if(type_specifiers != 0) {
1845 errorf(HERE, "multiple datatypes in declaration");
1849 type->base.qualifiers = type_qualifiers;
1851 type_t *result = typehash_insert(type);
1852 if(newtype && result != type) {
1856 specifiers->type = result;
1859 static type_qualifiers_t parse_type_qualifiers(void)
1861 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1864 switch(token.type) {
1865 /* type qualifiers */
1866 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1867 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1868 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1871 return type_qualifiers;
1876 static declaration_t *parse_identifier_list(void)
1878 declaration_t *declarations = NULL;
1879 declaration_t *last_declaration = NULL;
1881 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1883 declaration->source_position = token.source_position;
1884 declaration->symbol = token.v.symbol;
1887 if(last_declaration != NULL) {
1888 last_declaration->next = declaration;
1890 declarations = declaration;
1892 last_declaration = declaration;
1894 if(token.type != ',')
1897 } while(token.type == T_IDENTIFIER);
1899 return declarations;
1902 static void semantic_parameter(declaration_t *declaration)
1904 /* TODO: improve error messages */
1906 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1907 errorf(HERE, "typedef not allowed in parameter list");
1908 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1909 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1910 errorf(HERE, "parameter may only have none or register storage class");
1913 type_t *orig_type = declaration->type;
1914 if(orig_type == NULL)
1916 type_t *type = skip_typeref(orig_type);
1918 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1919 * into a pointer. § 6.7.5.3 (7) */
1920 if (is_type_array(type)) {
1921 const array_type_t *arr_type = &type->array;
1922 type_t *element_type = arr_type->element_type;
1924 type = make_pointer_type(element_type, type->base.qualifiers);
1926 declaration->type = type;
1929 if(is_type_incomplete(type)) {
1930 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1931 orig_type, declaration->symbol);
1935 static declaration_t *parse_parameter(void)
1937 declaration_specifiers_t specifiers;
1938 memset(&specifiers, 0, sizeof(specifiers));
1940 parse_declaration_specifiers(&specifiers);
1942 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1944 semantic_parameter(declaration);
1949 static declaration_t *parse_parameters(function_type_t *type)
1951 if(token.type == T_IDENTIFIER) {
1952 symbol_t *symbol = token.v.symbol;
1953 if(!is_typedef_symbol(symbol)) {
1954 type->kr_style_parameters = true;
1955 return parse_identifier_list();
1959 if(token.type == ')') {
1960 type->unspecified_parameters = 1;
1963 if(token.type == T_void && look_ahead(1)->type == ')') {
1968 declaration_t *declarations = NULL;
1969 declaration_t *declaration;
1970 declaration_t *last_declaration = NULL;
1971 function_parameter_t *parameter;
1972 function_parameter_t *last_parameter = NULL;
1975 switch(token.type) {
1979 return declarations;
1982 case T___extension__:
1984 declaration = parse_parameter();
1986 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1987 memset(parameter, 0, sizeof(parameter[0]));
1988 parameter->type = declaration->type;
1990 if(last_parameter != NULL) {
1991 last_declaration->next = declaration;
1992 last_parameter->next = parameter;
1994 type->parameters = parameter;
1995 declarations = declaration;
1997 last_parameter = parameter;
1998 last_declaration = declaration;
2002 return declarations;
2004 if(token.type != ',')
2005 return declarations;
2015 } construct_type_type_t;
2017 typedef struct construct_type_t construct_type_t;
2018 struct construct_type_t {
2019 construct_type_type_t type;
2020 construct_type_t *next;
2023 typedef struct parsed_pointer_t parsed_pointer_t;
2024 struct parsed_pointer_t {
2025 construct_type_t construct_type;
2026 type_qualifiers_t type_qualifiers;
2029 typedef struct construct_function_type_t construct_function_type_t;
2030 struct construct_function_type_t {
2031 construct_type_t construct_type;
2032 type_t *function_type;
2035 typedef struct parsed_array_t parsed_array_t;
2036 struct parsed_array_t {
2037 construct_type_t construct_type;
2038 type_qualifiers_t type_qualifiers;
2044 typedef struct construct_base_type_t construct_base_type_t;
2045 struct construct_base_type_t {
2046 construct_type_t construct_type;
2050 static construct_type_t *parse_pointer_declarator(void)
2054 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2055 memset(pointer, 0, sizeof(pointer[0]));
2056 pointer->construct_type.type = CONSTRUCT_POINTER;
2057 pointer->type_qualifiers = parse_type_qualifiers();
2059 return (construct_type_t*) pointer;
2062 static construct_type_t *parse_array_declarator(void)
2066 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2067 memset(array, 0, sizeof(array[0]));
2068 array->construct_type.type = CONSTRUCT_ARRAY;
2070 if(token.type == T_static) {
2071 array->is_static = true;
2075 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2076 if(type_qualifiers != 0) {
2077 if(token.type == T_static) {
2078 array->is_static = true;
2082 array->type_qualifiers = type_qualifiers;
2084 if(token.type == '*' && look_ahead(1)->type == ']') {
2085 array->is_variable = true;
2087 } else if(token.type != ']') {
2088 array->size = parse_assignment_expression();
2093 return (construct_type_t*) array;
2096 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2100 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2102 declaration_t *parameters = parse_parameters(&type->function);
2103 if(declaration != NULL) {
2104 declaration->context.declarations = parameters;
2107 construct_function_type_t *construct_function_type =
2108 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2109 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2110 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2111 construct_function_type->function_type = type;
2115 return (construct_type_t*) construct_function_type;
2118 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2119 bool may_be_abstract)
2121 /* construct a single linked list of construct_type_t's which describe
2122 * how to construct the final declarator type */
2123 construct_type_t *first = NULL;
2124 construct_type_t *last = NULL;
2127 while(token.type == '*') {
2128 construct_type_t *type = parse_pointer_declarator();
2139 /* TODO: find out if this is correct */
2142 construct_type_t *inner_types = NULL;
2144 switch(token.type) {
2146 if(declaration == NULL) {
2147 errorf(HERE, "no identifier expected in typename");
2149 declaration->symbol = token.v.symbol;
2150 declaration->source_position = token.source_position;
2156 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2162 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2163 /* avoid a loop in the outermost scope, because eat_statement doesn't
2165 if(token.type == '}' && current_function == NULL) {
2173 construct_type_t *p = last;
2176 construct_type_t *type;
2177 switch(token.type) {
2179 type = parse_function_declarator(declaration);
2182 type = parse_array_declarator();
2185 goto declarator_finished;
2188 /* insert in the middle of the list (behind p) */
2190 type->next = p->next;
2201 declarator_finished:
2204 /* append inner_types at the end of the list, we don't to set last anymore
2205 * as it's not needed anymore */
2207 assert(first == NULL);
2208 first = inner_types;
2210 last->next = inner_types;
2216 static type_t *construct_declarator_type(construct_type_t *construct_list,
2219 construct_type_t *iter = construct_list;
2220 for( ; iter != NULL; iter = iter->next) {
2221 switch(iter->type) {
2222 case CONSTRUCT_INVALID:
2223 panic("invalid type construction found");
2224 case CONSTRUCT_FUNCTION: {
2225 construct_function_type_t *construct_function_type
2226 = (construct_function_type_t*) iter;
2228 type_t *function_type = construct_function_type->function_type;
2230 function_type->function.return_type = type;
2232 type = function_type;
2236 case CONSTRUCT_POINTER: {
2237 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2238 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2239 pointer_type->pointer.points_to = type;
2240 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2242 type = pointer_type;
2246 case CONSTRUCT_ARRAY: {
2247 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2248 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2250 array_type->base.qualifiers = parsed_array->type_qualifiers;
2251 array_type->array.element_type = type;
2252 array_type->array.is_static = parsed_array->is_static;
2253 array_type->array.is_variable = parsed_array->is_variable;
2254 array_type->array.size = parsed_array->size;
2261 type_t *hashed_type = typehash_insert(type);
2262 if(hashed_type != type) {
2263 /* the function type was constructed earlier freeing it here will
2264 * destroy other types... */
2265 if(iter->type != CONSTRUCT_FUNCTION) {
2275 static declaration_t *parse_declarator(
2276 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2278 type_t *type = specifiers->type;
2279 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2280 declaration->storage_class = specifiers->storage_class;
2281 declaration->modifiers = specifiers->decl_modifiers;
2282 declaration->is_inline = specifiers->is_inline;
2284 construct_type_t *construct_type
2285 = parse_inner_declarator(declaration, may_be_abstract);
2286 declaration->type = construct_declarator_type(construct_type, type);
2288 if(construct_type != NULL) {
2289 obstack_free(&temp_obst, construct_type);
2295 static type_t *parse_abstract_declarator(type_t *base_type)
2297 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2299 type_t *result = construct_declarator_type(construct_type, base_type);
2300 if(construct_type != NULL) {
2301 obstack_free(&temp_obst, construct_type);
2307 static declaration_t *append_declaration(declaration_t* const declaration)
2309 if (last_declaration != NULL) {
2310 last_declaration->next = declaration;
2312 context->declarations = declaration;
2314 last_declaration = declaration;
2318 static declaration_t *internal_record_declaration(
2319 declaration_t *const declaration,
2320 const bool is_function_definition)
2322 const symbol_t *const symbol = declaration->symbol;
2323 const namespace_t namespc = (namespace_t)declaration->namespc;
2325 const type_t *const type = skip_typeref(declaration->type);
2326 if (is_type_function(type) && type->function.unspecified_parameters) {
2327 warningf(declaration->source_position,
2328 "function declaration '%#T' is not a prototype",
2329 type, declaration->symbol);
2332 declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2333 assert(declaration != previous_declaration);
2334 if (previous_declaration != NULL
2335 && previous_declaration->parent_context == context) {
2336 /* can happen for K&R style declarations */
2337 if(previous_declaration->type == NULL) {
2338 previous_declaration->type = declaration->type;
2341 const type_t *const prev_type = skip_typeref(previous_declaration->type);
2342 if (!types_compatible(type, prev_type)) {
2343 errorf(declaration->source_position,
2344 "declaration '%#T' is incompatible with previous declaration '%#T'",
2345 type, symbol, previous_declaration->type, symbol);
2346 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2348 unsigned old_storage_class = previous_declaration->storage_class;
2349 unsigned new_storage_class = declaration->storage_class;
2351 /* pretend no storage class means extern for function declarations
2352 * (except if the previous declaration is neither none nor extern) */
2353 if (is_type_function(type)) {
2354 switch (old_storage_class) {
2355 case STORAGE_CLASS_NONE:
2356 old_storage_class = STORAGE_CLASS_EXTERN;
2358 case STORAGE_CLASS_EXTERN:
2359 if (new_storage_class == STORAGE_CLASS_NONE && !is_function_definition) {
2360 new_storage_class = STORAGE_CLASS_EXTERN;
2368 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2369 new_storage_class == STORAGE_CLASS_EXTERN) {
2370 warn_redundant_declaration:
2371 warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
2372 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2373 } else if (current_function == NULL) {
2374 if (old_storage_class != STORAGE_CLASS_STATIC &&
2375 new_storage_class == STORAGE_CLASS_STATIC) {
2376 errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
2377 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2379 if (old_storage_class != STORAGE_CLASS_EXTERN) {
2380 goto warn_redundant_declaration;
2382 if (new_storage_class == STORAGE_CLASS_NONE) {
2383 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2387 if (old_storage_class == new_storage_class) {
2388 errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
2390 errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
2392 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2395 return previous_declaration;
2398 assert(declaration->parent_context == NULL);
2399 assert(declaration->symbol != NULL);
2400 assert(context != NULL);
2402 declaration->parent_context = context;
2404 environment_push(declaration);
2405 return append_declaration(declaration);
2408 static declaration_t *record_declaration(declaration_t *declaration)
2410 return internal_record_declaration(declaration, false);
2413 static declaration_t *record_function_definition(declaration_t *const declaration)
2415 return internal_record_declaration(declaration, true);
2418 static void parser_error_multiple_definition(declaration_t *declaration,
2419 const source_position_t source_position)
2421 errorf(source_position, "multiple definition of symbol '%Y'",
2422 declaration->symbol);
2423 errorf(declaration->source_position,
2424 "this is the location of the previous definition.");
2427 static bool is_declaration_specifier(const token_t *token,
2428 bool only_type_specifiers)
2430 switch(token->type) {
2434 return is_typedef_symbol(token->v.symbol);
2436 case T___extension__:
2439 return !only_type_specifiers;
2446 static void parse_init_declarator_rest(declaration_t *declaration)
2450 type_t *orig_type = declaration->type;
2451 type_t *type = NULL;
2452 if(orig_type != NULL)
2453 type = skip_typeref(orig_type);
2455 if(declaration->init.initializer != NULL) {
2456 parser_error_multiple_definition(declaration, token.source_position);
2459 initializer_t *initializer = parse_initializer(type);
2461 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2462 * the array type size */
2463 if(type != NULL && is_type_array(type) && initializer != NULL) {
2464 array_type_t *array_type = &type->array;
2466 if(array_type->size == NULL) {
2467 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2469 cnst->base.datatype = type_size_t;
2471 switch (initializer->kind) {
2472 case INITIALIZER_LIST: {
2473 initializer_list_t *const list = &initializer->list;
2474 cnst->conste.v.int_value = list->len;
2478 case INITIALIZER_STRING: {
2479 initializer_string_t *const string = &initializer->string;
2480 cnst->conste.v.int_value = strlen(string->string) + 1;
2484 case INITIALIZER_WIDE_STRING: {
2485 initializer_wide_string_t *const string = &initializer->wide_string;
2486 cnst->conste.v.int_value = string->string.size;
2491 panic("invalid initializer type");
2494 array_type->size = cnst;
2498 if(type != NULL && is_type_function(type)) {
2499 errorf(declaration->source_position,
2500 "initializers not allowed for function types at declator '%Y' (type '%T')",
2501 declaration->symbol, orig_type);
2503 declaration->init.initializer = initializer;
2507 /* parse rest of a declaration without any declarator */
2508 static void parse_anonymous_declaration_rest(
2509 const declaration_specifiers_t *specifiers,
2510 parsed_declaration_func finished_declaration)
2514 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2516 declaration->type = specifiers->type;
2517 declaration->storage_class = specifiers->storage_class;
2518 declaration->source_position = specifiers->source_position;
2520 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2521 warningf(declaration->source_position, "useless storage class in empty declaration");
2524 type_t *type = declaration->type;
2525 switch (type->kind) {
2526 case TYPE_COMPOUND_STRUCT:
2527 case TYPE_COMPOUND_UNION: {
2528 const compound_type_t *compound_type = &type->compound;
2529 if (compound_type->declaration->symbol == NULL) {
2530 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2539 warningf(declaration->source_position, "empty declaration");
2543 finished_declaration(declaration);
2546 static void parse_declaration_rest(declaration_t *ndeclaration,
2547 const declaration_specifiers_t *specifiers,
2548 parsed_declaration_func finished_declaration)
2551 declaration_t *declaration = finished_declaration(ndeclaration);
2553 type_t *orig_type = declaration->type;
2554 type_t *type = skip_typeref(orig_type);
2556 if(type->kind != TYPE_FUNCTION && declaration->is_inline) {
2557 warningf(declaration->source_position,
2558 "variable '%Y' declared 'inline'\n", declaration->symbol);
2561 if(token.type == '=') {
2562 parse_init_declarator_rest(declaration);
2565 if(token.type != ',')
2569 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2574 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2576 symbol_t *symbol = declaration->symbol;
2577 if(symbol == NULL) {
2578 errorf(HERE, "anonymous declaration not valid as function parameter");
2581 namespace_t namespc = (namespace_t) declaration->namespc;
2582 if(namespc != NAMESPACE_NORMAL) {
2583 return record_declaration(declaration);
2586 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2587 if(previous_declaration == NULL ||
2588 previous_declaration->parent_context != context) {
2589 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2594 if(previous_declaration->type == NULL) {
2595 previous_declaration->type = declaration->type;
2596 previous_declaration->storage_class = declaration->storage_class;
2597 previous_declaration->parent_context = context;
2598 return previous_declaration;
2600 return record_declaration(declaration);
2604 static void parse_declaration(parsed_declaration_func finished_declaration)
2606 declaration_specifiers_t specifiers;
2607 memset(&specifiers, 0, sizeof(specifiers));
2608 parse_declaration_specifiers(&specifiers);
2610 if(token.type == ';') {
2611 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2613 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2614 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2618 static void parse_kr_declaration_list(declaration_t *declaration)
2620 type_t *type = skip_typeref(declaration->type);
2621 if(!is_type_function(type))
2624 if(!type->function.kr_style_parameters)
2627 /* push function parameters */
2628 int top = environment_top();
2629 context_t *last_context = context;
2630 set_context(&declaration->context);
2632 declaration_t *parameter = declaration->context.declarations;
2633 for( ; parameter != NULL; parameter = parameter->next) {
2634 assert(parameter->parent_context == NULL);
2635 parameter->parent_context = context;
2636 environment_push(parameter);
2639 /* parse declaration list */
2640 while(is_declaration_specifier(&token, false)) {
2641 parse_declaration(finished_kr_declaration);
2644 /* pop function parameters */
2645 assert(context == &declaration->context);
2646 set_context(last_context);
2647 environment_pop_to(top);
2649 /* update function type */
2650 type_t *new_type = duplicate_type(type);
2651 new_type->function.kr_style_parameters = false;
2653 function_parameter_t *parameters = NULL;
2654 function_parameter_t *last_parameter = NULL;
2656 declaration_t *parameter_declaration = declaration->context.declarations;
2657 for( ; parameter_declaration != NULL;
2658 parameter_declaration = parameter_declaration->next) {
2659 type_t *parameter_type = parameter_declaration->type;
2660 if(parameter_type == NULL) {
2662 errorf(HERE, "no type specified for function parameter '%Y'",
2663 parameter_declaration->symbol);
2665 warningf(HERE, "no type specified for function parameter '%Y', using int",
2666 parameter_declaration->symbol);
2667 parameter_type = type_int;
2668 parameter_declaration->type = parameter_type;
2672 semantic_parameter(parameter_declaration);
2673 parameter_type = parameter_declaration->type;
2675 function_parameter_t *function_parameter
2676 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2677 memset(function_parameter, 0, sizeof(function_parameter[0]));
2679 function_parameter->type = parameter_type;
2680 if(last_parameter != NULL) {
2681 last_parameter->next = function_parameter;
2683 parameters = function_parameter;
2685 last_parameter = function_parameter;
2687 new_type->function.parameters = parameters;
2689 type = typehash_insert(new_type);
2690 if(type != new_type) {
2691 obstack_free(type_obst, new_type);
2694 declaration->type = type;
2697 static void parse_external_declaration(void)
2699 /* function-definitions and declarations both start with declaration
2701 declaration_specifiers_t specifiers;
2702 memset(&specifiers, 0, sizeof(specifiers));
2703 parse_declaration_specifiers(&specifiers);
2705 /* must be a declaration */
2706 if(token.type == ';') {
2707 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2711 /* declarator is common to both function-definitions and declarations */
2712 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2714 /* must be a declaration */
2715 if(token.type == ',' || token.type == '=' || token.type == ';') {
2716 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2720 /* must be a function definition */
2721 parse_kr_declaration_list(ndeclaration);
2723 if(token.type != '{') {
2724 parse_error_expected("while parsing function definition", '{', 0);
2729 type_t *type = ndeclaration->type;
2735 /* note that we don't skip typerefs: the standard doesn't allow them here
2736 * (so we can't use is_type_function here) */
2737 if(type->kind != TYPE_FUNCTION) {
2738 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2739 type, ndeclaration->symbol);
2744 /* § 6.7.5.3 (14) a function definition with () means no
2745 * parameters (and not unspecified parameters) */
2746 if(type->function.unspecified_parameters) {
2747 type_t *duplicate = duplicate_type(type);
2748 duplicate->function.unspecified_parameters = false;
2750 type = typehash_insert(duplicate);
2751 if(type != duplicate) {
2752 obstack_free(type_obst, duplicate);
2754 ndeclaration->type = type;
2757 declaration_t *const declaration = record_function_definition(ndeclaration);
2758 if(ndeclaration != declaration) {
2759 declaration->context = ndeclaration->context;
2761 type = skip_typeref(declaration->type);
2763 /* push function parameters and switch context */
2764 int top = environment_top();
2765 context_t *last_context = context;
2766 set_context(&declaration->context);
2768 declaration_t *parameter = declaration->context.declarations;
2769 for( ; parameter != NULL; parameter = parameter->next) {
2770 if(parameter->parent_context == &ndeclaration->context) {
2771 parameter->parent_context = context;
2773 assert(parameter->parent_context == NULL
2774 || parameter->parent_context == context);
2775 parameter->parent_context = context;
2776 environment_push(parameter);
2779 if(declaration->init.statement != NULL) {
2780 parser_error_multiple_definition(declaration, token.source_position);
2782 goto end_of_parse_external_declaration;
2784 /* parse function body */
2785 int label_stack_top = label_top();
2786 declaration_t *old_current_function = current_function;
2787 current_function = declaration;
2789 declaration->init.statement = parse_compound_statement();
2791 assert(current_function == declaration);
2792 current_function = old_current_function;
2793 label_pop_to(label_stack_top);
2796 end_of_parse_external_declaration:
2797 assert(context == &declaration->context);
2798 set_context(last_context);
2799 environment_pop_to(top);
2802 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2804 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2805 type->bitfield.base = base;
2806 type->bitfield.size = size;
2811 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2813 /* TODO: check constraints for struct declarations (in specifiers) */
2815 declaration_t *declaration;
2817 if(token.type == ':') {
2820 type_t *base_type = specifiers->type;
2821 expression_t *size = parse_constant_expression();
2823 type_t *type = make_bitfield_type(base_type, size);
2825 declaration = allocate_ast_zero(sizeof(declaration[0]));
2827 declaration->namespc = NAMESPACE_NORMAL;
2828 declaration->storage_class = STORAGE_CLASS_NONE;
2829 declaration->source_position = token.source_position;
2830 declaration->modifiers = specifiers->decl_modifiers;
2831 declaration->type = type;
2833 record_declaration(declaration);
2835 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2837 if(token.type == ':') {
2839 expression_t *size = parse_constant_expression();
2841 type_t *type = make_bitfield_type(declaration->type, size);
2842 declaration->type = type;
2845 record_declaration(declaration);
2847 if(token.type != ',')
2854 static void parse_compound_type_entries(void)
2858 while(token.type != '}' && token.type != T_EOF) {
2859 declaration_specifiers_t specifiers;
2860 memset(&specifiers, 0, sizeof(specifiers));
2861 parse_declaration_specifiers(&specifiers);
2863 parse_struct_declarators(&specifiers);
2865 if(token.type == T_EOF) {
2866 errorf(HERE, "EOF while parsing struct");
2871 static type_t *parse_typename(void)
2873 declaration_specifiers_t specifiers;
2874 memset(&specifiers, 0, sizeof(specifiers));
2875 parse_declaration_specifiers(&specifiers);
2876 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2877 /* TODO: improve error message, user does probably not know what a
2878 * storage class is...
2880 errorf(HERE, "typename may not have a storage class");
2883 type_t *result = parse_abstract_declarator(specifiers.type);
2891 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2892 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2893 expression_t *left);
2895 typedef struct expression_parser_function_t expression_parser_function_t;
2896 struct expression_parser_function_t {
2897 unsigned precedence;
2898 parse_expression_function parser;
2899 unsigned infix_precedence;
2900 parse_expression_infix_function infix_parser;
2903 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2906 * Creates a new invalid expression.
2908 static expression_t *create_invalid_expression(void)
2910 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2911 expression->base.source_position = token.source_position;
2915 static expression_t *expected_expression_error(void)
2917 errorf(HERE, "expected expression, got token '%K'", &token);
2921 return create_invalid_expression();
2925 * Parse a string constant.
2927 static expression_t *parse_string_const(void)
2929 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2930 cnst->base.datatype = type_string;
2931 cnst->string.value = parse_string_literals();
2937 * Parse a wide string constant.
2939 static expression_t *parse_wide_string_const(void)
2941 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2942 cnst->base.datatype = type_wchar_t_ptr;
2943 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2949 * Parse an integer constant.
2951 static expression_t *parse_int_const(void)
2953 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2954 cnst->base.datatype = token.datatype;
2955 cnst->conste.v.int_value = token.v.intvalue;
2963 * Parse a float constant.
2965 static expression_t *parse_float_const(void)
2967 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2968 cnst->base.datatype = token.datatype;
2969 cnst->conste.v.float_value = token.v.floatvalue;
2976 static declaration_t *create_implicit_function(symbol_t *symbol,
2977 const source_position_t source_position)
2979 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2980 ntype->function.return_type = type_int;
2981 ntype->function.unspecified_parameters = true;
2983 type_t *type = typehash_insert(ntype);
2988 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2990 declaration->storage_class = STORAGE_CLASS_EXTERN;
2991 declaration->type = type;
2992 declaration->symbol = symbol;
2993 declaration->source_position = source_position;
2994 declaration->parent_context = global_context;
2996 context_t *old_context = context;
2997 set_context(global_context);
2999 environment_push(declaration);
3000 /* prepend the declaration to the global declarations list */
3001 declaration->next = context->declarations;
3002 context->declarations = declaration;
3004 assert(context == global_context);
3005 set_context(old_context);
3011 * Creates a return_type (func)(argument_type) function type if not
3014 * @param return_type the return type
3015 * @param argument_type the argument type
3017 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3019 function_parameter_t *parameter
3020 = obstack_alloc(type_obst, sizeof(parameter[0]));
3021 memset(parameter, 0, sizeof(parameter[0]));
3022 parameter->type = argument_type;
3024 type_t *type = allocate_type_zero(TYPE_FUNCTION);
3025 type->function.return_type = return_type;
3026 type->function.parameters = parameter;
3028 type_t *result = typehash_insert(type);
3029 if(result != type) {
3037 * Creates a function type for some function like builtins.
3039 * @param symbol the symbol describing the builtin
3041 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3043 switch(symbol->ID) {
3044 case T___builtin_alloca:
3045 return make_function_1_type(type_void_ptr, type_size_t);
3046 case T___builtin_nan:
3047 return make_function_1_type(type_double, type_string);
3048 case T___builtin_nanf:
3049 return make_function_1_type(type_float, type_string);
3050 case T___builtin_nand:
3051 return make_function_1_type(type_long_double, type_string);
3052 case T___builtin_va_end:
3053 return make_function_1_type(type_void, type_valist);
3055 panic("not implemented builtin symbol found");
3060 * Performs automatic type cast as described in § 6.3.2.1.
3062 * @param orig_type the original type
3064 static type_t *automatic_type_conversion(type_t *orig_type)
3066 if(orig_type == NULL)
3069 type_t *type = skip_typeref(orig_type);
3070 if(is_type_array(type)) {
3071 array_type_t *array_type = &type->array;
3072 type_t *element_type = array_type->element_type;
3073 unsigned qualifiers = array_type->type.qualifiers;
3075 return make_pointer_type(element_type, qualifiers);
3078 if(is_type_function(type)) {
3079 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3086 * reverts the automatic casts of array to pointer types and function
3087 * to function-pointer types as defined § 6.3.2.1
3089 type_t *revert_automatic_type_conversion(const expression_t *expression)
3091 if(expression->base.datatype == NULL)
3094 switch(expression->kind) {
3095 case EXPR_REFERENCE: {
3096 const reference_expression_t *ref = &expression->reference;
3097 return ref->declaration->type;
3100 const select_expression_t *select = &expression->select;
3101 return select->compound_entry->type;
3103 case EXPR_UNARY_DEREFERENCE: {
3104 expression_t *value = expression->unary.value;
3105 type_t *type = skip_typeref(value->base.datatype);
3106 pointer_type_t *pointer_type = &type->pointer;
3108 return pointer_type->points_to;
3110 case EXPR_BUILTIN_SYMBOL: {
3111 const builtin_symbol_expression_t *builtin
3112 = &expression->builtin_symbol;
3113 return get_builtin_symbol_type(builtin->symbol);
3115 case EXPR_ARRAY_ACCESS: {
3116 const array_access_expression_t *array_access
3117 = &expression->array_access;
3118 const expression_t *array_ref = array_access->array_ref;
3119 type_t *type_left = skip_typeref(array_ref->base.datatype);
3120 assert(is_type_pointer(type_left));
3121 pointer_type_t *pointer_type = &type_left->pointer;
3122 return pointer_type->points_to;
3129 return expression->base.datatype;
3132 static expression_t *parse_reference(void)
3134 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3136 reference_expression_t *ref = &expression->reference;
3137 ref->symbol = token.v.symbol;
3139 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3141 source_position_t source_position = token.source_position;
3144 if(declaration == NULL) {
3145 if (! strict_mode && token.type == '(') {
3146 /* an implicitly defined function */
3147 warningf(HERE, "implicit declaration of function '%Y'",
3150 declaration = create_implicit_function(ref->symbol,
3153 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3158 type_t *type = declaration->type;
3160 /* we always do the auto-type conversions; the & and sizeof parser contains
3161 * code to revert this! */
3162 type = automatic_type_conversion(type);
3164 ref->declaration = declaration;
3165 ref->expression.datatype = type;
3170 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3174 /* TODO check if explicit cast is allowed and issue warnings/errors */
3177 static expression_t *parse_cast(void)
3179 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3181 cast->base.source_position = token.source_position;
3183 type_t *type = parse_typename();
3186 expression_t *value = parse_sub_expression(20);
3188 check_cast_allowed(value, type);
3190 cast->base.datatype = type;
3191 cast->unary.value = value;
3196 static expression_t *parse_statement_expression(void)
3198 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3200 statement_t *statement = parse_compound_statement();
3201 expression->statement.statement = statement;
3202 if(statement == NULL) {
3207 assert(statement->kind == STATEMENT_COMPOUND);
3208 compound_statement_t *compound_statement = &statement->compound;
3210 /* find last statement and use it's type */
3211 const statement_t *last_statement = NULL;
3212 const statement_t *iter = compound_statement->statements;
3213 for( ; iter != NULL; iter = iter->base.next) {
3214 last_statement = iter;
3217 if(last_statement->kind == STATEMENT_EXPRESSION) {
3218 const expression_statement_t *expression_statement
3219 = &last_statement->expression;
3220 expression->base.datatype
3221 = expression_statement->expression->base.datatype;
3223 expression->base.datatype = type_void;
3231 static expression_t *parse_brace_expression(void)
3235 switch(token.type) {
3237 /* gcc extension: a statement expression */
3238 return parse_statement_expression();
3242 return parse_cast();
3244 if(is_typedef_symbol(token.v.symbol)) {
3245 return parse_cast();
3249 expression_t *result = parse_expression();
3255 static expression_t *parse_function_keyword(void)
3260 if (current_function == NULL) {
3261 errorf(HERE, "'__func__' used outside of a function");
3264 string_literal_expression_t *expression
3265 = allocate_ast_zero(sizeof(expression[0]));
3267 expression->expression.kind = EXPR_FUNCTION;
3268 expression->expression.datatype = type_string;
3269 expression->value = current_function->symbol->string;
3271 return (expression_t*) expression;
3274 static expression_t *parse_pretty_function_keyword(void)
3276 eat(T___PRETTY_FUNCTION__);
3279 if (current_function == NULL) {
3280 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3283 string_literal_expression_t *expression
3284 = allocate_ast_zero(sizeof(expression[0]));
3286 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3287 expression->expression.datatype = type_string;
3288 expression->value = current_function->symbol->string;
3290 return (expression_t*) expression;
3293 static designator_t *parse_designator(void)
3295 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3297 if(token.type != T_IDENTIFIER) {
3298 parse_error_expected("while parsing member designator",
3303 result->symbol = token.v.symbol;
3306 designator_t *last_designator = result;
3308 if(token.type == '.') {
3310 if(token.type != T_IDENTIFIER) {
3311 parse_error_expected("while parsing member designator",
3316 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3317 designator->symbol = token.v.symbol;
3320 last_designator->next = designator;
3321 last_designator = designator;
3324 if(token.type == '[') {
3326 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3327 designator->array_access = parse_expression();
3328 if(designator->array_access == NULL) {
3334 last_designator->next = designator;
3335 last_designator = designator;
3344 static expression_t *parse_offsetof(void)
3346 eat(T___builtin_offsetof);
3348 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3349 expression->base.datatype = type_size_t;
3352 expression->offsetofe.type = parse_typename();
3354 expression->offsetofe.designator = parse_designator();
3360 static expression_t *parse_va_start(void)
3362 eat(T___builtin_va_start);
3364 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3367 expression->va_starte.ap = parse_assignment_expression();
3369 expression_t *const expr = parse_assignment_expression();
3370 if (expr->kind == EXPR_REFERENCE) {
3371 declaration_t *const decl = expr->reference.declaration;
3372 if (decl->parent_context == ¤t_function->context &&
3373 decl->next == NULL) {
3374 expression->va_starte.parameter = decl;
3379 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3381 return create_invalid_expression();
3384 static expression_t *parse_va_arg(void)
3386 eat(T___builtin_va_arg);
3388 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3391 expression->va_arge.ap = parse_assignment_expression();
3393 expression->base.datatype = parse_typename();
3399 static expression_t *parse_builtin_symbol(void)
3401 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3403 symbol_t *symbol = token.v.symbol;
3405 expression->builtin_symbol.symbol = symbol;
3408 type_t *type = get_builtin_symbol_type(symbol);
3409 type = automatic_type_conversion(type);
3411 expression->base.datatype = type;
3415 static expression_t *parse_builtin_constant(void)
3417 eat(T___builtin_constant_p);
3419 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3422 expression->builtin_constant.value = parse_assignment_expression();
3424 expression->base.datatype = type_int;
3429 static expression_t *parse_builtin_prefetch(void)
3431 eat(T___builtin_prefetch);
3433 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3436 expression->builtin_prefetch.adr = parse_assignment_expression();
3437 if (token.type == ',') {
3439 expression->builtin_prefetch.rw = parse_assignment_expression();
3441 if (token.type == ',') {
3443 expression->builtin_prefetch.locality = parse_assignment_expression();
3446 expression->base.datatype = type_void;
3451 static expression_t *parse_compare_builtin(void)
3453 expression_t *expression;
3455 switch(token.type) {
3456 case T___builtin_isgreater:
3457 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3459 case T___builtin_isgreaterequal:
3460 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3462 case T___builtin_isless:
3463 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3465 case T___builtin_islessequal:
3466 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3468 case T___builtin_islessgreater:
3469 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3471 case T___builtin_isunordered:
3472 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3475 panic("invalid compare builtin found");
3481 expression->binary.left = parse_assignment_expression();
3483 expression->binary.right = parse_assignment_expression();
3486 type_t *orig_type_left = expression->binary.left->base.datatype;
3487 type_t *orig_type_right = expression->binary.right->base.datatype;
3488 if(orig_type_left == NULL || orig_type_right == NULL)
3491 type_t *type_left = skip_typeref(orig_type_left);
3492 type_t *type_right = skip_typeref(orig_type_right);
3493 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3494 type_error_incompatible("invalid operands in comparison",
3495 token.source_position, type_left, type_right);
3497 semantic_comparison(&expression->binary);
3503 static expression_t *parse_builtin_expect(void)
3505 eat(T___builtin_expect);
3507 expression_t *expression
3508 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3511 expression->binary.left = parse_assignment_expression();
3513 expression->binary.right = parse_constant_expression();
3516 expression->base.datatype = expression->binary.left->base.datatype;
3521 static expression_t *parse_assume(void) {
3524 expression_t *expression
3525 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3528 expression->unary.value = parse_assignment_expression();
3531 expression->base.datatype = type_void;
3535 static expression_t *parse_alignof(void) {
3538 expression_t *expression
3539 = allocate_expression_zero(EXPR_ALIGNOF);
3542 expression->alignofe.type = parse_typename();
3545 expression->base.datatype = type_size_t;
3549 static expression_t *parse_primary_expression(void)
3551 switch(token.type) {
3553 return parse_int_const();
3554 case T_FLOATINGPOINT:
3555 return parse_float_const();
3556 case T_STRING_LITERAL:
3557 return parse_string_const();
3558 case T_WIDE_STRING_LITERAL:
3559 return parse_wide_string_const();
3561 return parse_reference();
3562 case T___FUNCTION__:
3564 return parse_function_keyword();
3565 case T___PRETTY_FUNCTION__:
3566 return parse_pretty_function_keyword();
3567 case T___builtin_offsetof:
3568 return parse_offsetof();
3569 case T___builtin_va_start:
3570 return parse_va_start();
3571 case T___builtin_va_arg:
3572 return parse_va_arg();
3573 case T___builtin_expect:
3574 return parse_builtin_expect();
3575 case T___builtin_nanf:
3576 case T___builtin_alloca:
3577 case T___builtin_va_end:
3578 return parse_builtin_symbol();
3579 case T___builtin_isgreater:
3580 case T___builtin_isgreaterequal:
3581 case T___builtin_isless:
3582 case T___builtin_islessequal:
3583 case T___builtin_islessgreater:
3584 case T___builtin_isunordered:
3585 return parse_compare_builtin();
3586 case T___builtin_constant_p:
3587 return parse_builtin_constant();
3588 case T___builtin_prefetch:
3589 return parse_builtin_prefetch();
3591 return parse_alignof();
3593 return parse_assume();
3596 return parse_brace_expression();
3599 errorf(HERE, "unexpected token '%K'", &token);
3602 return create_invalid_expression();
3606 * Check if the expression has the character type and issue a warning then.
3608 static void check_for_char_index_type(const expression_t *expression) {
3609 type_t *type = expression->base.datatype;
3610 type_t *base_type = skip_typeref(type);
3612 if (base_type->base.kind == TYPE_ATOMIC) {
3613 if (base_type->atomic.akind == ATOMIC_TYPE_CHAR) {
3614 warningf(expression->base.source_position,
3615 "array subscript has type '%T'", type);
3620 static expression_t *parse_array_expression(unsigned precedence,
3627 expression_t *inside = parse_expression();
3629 array_access_expression_t *array_access
3630 = allocate_ast_zero(sizeof(array_access[0]));
3632 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3634 type_t *type_left = left->base.datatype;
3635 type_t *type_inside = inside->base.datatype;
3636 type_t *return_type = NULL;
3638 if(type_left != NULL && type_inside != NULL) {
3639 type_left = skip_typeref(type_left);
3640 type_inside = skip_typeref(type_inside);
3642 if(is_type_pointer(type_left)) {
3643 pointer_type_t *pointer = &type_left->pointer;
3644 return_type = pointer->points_to;
3645 array_access->array_ref = left;
3646 array_access->index = inside;
3647 check_for_char_index_type(inside);
3648 } else if(is_type_pointer(type_inside)) {
3649 pointer_type_t *pointer = &type_inside->pointer;
3650 return_type = pointer->points_to;
3651 array_access->array_ref = inside;
3652 array_access->index = left;
3653 array_access->flipped = true;
3654 check_for_char_index_type(left);
3656 errorf(HERE, "array access on object with non-pointer types '%T', '%T'", type_left, type_inside);
3659 array_access->array_ref = left;
3660 array_access->index = inside;
3663 if(token.type != ']') {
3664 parse_error_expected("Problem while parsing array access", ']', 0);
3665 return (expression_t*) array_access;
3669 return_type = automatic_type_conversion(return_type);
3670 array_access->expression.datatype = return_type;
3672 return (expression_t*) array_access;
3675 static expression_t *parse_sizeof(unsigned precedence)
3679 sizeof_expression_t *sizeof_expression
3680 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3681 sizeof_expression->expression.kind = EXPR_SIZEOF;
3682 sizeof_expression->expression.datatype = type_size_t;
3684 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3686 sizeof_expression->type = parse_typename();
3689 expression_t *expression = parse_sub_expression(precedence);
3690 expression->base.datatype = revert_automatic_type_conversion(expression);
3692 sizeof_expression->type = expression->base.datatype;
3693 sizeof_expression->size_expression = expression;
3696 return (expression_t*) sizeof_expression;
3699 static expression_t *parse_select_expression(unsigned precedence,
3700 expression_t *compound)
3703 assert(token.type == '.' || token.type == T_MINUSGREATER);
3705 bool is_pointer = (token.type == T_MINUSGREATER);
3708 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3709 select->select.compound = compound;
3711 if(token.type != T_IDENTIFIER) {
3712 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3715 symbol_t *symbol = token.v.symbol;
3716 select->select.symbol = symbol;
3719 type_t *orig_type = compound->base.datatype;
3720 if(orig_type == NULL)
3721 return create_invalid_expression();
3723 type_t *type = skip_typeref(orig_type);
3725 type_t *type_left = type;
3727 if(type->kind != TYPE_POINTER) {
3728 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3729 return create_invalid_expression();
3731 pointer_type_t *pointer_type = &type->pointer;
3732 type_left = pointer_type->points_to;
3734 type_left = skip_typeref(type_left);
3736 if(type_left->kind != TYPE_COMPOUND_STRUCT
3737 && type_left->kind != TYPE_COMPOUND_UNION) {
3738 errorf(HERE, "request for member '%Y' in something not a struct or "
3739 "union, but '%T'", symbol, type_left);
3740 return create_invalid_expression();
3743 compound_type_t *compound_type = &type_left->compound;
3744 declaration_t *declaration = compound_type->declaration;
3746 if(!declaration->init.is_defined) {
3747 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3749 return create_invalid_expression();
3752 declaration_t *iter = declaration->context.declarations;
3753 for( ; iter != NULL; iter = iter->next) {
3754 if(iter->symbol == symbol) {
3759 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3760 return create_invalid_expression();
3763 /* we always do the auto-type conversions; the & and sizeof parser contains
3764 * code to revert this! */
3765 type_t *expression_type = automatic_type_conversion(iter->type);
3767 select->select.compound_entry = iter;
3768 select->base.datatype = expression_type;
3770 if(expression_type->kind == TYPE_BITFIELD) {
3771 expression_t *extract
3772 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3773 extract->unary.value = select;
3774 extract->base.datatype = expression_type->bitfield.base;
3783 * Parse a call expression, ie. expression '( ... )'.
3785 * @param expression the function address
3787 static expression_t *parse_call_expression(unsigned precedence,
3788 expression_t *expression)
3791 expression_t *result = allocate_expression_zero(EXPR_CALL);
3793 call_expression_t *call = &result->call;
3794 call->function = expression;
3796 function_type_t *function_type = NULL;
3797 type_t *orig_type = expression->base.datatype;
3798 if(orig_type != NULL) {
3799 type_t *type = skip_typeref(orig_type);
3801 if(is_type_pointer(type)) {
3802 pointer_type_t *pointer_type = &type->pointer;
3804 type = skip_typeref(pointer_type->points_to);
3806 if (is_type_function(type)) {
3807 function_type = &type->function;
3808 call->expression.datatype = function_type->return_type;
3811 if(function_type == NULL) {
3812 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3814 function_type = NULL;
3815 call->expression.datatype = NULL;
3819 /* parse arguments */
3822 if(token.type != ')') {
3823 call_argument_t *last_argument = NULL;
3826 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3828 argument->expression = parse_assignment_expression();
3829 if(last_argument == NULL) {
3830 call->arguments = argument;
3832 last_argument->next = argument;
3834 last_argument = argument;
3836 if(token.type != ',')
3843 if(function_type != NULL) {
3844 function_parameter_t *parameter = function_type->parameters;
3845 call_argument_t *argument = call->arguments;
3846 for( ; parameter != NULL && argument != NULL;
3847 parameter = parameter->next, argument = argument->next) {
3848 type_t *expected_type = parameter->type;
3849 /* TODO report context in error messages */
3850 argument->expression = create_implicit_cast(argument->expression,
3853 /* too few parameters */
3854 if(parameter != NULL) {
3855 errorf(HERE, "too few arguments to function '%E'", expression);
3856 } else if(argument != NULL) {
3857 /* too many parameters */
3858 if(!function_type->variadic
3859 && !function_type->unspecified_parameters) {
3860 errorf(HERE, "too many arguments to function '%E'", expression);
3862 /* do default promotion */
3863 for( ; argument != NULL; argument = argument->next) {
3864 type_t *type = argument->expression->base.datatype;
3869 type = skip_typeref(type);
3870 if(is_type_integer(type)) {
3871 type = promote_integer(type);
3872 } else if(type == type_float) {
3876 argument->expression
3877 = create_implicit_cast(argument->expression, type);
3880 check_format(&result->call);
3883 check_format(&result->call);
3890 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3892 static bool same_compound_type(const type_t *type1, const type_t *type2)
3894 if(!is_type_compound(type1))
3896 if(type1->kind != type2->kind)
3899 const compound_type_t *compound1 = &type1->compound;
3900 const compound_type_t *compound2 = &type2->compound;
3902 return compound1->declaration == compound2->declaration;
3906 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3908 * @param expression the conditional expression
3910 static expression_t *parse_conditional_expression(unsigned precedence,
3911 expression_t *expression)
3915 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3917 conditional_expression_t *conditional = &result->conditional;
3918 conditional->condition = expression;
3921 type_t *condition_type_orig = expression->base.datatype;
3922 if(condition_type_orig != NULL) {
3923 type_t *condition_type = skip_typeref(condition_type_orig);
3924 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3925 type_error("expected a scalar type in conditional condition",
3926 expression->base.source_position, condition_type_orig);
3930 expression_t *true_expression = parse_expression();
3932 expression_t *false_expression = parse_sub_expression(precedence);
3934 conditional->true_expression = true_expression;
3935 conditional->false_expression = false_expression;
3937 type_t *orig_true_type = true_expression->base.datatype;
3938 type_t *orig_false_type = false_expression->base.datatype;
3939 if(orig_true_type == NULL || orig_false_type == NULL)
3942 type_t *true_type = skip_typeref(orig_true_type);
3943 type_t *false_type = skip_typeref(orig_false_type);
3946 type_t *result_type = NULL;
3947 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3948 result_type = semantic_arithmetic(true_type, false_type);
3950 true_expression = create_implicit_cast(true_expression, result_type);
3951 false_expression = create_implicit_cast(false_expression, result_type);
3953 conditional->true_expression = true_expression;
3954 conditional->false_expression = false_expression;
3955 conditional->expression.datatype = result_type;
3956 } else if (same_compound_type(true_type, false_type)
3957 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3958 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3959 /* just take 1 of the 2 types */
3960 result_type = true_type;
3961 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3962 && pointers_compatible(true_type, false_type)) {
3964 result_type = true_type;
3967 type_error_incompatible("while parsing conditional",
3968 expression->base.source_position, true_type,
3972 conditional->expression.datatype = result_type;
3977 * Parse an extension expression.
3979 static expression_t *parse_extension(unsigned precedence)
3981 eat(T___extension__);
3983 /* TODO enable extensions */
3984 expression_t *expression = parse_sub_expression(precedence);
3985 /* TODO disable extensions */
3989 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3991 eat(T___builtin_classify_type);
3993 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3994 result->base.datatype = type_int;
3997 expression_t *expression = parse_sub_expression(precedence);
3999 result->classify_type.type_expression = expression;
4004 static void semantic_incdec(unary_expression_t *expression)
4006 type_t *orig_type = expression->value->base.datatype;
4007 if(orig_type == NULL)
4010 type_t *type = skip_typeref(orig_type);
4011 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4012 /* TODO: improve error message */
4013 errorf(HERE, "operation needs an arithmetic or pointer type");
4017 expression->expression.datatype = orig_type;
4020 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4022 type_t *orig_type = expression->value->base.datatype;
4023 if(orig_type == NULL)
4026 type_t *type = skip_typeref(orig_type);
4027 if(!is_type_arithmetic(type)) {
4028 /* TODO: improve error message */
4029 errorf(HERE, "operation needs an arithmetic type");
4033 expression->expression.datatype = orig_type;
4036 static void semantic_unexpr_scalar(unary_expression_t *expression)
4038 type_t *orig_type = expression->value->base.datatype;
4039 if(orig_type == NULL)
4042 type_t *type = skip_typeref(orig_type);
4043 if (!is_type_scalar(type)) {
4044 errorf(HERE, "operand of ! must be of scalar type");
4048 expression->expression.datatype = orig_type;
4051 static void semantic_unexpr_integer(unary_expression_t *expression)
4053 type_t *orig_type = expression->value->base.datatype;
4054 if(orig_type == NULL)
4057 type_t *type = skip_typeref(orig_type);
4058 if (!is_type_integer(type)) {
4059 errorf(HERE, "operand of ~ must be of integer type");
4063 expression->expression.datatype = orig_type;
4066 static void semantic_dereference(unary_expression_t *expression)
4068 type_t *orig_type = expression->value->base.datatype;
4069 if(orig_type == NULL)
4072 type_t *type = skip_typeref(orig_type);
4073 if(!is_type_pointer(type)) {
4074 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4078 pointer_type_t *pointer_type = &type->pointer;
4079 type_t *result_type = pointer_type->points_to;
4081 result_type = automatic_type_conversion(result_type);
4082 expression->expression.datatype = result_type;
4086 * Check the semantic of the address taken expression.
4088 static void semantic_take_addr(unary_expression_t *expression)
4090 expression_t *value = expression->value;
4091 value->base.datatype = revert_automatic_type_conversion(value);
4093 type_t *orig_type = value->base.datatype;
4094 if(orig_type == NULL)
4097 if(value->kind == EXPR_REFERENCE) {
4098 reference_expression_t *reference = (reference_expression_t*) value;
4099 declaration_t *declaration = reference->declaration;
4100 if(declaration != NULL) {
4101 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4102 errorf(expression->expression.source_position,
4103 "address of register variable '%Y' requested",
4104 declaration->symbol);
4106 declaration->address_taken = 1;
4110 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4113 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4114 static expression_t *parse_##unexpression_type(unsigned precedence) \
4118 expression_t *unary_expression \
4119 = allocate_expression_zero(unexpression_type); \
4120 unary_expression->base.source_position = HERE; \
4121 unary_expression->unary.value = parse_sub_expression(precedence); \
4123 sfunc(&unary_expression->unary); \
4125 return unary_expression; \
4128 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4129 semantic_unexpr_arithmetic)
4130 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4131 semantic_unexpr_arithmetic)
4132 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4133 semantic_unexpr_scalar)
4134 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4135 semantic_dereference)
4136 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4138 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4139 semantic_unexpr_integer)
4140 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4142 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4145 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4147 static expression_t *parse_##unexpression_type(unsigned precedence, \
4148 expression_t *left) \
4150 (void) precedence; \
4153 expression_t *unary_expression \
4154 = allocate_expression_zero(unexpression_type); \
4155 unary_expression->unary.value = left; \
4157 sfunc(&unary_expression->unary); \
4159 return unary_expression; \
4162 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4163 EXPR_UNARY_POSTFIX_INCREMENT,
4165 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4166 EXPR_UNARY_POSTFIX_DECREMENT,
4169 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4171 /* TODO: handle complex + imaginary types */
4173 /* § 6.3.1.8 Usual arithmetic conversions */
4174 if(type_left == type_long_double || type_right == type_long_double) {
4175 return type_long_double;
4176 } else if(type_left == type_double || type_right == type_double) {
4178 } else if(type_left == type_float || type_right == type_float) {
4182 type_right = promote_integer(type_right);
4183 type_left = promote_integer(type_left);
4185 if(type_left == type_right)
4188 bool signed_left = is_type_signed(type_left);
4189 bool signed_right = is_type_signed(type_right);
4190 int rank_left = get_rank(type_left);
4191 int rank_right = get_rank(type_right);
4192 if(rank_left < rank_right) {
4193 if(signed_left == signed_right || !signed_right) {
4199 if(signed_left == signed_right || !signed_left) {
4208 * Check the semantic restrictions for a binary expression.
4210 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4212 expression_t *left = expression->left;
4213 expression_t *right = expression->right;
4214 type_t *orig_type_left = left->base.datatype;
4215 type_t *orig_type_right = right->base.datatype;
4217 if(orig_type_left == NULL || orig_type_right == NULL)
4220 type_t *type_left = skip_typeref(orig_type_left);
4221 type_t *type_right = skip_typeref(orig_type_right);
4223 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4224 /* TODO: improve error message */
4225 errorf(HERE, "operation needs arithmetic types");
4229 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4230 expression->left = create_implicit_cast(left, arithmetic_type);
4231 expression->right = create_implicit_cast(right, arithmetic_type);
4232 expression->expression.datatype = arithmetic_type;
4235 static void semantic_shift_op(binary_expression_t *expression)
4237 expression_t *left = expression->left;
4238 expression_t *right = expression->right;
4239 type_t *orig_type_left = left->base.datatype;
4240 type_t *orig_type_right = right->base.datatype;
4242 if(orig_type_left == NULL || orig_type_right == NULL)
4245 type_t *type_left = skip_typeref(orig_type_left);
4246 type_t *type_right = skip_typeref(orig_type_right);
4248 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4249 /* TODO: improve error message */
4250 errorf(HERE, "operation needs integer types");
4254 type_left = promote_integer(type_left);
4255 type_right = promote_integer(type_right);
4257 expression->left = create_implicit_cast(left, type_left);
4258 expression->right = create_implicit_cast(right, type_right);
4259 expression->expression.datatype = type_left;
4262 static void semantic_add(binary_expression_t *expression)
4264 expression_t *left = expression->left;
4265 expression_t *right = expression->right;
4266 type_t *orig_type_left = left->base.datatype;
4267 type_t *orig_type_right = right->base.datatype;
4269 if(orig_type_left == NULL || orig_type_right == NULL)
4272 type_t *type_left = skip_typeref(orig_type_left);
4273 type_t *type_right = skip_typeref(orig_type_right);
4276 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4277 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4278 expression->left = create_implicit_cast(left, arithmetic_type);
4279 expression->right = create_implicit_cast(right, arithmetic_type);
4280 expression->expression.datatype = arithmetic_type;
4282 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4283 expression->expression.datatype = type_left;
4284 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4285 expression->expression.datatype = type_right;
4287 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4291 static void semantic_sub(binary_expression_t *expression)
4293 expression_t *left = expression->left;
4294 expression_t *right = expression->right;
4295 type_t *orig_type_left = left->base.datatype;
4296 type_t *orig_type_right = right->base.datatype;
4298 if(orig_type_left == NULL || orig_type_right == NULL)
4301 type_t *type_left = skip_typeref(orig_type_left);
4302 type_t *type_right = skip_typeref(orig_type_right);
4305 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4306 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4307 expression->left = create_implicit_cast(left, arithmetic_type);
4308 expression->right = create_implicit_cast(right, arithmetic_type);
4309 expression->expression.datatype = arithmetic_type;
4311 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4312 expression->expression.datatype = type_left;
4313 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4314 if(!pointers_compatible(type_left, type_right)) {
4315 errorf(HERE, "pointers to incompatible objects to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4317 expression->expression.datatype = type_ptrdiff_t;
4320 errorf(HERE, "invalid operands to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4324 static void semantic_comparison(binary_expression_t *expression)
4326 expression_t *left = expression->left;
4327 expression_t *right = expression->right;
4328 type_t *orig_type_left = left->base.datatype;
4329 type_t *orig_type_right = right->base.datatype;
4331 if(orig_type_left == NULL || orig_type_right == NULL)
4334 type_t *type_left = skip_typeref(orig_type_left);
4335 type_t *type_right = skip_typeref(orig_type_right);
4337 /* TODO non-arithmetic types */
4338 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4339 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4340 expression->left = create_implicit_cast(left, arithmetic_type);
4341 expression->right = create_implicit_cast(right, arithmetic_type);
4342 expression->expression.datatype = arithmetic_type;
4343 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4344 /* TODO check compatibility */
4345 } else if (is_type_pointer(type_left)) {
4346 expression->right = create_implicit_cast(right, type_left);
4347 } else if (is_type_pointer(type_right)) {
4348 expression->left = create_implicit_cast(left, type_right);
4350 type_error_incompatible("invalid operands in comparison",
4351 token.source_position, type_left, type_right);
4353 expression->expression.datatype = type_int;
4356 static void semantic_arithmetic_assign(binary_expression_t *expression)
4358 expression_t *left = expression->left;
4359 expression_t *right = expression->right;
4360 type_t *orig_type_left = left->base.datatype;
4361 type_t *orig_type_right = right->base.datatype;
4363 if(orig_type_left == NULL || orig_type_right == NULL)
4366 type_t *type_left = skip_typeref(orig_type_left);
4367 type_t *type_right = skip_typeref(orig_type_right);
4369 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4370 /* TODO: improve error message */
4371 errorf(HERE, "operation needs arithmetic types");
4375 /* combined instructions are tricky. We can't create an implicit cast on
4376 * the left side, because we need the uncasted form for the store.
4377 * The ast2firm pass has to know that left_type must be right_type
4378 * for the arithmetic operation and create a cast by itself */
4379 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4380 expression->right = create_implicit_cast(right, arithmetic_type);
4381 expression->expression.datatype = type_left;
4384 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4386 expression_t *left = expression->left;
4387 expression_t *right = expression->right;
4388 type_t *orig_type_left = left->base.datatype;
4389 type_t *orig_type_right = right->base.datatype;
4391 if(orig_type_left == NULL || orig_type_right == NULL)
4394 type_t *type_left = skip_typeref(orig_type_left);
4395 type_t *type_right = skip_typeref(orig_type_right);
4397 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4398 /* combined instructions are tricky. We can't create an implicit cast on
4399 * the left side, because we need the uncasted form for the store.
4400 * The ast2firm pass has to know that left_type must be right_type
4401 * for the arithmetic operation and create a cast by itself */
4402 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4403 expression->right = create_implicit_cast(right, arithmetic_type);
4404 expression->expression.datatype = type_left;
4405 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4406 expression->expression.datatype = type_left;
4408 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4414 * Check the semantic restrictions of a logical expression.
4416 static void semantic_logical_op(binary_expression_t *expression)
4418 expression_t *left = expression->left;
4419 expression_t *right = expression->right;
4420 type_t *orig_type_left = left->base.datatype;
4421 type_t *orig_type_right = right->base.datatype;
4423 if(orig_type_left == NULL || orig_type_right == NULL)
4426 type_t *type_left = skip_typeref(orig_type_left);
4427 type_t *type_right = skip_typeref(orig_type_right);
4429 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4430 /* TODO: improve error message */
4431 errorf(HERE, "operation needs scalar types");
4435 expression->expression.datatype = type_int;
4439 * Checks if a compound type has constant fields.
4441 static bool has_const_fields(const compound_type_t *type)
4443 const context_t *context = &type->declaration->context;
4444 const declaration_t *declaration = context->declarations;
4446 for (; declaration != NULL; declaration = declaration->next) {
4447 if (declaration->namespc != NAMESPACE_NORMAL)
4450 const type_t *decl_type = skip_typeref(declaration->type);
4451 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4459 * Check the semantic restrictions of a binary assign expression.
4461 static void semantic_binexpr_assign(binary_expression_t *expression)
4463 expression_t *left = expression->left;
4464 type_t *orig_type_left = left->base.datatype;
4466 if(orig_type_left == NULL)
4469 type_t *type_left = revert_automatic_type_conversion(left);
4470 type_left = skip_typeref(orig_type_left);
4472 /* must be a modifiable lvalue */
4473 if (is_type_array(type_left)) {
4474 errorf(HERE, "cannot assign to arrays ('%E')", left);
4477 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4478 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4482 if(is_type_incomplete(type_left)) {
4484 "left-hand side of assignment '%E' has incomplete type '%T'",
4485 left, orig_type_left);
4488 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4489 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4490 left, orig_type_left);
4494 semantic_assign(orig_type_left, &expression->right, "assignment");
4496 expression->expression.datatype = orig_type_left;
4499 static void semantic_comma(binary_expression_t *expression)
4501 expression->expression.datatype = expression->right->base.datatype;
4504 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4505 static expression_t *parse_##binexpression_type(unsigned precedence, \
4506 expression_t *left) \
4510 expression_t *right = parse_sub_expression(precedence + lr); \
4512 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4513 binexpr->binary.left = left; \
4514 binexpr->binary.right = right; \
4515 sfunc(&binexpr->binary); \
4520 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4521 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4522 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4523 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4524 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4525 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4526 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4527 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4528 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4530 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4531 semantic_comparison, 1)
4532 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4533 semantic_comparison, 1)
4534 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4535 semantic_comparison, 1)
4536 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4537 semantic_comparison, 1)
4539 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4540 semantic_binexpr_arithmetic, 1)
4541 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4542 semantic_binexpr_arithmetic, 1)
4543 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4544 semantic_binexpr_arithmetic, 1)
4545 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4546 semantic_logical_op, 1)
4547 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4548 semantic_logical_op, 1)
4549 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4550 semantic_shift_op, 1)
4551 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4552 semantic_shift_op, 1)
4553 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4554 semantic_arithmetic_addsubb_assign, 0)
4555 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4556 semantic_arithmetic_addsubb_assign, 0)
4557 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4558 semantic_arithmetic_assign, 0)
4559 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4560 semantic_arithmetic_assign, 0)
4561 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4562 semantic_arithmetic_assign, 0)
4563 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4564 semantic_arithmetic_assign, 0)
4565 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4566 semantic_arithmetic_assign, 0)
4567 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4568 semantic_arithmetic_assign, 0)
4569 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4570 semantic_arithmetic_assign, 0)
4571 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4572 semantic_arithmetic_assign, 0)
4574 static expression_t *parse_sub_expression(unsigned precedence)
4576 if(token.type < 0) {
4577 return expected_expression_error();
4580 expression_parser_function_t *parser
4581 = &expression_parsers[token.type];
4582 source_position_t source_position = token.source_position;
4585 if(parser->parser != NULL) {
4586 left = parser->parser(parser->precedence);
4588 left = parse_primary_expression();
4590 assert(left != NULL);
4591 left->base.source_position = source_position;
4594 if(token.type < 0) {
4595 return expected_expression_error();
4598 parser = &expression_parsers[token.type];
4599 if(parser->infix_parser == NULL)
4601 if(parser->infix_precedence < precedence)
4604 left = parser->infix_parser(parser->infix_precedence, left);
4606 assert(left != NULL);
4607 assert(left->kind != EXPR_UNKNOWN);
4608 left->base.source_position = source_position;
4615 * Parse an expression.
4617 static expression_t *parse_expression(void)
4619 return parse_sub_expression(1);
4623 * Register a parser for a prefix-like operator with given precedence.
4625 * @param parser the parser function
4626 * @param token_type the token type of the prefix token
4627 * @param precedence the precedence of the operator
4629 static void register_expression_parser(parse_expression_function parser,
4630 int token_type, unsigned precedence)
4632 expression_parser_function_t *entry = &expression_parsers[token_type];
4634 if(entry->parser != NULL) {
4635 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4636 panic("trying to register multiple expression parsers for a token");
4638 entry->parser = parser;
4639 entry->precedence = precedence;
4643 * Register a parser for an infix operator with given precedence.
4645 * @param parser the parser function
4646 * @param token_type the token type of the infix operator
4647 * @param precedence the precedence of the operator
4649 static void register_infix_parser(parse_expression_infix_function parser,
4650 int token_type, unsigned precedence)
4652 expression_parser_function_t *entry = &expression_parsers[token_type];
4654 if(entry->infix_parser != NULL) {
4655 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4656 panic("trying to register multiple infix expression parsers for a "
4659 entry->infix_parser = parser;
4660 entry->infix_precedence = precedence;
4664 * Initialize the expression parsers.
4666 static void init_expression_parsers(void)
4668 memset(&expression_parsers, 0, sizeof(expression_parsers));
4670 register_infix_parser(parse_array_expression, '[', 30);
4671 register_infix_parser(parse_call_expression, '(', 30);
4672 register_infix_parser(parse_select_expression, '.', 30);
4673 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4674 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4676 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4679 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4680 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4681 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4682 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4683 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4684 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4685 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4686 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4687 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4688 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4689 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4690 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4691 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4692 T_EXCLAMATIONMARKEQUAL, 13);
4693 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4694 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4695 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4696 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4697 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4698 register_infix_parser(parse_conditional_expression, '?', 7);
4699 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4700 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4701 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4702 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4703 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4704 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4705 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4706 T_LESSLESSEQUAL, 2);
4707 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4708 T_GREATERGREATEREQUAL, 2);
4709 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4711 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4713 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4716 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4718 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4719 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4720 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4721 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4722 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4723 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4724 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4726 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4728 register_expression_parser(parse_sizeof, T_sizeof, 25);
4729 register_expression_parser(parse_extension, T___extension__, 25);
4730 register_expression_parser(parse_builtin_classify_type,
4731 T___builtin_classify_type, 25);
4735 * Parse a asm statement constraints specification.
4737 static asm_constraint_t *parse_asm_constraints(void)
4739 asm_constraint_t *result = NULL;
4740 asm_constraint_t *last = NULL;
4742 while(token.type == T_STRING_LITERAL || token.type == '[') {
4743 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4744 memset(constraint, 0, sizeof(constraint[0]));
4746 if(token.type == '[') {
4748 if(token.type != T_IDENTIFIER) {
4749 parse_error_expected("while parsing asm constraint",
4753 constraint->symbol = token.v.symbol;
4758 constraint->constraints = parse_string_literals();
4760 constraint->expression = parse_expression();
4764 last->next = constraint;
4766 result = constraint;
4770 if(token.type != ',')
4779 * Parse a asm statement clobber specification.
4781 static asm_clobber_t *parse_asm_clobbers(void)
4783 asm_clobber_t *result = NULL;
4784 asm_clobber_t *last = NULL;
4786 while(token.type == T_STRING_LITERAL) {
4787 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4788 clobber->clobber = parse_string_literals();
4791 last->next = clobber;
4797 if(token.type != ',')
4806 * Parse an asm statement.
4808 static statement_t *parse_asm_statement(void)
4812 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4813 statement->base.source_position = token.source_position;
4815 asm_statement_t *asm_statement = &statement->asms;
4817 if(token.type == T_volatile) {
4819 asm_statement->is_volatile = true;
4823 asm_statement->asm_text = parse_string_literals();
4825 if(token.type != ':')
4829 asm_statement->inputs = parse_asm_constraints();
4830 if(token.type != ':')
4834 asm_statement->outputs = parse_asm_constraints();
4835 if(token.type != ':')
4839 asm_statement->clobbers = parse_asm_clobbers();
4848 * Parse a case statement.
4850 static statement_t *parse_case_statement(void)
4854 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4856 statement->base.source_position = token.source_position;
4857 statement->case_label.expression = parse_expression();
4861 if (current_switch != NULL) {
4862 /* link all cases into the switch statement */
4863 if (current_switch->last_case == NULL) {
4864 current_switch->first_case =
4865 current_switch->last_case = &statement->case_label;
4867 current_switch->last_case->next = &statement->case_label;
4870 errorf(statement->base.source_position,
4871 "case label not within a switch statement");
4873 statement->case_label.label_statement = parse_statement();
4879 * Parse a default statement.
4881 static statement_t *parse_default_statement(void)
4885 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4887 statement->base.source_position = token.source_position;
4890 statement->label.label_statement = parse_statement();
4896 * Return the declaration for a given label symbol or create a new one.
4898 static declaration_t *get_label(symbol_t *symbol)
4900 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4901 assert(current_function != NULL);
4902 /* if we found a label in the same function, then we already created the
4904 if(candidate != NULL
4905 && candidate->parent_context == ¤t_function->context) {
4909 /* otherwise we need to create a new one */
4910 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4911 declaration->namespc = NAMESPACE_LABEL;
4912 declaration->symbol = symbol;
4914 label_push(declaration);
4920 * Parse a label statement.
4922 static statement_t *parse_label_statement(void)
4924 assert(token.type == T_IDENTIFIER);
4925 symbol_t *symbol = token.v.symbol;
4928 declaration_t *label = get_label(symbol);
4930 /* if source position is already set then the label is defined twice,
4931 * otherwise it was just mentioned in a goto so far */
4932 if(label->source_position.input_name != NULL) {
4933 errorf(HERE, "duplicate label '%Y'", symbol);
4934 errorf(label->source_position, "previous definition of '%Y' was here",
4937 label->source_position = token.source_position;
4940 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4942 label_statement->statement.kind = STATEMENT_LABEL;
4943 label_statement->statement.source_position = token.source_position;
4944 label_statement->label = label;
4948 if(token.type == '}') {
4949 /* TODO only warn? */
4950 errorf(HERE, "label at end of compound statement");
4951 return (statement_t*) label_statement;
4953 label_statement->label_statement = parse_statement();
4956 return (statement_t*) label_statement;
4960 * Parse an if statement.
4962 static statement_t *parse_if(void)
4966 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4967 statement->statement.kind = STATEMENT_IF;
4968 statement->statement.source_position = token.source_position;
4971 statement->condition = parse_expression();
4974 statement->true_statement = parse_statement();
4975 if(token.type == T_else) {
4977 statement->false_statement = parse_statement();
4980 return (statement_t*) statement;
4984 * Parse a switch statement.
4986 static statement_t *parse_switch(void)
4990 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4991 statement->statement.kind = STATEMENT_SWITCH;
4992 statement->statement.source_position = token.source_position;
4995 expression_t *const expr = parse_expression();
4996 type_t *const type = promote_integer(skip_typeref(expr->base.datatype));
4997 statement->expression = create_implicit_cast(expr, type);
5000 switch_statement_t *rem = current_switch;
5001 current_switch = statement;
5002 statement->body = parse_statement();
5003 current_switch = rem;
5005 return (statement_t*) statement;
5009 * Parse a while statement.
5011 static statement_t *parse_while(void)
5015 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5016 statement->statement.kind = STATEMENT_WHILE;
5017 statement->statement.source_position = token.source_position;
5019 statement_t *rem = current_loop;
5021 statement->condition = parse_expression();
5024 statement->body = parse_statement();
5027 return (statement_t*) statement;
5034 * Parse a do statement.
5036 static statement_t *parse_do(void)
5040 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5041 statement->statement.kind = STATEMENT_DO_WHILE;
5042 statement->statement.source_position = token.source_position;
5044 statement_t *rem = current_loop;
5045 statement->body = parse_statement();
5046 expect_fail(T_while);
5048 statement->condition = parse_expression();
5053 return (statement_t*) statement;
5060 * Parse a for statement.
5062 static statement_t *parse_for(void)
5066 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5067 statement->statement.kind = STATEMENT_FOR;
5068 statement->statement.source_position = token.source_position;
5072 statement_t *rem = current_loop;
5073 int top = environment_top();
5074 context_t *last_context = context;
5075 set_context(&statement->context);
5077 if(token.type != ';') {
5078 if(is_declaration_specifier(&token, false)) {
5079 parse_declaration(record_declaration);
5081 statement->initialisation = parse_expression();
5088 if(token.type != ';') {
5089 statement->condition = parse_expression();
5092 if(token.type != ')') {
5093 statement->step = parse_expression();
5096 statement->body = parse_statement();
5098 assert(context == &statement->context);
5099 set_context(last_context);
5100 environment_pop_to(top);
5103 return (statement_t*) statement;
5110 * Parse a goto statement.
5112 static statement_t *parse_goto(void)
5116 if(token.type != T_IDENTIFIER) {
5117 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5121 symbol_t *symbol = token.v.symbol;
5124 declaration_t *label = get_label(symbol);
5126 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5128 statement->statement.kind = STATEMENT_GOTO;
5129 statement->statement.source_position = token.source_position;
5131 statement->label = label;
5135 return (statement_t*) statement;
5139 * Parse a continue statement.
5141 static statement_t *parse_continue(void)
5146 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5147 statement->kind = STATEMENT_CONTINUE;
5148 statement->base.source_position = token.source_position;
5150 if (current_loop == NULL) {
5151 errorf(HERE, "continue statement not within loop");
5159 * Parse a break statement.
5161 static statement_t *parse_break(void)
5166 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5167 statement->kind = STATEMENT_BREAK;
5168 statement->base.source_position = token.source_position;
5170 if (current_switch == NULL && current_loop == NULL) {
5171 errorf(HERE, "break statement not within loop or switch");
5178 * Check if a given declaration represents a local variable.
5180 static bool is_local_var_declaration(const declaration_t *declaration) {
5181 switch ((storage_class_tag_t) declaration->storage_class) {
5182 case STORAGE_CLASS_NONE:
5183 case STORAGE_CLASS_AUTO:
5184 case STORAGE_CLASS_REGISTER: {
5185 const type_t *type = skip_typeref(declaration->type);
5186 if(is_type_function(type)) {
5198 * Check if a given expression represents a local variable.
5200 static bool is_local_variable(const expression_t *expression)
5202 if (expression->base.kind != EXPR_REFERENCE) {
5205 const declaration_t *declaration = expression->reference.declaration;
5206 return is_local_var_declaration(declaration);
5210 * Parse a return statement.
5212 static statement_t *parse_return(void)
5216 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5218 statement->statement.kind = STATEMENT_RETURN;
5219 statement->statement.source_position = token.source_position;
5221 assert(is_type_function(current_function->type));
5222 function_type_t *function_type = ¤t_function->type->function;
5223 type_t *return_type = function_type->return_type;
5225 expression_t *return_value = NULL;
5226 if(token.type != ';') {
5227 return_value = parse_expression();
5231 if(return_type == NULL)
5232 return (statement_t*) statement;
5233 if(return_value != NULL && return_value->base.datatype == NULL)
5234 return (statement_t*) statement;
5236 return_type = skip_typeref(return_type);
5238 if(return_value != NULL) {
5239 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5241 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5242 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5243 warningf(statement->statement.source_position,
5244 "'return' with a value, in function returning void");
5245 return_value = NULL;
5247 if(return_type != NULL) {
5248 semantic_assign(return_type, &return_value, "'return'");
5251 /* check for returning address of a local var */
5252 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5253 const expression_t *expression = return_value->unary.value;
5254 if (is_local_variable(expression)) {
5255 warningf(statement->statement.source_position,
5256 "function returns address of local variable");
5260 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5261 warningf(statement->statement.source_position,
5262 "'return' without value, in function returning non-void");
5265 statement->return_value = return_value;
5267 return (statement_t*) statement;
5271 * Parse a declaration statement.
5273 static statement_t *parse_declaration_statement(void)
5275 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5277 statement->base.source_position = token.source_position;
5279 declaration_t *before = last_declaration;
5280 parse_declaration(record_declaration);
5282 if(before == NULL) {
5283 statement->declaration.declarations_begin = context->declarations;
5285 statement->declaration.declarations_begin = before->next;
5287 statement->declaration.declarations_end = last_declaration;
5293 * Parse an expression statement, ie. expr ';'.
5295 static statement_t *parse_expression_statement(void)
5297 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5299 statement->base.source_position = token.source_position;
5300 statement->expression.expression = parse_expression();
5308 * Parse a statement.
5310 static statement_t *parse_statement(void)
5312 statement_t *statement = NULL;
5314 /* declaration or statement */
5315 switch(token.type) {
5317 statement = parse_asm_statement();
5321 statement = parse_case_statement();
5325 statement = parse_default_statement();
5329 statement = parse_compound_statement();
5333 statement = parse_if();
5337 statement = parse_switch();
5341 statement = parse_while();
5345 statement = parse_do();
5349 statement = parse_for();
5353 statement = parse_goto();
5357 statement = parse_continue();
5361 statement = parse_break();
5365 statement = parse_return();
5374 if(look_ahead(1)->type == ':') {
5375 statement = parse_label_statement();
5379 if(is_typedef_symbol(token.v.symbol)) {
5380 statement = parse_declaration_statement();
5384 statement = parse_expression_statement();
5387 case T___extension__:
5388 /* this can be a prefix to a declaration or an expression statement */
5389 /* we simply eat it now and parse the rest with tail recursion */
5392 } while(token.type == T___extension__);
5393 statement = parse_statement();
5397 statement = parse_declaration_statement();
5401 statement = parse_expression_statement();
5405 assert(statement == NULL
5406 || statement->base.source_position.input_name != NULL);
5412 * Parse a compound statement.
5414 static statement_t *parse_compound_statement(void)
5416 compound_statement_t *compound_statement
5417 = allocate_ast_zero(sizeof(compound_statement[0]));
5418 compound_statement->statement.kind = STATEMENT_COMPOUND;
5419 compound_statement->statement.source_position = token.source_position;
5423 int top = environment_top();
5424 context_t *last_context = context;
5425 set_context(&compound_statement->context);
5427 statement_t *last_statement = NULL;
5429 while(token.type != '}' && token.type != T_EOF) {
5430 statement_t *statement = parse_statement();
5431 if(statement == NULL)
5434 if(last_statement != NULL) {
5435 last_statement->base.next = statement;
5437 compound_statement->statements = statement;
5440 while(statement->base.next != NULL)
5441 statement = statement->base.next;
5443 last_statement = statement;
5446 if(token.type == '}') {
5449 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5452 assert(context == &compound_statement->context);
5453 set_context(last_context);
5454 environment_pop_to(top);
5456 return (statement_t*) compound_statement;
5460 * Initialize builtin types.
5462 static void initialize_builtin_types(void)
5464 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5465 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5466 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5467 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5468 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5469 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5470 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5471 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5473 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5474 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5475 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5476 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5480 * Parse a translation unit.
5482 static translation_unit_t *parse_translation_unit(void)
5484 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5486 assert(global_context == NULL);
5487 global_context = &unit->context;
5489 assert(context == NULL);
5490 set_context(&unit->context);
5492 initialize_builtin_types();
5494 while(token.type != T_EOF) {
5495 if (token.type == ';') {
5496 /* TODO error in strict mode */
5497 warningf(HERE, "stray ';' outside of function");
5500 parse_external_declaration();
5504 assert(context == &unit->context);
5506 last_declaration = NULL;
5508 assert(global_context == &unit->context);
5509 global_context = NULL;
5517 * @return the translation unit or NULL if errors occurred.
5519 translation_unit_t *parse(void)
5521 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5522 label_stack = NEW_ARR_F(stack_entry_t, 0);
5523 diagnostic_count = 0;
5527 type_set_output(stderr);
5528 ast_set_output(stderr);
5530 lookahead_bufpos = 0;
5531 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5534 translation_unit_t *unit = parse_translation_unit();
5536 DEL_ARR_F(environment_stack);
5537 DEL_ARR_F(label_stack);
5546 * Initialize the parser.
5548 void init_parser(void)
5550 init_expression_parsers();
5551 obstack_init(&temp_obst);
5553 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5554 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5558 * Terminate the parser.
5560 void exit_parser(void)
5562 obstack_free(&temp_obst, NULL);