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 (type->kind != TYPE_FUNCTION &&
2502 declaration->is_inline &&
2503 is_type_valid(type)) {
2504 warningf(declaration->source_position,
2505 "variable '%Y' declared 'inline'\n", declaration->symbol);
2508 if(token.type == '=') {
2509 parse_init_declarator_rest(declaration);
2512 if(token.type != ',')
2516 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2521 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2523 symbol_t *symbol = declaration->symbol;
2524 if(symbol == NULL) {
2525 errorf(HERE, "anonymous declaration not valid as function parameter");
2528 namespace_t namespc = (namespace_t) declaration->namespc;
2529 if(namespc != NAMESPACE_NORMAL) {
2530 return record_declaration(declaration);
2533 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2534 if(previous_declaration == NULL ||
2535 previous_declaration->parent_context != context) {
2536 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2541 if(previous_declaration->type == NULL) {
2542 previous_declaration->type = declaration->type;
2543 previous_declaration->storage_class = declaration->storage_class;
2544 previous_declaration->parent_context = context;
2545 return previous_declaration;
2547 return record_declaration(declaration);
2551 static void parse_declaration(parsed_declaration_func finished_declaration)
2553 declaration_specifiers_t specifiers;
2554 memset(&specifiers, 0, sizeof(specifiers));
2555 parse_declaration_specifiers(&specifiers);
2557 if(token.type == ';') {
2558 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2560 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2561 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2565 static void parse_kr_declaration_list(declaration_t *declaration)
2567 type_t *type = skip_typeref(declaration->type);
2568 if(!is_type_function(type))
2571 if(!type->function.kr_style_parameters)
2574 /* push function parameters */
2575 int top = environment_top();
2576 context_t *last_context = context;
2577 set_context(&declaration->context);
2579 declaration_t *parameter = declaration->context.declarations;
2580 for( ; parameter != NULL; parameter = parameter->next) {
2581 assert(parameter->parent_context == NULL);
2582 parameter->parent_context = context;
2583 environment_push(parameter);
2586 /* parse declaration list */
2587 while(is_declaration_specifier(&token, false)) {
2588 parse_declaration(finished_kr_declaration);
2591 /* pop function parameters */
2592 assert(context == &declaration->context);
2593 set_context(last_context);
2594 environment_pop_to(top);
2596 /* update function type */
2597 type_t *new_type = duplicate_type(type);
2598 new_type->function.kr_style_parameters = false;
2600 function_parameter_t *parameters = NULL;
2601 function_parameter_t *last_parameter = NULL;
2603 declaration_t *parameter_declaration = declaration->context.declarations;
2604 for( ; parameter_declaration != NULL;
2605 parameter_declaration = parameter_declaration->next) {
2606 type_t *parameter_type = parameter_declaration->type;
2607 if(parameter_type == NULL) {
2609 errorf(HERE, "no type specified for function parameter '%Y'",
2610 parameter_declaration->symbol);
2612 warningf(HERE, "no type specified for function parameter '%Y', using int",
2613 parameter_declaration->symbol);
2614 parameter_type = type_int;
2615 parameter_declaration->type = parameter_type;
2619 semantic_parameter(parameter_declaration);
2620 parameter_type = parameter_declaration->type;
2622 function_parameter_t *function_parameter
2623 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2624 memset(function_parameter, 0, sizeof(function_parameter[0]));
2626 function_parameter->type = parameter_type;
2627 if(last_parameter != NULL) {
2628 last_parameter->next = function_parameter;
2630 parameters = function_parameter;
2632 last_parameter = function_parameter;
2634 new_type->function.parameters = parameters;
2636 type = typehash_insert(new_type);
2637 if(type != new_type) {
2638 obstack_free(type_obst, new_type);
2641 declaration->type = type;
2645 * Check if all labels are defined in the current function.
2647 static void check_for_missing_labels(void)
2649 bool first_err = true;
2650 for (const goto_statement_t *goto_statement = goto_first;
2651 goto_statement != NULL;
2652 goto_statement = goto_statement->next) {
2653 const declaration_t *label = goto_statement->label;
2655 if (label->source_position.input_name == NULL) {
2658 diagnosticf("%s: In function '%Y':\n",
2659 current_function->source_position.input_name,
2660 current_function->symbol);
2662 errorf(goto_statement->statement.source_position,
2663 "label '%Y' used but not defined", label->symbol);
2666 goto_first = goto_last = NULL;
2669 static void parse_external_declaration(void)
2671 /* function-definitions and declarations both start with declaration
2673 declaration_specifiers_t specifiers;
2674 memset(&specifiers, 0, sizeof(specifiers));
2675 parse_declaration_specifiers(&specifiers);
2677 /* must be a declaration */
2678 if(token.type == ';') {
2679 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2683 /* declarator is common to both function-definitions and declarations */
2684 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2686 /* must be a declaration */
2687 if(token.type == ',' || token.type == '=' || token.type == ';') {
2688 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2692 /* must be a function definition */
2693 parse_kr_declaration_list(ndeclaration);
2695 if(token.type != '{') {
2696 parse_error_expected("while parsing function definition", '{', 0);
2701 type_t *type = ndeclaration->type;
2703 /* note that we don't skip typerefs: the standard doesn't allow them here
2704 * (so we can't use is_type_function here) */
2705 if(type->kind != TYPE_FUNCTION) {
2706 if (is_type_valid(type)) {
2707 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2708 type, ndeclaration->symbol);
2714 /* § 6.7.5.3 (14) a function definition with () means no
2715 * parameters (and not unspecified parameters) */
2716 if(type->function.unspecified_parameters) {
2717 type_t *duplicate = duplicate_type(type);
2718 duplicate->function.unspecified_parameters = false;
2720 type = typehash_insert(duplicate);
2721 if(type != duplicate) {
2722 obstack_free(type_obst, duplicate);
2724 ndeclaration->type = type;
2727 declaration_t *const declaration = record_function_definition(ndeclaration);
2728 if(ndeclaration != declaration) {
2729 declaration->context = ndeclaration->context;
2731 type = skip_typeref(declaration->type);
2733 /* push function parameters and switch context */
2734 int top = environment_top();
2735 context_t *last_context = context;
2736 set_context(&declaration->context);
2738 declaration_t *parameter = declaration->context.declarations;
2739 for( ; parameter != NULL; parameter = parameter->next) {
2740 if(parameter->parent_context == &ndeclaration->context) {
2741 parameter->parent_context = context;
2743 assert(parameter->parent_context == NULL
2744 || parameter->parent_context == context);
2745 parameter->parent_context = context;
2746 environment_push(parameter);
2749 if(declaration->init.statement != NULL) {
2750 parser_error_multiple_definition(declaration, token.source_position);
2752 goto end_of_parse_external_declaration;
2754 /* parse function body */
2755 int label_stack_top = label_top();
2756 declaration_t *old_current_function = current_function;
2757 current_function = declaration;
2759 declaration->init.statement = parse_compound_statement();
2760 check_for_missing_labels();
2762 assert(current_function == declaration);
2763 current_function = old_current_function;
2764 label_pop_to(label_stack_top);
2767 end_of_parse_external_declaration:
2768 assert(context == &declaration->context);
2769 set_context(last_context);
2770 environment_pop_to(top);
2773 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2775 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2776 type->bitfield.base = base;
2777 type->bitfield.size = size;
2782 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2784 /* TODO: check constraints for struct declarations (in specifiers) */
2786 declaration_t *declaration;
2788 if(token.type == ':') {
2791 type_t *base_type = specifiers->type;
2792 expression_t *size = parse_constant_expression();
2794 type_t *type = make_bitfield_type(base_type, size);
2796 declaration = allocate_declaration_zero();
2797 declaration->namespc = NAMESPACE_NORMAL;
2798 declaration->storage_class = STORAGE_CLASS_NONE;
2799 declaration->source_position = token.source_position;
2800 declaration->modifiers = specifiers->decl_modifiers;
2801 declaration->type = type;
2803 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2805 if(token.type == ':') {
2807 expression_t *size = parse_constant_expression();
2809 type_t *type = make_bitfield_type(declaration->type, size);
2810 declaration->type = type;
2813 record_declaration(declaration);
2815 if(token.type != ',')
2822 static void parse_compound_type_entries(void)
2826 while(token.type != '}' && token.type != T_EOF) {
2827 declaration_specifiers_t specifiers;
2828 memset(&specifiers, 0, sizeof(specifiers));
2829 parse_declaration_specifiers(&specifiers);
2831 parse_struct_declarators(&specifiers);
2833 if(token.type == T_EOF) {
2834 errorf(HERE, "EOF while parsing struct");
2839 static type_t *parse_typename(void)
2841 declaration_specifiers_t specifiers;
2842 memset(&specifiers, 0, sizeof(specifiers));
2843 parse_declaration_specifiers(&specifiers);
2844 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2845 /* TODO: improve error message, user does probably not know what a
2846 * storage class is...
2848 errorf(HERE, "typename may not have a storage class");
2851 type_t *result = parse_abstract_declarator(specifiers.type);
2859 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2860 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2861 expression_t *left);
2863 typedef struct expression_parser_function_t expression_parser_function_t;
2864 struct expression_parser_function_t {
2865 unsigned precedence;
2866 parse_expression_function parser;
2867 unsigned infix_precedence;
2868 parse_expression_infix_function infix_parser;
2871 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2874 * Creates a new invalid expression.
2876 static expression_t *create_invalid_expression(void)
2878 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2879 expression->base.source_position = token.source_position;
2884 * Prints an error message if an expression was expected but not read
2886 static expression_t *expected_expression_error(void)
2888 /* skip the error message if the error token was read */
2889 if (token.type != T_ERROR) {
2890 errorf(HERE, "expected expression, got token '%K'", &token);
2894 return create_invalid_expression();
2898 * Parse a string constant.
2900 static expression_t *parse_string_const(void)
2902 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2903 cnst->base.datatype = type_string;
2904 cnst->string.value = parse_string_literals();
2910 * Parse a wide string constant.
2912 static expression_t *parse_wide_string_const(void)
2914 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2915 cnst->base.datatype = type_wchar_t_ptr;
2916 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2922 * Parse an integer constant.
2924 static expression_t *parse_int_const(void)
2926 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2927 cnst->base.datatype = token.datatype;
2928 cnst->conste.v.int_value = token.v.intvalue;
2936 * Parse a float constant.
2938 static expression_t *parse_float_const(void)
2940 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2941 cnst->base.datatype = token.datatype;
2942 cnst->conste.v.float_value = token.v.floatvalue;
2949 static declaration_t *create_implicit_function(symbol_t *symbol,
2950 const source_position_t source_position)
2952 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2953 ntype->function.return_type = type_int;
2954 ntype->function.unspecified_parameters = true;
2956 type_t *type = typehash_insert(ntype);
2961 declaration_t *const declaration = allocate_declaration_zero();
2962 declaration->storage_class = STORAGE_CLASS_EXTERN;
2963 declaration->type = type;
2964 declaration->symbol = symbol;
2965 declaration->source_position = source_position;
2966 declaration->parent_context = global_context;
2968 context_t *old_context = context;
2969 set_context(global_context);
2971 environment_push(declaration);
2972 /* prepend the declaration to the global declarations list */
2973 declaration->next = context->declarations;
2974 context->declarations = declaration;
2976 assert(context == global_context);
2977 set_context(old_context);
2983 * Creates a return_type (func)(argument_type) function type if not
2986 * @param return_type the return type
2987 * @param argument_type the argument type
2989 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2991 function_parameter_t *parameter
2992 = obstack_alloc(type_obst, sizeof(parameter[0]));
2993 memset(parameter, 0, sizeof(parameter[0]));
2994 parameter->type = argument_type;
2996 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2997 type->function.return_type = return_type;
2998 type->function.parameters = parameter;
3000 type_t *result = typehash_insert(type);
3001 if(result != type) {
3009 * Creates a function type for some function like builtins.
3011 * @param symbol the symbol describing the builtin
3013 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3015 switch(symbol->ID) {
3016 case T___builtin_alloca:
3017 return make_function_1_type(type_void_ptr, type_size_t);
3018 case T___builtin_nan:
3019 return make_function_1_type(type_double, type_string);
3020 case T___builtin_nanf:
3021 return make_function_1_type(type_float, type_string);
3022 case T___builtin_nand:
3023 return make_function_1_type(type_long_double, type_string);
3024 case T___builtin_va_end:
3025 return make_function_1_type(type_void, type_valist);
3027 panic("not implemented builtin symbol found");
3032 * Performs automatic type cast as described in § 6.3.2.1.
3034 * @param orig_type the original type
3036 static type_t *automatic_type_conversion(type_t *orig_type)
3038 type_t *type = skip_typeref(orig_type);
3039 if(is_type_array(type)) {
3040 array_type_t *array_type = &type->array;
3041 type_t *element_type = array_type->element_type;
3042 unsigned qualifiers = array_type->type.qualifiers;
3044 return make_pointer_type(element_type, qualifiers);
3047 if(is_type_function(type)) {
3048 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3055 * reverts the automatic casts of array to pointer types and function
3056 * to function-pointer types as defined § 6.3.2.1
3058 type_t *revert_automatic_type_conversion(const expression_t *expression)
3060 switch(expression->kind) {
3061 case EXPR_REFERENCE: {
3062 const reference_expression_t *ref = &expression->reference;
3063 return ref->declaration->type;
3066 const select_expression_t *select = &expression->select;
3067 return select->compound_entry->type;
3069 case EXPR_UNARY_DEREFERENCE: {
3070 expression_t *value = expression->unary.value;
3071 type_t *type = skip_typeref(value->base.datatype);
3072 pointer_type_t *pointer_type = &type->pointer;
3074 return pointer_type->points_to;
3076 case EXPR_BUILTIN_SYMBOL: {
3077 const builtin_symbol_expression_t *builtin
3078 = &expression->builtin_symbol;
3079 return get_builtin_symbol_type(builtin->symbol);
3081 case EXPR_ARRAY_ACCESS: {
3082 const expression_t *const array_ref = expression->array_access.array_ref;
3083 type_t *const type_left = skip_typeref(array_ref->base.datatype);
3084 if (!is_type_valid(type_left))
3086 assert(is_type_pointer(type_left));
3087 return type_left->pointer.points_to;
3094 return expression->base.datatype;
3097 static expression_t *parse_reference(void)
3099 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3101 reference_expression_t *ref = &expression->reference;
3102 ref->symbol = token.v.symbol;
3104 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3106 source_position_t source_position = token.source_position;
3109 if(declaration == NULL) {
3110 if (! strict_mode && token.type == '(') {
3111 /* an implicitly defined function */
3112 warningf(HERE, "implicit declaration of function '%Y'",
3115 declaration = create_implicit_function(ref->symbol,
3118 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3123 type_t *type = declaration->type;
3125 /* we always do the auto-type conversions; the & and sizeof parser contains
3126 * code to revert this! */
3127 type = automatic_type_conversion(type);
3129 ref->declaration = declaration;
3130 ref->expression.datatype = type;
3135 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3139 /* TODO check if explicit cast is allowed and issue warnings/errors */
3142 static expression_t *parse_cast(void)
3144 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3146 cast->base.source_position = token.source_position;
3148 type_t *type = parse_typename();
3151 expression_t *value = parse_sub_expression(20);
3153 check_cast_allowed(value, type);
3155 cast->base.datatype = type;
3156 cast->unary.value = value;
3161 static expression_t *parse_statement_expression(void)
3163 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3165 statement_t *statement = parse_compound_statement();
3166 expression->statement.statement = statement;
3167 if(statement == NULL) {
3172 assert(statement->kind == STATEMENT_COMPOUND);
3173 compound_statement_t *compound_statement = &statement->compound;
3175 /* find last statement and use it's type */
3176 const statement_t *last_statement = NULL;
3177 const statement_t *iter = compound_statement->statements;
3178 for( ; iter != NULL; iter = iter->base.next) {
3179 last_statement = iter;
3182 if(last_statement->kind == STATEMENT_EXPRESSION) {
3183 const expression_statement_t *expression_statement
3184 = &last_statement->expression;
3185 expression->base.datatype
3186 = expression_statement->expression->base.datatype;
3188 expression->base.datatype = type_void;
3196 static expression_t *parse_brace_expression(void)
3200 switch(token.type) {
3202 /* gcc extension: a statement expression */
3203 return parse_statement_expression();
3207 return parse_cast();
3209 if(is_typedef_symbol(token.v.symbol)) {
3210 return parse_cast();
3214 expression_t *result = parse_expression();
3220 static expression_t *parse_function_keyword(void)
3225 if (current_function == NULL) {
3226 errorf(HERE, "'__func__' used outside of a function");
3229 string_literal_expression_t *expression
3230 = allocate_ast_zero(sizeof(expression[0]));
3232 expression->expression.kind = EXPR_FUNCTION;
3233 expression->expression.datatype = type_string;
3235 return (expression_t*) expression;
3238 static expression_t *parse_pretty_function_keyword(void)
3240 eat(T___PRETTY_FUNCTION__);
3243 if (current_function == NULL) {
3244 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3247 string_literal_expression_t *expression
3248 = allocate_ast_zero(sizeof(expression[0]));
3250 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3251 expression->expression.datatype = type_string;
3253 return (expression_t*) expression;
3256 static designator_t *parse_designator(void)
3258 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3260 if(token.type != T_IDENTIFIER) {
3261 parse_error_expected("while parsing member designator",
3266 result->symbol = token.v.symbol;
3269 designator_t *last_designator = result;
3271 if(token.type == '.') {
3273 if(token.type != T_IDENTIFIER) {
3274 parse_error_expected("while parsing member designator",
3279 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3280 designator->symbol = token.v.symbol;
3283 last_designator->next = designator;
3284 last_designator = designator;
3287 if(token.type == '[') {
3289 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3290 designator->array_access = parse_expression();
3291 if(designator->array_access == NULL) {
3297 last_designator->next = designator;
3298 last_designator = designator;
3307 static expression_t *parse_offsetof(void)
3309 eat(T___builtin_offsetof);
3311 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3312 expression->base.datatype = type_size_t;
3315 expression->offsetofe.type = parse_typename();
3317 expression->offsetofe.designator = parse_designator();
3323 static expression_t *parse_va_start(void)
3325 eat(T___builtin_va_start);
3327 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3330 expression->va_starte.ap = parse_assignment_expression();
3332 expression_t *const expr = parse_assignment_expression();
3333 if (expr->kind == EXPR_REFERENCE) {
3334 declaration_t *const decl = expr->reference.declaration;
3335 if (decl->parent_context == ¤t_function->context &&
3336 decl->next == NULL) {
3337 expression->va_starte.parameter = decl;
3342 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3344 return create_invalid_expression();
3347 static expression_t *parse_va_arg(void)
3349 eat(T___builtin_va_arg);
3351 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3354 expression->va_arge.ap = parse_assignment_expression();
3356 expression->base.datatype = parse_typename();
3362 static expression_t *parse_builtin_symbol(void)
3364 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3366 symbol_t *symbol = token.v.symbol;
3368 expression->builtin_symbol.symbol = symbol;
3371 type_t *type = get_builtin_symbol_type(symbol);
3372 type = automatic_type_conversion(type);
3374 expression->base.datatype = type;
3378 static expression_t *parse_builtin_constant(void)
3380 eat(T___builtin_constant_p);
3382 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3385 expression->builtin_constant.value = parse_assignment_expression();
3387 expression->base.datatype = type_int;
3392 static expression_t *parse_builtin_prefetch(void)
3394 eat(T___builtin_prefetch);
3396 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3399 expression->builtin_prefetch.adr = parse_assignment_expression();
3400 if (token.type == ',') {
3402 expression->builtin_prefetch.rw = parse_assignment_expression();
3404 if (token.type == ',') {
3406 expression->builtin_prefetch.locality = parse_assignment_expression();
3409 expression->base.datatype = type_void;
3414 static expression_t *parse_compare_builtin(void)
3416 expression_t *expression;
3418 switch(token.type) {
3419 case T___builtin_isgreater:
3420 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3422 case T___builtin_isgreaterequal:
3423 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3425 case T___builtin_isless:
3426 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3428 case T___builtin_islessequal:
3429 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3431 case T___builtin_islessgreater:
3432 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3434 case T___builtin_isunordered:
3435 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3438 panic("invalid compare builtin found");
3444 expression->binary.left = parse_assignment_expression();
3446 expression->binary.right = parse_assignment_expression();
3449 type_t *const orig_type_left = expression->binary.left->base.datatype;
3450 type_t *const orig_type_right = expression->binary.right->base.datatype;
3452 type_t *const type_left = skip_typeref(orig_type_left);
3453 type_t *const type_right = skip_typeref(orig_type_right);
3454 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3455 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3456 type_error_incompatible("invalid operands in comparison",
3457 token.source_position, orig_type_left, orig_type_right);
3460 semantic_comparison(&expression->binary);
3466 static expression_t *parse_builtin_expect(void)
3468 eat(T___builtin_expect);
3470 expression_t *expression
3471 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3474 expression->binary.left = parse_assignment_expression();
3476 expression->binary.right = parse_constant_expression();
3479 expression->base.datatype = expression->binary.left->base.datatype;
3484 static expression_t *parse_assume(void) {
3487 expression_t *expression
3488 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3491 expression->unary.value = parse_assignment_expression();
3494 expression->base.datatype = type_void;
3498 static expression_t *parse_alignof(void) {
3501 expression_t *expression
3502 = allocate_expression_zero(EXPR_ALIGNOF);
3505 expression->alignofe.type = parse_typename();
3508 expression->base.datatype = type_size_t;
3512 static expression_t *parse_primary_expression(void)
3514 switch(token.type) {
3516 return parse_int_const();
3517 case T_FLOATINGPOINT:
3518 return parse_float_const();
3519 case T_STRING_LITERAL:
3520 return parse_string_const();
3521 case T_WIDE_STRING_LITERAL:
3522 return parse_wide_string_const();
3524 return parse_reference();
3525 case T___FUNCTION__:
3527 return parse_function_keyword();
3528 case T___PRETTY_FUNCTION__:
3529 return parse_pretty_function_keyword();
3530 case T___builtin_offsetof:
3531 return parse_offsetof();
3532 case T___builtin_va_start:
3533 return parse_va_start();
3534 case T___builtin_va_arg:
3535 return parse_va_arg();
3536 case T___builtin_expect:
3537 return parse_builtin_expect();
3538 case T___builtin_nanf:
3539 case T___builtin_alloca:
3540 case T___builtin_va_end:
3541 return parse_builtin_symbol();
3542 case T___builtin_isgreater:
3543 case T___builtin_isgreaterequal:
3544 case T___builtin_isless:
3545 case T___builtin_islessequal:
3546 case T___builtin_islessgreater:
3547 case T___builtin_isunordered:
3548 return parse_compare_builtin();
3549 case T___builtin_constant_p:
3550 return parse_builtin_constant();
3551 case T___builtin_prefetch:
3552 return parse_builtin_prefetch();
3554 return parse_alignof();
3556 return parse_assume();
3559 return parse_brace_expression();
3562 errorf(HERE, "unexpected token '%K'", &token);
3565 return create_invalid_expression();
3569 * Check if the expression has the character type and issue a warning then.
3571 static void check_for_char_index_type(const expression_t *expression) {
3572 type_t *type = expression->base.datatype;
3573 type_t *base_type = skip_typeref(type);
3575 if (base_type->base.kind == TYPE_ATOMIC) {
3576 switch (base_type->atomic.akind == ATOMIC_TYPE_CHAR) {
3577 warningf(expression->base.source_position,
3578 "array subscript has type '%T'", type);
3583 static expression_t *parse_array_expression(unsigned precedence,
3590 expression_t *inside = parse_expression();
3592 array_access_expression_t *array_access
3593 = allocate_ast_zero(sizeof(array_access[0]));
3595 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3597 type_t *const orig_type_left = left->base.datatype;
3598 type_t *const orig_type_inside = inside->base.datatype;
3600 type_t *const type_left = skip_typeref(orig_type_left);
3601 type_t *const type_inside = skip_typeref(orig_type_inside);
3603 type_t *return_type;
3604 if (is_type_pointer(type_left)) {
3605 pointer_type_t *const pointer = &type_left->pointer;
3606 return_type = pointer->points_to;
3607 array_access->array_ref = left;
3608 array_access->index = inside;
3609 check_for_char_index_type(inside);
3610 } else if (is_type_pointer(type_inside)) {
3611 pointer_type_t *const pointer = &type_inside->pointer;
3612 return_type = pointer->points_to;
3613 array_access->array_ref = inside;
3614 array_access->index = left;
3615 array_access->flipped = true;
3616 check_for_char_index_type(left);
3618 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3620 "array access on object with non-pointer types '%T', '%T'",
3621 orig_type_left, orig_type_inside);
3623 return_type = type_error_type;
3624 array_access->array_ref = create_invalid_expression();
3627 if(token.type != ']') {
3628 parse_error_expected("Problem while parsing array access", ']', 0);
3629 return (expression_t*) array_access;
3633 return_type = automatic_type_conversion(return_type);
3634 array_access->expression.datatype = return_type;
3636 return (expression_t*) array_access;
3639 static expression_t *parse_sizeof(unsigned precedence)
3643 sizeof_expression_t *sizeof_expression
3644 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3645 sizeof_expression->expression.kind = EXPR_SIZEOF;
3646 sizeof_expression->expression.datatype = type_size_t;
3648 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3650 sizeof_expression->type = parse_typename();
3653 expression_t *expression = parse_sub_expression(precedence);
3654 expression->base.datatype = revert_automatic_type_conversion(expression);
3656 sizeof_expression->type = expression->base.datatype;
3657 sizeof_expression->size_expression = expression;
3660 return (expression_t*) sizeof_expression;
3663 static expression_t *parse_select_expression(unsigned precedence,
3664 expression_t *compound)
3667 assert(token.type == '.' || token.type == T_MINUSGREATER);
3669 bool is_pointer = (token.type == T_MINUSGREATER);
3672 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3673 select->select.compound = compound;
3675 if(token.type != T_IDENTIFIER) {
3676 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3679 symbol_t *symbol = token.v.symbol;
3680 select->select.symbol = symbol;
3683 type_t *const orig_type = compound->base.datatype;
3684 type_t *const type = skip_typeref(orig_type);
3686 type_t *type_left = type;
3688 if (!is_type_pointer(type)) {
3689 if (is_type_valid(type)) {
3690 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3692 return create_invalid_expression();
3694 pointer_type_t *pointer_type = &type->pointer;
3695 type_left = pointer_type->points_to;
3697 type_left = skip_typeref(type_left);
3699 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3700 type_left->kind != TYPE_COMPOUND_UNION) {
3701 if (is_type_valid(type_left)) {
3702 errorf(HERE, "request for member '%Y' in something not a struct or "
3703 "union, but '%T'", symbol, type_left);
3705 return create_invalid_expression();
3708 compound_type_t *compound_type = &type_left->compound;
3709 declaration_t *declaration = compound_type->declaration;
3711 if(!declaration->init.is_defined) {
3712 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3714 return create_invalid_expression();
3717 declaration_t *iter = declaration->context.declarations;
3718 for( ; iter != NULL; iter = iter->next) {
3719 if(iter->symbol == symbol) {
3724 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3725 return create_invalid_expression();
3728 /* we always do the auto-type conversions; the & and sizeof parser contains
3729 * code to revert this! */
3730 type_t *expression_type = automatic_type_conversion(iter->type);
3732 select->select.compound_entry = iter;
3733 select->base.datatype = expression_type;
3735 if(expression_type->kind == TYPE_BITFIELD) {
3736 expression_t *extract
3737 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3738 extract->unary.value = select;
3739 extract->base.datatype = expression_type->bitfield.base;
3748 * Parse a call expression, ie. expression '( ... )'.
3750 * @param expression the function address
3752 static expression_t *parse_call_expression(unsigned precedence,
3753 expression_t *expression)
3756 expression_t *result = allocate_expression_zero(EXPR_CALL);
3758 call_expression_t *call = &result->call;
3759 call->function = expression;
3761 type_t *const orig_type = expression->base.datatype;
3762 type_t *const type = skip_typeref(orig_type);
3764 function_type_t *function_type = NULL;
3765 if (is_type_pointer(type)) {
3766 type_t *const to_type = skip_typeref(type->pointer.points_to);
3768 if (is_type_function(to_type)) {
3769 function_type = &to_type->function;
3770 call->expression.datatype = function_type->return_type;
3774 if (function_type == NULL && is_type_valid(type)) {
3775 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3778 /* parse arguments */
3781 if(token.type != ')') {
3782 call_argument_t *last_argument = NULL;
3785 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3787 argument->expression = parse_assignment_expression();
3788 if(last_argument == NULL) {
3789 call->arguments = argument;
3791 last_argument->next = argument;
3793 last_argument = argument;
3795 if(token.type != ',')
3802 if(function_type != NULL) {
3803 function_parameter_t *parameter = function_type->parameters;
3804 call_argument_t *argument = call->arguments;
3805 for( ; parameter != NULL && argument != NULL;
3806 parameter = parameter->next, argument = argument->next) {
3807 type_t *expected_type = parameter->type;
3808 /* TODO report context in error messages */
3809 expression_t *const arg_expr = argument->expression;
3810 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
3811 if (res_type == NULL) {
3812 /* TODO improve error message */
3813 errorf(arg_expr->base.source_position,
3814 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
3815 arg_expr, arg_expr->base.datatype, expected_type);
3817 argument->expression = create_implicit_cast(argument->expression, expected_type);
3820 /* too few parameters */
3821 if(parameter != NULL) {
3822 errorf(HERE, "too few arguments to function '%E'", expression);
3823 } else if(argument != NULL) {
3824 /* too many parameters */
3825 if(!function_type->variadic
3826 && !function_type->unspecified_parameters) {
3827 errorf(HERE, "too many arguments to function '%E'", expression);
3829 /* do default promotion */
3830 for( ; argument != NULL; argument = argument->next) {
3831 type_t *type = argument->expression->base.datatype;
3833 type = skip_typeref(type);
3834 if(is_type_integer(type)) {
3835 type = promote_integer(type);
3836 } else if(type == type_float) {
3840 argument->expression
3841 = create_implicit_cast(argument->expression, type);
3844 check_format(&result->call);
3847 check_format(&result->call);
3854 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3856 static bool same_compound_type(const type_t *type1, const type_t *type2)
3858 if(!is_type_compound(type1))
3860 if(type1->kind != type2->kind)
3863 const compound_type_t *compound1 = &type1->compound;
3864 const compound_type_t *compound2 = &type2->compound;
3866 return compound1->declaration == compound2->declaration;
3870 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3872 * @param expression the conditional expression
3874 static expression_t *parse_conditional_expression(unsigned precedence,
3875 expression_t *expression)
3879 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3881 conditional_expression_t *conditional = &result->conditional;
3882 conditional->condition = expression;
3885 type_t *const condition_type_orig = expression->base.datatype;
3886 type_t *const condition_type = skip_typeref(condition_type_orig);
3887 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
3888 type_error("expected a scalar type in conditional condition",
3889 expression->base.source_position, condition_type_orig);
3892 expression_t *true_expression = parse_expression();
3894 expression_t *false_expression = parse_sub_expression(precedence);
3896 conditional->true_expression = true_expression;
3897 conditional->false_expression = false_expression;
3899 type_t *const orig_true_type = true_expression->base.datatype;
3900 type_t *const orig_false_type = false_expression->base.datatype;
3901 type_t *const true_type = skip_typeref(orig_true_type);
3902 type_t *const false_type = skip_typeref(orig_false_type);
3905 type_t *result_type;
3906 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3907 result_type = semantic_arithmetic(true_type, false_type);
3909 true_expression = create_implicit_cast(true_expression, result_type);
3910 false_expression = create_implicit_cast(false_expression, result_type);
3912 conditional->true_expression = true_expression;
3913 conditional->false_expression = false_expression;
3914 conditional->expression.datatype = result_type;
3915 } else if (same_compound_type(true_type, false_type) || (
3916 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3917 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
3919 /* just take 1 of the 2 types */
3920 result_type = true_type;
3921 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3922 && pointers_compatible(true_type, false_type)) {
3924 result_type = true_type;
3927 if (is_type_valid(true_type) && is_type_valid(false_type)) {
3928 type_error_incompatible("while parsing conditional",
3929 expression->base.source_position, true_type,
3932 result_type = type_error_type;
3935 conditional->expression.datatype = result_type;
3940 * Parse an extension expression.
3942 static expression_t *parse_extension(unsigned precedence)
3944 eat(T___extension__);
3946 /* TODO enable extensions */
3947 expression_t *expression = parse_sub_expression(precedence);
3948 /* TODO disable extensions */
3952 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3954 eat(T___builtin_classify_type);
3956 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3957 result->base.datatype = type_int;
3960 expression_t *expression = parse_sub_expression(precedence);
3962 result->classify_type.type_expression = expression;
3967 static void semantic_incdec(unary_expression_t *expression)
3969 type_t *const orig_type = expression->value->base.datatype;
3970 type_t *const type = skip_typeref(orig_type);
3971 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3972 if (is_type_valid(type)) {
3973 /* TODO: improve error message */
3974 errorf(HERE, "operation needs an arithmetic or pointer type");
3979 expression->expression.datatype = orig_type;
3982 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3984 type_t *const orig_type = expression->value->base.datatype;
3985 type_t *const type = skip_typeref(orig_type);
3986 if(!is_type_arithmetic(type)) {
3987 if (is_type_valid(type)) {
3988 /* TODO: improve error message */
3989 errorf(HERE, "operation needs an arithmetic type");
3994 expression->expression.datatype = orig_type;
3997 static void semantic_unexpr_scalar(unary_expression_t *expression)
3999 type_t *const orig_type = expression->value->base.datatype;
4000 type_t *const type = skip_typeref(orig_type);
4001 if (!is_type_scalar(type)) {
4002 if (is_type_valid(type)) {
4003 errorf(HERE, "operand of ! must be of scalar type");
4008 expression->expression.datatype = orig_type;
4011 static void semantic_unexpr_integer(unary_expression_t *expression)
4013 type_t *const orig_type = expression->value->base.datatype;
4014 type_t *const type = skip_typeref(orig_type);
4015 if (!is_type_integer(type)) {
4016 if (is_type_valid(type)) {
4017 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 *const orig_type = expression->value->base.datatype;
4028 type_t *const type = skip_typeref(orig_type);
4029 if(!is_type_pointer(type)) {
4030 if (is_type_valid(type)) {
4031 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4036 pointer_type_t *pointer_type = &type->pointer;
4037 type_t *result_type = pointer_type->points_to;
4039 result_type = automatic_type_conversion(result_type);
4040 expression->expression.datatype = result_type;
4044 * Check the semantic of the address taken expression.
4046 static void semantic_take_addr(unary_expression_t *expression)
4048 expression_t *value = expression->value;
4049 value->base.datatype = revert_automatic_type_conversion(value);
4051 type_t *orig_type = value->base.datatype;
4052 if(!is_type_valid(orig_type))
4055 if(value->kind == EXPR_REFERENCE) {
4056 reference_expression_t *reference = (reference_expression_t*) value;
4057 declaration_t *declaration = reference->declaration;
4058 if(declaration != NULL) {
4059 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4060 errorf(expression->expression.source_position,
4061 "address of register variable '%Y' requested",
4062 declaration->symbol);
4064 declaration->address_taken = 1;
4068 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4071 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4072 static expression_t *parse_##unexpression_type(unsigned precedence) \
4076 expression_t *unary_expression \
4077 = allocate_expression_zero(unexpression_type); \
4078 unary_expression->base.source_position = HERE; \
4079 unary_expression->unary.value = parse_sub_expression(precedence); \
4081 sfunc(&unary_expression->unary); \
4083 return unary_expression; \
4086 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4087 semantic_unexpr_arithmetic)
4088 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4089 semantic_unexpr_arithmetic)
4090 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4091 semantic_unexpr_scalar)
4092 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4093 semantic_dereference)
4094 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4096 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4097 semantic_unexpr_integer)
4098 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4100 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4103 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4105 static expression_t *parse_##unexpression_type(unsigned precedence, \
4106 expression_t *left) \
4108 (void) precedence; \
4111 expression_t *unary_expression \
4112 = allocate_expression_zero(unexpression_type); \
4113 unary_expression->unary.value = left; \
4115 sfunc(&unary_expression->unary); \
4117 return unary_expression; \
4120 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4121 EXPR_UNARY_POSTFIX_INCREMENT,
4123 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4124 EXPR_UNARY_POSTFIX_DECREMENT,
4127 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4129 /* TODO: handle complex + imaginary types */
4131 /* § 6.3.1.8 Usual arithmetic conversions */
4132 if(type_left == type_long_double || type_right == type_long_double) {
4133 return type_long_double;
4134 } else if(type_left == type_double || type_right == type_double) {
4136 } else if(type_left == type_float || type_right == type_float) {
4140 type_right = promote_integer(type_right);
4141 type_left = promote_integer(type_left);
4143 if(type_left == type_right)
4146 bool signed_left = is_type_signed(type_left);
4147 bool signed_right = is_type_signed(type_right);
4148 int rank_left = get_rank(type_left);
4149 int rank_right = get_rank(type_right);
4150 if(rank_left < rank_right) {
4151 if(signed_left == signed_right || !signed_right) {
4157 if(signed_left == signed_right || !signed_left) {
4166 * Check the semantic restrictions for a binary expression.
4168 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4170 expression_t *const left = expression->left;
4171 expression_t *const right = expression->right;
4172 type_t *const orig_type_left = left->base.datatype;
4173 type_t *const orig_type_right = right->base.datatype;
4174 type_t *const type_left = skip_typeref(orig_type_left);
4175 type_t *const type_right = skip_typeref(orig_type_right);
4177 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4178 /* TODO: improve error message */
4179 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4180 errorf(HERE, "operation needs arithmetic types");
4185 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4186 expression->left = create_implicit_cast(left, arithmetic_type);
4187 expression->right = create_implicit_cast(right, arithmetic_type);
4188 expression->expression.datatype = arithmetic_type;
4191 static void semantic_shift_op(binary_expression_t *expression)
4193 expression_t *const left = expression->left;
4194 expression_t *const right = expression->right;
4195 type_t *const orig_type_left = left->base.datatype;
4196 type_t *const orig_type_right = right->base.datatype;
4197 type_t * type_left = skip_typeref(orig_type_left);
4198 type_t * type_right = skip_typeref(orig_type_right);
4200 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4201 /* TODO: improve error message */
4202 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4203 errorf(HERE, "operation needs integer types");
4208 type_left = promote_integer(type_left);
4209 type_right = promote_integer(type_right);
4211 expression->left = create_implicit_cast(left, type_left);
4212 expression->right = create_implicit_cast(right, type_right);
4213 expression->expression.datatype = type_left;
4216 static void semantic_add(binary_expression_t *expression)
4218 expression_t *const left = expression->left;
4219 expression_t *const right = expression->right;
4220 type_t *const orig_type_left = left->base.datatype;
4221 type_t *const orig_type_right = right->base.datatype;
4222 type_t *const type_left = skip_typeref(orig_type_left);
4223 type_t *const type_right = skip_typeref(orig_type_right);
4226 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4227 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4228 expression->left = create_implicit_cast(left, arithmetic_type);
4229 expression->right = create_implicit_cast(right, arithmetic_type);
4230 expression->expression.datatype = arithmetic_type;
4232 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4233 expression->expression.datatype = type_left;
4234 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4235 expression->expression.datatype = type_right;
4236 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4237 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4241 static void semantic_sub(binary_expression_t *expression)
4243 expression_t *const left = expression->left;
4244 expression_t *const right = expression->right;
4245 type_t *const orig_type_left = left->base.datatype;
4246 type_t *const orig_type_right = right->base.datatype;
4247 type_t *const type_left = skip_typeref(orig_type_left);
4248 type_t *const type_right = skip_typeref(orig_type_right);
4251 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4252 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4253 expression->left = create_implicit_cast(left, arithmetic_type);
4254 expression->right = create_implicit_cast(right, arithmetic_type);
4255 expression->expression.datatype = arithmetic_type;
4257 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4258 expression->expression.datatype = type_left;
4259 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4260 if(!pointers_compatible(type_left, type_right)) {
4261 errorf(HERE, "pointers to incompatible objects to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4263 expression->expression.datatype = type_ptrdiff_t;
4265 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4266 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4270 static void semantic_comparison(binary_expression_t *expression)
4272 expression_t *left = expression->left;
4273 expression_t *right = expression->right;
4274 type_t *orig_type_left = left->base.datatype;
4275 type_t *orig_type_right = right->base.datatype;
4277 type_t *type_left = skip_typeref(orig_type_left);
4278 type_t *type_right = skip_typeref(orig_type_right);
4280 /* TODO non-arithmetic types */
4281 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4282 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4283 expression->left = create_implicit_cast(left, arithmetic_type);
4284 expression->right = create_implicit_cast(right, arithmetic_type);
4285 expression->expression.datatype = arithmetic_type;
4286 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4287 /* TODO check compatibility */
4288 } else if (is_type_pointer(type_left)) {
4289 expression->right = create_implicit_cast(right, type_left);
4290 } else if (is_type_pointer(type_right)) {
4291 expression->left = create_implicit_cast(left, type_right);
4292 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4293 type_error_incompatible("invalid operands in comparison",
4294 token.source_position, type_left, type_right);
4296 expression->expression.datatype = type_int;
4299 static void semantic_arithmetic_assign(binary_expression_t *expression)
4301 expression_t *left = expression->left;
4302 expression_t *right = expression->right;
4303 type_t *orig_type_left = left->base.datatype;
4304 type_t *orig_type_right = right->base.datatype;
4306 type_t *type_left = skip_typeref(orig_type_left);
4307 type_t *type_right = skip_typeref(orig_type_right);
4309 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4310 /* TODO: improve error message */
4311 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4312 errorf(HERE, "operation needs arithmetic types");
4317 /* combined instructions are tricky. We can't create an implicit cast on
4318 * the left side, because we need the uncasted form for the store.
4319 * The ast2firm pass has to know that left_type must be right_type
4320 * for the arithmetic operation and create a cast by itself */
4321 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4322 expression->right = create_implicit_cast(right, arithmetic_type);
4323 expression->expression.datatype = type_left;
4326 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4328 expression_t *const left = expression->left;
4329 expression_t *const right = expression->right;
4330 type_t *const orig_type_left = left->base.datatype;
4331 type_t *const orig_type_right = right->base.datatype;
4332 type_t *const type_left = skip_typeref(orig_type_left);
4333 type_t *const type_right = skip_typeref(orig_type_right);
4335 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4336 /* combined instructions are tricky. We can't create an implicit cast on
4337 * the left side, because we need the uncasted form for the store.
4338 * The ast2firm pass has to know that left_type must be right_type
4339 * for the arithmetic operation and create a cast by itself */
4340 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4341 expression->right = create_implicit_cast(right, arithmetic_type);
4342 expression->expression.datatype = type_left;
4343 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4344 expression->expression.datatype = type_left;
4345 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4346 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4351 * Check the semantic restrictions of a logical expression.
4353 static void semantic_logical_op(binary_expression_t *expression)
4355 expression_t *const left = expression->left;
4356 expression_t *const right = expression->right;
4357 type_t *const orig_type_left = left->base.datatype;
4358 type_t *const orig_type_right = right->base.datatype;
4359 type_t *const type_left = skip_typeref(orig_type_left);
4360 type_t *const type_right = skip_typeref(orig_type_right);
4362 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4363 /* TODO: improve error message */
4364 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4365 errorf(HERE, "operation needs scalar types");
4370 expression->expression.datatype = type_int;
4374 * Checks if a compound type has constant fields.
4376 static bool has_const_fields(const compound_type_t *type)
4378 const context_t *context = &type->declaration->context;
4379 const declaration_t *declaration = context->declarations;
4381 for (; declaration != NULL; declaration = declaration->next) {
4382 if (declaration->namespc != NAMESPACE_NORMAL)
4385 const type_t *decl_type = skip_typeref(declaration->type);
4386 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4394 * Check the semantic restrictions of a binary assign expression.
4396 static void semantic_binexpr_assign(binary_expression_t *expression)
4398 expression_t *left = expression->left;
4399 type_t *orig_type_left = left->base.datatype;
4401 type_t *type_left = revert_automatic_type_conversion(left);
4402 type_left = skip_typeref(orig_type_left);
4404 /* must be a modifiable lvalue */
4405 if (is_type_array(type_left)) {
4406 errorf(HERE, "cannot assign to arrays ('%E')", left);
4409 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4410 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4414 if(is_type_incomplete(type_left)) {
4416 "left-hand side of assignment '%E' has incomplete type '%T'",
4417 left, orig_type_left);
4420 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4421 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4422 left, orig_type_left);
4426 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4428 if (res_type == NULL) {
4429 errorf(expression->expression.source_position,
4430 "cannot assign to '%T' from '%T'",
4431 orig_type_left, expression->right->base.datatype);
4433 expression->right = create_implicit_cast(expression->right, res_type);
4436 expression->expression.datatype = orig_type_left;
4439 static void semantic_comma(binary_expression_t *expression)
4441 expression->expression.datatype = expression->right->base.datatype;
4444 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4445 static expression_t *parse_##binexpression_type(unsigned precedence, \
4446 expression_t *left) \
4450 expression_t *right = parse_sub_expression(precedence + lr); \
4452 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4453 binexpr->binary.left = left; \
4454 binexpr->binary.right = right; \
4455 sfunc(&binexpr->binary); \
4460 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4461 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4462 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4463 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4464 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4465 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4466 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4467 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4468 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4470 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4471 semantic_comparison, 1)
4472 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4473 semantic_comparison, 1)
4474 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4475 semantic_comparison, 1)
4476 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4477 semantic_comparison, 1)
4479 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4480 semantic_binexpr_arithmetic, 1)
4481 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4482 semantic_binexpr_arithmetic, 1)
4483 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4484 semantic_binexpr_arithmetic, 1)
4485 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4486 semantic_logical_op, 1)
4487 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4488 semantic_logical_op, 1)
4489 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4490 semantic_shift_op, 1)
4491 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4492 semantic_shift_op, 1)
4493 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4494 semantic_arithmetic_addsubb_assign, 0)
4495 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4496 semantic_arithmetic_addsubb_assign, 0)
4497 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4498 semantic_arithmetic_assign, 0)
4499 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4500 semantic_arithmetic_assign, 0)
4501 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4502 semantic_arithmetic_assign, 0)
4503 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4504 semantic_arithmetic_assign, 0)
4505 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4506 semantic_arithmetic_assign, 0)
4507 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4508 semantic_arithmetic_assign, 0)
4509 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4510 semantic_arithmetic_assign, 0)
4511 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4512 semantic_arithmetic_assign, 0)
4514 static expression_t *parse_sub_expression(unsigned precedence)
4516 if(token.type < 0) {
4517 return expected_expression_error();
4520 expression_parser_function_t *parser
4521 = &expression_parsers[token.type];
4522 source_position_t source_position = token.source_position;
4525 if(parser->parser != NULL) {
4526 left = parser->parser(parser->precedence);
4528 left = parse_primary_expression();
4530 assert(left != NULL);
4531 left->base.source_position = source_position;
4534 if(token.type < 0) {
4535 return expected_expression_error();
4538 parser = &expression_parsers[token.type];
4539 if(parser->infix_parser == NULL)
4541 if(parser->infix_precedence < precedence)
4544 left = parser->infix_parser(parser->infix_precedence, left);
4546 assert(left != NULL);
4547 assert(left->kind != EXPR_UNKNOWN);
4548 left->base.source_position = source_position;
4555 * Parse an expression.
4557 static expression_t *parse_expression(void)
4559 return parse_sub_expression(1);
4563 * Register a parser for a prefix-like operator with given precedence.
4565 * @param parser the parser function
4566 * @param token_type the token type of the prefix token
4567 * @param precedence the precedence of the operator
4569 static void register_expression_parser(parse_expression_function parser,
4570 int token_type, unsigned precedence)
4572 expression_parser_function_t *entry = &expression_parsers[token_type];
4574 if(entry->parser != NULL) {
4575 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4576 panic("trying to register multiple expression parsers for a token");
4578 entry->parser = parser;
4579 entry->precedence = precedence;
4583 * Register a parser for an infix operator with given precedence.
4585 * @param parser the parser function
4586 * @param token_type the token type of the infix operator
4587 * @param precedence the precedence of the operator
4589 static void register_infix_parser(parse_expression_infix_function parser,
4590 int token_type, unsigned precedence)
4592 expression_parser_function_t *entry = &expression_parsers[token_type];
4594 if(entry->infix_parser != NULL) {
4595 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4596 panic("trying to register multiple infix expression parsers for a "
4599 entry->infix_parser = parser;
4600 entry->infix_precedence = precedence;
4604 * Initialize the expression parsers.
4606 static void init_expression_parsers(void)
4608 memset(&expression_parsers, 0, sizeof(expression_parsers));
4610 register_infix_parser(parse_array_expression, '[', 30);
4611 register_infix_parser(parse_call_expression, '(', 30);
4612 register_infix_parser(parse_select_expression, '.', 30);
4613 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4614 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4616 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4619 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4620 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4621 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4622 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4623 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4624 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4625 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4626 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4627 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4628 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4629 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4630 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4631 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4632 T_EXCLAMATIONMARKEQUAL, 13);
4633 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4634 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4635 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4636 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4637 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4638 register_infix_parser(parse_conditional_expression, '?', 7);
4639 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4640 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4641 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4642 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4643 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4644 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4645 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4646 T_LESSLESSEQUAL, 2);
4647 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4648 T_GREATERGREATEREQUAL, 2);
4649 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4651 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4653 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4656 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4658 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4659 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4660 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4661 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4662 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4663 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4664 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4666 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4668 register_expression_parser(parse_sizeof, T_sizeof, 25);
4669 register_expression_parser(parse_extension, T___extension__, 25);
4670 register_expression_parser(parse_builtin_classify_type,
4671 T___builtin_classify_type, 25);
4675 * Parse a asm statement constraints specification.
4677 static asm_constraint_t *parse_asm_constraints(void)
4679 asm_constraint_t *result = NULL;
4680 asm_constraint_t *last = NULL;
4682 while(token.type == T_STRING_LITERAL || token.type == '[') {
4683 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4684 memset(constraint, 0, sizeof(constraint[0]));
4686 if(token.type == '[') {
4688 if(token.type != T_IDENTIFIER) {
4689 parse_error_expected("while parsing asm constraint",
4693 constraint->symbol = token.v.symbol;
4698 constraint->constraints = parse_string_literals();
4700 constraint->expression = parse_expression();
4704 last->next = constraint;
4706 result = constraint;
4710 if(token.type != ',')
4719 * Parse a asm statement clobber specification.
4721 static asm_clobber_t *parse_asm_clobbers(void)
4723 asm_clobber_t *result = NULL;
4724 asm_clobber_t *last = NULL;
4726 while(token.type == T_STRING_LITERAL) {
4727 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4728 clobber->clobber = parse_string_literals();
4731 last->next = clobber;
4737 if(token.type != ',')
4746 * Parse an asm statement.
4748 static statement_t *parse_asm_statement(void)
4752 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4753 statement->base.source_position = token.source_position;
4755 asm_statement_t *asm_statement = &statement->asms;
4757 if(token.type == T_volatile) {
4759 asm_statement->is_volatile = true;
4763 asm_statement->asm_text = parse_string_literals();
4765 if(token.type != ':')
4769 asm_statement->inputs = parse_asm_constraints();
4770 if(token.type != ':')
4774 asm_statement->outputs = parse_asm_constraints();
4775 if(token.type != ':')
4779 asm_statement->clobbers = parse_asm_clobbers();
4788 * Parse a case statement.
4790 static statement_t *parse_case_statement(void)
4794 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4796 statement->base.source_position = token.source_position;
4797 statement->case_label.expression = parse_expression();
4801 if (! is_constant_expression(statement->case_label.expression)) {
4802 errorf(statement->base.source_position,
4803 "case label does not reduce to an integer constant");
4805 /* TODO: check if the case label is already known */
4806 if (current_switch != NULL) {
4807 /* link all cases into the switch statement */
4808 if (current_switch->last_case == NULL) {
4809 current_switch->first_case =
4810 current_switch->last_case = &statement->case_label;
4812 current_switch->last_case->next = &statement->case_label;
4815 errorf(statement->base.source_position,
4816 "case label not within a switch statement");
4819 statement->case_label.label_statement = parse_statement();
4825 * Finds an existing default label of a switch statement.
4827 static case_label_statement_t *
4828 find_default_label(const switch_statement_t *statement)
4830 for (case_label_statement_t *label = statement->first_case;
4832 label = label->next) {
4833 if (label->expression == NULL)
4840 * Parse a default statement.
4842 static statement_t *parse_default_statement(void)
4846 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4848 statement->base.source_position = token.source_position;
4851 if (current_switch != NULL) {
4852 const case_label_statement_t *def_label = find_default_label(current_switch);
4853 if (def_label != NULL) {
4854 errorf(HERE, "multiple default labels in one switch");
4855 errorf(def_label->statement.source_position,
4856 "this is the first default label");
4858 /* link all cases into the switch statement */
4859 if (current_switch->last_case == NULL) {
4860 current_switch->first_case =
4861 current_switch->last_case = &statement->case_label;
4863 current_switch->last_case->next = &statement->case_label;
4867 errorf(statement->base.source_position,
4868 "'default' label not within a switch statement");
4870 statement->label.label_statement = parse_statement();
4876 * Return the declaration for a given label symbol or create a new one.
4878 static declaration_t *get_label(symbol_t *symbol)
4880 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4881 assert(current_function != NULL);
4882 /* if we found a label in the same function, then we already created the
4884 if(candidate != NULL
4885 && candidate->parent_context == ¤t_function->context) {
4889 /* otherwise we need to create a new one */
4890 declaration_t *const declaration = allocate_declaration_zero();
4891 declaration->namespc = NAMESPACE_LABEL;
4892 declaration->symbol = symbol;
4894 label_push(declaration);
4900 * Parse a label statement.
4902 static statement_t *parse_label_statement(void)
4904 assert(token.type == T_IDENTIFIER);
4905 symbol_t *symbol = token.v.symbol;
4908 declaration_t *label = get_label(symbol);
4910 /* if source position is already set then the label is defined twice,
4911 * otherwise it was just mentioned in a goto so far */
4912 if(label->source_position.input_name != NULL) {
4913 errorf(HERE, "duplicate label '%Y'", symbol);
4914 errorf(label->source_position, "previous definition of '%Y' was here",
4917 label->source_position = token.source_position;
4920 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4922 label_statement->statement.kind = STATEMENT_LABEL;
4923 label_statement->statement.source_position = token.source_position;
4924 label_statement->label = label;
4928 if(token.type == '}') {
4929 /* TODO only warn? */
4930 errorf(HERE, "label at end of compound statement");
4931 return (statement_t*) label_statement;
4933 label_statement->label_statement = parse_statement();
4936 return (statement_t*) label_statement;
4940 * Parse an if statement.
4942 static statement_t *parse_if(void)
4946 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4947 statement->statement.kind = STATEMENT_IF;
4948 statement->statement.source_position = token.source_position;
4951 statement->condition = parse_expression();
4954 statement->true_statement = parse_statement();
4955 if(token.type == T_else) {
4957 statement->false_statement = parse_statement();
4960 return (statement_t*) statement;
4964 * Parse a switch statement.
4966 static statement_t *parse_switch(void)
4970 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4971 statement->statement.kind = STATEMENT_SWITCH;
4972 statement->statement.source_position = token.source_position;
4975 expression_t *const expr = parse_expression();
4976 type_t * type = skip_typeref(expr->base.datatype);
4977 if (is_type_integer(type)) {
4978 type = promote_integer(type);
4979 } else if (is_type_valid(type)) {
4980 errorf(expr->base.source_position, "switch quantity is not an integer, but '%T'", type);
4981 type = type_error_type;
4983 statement->expression = create_implicit_cast(expr, type);
4986 switch_statement_t *rem = current_switch;
4987 current_switch = statement;
4988 statement->body = parse_statement();
4989 current_switch = rem;
4991 return (statement_t*) statement;
4994 static statement_t *parse_loop_body(statement_t *const loop)
4996 statement_t *const rem = current_loop;
4997 current_loop = loop;
4998 statement_t *const body = parse_statement();
5004 * Parse a while statement.
5006 static statement_t *parse_while(void)
5010 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5011 statement->statement.kind = STATEMENT_WHILE;
5012 statement->statement.source_position = token.source_position;
5015 statement->condition = parse_expression();
5018 statement->body = parse_loop_body((statement_t*)statement);
5020 return (statement_t*) statement;
5024 * Parse a do statement.
5026 static statement_t *parse_do(void)
5030 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5031 statement->statement.kind = STATEMENT_DO_WHILE;
5032 statement->statement.source_position = token.source_position;
5034 statement->body = parse_loop_body((statement_t*)statement);
5037 statement->condition = parse_expression();
5041 return (statement_t*) statement;
5045 * Parse a for statement.
5047 static statement_t *parse_for(void)
5051 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5052 statement->statement.kind = STATEMENT_FOR;
5053 statement->statement.source_position = token.source_position;
5057 int top = environment_top();
5058 context_t *last_context = context;
5059 set_context(&statement->context);
5061 if(token.type != ';') {
5062 if(is_declaration_specifier(&token, false)) {
5063 parse_declaration(record_declaration);
5065 statement->initialisation = parse_expression();
5072 if(token.type != ';') {
5073 statement->condition = parse_expression();
5076 if(token.type != ')') {
5077 statement->step = parse_expression();
5080 statement->body = parse_loop_body((statement_t*)statement);
5082 assert(context == &statement->context);
5083 set_context(last_context);
5084 environment_pop_to(top);
5086 return (statement_t*) statement;
5090 * Parse a goto statement.
5092 static statement_t *parse_goto(void)
5096 if(token.type != T_IDENTIFIER) {
5097 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5101 symbol_t *symbol = token.v.symbol;
5104 declaration_t *label = get_label(symbol);
5106 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5108 statement->statement.kind = STATEMENT_GOTO;
5109 statement->statement.source_position = token.source_position;
5111 statement->label = label;
5113 /* remember the goto's in a list for later checking */
5114 if (goto_last == NULL) {
5115 goto_first = goto_last = statement;
5117 goto_last->next = statement;
5122 return (statement_t*) statement;
5126 * Parse a continue statement.
5128 static statement_t *parse_continue(void)
5130 statement_t *statement;
5131 if (current_loop == NULL) {
5132 errorf(HERE, "continue statement not within loop");
5135 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5137 statement->base.source_position = token.source_position;
5147 * Parse a break statement.
5149 static statement_t *parse_break(void)
5151 statement_t *statement;
5152 if (current_switch == NULL && current_loop == NULL) {
5153 errorf(HERE, "break statement not within loop or switch");
5156 statement = allocate_statement_zero(STATEMENT_BREAK);
5158 statement->base.source_position = token.source_position;
5168 * Check if a given declaration represents a local variable.
5170 static bool is_local_var_declaration(const declaration_t *declaration) {
5171 switch ((storage_class_tag_t) declaration->storage_class) {
5172 case STORAGE_CLASS_NONE:
5173 case STORAGE_CLASS_AUTO:
5174 case STORAGE_CLASS_REGISTER: {
5175 const type_t *type = skip_typeref(declaration->type);
5176 if(is_type_function(type)) {
5188 * Check if a given expression represents a local variable.
5190 static bool is_local_variable(const expression_t *expression)
5192 if (expression->base.kind != EXPR_REFERENCE) {
5195 const declaration_t *declaration = expression->reference.declaration;
5196 return is_local_var_declaration(declaration);
5200 * Parse a return statement.
5202 static statement_t *parse_return(void)
5206 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5208 statement->statement.kind = STATEMENT_RETURN;
5209 statement->statement.source_position = token.source_position;
5211 assert(is_type_function(current_function->type));
5212 function_type_t *function_type = ¤t_function->type->function;
5213 type_t *return_type = function_type->return_type;
5215 expression_t *return_value = NULL;
5216 if(token.type != ';') {
5217 return_value = parse_expression();
5221 return_type = skip_typeref(return_type);
5223 if(return_value != NULL) {
5224 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5226 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5227 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5228 warningf(statement->statement.source_position,
5229 "'return' with a value, in function returning void");
5230 return_value = NULL;
5232 type_t *const res_type = semantic_assign(return_type,
5233 return_value, "'return'");
5234 if (res_type == NULL) {
5235 errorf(statement->statement.source_position,
5236 "cannot return something of type '%T' in function returning '%T'",
5237 return_value->base.datatype, return_type);
5239 return_value = create_implicit_cast(return_value, res_type);
5242 /* check for returning address of a local var */
5243 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5244 const expression_t *expression = return_value->unary.value;
5245 if (is_local_variable(expression)) {
5246 warningf(statement->statement.source_position,
5247 "function returns address of local variable");
5251 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5252 warningf(statement->statement.source_position,
5253 "'return' without value, in function returning non-void");
5256 statement->return_value = return_value;
5258 return (statement_t*) statement;
5262 * Parse a declaration statement.
5264 static statement_t *parse_declaration_statement(void)
5266 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5268 statement->base.source_position = token.source_position;
5270 declaration_t *before = last_declaration;
5271 parse_declaration(record_declaration);
5273 if(before == NULL) {
5274 statement->declaration.declarations_begin = context->declarations;
5276 statement->declaration.declarations_begin = before->next;
5278 statement->declaration.declarations_end = last_declaration;
5284 * Parse an expression statement, ie. expr ';'.
5286 static statement_t *parse_expression_statement(void)
5288 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5290 statement->base.source_position = token.source_position;
5291 statement->expression.expression = parse_expression();
5299 * Parse a statement.
5301 static statement_t *parse_statement(void)
5303 statement_t *statement = NULL;
5305 /* declaration or statement */
5306 switch(token.type) {
5308 statement = parse_asm_statement();
5312 statement = parse_case_statement();
5316 statement = parse_default_statement();
5320 statement = parse_compound_statement();
5324 statement = parse_if();
5328 statement = parse_switch();
5332 statement = parse_while();
5336 statement = parse_do();
5340 statement = parse_for();
5344 statement = parse_goto();
5348 statement = parse_continue();
5352 statement = parse_break();
5356 statement = parse_return();
5365 if(look_ahead(1)->type == ':') {
5366 statement = parse_label_statement();
5370 if(is_typedef_symbol(token.v.symbol)) {
5371 statement = parse_declaration_statement();
5375 statement = parse_expression_statement();
5378 case T___extension__:
5379 /* this can be a prefix to a declaration or an expression statement */
5380 /* we simply eat it now and parse the rest with tail recursion */
5383 } while(token.type == T___extension__);
5384 statement = parse_statement();
5388 statement = parse_declaration_statement();
5392 statement = parse_expression_statement();
5396 assert(statement == NULL
5397 || statement->base.source_position.input_name != NULL);
5403 * Parse a compound statement.
5405 static statement_t *parse_compound_statement(void)
5407 compound_statement_t *compound_statement
5408 = allocate_ast_zero(sizeof(compound_statement[0]));
5409 compound_statement->statement.kind = STATEMENT_COMPOUND;
5410 compound_statement->statement.source_position = token.source_position;
5414 int top = environment_top();
5415 context_t *last_context = context;
5416 set_context(&compound_statement->context);
5418 statement_t *last_statement = NULL;
5420 while(token.type != '}' && token.type != T_EOF) {
5421 statement_t *statement = parse_statement();
5422 if(statement == NULL)
5425 if(last_statement != NULL) {
5426 last_statement->base.next = statement;
5428 compound_statement->statements = statement;
5431 while(statement->base.next != NULL)
5432 statement = statement->base.next;
5434 last_statement = statement;
5437 if(token.type == '}') {
5440 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5443 assert(context == &compound_statement->context);
5444 set_context(last_context);
5445 environment_pop_to(top);
5447 return (statement_t*) compound_statement;
5451 * Initialize builtin types.
5453 static void initialize_builtin_types(void)
5455 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5456 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5457 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5458 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5459 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5460 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5461 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5462 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5464 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5465 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5466 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5467 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5471 * Parse a translation unit.
5473 static translation_unit_t *parse_translation_unit(void)
5475 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5477 assert(global_context == NULL);
5478 global_context = &unit->context;
5480 assert(context == NULL);
5481 set_context(&unit->context);
5483 initialize_builtin_types();
5485 while(token.type != T_EOF) {
5486 if (token.type == ';') {
5487 /* TODO error in strict mode */
5488 warningf(HERE, "stray ';' outside of function");
5491 parse_external_declaration();
5495 assert(context == &unit->context);
5497 last_declaration = NULL;
5499 assert(global_context == &unit->context);
5500 global_context = NULL;
5508 * @return the translation unit or NULL if errors occurred.
5510 translation_unit_t *parse(void)
5512 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5513 label_stack = NEW_ARR_F(stack_entry_t, 0);
5514 diagnostic_count = 0;
5518 type_set_output(stderr);
5519 ast_set_output(stderr);
5521 lookahead_bufpos = 0;
5522 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5525 translation_unit_t *unit = parse_translation_unit();
5527 DEL_ARR_F(environment_stack);
5528 DEL_ARR_F(label_stack);
5537 * Initialize the parser.
5539 void init_parser(void)
5541 init_expression_parsers();
5542 obstack_init(&temp_obst);
5544 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5545 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5549 * Terminate the parser.
5551 void exit_parser(void)
5553 obstack_free(&temp_obst, NULL);