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) {
3148 expression->base.source_position = statement->base.source_position;
3150 /* find last statement and use its type */
3151 type_t *type = type_void;
3152 const statement_t *stmt = statement->compound.statements;
3154 while (stmt->base.next != NULL)
3155 stmt = stmt->base.next;
3157 if (stmt->kind == STATEMENT_EXPRESSION) {
3158 type = stmt->expression.expression->base.datatype;
3161 warningf(expression->base.source_position, "empty statement expression ({})");
3163 expression->base.datatype = type;
3170 static expression_t *parse_brace_expression(void)
3174 switch(token.type) {
3176 /* gcc extension: a statement expression */
3177 return parse_statement_expression();
3181 return parse_cast();
3183 if(is_typedef_symbol(token.v.symbol)) {
3184 return parse_cast();
3188 expression_t *result = parse_expression();
3194 static expression_t *parse_function_keyword(void)
3199 if (current_function == NULL) {
3200 errorf(HERE, "'__func__' used outside of a function");
3203 string_literal_expression_t *expression
3204 = allocate_ast_zero(sizeof(expression[0]));
3206 expression->expression.kind = EXPR_FUNCTION;
3207 expression->expression.datatype = type_string;
3209 return (expression_t*) expression;
3212 static expression_t *parse_pretty_function_keyword(void)
3214 eat(T___PRETTY_FUNCTION__);
3217 if (current_function == NULL) {
3218 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3221 string_literal_expression_t *expression
3222 = allocate_ast_zero(sizeof(expression[0]));
3224 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3225 expression->expression.datatype = type_string;
3227 return (expression_t*) expression;
3230 static designator_t *parse_designator(void)
3232 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3234 if(token.type != T_IDENTIFIER) {
3235 parse_error_expected("while parsing member designator",
3240 result->symbol = token.v.symbol;
3243 designator_t *last_designator = result;
3245 if(token.type == '.') {
3247 if(token.type != T_IDENTIFIER) {
3248 parse_error_expected("while parsing member designator",
3253 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3254 designator->symbol = token.v.symbol;
3257 last_designator->next = designator;
3258 last_designator = designator;
3261 if(token.type == '[') {
3263 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3264 designator->array_access = parse_expression();
3265 if(designator->array_access == NULL) {
3271 last_designator->next = designator;
3272 last_designator = designator;
3281 static expression_t *parse_offsetof(void)
3283 eat(T___builtin_offsetof);
3285 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3286 expression->base.datatype = type_size_t;
3289 expression->offsetofe.type = parse_typename();
3291 expression->offsetofe.designator = parse_designator();
3297 static expression_t *parse_va_start(void)
3299 eat(T___builtin_va_start);
3301 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3304 expression->va_starte.ap = parse_assignment_expression();
3306 expression_t *const expr = parse_assignment_expression();
3307 if (expr->kind == EXPR_REFERENCE) {
3308 declaration_t *const decl = expr->reference.declaration;
3309 if (decl->parent_context == ¤t_function->context &&
3310 decl->next == NULL) {
3311 expression->va_starte.parameter = decl;
3316 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3318 return create_invalid_expression();
3321 static expression_t *parse_va_arg(void)
3323 eat(T___builtin_va_arg);
3325 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3328 expression->va_arge.ap = parse_assignment_expression();
3330 expression->base.datatype = parse_typename();
3336 static expression_t *parse_builtin_symbol(void)
3338 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3340 symbol_t *symbol = token.v.symbol;
3342 expression->builtin_symbol.symbol = symbol;
3345 type_t *type = get_builtin_symbol_type(symbol);
3346 type = automatic_type_conversion(type);
3348 expression->base.datatype = type;
3352 static expression_t *parse_builtin_constant(void)
3354 eat(T___builtin_constant_p);
3356 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3359 expression->builtin_constant.value = parse_assignment_expression();
3361 expression->base.datatype = type_int;
3366 static expression_t *parse_builtin_prefetch(void)
3368 eat(T___builtin_prefetch);
3370 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3373 expression->builtin_prefetch.adr = parse_assignment_expression();
3374 if (token.type == ',') {
3376 expression->builtin_prefetch.rw = parse_assignment_expression();
3378 if (token.type == ',') {
3380 expression->builtin_prefetch.locality = parse_assignment_expression();
3383 expression->base.datatype = type_void;
3388 static expression_t *parse_compare_builtin(void)
3390 expression_t *expression;
3392 switch(token.type) {
3393 case T___builtin_isgreater:
3394 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3396 case T___builtin_isgreaterequal:
3397 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3399 case T___builtin_isless:
3400 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3402 case T___builtin_islessequal:
3403 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3405 case T___builtin_islessgreater:
3406 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3408 case T___builtin_isunordered:
3409 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3412 panic("invalid compare builtin found");
3418 expression->binary.left = parse_assignment_expression();
3420 expression->binary.right = parse_assignment_expression();
3423 type_t *const orig_type_left = expression->binary.left->base.datatype;
3424 type_t *const orig_type_right = expression->binary.right->base.datatype;
3426 type_t *const type_left = skip_typeref(orig_type_left);
3427 type_t *const type_right = skip_typeref(orig_type_right);
3428 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3429 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3430 type_error_incompatible("invalid operands in comparison",
3431 token.source_position, orig_type_left, orig_type_right);
3434 semantic_comparison(&expression->binary);
3440 static expression_t *parse_builtin_expect(void)
3442 eat(T___builtin_expect);
3444 expression_t *expression
3445 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3448 expression->binary.left = parse_assignment_expression();
3450 expression->binary.right = parse_constant_expression();
3453 expression->base.datatype = expression->binary.left->base.datatype;
3458 static expression_t *parse_assume(void) {
3461 expression_t *expression
3462 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3465 expression->unary.value = parse_assignment_expression();
3468 expression->base.datatype = type_void;
3472 static expression_t *parse_alignof(void) {
3475 expression_t *expression
3476 = allocate_expression_zero(EXPR_ALIGNOF);
3479 expression->alignofe.type = parse_typename();
3482 expression->base.datatype = type_size_t;
3486 static expression_t *parse_primary_expression(void)
3488 switch(token.type) {
3490 return parse_int_const();
3491 case T_FLOATINGPOINT:
3492 return parse_float_const();
3493 case T_STRING_LITERAL:
3494 return parse_string_const();
3495 case T_WIDE_STRING_LITERAL:
3496 return parse_wide_string_const();
3498 return parse_reference();
3499 case T___FUNCTION__:
3501 return parse_function_keyword();
3502 case T___PRETTY_FUNCTION__:
3503 return parse_pretty_function_keyword();
3504 case T___builtin_offsetof:
3505 return parse_offsetof();
3506 case T___builtin_va_start:
3507 return parse_va_start();
3508 case T___builtin_va_arg:
3509 return parse_va_arg();
3510 case T___builtin_expect:
3511 return parse_builtin_expect();
3512 case T___builtin_nanf:
3513 case T___builtin_alloca:
3514 case T___builtin_va_end:
3515 return parse_builtin_symbol();
3516 case T___builtin_isgreater:
3517 case T___builtin_isgreaterequal:
3518 case T___builtin_isless:
3519 case T___builtin_islessequal:
3520 case T___builtin_islessgreater:
3521 case T___builtin_isunordered:
3522 return parse_compare_builtin();
3523 case T___builtin_constant_p:
3524 return parse_builtin_constant();
3525 case T___builtin_prefetch:
3526 return parse_builtin_prefetch();
3528 return parse_alignof();
3530 return parse_assume();
3533 return parse_brace_expression();
3536 errorf(HERE, "unexpected token '%K'", &token);
3539 return create_invalid_expression();
3543 * Check if the expression has the character type and issue a warning then.
3545 static void check_for_char_index_type(const expression_t *expression) {
3546 type_t *type = expression->base.datatype;
3547 type_t *base_type = skip_typeref(type);
3549 if (base_type->base.kind == TYPE_ATOMIC) {
3550 switch (base_type->atomic.akind == ATOMIC_TYPE_CHAR) {
3551 warningf(expression->base.source_position,
3552 "array subscript has type '%T'", type);
3557 static expression_t *parse_array_expression(unsigned precedence,
3564 expression_t *inside = parse_expression();
3566 array_access_expression_t *array_access
3567 = allocate_ast_zero(sizeof(array_access[0]));
3569 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3571 type_t *const orig_type_left = left->base.datatype;
3572 type_t *const orig_type_inside = inside->base.datatype;
3574 type_t *const type_left = skip_typeref(orig_type_left);
3575 type_t *const type_inside = skip_typeref(orig_type_inside);
3577 type_t *return_type;
3578 if (is_type_pointer(type_left)) {
3579 return_type = type_left->pointer.points_to;
3580 array_access->array_ref = left;
3581 array_access->index = inside;
3582 check_for_char_index_type(inside);
3583 } else if (is_type_pointer(type_inside)) {
3584 return_type = type_inside->pointer.points_to;
3585 array_access->array_ref = inside;
3586 array_access->index = left;
3587 array_access->flipped = true;
3588 check_for_char_index_type(left);
3590 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3592 "array access on object with non-pointer types '%T', '%T'",
3593 orig_type_left, orig_type_inside);
3595 return_type = type_error_type;
3596 array_access->array_ref = create_invalid_expression();
3599 if(token.type != ']') {
3600 parse_error_expected("Problem while parsing array access", ']', 0);
3601 return (expression_t*) array_access;
3605 return_type = automatic_type_conversion(return_type);
3606 array_access->expression.datatype = return_type;
3608 return (expression_t*) array_access;
3611 static expression_t *parse_sizeof(unsigned precedence)
3615 sizeof_expression_t *sizeof_expression
3616 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3617 sizeof_expression->expression.kind = EXPR_SIZEOF;
3618 sizeof_expression->expression.datatype = type_size_t;
3620 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3622 sizeof_expression->type = parse_typename();
3625 expression_t *expression = parse_sub_expression(precedence);
3626 expression->base.datatype = revert_automatic_type_conversion(expression);
3628 sizeof_expression->type = expression->base.datatype;
3629 sizeof_expression->size_expression = expression;
3632 return (expression_t*) sizeof_expression;
3635 static expression_t *parse_select_expression(unsigned precedence,
3636 expression_t *compound)
3639 assert(token.type == '.' || token.type == T_MINUSGREATER);
3641 bool is_pointer = (token.type == T_MINUSGREATER);
3644 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3645 select->select.compound = compound;
3647 if(token.type != T_IDENTIFIER) {
3648 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3651 symbol_t *symbol = token.v.symbol;
3652 select->select.symbol = symbol;
3655 type_t *const orig_type = compound->base.datatype;
3656 type_t *const type = skip_typeref(orig_type);
3658 type_t *type_left = type;
3660 if (!is_type_pointer(type)) {
3661 if (is_type_valid(type)) {
3662 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3664 return create_invalid_expression();
3666 type_left = type->pointer.points_to;
3668 type_left = skip_typeref(type_left);
3670 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3671 type_left->kind != TYPE_COMPOUND_UNION) {
3672 if (is_type_valid(type_left)) {
3673 errorf(HERE, "request for member '%Y' in something not a struct or "
3674 "union, but '%T'", symbol, type_left);
3676 return create_invalid_expression();
3679 declaration_t *const declaration = type_left->compound.declaration;
3681 if(!declaration->init.is_defined) {
3682 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3684 return create_invalid_expression();
3687 declaration_t *iter = declaration->context.declarations;
3688 for( ; iter != NULL; iter = iter->next) {
3689 if(iter->symbol == symbol) {
3694 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3695 return create_invalid_expression();
3698 /* we always do the auto-type conversions; the & and sizeof parser contains
3699 * code to revert this! */
3700 type_t *expression_type = automatic_type_conversion(iter->type);
3702 select->select.compound_entry = iter;
3703 select->base.datatype = expression_type;
3705 if(expression_type->kind == TYPE_BITFIELD) {
3706 expression_t *extract
3707 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3708 extract->unary.value = select;
3709 extract->base.datatype = expression_type->bitfield.base;
3718 * Parse a call expression, ie. expression '( ... )'.
3720 * @param expression the function address
3722 static expression_t *parse_call_expression(unsigned precedence,
3723 expression_t *expression)
3726 expression_t *result = allocate_expression_zero(EXPR_CALL);
3728 call_expression_t *call = &result->call;
3729 call->function = expression;
3731 type_t *const orig_type = expression->base.datatype;
3732 type_t *const type = skip_typeref(orig_type);
3734 function_type_t *function_type = NULL;
3735 if (is_type_pointer(type)) {
3736 type_t *const to_type = skip_typeref(type->pointer.points_to);
3738 if (is_type_function(to_type)) {
3739 function_type = &to_type->function;
3740 call->expression.datatype = function_type->return_type;
3744 if (function_type == NULL && is_type_valid(type)) {
3745 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3748 /* parse arguments */
3751 if(token.type != ')') {
3752 call_argument_t *last_argument = NULL;
3755 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3757 argument->expression = parse_assignment_expression();
3758 if(last_argument == NULL) {
3759 call->arguments = argument;
3761 last_argument->next = argument;
3763 last_argument = argument;
3765 if(token.type != ',')
3772 if(function_type != NULL) {
3773 function_parameter_t *parameter = function_type->parameters;
3774 call_argument_t *argument = call->arguments;
3775 for( ; parameter != NULL && argument != NULL;
3776 parameter = parameter->next, argument = argument->next) {
3777 type_t *expected_type = parameter->type;
3778 /* TODO report context in error messages */
3779 expression_t *const arg_expr = argument->expression;
3780 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
3781 if (res_type == NULL) {
3782 /* TODO improve error message */
3783 errorf(arg_expr->base.source_position,
3784 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
3785 arg_expr, arg_expr->base.datatype, expected_type);
3787 argument->expression = create_implicit_cast(argument->expression, expected_type);
3790 /* too few parameters */
3791 if(parameter != NULL) {
3792 errorf(HERE, "too few arguments to function '%E'", expression);
3793 } else if(argument != NULL) {
3794 /* too many parameters */
3795 if(!function_type->variadic
3796 && !function_type->unspecified_parameters) {
3797 errorf(HERE, "too many arguments to function '%E'", expression);
3799 /* do default promotion */
3800 for( ; argument != NULL; argument = argument->next) {
3801 type_t *type = argument->expression->base.datatype;
3803 type = skip_typeref(type);
3804 if(is_type_integer(type)) {
3805 type = promote_integer(type);
3806 } else if(type == type_float) {
3810 argument->expression
3811 = create_implicit_cast(argument->expression, type);
3814 check_format(&result->call);
3817 check_format(&result->call);
3824 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3826 static bool same_compound_type(const type_t *type1, const type_t *type2)
3829 is_type_compound(type1) &&
3830 type1->kind == type2->kind &&
3831 type1->compound.declaration == type2->compound.declaration;
3835 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3837 * @param expression the conditional expression
3839 static expression_t *parse_conditional_expression(unsigned precedence,
3840 expression_t *expression)
3844 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3846 conditional_expression_t *conditional = &result->conditional;
3847 conditional->condition = expression;
3850 type_t *const condition_type_orig = expression->base.datatype;
3851 type_t *const condition_type = skip_typeref(condition_type_orig);
3852 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
3853 type_error("expected a scalar type in conditional condition",
3854 expression->base.source_position, condition_type_orig);
3857 expression_t *true_expression = parse_expression();
3859 expression_t *false_expression = parse_sub_expression(precedence);
3861 conditional->true_expression = true_expression;
3862 conditional->false_expression = false_expression;
3864 type_t *const orig_true_type = true_expression->base.datatype;
3865 type_t *const orig_false_type = false_expression->base.datatype;
3866 type_t *const true_type = skip_typeref(orig_true_type);
3867 type_t *const false_type = skip_typeref(orig_false_type);
3870 type_t *result_type;
3871 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3872 result_type = semantic_arithmetic(true_type, false_type);
3874 true_expression = create_implicit_cast(true_expression, result_type);
3875 false_expression = create_implicit_cast(false_expression, result_type);
3877 conditional->true_expression = true_expression;
3878 conditional->false_expression = false_expression;
3879 conditional->expression.datatype = result_type;
3880 } else if (same_compound_type(true_type, false_type) || (
3881 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3882 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
3884 /* just take 1 of the 2 types */
3885 result_type = true_type;
3886 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3887 && pointers_compatible(true_type, false_type)) {
3889 result_type = true_type;
3892 if (is_type_valid(true_type) && is_type_valid(false_type)) {
3893 type_error_incompatible("while parsing conditional",
3894 expression->base.source_position, true_type,
3897 result_type = type_error_type;
3900 conditional->expression.datatype = result_type;
3905 * Parse an extension expression.
3907 static expression_t *parse_extension(unsigned precedence)
3909 eat(T___extension__);
3911 /* TODO enable extensions */
3912 expression_t *expression = parse_sub_expression(precedence);
3913 /* TODO disable extensions */
3917 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3919 eat(T___builtin_classify_type);
3921 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3922 result->base.datatype = type_int;
3925 expression_t *expression = parse_sub_expression(precedence);
3927 result->classify_type.type_expression = expression;
3932 static void semantic_incdec(unary_expression_t *expression)
3934 type_t *const orig_type = expression->value->base.datatype;
3935 type_t *const type = skip_typeref(orig_type);
3936 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3937 if (is_type_valid(type)) {
3938 /* TODO: improve error message */
3939 errorf(HERE, "operation needs an arithmetic or pointer type");
3944 expression->expression.datatype = orig_type;
3947 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3949 type_t *const orig_type = expression->value->base.datatype;
3950 type_t *const type = skip_typeref(orig_type);
3951 if(!is_type_arithmetic(type)) {
3952 if (is_type_valid(type)) {
3953 /* TODO: improve error message */
3954 errorf(HERE, "operation needs an arithmetic type");
3959 expression->expression.datatype = orig_type;
3962 static void semantic_unexpr_scalar(unary_expression_t *expression)
3964 type_t *const orig_type = expression->value->base.datatype;
3965 type_t *const type = skip_typeref(orig_type);
3966 if (!is_type_scalar(type)) {
3967 if (is_type_valid(type)) {
3968 errorf(HERE, "operand of ! must be of scalar type");
3973 expression->expression.datatype = orig_type;
3976 static void semantic_unexpr_integer(unary_expression_t *expression)
3978 type_t *const orig_type = expression->value->base.datatype;
3979 type_t *const type = skip_typeref(orig_type);
3980 if (!is_type_integer(type)) {
3981 if (is_type_valid(type)) {
3982 errorf(HERE, "operand of ~ must be of integer type");
3987 expression->expression.datatype = orig_type;
3990 static void semantic_dereference(unary_expression_t *expression)
3992 type_t *const orig_type = expression->value->base.datatype;
3993 type_t *const type = skip_typeref(orig_type);
3994 if(!is_type_pointer(type)) {
3995 if (is_type_valid(type)) {
3996 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4001 type_t *result_type = type->pointer.points_to;
4002 result_type = automatic_type_conversion(result_type);
4003 expression->expression.datatype = result_type;
4007 * Check the semantic of the address taken expression.
4009 static void semantic_take_addr(unary_expression_t *expression)
4011 expression_t *value = expression->value;
4012 value->base.datatype = revert_automatic_type_conversion(value);
4014 type_t *orig_type = value->base.datatype;
4015 if(!is_type_valid(orig_type))
4018 if(value->kind == EXPR_REFERENCE) {
4019 declaration_t *const declaration = value->reference.declaration;
4020 if(declaration != NULL) {
4021 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4022 errorf(expression->expression.source_position,
4023 "address of register variable '%Y' requested",
4024 declaration->symbol);
4026 declaration->address_taken = 1;
4030 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4033 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4034 static expression_t *parse_##unexpression_type(unsigned precedence) \
4038 expression_t *unary_expression \
4039 = allocate_expression_zero(unexpression_type); \
4040 unary_expression->base.source_position = HERE; \
4041 unary_expression->unary.value = parse_sub_expression(precedence); \
4043 sfunc(&unary_expression->unary); \
4045 return unary_expression; \
4048 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4049 semantic_unexpr_arithmetic)
4050 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4051 semantic_unexpr_arithmetic)
4052 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4053 semantic_unexpr_scalar)
4054 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4055 semantic_dereference)
4056 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4058 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4059 semantic_unexpr_integer)
4060 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4062 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4065 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4067 static expression_t *parse_##unexpression_type(unsigned precedence, \
4068 expression_t *left) \
4070 (void) precedence; \
4073 expression_t *unary_expression \
4074 = allocate_expression_zero(unexpression_type); \
4075 unary_expression->unary.value = left; \
4077 sfunc(&unary_expression->unary); \
4079 return unary_expression; \
4082 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4083 EXPR_UNARY_POSTFIX_INCREMENT,
4085 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4086 EXPR_UNARY_POSTFIX_DECREMENT,
4089 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4091 /* TODO: handle complex + imaginary types */
4093 /* § 6.3.1.8 Usual arithmetic conversions */
4094 if(type_left == type_long_double || type_right == type_long_double) {
4095 return type_long_double;
4096 } else if(type_left == type_double || type_right == type_double) {
4098 } else if(type_left == type_float || type_right == type_float) {
4102 type_right = promote_integer(type_right);
4103 type_left = promote_integer(type_left);
4105 if(type_left == type_right)
4108 bool signed_left = is_type_signed(type_left);
4109 bool signed_right = is_type_signed(type_right);
4110 int rank_left = get_rank(type_left);
4111 int rank_right = get_rank(type_right);
4112 if(rank_left < rank_right) {
4113 if(signed_left == signed_right || !signed_right) {
4119 if(signed_left == signed_right || !signed_left) {
4128 * Check the semantic restrictions for a binary expression.
4130 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4132 expression_t *const left = expression->left;
4133 expression_t *const right = expression->right;
4134 type_t *const orig_type_left = left->base.datatype;
4135 type_t *const orig_type_right = right->base.datatype;
4136 type_t *const type_left = skip_typeref(orig_type_left);
4137 type_t *const type_right = skip_typeref(orig_type_right);
4139 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4140 /* TODO: improve error message */
4141 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4142 errorf(HERE, "operation needs arithmetic types");
4147 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4148 expression->left = create_implicit_cast(left, arithmetic_type);
4149 expression->right = create_implicit_cast(right, arithmetic_type);
4150 expression->expression.datatype = arithmetic_type;
4153 static void semantic_shift_op(binary_expression_t *expression)
4155 expression_t *const left = expression->left;
4156 expression_t *const right = expression->right;
4157 type_t *const orig_type_left = left->base.datatype;
4158 type_t *const orig_type_right = right->base.datatype;
4159 type_t * type_left = skip_typeref(orig_type_left);
4160 type_t * type_right = skip_typeref(orig_type_right);
4162 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4163 /* TODO: improve error message */
4164 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4165 errorf(HERE, "operation needs integer types");
4170 type_left = promote_integer(type_left);
4171 type_right = promote_integer(type_right);
4173 expression->left = create_implicit_cast(left, type_left);
4174 expression->right = create_implicit_cast(right, type_right);
4175 expression->expression.datatype = type_left;
4178 static void semantic_add(binary_expression_t *expression)
4180 expression_t *const left = expression->left;
4181 expression_t *const right = expression->right;
4182 type_t *const orig_type_left = left->base.datatype;
4183 type_t *const orig_type_right = right->base.datatype;
4184 type_t *const type_left = skip_typeref(orig_type_left);
4185 type_t *const type_right = skip_typeref(orig_type_right);
4188 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4189 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4190 expression->left = create_implicit_cast(left, arithmetic_type);
4191 expression->right = create_implicit_cast(right, arithmetic_type);
4192 expression->expression.datatype = arithmetic_type;
4194 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4195 expression->expression.datatype = type_left;
4196 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4197 expression->expression.datatype = type_right;
4198 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4199 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4203 static void semantic_sub(binary_expression_t *expression)
4205 expression_t *const left = expression->left;
4206 expression_t *const right = expression->right;
4207 type_t *const orig_type_left = left->base.datatype;
4208 type_t *const orig_type_right = right->base.datatype;
4209 type_t *const type_left = skip_typeref(orig_type_left);
4210 type_t *const type_right = skip_typeref(orig_type_right);
4213 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4214 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4215 expression->left = create_implicit_cast(left, arithmetic_type);
4216 expression->right = create_implicit_cast(right, arithmetic_type);
4217 expression->expression.datatype = arithmetic_type;
4219 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4220 expression->expression.datatype = type_left;
4221 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4222 if(!pointers_compatible(type_left, type_right)) {
4223 errorf(HERE, "pointers to incompatible objects to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4225 expression->expression.datatype = type_ptrdiff_t;
4227 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4228 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4232 static void semantic_comparison(binary_expression_t *expression)
4234 expression_t *left = expression->left;
4235 expression_t *right = expression->right;
4236 type_t *orig_type_left = left->base.datatype;
4237 type_t *orig_type_right = right->base.datatype;
4239 type_t *type_left = skip_typeref(orig_type_left);
4240 type_t *type_right = skip_typeref(orig_type_right);
4242 /* TODO non-arithmetic types */
4243 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4244 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4245 expression->left = create_implicit_cast(left, arithmetic_type);
4246 expression->right = create_implicit_cast(right, arithmetic_type);
4247 expression->expression.datatype = arithmetic_type;
4248 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4249 /* TODO check compatibility */
4250 } else if (is_type_pointer(type_left)) {
4251 expression->right = create_implicit_cast(right, type_left);
4252 } else if (is_type_pointer(type_right)) {
4253 expression->left = create_implicit_cast(left, type_right);
4254 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4255 type_error_incompatible("invalid operands in comparison",
4256 token.source_position, type_left, type_right);
4258 expression->expression.datatype = type_int;
4261 static void semantic_arithmetic_assign(binary_expression_t *expression)
4263 expression_t *left = expression->left;
4264 expression_t *right = expression->right;
4265 type_t *orig_type_left = left->base.datatype;
4266 type_t *orig_type_right = right->base.datatype;
4268 type_t *type_left = skip_typeref(orig_type_left);
4269 type_t *type_right = skip_typeref(orig_type_right);
4271 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4272 /* TODO: improve error message */
4273 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4274 errorf(HERE, "operation needs arithmetic types");
4279 /* combined instructions are tricky. We can't create an implicit cast on
4280 * the left side, because we need the uncasted form for the store.
4281 * The ast2firm pass has to know that left_type must be right_type
4282 * for the arithmetic operation and create a cast by itself */
4283 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4284 expression->right = create_implicit_cast(right, arithmetic_type);
4285 expression->expression.datatype = type_left;
4288 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4290 expression_t *const left = expression->left;
4291 expression_t *const right = expression->right;
4292 type_t *const orig_type_left = left->base.datatype;
4293 type_t *const orig_type_right = right->base.datatype;
4294 type_t *const type_left = skip_typeref(orig_type_left);
4295 type_t *const type_right = skip_typeref(orig_type_right);
4297 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4298 /* combined instructions are tricky. We can't create an implicit cast on
4299 * the left side, because we need the uncasted form for the store.
4300 * The ast2firm pass has to know that left_type must be right_type
4301 * for the arithmetic operation and create a cast by itself */
4302 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4303 expression->right = create_implicit_cast(right, arithmetic_type);
4304 expression->expression.datatype = type_left;
4305 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4306 expression->expression.datatype = type_left;
4307 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4308 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4313 * Check the semantic restrictions of a logical expression.
4315 static void semantic_logical_op(binary_expression_t *expression)
4317 expression_t *const left = expression->left;
4318 expression_t *const right = expression->right;
4319 type_t *const orig_type_left = left->base.datatype;
4320 type_t *const orig_type_right = right->base.datatype;
4321 type_t *const type_left = skip_typeref(orig_type_left);
4322 type_t *const type_right = skip_typeref(orig_type_right);
4324 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4325 /* TODO: improve error message */
4326 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4327 errorf(HERE, "operation needs scalar types");
4332 expression->expression.datatype = type_int;
4336 * Checks if a compound type has constant fields.
4338 static bool has_const_fields(const compound_type_t *type)
4340 const context_t *context = &type->declaration->context;
4341 const declaration_t *declaration = context->declarations;
4343 for (; declaration != NULL; declaration = declaration->next) {
4344 if (declaration->namespc != NAMESPACE_NORMAL)
4347 const type_t *decl_type = skip_typeref(declaration->type);
4348 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4356 * Check the semantic restrictions of a binary assign expression.
4358 static void semantic_binexpr_assign(binary_expression_t *expression)
4360 expression_t *left = expression->left;
4361 type_t *orig_type_left = left->base.datatype;
4363 type_t *type_left = revert_automatic_type_conversion(left);
4364 type_left = skip_typeref(orig_type_left);
4366 /* must be a modifiable lvalue */
4367 if (is_type_array(type_left)) {
4368 errorf(HERE, "cannot assign to arrays ('%E')", left);
4371 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4372 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4376 if(is_type_incomplete(type_left)) {
4378 "left-hand side of assignment '%E' has incomplete type '%T'",
4379 left, orig_type_left);
4382 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4383 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4384 left, orig_type_left);
4388 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4390 if (res_type == NULL) {
4391 errorf(expression->expression.source_position,
4392 "cannot assign to '%T' from '%T'",
4393 orig_type_left, expression->right->base.datatype);
4395 expression->right = create_implicit_cast(expression->right, res_type);
4398 expression->expression.datatype = orig_type_left;
4401 static void semantic_comma(binary_expression_t *expression)
4403 expression->expression.datatype = expression->right->base.datatype;
4406 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4407 static expression_t *parse_##binexpression_type(unsigned precedence, \
4408 expression_t *left) \
4412 expression_t *right = parse_sub_expression(precedence + lr); \
4414 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4415 binexpr->binary.left = left; \
4416 binexpr->binary.right = right; \
4417 sfunc(&binexpr->binary); \
4422 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4423 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4424 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4425 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4426 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4427 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4428 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4429 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4430 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4432 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4433 semantic_comparison, 1)
4434 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4435 semantic_comparison, 1)
4436 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4437 semantic_comparison, 1)
4438 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4439 semantic_comparison, 1)
4441 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4442 semantic_binexpr_arithmetic, 1)
4443 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4444 semantic_binexpr_arithmetic, 1)
4445 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4446 semantic_binexpr_arithmetic, 1)
4447 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4448 semantic_logical_op, 1)
4449 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4450 semantic_logical_op, 1)
4451 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4452 semantic_shift_op, 1)
4453 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4454 semantic_shift_op, 1)
4455 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4456 semantic_arithmetic_addsubb_assign, 0)
4457 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4458 semantic_arithmetic_addsubb_assign, 0)
4459 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4460 semantic_arithmetic_assign, 0)
4461 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4462 semantic_arithmetic_assign, 0)
4463 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4464 semantic_arithmetic_assign, 0)
4465 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4466 semantic_arithmetic_assign, 0)
4467 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4468 semantic_arithmetic_assign, 0)
4469 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4470 semantic_arithmetic_assign, 0)
4471 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4472 semantic_arithmetic_assign, 0)
4473 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4474 semantic_arithmetic_assign, 0)
4476 static expression_t *parse_sub_expression(unsigned precedence)
4478 if(token.type < 0) {
4479 return expected_expression_error();
4482 expression_parser_function_t *parser
4483 = &expression_parsers[token.type];
4484 source_position_t source_position = token.source_position;
4487 if(parser->parser != NULL) {
4488 left = parser->parser(parser->precedence);
4490 left = parse_primary_expression();
4492 assert(left != NULL);
4493 left->base.source_position = source_position;
4496 if(token.type < 0) {
4497 return expected_expression_error();
4500 parser = &expression_parsers[token.type];
4501 if(parser->infix_parser == NULL)
4503 if(parser->infix_precedence < precedence)
4506 left = parser->infix_parser(parser->infix_precedence, left);
4508 assert(left != NULL);
4509 assert(left->kind != EXPR_UNKNOWN);
4510 left->base.source_position = source_position;
4517 * Parse an expression.
4519 static expression_t *parse_expression(void)
4521 return parse_sub_expression(1);
4525 * Register a parser for a prefix-like operator with given precedence.
4527 * @param parser the parser function
4528 * @param token_type the token type of the prefix token
4529 * @param precedence the precedence of the operator
4531 static void register_expression_parser(parse_expression_function parser,
4532 int token_type, unsigned precedence)
4534 expression_parser_function_t *entry = &expression_parsers[token_type];
4536 if(entry->parser != NULL) {
4537 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4538 panic("trying to register multiple expression parsers for a token");
4540 entry->parser = parser;
4541 entry->precedence = precedence;
4545 * Register a parser for an infix operator with given precedence.
4547 * @param parser the parser function
4548 * @param token_type the token type of the infix operator
4549 * @param precedence the precedence of the operator
4551 static void register_infix_parser(parse_expression_infix_function parser,
4552 int token_type, unsigned precedence)
4554 expression_parser_function_t *entry = &expression_parsers[token_type];
4556 if(entry->infix_parser != NULL) {
4557 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4558 panic("trying to register multiple infix expression parsers for a "
4561 entry->infix_parser = parser;
4562 entry->infix_precedence = precedence;
4566 * Initialize the expression parsers.
4568 static void init_expression_parsers(void)
4570 memset(&expression_parsers, 0, sizeof(expression_parsers));
4572 register_infix_parser(parse_array_expression, '[', 30);
4573 register_infix_parser(parse_call_expression, '(', 30);
4574 register_infix_parser(parse_select_expression, '.', 30);
4575 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4576 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4578 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4581 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4582 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4583 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4584 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4585 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4586 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4587 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4588 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4589 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4590 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4591 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4592 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4593 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4594 T_EXCLAMATIONMARKEQUAL, 13);
4595 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4596 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4597 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4598 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4599 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4600 register_infix_parser(parse_conditional_expression, '?', 7);
4601 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4602 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4603 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4604 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4605 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4606 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4607 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4608 T_LESSLESSEQUAL, 2);
4609 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4610 T_GREATERGREATEREQUAL, 2);
4611 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4613 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4615 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4618 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4620 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4621 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4622 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4623 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4624 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4625 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4626 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4628 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4630 register_expression_parser(parse_sizeof, T_sizeof, 25);
4631 register_expression_parser(parse_extension, T___extension__, 25);
4632 register_expression_parser(parse_builtin_classify_type,
4633 T___builtin_classify_type, 25);
4637 * Parse a asm statement constraints specification.
4639 static asm_constraint_t *parse_asm_constraints(void)
4641 asm_constraint_t *result = NULL;
4642 asm_constraint_t *last = NULL;
4644 while(token.type == T_STRING_LITERAL || token.type == '[') {
4645 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4646 memset(constraint, 0, sizeof(constraint[0]));
4648 if(token.type == '[') {
4650 if(token.type != T_IDENTIFIER) {
4651 parse_error_expected("while parsing asm constraint",
4655 constraint->symbol = token.v.symbol;
4660 constraint->constraints = parse_string_literals();
4662 constraint->expression = parse_expression();
4666 last->next = constraint;
4668 result = constraint;
4672 if(token.type != ',')
4681 * Parse a asm statement clobber specification.
4683 static asm_clobber_t *parse_asm_clobbers(void)
4685 asm_clobber_t *result = NULL;
4686 asm_clobber_t *last = NULL;
4688 while(token.type == T_STRING_LITERAL) {
4689 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4690 clobber->clobber = parse_string_literals();
4693 last->next = clobber;
4699 if(token.type != ',')
4708 * Parse an asm statement.
4710 static statement_t *parse_asm_statement(void)
4714 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4715 statement->base.source_position = token.source_position;
4717 asm_statement_t *asm_statement = &statement->asms;
4719 if(token.type == T_volatile) {
4721 asm_statement->is_volatile = true;
4725 asm_statement->asm_text = parse_string_literals();
4727 if(token.type != ':')
4731 asm_statement->inputs = parse_asm_constraints();
4732 if(token.type != ':')
4736 asm_statement->outputs = parse_asm_constraints();
4737 if(token.type != ':')
4741 asm_statement->clobbers = parse_asm_clobbers();
4750 * Parse a case statement.
4752 static statement_t *parse_case_statement(void)
4756 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4758 statement->base.source_position = token.source_position;
4759 statement->case_label.expression = parse_expression();
4763 if (! is_constant_expression(statement->case_label.expression)) {
4764 errorf(statement->base.source_position,
4765 "case label does not reduce to an integer constant");
4767 /* TODO: check if the case label is already known */
4768 if (current_switch != NULL) {
4769 /* link all cases into the switch statement */
4770 if (current_switch->last_case == NULL) {
4771 current_switch->first_case =
4772 current_switch->last_case = &statement->case_label;
4774 current_switch->last_case->next = &statement->case_label;
4777 errorf(statement->base.source_position,
4778 "case label not within a switch statement");
4781 statement->case_label.label_statement = parse_statement();
4787 * Finds an existing default label of a switch statement.
4789 static case_label_statement_t *
4790 find_default_label(const switch_statement_t *statement)
4792 for (case_label_statement_t *label = statement->first_case;
4794 label = label->next) {
4795 if (label->expression == NULL)
4802 * Parse a default statement.
4804 static statement_t *parse_default_statement(void)
4808 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4810 statement->base.source_position = token.source_position;
4813 if (current_switch != NULL) {
4814 const case_label_statement_t *def_label = find_default_label(current_switch);
4815 if (def_label != NULL) {
4816 errorf(HERE, "multiple default labels in one switch");
4817 errorf(def_label->statement.source_position,
4818 "this is the first default label");
4820 /* link all cases into the switch statement */
4821 if (current_switch->last_case == NULL) {
4822 current_switch->first_case =
4823 current_switch->last_case = &statement->case_label;
4825 current_switch->last_case->next = &statement->case_label;
4829 errorf(statement->base.source_position,
4830 "'default' label not within a switch statement");
4832 statement->label.label_statement = parse_statement();
4838 * Return the declaration for a given label symbol or create a new one.
4840 static declaration_t *get_label(symbol_t *symbol)
4842 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4843 assert(current_function != NULL);
4844 /* if we found a label in the same function, then we already created the
4846 if(candidate != NULL
4847 && candidate->parent_context == ¤t_function->context) {
4851 /* otherwise we need to create a new one */
4852 declaration_t *const declaration = allocate_declaration_zero();
4853 declaration->namespc = NAMESPACE_LABEL;
4854 declaration->symbol = symbol;
4856 label_push(declaration);
4862 * Parse a label statement.
4864 static statement_t *parse_label_statement(void)
4866 assert(token.type == T_IDENTIFIER);
4867 symbol_t *symbol = token.v.symbol;
4870 declaration_t *label = get_label(symbol);
4872 /* if source position is already set then the label is defined twice,
4873 * otherwise it was just mentioned in a goto so far */
4874 if(label->source_position.input_name != NULL) {
4875 errorf(HERE, "duplicate label '%Y'", symbol);
4876 errorf(label->source_position, "previous definition of '%Y' was here",
4879 label->source_position = token.source_position;
4882 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4884 label_statement->statement.kind = STATEMENT_LABEL;
4885 label_statement->statement.source_position = token.source_position;
4886 label_statement->label = label;
4890 if(token.type == '}') {
4891 /* TODO only warn? */
4892 errorf(HERE, "label at end of compound statement");
4893 return (statement_t*) label_statement;
4895 label_statement->label_statement = parse_statement();
4898 return (statement_t*) label_statement;
4902 * Parse an if statement.
4904 static statement_t *parse_if(void)
4908 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4909 statement->statement.kind = STATEMENT_IF;
4910 statement->statement.source_position = token.source_position;
4913 statement->condition = parse_expression();
4916 statement->true_statement = parse_statement();
4917 if(token.type == T_else) {
4919 statement->false_statement = parse_statement();
4922 return (statement_t*) statement;
4926 * Parse a switch statement.
4928 static statement_t *parse_switch(void)
4932 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4933 statement->statement.kind = STATEMENT_SWITCH;
4934 statement->statement.source_position = token.source_position;
4937 expression_t *const expr = parse_expression();
4938 type_t * type = skip_typeref(expr->base.datatype);
4939 if (is_type_integer(type)) {
4940 type = promote_integer(type);
4941 } else if (is_type_valid(type)) {
4942 errorf(expr->base.source_position, "switch quantity is not an integer, but '%T'", type);
4943 type = type_error_type;
4945 statement->expression = create_implicit_cast(expr, type);
4948 switch_statement_t *rem = current_switch;
4949 current_switch = statement;
4950 statement->body = parse_statement();
4951 current_switch = rem;
4953 return (statement_t*) statement;
4956 static statement_t *parse_loop_body(statement_t *const loop)
4958 statement_t *const rem = current_loop;
4959 current_loop = loop;
4960 statement_t *const body = parse_statement();
4966 * Parse a while statement.
4968 static statement_t *parse_while(void)
4972 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4973 statement->statement.kind = STATEMENT_WHILE;
4974 statement->statement.source_position = token.source_position;
4977 statement->condition = parse_expression();
4980 statement->body = parse_loop_body((statement_t*)statement);
4982 return (statement_t*) statement;
4986 * Parse a do statement.
4988 static statement_t *parse_do(void)
4992 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4993 statement->statement.kind = STATEMENT_DO_WHILE;
4994 statement->statement.source_position = token.source_position;
4996 statement->body = parse_loop_body((statement_t*)statement);
4999 statement->condition = parse_expression();
5003 return (statement_t*) statement;
5007 * Parse a for statement.
5009 static statement_t *parse_for(void)
5013 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5014 statement->statement.kind = STATEMENT_FOR;
5015 statement->statement.source_position = token.source_position;
5019 int top = environment_top();
5020 context_t *last_context = context;
5021 set_context(&statement->context);
5023 if(token.type != ';') {
5024 if(is_declaration_specifier(&token, false)) {
5025 parse_declaration(record_declaration);
5027 statement->initialisation = parse_expression();
5034 if(token.type != ';') {
5035 statement->condition = parse_expression();
5038 if(token.type != ')') {
5039 statement->step = parse_expression();
5042 statement->body = parse_loop_body((statement_t*)statement);
5044 assert(context == &statement->context);
5045 set_context(last_context);
5046 environment_pop_to(top);
5048 return (statement_t*) statement;
5052 * Parse a goto statement.
5054 static statement_t *parse_goto(void)
5058 if(token.type != T_IDENTIFIER) {
5059 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5063 symbol_t *symbol = token.v.symbol;
5066 declaration_t *label = get_label(symbol);
5068 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5070 statement->statement.kind = STATEMENT_GOTO;
5071 statement->statement.source_position = token.source_position;
5073 statement->label = label;
5075 /* remember the goto's in a list for later checking */
5076 if (goto_last == NULL) {
5077 goto_first = goto_last = statement;
5079 goto_last->next = statement;
5084 return (statement_t*) statement;
5088 * Parse a continue statement.
5090 static statement_t *parse_continue(void)
5092 statement_t *statement;
5093 if (current_loop == NULL) {
5094 errorf(HERE, "continue statement not within loop");
5097 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5099 statement->base.source_position = token.source_position;
5109 * Parse a break statement.
5111 static statement_t *parse_break(void)
5113 statement_t *statement;
5114 if (current_switch == NULL && current_loop == NULL) {
5115 errorf(HERE, "break statement not within loop or switch");
5118 statement = allocate_statement_zero(STATEMENT_BREAK);
5120 statement->base.source_position = token.source_position;
5130 * Check if a given declaration represents a local variable.
5132 static bool is_local_var_declaration(const declaration_t *declaration) {
5133 switch ((storage_class_tag_t) declaration->storage_class) {
5134 case STORAGE_CLASS_NONE:
5135 case STORAGE_CLASS_AUTO:
5136 case STORAGE_CLASS_REGISTER: {
5137 const type_t *type = skip_typeref(declaration->type);
5138 if(is_type_function(type)) {
5150 * Check if a given expression represents a local variable.
5152 static bool is_local_variable(const expression_t *expression)
5154 if (expression->base.kind != EXPR_REFERENCE) {
5157 const declaration_t *declaration = expression->reference.declaration;
5158 return is_local_var_declaration(declaration);
5162 * Parse a return statement.
5164 static statement_t *parse_return(void)
5168 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5170 statement->statement.kind = STATEMENT_RETURN;
5171 statement->statement.source_position = token.source_position;
5173 expression_t *return_value = NULL;
5174 if(token.type != ';') {
5175 return_value = parse_expression();
5179 const type_t *const func_type = current_function->type;
5180 assert(is_type_function(func_type));
5181 type_t *const return_type = skip_typeref(func_type->function.return_type);
5183 if(return_value != NULL) {
5184 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5186 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5187 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5188 warningf(statement->statement.source_position,
5189 "'return' with a value, in function returning void");
5190 return_value = NULL;
5192 type_t *const res_type = semantic_assign(return_type,
5193 return_value, "'return'");
5194 if (res_type == NULL) {
5195 errorf(statement->statement.source_position,
5196 "cannot return something of type '%T' in function returning '%T'",
5197 return_value->base.datatype, return_type);
5199 return_value = create_implicit_cast(return_value, res_type);
5202 /* check for returning address of a local var */
5203 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5204 const expression_t *expression = return_value->unary.value;
5205 if (is_local_variable(expression)) {
5206 warningf(statement->statement.source_position,
5207 "function returns address of local variable");
5211 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5212 warningf(statement->statement.source_position,
5213 "'return' without value, in function returning non-void");
5216 statement->return_value = return_value;
5218 return (statement_t*) statement;
5222 * Parse a declaration statement.
5224 static statement_t *parse_declaration_statement(void)
5226 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5228 statement->base.source_position = token.source_position;
5230 declaration_t *before = last_declaration;
5231 parse_declaration(record_declaration);
5233 if(before == NULL) {
5234 statement->declaration.declarations_begin = context->declarations;
5236 statement->declaration.declarations_begin = before->next;
5238 statement->declaration.declarations_end = last_declaration;
5244 * Parse an expression statement, ie. expr ';'.
5246 static statement_t *parse_expression_statement(void)
5248 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5250 statement->base.source_position = token.source_position;
5251 statement->expression.expression = parse_expression();
5259 * Parse a statement.
5261 static statement_t *parse_statement(void)
5263 statement_t *statement = NULL;
5265 /* declaration or statement */
5266 switch(token.type) {
5268 statement = parse_asm_statement();
5272 statement = parse_case_statement();
5276 statement = parse_default_statement();
5280 statement = parse_compound_statement();
5284 statement = parse_if();
5288 statement = parse_switch();
5292 statement = parse_while();
5296 statement = parse_do();
5300 statement = parse_for();
5304 statement = parse_goto();
5308 statement = parse_continue();
5312 statement = parse_break();
5316 statement = parse_return();
5325 if(look_ahead(1)->type == ':') {
5326 statement = parse_label_statement();
5330 if(is_typedef_symbol(token.v.symbol)) {
5331 statement = parse_declaration_statement();
5335 statement = parse_expression_statement();
5338 case T___extension__:
5339 /* this can be a prefix to a declaration or an expression statement */
5340 /* we simply eat it now and parse the rest with tail recursion */
5343 } while(token.type == T___extension__);
5344 statement = parse_statement();
5348 statement = parse_declaration_statement();
5352 statement = parse_expression_statement();
5356 assert(statement == NULL
5357 || statement->base.source_position.input_name != NULL);
5363 * Parse a compound statement.
5365 static statement_t *parse_compound_statement(void)
5367 compound_statement_t *compound_statement
5368 = allocate_ast_zero(sizeof(compound_statement[0]));
5369 compound_statement->statement.kind = STATEMENT_COMPOUND;
5370 compound_statement->statement.source_position = token.source_position;
5374 int top = environment_top();
5375 context_t *last_context = context;
5376 set_context(&compound_statement->context);
5378 statement_t *last_statement = NULL;
5380 while(token.type != '}' && token.type != T_EOF) {
5381 statement_t *statement = parse_statement();
5382 if(statement == NULL)
5385 if(last_statement != NULL) {
5386 last_statement->base.next = statement;
5388 compound_statement->statements = statement;
5391 while(statement->base.next != NULL)
5392 statement = statement->base.next;
5394 last_statement = statement;
5397 if(token.type == '}') {
5400 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5403 assert(context == &compound_statement->context);
5404 set_context(last_context);
5405 environment_pop_to(top);
5407 return (statement_t*) compound_statement;
5411 * Initialize builtin types.
5413 static void initialize_builtin_types(void)
5415 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5416 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5417 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5418 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5419 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5420 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5421 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5422 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5424 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5425 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5426 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5427 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5431 * Parse a translation unit.
5433 static translation_unit_t *parse_translation_unit(void)
5435 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5437 assert(global_context == NULL);
5438 global_context = &unit->context;
5440 assert(context == NULL);
5441 set_context(&unit->context);
5443 initialize_builtin_types();
5445 while(token.type != T_EOF) {
5446 if (token.type == ';') {
5447 /* TODO error in strict mode */
5448 warningf(HERE, "stray ';' outside of function");
5451 parse_external_declaration();
5455 assert(context == &unit->context);
5457 last_declaration = NULL;
5459 assert(global_context == &unit->context);
5460 global_context = NULL;
5468 * @return the translation unit or NULL if errors occurred.
5470 translation_unit_t *parse(void)
5472 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5473 label_stack = NEW_ARR_F(stack_entry_t, 0);
5474 diagnostic_count = 0;
5478 type_set_output(stderr);
5479 ast_set_output(stderr);
5481 lookahead_bufpos = 0;
5482 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5485 translation_unit_t *unit = parse_translation_unit();
5487 DEL_ARR_F(environment_stack);
5488 DEL_ARR_F(label_stack);
5497 * Initialize the parser.
5499 void init_parser(void)
5501 init_expression_parsers();
5502 obstack_init(&temp_obst);
5504 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5505 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5509 * Terminate the parser.
5511 void exit_parser(void)
5513 obstack_free(&temp_obst, NULL);