Simplify condition.
[cparser] / type.c
diff --git a/type.c b/type.c
index 2c57f89..2725265 100644 (file)
--- a/type.c
+++ b/type.c
@@ -47,7 +47,7 @@ void print_atomic_type(const atomic_type_t *type)
        print_type_qualifiers(type->type.qualifiers);
 
        const char *s;
-       switch(type->atype) {
+       switch(type->akind) {
        case ATOMIC_TYPE_INVALID:     s = "INVALIDATOMIC";      break;
        case ATOMIC_TYPE_VOID:        s = "void";               break;
        case ATOMIC_TYPE_BOOL:        s = "_Bool";              break;
@@ -243,6 +243,7 @@ static void print_compound_type(const compound_type_t *type)
 
 static void print_typedef_type_pre(const typedef_type_t *const type)
 {
+       print_type_qualifiers(type->type.qualifiers);
        fputs(type->declaration->symbol->string, out);
 }
 
@@ -261,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);
@@ -314,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:
@@ -365,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");
@@ -415,7 +420,7 @@ bool is_type_integer(const type_t *type)
        if(type->kind != TYPE_ATOMIC)
                return false;
 
-       switch(type->atomic.atype) {
+       switch(type->atomic.akind) {
        case ATOMIC_TYPE_BOOL:
        case ATOMIC_TYPE_CHAR:
        case ATOMIC_TYPE_SCHAR:
@@ -441,7 +446,7 @@ bool is_type_floating(const type_t *type)
        if(type->kind != TYPE_ATOMIC)
                return false;
 
-       switch(type->atomic.atype) {
+       switch(type->atomic.akind) {
        case ATOMIC_TYPE_FLOAT:
        case ATOMIC_TYPE_DOUBLE:
        case ATOMIC_TYPE_LONG_DOUBLE:
@@ -470,7 +475,7 @@ bool is_type_signed(const type_t *type)
        if(type->kind != TYPE_ATOMIC)
                return false;
 
-       switch(type->atomic.atype) {
+       switch(type->atomic.akind) {
        case ATOMIC_TYPE_CHAR:
        case ATOMIC_TYPE_SCHAR:
        case ATOMIC_TYPE_SHORT:
@@ -534,6 +539,9 @@ bool is_type_scalar(const type_t *type)
        return is_type_arithmetic(type);
 }
 
+/**
+ * Check if a given type is incomplete
+ */
 bool is_type_incomplete(const type_t *type)
 {
        assert(!is_typeref(type));
@@ -545,6 +553,11 @@ bool is_type_incomplete(const type_t *type)
                declaration_t         *declaration   = compound_type->declaration;
                return !declaration->init.is_defined;
        }
+       case TYPE_ENUM: {
+               const enum_type_t *enum_type   = &type->enumt;
+               declaration_t     *declaration = enum_type->declaration;
+               return !declaration->init.is_defined;
+       }
        case TYPE_BITFIELD:
        case TYPE_FUNCTION:
                return true;
@@ -553,11 +566,11 @@ bool is_type_incomplete(const type_t *type)
                return type->array.size == NULL;
 
        case TYPE_ATOMIC:
-               return type->atomic.atype == ATOMIC_TYPE_VOID;
+               return type->atomic.akind == ATOMIC_TYPE_VOID;
 
        case TYPE_POINTER:
-       case TYPE_ENUM:
        case TYPE_BUILTIN:
+       case TYPE_ERROR:
                return false;
 
        case TYPE_TYPEDEF:
@@ -642,7 +655,7 @@ bool types_compatible(const type_t *type1, const type_t *type2)
        case TYPE_FUNCTION:
                return function_types_compatible(&type1->function, &type2->function);
        case TYPE_ATOMIC:
-               return type1->atomic.atype == type2->atomic.atype;
+               return type1->atomic.akind == type2->atomic.akind;
        case TYPE_ARRAY:
                return array_types_compatible(&type1->array, &type2->array);
 
@@ -664,6 +677,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:
@@ -686,12 +702,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->base.qualifiers;
+       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;
@@ -717,6 +738,16 @@ type_t *skip_typeref(type_t *type)
                break;
        }
 
+       if (qualifiers != TYPE_QUALIFIER_NONE) {
+               type_t *const copy     = duplicate_type(type);
+               copy->base.qualifiers |= qualifiers;
+
+               type = typehash_insert(copy);
+               if (type != copy) {
+                       obstack_free(type_obst, copy);
+               }
+       }
+
        return type;
 }
 
@@ -731,14 +762,14 @@ static type_t *identify_new_type(type_t *type)
        return result;
 }
 
-type_t *make_atomic_type(atomic_type_type_t atype, type_qualifiers_t qualifiers)
+type_t *make_atomic_type(atomic_type_kind_t atype, type_qualifiers_t qualifiers)
 {
        type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
        memset(type, 0, sizeof(atomic_type_t));
 
        type->kind            = TYPE_ATOMIC;
        type->base.qualifiers = qualifiers;
-       type->atomic.atype    = atype;
+       type->atomic.akind    = atype;
 
        return identify_new_type(type);
 }