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 * Called when we find a 2nd declarator for an identifier we already have a
493 static bool is_compatible_declaration(declaration_t *declaration,
494 declaration_t *previous)
496 /* happens for K&R style function parameters */
497 if(previous->type == NULL) {
498 previous->type = declaration->type;
502 type_t *type1 = skip_typeref(declaration->type);
503 type_t *type2 = skip_typeref(previous->type);
505 return types_compatible(type1, type2);
509 * Search a symbol in a given namespace and returns its declaration or
510 * NULL if this symbol was not found.
512 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
514 declaration_t *declaration = symbol->declaration;
515 for( ; declaration != NULL; declaration = declaration->symbol_next) {
516 if(declaration->namespc == namespc)
524 * Return the "prefix" of a given namespace.
526 static const char *get_namespace_prefix(namespace_t namespc)
529 case NAMESPACE_NORMAL:
531 case NAMESPACE_UNION:
533 case NAMESPACE_STRUCT:
537 case NAMESPACE_LABEL:
540 panic("invalid namespace found");
544 * pushs an environment_entry on the environment stack and links the
545 * corresponding symbol to the new entry
547 static declaration_t *stack_push(stack_entry_t **stack_ptr,
548 declaration_t *declaration,
549 context_t *parent_context)
551 symbol_t *symbol = declaration->symbol;
552 namespace_t namespc = (namespace_t)declaration->namespc;
554 /* a declaration should be only pushed once */
555 declaration->parent_context = parent_context;
557 declaration_t *previous_declaration = get_declaration(symbol, namespc);
558 assert(declaration != previous_declaration);
559 if(previous_declaration != NULL
560 && previous_declaration->parent_context == context) {
561 if(!is_compatible_declaration(declaration, previous_declaration)) {
562 errorf(declaration->source_position, "definition of symbol '%s%Y' with type '%T'", get_namespace_prefix(namespc), symbol, declaration->type);
563 errorf(previous_declaration->source_position, "is incompatible with previous declaration of type '%T'", previous_declaration->type);
565 unsigned old_storage_class = previous_declaration->storage_class;
566 unsigned new_storage_class = declaration->storage_class;
567 type_t *type = previous_declaration->type;
568 type = skip_typeref(type);
570 if (current_function == NULL) {
571 if (old_storage_class != STORAGE_CLASS_STATIC &&
572 new_storage_class == STORAGE_CLASS_STATIC) {
573 errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
574 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
576 if (old_storage_class == STORAGE_CLASS_EXTERN) {
577 if (new_storage_class == STORAGE_CLASS_NONE) {
578 previous_declaration->storage_class = STORAGE_CLASS_NONE;
580 } else if(!is_type_function(type)) {
581 warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
582 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
586 if (old_storage_class == STORAGE_CLASS_EXTERN &&
587 new_storage_class == STORAGE_CLASS_EXTERN) {
588 warningf(declaration->source_position, "redundant extern declaration for '%Y'\n", symbol);
589 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here\n", symbol);
591 if (old_storage_class == new_storage_class) {
592 errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
594 errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
596 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
600 return previous_declaration;
603 /* remember old declaration */
605 entry.symbol = symbol;
606 entry.old_declaration = symbol->declaration;
607 entry.namespc = (unsigned short) namespc;
608 ARR_APP1(stack_entry_t, *stack_ptr, entry);
610 /* replace/add declaration into declaration list of the symbol */
611 if(symbol->declaration == NULL) {
612 symbol->declaration = declaration;
614 declaration_t *iter_last = NULL;
615 declaration_t *iter = symbol->declaration;
616 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
617 /* replace an entry? */
618 if(iter->namespc == namespc) {
619 if(iter_last == NULL) {
620 symbol->declaration = declaration;
622 iter_last->symbol_next = declaration;
624 declaration->symbol_next = iter->symbol_next;
629 assert(iter_last->symbol_next == NULL);
630 iter_last->symbol_next = declaration;
637 static declaration_t *environment_push(declaration_t *declaration)
639 assert(declaration->source_position.input_name != NULL);
640 return stack_push(&environment_stack, declaration, context);
643 static declaration_t *label_push(declaration_t *declaration)
645 return stack_push(&label_stack, declaration, ¤t_function->context);
649 * pops symbols from the environment stack until @p new_top is the top element
651 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
653 stack_entry_t *stack = *stack_ptr;
654 size_t top = ARR_LEN(stack);
657 assert(new_top <= top);
661 for(i = top; i > new_top; --i) {
662 stack_entry_t *entry = &stack[i - 1];
664 declaration_t *old_declaration = entry->old_declaration;
665 symbol_t *symbol = entry->symbol;
666 namespace_t namespc = (namespace_t)entry->namespc;
668 /* replace/remove declaration */
669 declaration_t *declaration = symbol->declaration;
670 assert(declaration != NULL);
671 if(declaration->namespc == namespc) {
672 if(old_declaration == NULL) {
673 symbol->declaration = declaration->symbol_next;
675 symbol->declaration = old_declaration;
678 declaration_t *iter_last = declaration;
679 declaration_t *iter = declaration->symbol_next;
680 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
681 /* replace an entry? */
682 if(iter->namespc == namespc) {
683 assert(iter_last != NULL);
684 iter_last->symbol_next = old_declaration;
685 old_declaration->symbol_next = iter->symbol_next;
689 assert(iter != NULL);
693 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
696 static void environment_pop_to(size_t new_top)
698 stack_pop_to(&environment_stack, new_top);
701 static void label_pop_to(size_t new_top)
703 stack_pop_to(&label_stack, new_top);
707 static int get_rank(const type_t *type)
709 assert(!is_typeref(type));
710 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
711 * and esp. footnote 108). However we can't fold constants (yet), so we
712 * can't decide whether unsigned int is possible, while int always works.
713 * (unsigned int would be preferable when possible... for stuff like
714 * struct { enum { ... } bla : 4; } ) */
715 if(type->kind == TYPE_ENUM)
716 return ATOMIC_TYPE_INT;
718 assert(type->kind == TYPE_ATOMIC);
719 const atomic_type_t *atomic_type = &type->atomic;
720 atomic_type_type_t atype = atomic_type->atype;
724 static type_t *promote_integer(type_t *type)
726 if(type->kind == TYPE_BITFIELD)
727 return promote_integer(type->bitfield.base);
729 if(get_rank(type) < ATOMIC_TYPE_INT)
736 * Create a cast expression.
738 * @param expression the expression to cast
739 * @param dest_type the destination type
741 static expression_t *create_cast_expression(expression_t *expression,
744 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
746 cast->unary.value = expression;
747 cast->base.datatype = dest_type;
753 * Check if a given expression represents the 0 pointer constant.
755 static bool is_null_pointer_constant(const expression_t *expression)
757 /* skip void* cast */
758 if(expression->kind == EXPR_UNARY_CAST
759 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
760 expression = expression->unary.value;
763 /* TODO: not correct yet, should be any constant integer expression
764 * which evaluates to 0 */
765 if (expression->kind != EXPR_CONST)
768 type_t *const type = skip_typeref(expression->base.datatype);
769 if (!is_type_integer(type))
772 return expression->conste.v.int_value == 0;
776 * Create an implicit cast expression.
778 * @param expression the expression to cast
779 * @param dest_type the destination type
781 static expression_t *create_implicit_cast(expression_t *expression,
784 type_t *source_type = expression->base.datatype;
786 if(source_type == NULL)
789 source_type = skip_typeref(source_type);
790 dest_type = skip_typeref(dest_type);
792 if(source_type == dest_type)
795 switch (dest_type->kind) {
797 /* TODO warning for implicitly converting to enum */
800 if (source_type->kind != TYPE_ATOMIC &&
801 source_type->kind != TYPE_ENUM &&
802 source_type->kind != TYPE_BITFIELD) {
803 panic("casting of non-atomic types not implemented yet");
806 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
807 type_error_incompatible("can't cast types",
808 expression->base.source_position, source_type,
813 return create_cast_expression(expression, dest_type);
816 switch (source_type->kind) {
818 if (is_null_pointer_constant(expression)) {
819 return create_cast_expression(expression, dest_type);
824 if (pointers_compatible(source_type, dest_type)) {
825 return create_cast_expression(expression, dest_type);
830 array_type_t *array_type = &source_type->array;
831 pointer_type_t *pointer_type = &dest_type->pointer;
832 if (types_compatible(array_type->element_type,
833 pointer_type->points_to)) {
834 return create_cast_expression(expression, dest_type);
840 panic("casting of non-atomic types not implemented yet");
843 type_error_incompatible("can't implicitly cast types",
844 expression->base.source_position, source_type, dest_type);
848 panic("casting of non-atomic types not implemented yet");
852 /** Implements the rules from § 6.5.16.1 */
853 static void semantic_assign(type_t *orig_type_left, expression_t **right,
856 type_t *orig_type_right = (*right)->base.datatype;
858 if(orig_type_right == NULL)
861 type_t *const type_left = skip_typeref(orig_type_left);
862 type_t *const type_right = skip_typeref(orig_type_right);
864 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
865 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
866 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
867 && is_type_pointer(type_right))) {
868 *right = create_implicit_cast(*right, type_left);
872 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
873 pointer_type_t *pointer_type_left = &type_left->pointer;
874 pointer_type_t *pointer_type_right = &type_right->pointer;
875 type_t *points_to_left = pointer_type_left->points_to;
876 type_t *points_to_right = pointer_type_right->points_to;
878 points_to_left = skip_typeref(points_to_left);
879 points_to_right = skip_typeref(points_to_right);
881 /* the left type has all qualifiers from the right type */
882 unsigned missing_qualifiers
883 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
884 if(missing_qualifiers != 0) {
885 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
889 points_to_left = get_unqualified_type(points_to_left);
890 points_to_right = get_unqualified_type(points_to_right);
892 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
893 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
894 && !types_compatible(points_to_left, points_to_right)) {
895 goto incompatible_assign_types;
898 *right = create_implicit_cast(*right, type_left);
902 if (is_type_compound(type_left)
903 && types_compatible(type_left, type_right)) {
904 *right = create_implicit_cast(*right, type_left);
908 incompatible_assign_types:
909 /* TODO: improve error message */
910 errorf(HERE, "incompatible types in %s", context);
911 errorf(HERE, "'%T' <- '%T'", orig_type_left, orig_type_right);
914 static expression_t *parse_constant_expression(void)
916 /* start parsing at precedence 7 (conditional expression) */
917 expression_t *result = parse_sub_expression(7);
919 if(!is_constant_expression(result)) {
920 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
926 static expression_t *parse_assignment_expression(void)
928 /* start parsing at precedence 2 (assignment expression) */
929 return parse_sub_expression(2);
932 static type_t *make_global_typedef(const char *name, type_t *type)
934 symbol_t *const symbol = symbol_table_insert(name);
936 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
937 declaration->namespc = NAMESPACE_NORMAL;
938 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
939 declaration->type = type;
940 declaration->symbol = symbol;
941 declaration->source_position = builtin_source_position;
943 record_declaration(declaration);
945 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
946 typedef_type->typedeft.declaration = declaration;
951 static const char *parse_string_literals(void)
953 assert(token.type == T_STRING_LITERAL);
954 const char *result = token.v.string;
958 while(token.type == T_STRING_LITERAL) {
959 result = concat_strings(result, token.v.string);
966 static void parse_attributes(void)
970 case T___attribute__: {
978 errorf(HERE, "EOF while parsing attribute");
997 if(token.type != T_STRING_LITERAL) {
998 parse_error_expected("while parsing assembler attribute",
1003 parse_string_literals();
1008 goto attributes_finished;
1012 attributes_finished:
1017 static designator_t *parse_designation(void)
1019 if(token.type != '[' && token.type != '.')
1022 designator_t *result = NULL;
1023 designator_t *last = NULL;
1026 designator_t *designator;
1027 switch(token.type) {
1029 designator = allocate_ast_zero(sizeof(designator[0]));
1031 designator->array_access = parse_constant_expression();
1035 designator = allocate_ast_zero(sizeof(designator[0]));
1037 if(token.type != T_IDENTIFIER) {
1038 parse_error_expected("while parsing designator",
1042 designator->symbol = token.v.symbol;
1050 assert(designator != NULL);
1052 last->next = designator;
1054 result = designator;
1061 static initializer_t *initializer_from_string(array_type_t *type,
1064 /* TODO: check len vs. size of array type */
1067 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1068 initializer->string.string = string;
1073 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1074 wide_string_t *const string)
1076 /* TODO: check len vs. size of array type */
1079 initializer_t *const initializer =
1080 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1081 initializer->wide_string.string = *string;
1086 static initializer_t *initializer_from_expression(type_t *type,
1087 expression_t *expression)
1089 /* TODO check that expression is a constant expression */
1091 /* § 6.7.8.14/15 char array may be initialized by string literals */
1092 type_t *const expr_type = expression->base.datatype;
1093 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1094 array_type_t *const array_type = &type->array;
1095 type_t *const element_type = skip_typeref(array_type->element_type);
1097 if (element_type->kind == TYPE_ATOMIC) {
1098 switch (expression->kind) {
1099 case EXPR_STRING_LITERAL:
1100 if (element_type->atomic.atype == ATOMIC_TYPE_CHAR) {
1101 return initializer_from_string(array_type,
1102 expression->string.value);
1105 case EXPR_WIDE_STRING_LITERAL: {
1106 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1107 if (get_unqualified_type(element_type) == bare_wchar_type) {
1108 return initializer_from_wide_string(array_type,
1109 &expression->wide_string.value);
1119 type_t *expression_type = skip_typeref(expression->base.datatype);
1120 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1121 semantic_assign(type, &expression, "initializer");
1123 initializer_t *result = allocate_initializer_zero(INITIALIZER_VALUE);
1124 result->value.value = expression;
1132 static initializer_t *parse_sub_initializer(type_t *type,
1133 expression_t *expression,
1134 type_t *expression_type);
1136 static initializer_t *parse_sub_initializer_elem(type_t *type)
1138 if(token.type == '{') {
1139 return parse_sub_initializer(type, NULL, NULL);
1142 expression_t *expression = parse_assignment_expression();
1143 type_t *expression_type = skip_typeref(expression->base.datatype);
1145 return parse_sub_initializer(type, expression, expression_type);
1148 static bool had_initializer_brace_warning;
1150 static void skip_designator(void)
1153 if(token.type == '.') {
1155 if(token.type == T_IDENTIFIER)
1157 } else if(token.type == '[') {
1159 parse_constant_expression();
1160 if(token.type == ']')
1168 static initializer_t *parse_sub_initializer(type_t *type,
1169 expression_t *expression,
1170 type_t *expression_type)
1172 if(is_type_scalar(type)) {
1173 /* there might be extra {} hierarchies */
1174 if(token.type == '{') {
1176 if(!had_initializer_brace_warning) {
1177 warningf(HERE, "braces around scalar initializer");
1178 had_initializer_brace_warning = true;
1180 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1181 if(token.type == ',') {
1183 /* TODO: warn about excessive elements */
1189 if(expression == NULL) {
1190 expression = parse_assignment_expression();
1192 return initializer_from_expression(type, expression);
1195 /* does the expression match the currently looked at object to initialize */
1196 if(expression != NULL) {
1197 initializer_t *result = initializer_from_expression(type, expression);
1202 bool read_paren = false;
1203 if(token.type == '{') {
1208 /* descend into subtype */
1209 initializer_t *result = NULL;
1210 initializer_t **elems;
1211 if(is_type_array(type)) {
1212 array_type_t *array_type = &type->array;
1213 type_t *element_type = array_type->element_type;
1214 element_type = skip_typeref(element_type);
1216 if(token.type == '.') {
1218 "compound designator in initializer for array type '%T'",
1224 had_initializer_brace_warning = false;
1225 if(expression == NULL) {
1226 sub = parse_sub_initializer_elem(element_type);
1228 sub = parse_sub_initializer(element_type, expression,
1232 /* didn't match the subtypes -> try the parent type */
1234 assert(!read_paren);
1238 elems = NEW_ARR_F(initializer_t*, 0);
1239 ARR_APP1(initializer_t*, elems, sub);
1242 if(token.type == '}')
1245 if(token.type == '}')
1248 sub = parse_sub_initializer_elem(element_type);
1250 /* TODO error, do nicer cleanup */
1251 errorf(HERE, "member initializer didn't match");
1255 ARR_APP1(initializer_t*, elems, sub);
1258 assert(is_type_compound(type));
1259 compound_type_t *compound_type = &type->compound;
1260 context_t *context = &compound_type->declaration->context;
1262 if(token.type == '[') {
1264 "array designator in initializer for compound type '%T'",
1269 declaration_t *first = context->declarations;
1272 type_t *first_type = first->type;
1273 first_type = skip_typeref(first_type);
1276 had_initializer_brace_warning = false;
1277 if(expression == NULL) {
1278 sub = parse_sub_initializer_elem(first_type);
1280 sub = parse_sub_initializer(first_type, expression,expression_type);
1283 /* didn't match the subtypes -> try our parent type */
1285 assert(!read_paren);
1289 elems = NEW_ARR_F(initializer_t*, 0);
1290 ARR_APP1(initializer_t*, elems, sub);
1292 declaration_t *iter = first->next;
1293 for( ; iter != NULL; iter = iter->next) {
1294 if(iter->symbol == NULL)
1296 if(iter->namespc != NAMESPACE_NORMAL)
1299 if(token.type == '}')
1302 if(token.type == '}')
1305 type_t *iter_type = iter->type;
1306 iter_type = skip_typeref(iter_type);
1308 sub = parse_sub_initializer_elem(iter_type);
1310 /* TODO error, do nicer cleanup */
1311 errorf(HERE, "member initializer didn't match");
1315 ARR_APP1(initializer_t*, elems, sub);
1319 int len = ARR_LEN(elems);
1320 size_t elems_size = sizeof(initializer_t*) * len;
1322 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1324 init->initializer.kind = INITIALIZER_LIST;
1326 memcpy(init->initializers, elems, elems_size);
1329 result = (initializer_t*) init;
1332 if(token.type == ',')
1339 static initializer_t *parse_initializer(type_t *type)
1341 initializer_t *result;
1343 type = skip_typeref(type);
1345 if(token.type != '{') {
1346 expression_t *expression = parse_assignment_expression();
1347 initializer_t *initializer = initializer_from_expression(type, expression);
1348 if(initializer == NULL) {
1349 errorf(HERE, "initializer expression '%E', type '%T' is incompatible with type '%T'", expression, expression->base.datatype, type);
1354 if(is_type_scalar(type)) {
1358 expression_t *expression = parse_assignment_expression();
1359 result = initializer_from_expression(type, expression);
1361 if(token.type == ',')
1367 result = parse_sub_initializer(type, NULL, NULL);
1375 static declaration_t *parse_compound_type_specifier(bool is_struct)
1383 symbol_t *symbol = NULL;
1384 declaration_t *declaration = NULL;
1386 if (token.type == T___attribute__) {
1391 if(token.type == T_IDENTIFIER) {
1392 symbol = token.v.symbol;
1396 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1398 declaration = get_declaration(symbol, NAMESPACE_UNION);
1400 } else if(token.type != '{') {
1402 parse_error_expected("while parsing struct type specifier",
1403 T_IDENTIFIER, '{', 0);
1405 parse_error_expected("while parsing union type specifier",
1406 T_IDENTIFIER, '{', 0);
1412 if(declaration == NULL) {
1413 declaration = allocate_ast_zero(sizeof(declaration[0]));
1416 declaration->namespc = NAMESPACE_STRUCT;
1418 declaration->namespc = NAMESPACE_UNION;
1420 declaration->source_position = token.source_position;
1421 declaration->symbol = symbol;
1422 record_declaration(declaration);
1425 if(token.type == '{') {
1426 if(declaration->init.is_defined) {
1427 assert(symbol != NULL);
1428 errorf(HERE, "multiple definition of %s %Y",
1429 is_struct ? "struct" : "union", symbol);
1430 declaration->context.declarations = NULL;
1432 declaration->init.is_defined = true;
1434 int top = environment_top();
1435 context_t *last_context = context;
1436 set_context(&declaration->context);
1438 parse_compound_type_entries();
1441 assert(context == &declaration->context);
1442 set_context(last_context);
1443 environment_pop_to(top);
1449 static void parse_enum_entries(enum_type_t *const enum_type)
1453 if(token.type == '}') {
1455 errorf(HERE, "empty enum not allowed");
1460 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1462 if(token.type != T_IDENTIFIER) {
1463 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1467 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1468 entry->type = (type_t*) enum_type;
1469 entry->symbol = token.v.symbol;
1470 entry->source_position = token.source_position;
1473 if(token.type == '=') {
1475 entry->init.enum_value = parse_constant_expression();
1480 record_declaration(entry);
1482 if(token.type != ',')
1485 } while(token.type != '}');
1490 static type_t *parse_enum_specifier(void)
1494 declaration_t *declaration;
1497 if(token.type == T_IDENTIFIER) {
1498 symbol = token.v.symbol;
1501 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1502 } else if(token.type != '{') {
1503 parse_error_expected("while parsing enum type specifier",
1504 T_IDENTIFIER, '{', 0);
1511 if(declaration == NULL) {
1512 declaration = allocate_ast_zero(sizeof(declaration[0]));
1514 declaration->namespc = NAMESPACE_ENUM;
1515 declaration->source_position = token.source_position;
1516 declaration->symbol = symbol;
1519 type_t *const type = allocate_type_zero(TYPE_ENUM);
1520 type->enumt.declaration = declaration;
1522 if(token.type == '{') {
1523 if(declaration->init.is_defined) {
1524 errorf(HERE, "multiple definitions of enum %Y", symbol);
1526 record_declaration(declaration);
1527 declaration->init.is_defined = 1;
1529 parse_enum_entries(&type->enumt);
1537 * if a symbol is a typedef to another type, return true
1539 static bool is_typedef_symbol(symbol_t *symbol)
1541 const declaration_t *const declaration =
1542 get_declaration(symbol, NAMESPACE_NORMAL);
1544 declaration != NULL &&
1545 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1548 static type_t *parse_typeof(void)
1556 expression_t *expression = NULL;
1559 switch(token.type) {
1560 case T___extension__:
1561 /* this can be a prefix to a typename or an expression */
1562 /* we simply eat it now. */
1565 } while(token.type == T___extension__);
1569 if(is_typedef_symbol(token.v.symbol)) {
1570 type = parse_typename();
1572 expression = parse_expression();
1573 type = expression->base.datatype;
1578 type = parse_typename();
1582 expression = parse_expression();
1583 type = expression->base.datatype;
1589 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1590 typeof_type->typeoft.expression = expression;
1591 typeof_type->typeoft.typeof_type = type;
1597 SPECIFIER_SIGNED = 1 << 0,
1598 SPECIFIER_UNSIGNED = 1 << 1,
1599 SPECIFIER_LONG = 1 << 2,
1600 SPECIFIER_INT = 1 << 3,
1601 SPECIFIER_DOUBLE = 1 << 4,
1602 SPECIFIER_CHAR = 1 << 5,
1603 SPECIFIER_SHORT = 1 << 6,
1604 SPECIFIER_LONG_LONG = 1 << 7,
1605 SPECIFIER_FLOAT = 1 << 8,
1606 SPECIFIER_BOOL = 1 << 9,
1607 SPECIFIER_VOID = 1 << 10,
1608 #ifdef PROVIDE_COMPLEX
1609 SPECIFIER_COMPLEX = 1 << 11,
1610 SPECIFIER_IMAGINARY = 1 << 12,
1614 static type_t *create_builtin_type(symbol_t *const symbol,
1615 type_t *const real_type)
1617 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1618 type->builtin.symbol = symbol;
1619 type->builtin.real_type = real_type;
1621 type_t *result = typehash_insert(type);
1622 if (type != result) {
1629 static type_t *get_typedef_type(symbol_t *symbol)
1631 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1632 if(declaration == NULL
1633 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1636 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1637 type->typedeft.declaration = declaration;
1642 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1644 type_t *type = NULL;
1645 unsigned type_qualifiers = 0;
1646 unsigned type_specifiers = 0;
1649 specifiers->source_position = token.source_position;
1652 switch(token.type) {
1655 #define MATCH_STORAGE_CLASS(token, class) \
1657 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1658 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1660 specifiers->storage_class = class; \
1664 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1665 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1666 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1667 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1668 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1671 switch (specifiers->storage_class) {
1672 case STORAGE_CLASS_NONE:
1673 specifiers->storage_class = STORAGE_CLASS_THREAD;
1676 case STORAGE_CLASS_EXTERN:
1677 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1680 case STORAGE_CLASS_STATIC:
1681 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1685 errorf(HERE, "multiple storage classes in declaration specifiers");
1691 /* type qualifiers */
1692 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1694 type_qualifiers |= qualifier; \
1698 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1699 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1700 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1702 case T___extension__:
1707 /* type specifiers */
1708 #define MATCH_SPECIFIER(token, specifier, name) \
1711 if(type_specifiers & specifier) { \
1712 errorf(HERE, "multiple " name " type specifiers given"); \
1714 type_specifiers |= specifier; \
1718 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1719 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1720 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1721 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1722 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1723 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1724 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1725 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1726 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1727 #ifdef PROVIDE_COMPLEX
1728 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1729 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1732 /* only in microsoft mode */
1733 specifiers->decl_modifiers |= DM_FORCEINLINE;
1737 specifiers->is_inline = true;
1742 if(type_specifiers & SPECIFIER_LONG_LONG) {
1743 errorf(HERE, "multiple type specifiers given");
1744 } else if(type_specifiers & SPECIFIER_LONG) {
1745 type_specifiers |= SPECIFIER_LONG_LONG;
1747 type_specifiers |= SPECIFIER_LONG;
1751 /* TODO: if type != NULL for the following rules should issue
1754 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1756 type->compound.declaration = parse_compound_type_specifier(true);
1760 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1762 type->compound.declaration = parse_compound_type_specifier(false);
1766 type = parse_enum_specifier();
1769 type = parse_typeof();
1771 case T___builtin_va_list:
1772 type = duplicate_type(type_valist);
1776 case T___attribute__:
1781 case T_IDENTIFIER: {
1782 type_t *typedef_type = get_typedef_type(token.v.symbol);
1784 if(typedef_type == NULL)
1785 goto finish_specifiers;
1788 type = typedef_type;
1792 /* function specifier */
1794 goto finish_specifiers;
1801 atomic_type_type_t atomic_type;
1803 /* match valid basic types */
1804 switch(type_specifiers) {
1805 case SPECIFIER_VOID:
1806 atomic_type = ATOMIC_TYPE_VOID;
1808 case SPECIFIER_CHAR:
1809 atomic_type = ATOMIC_TYPE_CHAR;
1811 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1812 atomic_type = ATOMIC_TYPE_SCHAR;
1814 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1815 atomic_type = ATOMIC_TYPE_UCHAR;
1817 case SPECIFIER_SHORT:
1818 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1819 case SPECIFIER_SHORT | SPECIFIER_INT:
1820 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1821 atomic_type = ATOMIC_TYPE_SHORT;
1823 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1824 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1825 atomic_type = ATOMIC_TYPE_USHORT;
1828 case SPECIFIER_SIGNED:
1829 case SPECIFIER_SIGNED | SPECIFIER_INT:
1830 atomic_type = ATOMIC_TYPE_INT;
1832 case SPECIFIER_UNSIGNED:
1833 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1834 atomic_type = ATOMIC_TYPE_UINT;
1836 case SPECIFIER_LONG:
1837 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1838 case SPECIFIER_LONG | SPECIFIER_INT:
1839 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1840 atomic_type = ATOMIC_TYPE_LONG;
1842 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1843 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1844 atomic_type = ATOMIC_TYPE_ULONG;
1846 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1847 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1848 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1849 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1851 atomic_type = ATOMIC_TYPE_LONGLONG;
1853 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1854 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1856 atomic_type = ATOMIC_TYPE_ULONGLONG;
1858 case SPECIFIER_FLOAT:
1859 atomic_type = ATOMIC_TYPE_FLOAT;
1861 case SPECIFIER_DOUBLE:
1862 atomic_type = ATOMIC_TYPE_DOUBLE;
1864 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1865 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1867 case SPECIFIER_BOOL:
1868 atomic_type = ATOMIC_TYPE_BOOL;
1870 #ifdef PROVIDE_COMPLEX
1871 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1872 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1874 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1875 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1877 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1878 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1880 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1881 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1883 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1884 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1886 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1887 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1891 /* invalid specifier combination, give an error message */
1892 if(type_specifiers == 0) {
1893 if (! strict_mode) {
1894 warningf(HERE, "no type specifiers in declaration, using int");
1895 atomic_type = ATOMIC_TYPE_INT;
1898 errorf(HERE, "no type specifiers given in declaration");
1900 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1901 (type_specifiers & SPECIFIER_UNSIGNED)) {
1902 errorf(HERE, "signed and unsigned specifiers gives");
1903 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1904 errorf(HERE, "only integer types can be signed or unsigned");
1906 errorf(HERE, "multiple datatypes in declaration");
1908 atomic_type = ATOMIC_TYPE_INVALID;
1911 type = allocate_type_zero(TYPE_ATOMIC);
1912 type->atomic.atype = atomic_type;
1915 if(type_specifiers != 0) {
1916 errorf(HERE, "multiple datatypes in declaration");
1920 type->base.qualifiers = type_qualifiers;
1922 type_t *result = typehash_insert(type);
1923 if(newtype && result != type) {
1927 specifiers->type = result;
1930 static type_qualifiers_t parse_type_qualifiers(void)
1932 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1935 switch(token.type) {
1936 /* type qualifiers */
1937 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1938 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1939 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1942 return type_qualifiers;
1947 static declaration_t *parse_identifier_list(void)
1949 declaration_t *declarations = NULL;
1950 declaration_t *last_declaration = NULL;
1952 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1954 declaration->source_position = token.source_position;
1955 declaration->symbol = token.v.symbol;
1958 if(last_declaration != NULL) {
1959 last_declaration->next = declaration;
1961 declarations = declaration;
1963 last_declaration = declaration;
1965 if(token.type != ',')
1968 } while(token.type == T_IDENTIFIER);
1970 return declarations;
1973 static void semantic_parameter(declaration_t *declaration)
1975 /* TODO: improve error messages */
1977 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1978 errorf(HERE, "typedef not allowed in parameter list");
1979 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1980 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1981 errorf(HERE, "parameter may only have none or register storage class");
1984 type_t *orig_type = declaration->type;
1985 if(orig_type == NULL)
1987 type_t *type = skip_typeref(orig_type);
1989 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1990 * into a pointer. § 6.7.5.3 (7) */
1991 if (is_type_array(type)) {
1992 const array_type_t *arr_type = &type->array;
1993 type_t *element_type = arr_type->element_type;
1995 type = make_pointer_type(element_type, type->base.qualifiers);
1997 declaration->type = type;
2000 if(is_type_incomplete(type)) {
2001 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
2002 orig_type, declaration->symbol);
2006 static declaration_t *parse_parameter(void)
2008 declaration_specifiers_t specifiers;
2009 memset(&specifiers, 0, sizeof(specifiers));
2011 parse_declaration_specifiers(&specifiers);
2013 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2015 semantic_parameter(declaration);
2020 static declaration_t *parse_parameters(function_type_t *type)
2022 if(token.type == T_IDENTIFIER) {
2023 symbol_t *symbol = token.v.symbol;
2024 if(!is_typedef_symbol(symbol)) {
2025 type->kr_style_parameters = true;
2026 return parse_identifier_list();
2030 if(token.type == ')') {
2031 type->unspecified_parameters = 1;
2034 if(token.type == T_void && look_ahead(1)->type == ')') {
2039 declaration_t *declarations = NULL;
2040 declaration_t *declaration;
2041 declaration_t *last_declaration = NULL;
2042 function_parameter_t *parameter;
2043 function_parameter_t *last_parameter = NULL;
2046 switch(token.type) {
2050 return declarations;
2053 case T___extension__:
2055 declaration = parse_parameter();
2057 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2058 memset(parameter, 0, sizeof(parameter[0]));
2059 parameter->type = declaration->type;
2061 if(last_parameter != NULL) {
2062 last_declaration->next = declaration;
2063 last_parameter->next = parameter;
2065 type->parameters = parameter;
2066 declarations = declaration;
2068 last_parameter = parameter;
2069 last_declaration = declaration;
2073 return declarations;
2075 if(token.type != ',')
2076 return declarations;
2086 } construct_type_type_t;
2088 typedef struct construct_type_t construct_type_t;
2089 struct construct_type_t {
2090 construct_type_type_t type;
2091 construct_type_t *next;
2094 typedef struct parsed_pointer_t parsed_pointer_t;
2095 struct parsed_pointer_t {
2096 construct_type_t construct_type;
2097 type_qualifiers_t type_qualifiers;
2100 typedef struct construct_function_type_t construct_function_type_t;
2101 struct construct_function_type_t {
2102 construct_type_t construct_type;
2103 type_t *function_type;
2106 typedef struct parsed_array_t parsed_array_t;
2107 struct parsed_array_t {
2108 construct_type_t construct_type;
2109 type_qualifiers_t type_qualifiers;
2115 typedef struct construct_base_type_t construct_base_type_t;
2116 struct construct_base_type_t {
2117 construct_type_t construct_type;
2121 static construct_type_t *parse_pointer_declarator(void)
2125 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2126 memset(pointer, 0, sizeof(pointer[0]));
2127 pointer->construct_type.type = CONSTRUCT_POINTER;
2128 pointer->type_qualifiers = parse_type_qualifiers();
2130 return (construct_type_t*) pointer;
2133 static construct_type_t *parse_array_declarator(void)
2137 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2138 memset(array, 0, sizeof(array[0]));
2139 array->construct_type.type = CONSTRUCT_ARRAY;
2141 if(token.type == T_static) {
2142 array->is_static = true;
2146 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2147 if(type_qualifiers != 0) {
2148 if(token.type == T_static) {
2149 array->is_static = true;
2153 array->type_qualifiers = type_qualifiers;
2155 if(token.type == '*' && look_ahead(1)->type == ']') {
2156 array->is_variable = true;
2158 } else if(token.type != ']') {
2159 array->size = parse_assignment_expression();
2164 return (construct_type_t*) array;
2167 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2171 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2173 declaration_t *parameters = parse_parameters(&type->function);
2174 if(declaration != NULL) {
2175 declaration->context.declarations = parameters;
2178 construct_function_type_t *construct_function_type =
2179 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2180 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2181 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2182 construct_function_type->function_type = type;
2186 return (construct_type_t*) construct_function_type;
2189 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2190 bool may_be_abstract)
2192 /* construct a single linked list of construct_type_t's which describe
2193 * how to construct the final declarator type */
2194 construct_type_t *first = NULL;
2195 construct_type_t *last = NULL;
2198 while(token.type == '*') {
2199 construct_type_t *type = parse_pointer_declarator();
2210 /* TODO: find out if this is correct */
2213 construct_type_t *inner_types = NULL;
2215 switch(token.type) {
2217 if(declaration == NULL) {
2218 errorf(HERE, "no identifier expected in typename");
2220 declaration->symbol = token.v.symbol;
2221 declaration->source_position = token.source_position;
2227 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2233 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2234 /* avoid a loop in the outermost scope, because eat_statement doesn't
2236 if(token.type == '}' && current_function == NULL) {
2244 construct_type_t *p = last;
2247 construct_type_t *type;
2248 switch(token.type) {
2250 type = parse_function_declarator(declaration);
2253 type = parse_array_declarator();
2256 goto declarator_finished;
2259 /* insert in the middle of the list (behind p) */
2261 type->next = p->next;
2272 declarator_finished:
2275 /* append inner_types at the end of the list, we don't to set last anymore
2276 * as it's not needed anymore */
2278 assert(first == NULL);
2279 first = inner_types;
2281 last->next = inner_types;
2287 static type_t *construct_declarator_type(construct_type_t *construct_list,
2290 construct_type_t *iter = construct_list;
2291 for( ; iter != NULL; iter = iter->next) {
2292 switch(iter->type) {
2293 case CONSTRUCT_INVALID:
2294 panic("invalid type construction found");
2295 case CONSTRUCT_FUNCTION: {
2296 construct_function_type_t *construct_function_type
2297 = (construct_function_type_t*) iter;
2299 type_t *function_type = construct_function_type->function_type;
2301 function_type->function.return_type = type;
2303 type = function_type;
2307 case CONSTRUCT_POINTER: {
2308 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2309 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2310 pointer_type->pointer.points_to = type;
2311 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2313 type = pointer_type;
2317 case CONSTRUCT_ARRAY: {
2318 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2319 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2321 array_type->base.qualifiers = parsed_array->type_qualifiers;
2322 array_type->array.element_type = type;
2323 array_type->array.is_static = parsed_array->is_static;
2324 array_type->array.is_variable = parsed_array->is_variable;
2325 array_type->array.size = parsed_array->size;
2332 type_t *hashed_type = typehash_insert(type);
2333 if(hashed_type != type) {
2334 /* the function type was constructed earlier freeing it here will
2335 * destroy other types... */
2336 if(iter->type != CONSTRUCT_FUNCTION) {
2346 static declaration_t *parse_declarator(
2347 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2349 type_t *type = specifiers->type;
2350 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2351 declaration->storage_class = specifiers->storage_class;
2352 declaration->modifiers = specifiers->decl_modifiers;
2353 declaration->is_inline = specifiers->is_inline;
2355 construct_type_t *construct_type
2356 = parse_inner_declarator(declaration, may_be_abstract);
2357 declaration->type = construct_declarator_type(construct_type, type);
2359 if(construct_type != NULL) {
2360 obstack_free(&temp_obst, construct_type);
2366 static type_t *parse_abstract_declarator(type_t *base_type)
2368 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2370 type_t *result = construct_declarator_type(construct_type, base_type);
2371 if(construct_type != NULL) {
2372 obstack_free(&temp_obst, construct_type);
2378 static declaration_t *record_declaration(declaration_t *declaration)
2380 assert(declaration->parent_context == NULL);
2381 assert(context != NULL);
2383 symbol_t *symbol = declaration->symbol;
2384 if(symbol != NULL) {
2385 declaration_t *alias = environment_push(declaration);
2386 if(alias != declaration)
2389 declaration->parent_context = context;
2392 if(last_declaration != NULL) {
2393 last_declaration->next = declaration;
2395 context->declarations = declaration;
2397 last_declaration = declaration;
2402 static void parser_error_multiple_definition(declaration_t *declaration,
2403 const source_position_t source_position)
2405 errorf(source_position, "multiple definition of symbol '%Y'",
2406 declaration->symbol);
2407 errorf(declaration->source_position,
2408 "this is the location of the previous definition.");
2411 static bool is_declaration_specifier(const token_t *token,
2412 bool only_type_specifiers)
2414 switch(token->type) {
2418 return is_typedef_symbol(token->v.symbol);
2420 case T___extension__:
2423 return !only_type_specifiers;
2430 static void parse_init_declarator_rest(declaration_t *declaration)
2434 type_t *orig_type = declaration->type;
2435 type_t *type = NULL;
2436 if(orig_type != NULL)
2437 type = skip_typeref(orig_type);
2439 if(declaration->init.initializer != NULL) {
2440 parser_error_multiple_definition(declaration, token.source_position);
2443 initializer_t *initializer = parse_initializer(type);
2445 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2446 * the array type size */
2447 if(type != NULL && is_type_array(type) && initializer != NULL) {
2448 array_type_t *array_type = &type->array;
2450 if(array_type->size == NULL) {
2451 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2453 cnst->base.datatype = type_size_t;
2455 switch (initializer->kind) {
2456 case INITIALIZER_LIST: {
2457 initializer_list_t *const list = &initializer->list;
2458 cnst->conste.v.int_value = list->len;
2462 case INITIALIZER_STRING: {
2463 initializer_string_t *const string = &initializer->string;
2464 cnst->conste.v.int_value = strlen(string->string) + 1;
2468 case INITIALIZER_WIDE_STRING: {
2469 initializer_wide_string_t *const string = &initializer->wide_string;
2470 cnst->conste.v.int_value = string->string.size;
2475 panic("invalid initializer type");
2478 array_type->size = cnst;
2482 if(type != NULL && is_type_function(type)) {
2483 errorf(declaration->source_position,
2484 "initializers not allowed for function types at declator '%Y' (type '%T')",
2485 declaration->symbol, orig_type);
2487 declaration->init.initializer = initializer;
2491 /* parse rest of a declaration without any declarator */
2492 static void parse_anonymous_declaration_rest(
2493 const declaration_specifiers_t *specifiers,
2494 parsed_declaration_func finished_declaration)
2498 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2500 declaration->type = specifiers->type;
2501 declaration->storage_class = specifiers->storage_class;
2502 declaration->source_position = specifiers->source_position;
2504 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2505 warningf(declaration->source_position, "useless storage class in empty declaration");
2508 type_t *type = declaration->type;
2509 switch (type->kind) {
2510 case TYPE_COMPOUND_STRUCT:
2511 case TYPE_COMPOUND_UNION: {
2512 const compound_type_t *compound_type = &type->compound;
2513 if (compound_type->declaration->symbol == NULL) {
2514 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2523 warningf(declaration->source_position, "empty declaration");
2527 finished_declaration(declaration);
2530 static void parse_declaration_rest(declaration_t *ndeclaration,
2531 const declaration_specifiers_t *specifiers,
2532 parsed_declaration_func finished_declaration)
2535 declaration_t *declaration = finished_declaration(ndeclaration);
2537 type_t *orig_type = declaration->type;
2538 type_t *type = skip_typeref(orig_type);
2540 if(type->kind != TYPE_FUNCTION && declaration->is_inline) {
2541 warningf(declaration->source_position,
2542 "variable '%Y' declared 'inline'\n", declaration->symbol);
2545 if(token.type == '=') {
2546 parse_init_declarator_rest(declaration);
2549 if(token.type != ',')
2553 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2558 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2560 /* TODO: check that it was actually a parameter that gets a type */
2562 /* we should have a declaration for the parameter in the current
2564 return record_declaration(declaration);
2567 static void parse_declaration(parsed_declaration_func finished_declaration)
2569 declaration_specifiers_t specifiers;
2570 memset(&specifiers, 0, sizeof(specifiers));
2571 parse_declaration_specifiers(&specifiers);
2573 if(token.type == ';') {
2574 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2576 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2577 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2581 static void parse_kr_declaration_list(declaration_t *declaration)
2583 type_t *type = skip_typeref(declaration->type);
2584 if(!is_type_function(type))
2587 if(!type->function.kr_style_parameters)
2590 /* push function parameters */
2591 int top = environment_top();
2592 context_t *last_context = context;
2593 set_context(&declaration->context);
2595 declaration_t *parameter = declaration->context.declarations;
2596 for( ; parameter != NULL; parameter = parameter->next) {
2597 environment_push(parameter);
2600 /* parse declaration list */
2601 while(is_declaration_specifier(&token, false)) {
2602 parse_declaration(finished_kr_declaration);
2605 /* pop function parameters */
2606 assert(context == &declaration->context);
2607 set_context(last_context);
2608 environment_pop_to(top);
2610 /* update function type */
2611 type_t *new_type = duplicate_type(type);
2612 new_type->function.kr_style_parameters = false;
2614 function_parameter_t *parameters = NULL;
2615 function_parameter_t *last_parameter = NULL;
2617 declaration_t *parameter_declaration = declaration->context.declarations;
2618 for( ; parameter_declaration != NULL;
2619 parameter_declaration = parameter_declaration->next) {
2620 type_t *parameter_type = parameter_declaration->type;
2621 if(parameter_type == NULL) {
2623 errorf(HERE, "no type specified for function parameter '%Y'",
2624 parameter_declaration->symbol);
2626 warningf(HERE, "no type specified for function parameter '%Y', using int",
2627 parameter_declaration->symbol);
2628 parameter_type = type_int;
2629 parameter_declaration->type = parameter_type;
2633 semantic_parameter(parameter_declaration);
2634 parameter_type = parameter_declaration->type;
2636 function_parameter_t *function_parameter
2637 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2638 memset(function_parameter, 0, sizeof(function_parameter[0]));
2640 function_parameter->type = parameter_type;
2641 if(last_parameter != NULL) {
2642 last_parameter->next = function_parameter;
2644 parameters = function_parameter;
2646 last_parameter = function_parameter;
2648 new_type->function.parameters = parameters;
2650 type = typehash_insert(new_type);
2651 if(type != new_type) {
2652 obstack_free(type_obst, new_type);
2655 declaration->type = type;
2658 static void parse_external_declaration(void)
2660 /* function-definitions and declarations both start with declaration
2662 declaration_specifiers_t specifiers;
2663 memset(&specifiers, 0, sizeof(specifiers));
2664 parse_declaration_specifiers(&specifiers);
2666 /* must be a declaration */
2667 if(token.type == ';') {
2668 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2672 /* declarator is common to both function-definitions and declarations */
2673 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2675 /* must be a declaration */
2676 if(token.type == ',' || token.type == '=' || token.type == ';') {
2677 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2681 /* must be a function definition */
2682 parse_kr_declaration_list(ndeclaration);
2684 if(token.type != '{') {
2685 parse_error_expected("while parsing function definition", '{', 0);
2690 type_t *type = ndeclaration->type;
2696 /* note that we don't skip typerefs: the standard doesn't allow them here
2697 * (so we can't use is_type_function here) */
2698 if(type->kind != TYPE_FUNCTION) {
2699 errorf(HERE, "declarator '%#T' has a body but is not a function type", type, ndeclaration->symbol);
2704 /* § 6.7.5.3 (14) a function definition with () means no
2705 * parameters (and not unspecified parameters) */
2706 if(type->function.unspecified_parameters) {
2707 type_t *duplicate = duplicate_type(type);
2708 duplicate->function.unspecified_parameters = false;
2710 type = typehash_insert(duplicate);
2711 if(type != duplicate) {
2712 obstack_free(type_obst, duplicate);
2714 ndeclaration->type = type;
2717 declaration_t *declaration = record_declaration(ndeclaration);
2718 if(ndeclaration != declaration) {
2719 memcpy(&declaration->context, &ndeclaration->context,
2720 sizeof(declaration->context));
2722 type = skip_typeref(declaration->type);
2724 /* push function parameters and switch context */
2725 int top = environment_top();
2726 context_t *last_context = context;
2727 set_context(&declaration->context);
2729 declaration_t *parameter = declaration->context.declarations;
2730 for( ; parameter != NULL; parameter = parameter->next) {
2731 environment_push(parameter);
2734 if(declaration->init.statement != NULL) {
2735 parser_error_multiple_definition(declaration, token.source_position);
2737 goto end_of_parse_external_declaration;
2739 /* parse function body */
2740 int label_stack_top = label_top();
2741 declaration_t *old_current_function = current_function;
2742 current_function = declaration;
2744 declaration->init.statement = parse_compound_statement();
2746 assert(current_function == declaration);
2747 current_function = old_current_function;
2748 label_pop_to(label_stack_top);
2751 end_of_parse_external_declaration:
2752 assert(context == &declaration->context);
2753 set_context(last_context);
2754 environment_pop_to(top);
2757 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2759 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2760 type->bitfield.base = base;
2761 type->bitfield.size = size;
2766 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2768 /* TODO: check constraints for struct declarations (in specifiers) */
2770 declaration_t *declaration;
2772 if(token.type == ':') {
2775 type_t *base_type = specifiers->type;
2776 expression_t *size = parse_constant_expression();
2778 type_t *type = make_bitfield_type(base_type, size);
2780 declaration = allocate_ast_zero(sizeof(declaration[0]));
2782 declaration->namespc = NAMESPACE_NORMAL;
2783 declaration->storage_class = STORAGE_CLASS_NONE;
2784 declaration->source_position = token.source_position;
2785 declaration->modifiers = specifiers->decl_modifiers;
2786 declaration->type = type;
2788 record_declaration(declaration);
2790 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2792 if(token.type == ':') {
2794 expression_t *size = parse_constant_expression();
2796 type_t *type = make_bitfield_type(declaration->type, size);
2797 declaration->type = type;
2800 record_declaration(declaration);
2802 if(token.type != ',')
2809 static void parse_compound_type_entries(void)
2813 while(token.type != '}' && token.type != T_EOF) {
2814 declaration_specifiers_t specifiers;
2815 memset(&specifiers, 0, sizeof(specifiers));
2816 parse_declaration_specifiers(&specifiers);
2818 parse_struct_declarators(&specifiers);
2820 if(token.type == T_EOF) {
2821 errorf(HERE, "EOF while parsing struct");
2826 static type_t *parse_typename(void)
2828 declaration_specifiers_t specifiers;
2829 memset(&specifiers, 0, sizeof(specifiers));
2830 parse_declaration_specifiers(&specifiers);
2831 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2832 /* TODO: improve error message, user does probably not know what a
2833 * storage class is...
2835 errorf(HERE, "typename may not have a storage class");
2838 type_t *result = parse_abstract_declarator(specifiers.type);
2846 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2847 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2848 expression_t *left);
2850 typedef struct expression_parser_function_t expression_parser_function_t;
2851 struct expression_parser_function_t {
2852 unsigned precedence;
2853 parse_expression_function parser;
2854 unsigned infix_precedence;
2855 parse_expression_infix_function infix_parser;
2858 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2861 * Creates a new invalid expression.
2863 static expression_t *create_invalid_expression(void)
2865 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2866 expression->base.source_position = token.source_position;
2870 static expression_t *expected_expression_error(void)
2872 errorf(HERE, "expected expression, got token '%K'", &token);
2876 return create_invalid_expression();
2880 * Parse a string constant.
2882 static expression_t *parse_string_const(void)
2884 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2885 cnst->base.datatype = type_string;
2886 cnst->string.value = parse_string_literals();
2892 * Parse a wide string constant.
2894 static expression_t *parse_wide_string_const(void)
2896 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2897 cnst->base.datatype = type_wchar_t_ptr;
2898 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2904 * Parse an integer constant.
2906 static expression_t *parse_int_const(void)
2908 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2909 cnst->base.datatype = token.datatype;
2910 cnst->conste.v.int_value = token.v.intvalue;
2918 * Parse a float constant.
2920 static expression_t *parse_float_const(void)
2922 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2923 cnst->base.datatype = token.datatype;
2924 cnst->conste.v.float_value = token.v.floatvalue;
2931 static declaration_t *create_implicit_function(symbol_t *symbol,
2932 const source_position_t source_position)
2934 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2935 ntype->function.return_type = type_int;
2936 ntype->function.unspecified_parameters = true;
2938 type_t *type = typehash_insert(ntype);
2943 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2945 declaration->storage_class = STORAGE_CLASS_EXTERN;
2946 declaration->type = type;
2947 declaration->symbol = symbol;
2948 declaration->source_position = source_position;
2950 /* prepend the implicit definition to the global context
2951 * this is safe since the symbol wasn't declared as anything else yet
2953 assert(symbol->declaration == NULL);
2955 context_t *last_context = context;
2956 context = global_context;
2958 environment_push(declaration);
2959 declaration->next = context->declarations;
2960 context->declarations = declaration;
2962 context = last_context;
2968 * Creates a return_type (func)(argument_type) function type if not
2971 * @param return_type the return type
2972 * @param argument_type the argument type
2974 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2976 function_parameter_t *parameter
2977 = obstack_alloc(type_obst, sizeof(parameter[0]));
2978 memset(parameter, 0, sizeof(parameter[0]));
2979 parameter->type = argument_type;
2981 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2982 type->function.return_type = return_type;
2983 type->function.parameters = parameter;
2985 type_t *result = typehash_insert(type);
2986 if(result != type) {
2994 * Creates a function type for some function like builtins.
2996 * @param symbol the symbol describing the builtin
2998 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3000 switch(symbol->ID) {
3001 case T___builtin_alloca:
3002 return make_function_1_type(type_void_ptr, type_size_t);
3003 case T___builtin_nan:
3004 return make_function_1_type(type_double, type_string);
3005 case T___builtin_nanf:
3006 return make_function_1_type(type_float, type_string);
3007 case T___builtin_nand:
3008 return make_function_1_type(type_long_double, type_string);
3009 case T___builtin_va_end:
3010 return make_function_1_type(type_void, type_valist);
3012 panic("not implemented builtin symbol found");
3017 * Performs automatic type cast as described in § 6.3.2.1.
3019 * @param orig_type the original type
3021 static type_t *automatic_type_conversion(type_t *orig_type)
3023 if(orig_type == NULL)
3026 type_t *type = skip_typeref(orig_type);
3027 if(is_type_array(type)) {
3028 array_type_t *array_type = &type->array;
3029 type_t *element_type = array_type->element_type;
3030 unsigned qualifiers = array_type->type.qualifiers;
3032 return make_pointer_type(element_type, qualifiers);
3035 if(is_type_function(type)) {
3036 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3043 * reverts the automatic casts of array to pointer types and function
3044 * to function-pointer types as defined § 6.3.2.1
3046 type_t *revert_automatic_type_conversion(const expression_t *expression)
3048 if(expression->base.datatype == NULL)
3051 switch(expression->kind) {
3052 case EXPR_REFERENCE: {
3053 const reference_expression_t *ref = &expression->reference;
3054 return ref->declaration->type;
3057 const select_expression_t *select = &expression->select;
3058 type_t *orig_type = select->compound_entry->type;
3059 type_t *type = skip_typeref(orig_type);
3060 if(type->kind == TYPE_BITFIELD) {
3061 return type->bitfield.base;
3066 case EXPR_UNARY_DEREFERENCE: {
3067 expression_t *value = expression->unary.value;
3068 type_t *type = skip_typeref(value->base.datatype);
3069 pointer_type_t *pointer_type = &type->pointer;
3071 return pointer_type->points_to;
3073 case EXPR_BUILTIN_SYMBOL: {
3074 const builtin_symbol_expression_t *builtin
3075 = &expression->builtin_symbol;
3076 return get_builtin_symbol_type(builtin->symbol);
3078 case EXPR_ARRAY_ACCESS: {
3079 const array_access_expression_t *array_access
3080 = &expression->array_access;
3081 const expression_t *array_ref = array_access->array_ref;
3082 type_t *type_left = skip_typeref(array_ref->base.datatype);
3083 assert(is_type_pointer(type_left));
3084 pointer_type_t *pointer_type = &type_left->pointer;
3085 return pointer_type->points_to;
3092 return expression->base.datatype;
3095 static expression_t *parse_reference(void)
3097 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3099 reference_expression_t *ref = &expression->reference;
3100 ref->symbol = token.v.symbol;
3102 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3104 source_position_t source_position = token.source_position;
3107 if(declaration == NULL) {
3108 if (! strict_mode && token.type == '(') {
3109 /* an implicitly defined function */
3110 warningf(HERE, "implicit declaration of function '%Y'",
3113 declaration = create_implicit_function(ref->symbol,
3116 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3121 type_t *type = declaration->type;
3122 type_t *skipped_type = skip_typeref(type);
3123 if(skipped_type->kind == TYPE_BITFIELD) {
3124 type = skipped_type->bitfield.base;
3127 /* we always do the auto-type conversions; the & and sizeof parser contains
3128 * code to revert this! */
3129 type = automatic_type_conversion(type);
3131 ref->declaration = declaration;
3132 ref->expression.datatype = type;
3137 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3141 /* TODO check if explicit cast is allowed and issue warnings/errors */
3144 static expression_t *parse_cast(void)
3146 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3148 cast->base.source_position = token.source_position;
3150 type_t *type = parse_typename();
3153 expression_t *value = parse_sub_expression(20);
3155 check_cast_allowed(value, type);
3157 cast->base.datatype = type;
3158 cast->unary.value = value;
3163 static expression_t *parse_statement_expression(void)
3165 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3167 statement_t *statement = parse_compound_statement();
3168 expression->statement.statement = statement;
3169 if(statement == NULL) {
3174 assert(statement->kind == STATEMENT_COMPOUND);
3175 compound_statement_t *compound_statement = &statement->compound;
3177 /* find last statement and use it's type */
3178 const statement_t *last_statement = NULL;
3179 const statement_t *iter = compound_statement->statements;
3180 for( ; iter != NULL; iter = iter->base.next) {
3181 last_statement = iter;
3184 if(last_statement->kind == STATEMENT_EXPRESSION) {
3185 const expression_statement_t *expression_statement
3186 = &last_statement->expression;
3187 expression->base.datatype
3188 = expression_statement->expression->base.datatype;
3190 expression->base.datatype = type_void;
3198 static expression_t *parse_brace_expression(void)
3202 switch(token.type) {
3204 /* gcc extension: a statement expression */
3205 return parse_statement_expression();
3209 return parse_cast();
3211 if(is_typedef_symbol(token.v.symbol)) {
3212 return parse_cast();
3216 expression_t *result = parse_expression();
3222 static expression_t *parse_function_keyword(void)
3227 if (current_function == NULL) {
3228 errorf(HERE, "'__func__' used outside of a function");
3231 string_literal_expression_t *expression
3232 = allocate_ast_zero(sizeof(expression[0]));
3234 expression->expression.kind = EXPR_FUNCTION;
3235 expression->expression.datatype = type_string;
3236 expression->value = current_function->symbol->string;
3238 return (expression_t*) expression;
3241 static expression_t *parse_pretty_function_keyword(void)
3243 eat(T___PRETTY_FUNCTION__);
3246 if (current_function == NULL) {
3247 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3250 string_literal_expression_t *expression
3251 = allocate_ast_zero(sizeof(expression[0]));
3253 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3254 expression->expression.datatype = type_string;
3255 expression->value = current_function->symbol->string;
3257 return (expression_t*) expression;
3260 static designator_t *parse_designator(void)
3262 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3264 if(token.type != T_IDENTIFIER) {
3265 parse_error_expected("while parsing member designator",
3270 result->symbol = token.v.symbol;
3273 designator_t *last_designator = result;
3275 if(token.type == '.') {
3277 if(token.type != T_IDENTIFIER) {
3278 parse_error_expected("while parsing member designator",
3283 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3284 designator->symbol = token.v.symbol;
3287 last_designator->next = designator;
3288 last_designator = designator;
3291 if(token.type == '[') {
3293 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3294 designator->array_access = parse_expression();
3295 if(designator->array_access == NULL) {
3301 last_designator->next = designator;
3302 last_designator = designator;
3311 static expression_t *parse_offsetof(void)
3313 eat(T___builtin_offsetof);
3315 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3316 expression->base.datatype = type_size_t;
3319 expression->offsetofe.type = parse_typename();
3321 expression->offsetofe.designator = parse_designator();
3327 static expression_t *parse_va_start(void)
3329 eat(T___builtin_va_start);
3331 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3334 expression->va_starte.ap = parse_assignment_expression();
3336 expression_t *const expr = parse_assignment_expression();
3337 if (expr->kind == EXPR_REFERENCE) {
3338 declaration_t *const decl = expr->reference.declaration;
3339 if (decl->parent_context == ¤t_function->context &&
3340 decl->next == NULL) {
3341 expression->va_starte.parameter = decl;
3346 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3348 return create_invalid_expression();
3351 static expression_t *parse_va_arg(void)
3353 eat(T___builtin_va_arg);
3355 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3358 expression->va_arge.ap = parse_assignment_expression();
3360 expression->base.datatype = parse_typename();
3366 static expression_t *parse_builtin_symbol(void)
3368 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3370 symbol_t *symbol = token.v.symbol;
3372 expression->builtin_symbol.symbol = symbol;
3375 type_t *type = get_builtin_symbol_type(symbol);
3376 type = automatic_type_conversion(type);
3378 expression->base.datatype = type;
3382 static expression_t *parse_builtin_constant(void)
3384 eat(T___builtin_constant_p);
3386 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3389 expression->builtin_constant.value = parse_assignment_expression();
3391 expression->base.datatype = type_int;
3396 static expression_t *parse_builtin_prefetch(void)
3398 eat(T___builtin_prefetch);
3400 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3403 expression->builtin_prefetch.adr = parse_assignment_expression();
3404 if (token.type == ',') {
3406 expression->builtin_prefetch.rw = parse_assignment_expression();
3408 if (token.type == ',') {
3410 expression->builtin_prefetch.locality = parse_assignment_expression();
3413 expression->base.datatype = type_void;
3418 static expression_t *parse_compare_builtin(void)
3420 expression_t *expression;
3422 switch(token.type) {
3423 case T___builtin_isgreater:
3424 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3426 case T___builtin_isgreaterequal:
3427 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3429 case T___builtin_isless:
3430 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3432 case T___builtin_islessequal:
3433 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3435 case T___builtin_islessgreater:
3436 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3438 case T___builtin_isunordered:
3439 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3442 panic("invalid compare builtin found");
3448 expression->binary.left = parse_assignment_expression();
3450 expression->binary.right = parse_assignment_expression();
3453 type_t *orig_type_left = expression->binary.left->base.datatype;
3454 type_t *orig_type_right = expression->binary.right->base.datatype;
3455 if(orig_type_left == NULL || orig_type_right == NULL)
3458 type_t *type_left = skip_typeref(orig_type_left);
3459 type_t *type_right = skip_typeref(orig_type_right);
3460 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3461 type_error_incompatible("invalid operands in comparison",
3462 token.source_position, type_left, type_right);
3464 semantic_comparison(&expression->binary);
3470 static expression_t *parse_builtin_expect(void)
3472 eat(T___builtin_expect);
3474 expression_t *expression
3475 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3478 expression->binary.left = parse_assignment_expression();
3480 expression->binary.right = parse_constant_expression();
3483 expression->base.datatype = expression->binary.left->base.datatype;
3488 static expression_t *parse_assume(void) {
3491 expression_t *expression
3492 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3495 expression->unary.value = parse_assignment_expression();
3498 expression->base.datatype = type_void;
3502 static expression_t *parse_alignof(void) {
3505 expression_t *expression
3506 = allocate_expression_zero(EXPR_ALIGNOF);
3509 expression->alignofe.type = parse_typename();
3512 expression->base.datatype = type_size_t;
3516 static expression_t *parse_primary_expression(void)
3518 switch(token.type) {
3520 return parse_int_const();
3521 case T_FLOATINGPOINT:
3522 return parse_float_const();
3523 case T_STRING_LITERAL:
3524 return parse_string_const();
3525 case T_WIDE_STRING_LITERAL:
3526 return parse_wide_string_const();
3528 return parse_reference();
3529 case T___FUNCTION__:
3531 return parse_function_keyword();
3532 case T___PRETTY_FUNCTION__:
3533 return parse_pretty_function_keyword();
3534 case T___builtin_offsetof:
3535 return parse_offsetof();
3536 case T___builtin_va_start:
3537 return parse_va_start();
3538 case T___builtin_va_arg:
3539 return parse_va_arg();
3540 case T___builtin_expect:
3541 return parse_builtin_expect();
3542 case T___builtin_nanf:
3543 case T___builtin_alloca:
3544 case T___builtin_va_end:
3545 return parse_builtin_symbol();
3546 case T___builtin_isgreater:
3547 case T___builtin_isgreaterequal:
3548 case T___builtin_isless:
3549 case T___builtin_islessequal:
3550 case T___builtin_islessgreater:
3551 case T___builtin_isunordered:
3552 return parse_compare_builtin();
3553 case T___builtin_constant_p:
3554 return parse_builtin_constant();
3555 case T___builtin_prefetch:
3556 return parse_builtin_prefetch();
3558 return parse_alignof();
3560 return parse_assume();
3563 return parse_brace_expression();
3566 errorf(HERE, "unexpected token '%K'", &token);
3569 return create_invalid_expression();
3572 static expression_t *parse_array_expression(unsigned precedence,
3579 expression_t *inside = parse_expression();
3581 array_access_expression_t *array_access
3582 = allocate_ast_zero(sizeof(array_access[0]));
3584 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3586 type_t *type_left = left->base.datatype;
3587 type_t *type_inside = inside->base.datatype;
3588 type_t *return_type = NULL;
3590 if(type_left != NULL && type_inside != NULL) {
3591 type_left = skip_typeref(type_left);
3592 type_inside = skip_typeref(type_inside);
3594 if(is_type_pointer(type_left)) {
3595 pointer_type_t *pointer = &type_left->pointer;
3596 return_type = pointer->points_to;
3597 array_access->array_ref = left;
3598 array_access->index = inside;
3599 } else if(is_type_pointer(type_inside)) {
3600 pointer_type_t *pointer = &type_inside->pointer;
3601 return_type = pointer->points_to;
3602 array_access->array_ref = inside;
3603 array_access->index = left;
3604 array_access->flipped = true;
3606 errorf(HERE, "array access on object with non-pointer types '%T', '%T'", type_left, type_inside);
3609 array_access->array_ref = left;
3610 array_access->index = inside;
3613 if(token.type != ']') {
3614 parse_error_expected("Problem while parsing array access", ']', 0);
3615 return (expression_t*) array_access;
3619 return_type = automatic_type_conversion(return_type);
3620 array_access->expression.datatype = return_type;
3622 return (expression_t*) array_access;
3625 static expression_t *parse_sizeof(unsigned precedence)
3629 sizeof_expression_t *sizeof_expression
3630 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3631 sizeof_expression->expression.kind = EXPR_SIZEOF;
3632 sizeof_expression->expression.datatype = type_size_t;
3634 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3636 sizeof_expression->type = parse_typename();
3639 expression_t *expression = parse_sub_expression(precedence);
3640 expression->base.datatype = revert_automatic_type_conversion(expression);
3642 sizeof_expression->type = expression->base.datatype;
3643 sizeof_expression->size_expression = expression;
3646 return (expression_t*) sizeof_expression;
3649 static expression_t *parse_select_expression(unsigned precedence,
3650 expression_t *compound)
3653 assert(token.type == '.' || token.type == T_MINUSGREATER);
3655 bool is_pointer = (token.type == T_MINUSGREATER);
3658 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3659 select->select.compound = compound;
3661 if(token.type != T_IDENTIFIER) {
3662 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3665 symbol_t *symbol = token.v.symbol;
3666 select->select.symbol = symbol;
3669 type_t *orig_type = compound->base.datatype;
3670 if(orig_type == NULL)
3671 return create_invalid_expression();
3673 type_t *type = skip_typeref(orig_type);
3675 type_t *type_left = type;
3677 if(type->kind != TYPE_POINTER) {
3678 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3679 return create_invalid_expression();
3681 pointer_type_t *pointer_type = &type->pointer;
3682 type_left = pointer_type->points_to;
3684 type_left = skip_typeref(type_left);
3686 if(type_left->kind != TYPE_COMPOUND_STRUCT
3687 && type_left->kind != TYPE_COMPOUND_UNION) {
3688 errorf(HERE, "request for member '%Y' in something not a struct or "
3689 "union, but '%T'", symbol, type_left);
3690 return create_invalid_expression();
3693 compound_type_t *compound_type = &type_left->compound;
3694 declaration_t *declaration = compound_type->declaration;
3696 if(!declaration->init.is_defined) {
3697 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3699 return create_invalid_expression();
3702 declaration_t *iter = declaration->context.declarations;
3703 for( ; iter != NULL; iter = iter->next) {
3704 if(iter->symbol == symbol) {
3709 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3710 return create_invalid_expression();
3713 /* we always do the auto-type conversions; the & and sizeof parser contains
3714 * code to revert this! */
3715 type_t *expression_type = automatic_type_conversion(iter->type);
3716 if(expression_type->kind == TYPE_BITFIELD) {
3717 expression_type = expression_type->bitfield.base;
3720 select->select.compound_entry = iter;
3721 select->base.datatype = expression_type;
3727 * Parse a call expression, ie. expression '( ... )'.
3729 * @param expression the function address
3731 static expression_t *parse_call_expression(unsigned precedence,
3732 expression_t *expression)
3735 expression_t *result = allocate_expression_zero(EXPR_CALL);
3737 call_expression_t *call = &result->call;
3738 call->function = expression;
3740 function_type_t *function_type = NULL;
3741 type_t *orig_type = expression->base.datatype;
3742 if(orig_type != NULL) {
3743 type_t *type = skip_typeref(orig_type);
3745 if(is_type_pointer(type)) {
3746 pointer_type_t *pointer_type = &type->pointer;
3748 type = skip_typeref(pointer_type->points_to);
3750 if (is_type_function(type)) {
3751 function_type = &type->function;
3752 call->expression.datatype = function_type->return_type;
3755 if(function_type == NULL) {
3756 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3758 function_type = NULL;
3759 call->expression.datatype = NULL;
3763 /* parse arguments */
3766 if(token.type != ')') {
3767 call_argument_t *last_argument = NULL;
3770 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3772 argument->expression = parse_assignment_expression();
3773 if(last_argument == NULL) {
3774 call->arguments = argument;
3776 last_argument->next = argument;
3778 last_argument = argument;
3780 if(token.type != ',')
3787 if(function_type != NULL) {
3788 function_parameter_t *parameter = function_type->parameters;
3789 call_argument_t *argument = call->arguments;
3790 for( ; parameter != NULL && argument != NULL;
3791 parameter = parameter->next, argument = argument->next) {
3792 type_t *expected_type = parameter->type;
3793 /* TODO report context in error messages */
3794 argument->expression = create_implicit_cast(argument->expression,
3797 /* too few parameters */
3798 if(parameter != NULL) {
3799 errorf(HERE, "too few arguments to function '%E'", expression);
3800 } else if(argument != NULL) {
3801 /* too many parameters */
3802 if(!function_type->variadic
3803 && !function_type->unspecified_parameters) {
3804 errorf(HERE, "too many arguments to function '%E'", expression);
3806 /* do default promotion */
3807 for( ; argument != NULL; argument = argument->next) {
3808 type_t *type = argument->expression->base.datatype;
3813 type = skip_typeref(type);
3814 if(is_type_integer(type)) {
3815 type = promote_integer(type);
3816 } else if(type == type_float) {
3820 argument->expression
3821 = create_implicit_cast(argument->expression, type);
3824 check_format(&result->call);
3827 check_format(&result->call);
3834 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3836 static bool same_compound_type(const type_t *type1, const type_t *type2)
3838 if(!is_type_compound(type1))
3840 if(type1->kind != type2->kind)
3843 const compound_type_t *compound1 = &type1->compound;
3844 const compound_type_t *compound2 = &type2->compound;
3846 return compound1->declaration == compound2->declaration;
3850 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3852 * @param expression the conditional expression
3854 static expression_t *parse_conditional_expression(unsigned precedence,
3855 expression_t *expression)
3859 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3861 conditional_expression_t *conditional = &result->conditional;
3862 conditional->condition = expression;
3865 type_t *condition_type_orig = expression->base.datatype;
3866 if(condition_type_orig != NULL) {
3867 type_t *condition_type = skip_typeref(condition_type_orig);
3868 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3869 type_error("expected a scalar type in conditional condition",
3870 expression->base.source_position, condition_type_orig);
3874 expression_t *true_expression = parse_expression();
3876 expression_t *false_expression = parse_sub_expression(precedence);
3878 conditional->true_expression = true_expression;
3879 conditional->false_expression = false_expression;
3881 type_t *orig_true_type = true_expression->base.datatype;
3882 type_t *orig_false_type = false_expression->base.datatype;
3883 if(orig_true_type == NULL || orig_false_type == NULL)
3886 type_t *true_type = skip_typeref(orig_true_type);
3887 type_t *false_type = skip_typeref(orig_false_type);
3890 type_t *result_type = NULL;
3891 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3892 result_type = semantic_arithmetic(true_type, false_type);
3894 true_expression = create_implicit_cast(true_expression, result_type);
3895 false_expression = create_implicit_cast(false_expression, result_type);
3897 conditional->true_expression = true_expression;
3898 conditional->false_expression = false_expression;
3899 conditional->expression.datatype = result_type;
3900 } else if (same_compound_type(true_type, false_type)
3901 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3902 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3903 /* just take 1 of the 2 types */
3904 result_type = true_type;
3905 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3906 && pointers_compatible(true_type, false_type)) {
3908 result_type = true_type;
3911 type_error_incompatible("while parsing conditional",
3912 expression->base.source_position, true_type,
3916 conditional->expression.datatype = result_type;
3921 * Parse an extension expression.
3923 static expression_t *parse_extension(unsigned precedence)
3925 eat(T___extension__);
3927 /* TODO enable extensions */
3928 expression_t *expression = parse_sub_expression(precedence);
3929 /* TODO disable extensions */
3933 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3935 eat(T___builtin_classify_type);
3937 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3938 result->base.datatype = type_int;
3941 expression_t *expression = parse_sub_expression(precedence);
3943 result->classify_type.type_expression = expression;
3948 static void semantic_incdec(unary_expression_t *expression)
3950 type_t *orig_type = expression->value->base.datatype;
3951 if(orig_type == NULL)
3954 type_t *type = skip_typeref(orig_type);
3955 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3956 /* TODO: improve error message */
3957 errorf(HERE, "operation needs an arithmetic or pointer type");
3961 expression->expression.datatype = orig_type;
3964 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3966 type_t *orig_type = expression->value->base.datatype;
3967 if(orig_type == NULL)
3970 type_t *type = skip_typeref(orig_type);
3971 if(!is_type_arithmetic(type)) {
3972 /* TODO: improve error message */
3973 errorf(HERE, "operation needs an arithmetic type");
3977 expression->expression.datatype = orig_type;
3980 static void semantic_unexpr_scalar(unary_expression_t *expression)
3982 type_t *orig_type = expression->value->base.datatype;
3983 if(orig_type == NULL)
3986 type_t *type = skip_typeref(orig_type);
3987 if (!is_type_scalar(type)) {
3988 errorf(HERE, "operand of ! must be of scalar type");
3992 expression->expression.datatype = orig_type;
3995 static void semantic_unexpr_integer(unary_expression_t *expression)
3997 type_t *orig_type = expression->value->base.datatype;
3998 if(orig_type == NULL)
4001 type_t *type = skip_typeref(orig_type);
4002 if (!is_type_integer(type)) {
4003 errorf(HERE, "operand of ~ must be of integer type");
4007 expression->expression.datatype = orig_type;
4010 static void semantic_dereference(unary_expression_t *expression)
4012 type_t *orig_type = expression->value->base.datatype;
4013 if(orig_type == NULL)
4016 type_t *type = skip_typeref(orig_type);
4017 if(!is_type_pointer(type)) {
4018 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4022 pointer_type_t *pointer_type = &type->pointer;
4023 type_t *result_type = pointer_type->points_to;
4025 result_type = automatic_type_conversion(result_type);
4026 expression->expression.datatype = result_type;
4029 static void semantic_take_addr(unary_expression_t *expression)
4031 expression_t *value = expression->value;
4032 value->base.datatype = revert_automatic_type_conversion(value);
4034 type_t *orig_type = value->base.datatype;
4035 if(orig_type == NULL)
4038 if(value->kind == EXPR_REFERENCE) {
4039 reference_expression_t *reference = (reference_expression_t*) value;
4040 declaration_t *declaration = reference->declaration;
4041 if(declaration != NULL) {
4042 declaration->address_taken = 1;
4046 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4049 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4050 static expression_t *parse_##unexpression_type(unsigned precedence) \
4054 expression_t *unary_expression \
4055 = allocate_expression_zero(unexpression_type); \
4056 unary_expression->unary.value = parse_sub_expression(precedence); \
4058 sfunc(&unary_expression->unary); \
4060 return unary_expression; \
4063 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4064 semantic_unexpr_arithmetic)
4065 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4066 semantic_unexpr_arithmetic)
4067 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4068 semantic_unexpr_scalar)
4069 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4070 semantic_dereference)
4071 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4073 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4074 semantic_unexpr_integer)
4075 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4077 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4080 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4082 static expression_t *parse_##unexpression_type(unsigned precedence, \
4083 expression_t *left) \
4085 (void) precedence; \
4088 expression_t *unary_expression \
4089 = allocate_expression_zero(unexpression_type); \
4090 unary_expression->unary.value = left; \
4092 sfunc(&unary_expression->unary); \
4094 return unary_expression; \
4097 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4098 EXPR_UNARY_POSTFIX_INCREMENT,
4100 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4101 EXPR_UNARY_POSTFIX_DECREMENT,
4104 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4106 /* TODO: handle complex + imaginary types */
4108 /* § 6.3.1.8 Usual arithmetic conversions */
4109 if(type_left == type_long_double || type_right == type_long_double) {
4110 return type_long_double;
4111 } else if(type_left == type_double || type_right == type_double) {
4113 } else if(type_left == type_float || type_right == type_float) {
4117 type_right = promote_integer(type_right);
4118 type_left = promote_integer(type_left);
4120 if(type_left == type_right)
4123 bool signed_left = is_type_signed(type_left);
4124 bool signed_right = is_type_signed(type_right);
4125 int rank_left = get_rank(type_left);
4126 int rank_right = get_rank(type_right);
4127 if(rank_left < rank_right) {
4128 if(signed_left == signed_right || !signed_right) {
4134 if(signed_left == signed_right || !signed_left) {
4143 * Check the semantic restrictions for a binary expression.
4145 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4147 expression_t *left = expression->left;
4148 expression_t *right = expression->right;
4149 type_t *orig_type_left = left->base.datatype;
4150 type_t *orig_type_right = right->base.datatype;
4152 if(orig_type_left == NULL || orig_type_right == NULL)
4155 type_t *type_left = skip_typeref(orig_type_left);
4156 type_t *type_right = skip_typeref(orig_type_right);
4158 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4159 /* TODO: improve error message */
4160 errorf(HERE, "operation needs arithmetic types");
4164 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4165 expression->left = create_implicit_cast(left, arithmetic_type);
4166 expression->right = create_implicit_cast(right, arithmetic_type);
4167 expression->expression.datatype = arithmetic_type;
4170 static void semantic_shift_op(binary_expression_t *expression)
4172 expression_t *left = expression->left;
4173 expression_t *right = expression->right;
4174 type_t *orig_type_left = left->base.datatype;
4175 type_t *orig_type_right = right->base.datatype;
4177 if(orig_type_left == NULL || orig_type_right == NULL)
4180 type_t *type_left = skip_typeref(orig_type_left);
4181 type_t *type_right = skip_typeref(orig_type_right);
4183 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4184 /* TODO: improve error message */
4185 errorf(HERE, "operation needs integer types");
4189 type_left = promote_integer(type_left);
4190 type_right = promote_integer(type_right);
4192 expression->left = create_implicit_cast(left, type_left);
4193 expression->right = create_implicit_cast(right, type_right);
4194 expression->expression.datatype = type_left;
4197 static void semantic_add(binary_expression_t *expression)
4199 expression_t *left = expression->left;
4200 expression_t *right = expression->right;
4201 type_t *orig_type_left = left->base.datatype;
4202 type_t *orig_type_right = right->base.datatype;
4204 if(orig_type_left == NULL || orig_type_right == NULL)
4207 type_t *type_left = skip_typeref(orig_type_left);
4208 type_t *type_right = skip_typeref(orig_type_right);
4211 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4212 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4213 expression->left = create_implicit_cast(left, arithmetic_type);
4214 expression->right = create_implicit_cast(right, arithmetic_type);
4215 expression->expression.datatype = arithmetic_type;
4217 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4218 expression->expression.datatype = type_left;
4219 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4220 expression->expression.datatype = type_right;
4222 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4226 static void semantic_sub(binary_expression_t *expression)
4228 expression_t *left = expression->left;
4229 expression_t *right = expression->right;
4230 type_t *orig_type_left = left->base.datatype;
4231 type_t *orig_type_right = right->base.datatype;
4233 if(orig_type_left == NULL || orig_type_right == NULL)
4236 type_t *type_left = skip_typeref(orig_type_left);
4237 type_t *type_right = skip_typeref(orig_type_right);
4240 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4241 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4242 expression->left = create_implicit_cast(left, arithmetic_type);
4243 expression->right = create_implicit_cast(right, arithmetic_type);
4244 expression->expression.datatype = arithmetic_type;
4246 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4247 expression->expression.datatype = type_left;
4248 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4249 if(!pointers_compatible(type_left, type_right)) {
4250 errorf(HERE, "pointers to incompatible objects to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4252 expression->expression.datatype = type_ptrdiff_t;
4255 errorf(HERE, "invalid operands to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4259 static void semantic_comparison(binary_expression_t *expression)
4261 expression_t *left = expression->left;
4262 expression_t *right = expression->right;
4263 type_t *orig_type_left = left->base.datatype;
4264 type_t *orig_type_right = right->base.datatype;
4266 if(orig_type_left == NULL || orig_type_right == NULL)
4269 type_t *type_left = skip_typeref(orig_type_left);
4270 type_t *type_right = skip_typeref(orig_type_right);
4272 /* TODO non-arithmetic types */
4273 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4274 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4275 expression->left = create_implicit_cast(left, arithmetic_type);
4276 expression->right = create_implicit_cast(right, arithmetic_type);
4277 expression->expression.datatype = arithmetic_type;
4278 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4279 /* TODO check compatibility */
4280 } else if (is_type_pointer(type_left)) {
4281 expression->right = create_implicit_cast(right, type_left);
4282 } else if (is_type_pointer(type_right)) {
4283 expression->left = create_implicit_cast(left, type_right);
4285 type_error_incompatible("invalid operands in comparison",
4286 token.source_position, type_left, type_right);
4288 expression->expression.datatype = type_int;
4291 static void semantic_arithmetic_assign(binary_expression_t *expression)
4293 expression_t *left = expression->left;
4294 expression_t *right = expression->right;
4295 type_t *orig_type_left = left->base.datatype;
4296 type_t *orig_type_right = right->base.datatype;
4298 if(orig_type_left == NULL || orig_type_right == NULL)
4301 type_t *type_left = skip_typeref(orig_type_left);
4302 type_t *type_right = skip_typeref(orig_type_right);
4304 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4305 /* TODO: improve error message */
4306 errorf(HERE, "operation needs arithmetic types");
4310 /* combined instructions are tricky. We can't create an implicit cast on
4311 * the left side, because we need the uncasted form for the store.
4312 * The ast2firm pass has to know that left_type must be right_type
4313 * for the arithmetic operation and create a cast by itself */
4314 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4315 expression->right = create_implicit_cast(right, arithmetic_type);
4316 expression->expression.datatype = type_left;
4319 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4321 expression_t *left = expression->left;
4322 expression_t *right = expression->right;
4323 type_t *orig_type_left = left->base.datatype;
4324 type_t *orig_type_right = right->base.datatype;
4326 if(orig_type_left == NULL || orig_type_right == NULL)
4329 type_t *type_left = skip_typeref(orig_type_left);
4330 type_t *type_right = skip_typeref(orig_type_right);
4332 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4333 /* combined instructions are tricky. We can't create an implicit cast on
4334 * the left side, because we need the uncasted form for the store.
4335 * The ast2firm pass has to know that left_type must be right_type
4336 * for the arithmetic operation and create a cast by itself */
4337 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4338 expression->right = create_implicit_cast(right, arithmetic_type);
4339 expression->expression.datatype = type_left;
4340 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4341 expression->expression.datatype = type_left;
4343 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4349 * Check the semantic restrictions of a logical expression.
4351 static void semantic_logical_op(binary_expression_t *expression)
4353 expression_t *left = expression->left;
4354 expression_t *right = expression->right;
4355 type_t *orig_type_left = left->base.datatype;
4356 type_t *orig_type_right = right->base.datatype;
4358 if(orig_type_left == NULL || orig_type_right == NULL)
4361 type_t *type_left = skip_typeref(orig_type_left);
4362 type_t *type_right = skip_typeref(orig_type_right);
4364 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4365 /* TODO: improve error message */
4366 errorf(HERE, "operation needs scalar types");
4370 expression->expression.datatype = type_int;
4374 * Checks if a compound type has constant fields.
4376 static bool has_const_fields(const compound_type_t *type)
4378 const context_t *context = &type->declaration->context;
4379 const declaration_t *declaration = context->declarations;
4381 for (; declaration != NULL; declaration = declaration->next) {
4382 if (declaration->namespc != NAMESPACE_NORMAL)
4385 const type_t *decl_type = skip_typeref(declaration->type);
4386 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4394 * Check the semantic restrictions of a binary assign expression.
4396 static void semantic_binexpr_assign(binary_expression_t *expression)
4398 expression_t *left = expression->left;
4399 type_t *orig_type_left = left->base.datatype;
4401 if(orig_type_left == NULL)
4404 type_t *type_left = revert_automatic_type_conversion(left);
4405 type_left = skip_typeref(orig_type_left);
4407 /* must be a modifiable lvalue */
4408 if (is_type_array(type_left)) {
4409 errorf(HERE, "cannot assign to arrays ('%E')", left);
4412 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4413 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left, orig_type_left);
4416 if(is_type_incomplete(type_left)) {
4417 errorf(HERE, "left-hand side of assignment '%E' has incomplete type '%T'", left, orig_type_left);
4420 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4421 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields", left, orig_type_left);
4425 semantic_assign(orig_type_left, &expression->right, "assignment");
4427 expression->expression.datatype = orig_type_left;
4430 static void semantic_comma(binary_expression_t *expression)
4432 expression->expression.datatype = expression->right->base.datatype;
4435 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4436 static expression_t *parse_##binexpression_type(unsigned precedence, \
4437 expression_t *left) \
4441 expression_t *right = parse_sub_expression(precedence + lr); \
4443 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4444 binexpr->binary.left = left; \
4445 binexpr->binary.right = right; \
4446 sfunc(&binexpr->binary); \
4451 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4452 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4453 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4454 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4455 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4456 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4457 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4458 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4459 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4461 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4462 semantic_comparison, 1)
4463 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4464 semantic_comparison, 1)
4465 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4466 semantic_comparison, 1)
4467 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4468 semantic_comparison, 1)
4470 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4471 semantic_binexpr_arithmetic, 1)
4472 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4473 semantic_binexpr_arithmetic, 1)
4474 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4475 semantic_binexpr_arithmetic, 1)
4476 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4477 semantic_logical_op, 1)
4478 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4479 semantic_logical_op, 1)
4480 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4481 semantic_shift_op, 1)
4482 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4483 semantic_shift_op, 1)
4484 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4485 semantic_arithmetic_addsubb_assign, 0)
4486 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4487 semantic_arithmetic_addsubb_assign, 0)
4488 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4489 semantic_arithmetic_assign, 0)
4490 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4491 semantic_arithmetic_assign, 0)
4492 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4493 semantic_arithmetic_assign, 0)
4494 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4495 semantic_arithmetic_assign, 0)
4496 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4497 semantic_arithmetic_assign, 0)
4498 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4499 semantic_arithmetic_assign, 0)
4500 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4501 semantic_arithmetic_assign, 0)
4502 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4503 semantic_arithmetic_assign, 0)
4505 static expression_t *parse_sub_expression(unsigned precedence)
4507 if(token.type < 0) {
4508 return expected_expression_error();
4511 expression_parser_function_t *parser
4512 = &expression_parsers[token.type];
4513 source_position_t source_position = token.source_position;
4516 if(parser->parser != NULL) {
4517 left = parser->parser(parser->precedence);
4519 left = parse_primary_expression();
4521 assert(left != NULL);
4522 left->base.source_position = source_position;
4525 if(token.type < 0) {
4526 return expected_expression_error();
4529 parser = &expression_parsers[token.type];
4530 if(parser->infix_parser == NULL)
4532 if(parser->infix_precedence < precedence)
4535 left = parser->infix_parser(parser->infix_precedence, left);
4537 assert(left != NULL);
4538 assert(left->kind != EXPR_UNKNOWN);
4539 left->base.source_position = source_position;
4546 * Parse an expression.
4548 static expression_t *parse_expression(void)
4550 return parse_sub_expression(1);
4554 * Register a parser for a prefix-like operator with given precedence.
4556 * @param parser the parser function
4557 * @param token_type the token type of the prefix token
4558 * @param precedence the precedence of the operator
4560 static void register_expression_parser(parse_expression_function parser,
4561 int token_type, unsigned precedence)
4563 expression_parser_function_t *entry = &expression_parsers[token_type];
4565 if(entry->parser != NULL) {
4566 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4567 panic("trying to register multiple expression parsers for a token");
4569 entry->parser = parser;
4570 entry->precedence = precedence;
4574 * Register a parser for an infix operator with given precedence.
4576 * @param parser the parser function
4577 * @param token_type the token type of the infix operator
4578 * @param precedence the precedence of the operator
4580 static void register_infix_parser(parse_expression_infix_function parser,
4581 int token_type, unsigned precedence)
4583 expression_parser_function_t *entry = &expression_parsers[token_type];
4585 if(entry->infix_parser != NULL) {
4586 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4587 panic("trying to register multiple infix expression parsers for a "
4590 entry->infix_parser = parser;
4591 entry->infix_precedence = precedence;
4595 * Initialize the expression parsers.
4597 static void init_expression_parsers(void)
4599 memset(&expression_parsers, 0, sizeof(expression_parsers));
4601 register_infix_parser(parse_array_expression, '[', 30);
4602 register_infix_parser(parse_call_expression, '(', 30);
4603 register_infix_parser(parse_select_expression, '.', 30);
4604 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4605 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4607 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4610 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4611 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4612 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4613 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4614 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4615 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4616 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4617 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4618 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4619 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4620 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4621 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4622 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4623 T_EXCLAMATIONMARKEQUAL, 13);
4624 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4625 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4626 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4627 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4628 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4629 register_infix_parser(parse_conditional_expression, '?', 7);
4630 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4631 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4632 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4633 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4634 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4635 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4636 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4637 T_LESSLESSEQUAL, 2);
4638 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4639 T_GREATERGREATEREQUAL, 2);
4640 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4642 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4644 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4647 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4649 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4650 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4651 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4652 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4653 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4654 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4655 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4657 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4659 register_expression_parser(parse_sizeof, T_sizeof, 25);
4660 register_expression_parser(parse_extension, T___extension__, 25);
4661 register_expression_parser(parse_builtin_classify_type,
4662 T___builtin_classify_type, 25);
4666 * Parse a asm statement constraints specification.
4668 static asm_constraint_t *parse_asm_constraints(void)
4670 asm_constraint_t *result = NULL;
4671 asm_constraint_t *last = NULL;
4673 while(token.type == T_STRING_LITERAL || token.type == '[') {
4674 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4675 memset(constraint, 0, sizeof(constraint[0]));
4677 if(token.type == '[') {
4679 if(token.type != T_IDENTIFIER) {
4680 parse_error_expected("while parsing asm constraint",
4684 constraint->symbol = token.v.symbol;
4689 constraint->constraints = parse_string_literals();
4691 constraint->expression = parse_expression();
4695 last->next = constraint;
4697 result = constraint;
4701 if(token.type != ',')
4710 * Parse a asm statement clobber specification.
4712 static asm_clobber_t *parse_asm_clobbers(void)
4714 asm_clobber_t *result = NULL;
4715 asm_clobber_t *last = NULL;
4717 while(token.type == T_STRING_LITERAL) {
4718 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4719 clobber->clobber = parse_string_literals();
4722 last->next = clobber;
4728 if(token.type != ',')
4737 * Parse an asm statement.
4739 static statement_t *parse_asm_statement(void)
4743 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4744 statement->base.source_position = token.source_position;
4746 asm_statement_t *asm_statement = &statement->asms;
4748 if(token.type == T_volatile) {
4750 asm_statement->is_volatile = true;
4754 asm_statement->asm_text = parse_string_literals();
4756 if(token.type != ':')
4760 asm_statement->inputs = parse_asm_constraints();
4761 if(token.type != ':')
4765 asm_statement->outputs = parse_asm_constraints();
4766 if(token.type != ':')
4770 asm_statement->clobbers = parse_asm_clobbers();
4779 * Parse a case statement.
4781 static statement_t *parse_case_statement(void)
4785 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4787 statement->base.source_position = token.source_position;
4788 statement->case_label.expression = parse_expression();
4791 statement->case_label.label_statement = parse_statement();
4797 * Parse a default statement.
4799 static statement_t *parse_default_statement(void)
4803 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4805 statement->base.source_position = token.source_position;
4808 statement->label.label_statement = parse_statement();
4814 * Return the declaration for a given label symbol or create a new one.
4816 static declaration_t *get_label(symbol_t *symbol)
4818 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4819 assert(current_function != NULL);
4820 /* if we found a label in the same function, then we already created the
4822 if(candidate != NULL
4823 && candidate->parent_context == ¤t_function->context) {
4827 /* otherwise we need to create a new one */
4828 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4829 declaration->namespc = NAMESPACE_LABEL;
4830 declaration->symbol = symbol;
4832 label_push(declaration);
4838 * Parse a label statement.
4840 static statement_t *parse_label_statement(void)
4842 assert(token.type == T_IDENTIFIER);
4843 symbol_t *symbol = token.v.symbol;
4846 declaration_t *label = get_label(symbol);
4848 /* if source position is already set then the label is defined twice,
4849 * otherwise it was just mentioned in a goto so far */
4850 if(label->source_position.input_name != NULL) {
4851 errorf(HERE, "duplicate label '%Y'\n", symbol);
4852 errorf(label->source_position, "previous definition of '%Y' was here\n",
4855 label->source_position = token.source_position;
4858 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4860 label_statement->statement.kind = STATEMENT_LABEL;
4861 label_statement->statement.source_position = token.source_position;
4862 label_statement->label = label;
4866 if(token.type == '}') {
4867 /* TODO only warn? */
4868 errorf(HERE, "label at end of compound statement");
4869 return (statement_t*) label_statement;
4871 label_statement->label_statement = parse_statement();
4874 return (statement_t*) label_statement;
4878 * Parse an if statement.
4880 static statement_t *parse_if(void)
4884 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4885 statement->statement.kind = STATEMENT_IF;
4886 statement->statement.source_position = token.source_position;
4889 statement->condition = parse_expression();
4892 statement->true_statement = parse_statement();
4893 if(token.type == T_else) {
4895 statement->false_statement = parse_statement();
4898 return (statement_t*) statement;
4902 * Parse a switch statement.
4904 static statement_t *parse_switch(void)
4908 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4909 statement->statement.kind = STATEMENT_SWITCH;
4910 statement->statement.source_position = token.source_position;
4913 statement->expression = parse_expression();
4915 statement->body = parse_statement();
4917 return (statement_t*) statement;
4921 * Parse a while statement.
4923 static statement_t *parse_while(void)
4927 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4928 statement->statement.kind = STATEMENT_WHILE;
4929 statement->statement.source_position = token.source_position;
4932 statement->condition = parse_expression();
4934 statement->body = parse_statement();
4936 return (statement_t*) statement;
4940 * Parse a do statement.
4942 static statement_t *parse_do(void)
4946 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4947 statement->statement.kind = STATEMENT_DO_WHILE;
4948 statement->statement.source_position = token.source_position;
4950 statement->body = parse_statement();
4953 statement->condition = parse_expression();
4957 return (statement_t*) statement;
4961 * Parse a for statement.
4963 static statement_t *parse_for(void)
4967 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4968 statement->statement.kind = STATEMENT_FOR;
4969 statement->statement.source_position = token.source_position;
4973 int top = environment_top();
4974 context_t *last_context = context;
4975 set_context(&statement->context);
4977 if(token.type != ';') {
4978 if(is_declaration_specifier(&token, false)) {
4979 parse_declaration(record_declaration);
4981 statement->initialisation = parse_expression();
4988 if(token.type != ';') {
4989 statement->condition = parse_expression();
4992 if(token.type != ')') {
4993 statement->step = parse_expression();
4996 statement->body = parse_statement();
4998 assert(context == &statement->context);
4999 set_context(last_context);
5000 environment_pop_to(top);
5002 return (statement_t*) statement;
5006 * Parse a goto statement.
5008 static statement_t *parse_goto(void)
5012 if(token.type != T_IDENTIFIER) {
5013 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5017 symbol_t *symbol = token.v.symbol;
5020 declaration_t *label = get_label(symbol);
5022 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5024 statement->statement.kind = STATEMENT_GOTO;
5025 statement->statement.source_position = token.source_position;
5027 statement->label = label;
5031 return (statement_t*) statement;
5035 * Parse a continue statement.
5037 static statement_t *parse_continue(void)
5042 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5043 statement->kind = STATEMENT_CONTINUE;
5044 statement->base.source_position = token.source_position;
5050 * Parse a break statement.
5052 static statement_t *parse_break(void)
5057 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5058 statement->kind = STATEMENT_BREAK;
5059 statement->base.source_position = token.source_position;
5065 * Parse a return statement.
5067 static statement_t *parse_return(void)
5071 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5073 statement->statement.kind = STATEMENT_RETURN;
5074 statement->statement.source_position = token.source_position;
5076 assert(is_type_function(current_function->type));
5077 function_type_t *function_type = ¤t_function->type->function;
5078 type_t *return_type = function_type->return_type;
5080 expression_t *return_value = NULL;
5081 if(token.type != ';') {
5082 return_value = parse_expression();
5086 if(return_type == NULL)
5087 return (statement_t*) statement;
5088 if(return_value != NULL && return_value->base.datatype == NULL)
5089 return (statement_t*) statement;
5091 return_type = skip_typeref(return_type);
5093 if(return_value != NULL) {
5094 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5096 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5097 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5098 warningf(HERE, "'return' with a value, in function returning void");
5099 return_value = NULL;
5101 if(return_type != NULL) {
5102 semantic_assign(return_type, &return_value, "'return'");
5106 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5107 warningf(HERE, "'return' without value, in function returning non-void");
5110 statement->return_value = return_value;
5112 return (statement_t*) statement;
5116 * Parse a declaration statement.
5118 static statement_t *parse_declaration_statement(void)
5120 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5122 statement->base.source_position = token.source_position;
5124 declaration_t *before = last_declaration;
5125 parse_declaration(record_declaration);
5127 if(before == NULL) {
5128 statement->declaration.declarations_begin = context->declarations;
5130 statement->declaration.declarations_begin = before->next;
5132 statement->declaration.declarations_end = last_declaration;
5138 * Parse an expression statement, ie. expr ';'.
5140 static statement_t *parse_expression_statement(void)
5142 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5144 statement->base.source_position = token.source_position;
5145 statement->expression.expression = parse_expression();
5153 * Parse a statement.
5155 static statement_t *parse_statement(void)
5157 statement_t *statement = NULL;
5159 /* declaration or statement */
5160 switch(token.type) {
5162 statement = parse_asm_statement();
5166 statement = parse_case_statement();
5170 statement = parse_default_statement();
5174 statement = parse_compound_statement();
5178 statement = parse_if();
5182 statement = parse_switch();
5186 statement = parse_while();
5190 statement = parse_do();
5194 statement = parse_for();
5198 statement = parse_goto();
5202 statement = parse_continue();
5206 statement = parse_break();
5210 statement = parse_return();
5219 if(look_ahead(1)->type == ':') {
5220 statement = parse_label_statement();
5224 if(is_typedef_symbol(token.v.symbol)) {
5225 statement = parse_declaration_statement();
5229 statement = parse_expression_statement();
5232 case T___extension__:
5233 /* this can be a prefix to a declaration or an expression statement */
5234 /* we simply eat it now and parse the rest with tail recursion */
5237 } while(token.type == T___extension__);
5238 statement = parse_statement();
5242 statement = parse_declaration_statement();
5246 statement = parse_expression_statement();
5250 assert(statement == NULL
5251 || statement->base.source_position.input_name != NULL);
5257 * Parse a compound statement.
5259 static statement_t *parse_compound_statement(void)
5261 compound_statement_t *compound_statement
5262 = allocate_ast_zero(sizeof(compound_statement[0]));
5263 compound_statement->statement.kind = STATEMENT_COMPOUND;
5264 compound_statement->statement.source_position = token.source_position;
5268 int top = environment_top();
5269 context_t *last_context = context;
5270 set_context(&compound_statement->context);
5272 statement_t *last_statement = NULL;
5274 while(token.type != '}' && token.type != T_EOF) {
5275 statement_t *statement = parse_statement();
5276 if(statement == NULL)
5279 if(last_statement != NULL) {
5280 last_statement->base.next = statement;
5282 compound_statement->statements = statement;
5285 while(statement->base.next != NULL)
5286 statement = statement->base.next;
5288 last_statement = statement;
5291 if(token.type == '}') {
5294 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5297 assert(context == &compound_statement->context);
5298 set_context(last_context);
5299 environment_pop_to(top);
5301 return (statement_t*) compound_statement;
5305 * Initialize builtin types.
5307 static void initialize_builtin_types(void)
5309 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5310 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5311 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5312 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5313 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5314 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5315 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5316 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5318 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5319 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5320 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5321 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5325 * Parse a translation unit.
5327 static translation_unit_t *parse_translation_unit(void)
5329 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5331 assert(global_context == NULL);
5332 global_context = &unit->context;
5334 assert(context == NULL);
5335 set_context(&unit->context);
5337 initialize_builtin_types();
5339 while(token.type != T_EOF) {
5340 parse_external_declaration();
5343 assert(context == &unit->context);
5345 last_declaration = NULL;
5347 assert(global_context == &unit->context);
5348 global_context = NULL;
5356 * @return the translation unit or NULL if errors occurred.
5358 translation_unit_t *parse(void)
5360 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5361 label_stack = NEW_ARR_F(stack_entry_t, 0);
5362 diagnostic_count = 0;
5366 type_set_output(stderr);
5367 ast_set_output(stderr);
5369 lookahead_bufpos = 0;
5370 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5373 translation_unit_t *unit = parse_translation_unit();
5375 DEL_ARR_F(environment_stack);
5376 DEL_ARR_F(label_stack);
5385 * Initialize the parser.
5387 void init_parser(void)
5389 init_expression_parsers();
5390 obstack_init(&temp_obst);
5392 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5393 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5397 * Terminate the parser.
5399 void exit_parser(void)
5401 obstack_free(&temp_obst, NULL);