prelimiraries for -Wunused-parameter and -Wunused-variable:
[cparser] / type.c
diff --git a/type.c b/type.c
index 15adcc1..553cd5c 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;
@@ -82,7 +82,7 @@ static void print_function_type_pre(const function_type_t *type, bool top)
 }
 
 static void print_function_type_post(const function_type_t *type,
-                                     const context_t *context, bool top)
+                                     const scope_t *scope, bool top)
 {
        intern_print_type_post(type->return_type, false);
        /* don't emit braces if we're the toplevel type... */
@@ -91,8 +91,8 @@ static void print_function_type_post(const function_type_t *type,
 
        fputc('(', out);
 
-       int                 first     = 1;
-       if(context == NULL) {
+       int first = 1;
+       if(scope == NULL) {
                function_parameter_t *parameter = type->parameters;
                for( ; parameter != NULL; parameter = parameter->next) {
                        if(first) {
@@ -103,7 +103,7 @@ static void print_function_type_post(const function_type_t *type,
                        print_type(parameter->type);
                }
        } else {
-               declaration_t *parameter = context->declarations;
+               declaration_t *parameter = scope->declarations;
                for( ; parameter != NULL; parameter = parameter->next) {
                        if(first) {
                                first = 0;
@@ -111,7 +111,7 @@ static void print_function_type_post(const function_type_t *type,
                                fputs(", ", out);
                        }
                        print_type_ext(parameter->type, parameter->symbol,
-                                      &parameter->context);
+                                      &parameter->scope);
                }
        }
        if(type->variadic) {
@@ -209,7 +209,7 @@ void print_compound_definition(const declaration_t *declaration)
        fputs("{\n", out);
        change_indent(1);
 
-       declaration_t *iter = declaration->context.declarations;
+       declaration_t *iter = declaration->scope.declarations;
        for( ; iter != NULL; iter = iter->next) {
                print_indent();
                print_declaration(iter);
@@ -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:
@@ -332,7 +336,7 @@ void print_type(const type_t *const type)
 }
 
 void print_type_ext(const type_t *const type, const symbol_t *symbol,
-                    const context_t *context)
+                    const scope_t *scope)
 {
        if(type == NULL) {
                fputs("nil type", out);
@@ -345,7 +349,7 @@ void print_type_ext(const type_t *const type, const symbol_t *symbol,
                fputs(symbol->string, out);
        }
        if(type->kind == TYPE_FUNCTION) {
-               print_function_type_post(&type->function, context, true);
+               print_function_type_post(&type->function, scope, true);
        } else {
                intern_print_type_post(type, true);
        }
@@ -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:
@@ -573,7 +586,9 @@ bool is_type_incomplete(const type_t *type)
 static bool function_types_compatible(const function_type_t *func1,
                                       const function_type_t *func2)
 {
-       if(!types_compatible(func1->return_type, func2->return_type))
+       const type_t* const ret1 = skip_typeref(func1->return_type);
+       const type_t* const ret2 = skip_typeref(func2->return_type);
+       if (!types_compatible(ret1, ret2))
                return false;
 
        /* can parameters be compared? */
@@ -640,12 +655,16 @@ 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);
-       case TYPE_POINTER:
-               return types_compatible(type1->pointer.points_to,
-                                       type2->pointer.points_to);
+
+       case TYPE_POINTER: {
+               const type_t *const to1 = skip_typeref(type1->pointer.points_to);
+               const type_t *const to2 = skip_typeref(type2->pointer.points_to);
+               return types_compatible(to1, to2);
+       }
+
        case TYPE_COMPOUND_STRUCT:
        case TYPE_COMPOUND_UNION:
        case TYPE_ENUM:
@@ -658,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:
@@ -680,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;
@@ -711,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;
 }
 
@@ -725,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);
 }