more work on semantic analysis
authorMatthias Braun <matze@braunis.de>
Wed, 14 Nov 2007 01:26:04 +0000 (01:26 +0000)
committerMatthias Braun <matze@braunis.de>
Wed, 14 Nov 2007 01:26:04 +0000 (01:26 +0000)
[r18387]

TODO
ast2firm.c
main.c
parser.c
type.c
type.h
type_t.h

diff --git a/TODO b/TODO
index bb15cc2..ab1a85d 100644 (file)
--- a/TODO
+++ b/TODO
@@ -6,6 +6,8 @@ Lexer:
 - proper handling of different file encodings
 
 Parser:
+- the expect macros abort functions directly. This leads to some functions
+  not resetting the current context properly (parse_for)
 - proper handling of function pointer types
 - outermost typequalifiers can differ between function declarations and
   implementations...
index 24c8955..8fc001f 100644 (file)
@@ -114,33 +114,6 @@ static ident *unique_ident(const char *tag)
        return new_id_from_str(buf);
 }
 
-static type_t *skip_typeref(type_t *type)
-{
-       while(1) {
-               switch(type->type) {
-               case TYPE_TYPEDEF: {
-                       const typedef_type_t *typedef_type = (const typedef_type_t*) type;
-                       type = typedef_type->declaration->type;
-                       continue;
-               }
-               case TYPE_TYPEOF: {
-                       const typeof_type_t *typeof_type = (const typeof_type_t *) type;
-                       if(typeof_type->typeof_type != NULL) {
-                               type = typeof_type->typeof_type;
-                       } else {
-                               type = typeof_type->expression->datatype;
-                       }
-                       continue;
-               }
-               default:
-                       break;
-               }
-               break;
-       }
-
-       return type;
-}
-
 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
 {
        switch(atomic_type->atype) {
@@ -184,8 +157,8 @@ static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
                break;
 #endif
        case ATOMIC_TYPE_VOID:
-               panic("tried to get mode from void type");
-               break;
+               /* firm has no real void... */
+               return mode_Is;
        case ATOMIC_TYPE_INVALID:
                break;
        }
@@ -389,6 +362,9 @@ static ir_type *create_struct_type(type2firm_env_t *env, compound_type_t *type)
        int offset    = 0;
        declaration_t *entry = type->declaration->context.declarations;
        for( ; entry != NULL; entry = entry->next) {
+               if(entry->namespace != NAMESPACE_NORMAL)
+                       continue;
+
                ident       *ident         = new_id_from_str(entry->symbol->string);
                ir_type_ptr  entry_ir_type = _get_ir_type(env, entry->type);
 
@@ -439,6 +415,9 @@ static ir_type *create_union_type(type2firm_env_t *env, compound_type_t *type)
        int size      = 0;
        declaration_t *entry = declaration->context.declarations;
        for( ; entry != NULL; entry = entry->next) {
+               if(entry->namespace != NAMESPACE_NORMAL)
+                       continue;
+
                ident       *ident         = new_id_from_str(entry->symbol->string);
                ir_type_ptr  entry_ir_type = _get_ir_type(env, entry->type);
 
@@ -1656,6 +1635,9 @@ static void context_to_firm(context_t *context)
        for( ; declaration != NULL; declaration = declaration->next) {
                if(declaration->namespace != NAMESPACE_NORMAL)
                        continue;
+               if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
+                               || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
+                       continue;
 
                type_t *type = declaration->type;
                if(type->type == TYPE_FUNCTION) {
diff --git a/main.c b/main.c
index 1b1807f..c44fba6 100644 (file)
--- a/main.c
+++ b/main.c
@@ -230,6 +230,9 @@ int main(int argc, char **argv)
                get_output_name(outfname, sizeof(outfname), input, ".s");
 
                translation_unit_t *unit = do_parsing(input);
+               if(unit == NULL) {
+                       return 1;
+               }
                create_firm_prog(unit);
                emit(input, outfname);
                link(outfname, "a.out");
index 69dba39..c5673bd 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -25,21 +25,26 @@ typedef struct {
        unsigned short namespace;
 } stack_entry_t;
 
-static token_t               token;
-static token_t               lookahead_buffer[MAX_LOOKAHEAD];
-static int                   lookahead_bufpos;
-static stack_entry_t        *environment_stack = NULL;
-static context_t            *global_context    = NULL;
-static context_t            *context           = NULL;
-static declaration_t        *last_declaration  = NULL;
-static struct obstack        temp_obst;
-
-static type_t               *type_int        = NULL;
-static type_t               *type_double     = NULL;
-static type_t               *type_const_char = NULL;
-static type_t               *type_string     = NULL;
-static type_t               *type_void       = NULL;
-static type_t               *type_size_t     = NULL;
+static token_t         token;
+static token_t         lookahead_buffer[MAX_LOOKAHEAD];
+static int             lookahead_bufpos;
+static stack_entry_t  *environment_stack = NULL;
+static context_t      *global_context    = NULL;
+static context_t      *context           = NULL;
+static declaration_t  *last_declaration  = NULL;
+static declaration_t  *current_function  = NULL;
+static struct obstack  temp_obst;
+static bool            found_error;
+
+static type_t         *type_int         = NULL;
+static type_t         *type_uint        = NULL;
+static type_t         *type_long_double = NULL;
+static type_t         *type_double      = NULL;
+static type_t         *type_float       = NULL;
+static type_t         *type_const_char  = NULL;
+static type_t         *type_string      = NULL;
+static type_t         *type_void        = NULL;
+static type_t         *type_size_t      = NULL;
 
 static statement_t *parse_compound_statement(void);
 static statement_t *parse_statement(void);
@@ -156,6 +161,7 @@ static inline const token_t *look_ahead(int num)
 
 static void error(void)
 {
+       found_error = true;
 #ifdef ABORT_ON_ERROR
        abort();
 #endif
@@ -179,8 +185,7 @@ static void parser_print_error_prefix_pos(
 
 static void parser_print_error_prefix(void)
 {
-       parser_print_prefix_pos(token.source_position);
-       error();
+       parser_print_error_prefix_pos(token.source_position);
 }
 
 static void parse_error(const char *message)
@@ -189,7 +194,6 @@ static void parse_error(const char *message)
        fprintf(stderr, "parse error: %s\n", message);
 }
 
-__attribute__((unused))
 static void parse_warning(const char *message)
 {
        parser_print_prefix_pos(token.source_position);
@@ -888,6 +892,8 @@ static type_t *create_builtin_type(symbol_t *symbol)
        builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
        type->type.type      = TYPE_BUILTIN;
        type->symbol         = symbol;
+       /* TODO... */
+       type->real_type      = type_int;
 
        return (type_t*) type;
 }
@@ -1642,9 +1648,14 @@ static void parse_init_declarators(const declaration_specifiers_t *specifiers)
                        for( ; parameter != NULL; parameter = parameter->next) {
                                environment_push(parameter);
                        }
+                       declaration_t *old_current_function = current_function;
+                       current_function                    = declaration;
 
                        statement_t *statement = parse_compound_statement();
 
+                       assert(current_function == declaration);
+                       old_current_function = current_function;
+
                        assert(context == &declaration->context);
                        set_context(last_context);
                        environment_pop_to(top);
@@ -1893,7 +1904,7 @@ static void check_cast_allowed(expression_t *expression, type_t *dest_type)
 {
        (void) expression;
        (void) dest_type;
-       /* TODO check if cast is allowed and issue warnings/errors */
+       /* TODO check if explicit cast is allowed and issue warnings/errors */
 }
 
 static expression_t *parse_cast(void)
@@ -2243,9 +2254,24 @@ static expression_t *parse_call_expression(unsigned precedence,
 {
        (void) precedence;
        call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
+       call->expression.type   = EXPR_CALL;
+       call->function          = expression;
 
-       call->expression.type     = EXPR_CALL;
-       call->function            = expression;
+       type_t *type = expression->datatype;
+       if(type->type != TYPE_FUNCTION) {
+               /* TODO calling pointers to functions is ok */
+               parser_print_error_prefix();
+               fputs("called object '", stderr);
+               print_expression(expression);
+               fputs("' (type ", stderr);
+               print_type(type);
+               fputs("is not a function\n", stderr);
+
+               call->expression.datatype = NULL;
+       } else {
+               function_type_t *function_type = (function_type_t*) type;
+               call->expression.datatype = function_type->result_type;
+       }
 
        /* parse arguments */
        eat('(');
@@ -2271,26 +2297,6 @@ static expression_t *parse_call_expression(unsigned precedence,
        }
        expect(')');
 
-       type_t *type = expression->datatype;
-       if(type != NULL) {
-               /* we can call pointer to function */
-               if(type->type == TYPE_POINTER) {
-                       pointer_type_t *pointer = (pointer_type_t*) type;
-                       type = pointer->points_to;
-               }
-
-               if(type == NULL || type->type != TYPE_FUNCTION) {
-                       parser_print_error_prefix();
-                       fprintf(stderr, "expected a function type for call but found "
-                               "type ");
-                       print_type(expression->datatype);
-                       fprintf(stderr, "\n");
-               } else {
-                       function_type_t *function_type = (function_type_t*) type;
-                       call->expression.datatype      = function_type->result_type;
-               }
-       }
-
        return (expression_t*) call;
 }
 
@@ -2324,6 +2330,44 @@ static type_t *get_type_after_conversion(const type_t *type1,
        return (type_t*) type1;
 }
 
+static expression_t *create_cast_expression(expression_t *expression,
+                                            type_t *dest_type)
+{
+       unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
+
+       cast->expression.type     = EXPR_UNARY;
+       cast->type                = UNEXPR_CAST;
+       cast->value               = expression;
+       cast->expression.datatype = dest_type;
+
+       return (expression_t*) cast;
+}
+
+static expression_t *create_cast(expression_t *expression, type_t *dest_type)
+{
+       assert(expression->datatype != NULL);
+       type_t *source_type = expression->datatype;
+
+       if(expression->datatype == dest_type)
+               return expression;
+
+       if(dest_type->type == TYPE_ATOMIC) {
+               if(source_type->type != TYPE_ATOMIC)
+                       panic("casting of non-atomic types not implemented yet");
+
+               if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
+                       type_error_incompatible("can't cast types",
+                                               expression->source_position,
+                                               source_type, dest_type);
+                       return expression;
+               }
+
+               return create_cast_expression(expression, dest_type);
+       }
+
+       panic("casting of non-atomic types not implemented yet");
+}
+
 static expression_t *parse_conditional_expression(unsigned precedence,
                                                   expression_t *expression)
 {
@@ -2465,37 +2509,177 @@ CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_POSTFIX_INCREMENT,
 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
                                        get_unexpr_arithmetic_type)
 
-static type_t *get_binexpr_int_type(const expression_t *left,
-                                    const expression_t *right)
+static int get_rank(const type_t *type)
+{
+       /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
+        * and esp. footnote 108). However we can't fold constants (yet), so we
+        * can't decide wether unsigned int is possible, while int always works.
+        * (unsigned int would be preferable when possible... for stuff like
+        *  struct { enum { ... } bla : 4; } ) */
+       if(type->type == TYPE_ENUM)
+               return ATOMIC_TYPE_INT;
+
+       assert(type->type == TYPE_ATOMIC);
+       atomic_type_t      *atomic_type = (atomic_type_t*) type;
+       atomic_type_type_t  atype       = atomic_type->atype;
+       return atype;
+}
+
+static void semantic_arithmetic(expression_t **left, expression_t **right)
+{
+       type_t *type_left  = (*left)->datatype;
+       type_t *type_right = (*right)->datatype;
+       type_left  = skip_typeref(type_left);
+       type_right = skip_typeref(type_right);
+
+       /* TODO: handle complex + imaginary types */
+
+       /* § 6.3.1.8 Usual arithmetic conversions */
+       if(type_left == type_long_double || type_right == type_long_double) {
+               type_left  = type_long_double;
+               type_right = type_long_double;
+               goto finished;
+       } else if(type_left == type_double || type_right == type_double) {
+               type_left  = type_double;
+               type_right = type_double;
+               goto finished;
+       } else if(type_left == type_float || type_right == type_float) {
+               type_left  = type_float;
+               type_right = type_float;
+               goto finished;
+       }
+
+       /* integer promotion */
+       if(get_rank(type_left) < ATOMIC_TYPE_INT)
+               type_left = type_int;
+       if(get_rank(type_right) < ATOMIC_TYPE_INT)
+               type_right = type_int;
+
+       if(type_left == type_right)
+               goto finished;
+
+       bool signed_left  = is_type_signed(type_left);
+       bool signed_right = is_type_signed(type_right);
+       if(get_rank(type_left) < get_rank(type_right)) {
+               if(signed_left == signed_right || !signed_right) {
+                       type_left = type_right;
+               } else {
+                       type_right = type_left;
+               }
+       } else {
+               if(signed_left == signed_right || !signed_left) {
+                       type_right = type_left;
+               } else {
+                       type_left = type_right;
+               }
+       }
+
+finished:
+       assert(type_left == type_right);
+       *left  = create_cast(*left, type_left);
+       *right = create_cast(*right, type_right);
+}
+
+static void semantic_binexpr_arithmetic(binary_expression_t *expression)
 {
-       (void) left;
-       (void) right;
-       return type_int;
+       expression_t *left       = expression->left;
+       expression_t *right      = expression->right;
+       type_t       *type_left  = skip_typeref(left->datatype);
+       type_t       *type_right = skip_typeref(right->datatype);
+
+       if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
+               /* TODO: improve error message */
+               parser_print_error_prefix();
+               fprintf(stderr, "operation needs arithmetic types\n");
+               return;
+       }
+
+       semantic_arithmetic(&expression->left, &expression->right);
+       expression->expression.datatype = expression->left->datatype;
 }
 
-static type_t *get_binexpr_arithmetic_type(const expression_t *left,
-                                            const expression_t *right)
+static void semantic_comparison(binary_expression_t *expression)
 {
-       (void) right;
-       return left->datatype;
+       expression_t *left       = expression->left;
+       expression_t *right      = expression->right;
+       type_t       *type_left  = left->datatype;
+       type_t       *type_right = right->datatype;
+
+       if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
+               semantic_arithmetic(&expression->left, &expression->right);
+       }
+       expression->expression.datatype = type_int;
 }
 
-static type_t *get_binexpr_arithmetic_assign_type(const expression_t *left,
-                                                  const expression_t *right)
+static void semantic_arithmetic_assign(binary_expression_t *expression)
+{
+       expression_t *left       = expression->left;
+       expression_t *right      = expression->right;
+       type_t       *type_left  = left->datatype;
+       type_t       *type_right = right->datatype;
+
+       if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
+               /* TODO: improve error message */
+               parser_print_error_prefix();
+               fprintf(stderr, "operation needs arithmetic types\n");
+               return;
+       }
+
+       semantic_arithmetic(&expression->left, &expression->right);
+       /* note that we assign the original type_left before casting */
+       expression->expression.datatype = type_left;
+}
+
+static void semantic_logical_op(binary_expression_t *expression)
 {
-       (void) right;
        /* TODO */
-       return left->datatype;
+       expression->expression.datatype = type_int;
 }
 
-static type_t *get_binexpr_right_type(const expression_t *left,
-                                       const expression_t *right)
+static void semantic_assign(type_t *orig_type_left, expression_t **right,
+                            bool is_return)
 {
-       (void) left;
-       return right->datatype;
+       type_t *orig_type_right = (*right)->datatype;
+       type_t *type_left       = skip_typeref(orig_type_left);
+       type_t *type_right      = skip_typeref(orig_type_right);
+
+       if(type_left == type_right) {
+               /* fine */
+       } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
+               *right = create_cast(*right, type_left);
+       } else if(type_left->type == TYPE_POINTER
+                       && type_right->type == TYPE_POINTER) {
+               /* TODO */
+       } else {
+               /* TODO: improve error message */
+               parser_print_error_prefix();
+               fprintf(stderr, "incompatible types in %s\n",
+                       is_return ? "'return'" : "assignment");
+               parser_print_error_prefix();
+               print_type(type_left);
+               fputs(" <- ", stderr);
+               print_type(type_right);
+               fputs("\n", stderr);
+       }
+
+}
+
+static void semantic_binexpr_assign(binary_expression_t *expression)
+{
+       expression_t *left       = expression->left;
+       type_t       *type_left  = left->datatype;
+
+       semantic_assign(type_left, &expression->right, false);
+
+       expression->expression.datatype = type_left;
+}
+
+static void semantic_comma(binary_expression_t *expression)
+{
+       expression->expression.datatype = expression->right->datatype;
 }
 
-#define CREATE_BINEXPR_PARSER(token_type, binexpression_type, tfunc)    \
+#define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc)    \
 static expression_t *parse_##binexpression_type(unsigned precedence,    \
                                                 expression_t *left)     \
 {                                                                       \
@@ -2509,59 +2693,56 @@ static expression_t *parse_##binexpression_type(unsigned precedence,    \
        binexpr->type                = binexpression_type;                  \
        binexpr->left                = left;                                \
        binexpr->right               = right;                               \
-       binexpr->expression.datatype = tfunc(left, right);                  \
+       sfunc(binexpr);                                                     \
                                                                         \
        return (expression_t*) binexpr;                                     \
 }
 
-CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA,   get_binexpr_right_type)
-CREATE_BINEXPR_PARSER('*', BINEXPR_MUL,     get_binexpr_arithmetic_type)
-CREATE_BINEXPR_PARSER('/', BINEXPR_DIV,     get_binexpr_arithmetic_type)
-CREATE_BINEXPR_PARSER('%', BINEXPR_MOD,     get_binexpr_arithmetic_type)
-CREATE_BINEXPR_PARSER('+', BINEXPR_ADD,     get_binexpr_arithmetic_type)
-CREATE_BINEXPR_PARSER('-', BINEXPR_SUB,     get_binexpr_arithmetic_type)
-CREATE_BINEXPR_PARSER('<', BINEXPR_LESS,    get_binexpr_int_type)
-CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, get_binexpr_int_type)
-CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN,  get_binexpr_right_type)
-CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL,
-                      get_binexpr_int_type)
+CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA,          semantic_comma)
+CREATE_BINEXPR_PARSER('*', BINEXPR_MUL,            semantic_binexpr_arithmetic)
+CREATE_BINEXPR_PARSER('/', BINEXPR_DIV,            semantic_binexpr_arithmetic)
+CREATE_BINEXPR_PARSER('%', BINEXPR_MOD,            semantic_binexpr_arithmetic)
+CREATE_BINEXPR_PARSER('+', BINEXPR_ADD,            semantic_binexpr_arithmetic)
+CREATE_BINEXPR_PARSER('-', BINEXPR_SUB,            semantic_binexpr_arithmetic)
+CREATE_BINEXPR_PARSER('<', BINEXPR_LESS,           semantic_comparison)
+CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER,        semantic_comparison)
+CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN,         semantic_binexpr_assign)
+CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison)
 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
-                      get_binexpr_int_type)
-CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL,
-                      get_binexpr_int_type)
+                      semantic_comparison)
+CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison)
 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
-                      get_binexpr_int_type)
-CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, get_binexpr_arithmetic_type)
-CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR,  get_binexpr_arithmetic_type)
-CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, get_binexpr_arithmetic_type)
-CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND,
-                      get_binexpr_int_type)
-CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR,
-                      get_binexpr_int_type)
+                      semantic_comparison)
+CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND,    semantic_binexpr_arithmetic)
+CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR,     semantic_binexpr_arithmetic)
+CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR,    semantic_binexpr_arithmetic)
+CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND,  semantic_logical_op)
+CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op)
+/* TODO shift has a bit special semantic */
 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
-                      get_binexpr_arithmetic_type)
+                      semantic_binexpr_arithmetic)
 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
-                      get_binexpr_arithmetic_type)
+                      semantic_binexpr_arithmetic)
 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
-                      get_binexpr_arithmetic_assign_type)
+                      semantic_arithmetic_assign)
 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
-                      get_binexpr_arithmetic_assign_type)
+                      semantic_arithmetic_assign)
 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
-                      get_binexpr_arithmetic_assign_type)
+                      semantic_arithmetic_assign)
 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
-                      get_binexpr_arithmetic_assign_type)
+                      semantic_arithmetic_assign)
 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
-                      get_binexpr_arithmetic_assign_type)
+                      semantic_arithmetic_assign)
 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
-                      get_binexpr_arithmetic_assign_type)
+                      semantic_arithmetic_assign)
 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
-                      get_binexpr_arithmetic_assign_type)
+                      semantic_arithmetic_assign)
 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
-                      get_binexpr_arithmetic_assign_type)
+                      semantic_arithmetic_assign)
 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
-                      get_binexpr_arithmetic_assign_type)
+                      semantic_arithmetic_assign)
 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
-                      get_binexpr_arithmetic_assign_type)
+                      semantic_arithmetic_assign)
 
 static expression_t *parse_sub_expression(unsigned precedence)
 {
@@ -2904,9 +3085,27 @@ static statement_t *parse_return(void)
 
        statement->statement.type            = STATEMENT_RETURN;
        statement->statement.source_position = token.source_position;
+
+       assert(current_function->type->type == TYPE_FUNCTION);
+       function_type_t *function_type = (function_type_t*) current_function->type;
+       type_t          *return_type   = function_type->result_type;
+
+       expression_t *return_value;
        if(token.type != ';') {
-               statement->return_value = parse_expression();
+               return_value = parse_expression();
+
+               if(return_type == type_void && return_value->datatype != type_void) {
+                       parse_warning("'return' with a value, in function returning void");
+                       return_value = NULL;
+               } else {
+                       semantic_assign(return_type, &return_value, true);
+               }
+       } else {
+               return_value = NULL;
+               parse_warning("'return' without value, in function retruning non-void");
        }
+       statement->return_value = return_value;
+
        expect(';');
 
        return (statement_t*) statement;
@@ -3118,8 +3317,10 @@ static translation_unit_t *parse_translation_unit(void)
 translation_unit_t *parse(void)
 {
        environment_stack = NEW_ARR_F(stack_entry_t, 0);
+       found_error       = false;
 
        type_set_output(stderr);
+       ast_set_output(stderr);
 
        lookahead_bufpos = 0;
        for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
@@ -3129,6 +3330,9 @@ translation_unit_t *parse(void)
 
        DEL_ARR_F(environment_stack);
 
+       if(found_error)
+               return NULL;
+
        return unit;
 }
 
@@ -3137,12 +3341,15 @@ void init_parser(void)
        init_expression_parsers();
        obstack_init(&temp_obst);
 
-       type_int        = make_atomic_type(ATOMIC_TYPE_INT, 0);
-       type_double     = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
-       type_size_t     = make_atomic_type(ATOMIC_TYPE_UINT, 0);
-       type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
-       type_void       = make_atomic_type(ATOMIC_TYPE_VOID, 0);
-       type_string     = make_pointer_type(type_const_char, 0);
+       type_int         = make_atomic_type(ATOMIC_TYPE_INT, 0);
+       type_uint        = make_atomic_type(ATOMIC_TYPE_UINT, 0);
+       type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
+       type_double      = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
+       type_float       = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
+       type_size_t      = make_atomic_type(ATOMIC_TYPE_UINT, 0);
+       type_const_char  = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
+       type_void        = make_atomic_type(ATOMIC_TYPE_VOID, 0);
+       type_string      = make_pointer_type(type_const_char, 0);
 }
 
 void exit_parser(void)
diff --git a/type.c b/type.c
index 7ac4db7..8ec2495 100644 (file)
--- a/type.c
+++ b/type.c
@@ -289,8 +289,7 @@ static void intern_print_type_pre(type_t *type)
        fputs("unknown", out);
 }
 
-static
-void intern_print_type_post(type_t *type)
+static void intern_print_type_post(type_t *type)
 {
        switch(type->type) {
        case TYPE_FUNCTION:
@@ -347,10 +346,10 @@ bool type_valid(const type_t *type)
 bool is_type_integer(const type_t *type)
 {
        if(type->type == TYPE_ENUM)
-               return 1;
+               return true;
 
        if(type->type != TYPE_ATOMIC)
-               return 0;
+               return false;
 
        atomic_type_t *atomic_type = (atomic_type_t*) type;
        switch(atomic_type->atype) {
@@ -366,16 +365,16 @@ bool is_type_integer(const type_t *type)
        case ATOMIC_TYPE_ULONG:
        case ATOMIC_TYPE_LONGLONG:
        case ATOMIC_TYPE_ULONGLONG:
-               return 1;
+               return true;
        default:
-               return 0;
+               return false;
        }
 }
 
 bool is_type_floating(const type_t *type)
 {
        if(type->type != TYPE_ATOMIC)
-               return 0;
+               return false;
 
        atomic_type_t *atomic_type = (atomic_type_t*) type;
        switch(atomic_type->atype) {
@@ -392,10 +391,59 @@ bool is_type_floating(const type_t *type)
        case ATOMIC_TYPE_DOUBLE_IMAGINARY:
        case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
 #endif
-               return 1;
+               return true;
        default:
-               return 0;
+               return false;
+       }
+}
+
+bool is_type_signed(const type_t *type)
+{
+       /* enum types are int for now */
+       if(type->type == TYPE_ENUM)
+               return true;
+
+       if(type->type != TYPE_ATOMIC)
+               return false;
+
+       atomic_type_t *atomic_type = (atomic_type_t*) type;
+       switch(atomic_type->atype) {
+       case ATOMIC_TYPE_CHAR:
+       case ATOMIC_TYPE_SCHAR:
+       case ATOMIC_TYPE_SHORT:
+       case ATOMIC_TYPE_INT:
+       case ATOMIC_TYPE_LONG:
+       case ATOMIC_TYPE_LONGLONG:
+       case ATOMIC_TYPE_FLOAT:
+       case ATOMIC_TYPE_DOUBLE:
+       case ATOMIC_TYPE_LONG_DOUBLE:
+#ifdef PROVIDE_COMPLEX
+       case ATOMIC_TYPE_FLOAT_COMPLEX:
+       case ATOMIC_TYPE_DOUBLE_COMPLEX:
+       case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
+#endif
+#ifdef PROVIDE_IMAGINARY
+       case ATOMIC_TYPE_FLOAT_IMAGINARY:
+       case ATOMIC_TYPE_DOUBLE_IMAGINARY:
+       case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
+#endif
+               return true;
+
+       case ATOMIC_TYPE_BOOL:
+       case ATOMIC_TYPE_UCHAR:
+       case ATOMIC_TYPE_USHORT:
+       case ATOMIC_TYPE_UINT:
+       case ATOMIC_TYPE_ULONG:
+       case ATOMIC_TYPE_ULONGLONG:
+               return false;
+
+       case ATOMIC_TYPE_INVALID:
+       case ATOMIC_TYPE_VOID:
+               return false;
        }
+
+       panic("invalid atomic type found");
+       return false;
 }
 
 bool is_type_arithmetic(const type_t *type)
@@ -414,6 +462,40 @@ bool is_type_scalar(const type_t *type)
        return is_type_arithmetic(type);
 }
 
+type_t *skip_typeref(type_t *type)
+{
+       while(1) {
+               switch(type->type) {
+               case TYPE_TYPEDEF: {
+                       const typedef_type_t *typedef_type = (const typedef_type_t*) type;
+                       type = typedef_type->declaration->type;
+                       continue;
+               }
+               case TYPE_TYPEOF: {
+                       const typeof_type_t *typeof_type = (const typeof_type_t *) type;
+                       if(typeof_type->typeof_type != NULL) {
+                               type = typeof_type->typeof_type;
+                       } else {
+                               type = typeof_type->expression->datatype;
+                       }
+                       continue;
+               }
+               case TYPE_BUILTIN: {
+                       const builtin_type_t *builtin_type = (const builtin_type_t*) type;
+                       type = builtin_type->real_type;
+                       continue;
+               }
+               default:
+                       break;
+               }
+               break;
+       }
+
+       return type;
+}
+
+
+
 static type_t *identify_new_type(type_t *type)
 {
        type_t *result = typehash_insert(type);
diff --git a/type.h b/type.h
index 23f3d2c..3cb81e1 100644 (file)
--- a/type.h
+++ b/type.h
@@ -41,12 +41,16 @@ void inc_type_visited(void);
 
 void set_print_compound_entries(bool enabled);
 
-
 /**
  * returns true if type contains integer numbers
  */
 bool is_type_integer(const type_t *type);
 
+/**
+ * return true if type contains signed numbers
+ */
+bool is_type_signed(const type_t *type);
+
 /**
  * returns true if type contains floating point numbers
  */
@@ -68,4 +72,6 @@ bool is_type_arithmetic(const type_t *type);
  */
 bool is_type_scalar(const type_t *type);
 
+type_t *skip_typeref(type_t *type);
+
 #endif
index efc97b6..de76857 100644 (file)
--- a/type_t.h
+++ b/type_t.h
@@ -27,8 +27,10 @@ typedef enum {
        TYPE_TYPEOF
 } type_type_t;
 
+/* note that the constant values represent the rank of the types as defined
+ * in § 6.3.1 */
 typedef enum {
-       ATOMIC_TYPE_INVALID,
+       ATOMIC_TYPE_INVALID = 0,
        ATOMIC_TYPE_VOID,
        ATOMIC_TYPE_CHAR,
        ATOMIC_TYPE_SCHAR,