11 #include "type_hash.h"
13 #include "adt/bitfiddle.h"
14 #include "adt/error.h"
15 #include "adt/array.h"
17 //#define PRINT_TOKENS
18 //#define ABORT_ON_ERROR
19 #define MAX_LOOKAHEAD 2
23 declaration_t *old_declaration;
25 unsigned short namespace;
29 static token_t lookahead_buffer[MAX_LOOKAHEAD];
30 static int lookahead_bufpos;
31 static stack_entry_t *environment_stack = NULL;
32 static stack_entry_t *label_stack = NULL;
33 static context_t *global_context = NULL;
34 static context_t *context = NULL;
35 static declaration_t *last_declaration = NULL;
36 static declaration_t *current_function = NULL;
37 static struct obstack temp_obst;
38 static bool found_error;
40 static type_t *type_int = NULL;
41 static type_t *type_uint = NULL;
42 static type_t *type_long_double = NULL;
43 static type_t *type_double = NULL;
44 static type_t *type_float = NULL;
45 static type_t *type_const_char = NULL;
46 static type_t *type_string = NULL;
47 static type_t *type_void = NULL;
48 static type_t *type_size_t = NULL;
49 static type_t *type_ptrdiff_t = NULL;
51 static statement_t *parse_compound_statement(void);
52 static statement_t *parse_statement(void);
54 static expression_t *parse_sub_expression(unsigned precedence);
55 static expression_t *parse_expression(void);
56 static type_t *parse_typename(void);
58 #define STORAGE_CLASSES \
65 #define TYPE_QUALIFIERS \
71 #ifdef PROVIDE_COMPLEX
72 #define COMPLEX_SPECIFIERS \
75 #define COMPLEX_SPECIFIERS
78 #ifdef PROVIDE_IMAGINARY
79 #define IMAGINARY_SPECIFIERS \
82 #define IMAGINARY_SPECIFIERS
85 #define TYPE_SPECIFIERS \
103 #define DECLARATION_START \
108 #define TYPENAME_START \
112 static inline void *allocate_ast_zero(size_t size)
114 void *res = allocate_ast(size);
115 memset(res, 0, size);
119 static inline void *allocate_type_zero(size_t size)
121 void *res = obstack_alloc(type_obst, size);
122 memset(res, 0, size);
126 static inline void free_type(void *type)
128 obstack_free(type_obst, type);
132 * returns the top element of the environment stack
134 static inline size_t environment_top(void)
136 return ARR_LEN(environment_stack);
139 static inline size_t label_top(void)
141 return ARR_LEN(label_stack);
146 static inline void next_token(void)
148 token = lookahead_buffer[lookahead_bufpos];
149 lookahead_buffer[lookahead_bufpos] = lexer_token;
152 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
155 print_token(stderr, &token);
156 fprintf(stderr, "\n");
160 static inline const token_t *look_ahead(int num)
162 assert(num > 0 && num <= MAX_LOOKAHEAD);
163 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
164 return & lookahead_buffer[pos];
167 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
169 static void error(void)
172 #ifdef ABORT_ON_ERROR
177 static void parser_print_prefix_pos(const source_position_t source_position)
179 fputs(source_position.input_name, stderr);
181 fprintf(stderr, "%d", source_position.linenr);
185 static void parser_print_error_prefix_pos(
186 const source_position_t source_position)
188 parser_print_prefix_pos(source_position);
189 fputs("error: ", stderr);
193 static void parser_print_error_prefix(void)
195 parser_print_error_prefix_pos(token.source_position);
198 static void parse_error(const char *message)
200 parser_print_error_prefix();
201 fprintf(stderr, "parse error: %s\n", message);
204 static void parser_print_warning_prefix_pos(
205 const source_position_t source_position)
207 parser_print_prefix_pos(source_position);
208 fputs("warning: ", stderr);
211 static void parse_warning(const char *message)
213 parser_print_prefix_pos(token.source_position);
214 fprintf(stderr, "warning: %s\n", message);
217 static void parse_error_expected(const char *message, ...)
222 if(message != NULL) {
223 parser_print_error_prefix();
224 fprintf(stderr, "%s\n", message);
226 parser_print_error_prefix();
227 fputs("Parse error: got ", stderr);
228 print_token(stderr, &token);
229 fputs(", expected ", stderr);
231 va_start(args, message);
232 token_type_t token_type = va_arg(args, token_type_t);
233 while(token_type != 0) {
237 fprintf(stderr, ", ");
239 print_token_type(stderr, token_type);
240 token_type = va_arg(args, token_type_t);
243 fprintf(stderr, "\n");
246 static void type_error(const char *msg, const source_position_t source_position,
249 parser_print_error_prefix_pos(source_position);
250 fprintf(stderr, "%s, but found type ", msg);
256 static void type_error_incompatible(const char *msg,
257 const source_position_t source_position, type_t *type1, type_t *type2)
259 parser_print_error_prefix_pos(source_position);
260 fprintf(stderr, "%s, incompatible types: ", msg);
262 fprintf(stderr, " - ");
264 fprintf(stderr, ")\n");
268 static void eat_block(void)
270 if(token.type == '{')
273 while(token.type != '}') {
274 if(token.type == T_EOF)
276 if(token.type == '{') {
285 static void eat_statement(void)
287 while(token.type != ';') {
288 if(token.type == T_EOF)
290 if(token.type == '}')
292 if(token.type == '{') {
301 static void eat_brace(void)
303 if(token.type == '(')
306 while(token.type != ')') {
307 if(token.type == T_EOF)
309 if(token.type == ')' || token.type == ';' || token.type == '}') {
312 if(token.type == '(') {
316 if(token.type == '{') {
325 #define expect(expected) \
326 if(UNLIKELY(token.type != (expected))) { \
327 parse_error_expected(NULL, (expected), 0); \
333 #define expect_void(expected) \
334 if(UNLIKELY(token.type != (expected))) { \
335 parse_error_expected(NULL, (expected), 0); \
341 static void set_context(context_t *new_context)
343 context = new_context;
345 last_declaration = new_context->declarations;
346 if(last_declaration != NULL) {
347 while(last_declaration->next != NULL) {
348 last_declaration = last_declaration->next;
354 * called when we find a 2nd declarator for an identifier we already have a
357 static bool is_compatible_declaration (declaration_t *declaration,
358 declaration_t *previous)
360 /* TODO: not correct yet */
361 return declaration->type == previous->type;
364 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespace)
366 declaration_t *declaration = symbol->declaration;
367 for( ; declaration != NULL; declaration = declaration->symbol_next) {
368 if(declaration->namespace == namespace)
375 static const char *get_namespace_prefix(namespace_t namespace)
378 case NAMESPACE_NORMAL:
380 case NAMESPACE_UNION:
382 case NAMESPACE_STRUCT:
386 case NAMESPACE_LABEL:
389 panic("invalid namespace found");
393 * pushs an environment_entry on the environment stack and links the
394 * corresponding symbol to the new entry
396 static declaration_t *stack_push(stack_entry_t **stack_ptr,
397 declaration_t *declaration,
398 context_t *parent_context)
400 symbol_t *symbol = declaration->symbol;
401 namespace_t namespace = declaration->namespace;
403 /* a declaration should be only pushed once */
404 assert(declaration->parent_context == NULL);
405 declaration->parent_context = parent_context;
407 declaration_t *previous_declaration = get_declaration(symbol, namespace);
408 assert(declaration != previous_declaration);
409 if(previous_declaration != NULL
410 && previous_declaration->parent_context == context) {
411 if(!is_compatible_declaration(declaration, previous_declaration)) {
412 parser_print_error_prefix_pos(declaration->source_position);
413 fprintf(stderr, "definition of symbol %s%s with type ",
414 get_namespace_prefix(namespace), symbol->string);
416 print_type(declaration->type);
418 parser_print_error_prefix_pos(
419 previous_declaration->source_position);
420 fprintf(stderr, "is incompatible with previous declaration "
422 print_type(previous_declaration->type);
425 return previous_declaration;
428 /* remember old declaration */
430 entry.symbol = symbol;
431 entry.old_declaration = symbol->declaration;
432 entry.namespace = namespace;
433 ARR_APP1(*stack_ptr, entry);
435 /* replace/add declaration into declaration list of the symbol */
436 if(symbol->declaration == NULL) {
437 symbol->declaration = declaration;
439 declaration_t *iter_last = NULL;
440 declaration_t *iter = symbol->declaration;
441 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
442 /* replace an entry? */
443 if(iter->namespace == namespace) {
444 if(iter_last == NULL) {
445 symbol->declaration = declaration;
447 iter_last->symbol_next = declaration;
449 declaration->symbol_next = iter->symbol_next;
454 assert(iter_last->symbol_next == NULL);
455 iter_last->symbol_next = declaration;
462 static declaration_t *environment_push(declaration_t *declaration)
464 assert(declaration->source_position.input_name != NULL);
465 return stack_push(&environment_stack, declaration, context);
468 static declaration_t *label_push(declaration_t *declaration)
470 return stack_push(&label_stack, declaration, ¤t_function->context);
474 * pops symbols from the environment stack until @p new_top is the top element
476 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
478 stack_entry_t *stack = *stack_ptr;
479 size_t top = ARR_LEN(stack);
482 assert(new_top <= top);
486 for(i = top; i > new_top; --i) {
487 stack_entry_t *entry = & stack[i - 1];
489 declaration_t *old_declaration = entry->old_declaration;
490 symbol_t *symbol = entry->symbol;
491 namespace_t namespace = entry->namespace;
493 /* replace/remove declaration */
494 declaration_t *declaration = symbol->declaration;
495 assert(declaration != NULL);
496 if(declaration->namespace == namespace) {
497 if(old_declaration == NULL) {
498 symbol->declaration = declaration->symbol_next;
500 symbol->declaration = old_declaration;
501 assert(old_declaration->symbol_next ==
502 declaration->symbol_next);
505 for(; declaration != NULL; declaration = declaration->symbol_next) {
506 declaration_t *symbol_next = declaration->symbol_next;
507 if(symbol_next->namespace == namespace) {
508 declaration->symbol_next = old_declaration;
509 assert(old_declaration->symbol_next
510 == symbol_next->symbol_next);
514 assert(declaration != NULL);
518 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
521 static void environment_pop_to(size_t new_top)
523 stack_pop_to(&environment_stack, new_top);
526 static void label_pop_to(size_t new_top)
528 stack_pop_to(&label_stack, new_top);
532 static int get_rank(const type_t *type)
534 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
535 * and esp. footnote 108). However we can't fold constants (yet), so we
536 * can't decide wether unsigned int is possible, while int always works.
537 * (unsigned int would be preferable when possible... for stuff like
538 * struct { enum { ... } bla : 4; } ) */
539 if(type->type == TYPE_ENUM)
540 return ATOMIC_TYPE_INT;
542 assert(type->type == TYPE_ATOMIC);
543 atomic_type_t *atomic_type = (atomic_type_t*) type;
544 atomic_type_type_t atype = atomic_type->atype;
548 static type_t *promote_integer(type_t *type)
550 if(get_rank(type) < ATOMIC_TYPE_INT)
556 static expression_t *create_cast_expression(expression_t *expression,
559 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
561 cast->expression.type = EXPR_UNARY;
562 cast->type = UNEXPR_CAST;
563 cast->value = expression;
564 cast->expression.datatype = dest_type;
566 return (expression_t*) cast;
569 static expression_t *create_implicit_cast(expression_t *expression,
572 type_t *source_type = expression->datatype;
574 if(source_type == NULL)
577 source_type = skip_typeref(source_type);
578 dest_type = skip_typeref(dest_type);
580 if(source_type == dest_type)
583 if(dest_type->type == TYPE_ATOMIC) {
584 if(source_type->type != TYPE_ATOMIC)
585 panic("casting of non-atomic types not implemented yet");
587 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
588 type_error_incompatible("can't cast types",
589 expression->source_position,
590 source_type, dest_type);
594 return create_cast_expression(expression, dest_type);
596 if(dest_type->type == TYPE_POINTER) {
597 pointer_type_t *pointer_type
598 = (pointer_type_t*) dest_type;
599 if(source_type->type == TYPE_POINTER) {
600 if(!pointers_compatible(source_type, dest_type)) {
601 type_error_incompatible("can't implicitely cast types",
602 expression->source_position,
603 source_type, dest_type);
606 return create_cast_expression(expression, dest_type);
608 } else if(source_type->type == TYPE_ARRAY) {
609 array_type_t *array_type = (array_type_t*) source_type;
610 if(!types_compatible(array_type->element_type,
611 pointer_type->points_to)) {
612 type_error_incompatible("can't implicitely cast types",
613 expression->source_position,
614 source_type, dest_type);
617 return create_cast_expression(expression, dest_type);
621 panic("casting of non-atomic types not implemented yet");
624 static void semantic_assign(type_t *orig_type_left, expression_t **right,
627 type_t *orig_type_right = (*right)->datatype;
629 if(orig_type_right == NULL)
632 type_t *type_left = skip_typeref(orig_type_left);
633 type_t *type_right = skip_typeref(orig_type_right);
635 if(type_left == type_right) {
637 } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
638 *right = create_implicit_cast(*right, type_left);
639 } else if(type_left->type == TYPE_POINTER
640 && type_right->type == TYPE_POINTER) {
643 /* TODO: improve error message */
644 parser_print_error_prefix();
645 fprintf(stderr, "incompatible types in %s\n", context);
646 parser_print_error_prefix();
647 print_type(type_left);
648 fputs(" <- ", stderr);
649 print_type(type_right);
655 static expression_t *parse_constant_expression(void)
657 /* start parsing at precedence 7 (conditional expression) */
658 return parse_sub_expression(7);
661 static expression_t *parse_assignment_expression(void)
663 /* start parsing at precedence 2 (assignment expression) */
664 return parse_sub_expression(2);
667 typedef struct declaration_specifiers_t declaration_specifiers_t;
668 struct declaration_specifiers_t {
669 storage_class_t storage_class;
674 static void parse_compound_type_entries(void);
675 static declaration_t *parse_declarator(
676 const declaration_specifiers_t *specifiers, type_t *type,
677 bool may_be_abstract);
678 static declaration_t *record_declaration(declaration_t *declaration);
680 static const char *parse_string_literals(void)
682 assert(token.type == T_STRING_LITERAL);
683 const char *result = token.v.string;
687 while(token.type == T_STRING_LITERAL) {
688 result = concat_strings(result, token.v.string);
695 static void parse_attributes(void)
699 case T___attribute__:
707 parse_error("EOF while parsing attribute");
725 if(token.type != T_STRING_LITERAL) {
726 parse_error_expected("while parsing assembler attribute",
731 parse_string_literals();
736 goto attributes_finished;
744 static designator_t *parse_designation(void)
746 if(token.type != '[' && token.type != '.')
749 designator_t *result = NULL;
750 designator_t *last = NULL;
753 designator_t *designator;
756 designator = allocate_ast_zero(sizeof(designator[0]));
758 designator->array_access = parse_constant_expression();
762 designator = allocate_ast_zero(sizeof(designator[0]));
764 if(token.type != T_IDENTIFIER) {
765 parse_error_expected("problem while parsing designator",
769 designator->symbol = token.v.symbol;
777 assert(designator != NULL);
779 last->next = designator;
787 static initializer_t *parse_initializer_list(type_t *type);
789 static initializer_t *parse_initializer(type_t *type)
791 designator_t *designator = parse_designation();
793 initializer_t *result;
794 if(token.type == '{') {
795 result = parse_initializer_list(type);
797 result = allocate_ast_zero(sizeof(result[0]));
798 result->type = INITIALIZER_VALUE;
799 result->v.value = parse_assignment_expression();
802 semantic_assign(type, &result->v.value, "initializer");
805 result->designator = designator;
810 static initializer_t *parse_initializer_list(type_t *type)
817 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
818 result->type = INITIALIZER_LIST;
820 initializer_t *last = NULL;
822 initializer_t *initializer = parse_initializer(NULL);
824 last->next = initializer;
826 result->v.list = initializer;
830 if(token.type == '}')
833 if(token.type != ',') {
834 parse_error_expected("problem while parsing initializer list",
841 if(token.type == '}')
850 static declaration_t *parse_compound_type_specifier(bool is_struct)
858 symbol_t *symbol = NULL;
859 declaration_t *declaration = NULL;
861 if(token.type == T_IDENTIFIER) {
862 symbol = token.v.symbol;
866 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
868 declaration = get_declaration(symbol, NAMESPACE_UNION);
870 } else if(token.type != '{') {
872 parse_error_expected("problem while parsing struct type specifier",
873 T_IDENTIFIER, '{', 0);
875 parse_error_expected("problem while parsing union type specifier",
876 T_IDENTIFIER, '{', 0);
882 if(declaration == NULL) {
883 declaration = allocate_type_zero(sizeof(declaration[0]));
886 declaration->namespace = NAMESPACE_STRUCT;
888 declaration->namespace = NAMESPACE_UNION;
890 declaration->source_position = token.source_position;
891 declaration->symbol = symbol;
894 if(token.type == '{') {
895 if(declaration->init.is_defined) {
896 assert(symbol != NULL);
897 parser_print_error_prefix();
898 fprintf(stderr, "multiple definition of %s %s\n",
899 is_struct ? "struct" : "union", symbol->string);
900 declaration->context.declarations = NULL;
902 record_declaration(declaration);
903 declaration->init.is_defined = true;
905 int top = environment_top();
906 context_t *last_context = context;
907 set_context(& declaration->context);
909 parse_compound_type_entries();
912 assert(context == & declaration->context);
913 set_context(last_context);
914 environment_pop_to(top);
920 static void parse_enum_entries(void)
924 if(token.type == '}') {
926 parse_error("empty enum not allowed");
931 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
933 if(token.type != T_IDENTIFIER) {
934 parse_error_expected("problem while parsing enum entry",
939 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
940 entry->symbol = token.v.symbol;
941 entry->source_position = token.source_position;
944 if(token.type == '=') {
946 entry->init.initializer = parse_initializer(type_int);
949 record_declaration(entry);
951 if(token.type != ',')
954 } while(token.type != '}');
959 static declaration_t *parse_enum_specifier(void)
963 declaration_t *declaration;
966 if(token.type == T_IDENTIFIER) {
967 symbol = token.v.symbol;
970 declaration = get_declaration(symbol, NAMESPACE_ENUM);
971 } else if(token.type != '{') {
972 parse_error_expected("problem while parsing enum type specifier",
973 T_IDENTIFIER, '{', 0);
980 if(declaration == NULL) {
981 declaration = allocate_type_zero(sizeof(declaration[0]));
983 declaration->namespace = NAMESPACE_ENUM;
984 declaration->source_position = token.source_position;
985 declaration->symbol = symbol;
988 if(token.type == '{') {
989 if(declaration->init.is_defined) {
990 parser_print_error_prefix();
991 fprintf(stderr, "multiple definitions of enum %s\n",
994 record_declaration(declaration);
995 declaration->init.is_defined = 1;
997 parse_enum_entries();
1005 * if a symbol is a typedef to another type, return true
1007 static bool is_typedef_symbol(symbol_t *symbol)
1009 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1010 if(declaration == NULL
1011 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1017 static type_t *parse_typeof(void)
1025 expression_t *expression = NULL;
1028 switch(token.type) {
1029 case T___extension__:
1030 /* this can be a prefix to a typename or an expression */
1031 /* we simply eat it now. */
1034 } while(token.type == T___extension__);
1038 if(is_typedef_symbol(token.v.symbol)) {
1039 type = parse_typename();
1041 expression = parse_expression();
1042 type = expression->datatype;
1047 type = parse_typename();
1051 expression = parse_expression();
1052 type = expression->datatype;
1058 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1059 typeof->type.type = TYPE_TYPEOF;
1060 typeof->expression = expression;
1061 typeof->typeof_type = type;
1063 return (type_t*) typeof;
1067 SPECIFIER_SIGNED = 1 << 0,
1068 SPECIFIER_UNSIGNED = 1 << 1,
1069 SPECIFIER_LONG = 1 << 2,
1070 SPECIFIER_INT = 1 << 3,
1071 SPECIFIER_DOUBLE = 1 << 4,
1072 SPECIFIER_CHAR = 1 << 5,
1073 SPECIFIER_SHORT = 1 << 6,
1074 SPECIFIER_LONG_LONG = 1 << 7,
1075 SPECIFIER_FLOAT = 1 << 8,
1076 SPECIFIER_BOOL = 1 << 9,
1077 SPECIFIER_VOID = 1 << 10,
1078 #ifdef PROVIDE_COMPLEX
1079 SPECIFIER_COMPLEX = 1 << 11,
1081 #ifdef PROVIDE_IMAGINARY
1082 SPECIFIER_IMAGINARY = 1 << 12,
1086 static type_t *create_builtin_type(symbol_t *symbol)
1088 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1089 type->type.type = TYPE_BUILTIN;
1090 type->symbol = symbol;
1092 type->real_type = type_int;
1094 return (type_t*) type;
1097 static type_t *get_typedef_type(symbol_t *symbol)
1099 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1100 if(declaration == NULL
1101 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1104 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1105 typedef_type->type.type = TYPE_TYPEDEF;
1106 typedef_type->declaration = declaration;
1108 return (type_t*) typedef_type;
1111 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1113 type_t *type = NULL;
1114 unsigned type_qualifiers = 0;
1115 unsigned type_specifiers = 0;
1119 switch(token.type) {
1122 #define MATCH_STORAGE_CLASS(token, class) \
1124 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1125 parse_error("multiple storage classes in declaration " \
1128 specifiers->storage_class = class; \
1132 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1133 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1134 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1135 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1136 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1138 /* type qualifiers */
1139 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1141 type_qualifiers |= qualifier; \
1145 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1146 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1147 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1149 case T___extension__:
1154 /* type specifiers */
1155 #define MATCH_SPECIFIER(token, specifier, name) \
1158 if(type_specifiers & specifier) { \
1159 parse_error("multiple " name " type specifiers given"); \
1161 type_specifiers |= specifier; \
1165 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1166 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1167 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1168 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1169 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1170 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1171 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1172 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1173 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1174 #ifdef PROVIDE_COMPLEX
1175 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1177 #ifdef PROVIDE_IMAGINARY
1178 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1182 specifiers->is_inline = true;
1187 if(type_specifiers & SPECIFIER_LONG_LONG) {
1188 parse_error("multiple type specifiers given");
1189 } else if(type_specifiers & SPECIFIER_LONG) {
1190 type_specifiers |= SPECIFIER_LONG_LONG;
1192 type_specifiers |= SPECIFIER_LONG;
1196 /* TODO: if type != NULL for the following rules should issue
1199 compound_type_t *compound_type
1200 = allocate_type_zero(sizeof(compound_type[0]));
1201 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1202 compound_type->declaration = parse_compound_type_specifier(true);
1204 type = (type_t*) compound_type;
1208 compound_type_t *compound_type
1209 = allocate_type_zero(sizeof(compound_type[0]));
1210 compound_type->type.type = TYPE_COMPOUND_UNION;
1211 compound_type->declaration = parse_compound_type_specifier(false);
1213 type = (type_t*) compound_type;
1217 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1218 enum_type->type.type = TYPE_ENUM;
1219 enum_type->declaration = parse_enum_specifier();
1221 type = (type_t*) enum_type;
1225 type = parse_typeof();
1227 case T___builtin_va_list:
1228 type = create_builtin_type(token.v.symbol);
1232 case T___attribute__:
1237 case T_IDENTIFIER: {
1238 type_t *typedef_type = get_typedef_type(token.v.symbol);
1240 if(typedef_type == NULL)
1241 goto finish_specifiers;
1244 type = typedef_type;
1248 /* function specifier */
1250 goto finish_specifiers;
1257 atomic_type_type_t atomic_type;
1259 /* match valid basic types */
1260 switch(type_specifiers) {
1261 case SPECIFIER_VOID:
1262 atomic_type = ATOMIC_TYPE_VOID;
1264 case SPECIFIER_CHAR:
1265 atomic_type = ATOMIC_TYPE_CHAR;
1267 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1268 atomic_type = ATOMIC_TYPE_SCHAR;
1270 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1271 atomic_type = ATOMIC_TYPE_UCHAR;
1273 case SPECIFIER_SHORT:
1274 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1275 case SPECIFIER_SHORT | SPECIFIER_INT:
1276 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1277 atomic_type = ATOMIC_TYPE_SHORT;
1279 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1280 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1281 atomic_type = ATOMIC_TYPE_USHORT;
1284 case SPECIFIER_SIGNED:
1285 case SPECIFIER_SIGNED | SPECIFIER_INT:
1286 atomic_type = ATOMIC_TYPE_INT;
1288 case SPECIFIER_UNSIGNED:
1289 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1290 atomic_type = ATOMIC_TYPE_UINT;
1292 case SPECIFIER_LONG:
1293 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1294 case SPECIFIER_LONG | SPECIFIER_INT:
1295 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1296 atomic_type = ATOMIC_TYPE_LONG;
1298 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1299 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1300 atomic_type = ATOMIC_TYPE_ULONG;
1302 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1303 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1304 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1305 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1307 atomic_type = ATOMIC_TYPE_LONGLONG;
1309 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1310 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1312 atomic_type = ATOMIC_TYPE_ULONGLONG;
1314 case SPECIFIER_FLOAT:
1315 atomic_type = ATOMIC_TYPE_FLOAT;
1317 case SPECIFIER_DOUBLE:
1318 atomic_type = ATOMIC_TYPE_DOUBLE;
1320 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1321 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1323 case SPECIFIER_BOOL:
1324 atomic_type = ATOMIC_TYPE_BOOL;
1326 #ifdef PROVIDE_COMPLEX
1327 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1328 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1330 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1331 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1333 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1334 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1337 #ifdef PROVIDE_IMAGINARY
1338 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1339 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1341 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1342 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1344 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1345 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1349 /* invalid specifier combination, give an error message */
1350 if(type_specifiers == 0) {
1352 parse_warning("no type specifiers in declaration (using int)");
1353 atomic_type = ATOMIC_TYPE_INT;
1356 parse_error("no type specifiers given in declaration");
1358 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1359 (type_specifiers & SPECIFIER_UNSIGNED)) {
1360 parse_error("signed and unsigned specifiers gives");
1361 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1362 parse_error("only integer types can be signed or unsigned");
1364 parse_error("multiple datatypes in declaration");
1366 atomic_type = ATOMIC_TYPE_INVALID;
1369 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1370 atype->type.type = TYPE_ATOMIC;
1371 atype->atype = atomic_type;
1374 type = (type_t*) atype;
1376 if(type_specifiers != 0) {
1377 parse_error("multiple datatypes in declaration");
1381 type->qualifiers = type_qualifiers;
1383 type_t *result = typehash_insert(type);
1384 if(newtype && result != (type_t*) type) {
1388 specifiers->type = result;
1391 static type_qualifier_t parse_type_qualifiers(void)
1393 type_qualifier_t type_qualifiers = 0;
1396 switch(token.type) {
1397 /* type qualifiers */
1398 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1399 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1400 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1403 return type_qualifiers;
1408 static void parse_identifier_list(void)
1411 if(token.type != T_IDENTIFIER) {
1412 parse_error_expected("problem while parsing parameter identifier "
1413 "list", T_IDENTIFIER, 0);
1417 if(token.type != ',')
1423 static declaration_t *parse_parameter(void)
1425 declaration_specifiers_t specifiers;
1426 memset(&specifiers, 0, sizeof(specifiers));
1428 parse_declaration_specifiers(&specifiers);
1430 declaration_t *declaration
1431 = parse_declarator(&specifiers, specifiers.type, true);
1433 /* TODO check declaration constraints for parameters */
1434 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1435 parse_error("typedef not allowed in parameter list");
1441 static declaration_t *parse_parameters(function_type_t *type)
1443 if(token.type == T_IDENTIFIER) {
1444 symbol_t *symbol = token.v.symbol;
1445 if(!is_typedef_symbol(symbol)) {
1447 parse_identifier_list();
1452 if(token.type == ')') {
1453 type->unspecified_parameters = 1;
1456 if(token.type == T_void && look_ahead(1)->type == ')') {
1461 declaration_t *declarations = NULL;
1462 declaration_t *declaration;
1463 declaration_t *last_declaration = NULL;
1464 function_parameter_t *parameter;
1465 function_parameter_t *last_parameter = NULL;
1468 switch(token.type) {
1472 return declarations;
1475 case T___extension__:
1477 declaration = parse_parameter();
1479 parameter = allocate_type_zero(sizeof(parameter[0]));
1480 parameter->type = declaration->type;
1482 if(last_parameter != NULL) {
1483 last_declaration->next = declaration;
1484 last_parameter->next = parameter;
1486 type->parameters = parameter;
1487 declarations = declaration;
1489 last_parameter = parameter;
1490 last_declaration = declaration;
1494 return declarations;
1496 if(token.type != ',')
1497 return declarations;
1507 } construct_type_type_t;
1509 typedef struct construct_type_t construct_type_t;
1510 struct construct_type_t {
1511 construct_type_type_t type;
1512 construct_type_t *next;
1515 typedef struct parsed_pointer_t parsed_pointer_t;
1516 struct parsed_pointer_t {
1517 construct_type_t construct_type;
1518 type_qualifier_t type_qualifiers;
1521 typedef struct construct_function_type_t construct_function_type_t;
1522 struct construct_function_type_t {
1523 construct_type_t construct_type;
1524 function_type_t *function_type;
1527 typedef struct parsed_array_t parsed_array_t;
1528 struct parsed_array_t {
1529 construct_type_t construct_type;
1530 type_qualifier_t type_qualifiers;
1536 typedef struct construct_base_type_t construct_base_type_t;
1537 struct construct_base_type_t {
1538 construct_type_t construct_type;
1542 static construct_type_t *parse_pointer_declarator(void)
1546 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1547 memset(pointer, 0, sizeof(pointer[0]));
1548 pointer->construct_type.type = CONSTRUCT_POINTER;
1549 pointer->type_qualifiers = parse_type_qualifiers();
1551 return (construct_type_t*) pointer;
1554 static construct_type_t *parse_array_declarator(void)
1558 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1559 memset(array, 0, sizeof(array[0]));
1560 array->construct_type.type = CONSTRUCT_ARRAY;
1562 if(token.type == T_static) {
1563 array->is_static = true;
1567 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1568 if(type_qualifiers != 0) {
1569 if(token.type == T_static) {
1570 array->is_static = true;
1574 array->type_qualifiers = type_qualifiers;
1576 if(token.type == '*' && look_ahead(1)->type == ']') {
1577 array->is_variable = true;
1579 } else if(token.type != ']') {
1580 array->size = parse_assignment_expression();
1585 return (construct_type_t*) array;
1588 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1592 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1593 type->type.type = TYPE_FUNCTION;
1595 declaration_t *parameters = parse_parameters(type);
1596 if(declaration != NULL) {
1597 declaration->context.declarations = parameters;
1600 construct_function_type_t *construct_function_type =
1601 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1602 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1603 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1604 construct_function_type->function_type = type;
1608 return (construct_type_t*) construct_function_type;
1611 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1612 int may_be_abstract)
1614 construct_type_t *result = NULL;
1615 construct_type_t *last = NULL;
1617 while(token.type == '*') {
1618 construct_type_t *type = parse_pointer_declarator();
1627 /* TODO: find out if this is correct */
1630 construct_type_t *inner_types = NULL;
1632 switch(token.type) {
1634 if(declaration == NULL) {
1635 parse_error("no identifier expected in typename");
1637 declaration->symbol = token.v.symbol;
1638 declaration->source_position = token.source_position;
1644 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1650 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1655 construct_type_t *type;
1656 switch(token.type) {
1658 type = parse_function_declarator(declaration);
1661 type = parse_array_declarator();
1664 goto declarator_finished;
1675 declarator_finished:
1678 if(inner_types != NULL) {
1680 last->next = inner_types;
1682 result = inner_types;
1690 static type_t *construct_declarator_type(construct_type_t *construct_list,
1693 construct_type_t *iter = construct_list;
1694 for( ; iter != NULL; iter = iter->next) {
1695 parsed_pointer_t *parsed_pointer;
1696 parsed_array_t *parsed_array;
1697 construct_function_type_t *construct_function_type;
1698 function_type_t *function_type;
1699 pointer_type_t *pointer_type;
1700 array_type_t *array_type;
1702 switch(iter->type) {
1703 case CONSTRUCT_INVALID:
1704 panic("invalid type construction found");
1705 case CONSTRUCT_FUNCTION:
1706 construct_function_type = (construct_function_type_t*) iter;
1707 function_type = construct_function_type->function_type;
1709 function_type->result_type = type;
1710 type = (type_t*) function_type;
1713 case CONSTRUCT_POINTER:
1714 parsed_pointer = (parsed_pointer_t*) iter;
1715 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1717 pointer_type->type.type = TYPE_POINTER;
1718 pointer_type->points_to = type;
1719 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1720 type = (type_t*) pointer_type;
1723 case CONSTRUCT_ARRAY:
1724 parsed_array = (parsed_array_t*) iter;
1725 array_type = allocate_type_zero(sizeof(array_type[0]));
1727 array_type->type.type = TYPE_ARRAY;
1728 array_type->element_type = type;
1729 array_type->type.qualifiers = parsed_array->type_qualifiers;
1730 array_type->is_static = parsed_array->is_static;
1731 array_type->is_variable = parsed_array->is_variable;
1732 array_type->size = parsed_array->size;
1733 type = (type_t*) array_type;
1737 type_t *hashed_type = typehash_insert((type_t*) type);
1738 if(hashed_type != type) {
1747 static declaration_t *parse_declarator(
1748 const declaration_specifiers_t *specifiers,
1749 type_t *type, bool may_be_abstract)
1751 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1752 declaration->storage_class = specifiers->storage_class;
1753 declaration->is_inline = specifiers->is_inline;
1755 construct_type_t *construct_type
1756 = parse_inner_declarator(declaration, may_be_abstract);
1757 declaration->type = construct_declarator_type(construct_type, type);
1759 if(construct_type != NULL) {
1760 obstack_free(&temp_obst, construct_type);
1766 static type_t *parse_abstract_declarator(type_t *base_type)
1768 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1770 type_t *result = construct_declarator_type(construct_type, base_type);
1771 if(construct_type != NULL) {
1772 obstack_free(&temp_obst, construct_type);
1778 static declaration_t *record_declaration(declaration_t *declaration)
1780 assert(context != NULL);
1782 symbol_t *symbol = declaration->symbol;
1783 if(symbol != NULL) {
1784 declaration_t *alias = environment_push(declaration);
1785 if(alias != declaration)
1788 declaration->parent_context = context;
1791 if(last_declaration != NULL) {
1792 last_declaration->next = declaration;
1794 context->declarations = declaration;
1796 last_declaration = declaration;
1801 static void parser_error_multiple_definition(declaration_t *previous,
1802 declaration_t *declaration)
1804 parser_print_error_prefix_pos(declaration->source_position);
1805 fprintf(stderr, "multiple definition of symbol '%s'\n",
1806 declaration->symbol->string);
1807 parser_print_error_prefix_pos(previous->source_position);
1808 fprintf(stderr, "this is the location of the previous "
1813 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1816 declaration_t *ndeclaration
1817 = parse_declarator(specifiers, specifiers->type, false);
1819 declaration_t *declaration = record_declaration(ndeclaration);
1821 type_t *type = declaration->type;
1822 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
1823 parser_print_warning_prefix_pos(declaration->source_position);
1824 fprintf(stderr, "variable ‘%s’ declared ‘inline’\n",
1825 declaration->symbol->string);
1828 if(token.type == '=') {
1831 /* TODO: check that this is an allowed type (no function type) */
1833 if(declaration->init.initializer != NULL) {
1834 parser_error_multiple_definition(declaration, ndeclaration);
1837 ndeclaration->init.initializer = parse_initializer(declaration->type);
1838 } else if(token.type == '{') {
1839 if(declaration->type->type != TYPE_FUNCTION) {
1840 parser_print_error_prefix();
1841 fprintf(stderr, "Declarator ");
1842 print_type_ext(declaration->type, declaration->symbol, NULL);
1843 fprintf(stderr, " has a body but is not a function type.\n");
1848 if(declaration->init.statement != NULL) {
1849 parser_error_multiple_definition(declaration, ndeclaration);
1851 if(ndeclaration != declaration) {
1852 memcpy(&declaration->context, &ndeclaration->context,
1853 sizeof(declaration->context));
1856 int top = environment_top();
1857 context_t *last_context = context;
1858 set_context(&declaration->context);
1860 /* push function parameters */
1861 declaration_t *parameter = declaration->context.declarations;
1862 for( ; parameter != NULL; parameter = parameter->next) {
1863 environment_push(parameter);
1866 int label_stack_top = label_top();
1867 declaration_t *old_current_function = current_function;
1868 current_function = declaration;
1870 statement_t *statement = parse_compound_statement();
1872 assert(current_function == declaration);
1873 current_function = old_current_function;
1874 label_pop_to(label_stack_top);
1876 assert(context == &declaration->context);
1877 set_context(last_context);
1878 environment_pop_to(top);
1880 declaration->init.statement = statement;
1884 if(token.type != ',')
1891 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1894 if(token.type == ':') {
1896 parse_constant_expression();
1897 /* TODO (bitfields) */
1899 declaration_t *declaration
1900 = parse_declarator(specifiers, specifiers->type, true);
1902 /* TODO: check constraints for struct declarations */
1903 /* TODO: check for doubled fields */
1904 record_declaration(declaration);
1906 if(token.type == ':') {
1908 parse_constant_expression();
1909 /* TODO (bitfields) */
1913 if(token.type != ',')
1920 static void parse_compound_type_entries(void)
1924 while(token.type != '}' && token.type != T_EOF) {
1925 declaration_specifiers_t specifiers;
1926 memset(&specifiers, 0, sizeof(specifiers));
1927 parse_declaration_specifiers(&specifiers);
1929 parse_struct_declarators(&specifiers);
1931 if(token.type == T_EOF) {
1932 parse_error("unexpected error while parsing struct");
1937 static void parse_declaration(void)
1939 source_position_t source_position = token.source_position;
1941 declaration_specifiers_t specifiers;
1942 memset(&specifiers, 0, sizeof(specifiers));
1943 parse_declaration_specifiers(&specifiers);
1945 if(token.type == ';') {
1948 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1950 declaration->type = specifiers.type;
1951 declaration->storage_class = specifiers.storage_class;
1952 declaration->source_position = source_position;
1953 record_declaration(declaration);
1956 parse_init_declarators(&specifiers);
1959 static type_t *parse_typename(void)
1961 declaration_specifiers_t specifiers;
1962 memset(&specifiers, 0, sizeof(specifiers));
1963 parse_declaration_specifiers(&specifiers);
1964 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1965 /* TODO: improve error message, user does probably not know what a
1966 * storage class is...
1968 parse_error("typename may not have a storage class");
1971 type_t *result = parse_abstract_declarator(specifiers.type);
1979 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1980 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1981 expression_t *left);
1983 typedef struct expression_parser_function_t expression_parser_function_t;
1984 struct expression_parser_function_t {
1985 unsigned precedence;
1986 parse_expression_function parser;
1987 unsigned infix_precedence;
1988 parse_expression_infix_function infix_parser;
1991 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1993 static expression_t *expected_expression_error(void)
1995 parser_print_error_prefix();
1996 fprintf(stderr, "expected expression, got token ");
1997 print_token(stderr, & token);
1998 fprintf(stderr, "\n");
2000 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2001 expression->type = EXPR_INVALID;
2007 static expression_t *parse_string_const(void)
2009 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2011 cnst->expression.type = EXPR_STRING_LITERAL;
2012 cnst->expression.datatype = type_string;
2013 cnst->value = parse_string_literals();
2015 return (expression_t*) cnst;
2018 static expression_t *parse_int_const(void)
2020 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2022 cnst->expression.type = EXPR_CONST;
2023 cnst->expression.datatype = type_int;
2024 cnst->v.int_value = token.v.intvalue;
2028 return (expression_t*) cnst;
2031 static expression_t *parse_float_const(void)
2033 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2035 cnst->expression.type = EXPR_CONST;
2036 cnst->expression.datatype = type_double;
2037 cnst->v.float_value = token.v.floatvalue;
2041 return (expression_t*) cnst;
2044 static declaration_t *create_implicit_function(symbol_t *symbol,
2045 const source_position_t source_position)
2047 function_type_t *function_type
2048 = allocate_type_zero(sizeof(function_type[0]));
2050 function_type->type.type = TYPE_FUNCTION;
2051 function_type->result_type = type_int;
2052 function_type->unspecified_parameters = true;
2054 type_t *type = typehash_insert((type_t*) function_type);
2055 if(type != (type_t*) function_type) {
2056 free_type(function_type);
2059 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2061 declaration->storage_class = STORAGE_CLASS_EXTERN;
2062 declaration->type = type;
2063 declaration->symbol = symbol;
2064 declaration->source_position = source_position;
2066 /* prepend the implicit definition to the global context
2067 * this is safe since the symbol wasn't declared as anything else yet
2069 assert(symbol->declaration == NULL);
2071 context_t *last_context = context;
2072 context = global_context;
2074 environment_push(declaration);
2075 declaration->next = context->declarations;
2076 context->declarations = declaration;
2078 context = last_context;
2083 static expression_t *parse_reference(void)
2085 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2087 ref->expression.type = EXPR_REFERENCE;
2088 ref->symbol = token.v.symbol;
2090 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2092 source_position_t source_position = token.source_position;
2095 if(declaration == NULL) {
2097 /* an implicitely defined function */
2098 if(token.type == '(') {
2099 parser_print_prefix_pos(token.source_position);
2100 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2101 ref->symbol->string);
2103 declaration = create_implicit_function(ref->symbol,
2108 parser_print_error_prefix();
2109 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2110 return (expression_t*) ref;
2114 ref->declaration = declaration;
2115 ref->expression.datatype = declaration->type;
2117 return (expression_t*) ref;
2120 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2124 /* TODO check if explicit cast is allowed and issue warnings/errors */
2127 static expression_t *parse_cast(void)
2129 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2131 cast->expression.type = EXPR_UNARY;
2132 cast->type = UNEXPR_CAST;
2133 cast->expression.source_position = token.source_position;
2135 type_t *type = parse_typename();
2138 expression_t *value = parse_sub_expression(20);
2140 check_cast_allowed(value, type);
2142 cast->expression.datatype = type;
2143 cast->value = value;
2145 return (expression_t*) cast;
2148 static expression_t *parse_statement_expression(void)
2150 statement_expression_t *expression
2151 = allocate_ast_zero(sizeof(expression[0]));
2152 expression->expression.type = EXPR_STATEMENT;
2154 statement_t *statement = parse_compound_statement();
2155 expression->statement = statement;
2156 if(statement == NULL) {
2161 assert(statement->type == STATEMENT_COMPOUND);
2162 compound_statement_t *compound_statement
2163 = (compound_statement_t*) statement;
2165 /* find last statement and use it's type */
2166 const statement_t *last_statement = NULL;
2167 const statement_t *iter = compound_statement->statements;
2168 for( ; iter != NULL; iter = iter->next) {
2169 last_statement = iter;
2172 if(last_statement->type == STATEMENT_EXPRESSION) {
2173 const expression_statement_t *expression_statement =
2174 (const expression_statement_t*) last_statement;
2175 expression->expression.datatype
2176 = expression_statement->expression->datatype;
2178 expression->expression.datatype = type_void;
2183 return (expression_t*) expression;
2186 static expression_t *parse_brace_expression(void)
2190 switch(token.type) {
2192 /* gcc extension: a stement expression */
2193 return parse_statement_expression();
2197 return parse_cast();
2199 if(is_typedef_symbol(token.v.symbol)) {
2200 return parse_cast();
2204 expression_t *result = parse_expression();
2210 static expression_t *parse_function_keyword(void)
2212 eat(T___FUNCTION__);
2215 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2216 expression->expression.type = EXPR_FUNCTION;
2217 expression->expression.datatype = type_string;
2218 expression->value = "TODO: FUNCTION";
2220 return (expression_t*) expression;
2223 static expression_t *parse_pretty_function_keyword(void)
2225 eat(T___PRETTY_FUNCTION__);
2228 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2229 expression->expression.type = EXPR_PRETTY_FUNCTION;
2230 expression->expression.datatype = type_string;
2231 expression->value = "TODO: PRETTY FUNCTION";
2233 return (expression_t*) expression;
2236 static designator_t *parse_designator(void)
2238 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2240 if(token.type != T_IDENTIFIER) {
2241 parse_error_expected("problem while parsing member designator",
2246 result->symbol = token.v.symbol;
2249 designator_t *last_designator = result;
2251 if(token.type == '.') {
2253 if(token.type != T_IDENTIFIER) {
2254 parse_error_expected("problem while parsing member designator",
2259 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2260 designator->symbol = token.v.symbol;
2263 last_designator->next = designator;
2264 last_designator = designator;
2267 if(token.type == '[') {
2269 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2270 designator->array_access = parse_expression();
2271 if(designator->array_access == NULL) {
2277 last_designator->next = designator;
2278 last_designator = designator;
2287 static expression_t *parse_offsetof(void)
2289 eat(T___builtin_offsetof);
2291 offsetof_expression_t *expression
2292 = allocate_ast_zero(sizeof(expression[0]));
2293 expression->expression.type = EXPR_OFFSETOF;
2294 expression->expression.datatype = type_size_t;
2297 expression->type = parse_typename();
2299 expression->designator = parse_designator();
2302 return (expression_t*) expression;
2305 static expression_t *parse_va_arg(void)
2307 eat(T___builtin_va_arg);
2309 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2310 expression->expression.type = EXPR_VA_ARG;
2313 expression->arg = parse_assignment_expression();
2315 expression->expression.datatype = parse_typename();
2318 return (expression_t*) expression;
2321 static expression_t *parse_builtin_symbol(void)
2323 builtin_symbol_expression_t *expression
2324 = allocate_ast_zero(sizeof(expression[0]));
2325 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2327 /* TODO: set datatype */
2329 expression->symbol = token.v.symbol;
2333 return (expression_t*) expression;
2336 static expression_t *parse_primary_expression(void)
2338 switch(token.type) {
2340 return parse_int_const();
2341 case T_FLOATINGPOINT:
2342 return parse_float_const();
2343 case T_STRING_LITERAL:
2344 return parse_string_const();
2346 return parse_reference();
2347 case T___FUNCTION__:
2348 return parse_function_keyword();
2349 case T___PRETTY_FUNCTION__:
2350 return parse_pretty_function_keyword();
2351 case T___builtin_offsetof:
2352 return parse_offsetof();
2353 case T___builtin_va_arg:
2354 return parse_va_arg();
2355 case T___builtin_expect:
2356 case T___builtin_va_start:
2357 case T___builtin_va_end:
2358 return parse_builtin_symbol();
2361 return parse_brace_expression();
2364 parser_print_error_prefix();
2365 fprintf(stderr, "unexpected token ");
2366 print_token(stderr, &token);
2367 fprintf(stderr, "\n");
2370 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2371 expression->type = EXPR_INVALID;
2372 expression->datatype = type_void;
2377 static expression_t *parse_array_expression(unsigned precedence,
2378 expression_t *array_ref)
2384 array_access_expression_t *array_access
2385 = allocate_ast_zero(sizeof(array_access[0]));
2387 array_access->expression.type = EXPR_ARRAY_ACCESS;
2388 array_access->array_ref = array_ref;
2389 array_access->index = parse_expression();
2391 type_t *type = array_ref->datatype;
2393 if(type->type == TYPE_POINTER) {
2394 pointer_type_t *pointer = (pointer_type_t*) type;
2395 array_access->expression.datatype = pointer->points_to;
2396 } else if(type->type == TYPE_ARRAY) {
2397 array_type_t *array_type = (array_type_t*) type;
2398 array_access->expression.datatype = array_type->element_type;
2400 parser_print_error_prefix();
2401 fprintf(stderr, "array access on object with non-pointer type ");
2403 fprintf(stderr, "\n");
2407 if(token.type != ']') {
2408 parse_error_expected("Problem while parsing array access", ']', 0);
2409 return (expression_t*) array_access;
2413 return (expression_t*) array_access;
2416 static bool is_declaration_specifier(const token_t *token,
2417 bool only_type_specifiers)
2419 switch(token->type) {
2423 return is_typedef_symbol(token->v.symbol);
2426 if(only_type_specifiers)
2435 static expression_t *parse_sizeof(unsigned precedence)
2439 sizeof_expression_t *sizeof_expression
2440 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2441 sizeof_expression->expression.type = EXPR_SIZEOF;
2442 sizeof_expression->expression.datatype = type_size_t;
2444 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2446 sizeof_expression->type = parse_typename();
2449 expression_t *expression = parse_sub_expression(precedence);
2450 sizeof_expression->type = expression->datatype;
2451 sizeof_expression->size_expression = expression;
2454 return (expression_t*) sizeof_expression;
2457 static expression_t *parse_select_expression(unsigned precedence,
2458 expression_t *compound)
2462 assert(token.type == '.' || token.type == T_MINUSGREATER);
2465 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2467 select->expression.type = EXPR_SELECT;
2468 select->compound = compound;
2470 /* TODO: datatype */
2472 if(token.type != T_IDENTIFIER) {
2473 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2474 return (expression_t*) select;
2476 select->symbol = token.v.symbol;
2479 return (expression_t*) select;
2482 static expression_t *parse_call_expression(unsigned precedence,
2483 expression_t *expression)
2486 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2487 call->expression.type = EXPR_CALL;
2488 call->function = expression;
2490 function_type_t *function_type;
2491 type_t *type = expression->datatype;
2492 if(type->type != TYPE_FUNCTION) {
2493 /* TODO calling pointers to functions is ok */
2494 parser_print_error_prefix();
2495 fputs("called object '", stderr);
2496 print_expression(expression);
2497 fputs("' (type ", stderr);
2499 fputs("is not a function\n", stderr);
2501 function_type = NULL;
2502 call->expression.datatype = NULL;
2504 function_type = (function_type_t*) type;
2505 call->expression.datatype = function_type->result_type;
2508 /* parse arguments */
2511 if(token.type != ')') {
2512 call_argument_t *last_argument = NULL;
2515 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2517 argument->expression = parse_assignment_expression();
2518 if(last_argument == NULL) {
2519 call->arguments = argument;
2521 last_argument->next = argument;
2523 last_argument = argument;
2525 if(token.type != ',')
2532 if(function_type != NULL) {
2533 function_parameter_t *parameter = function_type->parameters;
2534 call_argument_t *argument = call->arguments;
2535 for( ; parameter != NULL && argument != NULL;
2536 parameter = parameter->next, argument = argument->next) {
2537 type_t *expected_type = parameter->type;
2538 /* TODO report context in error messages */
2539 argument->expression = create_implicit_cast(argument->expression,
2542 /* too few parameters */
2543 if(parameter != NULL) {
2544 parser_print_error_prefix();
2545 fprintf(stderr, "too few arguments to function '");
2546 print_expression(expression);
2547 fprintf(stderr, "'\n");
2548 } else if(argument != NULL) {
2549 /* too many parameters */
2550 if(!function_type->variadic
2551 && !function_type->unspecified_parameters) {
2552 parser_print_error_prefix();
2553 fprintf(stderr, "too many arguments to function '");
2554 print_expression(expression);
2555 fprintf(stderr, "'\n");
2557 /* do default promotion */
2558 for( ; argument != NULL; argument = argument->next) {
2559 type_t *type = argument->expression->datatype;
2564 if(is_type_integer(type)) {
2565 type = promote_integer(type);
2566 } else if(type == type_float) {
2569 argument->expression
2570 = create_implicit_cast(argument->expression, type);
2576 return (expression_t*) call;
2579 static type_t *get_type_after_conversion(const type_t *type1,
2580 const type_t *type2)
2584 return (type_t*) type1;
2587 static expression_t *parse_conditional_expression(unsigned precedence,
2588 expression_t *expression)
2592 conditional_expression_t *conditional
2593 = allocate_ast_zero(sizeof(conditional[0]));
2594 conditional->expression.type = EXPR_CONDITIONAL;
2595 conditional->condition = expression;
2598 type_t *condition_type = conditional->condition->datatype;
2599 if(condition_type != NULL) {
2600 if(!is_type_scalar(condition_type)) {
2601 type_error("expected a scalar type", expression->source_position,
2606 conditional->true_expression = parse_expression();
2608 conditional->false_expression = parse_sub_expression(precedence);
2610 type_t *true_type = conditional->true_expression->datatype;
2611 if(true_type == NULL)
2612 return (expression_t*) conditional;
2613 type_t *false_type = conditional->false_expression->datatype;
2614 if(false_type == NULL)
2615 return (expression_t*) conditional;
2618 if(true_type == false_type) {
2619 conditional->expression.datatype = true_type;
2620 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2621 type_t *result = get_type_after_conversion(true_type, false_type);
2622 /* TODO: create implicit convs if necessary */
2623 conditional->expression.datatype = result;
2624 } else if(true_type->type == TYPE_POINTER &&
2625 false_type->type == TYPE_POINTER &&
2626 true /* TODO compatible points_to types */) {
2628 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2629 || (is_null_ptr_const(false_type) &&
2630 true_type->type == TYPE_POINTER) TODO*/ false) {
2632 } else if(/* 1 is pointer to object type, other is void* */ false) {
2635 type_error_incompatible("problem while parsing conditional",
2636 expression->source_position, true_type,
2640 return (expression_t*) conditional;
2643 static expression_t *parse_extension(unsigned precedence)
2645 eat(T___extension__);
2647 /* TODO enable extensions */
2649 return parse_sub_expression(precedence);
2652 static void semantic_incdec(unary_expression_t *expression)
2654 type_t *orig_type = expression->value->datatype;
2655 if(orig_type == NULL)
2658 type_t *type = skip_typeref(orig_type);
2659 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
2660 /* TODO: improve error message */
2661 parser_print_error_prefix();
2662 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
2666 expression->expression.datatype = orig_type;
2669 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
2671 type_t *orig_type = expression->value->datatype;
2672 if(orig_type == NULL)
2675 type_t *type = skip_typeref(orig_type);
2676 if(!is_type_arithmetic(type)) {
2677 /* TODO: improve error message */
2678 parser_print_error_prefix();
2679 fprintf(stderr, "operation needs an arithmetic type\n");
2683 expression->expression.datatype = orig_type;
2686 static void semantic_dereference(unary_expression_t *expression)
2688 type_t *orig_type = expression->value->datatype;
2689 if(orig_type == NULL)
2692 type_t *type = skip_typeref(orig_type);
2693 if(type->type != TYPE_POINTER) {
2694 /* TODO: improve error message */
2695 parser_print_error_prefix();
2696 fprintf(stderr, "operation needs a pointer type\n");
2700 pointer_type_t *pointer_type = (pointer_type_t*) type;
2701 expression->expression.datatype = pointer_type->points_to;
2704 static void semantic_take_addr(unary_expression_t *expression)
2706 type_t *orig_type = expression->value->datatype;
2707 if(orig_type == NULL)
2710 expression->expression.datatype = make_pointer_type(orig_type, 0);
2713 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
2714 static expression_t *parse_##unexpression_type(unsigned precedence) \
2718 unary_expression_t *unary_expression \
2719 = allocate_ast_zero(sizeof(unary_expression[0])); \
2720 unary_expression->expression.type = EXPR_UNARY; \
2721 unary_expression->type = unexpression_type; \
2722 unary_expression->value = parse_sub_expression(precedence); \
2724 sfunc(unary_expression); \
2726 return (expression_t*) unary_expression; \
2729 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
2730 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
2731 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_arithmetic)
2732 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
2733 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
2734 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2735 semantic_unexpr_arithmetic)
2736 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2738 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2741 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2743 static expression_t *parse_##unexpression_type(unsigned precedence, \
2744 expression_t *left) \
2746 (void) precedence; \
2749 unary_expression_t *unary_expression \
2750 = allocate_ast_zero(sizeof(unary_expression[0])); \
2751 unary_expression->expression.type = EXPR_UNARY; \
2752 unary_expression->type = unexpression_type; \
2753 unary_expression->value = left; \
2755 sfunc(unary_expression); \
2757 return (expression_t*) unary_expression; \
2760 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2762 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2765 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
2767 /* TODO: handle complex + imaginary types */
2769 /* § 6.3.1.8 Usual arithmetic conversions */
2770 if(type_left == type_long_double || type_right == type_long_double) {
2771 return type_long_double;
2772 } else if(type_left == type_double || type_right == type_double) {
2774 } else if(type_left == type_float || type_right == type_float) {
2778 type_right = promote_integer(type_right);
2779 type_left = promote_integer(type_left);
2781 if(type_left == type_right)
2784 bool signed_left = is_type_signed(type_left);
2785 bool signed_right = is_type_signed(type_right);
2786 if(get_rank(type_left) < get_rank(type_right)) {
2787 if(signed_left == signed_right || !signed_right) {
2793 if(signed_left == signed_right || !signed_left) {
2801 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2803 expression_t *left = expression->left;
2804 expression_t *right = expression->right;
2805 type_t *orig_type_left = left->datatype;
2806 type_t *orig_type_right = right->datatype;
2808 if(orig_type_left == NULL || orig_type_right == NULL)
2811 type_t *type_left = skip_typeref(orig_type_left);
2812 type_t *type_right = skip_typeref(orig_type_right);
2814 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2815 /* TODO: improve error message */
2816 parser_print_error_prefix();
2817 fprintf(stderr, "operation needs arithmetic types\n");
2821 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2822 expression->left = create_implicit_cast(left, arithmetic_type);
2823 expression->right = create_implicit_cast(right, arithmetic_type);
2824 expression->expression.datatype = arithmetic_type;
2827 static void semantic_shift_op(binary_expression_t *expression)
2829 expression_t *left = expression->left;
2830 expression_t *right = expression->right;
2831 type_t *orig_type_left = left->datatype;
2832 type_t *orig_type_right = right->datatype;
2834 if(orig_type_left == NULL || orig_type_right == NULL)
2837 type_t *type_left = skip_typeref(orig_type_left);
2838 type_t *type_right = skip_typeref(orig_type_right);
2840 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
2841 /* TODO: improve error message */
2842 parser_print_error_prefix();
2843 fprintf(stderr, "operation needs integer types\n");
2847 type_left = promote_integer(type_left);
2848 type_right = promote_integer(type_right);
2850 expression->left = create_implicit_cast(left, type_left);
2851 expression->right = create_implicit_cast(right, type_right);
2852 expression->expression.datatype = type_left;
2855 static void semantic_add(binary_expression_t *expression)
2857 expression_t *left = expression->left;
2858 expression_t *right = expression->right;
2859 type_t *orig_type_left = left->datatype;
2860 type_t *orig_type_right = right->datatype;
2862 if(orig_type_left == NULL || orig_type_right == NULL)
2865 type_t *type_left = skip_typeref(orig_type_left);
2866 type_t *type_right = skip_typeref(orig_type_right);
2869 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2870 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2871 expression->left = create_implicit_cast(left, arithmetic_type);
2872 expression->right = create_implicit_cast(right, arithmetic_type);
2873 expression->expression.datatype = arithmetic_type;
2875 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2876 expression->expression.datatype = type_left;
2877 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
2878 expression->expression.datatype = type_right;
2880 parser_print_error_prefix();
2881 fprintf(stderr, "invalid operands to binary + (");
2882 print_type(orig_type_left);
2883 fprintf(stderr, ", ");
2884 print_type(orig_type_right);
2885 fprintf(stderr, ")\n");
2889 static void semantic_sub(binary_expression_t *expression)
2891 expression_t *left = expression->left;
2892 expression_t *right = expression->right;
2893 type_t *orig_type_left = left->datatype;
2894 type_t *orig_type_right = right->datatype;
2896 if(orig_type_left == NULL || orig_type_right == NULL)
2899 type_t *type_left = skip_typeref(orig_type_left);
2900 type_t *type_right = skip_typeref(orig_type_right);
2903 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2904 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2905 expression->left = create_implicit_cast(left, arithmetic_type);
2906 expression->right = create_implicit_cast(right, arithmetic_type);
2907 expression->expression.datatype = arithmetic_type;
2909 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2910 expression->expression.datatype = type_left;
2911 } else if(type_left->type == TYPE_POINTER &&
2912 type_right->type == TYPE_POINTER) {
2913 if(!pointers_compatible(type_left, type_right)) {
2914 parser_print_error_prefix();
2915 fprintf(stderr, "pointers to incompatible objects to binary - (");
2916 print_type(orig_type_left);
2917 fprintf(stderr, ", ");
2918 print_type(orig_type_right);
2919 fprintf(stderr, ")\n");
2921 expression->expression.datatype = type_ptrdiff_t;
2924 parser_print_error_prefix();
2925 fprintf(stderr, "invalid operands to binary - (");
2926 print_type(orig_type_left);
2927 fprintf(stderr, ", ");
2928 print_type(orig_type_right);
2929 fprintf(stderr, ")\n");
2933 static void semantic_comparison(binary_expression_t *expression)
2935 expression_t *left = expression->left;
2936 expression_t *right = expression->right;
2937 type_t *orig_type_left = left->datatype;
2938 type_t *orig_type_right = right->datatype;
2940 if(orig_type_left == NULL || orig_type_right == NULL)
2943 type_t *type_left = skip_typeref(orig_type_left);
2944 type_t *type_right = skip_typeref(orig_type_right);
2946 /* TODO non-arithmetic types */
2947 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2948 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2949 expression->left = create_implicit_cast(left, arithmetic_type);
2950 expression->right = create_implicit_cast(right, arithmetic_type);
2951 expression->expression.datatype = arithmetic_type;
2953 expression->expression.datatype = type_int;
2956 static void semantic_arithmetic_assign(binary_expression_t *expression)
2958 expression_t *left = expression->left;
2959 expression_t *right = expression->right;
2960 type_t *orig_type_left = left->datatype;
2961 type_t *orig_type_right = right->datatype;
2963 if(orig_type_left == NULL || orig_type_right == NULL)
2966 type_t *type_left = skip_typeref(orig_type_left);
2967 type_t *type_right = skip_typeref(orig_type_right);
2969 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2970 /* TODO: improve error message */
2971 parser_print_error_prefix();
2972 fprintf(stderr, "operation needs arithmetic types\n");
2976 /* combined instructions are tricky. We can't create an implicit cast on
2977 * the left side, because we need the uncasted form for the store.
2978 * The ast2firm pass has to know that left_type must be right_type
2979 * for the arithmeitc operation and create a cast by itself */
2980 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2981 expression->right = create_implicit_cast(right, arithmetic_type);
2982 expression->expression.datatype = type_left;
2985 static void semantic_logical_op(binary_expression_t *expression)
2987 expression_t *left = expression->left;
2988 expression_t *right = expression->right;
2989 type_t *orig_type_left = left->datatype;
2990 type_t *orig_type_right = right->datatype;
2992 if(orig_type_left == NULL || orig_type_right == NULL)
2995 type_t *type_left = skip_typeref(orig_type_left);
2996 type_t *type_right = skip_typeref(orig_type_right);
2998 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2999 /* TODO: improve error message */
3000 parser_print_error_prefix();
3001 fprintf(stderr, "operation needs arithmetic types\n");
3005 expression->expression.datatype = type_int;
3008 static void semantic_binexpr_assign(binary_expression_t *expression)
3010 expression_t *left = expression->left;
3011 type_t *type_left = left->datatype;
3013 if(type_left != NULL) {
3014 semantic_assign(type_left, &expression->right, "assignment");
3017 expression->expression.datatype = type_left;
3020 static void semantic_comma(binary_expression_t *expression)
3022 expression->expression.datatype = expression->right->datatype;
3025 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3026 static expression_t *parse_##binexpression_type(unsigned precedence, \
3027 expression_t *left) \
3031 expression_t *right = parse_sub_expression(precedence + lr); \
3033 binary_expression_t *binexpr \
3034 = allocate_ast_zero(sizeof(binexpr[0])); \
3035 binexpr->expression.type = EXPR_BINARY; \
3036 binexpr->type = binexpression_type; \
3037 binexpr->left = left; \
3038 binexpr->right = right; \
3041 return (expression_t*) binexpr; \
3044 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3045 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3046 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3047 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3048 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3049 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3050 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3051 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3052 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3053 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3054 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3055 semantic_comparison, 1)
3056 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3057 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3058 semantic_comparison, 1)
3059 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3060 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3061 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3062 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3063 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3064 /* TODO shift has a bit special semantic */
3065 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3066 semantic_shift_op, 1)
3067 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3068 semantic_shift_op, 1)
3069 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3070 semantic_arithmetic_assign, 0)
3071 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3072 semantic_arithmetic_assign, 0)
3073 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3074 semantic_arithmetic_assign, 0)
3075 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3076 semantic_arithmetic_assign, 0)
3077 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3078 semantic_arithmetic_assign, 0)
3079 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3080 semantic_arithmetic_assign, 0)
3081 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3082 semantic_arithmetic_assign, 0)
3083 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3084 semantic_arithmetic_assign, 0)
3085 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3086 semantic_arithmetic_assign, 0)
3087 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3088 semantic_arithmetic_assign, 0)
3090 static expression_t *parse_sub_expression(unsigned precedence)
3092 if(token.type < 0) {
3093 return expected_expression_error();
3096 expression_parser_function_t *parser
3097 = &expression_parsers[token.type];
3098 source_position_t source_position = token.source_position;
3101 if(parser->parser != NULL) {
3102 left = parser->parser(parser->precedence);
3104 left = parse_primary_expression();
3106 assert(left != NULL);
3107 left->source_position = source_position;
3110 if(token.type < 0) {
3111 return expected_expression_error();
3114 parser = &expression_parsers[token.type];
3115 if(parser->infix_parser == NULL)
3117 if(parser->infix_precedence < precedence)
3120 left = parser->infix_parser(parser->infix_precedence, left);
3122 assert(left != NULL);
3123 assert(left->type != EXPR_INVALID);
3124 left->source_position = source_position;
3130 static expression_t *parse_expression(void)
3132 return parse_sub_expression(1);
3137 static void register_expression_parser(parse_expression_function parser,
3138 int token_type, unsigned precedence)
3140 expression_parser_function_t *entry = &expression_parsers[token_type];
3142 if(entry->parser != NULL) {
3143 fprintf(stderr, "for token ");
3144 print_token_type(stderr, token_type);
3145 fprintf(stderr, "\n");
3146 panic("trying to register multiple expression parsers for a token");
3148 entry->parser = parser;
3149 entry->precedence = precedence;
3152 static void register_expression_infix_parser(
3153 parse_expression_infix_function parser, int token_type,
3154 unsigned precedence)
3156 expression_parser_function_t *entry = &expression_parsers[token_type];
3158 if(entry->infix_parser != NULL) {
3159 fprintf(stderr, "for token ");
3160 print_token_type(stderr, token_type);
3161 fprintf(stderr, "\n");
3162 panic("trying to register multiple infix expression parsers for a "
3165 entry->infix_parser = parser;
3166 entry->infix_precedence = precedence;
3169 static void init_expression_parsers(void)
3171 memset(&expression_parsers, 0, sizeof(expression_parsers));
3173 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3174 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3175 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3176 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3177 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3178 T_GREATERGREATER, 16);
3179 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3180 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3181 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3182 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3183 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3184 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3185 T_GREATEREQUAL, 14);
3186 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3187 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3188 T_EXCLAMATIONMARKEQUAL, 13);
3189 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3190 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3191 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3192 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3193 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3194 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3195 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3196 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3197 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3198 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3199 T_ASTERISKEQUAL, 2);
3200 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3201 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3203 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3204 T_LESSLESSEQUAL, 2);
3205 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3206 T_GREATERGREATEREQUAL, 2);
3207 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3209 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3211 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3214 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3216 register_expression_infix_parser(parse_array_expression, '[', 30);
3217 register_expression_infix_parser(parse_call_expression, '(', 30);
3218 register_expression_infix_parser(parse_select_expression, '.', 30);
3219 register_expression_infix_parser(parse_select_expression,
3220 T_MINUSGREATER, 30);
3221 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3223 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3226 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3227 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3228 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3229 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3230 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3231 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3232 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3233 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3234 register_expression_parser(parse_sizeof, T_sizeof, 25);
3235 register_expression_parser(parse_extension, T___extension__, 25);
3239 static statement_t *parse_case_statement(void)
3242 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3243 label->statement.type = STATEMENT_CASE_LABEL;
3244 label->statement.source_position = token.source_position;
3246 label->expression = parse_expression();
3249 label->statement.next = parse_statement();
3251 return (statement_t*) label;
3254 static statement_t *parse_default_statement(void)
3258 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3259 label->statement.type = STATEMENT_CASE_LABEL;
3260 label->statement.source_position = token.source_position;
3263 label->statement.next = parse_statement();
3265 return (statement_t*) label;
3268 static declaration_t *get_label(symbol_t *symbol)
3270 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3271 assert(current_function != NULL);
3272 /* if we found a label in the same function, then we already created the
3274 if(candidate != NULL
3275 && candidate->parent_context == ¤t_function->context) {
3279 /* otherwise we need to create a new one */
3280 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3281 declaration->namespace = NAMESPACE_LABEL;
3282 declaration->symbol = symbol;
3284 label_push(declaration);
3289 static statement_t *parse_label_statement(void)
3291 assert(token.type == T_IDENTIFIER);
3292 symbol_t *symbol = token.v.symbol;
3295 declaration_t *label = get_label(symbol);
3297 /* if source position is already set then the label is defined twice,
3298 * otherwise it was just mentioned in a goto so far */
3299 if(label->source_position.input_name != NULL) {
3300 parser_print_error_prefix();
3301 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3302 parser_print_error_prefix_pos(label->source_position);
3303 fprintf(stderr, "previous definition of '%s' was here\n",
3306 label->source_position = token.source_position;
3309 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3311 label_statement->statement.type = STATEMENT_LABEL;
3312 label_statement->statement.source_position = token.source_position;
3313 label_statement->label = label;
3317 if(token.type == '}') {
3318 parse_error("label at end of compound statement");
3319 return (statement_t*) label_statement;
3321 label_statement->label_statement = parse_statement();
3324 return (statement_t*) label_statement;
3327 static statement_t *parse_if(void)
3331 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3332 statement->statement.type = STATEMENT_IF;
3333 statement->statement.source_position = token.source_position;
3336 statement->condition = parse_expression();
3339 statement->true_statement = parse_statement();
3340 if(token.type == T_else) {
3342 statement->false_statement = parse_statement();
3345 return (statement_t*) statement;
3348 static statement_t *parse_switch(void)
3352 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3353 statement->statement.type = STATEMENT_SWITCH;
3354 statement->statement.source_position = token.source_position;
3357 statement->expression = parse_expression();
3359 statement->body = parse_statement();
3361 return (statement_t*) statement;
3364 static statement_t *parse_while(void)
3368 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3369 statement->statement.type = STATEMENT_WHILE;
3370 statement->statement.source_position = token.source_position;
3373 statement->condition = parse_expression();
3375 statement->body = parse_statement();
3377 return (statement_t*) statement;
3380 static statement_t *parse_do(void)
3384 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3385 statement->statement.type = STATEMENT_DO_WHILE;
3386 statement->statement.source_position = token.source_position;
3388 statement->body = parse_statement();
3391 statement->condition = parse_expression();
3395 return (statement_t*) statement;
3398 static statement_t *parse_for(void)
3402 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3403 statement->statement.type = STATEMENT_FOR;
3404 statement->statement.source_position = token.source_position;
3408 int top = environment_top();
3409 context_t *last_context = context;
3410 set_context(&statement->context);
3412 if(token.type != ';') {
3413 if(is_declaration_specifier(&token, false)) {
3414 parse_declaration();
3416 statement->initialisation = parse_expression();
3423 if(token.type != ';') {
3424 statement->condition = parse_expression();
3427 if(token.type != ')') {
3428 statement->step = parse_expression();
3431 statement->body = parse_statement();
3433 assert(context == &statement->context);
3434 set_context(last_context);
3435 environment_pop_to(top);
3437 return (statement_t*) statement;
3440 static statement_t *parse_goto(void)
3444 if(token.type != T_IDENTIFIER) {
3445 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
3449 symbol_t *symbol = token.v.symbol;
3452 declaration_t *label = get_label(symbol);
3454 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3456 statement->statement.type = STATEMENT_GOTO;
3457 statement->statement.source_position = token.source_position;
3459 statement->label = label;
3463 return (statement_t*) statement;
3466 static statement_t *parse_continue(void)
3471 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3472 statement->type = STATEMENT_CONTINUE;
3473 statement->source_position = token.source_position;
3478 static statement_t *parse_break(void)
3483 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3484 statement->type = STATEMENT_BREAK;
3485 statement->source_position = token.source_position;
3490 static statement_t *parse_return(void)
3494 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3496 statement->statement.type = STATEMENT_RETURN;
3497 statement->statement.source_position = token.source_position;
3499 assert(current_function->type->type == TYPE_FUNCTION);
3500 function_type_t *function_type = (function_type_t*) current_function->type;
3501 type_t *return_type = function_type->result_type;
3503 expression_t *return_value;
3504 if(token.type != ';') {
3505 return_value = parse_expression();
3507 if(return_type == type_void && return_value->datatype != type_void) {
3508 parse_warning("'return' with a value, in function returning void");
3509 return_value = NULL;
3511 if(return_type != NULL) {
3512 semantic_assign(return_type, &return_value, "'return'");
3516 return_value = NULL;
3517 if(return_type != type_void) {
3518 parse_warning("'return' without value, in function returning "
3522 statement->return_value = return_value;
3526 return (statement_t*) statement;
3529 static statement_t *parse_declaration_statement(void)
3531 declaration_t *before = last_declaration;
3533 declaration_statement_t *statement
3534 = allocate_ast_zero(sizeof(statement[0]));
3535 statement->statement.type = STATEMENT_DECLARATION;
3536 statement->statement.source_position = token.source_position;
3538 declaration_specifiers_t specifiers;
3539 memset(&specifiers, 0, sizeof(specifiers));
3540 parse_declaration_specifiers(&specifiers);
3542 if(token.type == ';') {
3545 parse_init_declarators(&specifiers);
3548 if(before == NULL) {
3549 statement->declarations_begin = context->declarations;
3551 statement->declarations_begin = before->next;
3553 statement->declarations_end = last_declaration;
3555 return (statement_t*) statement;
3558 static statement_t *parse_expression_statement(void)
3560 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3561 statement->statement.type = STATEMENT_EXPRESSION;
3562 statement->statement.source_position = token.source_position;
3564 statement->expression = parse_expression();
3568 return (statement_t*) statement;
3571 static statement_t *parse_statement(void)
3573 statement_t *statement = NULL;
3575 /* declaration or statement */
3576 switch(token.type) {
3578 statement = parse_case_statement();
3582 statement = parse_default_statement();
3586 statement = parse_compound_statement();
3590 statement = parse_if();
3594 statement = parse_switch();
3598 statement = parse_while();
3602 statement = parse_do();
3606 statement = parse_for();
3610 statement = parse_goto();
3614 statement = parse_continue();
3618 statement = parse_break();
3622 statement = parse_return();
3631 if(look_ahead(1)->type == ':') {
3632 statement = parse_label_statement();
3636 if(is_typedef_symbol(token.v.symbol)) {
3637 statement = parse_declaration_statement();
3641 statement = parse_expression_statement();
3644 case T___extension__:
3645 /* this can be a prefix to a declaration or an expression statement */
3646 /* we simply eat it now and parse the rest with tail recursion */
3649 } while(token.type == T___extension__);
3650 statement = parse_statement();
3654 statement = parse_declaration_statement();
3658 statement = parse_expression_statement();
3662 assert(statement == NULL || statement->source_position.input_name != NULL);
3667 static statement_t *parse_compound_statement(void)
3669 compound_statement_t *compound_statement
3670 = allocate_ast_zero(sizeof(compound_statement[0]));
3671 compound_statement->statement.type = STATEMENT_COMPOUND;
3672 compound_statement->statement.source_position = token.source_position;
3676 int top = environment_top();
3677 context_t *last_context = context;
3678 set_context(&compound_statement->context);
3680 statement_t *last_statement = NULL;
3682 while(token.type != '}' && token.type != T_EOF) {
3683 statement_t *statement = parse_statement();
3684 if(statement == NULL)
3687 if(last_statement != NULL) {
3688 last_statement->next = statement;
3690 compound_statement->statements = statement;
3693 while(statement->next != NULL)
3694 statement = statement->next;
3696 last_statement = statement;
3699 if(token.type != '}') {
3700 parser_print_error_prefix_pos(
3701 compound_statement->statement.source_position);
3702 fprintf(stderr, "end of file while looking for closing '}'\n");
3706 assert(context == &compound_statement->context);
3707 set_context(last_context);
3708 environment_pop_to(top);
3710 return (statement_t*) compound_statement;
3713 static translation_unit_t *parse_translation_unit(void)
3715 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3717 assert(global_context == NULL);
3718 global_context = &unit->context;
3720 assert(context == NULL);
3721 set_context(&unit->context);
3723 while(token.type != T_EOF) {
3724 parse_declaration();
3727 assert(context == &unit->context);
3729 last_declaration = NULL;
3731 assert(global_context == &unit->context);
3732 global_context = NULL;
3737 translation_unit_t *parse(void)
3739 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3740 label_stack = NEW_ARR_F(stack_entry_t, 0);
3741 found_error = false;
3743 type_set_output(stderr);
3744 ast_set_output(stderr);
3746 lookahead_bufpos = 0;
3747 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3750 translation_unit_t *unit = parse_translation_unit();
3752 DEL_ARR_F(environment_stack);
3753 DEL_ARR_F(label_stack);
3761 void init_parser(void)
3763 init_expression_parsers();
3764 obstack_init(&temp_obst);
3766 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3767 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3768 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
3769 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3770 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
3771 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
3772 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
3773 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3774 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3775 type_string = make_pointer_type(type_const_char, 0);
3778 void exit_parser(void)
3780 obstack_free(&temp_obst, NULL);