- create an error type
authorMichael Beck <beck@ipd.info.uni-karlsruhe.de>
Wed, 12 Dec 2007 16:48:37 +0000 (16:48 +0000)
committerMichael Beck <beck@ipd.info.uni-karlsruhe.de>
Wed, 12 Dec 2007 16:48:37 +0000 (16:48 +0000)
- improved declaration checking

[r18717]

ast2firm.c
parser.c
parsetest/shouldfail/function_return.c [new file with mode: 0644]
type.c
type_hash.c
type_t.h
types.c
types.h

index 3bda245..ac69667 100644 (file)
@@ -312,6 +312,8 @@ static unsigned get_type_size(type_t *type)
        type = skip_typeref(type);
 
        switch(type->kind) {
+       case TYPE_ERROR:
+               panic("error type occured");
        case TYPE_ATOMIC:
                return get_atomic_type_size(&type->atomic);
        case TYPE_ENUM:
@@ -733,6 +735,8 @@ static ir_type *get_ir_type(type_t *type)
 
        ir_type *firm_type = NULL;
        switch(type->kind) {
+       case TYPE_ERROR:
+               panic("error type occured");
        case TYPE_ATOMIC:
                firm_type = create_atomic_type(&type->atomic);
                break;
index 972e044..465f817 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -138,7 +138,9 @@ static void *allocate_ast_zero(size_t size)
 
 static declaration_t *allocate_declaration_zero(void)
 {
-       return allocate_ast_zero(sizeof(*allocate_declaration_zero()));
+       declaration_t *declaration = allocate_ast_zero(sizeof(*allocate_declaration_zero()));
+       declaration->type = type_error_type;
+       return declaration;
 }
 
 /**
@@ -234,7 +236,8 @@ static expression_t *allocate_expression_zero(expression_kind_t kind)
        size_t        size = get_expression_struct_size(kind);
        expression_t *res  = allocate_ast_zero(size);
 
-       res->base.kind = kind;
+       res->base.kind     = kind;
+       res->base.datatype = type_error_type;
        return res;
 }
 
@@ -1677,7 +1680,7 @@ static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                        }
                        break;
 
-               /* TODO: if type != NULL for the following rules should issue
+               /* TODO: if is_type_valid(type) for the following rules should issue
                 * an error */
                case T_struct: {
                        type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
@@ -2408,18 +2411,14 @@ warn_redundant_declaration:
  * Check if a given type is a vilid array type.
  */
 static bool is_valid_array_type(const type_t *type) {
-       if (type == NULL) {
-               /* the error type should be already handled */
-               return true;
-       }
        if (type->kind == TYPE_ARRAY) {
                const array_type_t *array = &type->array;
-               const type_t       *etype = array->element_type;
+               const type_t       *etype = skip_typeref(array->element_type);
 
                if (! is_valid_array_type(etype))
                        return false;
 
-               if (etype != NULL && etype->kind == TYPE_ATOMIC) {
+               if (etype->kind == TYPE_ATOMIC) {
                        const atomic_type_t *atype = &etype->atomic;
 
                        if (atype->akind == ATOMIC_TYPE_VOID) {
@@ -2433,14 +2432,24 @@ static bool is_valid_array_type(const type_t *type) {
 static declaration_t *record_declaration(declaration_t *declaration)
 {
        declaration = internal_record_declaration(declaration, false);
-       const type_t *type = declaration->type;
-       if (type != NULL) {
-               /* check the type here for several not allowed combinations */
-               if (! is_valid_array_type(type)) {
-                       errorf(declaration->source_position, "declaration of '%Y' as array of voids",
-                                       declaration->symbol);
+       const type_t *type = skip_typeref(declaration->type);
+
+       /* check the type here for several not allowed combinations */
+       if (type->kind == TYPE_FUNCTION) {
+               const function_type_t* function_type = &type->function;
+               const type_t*          ret_type      = skip_typeref(function_type->return_type);
+
+               if (ret_type->kind == TYPE_FUNCTION) {
+                       errorf(declaration->source_position, "'%Y' declared as function returning a function",
+                               declaration->symbol);
+                       declaration->type = type_error_type;
                }
        }
+       if (! is_valid_array_type(type)) {
+               errorf(declaration->source_position, "declaration of '%Y' as array of voids",
+                               declaration->symbol);
+               declaration->type = type_error_type;
+       }
        return declaration;
 }
 
@@ -2482,9 +2491,7 @@ static void parse_init_declarator_rest(declaration_t *declaration)
        eat('=');
 
        type_t *orig_type = declaration->type;
-       type_t *type      = NULL;
-       if(orig_type != NULL)
-               type = skip_typeref(orig_type);
+       type_t *type      = type = skip_typeref(orig_type);
 
        if(declaration->init.initializer != NULL) {
                parser_error_multiple_definition(declaration, token.source_position);
@@ -2494,7 +2501,7 @@ static void parse_init_declarator_rest(declaration_t *declaration)
 
        /* ยง 6.7.5 (22)  array initializers for arrays with unknown size determine
         * the array type size */
-       if(type != NULL && is_type_array(type) && initializer != NULL) {
+       if(is_type_array(type) && initializer != NULL) {
                array_type_t *array_type = &type->array;
 
                if(array_type->size == NULL) {
@@ -2529,7 +2536,7 @@ static void parse_init_declarator_rest(declaration_t *declaration)
                }
        }
 
-       if(type != NULL && is_type_function(type)) {
+       if(is_type_function(type)) {
                errorf(declaration->source_position,
                       "initializers not allowed for function types at declator '%Y' (type '%T')",
                       declaration->symbol, orig_type);
@@ -2586,7 +2593,8 @@ static void parse_declaration_rest(declaration_t *ndeclaration,
                type_t *orig_type = declaration->type;
                type_t *type      = skip_typeref(orig_type);
 
-               if(type->kind != TYPE_FUNCTION && declaration->is_inline) {
+               if(is_type_valid(type) &&
+                  type->kind != TYPE_FUNCTION && declaration->is_inline) {
                        warningf(declaration->source_position,
                                 "variable '%Y' declared 'inline'\n", declaration->symbol);
                }
@@ -3853,7 +3861,7 @@ static expression_t *parse_call_expression(unsigned precedence,
 
        function_type_t *function_type = NULL;
        type_t          *orig_type     = expression->base.datatype;
-       if(orig_type != NULL) {
+       if(is_type_valid(orig_type)) {
                type_t *type  = skip_typeref(orig_type);
 
                if(is_type_pointer(type)) {
@@ -3977,9 +3985,9 @@ static expression_t *parse_conditional_expression(unsigned precedence,
 
        /* 6.5.15.2 */
        type_t *condition_type_orig = expression->base.datatype;
-       if(condition_type_orig != NULL) {
+       if(is_type_valid(condition_type_orig)) {
                type_t *condition_type = skip_typeref(condition_type_orig);
-               if(condition_type != NULL && !is_type_scalar(condition_type)) {
+               if(condition_type->kind != TYPE_ERROR && !is_type_scalar(condition_type)) {
                        type_error("expected a scalar type in conditional condition",
                                   expression->base.source_position, condition_type_orig);
                }
@@ -3994,7 +4002,7 @@ static expression_t *parse_conditional_expression(unsigned precedence,
 
        type_t *orig_true_type  = true_expression->base.datatype;
        type_t *orig_false_type = false_expression->base.datatype;
-       if(orig_true_type == NULL || orig_false_type == NULL)
+       if(!is_type_valid(orig_true_type) || !is_type_valid(orig_false_type))
                return result;
 
        type_t *true_type  = skip_typeref(orig_true_type);
@@ -4062,7 +4070,7 @@ static expression_t *parse_builtin_classify_type(const unsigned precedence)
 static void semantic_incdec(unary_expression_t *expression)
 {
        type_t *orig_type = expression->value->base.datatype;
-       if(orig_type == NULL)
+       if(!is_type_valid(orig_type))
                return;
 
        type_t *type = skip_typeref(orig_type);
@@ -4078,7 +4086,7 @@ static void semantic_incdec(unary_expression_t *expression)
 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
 {
        type_t *orig_type = expression->value->base.datatype;
-       if(orig_type == NULL)
+       if(!is_type_valid(orig_type))
                return;
 
        type_t *type = skip_typeref(orig_type);
@@ -4094,7 +4102,7 @@ static void semantic_unexpr_arithmetic(unary_expression_t *expression)
 static void semantic_unexpr_scalar(unary_expression_t *expression)
 {
        type_t *orig_type = expression->value->base.datatype;
-       if(orig_type == NULL)
+       if(!is_type_valid(orig_type))
                return;
 
        type_t *type = skip_typeref(orig_type);
@@ -4109,7 +4117,7 @@ static void semantic_unexpr_scalar(unary_expression_t *expression)
 static void semantic_unexpr_integer(unary_expression_t *expression)
 {
        type_t *orig_type = expression->value->base.datatype;
-       if(orig_type == NULL)
+       if(!is_type_valid(orig_type))
                return;
 
        type_t *type = skip_typeref(orig_type);
@@ -4124,7 +4132,7 @@ static void semantic_unexpr_integer(unary_expression_t *expression)
 static void semantic_dereference(unary_expression_t *expression)
 {
        type_t *orig_type = expression->value->base.datatype;
-       if(orig_type == NULL)
+       if(!is_type_valid(orig_type))
                return;
 
        type_t *type = skip_typeref(orig_type);
@@ -4149,7 +4157,7 @@ static void semantic_take_addr(unary_expression_t *expression)
        value->base.datatype = revert_automatic_type_conversion(value);
 
        type_t *orig_type = value->base.datatype;
-       if(orig_type == NULL)
+       if(!is_type_valid(orig_type))
                return;
 
        if(value->kind == EXPR_REFERENCE) {
@@ -5346,7 +5354,7 @@ static statement_t *parse_return(void)
                                "'return' with a value, in function returning void");
                        return_value = NULL;
                } else {
-                       if(return_type != NULL) {
+                       if(is_type_valid(return_type)) {
                                semantic_assign(return_type, &return_value, "'return'");
                        }
                }
diff --git a/parsetest/shouldfail/function_return.c b/parsetest/shouldfail/function_return.c
new file mode 100644 (file)
index 0000000..adcde69
--- /dev/null
@@ -0,0 +1,8 @@
+typedef int (function)(int);
+typedef void broken_array[8];
+
+broken_array x;
+
+function test(void);
+
+broken_array test2(void);
diff --git a/type.c b/type.c
index d4ad8b9..9594ae8 100644 (file)
--- a/type.c
+++ b/type.c
@@ -262,8 +262,10 @@ static void print_typeof_type_pre(const typeof_type_t *const type)
 static void intern_print_type_pre(const type_t *const type, const bool top)
 {
        switch(type->kind) {
+       case TYPE_ERROR:
+               fputs("<error>", out);
        case TYPE_INVALID:
-               fputs("invalid", out);
+               fputs("<invalid>", out);
                return;
        case TYPE_ENUM:
                print_type_enum(&type->enumt);
@@ -315,6 +317,7 @@ static void intern_print_type_post(const type_t *const type, const bool top)
        case TYPE_BITFIELD:
                print_bitfield_type_post(&type->bitfield);
                return;
+       case TYPE_ERROR:
        case TYPE_INVALID:
        case TYPE_ATOMIC:
        case TYPE_ENUM:
@@ -366,6 +369,7 @@ static size_t get_type_size(type_t *type)
        case TYPE_TYPEDEF:         return sizeof(typedef_type_t);
        case TYPE_TYPEOF:          return sizeof(typeof_type_t);
        case TYPE_BITFIELD:        return sizeof(bitfield_type_t);
+       case TYPE_ERROR:           panic("error type found");
        case TYPE_INVALID:         panic("invalid type found");
        }
        panic("unknown type found");
@@ -564,6 +568,8 @@ bool is_type_incomplete(const type_t *type)
        case TYPE_TYPEDEF:
        case TYPE_TYPEOF:
                panic("is_type_incomplete called without typerefs skipped");
+       case TYPE_ERROR:
+               panic("error type found");
        case TYPE_INVALID:
                break;
        }
@@ -665,6 +671,9 @@ bool types_compatible(const type_t *type1, const type_t *type2)
                 * really need it! */
                panic("type compatibility check for bitfield type");
 
+       case TYPE_ERROR:
+               /* Hmm, the error type should be compatible to all other types */
+               return true;
        case TYPE_INVALID:
                panic("invalid type found in compatible types");
        case TYPE_TYPEDEF:
@@ -687,12 +696,17 @@ bool pointers_compatible(const type_t *type1, const type_t *type2)
        return true;
 }
 
+/**
+ * Skip all typerefs and return the underlying type.
+ */
 type_t *skip_typeref(type_t *type)
 {
        unsigned qualifiers = TYPE_QUALIFIER_NONE;
 
        while(true) {
                switch(type->kind) {
+               case TYPE_ERROR:
+                       return type;
                case TYPE_TYPEDEF: {
                        qualifiers |= type->base.qualifiers;
                        const typedef_type_t *typedef_type = &type->typedeft;
index ed07122..4168dde 100644 (file)
@@ -93,6 +93,8 @@ static unsigned hash_type(const type_t *type)
        case TYPE_INVALID:
                panic("internalizing void or invalid types not possible");
                return 0;
+       case TYPE_ERROR:
+               return 0;
        case TYPE_ATOMIC:
                hash = hash_atomic_type(&type->atomic);
                break;
@@ -239,6 +241,9 @@ static bool types_equal(const type_t *type1, const type_t *type2)
                return false;
 
        switch(type1->kind) {
+       case TYPE_ERROR:
+               /* Hmm, the error type is never equal */
+               return false;
        case TYPE_INVALID:
                return false;
        case TYPE_ATOMIC:
index 62a0e49..7b4fec9 100644 (file)
--- a/type_t.h
+++ b/type_t.h
@@ -16,6 +16,7 @@ extern struct obstack *type_obst;
 
 typedef enum {
        TYPE_INVALID,
+       TYPE_ERROR,
        TYPE_ATOMIC,
        TYPE_COMPOUND_STRUCT,
        TYPE_COMPOUND_UNION,
diff --git a/types.c b/types.c
index 53b78b5..3f095c0 100644 (file)
--- a/types.c
+++ b/types.c
@@ -1,6 +1,8 @@
 #include "type_t.h"
 #include "types.h"
 
+/** The error type. */
+type_t *type_error_type;
 
 type_t *type_char;
 type_t *type_double;
@@ -41,6 +43,8 @@ type_t *type_wchar_t_ptr;
 
 void init_basic_types(void)
 {
+       static const type_kind_t error = TYPE_ERROR;
+       type_error_type         = (type_t *)&error;
        type_signed_char        = make_atomic_type(ATOMIC_TYPE_SCHAR,       TYPE_QUALIFIER_NONE);
        type_short              = make_atomic_type(ATOMIC_TYPE_SHORT,       TYPE_QUALIFIER_NONE);
        type_int                = make_atomic_type(ATOMIC_TYPE_INT,         TYPE_QUALIFIER_NONE);
diff --git a/types.h b/types.h
index debdfef..690e683 100644 (file)
--- a/types.h
+++ b/types.h
@@ -3,6 +3,8 @@
 
 #include "type.h"
 
+extern type_t *type_error_type;
+
 extern type_t *type_char;
 extern type_t *type_double;
 extern type_t *type_float;
@@ -41,4 +43,6 @@ extern type_t *type_wchar_t_ptr;
 
 void init_basic_types(void);
 
+#define is_type_valid(type) ((type)->kind != TYPE_ERROR)
+
 #endif