add a mode that compiles and dumps 1 irg as result
[cparser] / type.c
diff --git a/type.c b/type.c
index 5c71465..8e42891 100644 (file)
--- a/type.c
+++ b/type.c
@@ -10,7 +10,6 @@ static struct obstack   _type_obst;
 struct obstack         *type_obst = &_type_obst;
 static FILE            *out;
 static int              type_visited = 0;
-static bool             print_compound_entries;
 
 static void intern_print_type_pre(type_t *type);
 static void intern_print_type_post(type_t *type);
@@ -30,11 +29,6 @@ void type_set_output(FILE *stream)
        out = stream;
 }
 
-void set_print_compound_entries(bool enabled)
-{
-       print_compound_entries = enabled;
-}
-
 void inc_type_visited(void)
 {
        type_visited++;
@@ -80,7 +74,7 @@ static void print_function_type_pre(const function_type_t *type)
 {
        print_type_qualifiers(type->type.qualifiers);
 
-       intern_print_type_pre(type->result_type);
+       intern_print_type_pre(type->return_type);
 
        /* TODO: don't emit braces if we're the toplevel type... */
        fputc('(', out);
@@ -90,7 +84,7 @@ static void print_function_type_post(const function_type_t *type,
                                      const context_t *context)
 {
        /* TODO: don't emit braces if we're the toplevel type... */
-       intern_print_type_post(type->result_type);
+       intern_print_type_post(type->return_type);
        fputc(')', out);
 
        fputc('(', out);
@@ -258,7 +252,6 @@ static void print_typeof_type_pre(typeof_type_t *type)
 static void intern_print_type_pre(type_t *type)
 {
        switch(type->type) {
-       case TYPE_COUNT:
        case TYPE_INVALID:
                fputs("invalid", out);
                return;
@@ -307,7 +300,6 @@ static void intern_print_type_post(type_t *type)
                print_array_type_post(&type->array);
                return;
        case TYPE_INVALID:
-       case TYPE_COUNT:
        case TYPE_ATOMIC:
        case TYPE_ENUM:
        case TYPE_COMPOUND_STRUCT:
@@ -344,6 +336,54 @@ void print_type_ext(type_t *type, const symbol_t *symbol,
        }
 }
 
+static size_t get_type_size(type_t *type)
+{
+       switch(type->type) {
+       case TYPE_ATOMIC:          return sizeof(atomic_type_t);
+       case TYPE_COMPOUND_STRUCT:
+       case TYPE_COMPOUND_UNION:  return sizeof(compound_type_t);
+       case TYPE_ENUM:            return sizeof(enum_type_t);
+       case TYPE_FUNCTION:        return sizeof(function_type_t);
+       case TYPE_POINTER:         return sizeof(pointer_type_t);
+       case TYPE_ARRAY:           return sizeof(array_type_t);
+       case TYPE_BUILTIN:         return sizeof(builtin_type_t);
+       case TYPE_TYPEDEF:         return sizeof(typedef_type_t);
+       case TYPE_TYPEOF:          return sizeof(typeof_type_t);
+       case TYPE_INVALID:         panic("invalid type found");
+       }
+       panic("unknown type found");
+}
+
+/**
+ * duplicates a type
+ * note that this does not produce a deep copy!
+ */
+type_t *duplicate_type(type_t *type)
+{
+       size_t size = get_type_size(type);
+
+       type_t *copy = obstack_alloc(type_obst, size);
+       memcpy(copy, type, size);
+
+       return copy;
+}
+
+type_t *get_unqualified_type(type_t *type)
+{
+       if(type->base.qualifiers == TYPE_QUALIFIER_NONE)
+               return type;
+
+       type_t *unqualified_type          = duplicate_type(type);
+       unqualified_type->base.qualifiers = TYPE_QUALIFIER_NONE;
+
+       type_t *result = typehash_insert(unqualified_type);
+       if(result != unqualified_type) {
+               obstack_free(type_obst, unqualified_type);
+       }
+
+       return result;
+}
+
 bool type_valid(const type_t *type)
 {
        return type->type != TYPE_INVALID;
@@ -456,17 +496,20 @@ bool is_type_arithmetic(const type_t *type)
        assert(!is_typeref(type));
 
        if(is_type_integer(type) || is_type_floating(type))
-               return 1;
+               return true;
 
-       return 0;
+       return false;
 }
 
 bool is_type_scalar(const type_t *type)
 {
        assert(!is_typeref(type));
 
-       if(type->type == TYPE_POINTER)
-               return 1;
+       switch (type->type) {
+               case TYPE_POINTER: return true;
+               case TYPE_BUILTIN: return is_type_scalar(type->builtin.real_type);
+               default:           break;
+       }
 
        return is_type_arithmetic(type);
 }
@@ -491,13 +534,12 @@ bool is_type_incomplete(const type_t *type)
        case TYPE_ATOMIC:
        case TYPE_POINTER:
        case TYPE_ENUM:
+       case TYPE_BUILTIN:
                return false;
 
        case TYPE_TYPEDEF:
        case TYPE_TYPEOF:
-       case TYPE_BUILTIN:
                panic("is_type_incomplete called without typerefs skipped");
-       case TYPE_COUNT:
        case TYPE_INVALID:
                break;
        }
@@ -505,19 +547,97 @@ bool is_type_incomplete(const type_t *type)
        panic("invalid type found");
 }
 
+static bool function_types_compatible(const function_type_t *func1,
+                                      const function_type_t *func2)
+{
+       if(!types_compatible(func1->return_type, func2->return_type))
+               return false;
+
+       /* can parameters be compared? */
+       if(func1->unspecified_parameters || func2->unspecified_parameters)
+               return true;
+
+       if(func1->variadic != func2->variadic)
+               return false;
+
+       /* TODO: handling of unspecified parameters not correct yet */
+
+       /* all argument types must be compatible */
+       function_parameter_t *parameter1 = func1->parameters;
+       function_parameter_t *parameter2 = func2->parameters;
+       for( ; parameter1 != NULL && parameter2 != NULL;
+                       parameter1 = parameter1->next, parameter2 = parameter2->next) {
+               type_t *parameter1_type = skip_typeref(parameter1->type);
+               type_t *parameter2_type = skip_typeref(parameter2->type);
+
+               parameter1_type = get_unqualified_type(parameter1_type);
+               parameter2_type = get_unqualified_type(parameter2_type);
+
+               if(!types_compatible(parameter1_type, parameter2_type))
+                       return false;
+       }
+       /* same number of arguments? */
+       if(parameter1 != NULL || parameter2 != NULL)
+               return false;
+
+       return true;
+}
+
+static bool array_types_compatible(const array_type_t *array1,
+                                   const array_type_t *array2)
+{
+       type_t *element_type1 = skip_typeref(array1->element_type);
+       type_t *element_type2 = skip_typeref(array2->element_type);
+       if(!types_compatible(element_type1, element_type2))
+               return false;
+
+       if(array1->size != NULL && array2->size != NULL) {
+               /* TODO: check if size expression evaulate to the same value
+                * if they are constant */
+       }
+
+       return true;
+}
+
 bool types_compatible(const type_t *type1, const type_t *type2)
 {
        assert(!is_typeref(type1));
        assert(!is_typeref(type2));
 
-       /* TODO: really incomplete */
+       /* shortcut: the same type is always compatible */
        if(type1 == type2)
                return true;
 
-       if(type1->type == TYPE_ATOMIC && type2->type == TYPE_ATOMIC) {
+       if(type1->base.qualifiers != type2->base.qualifiers)
+               return false;
+       if(type1->type != type2->type)
+               return false;
+
+       switch(type1->type) {
+       case TYPE_FUNCTION:
+               return function_types_compatible(&type1->function, &type2->function);
+       case TYPE_ATOMIC:
                return type1->atomic.atype == type2->atomic.atype;
+       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_COMPOUND_STRUCT:
+       case TYPE_COMPOUND_UNION:
+       case TYPE_ENUM:
+       case TYPE_BUILTIN:
+               /* TODO: not implemented */
+               break;
+
+       case TYPE_INVALID:
+               panic("invalid type found in compatible types");
+       case TYPE_TYPEDEF:
+       case TYPE_TYPEOF:
+               panic("typerefs not skipped in compatible types?!?");
        }
 
+       /* TODO: incomplete */
        return false;
 }
 
@@ -532,41 +652,6 @@ bool pointers_compatible(const type_t *type1, const type_t *type2)
        return true;
 }
 
-static size_t get_type_size(type_t *type)
-{
-       switch(type->type) {
-       case TYPE_ATOMIC:          return sizeof(atomic_type_t); break;
-       case TYPE_COMPOUND_STRUCT:
-       case TYPE_COMPOUND_UNION:  return sizeof(compound_type_t); break;
-       case TYPE_ENUM:            return sizeof(enum_type_t); break;
-       case TYPE_FUNCTION:        return sizeof(function_type_t); break;
-       case TYPE_POINTER:         return sizeof(pointer_type_t); break;
-       case TYPE_ARRAY:           return sizeof(array_type_t); break;
-       case TYPE_BUILTIN:         return sizeof(builtin_type_t); break;
-       case TYPE_TYPEDEF:         return sizeof(typedef_type_t); break;
-       case TYPE_TYPEOF:          return sizeof(typeof_type_t); break;
-       case TYPE_COUNT:
-       case TYPE_INVALID:         panic("invalid type found"); break;
-       }
-       panic("unknown type found");
-}
-
-/**
- * duplicates a type
- * note that this does not produce a deep copy!
- */
-static type_t *duplicate_type(type_t *type)
-{
-       size_t size = get_type_size(type);
-
-       type_t *copy = obstack_alloc(type_obst, size);
-       memcpy(copy, type, size);
-
-       (void) duplicate_type;
-
-       return type;
-}
-
 type_t *skip_typeref(type_t *type)
 {
        unsigned qualifiers = type->base.qualifiers;
@@ -592,11 +677,6 @@ type_t *skip_typeref(type_t *type)
                        }
                        continue;
                }
-               case TYPE_BUILTIN: {
-                       const builtin_type_t *builtin_type = &type->builtin;
-                       type = builtin_type->real_type;
-                       continue;
-               }
                default:
                        break;
                }