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%s' with type '%T'", get_namespace_prefix(namespc), symbol->string, 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 '%s' follows non-static declaration", symbol->string);
574 errorf(previous_declaration->source_position, "previous declaration of '%s' was here\n", symbol->string);
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 '%s'\n", symbol->string);
582 warningf(previous_declaration->source_position, "previous declaration of '%s' was here\n", symbol->string);
586 if (old_storage_class == STORAGE_CLASS_EXTERN &&
587 new_storage_class == STORAGE_CLASS_EXTERN) {
588 warningf(declaration->source_position, "redundant extern declaration for '%s'\n", symbol->string);
589 warningf(previous_declaration->source_position, "previous declaration of '%s' was here\n", symbol->string);
591 if (old_storage_class == new_storage_class) {
592 errorf(declaration->source_position, "redeclaration of '%s'\n", symbol->string);
594 errorf(declaration->source_position, "redeclaration of '%s' with different linkage\n", symbol->string);
596 errorf(previous_declaration->source_position, "previous declaration of '%s' was here", symbol->string);
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);
1118 type_t *expression_type = skip_typeref(expression->base.datatype);
1119 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1120 semantic_assign(type, &expression, "initializer");
1122 initializer_t *result = allocate_initializer_zero(INITIALIZER_VALUE);
1123 result->value.value = expression;
1131 static initializer_t *parse_sub_initializer(type_t *type,
1132 expression_t *expression,
1133 type_t *expression_type);
1135 static initializer_t *parse_sub_initializer_elem(type_t *type)
1137 if(token.type == '{') {
1138 return parse_sub_initializer(type, NULL, NULL);
1141 expression_t *expression = parse_assignment_expression();
1142 type_t *expression_type = skip_typeref(expression->base.datatype);
1144 return parse_sub_initializer(type, expression, expression_type);
1147 static bool had_initializer_brace_warning;
1149 static initializer_t *parse_sub_initializer(type_t *type,
1150 expression_t *expression,
1151 type_t *expression_type)
1153 if(is_type_scalar(type)) {
1154 /* there might be extra {} hierarchies */
1155 if(token.type == '{') {
1157 if(!had_initializer_brace_warning) {
1158 warningf(HERE, "braces around scalar initializer");
1159 had_initializer_brace_warning = true;
1161 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1162 if(token.type == ',') {
1164 /* TODO: warn about excessive elements */
1170 if(expression == NULL) {
1171 expression = parse_assignment_expression();
1173 return initializer_from_expression(type, expression);
1176 /* does the expression match the currently looked at object to initialize */
1177 if(expression != NULL) {
1178 initializer_t *result = initializer_from_expression(type, expression);
1183 bool read_paren = false;
1184 if(token.type == '{') {
1189 /* descend into subtype */
1190 initializer_t *result = NULL;
1191 initializer_t **elems;
1192 if(is_type_array(type)) {
1193 array_type_t *array_type = &type->array;
1194 type_t *element_type = array_type->element_type;
1195 element_type = skip_typeref(element_type);
1198 had_initializer_brace_warning = false;
1199 if(expression == NULL) {
1200 sub = parse_sub_initializer_elem(element_type);
1202 sub = parse_sub_initializer(element_type, expression,
1206 /* didn't match the subtypes -> try the parent type */
1208 assert(!read_paren);
1212 elems = NEW_ARR_F(initializer_t*, 0);
1213 ARR_APP1(initializer_t*, elems, sub);
1216 if(token.type == '}')
1219 if(token.type == '}')
1222 sub = parse_sub_initializer_elem(element_type);
1224 /* TODO error, do nicer cleanup */
1225 errorf(HERE, "member initializer didn't match");
1229 ARR_APP1(initializer_t*, elems, sub);
1232 assert(is_type_compound(type));
1233 compound_type_t *compound_type = &type->compound;
1234 context_t *context = &compound_type->declaration->context;
1236 declaration_t *first = context->declarations;
1239 type_t *first_type = first->type;
1240 first_type = skip_typeref(first_type);
1243 had_initializer_brace_warning = false;
1244 if(expression == NULL) {
1245 sub = parse_sub_initializer_elem(first_type);
1247 sub = parse_sub_initializer(first_type, expression,expression_type);
1250 /* didn't match the subtypes -> try our parent type */
1252 assert(!read_paren);
1256 elems = NEW_ARR_F(initializer_t*, 0);
1257 ARR_APP1(initializer_t*, elems, sub);
1259 declaration_t *iter = first->next;
1260 for( ; iter != NULL; iter = iter->next) {
1261 if(iter->symbol == NULL)
1263 if(iter->namespc != NAMESPACE_NORMAL)
1266 if(token.type == '}')
1269 if(token.type == '}')
1272 type_t *iter_type = iter->type;
1273 iter_type = skip_typeref(iter_type);
1275 sub = parse_sub_initializer_elem(iter_type);
1277 /* TODO error, do nicer cleanup */
1278 errorf(HERE, "member initializer didn't match");
1282 ARR_APP1(initializer_t*, elems, sub);
1286 int len = ARR_LEN(elems);
1287 size_t elems_size = sizeof(initializer_t*) * len;
1289 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1291 init->initializer.kind = INITIALIZER_LIST;
1293 memcpy(init->initializers, elems, elems_size);
1296 result = (initializer_t*) init;
1299 if(token.type == ',')
1306 static initializer_t *parse_initializer(type_t *type)
1308 initializer_t *result;
1310 type = skip_typeref(type);
1312 if(token.type != '{') {
1313 expression_t *expression = parse_assignment_expression();
1314 initializer_t *initializer = initializer_from_expression(type, expression);
1315 if(initializer == NULL) {
1316 errorf(HERE, "initializer expression '%E', type '%T' is incompatible with type '%T'", expression, expression->base.datatype, type);
1321 if(is_type_scalar(type)) {
1325 expression_t *expression = parse_assignment_expression();
1326 result = initializer_from_expression(type, expression);
1328 if(token.type == ',')
1334 result = parse_sub_initializer(type, NULL, NULL);
1342 static declaration_t *parse_compound_type_specifier(bool is_struct)
1350 symbol_t *symbol = NULL;
1351 declaration_t *declaration = NULL;
1353 if (token.type == T___attribute__) {
1358 if(token.type == T_IDENTIFIER) {
1359 symbol = token.v.symbol;
1363 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1365 declaration = get_declaration(symbol, NAMESPACE_UNION);
1367 } else if(token.type != '{') {
1369 parse_error_expected("while parsing struct type specifier",
1370 T_IDENTIFIER, '{', 0);
1372 parse_error_expected("while parsing union type specifier",
1373 T_IDENTIFIER, '{', 0);
1379 if(declaration == NULL) {
1380 declaration = allocate_ast_zero(sizeof(declaration[0]));
1383 declaration->namespc = NAMESPACE_STRUCT;
1385 declaration->namespc = NAMESPACE_UNION;
1387 declaration->source_position = token.source_position;
1388 declaration->symbol = symbol;
1389 record_declaration(declaration);
1392 if(token.type == '{') {
1393 if(declaration->init.is_defined) {
1394 assert(symbol != NULL);
1395 errorf(HERE, "multiple definition of %s %s", is_struct ? "struct" : "union", symbol->string);
1396 declaration->context.declarations = NULL;
1398 declaration->init.is_defined = true;
1400 int top = environment_top();
1401 context_t *last_context = context;
1402 set_context(&declaration->context);
1404 parse_compound_type_entries();
1407 assert(context == &declaration->context);
1408 set_context(last_context);
1409 environment_pop_to(top);
1415 static void parse_enum_entries(enum_type_t *const enum_type)
1419 if(token.type == '}') {
1421 errorf(HERE, "empty enum not allowed");
1426 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1428 if(token.type != T_IDENTIFIER) {
1429 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1433 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1434 entry->type = (type_t*) enum_type;
1435 entry->symbol = token.v.symbol;
1436 entry->source_position = token.source_position;
1439 if(token.type == '=') {
1441 entry->init.enum_value = parse_constant_expression();
1446 record_declaration(entry);
1448 if(token.type != ',')
1451 } while(token.type != '}');
1456 static type_t *parse_enum_specifier(void)
1460 declaration_t *declaration;
1463 if(token.type == T_IDENTIFIER) {
1464 symbol = token.v.symbol;
1467 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1468 } else if(token.type != '{') {
1469 parse_error_expected("while parsing enum type specifier",
1470 T_IDENTIFIER, '{', 0);
1477 if(declaration == NULL) {
1478 declaration = allocate_ast_zero(sizeof(declaration[0]));
1480 declaration->namespc = NAMESPACE_ENUM;
1481 declaration->source_position = token.source_position;
1482 declaration->symbol = symbol;
1485 type_t *const type = allocate_type_zero(TYPE_ENUM);
1486 type->enumt.declaration = declaration;
1488 if(token.type == '{') {
1489 if(declaration->init.is_defined) {
1490 errorf(HERE, "multiple definitions of enum %s", symbol->string);
1492 record_declaration(declaration);
1493 declaration->init.is_defined = 1;
1495 parse_enum_entries(&type->enumt);
1503 * if a symbol is a typedef to another type, return true
1505 static bool is_typedef_symbol(symbol_t *symbol)
1507 const declaration_t *const declaration =
1508 get_declaration(symbol, NAMESPACE_NORMAL);
1510 declaration != NULL &&
1511 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1514 static type_t *parse_typeof(void)
1522 expression_t *expression = NULL;
1525 switch(token.type) {
1526 case T___extension__:
1527 /* this can be a prefix to a typename or an expression */
1528 /* we simply eat it now. */
1531 } while(token.type == T___extension__);
1535 if(is_typedef_symbol(token.v.symbol)) {
1536 type = parse_typename();
1538 expression = parse_expression();
1539 type = expression->base.datatype;
1544 type = parse_typename();
1548 expression = parse_expression();
1549 type = expression->base.datatype;
1555 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1556 typeof_type->typeoft.expression = expression;
1557 typeof_type->typeoft.typeof_type = type;
1563 SPECIFIER_SIGNED = 1 << 0,
1564 SPECIFIER_UNSIGNED = 1 << 1,
1565 SPECIFIER_LONG = 1 << 2,
1566 SPECIFIER_INT = 1 << 3,
1567 SPECIFIER_DOUBLE = 1 << 4,
1568 SPECIFIER_CHAR = 1 << 5,
1569 SPECIFIER_SHORT = 1 << 6,
1570 SPECIFIER_LONG_LONG = 1 << 7,
1571 SPECIFIER_FLOAT = 1 << 8,
1572 SPECIFIER_BOOL = 1 << 9,
1573 SPECIFIER_VOID = 1 << 10,
1574 #ifdef PROVIDE_COMPLEX
1575 SPECIFIER_COMPLEX = 1 << 11,
1576 SPECIFIER_IMAGINARY = 1 << 12,
1580 static type_t *create_builtin_type(symbol_t *const symbol,
1581 type_t *const real_type)
1583 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1584 type->builtin.symbol = symbol;
1585 type->builtin.real_type = real_type;
1587 type_t *result = typehash_insert(type);
1588 if (type != result) {
1595 static type_t *get_typedef_type(symbol_t *symbol)
1597 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1598 if(declaration == NULL
1599 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1602 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1603 type->typedeft.declaration = declaration;
1608 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1610 type_t *type = NULL;
1611 unsigned type_qualifiers = 0;
1612 unsigned type_specifiers = 0;
1615 specifiers->source_position = token.source_position;
1618 switch(token.type) {
1621 #define MATCH_STORAGE_CLASS(token, class) \
1623 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1624 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1626 specifiers->storage_class = class; \
1630 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1631 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1632 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1633 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1634 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1637 switch (specifiers->storage_class) {
1638 case STORAGE_CLASS_NONE:
1639 specifiers->storage_class = STORAGE_CLASS_THREAD;
1642 case STORAGE_CLASS_EXTERN:
1643 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1646 case STORAGE_CLASS_STATIC:
1647 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1651 errorf(HERE, "multiple storage classes in declaration specifiers");
1657 /* type qualifiers */
1658 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1660 type_qualifiers |= qualifier; \
1664 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1665 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1666 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1668 case T___extension__:
1673 /* type specifiers */
1674 #define MATCH_SPECIFIER(token, specifier, name) \
1677 if(type_specifiers & specifier) { \
1678 errorf(HERE, "multiple " name " type specifiers given"); \
1680 type_specifiers |= specifier; \
1684 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1685 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1686 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1687 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1688 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1689 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1690 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1691 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1692 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1693 #ifdef PROVIDE_COMPLEX
1694 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1695 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1698 /* only in microsoft mode */
1699 specifiers->decl_modifiers |= DM_FORCEINLINE;
1703 specifiers->is_inline = true;
1708 if(type_specifiers & SPECIFIER_LONG_LONG) {
1709 errorf(HERE, "multiple type specifiers given");
1710 } else if(type_specifiers & SPECIFIER_LONG) {
1711 type_specifiers |= SPECIFIER_LONG_LONG;
1713 type_specifiers |= SPECIFIER_LONG;
1717 /* TODO: if type != NULL for the following rules should issue
1720 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1722 type->compound.declaration = parse_compound_type_specifier(true);
1726 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1728 type->compound.declaration = parse_compound_type_specifier(false);
1732 type = parse_enum_specifier();
1735 type = parse_typeof();
1737 case T___builtin_va_list:
1738 type = duplicate_type(type_valist);
1742 case T___attribute__:
1747 case T_IDENTIFIER: {
1748 type_t *typedef_type = get_typedef_type(token.v.symbol);
1750 if(typedef_type == NULL)
1751 goto finish_specifiers;
1754 type = typedef_type;
1758 /* function specifier */
1760 goto finish_specifiers;
1767 atomic_type_type_t atomic_type;
1769 /* match valid basic types */
1770 switch(type_specifiers) {
1771 case SPECIFIER_VOID:
1772 atomic_type = ATOMIC_TYPE_VOID;
1774 case SPECIFIER_CHAR:
1775 atomic_type = ATOMIC_TYPE_CHAR;
1777 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1778 atomic_type = ATOMIC_TYPE_SCHAR;
1780 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1781 atomic_type = ATOMIC_TYPE_UCHAR;
1783 case SPECIFIER_SHORT:
1784 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1785 case SPECIFIER_SHORT | SPECIFIER_INT:
1786 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1787 atomic_type = ATOMIC_TYPE_SHORT;
1789 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1790 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1791 atomic_type = ATOMIC_TYPE_USHORT;
1794 case SPECIFIER_SIGNED:
1795 case SPECIFIER_SIGNED | SPECIFIER_INT:
1796 atomic_type = ATOMIC_TYPE_INT;
1798 case SPECIFIER_UNSIGNED:
1799 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1800 atomic_type = ATOMIC_TYPE_UINT;
1802 case SPECIFIER_LONG:
1803 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1804 case SPECIFIER_LONG | SPECIFIER_INT:
1805 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1806 atomic_type = ATOMIC_TYPE_LONG;
1808 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1809 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1810 atomic_type = ATOMIC_TYPE_ULONG;
1812 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1813 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1814 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1815 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1817 atomic_type = ATOMIC_TYPE_LONGLONG;
1819 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1820 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1822 atomic_type = ATOMIC_TYPE_ULONGLONG;
1824 case SPECIFIER_FLOAT:
1825 atomic_type = ATOMIC_TYPE_FLOAT;
1827 case SPECIFIER_DOUBLE:
1828 atomic_type = ATOMIC_TYPE_DOUBLE;
1830 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1831 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1833 case SPECIFIER_BOOL:
1834 atomic_type = ATOMIC_TYPE_BOOL;
1836 #ifdef PROVIDE_COMPLEX
1837 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1838 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1840 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1841 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1843 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1844 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1846 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1847 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1849 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1850 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1852 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1853 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1857 /* invalid specifier combination, give an error message */
1858 if(type_specifiers == 0) {
1859 if (! strict_mode) {
1860 warningf(HERE, "no type specifiers in declaration, using int");
1861 atomic_type = ATOMIC_TYPE_INT;
1864 errorf(HERE, "no type specifiers given in declaration");
1866 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1867 (type_specifiers & SPECIFIER_UNSIGNED)) {
1868 errorf(HERE, "signed and unsigned specifiers gives");
1869 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1870 errorf(HERE, "only integer types can be signed or unsigned");
1872 errorf(HERE, "multiple datatypes in declaration");
1874 atomic_type = ATOMIC_TYPE_INVALID;
1877 type = allocate_type_zero(TYPE_ATOMIC);
1878 type->atomic.atype = atomic_type;
1881 if(type_specifiers != 0) {
1882 errorf(HERE, "multiple datatypes in declaration");
1886 type->base.qualifiers = type_qualifiers;
1888 type_t *result = typehash_insert(type);
1889 if(newtype && result != type) {
1893 specifiers->type = result;
1896 static type_qualifiers_t parse_type_qualifiers(void)
1898 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1901 switch(token.type) {
1902 /* type qualifiers */
1903 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1904 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1905 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1908 return type_qualifiers;
1913 static declaration_t *parse_identifier_list(void)
1915 declaration_t *declarations = NULL;
1916 declaration_t *last_declaration = NULL;
1918 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1920 declaration->source_position = token.source_position;
1921 declaration->symbol = token.v.symbol;
1924 if(last_declaration != NULL) {
1925 last_declaration->next = declaration;
1927 declarations = declaration;
1929 last_declaration = declaration;
1931 if(token.type != ',')
1934 } while(token.type == T_IDENTIFIER);
1936 return declarations;
1939 static void semantic_parameter(declaration_t *declaration)
1941 /* TODO: improve error messages */
1943 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1944 errorf(HERE, "typedef not allowed in parameter list");
1945 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1946 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1947 errorf(HERE, "parameter may only have none or register storage class");
1950 type_t *orig_type = declaration->type;
1951 if(orig_type == NULL)
1953 type_t *type = skip_typeref(orig_type);
1955 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1956 * into a pointer. § 6.7.5.3 (7) */
1957 if (is_type_array(type)) {
1958 const array_type_t *arr_type = &type->array;
1959 type_t *element_type = arr_type->element_type;
1961 type = make_pointer_type(element_type, type->base.qualifiers);
1963 declaration->type = type;
1966 if(is_type_incomplete(type)) {
1967 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%s'", orig_type, declaration->symbol->string);
1971 static declaration_t *parse_parameter(void)
1973 declaration_specifiers_t specifiers;
1974 memset(&specifiers, 0, sizeof(specifiers));
1976 parse_declaration_specifiers(&specifiers);
1978 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1980 semantic_parameter(declaration);
1985 static declaration_t *parse_parameters(function_type_t *type)
1987 if(token.type == T_IDENTIFIER) {
1988 symbol_t *symbol = token.v.symbol;
1989 if(!is_typedef_symbol(symbol)) {
1990 type->kr_style_parameters = true;
1991 return parse_identifier_list();
1995 if(token.type == ')') {
1996 type->unspecified_parameters = 1;
1999 if(token.type == T_void && look_ahead(1)->type == ')') {
2004 declaration_t *declarations = NULL;
2005 declaration_t *declaration;
2006 declaration_t *last_declaration = NULL;
2007 function_parameter_t *parameter;
2008 function_parameter_t *last_parameter = NULL;
2011 switch(token.type) {
2015 return declarations;
2018 case T___extension__:
2020 declaration = parse_parameter();
2022 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2023 memset(parameter, 0, sizeof(parameter[0]));
2024 parameter->type = declaration->type;
2026 if(last_parameter != NULL) {
2027 last_declaration->next = declaration;
2028 last_parameter->next = parameter;
2030 type->parameters = parameter;
2031 declarations = declaration;
2033 last_parameter = parameter;
2034 last_declaration = declaration;
2038 return declarations;
2040 if(token.type != ',')
2041 return declarations;
2051 } construct_type_type_t;
2053 typedef struct construct_type_t construct_type_t;
2054 struct construct_type_t {
2055 construct_type_type_t type;
2056 construct_type_t *next;
2059 typedef struct parsed_pointer_t parsed_pointer_t;
2060 struct parsed_pointer_t {
2061 construct_type_t construct_type;
2062 type_qualifiers_t type_qualifiers;
2065 typedef struct construct_function_type_t construct_function_type_t;
2066 struct construct_function_type_t {
2067 construct_type_t construct_type;
2068 type_t *function_type;
2071 typedef struct parsed_array_t parsed_array_t;
2072 struct parsed_array_t {
2073 construct_type_t construct_type;
2074 type_qualifiers_t type_qualifiers;
2080 typedef struct construct_base_type_t construct_base_type_t;
2081 struct construct_base_type_t {
2082 construct_type_t construct_type;
2086 static construct_type_t *parse_pointer_declarator(void)
2090 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2091 memset(pointer, 0, sizeof(pointer[0]));
2092 pointer->construct_type.type = CONSTRUCT_POINTER;
2093 pointer->type_qualifiers = parse_type_qualifiers();
2095 return (construct_type_t*) pointer;
2098 static construct_type_t *parse_array_declarator(void)
2102 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2103 memset(array, 0, sizeof(array[0]));
2104 array->construct_type.type = CONSTRUCT_ARRAY;
2106 if(token.type == T_static) {
2107 array->is_static = true;
2111 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2112 if(type_qualifiers != 0) {
2113 if(token.type == T_static) {
2114 array->is_static = true;
2118 array->type_qualifiers = type_qualifiers;
2120 if(token.type == '*' && look_ahead(1)->type == ']') {
2121 array->is_variable = true;
2123 } else if(token.type != ']') {
2124 array->size = parse_assignment_expression();
2129 return (construct_type_t*) array;
2132 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2136 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2138 declaration_t *parameters = parse_parameters(&type->function);
2139 if(declaration != NULL) {
2140 declaration->context.declarations = parameters;
2143 construct_function_type_t *construct_function_type =
2144 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2145 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2146 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2147 construct_function_type->function_type = type;
2151 return (construct_type_t*) construct_function_type;
2154 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2155 bool may_be_abstract)
2157 /* construct a single linked list of construct_type_t's which describe
2158 * how to construct the final declarator type */
2159 construct_type_t *first = NULL;
2160 construct_type_t *last = NULL;
2163 while(token.type == '*') {
2164 construct_type_t *type = parse_pointer_declarator();
2175 /* TODO: find out if this is correct */
2178 construct_type_t *inner_types = NULL;
2180 switch(token.type) {
2182 if(declaration == NULL) {
2183 errorf(HERE, "no identifier expected in typename");
2185 declaration->symbol = token.v.symbol;
2186 declaration->source_position = token.source_position;
2192 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2198 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2199 /* avoid a loop in the outermost scope, because eat_statement doesn't
2201 if(token.type == '}' && current_function == NULL) {
2209 construct_type_t *p = last;
2212 construct_type_t *type;
2213 switch(token.type) {
2215 type = parse_function_declarator(declaration);
2218 type = parse_array_declarator();
2221 goto declarator_finished;
2224 /* insert in the middle of the list (behind p) */
2226 type->next = p->next;
2237 declarator_finished:
2240 /* append inner_types at the end of the list, we don't to set last anymore
2241 * as it's not needed anymore */
2243 assert(first == NULL);
2244 first = inner_types;
2246 last->next = inner_types;
2252 static type_t *construct_declarator_type(construct_type_t *construct_list,
2255 construct_type_t *iter = construct_list;
2256 for( ; iter != NULL; iter = iter->next) {
2257 switch(iter->type) {
2258 case CONSTRUCT_INVALID:
2259 panic("invalid type construction found");
2260 case CONSTRUCT_FUNCTION: {
2261 construct_function_type_t *construct_function_type
2262 = (construct_function_type_t*) iter;
2264 type_t *function_type = construct_function_type->function_type;
2266 function_type->function.return_type = type;
2268 type = function_type;
2272 case CONSTRUCT_POINTER: {
2273 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2274 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2275 pointer_type->pointer.points_to = type;
2276 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2278 type = pointer_type;
2282 case CONSTRUCT_ARRAY: {
2283 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2284 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2286 array_type->base.qualifiers = parsed_array->type_qualifiers;
2287 array_type->array.element_type = type;
2288 array_type->array.is_static = parsed_array->is_static;
2289 array_type->array.is_variable = parsed_array->is_variable;
2290 array_type->array.size = parsed_array->size;
2297 type_t *hashed_type = typehash_insert(type);
2298 if(hashed_type != type) {
2299 /* the function type was constructed earlier freeing it here will
2300 * destroy other types... */
2301 if(iter->type != CONSTRUCT_FUNCTION) {
2311 static declaration_t *parse_declarator(
2312 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2314 type_t *type = specifiers->type;
2315 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2316 declaration->storage_class = specifiers->storage_class;
2317 declaration->modifiers = specifiers->decl_modifiers;
2318 declaration->is_inline = specifiers->is_inline;
2320 construct_type_t *construct_type
2321 = parse_inner_declarator(declaration, may_be_abstract);
2322 declaration->type = construct_declarator_type(construct_type, type);
2324 if(construct_type != NULL) {
2325 obstack_free(&temp_obst, construct_type);
2331 static type_t *parse_abstract_declarator(type_t *base_type)
2333 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2335 type_t *result = construct_declarator_type(construct_type, base_type);
2336 if(construct_type != NULL) {
2337 obstack_free(&temp_obst, construct_type);
2343 static declaration_t *record_declaration(declaration_t *declaration)
2345 assert(declaration->parent_context == NULL);
2346 assert(context != NULL);
2348 symbol_t *symbol = declaration->symbol;
2349 if(symbol != NULL) {
2350 declaration_t *alias = environment_push(declaration);
2351 if(alias != declaration)
2354 declaration->parent_context = context;
2357 if(last_declaration != NULL) {
2358 last_declaration->next = declaration;
2360 context->declarations = declaration;
2362 last_declaration = declaration;
2367 static void parser_error_multiple_definition(declaration_t *declaration,
2368 const source_position_t source_position)
2370 errorf(source_position, "multiple definition of symbol '%s'", declaration->symbol->string);
2371 errorf(declaration->source_position, "this is the location of the previous definition.");
2374 static bool is_declaration_specifier(const token_t *token,
2375 bool only_type_specifiers)
2377 switch(token->type) {
2381 return is_typedef_symbol(token->v.symbol);
2383 case T___extension__:
2386 return !only_type_specifiers;
2393 static void parse_init_declarator_rest(declaration_t *declaration)
2397 type_t *orig_type = declaration->type;
2398 type_t *type = NULL;
2399 if(orig_type != NULL)
2400 type = skip_typeref(orig_type);
2402 if(declaration->init.initializer != NULL) {
2403 parser_error_multiple_definition(declaration, token.source_position);
2406 initializer_t *initializer = parse_initializer(type);
2408 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2409 * the array type size */
2410 if(type != NULL && is_type_array(type) && initializer != NULL) {
2411 array_type_t *array_type = &type->array;
2413 if(array_type->size == NULL) {
2414 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2416 cnst->base.datatype = type_size_t;
2418 switch (initializer->kind) {
2419 case INITIALIZER_LIST: {
2420 initializer_list_t *const list = &initializer->list;
2421 cnst->conste.v.int_value = list->len;
2425 case INITIALIZER_STRING: {
2426 initializer_string_t *const string = &initializer->string;
2427 cnst->conste.v.int_value = strlen(string->string) + 1;
2431 case INITIALIZER_WIDE_STRING: {
2432 initializer_wide_string_t *const string = &initializer->wide_string;
2433 cnst->conste.v.int_value = string->string.size;
2438 panic("invalid initializer type");
2441 array_type->size = cnst;
2445 if(type != NULL && is_type_function(type)) {
2446 errorf(declaration->source_position, "initializers not allowed for function types at declator '%s' (type '%T')", declaration->symbol->string, orig_type);
2448 declaration->init.initializer = initializer;
2452 /* parse rest of a declaration without any declarator */
2453 static void parse_anonymous_declaration_rest(
2454 const declaration_specifiers_t *specifiers,
2455 parsed_declaration_func finished_declaration)
2459 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2461 declaration->type = specifiers->type;
2462 declaration->storage_class = specifiers->storage_class;
2463 declaration->source_position = specifiers->source_position;
2465 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2466 warningf(declaration->source_position, "useless storage class in empty declaration");
2469 type_t *type = declaration->type;
2470 switch (type->kind) {
2471 case TYPE_COMPOUND_STRUCT:
2472 case TYPE_COMPOUND_UNION: {
2473 const compound_type_t *compound_type = &type->compound;
2474 if (compound_type->declaration->symbol == NULL) {
2475 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2484 warningf(declaration->source_position, "empty declaration");
2488 finished_declaration(declaration);
2491 static void parse_declaration_rest(declaration_t *ndeclaration,
2492 const declaration_specifiers_t *specifiers,
2493 parsed_declaration_func finished_declaration)
2496 declaration_t *declaration = finished_declaration(ndeclaration);
2498 type_t *orig_type = declaration->type;
2499 type_t *type = skip_typeref(orig_type);
2501 if(type->kind != TYPE_FUNCTION && declaration->is_inline) {
2502 warningf(declaration->source_position, "variable '%s' declared 'inline'\n", declaration->symbol->string);
2505 if(token.type == '=') {
2506 parse_init_declarator_rest(declaration);
2509 if(token.type != ',')
2513 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2518 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2520 /* TODO: check that it was actually a parameter that gets a type */
2522 /* we should have a declaration for the parameter in the current
2524 return record_declaration(declaration);
2527 static void parse_declaration(parsed_declaration_func finished_declaration)
2529 declaration_specifiers_t specifiers;
2530 memset(&specifiers, 0, sizeof(specifiers));
2531 parse_declaration_specifiers(&specifiers);
2533 if(token.type == ';') {
2534 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2536 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2537 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2541 static void parse_kr_declaration_list(declaration_t *declaration)
2543 type_t *type = skip_typeref(declaration->type);
2544 if(!is_type_function(type))
2547 if(!type->function.kr_style_parameters)
2550 /* push function parameters */
2551 int top = environment_top();
2552 context_t *last_context = context;
2553 set_context(&declaration->context);
2555 declaration_t *parameter = declaration->context.declarations;
2556 for( ; parameter != NULL; parameter = parameter->next) {
2557 environment_push(parameter);
2560 /* parse declaration list */
2561 while(is_declaration_specifier(&token, false)) {
2562 parse_declaration(finished_kr_declaration);
2565 /* pop function parameters */
2566 assert(context == &declaration->context);
2567 set_context(last_context);
2568 environment_pop_to(top);
2570 /* update function type */
2571 type_t *new_type = duplicate_type(type);
2572 new_type->function.kr_style_parameters = false;
2574 function_parameter_t *parameters = NULL;
2575 function_parameter_t *last_parameter = NULL;
2577 declaration_t *parameter_declaration = declaration->context.declarations;
2578 for( ; parameter_declaration != NULL;
2579 parameter_declaration = parameter_declaration->next) {
2580 type_t *parameter_type = parameter_declaration->type;
2581 if(parameter_type == NULL) {
2583 errorf(HERE, "no type specified for function parameter '%s'", parameter_declaration->symbol->string);
2585 warningf(HERE, "no type specified for function parameter '%s', using int", parameter_declaration->symbol->string);
2586 parameter_type = type_int;
2587 parameter_declaration->type = parameter_type;
2591 semantic_parameter(parameter_declaration);
2592 parameter_type = parameter_declaration->type;
2594 function_parameter_t *function_parameter
2595 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2596 memset(function_parameter, 0, sizeof(function_parameter[0]));
2598 function_parameter->type = parameter_type;
2599 if(last_parameter != NULL) {
2600 last_parameter->next = function_parameter;
2602 parameters = function_parameter;
2604 last_parameter = function_parameter;
2606 new_type->function.parameters = parameters;
2608 type = typehash_insert(new_type);
2609 if(type != new_type) {
2610 obstack_free(type_obst, new_type);
2613 declaration->type = type;
2616 static void parse_external_declaration(void)
2618 /* function-definitions and declarations both start with declaration
2620 declaration_specifiers_t specifiers;
2621 memset(&specifiers, 0, sizeof(specifiers));
2622 parse_declaration_specifiers(&specifiers);
2624 /* must be a declaration */
2625 if(token.type == ';') {
2626 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2630 /* declarator is common to both function-definitions and declarations */
2631 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2633 /* must be a declaration */
2634 if(token.type == ',' || token.type == '=' || token.type == ';') {
2635 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2639 /* must be a function definition */
2640 parse_kr_declaration_list(ndeclaration);
2642 if(token.type != '{') {
2643 parse_error_expected("while parsing function definition", '{', 0);
2648 type_t *type = ndeclaration->type;
2654 /* note that we don't skip typerefs: the standard doesn't allow them here
2655 * (so we can't use is_type_function here) */
2656 if(type->kind != TYPE_FUNCTION) {
2657 errorf(HERE, "declarator '%#T' has a body but is not a function type", type, ndeclaration->symbol);
2662 /* § 6.7.5.3 (14) a function definition with () means no
2663 * parameters (and not unspecified parameters) */
2664 if(type->function.unspecified_parameters) {
2665 type_t *duplicate = duplicate_type(type);
2666 duplicate->function.unspecified_parameters = false;
2668 type = typehash_insert(duplicate);
2669 if(type != duplicate) {
2670 obstack_free(type_obst, duplicate);
2672 ndeclaration->type = type;
2675 declaration_t *declaration = record_declaration(ndeclaration);
2676 if(ndeclaration != declaration) {
2677 memcpy(&declaration->context, &ndeclaration->context,
2678 sizeof(declaration->context));
2680 type = skip_typeref(declaration->type);
2682 /* push function parameters and switch context */
2683 int top = environment_top();
2684 context_t *last_context = context;
2685 set_context(&declaration->context);
2687 declaration_t *parameter = declaration->context.declarations;
2688 for( ; parameter != NULL; parameter = parameter->next) {
2689 environment_push(parameter);
2692 if(declaration->init.statement != NULL) {
2693 parser_error_multiple_definition(declaration, token.source_position);
2695 goto end_of_parse_external_declaration;
2697 /* parse function body */
2698 int label_stack_top = label_top();
2699 declaration_t *old_current_function = current_function;
2700 current_function = declaration;
2702 declaration->init.statement = parse_compound_statement();
2704 assert(current_function == declaration);
2705 current_function = old_current_function;
2706 label_pop_to(label_stack_top);
2709 end_of_parse_external_declaration:
2710 assert(context == &declaration->context);
2711 set_context(last_context);
2712 environment_pop_to(top);
2715 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2717 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2718 type->bitfield.base = base;
2719 type->bitfield.size = size;
2724 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2726 /* TODO: check constraints for struct declarations (in specifiers) */
2728 declaration_t *declaration;
2730 if(token.type == ':') {
2733 type_t *base_type = specifiers->type;
2734 expression_t *size = parse_constant_expression();
2736 type_t *type = make_bitfield_type(base_type, size);
2738 declaration = allocate_ast_zero(sizeof(declaration[0]));
2740 declaration->namespc = NAMESPACE_NORMAL;
2741 declaration->storage_class = STORAGE_CLASS_NONE;
2742 declaration->source_position = token.source_position;
2743 declaration->modifiers = specifiers->decl_modifiers;
2744 declaration->type = type;
2746 record_declaration(declaration);
2748 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2750 if(token.type == ':') {
2752 expression_t *size = parse_constant_expression();
2754 type_t *type = make_bitfield_type(declaration->type, size);
2755 declaration->type = type;
2758 record_declaration(declaration);
2760 if(token.type != ',')
2767 static void parse_compound_type_entries(void)
2771 while(token.type != '}' && token.type != T_EOF) {
2772 declaration_specifiers_t specifiers;
2773 memset(&specifiers, 0, sizeof(specifiers));
2774 parse_declaration_specifiers(&specifiers);
2776 parse_struct_declarators(&specifiers);
2778 if(token.type == T_EOF) {
2779 errorf(HERE, "EOF while parsing struct");
2784 static type_t *parse_typename(void)
2786 declaration_specifiers_t specifiers;
2787 memset(&specifiers, 0, sizeof(specifiers));
2788 parse_declaration_specifiers(&specifiers);
2789 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2790 /* TODO: improve error message, user does probably not know what a
2791 * storage class is...
2793 errorf(HERE, "typename may not have a storage class");
2796 type_t *result = parse_abstract_declarator(specifiers.type);
2804 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2805 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2806 expression_t *left);
2808 typedef struct expression_parser_function_t expression_parser_function_t;
2809 struct expression_parser_function_t {
2810 unsigned precedence;
2811 parse_expression_function parser;
2812 unsigned infix_precedence;
2813 parse_expression_infix_function infix_parser;
2816 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2819 * Creates a new invalid expression.
2821 static expression_t *create_invalid_expression(void)
2823 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2824 expression->base.source_position = token.source_position;
2828 static expression_t *expected_expression_error(void)
2830 errorf(HERE, "expected expression, got token '%K'", &token);
2834 return create_invalid_expression();
2838 * Parse a string constant.
2840 static expression_t *parse_string_const(void)
2842 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2843 cnst->base.datatype = type_string;
2844 cnst->string.value = parse_string_literals();
2850 * Parse a wide string constant.
2852 static expression_t *parse_wide_string_const(void)
2854 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2855 cnst->base.datatype = type_wchar_t_ptr;
2856 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2862 * Parse an integer constant.
2864 static expression_t *parse_int_const(void)
2866 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2867 cnst->base.datatype = token.datatype;
2868 cnst->conste.v.int_value = token.v.intvalue;
2876 * Parse a float constant.
2878 static expression_t *parse_float_const(void)
2880 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2881 cnst->base.datatype = token.datatype;
2882 cnst->conste.v.float_value = token.v.floatvalue;
2889 static declaration_t *create_implicit_function(symbol_t *symbol,
2890 const source_position_t source_position)
2892 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2893 ntype->function.return_type = type_int;
2894 ntype->function.unspecified_parameters = true;
2896 type_t *type = typehash_insert(ntype);
2901 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2903 declaration->storage_class = STORAGE_CLASS_EXTERN;
2904 declaration->type = type;
2905 declaration->symbol = symbol;
2906 declaration->source_position = source_position;
2908 /* prepend the implicit definition to the global context
2909 * this is safe since the symbol wasn't declared as anything else yet
2911 assert(symbol->declaration == NULL);
2913 context_t *last_context = context;
2914 context = global_context;
2916 environment_push(declaration);
2917 declaration->next = context->declarations;
2918 context->declarations = declaration;
2920 context = last_context;
2926 * Creates a return_type (func)(argument_type) function type if not
2929 * @param return_type the return type
2930 * @param argument_type the argument type
2932 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2934 function_parameter_t *parameter
2935 = obstack_alloc(type_obst, sizeof(parameter[0]));
2936 memset(parameter, 0, sizeof(parameter[0]));
2937 parameter->type = argument_type;
2939 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2940 type->function.return_type = return_type;
2941 type->function.parameters = parameter;
2943 type_t *result = typehash_insert(type);
2944 if(result != type) {
2952 * Creates a function type for some function like builtins.
2954 * @param symbol the symbol describing the builtin
2956 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2958 switch(symbol->ID) {
2959 case T___builtin_alloca:
2960 return make_function_1_type(type_void_ptr, type_size_t);
2961 case T___builtin_nan:
2962 return make_function_1_type(type_double, type_string);
2963 case T___builtin_nanf:
2964 return make_function_1_type(type_float, type_string);
2965 case T___builtin_nand:
2966 return make_function_1_type(type_long_double, type_string);
2967 case T___builtin_va_end:
2968 return make_function_1_type(type_void, type_valist);
2970 panic("not implemented builtin symbol found");
2975 * Performs automatic type cast as described in § 6.3.2.1.
2977 * @param orig_type the original type
2979 static type_t *automatic_type_conversion(type_t *orig_type)
2981 if(orig_type == NULL)
2984 type_t *type = skip_typeref(orig_type);
2985 if(is_type_array(type)) {
2986 array_type_t *array_type = &type->array;
2987 type_t *element_type = array_type->element_type;
2988 unsigned qualifiers = array_type->type.qualifiers;
2990 return make_pointer_type(element_type, qualifiers);
2993 if(is_type_function(type)) {
2994 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3001 * reverts the automatic casts of array to pointer types and function
3002 * to function-pointer types as defined § 6.3.2.1
3004 type_t *revert_automatic_type_conversion(const expression_t *expression)
3006 if(expression->base.datatype == NULL)
3009 switch(expression->kind) {
3010 case EXPR_REFERENCE: {
3011 const reference_expression_t *ref = &expression->reference;
3012 return ref->declaration->type;
3015 const select_expression_t *select = &expression->select;
3016 return select->compound_entry->type;
3018 case EXPR_UNARY_DEREFERENCE: {
3019 expression_t *value = expression->unary.value;
3020 type_t *type = skip_typeref(value->base.datatype);
3021 pointer_type_t *pointer_type = &type->pointer;
3023 return pointer_type->points_to;
3025 case EXPR_BUILTIN_SYMBOL: {
3026 const builtin_symbol_expression_t *builtin
3027 = &expression->builtin_symbol;
3028 return get_builtin_symbol_type(builtin->symbol);
3030 case EXPR_ARRAY_ACCESS: {
3031 const array_access_expression_t *array_access
3032 = &expression->array_access;
3033 const expression_t *array_ref = array_access->array_ref;
3034 type_t *type_left = skip_typeref(array_ref->base.datatype);
3035 assert(is_type_pointer(type_left));
3036 pointer_type_t *pointer_type = &type_left->pointer;
3037 return pointer_type->points_to;
3044 return expression->base.datatype;
3047 static expression_t *parse_reference(void)
3049 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3051 reference_expression_t *ref = &expression->reference;
3052 ref->symbol = token.v.symbol;
3054 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3056 source_position_t source_position = token.source_position;
3059 if(declaration == NULL) {
3060 if (! strict_mode && token.type == '(') {
3061 /* an implicitly defined function */
3062 warningf(HERE, "implicit declaration of function '%s'\n", ref->symbol->string);
3064 declaration = create_implicit_function(ref->symbol,
3067 errorf(HERE, "unknown symbol '%s' found.\n", ref->symbol->string);
3072 type_t *type = declaration->type;
3073 /* we always do the auto-type conversions; the & and sizeof parser contains
3074 * code to revert this! */
3075 type = automatic_type_conversion(type);
3077 ref->declaration = declaration;
3078 ref->expression.datatype = type;
3083 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3087 /* TODO check if explicit cast is allowed and issue warnings/errors */
3090 static expression_t *parse_cast(void)
3092 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3094 cast->base.source_position = token.source_position;
3096 type_t *type = parse_typename();
3099 expression_t *value = parse_sub_expression(20);
3101 check_cast_allowed(value, type);
3103 cast->base.datatype = type;
3104 cast->unary.value = value;
3109 static expression_t *parse_statement_expression(void)
3111 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3113 statement_t *statement = parse_compound_statement();
3114 expression->statement.statement = statement;
3115 if(statement == NULL) {
3120 assert(statement->kind == STATEMENT_COMPOUND);
3121 compound_statement_t *compound_statement = &statement->compound;
3123 /* find last statement and use it's type */
3124 const statement_t *last_statement = NULL;
3125 const statement_t *iter = compound_statement->statements;
3126 for( ; iter != NULL; iter = iter->base.next) {
3127 last_statement = iter;
3130 if(last_statement->kind == STATEMENT_EXPRESSION) {
3131 const expression_statement_t *expression_statement
3132 = &last_statement->expression;
3133 expression->base.datatype
3134 = expression_statement->expression->base.datatype;
3136 expression->base.datatype = type_void;
3144 static expression_t *parse_brace_expression(void)
3148 switch(token.type) {
3150 /* gcc extension: a statement expression */
3151 return parse_statement_expression();
3155 return parse_cast();
3157 if(is_typedef_symbol(token.v.symbol)) {
3158 return parse_cast();
3162 expression_t *result = parse_expression();
3168 static expression_t *parse_function_keyword(void)
3173 if (current_function == NULL) {
3174 errorf(HERE, "'__func__' used outside of a function");
3177 string_literal_expression_t *expression
3178 = allocate_ast_zero(sizeof(expression[0]));
3180 expression->expression.kind = EXPR_FUNCTION;
3181 expression->expression.datatype = type_string;
3182 expression->value = current_function->symbol->string;
3184 return (expression_t*) expression;
3187 static expression_t *parse_pretty_function_keyword(void)
3189 eat(T___PRETTY_FUNCTION__);
3192 if (current_function == NULL) {
3193 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3196 string_literal_expression_t *expression
3197 = allocate_ast_zero(sizeof(expression[0]));
3199 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3200 expression->expression.datatype = type_string;
3201 expression->value = current_function->symbol->string;
3203 return (expression_t*) expression;
3206 static designator_t *parse_designator(void)
3208 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3210 if(token.type != T_IDENTIFIER) {
3211 parse_error_expected("while parsing member designator",
3216 result->symbol = token.v.symbol;
3219 designator_t *last_designator = result;
3221 if(token.type == '.') {
3223 if(token.type != T_IDENTIFIER) {
3224 parse_error_expected("while parsing member designator",
3229 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3230 designator->symbol = token.v.symbol;
3233 last_designator->next = designator;
3234 last_designator = designator;
3237 if(token.type == '[') {
3239 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3240 designator->array_access = parse_expression();
3241 if(designator->array_access == NULL) {
3247 last_designator->next = designator;
3248 last_designator = designator;
3257 static expression_t *parse_offsetof(void)
3259 eat(T___builtin_offsetof);
3261 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3262 expression->base.datatype = type_size_t;
3265 expression->offsetofe.type = parse_typename();
3267 expression->offsetofe.designator = parse_designator();
3273 static expression_t *parse_va_start(void)
3275 eat(T___builtin_va_start);
3277 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3280 expression->va_starte.ap = parse_assignment_expression();
3282 expression_t *const expr = parse_assignment_expression();
3283 if (expr->kind == EXPR_REFERENCE) {
3284 declaration_t *const decl = expr->reference.declaration;
3285 if (decl->parent_context == ¤t_function->context &&
3286 decl->next == NULL) {
3287 expression->va_starte.parameter = decl;
3292 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3294 return create_invalid_expression();
3297 static expression_t *parse_va_arg(void)
3299 eat(T___builtin_va_arg);
3301 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3304 expression->va_arge.ap = parse_assignment_expression();
3306 expression->base.datatype = parse_typename();
3312 static expression_t *parse_builtin_symbol(void)
3314 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3316 symbol_t *symbol = token.v.symbol;
3318 expression->builtin_symbol.symbol = symbol;
3321 type_t *type = get_builtin_symbol_type(symbol);
3322 type = automatic_type_conversion(type);
3324 expression->base.datatype = type;
3328 static expression_t *parse_builtin_constant(void)
3330 eat(T___builtin_constant_p);
3332 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3335 expression->builtin_constant.value = parse_assignment_expression();
3337 expression->base.datatype = type_int;
3342 static expression_t *parse_builtin_prefetch(void)
3344 eat(T___builtin_prefetch);
3346 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3349 expression->builtin_prefetch.adr = parse_assignment_expression();
3350 if (token.type == ',') {
3352 expression->builtin_prefetch.rw = parse_assignment_expression();
3354 if (token.type == ',') {
3356 expression->builtin_prefetch.locality = parse_assignment_expression();
3359 expression->base.datatype = type_void;
3364 static expression_t *parse_compare_builtin(void)
3366 expression_t *expression;
3368 switch(token.type) {
3369 case T___builtin_isgreater:
3370 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3372 case T___builtin_isgreaterequal:
3373 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3375 case T___builtin_isless:
3376 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3378 case T___builtin_islessequal:
3379 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3381 case T___builtin_islessgreater:
3382 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3384 case T___builtin_isunordered:
3385 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3388 panic("invalid compare builtin found");
3394 expression->binary.left = parse_assignment_expression();
3396 expression->binary.right = parse_assignment_expression();
3399 type_t *orig_type_left = expression->binary.left->base.datatype;
3400 type_t *orig_type_right = expression->binary.right->base.datatype;
3401 if(orig_type_left == NULL || orig_type_right == NULL)
3404 type_t *type_left = skip_typeref(orig_type_left);
3405 type_t *type_right = skip_typeref(orig_type_right);
3406 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3407 type_error_incompatible("invalid operands in comparison",
3408 token.source_position, type_left, type_right);
3410 semantic_comparison(&expression->binary);
3416 static expression_t *parse_builtin_expect(void)
3418 eat(T___builtin_expect);
3420 expression_t *expression
3421 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3424 expression->binary.left = parse_assignment_expression();
3426 expression->binary.right = parse_constant_expression();
3429 expression->base.datatype = expression->binary.left->base.datatype;
3434 static expression_t *parse_assume(void) {
3437 expression_t *expression
3438 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3441 expression->unary.value = parse_assignment_expression();
3444 expression->base.datatype = type_void;
3448 static expression_t *parse_alignof(void) {
3451 expression_t *expression
3452 = allocate_expression_zero(EXPR_ALIGNOF);
3455 expression->alignofe.type = parse_typename();
3458 expression->base.datatype = type_size_t;
3462 static expression_t *parse_primary_expression(void)
3464 switch(token.type) {
3466 return parse_int_const();
3467 case T_FLOATINGPOINT:
3468 return parse_float_const();
3469 case T_STRING_LITERAL:
3470 return parse_string_const();
3471 case T_WIDE_STRING_LITERAL:
3472 return parse_wide_string_const();
3474 return parse_reference();
3475 case T___FUNCTION__:
3477 return parse_function_keyword();
3478 case T___PRETTY_FUNCTION__:
3479 return parse_pretty_function_keyword();
3480 case T___builtin_offsetof:
3481 return parse_offsetof();
3482 case T___builtin_va_start:
3483 return parse_va_start();
3484 case T___builtin_va_arg:
3485 return parse_va_arg();
3486 case T___builtin_expect:
3487 return parse_builtin_expect();
3488 case T___builtin_nanf:
3489 case T___builtin_alloca:
3490 case T___builtin_va_end:
3491 return parse_builtin_symbol();
3492 case T___builtin_isgreater:
3493 case T___builtin_isgreaterequal:
3494 case T___builtin_isless:
3495 case T___builtin_islessequal:
3496 case T___builtin_islessgreater:
3497 case T___builtin_isunordered:
3498 return parse_compare_builtin();
3499 case T___builtin_constant_p:
3500 return parse_builtin_constant();
3501 case T___builtin_prefetch:
3502 return parse_builtin_prefetch();
3504 return parse_alignof();
3506 return parse_assume();
3509 return parse_brace_expression();
3512 errorf(HERE, "unexpected token '%K'", &token);
3515 return create_invalid_expression();
3518 static expression_t *parse_array_expression(unsigned precedence,
3525 expression_t *inside = parse_expression();
3527 array_access_expression_t *array_access
3528 = allocate_ast_zero(sizeof(array_access[0]));
3530 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3532 type_t *type_left = left->base.datatype;
3533 type_t *type_inside = inside->base.datatype;
3534 type_t *return_type = NULL;
3536 if(type_left != NULL && type_inside != NULL) {
3537 type_left = skip_typeref(type_left);
3538 type_inside = skip_typeref(type_inside);
3540 if(is_type_pointer(type_left)) {
3541 pointer_type_t *pointer = &type_left->pointer;
3542 return_type = pointer->points_to;
3543 array_access->array_ref = left;
3544 array_access->index = inside;
3545 } else if(is_type_pointer(type_inside)) {
3546 pointer_type_t *pointer = &type_inside->pointer;
3547 return_type = pointer->points_to;
3548 array_access->array_ref = inside;
3549 array_access->index = left;
3550 array_access->flipped = true;
3552 errorf(HERE, "array access on object with non-pointer types '%T', '%T'", type_left, type_inside);
3555 array_access->array_ref = left;
3556 array_access->index = inside;
3559 if(token.type != ']') {
3560 parse_error_expected("Problem while parsing array access", ']', 0);
3561 return (expression_t*) array_access;
3565 return_type = automatic_type_conversion(return_type);
3566 array_access->expression.datatype = return_type;
3568 return (expression_t*) array_access;
3571 static expression_t *parse_sizeof(unsigned precedence)
3575 sizeof_expression_t *sizeof_expression
3576 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3577 sizeof_expression->expression.kind = EXPR_SIZEOF;
3578 sizeof_expression->expression.datatype = type_size_t;
3580 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3582 sizeof_expression->type = parse_typename();
3585 expression_t *expression = parse_sub_expression(precedence);
3586 expression->base.datatype = revert_automatic_type_conversion(expression);
3588 sizeof_expression->type = expression->base.datatype;
3589 sizeof_expression->size_expression = expression;
3592 return (expression_t*) sizeof_expression;
3595 static expression_t *parse_select_expression(unsigned precedence,
3596 expression_t *compound)
3599 assert(token.type == '.' || token.type == T_MINUSGREATER);
3601 bool is_pointer = (token.type == T_MINUSGREATER);
3604 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3605 select->select.compound = compound;
3607 if(token.type != T_IDENTIFIER) {
3608 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3611 symbol_t *symbol = token.v.symbol;
3612 select->select.symbol = symbol;
3615 type_t *orig_type = compound->base.datatype;
3616 if(orig_type == NULL)
3617 return create_invalid_expression();
3619 type_t *type = skip_typeref(orig_type);
3621 type_t *type_left = type;
3623 if(type->kind != TYPE_POINTER) {
3624 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3625 return create_invalid_expression();
3627 pointer_type_t *pointer_type = &type->pointer;
3628 type_left = pointer_type->points_to;
3630 type_left = skip_typeref(type_left);
3632 if(type_left->kind != TYPE_COMPOUND_STRUCT
3633 && type_left->kind != TYPE_COMPOUND_UNION) {
3634 errorf(HERE, "request for member '%s' in something not a struct or union, but '%T'", symbol->string, type_left);
3635 return create_invalid_expression();
3638 compound_type_t *compound_type = &type_left->compound;
3639 declaration_t *declaration = compound_type->declaration;
3641 if(!declaration->init.is_defined) {
3642 errorf(HERE, "request for member '%s' of incomplete type '%T'", symbol->string, type_left);
3643 return create_invalid_expression();
3646 declaration_t *iter = declaration->context.declarations;
3647 for( ; iter != NULL; iter = iter->next) {
3648 if(iter->symbol == symbol) {
3653 errorf(HERE, "'%T' has no member names '%s'", type_left, symbol->string);
3654 return create_invalid_expression();
3657 /* we always do the auto-type conversions; the & and sizeof parser contains
3658 * code to revert this! */
3659 type_t *expression_type = automatic_type_conversion(iter->type);
3661 select->select.compound_entry = iter;
3662 select->base.datatype = expression_type;
3667 * Parse a call expression, ie. expression '( ... )'.
3669 * @param expression the function address
3671 static expression_t *parse_call_expression(unsigned precedence,
3672 expression_t *expression)
3675 expression_t *result = allocate_expression_zero(EXPR_CALL);
3677 call_expression_t *call = &result->call;
3678 call->function = expression;
3680 function_type_t *function_type = NULL;
3681 type_t *orig_type = expression->base.datatype;
3682 if(orig_type != NULL) {
3683 type_t *type = skip_typeref(orig_type);
3685 if(is_type_pointer(type)) {
3686 pointer_type_t *pointer_type = &type->pointer;
3688 type = skip_typeref(pointer_type->points_to);
3690 if (is_type_function(type)) {
3691 function_type = &type->function;
3692 call->expression.datatype = function_type->return_type;
3695 if(function_type == NULL) {
3696 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3698 function_type = NULL;
3699 call->expression.datatype = NULL;
3703 /* parse arguments */
3706 if(token.type != ')') {
3707 call_argument_t *last_argument = NULL;
3710 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3712 argument->expression = parse_assignment_expression();
3713 if(last_argument == NULL) {
3714 call->arguments = argument;
3716 last_argument->next = argument;
3718 last_argument = argument;
3720 if(token.type != ',')
3727 if(function_type != NULL) {
3728 function_parameter_t *parameter = function_type->parameters;
3729 call_argument_t *argument = call->arguments;
3730 for( ; parameter != NULL && argument != NULL;
3731 parameter = parameter->next, argument = argument->next) {
3732 type_t *expected_type = parameter->type;
3733 /* TODO report context in error messages */
3734 argument->expression = create_implicit_cast(argument->expression,
3737 /* too few parameters */
3738 if(parameter != NULL) {
3739 errorf(HERE, "too few arguments to function '%E'", expression);
3740 } else if(argument != NULL) {
3741 /* too many parameters */
3742 if(!function_type->variadic
3743 && !function_type->unspecified_parameters) {
3744 errorf(HERE, "too many arguments to function '%E'", expression);
3746 /* do default promotion */
3747 for( ; argument != NULL; argument = argument->next) {
3748 type_t *type = argument->expression->base.datatype;
3753 type = skip_typeref(type);
3754 if(is_type_integer(type)) {
3755 type = promote_integer(type);
3756 } else if(type == type_float) {
3760 argument->expression
3761 = create_implicit_cast(argument->expression, type);
3764 check_format(&result->call);
3767 check_format(&result->call);
3774 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3776 static bool same_compound_type(const type_t *type1, const type_t *type2)
3778 if(!is_type_compound(type1))
3780 if(type1->kind != type2->kind)
3783 const compound_type_t *compound1 = &type1->compound;
3784 const compound_type_t *compound2 = &type2->compound;
3786 return compound1->declaration == compound2->declaration;
3790 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3792 * @param expression the conditional expression
3794 static expression_t *parse_conditional_expression(unsigned precedence,
3795 expression_t *expression)
3799 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3801 conditional_expression_t *conditional = &result->conditional;
3802 conditional->condition = expression;
3805 type_t *condition_type_orig = expression->base.datatype;
3806 if(condition_type_orig != NULL) {
3807 type_t *condition_type = skip_typeref(condition_type_orig);
3808 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3809 type_error("expected a scalar type in conditional condition",
3810 expression->base.source_position, condition_type_orig);
3814 expression_t *true_expression = parse_expression();
3816 expression_t *false_expression = parse_sub_expression(precedence);
3818 conditional->true_expression = true_expression;
3819 conditional->false_expression = false_expression;
3821 type_t *orig_true_type = true_expression->base.datatype;
3822 type_t *orig_false_type = false_expression->base.datatype;
3823 if(orig_true_type == NULL || orig_false_type == NULL)
3826 type_t *true_type = skip_typeref(orig_true_type);
3827 type_t *false_type = skip_typeref(orig_false_type);
3830 type_t *result_type = NULL;
3831 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3832 result_type = semantic_arithmetic(true_type, false_type);
3834 true_expression = create_implicit_cast(true_expression, result_type);
3835 false_expression = create_implicit_cast(false_expression, result_type);
3837 conditional->true_expression = true_expression;
3838 conditional->false_expression = false_expression;
3839 conditional->expression.datatype = result_type;
3840 } else if (same_compound_type(true_type, false_type)
3841 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3842 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3843 /* just take 1 of the 2 types */
3844 result_type = true_type;
3845 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3846 && pointers_compatible(true_type, false_type)) {
3848 result_type = true_type;
3851 type_error_incompatible("while parsing conditional",
3852 expression->base.source_position, true_type,
3856 conditional->expression.datatype = result_type;
3861 * Parse an extension expression.
3863 static expression_t *parse_extension(unsigned precedence)
3865 eat(T___extension__);
3867 /* TODO enable extensions */
3868 expression_t *expression = parse_sub_expression(precedence);
3869 /* TODO disable extensions */
3873 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3875 eat(T___builtin_classify_type);
3877 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3878 result->base.datatype = type_int;
3881 expression_t *expression = parse_sub_expression(precedence);
3883 result->classify_type.type_expression = expression;
3888 static void semantic_incdec(unary_expression_t *expression)
3890 type_t *orig_type = expression->value->base.datatype;
3891 if(orig_type == NULL)
3894 type_t *type = skip_typeref(orig_type);
3895 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3896 /* TODO: improve error message */
3897 errorf(HERE, "operation needs an arithmetic or pointer type");
3901 expression->expression.datatype = orig_type;
3904 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3906 type_t *orig_type = expression->value->base.datatype;
3907 if(orig_type == NULL)
3910 type_t *type = skip_typeref(orig_type);
3911 if(!is_type_arithmetic(type)) {
3912 /* TODO: improve error message */
3913 errorf(HERE, "operation needs an arithmetic type");
3917 expression->expression.datatype = orig_type;
3920 static void semantic_unexpr_scalar(unary_expression_t *expression)
3922 type_t *orig_type = expression->value->base.datatype;
3923 if(orig_type == NULL)
3926 type_t *type = skip_typeref(orig_type);
3927 if (!is_type_scalar(type)) {
3928 errorf(HERE, "operand of ! must be of scalar type");
3932 expression->expression.datatype = orig_type;
3935 static void semantic_unexpr_integer(unary_expression_t *expression)
3937 type_t *orig_type = expression->value->base.datatype;
3938 if(orig_type == NULL)
3941 type_t *type = skip_typeref(orig_type);
3942 if (!is_type_integer(type)) {
3943 errorf(HERE, "operand of ~ must be of integer type");
3947 expression->expression.datatype = orig_type;
3950 static void semantic_dereference(unary_expression_t *expression)
3952 type_t *orig_type = expression->value->base.datatype;
3953 if(orig_type == NULL)
3956 type_t *type = skip_typeref(orig_type);
3957 if(!is_type_pointer(type)) {
3958 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
3962 pointer_type_t *pointer_type = &type->pointer;
3963 type_t *result_type = pointer_type->points_to;
3965 result_type = automatic_type_conversion(result_type);
3966 expression->expression.datatype = result_type;
3969 static void semantic_take_addr(unary_expression_t *expression)
3971 expression_t *value = expression->value;
3972 value->base.datatype = revert_automatic_type_conversion(value);
3974 type_t *orig_type = value->base.datatype;
3975 if(orig_type == NULL)
3978 if(value->kind == EXPR_REFERENCE) {
3979 reference_expression_t *reference = (reference_expression_t*) value;
3980 declaration_t *declaration = reference->declaration;
3981 if(declaration != NULL) {
3982 declaration->address_taken = 1;
3986 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3989 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3990 static expression_t *parse_##unexpression_type(unsigned precedence) \
3994 expression_t *unary_expression \
3995 = allocate_expression_zero(unexpression_type); \
3996 unary_expression->unary.value = parse_sub_expression(precedence); \
3998 sfunc(&unary_expression->unary); \
4000 return unary_expression; \
4003 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4004 semantic_unexpr_arithmetic)
4005 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4006 semantic_unexpr_arithmetic)
4007 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4008 semantic_unexpr_scalar)
4009 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4010 semantic_dereference)
4011 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4013 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4014 semantic_unexpr_integer)
4015 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4017 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4020 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4022 static expression_t *parse_##unexpression_type(unsigned precedence, \
4023 expression_t *left) \
4025 (void) precedence; \
4028 expression_t *unary_expression \
4029 = allocate_expression_zero(unexpression_type); \
4030 unary_expression->unary.value = left; \
4032 sfunc(&unary_expression->unary); \
4034 return unary_expression; \
4037 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4038 EXPR_UNARY_POSTFIX_INCREMENT,
4040 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4041 EXPR_UNARY_POSTFIX_DECREMENT,
4044 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4046 /* TODO: handle complex + imaginary types */
4048 /* § 6.3.1.8 Usual arithmetic conversions */
4049 if(type_left == type_long_double || type_right == type_long_double) {
4050 return type_long_double;
4051 } else if(type_left == type_double || type_right == type_double) {
4053 } else if(type_left == type_float || type_right == type_float) {
4057 type_right = promote_integer(type_right);
4058 type_left = promote_integer(type_left);
4060 if(type_left == type_right)
4063 bool signed_left = is_type_signed(type_left);
4064 bool signed_right = is_type_signed(type_right);
4065 int rank_left = get_rank(type_left);
4066 int rank_right = get_rank(type_right);
4067 if(rank_left < rank_right) {
4068 if(signed_left == signed_right || !signed_right) {
4074 if(signed_left == signed_right || !signed_left) {
4083 * Check the semantic restrictions for a binary expression.
4085 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4087 expression_t *left = expression->left;
4088 expression_t *right = expression->right;
4089 type_t *orig_type_left = left->base.datatype;
4090 type_t *orig_type_right = right->base.datatype;
4092 if(orig_type_left == NULL || orig_type_right == NULL)
4095 type_t *type_left = skip_typeref(orig_type_left);
4096 type_t *type_right = skip_typeref(orig_type_right);
4098 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4099 /* TODO: improve error message */
4100 errorf(HERE, "operation needs arithmetic types");
4104 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4105 expression->left = create_implicit_cast(left, arithmetic_type);
4106 expression->right = create_implicit_cast(right, arithmetic_type);
4107 expression->expression.datatype = arithmetic_type;
4110 static void semantic_shift_op(binary_expression_t *expression)
4112 expression_t *left = expression->left;
4113 expression_t *right = expression->right;
4114 type_t *orig_type_left = left->base.datatype;
4115 type_t *orig_type_right = right->base.datatype;
4117 if(orig_type_left == NULL || orig_type_right == NULL)
4120 type_t *type_left = skip_typeref(orig_type_left);
4121 type_t *type_right = skip_typeref(orig_type_right);
4123 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4124 /* TODO: improve error message */
4125 errorf(HERE, "operation needs integer types");
4129 type_left = promote_integer(type_left);
4130 type_right = promote_integer(type_right);
4132 expression->left = create_implicit_cast(left, type_left);
4133 expression->right = create_implicit_cast(right, type_right);
4134 expression->expression.datatype = type_left;
4137 static void semantic_add(binary_expression_t *expression)
4139 expression_t *left = expression->left;
4140 expression_t *right = expression->right;
4141 type_t *orig_type_left = left->base.datatype;
4142 type_t *orig_type_right = right->base.datatype;
4144 if(orig_type_left == NULL || orig_type_right == NULL)
4147 type_t *type_left = skip_typeref(orig_type_left);
4148 type_t *type_right = skip_typeref(orig_type_right);
4151 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4152 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4153 expression->left = create_implicit_cast(left, arithmetic_type);
4154 expression->right = create_implicit_cast(right, arithmetic_type);
4155 expression->expression.datatype = arithmetic_type;
4157 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4158 expression->expression.datatype = type_left;
4159 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4160 expression->expression.datatype = type_right;
4162 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4166 static void semantic_sub(binary_expression_t *expression)
4168 expression_t *left = expression->left;
4169 expression_t *right = expression->right;
4170 type_t *orig_type_left = left->base.datatype;
4171 type_t *orig_type_right = right->base.datatype;
4173 if(orig_type_left == NULL || orig_type_right == NULL)
4176 type_t *type_left = skip_typeref(orig_type_left);
4177 type_t *type_right = skip_typeref(orig_type_right);
4180 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4181 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4182 expression->left = create_implicit_cast(left, arithmetic_type);
4183 expression->right = create_implicit_cast(right, arithmetic_type);
4184 expression->expression.datatype = arithmetic_type;
4186 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4187 expression->expression.datatype = type_left;
4188 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4189 if(!pointers_compatible(type_left, type_right)) {
4190 errorf(HERE, "pointers to incompatible objects to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4192 expression->expression.datatype = type_ptrdiff_t;
4195 errorf(HERE, "invalid operands to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4199 static void semantic_comparison(binary_expression_t *expression)
4201 expression_t *left = expression->left;
4202 expression_t *right = expression->right;
4203 type_t *orig_type_left = left->base.datatype;
4204 type_t *orig_type_right = right->base.datatype;
4206 if(orig_type_left == NULL || orig_type_right == NULL)
4209 type_t *type_left = skip_typeref(orig_type_left);
4210 type_t *type_right = skip_typeref(orig_type_right);
4212 /* TODO non-arithmetic types */
4213 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4214 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4215 expression->left = create_implicit_cast(left, arithmetic_type);
4216 expression->right = create_implicit_cast(right, arithmetic_type);
4217 expression->expression.datatype = arithmetic_type;
4218 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4219 /* TODO check compatibility */
4220 } else if (is_type_pointer(type_left)) {
4221 expression->right = create_implicit_cast(right, type_left);
4222 } else if (is_type_pointer(type_right)) {
4223 expression->left = create_implicit_cast(left, type_right);
4225 type_error_incompatible("invalid operands in comparison",
4226 token.source_position, type_left, type_right);
4228 expression->expression.datatype = type_int;
4231 static void semantic_arithmetic_assign(binary_expression_t *expression)
4233 expression_t *left = expression->left;
4234 expression_t *right = expression->right;
4235 type_t *orig_type_left = left->base.datatype;
4236 type_t *orig_type_right = right->base.datatype;
4238 if(orig_type_left == NULL || orig_type_right == NULL)
4241 type_t *type_left = skip_typeref(orig_type_left);
4242 type_t *type_right = skip_typeref(orig_type_right);
4244 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4245 /* TODO: improve error message */
4246 errorf(HERE, "operation needs arithmetic types");
4250 /* combined instructions are tricky. We can't create an implicit cast on
4251 * the left side, because we need the uncasted form for the store.
4252 * The ast2firm pass has to know that left_type must be right_type
4253 * for the arithmetic operation and create a cast by itself */
4254 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4255 expression->right = create_implicit_cast(right, arithmetic_type);
4256 expression->expression.datatype = type_left;
4259 static void semantic_arithmetic_addsubb_assign(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 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4273 /* combined instructions are tricky. We can't create an implicit cast on
4274 * the left side, because we need the uncasted form for the store.
4275 * The ast2firm pass has to know that left_type must be right_type
4276 * for the arithmetic operation and create a cast by itself */
4277 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4278 expression->right = create_implicit_cast(right, arithmetic_type);
4279 expression->expression.datatype = type_left;
4280 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4281 expression->expression.datatype = type_left;
4283 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4289 * Check the semantic restrictions of a logical expression.
4291 static void semantic_logical_op(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_scalar(type_left) || !is_type_scalar(type_right)) {
4305 /* TODO: improve error message */
4306 errorf(HERE, "operation needs scalar types");
4310 expression->expression.datatype = type_int;
4314 * Checks if a compound type has constant fields.
4316 static bool has_const_fields(const compound_type_t *type)
4318 const context_t *context = &type->declaration->context;
4319 const declaration_t *declaration = context->declarations;
4321 for (; declaration != NULL; declaration = declaration->next) {
4322 const type_t *decl_type = skip_typeref(declaration->type);
4323 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4331 * Check the semantic restrictions of a binary assign expression.
4333 static void semantic_binexpr_assign(binary_expression_t *expression)
4335 expression_t *left = expression->left;
4336 type_t *orig_type_left = left->base.datatype;
4338 if(orig_type_left == NULL)
4341 type_t *type_left = revert_automatic_type_conversion(left);
4342 type_left = skip_typeref(orig_type_left);
4344 /* must be a modifiable lvalue */
4345 if (is_type_array(type_left)) {
4346 errorf(HERE, "cannot assign to arrays ('%E')", left);
4349 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4350 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left, orig_type_left);
4353 if(is_type_incomplete(type_left)) {
4354 errorf(HERE, "left-hand side of assignment '%E' has incomplete type '%T'", left, orig_type_left);
4357 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4358 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields", left, orig_type_left);
4362 semantic_assign(orig_type_left, &expression->right, "assignment");
4364 expression->expression.datatype = orig_type_left;
4367 static void semantic_comma(binary_expression_t *expression)
4369 expression->expression.datatype = expression->right->base.datatype;
4372 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4373 static expression_t *parse_##binexpression_type(unsigned precedence, \
4374 expression_t *left) \
4378 expression_t *right = parse_sub_expression(precedence + lr); \
4380 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4381 binexpr->binary.left = left; \
4382 binexpr->binary.right = right; \
4383 sfunc(&binexpr->binary); \
4388 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4389 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4390 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4391 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4392 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4393 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4394 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4395 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4396 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4398 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4399 semantic_comparison, 1)
4400 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4401 semantic_comparison, 1)
4402 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4403 semantic_comparison, 1)
4404 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4405 semantic_comparison, 1)
4407 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4408 semantic_binexpr_arithmetic, 1)
4409 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4410 semantic_binexpr_arithmetic, 1)
4411 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4412 semantic_binexpr_arithmetic, 1)
4413 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4414 semantic_logical_op, 1)
4415 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4416 semantic_logical_op, 1)
4417 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4418 semantic_shift_op, 1)
4419 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4420 semantic_shift_op, 1)
4421 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4422 semantic_arithmetic_addsubb_assign, 0)
4423 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4424 semantic_arithmetic_addsubb_assign, 0)
4425 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4426 semantic_arithmetic_assign, 0)
4427 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4428 semantic_arithmetic_assign, 0)
4429 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4430 semantic_arithmetic_assign, 0)
4431 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4432 semantic_arithmetic_assign, 0)
4433 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4434 semantic_arithmetic_assign, 0)
4435 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4436 semantic_arithmetic_assign, 0)
4437 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4438 semantic_arithmetic_assign, 0)
4439 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4440 semantic_arithmetic_assign, 0)
4442 static expression_t *parse_sub_expression(unsigned precedence)
4444 if(token.type < 0) {
4445 return expected_expression_error();
4448 expression_parser_function_t *parser
4449 = &expression_parsers[token.type];
4450 source_position_t source_position = token.source_position;
4453 if(parser->parser != NULL) {
4454 left = parser->parser(parser->precedence);
4456 left = parse_primary_expression();
4458 assert(left != NULL);
4459 left->base.source_position = source_position;
4462 if(token.type < 0) {
4463 return expected_expression_error();
4466 parser = &expression_parsers[token.type];
4467 if(parser->infix_parser == NULL)
4469 if(parser->infix_precedence < precedence)
4472 left = parser->infix_parser(parser->infix_precedence, left);
4474 assert(left != NULL);
4475 assert(left->kind != EXPR_UNKNOWN);
4476 left->base.source_position = source_position;
4483 * Parse an expression.
4485 static expression_t *parse_expression(void)
4487 return parse_sub_expression(1);
4491 * Register a parser for a prefix-like operator with given precedence.
4493 * @param parser the parser function
4494 * @param token_type the token type of the prefix token
4495 * @param precedence the precedence of the operator
4497 static void register_expression_parser(parse_expression_function parser,
4498 int token_type, unsigned precedence)
4500 expression_parser_function_t *entry = &expression_parsers[token_type];
4502 if(entry->parser != NULL) {
4503 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4504 panic("trying to register multiple expression parsers for a token");
4506 entry->parser = parser;
4507 entry->precedence = precedence;
4511 * Register a parser for an infix operator with given precedence.
4513 * @param parser the parser function
4514 * @param token_type the token type of the infix operator
4515 * @param precedence the precedence of the operator
4517 static void register_infix_parser(parse_expression_infix_function parser,
4518 int token_type, unsigned precedence)
4520 expression_parser_function_t *entry = &expression_parsers[token_type];
4522 if(entry->infix_parser != NULL) {
4523 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4524 panic("trying to register multiple infix expression parsers for a "
4527 entry->infix_parser = parser;
4528 entry->infix_precedence = precedence;
4532 * Initialize the expression parsers.
4534 static void init_expression_parsers(void)
4536 memset(&expression_parsers, 0, sizeof(expression_parsers));
4538 register_infix_parser(parse_array_expression, '[', 30);
4539 register_infix_parser(parse_call_expression, '(', 30);
4540 register_infix_parser(parse_select_expression, '.', 30);
4541 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4542 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4544 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4547 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4548 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4549 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4550 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4551 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4552 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4553 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4554 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4555 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4556 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4557 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4558 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4559 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4560 T_EXCLAMATIONMARKEQUAL, 13);
4561 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4562 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4563 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4564 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4565 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4566 register_infix_parser(parse_conditional_expression, '?', 7);
4567 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4568 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4569 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4570 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4571 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4572 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4573 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4574 T_LESSLESSEQUAL, 2);
4575 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4576 T_GREATERGREATEREQUAL, 2);
4577 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4579 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4581 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4584 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4586 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4587 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4588 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4589 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4590 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4591 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4592 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4594 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4596 register_expression_parser(parse_sizeof, T_sizeof, 25);
4597 register_expression_parser(parse_extension, T___extension__, 25);
4598 register_expression_parser(parse_builtin_classify_type,
4599 T___builtin_classify_type, 25);
4603 * Parse a asm statement constraints specification.
4605 static asm_constraint_t *parse_asm_constraints(void)
4607 asm_constraint_t *result = NULL;
4608 asm_constraint_t *last = NULL;
4610 while(token.type == T_STRING_LITERAL || token.type == '[') {
4611 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4612 memset(constraint, 0, sizeof(constraint[0]));
4614 if(token.type == '[') {
4616 if(token.type != T_IDENTIFIER) {
4617 parse_error_expected("while parsing asm constraint",
4621 constraint->symbol = token.v.symbol;
4626 constraint->constraints = parse_string_literals();
4628 constraint->expression = parse_expression();
4632 last->next = constraint;
4634 result = constraint;
4638 if(token.type != ',')
4647 * Parse a asm statement clobber specification.
4649 static asm_clobber_t *parse_asm_clobbers(void)
4651 asm_clobber_t *result = NULL;
4652 asm_clobber_t *last = NULL;
4654 while(token.type == T_STRING_LITERAL) {
4655 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4656 clobber->clobber = parse_string_literals();
4659 last->next = clobber;
4665 if(token.type != ',')
4674 * Parse an asm statement.
4676 static statement_t *parse_asm_statement(void)
4680 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4681 statement->base.source_position = token.source_position;
4683 asm_statement_t *asm_statement = &statement->asms;
4685 if(token.type == T_volatile) {
4687 asm_statement->is_volatile = true;
4691 asm_statement->asm_text = parse_string_literals();
4693 if(token.type != ':')
4697 asm_statement->inputs = parse_asm_constraints();
4698 if(token.type != ':')
4702 asm_statement->outputs = parse_asm_constraints();
4703 if(token.type != ':')
4707 asm_statement->clobbers = parse_asm_clobbers();
4716 * Parse a case statement.
4718 static statement_t *parse_case_statement(void)
4722 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4724 statement->base.source_position = token.source_position;
4725 statement->case_label.expression = parse_expression();
4728 statement->case_label.label_statement = parse_statement();
4734 * Parse a default statement.
4736 static statement_t *parse_default_statement(void)
4740 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4742 statement->base.source_position = token.source_position;
4745 statement->label.label_statement = parse_statement();
4751 * Return the declaration for a given label symbol or create a new one.
4753 static declaration_t *get_label(symbol_t *symbol)
4755 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4756 assert(current_function != NULL);
4757 /* if we found a label in the same function, then we already created the
4759 if(candidate != NULL
4760 && candidate->parent_context == ¤t_function->context) {
4764 /* otherwise we need to create a new one */
4765 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4766 declaration->namespc = NAMESPACE_LABEL;
4767 declaration->symbol = symbol;
4769 label_push(declaration);
4775 * Parse a label statement.
4777 static statement_t *parse_label_statement(void)
4779 assert(token.type == T_IDENTIFIER);
4780 symbol_t *symbol = token.v.symbol;
4783 declaration_t *label = get_label(symbol);
4785 /* if source position is already set then the label is defined twice,
4786 * otherwise it was just mentioned in a goto so far */
4787 if(label->source_position.input_name != NULL) {
4788 errorf(HERE, "duplicate label '%s'\n", symbol->string);
4789 errorf(label->source_position, "previous definition of '%s' was here\n", symbol->string);
4791 label->source_position = token.source_position;
4794 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4796 label_statement->statement.kind = STATEMENT_LABEL;
4797 label_statement->statement.source_position = token.source_position;
4798 label_statement->label = label;
4802 if(token.type == '}') {
4803 /* TODO only warn? */
4804 errorf(HERE, "label at end of compound statement");
4805 return (statement_t*) label_statement;
4807 label_statement->label_statement = parse_statement();
4810 return (statement_t*) label_statement;
4814 * Parse an if statement.
4816 static statement_t *parse_if(void)
4820 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4821 statement->statement.kind = STATEMENT_IF;
4822 statement->statement.source_position = token.source_position;
4825 statement->condition = parse_expression();
4828 statement->true_statement = parse_statement();
4829 if(token.type == T_else) {
4831 statement->false_statement = parse_statement();
4834 return (statement_t*) statement;
4838 * Parse a switch statement.
4840 static statement_t *parse_switch(void)
4844 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4845 statement->statement.kind = STATEMENT_SWITCH;
4846 statement->statement.source_position = token.source_position;
4849 statement->expression = parse_expression();
4851 statement->body = parse_statement();
4853 return (statement_t*) statement;
4857 * Parse a while statement.
4859 static statement_t *parse_while(void)
4863 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4864 statement->statement.kind = STATEMENT_WHILE;
4865 statement->statement.source_position = token.source_position;
4868 statement->condition = parse_expression();
4870 statement->body = parse_statement();
4872 return (statement_t*) statement;
4876 * Parse a do statement.
4878 static statement_t *parse_do(void)
4882 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4883 statement->statement.kind = STATEMENT_DO_WHILE;
4884 statement->statement.source_position = token.source_position;
4886 statement->body = parse_statement();
4889 statement->condition = parse_expression();
4893 return (statement_t*) statement;
4897 * Parse a for statement.
4899 static statement_t *parse_for(void)
4903 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4904 statement->statement.kind = STATEMENT_FOR;
4905 statement->statement.source_position = token.source_position;
4909 int top = environment_top();
4910 context_t *last_context = context;
4911 set_context(&statement->context);
4913 if(token.type != ';') {
4914 if(is_declaration_specifier(&token, false)) {
4915 parse_declaration(record_declaration);
4917 statement->initialisation = parse_expression();
4924 if(token.type != ';') {
4925 statement->condition = parse_expression();
4928 if(token.type != ')') {
4929 statement->step = parse_expression();
4932 statement->body = parse_statement();
4934 assert(context == &statement->context);
4935 set_context(last_context);
4936 environment_pop_to(top);
4938 return (statement_t*) statement;
4942 * Parse a goto statement.
4944 static statement_t *parse_goto(void)
4948 if(token.type != T_IDENTIFIER) {
4949 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4953 symbol_t *symbol = token.v.symbol;
4956 declaration_t *label = get_label(symbol);
4958 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4960 statement->statement.kind = STATEMENT_GOTO;
4961 statement->statement.source_position = token.source_position;
4963 statement->label = label;
4967 return (statement_t*) statement;
4971 * Parse a continue statement.
4973 static statement_t *parse_continue(void)
4978 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4979 statement->kind = STATEMENT_CONTINUE;
4980 statement->base.source_position = token.source_position;
4986 * Parse a break statement.
4988 static statement_t *parse_break(void)
4993 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4994 statement->kind = STATEMENT_BREAK;
4995 statement->base.source_position = token.source_position;
5001 * Parse a return statement.
5003 static statement_t *parse_return(void)
5007 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5009 statement->statement.kind = STATEMENT_RETURN;
5010 statement->statement.source_position = token.source_position;
5012 assert(is_type_function(current_function->type));
5013 function_type_t *function_type = ¤t_function->type->function;
5014 type_t *return_type = function_type->return_type;
5016 expression_t *return_value = NULL;
5017 if(token.type != ';') {
5018 return_value = parse_expression();
5022 if(return_type == NULL)
5023 return (statement_t*) statement;
5024 if(return_value != NULL && return_value->base.datatype == NULL)
5025 return (statement_t*) statement;
5027 return_type = skip_typeref(return_type);
5029 if(return_value != NULL) {
5030 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5032 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5033 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5034 warningf(HERE, "'return' with a value, in function returning void");
5035 return_value = NULL;
5037 if(return_type != NULL) {
5038 semantic_assign(return_type, &return_value, "'return'");
5042 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5043 warningf(HERE, "'return' without value, in function returning non-void");
5046 statement->return_value = return_value;
5048 return (statement_t*) statement;
5052 * Parse a declaration statement.
5054 static statement_t *parse_declaration_statement(void)
5056 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5058 statement->base.source_position = token.source_position;
5060 declaration_t *before = last_declaration;
5061 parse_declaration(record_declaration);
5063 if(before == NULL) {
5064 statement->declaration.declarations_begin = context->declarations;
5066 statement->declaration.declarations_begin = before->next;
5068 statement->declaration.declarations_end = last_declaration;
5074 * Parse an expression statement, ie. expr ';'.
5076 static statement_t *parse_expression_statement(void)
5078 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5080 statement->base.source_position = token.source_position;
5081 statement->expression.expression = parse_expression();
5089 * Parse a statement.
5091 static statement_t *parse_statement(void)
5093 statement_t *statement = NULL;
5095 /* declaration or statement */
5096 switch(token.type) {
5098 statement = parse_asm_statement();
5102 statement = parse_case_statement();
5106 statement = parse_default_statement();
5110 statement = parse_compound_statement();
5114 statement = parse_if();
5118 statement = parse_switch();
5122 statement = parse_while();
5126 statement = parse_do();
5130 statement = parse_for();
5134 statement = parse_goto();
5138 statement = parse_continue();
5142 statement = parse_break();
5146 statement = parse_return();
5155 if(look_ahead(1)->type == ':') {
5156 statement = parse_label_statement();
5160 if(is_typedef_symbol(token.v.symbol)) {
5161 statement = parse_declaration_statement();
5165 statement = parse_expression_statement();
5168 case T___extension__:
5169 /* this can be a prefix to a declaration or an expression statement */
5170 /* we simply eat it now and parse the rest with tail recursion */
5173 } while(token.type == T___extension__);
5174 statement = parse_statement();
5178 statement = parse_declaration_statement();
5182 statement = parse_expression_statement();
5186 assert(statement == NULL
5187 || statement->base.source_position.input_name != NULL);
5193 * Parse a compound statement.
5195 static statement_t *parse_compound_statement(void)
5197 compound_statement_t *compound_statement
5198 = allocate_ast_zero(sizeof(compound_statement[0]));
5199 compound_statement->statement.kind = STATEMENT_COMPOUND;
5200 compound_statement->statement.source_position = token.source_position;
5204 int top = environment_top();
5205 context_t *last_context = context;
5206 set_context(&compound_statement->context);
5208 statement_t *last_statement = NULL;
5210 while(token.type != '}' && token.type != T_EOF) {
5211 statement_t *statement = parse_statement();
5212 if(statement == NULL)
5215 if(last_statement != NULL) {
5216 last_statement->base.next = statement;
5218 compound_statement->statements = statement;
5221 while(statement->base.next != NULL)
5222 statement = statement->base.next;
5224 last_statement = statement;
5227 if(token.type == '}') {
5230 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5233 assert(context == &compound_statement->context);
5234 set_context(last_context);
5235 environment_pop_to(top);
5237 return (statement_t*) compound_statement;
5241 * Initialize builtin types.
5243 static void initialize_builtin_types(void)
5245 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5246 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5247 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5248 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5249 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5250 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5251 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5252 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5254 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5255 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5256 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5257 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5261 * Parse a translation unit.
5263 static translation_unit_t *parse_translation_unit(void)
5265 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5267 assert(global_context == NULL);
5268 global_context = &unit->context;
5270 assert(context == NULL);
5271 set_context(&unit->context);
5273 initialize_builtin_types();
5275 while(token.type != T_EOF) {
5276 parse_external_declaration();
5279 assert(context == &unit->context);
5281 last_declaration = NULL;
5283 assert(global_context == &unit->context);
5284 global_context = NULL;
5292 * @return the translation unit or NULL if errors occurred.
5294 translation_unit_t *parse(void)
5296 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5297 label_stack = NEW_ARR_F(stack_entry_t, 0);
5298 diagnostic_count = 0;
5302 type_set_output(stderr);
5303 ast_set_output(stderr);
5305 lookahead_bufpos = 0;
5306 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5309 translation_unit_t *unit = parse_translation_unit();
5311 DEL_ARR_F(environment_stack);
5312 DEL_ARR_F(label_stack);
5321 * Initialize the parser.
5323 void init_parser(void)
5325 init_expression_parsers();
5326 obstack_init(&temp_obst);
5328 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5329 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5333 * Terminate the parser.
5335 void exit_parser(void)
5337 obstack_free(&temp_obst, NULL);