put Type types into a union again, but so that all the subtype are still available...
authorMatthias Braun <matze@braunis.de>
Mon, 26 Nov 2007 20:41:18 +0000 (20:41 +0000)
committerMatthias Braun <matze@braunis.de>
Mon, 26 Nov 2007 20:41:18 +0000 (20:41 +0000)
[r18545]

ast2firm.c
parser.c
type.c
type.h
type_hash.c
type_t.h

index 7405cb8..ab0cd4c 100644 (file)
@@ -100,7 +100,7 @@ void init_ast2firm(void)
        ir_type_void_ptr   = new_type_pointer(new_id_from_str("void_ptr"),
                                              ir_type_void, mode_P_data);
 
-       type_void->firm_type = ir_type_void;
+       type_void->base.firm_type = ir_type_void;
 
        symbol_alloca = symbol_table_insert("__builtin_alloca");
 }
@@ -213,7 +213,7 @@ static unsigned get_atomic_type_size(const atomic_type_t *type)
 
 static unsigned get_compound_type_size(compound_type_t *type)
 {
-       ir_type *irtype = get_ir_type(&type->type);
+       ir_type *irtype = get_ir_type((type_t*) type);
        return get_type_size_bytes(irtype);
 }
 
@@ -495,9 +495,9 @@ static ir_type *get_ir_type(type_t *type)
 
        type = skip_typeref(type);
 
-       if(type->firm_type != NULL) {
-               assert(type->firm_type != INVALID_TYPE);
-               return type->firm_type;
+       if(type->base.firm_type != NULL) {
+               assert(type->base.firm_type != INVALID_TYPE);
+               return type->base.firm_type;
        }
 
        ir_type *firm_type = NULL;
@@ -532,7 +532,7 @@ static ir_type *get_ir_type(type_t *type)
        if(firm_type == NULL)
                panic("unknown type found");
 
-       type->firm_type = firm_type;
+       type->base.firm_type = firm_type;
        return firm_type;
 }
 
index b61c609..ee5cfed 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -623,8 +623,8 @@ static int get_rank(const type_t *type)
                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;
+       const atomic_type_t *atomic_type = &type->atomic;
+       atomic_type_type_t   atype       = atomic_type->atype;
        return atype;
 }
 
@@ -706,9 +706,8 @@ static expression_t *create_implicit_cast(expression_t *expression,
                                        break;
 
                                case TYPE_ARRAY: {
-                                       array_type_t   *array_type = (array_type_t*) source_type;
-                                       pointer_type_t *pointer_type
-                                               = (pointer_type_t*) dest_type;
+                                       array_type_t   *array_type   = &source_type->array;
+                                       pointer_type_t *pointer_type = &dest_type->pointer;
                                        if (types_compatible(array_type->element_type,
                                                                                 pointer_type->points_to)) {
                                                return create_cast_expression(expression, dest_type);
@@ -766,7 +765,7 @@ static void semantic_assign(type_t *orig_type_left, expression_t **right,
 
                /* the left type has all qualifiers from the right type */
                unsigned missing_qualifiers
-                       = points_to_right->qualifiers & ~points_to_left->qualifiers;
+                       = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
                if(missing_qualifiers != 0) {
                        parser_print_error_prefix();
                        fprintf(stderr, "destination type ");
@@ -953,11 +952,11 @@ static initializer_t *initializer_from_expression(type_t *type,
 
        /* ยง 6.7.8.14/15 char array may be initialized by string literals */
        if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
-               array_type_t *array_type   = (array_type_t*) type;
+               array_type_t *array_type   = &type->array;
                type_t       *element_type = array_type->element_type;
 
                if(element_type->type == TYPE_ATOMIC) {
-                       atomic_type_t      *atomic_type = (atomic_type_t*) element_type;
+                       atomic_type_t      *atomic_type = &element_type->atomic;
                        atomic_type_type_t  atype       = atomic_type->atype;
 
                        /* TODO handle wide strings */
@@ -1722,7 +1721,7 @@ finish_specifiers:
                }
        }
 
-       type->qualifiers = (type_qualifier_t)type_qualifiers;
+       type->base.qualifiers = (type_qualifier_t)type_qualifiers;
 
        type_t *result = typehash_insert(type);
        if(newtype && result != (type_t*) type) {
@@ -3774,7 +3773,7 @@ static void semantic_binexpr_assign(binary_expression_t *expression)
                fprintf(stderr, "')\n");
                return;
        }
-       if(type_left->qualifiers & TYPE_QUALIFIER_CONST) {
+       if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
                parser_print_error_prefix();
                fprintf(stderr, "assignment to readonly location '");
                print_expression(left);
diff --git a/type.c b/type.c
index 446d035..c84fd41 100644 (file)
--- a/type.c
+++ b/type.c
@@ -40,7 +40,7 @@ void inc_type_visited(void)
        type_visited++;
 }
 
-void print_type_qualifiers(unsigned qualifiers)
+void print_type_qualifiers(type_qualifiers_t qualifiers)
 {
        if(qualifiers & TYPE_QUALIFIER_CONST)    fputs("const ",    out);
        if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
@@ -262,32 +262,32 @@ static void intern_print_type_pre(type_t *type)
                fputs("invalid", out);
                return;
        case TYPE_ENUM:
-               print_type_enum((enum_type_t*) type);
+               print_type_enum(&type->enumt);
                return;
        case TYPE_ATOMIC:
-               print_atomic_type((atomic_type_t*) type);
+               print_atomic_type(&type->atomic);
                return;
        case TYPE_COMPOUND_STRUCT:
        case TYPE_COMPOUND_UNION:
-               print_compound_type((compound_type_t*) type);
+               print_compound_type(&type->compound);
                return;
        case TYPE_BUILTIN:
-               fputs(((builtin_type_t*) type)->symbol->string, out);
+               fputs(type->builtin.symbol->string, out);
                return;
        case TYPE_FUNCTION:
-               print_function_type_pre((function_type_t*) type);
+               print_function_type_pre(&type->function);
                return;
        case TYPE_POINTER:
-               print_pointer_type_pre((pointer_type_t*) type);
+               print_pointer_type_pre(&type->pointer);
                return;
        case TYPE_ARRAY:
-               print_array_type_pre((array_type_t*) type);
+               print_array_type_pre(&type->array);
                return;
        case TYPE_TYPEDEF:
-               print_typedef_type_pre((typedef_type_t*) type);
+               print_typedef_type_pre(&type->typedeft);
                return;
        case TYPE_TYPEOF:
-               print_typeof_type_pre((typeof_type_t*) type);
+               print_typeof_type_pre(&type->typeoft);
                return;
        }
        fputs("unknown", out);
@@ -297,13 +297,13 @@ static void intern_print_type_post(type_t *type)
 {
        switch(type->type) {
        case TYPE_FUNCTION:
-               print_function_type_post((const function_type_t*) type, NULL);
+               print_function_type_post(&type->function, NULL);
                return;
        case TYPE_POINTER:
-               print_pointer_type_post((const pointer_type_t*) type);
+               print_pointer_type_post(&type->pointer);
                return;
        case TYPE_ARRAY:
-               print_array_type_post((const array_type_t*) type);
+               print_array_type_post(&type->array);
                return;
        case TYPE_INVALID:
        case TYPE_ATOMIC:
@@ -357,8 +357,7 @@ bool is_type_integer(const type_t *type)
        if(type->type != TYPE_ATOMIC)
                return false;
 
-       atomic_type_t *atomic_type = (atomic_type_t*) type;
-       switch(atomic_type->atype) {
+       switch(type->atomic.atype) {
        case ATOMIC_TYPE_BOOL:
        case ATOMIC_TYPE_CHAR:
        case ATOMIC_TYPE_SCHAR:
@@ -384,8 +383,7 @@ bool is_type_floating(const type_t *type)
        if(type->type != TYPE_ATOMIC)
                return false;
 
-       atomic_type_t *atomic_type = (atomic_type_t*) type;
-       switch(atomic_type->atype) {
+       switch(type->atomic.atype) {
        case ATOMIC_TYPE_FLOAT:
        case ATOMIC_TYPE_DOUBLE:
        case ATOMIC_TYPE_LONG_DOUBLE:
@@ -414,8 +412,7 @@ bool is_type_signed(const type_t *type)
        if(type->type != TYPE_ATOMIC)
                return false;
 
-       atomic_type_t *atomic_type = (atomic_type_t*) type;
-       switch(atomic_type->atype) {
+       switch(type->atomic.atype) {
        case ATOMIC_TYPE_CHAR:
        case ATOMIC_TYPE_SCHAR:
        case ATOMIC_TYPE_SHORT:
@@ -479,19 +476,15 @@ bool is_type_incomplete(const type_t *type)
        switch(type->type) {
        case TYPE_COMPOUND_STRUCT:
        case TYPE_COMPOUND_UNION: {
-               const compound_type_t *compound_type
-                       = (const compound_type_t*) type;
-               declaration_t *declaration = compound_type->declaration;
+               const compound_type_t *compound_type = &type->compound;
+               declaration_t         *declaration   = compound_type->declaration;
                return !declaration->init.is_defined;
        }
        case TYPE_FUNCTION:
                return true;
 
-       case TYPE_ARRAY: {
-               const array_type_t *array_type = (const array_type_t*) type;
-
-               return array_type->size == NULL;
-       }
+       case TYPE_ARRAY:
+               return type->array.size == NULL;
 
        case TYPE_ATOMIC:
        case TYPE_POINTER:
@@ -519,10 +512,7 @@ bool types_compatible(const type_t *type1, const type_t *type2)
                return true;
 
        if(type1->type == TYPE_ATOMIC && type2->type == TYPE_ATOMIC) {
-               const atomic_type_t *atomic1 = (const atomic_type_t*) type1;
-               const atomic_type_t *atomic2 = (const atomic_type_t*) type2;
-
-               return atomic1->atype == atomic2->atype;
+               return type1->atomic.atype == type2->atomic.atype;
        }
 
        return false;
@@ -535,12 +525,7 @@ bool pointers_compatible(const type_t *type1, const type_t *type2)
 
        assert(type1->type == TYPE_POINTER);
        assert(type2->type == TYPE_POINTER);
-#if 0
-       pointer_type_t *pointer_type1 = (pointer_type_t*) type1;
-       pointer_type_t *pointer_type2 = (pointer_type_t*) type2;
-       return types_compatible(pointer_type1->points_to,
-                               pointer_type2->points_to);
-#endif
+       /* TODO */
        return true;
 }
 
@@ -580,13 +565,13 @@ static type_t *duplicate_type(type_t *type)
 
 type_t *skip_typeref(type_t *type)
 {
-       unsigned qualifiers = type->qualifiers;
+       unsigned qualifiers = type->base.qualifiers;
 
        while(1) {
                switch(type->type) {
                case TYPE_TYPEDEF: {
-                       qualifiers |= type->qualifiers;
-                       const typedef_type_t *typedef_type = (const typedef_type_t*) type;
+                       qualifiers |= type->base.qualifiers;
+                       const typedef_type_t *typedef_type = &type->typedeft;
                        if(typedef_type->resolved_type != NULL) {
                                type = typedef_type->resolved_type;
                                break;
@@ -595,7 +580,7 @@ type_t *skip_typeref(type_t *type)
                        continue;
                }
                case TYPE_TYPEOF: {
-                       const typeof_type_t *typeof_type = (const typeof_type_t *) type;
+                       const typeof_type_t *typeof_type = &type->typeoft;
                        if(typeof_type->typeof_type != NULL) {
                                type = typeof_type->typeof_type;
                        } else {
@@ -604,7 +589,7 @@ type_t *skip_typeref(type_t *type)
                        continue;
                }
                case TYPE_BUILTIN: {
-                       const builtin_type_t *builtin_type = (const builtin_type_t*) type;
+                       const builtin_type_t *builtin_type = &type->builtin;
                        type = builtin_type->real_type;
                        continue;
                }
@@ -628,28 +613,28 @@ static type_t *identify_new_type(type_t *type)
        return result;
 }
 
-type_t *make_atomic_type(atomic_type_type_t type, type_qualifiers_t qualifiers)
+type_t *make_atomic_type(atomic_type_type_t atype, type_qualifiers_t qualifiers)
 {
-       atomic_type_t *atomic_type
-               = obstack_alloc(type_obst, sizeof(atomic_type[0]));
-       memset(atomic_type, 0, sizeof(atomic_type[0]));
-       atomic_type->type.type       = TYPE_ATOMIC;
-       atomic_type->type.qualifiers = qualifiers;
-       atomic_type->atype           = type;
+       type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
+       memset(type, 0, sizeof(atomic_type_t));
+
+       type->type            = TYPE_ATOMIC;
+       type->base.qualifiers = qualifiers;
+       type->atomic.atype    = atype;
 
-       return identify_new_type((type_t*) atomic_type);
+       return identify_new_type(type);
 }
 
 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
 {
-       pointer_type_t *pointer_type
-               = obstack_alloc(type_obst, sizeof(pointer_type[0]));
-       memset(pointer_type, 0, sizeof(pointer_type[0]));
-       pointer_type->type.type       = TYPE_POINTER;
-       pointer_type->type.qualifiers = qualifiers;
-       pointer_type->points_to       = points_to;
+       type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
+       memset(type, 0, sizeof(pointer_type_t));
+
+       type->type              = TYPE_POINTER;
+       type->base.qualifiers   = qualifiers;
+       type->pointer.points_to = points_to;
 
-       return identify_new_type((type_t*) pointer_type);
+       return identify_new_type(type);
 }
 
 static __attribute__((unused))
diff --git a/type.h b/type.h
index 44b721c..07dac8e 100644 (file)
--- a/type.h
+++ b/type.h
@@ -5,7 +5,7 @@
 #include <stdbool.h>
 #include "symbol.h"
 
-typedef struct type_t                type_t;
+typedef struct type_base_t           type_base_t;
 typedef struct atomic_type_t         atomic_type_t;
 typedef struct pointer_type_t        pointer_type_t;
 typedef struct function_parameter_t  function_parameter_t;
@@ -16,6 +16,7 @@ typedef struct builtin_type_t        builtin_type_t;
 typedef struct array_type_t          array_type_t;
 typedef struct typedef_type_t        typedef_type_t;
 typedef struct typeof_type_t         typeof_type_t;
+typedef union  type_t                type_t;
 
 void init_types(void);
 void exit_types(void);
index 1e07c1f..cedfb17 100644 (file)
@@ -116,7 +116,7 @@ static unsigned hash_type(const type_t *type)
        }
 
        unsigned some_prime = 99991;
-       hash ^= some_prime * type->qualifiers;
+       hash ^= some_prime * type->base.qualifiers;
 
        return hash;
 }
@@ -214,7 +214,7 @@ static bool types_equal(const type_t *type1, const type_t *type2)
                return true;
        if(type1->type != type2->type)
                return false;
-       if(type1->qualifiers != type2->qualifiers)
+       if(type1->base.qualifiers != type2->base.qualifiers)
                return false;
 
        switch(type1->type) {
index b7511e6..c3bde17 100644 (file)
--- a/type_t.h
+++ b/type_t.h
@@ -67,7 +67,7 @@ typedef enum {
 
 typedef unsigned int type_qualifiers_t;
 
-struct type_t {
+struct type_base_t {
        type_type_t       type;
        type_qualifiers_t qualifiers;
 
@@ -75,23 +75,23 @@ struct type_t {
 };
 
 struct atomic_type_t {
-       type_t              type;
+       type_base_t         type;
        atomic_type_type_t  atype;
 };
 
 struct builtin_type_t {
-       type_t    type;
-       symbol_t *symbol;
-       type_t   *real_type;
+       type_base_t  type;
+       symbol_t    *symbol;
+       type_t      *real_type;
 };
 
 struct pointer_type_t {
-       type_  type;
-       type_t  *points_to;
+       type_base_t  type;
+       type_t      *points_to;
 };
 
 struct array_type_t {
-       type_t        type;
+       type_base_t   type;
        type_t       *element_type;
        expression_t *size;
        bool          is_static;
@@ -104,7 +104,7 @@ struct function_parameter_t {
 };
 
 struct function_type_t {
-       type_t                type;
+       type_base_t           type;
        type_t               *result_type;
        function_parameter_t *parameters;
        bool                  variadic;
@@ -112,33 +112,47 @@ struct function_type_t {
 };
 
 struct compound_type_t {
-       type_t         type;
+       type_base_t    type;
        /** the declaration of the compound type, its context field
-        * contains the compound entries. */
+        *  contains the compound entries. */
        declaration_t *declaration;
 };
 
 struct enum_type_t {
-       type_t         type;
+       type_base_t    type;
        /** the declaration of the enum type. You can find the enum entries by
-        * walking the declaration->next list until you don't find
-        * STORAGE_CLASS_ENUM_ENTRY declarations anymore */
+        *  walking the declaration->next list until you don't find
+        *  STORAGE_CLASS_ENUM_ENTRY declarations anymore */
        declaration_t *declaration;
 };
 
 struct typedef_type_t {
-       type_t         type;
+       type_base_t    type;
        declaration_t *declaration;
        type_t        *resolved_type;
 };
 
 struct typeof_type_t {
-       type_t        type;
+       type_base_t   type;
        expression_t *expression;
        type_t       *typeof_type;
        type_t       *resolved_type;
 };
 
+union type_t {
+       type_type_t      type;
+       type_base_t      base;
+       atomic_type_t    atomic;
+       builtin_type_t   builtin;
+       pointer_type_t   pointer;
+       array_type_t     array;
+       function_type_t  function;
+       compound_type_t  compound;
+       enum_type_t      enumt;
+       typedef_type_t   typedeft;
+       typeof_type_t    typeoft;
+};
+
 type_t *make_atomic_type(atomic_type_type_t type, type_qualifiers_t qualifiers);
 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers);
 
@@ -153,7 +167,7 @@ static inline bool is_type_atomic(const type_t *type, atomic_type_type_t atype)
 
        if(type->type != TYPE_ATOMIC)
                return false;
-       const atomic_type_t *atomic_type = (const atomic_type_t*) type;
+       const atomic_type_t *atomic_type = &type->atomic;
 
        return atomic_type->atype == atype;
 }