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 struct obstack temp_obst;
53 #define HERE token.source_position
55 static type_t *type_valist;
57 static statement_t *parse_compound_statement(void);
58 static statement_t *parse_statement(void);
60 static expression_t *parse_sub_expression(unsigned precedence);
61 static expression_t *parse_expression(void);
62 static type_t *parse_typename(void);
64 static void parse_compound_type_entries(void);
65 static declaration_t *parse_declarator(
66 const declaration_specifiers_t *specifiers, bool may_be_abstract);
67 static declaration_t *record_declaration(declaration_t *declaration);
69 static void semantic_comparison(binary_expression_t *expression);
71 #define STORAGE_CLASSES \
78 #define TYPE_QUALIFIERS \
85 #ifdef PROVIDE_COMPLEX
86 #define COMPLEX_SPECIFIERS \
88 #define IMAGINARY_SPECIFIERS \
91 #define COMPLEX_SPECIFIERS
92 #define IMAGINARY_SPECIFIERS
95 #define TYPE_SPECIFIERS \
110 case T___builtin_va_list: \
114 #define DECLARATION_START \
119 #define TYPENAME_START \
124 * Allocate an AST node with given size and
125 * initialize all fields with zero.
127 static void *allocate_ast_zero(size_t size)
129 void *res = allocate_ast(size);
130 memset(res, 0, size);
135 * Returns the size of a statement node.
137 * @param kind the statement kind
139 static size_t get_statement_struct_size(statement_kind_t kind)
141 static const size_t sizes[] = {
142 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
143 [STATEMENT_RETURN] = sizeof(return_statement_t),
144 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
145 [STATEMENT_IF] = sizeof(if_statement_t),
146 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
147 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
148 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
149 [STATEMENT_BREAK] = sizeof(statement_base_t),
150 [STATEMENT_GOTO] = sizeof(goto_statement_t),
151 [STATEMENT_LABEL] = sizeof(label_statement_t),
152 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
153 [STATEMENT_WHILE] = sizeof(while_statement_t),
154 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
155 [STATEMENT_FOR] = sizeof(for_statement_t),
156 [STATEMENT_ASM] = sizeof(asm_statement_t)
158 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
159 assert(sizes[kind] != 0);
164 * Allocate a statement node of given kind and initialize all
167 static statement_t *allocate_statement_zero(statement_kind_t kind)
169 size_t size = get_statement_struct_size(kind);
170 statement_t *res = allocate_ast_zero(size);
172 res->base.kind = kind;
177 * Returns the size of an expression node.
179 * @param kind the expression kind
181 static size_t get_expression_struct_size(expression_kind_t kind)
183 static const size_t sizes[] = {
184 [EXPR_INVALID] = sizeof(expression_base_t),
185 [EXPR_REFERENCE] = sizeof(reference_expression_t),
186 [EXPR_CONST] = sizeof(const_expression_t),
187 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
188 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
189 [EXPR_CALL] = sizeof(call_expression_t),
190 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
191 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
192 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
193 [EXPR_SELECT] = sizeof(select_expression_t),
194 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
195 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
196 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
197 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
198 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
199 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
200 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
201 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
202 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
203 [EXPR_VA_START] = sizeof(va_start_expression_t),
204 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
205 [EXPR_STATEMENT] = sizeof(statement_expression_t),
207 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
208 return sizes[EXPR_UNARY_FIRST];
210 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
211 return sizes[EXPR_BINARY_FIRST];
213 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
214 assert(sizes[kind] != 0);
219 * Allocate an expression node of given kind and initialize all
222 static expression_t *allocate_expression_zero(expression_kind_t kind)
224 size_t size = get_expression_struct_size(kind);
225 expression_t *res = allocate_ast_zero(size);
227 res->base.kind = kind;
232 * Returns the size of a type node.
234 * @param kind the type kind
236 static size_t get_type_struct_size(type_kind_t kind)
238 static const size_t sizes[] = {
239 [TYPE_ATOMIC] = sizeof(atomic_type_t),
240 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
241 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
242 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
243 [TYPE_ENUM] = sizeof(enum_type_t),
244 [TYPE_FUNCTION] = sizeof(function_type_t),
245 [TYPE_POINTER] = sizeof(pointer_type_t),
246 [TYPE_ARRAY] = sizeof(array_type_t),
247 [TYPE_BUILTIN] = sizeof(builtin_type_t),
248 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
249 [TYPE_TYPEOF] = sizeof(typeof_type_t),
251 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
252 assert(kind <= TYPE_TYPEOF);
253 assert(sizes[kind] != 0);
258 * Allocate a type node of given kind and initialize all
261 static type_t *allocate_type_zero(type_kind_t kind)
263 size_t size = get_type_struct_size(kind);
264 type_t *res = obstack_alloc(type_obst, size);
265 memset(res, 0, size);
267 res->base.kind = kind;
272 * Returns the size of an initializer node.
274 * @param kind the initializer kind
276 static size_t get_initializer_size(initializer_kind_t kind)
278 static const size_t sizes[] = {
279 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
280 [INITIALIZER_STRING] = sizeof(initializer_string_t),
281 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
282 [INITIALIZER_LIST] = sizeof(initializer_list_t)
284 assert(kind < sizeof(sizes) / sizeof(*sizes));
285 assert(sizes[kind] != 0);
290 * Allocate an initializer node of given kind and initialize all
293 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
295 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
302 * Free a type from the type obstack.
304 static void free_type(void *type)
306 obstack_free(type_obst, type);
310 * Returns the index of the top element of the environment stack.
312 static size_t environment_top(void)
314 return ARR_LEN(environment_stack);
318 * Returns the index of the top element of the label stack.
320 static size_t label_top(void)
322 return ARR_LEN(label_stack);
327 * Return the next token.
329 static inline void next_token(void)
331 token = lookahead_buffer[lookahead_bufpos];
332 lookahead_buffer[lookahead_bufpos] = lexer_token;
335 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
338 print_token(stderr, &token);
339 fprintf(stderr, "\n");
344 * Return the next token with a given lookahead.
346 static inline const token_t *look_ahead(int num)
348 assert(num > 0 && num <= MAX_LOOKAHEAD);
349 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
350 return &lookahead_buffer[pos];
353 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
356 * Report a parse error because an expected token was not found.
358 static void parse_error_expected(const char *message, ...)
360 if(message != NULL) {
361 errorf(HERE, "%s", message);
364 va_start(ap, message);
365 errorf(HERE, "got '%K', expected %#k", &token, &ap, ", ");
370 * Report a type error.
372 static void type_error(const char *msg, const source_position_t source_position,
375 errorf(source_position, "%s, but found type '%T'", msg, type);
379 * Report an incompatible type.
381 static void type_error_incompatible(const char *msg,
382 const source_position_t source_position, type_t *type1, type_t *type2)
384 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
388 * Eat an complete block, ie. '{ ... }'.
390 static void eat_block(void)
392 if(token.type == '{')
395 while(token.type != '}') {
396 if(token.type == T_EOF)
398 if(token.type == '{') {
408 * Eat a statement until an ';' token.
410 static void eat_statement(void)
412 while(token.type != ';') {
413 if(token.type == T_EOF)
415 if(token.type == '}')
417 if(token.type == '{') {
427 * Eat a parenthesed term, ie. '( ... )'.
429 static void eat_paren(void)
431 if(token.type == '(')
434 while(token.type != ')') {
435 if(token.type == T_EOF)
437 if(token.type == ')' || token.type == ';' || token.type == '}') {
440 if(token.type == '(') {
444 if(token.type == '{') {
453 #define expect(expected) \
454 if(UNLIKELY(token.type != (expected))) { \
455 parse_error_expected(NULL, (expected), 0); \
461 #define expect_block(expected) \
462 if(UNLIKELY(token.type != (expected))) { \
463 parse_error_expected(NULL, (expected), 0); \
469 #define expect_void(expected) \
470 if(UNLIKELY(token.type != (expected))) { \
471 parse_error_expected(NULL, (expected), 0); \
477 static void set_context(context_t *new_context)
479 context = new_context;
481 last_declaration = new_context->declarations;
482 if(last_declaration != NULL) {
483 while(last_declaration->next != NULL) {
484 last_declaration = last_declaration->next;
490 * Search a symbol in a given namespace and returns its declaration or
491 * NULL if this symbol was not found.
493 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
495 declaration_t *declaration = symbol->declaration;
496 for( ; declaration != NULL; declaration = declaration->symbol_next) {
497 if(declaration->namespc == namespc)
505 * pushs an environment_entry on the environment stack and links the
506 * corresponding symbol to the new entry
508 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
510 symbol_t *symbol = declaration->symbol;
511 namespace_t namespc = (namespace_t)declaration->namespc;
513 /* remember old declaration */
515 entry.symbol = symbol;
516 entry.old_declaration = symbol->declaration;
517 entry.namespc = (unsigned short) namespc;
518 ARR_APP1(stack_entry_t, *stack_ptr, entry);
520 /* replace/add declaration into declaration list of the symbol */
521 if(symbol->declaration == NULL) {
522 symbol->declaration = declaration;
524 declaration_t *iter_last = NULL;
525 declaration_t *iter = symbol->declaration;
526 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
527 /* replace an entry? */
528 if(iter->namespc == namespc) {
529 if(iter_last == NULL) {
530 symbol->declaration = declaration;
532 iter_last->symbol_next = declaration;
534 declaration->symbol_next = iter->symbol_next;
539 assert(iter_last->symbol_next == NULL);
540 iter_last->symbol_next = declaration;
545 static void environment_push(declaration_t *declaration)
547 assert(declaration->source_position.input_name != NULL);
548 assert(declaration->parent_context != NULL);
549 stack_push(&environment_stack, declaration);
552 static void label_push(declaration_t *declaration)
554 declaration->parent_context = ¤t_function->context;
555 stack_push(&label_stack, declaration);
559 * pops symbols from the environment stack until @p new_top is the top element
561 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
563 stack_entry_t *stack = *stack_ptr;
564 size_t top = ARR_LEN(stack);
567 assert(new_top <= top);
571 for(i = top; i > new_top; --i) {
572 stack_entry_t *entry = &stack[i - 1];
574 declaration_t *old_declaration = entry->old_declaration;
575 symbol_t *symbol = entry->symbol;
576 namespace_t namespc = (namespace_t)entry->namespc;
578 /* replace/remove declaration */
579 declaration_t *declaration = symbol->declaration;
580 assert(declaration != NULL);
581 if(declaration->namespc == namespc) {
582 if(old_declaration == NULL) {
583 symbol->declaration = declaration->symbol_next;
585 symbol->declaration = old_declaration;
588 declaration_t *iter_last = declaration;
589 declaration_t *iter = declaration->symbol_next;
590 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
591 /* replace an entry? */
592 if(iter->namespc == namespc) {
593 assert(iter_last != NULL);
594 iter_last->symbol_next = old_declaration;
595 old_declaration->symbol_next = iter->symbol_next;
599 assert(iter != NULL);
603 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
606 static void environment_pop_to(size_t new_top)
608 stack_pop_to(&environment_stack, new_top);
611 static void label_pop_to(size_t new_top)
613 stack_pop_to(&label_stack, new_top);
617 static int get_rank(const type_t *type)
619 assert(!is_typeref(type));
620 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
621 * and esp. footnote 108). However we can't fold constants (yet), so we
622 * can't decide whether unsigned int is possible, while int always works.
623 * (unsigned int would be preferable when possible... for stuff like
624 * struct { enum { ... } bla : 4; } ) */
625 if(type->kind == TYPE_ENUM)
626 return ATOMIC_TYPE_INT;
628 assert(type->kind == TYPE_ATOMIC);
629 const atomic_type_t *atomic_type = &type->atomic;
630 atomic_type_type_t atype = atomic_type->atype;
634 static type_t *promote_integer(type_t *type)
636 if(type->kind == TYPE_BITFIELD)
637 return promote_integer(type->bitfield.base);
639 if(get_rank(type) < ATOMIC_TYPE_INT)
646 * Create a cast expression.
648 * @param expression the expression to cast
649 * @param dest_type the destination type
651 static expression_t *create_cast_expression(expression_t *expression,
654 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
656 cast->unary.value = expression;
657 cast->base.datatype = dest_type;
663 * Check if a given expression represents the 0 pointer constant.
665 static bool is_null_pointer_constant(const expression_t *expression)
667 /* skip void* cast */
668 if(expression->kind == EXPR_UNARY_CAST
669 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
670 expression = expression->unary.value;
673 /* TODO: not correct yet, should be any constant integer expression
674 * which evaluates to 0 */
675 if (expression->kind != EXPR_CONST)
678 type_t *const type = skip_typeref(expression->base.datatype);
679 if (!is_type_integer(type))
682 return expression->conste.v.int_value == 0;
686 * Create an implicit cast expression.
688 * @param expression the expression to cast
689 * @param dest_type the destination type
691 static expression_t *create_implicit_cast(expression_t *expression,
694 type_t *source_type = expression->base.datatype;
696 if(source_type == NULL)
699 source_type = skip_typeref(source_type);
700 dest_type = skip_typeref(dest_type);
702 if(source_type == dest_type)
705 switch (dest_type->kind) {
707 /* TODO warning for implicitly converting to enum */
710 if (source_type->kind != TYPE_ATOMIC &&
711 source_type->kind != TYPE_ENUM &&
712 source_type->kind != TYPE_BITFIELD) {
713 panic("casting of non-atomic types not implemented yet");
716 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
717 type_error_incompatible("can't cast types",
718 expression->base.source_position, source_type,
723 return create_cast_expression(expression, dest_type);
726 switch (source_type->kind) {
728 if (is_null_pointer_constant(expression)) {
729 return create_cast_expression(expression, dest_type);
734 if (pointers_compatible(source_type, dest_type)) {
735 return create_cast_expression(expression, dest_type);
740 array_type_t *array_type = &source_type->array;
741 pointer_type_t *pointer_type = &dest_type->pointer;
742 if (types_compatible(array_type->element_type,
743 pointer_type->points_to)) {
744 return create_cast_expression(expression, dest_type);
750 panic("casting of non-atomic types not implemented yet");
753 type_error_incompatible("can't implicitly cast types",
754 expression->base.source_position, source_type, dest_type);
758 panic("casting of non-atomic types not implemented yet");
762 /** Implements the rules from § 6.5.16.1 */
763 static void semantic_assign(type_t *orig_type_left, expression_t **right,
766 type_t *orig_type_right = (*right)->base.datatype;
768 if(orig_type_right == NULL)
771 type_t *const type_left = skip_typeref(orig_type_left);
772 type_t *const type_right = skip_typeref(orig_type_right);
774 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
775 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
776 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
777 && is_type_pointer(type_right))) {
778 *right = create_implicit_cast(*right, type_left);
782 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
783 pointer_type_t *pointer_type_left = &type_left->pointer;
784 pointer_type_t *pointer_type_right = &type_right->pointer;
785 type_t *points_to_left = pointer_type_left->points_to;
786 type_t *points_to_right = pointer_type_right->points_to;
788 points_to_left = skip_typeref(points_to_left);
789 points_to_right = skip_typeref(points_to_right);
791 /* the left type has all qualifiers from the right type */
792 unsigned missing_qualifiers
793 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
794 if(missing_qualifiers != 0) {
795 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
799 points_to_left = get_unqualified_type(points_to_left);
800 points_to_right = get_unqualified_type(points_to_right);
802 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
803 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
804 && !types_compatible(points_to_left, points_to_right)) {
805 goto incompatible_assign_types;
808 *right = create_implicit_cast(*right, type_left);
812 if (is_type_compound(type_left)
813 && types_compatible(type_left, type_right)) {
814 *right = create_implicit_cast(*right, type_left);
818 incompatible_assign_types:
819 /* TODO: improve error message */
820 errorf(HERE, "incompatible types in %s", context);
821 errorf(HERE, "'%T' <- '%T'", orig_type_left, orig_type_right);
824 static expression_t *parse_constant_expression(void)
826 /* start parsing at precedence 7 (conditional expression) */
827 expression_t *result = parse_sub_expression(7);
829 if(!is_constant_expression(result)) {
830 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
836 static expression_t *parse_assignment_expression(void)
838 /* start parsing at precedence 2 (assignment expression) */
839 return parse_sub_expression(2);
842 static type_t *make_global_typedef(const char *name, type_t *type)
844 symbol_t *const symbol = symbol_table_insert(name);
846 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
847 declaration->namespc = NAMESPACE_NORMAL;
848 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
849 declaration->type = type;
850 declaration->symbol = symbol;
851 declaration->source_position = builtin_source_position;
853 record_declaration(declaration);
855 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
856 typedef_type->typedeft.declaration = declaration;
861 static const char *parse_string_literals(void)
863 assert(token.type == T_STRING_LITERAL);
864 const char *result = token.v.string;
868 while(token.type == T_STRING_LITERAL) {
869 result = concat_strings(result, token.v.string);
876 static void parse_attributes(void)
880 case T___attribute__: {
888 errorf(HERE, "EOF while parsing attribute");
907 if(token.type != T_STRING_LITERAL) {
908 parse_error_expected("while parsing assembler attribute",
913 parse_string_literals();
918 goto attributes_finished;
927 static designator_t *parse_designation(void)
929 if(token.type != '[' && token.type != '.')
932 designator_t *result = NULL;
933 designator_t *last = NULL;
936 designator_t *designator;
939 designator = allocate_ast_zero(sizeof(designator[0]));
941 designator->array_access = parse_constant_expression();
945 designator = allocate_ast_zero(sizeof(designator[0]));
947 if(token.type != T_IDENTIFIER) {
948 parse_error_expected("while parsing designator",
952 designator->symbol = token.v.symbol;
960 assert(designator != NULL);
962 last->next = designator;
971 static initializer_t *initializer_from_string(array_type_t *type,
974 /* TODO: check len vs. size of array type */
977 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
978 initializer->string.string = string;
983 static initializer_t *initializer_from_wide_string(array_type_t *const type,
984 wide_string_t *const string)
986 /* TODO: check len vs. size of array type */
989 initializer_t *const initializer =
990 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
991 initializer->wide_string.string = *string;
996 static initializer_t *initializer_from_expression(type_t *type,
997 expression_t *expression)
999 /* TODO check that expression is a constant expression */
1001 /* § 6.7.8.14/15 char array may be initialized by string literals */
1002 type_t *const expr_type = expression->base.datatype;
1003 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1004 array_type_t *const array_type = &type->array;
1005 type_t *const element_type = skip_typeref(array_type->element_type);
1007 if (element_type->kind == TYPE_ATOMIC) {
1008 switch (expression->kind) {
1009 case EXPR_STRING_LITERAL:
1010 if (element_type->atomic.atype == ATOMIC_TYPE_CHAR) {
1011 return initializer_from_string(array_type,
1012 expression->string.value);
1015 case EXPR_WIDE_STRING_LITERAL: {
1016 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1017 if (get_unqualified_type(element_type) == bare_wchar_type) {
1018 return initializer_from_wide_string(array_type,
1019 &expression->wide_string.value);
1029 type_t *expression_type = skip_typeref(expression->base.datatype);
1030 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1031 semantic_assign(type, &expression, "initializer");
1033 initializer_t *result = allocate_initializer_zero(INITIALIZER_VALUE);
1034 result->value.value = expression;
1042 static initializer_t *parse_sub_initializer(type_t *type,
1043 expression_t *expression,
1044 type_t *expression_type);
1046 static initializer_t *parse_sub_initializer_elem(type_t *type)
1048 if(token.type == '{') {
1049 return parse_sub_initializer(type, NULL, NULL);
1052 expression_t *expression = parse_assignment_expression();
1053 type_t *expression_type = skip_typeref(expression->base.datatype);
1055 return parse_sub_initializer(type, expression, expression_type);
1058 static bool had_initializer_brace_warning;
1060 static void skip_designator(void)
1063 if(token.type == '.') {
1065 if(token.type == T_IDENTIFIER)
1067 } else if(token.type == '[') {
1069 parse_constant_expression();
1070 if(token.type == ']')
1078 static initializer_t *parse_sub_initializer(type_t *type,
1079 expression_t *expression,
1080 type_t *expression_type)
1082 if(is_type_scalar(type)) {
1083 /* there might be extra {} hierarchies */
1084 if(token.type == '{') {
1086 if(!had_initializer_brace_warning) {
1087 warningf(HERE, "braces around scalar initializer");
1088 had_initializer_brace_warning = true;
1090 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1091 if(token.type == ',') {
1093 /* TODO: warn about excessive elements */
1099 if(expression == NULL) {
1100 expression = parse_assignment_expression();
1102 return initializer_from_expression(type, expression);
1105 /* does the expression match the currently looked at object to initialize */
1106 if(expression != NULL) {
1107 initializer_t *result = initializer_from_expression(type, expression);
1112 bool read_paren = false;
1113 if(token.type == '{') {
1118 /* descend into subtype */
1119 initializer_t *result = NULL;
1120 initializer_t **elems;
1121 if(is_type_array(type)) {
1122 array_type_t *array_type = &type->array;
1123 type_t *element_type = array_type->element_type;
1124 element_type = skip_typeref(element_type);
1126 if(token.type == '.') {
1128 "compound designator in initializer for array type '%T'",
1134 had_initializer_brace_warning = false;
1135 if(expression == NULL) {
1136 sub = parse_sub_initializer_elem(element_type);
1138 sub = parse_sub_initializer(element_type, expression,
1142 /* didn't match the subtypes -> try the parent type */
1144 assert(!read_paren);
1148 elems = NEW_ARR_F(initializer_t*, 0);
1149 ARR_APP1(initializer_t*, elems, sub);
1152 if(token.type == '}')
1155 if(token.type == '}')
1158 sub = parse_sub_initializer_elem(element_type);
1160 /* TODO error, do nicer cleanup */
1161 errorf(HERE, "member initializer didn't match");
1165 ARR_APP1(initializer_t*, elems, sub);
1168 assert(is_type_compound(type));
1169 compound_type_t *compound_type = &type->compound;
1170 context_t *context = &compound_type->declaration->context;
1172 if(token.type == '[') {
1174 "array designator in initializer for compound type '%T'",
1179 declaration_t *first = context->declarations;
1182 type_t *first_type = first->type;
1183 first_type = skip_typeref(first_type);
1186 had_initializer_brace_warning = false;
1187 if(expression == NULL) {
1188 sub = parse_sub_initializer_elem(first_type);
1190 sub = parse_sub_initializer(first_type, expression,expression_type);
1193 /* didn't match the subtypes -> try our parent type */
1195 assert(!read_paren);
1199 elems = NEW_ARR_F(initializer_t*, 0);
1200 ARR_APP1(initializer_t*, elems, sub);
1202 declaration_t *iter = first->next;
1203 for( ; iter != NULL; iter = iter->next) {
1204 if(iter->symbol == NULL)
1206 if(iter->namespc != NAMESPACE_NORMAL)
1209 if(token.type == '}')
1212 if(token.type == '}')
1215 type_t *iter_type = iter->type;
1216 iter_type = skip_typeref(iter_type);
1218 sub = parse_sub_initializer_elem(iter_type);
1220 /* TODO error, do nicer cleanup */
1221 errorf(HERE, "member initializer didn't match");
1225 ARR_APP1(initializer_t*, elems, sub);
1229 int len = ARR_LEN(elems);
1230 size_t elems_size = sizeof(initializer_t*) * len;
1232 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1234 init->initializer.kind = INITIALIZER_LIST;
1236 memcpy(init->initializers, elems, elems_size);
1239 result = (initializer_t*) init;
1242 if(token.type == ',')
1249 static initializer_t *parse_initializer(type_t *type)
1251 initializer_t *result;
1253 type = skip_typeref(type);
1255 if(token.type != '{') {
1256 expression_t *expression = parse_assignment_expression();
1257 initializer_t *initializer = initializer_from_expression(type, expression);
1258 if(initializer == NULL) {
1259 errorf(HERE, "initializer expression '%E', type '%T' is incompatible with type '%T'", expression, expression->base.datatype, type);
1264 if(is_type_scalar(type)) {
1268 expression_t *expression = parse_assignment_expression();
1269 result = initializer_from_expression(type, expression);
1271 if(token.type == ',')
1277 result = parse_sub_initializer(type, NULL, NULL);
1285 static declaration_t *parse_compound_type_specifier(bool is_struct)
1293 symbol_t *symbol = NULL;
1294 declaration_t *declaration = NULL;
1296 if (token.type == T___attribute__) {
1301 if(token.type == T_IDENTIFIER) {
1302 symbol = token.v.symbol;
1306 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1308 declaration = get_declaration(symbol, NAMESPACE_UNION);
1310 } else if(token.type != '{') {
1312 parse_error_expected("while parsing struct type specifier",
1313 T_IDENTIFIER, '{', 0);
1315 parse_error_expected("while parsing union type specifier",
1316 T_IDENTIFIER, '{', 0);
1322 if(declaration == NULL) {
1323 declaration = allocate_ast_zero(sizeof(declaration[0]));
1326 declaration->namespc = NAMESPACE_STRUCT;
1328 declaration->namespc = NAMESPACE_UNION;
1330 declaration->source_position = token.source_position;
1331 declaration->symbol = symbol;
1332 record_declaration(declaration);
1335 if(token.type == '{') {
1336 if(declaration->init.is_defined) {
1337 assert(symbol != NULL);
1338 errorf(HERE, "multiple definition of '%s %Y'",
1339 is_struct ? "struct" : "union", symbol);
1340 declaration->context.declarations = NULL;
1342 declaration->init.is_defined = true;
1344 int top = environment_top();
1345 context_t *last_context = context;
1346 set_context(&declaration->context);
1348 parse_compound_type_entries();
1351 assert(context == &declaration->context);
1352 set_context(last_context);
1353 environment_pop_to(top);
1359 static void parse_enum_entries(enum_type_t *const enum_type)
1363 if(token.type == '}') {
1365 errorf(HERE, "empty enum not allowed");
1370 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1372 if(token.type != T_IDENTIFIER) {
1373 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1377 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1378 entry->type = (type_t*) enum_type;
1379 entry->symbol = token.v.symbol;
1380 entry->source_position = token.source_position;
1383 if(token.type == '=') {
1385 entry->init.enum_value = parse_constant_expression();
1390 record_declaration(entry);
1392 if(token.type != ',')
1395 } while(token.type != '}');
1400 static type_t *parse_enum_specifier(void)
1404 declaration_t *declaration;
1407 if(token.type == T_IDENTIFIER) {
1408 symbol = token.v.symbol;
1411 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1412 } else if(token.type != '{') {
1413 parse_error_expected("while parsing enum type specifier",
1414 T_IDENTIFIER, '{', 0);
1421 if(declaration == NULL) {
1422 declaration = allocate_ast_zero(sizeof(declaration[0]));
1424 declaration->namespc = NAMESPACE_ENUM;
1425 declaration->source_position = token.source_position;
1426 declaration->symbol = symbol;
1429 type_t *const type = allocate_type_zero(TYPE_ENUM);
1430 type->enumt.declaration = declaration;
1432 if(token.type == '{') {
1433 if(declaration->init.is_defined) {
1434 errorf(HERE, "multiple definitions of enum %Y", symbol);
1436 record_declaration(declaration);
1437 declaration->init.is_defined = 1;
1439 parse_enum_entries(&type->enumt);
1447 * if a symbol is a typedef to another type, return true
1449 static bool is_typedef_symbol(symbol_t *symbol)
1451 const declaration_t *const declaration =
1452 get_declaration(symbol, NAMESPACE_NORMAL);
1454 declaration != NULL &&
1455 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1458 static type_t *parse_typeof(void)
1466 expression_t *expression = NULL;
1469 switch(token.type) {
1470 case T___extension__:
1471 /* this can be a prefix to a typename or an expression */
1472 /* we simply eat it now. */
1475 } while(token.type == T___extension__);
1479 if(is_typedef_symbol(token.v.symbol)) {
1480 type = parse_typename();
1482 expression = parse_expression();
1483 type = expression->base.datatype;
1488 type = parse_typename();
1492 expression = parse_expression();
1493 type = expression->base.datatype;
1499 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1500 typeof_type->typeoft.expression = expression;
1501 typeof_type->typeoft.typeof_type = type;
1507 SPECIFIER_SIGNED = 1 << 0,
1508 SPECIFIER_UNSIGNED = 1 << 1,
1509 SPECIFIER_LONG = 1 << 2,
1510 SPECIFIER_INT = 1 << 3,
1511 SPECIFIER_DOUBLE = 1 << 4,
1512 SPECIFIER_CHAR = 1 << 5,
1513 SPECIFIER_SHORT = 1 << 6,
1514 SPECIFIER_LONG_LONG = 1 << 7,
1515 SPECIFIER_FLOAT = 1 << 8,
1516 SPECIFIER_BOOL = 1 << 9,
1517 SPECIFIER_VOID = 1 << 10,
1518 #ifdef PROVIDE_COMPLEX
1519 SPECIFIER_COMPLEX = 1 << 11,
1520 SPECIFIER_IMAGINARY = 1 << 12,
1524 static type_t *create_builtin_type(symbol_t *const symbol,
1525 type_t *const real_type)
1527 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1528 type->builtin.symbol = symbol;
1529 type->builtin.real_type = real_type;
1531 type_t *result = typehash_insert(type);
1532 if (type != result) {
1539 static type_t *get_typedef_type(symbol_t *symbol)
1541 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1542 if(declaration == NULL
1543 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1546 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1547 type->typedeft.declaration = declaration;
1552 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1554 type_t *type = NULL;
1555 unsigned type_qualifiers = 0;
1556 unsigned type_specifiers = 0;
1559 specifiers->source_position = token.source_position;
1562 switch(token.type) {
1565 #define MATCH_STORAGE_CLASS(token, class) \
1567 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1568 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1570 specifiers->storage_class = class; \
1574 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1575 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1576 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1577 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1578 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1581 switch (specifiers->storage_class) {
1582 case STORAGE_CLASS_NONE:
1583 specifiers->storage_class = STORAGE_CLASS_THREAD;
1586 case STORAGE_CLASS_EXTERN:
1587 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1590 case STORAGE_CLASS_STATIC:
1591 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1595 errorf(HERE, "multiple storage classes in declaration specifiers");
1601 /* type qualifiers */
1602 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1604 type_qualifiers |= qualifier; \
1608 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1609 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1610 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1612 case T___extension__:
1617 /* type specifiers */
1618 #define MATCH_SPECIFIER(token, specifier, name) \
1621 if(type_specifiers & specifier) { \
1622 errorf(HERE, "multiple " name " type specifiers given"); \
1624 type_specifiers |= specifier; \
1628 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1629 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1630 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1631 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1632 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1633 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1634 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1635 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1636 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1637 #ifdef PROVIDE_COMPLEX
1638 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1639 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1642 /* only in microsoft mode */
1643 specifiers->decl_modifiers |= DM_FORCEINLINE;
1647 specifiers->is_inline = true;
1652 if(type_specifiers & SPECIFIER_LONG_LONG) {
1653 errorf(HERE, "multiple type specifiers given");
1654 } else if(type_specifiers & SPECIFIER_LONG) {
1655 type_specifiers |= SPECIFIER_LONG_LONG;
1657 type_specifiers |= SPECIFIER_LONG;
1661 /* TODO: if type != NULL for the following rules should issue
1664 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1666 type->compound.declaration = parse_compound_type_specifier(true);
1670 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1672 type->compound.declaration = parse_compound_type_specifier(false);
1676 type = parse_enum_specifier();
1679 type = parse_typeof();
1681 case T___builtin_va_list:
1682 type = duplicate_type(type_valist);
1686 case T___attribute__:
1691 case T_IDENTIFIER: {
1692 type_t *typedef_type = get_typedef_type(token.v.symbol);
1694 if(typedef_type == NULL)
1695 goto finish_specifiers;
1698 type = typedef_type;
1702 /* function specifier */
1704 goto finish_specifiers;
1711 atomic_type_type_t atomic_type;
1713 /* match valid basic types */
1714 switch(type_specifiers) {
1715 case SPECIFIER_VOID:
1716 atomic_type = ATOMIC_TYPE_VOID;
1718 case SPECIFIER_CHAR:
1719 atomic_type = ATOMIC_TYPE_CHAR;
1721 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1722 atomic_type = ATOMIC_TYPE_SCHAR;
1724 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1725 atomic_type = ATOMIC_TYPE_UCHAR;
1727 case SPECIFIER_SHORT:
1728 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1729 case SPECIFIER_SHORT | SPECIFIER_INT:
1730 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1731 atomic_type = ATOMIC_TYPE_SHORT;
1733 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1734 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1735 atomic_type = ATOMIC_TYPE_USHORT;
1738 case SPECIFIER_SIGNED:
1739 case SPECIFIER_SIGNED | SPECIFIER_INT:
1740 atomic_type = ATOMIC_TYPE_INT;
1742 case SPECIFIER_UNSIGNED:
1743 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1744 atomic_type = ATOMIC_TYPE_UINT;
1746 case SPECIFIER_LONG:
1747 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1748 case SPECIFIER_LONG | SPECIFIER_INT:
1749 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1750 atomic_type = ATOMIC_TYPE_LONG;
1752 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1753 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1754 atomic_type = ATOMIC_TYPE_ULONG;
1756 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1757 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1758 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1759 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1761 atomic_type = ATOMIC_TYPE_LONGLONG;
1763 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1764 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1766 atomic_type = ATOMIC_TYPE_ULONGLONG;
1768 case SPECIFIER_FLOAT:
1769 atomic_type = ATOMIC_TYPE_FLOAT;
1771 case SPECIFIER_DOUBLE:
1772 atomic_type = ATOMIC_TYPE_DOUBLE;
1774 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1775 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1777 case SPECIFIER_BOOL:
1778 atomic_type = ATOMIC_TYPE_BOOL;
1780 #ifdef PROVIDE_COMPLEX
1781 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1782 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1784 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1785 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1787 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1788 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1790 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1791 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1793 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1794 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1796 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1797 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1801 /* invalid specifier combination, give an error message */
1802 if(type_specifiers == 0) {
1803 if (! strict_mode) {
1804 warningf(HERE, "no type specifiers in declaration, using int");
1805 atomic_type = ATOMIC_TYPE_INT;
1808 errorf(HERE, "no type specifiers given in declaration");
1810 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1811 (type_specifiers & SPECIFIER_UNSIGNED)) {
1812 errorf(HERE, "signed and unsigned specifiers gives");
1813 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1814 errorf(HERE, "only integer types can be signed or unsigned");
1816 errorf(HERE, "multiple datatypes in declaration");
1818 atomic_type = ATOMIC_TYPE_INVALID;
1821 type = allocate_type_zero(TYPE_ATOMIC);
1822 type->atomic.atype = atomic_type;
1825 if(type_specifiers != 0) {
1826 errorf(HERE, "multiple datatypes in declaration");
1830 type->base.qualifiers = type_qualifiers;
1832 type_t *result = typehash_insert(type);
1833 if(newtype && result != type) {
1837 specifiers->type = result;
1840 static type_qualifiers_t parse_type_qualifiers(void)
1842 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1845 switch(token.type) {
1846 /* type qualifiers */
1847 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1848 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1849 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1852 return type_qualifiers;
1857 static declaration_t *parse_identifier_list(void)
1859 declaration_t *declarations = NULL;
1860 declaration_t *last_declaration = NULL;
1862 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1864 declaration->source_position = token.source_position;
1865 declaration->symbol = token.v.symbol;
1868 if(last_declaration != NULL) {
1869 last_declaration->next = declaration;
1871 declarations = declaration;
1873 last_declaration = declaration;
1875 if(token.type != ',')
1878 } while(token.type == T_IDENTIFIER);
1880 return declarations;
1883 static void semantic_parameter(declaration_t *declaration)
1885 /* TODO: improve error messages */
1887 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1888 errorf(HERE, "typedef not allowed in parameter list");
1889 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1890 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1891 errorf(HERE, "parameter may only have none or register storage class");
1894 type_t *orig_type = declaration->type;
1895 if(orig_type == NULL)
1897 type_t *type = skip_typeref(orig_type);
1899 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1900 * into a pointer. § 6.7.5.3 (7) */
1901 if (is_type_array(type)) {
1902 const array_type_t *arr_type = &type->array;
1903 type_t *element_type = arr_type->element_type;
1905 type = make_pointer_type(element_type, type->base.qualifiers);
1907 declaration->type = type;
1910 if(is_type_incomplete(type)) {
1911 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1912 orig_type, declaration->symbol);
1916 static declaration_t *parse_parameter(void)
1918 declaration_specifiers_t specifiers;
1919 memset(&specifiers, 0, sizeof(specifiers));
1921 parse_declaration_specifiers(&specifiers);
1923 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1925 semantic_parameter(declaration);
1930 static declaration_t *parse_parameters(function_type_t *type)
1932 if(token.type == T_IDENTIFIER) {
1933 symbol_t *symbol = token.v.symbol;
1934 if(!is_typedef_symbol(symbol)) {
1935 type->kr_style_parameters = true;
1936 return parse_identifier_list();
1940 if(token.type == ')') {
1941 type->unspecified_parameters = 1;
1944 if(token.type == T_void && look_ahead(1)->type == ')') {
1949 declaration_t *declarations = NULL;
1950 declaration_t *declaration;
1951 declaration_t *last_declaration = NULL;
1952 function_parameter_t *parameter;
1953 function_parameter_t *last_parameter = NULL;
1956 switch(token.type) {
1960 return declarations;
1963 case T___extension__:
1965 declaration = parse_parameter();
1967 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1968 memset(parameter, 0, sizeof(parameter[0]));
1969 parameter->type = declaration->type;
1971 if(last_parameter != NULL) {
1972 last_declaration->next = declaration;
1973 last_parameter->next = parameter;
1975 type->parameters = parameter;
1976 declarations = declaration;
1978 last_parameter = parameter;
1979 last_declaration = declaration;
1983 return declarations;
1985 if(token.type != ',')
1986 return declarations;
1996 } construct_type_type_t;
1998 typedef struct construct_type_t construct_type_t;
1999 struct construct_type_t {
2000 construct_type_type_t type;
2001 construct_type_t *next;
2004 typedef struct parsed_pointer_t parsed_pointer_t;
2005 struct parsed_pointer_t {
2006 construct_type_t construct_type;
2007 type_qualifiers_t type_qualifiers;
2010 typedef struct construct_function_type_t construct_function_type_t;
2011 struct construct_function_type_t {
2012 construct_type_t construct_type;
2013 type_t *function_type;
2016 typedef struct parsed_array_t parsed_array_t;
2017 struct parsed_array_t {
2018 construct_type_t construct_type;
2019 type_qualifiers_t type_qualifiers;
2025 typedef struct construct_base_type_t construct_base_type_t;
2026 struct construct_base_type_t {
2027 construct_type_t construct_type;
2031 static construct_type_t *parse_pointer_declarator(void)
2035 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2036 memset(pointer, 0, sizeof(pointer[0]));
2037 pointer->construct_type.type = CONSTRUCT_POINTER;
2038 pointer->type_qualifiers = parse_type_qualifiers();
2040 return (construct_type_t*) pointer;
2043 static construct_type_t *parse_array_declarator(void)
2047 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2048 memset(array, 0, sizeof(array[0]));
2049 array->construct_type.type = CONSTRUCT_ARRAY;
2051 if(token.type == T_static) {
2052 array->is_static = true;
2056 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2057 if(type_qualifiers != 0) {
2058 if(token.type == T_static) {
2059 array->is_static = true;
2063 array->type_qualifiers = type_qualifiers;
2065 if(token.type == '*' && look_ahead(1)->type == ']') {
2066 array->is_variable = true;
2068 } else if(token.type != ']') {
2069 array->size = parse_assignment_expression();
2074 return (construct_type_t*) array;
2077 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2081 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2083 declaration_t *parameters = parse_parameters(&type->function);
2084 if(declaration != NULL) {
2085 declaration->context.declarations = parameters;
2088 construct_function_type_t *construct_function_type =
2089 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2090 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2091 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2092 construct_function_type->function_type = type;
2096 return (construct_type_t*) construct_function_type;
2099 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2100 bool may_be_abstract)
2102 /* construct a single linked list of construct_type_t's which describe
2103 * how to construct the final declarator type */
2104 construct_type_t *first = NULL;
2105 construct_type_t *last = NULL;
2108 while(token.type == '*') {
2109 construct_type_t *type = parse_pointer_declarator();
2120 /* TODO: find out if this is correct */
2123 construct_type_t *inner_types = NULL;
2125 switch(token.type) {
2127 if(declaration == NULL) {
2128 errorf(HERE, "no identifier expected in typename");
2130 declaration->symbol = token.v.symbol;
2131 declaration->source_position = token.source_position;
2137 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2143 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2144 /* avoid a loop in the outermost scope, because eat_statement doesn't
2146 if(token.type == '}' && current_function == NULL) {
2154 construct_type_t *p = last;
2157 construct_type_t *type;
2158 switch(token.type) {
2160 type = parse_function_declarator(declaration);
2163 type = parse_array_declarator();
2166 goto declarator_finished;
2169 /* insert in the middle of the list (behind p) */
2171 type->next = p->next;
2182 declarator_finished:
2185 /* append inner_types at the end of the list, we don't to set last anymore
2186 * as it's not needed anymore */
2188 assert(first == NULL);
2189 first = inner_types;
2191 last->next = inner_types;
2197 static type_t *construct_declarator_type(construct_type_t *construct_list,
2200 construct_type_t *iter = construct_list;
2201 for( ; iter != NULL; iter = iter->next) {
2202 switch(iter->type) {
2203 case CONSTRUCT_INVALID:
2204 panic("invalid type construction found");
2205 case CONSTRUCT_FUNCTION: {
2206 construct_function_type_t *construct_function_type
2207 = (construct_function_type_t*) iter;
2209 type_t *function_type = construct_function_type->function_type;
2211 function_type->function.return_type = type;
2213 type = function_type;
2217 case CONSTRUCT_POINTER: {
2218 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2219 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2220 pointer_type->pointer.points_to = type;
2221 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2223 type = pointer_type;
2227 case CONSTRUCT_ARRAY: {
2228 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2229 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2231 array_type->base.qualifiers = parsed_array->type_qualifiers;
2232 array_type->array.element_type = type;
2233 array_type->array.is_static = parsed_array->is_static;
2234 array_type->array.is_variable = parsed_array->is_variable;
2235 array_type->array.size = parsed_array->size;
2242 type_t *hashed_type = typehash_insert(type);
2243 if(hashed_type != type) {
2244 /* the function type was constructed earlier freeing it here will
2245 * destroy other types... */
2246 if(iter->type != CONSTRUCT_FUNCTION) {
2256 static declaration_t *parse_declarator(
2257 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2259 type_t *type = specifiers->type;
2260 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2261 declaration->storage_class = specifiers->storage_class;
2262 declaration->modifiers = specifiers->decl_modifiers;
2263 declaration->is_inline = specifiers->is_inline;
2265 construct_type_t *construct_type
2266 = parse_inner_declarator(declaration, may_be_abstract);
2267 declaration->type = construct_declarator_type(construct_type, type);
2269 if(construct_type != NULL) {
2270 obstack_free(&temp_obst, construct_type);
2276 static type_t *parse_abstract_declarator(type_t *base_type)
2278 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2280 type_t *result = construct_declarator_type(construct_type, base_type);
2281 if(construct_type != NULL) {
2282 obstack_free(&temp_obst, construct_type);
2288 static declaration_t *append_declaration(declaration_t* const declaration)
2290 if (last_declaration != NULL) {
2291 last_declaration->next = declaration;
2293 context->declarations = declaration;
2295 last_declaration = declaration;
2299 static declaration_t *internal_record_declaration(
2300 declaration_t *const declaration,
2301 const bool is_function_definition)
2303 const symbol_t *const symbol = declaration->symbol;
2304 const namespace_t namespc = (namespace_t)declaration->namespc;
2306 const type_t *const type = skip_typeref(declaration->type);
2307 if (is_type_function(type) && type->function.unspecified_parameters) {
2308 warningf(declaration->source_position,
2309 "function declaration '%#T' is not a prototype",
2310 type, declaration->symbol);
2313 declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2314 assert(declaration != previous_declaration);
2315 if (previous_declaration != NULL
2316 && previous_declaration->parent_context == context) {
2317 const type_t *const prev_type = skip_typeref(previous_declaration->type);
2318 if (!types_compatible(type, prev_type)) {
2319 errorf(declaration->source_position,
2320 "declaration '%#T' is incompatible with previous declaration '%#T'",
2321 type, symbol, previous_declaration->type, symbol);
2322 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2324 unsigned old_storage_class = previous_declaration->storage_class;
2325 unsigned new_storage_class = declaration->storage_class;
2327 /* pretend no storage class means extern for function declarations
2328 * (except if the previous declaration is neither none nor extern) */
2329 if (is_type_function(type)) {
2330 switch (old_storage_class) {
2331 case STORAGE_CLASS_NONE:
2332 old_storage_class = STORAGE_CLASS_EXTERN;
2334 case STORAGE_CLASS_EXTERN:
2335 if (new_storage_class == STORAGE_CLASS_NONE && !is_function_definition) {
2336 new_storage_class = STORAGE_CLASS_EXTERN;
2344 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2345 new_storage_class == STORAGE_CLASS_EXTERN) {
2346 warn_redundant_declaration:
2347 warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
2348 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2349 } else if (current_function == NULL) {
2350 if (old_storage_class != STORAGE_CLASS_STATIC &&
2351 new_storage_class == STORAGE_CLASS_STATIC) {
2352 errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
2353 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2355 if (old_storage_class != STORAGE_CLASS_EXTERN) {
2356 goto warn_redundant_declaration;
2358 if (new_storage_class == STORAGE_CLASS_NONE) {
2359 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2363 if (old_storage_class == new_storage_class) {
2364 errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
2366 errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
2368 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2371 return previous_declaration;
2374 assert(declaration->parent_context == NULL);
2375 assert(declaration->symbol != NULL);
2376 assert(context != NULL);
2378 declaration->parent_context = context;
2380 environment_push(declaration);
2381 return append_declaration(declaration);
2384 static declaration_t *record_declaration(declaration_t *declaration)
2386 return internal_record_declaration(declaration, false);
2389 static declaration_t *record_function_definition(declaration_t *const declaration)
2391 return internal_record_declaration(declaration, true);
2394 static void parser_error_multiple_definition(declaration_t *declaration,
2395 const source_position_t source_position)
2397 errorf(source_position, "multiple definition of symbol '%Y'",
2398 declaration->symbol);
2399 errorf(declaration->source_position,
2400 "this is the location of the previous definition.");
2403 static bool is_declaration_specifier(const token_t *token,
2404 bool only_type_specifiers)
2406 switch(token->type) {
2410 return is_typedef_symbol(token->v.symbol);
2412 case T___extension__:
2415 return !only_type_specifiers;
2422 static void parse_init_declarator_rest(declaration_t *declaration)
2426 type_t *orig_type = declaration->type;
2427 type_t *type = NULL;
2428 if(orig_type != NULL)
2429 type = skip_typeref(orig_type);
2431 if(declaration->init.initializer != NULL) {
2432 parser_error_multiple_definition(declaration, token.source_position);
2435 initializer_t *initializer = parse_initializer(type);
2437 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2438 * the array type size */
2439 if(type != NULL && is_type_array(type) && initializer != NULL) {
2440 array_type_t *array_type = &type->array;
2442 if(array_type->size == NULL) {
2443 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2445 cnst->base.datatype = type_size_t;
2447 switch (initializer->kind) {
2448 case INITIALIZER_LIST: {
2449 initializer_list_t *const list = &initializer->list;
2450 cnst->conste.v.int_value = list->len;
2454 case INITIALIZER_STRING: {
2455 initializer_string_t *const string = &initializer->string;
2456 cnst->conste.v.int_value = strlen(string->string) + 1;
2460 case INITIALIZER_WIDE_STRING: {
2461 initializer_wide_string_t *const string = &initializer->wide_string;
2462 cnst->conste.v.int_value = string->string.size;
2467 panic("invalid initializer type");
2470 array_type->size = cnst;
2474 if(type != NULL && is_type_function(type)) {
2475 errorf(declaration->source_position,
2476 "initializers not allowed for function types at declator '%Y' (type '%T')",
2477 declaration->symbol, orig_type);
2479 declaration->init.initializer = initializer;
2483 /* parse rest of a declaration without any declarator */
2484 static void parse_anonymous_declaration_rest(
2485 const declaration_specifiers_t *specifiers,
2486 parsed_declaration_func finished_declaration)
2490 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2492 declaration->type = specifiers->type;
2493 declaration->storage_class = specifiers->storage_class;
2494 declaration->source_position = specifiers->source_position;
2496 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2497 warningf(declaration->source_position, "useless storage class in empty declaration");
2500 type_t *type = declaration->type;
2501 switch (type->kind) {
2502 case TYPE_COMPOUND_STRUCT:
2503 case TYPE_COMPOUND_UNION: {
2504 const compound_type_t *compound_type = &type->compound;
2505 if (compound_type->declaration->symbol == NULL) {
2506 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2515 warningf(declaration->source_position, "empty declaration");
2519 finished_declaration(declaration);
2522 static void parse_declaration_rest(declaration_t *ndeclaration,
2523 const declaration_specifiers_t *specifiers,
2524 parsed_declaration_func finished_declaration)
2527 declaration_t *declaration = finished_declaration(ndeclaration);
2529 type_t *orig_type = declaration->type;
2530 type_t *type = skip_typeref(orig_type);
2532 if(type->kind != TYPE_FUNCTION && declaration->is_inline) {
2533 warningf(declaration->source_position,
2534 "variable '%Y' declared 'inline'\n", declaration->symbol);
2537 if(token.type == '=') {
2538 parse_init_declarator_rest(declaration);
2541 if(token.type != ',')
2545 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2550 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2552 /* TODO: check that it was actually a parameter that gets a type */
2554 /* we should have a declaration for the parameter in the current
2556 return record_declaration(declaration);
2559 static void parse_declaration(parsed_declaration_func finished_declaration)
2561 declaration_specifiers_t specifiers;
2562 memset(&specifiers, 0, sizeof(specifiers));
2563 parse_declaration_specifiers(&specifiers);
2565 if(token.type == ';') {
2566 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2568 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2569 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2573 static void parse_kr_declaration_list(declaration_t *declaration)
2575 type_t *type = skip_typeref(declaration->type);
2576 if(!is_type_function(type))
2579 if(!type->function.kr_style_parameters)
2582 /* push function parameters */
2583 int top = environment_top();
2584 context_t *last_context = context;
2585 set_context(&declaration->context);
2587 declaration_t *parameter = declaration->context.declarations;
2588 for( ; parameter != NULL; parameter = parameter->next) {
2589 assert(parameter->parent_context == NULL);
2590 parameter->parent_context = context;
2591 environment_push(parameter);
2594 /* parse declaration list */
2595 while(is_declaration_specifier(&token, false)) {
2596 parse_declaration(finished_kr_declaration);
2599 /* pop function parameters */
2600 assert(context == &declaration->context);
2601 set_context(last_context);
2602 environment_pop_to(top);
2604 /* update function type */
2605 type_t *new_type = duplicate_type(type);
2606 new_type->function.kr_style_parameters = false;
2608 function_parameter_t *parameters = NULL;
2609 function_parameter_t *last_parameter = NULL;
2611 declaration_t *parameter_declaration = declaration->context.declarations;
2612 for( ; parameter_declaration != NULL;
2613 parameter_declaration = parameter_declaration->next) {
2614 type_t *parameter_type = parameter_declaration->type;
2615 if(parameter_type == NULL) {
2617 errorf(HERE, "no type specified for function parameter '%Y'",
2618 parameter_declaration->symbol);
2620 warningf(HERE, "no type specified for function parameter '%Y', using int",
2621 parameter_declaration->symbol);
2622 parameter_type = type_int;
2623 parameter_declaration->type = parameter_type;
2627 semantic_parameter(parameter_declaration);
2628 parameter_type = parameter_declaration->type;
2630 function_parameter_t *function_parameter
2631 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2632 memset(function_parameter, 0, sizeof(function_parameter[0]));
2634 function_parameter->type = parameter_type;
2635 if(last_parameter != NULL) {
2636 last_parameter->next = function_parameter;
2638 parameters = function_parameter;
2640 last_parameter = function_parameter;
2642 new_type->function.parameters = parameters;
2644 type = typehash_insert(new_type);
2645 if(type != new_type) {
2646 obstack_free(type_obst, new_type);
2649 declaration->type = type;
2652 static void parse_external_declaration(void)
2654 /* function-definitions and declarations both start with declaration
2656 declaration_specifiers_t specifiers;
2657 memset(&specifiers, 0, sizeof(specifiers));
2658 parse_declaration_specifiers(&specifiers);
2660 /* must be a declaration */
2661 if(token.type == ';') {
2662 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2666 /* declarator is common to both function-definitions and declarations */
2667 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2669 /* must be a declaration */
2670 if(token.type == ',' || token.type == '=' || token.type == ';') {
2671 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2675 /* must be a function definition */
2676 parse_kr_declaration_list(ndeclaration);
2678 if(token.type != '{') {
2679 parse_error_expected("while parsing function definition", '{', 0);
2684 type_t *type = ndeclaration->type;
2690 /* note that we don't skip typerefs: the standard doesn't allow them here
2691 * (so we can't use is_type_function here) */
2692 if(type->kind != TYPE_FUNCTION) {
2693 errorf(HERE, "declarator '%#T' has a body but is not a function type", type, ndeclaration->symbol);
2698 /* § 6.7.5.3 (14) a function definition with () means no
2699 * parameters (and not unspecified parameters) */
2700 if(type->function.unspecified_parameters) {
2701 type_t *duplicate = duplicate_type(type);
2702 duplicate->function.unspecified_parameters = false;
2704 type = typehash_insert(duplicate);
2705 if(type != duplicate) {
2706 obstack_free(type_obst, duplicate);
2708 ndeclaration->type = type;
2711 declaration_t *const declaration = record_function_definition(ndeclaration);
2712 if(ndeclaration != declaration) {
2713 declaration->context = ndeclaration->context;
2715 type = skip_typeref(declaration->type);
2717 /* push function parameters and switch context */
2718 int top = environment_top();
2719 context_t *last_context = context;
2720 set_context(&declaration->context);
2722 declaration_t *parameter = declaration->context.declarations;
2723 for( ; parameter != NULL; parameter = parameter->next) {
2724 assert(parameter->parent_context == NULL || parameter->parent_context == context);
2725 parameter->parent_context = context;
2726 environment_push(parameter);
2729 if(declaration->init.statement != NULL) {
2730 parser_error_multiple_definition(declaration, token.source_position);
2732 goto end_of_parse_external_declaration;
2734 /* parse function body */
2735 int label_stack_top = label_top();
2736 declaration_t *old_current_function = current_function;
2737 current_function = declaration;
2739 declaration->init.statement = parse_compound_statement();
2741 assert(current_function == declaration);
2742 current_function = old_current_function;
2743 label_pop_to(label_stack_top);
2746 end_of_parse_external_declaration:
2747 assert(context == &declaration->context);
2748 set_context(last_context);
2749 environment_pop_to(top);
2752 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2754 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2755 type->bitfield.base = base;
2756 type->bitfield.size = size;
2761 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2763 /* TODO: check constraints for struct declarations (in specifiers) */
2765 declaration_t *declaration;
2767 if(token.type == ':') {
2770 type_t *base_type = specifiers->type;
2771 expression_t *size = parse_constant_expression();
2773 type_t *type = make_bitfield_type(base_type, size);
2775 declaration = allocate_ast_zero(sizeof(declaration[0]));
2777 declaration->namespc = NAMESPACE_NORMAL;
2778 declaration->storage_class = STORAGE_CLASS_NONE;
2779 declaration->source_position = token.source_position;
2780 declaration->modifiers = specifiers->decl_modifiers;
2781 declaration->type = type;
2783 record_declaration(declaration);
2785 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2787 if(token.type == ':') {
2789 expression_t *size = parse_constant_expression();
2791 type_t *type = make_bitfield_type(declaration->type, size);
2792 declaration->type = type;
2795 record_declaration(declaration);
2797 if(token.type != ',')
2804 static void parse_compound_type_entries(void)
2808 while(token.type != '}' && token.type != T_EOF) {
2809 declaration_specifiers_t specifiers;
2810 memset(&specifiers, 0, sizeof(specifiers));
2811 parse_declaration_specifiers(&specifiers);
2813 parse_struct_declarators(&specifiers);
2815 if(token.type == T_EOF) {
2816 errorf(HERE, "EOF while parsing struct");
2821 static type_t *parse_typename(void)
2823 declaration_specifiers_t specifiers;
2824 memset(&specifiers, 0, sizeof(specifiers));
2825 parse_declaration_specifiers(&specifiers);
2826 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2827 /* TODO: improve error message, user does probably not know what a
2828 * storage class is...
2830 errorf(HERE, "typename may not have a storage class");
2833 type_t *result = parse_abstract_declarator(specifiers.type);
2841 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2842 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2843 expression_t *left);
2845 typedef struct expression_parser_function_t expression_parser_function_t;
2846 struct expression_parser_function_t {
2847 unsigned precedence;
2848 parse_expression_function parser;
2849 unsigned infix_precedence;
2850 parse_expression_infix_function infix_parser;
2853 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2856 * Creates a new invalid expression.
2858 static expression_t *create_invalid_expression(void)
2860 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2861 expression->base.source_position = token.source_position;
2865 static expression_t *expected_expression_error(void)
2867 errorf(HERE, "expected expression, got token '%K'", &token);
2871 return create_invalid_expression();
2875 * Parse a string constant.
2877 static expression_t *parse_string_const(void)
2879 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2880 cnst->base.datatype = type_string;
2881 cnst->string.value = parse_string_literals();
2887 * Parse a wide string constant.
2889 static expression_t *parse_wide_string_const(void)
2891 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2892 cnst->base.datatype = type_wchar_t_ptr;
2893 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2899 * Parse an integer constant.
2901 static expression_t *parse_int_const(void)
2903 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2904 cnst->base.datatype = token.datatype;
2905 cnst->conste.v.int_value = token.v.intvalue;
2913 * Parse a float constant.
2915 static expression_t *parse_float_const(void)
2917 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2918 cnst->base.datatype = token.datatype;
2919 cnst->conste.v.float_value = token.v.floatvalue;
2926 static declaration_t *create_implicit_function(symbol_t *symbol,
2927 const source_position_t source_position)
2929 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2930 ntype->function.return_type = type_int;
2931 ntype->function.unspecified_parameters = true;
2933 type_t *type = typehash_insert(ntype);
2938 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2940 declaration->storage_class = STORAGE_CLASS_EXTERN;
2941 declaration->type = type;
2942 declaration->symbol = symbol;
2943 declaration->source_position = source_position;
2944 declaration->parent_context = global_context;
2946 environment_push(declaration);
2947 declaration->next = context->declarations;
2948 context->declarations = declaration;
2954 * Creates a return_type (func)(argument_type) function type if not
2957 * @param return_type the return type
2958 * @param argument_type the argument type
2960 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2962 function_parameter_t *parameter
2963 = obstack_alloc(type_obst, sizeof(parameter[0]));
2964 memset(parameter, 0, sizeof(parameter[0]));
2965 parameter->type = argument_type;
2967 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2968 type->function.return_type = return_type;
2969 type->function.parameters = parameter;
2971 type_t *result = typehash_insert(type);
2972 if(result != type) {
2980 * Creates a function type for some function like builtins.
2982 * @param symbol the symbol describing the builtin
2984 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2986 switch(symbol->ID) {
2987 case T___builtin_alloca:
2988 return make_function_1_type(type_void_ptr, type_size_t);
2989 case T___builtin_nan:
2990 return make_function_1_type(type_double, type_string);
2991 case T___builtin_nanf:
2992 return make_function_1_type(type_float, type_string);
2993 case T___builtin_nand:
2994 return make_function_1_type(type_long_double, type_string);
2995 case T___builtin_va_end:
2996 return make_function_1_type(type_void, type_valist);
2998 panic("not implemented builtin symbol found");
3003 * Performs automatic type cast as described in § 6.3.2.1.
3005 * @param orig_type the original type
3007 static type_t *automatic_type_conversion(type_t *orig_type)
3009 if(orig_type == NULL)
3012 type_t *type = skip_typeref(orig_type);
3013 if(is_type_array(type)) {
3014 array_type_t *array_type = &type->array;
3015 type_t *element_type = array_type->element_type;
3016 unsigned qualifiers = array_type->type.qualifiers;
3018 return make_pointer_type(element_type, qualifiers);
3021 if(is_type_function(type)) {
3022 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3029 * reverts the automatic casts of array to pointer types and function
3030 * to function-pointer types as defined § 6.3.2.1
3032 type_t *revert_automatic_type_conversion(const expression_t *expression)
3034 if(expression->base.datatype == NULL)
3037 switch(expression->kind) {
3038 case EXPR_REFERENCE: {
3039 const reference_expression_t *ref = &expression->reference;
3040 return ref->declaration->type;
3043 const select_expression_t *select = &expression->select;
3044 return select->compound_entry->type;
3046 case EXPR_UNARY_DEREFERENCE: {
3047 expression_t *value = expression->unary.value;
3048 type_t *type = skip_typeref(value->base.datatype);
3049 pointer_type_t *pointer_type = &type->pointer;
3051 return pointer_type->points_to;
3053 case EXPR_BUILTIN_SYMBOL: {
3054 const builtin_symbol_expression_t *builtin
3055 = &expression->builtin_symbol;
3056 return get_builtin_symbol_type(builtin->symbol);
3058 case EXPR_ARRAY_ACCESS: {
3059 const array_access_expression_t *array_access
3060 = &expression->array_access;
3061 const expression_t *array_ref = array_access->array_ref;
3062 type_t *type_left = skip_typeref(array_ref->base.datatype);
3063 assert(is_type_pointer(type_left));
3064 pointer_type_t *pointer_type = &type_left->pointer;
3065 return pointer_type->points_to;
3072 return expression->base.datatype;
3075 static expression_t *parse_reference(void)
3077 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3079 reference_expression_t *ref = &expression->reference;
3080 ref->symbol = token.v.symbol;
3082 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3084 source_position_t source_position = token.source_position;
3087 if(declaration == NULL) {
3088 if (! strict_mode && token.type == '(') {
3089 /* an implicitly defined function */
3090 warningf(HERE, "implicit declaration of function '%Y'",
3093 declaration = create_implicit_function(ref->symbol,
3096 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3101 type_t *type = declaration->type;
3103 /* we always do the auto-type conversions; the & and sizeof parser contains
3104 * code to revert this! */
3105 type = automatic_type_conversion(type);
3107 ref->declaration = declaration;
3108 ref->expression.datatype = type;
3113 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3117 /* TODO check if explicit cast is allowed and issue warnings/errors */
3120 static expression_t *parse_cast(void)
3122 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3124 cast->base.source_position = token.source_position;
3126 type_t *type = parse_typename();
3129 expression_t *value = parse_sub_expression(20);
3131 check_cast_allowed(value, type);
3133 cast->base.datatype = type;
3134 cast->unary.value = value;
3139 static expression_t *parse_statement_expression(void)
3141 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3143 statement_t *statement = parse_compound_statement();
3144 expression->statement.statement = statement;
3145 if(statement == NULL) {
3150 assert(statement->kind == STATEMENT_COMPOUND);
3151 compound_statement_t *compound_statement = &statement->compound;
3153 /* find last statement and use it's type */
3154 const statement_t *last_statement = NULL;
3155 const statement_t *iter = compound_statement->statements;
3156 for( ; iter != NULL; iter = iter->base.next) {
3157 last_statement = iter;
3160 if(last_statement->kind == STATEMENT_EXPRESSION) {
3161 const expression_statement_t *expression_statement
3162 = &last_statement->expression;
3163 expression->base.datatype
3164 = expression_statement->expression->base.datatype;
3166 expression->base.datatype = type_void;
3174 static expression_t *parse_brace_expression(void)
3178 switch(token.type) {
3180 /* gcc extension: a statement expression */
3181 return parse_statement_expression();
3185 return parse_cast();
3187 if(is_typedef_symbol(token.v.symbol)) {
3188 return parse_cast();
3192 expression_t *result = parse_expression();
3198 static expression_t *parse_function_keyword(void)
3203 if (current_function == NULL) {
3204 errorf(HERE, "'__func__' used outside of a function");
3207 string_literal_expression_t *expression
3208 = allocate_ast_zero(sizeof(expression[0]));
3210 expression->expression.kind = EXPR_FUNCTION;
3211 expression->expression.datatype = type_string;
3212 expression->value = current_function->symbol->string;
3214 return (expression_t*) expression;
3217 static expression_t *parse_pretty_function_keyword(void)
3219 eat(T___PRETTY_FUNCTION__);
3222 if (current_function == NULL) {
3223 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3226 string_literal_expression_t *expression
3227 = allocate_ast_zero(sizeof(expression[0]));
3229 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3230 expression->expression.datatype = type_string;
3231 expression->value = current_function->symbol->string;
3233 return (expression_t*) expression;
3236 static designator_t *parse_designator(void)
3238 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3240 if(token.type != T_IDENTIFIER) {
3241 parse_error_expected("while parsing member designator",
3246 result->symbol = token.v.symbol;
3249 designator_t *last_designator = result;
3251 if(token.type == '.') {
3253 if(token.type != T_IDENTIFIER) {
3254 parse_error_expected("while parsing member designator",
3259 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3260 designator->symbol = token.v.symbol;
3263 last_designator->next = designator;
3264 last_designator = designator;
3267 if(token.type == '[') {
3269 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3270 designator->array_access = parse_expression();
3271 if(designator->array_access == NULL) {
3277 last_designator->next = designator;
3278 last_designator = designator;
3287 static expression_t *parse_offsetof(void)
3289 eat(T___builtin_offsetof);
3291 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3292 expression->base.datatype = type_size_t;
3295 expression->offsetofe.type = parse_typename();
3297 expression->offsetofe.designator = parse_designator();
3303 static expression_t *parse_va_start(void)
3305 eat(T___builtin_va_start);
3307 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3310 expression->va_starte.ap = parse_assignment_expression();
3312 expression_t *const expr = parse_assignment_expression();
3313 if (expr->kind == EXPR_REFERENCE) {
3314 declaration_t *const decl = expr->reference.declaration;
3315 if (decl->parent_context == ¤t_function->context &&
3316 decl->next == NULL) {
3317 expression->va_starte.parameter = decl;
3322 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3324 return create_invalid_expression();
3327 static expression_t *parse_va_arg(void)
3329 eat(T___builtin_va_arg);
3331 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3334 expression->va_arge.ap = parse_assignment_expression();
3336 expression->base.datatype = parse_typename();
3342 static expression_t *parse_builtin_symbol(void)
3344 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3346 symbol_t *symbol = token.v.symbol;
3348 expression->builtin_symbol.symbol = symbol;
3351 type_t *type = get_builtin_symbol_type(symbol);
3352 type = automatic_type_conversion(type);
3354 expression->base.datatype = type;
3358 static expression_t *parse_builtin_constant(void)
3360 eat(T___builtin_constant_p);
3362 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3365 expression->builtin_constant.value = parse_assignment_expression();
3367 expression->base.datatype = type_int;
3372 static expression_t *parse_builtin_prefetch(void)
3374 eat(T___builtin_prefetch);
3376 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3379 expression->builtin_prefetch.adr = parse_assignment_expression();
3380 if (token.type == ',') {
3382 expression->builtin_prefetch.rw = parse_assignment_expression();
3384 if (token.type == ',') {
3386 expression->builtin_prefetch.locality = parse_assignment_expression();
3389 expression->base.datatype = type_void;
3394 static expression_t *parse_compare_builtin(void)
3396 expression_t *expression;
3398 switch(token.type) {
3399 case T___builtin_isgreater:
3400 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3402 case T___builtin_isgreaterequal:
3403 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3405 case T___builtin_isless:
3406 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3408 case T___builtin_islessequal:
3409 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3411 case T___builtin_islessgreater:
3412 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3414 case T___builtin_isunordered:
3415 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3418 panic("invalid compare builtin found");
3424 expression->binary.left = parse_assignment_expression();
3426 expression->binary.right = parse_assignment_expression();
3429 type_t *orig_type_left = expression->binary.left->base.datatype;
3430 type_t *orig_type_right = expression->binary.right->base.datatype;
3431 if(orig_type_left == NULL || orig_type_right == NULL)
3434 type_t *type_left = skip_typeref(orig_type_left);
3435 type_t *type_right = skip_typeref(orig_type_right);
3436 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3437 type_error_incompatible("invalid operands in comparison",
3438 token.source_position, type_left, type_right);
3440 semantic_comparison(&expression->binary);
3446 static expression_t *parse_builtin_expect(void)
3448 eat(T___builtin_expect);
3450 expression_t *expression
3451 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3454 expression->binary.left = parse_assignment_expression();
3456 expression->binary.right = parse_constant_expression();
3459 expression->base.datatype = expression->binary.left->base.datatype;
3464 static expression_t *parse_assume(void) {
3467 expression_t *expression
3468 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3471 expression->unary.value = parse_assignment_expression();
3474 expression->base.datatype = type_void;
3478 static expression_t *parse_alignof(void) {
3481 expression_t *expression
3482 = allocate_expression_zero(EXPR_ALIGNOF);
3485 expression->alignofe.type = parse_typename();
3488 expression->base.datatype = type_size_t;
3492 static expression_t *parse_primary_expression(void)
3494 switch(token.type) {
3496 return parse_int_const();
3497 case T_FLOATINGPOINT:
3498 return parse_float_const();
3499 case T_STRING_LITERAL:
3500 return parse_string_const();
3501 case T_WIDE_STRING_LITERAL:
3502 return parse_wide_string_const();
3504 return parse_reference();
3505 case T___FUNCTION__:
3507 return parse_function_keyword();
3508 case T___PRETTY_FUNCTION__:
3509 return parse_pretty_function_keyword();
3510 case T___builtin_offsetof:
3511 return parse_offsetof();
3512 case T___builtin_va_start:
3513 return parse_va_start();
3514 case T___builtin_va_arg:
3515 return parse_va_arg();
3516 case T___builtin_expect:
3517 return parse_builtin_expect();
3518 case T___builtin_nanf:
3519 case T___builtin_alloca:
3520 case T___builtin_va_end:
3521 return parse_builtin_symbol();
3522 case T___builtin_isgreater:
3523 case T___builtin_isgreaterequal:
3524 case T___builtin_isless:
3525 case T___builtin_islessequal:
3526 case T___builtin_islessgreater:
3527 case T___builtin_isunordered:
3528 return parse_compare_builtin();
3529 case T___builtin_constant_p:
3530 return parse_builtin_constant();
3531 case T___builtin_prefetch:
3532 return parse_builtin_prefetch();
3534 return parse_alignof();
3536 return parse_assume();
3539 return parse_brace_expression();
3542 errorf(HERE, "unexpected token '%K'", &token);
3545 return create_invalid_expression();
3548 static expression_t *parse_array_expression(unsigned precedence,
3555 expression_t *inside = parse_expression();
3557 array_access_expression_t *array_access
3558 = allocate_ast_zero(sizeof(array_access[0]));
3560 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3562 type_t *type_left = left->base.datatype;
3563 type_t *type_inside = inside->base.datatype;
3564 type_t *return_type = NULL;
3566 if(type_left != NULL && type_inside != NULL) {
3567 type_left = skip_typeref(type_left);
3568 type_inside = skip_typeref(type_inside);
3570 if(is_type_pointer(type_left)) {
3571 pointer_type_t *pointer = &type_left->pointer;
3572 return_type = pointer->points_to;
3573 array_access->array_ref = left;
3574 array_access->index = inside;
3575 } else if(is_type_pointer(type_inside)) {
3576 pointer_type_t *pointer = &type_inside->pointer;
3577 return_type = pointer->points_to;
3578 array_access->array_ref = inside;
3579 array_access->index = left;
3580 array_access->flipped = true;
3582 errorf(HERE, "array access on object with non-pointer types '%T', '%T'", type_left, type_inside);
3585 array_access->array_ref = left;
3586 array_access->index = inside;
3589 if(token.type != ']') {
3590 parse_error_expected("Problem while parsing array access", ']', 0);
3591 return (expression_t*) array_access;
3595 return_type = automatic_type_conversion(return_type);
3596 array_access->expression.datatype = return_type;
3598 return (expression_t*) array_access;
3601 static expression_t *parse_sizeof(unsigned precedence)
3605 sizeof_expression_t *sizeof_expression
3606 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3607 sizeof_expression->expression.kind = EXPR_SIZEOF;
3608 sizeof_expression->expression.datatype = type_size_t;
3610 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3612 sizeof_expression->type = parse_typename();
3615 expression_t *expression = parse_sub_expression(precedence);
3616 expression->base.datatype = revert_automatic_type_conversion(expression);
3618 sizeof_expression->type = expression->base.datatype;
3619 sizeof_expression->size_expression = expression;
3622 return (expression_t*) sizeof_expression;
3625 static expression_t *parse_select_expression(unsigned precedence,
3626 expression_t *compound)
3629 assert(token.type == '.' || token.type == T_MINUSGREATER);
3631 bool is_pointer = (token.type == T_MINUSGREATER);
3634 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3635 select->select.compound = compound;
3637 if(token.type != T_IDENTIFIER) {
3638 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3641 symbol_t *symbol = token.v.symbol;
3642 select->select.symbol = symbol;
3645 type_t *orig_type = compound->base.datatype;
3646 if(orig_type == NULL)
3647 return create_invalid_expression();
3649 type_t *type = skip_typeref(orig_type);
3651 type_t *type_left = type;
3653 if(type->kind != TYPE_POINTER) {
3654 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3655 return create_invalid_expression();
3657 pointer_type_t *pointer_type = &type->pointer;
3658 type_left = pointer_type->points_to;
3660 type_left = skip_typeref(type_left);
3662 if(type_left->kind != TYPE_COMPOUND_STRUCT
3663 && type_left->kind != TYPE_COMPOUND_UNION) {
3664 errorf(HERE, "request for member '%Y' in something not a struct or "
3665 "union, but '%T'", symbol, type_left);
3666 return create_invalid_expression();
3669 compound_type_t *compound_type = &type_left->compound;
3670 declaration_t *declaration = compound_type->declaration;
3672 if(!declaration->init.is_defined) {
3673 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3675 return create_invalid_expression();
3678 declaration_t *iter = declaration->context.declarations;
3679 for( ; iter != NULL; iter = iter->next) {
3680 if(iter->symbol == symbol) {
3685 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3686 return create_invalid_expression();
3689 /* we always do the auto-type conversions; the & and sizeof parser contains
3690 * code to revert this! */
3691 type_t *expression_type = automatic_type_conversion(iter->type);
3693 select->select.compound_entry = iter;
3694 select->base.datatype = expression_type;
3696 if(expression_type->kind == TYPE_BITFIELD) {
3697 expression_t *extract
3698 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3699 extract->unary.value = select;
3700 extract->base.datatype = expression_type->bitfield.base;
3709 * Parse a call expression, ie. expression '( ... )'.
3711 * @param expression the function address
3713 static expression_t *parse_call_expression(unsigned precedence,
3714 expression_t *expression)
3717 expression_t *result = allocate_expression_zero(EXPR_CALL);
3719 call_expression_t *call = &result->call;
3720 call->function = expression;
3722 function_type_t *function_type = NULL;
3723 type_t *orig_type = expression->base.datatype;
3724 if(orig_type != NULL) {
3725 type_t *type = skip_typeref(orig_type);
3727 if(is_type_pointer(type)) {
3728 pointer_type_t *pointer_type = &type->pointer;
3730 type = skip_typeref(pointer_type->points_to);
3732 if (is_type_function(type)) {
3733 function_type = &type->function;
3734 call->expression.datatype = function_type->return_type;
3737 if(function_type == NULL) {
3738 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3740 function_type = NULL;
3741 call->expression.datatype = NULL;
3745 /* parse arguments */
3748 if(token.type != ')') {
3749 call_argument_t *last_argument = NULL;
3752 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3754 argument->expression = parse_assignment_expression();
3755 if(last_argument == NULL) {
3756 call->arguments = argument;
3758 last_argument->next = argument;
3760 last_argument = argument;
3762 if(token.type != ',')
3769 if(function_type != NULL) {
3770 function_parameter_t *parameter = function_type->parameters;
3771 call_argument_t *argument = call->arguments;
3772 for( ; parameter != NULL && argument != NULL;
3773 parameter = parameter->next, argument = argument->next) {
3774 type_t *expected_type = parameter->type;
3775 /* TODO report context in error messages */
3776 argument->expression = create_implicit_cast(argument->expression,
3779 /* too few parameters */
3780 if(parameter != NULL) {
3781 errorf(HERE, "too few arguments to function '%E'", expression);
3782 } else if(argument != NULL) {
3783 /* too many parameters */
3784 if(!function_type->variadic
3785 && !function_type->unspecified_parameters) {
3786 errorf(HERE, "too many arguments to function '%E'", expression);
3788 /* do default promotion */
3789 for( ; argument != NULL; argument = argument->next) {
3790 type_t *type = argument->expression->base.datatype;
3795 type = skip_typeref(type);
3796 if(is_type_integer(type)) {
3797 type = promote_integer(type);
3798 } else if(type == type_float) {
3802 argument->expression
3803 = create_implicit_cast(argument->expression, type);
3806 check_format(&result->call);
3809 check_format(&result->call);
3816 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3818 static bool same_compound_type(const type_t *type1, const type_t *type2)
3820 if(!is_type_compound(type1))
3822 if(type1->kind != type2->kind)
3825 const compound_type_t *compound1 = &type1->compound;
3826 const compound_type_t *compound2 = &type2->compound;
3828 return compound1->declaration == compound2->declaration;
3832 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3834 * @param expression the conditional expression
3836 static expression_t *parse_conditional_expression(unsigned precedence,
3837 expression_t *expression)
3841 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3843 conditional_expression_t *conditional = &result->conditional;
3844 conditional->condition = expression;
3847 type_t *condition_type_orig = expression->base.datatype;
3848 if(condition_type_orig != NULL) {
3849 type_t *condition_type = skip_typeref(condition_type_orig);
3850 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3851 type_error("expected a scalar type in conditional condition",
3852 expression->base.source_position, condition_type_orig);
3856 expression_t *true_expression = parse_expression();
3858 expression_t *false_expression = parse_sub_expression(precedence);
3860 conditional->true_expression = true_expression;
3861 conditional->false_expression = false_expression;
3863 type_t *orig_true_type = true_expression->base.datatype;
3864 type_t *orig_false_type = false_expression->base.datatype;
3865 if(orig_true_type == NULL || orig_false_type == NULL)
3868 type_t *true_type = skip_typeref(orig_true_type);
3869 type_t *false_type = skip_typeref(orig_false_type);
3872 type_t *result_type = NULL;
3873 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3874 result_type = semantic_arithmetic(true_type, false_type);
3876 true_expression = create_implicit_cast(true_expression, result_type);
3877 false_expression = create_implicit_cast(false_expression, result_type);
3879 conditional->true_expression = true_expression;
3880 conditional->false_expression = false_expression;
3881 conditional->expression.datatype = result_type;
3882 } else if (same_compound_type(true_type, false_type)
3883 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3884 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3885 /* just take 1 of the 2 types */
3886 result_type = true_type;
3887 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3888 && pointers_compatible(true_type, false_type)) {
3890 result_type = true_type;
3893 type_error_incompatible("while parsing conditional",
3894 expression->base.source_position, true_type,
3898 conditional->expression.datatype = result_type;
3903 * Parse an extension expression.
3905 static expression_t *parse_extension(unsigned precedence)
3907 eat(T___extension__);
3909 /* TODO enable extensions */
3910 expression_t *expression = parse_sub_expression(precedence);
3911 /* TODO disable extensions */
3915 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3917 eat(T___builtin_classify_type);
3919 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3920 result->base.datatype = type_int;
3923 expression_t *expression = parse_sub_expression(precedence);
3925 result->classify_type.type_expression = expression;
3930 static void semantic_incdec(unary_expression_t *expression)
3932 type_t *orig_type = expression->value->base.datatype;
3933 if(orig_type == NULL)
3936 type_t *type = skip_typeref(orig_type);
3937 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3938 /* TODO: improve error message */
3939 errorf(HERE, "operation needs an arithmetic or pointer type");
3943 expression->expression.datatype = orig_type;
3946 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3948 type_t *orig_type = expression->value->base.datatype;
3949 if(orig_type == NULL)
3952 type_t *type = skip_typeref(orig_type);
3953 if(!is_type_arithmetic(type)) {
3954 /* TODO: improve error message */
3955 errorf(HERE, "operation needs an arithmetic type");
3959 expression->expression.datatype = orig_type;
3962 static void semantic_unexpr_scalar(unary_expression_t *expression)
3964 type_t *orig_type = expression->value->base.datatype;
3965 if(orig_type == NULL)
3968 type_t *type = skip_typeref(orig_type);
3969 if (!is_type_scalar(type)) {
3970 errorf(HERE, "operand of ! must be of scalar type");
3974 expression->expression.datatype = orig_type;
3977 static void semantic_unexpr_integer(unary_expression_t *expression)
3979 type_t *orig_type = expression->value->base.datatype;
3980 if(orig_type == NULL)
3983 type_t *type = skip_typeref(orig_type);
3984 if (!is_type_integer(type)) {
3985 errorf(HERE, "operand of ~ must be of integer type");
3989 expression->expression.datatype = orig_type;
3992 static void semantic_dereference(unary_expression_t *expression)
3994 type_t *orig_type = expression->value->base.datatype;
3995 if(orig_type == NULL)
3998 type_t *type = skip_typeref(orig_type);
3999 if(!is_type_pointer(type)) {
4000 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4004 pointer_type_t *pointer_type = &type->pointer;
4005 type_t *result_type = pointer_type->points_to;
4007 result_type = automatic_type_conversion(result_type);
4008 expression->expression.datatype = result_type;
4011 static void semantic_take_addr(unary_expression_t *expression)
4013 expression_t *value = expression->value;
4014 value->base.datatype = revert_automatic_type_conversion(value);
4016 type_t *orig_type = value->base.datatype;
4017 if(orig_type == NULL)
4020 if(value->kind == EXPR_REFERENCE) {
4021 reference_expression_t *reference = (reference_expression_t*) value;
4022 declaration_t *declaration = reference->declaration;
4023 if(declaration != NULL) {
4024 declaration->address_taken = 1;
4028 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4031 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4032 static expression_t *parse_##unexpression_type(unsigned precedence) \
4036 expression_t *unary_expression \
4037 = allocate_expression_zero(unexpression_type); \
4038 unary_expression->unary.value = parse_sub_expression(precedence); \
4040 sfunc(&unary_expression->unary); \
4042 return unary_expression; \
4045 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4046 semantic_unexpr_arithmetic)
4047 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4048 semantic_unexpr_arithmetic)
4049 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4050 semantic_unexpr_scalar)
4051 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4052 semantic_dereference)
4053 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4055 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4056 semantic_unexpr_integer)
4057 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4059 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4062 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4064 static expression_t *parse_##unexpression_type(unsigned precedence, \
4065 expression_t *left) \
4067 (void) precedence; \
4070 expression_t *unary_expression \
4071 = allocate_expression_zero(unexpression_type); \
4072 unary_expression->unary.value = left; \
4074 sfunc(&unary_expression->unary); \
4076 return unary_expression; \
4079 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4080 EXPR_UNARY_POSTFIX_INCREMENT,
4082 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4083 EXPR_UNARY_POSTFIX_DECREMENT,
4086 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4088 /* TODO: handle complex + imaginary types */
4090 /* § 6.3.1.8 Usual arithmetic conversions */
4091 if(type_left == type_long_double || type_right == type_long_double) {
4092 return type_long_double;
4093 } else if(type_left == type_double || type_right == type_double) {
4095 } else if(type_left == type_float || type_right == type_float) {
4099 type_right = promote_integer(type_right);
4100 type_left = promote_integer(type_left);
4102 if(type_left == type_right)
4105 bool signed_left = is_type_signed(type_left);
4106 bool signed_right = is_type_signed(type_right);
4107 int rank_left = get_rank(type_left);
4108 int rank_right = get_rank(type_right);
4109 if(rank_left < rank_right) {
4110 if(signed_left == signed_right || !signed_right) {
4116 if(signed_left == signed_right || !signed_left) {
4125 * Check the semantic restrictions for a binary expression.
4127 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4129 expression_t *left = expression->left;
4130 expression_t *right = expression->right;
4131 type_t *orig_type_left = left->base.datatype;
4132 type_t *orig_type_right = right->base.datatype;
4134 if(orig_type_left == NULL || orig_type_right == NULL)
4137 type_t *type_left = skip_typeref(orig_type_left);
4138 type_t *type_right = skip_typeref(orig_type_right);
4140 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4141 /* TODO: improve error message */
4142 errorf(HERE, "operation needs arithmetic types");
4146 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4147 expression->left = create_implicit_cast(left, arithmetic_type);
4148 expression->right = create_implicit_cast(right, arithmetic_type);
4149 expression->expression.datatype = arithmetic_type;
4152 static void semantic_shift_op(binary_expression_t *expression)
4154 expression_t *left = expression->left;
4155 expression_t *right = expression->right;
4156 type_t *orig_type_left = left->base.datatype;
4157 type_t *orig_type_right = right->base.datatype;
4159 if(orig_type_left == NULL || orig_type_right == NULL)
4162 type_t *type_left = skip_typeref(orig_type_left);
4163 type_t *type_right = skip_typeref(orig_type_right);
4165 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4166 /* TODO: improve error message */
4167 errorf(HERE, "operation needs integer types");
4171 type_left = promote_integer(type_left);
4172 type_right = promote_integer(type_right);
4174 expression->left = create_implicit_cast(left, type_left);
4175 expression->right = create_implicit_cast(right, type_right);
4176 expression->expression.datatype = type_left;
4179 static void semantic_add(binary_expression_t *expression)
4181 expression_t *left = expression->left;
4182 expression_t *right = expression->right;
4183 type_t *orig_type_left = left->base.datatype;
4184 type_t *orig_type_right = right->base.datatype;
4186 if(orig_type_left == NULL || orig_type_right == NULL)
4189 type_t *type_left = skip_typeref(orig_type_left);
4190 type_t *type_right = skip_typeref(orig_type_right);
4193 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4194 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4195 expression->left = create_implicit_cast(left, arithmetic_type);
4196 expression->right = create_implicit_cast(right, arithmetic_type);
4197 expression->expression.datatype = arithmetic_type;
4199 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4200 expression->expression.datatype = type_left;
4201 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4202 expression->expression.datatype = type_right;
4204 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4208 static void semantic_sub(binary_expression_t *expression)
4210 expression_t *left = expression->left;
4211 expression_t *right = expression->right;
4212 type_t *orig_type_left = left->base.datatype;
4213 type_t *orig_type_right = right->base.datatype;
4215 if(orig_type_left == NULL || orig_type_right == NULL)
4218 type_t *type_left = skip_typeref(orig_type_left);
4219 type_t *type_right = skip_typeref(orig_type_right);
4222 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4223 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4224 expression->left = create_implicit_cast(left, arithmetic_type);
4225 expression->right = create_implicit_cast(right, arithmetic_type);
4226 expression->expression.datatype = arithmetic_type;
4228 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4229 expression->expression.datatype = type_left;
4230 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4231 if(!pointers_compatible(type_left, type_right)) {
4232 errorf(HERE, "pointers to incompatible objects to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4234 expression->expression.datatype = type_ptrdiff_t;
4237 errorf(HERE, "invalid operands to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4241 static void semantic_comparison(binary_expression_t *expression)
4243 expression_t *left = expression->left;
4244 expression_t *right = expression->right;
4245 type_t *orig_type_left = left->base.datatype;
4246 type_t *orig_type_right = right->base.datatype;
4248 if(orig_type_left == NULL || orig_type_right == NULL)
4251 type_t *type_left = skip_typeref(orig_type_left);
4252 type_t *type_right = skip_typeref(orig_type_right);
4254 /* TODO non-arithmetic types */
4255 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4256 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4257 expression->left = create_implicit_cast(left, arithmetic_type);
4258 expression->right = create_implicit_cast(right, arithmetic_type);
4259 expression->expression.datatype = arithmetic_type;
4260 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4261 /* TODO check compatibility */
4262 } else if (is_type_pointer(type_left)) {
4263 expression->right = create_implicit_cast(right, type_left);
4264 } else if (is_type_pointer(type_right)) {
4265 expression->left = create_implicit_cast(left, type_right);
4267 type_error_incompatible("invalid operands in comparison",
4268 token.source_position, type_left, type_right);
4270 expression->expression.datatype = type_int;
4273 static void semantic_arithmetic_assign(binary_expression_t *expression)
4275 expression_t *left = expression->left;
4276 expression_t *right = expression->right;
4277 type_t *orig_type_left = left->base.datatype;
4278 type_t *orig_type_right = right->base.datatype;
4280 if(orig_type_left == NULL || orig_type_right == NULL)
4283 type_t *type_left = skip_typeref(orig_type_left);
4284 type_t *type_right = skip_typeref(orig_type_right);
4286 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4287 /* TODO: improve error message */
4288 errorf(HERE, "operation needs arithmetic types");
4292 /* combined instructions are tricky. We can't create an implicit cast on
4293 * the left side, because we need the uncasted form for the store.
4294 * The ast2firm pass has to know that left_type must be right_type
4295 * for the arithmetic operation and create a cast by itself */
4296 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4297 expression->right = create_implicit_cast(right, arithmetic_type);
4298 expression->expression.datatype = type_left;
4301 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4303 expression_t *left = expression->left;
4304 expression_t *right = expression->right;
4305 type_t *orig_type_left = left->base.datatype;
4306 type_t *orig_type_right = right->base.datatype;
4308 if(orig_type_left == NULL || orig_type_right == NULL)
4311 type_t *type_left = skip_typeref(orig_type_left);
4312 type_t *type_right = skip_typeref(orig_type_right);
4314 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4315 /* combined instructions are tricky. We can't create an implicit cast on
4316 * the left side, because we need the uncasted form for the store.
4317 * The ast2firm pass has to know that left_type must be right_type
4318 * for the arithmetic operation and create a cast by itself */
4319 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4320 expression->right = create_implicit_cast(right, arithmetic_type);
4321 expression->expression.datatype = type_left;
4322 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4323 expression->expression.datatype = type_left;
4325 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4331 * Check the semantic restrictions of a logical expression.
4333 static void semantic_logical_op(binary_expression_t *expression)
4335 expression_t *left = expression->left;
4336 expression_t *right = expression->right;
4337 type_t *orig_type_left = left->base.datatype;
4338 type_t *orig_type_right = right->base.datatype;
4340 if(orig_type_left == NULL || orig_type_right == NULL)
4343 type_t *type_left = skip_typeref(orig_type_left);
4344 type_t *type_right = skip_typeref(orig_type_right);
4346 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4347 /* TODO: improve error message */
4348 errorf(HERE, "operation needs scalar types");
4352 expression->expression.datatype = type_int;
4356 * Checks if a compound type has constant fields.
4358 static bool has_const_fields(const compound_type_t *type)
4360 const context_t *context = &type->declaration->context;
4361 const declaration_t *declaration = context->declarations;
4363 for (; declaration != NULL; declaration = declaration->next) {
4364 if (declaration->namespc != NAMESPACE_NORMAL)
4367 const type_t *decl_type = skip_typeref(declaration->type);
4368 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4376 * Check the semantic restrictions of a binary assign expression.
4378 static void semantic_binexpr_assign(binary_expression_t *expression)
4380 expression_t *left = expression->left;
4381 type_t *orig_type_left = left->base.datatype;
4383 if(orig_type_left == NULL)
4386 type_t *type_left = revert_automatic_type_conversion(left);
4387 type_left = skip_typeref(orig_type_left);
4389 /* must be a modifiable lvalue */
4390 if (is_type_array(type_left)) {
4391 errorf(HERE, "cannot assign to arrays ('%E')", left);
4394 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4395 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4399 if(is_type_incomplete(type_left)) {
4401 "left-hand side of assignment '%E' has incomplete type '%T'",
4402 left, orig_type_left);
4405 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4406 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4407 left, orig_type_left);
4411 semantic_assign(orig_type_left, &expression->right, "assignment");
4413 expression->expression.datatype = orig_type_left;
4416 static void semantic_comma(binary_expression_t *expression)
4418 expression->expression.datatype = expression->right->base.datatype;
4421 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4422 static expression_t *parse_##binexpression_type(unsigned precedence, \
4423 expression_t *left) \
4427 expression_t *right = parse_sub_expression(precedence + lr); \
4429 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4430 binexpr->binary.left = left; \
4431 binexpr->binary.right = right; \
4432 sfunc(&binexpr->binary); \
4437 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4438 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4439 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4440 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4441 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4442 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4443 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4444 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4445 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4447 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4448 semantic_comparison, 1)
4449 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4450 semantic_comparison, 1)
4451 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4452 semantic_comparison, 1)
4453 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4454 semantic_comparison, 1)
4456 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4457 semantic_binexpr_arithmetic, 1)
4458 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4459 semantic_binexpr_arithmetic, 1)
4460 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4461 semantic_binexpr_arithmetic, 1)
4462 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4463 semantic_logical_op, 1)
4464 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4465 semantic_logical_op, 1)
4466 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4467 semantic_shift_op, 1)
4468 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4469 semantic_shift_op, 1)
4470 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4471 semantic_arithmetic_addsubb_assign, 0)
4472 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4473 semantic_arithmetic_addsubb_assign, 0)
4474 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4475 semantic_arithmetic_assign, 0)
4476 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4477 semantic_arithmetic_assign, 0)
4478 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4479 semantic_arithmetic_assign, 0)
4480 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4481 semantic_arithmetic_assign, 0)
4482 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4483 semantic_arithmetic_assign, 0)
4484 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4485 semantic_arithmetic_assign, 0)
4486 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4487 semantic_arithmetic_assign, 0)
4488 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4489 semantic_arithmetic_assign, 0)
4491 static expression_t *parse_sub_expression(unsigned precedence)
4493 if(token.type < 0) {
4494 return expected_expression_error();
4497 expression_parser_function_t *parser
4498 = &expression_parsers[token.type];
4499 source_position_t source_position = token.source_position;
4502 if(parser->parser != NULL) {
4503 left = parser->parser(parser->precedence);
4505 left = parse_primary_expression();
4507 assert(left != NULL);
4508 left->base.source_position = source_position;
4511 if(token.type < 0) {
4512 return expected_expression_error();
4515 parser = &expression_parsers[token.type];
4516 if(parser->infix_parser == NULL)
4518 if(parser->infix_precedence < precedence)
4521 left = parser->infix_parser(parser->infix_precedence, left);
4523 assert(left != NULL);
4524 assert(left->kind != EXPR_UNKNOWN);
4525 left->base.source_position = source_position;
4532 * Parse an expression.
4534 static expression_t *parse_expression(void)
4536 return parse_sub_expression(1);
4540 * Register a parser for a prefix-like operator with given precedence.
4542 * @param parser the parser function
4543 * @param token_type the token type of the prefix token
4544 * @param precedence the precedence of the operator
4546 static void register_expression_parser(parse_expression_function parser,
4547 int token_type, unsigned precedence)
4549 expression_parser_function_t *entry = &expression_parsers[token_type];
4551 if(entry->parser != NULL) {
4552 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4553 panic("trying to register multiple expression parsers for a token");
4555 entry->parser = parser;
4556 entry->precedence = precedence;
4560 * Register a parser for an infix operator with given precedence.
4562 * @param parser the parser function
4563 * @param token_type the token type of the infix operator
4564 * @param precedence the precedence of the operator
4566 static void register_infix_parser(parse_expression_infix_function parser,
4567 int token_type, unsigned precedence)
4569 expression_parser_function_t *entry = &expression_parsers[token_type];
4571 if(entry->infix_parser != NULL) {
4572 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4573 panic("trying to register multiple infix expression parsers for a "
4576 entry->infix_parser = parser;
4577 entry->infix_precedence = precedence;
4581 * Initialize the expression parsers.
4583 static void init_expression_parsers(void)
4585 memset(&expression_parsers, 0, sizeof(expression_parsers));
4587 register_infix_parser(parse_array_expression, '[', 30);
4588 register_infix_parser(parse_call_expression, '(', 30);
4589 register_infix_parser(parse_select_expression, '.', 30);
4590 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4591 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4593 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4596 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4597 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4598 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4599 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4600 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4601 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4602 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4603 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4604 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4605 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4606 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4607 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4608 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4609 T_EXCLAMATIONMARKEQUAL, 13);
4610 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4611 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4612 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4613 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4614 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4615 register_infix_parser(parse_conditional_expression, '?', 7);
4616 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4617 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4618 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4619 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4620 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4621 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4622 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4623 T_LESSLESSEQUAL, 2);
4624 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4625 T_GREATERGREATEREQUAL, 2);
4626 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4628 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4630 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4633 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4635 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4636 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4637 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4638 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4639 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4640 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4641 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4643 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4645 register_expression_parser(parse_sizeof, T_sizeof, 25);
4646 register_expression_parser(parse_extension, T___extension__, 25);
4647 register_expression_parser(parse_builtin_classify_type,
4648 T___builtin_classify_type, 25);
4652 * Parse a asm statement constraints specification.
4654 static asm_constraint_t *parse_asm_constraints(void)
4656 asm_constraint_t *result = NULL;
4657 asm_constraint_t *last = NULL;
4659 while(token.type == T_STRING_LITERAL || token.type == '[') {
4660 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4661 memset(constraint, 0, sizeof(constraint[0]));
4663 if(token.type == '[') {
4665 if(token.type != T_IDENTIFIER) {
4666 parse_error_expected("while parsing asm constraint",
4670 constraint->symbol = token.v.symbol;
4675 constraint->constraints = parse_string_literals();
4677 constraint->expression = parse_expression();
4681 last->next = constraint;
4683 result = constraint;
4687 if(token.type != ',')
4696 * Parse a asm statement clobber specification.
4698 static asm_clobber_t *parse_asm_clobbers(void)
4700 asm_clobber_t *result = NULL;
4701 asm_clobber_t *last = NULL;
4703 while(token.type == T_STRING_LITERAL) {
4704 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4705 clobber->clobber = parse_string_literals();
4708 last->next = clobber;
4714 if(token.type != ',')
4723 * Parse an asm statement.
4725 static statement_t *parse_asm_statement(void)
4729 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4730 statement->base.source_position = token.source_position;
4732 asm_statement_t *asm_statement = &statement->asms;
4734 if(token.type == T_volatile) {
4736 asm_statement->is_volatile = true;
4740 asm_statement->asm_text = parse_string_literals();
4742 if(token.type != ':')
4746 asm_statement->inputs = parse_asm_constraints();
4747 if(token.type != ':')
4751 asm_statement->outputs = parse_asm_constraints();
4752 if(token.type != ':')
4756 asm_statement->clobbers = parse_asm_clobbers();
4765 * Parse a case statement.
4767 static statement_t *parse_case_statement(void)
4771 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4773 statement->base.source_position = token.source_position;
4774 statement->case_label.expression = parse_expression();
4777 statement->case_label.label_statement = parse_statement();
4783 * Parse a default statement.
4785 static statement_t *parse_default_statement(void)
4789 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4791 statement->base.source_position = token.source_position;
4794 statement->label.label_statement = parse_statement();
4800 * Return the declaration for a given label symbol or create a new one.
4802 static declaration_t *get_label(symbol_t *symbol)
4804 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4805 assert(current_function != NULL);
4806 /* if we found a label in the same function, then we already created the
4808 if(candidate != NULL
4809 && candidate->parent_context == ¤t_function->context) {
4813 /* otherwise we need to create a new one */
4814 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4815 declaration->namespc = NAMESPACE_LABEL;
4816 declaration->symbol = symbol;
4818 label_push(declaration);
4824 * Parse a label statement.
4826 static statement_t *parse_label_statement(void)
4828 assert(token.type == T_IDENTIFIER);
4829 symbol_t *symbol = token.v.symbol;
4832 declaration_t *label = get_label(symbol);
4834 /* if source position is already set then the label is defined twice,
4835 * otherwise it was just mentioned in a goto so far */
4836 if(label->source_position.input_name != NULL) {
4837 errorf(HERE, "duplicate label '%Y'", symbol);
4838 errorf(label->source_position, "previous definition of '%Y' was here",
4841 label->source_position = token.source_position;
4844 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4846 label_statement->statement.kind = STATEMENT_LABEL;
4847 label_statement->statement.source_position = token.source_position;
4848 label_statement->label = label;
4852 if(token.type == '}') {
4853 /* TODO only warn? */
4854 errorf(HERE, "label at end of compound statement");
4855 return (statement_t*) label_statement;
4857 label_statement->label_statement = parse_statement();
4860 return (statement_t*) label_statement;
4864 * Parse an if statement.
4866 static statement_t *parse_if(void)
4870 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4871 statement->statement.kind = STATEMENT_IF;
4872 statement->statement.source_position = token.source_position;
4875 statement->condition = parse_expression();
4878 statement->true_statement = parse_statement();
4879 if(token.type == T_else) {
4881 statement->false_statement = parse_statement();
4884 return (statement_t*) statement;
4888 * Parse a switch statement.
4890 static statement_t *parse_switch(void)
4894 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4895 statement->statement.kind = STATEMENT_SWITCH;
4896 statement->statement.source_position = token.source_position;
4899 statement->expression = parse_expression();
4901 statement->body = parse_statement();
4903 return (statement_t*) statement;
4907 * Parse a while statement.
4909 static statement_t *parse_while(void)
4913 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4914 statement->statement.kind = STATEMENT_WHILE;
4915 statement->statement.source_position = token.source_position;
4918 statement->condition = parse_expression();
4920 statement->body = parse_statement();
4922 return (statement_t*) statement;
4926 * Parse a do statement.
4928 static statement_t *parse_do(void)
4932 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4933 statement->statement.kind = STATEMENT_DO_WHILE;
4934 statement->statement.source_position = token.source_position;
4936 statement->body = parse_statement();
4939 statement->condition = parse_expression();
4943 return (statement_t*) statement;
4947 * Parse a for statement.
4949 static statement_t *parse_for(void)
4953 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4954 statement->statement.kind = STATEMENT_FOR;
4955 statement->statement.source_position = token.source_position;
4959 int top = environment_top();
4960 context_t *last_context = context;
4961 set_context(&statement->context);
4963 if(token.type != ';') {
4964 if(is_declaration_specifier(&token, false)) {
4965 parse_declaration(record_declaration);
4967 statement->initialisation = parse_expression();
4974 if(token.type != ';') {
4975 statement->condition = parse_expression();
4978 if(token.type != ')') {
4979 statement->step = parse_expression();
4982 statement->body = parse_statement();
4984 assert(context == &statement->context);
4985 set_context(last_context);
4986 environment_pop_to(top);
4988 return (statement_t*) statement;
4992 * Parse a goto statement.
4994 static statement_t *parse_goto(void)
4998 if(token.type != T_IDENTIFIER) {
4999 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5003 symbol_t *symbol = token.v.symbol;
5006 declaration_t *label = get_label(symbol);
5008 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5010 statement->statement.kind = STATEMENT_GOTO;
5011 statement->statement.source_position = token.source_position;
5013 statement->label = label;
5017 return (statement_t*) statement;
5021 * Parse a continue statement.
5023 static statement_t *parse_continue(void)
5028 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5029 statement->kind = STATEMENT_CONTINUE;
5030 statement->base.source_position = token.source_position;
5036 * Parse a break statement.
5038 static statement_t *parse_break(void)
5043 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5044 statement->kind = STATEMENT_BREAK;
5045 statement->base.source_position = token.source_position;
5051 * Parse a return statement.
5053 static statement_t *parse_return(void)
5057 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5059 statement->statement.kind = STATEMENT_RETURN;
5060 statement->statement.source_position = token.source_position;
5062 assert(is_type_function(current_function->type));
5063 function_type_t *function_type = ¤t_function->type->function;
5064 type_t *return_type = function_type->return_type;
5066 expression_t *return_value = NULL;
5067 if(token.type != ';') {
5068 return_value = parse_expression();
5072 if(return_type == NULL)
5073 return (statement_t*) statement;
5074 if(return_value != NULL && return_value->base.datatype == NULL)
5075 return (statement_t*) statement;
5077 return_type = skip_typeref(return_type);
5079 if(return_value != NULL) {
5080 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5082 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5083 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5084 warningf(HERE, "'return' with a value, in function returning void");
5085 return_value = NULL;
5087 if(return_type != NULL) {
5088 semantic_assign(return_type, &return_value, "'return'");
5092 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5093 warningf(HERE, "'return' without value, in function returning non-void");
5096 statement->return_value = return_value;
5098 return (statement_t*) statement;
5102 * Parse a declaration statement.
5104 static statement_t *parse_declaration_statement(void)
5106 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5108 statement->base.source_position = token.source_position;
5110 declaration_t *before = last_declaration;
5111 parse_declaration(record_declaration);
5113 if(before == NULL) {
5114 statement->declaration.declarations_begin = context->declarations;
5116 statement->declaration.declarations_begin = before->next;
5118 statement->declaration.declarations_end = last_declaration;
5124 * Parse an expression statement, ie. expr ';'.
5126 static statement_t *parse_expression_statement(void)
5128 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5130 statement->base.source_position = token.source_position;
5131 statement->expression.expression = parse_expression();
5139 * Parse a statement.
5141 static statement_t *parse_statement(void)
5143 statement_t *statement = NULL;
5145 /* declaration or statement */
5146 switch(token.type) {
5148 statement = parse_asm_statement();
5152 statement = parse_case_statement();
5156 statement = parse_default_statement();
5160 statement = parse_compound_statement();
5164 statement = parse_if();
5168 statement = parse_switch();
5172 statement = parse_while();
5176 statement = parse_do();
5180 statement = parse_for();
5184 statement = parse_goto();
5188 statement = parse_continue();
5192 statement = parse_break();
5196 statement = parse_return();
5205 if(look_ahead(1)->type == ':') {
5206 statement = parse_label_statement();
5210 if(is_typedef_symbol(token.v.symbol)) {
5211 statement = parse_declaration_statement();
5215 statement = parse_expression_statement();
5218 case T___extension__:
5219 /* this can be a prefix to a declaration or an expression statement */
5220 /* we simply eat it now and parse the rest with tail recursion */
5223 } while(token.type == T___extension__);
5224 statement = parse_statement();
5228 statement = parse_declaration_statement();
5232 statement = parse_expression_statement();
5236 assert(statement == NULL
5237 || statement->base.source_position.input_name != NULL);
5243 * Parse a compound statement.
5245 static statement_t *parse_compound_statement(void)
5247 compound_statement_t *compound_statement
5248 = allocate_ast_zero(sizeof(compound_statement[0]));
5249 compound_statement->statement.kind = STATEMENT_COMPOUND;
5250 compound_statement->statement.source_position = token.source_position;
5254 int top = environment_top();
5255 context_t *last_context = context;
5256 set_context(&compound_statement->context);
5258 statement_t *last_statement = NULL;
5260 while(token.type != '}' && token.type != T_EOF) {
5261 statement_t *statement = parse_statement();
5262 if(statement == NULL)
5265 if(last_statement != NULL) {
5266 last_statement->base.next = statement;
5268 compound_statement->statements = statement;
5271 while(statement->base.next != NULL)
5272 statement = statement->base.next;
5274 last_statement = statement;
5277 if(token.type == '}') {
5280 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5283 assert(context == &compound_statement->context);
5284 set_context(last_context);
5285 environment_pop_to(top);
5287 return (statement_t*) compound_statement;
5291 * Initialize builtin types.
5293 static void initialize_builtin_types(void)
5295 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5296 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5297 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5298 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5299 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5300 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5301 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5302 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5304 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5305 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5306 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5307 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5311 * Parse a translation unit.
5313 static translation_unit_t *parse_translation_unit(void)
5315 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5317 assert(global_context == NULL);
5318 global_context = &unit->context;
5320 assert(context == NULL);
5321 set_context(&unit->context);
5323 initialize_builtin_types();
5325 while(token.type != T_EOF) {
5326 if (token.type == ';') {
5327 /* TODO error in strict mode */
5328 warningf(HERE, "stray ';' outside of function");
5331 parse_external_declaration();
5335 assert(context == &unit->context);
5337 last_declaration = NULL;
5339 assert(global_context == &unit->context);
5340 global_context = NULL;
5348 * @return the translation unit or NULL if errors occurred.
5350 translation_unit_t *parse(void)
5352 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5353 label_stack = NEW_ARR_F(stack_entry_t, 0);
5354 diagnostic_count = 0;
5358 type_set_output(stderr);
5359 ast_set_output(stderr);
5361 lookahead_bufpos = 0;
5362 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5365 translation_unit_t *unit = parse_translation_unit();
5367 DEL_ARR_F(environment_stack);
5368 DEL_ARR_F(label_stack);
5377 * Initialize the parser.
5379 void init_parser(void)
5381 init_expression_parsers();
5382 obstack_init(&temp_obst);
5384 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5385 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5389 * Terminate the parser.
5391 void exit_parser(void)
5393 obstack_free(&temp_obst, NULL);