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 assert(statement->kind == STATEMENT_COMPOUND);
3151 const statement_t *iter = statement->compound.statements;
3152 const statement_t *last_statement = NULL;
3153 for( ; iter != NULL; iter = iter->base.next) {
3154 last_statement = iter;
3157 if(last_statement->kind == STATEMENT_EXPRESSION) {
3158 expression->base.datatype
3159 = last_statement->expression.expression->base.datatype;
3161 expression->base.datatype = type_void;
3169 static expression_t *parse_brace_expression(void)
3173 switch(token.type) {
3175 /* gcc extension: a statement expression */
3176 return parse_statement_expression();
3180 return parse_cast();
3182 if(is_typedef_symbol(token.v.symbol)) {
3183 return parse_cast();
3187 expression_t *result = parse_expression();
3193 static expression_t *parse_function_keyword(void)
3198 if (current_function == NULL) {
3199 errorf(HERE, "'__func__' used outside of a function");
3202 string_literal_expression_t *expression
3203 = allocate_ast_zero(sizeof(expression[0]));
3205 expression->expression.kind = EXPR_FUNCTION;
3206 expression->expression.datatype = type_string;
3208 return (expression_t*) expression;
3211 static expression_t *parse_pretty_function_keyword(void)
3213 eat(T___PRETTY_FUNCTION__);
3216 if (current_function == NULL) {
3217 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3220 string_literal_expression_t *expression
3221 = allocate_ast_zero(sizeof(expression[0]));
3223 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3224 expression->expression.datatype = type_string;
3226 return (expression_t*) expression;
3229 static designator_t *parse_designator(void)
3231 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3233 if(token.type != T_IDENTIFIER) {
3234 parse_error_expected("while parsing member designator",
3239 result->symbol = token.v.symbol;
3242 designator_t *last_designator = result;
3244 if(token.type == '.') {
3246 if(token.type != T_IDENTIFIER) {
3247 parse_error_expected("while parsing member designator",
3252 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3253 designator->symbol = token.v.symbol;
3256 last_designator->next = designator;
3257 last_designator = designator;
3260 if(token.type == '[') {
3262 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3263 designator->array_access = parse_expression();
3264 if(designator->array_access == NULL) {
3270 last_designator->next = designator;
3271 last_designator = designator;
3280 static expression_t *parse_offsetof(void)
3282 eat(T___builtin_offsetof);
3284 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3285 expression->base.datatype = type_size_t;
3288 expression->offsetofe.type = parse_typename();
3290 expression->offsetofe.designator = parse_designator();
3296 static expression_t *parse_va_start(void)
3298 eat(T___builtin_va_start);
3300 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3303 expression->va_starte.ap = parse_assignment_expression();
3305 expression_t *const expr = parse_assignment_expression();
3306 if (expr->kind == EXPR_REFERENCE) {
3307 declaration_t *const decl = expr->reference.declaration;
3308 if (decl->parent_context == ¤t_function->context &&
3309 decl->next == NULL) {
3310 expression->va_starte.parameter = decl;
3315 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3317 return create_invalid_expression();
3320 static expression_t *parse_va_arg(void)
3322 eat(T___builtin_va_arg);
3324 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3327 expression->va_arge.ap = parse_assignment_expression();
3329 expression->base.datatype = parse_typename();
3335 static expression_t *parse_builtin_symbol(void)
3337 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3339 symbol_t *symbol = token.v.symbol;
3341 expression->builtin_symbol.symbol = symbol;
3344 type_t *type = get_builtin_symbol_type(symbol);
3345 type = automatic_type_conversion(type);
3347 expression->base.datatype = type;
3351 static expression_t *parse_builtin_constant(void)
3353 eat(T___builtin_constant_p);
3355 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3358 expression->builtin_constant.value = parse_assignment_expression();
3360 expression->base.datatype = type_int;
3365 static expression_t *parse_builtin_prefetch(void)
3367 eat(T___builtin_prefetch);
3369 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3372 expression->builtin_prefetch.adr = parse_assignment_expression();
3373 if (token.type == ',') {
3375 expression->builtin_prefetch.rw = parse_assignment_expression();
3377 if (token.type == ',') {
3379 expression->builtin_prefetch.locality = parse_assignment_expression();
3382 expression->base.datatype = type_void;
3387 static expression_t *parse_compare_builtin(void)
3389 expression_t *expression;
3391 switch(token.type) {
3392 case T___builtin_isgreater:
3393 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3395 case T___builtin_isgreaterequal:
3396 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3398 case T___builtin_isless:
3399 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3401 case T___builtin_islessequal:
3402 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3404 case T___builtin_islessgreater:
3405 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3407 case T___builtin_isunordered:
3408 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3411 panic("invalid compare builtin found");
3417 expression->binary.left = parse_assignment_expression();
3419 expression->binary.right = parse_assignment_expression();
3422 type_t *const orig_type_left = expression->binary.left->base.datatype;
3423 type_t *const orig_type_right = expression->binary.right->base.datatype;
3425 type_t *const type_left = skip_typeref(orig_type_left);
3426 type_t *const type_right = skip_typeref(orig_type_right);
3427 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3428 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3429 type_error_incompatible("invalid operands in comparison",
3430 token.source_position, orig_type_left, orig_type_right);
3433 semantic_comparison(&expression->binary);
3439 static expression_t *parse_builtin_expect(void)
3441 eat(T___builtin_expect);
3443 expression_t *expression
3444 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3447 expression->binary.left = parse_assignment_expression();
3449 expression->binary.right = parse_constant_expression();
3452 expression->base.datatype = expression->binary.left->base.datatype;
3457 static expression_t *parse_assume(void) {
3460 expression_t *expression
3461 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3464 expression->unary.value = parse_assignment_expression();
3467 expression->base.datatype = type_void;
3471 static expression_t *parse_alignof(void) {
3474 expression_t *expression
3475 = allocate_expression_zero(EXPR_ALIGNOF);
3478 expression->alignofe.type = parse_typename();
3481 expression->base.datatype = type_size_t;
3485 static expression_t *parse_primary_expression(void)
3487 switch(token.type) {
3489 return parse_int_const();
3490 case T_FLOATINGPOINT:
3491 return parse_float_const();
3492 case T_STRING_LITERAL:
3493 return parse_string_const();
3494 case T_WIDE_STRING_LITERAL:
3495 return parse_wide_string_const();
3497 return parse_reference();
3498 case T___FUNCTION__:
3500 return parse_function_keyword();
3501 case T___PRETTY_FUNCTION__:
3502 return parse_pretty_function_keyword();
3503 case T___builtin_offsetof:
3504 return parse_offsetof();
3505 case T___builtin_va_start:
3506 return parse_va_start();
3507 case T___builtin_va_arg:
3508 return parse_va_arg();
3509 case T___builtin_expect:
3510 return parse_builtin_expect();
3511 case T___builtin_nanf:
3512 case T___builtin_alloca:
3513 case T___builtin_va_end:
3514 return parse_builtin_symbol();
3515 case T___builtin_isgreater:
3516 case T___builtin_isgreaterequal:
3517 case T___builtin_isless:
3518 case T___builtin_islessequal:
3519 case T___builtin_islessgreater:
3520 case T___builtin_isunordered:
3521 return parse_compare_builtin();
3522 case T___builtin_constant_p:
3523 return parse_builtin_constant();
3524 case T___builtin_prefetch:
3525 return parse_builtin_prefetch();
3527 return parse_alignof();
3529 return parse_assume();
3532 return parse_brace_expression();
3535 errorf(HERE, "unexpected token '%K'", &token);
3538 return create_invalid_expression();
3542 * Check if the expression has the character type and issue a warning then.
3544 static void check_for_char_index_type(const expression_t *expression) {
3545 type_t *type = expression->base.datatype;
3546 type_t *base_type = skip_typeref(type);
3548 if (base_type->base.kind == TYPE_ATOMIC) {
3549 switch (base_type->atomic.akind == ATOMIC_TYPE_CHAR) {
3550 warningf(expression->base.source_position,
3551 "array subscript has type '%T'", type);
3556 static expression_t *parse_array_expression(unsigned precedence,
3563 expression_t *inside = parse_expression();
3565 array_access_expression_t *array_access
3566 = allocate_ast_zero(sizeof(array_access[0]));
3568 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3570 type_t *const orig_type_left = left->base.datatype;
3571 type_t *const orig_type_inside = inside->base.datatype;
3573 type_t *const type_left = skip_typeref(orig_type_left);
3574 type_t *const type_inside = skip_typeref(orig_type_inside);
3576 type_t *return_type;
3577 if (is_type_pointer(type_left)) {
3578 return_type = type_left->pointer.points_to;
3579 array_access->array_ref = left;
3580 array_access->index = inside;
3581 check_for_char_index_type(inside);
3582 } else if (is_type_pointer(type_inside)) {
3583 return_type = type_inside->pointer.points_to;
3584 array_access->array_ref = inside;
3585 array_access->index = left;
3586 array_access->flipped = true;
3587 check_for_char_index_type(left);
3589 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3591 "array access on object with non-pointer types '%T', '%T'",
3592 orig_type_left, orig_type_inside);
3594 return_type = type_error_type;
3595 array_access->array_ref = create_invalid_expression();
3598 if(token.type != ']') {
3599 parse_error_expected("Problem while parsing array access", ']', 0);
3600 return (expression_t*) array_access;
3604 return_type = automatic_type_conversion(return_type);
3605 array_access->expression.datatype = return_type;
3607 return (expression_t*) array_access;
3610 static expression_t *parse_sizeof(unsigned precedence)
3614 sizeof_expression_t *sizeof_expression
3615 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3616 sizeof_expression->expression.kind = EXPR_SIZEOF;
3617 sizeof_expression->expression.datatype = type_size_t;
3619 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3621 sizeof_expression->type = parse_typename();
3624 expression_t *expression = parse_sub_expression(precedence);
3625 expression->base.datatype = revert_automatic_type_conversion(expression);
3627 sizeof_expression->type = expression->base.datatype;
3628 sizeof_expression->size_expression = expression;
3631 return (expression_t*) sizeof_expression;
3634 static expression_t *parse_select_expression(unsigned precedence,
3635 expression_t *compound)
3638 assert(token.type == '.' || token.type == T_MINUSGREATER);
3640 bool is_pointer = (token.type == T_MINUSGREATER);
3643 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3644 select->select.compound = compound;
3646 if(token.type != T_IDENTIFIER) {
3647 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3650 symbol_t *symbol = token.v.symbol;
3651 select->select.symbol = symbol;
3654 type_t *const orig_type = compound->base.datatype;
3655 type_t *const type = skip_typeref(orig_type);
3657 type_t *type_left = type;
3659 if (!is_type_pointer(type)) {
3660 if (is_type_valid(type)) {
3661 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3663 return create_invalid_expression();
3665 type_left = type->pointer.points_to;
3667 type_left = skip_typeref(type_left);
3669 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3670 type_left->kind != TYPE_COMPOUND_UNION) {
3671 if (is_type_valid(type_left)) {
3672 errorf(HERE, "request for member '%Y' in something not a struct or "
3673 "union, but '%T'", symbol, type_left);
3675 return create_invalid_expression();
3678 declaration_t *const declaration = type_left->compound.declaration;
3680 if(!declaration->init.is_defined) {
3681 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3683 return create_invalid_expression();
3686 declaration_t *iter = declaration->context.declarations;
3687 for( ; iter != NULL; iter = iter->next) {
3688 if(iter->symbol == symbol) {
3693 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3694 return create_invalid_expression();
3697 /* we always do the auto-type conversions; the & and sizeof parser contains
3698 * code to revert this! */
3699 type_t *expression_type = automatic_type_conversion(iter->type);
3701 select->select.compound_entry = iter;
3702 select->base.datatype = expression_type;
3704 if(expression_type->kind == TYPE_BITFIELD) {
3705 expression_t *extract
3706 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3707 extract->unary.value = select;
3708 extract->base.datatype = expression_type->bitfield.base;
3717 * Parse a call expression, ie. expression '( ... )'.
3719 * @param expression the function address
3721 static expression_t *parse_call_expression(unsigned precedence,
3722 expression_t *expression)
3725 expression_t *result = allocate_expression_zero(EXPR_CALL);
3727 call_expression_t *call = &result->call;
3728 call->function = expression;
3730 type_t *const orig_type = expression->base.datatype;
3731 type_t *const type = skip_typeref(orig_type);
3733 function_type_t *function_type = NULL;
3734 if (is_type_pointer(type)) {
3735 type_t *const to_type = skip_typeref(type->pointer.points_to);
3737 if (is_type_function(to_type)) {
3738 function_type = &to_type->function;
3739 call->expression.datatype = function_type->return_type;
3743 if (function_type == NULL && is_type_valid(type)) {
3744 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3747 /* parse arguments */
3750 if(token.type != ')') {
3751 call_argument_t *last_argument = NULL;
3754 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3756 argument->expression = parse_assignment_expression();
3757 if(last_argument == NULL) {
3758 call->arguments = argument;
3760 last_argument->next = argument;
3762 last_argument = argument;
3764 if(token.type != ',')
3771 if(function_type != NULL) {
3772 function_parameter_t *parameter = function_type->parameters;
3773 call_argument_t *argument = call->arguments;
3774 for( ; parameter != NULL && argument != NULL;
3775 parameter = parameter->next, argument = argument->next) {
3776 type_t *expected_type = parameter->type;
3777 /* TODO report context in error messages */
3778 expression_t *const arg_expr = argument->expression;
3779 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
3780 if (res_type == NULL) {
3781 /* TODO improve error message */
3782 errorf(arg_expr->base.source_position,
3783 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
3784 arg_expr, arg_expr->base.datatype, expected_type);
3786 argument->expression = create_implicit_cast(argument->expression, expected_type);
3789 /* too few parameters */
3790 if(parameter != NULL) {
3791 errorf(HERE, "too few arguments to function '%E'", expression);
3792 } else if(argument != NULL) {
3793 /* too many parameters */
3794 if(!function_type->variadic
3795 && !function_type->unspecified_parameters) {
3796 errorf(HERE, "too many arguments to function '%E'", expression);
3798 /* do default promotion */
3799 for( ; argument != NULL; argument = argument->next) {
3800 type_t *type = argument->expression->base.datatype;
3802 type = skip_typeref(type);
3803 if(is_type_integer(type)) {
3804 type = promote_integer(type);
3805 } else if(type == type_float) {
3809 argument->expression
3810 = create_implicit_cast(argument->expression, type);
3813 check_format(&result->call);
3816 check_format(&result->call);
3823 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3825 static bool same_compound_type(const type_t *type1, const type_t *type2)
3828 is_type_compound(type1) &&
3829 type1->kind == type2->kind &&
3830 type1->compound.declaration == type2->compound.declaration;
3834 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3836 * @param expression the conditional expression
3838 static expression_t *parse_conditional_expression(unsigned precedence,
3839 expression_t *expression)
3843 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3845 conditional_expression_t *conditional = &result->conditional;
3846 conditional->condition = expression;
3849 type_t *const condition_type_orig = expression->base.datatype;
3850 type_t *const condition_type = skip_typeref(condition_type_orig);
3851 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
3852 type_error("expected a scalar type in conditional condition",
3853 expression->base.source_position, condition_type_orig);
3856 expression_t *true_expression = parse_expression();
3858 expression_t *false_expression = parse_sub_expression(precedence);
3860 conditional->true_expression = true_expression;
3861 conditional->false_expression = false_expression;
3863 type_t *const orig_true_type = true_expression->base.datatype;
3864 type_t *const orig_false_type = false_expression->base.datatype;
3865 type_t *const true_type = skip_typeref(orig_true_type);
3866 type_t *const false_type = skip_typeref(orig_false_type);
3869 type_t *result_type;
3870 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3871 result_type = semantic_arithmetic(true_type, false_type);
3873 true_expression = create_implicit_cast(true_expression, result_type);
3874 false_expression = create_implicit_cast(false_expression, result_type);
3876 conditional->true_expression = true_expression;
3877 conditional->false_expression = false_expression;
3878 conditional->expression.datatype = result_type;
3879 } else if (same_compound_type(true_type, false_type) || (
3880 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3881 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
3883 /* just take 1 of the 2 types */
3884 result_type = true_type;
3885 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3886 && pointers_compatible(true_type, false_type)) {
3888 result_type = true_type;
3891 if (is_type_valid(true_type) && is_type_valid(false_type)) {
3892 type_error_incompatible("while parsing conditional",
3893 expression->base.source_position, true_type,
3896 result_type = type_error_type;
3899 conditional->expression.datatype = result_type;
3904 * Parse an extension expression.
3906 static expression_t *parse_extension(unsigned precedence)
3908 eat(T___extension__);
3910 /* TODO enable extensions */
3911 expression_t *expression = parse_sub_expression(precedence);
3912 /* TODO disable extensions */
3916 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3918 eat(T___builtin_classify_type);
3920 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3921 result->base.datatype = type_int;
3924 expression_t *expression = parse_sub_expression(precedence);
3926 result->classify_type.type_expression = expression;
3931 static void semantic_incdec(unary_expression_t *expression)
3933 type_t *const orig_type = expression->value->base.datatype;
3934 type_t *const type = skip_typeref(orig_type);
3935 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3936 if (is_type_valid(type)) {
3937 /* TODO: improve error message */
3938 errorf(HERE, "operation needs an arithmetic or pointer type");
3943 expression->expression.datatype = orig_type;
3946 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3948 type_t *const orig_type = expression->value->base.datatype;
3949 type_t *const type = skip_typeref(orig_type);
3950 if(!is_type_arithmetic(type)) {
3951 if (is_type_valid(type)) {
3952 /* TODO: improve error message */
3953 errorf(HERE, "operation needs an arithmetic type");
3958 expression->expression.datatype = orig_type;
3961 static void semantic_unexpr_scalar(unary_expression_t *expression)
3963 type_t *const orig_type = expression->value->base.datatype;
3964 type_t *const type = skip_typeref(orig_type);
3965 if (!is_type_scalar(type)) {
3966 if (is_type_valid(type)) {
3967 errorf(HERE, "operand of ! must be of scalar type");
3972 expression->expression.datatype = orig_type;
3975 static void semantic_unexpr_integer(unary_expression_t *expression)
3977 type_t *const orig_type = expression->value->base.datatype;
3978 type_t *const type = skip_typeref(orig_type);
3979 if (!is_type_integer(type)) {
3980 if (is_type_valid(type)) {
3981 errorf(HERE, "operand of ~ must be of integer type");
3986 expression->expression.datatype = orig_type;
3989 static void semantic_dereference(unary_expression_t *expression)
3991 type_t *const orig_type = expression->value->base.datatype;
3992 type_t *const type = skip_typeref(orig_type);
3993 if(!is_type_pointer(type)) {
3994 if (is_type_valid(type)) {
3995 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4000 type_t *result_type = type->pointer.points_to;
4001 result_type = automatic_type_conversion(result_type);
4002 expression->expression.datatype = result_type;
4006 * Check the semantic of the address taken expression.
4008 static void semantic_take_addr(unary_expression_t *expression)
4010 expression_t *value = expression->value;
4011 value->base.datatype = revert_automatic_type_conversion(value);
4013 type_t *orig_type = value->base.datatype;
4014 if(!is_type_valid(orig_type))
4017 if(value->kind == EXPR_REFERENCE) {
4018 declaration_t *const declaration = value->reference.declaration;
4019 if(declaration != NULL) {
4020 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4021 errorf(expression->expression.source_position,
4022 "address of register variable '%Y' requested",
4023 declaration->symbol);
4025 declaration->address_taken = 1;
4029 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4032 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4033 static expression_t *parse_##unexpression_type(unsigned precedence) \
4037 expression_t *unary_expression \
4038 = allocate_expression_zero(unexpression_type); \
4039 unary_expression->base.source_position = HERE; \
4040 unary_expression->unary.value = parse_sub_expression(precedence); \
4042 sfunc(&unary_expression->unary); \
4044 return unary_expression; \
4047 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4048 semantic_unexpr_arithmetic)
4049 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4050 semantic_unexpr_arithmetic)
4051 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4052 semantic_unexpr_scalar)
4053 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4054 semantic_dereference)
4055 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4057 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4058 semantic_unexpr_integer)
4059 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4061 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4064 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4066 static expression_t *parse_##unexpression_type(unsigned precedence, \
4067 expression_t *left) \
4069 (void) precedence; \
4072 expression_t *unary_expression \
4073 = allocate_expression_zero(unexpression_type); \
4074 unary_expression->unary.value = left; \
4076 sfunc(&unary_expression->unary); \
4078 return unary_expression; \
4081 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4082 EXPR_UNARY_POSTFIX_INCREMENT,
4084 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4085 EXPR_UNARY_POSTFIX_DECREMENT,
4088 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4090 /* TODO: handle complex + imaginary types */
4092 /* § 6.3.1.8 Usual arithmetic conversions */
4093 if(type_left == type_long_double || type_right == type_long_double) {
4094 return type_long_double;
4095 } else if(type_left == type_double || type_right == type_double) {
4097 } else if(type_left == type_float || type_right == type_float) {
4101 type_right = promote_integer(type_right);
4102 type_left = promote_integer(type_left);
4104 if(type_left == type_right)
4107 bool signed_left = is_type_signed(type_left);
4108 bool signed_right = is_type_signed(type_right);
4109 int rank_left = get_rank(type_left);
4110 int rank_right = get_rank(type_right);
4111 if(rank_left < rank_right) {
4112 if(signed_left == signed_right || !signed_right) {
4118 if(signed_left == signed_right || !signed_left) {
4127 * Check the semantic restrictions for a binary expression.
4129 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4131 expression_t *const left = expression->left;
4132 expression_t *const right = expression->right;
4133 type_t *const orig_type_left = left->base.datatype;
4134 type_t *const orig_type_right = right->base.datatype;
4135 type_t *const type_left = skip_typeref(orig_type_left);
4136 type_t *const type_right = skip_typeref(orig_type_right);
4138 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4139 /* TODO: improve error message */
4140 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4141 errorf(HERE, "operation needs arithmetic types");
4146 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4147 expression->left = create_implicit_cast(left, arithmetic_type);
4148 expression->right = create_implicit_cast(right, arithmetic_type);
4149 expression->expression.datatype = arithmetic_type;
4152 static void semantic_shift_op(binary_expression_t *expression)
4154 expression_t *const left = expression->left;
4155 expression_t *const right = expression->right;
4156 type_t *const orig_type_left = left->base.datatype;
4157 type_t *const orig_type_right = right->base.datatype;
4158 type_t * type_left = skip_typeref(orig_type_left);
4159 type_t * type_right = skip_typeref(orig_type_right);
4161 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4162 /* TODO: improve error message */
4163 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4164 errorf(HERE, "operation needs integer types");
4169 type_left = promote_integer(type_left);
4170 type_right = promote_integer(type_right);
4172 expression->left = create_implicit_cast(left, type_left);
4173 expression->right = create_implicit_cast(right, type_right);
4174 expression->expression.datatype = type_left;
4177 static void semantic_add(binary_expression_t *expression)
4179 expression_t *const left = expression->left;
4180 expression_t *const right = expression->right;
4181 type_t *const orig_type_left = left->base.datatype;
4182 type_t *const orig_type_right = right->base.datatype;
4183 type_t *const type_left = skip_typeref(orig_type_left);
4184 type_t *const type_right = skip_typeref(orig_type_right);
4187 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4188 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4189 expression->left = create_implicit_cast(left, arithmetic_type);
4190 expression->right = create_implicit_cast(right, arithmetic_type);
4191 expression->expression.datatype = arithmetic_type;
4193 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4194 expression->expression.datatype = type_left;
4195 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4196 expression->expression.datatype = type_right;
4197 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4198 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4202 static void semantic_sub(binary_expression_t *expression)
4204 expression_t *const left = expression->left;
4205 expression_t *const right = expression->right;
4206 type_t *const orig_type_left = left->base.datatype;
4207 type_t *const orig_type_right = right->base.datatype;
4208 type_t *const type_left = skip_typeref(orig_type_left);
4209 type_t *const type_right = skip_typeref(orig_type_right);
4212 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4213 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4214 expression->left = create_implicit_cast(left, arithmetic_type);
4215 expression->right = create_implicit_cast(right, arithmetic_type);
4216 expression->expression.datatype = arithmetic_type;
4218 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4219 expression->expression.datatype = type_left;
4220 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4221 if(!pointers_compatible(type_left, type_right)) {
4222 errorf(HERE, "pointers to incompatible objects to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4224 expression->expression.datatype = type_ptrdiff_t;
4226 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4227 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4231 static void semantic_comparison(binary_expression_t *expression)
4233 expression_t *left = expression->left;
4234 expression_t *right = expression->right;
4235 type_t *orig_type_left = left->base.datatype;
4236 type_t *orig_type_right = right->base.datatype;
4238 type_t *type_left = skip_typeref(orig_type_left);
4239 type_t *type_right = skip_typeref(orig_type_right);
4241 /* TODO non-arithmetic types */
4242 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4243 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4244 expression->left = create_implicit_cast(left, arithmetic_type);
4245 expression->right = create_implicit_cast(right, arithmetic_type);
4246 expression->expression.datatype = arithmetic_type;
4247 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4248 /* TODO check compatibility */
4249 } else if (is_type_pointer(type_left)) {
4250 expression->right = create_implicit_cast(right, type_left);
4251 } else if (is_type_pointer(type_right)) {
4252 expression->left = create_implicit_cast(left, type_right);
4253 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4254 type_error_incompatible("invalid operands in comparison",
4255 token.source_position, type_left, type_right);
4257 expression->expression.datatype = type_int;
4260 static void semantic_arithmetic_assign(binary_expression_t *expression)
4262 expression_t *left = expression->left;
4263 expression_t *right = expression->right;
4264 type_t *orig_type_left = left->base.datatype;
4265 type_t *orig_type_right = right->base.datatype;
4267 type_t *type_left = skip_typeref(orig_type_left);
4268 type_t *type_right = skip_typeref(orig_type_right);
4270 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4271 /* TODO: improve error message */
4272 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4273 errorf(HERE, "operation needs arithmetic types");
4278 /* combined instructions are tricky. We can't create an implicit cast on
4279 * the left side, because we need the uncasted form for the store.
4280 * The ast2firm pass has to know that left_type must be right_type
4281 * for the arithmetic operation and create a cast by itself */
4282 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4283 expression->right = create_implicit_cast(right, arithmetic_type);
4284 expression->expression.datatype = type_left;
4287 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4289 expression_t *const left = expression->left;
4290 expression_t *const right = expression->right;
4291 type_t *const orig_type_left = left->base.datatype;
4292 type_t *const orig_type_right = right->base.datatype;
4293 type_t *const type_left = skip_typeref(orig_type_left);
4294 type_t *const type_right = skip_typeref(orig_type_right);
4296 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4297 /* combined instructions are tricky. We can't create an implicit cast on
4298 * the left side, because we need the uncasted form for the store.
4299 * The ast2firm pass has to know that left_type must be right_type
4300 * for the arithmetic operation and create a cast by itself */
4301 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4302 expression->right = create_implicit_cast(right, arithmetic_type);
4303 expression->expression.datatype = type_left;
4304 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4305 expression->expression.datatype = type_left;
4306 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4307 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4312 * Check the semantic restrictions of a logical expression.
4314 static void semantic_logical_op(binary_expression_t *expression)
4316 expression_t *const left = expression->left;
4317 expression_t *const right = expression->right;
4318 type_t *const orig_type_left = left->base.datatype;
4319 type_t *const orig_type_right = right->base.datatype;
4320 type_t *const type_left = skip_typeref(orig_type_left);
4321 type_t *const type_right = skip_typeref(orig_type_right);
4323 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4324 /* TODO: improve error message */
4325 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4326 errorf(HERE, "operation needs scalar types");
4331 expression->expression.datatype = type_int;
4335 * Checks if a compound type has constant fields.
4337 static bool has_const_fields(const compound_type_t *type)
4339 const context_t *context = &type->declaration->context;
4340 const declaration_t *declaration = context->declarations;
4342 for (; declaration != NULL; declaration = declaration->next) {
4343 if (declaration->namespc != NAMESPACE_NORMAL)
4346 const type_t *decl_type = skip_typeref(declaration->type);
4347 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4355 * Check the semantic restrictions of a binary assign expression.
4357 static void semantic_binexpr_assign(binary_expression_t *expression)
4359 expression_t *left = expression->left;
4360 type_t *orig_type_left = left->base.datatype;
4362 type_t *type_left = revert_automatic_type_conversion(left);
4363 type_left = skip_typeref(orig_type_left);
4365 /* must be a modifiable lvalue */
4366 if (is_type_array(type_left)) {
4367 errorf(HERE, "cannot assign to arrays ('%E')", left);
4370 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4371 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4375 if(is_type_incomplete(type_left)) {
4377 "left-hand side of assignment '%E' has incomplete type '%T'",
4378 left, orig_type_left);
4381 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4382 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4383 left, orig_type_left);
4387 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4389 if (res_type == NULL) {
4390 errorf(expression->expression.source_position,
4391 "cannot assign to '%T' from '%T'",
4392 orig_type_left, expression->right->base.datatype);
4394 expression->right = create_implicit_cast(expression->right, res_type);
4397 expression->expression.datatype = orig_type_left;
4400 static void semantic_comma(binary_expression_t *expression)
4402 expression->expression.datatype = expression->right->base.datatype;
4405 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4406 static expression_t *parse_##binexpression_type(unsigned precedence, \
4407 expression_t *left) \
4411 expression_t *right = parse_sub_expression(precedence + lr); \
4413 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4414 binexpr->binary.left = left; \
4415 binexpr->binary.right = right; \
4416 sfunc(&binexpr->binary); \
4421 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4422 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4423 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4424 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4425 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4426 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4427 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4428 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4429 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4431 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4432 semantic_comparison, 1)
4433 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4434 semantic_comparison, 1)
4435 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4436 semantic_comparison, 1)
4437 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4438 semantic_comparison, 1)
4440 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4441 semantic_binexpr_arithmetic, 1)
4442 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4443 semantic_binexpr_arithmetic, 1)
4444 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4445 semantic_binexpr_arithmetic, 1)
4446 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4447 semantic_logical_op, 1)
4448 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4449 semantic_logical_op, 1)
4450 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4451 semantic_shift_op, 1)
4452 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4453 semantic_shift_op, 1)
4454 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4455 semantic_arithmetic_addsubb_assign, 0)
4456 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4457 semantic_arithmetic_addsubb_assign, 0)
4458 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4459 semantic_arithmetic_assign, 0)
4460 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4461 semantic_arithmetic_assign, 0)
4462 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4463 semantic_arithmetic_assign, 0)
4464 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4465 semantic_arithmetic_assign, 0)
4466 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4467 semantic_arithmetic_assign, 0)
4468 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4469 semantic_arithmetic_assign, 0)
4470 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4471 semantic_arithmetic_assign, 0)
4472 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4473 semantic_arithmetic_assign, 0)
4475 static expression_t *parse_sub_expression(unsigned precedence)
4477 if(token.type < 0) {
4478 return expected_expression_error();
4481 expression_parser_function_t *parser
4482 = &expression_parsers[token.type];
4483 source_position_t source_position = token.source_position;
4486 if(parser->parser != NULL) {
4487 left = parser->parser(parser->precedence);
4489 left = parse_primary_expression();
4491 assert(left != NULL);
4492 left->base.source_position = source_position;
4495 if(token.type < 0) {
4496 return expected_expression_error();
4499 parser = &expression_parsers[token.type];
4500 if(parser->infix_parser == NULL)
4502 if(parser->infix_precedence < precedence)
4505 left = parser->infix_parser(parser->infix_precedence, left);
4507 assert(left != NULL);
4508 assert(left->kind != EXPR_UNKNOWN);
4509 left->base.source_position = source_position;
4516 * Parse an expression.
4518 static expression_t *parse_expression(void)
4520 return parse_sub_expression(1);
4524 * Register a parser for a prefix-like operator with given precedence.
4526 * @param parser the parser function
4527 * @param token_type the token type of the prefix token
4528 * @param precedence the precedence of the operator
4530 static void register_expression_parser(parse_expression_function parser,
4531 int token_type, unsigned precedence)
4533 expression_parser_function_t *entry = &expression_parsers[token_type];
4535 if(entry->parser != NULL) {
4536 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4537 panic("trying to register multiple expression parsers for a token");
4539 entry->parser = parser;
4540 entry->precedence = precedence;
4544 * Register a parser for an infix operator with given precedence.
4546 * @param parser the parser function
4547 * @param token_type the token type of the infix operator
4548 * @param precedence the precedence of the operator
4550 static void register_infix_parser(parse_expression_infix_function parser,
4551 int token_type, unsigned precedence)
4553 expression_parser_function_t *entry = &expression_parsers[token_type];
4555 if(entry->infix_parser != NULL) {
4556 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4557 panic("trying to register multiple infix expression parsers for a "
4560 entry->infix_parser = parser;
4561 entry->infix_precedence = precedence;
4565 * Initialize the expression parsers.
4567 static void init_expression_parsers(void)
4569 memset(&expression_parsers, 0, sizeof(expression_parsers));
4571 register_infix_parser(parse_array_expression, '[', 30);
4572 register_infix_parser(parse_call_expression, '(', 30);
4573 register_infix_parser(parse_select_expression, '.', 30);
4574 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4575 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4577 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4580 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4581 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4582 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4583 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4584 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4585 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4586 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4587 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4588 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4589 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4590 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4591 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4592 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4593 T_EXCLAMATIONMARKEQUAL, 13);
4594 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4595 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4596 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4597 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4598 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4599 register_infix_parser(parse_conditional_expression, '?', 7);
4600 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4601 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4602 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4603 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4604 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4605 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4606 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4607 T_LESSLESSEQUAL, 2);
4608 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4609 T_GREATERGREATEREQUAL, 2);
4610 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4612 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4614 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4617 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4619 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4620 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4621 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4622 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4623 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4624 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4625 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4627 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4629 register_expression_parser(parse_sizeof, T_sizeof, 25);
4630 register_expression_parser(parse_extension, T___extension__, 25);
4631 register_expression_parser(parse_builtin_classify_type,
4632 T___builtin_classify_type, 25);
4636 * Parse a asm statement constraints specification.
4638 static asm_constraint_t *parse_asm_constraints(void)
4640 asm_constraint_t *result = NULL;
4641 asm_constraint_t *last = NULL;
4643 while(token.type == T_STRING_LITERAL || token.type == '[') {
4644 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4645 memset(constraint, 0, sizeof(constraint[0]));
4647 if(token.type == '[') {
4649 if(token.type != T_IDENTIFIER) {
4650 parse_error_expected("while parsing asm constraint",
4654 constraint->symbol = token.v.symbol;
4659 constraint->constraints = parse_string_literals();
4661 constraint->expression = parse_expression();
4665 last->next = constraint;
4667 result = constraint;
4671 if(token.type != ',')
4680 * Parse a asm statement clobber specification.
4682 static asm_clobber_t *parse_asm_clobbers(void)
4684 asm_clobber_t *result = NULL;
4685 asm_clobber_t *last = NULL;
4687 while(token.type == T_STRING_LITERAL) {
4688 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4689 clobber->clobber = parse_string_literals();
4692 last->next = clobber;
4698 if(token.type != ',')
4707 * Parse an asm statement.
4709 static statement_t *parse_asm_statement(void)
4713 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4714 statement->base.source_position = token.source_position;
4716 asm_statement_t *asm_statement = &statement->asms;
4718 if(token.type == T_volatile) {
4720 asm_statement->is_volatile = true;
4724 asm_statement->asm_text = parse_string_literals();
4726 if(token.type != ':')
4730 asm_statement->inputs = parse_asm_constraints();
4731 if(token.type != ':')
4735 asm_statement->outputs = parse_asm_constraints();
4736 if(token.type != ':')
4740 asm_statement->clobbers = parse_asm_clobbers();
4749 * Parse a case statement.
4751 static statement_t *parse_case_statement(void)
4755 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4757 statement->base.source_position = token.source_position;
4758 statement->case_label.expression = parse_expression();
4762 if (! is_constant_expression(statement->case_label.expression)) {
4763 errorf(statement->base.source_position,
4764 "case label does not reduce to an integer constant");
4766 /* TODO: check if the case label is already known */
4767 if (current_switch != NULL) {
4768 /* link all cases into the switch statement */
4769 if (current_switch->last_case == NULL) {
4770 current_switch->first_case =
4771 current_switch->last_case = &statement->case_label;
4773 current_switch->last_case->next = &statement->case_label;
4776 errorf(statement->base.source_position,
4777 "case label not within a switch statement");
4780 statement->case_label.label_statement = parse_statement();
4786 * Finds an existing default label of a switch statement.
4788 static case_label_statement_t *
4789 find_default_label(const switch_statement_t *statement)
4791 for (case_label_statement_t *label = statement->first_case;
4793 label = label->next) {
4794 if (label->expression == NULL)
4801 * Parse a default statement.
4803 static statement_t *parse_default_statement(void)
4807 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4809 statement->base.source_position = token.source_position;
4812 if (current_switch != NULL) {
4813 const case_label_statement_t *def_label = find_default_label(current_switch);
4814 if (def_label != NULL) {
4815 errorf(HERE, "multiple default labels in one switch");
4816 errorf(def_label->statement.source_position,
4817 "this is the first default label");
4819 /* link all cases into the switch statement */
4820 if (current_switch->last_case == NULL) {
4821 current_switch->first_case =
4822 current_switch->last_case = &statement->case_label;
4824 current_switch->last_case->next = &statement->case_label;
4828 errorf(statement->base.source_position,
4829 "'default' label not within a switch statement");
4831 statement->label.label_statement = parse_statement();
4837 * Return the declaration for a given label symbol or create a new one.
4839 static declaration_t *get_label(symbol_t *symbol)
4841 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4842 assert(current_function != NULL);
4843 /* if we found a label in the same function, then we already created the
4845 if(candidate != NULL
4846 && candidate->parent_context == ¤t_function->context) {
4850 /* otherwise we need to create a new one */
4851 declaration_t *const declaration = allocate_declaration_zero();
4852 declaration->namespc = NAMESPACE_LABEL;
4853 declaration->symbol = symbol;
4855 label_push(declaration);
4861 * Parse a label statement.
4863 static statement_t *parse_label_statement(void)
4865 assert(token.type == T_IDENTIFIER);
4866 symbol_t *symbol = token.v.symbol;
4869 declaration_t *label = get_label(symbol);
4871 /* if source position is already set then the label is defined twice,
4872 * otherwise it was just mentioned in a goto so far */
4873 if(label->source_position.input_name != NULL) {
4874 errorf(HERE, "duplicate label '%Y'", symbol);
4875 errorf(label->source_position, "previous definition of '%Y' was here",
4878 label->source_position = token.source_position;
4881 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4883 label_statement->statement.kind = STATEMENT_LABEL;
4884 label_statement->statement.source_position = token.source_position;
4885 label_statement->label = label;
4889 if(token.type == '}') {
4890 /* TODO only warn? */
4891 errorf(HERE, "label at end of compound statement");
4892 return (statement_t*) label_statement;
4894 label_statement->label_statement = parse_statement();
4897 return (statement_t*) label_statement;
4901 * Parse an if statement.
4903 static statement_t *parse_if(void)
4907 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4908 statement->statement.kind = STATEMENT_IF;
4909 statement->statement.source_position = token.source_position;
4912 statement->condition = parse_expression();
4915 statement->true_statement = parse_statement();
4916 if(token.type == T_else) {
4918 statement->false_statement = parse_statement();
4921 return (statement_t*) statement;
4925 * Parse a switch statement.
4927 static statement_t *parse_switch(void)
4931 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4932 statement->statement.kind = STATEMENT_SWITCH;
4933 statement->statement.source_position = token.source_position;
4936 expression_t *const expr = parse_expression();
4937 type_t * type = skip_typeref(expr->base.datatype);
4938 if (is_type_integer(type)) {
4939 type = promote_integer(type);
4940 } else if (is_type_valid(type)) {
4941 errorf(expr->base.source_position, "switch quantity is not an integer, but '%T'", type);
4942 type = type_error_type;
4944 statement->expression = create_implicit_cast(expr, type);
4947 switch_statement_t *rem = current_switch;
4948 current_switch = statement;
4949 statement->body = parse_statement();
4950 current_switch = rem;
4952 return (statement_t*) statement;
4955 static statement_t *parse_loop_body(statement_t *const loop)
4957 statement_t *const rem = current_loop;
4958 current_loop = loop;
4959 statement_t *const body = parse_statement();
4965 * Parse a while statement.
4967 static statement_t *parse_while(void)
4971 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4972 statement->statement.kind = STATEMENT_WHILE;
4973 statement->statement.source_position = token.source_position;
4976 statement->condition = parse_expression();
4979 statement->body = parse_loop_body((statement_t*)statement);
4981 return (statement_t*) statement;
4985 * Parse a do statement.
4987 static statement_t *parse_do(void)
4991 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4992 statement->statement.kind = STATEMENT_DO_WHILE;
4993 statement->statement.source_position = token.source_position;
4995 statement->body = parse_loop_body((statement_t*)statement);
4998 statement->condition = parse_expression();
5002 return (statement_t*) statement;
5006 * Parse a for statement.
5008 static statement_t *parse_for(void)
5012 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5013 statement->statement.kind = STATEMENT_FOR;
5014 statement->statement.source_position = token.source_position;
5018 int top = environment_top();
5019 context_t *last_context = context;
5020 set_context(&statement->context);
5022 if(token.type != ';') {
5023 if(is_declaration_specifier(&token, false)) {
5024 parse_declaration(record_declaration);
5026 statement->initialisation = parse_expression();
5033 if(token.type != ';') {
5034 statement->condition = parse_expression();
5037 if(token.type != ')') {
5038 statement->step = parse_expression();
5041 statement->body = parse_loop_body((statement_t*)statement);
5043 assert(context == &statement->context);
5044 set_context(last_context);
5045 environment_pop_to(top);
5047 return (statement_t*) statement;
5051 * Parse a goto statement.
5053 static statement_t *parse_goto(void)
5057 if(token.type != T_IDENTIFIER) {
5058 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5062 symbol_t *symbol = token.v.symbol;
5065 declaration_t *label = get_label(symbol);
5067 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5069 statement->statement.kind = STATEMENT_GOTO;
5070 statement->statement.source_position = token.source_position;
5072 statement->label = label;
5074 /* remember the goto's in a list for later checking */
5075 if (goto_last == NULL) {
5076 goto_first = goto_last = statement;
5078 goto_last->next = statement;
5083 return (statement_t*) statement;
5087 * Parse a continue statement.
5089 static statement_t *parse_continue(void)
5091 statement_t *statement;
5092 if (current_loop == NULL) {
5093 errorf(HERE, "continue statement not within loop");
5096 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5098 statement->base.source_position = token.source_position;
5108 * Parse a break statement.
5110 static statement_t *parse_break(void)
5112 statement_t *statement;
5113 if (current_switch == NULL && current_loop == NULL) {
5114 errorf(HERE, "break statement not within loop or switch");
5117 statement = allocate_statement_zero(STATEMENT_BREAK);
5119 statement->base.source_position = token.source_position;
5129 * Check if a given declaration represents a local variable.
5131 static bool is_local_var_declaration(const declaration_t *declaration) {
5132 switch ((storage_class_tag_t) declaration->storage_class) {
5133 case STORAGE_CLASS_NONE:
5134 case STORAGE_CLASS_AUTO:
5135 case STORAGE_CLASS_REGISTER: {
5136 const type_t *type = skip_typeref(declaration->type);
5137 if(is_type_function(type)) {
5149 * Check if a given expression represents a local variable.
5151 static bool is_local_variable(const expression_t *expression)
5153 if (expression->base.kind != EXPR_REFERENCE) {
5156 const declaration_t *declaration = expression->reference.declaration;
5157 return is_local_var_declaration(declaration);
5161 * Parse a return statement.
5163 static statement_t *parse_return(void)
5167 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5169 statement->statement.kind = STATEMENT_RETURN;
5170 statement->statement.source_position = token.source_position;
5172 expression_t *return_value = NULL;
5173 if(token.type != ';') {
5174 return_value = parse_expression();
5178 const type_t *const func_type = current_function->type;
5179 assert(is_type_function(func_type));
5180 type_t *const return_type = skip_typeref(func_type->function.return_type);
5182 if(return_value != NULL) {
5183 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5185 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5186 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5187 warningf(statement->statement.source_position,
5188 "'return' with a value, in function returning void");
5189 return_value = NULL;
5191 type_t *const res_type = semantic_assign(return_type,
5192 return_value, "'return'");
5193 if (res_type == NULL) {
5194 errorf(statement->statement.source_position,
5195 "cannot return something of type '%T' in function returning '%T'",
5196 return_value->base.datatype, return_type);
5198 return_value = create_implicit_cast(return_value, res_type);
5201 /* check for returning address of a local var */
5202 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5203 const expression_t *expression = return_value->unary.value;
5204 if (is_local_variable(expression)) {
5205 warningf(statement->statement.source_position,
5206 "function returns address of local variable");
5210 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5211 warningf(statement->statement.source_position,
5212 "'return' without value, in function returning non-void");
5215 statement->return_value = return_value;
5217 return (statement_t*) statement;
5221 * Parse a declaration statement.
5223 static statement_t *parse_declaration_statement(void)
5225 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5227 statement->base.source_position = token.source_position;
5229 declaration_t *before = last_declaration;
5230 parse_declaration(record_declaration);
5232 if(before == NULL) {
5233 statement->declaration.declarations_begin = context->declarations;
5235 statement->declaration.declarations_begin = before->next;
5237 statement->declaration.declarations_end = last_declaration;
5243 * Parse an expression statement, ie. expr ';'.
5245 static statement_t *parse_expression_statement(void)
5247 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5249 statement->base.source_position = token.source_position;
5250 statement->expression.expression = parse_expression();
5258 * Parse a statement.
5260 static statement_t *parse_statement(void)
5262 statement_t *statement = NULL;
5264 /* declaration or statement */
5265 switch(token.type) {
5267 statement = parse_asm_statement();
5271 statement = parse_case_statement();
5275 statement = parse_default_statement();
5279 statement = parse_compound_statement();
5283 statement = parse_if();
5287 statement = parse_switch();
5291 statement = parse_while();
5295 statement = parse_do();
5299 statement = parse_for();
5303 statement = parse_goto();
5307 statement = parse_continue();
5311 statement = parse_break();
5315 statement = parse_return();
5324 if(look_ahead(1)->type == ':') {
5325 statement = parse_label_statement();
5329 if(is_typedef_symbol(token.v.symbol)) {
5330 statement = parse_declaration_statement();
5334 statement = parse_expression_statement();
5337 case T___extension__:
5338 /* this can be a prefix to a declaration or an expression statement */
5339 /* we simply eat it now and parse the rest with tail recursion */
5342 } while(token.type == T___extension__);
5343 statement = parse_statement();
5347 statement = parse_declaration_statement();
5351 statement = parse_expression_statement();
5355 assert(statement == NULL
5356 || statement->base.source_position.input_name != NULL);
5362 * Parse a compound statement.
5364 static statement_t *parse_compound_statement(void)
5366 compound_statement_t *compound_statement
5367 = allocate_ast_zero(sizeof(compound_statement[0]));
5368 compound_statement->statement.kind = STATEMENT_COMPOUND;
5369 compound_statement->statement.source_position = token.source_position;
5373 int top = environment_top();
5374 context_t *last_context = context;
5375 set_context(&compound_statement->context);
5377 statement_t *last_statement = NULL;
5379 while(token.type != '}' && token.type != T_EOF) {
5380 statement_t *statement = parse_statement();
5381 if(statement == NULL)
5384 if(last_statement != NULL) {
5385 last_statement->base.next = statement;
5387 compound_statement->statements = statement;
5390 while(statement->base.next != NULL)
5391 statement = statement->base.next;
5393 last_statement = statement;
5396 if(token.type == '}') {
5399 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5402 assert(context == &compound_statement->context);
5403 set_context(last_context);
5404 environment_pop_to(top);
5406 return (statement_t*) compound_statement;
5410 * Initialize builtin types.
5412 static void initialize_builtin_types(void)
5414 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5415 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5416 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5417 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5418 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5419 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5420 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5421 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5423 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5424 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5425 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5426 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5430 * Parse a translation unit.
5432 static translation_unit_t *parse_translation_unit(void)
5434 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5436 assert(global_context == NULL);
5437 global_context = &unit->context;
5439 assert(context == NULL);
5440 set_context(&unit->context);
5442 initialize_builtin_types();
5444 while(token.type != T_EOF) {
5445 if (token.type == ';') {
5446 /* TODO error in strict mode */
5447 warningf(HERE, "stray ';' outside of function");
5450 parse_external_declaration();
5454 assert(context == &unit->context);
5456 last_declaration = NULL;
5458 assert(global_context == &unit->context);
5459 global_context = NULL;
5467 * @return the translation unit or NULL if errors occurred.
5469 translation_unit_t *parse(void)
5471 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5472 label_stack = NEW_ARR_F(stack_entry_t, 0);
5473 diagnostic_count = 0;
5477 type_set_output(stderr);
5478 ast_set_output(stderr);
5480 lookahead_bufpos = 0;
5481 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5484 translation_unit_t *unit = parse_translation_unit();
5486 DEL_ARR_F(environment_stack);
5487 DEL_ARR_F(label_stack);
5496 * Initialize the parser.
5498 void init_parser(void)
5500 init_expression_parsers();
5501 obstack_init(&temp_obst);
5503 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5504 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5508 * Terminate the parser.
5510 void exit_parser(void)
5512 obstack_free(&temp_obst, NULL);