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 if(source_type->type == TYPE_POINTER) {
598 if(!pointers_compatible(source_type, dest_type)) {
599 type_error_incompatible("can't implicitely cast types",
600 expression->source_position,
601 source_type, dest_type);
603 return create_cast_expression(expression, dest_type);
608 panic("casting of non-atomic types not implemented yet");
611 static void semantic_assign(type_t *orig_type_left, expression_t **right,
614 type_t *orig_type_right = (*right)->datatype;
616 if(orig_type_right == NULL)
619 type_t *type_left = skip_typeref(orig_type_left);
620 type_t *type_right = skip_typeref(orig_type_right);
622 if(type_left == type_right) {
624 } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
625 *right = create_implicit_cast(*right, type_left);
626 } else if(type_left->type == TYPE_POINTER
627 && type_right->type == TYPE_POINTER) {
630 /* TODO: improve error message */
631 parser_print_error_prefix();
632 fprintf(stderr, "incompatible types in %s\n", context);
633 parser_print_error_prefix();
634 print_type(type_left);
635 fputs(" <- ", stderr);
636 print_type(type_right);
642 static expression_t *parse_constant_expression(void)
644 /* start parsing at precedence 7 (conditional expression) */
645 return parse_sub_expression(7);
648 static expression_t *parse_assignment_expression(void)
650 /* start parsing at precedence 2 (assignment expression) */
651 return parse_sub_expression(2);
654 typedef struct declaration_specifiers_t declaration_specifiers_t;
655 struct declaration_specifiers_t {
656 storage_class_t storage_class;
661 static void parse_compound_type_entries(void);
662 static declaration_t *parse_declarator(
663 const declaration_specifiers_t *specifiers, type_t *type,
664 bool may_be_abstract);
665 static declaration_t *record_declaration(declaration_t *declaration);
667 static const char *parse_string_literals(void)
669 assert(token.type == T_STRING_LITERAL);
670 const char *result = token.v.string;
674 while(token.type == T_STRING_LITERAL) {
675 result = concat_strings(result, token.v.string);
682 static void parse_attributes(void)
686 case T___attribute__:
694 parse_error("EOF while parsing attribute");
712 if(token.type != T_STRING_LITERAL) {
713 parse_error_expected("while parsing assembler attribute",
718 parse_string_literals();
723 goto attributes_finished;
731 static designator_t *parse_designation(void)
733 if(token.type != '[' && token.type != '.')
736 designator_t *result = NULL;
737 designator_t *last = NULL;
740 designator_t *designator;
743 designator = allocate_ast_zero(sizeof(designator[0]));
745 designator->array_access = parse_constant_expression();
749 designator = allocate_ast_zero(sizeof(designator[0]));
751 if(token.type != T_IDENTIFIER) {
752 parse_error_expected("problem while parsing designator",
756 designator->symbol = token.v.symbol;
764 assert(designator != NULL);
766 last->next = designator;
774 static initializer_t *parse_initializer_list(type_t *type);
776 static initializer_t *parse_initializer(type_t *type)
778 designator_t *designator = parse_designation();
780 initializer_t *result;
781 if(token.type == '{') {
782 result = parse_initializer_list(type);
784 result = allocate_ast_zero(sizeof(result[0]));
785 result->type = INITIALIZER_VALUE;
786 result->v.value = parse_assignment_expression();
789 semantic_assign(type, &result->v.value, "initializer");
792 result->designator = designator;
797 static initializer_t *parse_initializer_list(type_t *type)
804 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
805 result->type = INITIALIZER_LIST;
807 initializer_t *last = NULL;
809 initializer_t *initializer = parse_initializer(NULL);
811 last->next = initializer;
813 result->v.list = initializer;
817 if(token.type == '}')
820 if(token.type != ',') {
821 parse_error_expected("problem while parsing initializer list",
828 if(token.type == '}')
837 static declaration_t *parse_compound_type_specifier(bool is_struct)
845 symbol_t *symbol = NULL;
846 declaration_t *declaration = NULL;
848 if(token.type == T_IDENTIFIER) {
849 symbol = token.v.symbol;
853 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
855 declaration = get_declaration(symbol, NAMESPACE_UNION);
857 } else if(token.type != '{') {
859 parse_error_expected("problem while parsing struct type specifier",
860 T_IDENTIFIER, '{', 0);
862 parse_error_expected("problem while parsing union type specifier",
863 T_IDENTIFIER, '{', 0);
869 if(declaration == NULL) {
870 declaration = allocate_type_zero(sizeof(declaration[0]));
873 declaration->namespace = NAMESPACE_STRUCT;
875 declaration->namespace = NAMESPACE_UNION;
877 declaration->source_position = token.source_position;
878 declaration->symbol = symbol;
881 if(token.type == '{') {
882 if(declaration->init.is_defined) {
883 assert(symbol != NULL);
884 parser_print_error_prefix();
885 fprintf(stderr, "multiple definition of %s %s\n",
886 is_struct ? "struct" : "union", symbol->string);
887 declaration->context.declarations = NULL;
889 record_declaration(declaration);
890 declaration->init.is_defined = true;
892 int top = environment_top();
893 context_t *last_context = context;
894 set_context(& declaration->context);
896 parse_compound_type_entries();
899 assert(context == & declaration->context);
900 set_context(last_context);
901 environment_pop_to(top);
907 static void parse_enum_entries(void)
911 if(token.type == '}') {
913 parse_error("empty enum not allowed");
918 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
920 if(token.type != T_IDENTIFIER) {
921 parse_error_expected("problem while parsing enum entry",
926 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
927 entry->symbol = token.v.symbol;
928 entry->source_position = token.source_position;
931 if(token.type == '=') {
933 entry->init.initializer = parse_initializer(type_int);
936 record_declaration(entry);
938 if(token.type != ',')
941 } while(token.type != '}');
946 static declaration_t *parse_enum_specifier(void)
950 declaration_t *declaration;
953 if(token.type == T_IDENTIFIER) {
954 symbol = token.v.symbol;
957 declaration = get_declaration(symbol, NAMESPACE_ENUM);
958 } else if(token.type != '{') {
959 parse_error_expected("problem while parsing enum type specifier",
960 T_IDENTIFIER, '{', 0);
967 if(declaration == NULL) {
968 declaration = allocate_type_zero(sizeof(declaration[0]));
970 declaration->namespace = NAMESPACE_ENUM;
971 declaration->source_position = token.source_position;
972 declaration->symbol = symbol;
975 if(token.type == '{') {
976 if(declaration->init.is_defined) {
977 parser_print_error_prefix();
978 fprintf(stderr, "multiple definitions of enum %s\n",
981 record_declaration(declaration);
982 declaration->init.is_defined = 1;
984 parse_enum_entries();
992 * if a symbol is a typedef to another type, return true
994 static bool is_typedef_symbol(symbol_t *symbol)
996 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
997 if(declaration == NULL
998 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1004 static type_t *parse_typeof(void)
1012 expression_t *expression = NULL;
1015 switch(token.type) {
1016 case T___extension__:
1017 /* this can be a prefix to a typename or an expression */
1018 /* we simply eat it now. */
1021 } while(token.type == T___extension__);
1025 if(is_typedef_symbol(token.v.symbol)) {
1026 type = parse_typename();
1028 expression = parse_expression();
1029 type = expression->datatype;
1034 type = parse_typename();
1038 expression = parse_expression();
1039 type = expression->datatype;
1045 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1046 typeof->type.type = TYPE_TYPEOF;
1047 typeof->expression = expression;
1048 typeof->typeof_type = type;
1050 return (type_t*) typeof;
1054 SPECIFIER_SIGNED = 1 << 0,
1055 SPECIFIER_UNSIGNED = 1 << 1,
1056 SPECIFIER_LONG = 1 << 2,
1057 SPECIFIER_INT = 1 << 3,
1058 SPECIFIER_DOUBLE = 1 << 4,
1059 SPECIFIER_CHAR = 1 << 5,
1060 SPECIFIER_SHORT = 1 << 6,
1061 SPECIFIER_LONG_LONG = 1 << 7,
1062 SPECIFIER_FLOAT = 1 << 8,
1063 SPECIFIER_BOOL = 1 << 9,
1064 SPECIFIER_VOID = 1 << 10,
1065 #ifdef PROVIDE_COMPLEX
1066 SPECIFIER_COMPLEX = 1 << 11,
1068 #ifdef PROVIDE_IMAGINARY
1069 SPECIFIER_IMAGINARY = 1 << 12,
1073 static type_t *create_builtin_type(symbol_t *symbol)
1075 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1076 type->type.type = TYPE_BUILTIN;
1077 type->symbol = symbol;
1079 type->real_type = type_int;
1081 return (type_t*) type;
1084 static type_t *get_typedef_type(symbol_t *symbol)
1086 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1087 if(declaration == NULL
1088 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1091 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1092 typedef_type->type.type = TYPE_TYPEDEF;
1093 typedef_type->declaration = declaration;
1095 return (type_t*) typedef_type;
1098 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1100 type_t *type = NULL;
1101 unsigned type_qualifiers = 0;
1102 unsigned type_specifiers = 0;
1106 switch(token.type) {
1109 #define MATCH_STORAGE_CLASS(token, class) \
1111 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1112 parse_error("multiple storage classes in declaration " \
1115 specifiers->storage_class = class; \
1119 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1120 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1121 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1122 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1123 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1125 /* type qualifiers */
1126 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1128 type_qualifiers |= qualifier; \
1132 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1133 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1134 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1136 case T___extension__:
1141 /* type specifiers */
1142 #define MATCH_SPECIFIER(token, specifier, name) \
1145 if(type_specifiers & specifier) { \
1146 parse_error("multiple " name " type specifiers given"); \
1148 type_specifiers |= specifier; \
1152 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1153 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1154 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1155 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1156 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1157 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1158 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1159 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1160 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1161 #ifdef PROVIDE_COMPLEX
1162 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1164 #ifdef PROVIDE_IMAGINARY
1165 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1169 specifiers->is_inline = true;
1174 if(type_specifiers & SPECIFIER_LONG_LONG) {
1175 parse_error("multiple type specifiers given");
1176 } else if(type_specifiers & SPECIFIER_LONG) {
1177 type_specifiers |= SPECIFIER_LONG_LONG;
1179 type_specifiers |= SPECIFIER_LONG;
1183 /* TODO: if type != NULL for the following rules should issue
1186 compound_type_t *compound_type
1187 = allocate_type_zero(sizeof(compound_type[0]));
1188 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1189 compound_type->declaration = parse_compound_type_specifier(true);
1191 type = (type_t*) compound_type;
1195 compound_type_t *compound_type
1196 = allocate_type_zero(sizeof(compound_type[0]));
1197 compound_type->type.type = TYPE_COMPOUND_UNION;
1198 compound_type->declaration = parse_compound_type_specifier(false);
1200 type = (type_t*) compound_type;
1204 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1205 enum_type->type.type = TYPE_ENUM;
1206 enum_type->declaration = parse_enum_specifier();
1208 type = (type_t*) enum_type;
1212 type = parse_typeof();
1214 case T___builtin_va_list:
1215 type = create_builtin_type(token.v.symbol);
1219 case T___attribute__:
1224 case T_IDENTIFIER: {
1225 type_t *typedef_type = get_typedef_type(token.v.symbol);
1227 if(typedef_type == NULL)
1228 goto finish_specifiers;
1231 type = typedef_type;
1235 /* function specifier */
1237 goto finish_specifiers;
1244 atomic_type_type_t atomic_type;
1246 /* match valid basic types */
1247 switch(type_specifiers) {
1248 case SPECIFIER_VOID:
1249 atomic_type = ATOMIC_TYPE_VOID;
1251 case SPECIFIER_CHAR:
1252 atomic_type = ATOMIC_TYPE_CHAR;
1254 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1255 atomic_type = ATOMIC_TYPE_SCHAR;
1257 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1258 atomic_type = ATOMIC_TYPE_UCHAR;
1260 case SPECIFIER_SHORT:
1261 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1262 case SPECIFIER_SHORT | SPECIFIER_INT:
1263 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1264 atomic_type = ATOMIC_TYPE_SHORT;
1266 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1267 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1268 atomic_type = ATOMIC_TYPE_USHORT;
1271 case SPECIFIER_SIGNED:
1272 case SPECIFIER_SIGNED | SPECIFIER_INT:
1273 atomic_type = ATOMIC_TYPE_INT;
1275 case SPECIFIER_UNSIGNED:
1276 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1277 atomic_type = ATOMIC_TYPE_UINT;
1279 case SPECIFIER_LONG:
1280 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1281 case SPECIFIER_LONG | SPECIFIER_INT:
1282 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1283 atomic_type = ATOMIC_TYPE_LONG;
1285 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1286 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1287 atomic_type = ATOMIC_TYPE_ULONG;
1289 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1290 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1291 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1292 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1294 atomic_type = ATOMIC_TYPE_LONGLONG;
1296 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1297 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1299 atomic_type = ATOMIC_TYPE_ULONGLONG;
1301 case SPECIFIER_FLOAT:
1302 atomic_type = ATOMIC_TYPE_FLOAT;
1304 case SPECIFIER_DOUBLE:
1305 atomic_type = ATOMIC_TYPE_DOUBLE;
1307 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1308 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1310 case SPECIFIER_BOOL:
1311 atomic_type = ATOMIC_TYPE_BOOL;
1313 #ifdef PROVIDE_COMPLEX
1314 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1315 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1317 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1318 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1320 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1321 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1324 #ifdef PROVIDE_IMAGINARY
1325 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1326 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1328 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1329 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1331 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1332 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1336 /* invalid specifier combination, give an error message */
1337 if(type_specifiers == 0) {
1339 parse_warning("no type specifiers in declaration (using int)");
1340 atomic_type = ATOMIC_TYPE_INT;
1343 parse_error("no type specifiers given in declaration");
1345 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1346 (type_specifiers & SPECIFIER_UNSIGNED)) {
1347 parse_error("signed and unsigned specifiers gives");
1348 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1349 parse_error("only integer types can be signed or unsigned");
1351 parse_error("multiple datatypes in declaration");
1353 atomic_type = ATOMIC_TYPE_INVALID;
1356 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1357 atype->type.type = TYPE_ATOMIC;
1358 atype->atype = atomic_type;
1361 type = (type_t*) atype;
1363 if(type_specifiers != 0) {
1364 parse_error("multiple datatypes in declaration");
1368 type->qualifiers = type_qualifiers;
1370 type_t *result = typehash_insert(type);
1371 if(newtype && result != (type_t*) type) {
1375 specifiers->type = result;
1378 static type_qualifier_t parse_type_qualifiers(void)
1380 type_qualifier_t type_qualifiers = 0;
1383 switch(token.type) {
1384 /* type qualifiers */
1385 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1386 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1387 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1390 return type_qualifiers;
1395 static void parse_identifier_list(void)
1398 if(token.type != T_IDENTIFIER) {
1399 parse_error_expected("problem while parsing parameter identifier "
1400 "list", T_IDENTIFIER, 0);
1404 if(token.type != ',')
1410 static declaration_t *parse_parameter(void)
1412 declaration_specifiers_t specifiers;
1413 memset(&specifiers, 0, sizeof(specifiers));
1415 parse_declaration_specifiers(&specifiers);
1417 declaration_t *declaration
1418 = parse_declarator(&specifiers, specifiers.type, true);
1420 /* TODO check declaration constraints for parameters */
1421 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1422 parse_error("typedef not allowed in parameter list");
1428 static declaration_t *parse_parameters(function_type_t *type)
1430 if(token.type == T_IDENTIFIER) {
1431 symbol_t *symbol = token.v.symbol;
1432 if(!is_typedef_symbol(symbol)) {
1434 parse_identifier_list();
1439 if(token.type == ')') {
1440 type->unspecified_parameters = 1;
1443 if(token.type == T_void && look_ahead(1)->type == ')') {
1448 declaration_t *declarations = NULL;
1449 declaration_t *declaration;
1450 declaration_t *last_declaration = NULL;
1451 function_parameter_t *parameter;
1452 function_parameter_t *last_parameter = NULL;
1455 switch(token.type) {
1459 return declarations;
1462 case T___extension__:
1464 declaration = parse_parameter();
1466 parameter = allocate_type_zero(sizeof(parameter[0]));
1467 parameter->type = declaration->type;
1469 if(last_parameter != NULL) {
1470 last_declaration->next = declaration;
1471 last_parameter->next = parameter;
1473 type->parameters = parameter;
1474 declarations = declaration;
1476 last_parameter = parameter;
1477 last_declaration = declaration;
1481 return declarations;
1483 if(token.type != ',')
1484 return declarations;
1494 } construct_type_type_t;
1496 typedef struct construct_type_t construct_type_t;
1497 struct construct_type_t {
1498 construct_type_type_t type;
1499 construct_type_t *next;
1502 typedef struct parsed_pointer_t parsed_pointer_t;
1503 struct parsed_pointer_t {
1504 construct_type_t construct_type;
1505 type_qualifier_t type_qualifiers;
1508 typedef struct construct_function_type_t construct_function_type_t;
1509 struct construct_function_type_t {
1510 construct_type_t construct_type;
1511 function_type_t *function_type;
1514 typedef struct parsed_array_t parsed_array_t;
1515 struct parsed_array_t {
1516 construct_type_t construct_type;
1517 type_qualifier_t type_qualifiers;
1523 typedef struct construct_base_type_t construct_base_type_t;
1524 struct construct_base_type_t {
1525 construct_type_t construct_type;
1529 static construct_type_t *parse_pointer_declarator(void)
1533 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1534 memset(pointer, 0, sizeof(pointer[0]));
1535 pointer->construct_type.type = CONSTRUCT_POINTER;
1536 pointer->type_qualifiers = parse_type_qualifiers();
1538 return (construct_type_t*) pointer;
1541 static construct_type_t *parse_array_declarator(void)
1545 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1546 memset(array, 0, sizeof(array[0]));
1547 array->construct_type.type = CONSTRUCT_ARRAY;
1549 if(token.type == T_static) {
1550 array->is_static = true;
1554 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1555 if(type_qualifiers != 0) {
1556 if(token.type == T_static) {
1557 array->is_static = true;
1561 array->type_qualifiers = type_qualifiers;
1563 if(token.type == '*' && look_ahead(1)->type == ']') {
1564 array->is_variable = true;
1566 } else if(token.type != ']') {
1567 array->size = parse_assignment_expression();
1572 return (construct_type_t*) array;
1575 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1579 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1580 type->type.type = TYPE_FUNCTION;
1582 declaration_t *parameters = parse_parameters(type);
1583 if(declaration != NULL) {
1584 declaration->context.declarations = parameters;
1587 construct_function_type_t *construct_function_type =
1588 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1589 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1590 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1591 construct_function_type->function_type = type;
1595 return (construct_type_t*) construct_function_type;
1598 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1599 int may_be_abstract)
1601 construct_type_t *result = NULL;
1602 construct_type_t *last = NULL;
1604 while(token.type == '*') {
1605 construct_type_t *type = parse_pointer_declarator();
1614 /* TODO: find out if this is correct */
1617 construct_type_t *inner_types = NULL;
1619 switch(token.type) {
1621 if(declaration == NULL) {
1622 parse_error("no identifier expected in typename");
1624 declaration->symbol = token.v.symbol;
1625 declaration->source_position = token.source_position;
1631 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1637 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1642 construct_type_t *type;
1643 switch(token.type) {
1645 type = parse_function_declarator(declaration);
1648 type = parse_array_declarator();
1651 goto declarator_finished;
1662 declarator_finished:
1665 if(inner_types != NULL) {
1667 last->next = inner_types;
1669 result = inner_types;
1677 static type_t *construct_declarator_type(construct_type_t *construct_list,
1680 construct_type_t *iter = construct_list;
1681 for( ; iter != NULL; iter = iter->next) {
1682 parsed_pointer_t *parsed_pointer;
1683 parsed_array_t *parsed_array;
1684 construct_function_type_t *construct_function_type;
1685 function_type_t *function_type;
1686 pointer_type_t *pointer_type;
1687 array_type_t *array_type;
1689 switch(iter->type) {
1690 case CONSTRUCT_INVALID:
1691 panic("invalid type construction found");
1692 case CONSTRUCT_FUNCTION:
1693 construct_function_type = (construct_function_type_t*) iter;
1694 function_type = construct_function_type->function_type;
1696 function_type->result_type = type;
1697 type = (type_t*) function_type;
1700 case CONSTRUCT_POINTER:
1701 parsed_pointer = (parsed_pointer_t*) iter;
1702 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1704 pointer_type->type.type = TYPE_POINTER;
1705 pointer_type->points_to = type;
1706 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1707 type = (type_t*) pointer_type;
1710 case CONSTRUCT_ARRAY:
1711 parsed_array = (parsed_array_t*) iter;
1712 array_type = allocate_type_zero(sizeof(array_type[0]));
1714 array_type->type.type = TYPE_ARRAY;
1715 array_type->element_type = type;
1716 array_type->type.qualifiers = parsed_array->type_qualifiers;
1717 array_type->is_static = parsed_array->is_static;
1718 array_type->is_variable = parsed_array->is_variable;
1719 array_type->size = parsed_array->size;
1720 type = (type_t*) array_type;
1724 type_t *hashed_type = typehash_insert((type_t*) type);
1725 if(hashed_type != type) {
1734 static declaration_t *parse_declarator(
1735 const declaration_specifiers_t *specifiers,
1736 type_t *type, bool may_be_abstract)
1738 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1739 declaration->storage_class = specifiers->storage_class;
1740 declaration->is_inline = specifiers->is_inline;
1742 construct_type_t *construct_type
1743 = parse_inner_declarator(declaration, may_be_abstract);
1744 declaration->type = construct_declarator_type(construct_type, type);
1746 if(construct_type != NULL) {
1747 obstack_free(&temp_obst, construct_type);
1753 static type_t *parse_abstract_declarator(type_t *base_type)
1755 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1757 type_t *result = construct_declarator_type(construct_type, base_type);
1758 if(construct_type != NULL) {
1759 obstack_free(&temp_obst, construct_type);
1765 static declaration_t *record_declaration(declaration_t *declaration)
1767 assert(context != NULL);
1769 symbol_t *symbol = declaration->symbol;
1770 if(symbol != NULL) {
1771 declaration_t *alias = environment_push(declaration);
1772 if(alias != declaration)
1775 declaration->parent_context = context;
1778 if(last_declaration != NULL) {
1779 last_declaration->next = declaration;
1781 context->declarations = declaration;
1783 last_declaration = declaration;
1788 static void parser_error_multiple_definition(declaration_t *previous,
1789 declaration_t *declaration)
1791 parser_print_error_prefix_pos(declaration->source_position);
1792 fprintf(stderr, "multiple definition of symbol '%s'\n",
1793 declaration->symbol->string);
1794 parser_print_error_prefix_pos(previous->source_position);
1795 fprintf(stderr, "this is the location of the previous "
1800 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1803 declaration_t *ndeclaration
1804 = parse_declarator(specifiers, specifiers->type, false);
1806 declaration_t *declaration = record_declaration(ndeclaration);
1808 type_t *type = declaration->type;
1809 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
1810 parser_print_warning_prefix_pos(declaration->source_position);
1811 fprintf(stderr, "variable ‘%s’ declared ‘inline’\n",
1812 declaration->symbol->string);
1815 if(token.type == '=') {
1818 /* TODO: check that this is an allowed type (no function type) */
1820 if(declaration->init.initializer != NULL) {
1821 parser_error_multiple_definition(declaration, ndeclaration);
1824 ndeclaration->init.initializer = parse_initializer(declaration->type);
1825 } else if(token.type == '{') {
1826 if(declaration->type->type != TYPE_FUNCTION) {
1827 parser_print_error_prefix();
1828 fprintf(stderr, "Declarator ");
1829 print_type_ext(declaration->type, declaration->symbol, NULL);
1830 fprintf(stderr, " has a body but is not a function type.\n");
1835 if(declaration->init.statement != NULL) {
1836 parser_error_multiple_definition(declaration, ndeclaration);
1838 if(ndeclaration != declaration) {
1839 memcpy(&declaration->context, &ndeclaration->context,
1840 sizeof(declaration->context));
1843 int top = environment_top();
1844 context_t *last_context = context;
1845 set_context(&declaration->context);
1847 /* push function parameters */
1848 declaration_t *parameter = declaration->context.declarations;
1849 for( ; parameter != NULL; parameter = parameter->next) {
1850 environment_push(parameter);
1853 int label_stack_top = label_top();
1854 declaration_t *old_current_function = current_function;
1855 current_function = declaration;
1857 statement_t *statement = parse_compound_statement();
1859 assert(current_function == declaration);
1860 current_function = old_current_function;
1861 label_pop_to(label_stack_top);
1863 assert(context == &declaration->context);
1864 set_context(last_context);
1865 environment_pop_to(top);
1867 declaration->init.statement = statement;
1871 if(token.type != ',')
1878 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1881 if(token.type == ':') {
1883 parse_constant_expression();
1884 /* TODO (bitfields) */
1886 declaration_t *declaration
1887 = parse_declarator(specifiers, specifiers->type, true);
1889 /* TODO: check constraints for struct declarations */
1890 /* TODO: check for doubled fields */
1891 record_declaration(declaration);
1893 if(token.type == ':') {
1895 parse_constant_expression();
1896 /* TODO (bitfields) */
1900 if(token.type != ',')
1907 static void parse_compound_type_entries(void)
1911 while(token.type != '}' && token.type != T_EOF) {
1912 declaration_specifiers_t specifiers;
1913 memset(&specifiers, 0, sizeof(specifiers));
1914 parse_declaration_specifiers(&specifiers);
1916 parse_struct_declarators(&specifiers);
1918 if(token.type == T_EOF) {
1919 parse_error("unexpected error while parsing struct");
1924 static void parse_declaration(void)
1926 source_position_t source_position = token.source_position;
1928 declaration_specifiers_t specifiers;
1929 memset(&specifiers, 0, sizeof(specifiers));
1930 parse_declaration_specifiers(&specifiers);
1932 if(token.type == ';') {
1935 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1937 declaration->type = specifiers.type;
1938 declaration->storage_class = specifiers.storage_class;
1939 declaration->source_position = source_position;
1940 record_declaration(declaration);
1943 parse_init_declarators(&specifiers);
1946 static type_t *parse_typename(void)
1948 declaration_specifiers_t specifiers;
1949 memset(&specifiers, 0, sizeof(specifiers));
1950 parse_declaration_specifiers(&specifiers);
1951 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1952 /* TODO: improve error message, user does probably not know what a
1953 * storage class is...
1955 parse_error("typename may not have a storage class");
1958 type_t *result = parse_abstract_declarator(specifiers.type);
1966 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1967 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1968 expression_t *left);
1970 typedef struct expression_parser_function_t expression_parser_function_t;
1971 struct expression_parser_function_t {
1972 unsigned precedence;
1973 parse_expression_function parser;
1974 unsigned infix_precedence;
1975 parse_expression_infix_function infix_parser;
1978 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1980 static expression_t *expected_expression_error(void)
1982 parser_print_error_prefix();
1983 fprintf(stderr, "expected expression, got token ");
1984 print_token(stderr, & token);
1985 fprintf(stderr, "\n");
1987 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1988 expression->type = EXPR_INVALID;
1994 static expression_t *parse_string_const(void)
1996 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1998 cnst->expression.type = EXPR_STRING_LITERAL;
1999 cnst->expression.datatype = type_string;
2000 cnst->value = parse_string_literals();
2002 return (expression_t*) cnst;
2005 static expression_t *parse_int_const(void)
2007 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2009 cnst->expression.type = EXPR_CONST;
2010 cnst->expression.datatype = type_int;
2011 cnst->v.int_value = token.v.intvalue;
2015 return (expression_t*) cnst;
2018 static expression_t *parse_float_const(void)
2020 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2022 cnst->expression.type = EXPR_CONST;
2023 cnst->expression.datatype = type_double;
2024 cnst->v.float_value = token.v.floatvalue;
2028 return (expression_t*) cnst;
2031 static declaration_t *create_implicit_function(symbol_t *symbol,
2032 const source_position_t source_position)
2034 function_type_t *function_type
2035 = allocate_type_zero(sizeof(function_type[0]));
2037 function_type->type.type = TYPE_FUNCTION;
2038 function_type->result_type = type_int;
2039 function_type->unspecified_parameters = true;
2041 type_t *type = typehash_insert((type_t*) function_type);
2042 if(type != (type_t*) function_type) {
2043 free_type(function_type);
2046 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2048 declaration->storage_class = STORAGE_CLASS_EXTERN;
2049 declaration->type = type;
2050 declaration->symbol = symbol;
2051 declaration->source_position = source_position;
2053 /* prepend the implicit definition to the global context
2054 * this is safe since the symbol wasn't declared as anything else yet
2056 assert(symbol->declaration == NULL);
2058 context_t *last_context = context;
2059 context = global_context;
2061 environment_push(declaration);
2062 declaration->next = context->declarations;
2063 context->declarations = declaration;
2065 context = last_context;
2070 static expression_t *parse_reference(void)
2072 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2074 ref->expression.type = EXPR_REFERENCE;
2075 ref->symbol = token.v.symbol;
2077 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2079 source_position_t source_position = token.source_position;
2082 if(declaration == NULL) {
2084 /* an implicitely defined function */
2085 if(token.type == '(') {
2086 parser_print_prefix_pos(token.source_position);
2087 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2088 ref->symbol->string);
2090 declaration = create_implicit_function(ref->symbol,
2095 parser_print_error_prefix();
2096 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2097 return (expression_t*) ref;
2101 ref->declaration = declaration;
2102 ref->expression.datatype = declaration->type;
2104 return (expression_t*) ref;
2107 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2111 /* TODO check if explicit cast is allowed and issue warnings/errors */
2114 static expression_t *parse_cast(void)
2116 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2118 cast->expression.type = EXPR_UNARY;
2119 cast->type = UNEXPR_CAST;
2120 cast->expression.source_position = token.source_position;
2122 type_t *type = parse_typename();
2125 expression_t *value = parse_sub_expression(20);
2127 check_cast_allowed(value, type);
2129 cast->expression.datatype = type;
2130 cast->value = value;
2132 return (expression_t*) cast;
2135 static expression_t *parse_statement_expression(void)
2137 statement_expression_t *expression
2138 = allocate_ast_zero(sizeof(expression[0]));
2139 expression->expression.type = EXPR_STATEMENT;
2141 statement_t *statement = parse_compound_statement();
2142 expression->statement = statement;
2143 if(statement == NULL) {
2148 assert(statement->type == STATEMENT_COMPOUND);
2149 compound_statement_t *compound_statement
2150 = (compound_statement_t*) statement;
2152 /* find last statement and use it's type */
2153 const statement_t *last_statement = NULL;
2154 const statement_t *iter = compound_statement->statements;
2155 for( ; iter != NULL; iter = iter->next) {
2156 last_statement = iter;
2159 if(last_statement->type == STATEMENT_EXPRESSION) {
2160 const expression_statement_t *expression_statement =
2161 (const expression_statement_t*) last_statement;
2162 expression->expression.datatype
2163 = expression_statement->expression->datatype;
2165 expression->expression.datatype = type_void;
2170 return (expression_t*) expression;
2173 static expression_t *parse_brace_expression(void)
2177 switch(token.type) {
2179 /* gcc extension: a stement expression */
2180 return parse_statement_expression();
2184 return parse_cast();
2186 if(is_typedef_symbol(token.v.symbol)) {
2187 return parse_cast();
2191 expression_t *result = parse_expression();
2197 static expression_t *parse_function_keyword(void)
2199 eat(T___FUNCTION__);
2202 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2203 expression->expression.type = EXPR_FUNCTION;
2204 expression->expression.datatype = type_string;
2205 expression->value = "TODO: FUNCTION";
2207 return (expression_t*) expression;
2210 static expression_t *parse_pretty_function_keyword(void)
2212 eat(T___PRETTY_FUNCTION__);
2215 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2216 expression->expression.type = EXPR_PRETTY_FUNCTION;
2217 expression->expression.datatype = type_string;
2218 expression->value = "TODO: PRETTY FUNCTION";
2220 return (expression_t*) expression;
2223 static designator_t *parse_designator(void)
2225 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2227 if(token.type != T_IDENTIFIER) {
2228 parse_error_expected("problem while parsing member designator",
2233 result->symbol = token.v.symbol;
2236 designator_t *last_designator = result;
2238 if(token.type == '.') {
2240 if(token.type != T_IDENTIFIER) {
2241 parse_error_expected("problem while parsing member designator",
2246 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2247 designator->symbol = token.v.symbol;
2250 last_designator->next = designator;
2251 last_designator = designator;
2254 if(token.type == '[') {
2256 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2257 designator->array_access = parse_expression();
2258 if(designator->array_access == NULL) {
2264 last_designator->next = designator;
2265 last_designator = designator;
2274 static expression_t *parse_offsetof(void)
2276 eat(T___builtin_offsetof);
2278 offsetof_expression_t *expression
2279 = allocate_ast_zero(sizeof(expression[0]));
2280 expression->expression.type = EXPR_OFFSETOF;
2281 expression->expression.datatype = type_size_t;
2284 expression->type = parse_typename();
2286 expression->designator = parse_designator();
2289 return (expression_t*) expression;
2292 static expression_t *parse_va_arg(void)
2294 eat(T___builtin_va_arg);
2296 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2297 expression->expression.type = EXPR_VA_ARG;
2300 expression->arg = parse_assignment_expression();
2302 expression->expression.datatype = parse_typename();
2305 return (expression_t*) expression;
2308 static expression_t *parse_builtin_symbol(void)
2310 builtin_symbol_expression_t *expression
2311 = allocate_ast_zero(sizeof(expression[0]));
2312 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2314 /* TODO: set datatype */
2316 expression->symbol = token.v.symbol;
2320 return (expression_t*) expression;
2323 static expression_t *parse_primary_expression(void)
2325 switch(token.type) {
2327 return parse_int_const();
2328 case T_FLOATINGPOINT:
2329 return parse_float_const();
2330 case T_STRING_LITERAL:
2331 return parse_string_const();
2333 return parse_reference();
2334 case T___FUNCTION__:
2335 return parse_function_keyword();
2336 case T___PRETTY_FUNCTION__:
2337 return parse_pretty_function_keyword();
2338 case T___builtin_offsetof:
2339 return parse_offsetof();
2340 case T___builtin_va_arg:
2341 return parse_va_arg();
2342 case T___builtin_expect:
2343 case T___builtin_va_start:
2344 case T___builtin_va_end:
2345 return parse_builtin_symbol();
2348 return parse_brace_expression();
2351 parser_print_error_prefix();
2352 fprintf(stderr, "unexpected token ");
2353 print_token(stderr, &token);
2354 fprintf(stderr, "\n");
2357 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2358 expression->type = EXPR_INVALID;
2359 expression->datatype = type_void;
2364 static expression_t *parse_array_expression(unsigned precedence,
2365 expression_t *array_ref)
2371 array_access_expression_t *array_access
2372 = allocate_ast_zero(sizeof(array_access[0]));
2374 array_access->expression.type = EXPR_ARRAY_ACCESS;
2375 array_access->array_ref = array_ref;
2376 array_access->index = parse_expression();
2378 type_t *type = array_ref->datatype;
2380 if(type->type == TYPE_POINTER) {
2381 pointer_type_t *pointer = (pointer_type_t*) type;
2382 array_access->expression.datatype = pointer->points_to;
2383 } else if(type->type == TYPE_ARRAY) {
2384 array_type_t *array_type = (array_type_t*) type;
2385 array_access->expression.datatype = array_type->element_type;
2387 parser_print_error_prefix();
2388 fprintf(stderr, "array access on object with non-pointer type ");
2390 fprintf(stderr, "\n");
2394 if(token.type != ']') {
2395 parse_error_expected("Problem while parsing array access", ']', 0);
2396 return (expression_t*) array_access;
2400 return (expression_t*) array_access;
2403 static bool is_declaration_specifier(const token_t *token,
2404 bool only_type_specifiers)
2406 switch(token->type) {
2410 return is_typedef_symbol(token->v.symbol);
2413 if(only_type_specifiers)
2422 static expression_t *parse_sizeof(unsigned precedence)
2426 sizeof_expression_t *sizeof_expression
2427 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2428 sizeof_expression->expression.type = EXPR_SIZEOF;
2429 sizeof_expression->expression.datatype = type_size_t;
2431 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2433 sizeof_expression->type = parse_typename();
2436 expression_t *expression = parse_sub_expression(precedence);
2437 sizeof_expression->type = expression->datatype;
2438 sizeof_expression->size_expression = expression;
2441 return (expression_t*) sizeof_expression;
2444 static expression_t *parse_select_expression(unsigned precedence,
2445 expression_t *compound)
2449 assert(token.type == '.' || token.type == T_MINUSGREATER);
2452 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2454 select->expression.type = EXPR_SELECT;
2455 select->compound = compound;
2457 /* TODO: datatype */
2459 if(token.type != T_IDENTIFIER) {
2460 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2461 return (expression_t*) select;
2463 select->symbol = token.v.symbol;
2466 return (expression_t*) select;
2469 static expression_t *parse_call_expression(unsigned precedence,
2470 expression_t *expression)
2473 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2474 call->expression.type = EXPR_CALL;
2475 call->function = expression;
2477 function_type_t *function_type;
2478 type_t *type = expression->datatype;
2479 if(type->type != TYPE_FUNCTION) {
2480 /* TODO calling pointers to functions is ok */
2481 parser_print_error_prefix();
2482 fputs("called object '", stderr);
2483 print_expression(expression);
2484 fputs("' (type ", stderr);
2486 fputs("is not a function\n", stderr);
2488 function_type = NULL;
2489 call->expression.datatype = NULL;
2491 function_type = (function_type_t*) type;
2492 call->expression.datatype = function_type->result_type;
2495 /* parse arguments */
2498 if(token.type != ')') {
2499 call_argument_t *last_argument = NULL;
2502 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2504 argument->expression = parse_assignment_expression();
2505 if(last_argument == NULL) {
2506 call->arguments = argument;
2508 last_argument->next = argument;
2510 last_argument = argument;
2512 if(token.type != ',')
2519 if(function_type != NULL) {
2520 function_parameter_t *parameter = function_type->parameters;
2521 call_argument_t *argument = call->arguments;
2522 for( ; parameter != NULL && argument != NULL;
2523 parameter = parameter->next, argument = argument->next) {
2524 type_t *expected_type = parameter->type;
2525 /* TODO report context in error messages */
2526 argument->expression = create_implicit_cast(argument->expression,
2529 /* too few parameters */
2530 if(parameter != NULL) {
2531 parser_print_error_prefix();
2532 fprintf(stderr, "too few arguments to function '");
2533 print_expression(expression);
2534 fprintf(stderr, "'\n");
2535 } else if(argument != NULL) {
2536 /* too many parameters */
2537 if(!function_type->variadic
2538 && !function_type->unspecified_parameters) {
2539 parser_print_error_prefix();
2540 fprintf(stderr, "too many arguments to function '");
2541 print_expression(expression);
2542 fprintf(stderr, "'\n");
2544 /* do default promotion */
2545 for( ; argument != NULL; argument = argument->next) {
2546 type_t *type = argument->expression->datatype;
2551 if(is_type_integer(type)) {
2552 type = promote_integer(type);
2553 } else if(type == type_float) {
2556 argument->expression
2557 = create_implicit_cast(argument->expression, type);
2563 return (expression_t*) call;
2566 static type_t *get_type_after_conversion(const type_t *type1,
2567 const type_t *type2)
2571 return (type_t*) type1;
2574 static expression_t *parse_conditional_expression(unsigned precedence,
2575 expression_t *expression)
2579 conditional_expression_t *conditional
2580 = allocate_ast_zero(sizeof(conditional[0]));
2581 conditional->expression.type = EXPR_CONDITIONAL;
2582 conditional->condition = expression;
2585 type_t *condition_type = conditional->condition->datatype;
2586 if(condition_type != NULL) {
2587 if(!is_type_scalar(condition_type)) {
2588 type_error("expected a scalar type", expression->source_position,
2593 conditional->true_expression = parse_expression();
2595 conditional->false_expression = parse_sub_expression(precedence);
2597 type_t *true_type = conditional->true_expression->datatype;
2598 if(true_type == NULL)
2599 return (expression_t*) conditional;
2600 type_t *false_type = conditional->false_expression->datatype;
2601 if(false_type == NULL)
2602 return (expression_t*) conditional;
2605 if(true_type == false_type) {
2606 conditional->expression.datatype = true_type;
2607 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2608 type_t *result = get_type_after_conversion(true_type, false_type);
2609 /* TODO: create implicit convs if necessary */
2610 conditional->expression.datatype = result;
2611 } else if(true_type->type == TYPE_POINTER &&
2612 false_type->type == TYPE_POINTER &&
2613 true /* TODO compatible points_to types */) {
2615 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2616 || (is_null_ptr_const(false_type) &&
2617 true_type->type == TYPE_POINTER) TODO*/ false) {
2619 } else if(/* 1 is pointer to object type, other is void* */ false) {
2622 type_error_incompatible("problem while parsing conditional",
2623 expression->source_position, true_type,
2627 return (expression_t*) conditional;
2630 static expression_t *parse_extension(unsigned precedence)
2632 eat(T___extension__);
2634 /* TODO enable extensions */
2636 return parse_sub_expression(precedence);
2639 static void semantic_incdec(unary_expression_t *expression)
2641 type_t *orig_type = expression->value->datatype;
2642 if(orig_type == NULL)
2645 type_t *type = skip_typeref(orig_type);
2646 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
2647 /* TODO: improve error message */
2648 parser_print_error_prefix();
2649 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
2653 expression->expression.datatype = orig_type;
2656 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
2658 type_t *orig_type = expression->value->datatype;
2659 if(orig_type == NULL)
2662 type_t *type = skip_typeref(orig_type);
2663 if(!is_type_arithmetic(type)) {
2664 /* TODO: improve error message */
2665 parser_print_error_prefix();
2666 fprintf(stderr, "operation needs an arithmetic type\n");
2670 expression->expression.datatype = orig_type;
2673 static void semantic_dereference(unary_expression_t *expression)
2675 type_t *orig_type = expression->value->datatype;
2676 if(orig_type == NULL)
2679 type_t *type = skip_typeref(orig_type);
2680 if(type->type != TYPE_POINTER) {
2681 /* TODO: improve error message */
2682 parser_print_error_prefix();
2683 fprintf(stderr, "operation needs a pointer type\n");
2687 pointer_type_t *pointer_type = (pointer_type_t*) type;
2688 expression->expression.datatype = pointer_type->points_to;
2691 static void semantic_take_addr(unary_expression_t *expression)
2693 type_t *orig_type = expression->value->datatype;
2694 if(orig_type == NULL)
2697 expression->expression.datatype = make_pointer_type(orig_type, 0);
2700 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
2701 static expression_t *parse_##unexpression_type(unsigned precedence) \
2705 unary_expression_t *unary_expression \
2706 = allocate_ast_zero(sizeof(unary_expression[0])); \
2707 unary_expression->expression.type = EXPR_UNARY; \
2708 unary_expression->type = unexpression_type; \
2709 unary_expression->value = parse_sub_expression(precedence); \
2711 sfunc(unary_expression); \
2713 return (expression_t*) unary_expression; \
2716 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
2717 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
2718 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_arithmetic)
2719 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
2720 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
2721 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2722 semantic_unexpr_arithmetic)
2723 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2725 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2728 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2730 static expression_t *parse_##unexpression_type(unsigned precedence, \
2731 expression_t *left) \
2733 (void) precedence; \
2736 unary_expression_t *unary_expression \
2737 = allocate_ast_zero(sizeof(unary_expression[0])); \
2738 unary_expression->expression.type = EXPR_UNARY; \
2739 unary_expression->type = unexpression_type; \
2740 unary_expression->value = left; \
2742 sfunc(unary_expression); \
2744 return (expression_t*) unary_expression; \
2747 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2749 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2752 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
2754 /* TODO: handle complex + imaginary types */
2756 /* § 6.3.1.8 Usual arithmetic conversions */
2757 if(type_left == type_long_double || type_right == type_long_double) {
2758 return type_long_double;
2759 } else if(type_left == type_double || type_right == type_double) {
2761 } else if(type_left == type_float || type_right == type_float) {
2765 type_right = promote_integer(type_right);
2766 type_left = promote_integer(type_left);
2768 if(type_left == type_right)
2771 bool signed_left = is_type_signed(type_left);
2772 bool signed_right = is_type_signed(type_right);
2773 if(get_rank(type_left) < get_rank(type_right)) {
2774 if(signed_left == signed_right || !signed_right) {
2780 if(signed_left == signed_right || !signed_left) {
2788 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2790 expression_t *left = expression->left;
2791 expression_t *right = expression->right;
2792 type_t *orig_type_left = left->datatype;
2793 type_t *orig_type_right = right->datatype;
2795 if(orig_type_left == NULL || orig_type_right == NULL)
2798 type_t *type_left = skip_typeref(orig_type_left);
2799 type_t *type_right = skip_typeref(orig_type_right);
2801 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2802 /* TODO: improve error message */
2803 parser_print_error_prefix();
2804 fprintf(stderr, "operation needs arithmetic types\n");
2808 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2809 expression->left = create_implicit_cast(left, arithmetic_type);
2810 expression->right = create_implicit_cast(right, arithmetic_type);
2811 expression->expression.datatype = arithmetic_type;
2814 static void semantic_add(binary_expression_t *expression)
2816 expression_t *left = expression->left;
2817 expression_t *right = expression->right;
2818 type_t *orig_type_left = left->datatype;
2819 type_t *orig_type_right = right->datatype;
2821 if(orig_type_left == NULL || orig_type_right == NULL)
2824 type_t *type_left = skip_typeref(orig_type_left);
2825 type_t *type_right = skip_typeref(orig_type_right);
2828 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2829 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2830 expression->left = create_implicit_cast(left, arithmetic_type);
2831 expression->right = create_implicit_cast(right, arithmetic_type);
2832 expression->expression.datatype = arithmetic_type;
2834 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2835 expression->expression.datatype = type_left;
2836 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
2837 expression->expression.datatype = type_right;
2839 parser_print_error_prefix();
2840 fprintf(stderr, "invalid operands to binary + (");
2841 print_type(orig_type_left);
2842 fprintf(stderr, ", ");
2843 print_type(orig_type_right);
2844 fprintf(stderr, ")\n");
2848 static void semantic_sub(binary_expression_t *expression)
2850 expression_t *left = expression->left;
2851 expression_t *right = expression->right;
2852 type_t *orig_type_left = left->datatype;
2853 type_t *orig_type_right = right->datatype;
2855 if(orig_type_left == NULL || orig_type_right == NULL)
2858 type_t *type_left = skip_typeref(orig_type_left);
2859 type_t *type_right = skip_typeref(orig_type_right);
2862 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2863 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2864 expression->left = create_implicit_cast(left, arithmetic_type);
2865 expression->right = create_implicit_cast(right, arithmetic_type);
2866 expression->expression.datatype = arithmetic_type;
2868 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2869 expression->expression.datatype = type_left;
2870 } else if(type_left->type == TYPE_POINTER &&
2871 type_right->type == TYPE_POINTER) {
2872 if(!pointers_compatible(type_left, type_right)) {
2873 parser_print_error_prefix();
2874 fprintf(stderr, "pointers to incompatible objects to binary - (");
2875 print_type(orig_type_left);
2876 fprintf(stderr, ", ");
2877 print_type(orig_type_right);
2878 fprintf(stderr, ")\n");
2880 expression->expression.datatype = type_ptrdiff_t;
2883 parser_print_error_prefix();
2884 fprintf(stderr, "invalid operands to binary - (");
2885 print_type(orig_type_left);
2886 fprintf(stderr, ", ");
2887 print_type(orig_type_right);
2888 fprintf(stderr, ")\n");
2892 static void semantic_comparison(binary_expression_t *expression)
2894 expression_t *left = expression->left;
2895 expression_t *right = expression->right;
2896 type_t *orig_type_left = left->datatype;
2897 type_t *orig_type_right = right->datatype;
2899 if(orig_type_left == NULL || orig_type_right == NULL)
2902 type_t *type_left = skip_typeref(orig_type_left);
2903 type_t *type_right = skip_typeref(orig_type_right);
2905 /* TODO non-arithmetic types */
2906 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2907 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2908 expression->left = create_implicit_cast(left, arithmetic_type);
2909 expression->right = create_implicit_cast(right, arithmetic_type);
2910 expression->expression.datatype = arithmetic_type;
2912 expression->expression.datatype = type_int;
2915 static void semantic_arithmetic_assign(binary_expression_t *expression)
2917 expression_t *left = expression->left;
2918 expression_t *right = expression->right;
2919 type_t *orig_type_left = left->datatype;
2920 type_t *orig_type_right = right->datatype;
2922 if(orig_type_left == NULL || orig_type_right == NULL)
2925 type_t *type_left = skip_typeref(orig_type_left);
2926 type_t *type_right = skip_typeref(orig_type_right);
2928 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2929 /* TODO: improve error message */
2930 parser_print_error_prefix();
2931 fprintf(stderr, "operation needs arithmetic types\n");
2935 /* combined instructions are tricky. We can't create an implicit cast on
2936 * the left side, because we need the uncasted form for the store.
2937 * The ast2firm pass has to know that left_type must be right_type
2938 * for the arithmeitc operation and create a cast by itself */
2939 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2940 expression->right = create_implicit_cast(right, arithmetic_type);
2941 expression->expression.datatype = type_left;
2944 static void semantic_logical_op(binary_expression_t *expression)
2946 expression_t *left = expression->left;
2947 expression_t *right = expression->right;
2948 type_t *orig_type_left = left->datatype;
2949 type_t *orig_type_right = right->datatype;
2951 if(orig_type_left == NULL || orig_type_right == NULL)
2954 type_t *type_left = skip_typeref(orig_type_left);
2955 type_t *type_right = skip_typeref(orig_type_right);
2957 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2958 /* TODO: improve error message */
2959 parser_print_error_prefix();
2960 fprintf(stderr, "operation needs arithmetic types\n");
2964 expression->expression.datatype = type_int;
2967 static void semantic_binexpr_assign(binary_expression_t *expression)
2969 expression_t *left = expression->left;
2970 type_t *type_left = left->datatype;
2972 if(type_left != NULL) {
2973 semantic_assign(type_left, &expression->right, "assignment");
2976 expression->expression.datatype = type_left;
2979 static void semantic_comma(binary_expression_t *expression)
2981 expression->expression.datatype = expression->right->datatype;
2984 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
2985 static expression_t *parse_##binexpression_type(unsigned precedence, \
2986 expression_t *left) \
2990 expression_t *right = parse_sub_expression(precedence + lr); \
2992 binary_expression_t *binexpr \
2993 = allocate_ast_zero(sizeof(binexpr[0])); \
2994 binexpr->expression.type = EXPR_BINARY; \
2995 binexpr->type = binexpression_type; \
2996 binexpr->left = left; \
2997 binexpr->right = right; \
3000 return (expression_t*) binexpr; \
3003 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3004 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3005 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3006 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3007 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3008 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3009 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3010 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3011 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3012 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3013 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3014 semantic_comparison, 1)
3015 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3016 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3017 semantic_comparison, 1)
3018 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3019 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3020 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3021 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3022 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3023 /* TODO shift has a bit special semantic */
3024 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3025 semantic_binexpr_arithmetic, 1)
3026 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3027 semantic_binexpr_arithmetic, 1)
3028 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3029 semantic_arithmetic_assign, 0)
3030 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3031 semantic_arithmetic_assign, 0)
3032 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3033 semantic_arithmetic_assign, 0)
3034 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3035 semantic_arithmetic_assign, 0)
3036 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3037 semantic_arithmetic_assign, 0)
3038 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3039 semantic_arithmetic_assign, 0)
3040 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3041 semantic_arithmetic_assign, 0)
3042 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3043 semantic_arithmetic_assign, 0)
3044 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3045 semantic_arithmetic_assign, 0)
3046 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3047 semantic_arithmetic_assign, 0)
3049 static expression_t *parse_sub_expression(unsigned precedence)
3051 if(token.type < 0) {
3052 return expected_expression_error();
3055 expression_parser_function_t *parser
3056 = &expression_parsers[token.type];
3057 source_position_t source_position = token.source_position;
3060 if(parser->parser != NULL) {
3061 left = parser->parser(parser->precedence);
3063 left = parse_primary_expression();
3065 assert(left != NULL);
3066 left->source_position = source_position;
3069 if(token.type < 0) {
3070 return expected_expression_error();
3073 parser = &expression_parsers[token.type];
3074 if(parser->infix_parser == NULL)
3076 if(parser->infix_precedence < precedence)
3079 left = parser->infix_parser(parser->infix_precedence, left);
3081 assert(left != NULL);
3082 assert(left->type != EXPR_INVALID);
3083 left->source_position = source_position;
3089 static expression_t *parse_expression(void)
3091 return parse_sub_expression(1);
3096 static void register_expression_parser(parse_expression_function parser,
3097 int token_type, unsigned precedence)
3099 expression_parser_function_t *entry = &expression_parsers[token_type];
3101 if(entry->parser != NULL) {
3102 fprintf(stderr, "for token ");
3103 print_token_type(stderr, token_type);
3104 fprintf(stderr, "\n");
3105 panic("trying to register multiple expression parsers for a token");
3107 entry->parser = parser;
3108 entry->precedence = precedence;
3111 static void register_expression_infix_parser(
3112 parse_expression_infix_function parser, int token_type,
3113 unsigned precedence)
3115 expression_parser_function_t *entry = &expression_parsers[token_type];
3117 if(entry->infix_parser != NULL) {
3118 fprintf(stderr, "for token ");
3119 print_token_type(stderr, token_type);
3120 fprintf(stderr, "\n");
3121 panic("trying to register multiple infix expression parsers for a "
3124 entry->infix_parser = parser;
3125 entry->infix_precedence = precedence;
3128 static void init_expression_parsers(void)
3130 memset(&expression_parsers, 0, sizeof(expression_parsers));
3132 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3133 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3134 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3135 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3136 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3137 T_GREATERGREATER, 16);
3138 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3139 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3140 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3141 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3142 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3143 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3144 T_GREATEREQUAL, 14);
3145 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3146 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3147 T_EXCLAMATIONMARKEQUAL, 13);
3148 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3149 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3150 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3151 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3152 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3153 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3154 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3155 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3156 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3157 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3158 T_ASTERISKEQUAL, 2);
3159 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3160 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3162 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3163 T_LESSLESSEQUAL, 2);
3164 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3165 T_GREATERGREATEREQUAL, 2);
3166 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3168 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3170 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3173 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3175 register_expression_infix_parser(parse_array_expression, '[', 30);
3176 register_expression_infix_parser(parse_call_expression, '(', 30);
3177 register_expression_infix_parser(parse_select_expression, '.', 30);
3178 register_expression_infix_parser(parse_select_expression,
3179 T_MINUSGREATER, 30);
3180 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3182 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3185 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3186 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3187 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3188 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3189 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3190 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3191 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3192 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3193 register_expression_parser(parse_sizeof, T_sizeof, 25);
3194 register_expression_parser(parse_extension, T___extension__, 25);
3198 static statement_t *parse_case_statement(void)
3201 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3202 label->statement.type = STATEMENT_CASE_LABEL;
3203 label->statement.source_position = token.source_position;
3205 label->expression = parse_expression();
3208 label->statement.next = parse_statement();
3210 return (statement_t*) label;
3213 static statement_t *parse_default_statement(void)
3217 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3218 label->statement.type = STATEMENT_CASE_LABEL;
3219 label->statement.source_position = token.source_position;
3222 label->statement.next = parse_statement();
3224 return (statement_t*) label;
3227 static declaration_t *get_label(symbol_t *symbol)
3229 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3230 assert(current_function != NULL);
3231 /* if we found a label in the same function, then we already created the
3233 if(candidate != NULL
3234 && candidate->parent_context == ¤t_function->context) {
3238 /* otherwise we need to create a new one */
3239 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3240 declaration->namespace = NAMESPACE_LABEL;
3241 declaration->symbol = symbol;
3243 label_push(declaration);
3248 static statement_t *parse_label_statement(void)
3250 assert(token.type == T_IDENTIFIER);
3251 symbol_t *symbol = token.v.symbol;
3254 declaration_t *label = get_label(symbol);
3256 /* if source position is already set then the label is defined twice,
3257 * otherwise it was just mentioned in a goto so far */
3258 if(label->source_position.input_name != NULL) {
3259 parser_print_error_prefix();
3260 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3261 parser_print_error_prefix_pos(label->source_position);
3262 fprintf(stderr, "previous definition of '%s' was here\n",
3265 label->source_position = token.source_position;
3268 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3270 label_statement->statement.type = STATEMENT_LABEL;
3271 label_statement->statement.source_position = token.source_position;
3272 label_statement->label = label;
3276 if(token.type == '}') {
3277 parse_error("label at end of compound statement");
3278 return (statement_t*) label_statement;
3280 label_statement->label_statement = parse_statement();
3283 return (statement_t*) label_statement;
3286 static statement_t *parse_if(void)
3290 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3291 statement->statement.type = STATEMENT_IF;
3292 statement->statement.source_position = token.source_position;
3295 statement->condition = parse_expression();
3298 statement->true_statement = parse_statement();
3299 if(token.type == T_else) {
3301 statement->false_statement = parse_statement();
3304 return (statement_t*) statement;
3307 static statement_t *parse_switch(void)
3311 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3312 statement->statement.type = STATEMENT_SWITCH;
3313 statement->statement.source_position = token.source_position;
3316 statement->expression = parse_expression();
3318 statement->body = parse_statement();
3320 return (statement_t*) statement;
3323 static statement_t *parse_while(void)
3327 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3328 statement->statement.type = STATEMENT_WHILE;
3329 statement->statement.source_position = token.source_position;
3332 statement->condition = parse_expression();
3334 statement->body = parse_statement();
3336 return (statement_t*) statement;
3339 static statement_t *parse_do(void)
3343 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3344 statement->statement.type = STATEMENT_DO_WHILE;
3345 statement->statement.source_position = token.source_position;
3347 statement->body = parse_statement();
3350 statement->condition = parse_expression();
3354 return (statement_t*) statement;
3357 static statement_t *parse_for(void)
3361 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3362 statement->statement.type = STATEMENT_FOR;
3363 statement->statement.source_position = token.source_position;
3367 int top = environment_top();
3368 context_t *last_context = context;
3369 set_context(&statement->context);
3371 if(token.type != ';') {
3372 if(is_declaration_specifier(&token, false)) {
3373 parse_declaration();
3375 statement->initialisation = parse_expression();
3382 if(token.type != ';') {
3383 statement->condition = parse_expression();
3386 if(token.type != ')') {
3387 statement->step = parse_expression();
3390 statement->body = parse_statement();
3392 assert(context == &statement->context);
3393 set_context(last_context);
3394 environment_pop_to(top);
3396 return (statement_t*) statement;
3399 static statement_t *parse_goto(void)
3403 if(token.type != T_IDENTIFIER) {
3404 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
3408 symbol_t *symbol = token.v.symbol;
3411 declaration_t *label = get_label(symbol);
3413 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3415 statement->statement.type = STATEMENT_GOTO;
3416 statement->statement.source_position = token.source_position;
3418 statement->label = label;
3422 return (statement_t*) statement;
3425 static statement_t *parse_continue(void)
3430 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3431 statement->type = STATEMENT_CONTINUE;
3432 statement->source_position = token.source_position;
3437 static statement_t *parse_break(void)
3442 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3443 statement->type = STATEMENT_BREAK;
3444 statement->source_position = token.source_position;
3449 static statement_t *parse_return(void)
3453 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3455 statement->statement.type = STATEMENT_RETURN;
3456 statement->statement.source_position = token.source_position;
3458 assert(current_function->type->type == TYPE_FUNCTION);
3459 function_type_t *function_type = (function_type_t*) current_function->type;
3460 type_t *return_type = function_type->result_type;
3462 expression_t *return_value;
3463 if(token.type != ';') {
3464 return_value = parse_expression();
3466 if(return_type == type_void && return_value->datatype != type_void) {
3467 parse_warning("'return' with a value, in function returning void");
3468 return_value = NULL;
3470 if(return_type != NULL) {
3471 semantic_assign(return_type, &return_value, "'return'");
3475 return_value = NULL;
3476 if(return_type != type_void) {
3477 parse_warning("'return' without value, in function returning "
3481 statement->return_value = return_value;
3485 return (statement_t*) statement;
3488 static statement_t *parse_declaration_statement(void)
3490 declaration_t *before = last_declaration;
3492 declaration_statement_t *statement
3493 = allocate_ast_zero(sizeof(statement[0]));
3494 statement->statement.type = STATEMENT_DECLARATION;
3495 statement->statement.source_position = token.source_position;
3497 declaration_specifiers_t specifiers;
3498 memset(&specifiers, 0, sizeof(specifiers));
3499 parse_declaration_specifiers(&specifiers);
3501 if(token.type == ';') {
3504 parse_init_declarators(&specifiers);
3507 if(before == NULL) {
3508 statement->declarations_begin = context->declarations;
3510 statement->declarations_begin = before->next;
3512 statement->declarations_end = last_declaration;
3514 return (statement_t*) statement;
3517 static statement_t *parse_expression_statement(void)
3519 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3520 statement->statement.type = STATEMENT_EXPRESSION;
3521 statement->statement.source_position = token.source_position;
3523 statement->expression = parse_expression();
3527 return (statement_t*) statement;
3530 static statement_t *parse_statement(void)
3532 statement_t *statement = NULL;
3534 /* declaration or statement */
3535 switch(token.type) {
3537 statement = parse_case_statement();
3541 statement = parse_default_statement();
3545 statement = parse_compound_statement();
3549 statement = parse_if();
3553 statement = parse_switch();
3557 statement = parse_while();
3561 statement = parse_do();
3565 statement = parse_for();
3569 statement = parse_goto();
3573 statement = parse_continue();
3577 statement = parse_break();
3581 statement = parse_return();
3590 if(look_ahead(1)->type == ':') {
3591 statement = parse_label_statement();
3595 if(is_typedef_symbol(token.v.symbol)) {
3596 statement = parse_declaration_statement();
3600 statement = parse_expression_statement();
3603 case T___extension__:
3604 /* this can be a prefix to a declaration or an expression statement */
3605 /* we simply eat it now and parse the rest with tail recursion */
3608 } while(token.type == T___extension__);
3609 statement = parse_statement();
3613 statement = parse_declaration_statement();
3617 statement = parse_expression_statement();
3621 assert(statement == NULL || statement->source_position.input_name != NULL);
3626 static statement_t *parse_compound_statement(void)
3628 compound_statement_t *compound_statement
3629 = allocate_ast_zero(sizeof(compound_statement[0]));
3630 compound_statement->statement.type = STATEMENT_COMPOUND;
3631 compound_statement->statement.source_position = token.source_position;
3635 int top = environment_top();
3636 context_t *last_context = context;
3637 set_context(&compound_statement->context);
3639 statement_t *last_statement = NULL;
3641 while(token.type != '}' && token.type != T_EOF) {
3642 statement_t *statement = parse_statement();
3643 if(statement == NULL)
3646 if(last_statement != NULL) {
3647 last_statement->next = statement;
3649 compound_statement->statements = statement;
3652 while(statement->next != NULL)
3653 statement = statement->next;
3655 last_statement = statement;
3658 if(token.type != '}') {
3659 parser_print_error_prefix_pos(
3660 compound_statement->statement.source_position);
3661 fprintf(stderr, "end of file while looking for closing '}'\n");
3665 assert(context == &compound_statement->context);
3666 set_context(last_context);
3667 environment_pop_to(top);
3669 return (statement_t*) compound_statement;
3672 static translation_unit_t *parse_translation_unit(void)
3674 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3676 assert(global_context == NULL);
3677 global_context = &unit->context;
3679 assert(context == NULL);
3680 set_context(&unit->context);
3682 while(token.type != T_EOF) {
3683 parse_declaration();
3686 assert(context == &unit->context);
3688 last_declaration = NULL;
3690 assert(global_context == &unit->context);
3691 global_context = NULL;
3696 translation_unit_t *parse(void)
3698 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3699 label_stack = NEW_ARR_F(stack_entry_t, 0);
3700 found_error = false;
3702 type_set_output(stderr);
3703 ast_set_output(stderr);
3705 lookahead_bufpos = 0;
3706 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3709 translation_unit_t *unit = parse_translation_unit();
3711 DEL_ARR_F(environment_stack);
3712 DEL_ARR_F(label_stack);
3720 void init_parser(void)
3722 init_expression_parsers();
3723 obstack_init(&temp_obst);
3725 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3726 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3727 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
3728 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3729 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
3730 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
3731 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
3732 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3733 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3734 type_string = make_pointer_type(type_const_char, 0);
3737 void exit_parser(void)
3739 obstack_free(&temp_obst, NULL);