7 #include "diagnostic.h"
8 #include "format_check.h"
14 #include "type_hash.h"
16 #include "lang_features.h"
17 #include "adt/bitfiddle.h"
18 #include "adt/error.h"
19 #include "adt/array.h"
21 //#define PRINT_TOKENS
22 //#define ABORT_ON_ERROR
23 #define MAX_LOOKAHEAD 2
26 declaration_t *old_declaration;
28 unsigned short namespc;
31 typedef struct declaration_specifiers_t declaration_specifiers_t;
32 struct declaration_specifiers_t {
33 source_position_t source_position;
34 unsigned char storage_class;
36 decl_modifiers_t decl_modifiers;
40 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
43 static token_t lookahead_buffer[MAX_LOOKAHEAD];
44 static int lookahead_bufpos;
45 static stack_entry_t *environment_stack = NULL;
46 static stack_entry_t *label_stack = NULL;
47 static context_t *global_context = NULL;
48 static context_t *context = NULL;
49 static declaration_t *last_declaration = NULL;
50 static declaration_t *current_function = NULL;
51 static switch_statement_t *current_switch = NULL;
52 static statement_t *current_loop = NULL;
53 static goto_statement_t *goto_first = NULL;
54 static goto_statement_t *goto_last = NULL;
55 static struct obstack temp_obst;
57 /** The current source position. */
58 #define HERE token.source_position
60 static type_t *type_valist;
62 static statement_t *parse_compound_statement(void);
63 static statement_t *parse_statement(void);
65 static expression_t *parse_sub_expression(unsigned precedence);
66 static expression_t *parse_expression(void);
67 static type_t *parse_typename(void);
69 static void parse_compound_type_entries(void);
70 static declaration_t *parse_declarator(
71 const declaration_specifiers_t *specifiers, bool may_be_abstract);
72 static declaration_t *record_declaration(declaration_t *declaration);
74 static void semantic_comparison(binary_expression_t *expression);
76 #define STORAGE_CLASSES \
83 #define TYPE_QUALIFIERS \
90 #ifdef PROVIDE_COMPLEX
91 #define COMPLEX_SPECIFIERS \
93 #define IMAGINARY_SPECIFIERS \
96 #define COMPLEX_SPECIFIERS
97 #define IMAGINARY_SPECIFIERS
100 #define TYPE_SPECIFIERS \
115 case T___builtin_va_list: \
119 #define DECLARATION_START \
124 #define TYPENAME_START \
129 * Allocate an AST node with given size and
130 * initialize all fields with zero.
132 static void *allocate_ast_zero(size_t size)
134 void *res = allocate_ast(size);
135 memset(res, 0, size);
139 static declaration_t *allocate_declaration_zero(void)
141 declaration_t *declaration = allocate_ast_zero(sizeof(*allocate_declaration_zero()));
142 declaration->type = type_error_type;
147 * Returns the size of a statement node.
149 * @param kind the statement kind
151 static size_t get_statement_struct_size(statement_kind_t kind)
153 static const size_t sizes[] = {
154 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
155 [STATEMENT_RETURN] = sizeof(return_statement_t),
156 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
157 [STATEMENT_IF] = sizeof(if_statement_t),
158 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
159 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
160 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
161 [STATEMENT_BREAK] = sizeof(statement_base_t),
162 [STATEMENT_GOTO] = sizeof(goto_statement_t),
163 [STATEMENT_LABEL] = sizeof(label_statement_t),
164 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
165 [STATEMENT_WHILE] = sizeof(while_statement_t),
166 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
167 [STATEMENT_FOR] = sizeof(for_statement_t),
168 [STATEMENT_ASM] = sizeof(asm_statement_t)
170 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
171 assert(sizes[kind] != 0);
176 * Allocate a statement node of given kind and initialize all
179 static statement_t *allocate_statement_zero(statement_kind_t kind)
181 size_t size = get_statement_struct_size(kind);
182 statement_t *res = allocate_ast_zero(size);
184 res->base.kind = kind;
189 * Returns the size of an expression node.
191 * @param kind the expression kind
193 static size_t get_expression_struct_size(expression_kind_t kind)
195 static const size_t sizes[] = {
196 [EXPR_INVALID] = sizeof(expression_base_t),
197 [EXPR_REFERENCE] = sizeof(reference_expression_t),
198 [EXPR_CONST] = sizeof(const_expression_t),
199 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
200 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
201 [EXPR_CALL] = sizeof(call_expression_t),
202 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
203 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
204 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
205 [EXPR_SELECT] = sizeof(select_expression_t),
206 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
207 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
208 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
209 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
210 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
211 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
212 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
213 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
214 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
215 [EXPR_VA_START] = sizeof(va_start_expression_t),
216 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
217 [EXPR_STATEMENT] = sizeof(statement_expression_t),
219 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
220 return sizes[EXPR_UNARY_FIRST];
222 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
223 return sizes[EXPR_BINARY_FIRST];
225 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
226 assert(sizes[kind] != 0);
231 * Allocate an expression node of given kind and initialize all
234 static expression_t *allocate_expression_zero(expression_kind_t kind)
236 size_t size = get_expression_struct_size(kind);
237 expression_t *res = allocate_ast_zero(size);
239 res->base.kind = kind;
240 res->base.datatype = type_error_type;
245 * Returns the size of a type node.
247 * @param kind the type kind
249 static size_t get_type_struct_size(type_kind_t kind)
251 static const size_t sizes[] = {
252 [TYPE_ATOMIC] = sizeof(atomic_type_t),
253 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
254 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
255 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
256 [TYPE_ENUM] = sizeof(enum_type_t),
257 [TYPE_FUNCTION] = sizeof(function_type_t),
258 [TYPE_POINTER] = sizeof(pointer_type_t),
259 [TYPE_ARRAY] = sizeof(array_type_t),
260 [TYPE_BUILTIN] = sizeof(builtin_type_t),
261 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
262 [TYPE_TYPEOF] = sizeof(typeof_type_t),
264 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
265 assert(kind <= TYPE_TYPEOF);
266 assert(sizes[kind] != 0);
271 * Allocate a type node of given kind and initialize all
274 static type_t *allocate_type_zero(type_kind_t kind)
276 size_t size = get_type_struct_size(kind);
277 type_t *res = obstack_alloc(type_obst, size);
278 memset(res, 0, size);
280 res->base.kind = kind;
285 * Returns the size of an initializer node.
287 * @param kind the initializer kind
289 static size_t get_initializer_size(initializer_kind_t kind)
291 static const size_t sizes[] = {
292 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
293 [INITIALIZER_STRING] = sizeof(initializer_string_t),
294 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
295 [INITIALIZER_LIST] = sizeof(initializer_list_t)
297 assert(kind < sizeof(sizes) / sizeof(*sizes));
298 assert(sizes[kind] != 0);
303 * Allocate an initializer node of given kind and initialize all
306 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
308 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
315 * Free a type from the type obstack.
317 static void free_type(void *type)
319 obstack_free(type_obst, type);
323 * Returns the index of the top element of the environment stack.
325 static size_t environment_top(void)
327 return ARR_LEN(environment_stack);
331 * Returns the index of the top element of the label stack.
333 static size_t label_top(void)
335 return ARR_LEN(label_stack);
340 * Return the next token.
342 static inline void next_token(void)
344 token = lookahead_buffer[lookahead_bufpos];
345 lookahead_buffer[lookahead_bufpos] = lexer_token;
348 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
351 print_token(stderr, &token);
352 fprintf(stderr, "\n");
357 * Return the next token with a given lookahead.
359 static inline const token_t *look_ahead(int num)
361 assert(num > 0 && num <= MAX_LOOKAHEAD);
362 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
363 return &lookahead_buffer[pos];
366 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
369 * Report a parse error because an expected token was not found.
371 static void parse_error_expected(const char *message, ...)
373 if(message != NULL) {
374 errorf(HERE, "%s", message);
377 va_start(ap, message);
378 errorf(HERE, "got '%K', expected %#k", &token, &ap, ", ");
383 * Report a type error.
385 static void type_error(const char *msg, const source_position_t source_position,
388 errorf(source_position, "%s, but found type '%T'", msg, type);
392 * Report an incompatible type.
394 static void type_error_incompatible(const char *msg,
395 const source_position_t source_position, type_t *type1, type_t *type2)
397 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
401 * Eat an complete block, ie. '{ ... }'.
403 static void eat_block(void)
405 if(token.type == '{')
408 while(token.type != '}') {
409 if(token.type == T_EOF)
411 if(token.type == '{') {
421 * Eat a statement until an ';' token.
423 static void eat_statement(void)
425 while(token.type != ';') {
426 if(token.type == T_EOF)
428 if(token.type == '}')
430 if(token.type == '{') {
440 * Eat a parenthesed term, ie. '( ... )'.
442 static void eat_paren(void)
444 if(token.type == '(')
447 while(token.type != ')') {
448 if(token.type == T_EOF)
450 if(token.type == ')' || token.type == ';' || token.type == '}') {
453 if(token.type == '(') {
457 if(token.type == '{') {
466 #define expect(expected) \
467 if(UNLIKELY(token.type != (expected))) { \
468 parse_error_expected(NULL, (expected), 0); \
474 #define expect_block(expected) \
475 if(UNLIKELY(token.type != (expected))) { \
476 parse_error_expected(NULL, (expected), 0); \
482 #define expect_void(expected) \
483 if(UNLIKELY(token.type != (expected))) { \
484 parse_error_expected(NULL, (expected), 0); \
490 static void set_context(context_t *new_context)
492 context = new_context;
494 last_declaration = new_context->declarations;
495 if(last_declaration != NULL) {
496 while(last_declaration->next != NULL) {
497 last_declaration = last_declaration->next;
503 * Search a symbol in a given namespace and returns its declaration or
504 * NULL if this symbol was not found.
506 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
508 declaration_t *declaration = symbol->declaration;
509 for( ; declaration != NULL; declaration = declaration->symbol_next) {
510 if(declaration->namespc == namespc)
518 * pushs an environment_entry on the environment stack and links the
519 * corresponding symbol to the new entry
521 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
523 symbol_t *symbol = declaration->symbol;
524 namespace_t namespc = (namespace_t)declaration->namespc;
526 /* remember old declaration */
528 entry.symbol = symbol;
529 entry.old_declaration = symbol->declaration;
530 entry.namespc = (unsigned short) namespc;
531 ARR_APP1(stack_entry_t, *stack_ptr, entry);
533 /* replace/add declaration into declaration list of the symbol */
534 if(symbol->declaration == NULL) {
535 symbol->declaration = declaration;
537 declaration_t *iter_last = NULL;
538 declaration_t *iter = symbol->declaration;
539 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
540 /* replace an entry? */
541 if(iter->namespc == namespc) {
542 if(iter_last == NULL) {
543 symbol->declaration = declaration;
545 iter_last->symbol_next = declaration;
547 declaration->symbol_next = iter->symbol_next;
552 assert(iter_last->symbol_next == NULL);
553 iter_last->symbol_next = declaration;
558 static void environment_push(declaration_t *declaration)
560 assert(declaration->source_position.input_name != NULL);
561 assert(declaration->parent_context != NULL);
562 stack_push(&environment_stack, declaration);
565 static void label_push(declaration_t *declaration)
567 declaration->parent_context = ¤t_function->context;
568 stack_push(&label_stack, declaration);
572 * pops symbols from the environment stack until @p new_top is the top element
574 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
576 stack_entry_t *stack = *stack_ptr;
577 size_t top = ARR_LEN(stack);
580 assert(new_top <= top);
584 for(i = top; i > new_top; --i) {
585 stack_entry_t *entry = &stack[i - 1];
587 declaration_t *old_declaration = entry->old_declaration;
588 symbol_t *symbol = entry->symbol;
589 namespace_t namespc = (namespace_t)entry->namespc;
591 /* replace/remove declaration */
592 declaration_t *declaration = symbol->declaration;
593 assert(declaration != NULL);
594 if(declaration->namespc == namespc) {
595 if(old_declaration == NULL) {
596 symbol->declaration = declaration->symbol_next;
598 symbol->declaration = old_declaration;
601 declaration_t *iter_last = declaration;
602 declaration_t *iter = declaration->symbol_next;
603 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
604 /* replace an entry? */
605 if(iter->namespc == namespc) {
606 assert(iter_last != NULL);
607 iter_last->symbol_next = old_declaration;
608 old_declaration->symbol_next = iter->symbol_next;
612 assert(iter != NULL);
616 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
619 static void environment_pop_to(size_t new_top)
621 stack_pop_to(&environment_stack, new_top);
624 static void label_pop_to(size_t new_top)
626 stack_pop_to(&label_stack, new_top);
630 static int get_rank(const type_t *type)
632 assert(!is_typeref(type));
633 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
634 * and esp. footnote 108). However we can't fold constants (yet), so we
635 * can't decide whether unsigned int is possible, while int always works.
636 * (unsigned int would be preferable when possible... for stuff like
637 * struct { enum { ... } bla : 4; } ) */
638 if(type->kind == TYPE_ENUM)
639 return ATOMIC_TYPE_INT;
641 assert(type->kind == TYPE_ATOMIC);
642 const atomic_type_t *atomic_type = &type->atomic;
643 atomic_type_kind_t atype = atomic_type->akind;
647 static type_t *promote_integer(type_t *type)
649 if(type->kind == TYPE_BITFIELD)
650 type = type->bitfield.base;
652 if(get_rank(type) < ATOMIC_TYPE_INT)
659 * Create a cast expression.
661 * @param expression the expression to cast
662 * @param dest_type the destination type
664 static expression_t *create_cast_expression(expression_t *expression,
667 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
669 cast->unary.value = expression;
670 cast->base.datatype = dest_type;
676 * Check if a given expression represents the 0 pointer constant.
678 static bool is_null_pointer_constant(const expression_t *expression)
680 /* skip void* cast */
681 if(expression->kind == EXPR_UNARY_CAST
682 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
683 expression = expression->unary.value;
686 /* TODO: not correct yet, should be any constant integer expression
687 * which evaluates to 0 */
688 if (expression->kind != EXPR_CONST)
691 type_t *const type = skip_typeref(expression->base.datatype);
692 if (!is_type_integer(type))
695 return expression->conste.v.int_value == 0;
699 * Create an implicit cast expression.
701 * @param expression the expression to cast
702 * @param dest_type the destination type
704 static expression_t *create_implicit_cast(expression_t *expression,
707 type_t *const source_type = expression->base.datatype;
709 if (source_type == dest_type)
712 return create_cast_expression(expression, dest_type);
715 /** Implements the rules from § 6.5.16.1 */
716 static type_t *semantic_assign(type_t *orig_type_left,
717 const expression_t *const right,
720 type_t *const orig_type_right = right->base.datatype;
721 type_t *const type_left = skip_typeref(orig_type_left);
722 type_t *const type_right = skip_typeref(orig_type_right);
724 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
725 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
726 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
727 && is_type_pointer(type_right))) {
728 return orig_type_left;
731 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
732 pointer_type_t *pointer_type_left = &type_left->pointer;
733 pointer_type_t *pointer_type_right = &type_right->pointer;
734 type_t *points_to_left = pointer_type_left->points_to;
735 type_t *points_to_right = pointer_type_right->points_to;
737 points_to_left = skip_typeref(points_to_left);
738 points_to_right = skip_typeref(points_to_right);
740 /* the left type has all qualifiers from the right type */
741 unsigned missing_qualifiers
742 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
743 if(missing_qualifiers != 0) {
744 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
745 return orig_type_left;
748 points_to_left = get_unqualified_type(points_to_left);
749 points_to_right = get_unqualified_type(points_to_right);
751 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
752 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
753 && !types_compatible(points_to_left, points_to_right)) {
757 return orig_type_left;
760 if (is_type_compound(type_left) && is_type_compound(type_right)) {
761 type_t *const unqual_type_left = get_unqualified_type(type_left);
762 type_t *const unqual_type_right = get_unqualified_type(type_right);
763 if (types_compatible(unqual_type_left, unqual_type_right)) {
764 return orig_type_left;
768 if (!is_type_valid(type_left))
771 if (!is_type_valid(type_right))
772 return orig_type_right;
777 static expression_t *parse_constant_expression(void)
779 /* start parsing at precedence 7 (conditional expression) */
780 expression_t *result = parse_sub_expression(7);
782 if(!is_constant_expression(result)) {
783 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
789 static expression_t *parse_assignment_expression(void)
791 /* start parsing at precedence 2 (assignment expression) */
792 return parse_sub_expression(2);
795 static type_t *make_global_typedef(const char *name, type_t *type)
797 symbol_t *const symbol = symbol_table_insert(name);
799 declaration_t *const declaration = allocate_declaration_zero();
800 declaration->namespc = NAMESPACE_NORMAL;
801 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
802 declaration->type = type;
803 declaration->symbol = symbol;
804 declaration->source_position = builtin_source_position;
806 record_declaration(declaration);
808 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
809 typedef_type->typedeft.declaration = declaration;
814 static string_t parse_string_literals(void)
816 assert(token.type == T_STRING_LITERAL);
817 string_t result = token.v.string;
821 while (token.type == T_STRING_LITERAL) {
822 result = concat_strings(&result, &token.v.string);
829 static void parse_attributes(void)
833 case T___attribute__: {
841 errorf(HERE, "EOF while parsing attribute");
860 if(token.type != T_STRING_LITERAL) {
861 parse_error_expected("while parsing assembler attribute",
866 parse_string_literals();
871 goto attributes_finished;
880 static designator_t *parse_designation(void)
882 if(token.type != '[' && token.type != '.')
885 designator_t *result = NULL;
886 designator_t *last = NULL;
889 designator_t *designator;
892 designator = allocate_ast_zero(sizeof(designator[0]));
894 designator->array_access = parse_constant_expression();
898 designator = allocate_ast_zero(sizeof(designator[0]));
900 if(token.type != T_IDENTIFIER) {
901 parse_error_expected("while parsing designator",
905 designator->symbol = token.v.symbol;
913 assert(designator != NULL);
915 last->next = designator;
924 static initializer_t *initializer_from_string(array_type_t *type,
925 const string_t *const string)
927 /* TODO: check len vs. size of array type */
930 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
931 initializer->string.string = *string;
936 static initializer_t *initializer_from_wide_string(array_type_t *const type,
937 wide_string_t *const string)
939 /* TODO: check len vs. size of array type */
942 initializer_t *const initializer =
943 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
944 initializer->wide_string.string = *string;
949 static initializer_t *initializer_from_expression(type_t *type,
950 expression_t *expression)
952 /* TODO check that expression is a constant expression */
954 /* § 6.7.8.14/15 char array may be initialized by string literals */
955 type_t *const expr_type = expression->base.datatype;
956 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
957 array_type_t *const array_type = &type->array;
958 type_t *const element_type = skip_typeref(array_type->element_type);
960 if (element_type->kind == TYPE_ATOMIC) {
961 switch (expression->kind) {
962 case EXPR_STRING_LITERAL:
963 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
964 return initializer_from_string(array_type,
965 &expression->string.value);
968 case EXPR_WIDE_STRING_LITERAL: {
969 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
970 if (get_unqualified_type(element_type) == bare_wchar_type) {
971 return initializer_from_wide_string(array_type,
972 &expression->wide_string.value);
982 type_t *const res_type = semantic_assign(type, expression, "initializer");
983 if (res_type == NULL)
986 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
987 result->value.value = create_implicit_cast(expression, res_type);
992 static initializer_t *parse_sub_initializer(type_t *type,
993 expression_t *expression,
994 type_t *expression_type);
996 static initializer_t *parse_sub_initializer_elem(type_t *type)
998 if(token.type == '{') {
999 return parse_sub_initializer(type, NULL, NULL);
1002 expression_t *expression = parse_assignment_expression();
1003 type_t *expression_type = skip_typeref(expression->base.datatype);
1005 return parse_sub_initializer(type, expression, expression_type);
1008 static bool had_initializer_brace_warning;
1010 static void skip_designator(void)
1013 if(token.type == '.') {
1015 if(token.type == T_IDENTIFIER)
1017 } else if(token.type == '[') {
1019 parse_constant_expression();
1020 if(token.type == ']')
1028 static initializer_t *parse_sub_initializer(type_t *type,
1029 expression_t *expression,
1030 type_t *expression_type)
1032 if(is_type_scalar(type)) {
1033 /* there might be extra {} hierarchies */
1034 if(token.type == '{') {
1036 if(!had_initializer_brace_warning) {
1037 warningf(HERE, "braces around scalar initializer");
1038 had_initializer_brace_warning = true;
1040 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1041 if(token.type == ',') {
1043 /* TODO: warn about excessive elements */
1049 if(expression == NULL) {
1050 expression = parse_assignment_expression();
1052 return initializer_from_expression(type, expression);
1055 /* does the expression match the currently looked at object to initialize */
1056 if(expression != NULL) {
1057 initializer_t *result = initializer_from_expression(type, expression);
1062 bool read_paren = false;
1063 if(token.type == '{') {
1068 /* descend into subtype */
1069 initializer_t *result = NULL;
1070 initializer_t **elems;
1071 if(is_type_array(type)) {
1072 array_type_t *array_type = &type->array;
1073 type_t *element_type = array_type->element_type;
1074 element_type = skip_typeref(element_type);
1076 if(token.type == '.') {
1078 "compound designator in initializer for array type '%T'",
1084 had_initializer_brace_warning = false;
1085 if(expression == NULL) {
1086 sub = parse_sub_initializer_elem(element_type);
1088 sub = parse_sub_initializer(element_type, expression,
1092 /* didn't match the subtypes -> try the parent type */
1094 assert(!read_paren);
1098 elems = NEW_ARR_F(initializer_t*, 0);
1099 ARR_APP1(initializer_t*, elems, sub);
1102 if(token.type == '}')
1105 if(token.type == '}')
1108 sub = parse_sub_initializer_elem(element_type);
1110 /* TODO error, do nicer cleanup */
1111 errorf(HERE, "member initializer didn't match");
1115 ARR_APP1(initializer_t*, elems, sub);
1118 assert(is_type_compound(type));
1119 compound_type_t *compound_type = &type->compound;
1120 context_t *context = &compound_type->declaration->context;
1122 if(token.type == '[') {
1124 "array designator in initializer for compound type '%T'",
1129 declaration_t *first = context->declarations;
1132 type_t *first_type = first->type;
1133 first_type = skip_typeref(first_type);
1136 had_initializer_brace_warning = false;
1137 if(expression == NULL) {
1138 sub = parse_sub_initializer_elem(first_type);
1140 sub = parse_sub_initializer(first_type, expression,expression_type);
1143 /* didn't match the subtypes -> try our parent type */
1145 assert(!read_paren);
1149 elems = NEW_ARR_F(initializer_t*, 0);
1150 ARR_APP1(initializer_t*, elems, sub);
1152 declaration_t *iter = first->next;
1153 for( ; iter != NULL; iter = iter->next) {
1154 if(iter->symbol == NULL)
1156 if(iter->namespc != NAMESPACE_NORMAL)
1159 if(token.type == '}')
1162 if(token.type == '}')
1165 type_t *iter_type = iter->type;
1166 iter_type = skip_typeref(iter_type);
1168 sub = parse_sub_initializer_elem(iter_type);
1170 /* TODO error, do nicer cleanup */
1171 errorf(HERE, "member initializer didn't match");
1175 ARR_APP1(initializer_t*, elems, sub);
1179 int len = ARR_LEN(elems);
1180 size_t elems_size = sizeof(initializer_t*) * len;
1182 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1184 init->initializer.kind = INITIALIZER_LIST;
1186 memcpy(init->initializers, elems, elems_size);
1189 result = (initializer_t*) init;
1192 if(token.type == ',')
1199 static initializer_t *parse_initializer(type_t *const orig_type)
1201 initializer_t *result;
1203 type_t *const type = skip_typeref(orig_type);
1205 if(token.type != '{') {
1206 expression_t *expression = parse_assignment_expression();
1207 initializer_t *initializer = initializer_from_expression(type, expression);
1208 if(initializer == NULL) {
1210 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1211 expression, expression->base.datatype, orig_type);
1216 if(is_type_scalar(type)) {
1220 expression_t *expression = parse_assignment_expression();
1221 result = initializer_from_expression(type, expression);
1223 if(token.type == ',')
1229 result = parse_sub_initializer(type, NULL, NULL);
1235 static declaration_t *append_declaration(declaration_t *declaration);
1237 static declaration_t *parse_compound_type_specifier(bool is_struct)
1245 symbol_t *symbol = NULL;
1246 declaration_t *declaration = NULL;
1248 if (token.type == T___attribute__) {
1253 if(token.type == T_IDENTIFIER) {
1254 symbol = token.v.symbol;
1258 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1260 declaration = get_declaration(symbol, NAMESPACE_UNION);
1262 } else if(token.type != '{') {
1264 parse_error_expected("while parsing struct type specifier",
1265 T_IDENTIFIER, '{', 0);
1267 parse_error_expected("while parsing union type specifier",
1268 T_IDENTIFIER, '{', 0);
1274 if(declaration == NULL) {
1275 declaration = allocate_declaration_zero();
1276 declaration->namespc =
1277 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1278 declaration->source_position = token.source_position;
1279 declaration->symbol = symbol;
1280 declaration->parent_context = context;
1281 if (symbol != NULL) {
1282 environment_push(declaration);
1284 append_declaration(declaration);
1287 if(token.type == '{') {
1288 if(declaration->init.is_defined) {
1289 assert(symbol != NULL);
1290 errorf(HERE, "multiple definition of '%s %Y'",
1291 is_struct ? "struct" : "union", symbol);
1292 declaration->context.declarations = NULL;
1294 declaration->init.is_defined = true;
1296 int top = environment_top();
1297 context_t *last_context = context;
1298 set_context(&declaration->context);
1300 parse_compound_type_entries();
1303 assert(context == &declaration->context);
1304 set_context(last_context);
1305 environment_pop_to(top);
1311 static void parse_enum_entries(type_t *const enum_type)
1315 if(token.type == '}') {
1317 errorf(HERE, "empty enum not allowed");
1322 if(token.type != T_IDENTIFIER) {
1323 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1328 declaration_t *const entry = allocate_declaration_zero();
1329 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1330 entry->type = enum_type;
1331 entry->symbol = token.v.symbol;
1332 entry->source_position = token.source_position;
1335 if(token.type == '=') {
1337 entry->init.enum_value = parse_constant_expression();
1342 record_declaration(entry);
1344 if(token.type != ',')
1347 } while(token.type != '}');
1352 static type_t *parse_enum_specifier(void)
1356 declaration_t *declaration;
1359 if(token.type == T_IDENTIFIER) {
1360 symbol = token.v.symbol;
1363 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1364 } else if(token.type != '{') {
1365 parse_error_expected("while parsing enum type specifier",
1366 T_IDENTIFIER, '{', 0);
1373 if(declaration == NULL) {
1374 declaration = allocate_declaration_zero();
1375 declaration->namespc = NAMESPACE_ENUM;
1376 declaration->source_position = token.source_position;
1377 declaration->symbol = symbol;
1378 declaration->parent_context = context;
1381 type_t *const type = allocate_type_zero(TYPE_ENUM);
1382 type->enumt.declaration = declaration;
1384 if(token.type == '{') {
1385 if(declaration->init.is_defined) {
1386 errorf(HERE, "multiple definitions of enum %Y", symbol);
1388 if (symbol != NULL) {
1389 environment_push(declaration);
1391 append_declaration(declaration);
1392 declaration->init.is_defined = 1;
1394 parse_enum_entries(type);
1402 * if a symbol is a typedef to another type, return true
1404 static bool is_typedef_symbol(symbol_t *symbol)
1406 const declaration_t *const declaration =
1407 get_declaration(symbol, NAMESPACE_NORMAL);
1409 declaration != NULL &&
1410 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1413 static type_t *parse_typeof(void)
1421 expression_t *expression = NULL;
1424 switch(token.type) {
1425 case T___extension__:
1426 /* this can be a prefix to a typename or an expression */
1427 /* we simply eat it now. */
1430 } while(token.type == T___extension__);
1434 if(is_typedef_symbol(token.v.symbol)) {
1435 type = parse_typename();
1437 expression = parse_expression();
1438 type = expression->base.datatype;
1443 type = parse_typename();
1447 expression = parse_expression();
1448 type = expression->base.datatype;
1454 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1455 typeof_type->typeoft.expression = expression;
1456 typeof_type->typeoft.typeof_type = type;
1462 SPECIFIER_SIGNED = 1 << 0,
1463 SPECIFIER_UNSIGNED = 1 << 1,
1464 SPECIFIER_LONG = 1 << 2,
1465 SPECIFIER_INT = 1 << 3,
1466 SPECIFIER_DOUBLE = 1 << 4,
1467 SPECIFIER_CHAR = 1 << 5,
1468 SPECIFIER_SHORT = 1 << 6,
1469 SPECIFIER_LONG_LONG = 1 << 7,
1470 SPECIFIER_FLOAT = 1 << 8,
1471 SPECIFIER_BOOL = 1 << 9,
1472 SPECIFIER_VOID = 1 << 10,
1473 #ifdef PROVIDE_COMPLEX
1474 SPECIFIER_COMPLEX = 1 << 11,
1475 SPECIFIER_IMAGINARY = 1 << 12,
1479 static type_t *create_builtin_type(symbol_t *const symbol,
1480 type_t *const real_type)
1482 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1483 type->builtin.symbol = symbol;
1484 type->builtin.real_type = real_type;
1486 type_t *result = typehash_insert(type);
1487 if (type != result) {
1494 static type_t *get_typedef_type(symbol_t *symbol)
1496 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1497 if(declaration == NULL
1498 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1501 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1502 type->typedeft.declaration = declaration;
1507 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1509 type_t *type = NULL;
1510 unsigned type_qualifiers = 0;
1511 unsigned type_specifiers = 0;
1514 specifiers->source_position = token.source_position;
1517 switch(token.type) {
1520 #define MATCH_STORAGE_CLASS(token, class) \
1522 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1523 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1525 specifiers->storage_class = class; \
1529 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1530 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1531 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1532 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1533 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1536 switch (specifiers->storage_class) {
1537 case STORAGE_CLASS_NONE:
1538 specifiers->storage_class = STORAGE_CLASS_THREAD;
1541 case STORAGE_CLASS_EXTERN:
1542 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1545 case STORAGE_CLASS_STATIC:
1546 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1550 errorf(HERE, "multiple storage classes in declaration specifiers");
1556 /* type qualifiers */
1557 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1559 type_qualifiers |= qualifier; \
1563 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1564 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1565 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1567 case T___extension__:
1572 /* type specifiers */
1573 #define MATCH_SPECIFIER(token, specifier, name) \
1576 if(type_specifiers & specifier) { \
1577 errorf(HERE, "multiple " name " type specifiers given"); \
1579 type_specifiers |= specifier; \
1583 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1584 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1585 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1586 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1587 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1588 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1589 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1590 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1591 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1592 #ifdef PROVIDE_COMPLEX
1593 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1594 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1597 /* only in microsoft mode */
1598 specifiers->decl_modifiers |= DM_FORCEINLINE;
1602 specifiers->is_inline = true;
1607 if(type_specifiers & SPECIFIER_LONG_LONG) {
1608 errorf(HERE, "multiple type specifiers given");
1609 } else if(type_specifiers & SPECIFIER_LONG) {
1610 type_specifiers |= SPECIFIER_LONG_LONG;
1612 type_specifiers |= SPECIFIER_LONG;
1616 /* TODO: if is_type_valid(type) for the following rules should issue
1619 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1621 type->compound.declaration = parse_compound_type_specifier(true);
1625 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1627 type->compound.declaration = parse_compound_type_specifier(false);
1631 type = parse_enum_specifier();
1634 type = parse_typeof();
1636 case T___builtin_va_list:
1637 type = duplicate_type(type_valist);
1641 case T___attribute__:
1646 case T_IDENTIFIER: {
1647 type_t *typedef_type = get_typedef_type(token.v.symbol);
1649 if(typedef_type == NULL)
1650 goto finish_specifiers;
1653 type = typedef_type;
1657 /* function specifier */
1659 goto finish_specifiers;
1666 atomic_type_kind_t atomic_type;
1668 /* match valid basic types */
1669 switch(type_specifiers) {
1670 case SPECIFIER_VOID:
1671 atomic_type = ATOMIC_TYPE_VOID;
1673 case SPECIFIER_CHAR:
1674 atomic_type = ATOMIC_TYPE_CHAR;
1676 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1677 atomic_type = ATOMIC_TYPE_SCHAR;
1679 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1680 atomic_type = ATOMIC_TYPE_UCHAR;
1682 case SPECIFIER_SHORT:
1683 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1684 case SPECIFIER_SHORT | SPECIFIER_INT:
1685 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1686 atomic_type = ATOMIC_TYPE_SHORT;
1688 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1689 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1690 atomic_type = ATOMIC_TYPE_USHORT;
1693 case SPECIFIER_SIGNED:
1694 case SPECIFIER_SIGNED | SPECIFIER_INT:
1695 atomic_type = ATOMIC_TYPE_INT;
1697 case SPECIFIER_UNSIGNED:
1698 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1699 atomic_type = ATOMIC_TYPE_UINT;
1701 case SPECIFIER_LONG:
1702 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1703 case SPECIFIER_LONG | SPECIFIER_INT:
1704 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1705 atomic_type = ATOMIC_TYPE_LONG;
1707 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1708 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1709 atomic_type = ATOMIC_TYPE_ULONG;
1711 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1712 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1713 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1714 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1716 atomic_type = ATOMIC_TYPE_LONGLONG;
1718 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1719 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1721 atomic_type = ATOMIC_TYPE_ULONGLONG;
1723 case SPECIFIER_FLOAT:
1724 atomic_type = ATOMIC_TYPE_FLOAT;
1726 case SPECIFIER_DOUBLE:
1727 atomic_type = ATOMIC_TYPE_DOUBLE;
1729 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1730 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1732 case SPECIFIER_BOOL:
1733 atomic_type = ATOMIC_TYPE_BOOL;
1735 #ifdef PROVIDE_COMPLEX
1736 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1737 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1739 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1740 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1742 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1743 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1745 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1746 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1748 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1749 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1751 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1752 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1756 /* invalid specifier combination, give an error message */
1757 if(type_specifiers == 0) {
1758 if (! strict_mode) {
1759 warningf(HERE, "no type specifiers in declaration, using int");
1760 atomic_type = ATOMIC_TYPE_INT;
1763 errorf(HERE, "no type specifiers given in declaration");
1765 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1766 (type_specifiers & SPECIFIER_UNSIGNED)) {
1767 errorf(HERE, "signed and unsigned specifiers gives");
1768 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1769 errorf(HERE, "only integer types can be signed or unsigned");
1771 errorf(HERE, "multiple datatypes in declaration");
1773 atomic_type = ATOMIC_TYPE_INVALID;
1776 type = allocate_type_zero(TYPE_ATOMIC);
1777 type->atomic.akind = atomic_type;
1780 if(type_specifiers != 0) {
1781 errorf(HERE, "multiple datatypes in declaration");
1785 type->base.qualifiers = type_qualifiers;
1787 type_t *result = typehash_insert(type);
1788 if(newtype && result != type) {
1792 specifiers->type = result;
1795 static type_qualifiers_t parse_type_qualifiers(void)
1797 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1800 switch(token.type) {
1801 /* type qualifiers */
1802 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1803 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1804 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1807 return type_qualifiers;
1812 static declaration_t *parse_identifier_list(void)
1814 declaration_t *declarations = NULL;
1815 declaration_t *last_declaration = NULL;
1817 declaration_t *const declaration = allocate_declaration_zero();
1818 declaration->source_position = token.source_position;
1819 declaration->symbol = token.v.symbol;
1822 if(last_declaration != NULL) {
1823 last_declaration->next = declaration;
1825 declarations = declaration;
1827 last_declaration = declaration;
1829 if(token.type != ',')
1832 } while(token.type == T_IDENTIFIER);
1834 return declarations;
1837 static void semantic_parameter(declaration_t *declaration)
1839 /* TODO: improve error messages */
1841 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1842 errorf(HERE, "typedef not allowed in parameter list");
1843 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1844 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1845 errorf(HERE, "parameter may only have none or register storage class");
1848 type_t *const orig_type = declaration->type;
1849 type_t * type = skip_typeref(orig_type);
1851 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1852 * into a pointer. § 6.7.5.3 (7) */
1853 if (is_type_array(type)) {
1854 const array_type_t *arr_type = &type->array;
1855 type_t *element_type = arr_type->element_type;
1857 type = make_pointer_type(element_type, type->base.qualifiers);
1859 declaration->type = type;
1862 if(is_type_incomplete(type)) {
1863 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1864 orig_type, declaration->symbol);
1868 static declaration_t *parse_parameter(void)
1870 declaration_specifiers_t specifiers;
1871 memset(&specifiers, 0, sizeof(specifiers));
1873 parse_declaration_specifiers(&specifiers);
1875 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1877 semantic_parameter(declaration);
1882 static declaration_t *parse_parameters(function_type_t *type)
1884 if(token.type == T_IDENTIFIER) {
1885 symbol_t *symbol = token.v.symbol;
1886 if(!is_typedef_symbol(symbol)) {
1887 type->kr_style_parameters = true;
1888 return parse_identifier_list();
1892 if(token.type == ')') {
1893 type->unspecified_parameters = 1;
1896 if(token.type == T_void && look_ahead(1)->type == ')') {
1901 declaration_t *declarations = NULL;
1902 declaration_t *declaration;
1903 declaration_t *last_declaration = NULL;
1904 function_parameter_t *parameter;
1905 function_parameter_t *last_parameter = NULL;
1908 switch(token.type) {
1912 return declarations;
1915 case T___extension__:
1917 declaration = parse_parameter();
1919 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1920 memset(parameter, 0, sizeof(parameter[0]));
1921 parameter->type = declaration->type;
1923 if(last_parameter != NULL) {
1924 last_declaration->next = declaration;
1925 last_parameter->next = parameter;
1927 type->parameters = parameter;
1928 declarations = declaration;
1930 last_parameter = parameter;
1931 last_declaration = declaration;
1935 return declarations;
1937 if(token.type != ',')
1938 return declarations;
1948 } construct_type_type_t;
1950 typedef struct construct_type_t construct_type_t;
1951 struct construct_type_t {
1952 construct_type_type_t type;
1953 construct_type_t *next;
1956 typedef struct parsed_pointer_t parsed_pointer_t;
1957 struct parsed_pointer_t {
1958 construct_type_t construct_type;
1959 type_qualifiers_t type_qualifiers;
1962 typedef struct construct_function_type_t construct_function_type_t;
1963 struct construct_function_type_t {
1964 construct_type_t construct_type;
1965 type_t *function_type;
1968 typedef struct parsed_array_t parsed_array_t;
1969 struct parsed_array_t {
1970 construct_type_t construct_type;
1971 type_qualifiers_t type_qualifiers;
1977 typedef struct construct_base_type_t construct_base_type_t;
1978 struct construct_base_type_t {
1979 construct_type_t construct_type;
1983 static construct_type_t *parse_pointer_declarator(void)
1987 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1988 memset(pointer, 0, sizeof(pointer[0]));
1989 pointer->construct_type.type = CONSTRUCT_POINTER;
1990 pointer->type_qualifiers = parse_type_qualifiers();
1992 return (construct_type_t*) pointer;
1995 static construct_type_t *parse_array_declarator(void)
1999 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2000 memset(array, 0, sizeof(array[0]));
2001 array->construct_type.type = CONSTRUCT_ARRAY;
2003 if(token.type == T_static) {
2004 array->is_static = true;
2008 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2009 if(type_qualifiers != 0) {
2010 if(token.type == T_static) {
2011 array->is_static = true;
2015 array->type_qualifiers = type_qualifiers;
2017 if(token.type == '*' && look_ahead(1)->type == ']') {
2018 array->is_variable = true;
2020 } else if(token.type != ']') {
2021 array->size = parse_assignment_expression();
2026 return (construct_type_t*) array;
2029 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2033 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2035 declaration_t *parameters = parse_parameters(&type->function);
2036 if(declaration != NULL) {
2037 declaration->context.declarations = parameters;
2040 construct_function_type_t *construct_function_type =
2041 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2042 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2043 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2044 construct_function_type->function_type = type;
2048 return (construct_type_t*) construct_function_type;
2051 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2052 bool may_be_abstract)
2054 /* construct a single linked list of construct_type_t's which describe
2055 * how to construct the final declarator type */
2056 construct_type_t *first = NULL;
2057 construct_type_t *last = NULL;
2060 while(token.type == '*') {
2061 construct_type_t *type = parse_pointer_declarator();
2072 /* TODO: find out if this is correct */
2075 construct_type_t *inner_types = NULL;
2077 switch(token.type) {
2079 if(declaration == NULL) {
2080 errorf(HERE, "no identifier expected in typename");
2082 declaration->symbol = token.v.symbol;
2083 declaration->source_position = token.source_position;
2089 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2095 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2096 /* avoid a loop in the outermost scope, because eat_statement doesn't
2098 if(token.type == '}' && current_function == NULL) {
2106 construct_type_t *p = last;
2109 construct_type_t *type;
2110 switch(token.type) {
2112 type = parse_function_declarator(declaration);
2115 type = parse_array_declarator();
2118 goto declarator_finished;
2121 /* insert in the middle of the list (behind p) */
2123 type->next = p->next;
2134 declarator_finished:
2137 /* append inner_types at the end of the list, we don't to set last anymore
2138 * as it's not needed anymore */
2140 assert(first == NULL);
2141 first = inner_types;
2143 last->next = inner_types;
2149 static type_t *construct_declarator_type(construct_type_t *construct_list,
2152 construct_type_t *iter = construct_list;
2153 for( ; iter != NULL; iter = iter->next) {
2154 switch(iter->type) {
2155 case CONSTRUCT_INVALID:
2156 panic("invalid type construction found");
2157 case CONSTRUCT_FUNCTION: {
2158 construct_function_type_t *construct_function_type
2159 = (construct_function_type_t*) iter;
2161 type_t *function_type = construct_function_type->function_type;
2163 function_type->function.return_type = type;
2165 type_t *skipped_return_type = skip_typeref(type);
2166 if (is_type_function(skipped_return_type)) {
2167 errorf(HERE, "function returning function is not allowed");
2168 type = type_error_type;
2169 } else if (is_type_array(skipped_return_type)) {
2170 errorf(HERE, "function returning array is not allowed");
2171 type = type_error_type;
2173 type = function_type;
2178 case CONSTRUCT_POINTER: {
2179 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2180 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2181 pointer_type->pointer.points_to = type;
2182 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2184 type = pointer_type;
2188 case CONSTRUCT_ARRAY: {
2189 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2190 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2192 array_type->base.qualifiers = parsed_array->type_qualifiers;
2193 array_type->array.element_type = type;
2194 array_type->array.is_static = parsed_array->is_static;
2195 array_type->array.is_variable = parsed_array->is_variable;
2196 array_type->array.size = parsed_array->size;
2198 type_t *skipped_type = skip_typeref(type);
2199 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2200 errorf(HERE, "array of void is not allowed");
2201 type = type_error_type;
2209 type_t *hashed_type = typehash_insert(type);
2210 if(hashed_type != type) {
2211 /* the function type was constructed earlier freeing it here will
2212 * destroy other types... */
2213 if(iter->type != CONSTRUCT_FUNCTION) {
2223 static declaration_t *parse_declarator(
2224 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2226 declaration_t *const declaration = allocate_declaration_zero();
2227 declaration->storage_class = specifiers->storage_class;
2228 declaration->modifiers = specifiers->decl_modifiers;
2229 declaration->is_inline = specifiers->is_inline;
2231 construct_type_t *construct_type
2232 = parse_inner_declarator(declaration, may_be_abstract);
2233 type_t *const type = specifiers->type;
2234 declaration->type = construct_declarator_type(construct_type, type);
2236 if(construct_type != NULL) {
2237 obstack_free(&temp_obst, construct_type);
2243 static type_t *parse_abstract_declarator(type_t *base_type)
2245 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2247 type_t *result = construct_declarator_type(construct_type, base_type);
2248 if(construct_type != NULL) {
2249 obstack_free(&temp_obst, construct_type);
2255 static declaration_t *append_declaration(declaration_t* const declaration)
2257 if (last_declaration != NULL) {
2258 last_declaration->next = declaration;
2260 context->declarations = declaration;
2262 last_declaration = declaration;
2266 static declaration_t *internal_record_declaration(
2267 declaration_t *const declaration,
2268 const bool is_function_definition)
2270 const symbol_t *const symbol = declaration->symbol;
2271 const namespace_t namespc = (namespace_t)declaration->namespc;
2273 const type_t *const type = skip_typeref(declaration->type);
2274 if (is_type_function(type) && type->function.unspecified_parameters) {
2275 warningf(declaration->source_position,
2276 "function declaration '%#T' is not a prototype",
2277 type, declaration->symbol);
2280 declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2281 assert(declaration != previous_declaration);
2282 if (previous_declaration != NULL
2283 && previous_declaration->parent_context == context) {
2284 /* can happen for K&R style declarations */
2285 if(previous_declaration->type == NULL) {
2286 previous_declaration->type = declaration->type;
2289 const type_t *const prev_type = skip_typeref(previous_declaration->type);
2290 if (!types_compatible(type, prev_type)) {
2291 errorf(declaration->source_position,
2292 "declaration '%#T' is incompatible with previous declaration '%#T'",
2293 type, symbol, previous_declaration->type, symbol);
2294 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2296 unsigned old_storage_class = previous_declaration->storage_class;
2297 unsigned new_storage_class = declaration->storage_class;
2299 /* pretend no storage class means extern for function declarations
2300 * (except if the previous declaration is neither none nor extern) */
2301 if (is_type_function(type)) {
2302 switch (old_storage_class) {
2303 case STORAGE_CLASS_NONE:
2304 old_storage_class = STORAGE_CLASS_EXTERN;
2306 case STORAGE_CLASS_EXTERN:
2307 if (new_storage_class == STORAGE_CLASS_NONE && !is_function_definition) {
2308 new_storage_class = STORAGE_CLASS_EXTERN;
2316 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2317 new_storage_class == STORAGE_CLASS_EXTERN) {
2318 warn_redundant_declaration:
2319 warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
2320 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2321 } else if (current_function == NULL) {
2322 if (old_storage_class != STORAGE_CLASS_STATIC &&
2323 new_storage_class == STORAGE_CLASS_STATIC) {
2324 errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
2325 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2327 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2328 goto warn_redundant_declaration;
2330 if (new_storage_class == STORAGE_CLASS_NONE) {
2331 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2335 if (old_storage_class == new_storage_class) {
2336 errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
2338 errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
2340 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2343 return previous_declaration;
2346 assert(declaration->parent_context == NULL);
2347 assert(declaration->symbol != NULL);
2348 assert(context != NULL);
2350 declaration->parent_context = context;
2352 environment_push(declaration);
2353 return append_declaration(declaration);
2356 static declaration_t *record_declaration(declaration_t *declaration)
2358 return internal_record_declaration(declaration, false);
2361 static declaration_t *record_function_definition(declaration_t *declaration)
2363 return internal_record_declaration(declaration, true);
2366 static void parser_error_multiple_definition(declaration_t *declaration,
2367 const source_position_t source_position)
2369 errorf(source_position, "multiple definition of symbol '%Y'",
2370 declaration->symbol);
2371 errorf(declaration->source_position,
2372 "this is the location of the previous definition.");
2375 static bool is_declaration_specifier(const token_t *token,
2376 bool only_type_specifiers)
2378 switch(token->type) {
2382 return is_typedef_symbol(token->v.symbol);
2384 case T___extension__:
2387 return !only_type_specifiers;
2394 static void parse_init_declarator_rest(declaration_t *declaration)
2398 type_t *orig_type = declaration->type;
2399 type_t *type = type = skip_typeref(orig_type);
2401 if(declaration->init.initializer != NULL) {
2402 parser_error_multiple_definition(declaration, token.source_position);
2405 initializer_t *initializer = parse_initializer(type);
2407 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2408 * the array type size */
2409 if(is_type_array(type) && initializer != NULL) {
2410 array_type_t *array_type = &type->array;
2412 if(array_type->size == NULL) {
2413 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2415 cnst->base.datatype = type_size_t;
2417 switch (initializer->kind) {
2418 case INITIALIZER_LIST: {
2419 initializer_list_t *const list = &initializer->list;
2420 cnst->conste.v.int_value = list->len;
2424 case INITIALIZER_STRING: {
2425 initializer_string_t *const string = &initializer->string;
2426 cnst->conste.v.int_value = string->string.size;
2430 case INITIALIZER_WIDE_STRING: {
2431 initializer_wide_string_t *const string = &initializer->wide_string;
2432 cnst->conste.v.int_value = string->string.size;
2437 panic("invalid initializer type");
2440 array_type->size = cnst;
2444 if(is_type_function(type)) {
2445 errorf(declaration->source_position,
2446 "initializers not allowed for function types at declator '%Y' (type '%T')",
2447 declaration->symbol, orig_type);
2449 declaration->init.initializer = initializer;
2453 /* parse rest of a declaration without any declarator */
2454 static void parse_anonymous_declaration_rest(
2455 const declaration_specifiers_t *specifiers,
2456 parsed_declaration_func finished_declaration)
2460 declaration_t *const declaration = allocate_declaration_zero();
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(is_type_valid(type) &&
2502 type->kind != TYPE_FUNCTION && declaration->is_inline) {
2503 warningf(declaration->source_position,
2504 "variable '%Y' declared 'inline'\n", declaration->symbol);
2507 if(token.type == '=') {
2508 parse_init_declarator_rest(declaration);
2511 if(token.type != ',')
2515 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2520 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2522 symbol_t *symbol = declaration->symbol;
2523 if(symbol == NULL) {
2524 errorf(HERE, "anonymous declaration not valid as function parameter");
2527 namespace_t namespc = (namespace_t) declaration->namespc;
2528 if(namespc != NAMESPACE_NORMAL) {
2529 return record_declaration(declaration);
2532 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2533 if(previous_declaration == NULL ||
2534 previous_declaration->parent_context != context) {
2535 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2540 if(previous_declaration->type == NULL) {
2541 previous_declaration->type = declaration->type;
2542 previous_declaration->storage_class = declaration->storage_class;
2543 previous_declaration->parent_context = context;
2544 return previous_declaration;
2546 return record_declaration(declaration);
2550 static void parse_declaration(parsed_declaration_func finished_declaration)
2552 declaration_specifiers_t specifiers;
2553 memset(&specifiers, 0, sizeof(specifiers));
2554 parse_declaration_specifiers(&specifiers);
2556 if(token.type == ';') {
2557 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2559 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2560 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2564 static void parse_kr_declaration_list(declaration_t *declaration)
2566 type_t *type = skip_typeref(declaration->type);
2567 if(!is_type_function(type))
2570 if(!type->function.kr_style_parameters)
2573 /* push function parameters */
2574 int top = environment_top();
2575 context_t *last_context = context;
2576 set_context(&declaration->context);
2578 declaration_t *parameter = declaration->context.declarations;
2579 for( ; parameter != NULL; parameter = parameter->next) {
2580 assert(parameter->parent_context == NULL);
2581 parameter->parent_context = context;
2582 environment_push(parameter);
2585 /* parse declaration list */
2586 while(is_declaration_specifier(&token, false)) {
2587 parse_declaration(finished_kr_declaration);
2590 /* pop function parameters */
2591 assert(context == &declaration->context);
2592 set_context(last_context);
2593 environment_pop_to(top);
2595 /* update function type */
2596 type_t *new_type = duplicate_type(type);
2597 new_type->function.kr_style_parameters = false;
2599 function_parameter_t *parameters = NULL;
2600 function_parameter_t *last_parameter = NULL;
2602 declaration_t *parameter_declaration = declaration->context.declarations;
2603 for( ; parameter_declaration != NULL;
2604 parameter_declaration = parameter_declaration->next) {
2605 type_t *parameter_type = parameter_declaration->type;
2606 if(parameter_type == NULL) {
2608 errorf(HERE, "no type specified for function parameter '%Y'",
2609 parameter_declaration->symbol);
2611 warningf(HERE, "no type specified for function parameter '%Y', using int",
2612 parameter_declaration->symbol);
2613 parameter_type = type_int;
2614 parameter_declaration->type = parameter_type;
2618 semantic_parameter(parameter_declaration);
2619 parameter_type = parameter_declaration->type;
2621 function_parameter_t *function_parameter
2622 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2623 memset(function_parameter, 0, sizeof(function_parameter[0]));
2625 function_parameter->type = parameter_type;
2626 if(last_parameter != NULL) {
2627 last_parameter->next = function_parameter;
2629 parameters = function_parameter;
2631 last_parameter = function_parameter;
2633 new_type->function.parameters = parameters;
2635 type = typehash_insert(new_type);
2636 if(type != new_type) {
2637 obstack_free(type_obst, new_type);
2640 declaration->type = type;
2644 * Check if all labels are defined in the current function.
2646 static void check_for_missing_labels(void)
2648 bool first_err = true;
2649 for (const goto_statement_t *goto_statement = goto_first;
2650 goto_statement != NULL;
2651 goto_statement = goto_statement->next) {
2652 const declaration_t *label = goto_statement->label;
2654 if (label->source_position.input_name == NULL) {
2657 diagnosticf("%s: In function '%Y':\n",
2658 current_function->source_position.input_name,
2659 current_function->symbol);
2661 errorf(goto_statement->statement.source_position,
2662 "label '%Y' used but not defined", label->symbol);
2665 goto_first = goto_last = NULL;
2668 static void parse_external_declaration(void)
2670 /* function-definitions and declarations both start with declaration
2672 declaration_specifiers_t specifiers;
2673 memset(&specifiers, 0, sizeof(specifiers));
2674 parse_declaration_specifiers(&specifiers);
2676 /* must be a declaration */
2677 if(token.type == ';') {
2678 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2682 /* declarator is common to both function-definitions and declarations */
2683 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2685 /* must be a declaration */
2686 if(token.type == ',' || token.type == '=' || token.type == ';') {
2687 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2691 /* must be a function definition */
2692 parse_kr_declaration_list(ndeclaration);
2694 if(token.type != '{') {
2695 parse_error_expected("while parsing function definition", '{', 0);
2700 type_t *type = ndeclaration->type;
2702 /* note that we don't skip typerefs: the standard doesn't allow them here
2703 * (so we can't use is_type_function here) */
2704 if(type->kind != TYPE_FUNCTION) {
2705 if (is_type_valid(type)) {
2706 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2707 type, ndeclaration->symbol);
2713 /* § 6.7.5.3 (14) a function definition with () means no
2714 * parameters (and not unspecified parameters) */
2715 if(type->function.unspecified_parameters) {
2716 type_t *duplicate = duplicate_type(type);
2717 duplicate->function.unspecified_parameters = false;
2719 type = typehash_insert(duplicate);
2720 if(type != duplicate) {
2721 obstack_free(type_obst, duplicate);
2723 ndeclaration->type = type;
2726 declaration_t *const declaration = record_function_definition(ndeclaration);
2727 if(ndeclaration != declaration) {
2728 declaration->context = ndeclaration->context;
2730 type = skip_typeref(declaration->type);
2732 /* push function parameters and switch context */
2733 int top = environment_top();
2734 context_t *last_context = context;
2735 set_context(&declaration->context);
2737 declaration_t *parameter = declaration->context.declarations;
2738 for( ; parameter != NULL; parameter = parameter->next) {
2739 if(parameter->parent_context == &ndeclaration->context) {
2740 parameter->parent_context = context;
2742 assert(parameter->parent_context == NULL
2743 || parameter->parent_context == context);
2744 parameter->parent_context = context;
2745 environment_push(parameter);
2748 if(declaration->init.statement != NULL) {
2749 parser_error_multiple_definition(declaration, token.source_position);
2751 goto end_of_parse_external_declaration;
2753 /* parse function body */
2754 int label_stack_top = label_top();
2755 declaration_t *old_current_function = current_function;
2756 current_function = declaration;
2758 declaration->init.statement = parse_compound_statement();
2759 check_for_missing_labels();
2761 assert(current_function == declaration);
2762 current_function = old_current_function;
2763 label_pop_to(label_stack_top);
2766 end_of_parse_external_declaration:
2767 assert(context == &declaration->context);
2768 set_context(last_context);
2769 environment_pop_to(top);
2772 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2774 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2775 type->bitfield.base = base;
2776 type->bitfield.size = size;
2781 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2783 /* TODO: check constraints for struct declarations (in specifiers) */
2785 declaration_t *declaration;
2787 if(token.type == ':') {
2790 type_t *base_type = specifiers->type;
2791 expression_t *size = parse_constant_expression();
2793 type_t *type = make_bitfield_type(base_type, size);
2795 declaration = allocate_declaration_zero();
2796 declaration->namespc = NAMESPACE_NORMAL;
2797 declaration->storage_class = STORAGE_CLASS_NONE;
2798 declaration->source_position = token.source_position;
2799 declaration->modifiers = specifiers->decl_modifiers;
2800 declaration->type = type;
2802 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2804 if(token.type == ':') {
2806 expression_t *size = parse_constant_expression();
2808 type_t *type = make_bitfield_type(declaration->type, size);
2809 declaration->type = type;
2812 record_declaration(declaration);
2814 if(token.type != ',')
2821 static void parse_compound_type_entries(void)
2825 while(token.type != '}' && token.type != T_EOF) {
2826 declaration_specifiers_t specifiers;
2827 memset(&specifiers, 0, sizeof(specifiers));
2828 parse_declaration_specifiers(&specifiers);
2830 parse_struct_declarators(&specifiers);
2832 if(token.type == T_EOF) {
2833 errorf(HERE, "EOF while parsing struct");
2838 static type_t *parse_typename(void)
2840 declaration_specifiers_t specifiers;
2841 memset(&specifiers, 0, sizeof(specifiers));
2842 parse_declaration_specifiers(&specifiers);
2843 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2844 /* TODO: improve error message, user does probably not know what a
2845 * storage class is...
2847 errorf(HERE, "typename may not have a storage class");
2850 type_t *result = parse_abstract_declarator(specifiers.type);
2858 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2859 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2860 expression_t *left);
2862 typedef struct expression_parser_function_t expression_parser_function_t;
2863 struct expression_parser_function_t {
2864 unsigned precedence;
2865 parse_expression_function parser;
2866 unsigned infix_precedence;
2867 parse_expression_infix_function infix_parser;
2870 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2873 * Creates a new invalid expression.
2875 static expression_t *create_invalid_expression(void)
2877 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2878 expression->base.source_position = token.source_position;
2883 * Prints an error message if an expression was expected but not read
2885 static expression_t *expected_expression_error(void)
2887 /* skip the error message if the error token was read */
2888 if (token.type != T_ERROR) {
2889 errorf(HERE, "expected expression, got token '%K'", &token);
2893 return create_invalid_expression();
2897 * Parse a string constant.
2899 static expression_t *parse_string_const(void)
2901 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2902 cnst->base.datatype = type_string;
2903 cnst->string.value = parse_string_literals();
2909 * Parse a wide string constant.
2911 static expression_t *parse_wide_string_const(void)
2913 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2914 cnst->base.datatype = type_wchar_t_ptr;
2915 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2921 * Parse an integer constant.
2923 static expression_t *parse_int_const(void)
2925 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2926 cnst->base.datatype = token.datatype;
2927 cnst->conste.v.int_value = token.v.intvalue;
2935 * Parse a float constant.
2937 static expression_t *parse_float_const(void)
2939 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2940 cnst->base.datatype = token.datatype;
2941 cnst->conste.v.float_value = token.v.floatvalue;
2948 static declaration_t *create_implicit_function(symbol_t *symbol,
2949 const source_position_t source_position)
2951 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2952 ntype->function.return_type = type_int;
2953 ntype->function.unspecified_parameters = true;
2955 type_t *type = typehash_insert(ntype);
2960 declaration_t *const declaration = allocate_declaration_zero();
2961 declaration->storage_class = STORAGE_CLASS_EXTERN;
2962 declaration->type = type;
2963 declaration->symbol = symbol;
2964 declaration->source_position = source_position;
2965 declaration->parent_context = global_context;
2967 context_t *old_context = context;
2968 set_context(global_context);
2970 environment_push(declaration);
2971 /* prepend the declaration to the global declarations list */
2972 declaration->next = context->declarations;
2973 context->declarations = declaration;
2975 assert(context == global_context);
2976 set_context(old_context);
2982 * Creates a return_type (func)(argument_type) function type if not
2985 * @param return_type the return type
2986 * @param argument_type the argument type
2988 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2990 function_parameter_t *parameter
2991 = obstack_alloc(type_obst, sizeof(parameter[0]));
2992 memset(parameter, 0, sizeof(parameter[0]));
2993 parameter->type = argument_type;
2995 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2996 type->function.return_type = return_type;
2997 type->function.parameters = parameter;
2999 type_t *result = typehash_insert(type);
3000 if(result != type) {
3008 * Creates a function type for some function like builtins.
3010 * @param symbol the symbol describing the builtin
3012 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3014 switch(symbol->ID) {
3015 case T___builtin_alloca:
3016 return make_function_1_type(type_void_ptr, type_size_t);
3017 case T___builtin_nan:
3018 return make_function_1_type(type_double, type_string);
3019 case T___builtin_nanf:
3020 return make_function_1_type(type_float, type_string);
3021 case T___builtin_nand:
3022 return make_function_1_type(type_long_double, type_string);
3023 case T___builtin_va_end:
3024 return make_function_1_type(type_void, type_valist);
3026 panic("not implemented builtin symbol found");
3031 * Performs automatic type cast as described in § 6.3.2.1.
3033 * @param orig_type the original type
3035 static type_t *automatic_type_conversion(type_t *orig_type)
3037 type_t *type = skip_typeref(orig_type);
3038 if(is_type_array(type)) {
3039 array_type_t *array_type = &type->array;
3040 type_t *element_type = array_type->element_type;
3041 unsigned qualifiers = array_type->type.qualifiers;
3043 return make_pointer_type(element_type, qualifiers);
3046 if(is_type_function(type)) {
3047 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3054 * reverts the automatic casts of array to pointer types and function
3055 * to function-pointer types as defined § 6.3.2.1
3057 type_t *revert_automatic_type_conversion(const expression_t *expression)
3059 switch(expression->kind) {
3060 case EXPR_REFERENCE: {
3061 const reference_expression_t *ref = &expression->reference;
3062 return ref->declaration->type;
3065 const select_expression_t *select = &expression->select;
3066 return select->compound_entry->type;
3068 case EXPR_UNARY_DEREFERENCE: {
3069 expression_t *value = expression->unary.value;
3070 type_t *type = skip_typeref(value->base.datatype);
3071 pointer_type_t *pointer_type = &type->pointer;
3073 return pointer_type->points_to;
3075 case EXPR_BUILTIN_SYMBOL: {
3076 const builtin_symbol_expression_t *builtin
3077 = &expression->builtin_symbol;
3078 return get_builtin_symbol_type(builtin->symbol);
3080 case EXPR_ARRAY_ACCESS: {
3081 const expression_t *const array_ref = expression->array_access.array_ref;
3082 type_t *const type_left = skip_typeref(array_ref->base.datatype);
3083 if (!is_type_valid(type_left))
3085 assert(is_type_pointer(type_left));
3086 return type_left->pointer.points_to;
3093 return expression->base.datatype;
3096 static expression_t *parse_reference(void)
3098 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3100 reference_expression_t *ref = &expression->reference;
3101 ref->symbol = token.v.symbol;
3103 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3105 source_position_t source_position = token.source_position;
3108 if(declaration == NULL) {
3109 if (! strict_mode && token.type == '(') {
3110 /* an implicitly defined function */
3111 warningf(HERE, "implicit declaration of function '%Y'",
3114 declaration = create_implicit_function(ref->symbol,
3117 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3122 type_t *type = declaration->type;
3124 /* we always do the auto-type conversions; the & and sizeof parser contains
3125 * code to revert this! */
3126 type = automatic_type_conversion(type);
3128 ref->declaration = declaration;
3129 ref->expression.datatype = type;
3134 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3138 /* TODO check if explicit cast is allowed and issue warnings/errors */
3141 static expression_t *parse_cast(void)
3143 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3145 cast->base.source_position = token.source_position;
3147 type_t *type = parse_typename();
3150 expression_t *value = parse_sub_expression(20);
3152 check_cast_allowed(value, type);
3154 cast->base.datatype = type;
3155 cast->unary.value = value;
3160 static expression_t *parse_statement_expression(void)
3162 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3164 statement_t *statement = parse_compound_statement();
3165 expression->statement.statement = statement;
3166 if(statement == NULL) {
3171 assert(statement->kind == STATEMENT_COMPOUND);
3172 compound_statement_t *compound_statement = &statement->compound;
3174 /* find last statement and use it's type */
3175 const statement_t *last_statement = NULL;
3176 const statement_t *iter = compound_statement->statements;
3177 for( ; iter != NULL; iter = iter->base.next) {
3178 last_statement = iter;
3181 if(last_statement->kind == STATEMENT_EXPRESSION) {
3182 const expression_statement_t *expression_statement
3183 = &last_statement->expression;
3184 expression->base.datatype
3185 = expression_statement->expression->base.datatype;
3187 expression->base.datatype = type_void;
3195 static expression_t *parse_brace_expression(void)
3199 switch(token.type) {
3201 /* gcc extension: a statement expression */
3202 return parse_statement_expression();
3206 return parse_cast();
3208 if(is_typedef_symbol(token.v.symbol)) {
3209 return parse_cast();
3213 expression_t *result = parse_expression();
3219 static expression_t *parse_function_keyword(void)
3224 if (current_function == NULL) {
3225 errorf(HERE, "'__func__' used outside of a function");
3228 string_literal_expression_t *expression
3229 = allocate_ast_zero(sizeof(expression[0]));
3231 expression->expression.kind = EXPR_FUNCTION;
3232 expression->expression.datatype = type_string;
3234 return (expression_t*) expression;
3237 static expression_t *parse_pretty_function_keyword(void)
3239 eat(T___PRETTY_FUNCTION__);
3242 if (current_function == NULL) {
3243 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3246 string_literal_expression_t *expression
3247 = allocate_ast_zero(sizeof(expression[0]));
3249 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3250 expression->expression.datatype = type_string;
3252 return (expression_t*) expression;
3255 static designator_t *parse_designator(void)
3257 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3259 if(token.type != T_IDENTIFIER) {
3260 parse_error_expected("while parsing member designator",
3265 result->symbol = token.v.symbol;
3268 designator_t *last_designator = result;
3270 if(token.type == '.') {
3272 if(token.type != T_IDENTIFIER) {
3273 parse_error_expected("while parsing member designator",
3278 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3279 designator->symbol = token.v.symbol;
3282 last_designator->next = designator;
3283 last_designator = designator;
3286 if(token.type == '[') {
3288 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3289 designator->array_access = parse_expression();
3290 if(designator->array_access == NULL) {
3296 last_designator->next = designator;
3297 last_designator = designator;
3306 static expression_t *parse_offsetof(void)
3308 eat(T___builtin_offsetof);
3310 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3311 expression->base.datatype = type_size_t;
3314 expression->offsetofe.type = parse_typename();
3316 expression->offsetofe.designator = parse_designator();
3322 static expression_t *parse_va_start(void)
3324 eat(T___builtin_va_start);
3326 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3329 expression->va_starte.ap = parse_assignment_expression();
3331 expression_t *const expr = parse_assignment_expression();
3332 if (expr->kind == EXPR_REFERENCE) {
3333 declaration_t *const decl = expr->reference.declaration;
3334 if (decl->parent_context == ¤t_function->context &&
3335 decl->next == NULL) {
3336 expression->va_starte.parameter = decl;
3341 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3343 return create_invalid_expression();
3346 static expression_t *parse_va_arg(void)
3348 eat(T___builtin_va_arg);
3350 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3353 expression->va_arge.ap = parse_assignment_expression();
3355 expression->base.datatype = parse_typename();
3361 static expression_t *parse_builtin_symbol(void)
3363 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3365 symbol_t *symbol = token.v.symbol;
3367 expression->builtin_symbol.symbol = symbol;
3370 type_t *type = get_builtin_symbol_type(symbol);
3371 type = automatic_type_conversion(type);
3373 expression->base.datatype = type;
3377 static expression_t *parse_builtin_constant(void)
3379 eat(T___builtin_constant_p);
3381 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3384 expression->builtin_constant.value = parse_assignment_expression();
3386 expression->base.datatype = type_int;
3391 static expression_t *parse_builtin_prefetch(void)
3393 eat(T___builtin_prefetch);
3395 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3398 expression->builtin_prefetch.adr = parse_assignment_expression();
3399 if (token.type == ',') {
3401 expression->builtin_prefetch.rw = parse_assignment_expression();
3403 if (token.type == ',') {
3405 expression->builtin_prefetch.locality = parse_assignment_expression();
3408 expression->base.datatype = type_void;
3413 static expression_t *parse_compare_builtin(void)
3415 expression_t *expression;
3417 switch(token.type) {
3418 case T___builtin_isgreater:
3419 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3421 case T___builtin_isgreaterequal:
3422 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3424 case T___builtin_isless:
3425 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3427 case T___builtin_islessequal:
3428 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3430 case T___builtin_islessgreater:
3431 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3433 case T___builtin_isunordered:
3434 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3437 panic("invalid compare builtin found");
3443 expression->binary.left = parse_assignment_expression();
3445 expression->binary.right = parse_assignment_expression();
3448 type_t *const orig_type_left = expression->binary.left->base.datatype;
3449 type_t *const orig_type_right = expression->binary.right->base.datatype;
3451 type_t *const type_left = skip_typeref(orig_type_left);
3452 type_t *const type_right = skip_typeref(orig_type_right);
3453 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3454 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3455 type_error_incompatible("invalid operands in comparison",
3456 token.source_position, orig_type_left, orig_type_right);
3459 semantic_comparison(&expression->binary);
3465 static expression_t *parse_builtin_expect(void)
3467 eat(T___builtin_expect);
3469 expression_t *expression
3470 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3473 expression->binary.left = parse_assignment_expression();
3475 expression->binary.right = parse_constant_expression();
3478 expression->base.datatype = expression->binary.left->base.datatype;
3483 static expression_t *parse_assume(void) {
3486 expression_t *expression
3487 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3490 expression->unary.value = parse_assignment_expression();
3493 expression->base.datatype = type_void;
3497 static expression_t *parse_alignof(void) {
3500 expression_t *expression
3501 = allocate_expression_zero(EXPR_ALIGNOF);
3504 expression->alignofe.type = parse_typename();
3507 expression->base.datatype = type_size_t;
3511 static expression_t *parse_primary_expression(void)
3513 switch(token.type) {
3515 return parse_int_const();
3516 case T_FLOATINGPOINT:
3517 return parse_float_const();
3518 case T_STRING_LITERAL:
3519 return parse_string_const();
3520 case T_WIDE_STRING_LITERAL:
3521 return parse_wide_string_const();
3523 return parse_reference();
3524 case T___FUNCTION__:
3526 return parse_function_keyword();
3527 case T___PRETTY_FUNCTION__:
3528 return parse_pretty_function_keyword();
3529 case T___builtin_offsetof:
3530 return parse_offsetof();
3531 case T___builtin_va_start:
3532 return parse_va_start();
3533 case T___builtin_va_arg:
3534 return parse_va_arg();
3535 case T___builtin_expect:
3536 return parse_builtin_expect();
3537 case T___builtin_nanf:
3538 case T___builtin_alloca:
3539 case T___builtin_va_end:
3540 return parse_builtin_symbol();
3541 case T___builtin_isgreater:
3542 case T___builtin_isgreaterequal:
3543 case T___builtin_isless:
3544 case T___builtin_islessequal:
3545 case T___builtin_islessgreater:
3546 case T___builtin_isunordered:
3547 return parse_compare_builtin();
3548 case T___builtin_constant_p:
3549 return parse_builtin_constant();
3550 case T___builtin_prefetch:
3551 return parse_builtin_prefetch();
3553 return parse_alignof();
3555 return parse_assume();
3558 return parse_brace_expression();
3561 errorf(HERE, "unexpected token '%K'", &token);
3564 return create_invalid_expression();
3568 * Check if the expression has the character type and issue a warning then.
3570 static void check_for_char_index_type(const expression_t *expression) {
3571 type_t *type = expression->base.datatype;
3572 type_t *base_type = skip_typeref(type);
3574 if (base_type->base.kind == TYPE_ATOMIC) {
3575 switch (base_type->atomic.akind == ATOMIC_TYPE_CHAR) {
3576 warningf(expression->base.source_position,
3577 "array subscript has type '%T'", type);
3582 static expression_t *parse_array_expression(unsigned precedence,
3589 expression_t *inside = parse_expression();
3591 array_access_expression_t *array_access
3592 = allocate_ast_zero(sizeof(array_access[0]));
3594 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3596 type_t *const orig_type_left = left->base.datatype;
3597 type_t *const orig_type_inside = inside->base.datatype;
3599 type_t *const type_left = skip_typeref(orig_type_left);
3600 type_t *const type_inside = skip_typeref(orig_type_inside);
3602 type_t *return_type;
3603 if (is_type_pointer(type_left)) {
3604 pointer_type_t *const pointer = &type_left->pointer;
3605 return_type = pointer->points_to;
3606 array_access->array_ref = left;
3607 array_access->index = inside;
3608 check_for_char_index_type(inside);
3609 } else if (is_type_pointer(type_inside)) {
3610 pointer_type_t *const pointer = &type_inside->pointer;
3611 return_type = pointer->points_to;
3612 array_access->array_ref = inside;
3613 array_access->index = left;
3614 array_access->flipped = true;
3615 check_for_char_index_type(left);
3617 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3619 "array access on object with non-pointer types '%T', '%T'",
3620 orig_type_left, orig_type_inside);
3622 return_type = type_error_type;
3623 array_access->array_ref = create_invalid_expression();
3626 if(token.type != ']') {
3627 parse_error_expected("Problem while parsing array access", ']', 0);
3628 return (expression_t*) array_access;
3632 return_type = automatic_type_conversion(return_type);
3633 array_access->expression.datatype = return_type;
3635 return (expression_t*) array_access;
3638 static expression_t *parse_sizeof(unsigned precedence)
3642 sizeof_expression_t *sizeof_expression
3643 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3644 sizeof_expression->expression.kind = EXPR_SIZEOF;
3645 sizeof_expression->expression.datatype = type_size_t;
3647 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3649 sizeof_expression->type = parse_typename();
3652 expression_t *expression = parse_sub_expression(precedence);
3653 expression->base.datatype = revert_automatic_type_conversion(expression);
3655 sizeof_expression->type = expression->base.datatype;
3656 sizeof_expression->size_expression = expression;
3659 return (expression_t*) sizeof_expression;
3662 static expression_t *parse_select_expression(unsigned precedence,
3663 expression_t *compound)
3666 assert(token.type == '.' || token.type == T_MINUSGREATER);
3668 bool is_pointer = (token.type == T_MINUSGREATER);
3671 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3672 select->select.compound = compound;
3674 if(token.type != T_IDENTIFIER) {
3675 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3678 symbol_t *symbol = token.v.symbol;
3679 select->select.symbol = symbol;
3682 type_t *const orig_type = compound->base.datatype;
3683 type_t *const type = skip_typeref(orig_type);
3685 type_t *type_left = type;
3687 if (!is_type_pointer(type)) {
3688 if (is_type_valid(type)) {
3689 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3691 return create_invalid_expression();
3693 pointer_type_t *pointer_type = &type->pointer;
3694 type_left = pointer_type->points_to;
3696 type_left = skip_typeref(type_left);
3698 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3699 type_left->kind != TYPE_COMPOUND_UNION) {
3700 if (is_type_valid(type_left)) {
3701 errorf(HERE, "request for member '%Y' in something not a struct or "
3702 "union, but '%T'", symbol, type_left);
3704 return create_invalid_expression();
3707 compound_type_t *compound_type = &type_left->compound;
3708 declaration_t *declaration = compound_type->declaration;
3710 if(!declaration->init.is_defined) {
3711 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3713 return create_invalid_expression();
3716 declaration_t *iter = declaration->context.declarations;
3717 for( ; iter != NULL; iter = iter->next) {
3718 if(iter->symbol == symbol) {
3723 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3724 return create_invalid_expression();
3727 /* we always do the auto-type conversions; the & and sizeof parser contains
3728 * code to revert this! */
3729 type_t *expression_type = automatic_type_conversion(iter->type);
3731 select->select.compound_entry = iter;
3732 select->base.datatype = expression_type;
3734 if(expression_type->kind == TYPE_BITFIELD) {
3735 expression_t *extract
3736 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3737 extract->unary.value = select;
3738 extract->base.datatype = expression_type->bitfield.base;
3747 * Parse a call expression, ie. expression '( ... )'.
3749 * @param expression the function address
3751 static expression_t *parse_call_expression(unsigned precedence,
3752 expression_t *expression)
3755 expression_t *result = allocate_expression_zero(EXPR_CALL);
3757 call_expression_t *call = &result->call;
3758 call->function = expression;
3760 type_t *const orig_type = expression->base.datatype;
3761 type_t *const type = skip_typeref(orig_type);
3763 function_type_t *function_type = NULL;
3764 if (is_type_pointer(type)) {
3765 type_t *const to_type = skip_typeref(type->pointer.points_to);
3767 if (is_type_function(to_type)) {
3768 function_type = &to_type->function;
3769 call->expression.datatype = function_type->return_type;
3773 if (function_type == NULL && is_type_valid(type)) {
3774 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3777 /* parse arguments */
3780 if(token.type != ')') {
3781 call_argument_t *last_argument = NULL;
3784 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3786 argument->expression = parse_assignment_expression();
3787 if(last_argument == NULL) {
3788 call->arguments = argument;
3790 last_argument->next = argument;
3792 last_argument = argument;
3794 if(token.type != ',')
3801 if(function_type != NULL) {
3802 function_parameter_t *parameter = function_type->parameters;
3803 call_argument_t *argument = call->arguments;
3804 for( ; parameter != NULL && argument != NULL;
3805 parameter = parameter->next, argument = argument->next) {
3806 type_t *expected_type = parameter->type;
3807 /* TODO report context in error messages */
3808 argument->expression = create_implicit_cast(argument->expression,
3811 /* too few parameters */
3812 if(parameter != NULL) {
3813 errorf(HERE, "too few arguments to function '%E'", expression);
3814 } else if(argument != NULL) {
3815 /* too many parameters */
3816 if(!function_type->variadic
3817 && !function_type->unspecified_parameters) {
3818 errorf(HERE, "too many arguments to function '%E'", expression);
3820 /* do default promotion */
3821 for( ; argument != NULL; argument = argument->next) {
3822 type_t *type = argument->expression->base.datatype;
3824 type = skip_typeref(type);
3825 if(is_type_integer(type)) {
3826 type = promote_integer(type);
3827 } else if(type == type_float) {
3831 argument->expression
3832 = create_implicit_cast(argument->expression, type);
3835 check_format(&result->call);
3838 check_format(&result->call);
3845 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3847 static bool same_compound_type(const type_t *type1, const type_t *type2)
3849 if(!is_type_compound(type1))
3851 if(type1->kind != type2->kind)
3854 const compound_type_t *compound1 = &type1->compound;
3855 const compound_type_t *compound2 = &type2->compound;
3857 return compound1->declaration == compound2->declaration;
3861 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3863 * @param expression the conditional expression
3865 static expression_t *parse_conditional_expression(unsigned precedence,
3866 expression_t *expression)
3870 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3872 conditional_expression_t *conditional = &result->conditional;
3873 conditional->condition = expression;
3876 type_t *condition_type_orig = expression->base.datatype;
3877 if(is_type_valid(condition_type_orig)) {
3878 type_t *condition_type = skip_typeref(condition_type_orig);
3879 if(condition_type->kind != TYPE_ERROR && !is_type_scalar(condition_type)) {
3880 type_error("expected a scalar type in conditional condition",
3881 expression->base.source_position, condition_type_orig);
3885 expression_t *true_expression = parse_expression();
3887 expression_t *false_expression = parse_sub_expression(precedence);
3889 conditional->true_expression = true_expression;
3890 conditional->false_expression = false_expression;
3892 type_t *orig_true_type = true_expression->base.datatype;
3893 type_t *orig_false_type = false_expression->base.datatype;
3894 if(!is_type_valid(orig_true_type) || !is_type_valid(orig_false_type))
3897 type_t *true_type = skip_typeref(orig_true_type);
3898 type_t *false_type = skip_typeref(orig_false_type);
3901 type_t *result_type;
3902 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3903 result_type = semantic_arithmetic(true_type, false_type);
3905 true_expression = create_implicit_cast(true_expression, result_type);
3906 false_expression = create_implicit_cast(false_expression, result_type);
3908 conditional->true_expression = true_expression;
3909 conditional->false_expression = false_expression;
3910 conditional->expression.datatype = result_type;
3911 } else if (same_compound_type(true_type, false_type) || (
3912 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3913 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
3915 /* just take 1 of the 2 types */
3916 result_type = true_type;
3917 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3918 && pointers_compatible(true_type, false_type)) {
3920 result_type = true_type;
3923 if (is_type_valid(true_type) && is_type_valid(false_type)) {
3924 type_error_incompatible("while parsing conditional",
3925 expression->base.source_position, true_type,
3928 result_type = type_error_type;
3931 conditional->expression.datatype = result_type;
3936 * Parse an extension expression.
3938 static expression_t *parse_extension(unsigned precedence)
3940 eat(T___extension__);
3942 /* TODO enable extensions */
3943 expression_t *expression = parse_sub_expression(precedence);
3944 /* TODO disable extensions */
3948 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3950 eat(T___builtin_classify_type);
3952 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3953 result->base.datatype = type_int;
3956 expression_t *expression = parse_sub_expression(precedence);
3958 result->classify_type.type_expression = expression;
3963 static void semantic_incdec(unary_expression_t *expression)
3965 type_t *orig_type = expression->value->base.datatype;
3966 if(!is_type_valid(orig_type))
3969 type_t *type = skip_typeref(orig_type);
3970 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3971 /* TODO: improve error message */
3972 errorf(HERE, "operation needs an arithmetic or pointer type");
3976 expression->expression.datatype = orig_type;
3979 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3981 type_t *orig_type = expression->value->base.datatype;
3982 if(!is_type_valid(orig_type))
3985 type_t *type = skip_typeref(orig_type);
3986 if(!is_type_arithmetic(type)) {
3987 /* TODO: improve error message */
3988 errorf(HERE, "operation needs an arithmetic type");
3992 expression->expression.datatype = orig_type;
3995 static void semantic_unexpr_scalar(unary_expression_t *expression)
3997 type_t *orig_type = expression->value->base.datatype;
3998 if(!is_type_valid(orig_type))
4001 type_t *type = skip_typeref(orig_type);
4002 if (!is_type_scalar(type)) {
4003 errorf(HERE, "operand of ! must be of scalar type");
4007 expression->expression.datatype = orig_type;
4010 static void semantic_unexpr_integer(unary_expression_t *expression)
4012 type_t *orig_type = expression->value->base.datatype;
4013 if(!is_type_valid(orig_type))
4016 type_t *type = skip_typeref(orig_type);
4017 if (!is_type_integer(type)) {
4018 errorf(HERE, "operand of ~ must be of integer type");
4022 expression->expression.datatype = orig_type;
4025 static void semantic_dereference(unary_expression_t *expression)
4027 type_t *orig_type = expression->value->base.datatype;
4028 if(!is_type_valid(orig_type))
4031 type_t *type = skip_typeref(orig_type);
4032 if(!is_type_pointer(type)) {
4033 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4037 pointer_type_t *pointer_type = &type->pointer;
4038 type_t *result_type = pointer_type->points_to;
4040 result_type = automatic_type_conversion(result_type);
4041 expression->expression.datatype = result_type;
4045 * Check the semantic of the address taken expression.
4047 static void semantic_take_addr(unary_expression_t *expression)
4049 expression_t *value = expression->value;
4050 value->base.datatype = revert_automatic_type_conversion(value);
4052 type_t *orig_type = value->base.datatype;
4053 if(!is_type_valid(orig_type))
4056 if(value->kind == EXPR_REFERENCE) {
4057 reference_expression_t *reference = (reference_expression_t*) value;
4058 declaration_t *declaration = reference->declaration;
4059 if(declaration != NULL) {
4060 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4061 errorf(expression->expression.source_position,
4062 "address of register variable '%Y' requested",
4063 declaration->symbol);
4065 declaration->address_taken = 1;
4069 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4072 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4073 static expression_t *parse_##unexpression_type(unsigned precedence) \
4077 expression_t *unary_expression \
4078 = allocate_expression_zero(unexpression_type); \
4079 unary_expression->base.source_position = HERE; \
4080 unary_expression->unary.value = parse_sub_expression(precedence); \
4082 sfunc(&unary_expression->unary); \
4084 return unary_expression; \
4087 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4088 semantic_unexpr_arithmetic)
4089 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4090 semantic_unexpr_arithmetic)
4091 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4092 semantic_unexpr_scalar)
4093 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4094 semantic_dereference)
4095 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4097 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4098 semantic_unexpr_integer)
4099 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4101 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4104 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4106 static expression_t *parse_##unexpression_type(unsigned precedence, \
4107 expression_t *left) \
4109 (void) precedence; \
4112 expression_t *unary_expression \
4113 = allocate_expression_zero(unexpression_type); \
4114 unary_expression->unary.value = left; \
4116 sfunc(&unary_expression->unary); \
4118 return unary_expression; \
4121 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4122 EXPR_UNARY_POSTFIX_INCREMENT,
4124 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4125 EXPR_UNARY_POSTFIX_DECREMENT,
4128 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4130 /* TODO: handle complex + imaginary types */
4132 /* § 6.3.1.8 Usual arithmetic conversions */
4133 if(type_left == type_long_double || type_right == type_long_double) {
4134 return type_long_double;
4135 } else if(type_left == type_double || type_right == type_double) {
4137 } else if(type_left == type_float || type_right == type_float) {
4141 type_right = promote_integer(type_right);
4142 type_left = promote_integer(type_left);
4144 if(type_left == type_right)
4147 bool signed_left = is_type_signed(type_left);
4148 bool signed_right = is_type_signed(type_right);
4149 int rank_left = get_rank(type_left);
4150 int rank_right = get_rank(type_right);
4151 if(rank_left < rank_right) {
4152 if(signed_left == signed_right || !signed_right) {
4158 if(signed_left == signed_right || !signed_left) {
4167 * Check the semantic restrictions for a binary expression.
4169 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4171 expression_t *const left = expression->left;
4172 expression_t *const right = expression->right;
4173 type_t *const orig_type_left = left->base.datatype;
4174 type_t *const orig_type_right = right->base.datatype;
4175 type_t *const type_left = skip_typeref(orig_type_left);
4176 type_t *const type_right = skip_typeref(orig_type_right);
4178 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4179 /* TODO: improve error message */
4180 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4181 errorf(HERE, "operation needs arithmetic types");
4186 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4187 expression->left = create_implicit_cast(left, arithmetic_type);
4188 expression->right = create_implicit_cast(right, arithmetic_type);
4189 expression->expression.datatype = arithmetic_type;
4192 static void semantic_shift_op(binary_expression_t *expression)
4194 expression_t *const left = expression->left;
4195 expression_t *const right = expression->right;
4196 type_t *const orig_type_left = left->base.datatype;
4197 type_t *const orig_type_right = right->base.datatype;
4198 type_t * type_left = skip_typeref(orig_type_left);
4199 type_t * type_right = skip_typeref(orig_type_right);
4201 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4202 /* TODO: improve error message */
4203 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4204 errorf(HERE, "operation needs integer types");
4209 type_left = promote_integer(type_left);
4210 type_right = promote_integer(type_right);
4212 expression->left = create_implicit_cast(left, type_left);
4213 expression->right = create_implicit_cast(right, type_right);
4214 expression->expression.datatype = type_left;
4217 static void semantic_add(binary_expression_t *expression)
4219 expression_t *const left = expression->left;
4220 expression_t *const right = expression->right;
4221 type_t *const orig_type_left = left->base.datatype;
4222 type_t *const orig_type_right = right->base.datatype;
4223 type_t *const type_left = skip_typeref(orig_type_left);
4224 type_t *const type_right = skip_typeref(orig_type_right);
4227 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4228 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4229 expression->left = create_implicit_cast(left, arithmetic_type);
4230 expression->right = create_implicit_cast(right, arithmetic_type);
4231 expression->expression.datatype = arithmetic_type;
4233 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4234 expression->expression.datatype = type_left;
4235 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4236 expression->expression.datatype = type_right;
4237 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4238 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4242 static void semantic_sub(binary_expression_t *expression)
4244 expression_t *const left = expression->left;
4245 expression_t *const right = expression->right;
4246 type_t *const orig_type_left = left->base.datatype;
4247 type_t *const orig_type_right = right->base.datatype;
4248 type_t *const type_left = skip_typeref(orig_type_left);
4249 type_t *const type_right = skip_typeref(orig_type_right);
4252 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4253 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4254 expression->left = create_implicit_cast(left, arithmetic_type);
4255 expression->right = create_implicit_cast(right, arithmetic_type);
4256 expression->expression.datatype = arithmetic_type;
4258 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4259 expression->expression.datatype = type_left;
4260 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4261 if(!pointers_compatible(type_left, type_right)) {
4262 errorf(HERE, "pointers to incompatible objects to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4264 expression->expression.datatype = type_ptrdiff_t;
4266 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4267 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4271 static void semantic_comparison(binary_expression_t *expression)
4273 expression_t *left = expression->left;
4274 expression_t *right = expression->right;
4275 type_t *orig_type_left = left->base.datatype;
4276 type_t *orig_type_right = right->base.datatype;
4278 type_t *type_left = skip_typeref(orig_type_left);
4279 type_t *type_right = skip_typeref(orig_type_right);
4281 /* TODO non-arithmetic types */
4282 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4283 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4284 expression->left = create_implicit_cast(left, arithmetic_type);
4285 expression->right = create_implicit_cast(right, arithmetic_type);
4286 expression->expression.datatype = arithmetic_type;
4287 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4288 /* TODO check compatibility */
4289 } else if (is_type_pointer(type_left)) {
4290 expression->right = create_implicit_cast(right, type_left);
4291 } else if (is_type_pointer(type_right)) {
4292 expression->left = create_implicit_cast(left, type_right);
4293 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4294 type_error_incompatible("invalid operands in comparison",
4295 token.source_position, type_left, type_right);
4297 expression->expression.datatype = type_int;
4300 static void semantic_arithmetic_assign(binary_expression_t *expression)
4302 expression_t *left = expression->left;
4303 expression_t *right = expression->right;
4304 type_t *orig_type_left = left->base.datatype;
4305 type_t *orig_type_right = right->base.datatype;
4307 type_t *type_left = skip_typeref(orig_type_left);
4308 type_t *type_right = skip_typeref(orig_type_right);
4310 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4311 /* TODO: improve error message */
4312 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4313 errorf(HERE, "operation needs arithmetic types");
4318 /* combined instructions are tricky. We can't create an implicit cast on
4319 * the left side, because we need the uncasted form for the store.
4320 * The ast2firm pass has to know that left_type must be right_type
4321 * for the arithmetic operation and create a cast by itself */
4322 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4323 expression->right = create_implicit_cast(right, arithmetic_type);
4324 expression->expression.datatype = type_left;
4327 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4329 expression_t *const left = expression->left;
4330 expression_t *const right = expression->right;
4331 type_t *const orig_type_left = left->base.datatype;
4332 type_t *const orig_type_right = right->base.datatype;
4333 type_t *const type_left = skip_typeref(orig_type_left);
4334 type_t *const type_right = skip_typeref(orig_type_right);
4336 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4337 /* combined instructions are tricky. We can't create an implicit cast on
4338 * the left side, because we need the uncasted form for the store.
4339 * The ast2firm pass has to know that left_type must be right_type
4340 * for the arithmetic operation and create a cast by itself */
4341 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4342 expression->right = create_implicit_cast(right, arithmetic_type);
4343 expression->expression.datatype = type_left;
4344 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4345 expression->expression.datatype = type_left;
4346 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4347 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4352 * Check the semantic restrictions of a logical expression.
4354 static void semantic_logical_op(binary_expression_t *expression)
4356 expression_t *const left = expression->left;
4357 expression_t *const right = expression->right;
4358 type_t *const orig_type_left = left->base.datatype;
4359 type_t *const orig_type_right = right->base.datatype;
4360 type_t *const type_left = skip_typeref(orig_type_left);
4361 type_t *const type_right = skip_typeref(orig_type_right);
4363 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4364 /* TODO: improve error message */
4365 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4366 errorf(HERE, "operation needs scalar types");
4371 expression->expression.datatype = type_int;
4375 * Checks if a compound type has constant fields.
4377 static bool has_const_fields(const compound_type_t *type)
4379 const context_t *context = &type->declaration->context;
4380 const declaration_t *declaration = context->declarations;
4382 for (; declaration != NULL; declaration = declaration->next) {
4383 if (declaration->namespc != NAMESPACE_NORMAL)
4386 const type_t *decl_type = skip_typeref(declaration->type);
4387 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4395 * Check the semantic restrictions of a binary assign expression.
4397 static void semantic_binexpr_assign(binary_expression_t *expression)
4399 expression_t *left = expression->left;
4400 type_t *orig_type_left = left->base.datatype;
4402 type_t *type_left = revert_automatic_type_conversion(left);
4403 type_left = skip_typeref(orig_type_left);
4405 /* must be a modifiable lvalue */
4406 if (is_type_array(type_left)) {
4407 errorf(HERE, "cannot assign to arrays ('%E')", left);
4410 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4411 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4415 if(is_type_incomplete(type_left)) {
4417 "left-hand side of assignment '%E' has incomplete type '%T'",
4418 left, orig_type_left);
4421 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4422 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4423 left, orig_type_left);
4427 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4429 if (res_type == NULL) {
4430 errorf(expression->expression.source_position,
4431 "cannot assign to '%T' from '%T'",
4432 orig_type_left, expression->right->base.datatype);
4434 expression->right = create_implicit_cast(expression->right, res_type);
4437 expression->expression.datatype = orig_type_left;
4440 static void semantic_comma(binary_expression_t *expression)
4442 expression->expression.datatype = expression->right->base.datatype;
4445 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4446 static expression_t *parse_##binexpression_type(unsigned precedence, \
4447 expression_t *left) \
4451 expression_t *right = parse_sub_expression(precedence + lr); \
4453 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4454 binexpr->binary.left = left; \
4455 binexpr->binary.right = right; \
4456 sfunc(&binexpr->binary); \
4461 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4462 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4463 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4464 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4465 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4466 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4467 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4468 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4469 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4471 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4472 semantic_comparison, 1)
4473 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4474 semantic_comparison, 1)
4475 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4476 semantic_comparison, 1)
4477 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4478 semantic_comparison, 1)
4480 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4481 semantic_binexpr_arithmetic, 1)
4482 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4483 semantic_binexpr_arithmetic, 1)
4484 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4485 semantic_binexpr_arithmetic, 1)
4486 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4487 semantic_logical_op, 1)
4488 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4489 semantic_logical_op, 1)
4490 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4491 semantic_shift_op, 1)
4492 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4493 semantic_shift_op, 1)
4494 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4495 semantic_arithmetic_addsubb_assign, 0)
4496 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4497 semantic_arithmetic_addsubb_assign, 0)
4498 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4499 semantic_arithmetic_assign, 0)
4500 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4501 semantic_arithmetic_assign, 0)
4502 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4503 semantic_arithmetic_assign, 0)
4504 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4505 semantic_arithmetic_assign, 0)
4506 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4507 semantic_arithmetic_assign, 0)
4508 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4509 semantic_arithmetic_assign, 0)
4510 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4511 semantic_arithmetic_assign, 0)
4512 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4513 semantic_arithmetic_assign, 0)
4515 static expression_t *parse_sub_expression(unsigned precedence)
4517 if(token.type < 0) {
4518 return expected_expression_error();
4521 expression_parser_function_t *parser
4522 = &expression_parsers[token.type];
4523 source_position_t source_position = token.source_position;
4526 if(parser->parser != NULL) {
4527 left = parser->parser(parser->precedence);
4529 left = parse_primary_expression();
4531 assert(left != NULL);
4532 left->base.source_position = source_position;
4535 if(token.type < 0) {
4536 return expected_expression_error();
4539 parser = &expression_parsers[token.type];
4540 if(parser->infix_parser == NULL)
4542 if(parser->infix_precedence < precedence)
4545 left = parser->infix_parser(parser->infix_precedence, left);
4547 assert(left != NULL);
4548 assert(left->kind != EXPR_UNKNOWN);
4549 left->base.source_position = source_position;
4556 * Parse an expression.
4558 static expression_t *parse_expression(void)
4560 return parse_sub_expression(1);
4564 * Register a parser for a prefix-like operator with given precedence.
4566 * @param parser the parser function
4567 * @param token_type the token type of the prefix token
4568 * @param precedence the precedence of the operator
4570 static void register_expression_parser(parse_expression_function parser,
4571 int token_type, unsigned precedence)
4573 expression_parser_function_t *entry = &expression_parsers[token_type];
4575 if(entry->parser != NULL) {
4576 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4577 panic("trying to register multiple expression parsers for a token");
4579 entry->parser = parser;
4580 entry->precedence = precedence;
4584 * Register a parser for an infix operator with given precedence.
4586 * @param parser the parser function
4587 * @param token_type the token type of the infix operator
4588 * @param precedence the precedence of the operator
4590 static void register_infix_parser(parse_expression_infix_function parser,
4591 int token_type, unsigned precedence)
4593 expression_parser_function_t *entry = &expression_parsers[token_type];
4595 if(entry->infix_parser != NULL) {
4596 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4597 panic("trying to register multiple infix expression parsers for a "
4600 entry->infix_parser = parser;
4601 entry->infix_precedence = precedence;
4605 * Initialize the expression parsers.
4607 static void init_expression_parsers(void)
4609 memset(&expression_parsers, 0, sizeof(expression_parsers));
4611 register_infix_parser(parse_array_expression, '[', 30);
4612 register_infix_parser(parse_call_expression, '(', 30);
4613 register_infix_parser(parse_select_expression, '.', 30);
4614 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4615 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4617 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4620 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4621 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4622 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4623 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4624 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4625 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4626 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4627 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4628 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4629 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4630 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4631 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4632 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4633 T_EXCLAMATIONMARKEQUAL, 13);
4634 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4635 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4636 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4637 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4638 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4639 register_infix_parser(parse_conditional_expression, '?', 7);
4640 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4641 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4642 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4643 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4644 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4645 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4646 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4647 T_LESSLESSEQUAL, 2);
4648 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4649 T_GREATERGREATEREQUAL, 2);
4650 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4652 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4654 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4657 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4659 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4660 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4661 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4662 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4663 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4664 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4665 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4667 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4669 register_expression_parser(parse_sizeof, T_sizeof, 25);
4670 register_expression_parser(parse_extension, T___extension__, 25);
4671 register_expression_parser(parse_builtin_classify_type,
4672 T___builtin_classify_type, 25);
4676 * Parse a asm statement constraints specification.
4678 static asm_constraint_t *parse_asm_constraints(void)
4680 asm_constraint_t *result = NULL;
4681 asm_constraint_t *last = NULL;
4683 while(token.type == T_STRING_LITERAL || token.type == '[') {
4684 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4685 memset(constraint, 0, sizeof(constraint[0]));
4687 if(token.type == '[') {
4689 if(token.type != T_IDENTIFIER) {
4690 parse_error_expected("while parsing asm constraint",
4694 constraint->symbol = token.v.symbol;
4699 constraint->constraints = parse_string_literals();
4701 constraint->expression = parse_expression();
4705 last->next = constraint;
4707 result = constraint;
4711 if(token.type != ',')
4720 * Parse a asm statement clobber specification.
4722 static asm_clobber_t *parse_asm_clobbers(void)
4724 asm_clobber_t *result = NULL;
4725 asm_clobber_t *last = NULL;
4727 while(token.type == T_STRING_LITERAL) {
4728 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4729 clobber->clobber = parse_string_literals();
4732 last->next = clobber;
4738 if(token.type != ',')
4747 * Parse an asm statement.
4749 static statement_t *parse_asm_statement(void)
4753 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4754 statement->base.source_position = token.source_position;
4756 asm_statement_t *asm_statement = &statement->asms;
4758 if(token.type == T_volatile) {
4760 asm_statement->is_volatile = true;
4764 asm_statement->asm_text = parse_string_literals();
4766 if(token.type != ':')
4770 asm_statement->inputs = parse_asm_constraints();
4771 if(token.type != ':')
4775 asm_statement->outputs = parse_asm_constraints();
4776 if(token.type != ':')
4780 asm_statement->clobbers = parse_asm_clobbers();
4789 * Parse a case statement.
4791 static statement_t *parse_case_statement(void)
4795 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4797 statement->base.source_position = token.source_position;
4798 statement->case_label.expression = parse_expression();
4802 if (! is_constant_expression(statement->case_label.expression)) {
4803 errorf(statement->base.source_position,
4804 "case label does not reduce to an integer constant");
4806 /* TODO: check if the case label is already known */
4807 if (current_switch != NULL) {
4808 /* link all cases into the switch statement */
4809 if (current_switch->last_case == NULL) {
4810 current_switch->first_case =
4811 current_switch->last_case = &statement->case_label;
4813 current_switch->last_case->next = &statement->case_label;
4816 errorf(statement->base.source_position,
4817 "case label not within a switch statement");
4820 statement->case_label.label_statement = parse_statement();
4826 * Finds an existing default label of a switch statement.
4828 static case_label_statement_t *
4829 find_default_label(const switch_statement_t *statement)
4831 for (case_label_statement_t *label = statement->first_case;
4833 label = label->next) {
4834 if (label->expression == NULL)
4841 * Parse a default statement.
4843 static statement_t *parse_default_statement(void)
4847 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4849 statement->base.source_position = token.source_position;
4852 if (current_switch != NULL) {
4853 const case_label_statement_t *def_label = find_default_label(current_switch);
4854 if (def_label != NULL) {
4855 errorf(HERE, "multiple default labels in one switch");
4856 errorf(def_label->statement.source_position,
4857 "this is the first default label");
4859 /* link all cases into the switch statement */
4860 if (current_switch->last_case == NULL) {
4861 current_switch->first_case =
4862 current_switch->last_case = &statement->case_label;
4864 current_switch->last_case->next = &statement->case_label;
4868 errorf(statement->base.source_position,
4869 "'default' label not within a switch statement");
4871 statement->label.label_statement = parse_statement();
4877 * Return the declaration for a given label symbol or create a new one.
4879 static declaration_t *get_label(symbol_t *symbol)
4881 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4882 assert(current_function != NULL);
4883 /* if we found a label in the same function, then we already created the
4885 if(candidate != NULL
4886 && candidate->parent_context == ¤t_function->context) {
4890 /* otherwise we need to create a new one */
4891 declaration_t *const declaration = allocate_declaration_zero();
4892 declaration->namespc = NAMESPACE_LABEL;
4893 declaration->symbol = symbol;
4895 label_push(declaration);
4901 * Parse a label statement.
4903 static statement_t *parse_label_statement(void)
4905 assert(token.type == T_IDENTIFIER);
4906 symbol_t *symbol = token.v.symbol;
4909 declaration_t *label = get_label(symbol);
4911 /* if source position is already set then the label is defined twice,
4912 * otherwise it was just mentioned in a goto so far */
4913 if(label->source_position.input_name != NULL) {
4914 errorf(HERE, "duplicate label '%Y'", symbol);
4915 errorf(label->source_position, "previous definition of '%Y' was here",
4918 label->source_position = token.source_position;
4921 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4923 label_statement->statement.kind = STATEMENT_LABEL;
4924 label_statement->statement.source_position = token.source_position;
4925 label_statement->label = label;
4929 if(token.type == '}') {
4930 /* TODO only warn? */
4931 errorf(HERE, "label at end of compound statement");
4932 return (statement_t*) label_statement;
4934 label_statement->label_statement = parse_statement();
4937 return (statement_t*) label_statement;
4941 * Parse an if statement.
4943 static statement_t *parse_if(void)
4947 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4948 statement->statement.kind = STATEMENT_IF;
4949 statement->statement.source_position = token.source_position;
4952 statement->condition = parse_expression();
4955 statement->true_statement = parse_statement();
4956 if(token.type == T_else) {
4958 statement->false_statement = parse_statement();
4961 return (statement_t*) statement;
4965 * Parse a switch statement.
4967 static statement_t *parse_switch(void)
4971 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4972 statement->statement.kind = STATEMENT_SWITCH;
4973 statement->statement.source_position = token.source_position;
4976 expression_t *const expr = parse_expression();
4977 type_t * type = skip_typeref(expr->base.datatype);
4978 if (is_type_integer(type)) {
4979 type = promote_integer(type);
4980 } else if (is_type_valid(type)) {
4981 errorf(expr->base.source_position, "switch quantity is not an integer, but '%T'", type);
4982 type = type_error_type;
4984 statement->expression = create_implicit_cast(expr, type);
4987 switch_statement_t *rem = current_switch;
4988 current_switch = statement;
4989 statement->body = parse_statement();
4990 current_switch = rem;
4992 return (statement_t*) statement;
4995 static statement_t *parse_loop_body(statement_t *const loop)
4997 statement_t *const rem = current_loop;
4998 current_loop = loop;
4999 statement_t *const body = parse_statement();
5005 * Parse a while statement.
5007 static statement_t *parse_while(void)
5011 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5012 statement->statement.kind = STATEMENT_WHILE;
5013 statement->statement.source_position = token.source_position;
5016 statement->condition = parse_expression();
5019 statement->body = parse_loop_body((statement_t*)statement);
5021 return (statement_t*) statement;
5025 * Parse a do statement.
5027 static statement_t *parse_do(void)
5031 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5032 statement->statement.kind = STATEMENT_DO_WHILE;
5033 statement->statement.source_position = token.source_position;
5035 statement->body = parse_loop_body((statement_t*)statement);
5038 statement->condition = parse_expression();
5042 return (statement_t*) statement;
5046 * Parse a for statement.
5048 static statement_t *parse_for(void)
5052 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5053 statement->statement.kind = STATEMENT_FOR;
5054 statement->statement.source_position = token.source_position;
5058 int top = environment_top();
5059 context_t *last_context = context;
5060 set_context(&statement->context);
5062 if(token.type != ';') {
5063 if(is_declaration_specifier(&token, false)) {
5064 parse_declaration(record_declaration);
5066 statement->initialisation = parse_expression();
5073 if(token.type != ';') {
5074 statement->condition = parse_expression();
5077 if(token.type != ')') {
5078 statement->step = parse_expression();
5081 statement->body = parse_loop_body((statement_t*)statement);
5083 assert(context == &statement->context);
5084 set_context(last_context);
5085 environment_pop_to(top);
5087 return (statement_t*) statement;
5091 * Parse a goto statement.
5093 static statement_t *parse_goto(void)
5097 if(token.type != T_IDENTIFIER) {
5098 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5102 symbol_t *symbol = token.v.symbol;
5105 declaration_t *label = get_label(symbol);
5107 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5109 statement->statement.kind = STATEMENT_GOTO;
5110 statement->statement.source_position = token.source_position;
5112 statement->label = label;
5114 /* remember the goto's in a list for later checking */
5115 if (goto_last == NULL) {
5116 goto_first = goto_last = statement;
5118 goto_last->next = statement;
5123 return (statement_t*) statement;
5127 * Parse a continue statement.
5129 static statement_t *parse_continue(void)
5131 statement_t *statement;
5132 if (current_loop == NULL) {
5133 errorf(HERE, "continue statement not within loop");
5136 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5138 statement->base.source_position = token.source_position;
5148 * Parse a break statement.
5150 static statement_t *parse_break(void)
5152 statement_t *statement;
5153 if (current_switch == NULL && current_loop == NULL) {
5154 errorf(HERE, "break statement not within loop or switch");
5157 statement = allocate_statement_zero(STATEMENT_BREAK);
5159 statement->base.source_position = token.source_position;
5169 * Check if a given declaration represents a local variable.
5171 static bool is_local_var_declaration(const declaration_t *declaration) {
5172 switch ((storage_class_tag_t) declaration->storage_class) {
5173 case STORAGE_CLASS_NONE:
5174 case STORAGE_CLASS_AUTO:
5175 case STORAGE_CLASS_REGISTER: {
5176 const type_t *type = skip_typeref(declaration->type);
5177 if(is_type_function(type)) {
5189 * Check if a given expression represents a local variable.
5191 static bool is_local_variable(const expression_t *expression)
5193 if (expression->base.kind != EXPR_REFERENCE) {
5196 const declaration_t *declaration = expression->reference.declaration;
5197 return is_local_var_declaration(declaration);
5201 * Parse a return statement.
5203 static statement_t *parse_return(void)
5207 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5209 statement->statement.kind = STATEMENT_RETURN;
5210 statement->statement.source_position = token.source_position;
5212 assert(is_type_function(current_function->type));
5213 function_type_t *function_type = ¤t_function->type->function;
5214 type_t *return_type = function_type->return_type;
5216 expression_t *return_value = NULL;
5217 if(token.type != ';') {
5218 return_value = parse_expression();
5222 return_type = skip_typeref(return_type);
5224 if(return_value != NULL) {
5225 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5227 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5228 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5229 warningf(statement->statement.source_position,
5230 "'return' with a value, in function returning void");
5231 return_value = NULL;
5233 type_t *const res_type = semantic_assign(return_type,
5234 return_value, "'return'");
5235 if (res_type == NULL) {
5236 errorf(statement->statement.source_position,
5237 "cannot return something of type '%T' in function returning '%T'",
5238 return_value->base.datatype, return_type);
5240 return_value = create_implicit_cast(return_value, res_type);
5243 /* check for returning address of a local var */
5244 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5245 const expression_t *expression = return_value->unary.value;
5246 if (is_local_variable(expression)) {
5247 warningf(statement->statement.source_position,
5248 "function returns address of local variable");
5252 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5253 warningf(statement->statement.source_position,
5254 "'return' without value, in function returning non-void");
5257 statement->return_value = return_value;
5259 return (statement_t*) statement;
5263 * Parse a declaration statement.
5265 static statement_t *parse_declaration_statement(void)
5267 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5269 statement->base.source_position = token.source_position;
5271 declaration_t *before = last_declaration;
5272 parse_declaration(record_declaration);
5274 if(before == NULL) {
5275 statement->declaration.declarations_begin = context->declarations;
5277 statement->declaration.declarations_begin = before->next;
5279 statement->declaration.declarations_end = last_declaration;
5285 * Parse an expression statement, ie. expr ';'.
5287 static statement_t *parse_expression_statement(void)
5289 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5291 statement->base.source_position = token.source_position;
5292 statement->expression.expression = parse_expression();
5300 * Parse a statement.
5302 static statement_t *parse_statement(void)
5304 statement_t *statement = NULL;
5306 /* declaration or statement */
5307 switch(token.type) {
5309 statement = parse_asm_statement();
5313 statement = parse_case_statement();
5317 statement = parse_default_statement();
5321 statement = parse_compound_statement();
5325 statement = parse_if();
5329 statement = parse_switch();
5333 statement = parse_while();
5337 statement = parse_do();
5341 statement = parse_for();
5345 statement = parse_goto();
5349 statement = parse_continue();
5353 statement = parse_break();
5357 statement = parse_return();
5366 if(look_ahead(1)->type == ':') {
5367 statement = parse_label_statement();
5371 if(is_typedef_symbol(token.v.symbol)) {
5372 statement = parse_declaration_statement();
5376 statement = parse_expression_statement();
5379 case T___extension__:
5380 /* this can be a prefix to a declaration or an expression statement */
5381 /* we simply eat it now and parse the rest with tail recursion */
5384 } while(token.type == T___extension__);
5385 statement = parse_statement();
5389 statement = parse_declaration_statement();
5393 statement = parse_expression_statement();
5397 assert(statement == NULL
5398 || statement->base.source_position.input_name != NULL);
5404 * Parse a compound statement.
5406 static statement_t *parse_compound_statement(void)
5408 compound_statement_t *compound_statement
5409 = allocate_ast_zero(sizeof(compound_statement[0]));
5410 compound_statement->statement.kind = STATEMENT_COMPOUND;
5411 compound_statement->statement.source_position = token.source_position;
5415 int top = environment_top();
5416 context_t *last_context = context;
5417 set_context(&compound_statement->context);
5419 statement_t *last_statement = NULL;
5421 while(token.type != '}' && token.type != T_EOF) {
5422 statement_t *statement = parse_statement();
5423 if(statement == NULL)
5426 if(last_statement != NULL) {
5427 last_statement->base.next = statement;
5429 compound_statement->statements = statement;
5432 while(statement->base.next != NULL)
5433 statement = statement->base.next;
5435 last_statement = statement;
5438 if(token.type == '}') {
5441 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5444 assert(context == &compound_statement->context);
5445 set_context(last_context);
5446 environment_pop_to(top);
5448 return (statement_t*) compound_statement;
5452 * Initialize builtin types.
5454 static void initialize_builtin_types(void)
5456 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5457 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5458 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5459 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5460 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5461 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5462 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5463 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5465 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5466 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5467 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5468 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5472 * Parse a translation unit.
5474 static translation_unit_t *parse_translation_unit(void)
5476 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5478 assert(global_context == NULL);
5479 global_context = &unit->context;
5481 assert(context == NULL);
5482 set_context(&unit->context);
5484 initialize_builtin_types();
5486 while(token.type != T_EOF) {
5487 if (token.type == ';') {
5488 /* TODO error in strict mode */
5489 warningf(HERE, "stray ';' outside of function");
5492 parse_external_declaration();
5496 assert(context == &unit->context);
5498 last_declaration = NULL;
5500 assert(global_context == &unit->context);
5501 global_context = NULL;
5509 * @return the translation unit or NULL if errors occurred.
5511 translation_unit_t *parse(void)
5513 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5514 label_stack = NEW_ARR_F(stack_entry_t, 0);
5515 diagnostic_count = 0;
5519 type_set_output(stderr);
5520 ast_set_output(stderr);
5522 lookahead_bufpos = 0;
5523 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5526 translation_unit_t *unit = parse_translation_unit();
5528 DEL_ARR_F(environment_stack);
5529 DEL_ARR_F(label_stack);
5538 * Initialize the parser.
5540 void init_parser(void)
5542 init_expression_parsers();
5543 obstack_init(&temp_obst);
5545 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5546 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5550 * Terminate the parser.
5552 void exit_parser(void)
5554 obstack_free(&temp_obst, NULL);