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 return type->atomic.akind;
645 static type_t *promote_integer(type_t *type)
647 if(type->kind == TYPE_BITFIELD)
648 type = type->bitfield.base;
650 if(get_rank(type) < ATOMIC_TYPE_INT)
657 * Create a cast expression.
659 * @param expression the expression to cast
660 * @param dest_type the destination type
662 static expression_t *create_cast_expression(expression_t *expression,
665 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
667 cast->unary.value = expression;
668 cast->base.datatype = dest_type;
674 * Check if a given expression represents the 0 pointer constant.
676 static bool is_null_pointer_constant(const expression_t *expression)
678 /* skip void* cast */
679 if(expression->kind == EXPR_UNARY_CAST
680 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
681 expression = expression->unary.value;
684 /* TODO: not correct yet, should be any constant integer expression
685 * which evaluates to 0 */
686 if (expression->kind != EXPR_CONST)
689 type_t *const type = skip_typeref(expression->base.datatype);
690 if (!is_type_integer(type))
693 return expression->conste.v.int_value == 0;
697 * Create an implicit cast expression.
699 * @param expression the expression to cast
700 * @param dest_type the destination type
702 static expression_t *create_implicit_cast(expression_t *expression,
705 type_t *const source_type = expression->base.datatype;
707 if (source_type == dest_type)
710 return create_cast_expression(expression, dest_type);
713 /** Implements the rules from § 6.5.16.1 */
714 static type_t *semantic_assign(type_t *orig_type_left,
715 const expression_t *const right,
718 type_t *const orig_type_right = right->base.datatype;
719 type_t *const type_left = skip_typeref(orig_type_left);
720 type_t *const type_right = skip_typeref(orig_type_right);
722 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
723 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
724 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
725 && is_type_pointer(type_right))) {
726 return orig_type_left;
729 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
730 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
731 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
733 /* the left type has all qualifiers from the right type */
734 unsigned missing_qualifiers
735 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
736 if(missing_qualifiers != 0) {
737 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
738 return orig_type_left;
741 points_to_left = get_unqualified_type(points_to_left);
742 points_to_right = get_unqualified_type(points_to_right);
744 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
745 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
746 && !types_compatible(points_to_left, points_to_right)) {
750 return orig_type_left;
753 if (is_type_compound(type_left) && is_type_compound(type_right)) {
754 type_t *const unqual_type_left = get_unqualified_type(type_left);
755 type_t *const unqual_type_right = get_unqualified_type(type_right);
756 if (types_compatible(unqual_type_left, unqual_type_right)) {
757 return orig_type_left;
761 if (!is_type_valid(type_left))
764 if (!is_type_valid(type_right))
765 return orig_type_right;
770 static expression_t *parse_constant_expression(void)
772 /* start parsing at precedence 7 (conditional expression) */
773 expression_t *result = parse_sub_expression(7);
775 if(!is_constant_expression(result)) {
776 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
782 static expression_t *parse_assignment_expression(void)
784 /* start parsing at precedence 2 (assignment expression) */
785 return parse_sub_expression(2);
788 static type_t *make_global_typedef(const char *name, type_t *type)
790 symbol_t *const symbol = symbol_table_insert(name);
792 declaration_t *const declaration = allocate_declaration_zero();
793 declaration->namespc = NAMESPACE_NORMAL;
794 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
795 declaration->type = type;
796 declaration->symbol = symbol;
797 declaration->source_position = builtin_source_position;
799 record_declaration(declaration);
801 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
802 typedef_type->typedeft.declaration = declaration;
807 static string_t parse_string_literals(void)
809 assert(token.type == T_STRING_LITERAL);
810 string_t result = token.v.string;
814 while (token.type == T_STRING_LITERAL) {
815 result = concat_strings(&result, &token.v.string);
822 static void parse_attributes(void)
826 case T___attribute__: {
834 errorf(HERE, "EOF while parsing attribute");
853 if(token.type != T_STRING_LITERAL) {
854 parse_error_expected("while parsing assembler attribute",
859 parse_string_literals();
864 goto attributes_finished;
873 static designator_t *parse_designation(void)
875 if(token.type != '[' && token.type != '.')
878 designator_t *result = NULL;
879 designator_t *last = NULL;
882 designator_t *designator;
885 designator = allocate_ast_zero(sizeof(designator[0]));
887 designator->array_access = parse_constant_expression();
891 designator = allocate_ast_zero(sizeof(designator[0]));
893 if(token.type != T_IDENTIFIER) {
894 parse_error_expected("while parsing designator",
898 designator->symbol = token.v.symbol;
906 assert(designator != NULL);
908 last->next = designator;
917 static initializer_t *initializer_from_string(array_type_t *type,
918 const string_t *const string)
920 /* TODO: check len vs. size of array type */
923 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
924 initializer->string.string = *string;
929 static initializer_t *initializer_from_wide_string(array_type_t *const type,
930 wide_string_t *const string)
932 /* TODO: check len vs. size of array type */
935 initializer_t *const initializer =
936 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
937 initializer->wide_string.string = *string;
942 static initializer_t *initializer_from_expression(type_t *type,
943 expression_t *expression)
945 /* TODO check that expression is a constant expression */
947 /* § 6.7.8.14/15 char array may be initialized by string literals */
948 type_t *const expr_type = expression->base.datatype;
949 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
950 array_type_t *const array_type = &type->array;
951 type_t *const element_type = skip_typeref(array_type->element_type);
953 if (element_type->kind == TYPE_ATOMIC) {
954 switch (expression->kind) {
955 case EXPR_STRING_LITERAL:
956 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
957 return initializer_from_string(array_type,
958 &expression->string.value);
961 case EXPR_WIDE_STRING_LITERAL: {
962 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
963 if (get_unqualified_type(element_type) == bare_wchar_type) {
964 return initializer_from_wide_string(array_type,
965 &expression->wide_string.value);
975 type_t *const res_type = semantic_assign(type, expression, "initializer");
976 if (res_type == NULL)
979 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
980 result->value.value = create_implicit_cast(expression, res_type);
985 static initializer_t *parse_sub_initializer(type_t *type,
986 expression_t *expression,
987 type_t *expression_type);
989 static initializer_t *parse_sub_initializer_elem(type_t *type)
991 if(token.type == '{') {
992 return parse_sub_initializer(type, NULL, NULL);
995 expression_t *expression = parse_assignment_expression();
996 type_t *expression_type = skip_typeref(expression->base.datatype);
998 return parse_sub_initializer(type, expression, expression_type);
1001 static bool had_initializer_brace_warning;
1003 static void skip_designator(void)
1006 if(token.type == '.') {
1008 if(token.type == T_IDENTIFIER)
1010 } else if(token.type == '[') {
1012 parse_constant_expression();
1013 if(token.type == ']')
1021 static initializer_t *parse_sub_initializer(type_t *type,
1022 expression_t *expression,
1023 type_t *expression_type)
1025 if(is_type_scalar(type)) {
1026 /* there might be extra {} hierarchies */
1027 if(token.type == '{') {
1029 if(!had_initializer_brace_warning) {
1030 warningf(HERE, "braces around scalar initializer");
1031 had_initializer_brace_warning = true;
1033 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1034 if(token.type == ',') {
1036 /* TODO: warn about excessive elements */
1042 if(expression == NULL) {
1043 expression = parse_assignment_expression();
1045 return initializer_from_expression(type, expression);
1048 /* does the expression match the currently looked at object to initialize */
1049 if(expression != NULL) {
1050 initializer_t *result = initializer_from_expression(type, expression);
1055 bool read_paren = false;
1056 if(token.type == '{') {
1061 /* descend into subtype */
1062 initializer_t *result = NULL;
1063 initializer_t **elems;
1064 if(is_type_array(type)) {
1065 if(token.type == '.') {
1067 "compound designator in initializer for array type '%T'",
1072 type_t *const element_type = skip_typeref(type->array.element_type);
1075 had_initializer_brace_warning = false;
1076 if(expression == NULL) {
1077 sub = parse_sub_initializer_elem(element_type);
1079 sub = parse_sub_initializer(element_type, expression,
1083 /* didn't match the subtypes -> try the parent type */
1085 assert(!read_paren);
1089 elems = NEW_ARR_F(initializer_t*, 0);
1090 ARR_APP1(initializer_t*, elems, sub);
1093 if(token.type == '}')
1096 if(token.type == '}')
1099 sub = parse_sub_initializer_elem(element_type);
1101 /* TODO error, do nicer cleanup */
1102 errorf(HERE, "member initializer didn't match");
1106 ARR_APP1(initializer_t*, elems, sub);
1109 assert(is_type_compound(type));
1110 context_t *const context = &type->compound.declaration->context;
1112 if(token.type == '[') {
1114 "array designator in initializer for compound type '%T'",
1119 declaration_t *first = context->declarations;
1122 type_t *first_type = first->type;
1123 first_type = skip_typeref(first_type);
1126 had_initializer_brace_warning = false;
1127 if(expression == NULL) {
1128 sub = parse_sub_initializer_elem(first_type);
1130 sub = parse_sub_initializer(first_type, expression,expression_type);
1133 /* didn't match the subtypes -> try our parent type */
1135 assert(!read_paren);
1139 elems = NEW_ARR_F(initializer_t*, 0);
1140 ARR_APP1(initializer_t*, elems, sub);
1142 declaration_t *iter = first->next;
1143 for( ; iter != NULL; iter = iter->next) {
1144 if(iter->symbol == NULL)
1146 if(iter->namespc != NAMESPACE_NORMAL)
1149 if(token.type == '}')
1152 if(token.type == '}')
1155 type_t *iter_type = iter->type;
1156 iter_type = skip_typeref(iter_type);
1158 sub = parse_sub_initializer_elem(iter_type);
1160 /* TODO error, do nicer cleanup */
1161 errorf(HERE, "member initializer didn't match");
1165 ARR_APP1(initializer_t*, elems, sub);
1169 int len = ARR_LEN(elems);
1170 size_t elems_size = sizeof(initializer_t*) * len;
1172 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1174 init->initializer.kind = INITIALIZER_LIST;
1176 memcpy(init->initializers, elems, elems_size);
1179 result = (initializer_t*) init;
1182 if(token.type == ',')
1189 static initializer_t *parse_initializer(type_t *const orig_type)
1191 initializer_t *result;
1193 type_t *const type = skip_typeref(orig_type);
1195 if(token.type != '{') {
1196 expression_t *expression = parse_assignment_expression();
1197 initializer_t *initializer = initializer_from_expression(type, expression);
1198 if(initializer == NULL) {
1200 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1201 expression, expression->base.datatype, orig_type);
1206 if(is_type_scalar(type)) {
1210 expression_t *expression = parse_assignment_expression();
1211 result = initializer_from_expression(type, expression);
1213 if(token.type == ',')
1219 result = parse_sub_initializer(type, NULL, NULL);
1225 static declaration_t *append_declaration(declaration_t *declaration);
1227 static declaration_t *parse_compound_type_specifier(bool is_struct)
1235 symbol_t *symbol = NULL;
1236 declaration_t *declaration = NULL;
1238 if (token.type == T___attribute__) {
1243 if(token.type == T_IDENTIFIER) {
1244 symbol = token.v.symbol;
1248 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1250 declaration = get_declaration(symbol, NAMESPACE_UNION);
1252 } else if(token.type != '{') {
1254 parse_error_expected("while parsing struct type specifier",
1255 T_IDENTIFIER, '{', 0);
1257 parse_error_expected("while parsing union type specifier",
1258 T_IDENTIFIER, '{', 0);
1264 if(declaration == NULL) {
1265 declaration = allocate_declaration_zero();
1266 declaration->namespc =
1267 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1268 declaration->source_position = token.source_position;
1269 declaration->symbol = symbol;
1270 declaration->parent_context = context;
1271 if (symbol != NULL) {
1272 environment_push(declaration);
1274 append_declaration(declaration);
1277 if(token.type == '{') {
1278 if(declaration->init.is_defined) {
1279 assert(symbol != NULL);
1280 errorf(HERE, "multiple definition of '%s %Y'",
1281 is_struct ? "struct" : "union", symbol);
1282 declaration->context.declarations = NULL;
1284 declaration->init.is_defined = true;
1286 int top = environment_top();
1287 context_t *last_context = context;
1288 set_context(&declaration->context);
1290 parse_compound_type_entries();
1293 assert(context == &declaration->context);
1294 set_context(last_context);
1295 environment_pop_to(top);
1301 static void parse_enum_entries(type_t *const enum_type)
1305 if(token.type == '}') {
1307 errorf(HERE, "empty enum not allowed");
1312 if(token.type != T_IDENTIFIER) {
1313 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1318 declaration_t *const entry = allocate_declaration_zero();
1319 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1320 entry->type = enum_type;
1321 entry->symbol = token.v.symbol;
1322 entry->source_position = token.source_position;
1325 if(token.type == '=') {
1327 entry->init.enum_value = parse_constant_expression();
1332 record_declaration(entry);
1334 if(token.type != ',')
1337 } while(token.type != '}');
1342 static type_t *parse_enum_specifier(void)
1346 declaration_t *declaration;
1349 if(token.type == T_IDENTIFIER) {
1350 symbol = token.v.symbol;
1353 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1354 } else if(token.type != '{') {
1355 parse_error_expected("while parsing enum type specifier",
1356 T_IDENTIFIER, '{', 0);
1363 if(declaration == NULL) {
1364 declaration = allocate_declaration_zero();
1365 declaration->namespc = NAMESPACE_ENUM;
1366 declaration->source_position = token.source_position;
1367 declaration->symbol = symbol;
1368 declaration->parent_context = context;
1371 type_t *const type = allocate_type_zero(TYPE_ENUM);
1372 type->enumt.declaration = declaration;
1374 if(token.type == '{') {
1375 if(declaration->init.is_defined) {
1376 errorf(HERE, "multiple definitions of enum %Y", symbol);
1378 if (symbol != NULL) {
1379 environment_push(declaration);
1381 append_declaration(declaration);
1382 declaration->init.is_defined = 1;
1384 parse_enum_entries(type);
1392 * if a symbol is a typedef to another type, return true
1394 static bool is_typedef_symbol(symbol_t *symbol)
1396 const declaration_t *const declaration =
1397 get_declaration(symbol, NAMESPACE_NORMAL);
1399 declaration != NULL &&
1400 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1403 static type_t *parse_typeof(void)
1411 expression_t *expression = NULL;
1414 switch(token.type) {
1415 case T___extension__:
1416 /* this can be a prefix to a typename or an expression */
1417 /* we simply eat it now. */
1420 } while(token.type == T___extension__);
1424 if(is_typedef_symbol(token.v.symbol)) {
1425 type = parse_typename();
1427 expression = parse_expression();
1428 type = expression->base.datatype;
1433 type = parse_typename();
1437 expression = parse_expression();
1438 type = expression->base.datatype;
1444 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1445 typeof_type->typeoft.expression = expression;
1446 typeof_type->typeoft.typeof_type = type;
1452 SPECIFIER_SIGNED = 1 << 0,
1453 SPECIFIER_UNSIGNED = 1 << 1,
1454 SPECIFIER_LONG = 1 << 2,
1455 SPECIFIER_INT = 1 << 3,
1456 SPECIFIER_DOUBLE = 1 << 4,
1457 SPECIFIER_CHAR = 1 << 5,
1458 SPECIFIER_SHORT = 1 << 6,
1459 SPECIFIER_LONG_LONG = 1 << 7,
1460 SPECIFIER_FLOAT = 1 << 8,
1461 SPECIFIER_BOOL = 1 << 9,
1462 SPECIFIER_VOID = 1 << 10,
1463 #ifdef PROVIDE_COMPLEX
1464 SPECIFIER_COMPLEX = 1 << 11,
1465 SPECIFIER_IMAGINARY = 1 << 12,
1469 static type_t *create_builtin_type(symbol_t *const symbol,
1470 type_t *const real_type)
1472 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1473 type->builtin.symbol = symbol;
1474 type->builtin.real_type = real_type;
1476 type_t *result = typehash_insert(type);
1477 if (type != result) {
1484 static type_t *get_typedef_type(symbol_t *symbol)
1486 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1487 if(declaration == NULL
1488 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1491 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1492 type->typedeft.declaration = declaration;
1497 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1499 type_t *type = NULL;
1500 unsigned type_qualifiers = 0;
1501 unsigned type_specifiers = 0;
1504 specifiers->source_position = token.source_position;
1507 switch(token.type) {
1510 #define MATCH_STORAGE_CLASS(token, class) \
1512 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1513 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1515 specifiers->storage_class = class; \
1519 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1520 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1521 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1522 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1523 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1526 switch (specifiers->storage_class) {
1527 case STORAGE_CLASS_NONE:
1528 specifiers->storage_class = STORAGE_CLASS_THREAD;
1531 case STORAGE_CLASS_EXTERN:
1532 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1535 case STORAGE_CLASS_STATIC:
1536 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1540 errorf(HERE, "multiple storage classes in declaration specifiers");
1546 /* type qualifiers */
1547 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1549 type_qualifiers |= qualifier; \
1553 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1554 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1555 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1557 case T___extension__:
1562 /* type specifiers */
1563 #define MATCH_SPECIFIER(token, specifier, name) \
1566 if(type_specifiers & specifier) { \
1567 errorf(HERE, "multiple " name " type specifiers given"); \
1569 type_specifiers |= specifier; \
1573 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1574 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1575 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1576 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1577 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1578 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1579 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1580 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1581 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1582 #ifdef PROVIDE_COMPLEX
1583 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1584 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1587 /* only in microsoft mode */
1588 specifiers->decl_modifiers |= DM_FORCEINLINE;
1592 specifiers->is_inline = true;
1597 if(type_specifiers & SPECIFIER_LONG_LONG) {
1598 errorf(HERE, "multiple type specifiers given");
1599 } else if(type_specifiers & SPECIFIER_LONG) {
1600 type_specifiers |= SPECIFIER_LONG_LONG;
1602 type_specifiers |= SPECIFIER_LONG;
1606 /* TODO: if is_type_valid(type) for the following rules should issue
1609 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1611 type->compound.declaration = parse_compound_type_specifier(true);
1615 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1617 type->compound.declaration = parse_compound_type_specifier(false);
1621 type = parse_enum_specifier();
1624 type = parse_typeof();
1626 case T___builtin_va_list:
1627 type = duplicate_type(type_valist);
1631 case T___attribute__:
1636 case T_IDENTIFIER: {
1637 type_t *typedef_type = get_typedef_type(token.v.symbol);
1639 if(typedef_type == NULL)
1640 goto finish_specifiers;
1643 type = typedef_type;
1647 /* function specifier */
1649 goto finish_specifiers;
1656 atomic_type_kind_t atomic_type;
1658 /* match valid basic types */
1659 switch(type_specifiers) {
1660 case SPECIFIER_VOID:
1661 atomic_type = ATOMIC_TYPE_VOID;
1663 case SPECIFIER_CHAR:
1664 atomic_type = ATOMIC_TYPE_CHAR;
1666 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1667 atomic_type = ATOMIC_TYPE_SCHAR;
1669 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1670 atomic_type = ATOMIC_TYPE_UCHAR;
1672 case SPECIFIER_SHORT:
1673 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1674 case SPECIFIER_SHORT | SPECIFIER_INT:
1675 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1676 atomic_type = ATOMIC_TYPE_SHORT;
1678 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1679 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1680 atomic_type = ATOMIC_TYPE_USHORT;
1683 case SPECIFIER_SIGNED:
1684 case SPECIFIER_SIGNED | SPECIFIER_INT:
1685 atomic_type = ATOMIC_TYPE_INT;
1687 case SPECIFIER_UNSIGNED:
1688 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1689 atomic_type = ATOMIC_TYPE_UINT;
1691 case SPECIFIER_LONG:
1692 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1693 case SPECIFIER_LONG | SPECIFIER_INT:
1694 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1695 atomic_type = ATOMIC_TYPE_LONG;
1697 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1698 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1699 atomic_type = ATOMIC_TYPE_ULONG;
1701 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1702 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1703 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1704 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1706 atomic_type = ATOMIC_TYPE_LONGLONG;
1708 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1709 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1711 atomic_type = ATOMIC_TYPE_ULONGLONG;
1713 case SPECIFIER_FLOAT:
1714 atomic_type = ATOMIC_TYPE_FLOAT;
1716 case SPECIFIER_DOUBLE:
1717 atomic_type = ATOMIC_TYPE_DOUBLE;
1719 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1720 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1722 case SPECIFIER_BOOL:
1723 atomic_type = ATOMIC_TYPE_BOOL;
1725 #ifdef PROVIDE_COMPLEX
1726 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1727 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1729 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1730 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1732 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1733 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1735 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1736 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1738 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1739 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1741 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1742 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1746 /* invalid specifier combination, give an error message */
1747 if(type_specifiers == 0) {
1748 if (! strict_mode) {
1749 warningf(HERE, "no type specifiers in declaration, using int");
1750 atomic_type = ATOMIC_TYPE_INT;
1753 errorf(HERE, "no type specifiers given in declaration");
1755 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1756 (type_specifiers & SPECIFIER_UNSIGNED)) {
1757 errorf(HERE, "signed and unsigned specifiers gives");
1758 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1759 errorf(HERE, "only integer types can be signed or unsigned");
1761 errorf(HERE, "multiple datatypes in declaration");
1763 atomic_type = ATOMIC_TYPE_INVALID;
1766 type = allocate_type_zero(TYPE_ATOMIC);
1767 type->atomic.akind = atomic_type;
1770 if(type_specifiers != 0) {
1771 errorf(HERE, "multiple datatypes in declaration");
1775 type->base.qualifiers = type_qualifiers;
1777 type_t *result = typehash_insert(type);
1778 if(newtype && result != type) {
1782 specifiers->type = result;
1785 static type_qualifiers_t parse_type_qualifiers(void)
1787 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1790 switch(token.type) {
1791 /* type qualifiers */
1792 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1793 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1794 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1797 return type_qualifiers;
1802 static declaration_t *parse_identifier_list(void)
1804 declaration_t *declarations = NULL;
1805 declaration_t *last_declaration = NULL;
1807 declaration_t *const declaration = allocate_declaration_zero();
1808 declaration->source_position = token.source_position;
1809 declaration->symbol = token.v.symbol;
1812 if(last_declaration != NULL) {
1813 last_declaration->next = declaration;
1815 declarations = declaration;
1817 last_declaration = declaration;
1819 if(token.type != ',')
1822 } while(token.type == T_IDENTIFIER);
1824 return declarations;
1827 static void semantic_parameter(declaration_t *declaration)
1829 /* TODO: improve error messages */
1831 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1832 errorf(HERE, "typedef not allowed in parameter list");
1833 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1834 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1835 errorf(HERE, "parameter may only have none or register storage class");
1838 type_t *const orig_type = declaration->type;
1839 type_t * type = skip_typeref(orig_type);
1841 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1842 * into a pointer. § 6.7.5.3 (7) */
1843 if (is_type_array(type)) {
1844 type_t *const element_type = type->array.element_type;
1846 type = make_pointer_type(element_type, type->base.qualifiers);
1848 declaration->type = type;
1851 if(is_type_incomplete(type)) {
1852 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1853 orig_type, declaration->symbol);
1857 static declaration_t *parse_parameter(void)
1859 declaration_specifiers_t specifiers;
1860 memset(&specifiers, 0, sizeof(specifiers));
1862 parse_declaration_specifiers(&specifiers);
1864 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1866 semantic_parameter(declaration);
1871 static declaration_t *parse_parameters(function_type_t *type)
1873 if(token.type == T_IDENTIFIER) {
1874 symbol_t *symbol = token.v.symbol;
1875 if(!is_typedef_symbol(symbol)) {
1876 type->kr_style_parameters = true;
1877 return parse_identifier_list();
1881 if(token.type == ')') {
1882 type->unspecified_parameters = 1;
1885 if(token.type == T_void && look_ahead(1)->type == ')') {
1890 declaration_t *declarations = NULL;
1891 declaration_t *declaration;
1892 declaration_t *last_declaration = NULL;
1893 function_parameter_t *parameter;
1894 function_parameter_t *last_parameter = NULL;
1897 switch(token.type) {
1901 return declarations;
1904 case T___extension__:
1906 declaration = parse_parameter();
1908 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1909 memset(parameter, 0, sizeof(parameter[0]));
1910 parameter->type = declaration->type;
1912 if(last_parameter != NULL) {
1913 last_declaration->next = declaration;
1914 last_parameter->next = parameter;
1916 type->parameters = parameter;
1917 declarations = declaration;
1919 last_parameter = parameter;
1920 last_declaration = declaration;
1924 return declarations;
1926 if(token.type != ',')
1927 return declarations;
1937 } construct_type_type_t;
1939 typedef struct construct_type_t construct_type_t;
1940 struct construct_type_t {
1941 construct_type_type_t type;
1942 construct_type_t *next;
1945 typedef struct parsed_pointer_t parsed_pointer_t;
1946 struct parsed_pointer_t {
1947 construct_type_t construct_type;
1948 type_qualifiers_t type_qualifiers;
1951 typedef struct construct_function_type_t construct_function_type_t;
1952 struct construct_function_type_t {
1953 construct_type_t construct_type;
1954 type_t *function_type;
1957 typedef struct parsed_array_t parsed_array_t;
1958 struct parsed_array_t {
1959 construct_type_t construct_type;
1960 type_qualifiers_t type_qualifiers;
1966 typedef struct construct_base_type_t construct_base_type_t;
1967 struct construct_base_type_t {
1968 construct_type_t construct_type;
1972 static construct_type_t *parse_pointer_declarator(void)
1976 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1977 memset(pointer, 0, sizeof(pointer[0]));
1978 pointer->construct_type.type = CONSTRUCT_POINTER;
1979 pointer->type_qualifiers = parse_type_qualifiers();
1981 return (construct_type_t*) pointer;
1984 static construct_type_t *parse_array_declarator(void)
1988 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1989 memset(array, 0, sizeof(array[0]));
1990 array->construct_type.type = CONSTRUCT_ARRAY;
1992 if(token.type == T_static) {
1993 array->is_static = true;
1997 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
1998 if(type_qualifiers != 0) {
1999 if(token.type == T_static) {
2000 array->is_static = true;
2004 array->type_qualifiers = type_qualifiers;
2006 if(token.type == '*' && look_ahead(1)->type == ']') {
2007 array->is_variable = true;
2009 } else if(token.type != ']') {
2010 array->size = parse_assignment_expression();
2015 return (construct_type_t*) array;
2018 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2022 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2024 declaration_t *parameters = parse_parameters(&type->function);
2025 if(declaration != NULL) {
2026 declaration->context.declarations = parameters;
2029 construct_function_type_t *construct_function_type =
2030 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2031 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2032 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2033 construct_function_type->function_type = type;
2037 return (construct_type_t*) construct_function_type;
2040 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2041 bool may_be_abstract)
2043 /* construct a single linked list of construct_type_t's which describe
2044 * how to construct the final declarator type */
2045 construct_type_t *first = NULL;
2046 construct_type_t *last = NULL;
2049 while(token.type == '*') {
2050 construct_type_t *type = parse_pointer_declarator();
2061 /* TODO: find out if this is correct */
2064 construct_type_t *inner_types = NULL;
2066 switch(token.type) {
2068 if(declaration == NULL) {
2069 errorf(HERE, "no identifier expected in typename");
2071 declaration->symbol = token.v.symbol;
2072 declaration->source_position = token.source_position;
2078 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2084 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2085 /* avoid a loop in the outermost scope, because eat_statement doesn't
2087 if(token.type == '}' && current_function == NULL) {
2095 construct_type_t *p = last;
2098 construct_type_t *type;
2099 switch(token.type) {
2101 type = parse_function_declarator(declaration);
2104 type = parse_array_declarator();
2107 goto declarator_finished;
2110 /* insert in the middle of the list (behind p) */
2112 type->next = p->next;
2123 declarator_finished:
2126 /* append inner_types at the end of the list, we don't to set last anymore
2127 * as it's not needed anymore */
2129 assert(first == NULL);
2130 first = inner_types;
2132 last->next = inner_types;
2138 static type_t *construct_declarator_type(construct_type_t *construct_list,
2141 construct_type_t *iter = construct_list;
2142 for( ; iter != NULL; iter = iter->next) {
2143 switch(iter->type) {
2144 case CONSTRUCT_INVALID:
2145 panic("invalid type construction found");
2146 case CONSTRUCT_FUNCTION: {
2147 construct_function_type_t *construct_function_type
2148 = (construct_function_type_t*) iter;
2150 type_t *function_type = construct_function_type->function_type;
2152 function_type->function.return_type = type;
2154 type_t *skipped_return_type = skip_typeref(type);
2155 if (is_type_function(skipped_return_type)) {
2156 errorf(HERE, "function returning function is not allowed");
2157 type = type_error_type;
2158 } else if (is_type_array(skipped_return_type)) {
2159 errorf(HERE, "function returning array is not allowed");
2160 type = type_error_type;
2162 type = function_type;
2167 case CONSTRUCT_POINTER: {
2168 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2169 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2170 pointer_type->pointer.points_to = type;
2171 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2173 type = pointer_type;
2177 case CONSTRUCT_ARRAY: {
2178 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2179 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2181 array_type->base.qualifiers = parsed_array->type_qualifiers;
2182 array_type->array.element_type = type;
2183 array_type->array.is_static = parsed_array->is_static;
2184 array_type->array.is_variable = parsed_array->is_variable;
2185 array_type->array.size = parsed_array->size;
2187 type_t *skipped_type = skip_typeref(type);
2188 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2189 errorf(HERE, "array of void is not allowed");
2190 type = type_error_type;
2198 type_t *hashed_type = typehash_insert(type);
2199 if(hashed_type != type) {
2200 /* the function type was constructed earlier freeing it here will
2201 * destroy other types... */
2202 if(iter->type != CONSTRUCT_FUNCTION) {
2212 static declaration_t *parse_declarator(
2213 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2215 declaration_t *const declaration = allocate_declaration_zero();
2216 declaration->storage_class = specifiers->storage_class;
2217 declaration->modifiers = specifiers->decl_modifiers;
2218 declaration->is_inline = specifiers->is_inline;
2220 construct_type_t *construct_type
2221 = parse_inner_declarator(declaration, may_be_abstract);
2222 type_t *const type = specifiers->type;
2223 declaration->type = construct_declarator_type(construct_type, type);
2225 if(construct_type != NULL) {
2226 obstack_free(&temp_obst, construct_type);
2232 static type_t *parse_abstract_declarator(type_t *base_type)
2234 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2236 type_t *result = construct_declarator_type(construct_type, base_type);
2237 if(construct_type != NULL) {
2238 obstack_free(&temp_obst, construct_type);
2244 static declaration_t *append_declaration(declaration_t* const declaration)
2246 if (last_declaration != NULL) {
2247 last_declaration->next = declaration;
2249 context->declarations = declaration;
2251 last_declaration = declaration;
2255 static declaration_t *internal_record_declaration(
2256 declaration_t *const declaration,
2257 const bool is_function_definition)
2259 const symbol_t *const symbol = declaration->symbol;
2260 const namespace_t namespc = (namespace_t)declaration->namespc;
2262 const type_t *const type = skip_typeref(declaration->type);
2263 if (is_type_function(type) && type->function.unspecified_parameters) {
2264 warningf(declaration->source_position,
2265 "function declaration '%#T' is not a prototype",
2266 type, declaration->symbol);
2269 declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2270 assert(declaration != previous_declaration);
2271 if (previous_declaration != NULL
2272 && previous_declaration->parent_context == context) {
2273 /* can happen for K&R style declarations */
2274 if(previous_declaration->type == NULL) {
2275 previous_declaration->type = declaration->type;
2278 const type_t *const prev_type = skip_typeref(previous_declaration->type);
2279 if (!types_compatible(type, prev_type)) {
2280 errorf(declaration->source_position,
2281 "declaration '%#T' is incompatible with previous declaration '%#T'",
2282 type, symbol, previous_declaration->type, symbol);
2283 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2285 unsigned old_storage_class = previous_declaration->storage_class;
2286 unsigned new_storage_class = declaration->storage_class;
2288 /* pretend no storage class means extern for function declarations
2289 * (except if the previous declaration is neither none nor extern) */
2290 if (is_type_function(type)) {
2291 switch (old_storage_class) {
2292 case STORAGE_CLASS_NONE:
2293 old_storage_class = STORAGE_CLASS_EXTERN;
2295 case STORAGE_CLASS_EXTERN:
2296 if (new_storage_class == STORAGE_CLASS_NONE && !is_function_definition) {
2297 new_storage_class = STORAGE_CLASS_EXTERN;
2305 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2306 new_storage_class == STORAGE_CLASS_EXTERN) {
2307 warn_redundant_declaration:
2308 warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
2309 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2310 } else if (current_function == NULL) {
2311 if (old_storage_class != STORAGE_CLASS_STATIC &&
2312 new_storage_class == STORAGE_CLASS_STATIC) {
2313 errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
2314 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2316 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2317 goto warn_redundant_declaration;
2319 if (new_storage_class == STORAGE_CLASS_NONE) {
2320 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2324 if (old_storage_class == new_storage_class) {
2325 errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
2327 errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
2329 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2332 return previous_declaration;
2335 assert(declaration->parent_context == NULL);
2336 assert(declaration->symbol != NULL);
2337 assert(context != NULL);
2339 declaration->parent_context = context;
2341 environment_push(declaration);
2342 return append_declaration(declaration);
2345 static declaration_t *record_declaration(declaration_t *declaration)
2347 return internal_record_declaration(declaration, false);
2350 static declaration_t *record_function_definition(declaration_t *declaration)
2352 return internal_record_declaration(declaration, true);
2355 static void parser_error_multiple_definition(declaration_t *declaration,
2356 const source_position_t source_position)
2358 errorf(source_position, "multiple definition of symbol '%Y'",
2359 declaration->symbol);
2360 errorf(declaration->source_position,
2361 "this is the location of the previous definition.");
2364 static bool is_declaration_specifier(const token_t *token,
2365 bool only_type_specifiers)
2367 switch(token->type) {
2371 return is_typedef_symbol(token->v.symbol);
2373 case T___extension__:
2376 return !only_type_specifiers;
2383 static void parse_init_declarator_rest(declaration_t *declaration)
2387 type_t *orig_type = declaration->type;
2388 type_t *type = type = skip_typeref(orig_type);
2390 if(declaration->init.initializer != NULL) {
2391 parser_error_multiple_definition(declaration, token.source_position);
2394 initializer_t *initializer = parse_initializer(type);
2396 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2397 * the array type size */
2398 if(is_type_array(type) && initializer != NULL) {
2399 array_type_t *array_type = &type->array;
2401 if(array_type->size == NULL) {
2402 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2404 cnst->base.datatype = type_size_t;
2406 switch (initializer->kind) {
2407 case INITIALIZER_LIST: {
2408 cnst->conste.v.int_value = initializer->list.len;
2412 case INITIALIZER_STRING: {
2413 cnst->conste.v.int_value = initializer->string.string.size;
2417 case INITIALIZER_WIDE_STRING: {
2418 cnst->conste.v.int_value = initializer->wide_string.string.size;
2423 panic("invalid initializer type");
2426 array_type->size = cnst;
2430 if(is_type_function(type)) {
2431 errorf(declaration->source_position,
2432 "initializers not allowed for function types at declator '%Y' (type '%T')",
2433 declaration->symbol, orig_type);
2435 declaration->init.initializer = initializer;
2439 /* parse rest of a declaration without any declarator */
2440 static void parse_anonymous_declaration_rest(
2441 const declaration_specifiers_t *specifiers,
2442 parsed_declaration_func finished_declaration)
2446 declaration_t *const declaration = allocate_declaration_zero();
2447 declaration->type = specifiers->type;
2448 declaration->storage_class = specifiers->storage_class;
2449 declaration->source_position = specifiers->source_position;
2451 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2452 warningf(declaration->source_position, "useless storage class in empty declaration");
2455 type_t *type = declaration->type;
2456 switch (type->kind) {
2457 case TYPE_COMPOUND_STRUCT:
2458 case TYPE_COMPOUND_UNION: {
2459 if (type->compound.declaration->symbol == NULL) {
2460 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2469 warningf(declaration->source_position, "empty declaration");
2473 finished_declaration(declaration);
2476 static void parse_declaration_rest(declaration_t *ndeclaration,
2477 const declaration_specifiers_t *specifiers,
2478 parsed_declaration_func finished_declaration)
2481 declaration_t *declaration = finished_declaration(ndeclaration);
2483 type_t *orig_type = declaration->type;
2484 type_t *type = skip_typeref(orig_type);
2486 if (type->kind != TYPE_FUNCTION &&
2487 declaration->is_inline &&
2488 is_type_valid(type)) {
2489 warningf(declaration->source_position,
2490 "variable '%Y' declared 'inline'\n", declaration->symbol);
2493 if(token.type == '=') {
2494 parse_init_declarator_rest(declaration);
2497 if(token.type != ',')
2501 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2506 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2508 symbol_t *symbol = declaration->symbol;
2509 if(symbol == NULL) {
2510 errorf(HERE, "anonymous declaration not valid as function parameter");
2513 namespace_t namespc = (namespace_t) declaration->namespc;
2514 if(namespc != NAMESPACE_NORMAL) {
2515 return record_declaration(declaration);
2518 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2519 if(previous_declaration == NULL ||
2520 previous_declaration->parent_context != context) {
2521 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2526 if(previous_declaration->type == NULL) {
2527 previous_declaration->type = declaration->type;
2528 previous_declaration->storage_class = declaration->storage_class;
2529 previous_declaration->parent_context = context;
2530 return previous_declaration;
2532 return record_declaration(declaration);
2536 static void parse_declaration(parsed_declaration_func finished_declaration)
2538 declaration_specifiers_t specifiers;
2539 memset(&specifiers, 0, sizeof(specifiers));
2540 parse_declaration_specifiers(&specifiers);
2542 if(token.type == ';') {
2543 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2545 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2546 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2550 static void parse_kr_declaration_list(declaration_t *declaration)
2552 type_t *type = skip_typeref(declaration->type);
2553 if(!is_type_function(type))
2556 if(!type->function.kr_style_parameters)
2559 /* push function parameters */
2560 int top = environment_top();
2561 context_t *last_context = context;
2562 set_context(&declaration->context);
2564 declaration_t *parameter = declaration->context.declarations;
2565 for( ; parameter != NULL; parameter = parameter->next) {
2566 assert(parameter->parent_context == NULL);
2567 parameter->parent_context = context;
2568 environment_push(parameter);
2571 /* parse declaration list */
2572 while(is_declaration_specifier(&token, false)) {
2573 parse_declaration(finished_kr_declaration);
2576 /* pop function parameters */
2577 assert(context == &declaration->context);
2578 set_context(last_context);
2579 environment_pop_to(top);
2581 /* update function type */
2582 type_t *new_type = duplicate_type(type);
2583 new_type->function.kr_style_parameters = false;
2585 function_parameter_t *parameters = NULL;
2586 function_parameter_t *last_parameter = NULL;
2588 declaration_t *parameter_declaration = declaration->context.declarations;
2589 for( ; parameter_declaration != NULL;
2590 parameter_declaration = parameter_declaration->next) {
2591 type_t *parameter_type = parameter_declaration->type;
2592 if(parameter_type == NULL) {
2594 errorf(HERE, "no type specified for function parameter '%Y'",
2595 parameter_declaration->symbol);
2597 warningf(HERE, "no type specified for function parameter '%Y', using int",
2598 parameter_declaration->symbol);
2599 parameter_type = type_int;
2600 parameter_declaration->type = parameter_type;
2604 semantic_parameter(parameter_declaration);
2605 parameter_type = parameter_declaration->type;
2607 function_parameter_t *function_parameter
2608 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2609 memset(function_parameter, 0, sizeof(function_parameter[0]));
2611 function_parameter->type = parameter_type;
2612 if(last_parameter != NULL) {
2613 last_parameter->next = function_parameter;
2615 parameters = function_parameter;
2617 last_parameter = function_parameter;
2619 new_type->function.parameters = parameters;
2621 type = typehash_insert(new_type);
2622 if(type != new_type) {
2623 obstack_free(type_obst, new_type);
2626 declaration->type = type;
2630 * Check if all labels are defined in the current function.
2632 static void check_for_missing_labels(void)
2634 bool first_err = true;
2635 for (const goto_statement_t *goto_statement = goto_first;
2636 goto_statement != NULL;
2637 goto_statement = goto_statement->next) {
2638 const declaration_t *label = goto_statement->label;
2640 if (label->source_position.input_name == NULL) {
2643 diagnosticf("%s: In function '%Y':\n",
2644 current_function->source_position.input_name,
2645 current_function->symbol);
2647 errorf(goto_statement->statement.source_position,
2648 "label '%Y' used but not defined", label->symbol);
2651 goto_first = goto_last = NULL;
2654 static void parse_external_declaration(void)
2656 /* function-definitions and declarations both start with declaration
2658 declaration_specifiers_t specifiers;
2659 memset(&specifiers, 0, sizeof(specifiers));
2660 parse_declaration_specifiers(&specifiers);
2662 /* must be a declaration */
2663 if(token.type == ';') {
2664 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2668 /* declarator is common to both function-definitions and declarations */
2669 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2671 /* must be a declaration */
2672 if(token.type == ',' || token.type == '=' || token.type == ';') {
2673 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2677 /* must be a function definition */
2678 parse_kr_declaration_list(ndeclaration);
2680 if(token.type != '{') {
2681 parse_error_expected("while parsing function definition", '{', 0);
2686 type_t *type = ndeclaration->type;
2688 /* note that we don't skip typerefs: the standard doesn't allow them here
2689 * (so we can't use is_type_function here) */
2690 if(type->kind != TYPE_FUNCTION) {
2691 if (is_type_valid(type)) {
2692 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2693 type, ndeclaration->symbol);
2699 /* § 6.7.5.3 (14) a function definition with () means no
2700 * parameters (and not unspecified parameters) */
2701 if(type->function.unspecified_parameters) {
2702 type_t *duplicate = duplicate_type(type);
2703 duplicate->function.unspecified_parameters = false;
2705 type = typehash_insert(duplicate);
2706 if(type != duplicate) {
2707 obstack_free(type_obst, duplicate);
2709 ndeclaration->type = type;
2712 declaration_t *const declaration = record_function_definition(ndeclaration);
2713 if(ndeclaration != declaration) {
2714 declaration->context = ndeclaration->context;
2716 type = skip_typeref(declaration->type);
2718 /* push function parameters and switch context */
2719 int top = environment_top();
2720 context_t *last_context = context;
2721 set_context(&declaration->context);
2723 declaration_t *parameter = declaration->context.declarations;
2724 for( ; parameter != NULL; parameter = parameter->next) {
2725 if(parameter->parent_context == &ndeclaration->context) {
2726 parameter->parent_context = context;
2728 assert(parameter->parent_context == NULL
2729 || parameter->parent_context == context);
2730 parameter->parent_context = context;
2731 environment_push(parameter);
2734 if(declaration->init.statement != NULL) {
2735 parser_error_multiple_definition(declaration, token.source_position);
2737 goto end_of_parse_external_declaration;
2739 /* parse function body */
2740 int label_stack_top = label_top();
2741 declaration_t *old_current_function = current_function;
2742 current_function = declaration;
2744 declaration->init.statement = parse_compound_statement();
2745 check_for_missing_labels();
2747 assert(current_function == declaration);
2748 current_function = old_current_function;
2749 label_pop_to(label_stack_top);
2752 end_of_parse_external_declaration:
2753 assert(context == &declaration->context);
2754 set_context(last_context);
2755 environment_pop_to(top);
2758 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2760 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2761 type->bitfield.base = base;
2762 type->bitfield.size = size;
2767 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2769 /* TODO: check constraints for struct declarations (in specifiers) */
2771 declaration_t *declaration;
2773 if(token.type == ':') {
2776 type_t *base_type = specifiers->type;
2777 expression_t *size = parse_constant_expression();
2779 type_t *type = make_bitfield_type(base_type, size);
2781 declaration = allocate_declaration_zero();
2782 declaration->namespc = NAMESPACE_NORMAL;
2783 declaration->storage_class = STORAGE_CLASS_NONE;
2784 declaration->source_position = token.source_position;
2785 declaration->modifiers = specifiers->decl_modifiers;
2786 declaration->type = type;
2788 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2790 if(token.type == ':') {
2792 expression_t *size = parse_constant_expression();
2794 type_t *type = make_bitfield_type(declaration->type, size);
2795 declaration->type = type;
2798 record_declaration(declaration);
2800 if(token.type != ',')
2807 static void parse_compound_type_entries(void)
2811 while(token.type != '}' && token.type != T_EOF) {
2812 declaration_specifiers_t specifiers;
2813 memset(&specifiers, 0, sizeof(specifiers));
2814 parse_declaration_specifiers(&specifiers);
2816 parse_struct_declarators(&specifiers);
2818 if(token.type == T_EOF) {
2819 errorf(HERE, "EOF while parsing struct");
2824 static type_t *parse_typename(void)
2826 declaration_specifiers_t specifiers;
2827 memset(&specifiers, 0, sizeof(specifiers));
2828 parse_declaration_specifiers(&specifiers);
2829 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2830 /* TODO: improve error message, user does probably not know what a
2831 * storage class is...
2833 errorf(HERE, "typename may not have a storage class");
2836 type_t *result = parse_abstract_declarator(specifiers.type);
2844 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2845 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2846 expression_t *left);
2848 typedef struct expression_parser_function_t expression_parser_function_t;
2849 struct expression_parser_function_t {
2850 unsigned precedence;
2851 parse_expression_function parser;
2852 unsigned infix_precedence;
2853 parse_expression_infix_function infix_parser;
2856 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2859 * Creates a new invalid expression.
2861 static expression_t *create_invalid_expression(void)
2863 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2864 expression->base.source_position = token.source_position;
2869 * Prints an error message if an expression was expected but not read
2871 static expression_t *expected_expression_error(void)
2873 /* skip the error message if the error token was read */
2874 if (token.type != T_ERROR) {
2875 errorf(HERE, "expected expression, got token '%K'", &token);
2879 return create_invalid_expression();
2883 * Parse a string constant.
2885 static expression_t *parse_string_const(void)
2887 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2888 cnst->base.datatype = type_string;
2889 cnst->string.value = parse_string_literals();
2895 * Parse a wide string constant.
2897 static expression_t *parse_wide_string_const(void)
2899 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2900 cnst->base.datatype = type_wchar_t_ptr;
2901 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2907 * Parse an integer constant.
2909 static expression_t *parse_int_const(void)
2911 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2912 cnst->base.datatype = token.datatype;
2913 cnst->conste.v.int_value = token.v.intvalue;
2921 * Parse a float constant.
2923 static expression_t *parse_float_const(void)
2925 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2926 cnst->base.datatype = token.datatype;
2927 cnst->conste.v.float_value = token.v.floatvalue;
2934 static declaration_t *create_implicit_function(symbol_t *symbol,
2935 const source_position_t source_position)
2937 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2938 ntype->function.return_type = type_int;
2939 ntype->function.unspecified_parameters = true;
2941 type_t *type = typehash_insert(ntype);
2946 declaration_t *const declaration = allocate_declaration_zero();
2947 declaration->storage_class = STORAGE_CLASS_EXTERN;
2948 declaration->type = type;
2949 declaration->symbol = symbol;
2950 declaration->source_position = source_position;
2951 declaration->parent_context = global_context;
2953 context_t *old_context = context;
2954 set_context(global_context);
2956 environment_push(declaration);
2957 /* prepend the declaration to the global declarations list */
2958 declaration->next = context->declarations;
2959 context->declarations = declaration;
2961 assert(context == global_context);
2962 set_context(old_context);
2968 * Creates a return_type (func)(argument_type) function type if not
2971 * @param return_type the return type
2972 * @param argument_type the argument type
2974 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2976 function_parameter_t *parameter
2977 = obstack_alloc(type_obst, sizeof(parameter[0]));
2978 memset(parameter, 0, sizeof(parameter[0]));
2979 parameter->type = argument_type;
2981 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2982 type->function.return_type = return_type;
2983 type->function.parameters = parameter;
2985 type_t *result = typehash_insert(type);
2986 if(result != type) {
2994 * Creates a function type for some function like builtins.
2996 * @param symbol the symbol describing the builtin
2998 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3000 switch(symbol->ID) {
3001 case T___builtin_alloca:
3002 return make_function_1_type(type_void_ptr, type_size_t);
3003 case T___builtin_nan:
3004 return make_function_1_type(type_double, type_string);
3005 case T___builtin_nanf:
3006 return make_function_1_type(type_float, type_string);
3007 case T___builtin_nand:
3008 return make_function_1_type(type_long_double, type_string);
3009 case T___builtin_va_end:
3010 return make_function_1_type(type_void, type_valist);
3012 panic("not implemented builtin symbol found");
3017 * Performs automatic type cast as described in § 6.3.2.1.
3019 * @param orig_type the original type
3021 static type_t *automatic_type_conversion(type_t *orig_type)
3023 type_t *type = skip_typeref(orig_type);
3024 if(is_type_array(type)) {
3025 array_type_t *array_type = &type->array;
3026 type_t *element_type = array_type->element_type;
3027 unsigned qualifiers = array_type->type.qualifiers;
3029 return make_pointer_type(element_type, qualifiers);
3032 if(is_type_function(type)) {
3033 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3040 * reverts the automatic casts of array to pointer types and function
3041 * to function-pointer types as defined § 6.3.2.1
3043 type_t *revert_automatic_type_conversion(const expression_t *expression)
3045 switch (expression->kind) {
3046 case EXPR_REFERENCE: return expression->reference.declaration->type;
3047 case EXPR_SELECT: return expression->select.compound_entry->type;
3049 case EXPR_UNARY_DEREFERENCE: {
3050 const expression_t *const value = expression->unary.value;
3051 type_t *const type = skip_typeref(value->base.datatype);
3052 assert(is_type_pointer(type));
3053 return type->pointer.points_to;
3056 case EXPR_BUILTIN_SYMBOL:
3057 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3059 case EXPR_ARRAY_ACCESS: {
3060 const expression_t *const array_ref = expression->array_access.array_ref;
3061 type_t *const type_left = skip_typeref(array_ref->base.datatype);
3062 if (!is_type_valid(type_left))
3064 assert(is_type_pointer(type_left));
3065 return type_left->pointer.points_to;
3071 return expression->base.datatype;
3074 static expression_t *parse_reference(void)
3076 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3078 reference_expression_t *ref = &expression->reference;
3079 ref->symbol = token.v.symbol;
3081 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3083 source_position_t source_position = token.source_position;
3086 if(declaration == NULL) {
3087 if (! strict_mode && token.type == '(') {
3088 /* an implicitly defined function */
3089 warningf(HERE, "implicit declaration of function '%Y'",
3092 declaration = create_implicit_function(ref->symbol,
3095 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3100 type_t *type = declaration->type;
3102 /* we always do the auto-type conversions; the & and sizeof parser contains
3103 * code to revert this! */
3104 type = automatic_type_conversion(type);
3106 ref->declaration = declaration;
3107 ref->expression.datatype = type;
3112 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3116 /* TODO check if explicit cast is allowed and issue warnings/errors */
3119 static expression_t *parse_cast(void)
3121 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3123 cast->base.source_position = token.source_position;
3125 type_t *type = parse_typename();
3128 expression_t *value = parse_sub_expression(20);
3130 check_cast_allowed(value, type);
3132 cast->base.datatype = type;
3133 cast->unary.value = value;
3138 static expression_t *parse_statement_expression(void)
3140 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3142 statement_t *statement = parse_compound_statement();
3143 expression->statement.statement = statement;
3144 if(statement == NULL) {
3149 /* find last statement and use its type */
3150 type_t *type = type_void;
3151 const statement_t *stmt = statement->compound.statements;
3153 while (stmt->base.next != NULL)
3154 stmt = stmt->base.next;
3156 if (stmt->kind == STATEMENT_EXPRESSION) {
3157 type = stmt->expression.expression->base.datatype;
3160 expression->base.datatype = type;
3167 static expression_t *parse_brace_expression(void)
3171 switch(token.type) {
3173 /* gcc extension: a statement expression */
3174 return parse_statement_expression();
3178 return parse_cast();
3180 if(is_typedef_symbol(token.v.symbol)) {
3181 return parse_cast();
3185 expression_t *result = parse_expression();
3191 static expression_t *parse_function_keyword(void)
3196 if (current_function == NULL) {
3197 errorf(HERE, "'__func__' used outside of a function");
3200 string_literal_expression_t *expression
3201 = allocate_ast_zero(sizeof(expression[0]));
3203 expression->expression.kind = EXPR_FUNCTION;
3204 expression->expression.datatype = type_string;
3206 return (expression_t*) expression;
3209 static expression_t *parse_pretty_function_keyword(void)
3211 eat(T___PRETTY_FUNCTION__);
3214 if (current_function == NULL) {
3215 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3218 string_literal_expression_t *expression
3219 = allocate_ast_zero(sizeof(expression[0]));
3221 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3222 expression->expression.datatype = type_string;
3224 return (expression_t*) expression;
3227 static designator_t *parse_designator(void)
3229 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3231 if(token.type != T_IDENTIFIER) {
3232 parse_error_expected("while parsing member designator",
3237 result->symbol = token.v.symbol;
3240 designator_t *last_designator = result;
3242 if(token.type == '.') {
3244 if(token.type != T_IDENTIFIER) {
3245 parse_error_expected("while parsing member designator",
3250 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3251 designator->symbol = token.v.symbol;
3254 last_designator->next = designator;
3255 last_designator = designator;
3258 if(token.type == '[') {
3260 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3261 designator->array_access = parse_expression();
3262 if(designator->array_access == NULL) {
3268 last_designator->next = designator;
3269 last_designator = designator;
3278 static expression_t *parse_offsetof(void)
3280 eat(T___builtin_offsetof);
3282 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3283 expression->base.datatype = type_size_t;
3286 expression->offsetofe.type = parse_typename();
3288 expression->offsetofe.designator = parse_designator();
3294 static expression_t *parse_va_start(void)
3296 eat(T___builtin_va_start);
3298 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3301 expression->va_starte.ap = parse_assignment_expression();
3303 expression_t *const expr = parse_assignment_expression();
3304 if (expr->kind == EXPR_REFERENCE) {
3305 declaration_t *const decl = expr->reference.declaration;
3306 if (decl->parent_context == ¤t_function->context &&
3307 decl->next == NULL) {
3308 expression->va_starte.parameter = decl;
3313 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3315 return create_invalid_expression();
3318 static expression_t *parse_va_arg(void)
3320 eat(T___builtin_va_arg);
3322 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3325 expression->va_arge.ap = parse_assignment_expression();
3327 expression->base.datatype = parse_typename();
3333 static expression_t *parse_builtin_symbol(void)
3335 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3337 symbol_t *symbol = token.v.symbol;
3339 expression->builtin_symbol.symbol = symbol;
3342 type_t *type = get_builtin_symbol_type(symbol);
3343 type = automatic_type_conversion(type);
3345 expression->base.datatype = type;
3349 static expression_t *parse_builtin_constant(void)
3351 eat(T___builtin_constant_p);
3353 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3356 expression->builtin_constant.value = parse_assignment_expression();
3358 expression->base.datatype = type_int;
3363 static expression_t *parse_builtin_prefetch(void)
3365 eat(T___builtin_prefetch);
3367 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3370 expression->builtin_prefetch.adr = parse_assignment_expression();
3371 if (token.type == ',') {
3373 expression->builtin_prefetch.rw = parse_assignment_expression();
3375 if (token.type == ',') {
3377 expression->builtin_prefetch.locality = parse_assignment_expression();
3380 expression->base.datatype = type_void;
3385 static expression_t *parse_compare_builtin(void)
3387 expression_t *expression;
3389 switch(token.type) {
3390 case T___builtin_isgreater:
3391 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3393 case T___builtin_isgreaterequal:
3394 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3396 case T___builtin_isless:
3397 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3399 case T___builtin_islessequal:
3400 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3402 case T___builtin_islessgreater:
3403 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3405 case T___builtin_isunordered:
3406 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3409 panic("invalid compare builtin found");
3415 expression->binary.left = parse_assignment_expression();
3417 expression->binary.right = parse_assignment_expression();
3420 type_t *const orig_type_left = expression->binary.left->base.datatype;
3421 type_t *const orig_type_right = expression->binary.right->base.datatype;
3423 type_t *const type_left = skip_typeref(orig_type_left);
3424 type_t *const type_right = skip_typeref(orig_type_right);
3425 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3426 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3427 type_error_incompatible("invalid operands in comparison",
3428 token.source_position, orig_type_left, orig_type_right);
3431 semantic_comparison(&expression->binary);
3437 static expression_t *parse_builtin_expect(void)
3439 eat(T___builtin_expect);
3441 expression_t *expression
3442 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3445 expression->binary.left = parse_assignment_expression();
3447 expression->binary.right = parse_constant_expression();
3450 expression->base.datatype = expression->binary.left->base.datatype;
3455 static expression_t *parse_assume(void) {
3458 expression_t *expression
3459 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3462 expression->unary.value = parse_assignment_expression();
3465 expression->base.datatype = type_void;
3469 static expression_t *parse_alignof(void) {
3472 expression_t *expression
3473 = allocate_expression_zero(EXPR_ALIGNOF);
3476 expression->alignofe.type = parse_typename();
3479 expression->base.datatype = type_size_t;
3483 static expression_t *parse_primary_expression(void)
3485 switch(token.type) {
3487 return parse_int_const();
3488 case T_FLOATINGPOINT:
3489 return parse_float_const();
3490 case T_STRING_LITERAL:
3491 return parse_string_const();
3492 case T_WIDE_STRING_LITERAL:
3493 return parse_wide_string_const();
3495 return parse_reference();
3496 case T___FUNCTION__:
3498 return parse_function_keyword();
3499 case T___PRETTY_FUNCTION__:
3500 return parse_pretty_function_keyword();
3501 case T___builtin_offsetof:
3502 return parse_offsetof();
3503 case T___builtin_va_start:
3504 return parse_va_start();
3505 case T___builtin_va_arg:
3506 return parse_va_arg();
3507 case T___builtin_expect:
3508 return parse_builtin_expect();
3509 case T___builtin_nanf:
3510 case T___builtin_alloca:
3511 case T___builtin_va_end:
3512 return parse_builtin_symbol();
3513 case T___builtin_isgreater:
3514 case T___builtin_isgreaterequal:
3515 case T___builtin_isless:
3516 case T___builtin_islessequal:
3517 case T___builtin_islessgreater:
3518 case T___builtin_isunordered:
3519 return parse_compare_builtin();
3520 case T___builtin_constant_p:
3521 return parse_builtin_constant();
3522 case T___builtin_prefetch:
3523 return parse_builtin_prefetch();
3525 return parse_alignof();
3527 return parse_assume();
3530 return parse_brace_expression();
3533 errorf(HERE, "unexpected token '%K'", &token);
3536 return create_invalid_expression();
3540 * Check if the expression has the character type and issue a warning then.
3542 static void check_for_char_index_type(const expression_t *expression) {
3543 type_t *type = expression->base.datatype;
3544 type_t *base_type = skip_typeref(type);
3546 if (base_type->base.kind == TYPE_ATOMIC) {
3547 switch (base_type->atomic.akind == ATOMIC_TYPE_CHAR) {
3548 warningf(expression->base.source_position,
3549 "array subscript has type '%T'", type);
3554 static expression_t *parse_array_expression(unsigned precedence,
3561 expression_t *inside = parse_expression();
3563 array_access_expression_t *array_access
3564 = allocate_ast_zero(sizeof(array_access[0]));
3566 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3568 type_t *const orig_type_left = left->base.datatype;
3569 type_t *const orig_type_inside = inside->base.datatype;
3571 type_t *const type_left = skip_typeref(orig_type_left);
3572 type_t *const type_inside = skip_typeref(orig_type_inside);
3574 type_t *return_type;
3575 if (is_type_pointer(type_left)) {
3576 return_type = type_left->pointer.points_to;
3577 array_access->array_ref = left;
3578 array_access->index = inside;
3579 check_for_char_index_type(inside);
3580 } else if (is_type_pointer(type_inside)) {
3581 return_type = type_inside->pointer.points_to;
3582 array_access->array_ref = inside;
3583 array_access->index = left;
3584 array_access->flipped = true;
3585 check_for_char_index_type(left);
3587 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3589 "array access on object with non-pointer types '%T', '%T'",
3590 orig_type_left, orig_type_inside);
3592 return_type = type_error_type;
3593 array_access->array_ref = create_invalid_expression();
3596 if(token.type != ']') {
3597 parse_error_expected("Problem while parsing array access", ']', 0);
3598 return (expression_t*) array_access;
3602 return_type = automatic_type_conversion(return_type);
3603 array_access->expression.datatype = return_type;
3605 return (expression_t*) array_access;
3608 static expression_t *parse_sizeof(unsigned precedence)
3612 sizeof_expression_t *sizeof_expression
3613 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3614 sizeof_expression->expression.kind = EXPR_SIZEOF;
3615 sizeof_expression->expression.datatype = type_size_t;
3617 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3619 sizeof_expression->type = parse_typename();
3622 expression_t *expression = parse_sub_expression(precedence);
3623 expression->base.datatype = revert_automatic_type_conversion(expression);
3625 sizeof_expression->type = expression->base.datatype;
3626 sizeof_expression->size_expression = expression;
3629 return (expression_t*) sizeof_expression;
3632 static expression_t *parse_select_expression(unsigned precedence,
3633 expression_t *compound)
3636 assert(token.type == '.' || token.type == T_MINUSGREATER);
3638 bool is_pointer = (token.type == T_MINUSGREATER);
3641 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3642 select->select.compound = compound;
3644 if(token.type != T_IDENTIFIER) {
3645 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3648 symbol_t *symbol = token.v.symbol;
3649 select->select.symbol = symbol;
3652 type_t *const orig_type = compound->base.datatype;
3653 type_t *const type = skip_typeref(orig_type);
3655 type_t *type_left = type;
3657 if (!is_type_pointer(type)) {
3658 if (is_type_valid(type)) {
3659 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3661 return create_invalid_expression();
3663 type_left = type->pointer.points_to;
3665 type_left = skip_typeref(type_left);
3667 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3668 type_left->kind != TYPE_COMPOUND_UNION) {
3669 if (is_type_valid(type_left)) {
3670 errorf(HERE, "request for member '%Y' in something not a struct or "
3671 "union, but '%T'", symbol, type_left);
3673 return create_invalid_expression();
3676 declaration_t *const declaration = type_left->compound.declaration;
3678 if(!declaration->init.is_defined) {
3679 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3681 return create_invalid_expression();
3684 declaration_t *iter = declaration->context.declarations;
3685 for( ; iter != NULL; iter = iter->next) {
3686 if(iter->symbol == symbol) {
3691 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3692 return create_invalid_expression();
3695 /* we always do the auto-type conversions; the & and sizeof parser contains
3696 * code to revert this! */
3697 type_t *expression_type = automatic_type_conversion(iter->type);
3699 select->select.compound_entry = iter;
3700 select->base.datatype = expression_type;
3702 if(expression_type->kind == TYPE_BITFIELD) {
3703 expression_t *extract
3704 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3705 extract->unary.value = select;
3706 extract->base.datatype = expression_type->bitfield.base;
3715 * Parse a call expression, ie. expression '( ... )'.
3717 * @param expression the function address
3719 static expression_t *parse_call_expression(unsigned precedence,
3720 expression_t *expression)
3723 expression_t *result = allocate_expression_zero(EXPR_CALL);
3725 call_expression_t *call = &result->call;
3726 call->function = expression;
3728 type_t *const orig_type = expression->base.datatype;
3729 type_t *const type = skip_typeref(orig_type);
3731 function_type_t *function_type = NULL;
3732 if (is_type_pointer(type)) {
3733 type_t *const to_type = skip_typeref(type->pointer.points_to);
3735 if (is_type_function(to_type)) {
3736 function_type = &to_type->function;
3737 call->expression.datatype = function_type->return_type;
3741 if (function_type == NULL && is_type_valid(type)) {
3742 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3745 /* parse arguments */
3748 if(token.type != ')') {
3749 call_argument_t *last_argument = NULL;
3752 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3754 argument->expression = parse_assignment_expression();
3755 if(last_argument == NULL) {
3756 call->arguments = argument;
3758 last_argument->next = argument;
3760 last_argument = argument;
3762 if(token.type != ',')
3769 if(function_type != NULL) {
3770 function_parameter_t *parameter = function_type->parameters;
3771 call_argument_t *argument = call->arguments;
3772 for( ; parameter != NULL && argument != NULL;
3773 parameter = parameter->next, argument = argument->next) {
3774 type_t *expected_type = parameter->type;
3775 /* TODO report context in error messages */
3776 expression_t *const arg_expr = argument->expression;
3777 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
3778 if (res_type == NULL) {
3779 /* TODO improve error message */
3780 errorf(arg_expr->base.source_position,
3781 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
3782 arg_expr, arg_expr->base.datatype, expected_type);
3784 argument->expression = create_implicit_cast(argument->expression, expected_type);
3787 /* too few parameters */
3788 if(parameter != NULL) {
3789 errorf(HERE, "too few arguments to function '%E'", expression);
3790 } else if(argument != NULL) {
3791 /* too many parameters */
3792 if(!function_type->variadic
3793 && !function_type->unspecified_parameters) {
3794 errorf(HERE, "too many arguments to function '%E'", expression);
3796 /* do default promotion */
3797 for( ; argument != NULL; argument = argument->next) {
3798 type_t *type = argument->expression->base.datatype;
3800 type = skip_typeref(type);
3801 if(is_type_integer(type)) {
3802 type = promote_integer(type);
3803 } else if(type == type_float) {
3807 argument->expression
3808 = create_implicit_cast(argument->expression, type);
3811 check_format(&result->call);
3814 check_format(&result->call);
3821 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3823 static bool same_compound_type(const type_t *type1, const type_t *type2)
3826 is_type_compound(type1) &&
3827 type1->kind == type2->kind &&
3828 type1->compound.declaration == type2->compound.declaration;
3832 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3834 * @param expression the conditional expression
3836 static expression_t *parse_conditional_expression(unsigned precedence,
3837 expression_t *expression)
3841 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3843 conditional_expression_t *conditional = &result->conditional;
3844 conditional->condition = expression;
3847 type_t *const condition_type_orig = expression->base.datatype;
3848 type_t *const condition_type = skip_typeref(condition_type_orig);
3849 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
3850 type_error("expected a scalar type in conditional condition",
3851 expression->base.source_position, condition_type_orig);
3854 expression_t *true_expression = parse_expression();
3856 expression_t *false_expression = parse_sub_expression(precedence);
3858 conditional->true_expression = true_expression;
3859 conditional->false_expression = false_expression;
3861 type_t *const orig_true_type = true_expression->base.datatype;
3862 type_t *const orig_false_type = false_expression->base.datatype;
3863 type_t *const true_type = skip_typeref(orig_true_type);
3864 type_t *const false_type = skip_typeref(orig_false_type);
3867 type_t *result_type;
3868 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3869 result_type = semantic_arithmetic(true_type, false_type);
3871 true_expression = create_implicit_cast(true_expression, result_type);
3872 false_expression = create_implicit_cast(false_expression, result_type);
3874 conditional->true_expression = true_expression;
3875 conditional->false_expression = false_expression;
3876 conditional->expression.datatype = result_type;
3877 } else if (same_compound_type(true_type, false_type) || (
3878 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3879 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
3881 /* just take 1 of the 2 types */
3882 result_type = true_type;
3883 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3884 && pointers_compatible(true_type, false_type)) {
3886 result_type = true_type;
3889 if (is_type_valid(true_type) && is_type_valid(false_type)) {
3890 type_error_incompatible("while parsing conditional",
3891 expression->base.source_position, true_type,
3894 result_type = type_error_type;
3897 conditional->expression.datatype = result_type;
3902 * Parse an extension expression.
3904 static expression_t *parse_extension(unsigned precedence)
3906 eat(T___extension__);
3908 /* TODO enable extensions */
3909 expression_t *expression = parse_sub_expression(precedence);
3910 /* TODO disable extensions */
3914 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3916 eat(T___builtin_classify_type);
3918 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3919 result->base.datatype = type_int;
3922 expression_t *expression = parse_sub_expression(precedence);
3924 result->classify_type.type_expression = expression;
3929 static void semantic_incdec(unary_expression_t *expression)
3931 type_t *const orig_type = expression->value->base.datatype;
3932 type_t *const type = skip_typeref(orig_type);
3933 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3934 if (is_type_valid(type)) {
3935 /* TODO: improve error message */
3936 errorf(HERE, "operation needs an arithmetic or pointer type");
3941 expression->expression.datatype = orig_type;
3944 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3946 type_t *const orig_type = expression->value->base.datatype;
3947 type_t *const type = skip_typeref(orig_type);
3948 if(!is_type_arithmetic(type)) {
3949 if (is_type_valid(type)) {
3950 /* TODO: improve error message */
3951 errorf(HERE, "operation needs an arithmetic type");
3956 expression->expression.datatype = orig_type;
3959 static void semantic_unexpr_scalar(unary_expression_t *expression)
3961 type_t *const orig_type = expression->value->base.datatype;
3962 type_t *const type = skip_typeref(orig_type);
3963 if (!is_type_scalar(type)) {
3964 if (is_type_valid(type)) {
3965 errorf(HERE, "operand of ! must be of scalar type");
3970 expression->expression.datatype = orig_type;
3973 static void semantic_unexpr_integer(unary_expression_t *expression)
3975 type_t *const orig_type = expression->value->base.datatype;
3976 type_t *const type = skip_typeref(orig_type);
3977 if (!is_type_integer(type)) {
3978 if (is_type_valid(type)) {
3979 errorf(HERE, "operand of ~ must be of integer type");
3984 expression->expression.datatype = orig_type;
3987 static void semantic_dereference(unary_expression_t *expression)
3989 type_t *const orig_type = expression->value->base.datatype;
3990 type_t *const type = skip_typeref(orig_type);
3991 if(!is_type_pointer(type)) {
3992 if (is_type_valid(type)) {
3993 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
3998 type_t *result_type = type->pointer.points_to;
3999 result_type = automatic_type_conversion(result_type);
4000 expression->expression.datatype = result_type;
4004 * Check the semantic of the address taken expression.
4006 static void semantic_take_addr(unary_expression_t *expression)
4008 expression_t *value = expression->value;
4009 value->base.datatype = revert_automatic_type_conversion(value);
4011 type_t *orig_type = value->base.datatype;
4012 if(!is_type_valid(orig_type))
4015 if(value->kind == EXPR_REFERENCE) {
4016 declaration_t *const declaration = value->reference.declaration;
4017 if(declaration != NULL) {
4018 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4019 errorf(expression->expression.source_position,
4020 "address of register variable '%Y' requested",
4021 declaration->symbol);
4023 declaration->address_taken = 1;
4027 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4030 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4031 static expression_t *parse_##unexpression_type(unsigned precedence) \
4035 expression_t *unary_expression \
4036 = allocate_expression_zero(unexpression_type); \
4037 unary_expression->base.source_position = HERE; \
4038 unary_expression->unary.value = parse_sub_expression(precedence); \
4040 sfunc(&unary_expression->unary); \
4042 return unary_expression; \
4045 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4046 semantic_unexpr_arithmetic)
4047 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4048 semantic_unexpr_arithmetic)
4049 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4050 semantic_unexpr_scalar)
4051 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4052 semantic_dereference)
4053 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4055 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4056 semantic_unexpr_integer)
4057 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4059 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4062 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4064 static expression_t *parse_##unexpression_type(unsigned precedence, \
4065 expression_t *left) \
4067 (void) precedence; \
4070 expression_t *unary_expression \
4071 = allocate_expression_zero(unexpression_type); \
4072 unary_expression->unary.value = left; \
4074 sfunc(&unary_expression->unary); \
4076 return unary_expression; \
4079 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4080 EXPR_UNARY_POSTFIX_INCREMENT,
4082 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4083 EXPR_UNARY_POSTFIX_DECREMENT,
4086 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4088 /* TODO: handle complex + imaginary types */
4090 /* § 6.3.1.8 Usual arithmetic conversions */
4091 if(type_left == type_long_double || type_right == type_long_double) {
4092 return type_long_double;
4093 } else if(type_left == type_double || type_right == type_double) {
4095 } else if(type_left == type_float || type_right == type_float) {
4099 type_right = promote_integer(type_right);
4100 type_left = promote_integer(type_left);
4102 if(type_left == type_right)
4105 bool signed_left = is_type_signed(type_left);
4106 bool signed_right = is_type_signed(type_right);
4107 int rank_left = get_rank(type_left);
4108 int rank_right = get_rank(type_right);
4109 if(rank_left < rank_right) {
4110 if(signed_left == signed_right || !signed_right) {
4116 if(signed_left == signed_right || !signed_left) {
4125 * Check the semantic restrictions for a binary expression.
4127 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4129 expression_t *const left = expression->left;
4130 expression_t *const right = expression->right;
4131 type_t *const orig_type_left = left->base.datatype;
4132 type_t *const orig_type_right = right->base.datatype;
4133 type_t *const type_left = skip_typeref(orig_type_left);
4134 type_t *const type_right = skip_typeref(orig_type_right);
4136 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4137 /* TODO: improve error message */
4138 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4139 errorf(HERE, "operation needs arithmetic types");
4144 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4145 expression->left = create_implicit_cast(left, arithmetic_type);
4146 expression->right = create_implicit_cast(right, arithmetic_type);
4147 expression->expression.datatype = arithmetic_type;
4150 static void semantic_shift_op(binary_expression_t *expression)
4152 expression_t *const left = expression->left;
4153 expression_t *const right = expression->right;
4154 type_t *const orig_type_left = left->base.datatype;
4155 type_t *const orig_type_right = right->base.datatype;
4156 type_t * type_left = skip_typeref(orig_type_left);
4157 type_t * type_right = skip_typeref(orig_type_right);
4159 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4160 /* TODO: improve error message */
4161 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4162 errorf(HERE, "operation needs integer types");
4167 type_left = promote_integer(type_left);
4168 type_right = promote_integer(type_right);
4170 expression->left = create_implicit_cast(left, type_left);
4171 expression->right = create_implicit_cast(right, type_right);
4172 expression->expression.datatype = type_left;
4175 static void semantic_add(binary_expression_t *expression)
4177 expression_t *const left = expression->left;
4178 expression_t *const right = expression->right;
4179 type_t *const orig_type_left = left->base.datatype;
4180 type_t *const orig_type_right = right->base.datatype;
4181 type_t *const type_left = skip_typeref(orig_type_left);
4182 type_t *const type_right = skip_typeref(orig_type_right);
4185 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4186 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4187 expression->left = create_implicit_cast(left, arithmetic_type);
4188 expression->right = create_implicit_cast(right, arithmetic_type);
4189 expression->expression.datatype = arithmetic_type;
4191 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4192 expression->expression.datatype = type_left;
4193 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4194 expression->expression.datatype = type_right;
4195 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4196 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4200 static void semantic_sub(binary_expression_t *expression)
4202 expression_t *const left = expression->left;
4203 expression_t *const right = expression->right;
4204 type_t *const orig_type_left = left->base.datatype;
4205 type_t *const orig_type_right = right->base.datatype;
4206 type_t *const type_left = skip_typeref(orig_type_left);
4207 type_t *const type_right = skip_typeref(orig_type_right);
4210 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4211 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4212 expression->left = create_implicit_cast(left, arithmetic_type);
4213 expression->right = create_implicit_cast(right, arithmetic_type);
4214 expression->expression.datatype = arithmetic_type;
4216 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4217 expression->expression.datatype = type_left;
4218 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4219 if(!pointers_compatible(type_left, type_right)) {
4220 errorf(HERE, "pointers to incompatible objects to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4222 expression->expression.datatype = type_ptrdiff_t;
4224 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4225 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4229 static void semantic_comparison(binary_expression_t *expression)
4231 expression_t *left = expression->left;
4232 expression_t *right = expression->right;
4233 type_t *orig_type_left = left->base.datatype;
4234 type_t *orig_type_right = right->base.datatype;
4236 type_t *type_left = skip_typeref(orig_type_left);
4237 type_t *type_right = skip_typeref(orig_type_right);
4239 /* TODO non-arithmetic types */
4240 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4241 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4242 expression->left = create_implicit_cast(left, arithmetic_type);
4243 expression->right = create_implicit_cast(right, arithmetic_type);
4244 expression->expression.datatype = arithmetic_type;
4245 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4246 /* TODO check compatibility */
4247 } else if (is_type_pointer(type_left)) {
4248 expression->right = create_implicit_cast(right, type_left);
4249 } else if (is_type_pointer(type_right)) {
4250 expression->left = create_implicit_cast(left, type_right);
4251 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4252 type_error_incompatible("invalid operands in comparison",
4253 token.source_position, type_left, type_right);
4255 expression->expression.datatype = type_int;
4258 static void semantic_arithmetic_assign(binary_expression_t *expression)
4260 expression_t *left = expression->left;
4261 expression_t *right = expression->right;
4262 type_t *orig_type_left = left->base.datatype;
4263 type_t *orig_type_right = right->base.datatype;
4265 type_t *type_left = skip_typeref(orig_type_left);
4266 type_t *type_right = skip_typeref(orig_type_right);
4268 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4269 /* TODO: improve error message */
4270 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4271 errorf(HERE, "operation needs arithmetic types");
4276 /* combined instructions are tricky. We can't create an implicit cast on
4277 * the left side, because we need the uncasted form for the store.
4278 * The ast2firm pass has to know that left_type must be right_type
4279 * for the arithmetic operation and create a cast by itself */
4280 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4281 expression->right = create_implicit_cast(right, arithmetic_type);
4282 expression->expression.datatype = type_left;
4285 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4287 expression_t *const left = expression->left;
4288 expression_t *const right = expression->right;
4289 type_t *const orig_type_left = left->base.datatype;
4290 type_t *const orig_type_right = right->base.datatype;
4291 type_t *const type_left = skip_typeref(orig_type_left);
4292 type_t *const type_right = skip_typeref(orig_type_right);
4294 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4295 /* combined instructions are tricky. We can't create an implicit cast on
4296 * the left side, because we need the uncasted form for the store.
4297 * The ast2firm pass has to know that left_type must be right_type
4298 * for the arithmetic operation and create a cast by itself */
4299 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4300 expression->right = create_implicit_cast(right, arithmetic_type);
4301 expression->expression.datatype = type_left;
4302 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4303 expression->expression.datatype = type_left;
4304 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4305 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4310 * Check the semantic restrictions of a logical expression.
4312 static void semantic_logical_op(binary_expression_t *expression)
4314 expression_t *const left = expression->left;
4315 expression_t *const right = expression->right;
4316 type_t *const orig_type_left = left->base.datatype;
4317 type_t *const orig_type_right = right->base.datatype;
4318 type_t *const type_left = skip_typeref(orig_type_left);
4319 type_t *const type_right = skip_typeref(orig_type_right);
4321 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4322 /* TODO: improve error message */
4323 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4324 errorf(HERE, "operation needs scalar types");
4329 expression->expression.datatype = type_int;
4333 * Checks if a compound type has constant fields.
4335 static bool has_const_fields(const compound_type_t *type)
4337 const context_t *context = &type->declaration->context;
4338 const declaration_t *declaration = context->declarations;
4340 for (; declaration != NULL; declaration = declaration->next) {
4341 if (declaration->namespc != NAMESPACE_NORMAL)
4344 const type_t *decl_type = skip_typeref(declaration->type);
4345 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4353 * Check the semantic restrictions of a binary assign expression.
4355 static void semantic_binexpr_assign(binary_expression_t *expression)
4357 expression_t *left = expression->left;
4358 type_t *orig_type_left = left->base.datatype;
4360 type_t *type_left = revert_automatic_type_conversion(left);
4361 type_left = skip_typeref(orig_type_left);
4363 /* must be a modifiable lvalue */
4364 if (is_type_array(type_left)) {
4365 errorf(HERE, "cannot assign to arrays ('%E')", left);
4368 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4369 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4373 if(is_type_incomplete(type_left)) {
4375 "left-hand side of assignment '%E' has incomplete type '%T'",
4376 left, orig_type_left);
4379 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4380 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4381 left, orig_type_left);
4385 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4387 if (res_type == NULL) {
4388 errorf(expression->expression.source_position,
4389 "cannot assign to '%T' from '%T'",
4390 orig_type_left, expression->right->base.datatype);
4392 expression->right = create_implicit_cast(expression->right, res_type);
4395 expression->expression.datatype = orig_type_left;
4398 static void semantic_comma(binary_expression_t *expression)
4400 expression->expression.datatype = expression->right->base.datatype;
4403 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4404 static expression_t *parse_##binexpression_type(unsigned precedence, \
4405 expression_t *left) \
4409 expression_t *right = parse_sub_expression(precedence + lr); \
4411 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4412 binexpr->binary.left = left; \
4413 binexpr->binary.right = right; \
4414 sfunc(&binexpr->binary); \
4419 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4420 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4421 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4422 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4423 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4424 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4425 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4426 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4427 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4429 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4430 semantic_comparison, 1)
4431 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4432 semantic_comparison, 1)
4433 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4434 semantic_comparison, 1)
4435 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4436 semantic_comparison, 1)
4438 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4439 semantic_binexpr_arithmetic, 1)
4440 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4441 semantic_binexpr_arithmetic, 1)
4442 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4443 semantic_binexpr_arithmetic, 1)
4444 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4445 semantic_logical_op, 1)
4446 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4447 semantic_logical_op, 1)
4448 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4449 semantic_shift_op, 1)
4450 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4451 semantic_shift_op, 1)
4452 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4453 semantic_arithmetic_addsubb_assign, 0)
4454 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4455 semantic_arithmetic_addsubb_assign, 0)
4456 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4457 semantic_arithmetic_assign, 0)
4458 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4459 semantic_arithmetic_assign, 0)
4460 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4461 semantic_arithmetic_assign, 0)
4462 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4463 semantic_arithmetic_assign, 0)
4464 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4465 semantic_arithmetic_assign, 0)
4466 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4467 semantic_arithmetic_assign, 0)
4468 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4469 semantic_arithmetic_assign, 0)
4470 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4471 semantic_arithmetic_assign, 0)
4473 static expression_t *parse_sub_expression(unsigned precedence)
4475 if(token.type < 0) {
4476 return expected_expression_error();
4479 expression_parser_function_t *parser
4480 = &expression_parsers[token.type];
4481 source_position_t source_position = token.source_position;
4484 if(parser->parser != NULL) {
4485 left = parser->parser(parser->precedence);
4487 left = parse_primary_expression();
4489 assert(left != NULL);
4490 left->base.source_position = source_position;
4493 if(token.type < 0) {
4494 return expected_expression_error();
4497 parser = &expression_parsers[token.type];
4498 if(parser->infix_parser == NULL)
4500 if(parser->infix_precedence < precedence)
4503 left = parser->infix_parser(parser->infix_precedence, left);
4505 assert(left != NULL);
4506 assert(left->kind != EXPR_UNKNOWN);
4507 left->base.source_position = source_position;
4514 * Parse an expression.
4516 static expression_t *parse_expression(void)
4518 return parse_sub_expression(1);
4522 * Register a parser for a prefix-like operator with given precedence.
4524 * @param parser the parser function
4525 * @param token_type the token type of the prefix token
4526 * @param precedence the precedence of the operator
4528 static void register_expression_parser(parse_expression_function parser,
4529 int token_type, unsigned precedence)
4531 expression_parser_function_t *entry = &expression_parsers[token_type];
4533 if(entry->parser != NULL) {
4534 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4535 panic("trying to register multiple expression parsers for a token");
4537 entry->parser = parser;
4538 entry->precedence = precedence;
4542 * Register a parser for an infix operator with given precedence.
4544 * @param parser the parser function
4545 * @param token_type the token type of the infix operator
4546 * @param precedence the precedence of the operator
4548 static void register_infix_parser(parse_expression_infix_function parser,
4549 int token_type, unsigned precedence)
4551 expression_parser_function_t *entry = &expression_parsers[token_type];
4553 if(entry->infix_parser != NULL) {
4554 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4555 panic("trying to register multiple infix expression parsers for a "
4558 entry->infix_parser = parser;
4559 entry->infix_precedence = precedence;
4563 * Initialize the expression parsers.
4565 static void init_expression_parsers(void)
4567 memset(&expression_parsers, 0, sizeof(expression_parsers));
4569 register_infix_parser(parse_array_expression, '[', 30);
4570 register_infix_parser(parse_call_expression, '(', 30);
4571 register_infix_parser(parse_select_expression, '.', 30);
4572 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4573 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4575 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4578 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4579 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4580 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4581 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4582 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4583 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4584 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4585 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4586 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4587 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4588 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4589 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4590 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4591 T_EXCLAMATIONMARKEQUAL, 13);
4592 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4593 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4594 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4595 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4596 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4597 register_infix_parser(parse_conditional_expression, '?', 7);
4598 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4599 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4600 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4601 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4602 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4603 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4604 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4605 T_LESSLESSEQUAL, 2);
4606 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4607 T_GREATERGREATEREQUAL, 2);
4608 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4610 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4612 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4615 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4617 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4618 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4619 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4620 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4621 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4622 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4623 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4625 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4627 register_expression_parser(parse_sizeof, T_sizeof, 25);
4628 register_expression_parser(parse_extension, T___extension__, 25);
4629 register_expression_parser(parse_builtin_classify_type,
4630 T___builtin_classify_type, 25);
4634 * Parse a asm statement constraints specification.
4636 static asm_constraint_t *parse_asm_constraints(void)
4638 asm_constraint_t *result = NULL;
4639 asm_constraint_t *last = NULL;
4641 while(token.type == T_STRING_LITERAL || token.type == '[') {
4642 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4643 memset(constraint, 0, sizeof(constraint[0]));
4645 if(token.type == '[') {
4647 if(token.type != T_IDENTIFIER) {
4648 parse_error_expected("while parsing asm constraint",
4652 constraint->symbol = token.v.symbol;
4657 constraint->constraints = parse_string_literals();
4659 constraint->expression = parse_expression();
4663 last->next = constraint;
4665 result = constraint;
4669 if(token.type != ',')
4678 * Parse a asm statement clobber specification.
4680 static asm_clobber_t *parse_asm_clobbers(void)
4682 asm_clobber_t *result = NULL;
4683 asm_clobber_t *last = NULL;
4685 while(token.type == T_STRING_LITERAL) {
4686 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4687 clobber->clobber = parse_string_literals();
4690 last->next = clobber;
4696 if(token.type != ',')
4705 * Parse an asm statement.
4707 static statement_t *parse_asm_statement(void)
4711 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4712 statement->base.source_position = token.source_position;
4714 asm_statement_t *asm_statement = &statement->asms;
4716 if(token.type == T_volatile) {
4718 asm_statement->is_volatile = true;
4722 asm_statement->asm_text = parse_string_literals();
4724 if(token.type != ':')
4728 asm_statement->inputs = parse_asm_constraints();
4729 if(token.type != ':')
4733 asm_statement->outputs = parse_asm_constraints();
4734 if(token.type != ':')
4738 asm_statement->clobbers = parse_asm_clobbers();
4747 * Parse a case statement.
4749 static statement_t *parse_case_statement(void)
4753 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4755 statement->base.source_position = token.source_position;
4756 statement->case_label.expression = parse_expression();
4760 if (! is_constant_expression(statement->case_label.expression)) {
4761 errorf(statement->base.source_position,
4762 "case label does not reduce to an integer constant");
4764 /* TODO: check if the case label is already known */
4765 if (current_switch != NULL) {
4766 /* link all cases into the switch statement */
4767 if (current_switch->last_case == NULL) {
4768 current_switch->first_case =
4769 current_switch->last_case = &statement->case_label;
4771 current_switch->last_case->next = &statement->case_label;
4774 errorf(statement->base.source_position,
4775 "case label not within a switch statement");
4778 statement->case_label.label_statement = parse_statement();
4784 * Finds an existing default label of a switch statement.
4786 static case_label_statement_t *
4787 find_default_label(const switch_statement_t *statement)
4789 for (case_label_statement_t *label = statement->first_case;
4791 label = label->next) {
4792 if (label->expression == NULL)
4799 * Parse a default statement.
4801 static statement_t *parse_default_statement(void)
4805 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4807 statement->base.source_position = token.source_position;
4810 if (current_switch != NULL) {
4811 const case_label_statement_t *def_label = find_default_label(current_switch);
4812 if (def_label != NULL) {
4813 errorf(HERE, "multiple default labels in one switch");
4814 errorf(def_label->statement.source_position,
4815 "this is the first default label");
4817 /* link all cases into the switch statement */
4818 if (current_switch->last_case == NULL) {
4819 current_switch->first_case =
4820 current_switch->last_case = &statement->case_label;
4822 current_switch->last_case->next = &statement->case_label;
4826 errorf(statement->base.source_position,
4827 "'default' label not within a switch statement");
4829 statement->label.label_statement = parse_statement();
4835 * Return the declaration for a given label symbol or create a new one.
4837 static declaration_t *get_label(symbol_t *symbol)
4839 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4840 assert(current_function != NULL);
4841 /* if we found a label in the same function, then we already created the
4843 if(candidate != NULL
4844 && candidate->parent_context == ¤t_function->context) {
4848 /* otherwise we need to create a new one */
4849 declaration_t *const declaration = allocate_declaration_zero();
4850 declaration->namespc = NAMESPACE_LABEL;
4851 declaration->symbol = symbol;
4853 label_push(declaration);
4859 * Parse a label statement.
4861 static statement_t *parse_label_statement(void)
4863 assert(token.type == T_IDENTIFIER);
4864 symbol_t *symbol = token.v.symbol;
4867 declaration_t *label = get_label(symbol);
4869 /* if source position is already set then the label is defined twice,
4870 * otherwise it was just mentioned in a goto so far */
4871 if(label->source_position.input_name != NULL) {
4872 errorf(HERE, "duplicate label '%Y'", symbol);
4873 errorf(label->source_position, "previous definition of '%Y' was here",
4876 label->source_position = token.source_position;
4879 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4881 label_statement->statement.kind = STATEMENT_LABEL;
4882 label_statement->statement.source_position = token.source_position;
4883 label_statement->label = label;
4887 if(token.type == '}') {
4888 /* TODO only warn? */
4889 errorf(HERE, "label at end of compound statement");
4890 return (statement_t*) label_statement;
4892 label_statement->label_statement = parse_statement();
4895 return (statement_t*) label_statement;
4899 * Parse an if statement.
4901 static statement_t *parse_if(void)
4905 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4906 statement->statement.kind = STATEMENT_IF;
4907 statement->statement.source_position = token.source_position;
4910 statement->condition = parse_expression();
4913 statement->true_statement = parse_statement();
4914 if(token.type == T_else) {
4916 statement->false_statement = parse_statement();
4919 return (statement_t*) statement;
4923 * Parse a switch statement.
4925 static statement_t *parse_switch(void)
4929 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4930 statement->statement.kind = STATEMENT_SWITCH;
4931 statement->statement.source_position = token.source_position;
4934 expression_t *const expr = parse_expression();
4935 type_t * type = skip_typeref(expr->base.datatype);
4936 if (is_type_integer(type)) {
4937 type = promote_integer(type);
4938 } else if (is_type_valid(type)) {
4939 errorf(expr->base.source_position, "switch quantity is not an integer, but '%T'", type);
4940 type = type_error_type;
4942 statement->expression = create_implicit_cast(expr, type);
4945 switch_statement_t *rem = current_switch;
4946 current_switch = statement;
4947 statement->body = parse_statement();
4948 current_switch = rem;
4950 return (statement_t*) statement;
4953 static statement_t *parse_loop_body(statement_t *const loop)
4955 statement_t *const rem = current_loop;
4956 current_loop = loop;
4957 statement_t *const body = parse_statement();
4963 * Parse a while statement.
4965 static statement_t *parse_while(void)
4969 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4970 statement->statement.kind = STATEMENT_WHILE;
4971 statement->statement.source_position = token.source_position;
4974 statement->condition = parse_expression();
4977 statement->body = parse_loop_body((statement_t*)statement);
4979 return (statement_t*) statement;
4983 * Parse a do statement.
4985 static statement_t *parse_do(void)
4989 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4990 statement->statement.kind = STATEMENT_DO_WHILE;
4991 statement->statement.source_position = token.source_position;
4993 statement->body = parse_loop_body((statement_t*)statement);
4996 statement->condition = parse_expression();
5000 return (statement_t*) statement;
5004 * Parse a for statement.
5006 static statement_t *parse_for(void)
5010 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5011 statement->statement.kind = STATEMENT_FOR;
5012 statement->statement.source_position = token.source_position;
5016 int top = environment_top();
5017 context_t *last_context = context;
5018 set_context(&statement->context);
5020 if(token.type != ';') {
5021 if(is_declaration_specifier(&token, false)) {
5022 parse_declaration(record_declaration);
5024 statement->initialisation = parse_expression();
5031 if(token.type != ';') {
5032 statement->condition = parse_expression();
5035 if(token.type != ')') {
5036 statement->step = parse_expression();
5039 statement->body = parse_loop_body((statement_t*)statement);
5041 assert(context == &statement->context);
5042 set_context(last_context);
5043 environment_pop_to(top);
5045 return (statement_t*) statement;
5049 * Parse a goto statement.
5051 static statement_t *parse_goto(void)
5055 if(token.type != T_IDENTIFIER) {
5056 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5060 symbol_t *symbol = token.v.symbol;
5063 declaration_t *label = get_label(symbol);
5065 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5067 statement->statement.kind = STATEMENT_GOTO;
5068 statement->statement.source_position = token.source_position;
5070 statement->label = label;
5072 /* remember the goto's in a list for later checking */
5073 if (goto_last == NULL) {
5074 goto_first = goto_last = statement;
5076 goto_last->next = statement;
5081 return (statement_t*) statement;
5085 * Parse a continue statement.
5087 static statement_t *parse_continue(void)
5089 statement_t *statement;
5090 if (current_loop == NULL) {
5091 errorf(HERE, "continue statement not within loop");
5094 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5096 statement->base.source_position = token.source_position;
5106 * Parse a break statement.
5108 static statement_t *parse_break(void)
5110 statement_t *statement;
5111 if (current_switch == NULL && current_loop == NULL) {
5112 errorf(HERE, "break statement not within loop or switch");
5115 statement = allocate_statement_zero(STATEMENT_BREAK);
5117 statement->base.source_position = token.source_position;
5127 * Check if a given declaration represents a local variable.
5129 static bool is_local_var_declaration(const declaration_t *declaration) {
5130 switch ((storage_class_tag_t) declaration->storage_class) {
5131 case STORAGE_CLASS_NONE:
5132 case STORAGE_CLASS_AUTO:
5133 case STORAGE_CLASS_REGISTER: {
5134 const type_t *type = skip_typeref(declaration->type);
5135 if(is_type_function(type)) {
5147 * Check if a given expression represents a local variable.
5149 static bool is_local_variable(const expression_t *expression)
5151 if (expression->base.kind != EXPR_REFERENCE) {
5154 const declaration_t *declaration = expression->reference.declaration;
5155 return is_local_var_declaration(declaration);
5159 * Parse a return statement.
5161 static statement_t *parse_return(void)
5165 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5167 statement->statement.kind = STATEMENT_RETURN;
5168 statement->statement.source_position = token.source_position;
5170 expression_t *return_value = NULL;
5171 if(token.type != ';') {
5172 return_value = parse_expression();
5176 const type_t *const func_type = current_function->type;
5177 assert(is_type_function(func_type));
5178 type_t *const return_type = skip_typeref(func_type->function.return_type);
5180 if(return_value != NULL) {
5181 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5183 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5184 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5185 warningf(statement->statement.source_position,
5186 "'return' with a value, in function returning void");
5187 return_value = NULL;
5189 type_t *const res_type = semantic_assign(return_type,
5190 return_value, "'return'");
5191 if (res_type == NULL) {
5192 errorf(statement->statement.source_position,
5193 "cannot return something of type '%T' in function returning '%T'",
5194 return_value->base.datatype, return_type);
5196 return_value = create_implicit_cast(return_value, res_type);
5199 /* check for returning address of a local var */
5200 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5201 const expression_t *expression = return_value->unary.value;
5202 if (is_local_variable(expression)) {
5203 warningf(statement->statement.source_position,
5204 "function returns address of local variable");
5208 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5209 warningf(statement->statement.source_position,
5210 "'return' without value, in function returning non-void");
5213 statement->return_value = return_value;
5215 return (statement_t*) statement;
5219 * Parse a declaration statement.
5221 static statement_t *parse_declaration_statement(void)
5223 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5225 statement->base.source_position = token.source_position;
5227 declaration_t *before = last_declaration;
5228 parse_declaration(record_declaration);
5230 if(before == NULL) {
5231 statement->declaration.declarations_begin = context->declarations;
5233 statement->declaration.declarations_begin = before->next;
5235 statement->declaration.declarations_end = last_declaration;
5241 * Parse an expression statement, ie. expr ';'.
5243 static statement_t *parse_expression_statement(void)
5245 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5247 statement->base.source_position = token.source_position;
5248 statement->expression.expression = parse_expression();
5256 * Parse a statement.
5258 static statement_t *parse_statement(void)
5260 statement_t *statement = NULL;
5262 /* declaration or statement */
5263 switch(token.type) {
5265 statement = parse_asm_statement();
5269 statement = parse_case_statement();
5273 statement = parse_default_statement();
5277 statement = parse_compound_statement();
5281 statement = parse_if();
5285 statement = parse_switch();
5289 statement = parse_while();
5293 statement = parse_do();
5297 statement = parse_for();
5301 statement = parse_goto();
5305 statement = parse_continue();
5309 statement = parse_break();
5313 statement = parse_return();
5322 if(look_ahead(1)->type == ':') {
5323 statement = parse_label_statement();
5327 if(is_typedef_symbol(token.v.symbol)) {
5328 statement = parse_declaration_statement();
5332 statement = parse_expression_statement();
5335 case T___extension__:
5336 /* this can be a prefix to a declaration or an expression statement */
5337 /* we simply eat it now and parse the rest with tail recursion */
5340 } while(token.type == T___extension__);
5341 statement = parse_statement();
5345 statement = parse_declaration_statement();
5349 statement = parse_expression_statement();
5353 assert(statement == NULL
5354 || statement->base.source_position.input_name != NULL);
5360 * Parse a compound statement.
5362 static statement_t *parse_compound_statement(void)
5364 compound_statement_t *compound_statement
5365 = allocate_ast_zero(sizeof(compound_statement[0]));
5366 compound_statement->statement.kind = STATEMENT_COMPOUND;
5367 compound_statement->statement.source_position = token.source_position;
5371 int top = environment_top();
5372 context_t *last_context = context;
5373 set_context(&compound_statement->context);
5375 statement_t *last_statement = NULL;
5377 while(token.type != '}' && token.type != T_EOF) {
5378 statement_t *statement = parse_statement();
5379 if(statement == NULL)
5382 if(last_statement != NULL) {
5383 last_statement->base.next = statement;
5385 compound_statement->statements = statement;
5388 while(statement->base.next != NULL)
5389 statement = statement->base.next;
5391 last_statement = statement;
5394 if(token.type == '}') {
5397 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5400 assert(context == &compound_statement->context);
5401 set_context(last_context);
5402 environment_pop_to(top);
5404 return (statement_t*) compound_statement;
5408 * Initialize builtin types.
5410 static void initialize_builtin_types(void)
5412 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5413 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5414 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5415 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5416 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5417 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5418 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5419 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5421 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5422 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5423 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5424 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5428 * Parse a translation unit.
5430 static translation_unit_t *parse_translation_unit(void)
5432 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5434 assert(global_context == NULL);
5435 global_context = &unit->context;
5437 assert(context == NULL);
5438 set_context(&unit->context);
5440 initialize_builtin_types();
5442 while(token.type != T_EOF) {
5443 if (token.type == ';') {
5444 /* TODO error in strict mode */
5445 warningf(HERE, "stray ';' outside of function");
5448 parse_external_declaration();
5452 assert(context == &unit->context);
5454 last_declaration = NULL;
5456 assert(global_context == &unit->context);
5457 global_context = NULL;
5465 * @return the translation unit or NULL if errors occurred.
5467 translation_unit_t *parse(void)
5469 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5470 label_stack = NEW_ARR_F(stack_entry_t, 0);
5471 diagnostic_count = 0;
5475 type_set_output(stderr);
5476 ast_set_output(stderr);
5478 lookahead_bufpos = 0;
5479 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5482 translation_unit_t *unit = parse_translation_unit();
5484 DEL_ARR_F(environment_stack);
5485 DEL_ARR_F(label_stack);
5494 * Initialize the parser.
5496 void init_parser(void)
5498 init_expression_parsers();
5499 obstack_init(&temp_obst);
5501 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5502 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5506 * Terminate the parser.
5508 void exit_parser(void)
5510 obstack_free(&temp_obst, NULL);