becopyopt: Remove the unnecessary attribute name from struct copy_opt_t.
[libfirm] / ir / tr / tr_inheritance.c
index 03bc472..31f7043 100644 (file)
@@ -1,27 +1,12 @@
 /*
- * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
- *
  * This file is part of libFirm.
- *
- * This file may be distributed and/or modified under the terms of the
- * GNU General Public License version 2 as published by the Free Software
- * Foundation and appearing in the file LICENSE.GPL included in the
- * packaging of this file.
- *
- * Licensees holding valid libFirm Professional Edition licenses may use
- * this file in accordance with the libFirm Commercial License.
- * Agreement provided with the Software.
- *
- * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
- * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE.
+ * Copyright (C) 2012 University of Karlsruhe.
  */
 
 /**
- * @file    tr_inheritance.c
+ * @file
  * @brief   Utility routines for inheritance representation
  * @author  Goetz Lindenmaier
- * @version $Id$
  */
 #include "config.h"
 
@@ -35,8 +20,6 @@
 #include "irgwalk.h"
 #include "irflag.h"
 
-DEBUG_ONLY(static firm_dbg_module_t *dbg);
-
 /* ----------------------------------------------------------------------- */
 /* Resolve implicit inheritance.                                           */
 /* ----------------------------------------------------------------------- */
@@ -55,16 +38,14 @@ static void copy_entities_from_superclass(ir_type *clss, void *env)
        size_t k;
        size_t l;
        int overwritten;
-       ir_type *super, *inhenttype;
+       ir_type *super;
        ir_entity *inhent, *thisent;
        mangle_inherited_name_func *mfunc = *(mangle_inherited_name_func **)env;
 
        for (i = 0; i < get_class_n_supertypes(clss); i++) {
                super = get_class_supertype(clss, i);
-               assert(is_Class_type(super) && "not a class");
                for (j = 0; j < get_class_n_members(super); j++) {
                        inhent = get_class_member(super, j);
-                       inhenttype = get_entity_type(inhent);
                        /* check whether inhent is already overwritten */
                        overwritten = 0;
                        for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
@@ -95,10 +76,6 @@ static void copy_entities_from_superclass(ir_type *clss, void *env)
        }
 }
 
-/* Resolve implicit inheritance.
- *
- *  Resolves the implicit inheritance supplied by firm.
- */
 void resolve_inheritance(mangle_inherited_name_func *mfunc)
 {
        if (!mfunc)
@@ -178,7 +155,7 @@ static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size)
  */
 static inline unsigned int tr_inh_trans_hash(const tr_inh_trans_tp *v)
 {
-       return HASH_PTR(v->kind);
+       return hash_ptr(v->kind);
 }
 
 /* This always completes successfully. */
@@ -189,11 +166,11 @@ static tr_inh_trans_tp *get_firm_kind_entry(const firm_kind *k)
 
        if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
 
-       found = (tr_inh_trans_tp*)set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
+       found = set_find(tr_inh_trans_tp, tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
        if (!found) {
                a.directions[d_up]   = pset_new_ptr(16);
                a.directions[d_down] = pset_new_ptr(16);
-               found = (tr_inh_trans_tp*)set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
+               found = set_insert(tr_inh_trans_tp, tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
        }
        return found;
 }
@@ -239,8 +216,6 @@ static void compute_down_closure(ir_type *tp)
        size_t i, n_subtypes, n_members, n_supertypes;
        ir_visited_t master_visited = get_master_type_visited();
 
-       assert(is_Class_type(tp));
-
        set_type_visited(tp, master_visited-1);
 
        /* Recursive descend. */
@@ -294,8 +269,6 @@ static void compute_up_closure(ir_type *tp)
        size_t i, n_subtypes, n_members, n_supertypes;
        ir_visited_t master_visited = get_master_type_visited();
 
-       assert(is_Class_type(tp));
-
        set_type_visited(tp, master_visited-1);
 
        /* Recursive descend. */
@@ -343,18 +316,13 @@ static void compute_up_closure(ir_type *tp)
        }
 }
 
-/** Compute the transitive closure of the subclass/superclass and
- *  overwrites/overwrittenby relation.
- *
- *  This function walks over the ir (O(#types+#entities)) to compute the
- *  transitive closure.    */
 void compute_inh_transitive_closure(void)
 {
        size_t i, n_types = get_irp_n_types();
        free_inh_transitive_closure();
 
        /* The 'down' relation */
-       irp_reserve_resources(irp, IR_RESOURCE_TYPE_VISITED);
+       irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
        inc_master_type_visited();  /* Inc twice: one if on stack, second if values computed. */
        inc_master_type_visited();
        for (i = 0; i < n_types; ++i) {
@@ -403,16 +371,13 @@ void compute_inh_transitive_closure(void)
        }
 
        irp->inh_trans_closure_state = inh_transitive_closure_valid;
-       irp_free_resources(irp, IR_RESOURCE_TYPE_VISITED);
+       irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
 }
 
-/** Free memory occupied by the transitive closure information. */
 void free_inh_transitive_closure(void)
 {
        if (tr_inh_trans_set) {
-               tr_inh_trans_tp *elt;
-               for (elt = (tr_inh_trans_tp*)set_first(tr_inh_trans_set); elt != NULL;
-                    elt = (tr_inh_trans_tp*)set_next(tr_inh_trans_set)) {
+               foreach_set(tr_inh_trans_set, tr_inh_trans_tp, elt) {
                        del_pset(elt->directions[d_up]);
                        del_pset(elt->directions[d_down]);
                }
@@ -427,13 +392,13 @@ void free_inh_transitive_closure(void)
 ir_type *get_class_trans_subtype_first(const ir_type *tp)
 {
        assert_valid_state();
-       return (ir_type*)pset_first(get_type_map(tp, d_down));
+       return pset_first(ir_type, get_type_map(tp, d_down));
 }
 
 ir_type *get_class_trans_subtype_next(const ir_type *tp)
 {
        assert_valid_state();
-       return (ir_type*)pset_next(get_type_map(tp, d_down));
+       return pset_next(ir_type, get_type_map(tp, d_down));
 }
 
 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp)
@@ -447,13 +412,13 @@ int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp)
 ir_type *get_class_trans_supertype_first(const ir_type *tp)
 {
        assert_valid_state();
-       return (ir_type*)pset_first(get_type_map(tp, d_up));
+       return pset_first(ir_type, get_type_map(tp, d_up));
 }
 
 ir_type *get_class_trans_supertype_next(const ir_type *tp)
 {
        assert_valid_state();
-       return (ir_type*)pset_next(get_type_map(tp, d_up));
+       return pset_next(ir_type, get_type_map(tp, d_up));
 }
 
 /* - overwrittenby ------------------------------------------------------- */
@@ -461,29 +426,28 @@ ir_type *get_class_trans_supertype_next(const ir_type *tp)
 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent)
 {
        assert_valid_state();
-       return (ir_entity*)pset_first(get_entity_map(ent, d_down));
+       return pset_first(ir_entity, get_entity_map(ent, d_down));
 }
 
 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent)
 {
        assert_valid_state();
-       return (ir_entity*)pset_next(get_entity_map(ent, d_down));
+       return pset_next(ir_entity, get_entity_map(ent, d_down));
 }
 
 /* - overwrites ---------------------------------------------------------- */
 
 
-/** Iterate over all transitive overwritten entities. */
 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent)
 {
        assert_valid_state();
-       return (ir_entity*)pset_first(get_entity_map(ent, d_up));
+       return pset_first(ir_entity, get_entity_map(ent, d_up));
 }
 
 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent)
 {
        assert_valid_state();
-       return (ir_entity*)pset_next(get_entity_map(ent, d_up));
+       return pset_next(ir_entity, get_entity_map(ent, d_up));
 }
 
 
@@ -507,7 +471,6 @@ static int check_is_SubClass_of(ir_type *low, ir_type *high)
        return 0;
 }
 
-/* Returns true if low is subclass of high. */
 int is_SubClass_of(ir_type *low, ir_type *high)
 {
        assert(is_Class_type(low) && is_Class_type(high));
@@ -521,13 +484,6 @@ int is_SubClass_of(ir_type *low, ir_type *high)
        return check_is_SubClass_of(low, high);
 }
 
-
-/* Subclass check for pointers to classes.
- *
- *  Dereferences at both types the same amount of pointer types (as
- *  many as possible).  If the remaining types are both class types
- *  and subclasses, returns true, else false.  Can also be called with
- *  two class types.  */
 int is_SubClass_ptr_of(ir_type *low, ir_type *high)
 {
        while (is_Pointer_type(low) && is_Pointer_type(high)) {
@@ -573,7 +529,12 @@ static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *s
 {
        size_t i, n_overwrittenby;
 
-       if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
+       ir_type *owner = get_entity_owner(static_ent);
+       if (owner == dynamic_class) return static_ent;
+
+       // if the owner of the static_ent already is more special than the dynamic
+       // type to check against - stop here.
+       if (! is_SubClass_of(dynamic_class, owner)) return NULL;
 
        n_overwrittenby = get_entity_n_overwrittenby(static_ent);
        for (i = 0; i < n_overwrittenby; ++i) {
@@ -581,15 +542,11 @@ static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *s
                ent = do_resolve_ent_polymorphy(dynamic_class, ent);
                if (ent) return ent;
        }
-       return NULL;
+
+       // No further specialization of static_ent has been found
+       return static_ent;
 }
 
-/* Resolve polymorphy in the inheritance relation.
- *
- * Returns the dynamically referenced entity if the static entity and the
- * dynamic type are given.
- * Search downwards in overwritten tree.
- */
 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
 {
        ir_entity *res;
@@ -600,127 +557,3 @@ ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
 
        return res;
 }
-
-
-
-/* ----------------------------------------------------------------------- */
-/* Class cast state handling.                                              */
-/* ----------------------------------------------------------------------- */
-
-/* - State handling. ----------------------------------------- */
-
-void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s)
-{
-       if (get_irp_class_cast_state() > s)
-               set_irp_class_cast_state(s);
-       irg->class_cast_state = s;
-}
-
-ir_class_cast_state get_irg_class_cast_state(const ir_graph *irg)
-{
-       return irg->class_cast_state;
-}
-
-void set_irp_class_cast_state(ir_class_cast_state s)
-{
-#ifndef NDEBUG
-       size_t i, n;
-       for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
-               assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
-#endif
-       irp->class_cast_state = s;
-}
-
-ir_class_cast_state get_irp_class_cast_state(void)
-{
-       return irp->class_cast_state;
-}
-
-const char *get_class_cast_state_string(ir_class_cast_state s)
-{
-#define X(a)    case a: return #a
-       switch (s) {
-       X(ir_class_casts_any);
-       X(ir_class_casts_transitive);
-       X(ir_class_casts_normalized);
-       X(ir_class_casts_state_max);
-       default: return "invalid class cast state";
-       }
-#undef X
-}
-
-/* - State verification. ------------------------------------- */
-
-typedef struct ccs_env {
-       ir_class_cast_state expected_state;
-       ir_class_cast_state worst_situation;
-} ccs_env;
-
-/**
- * Walker: check Casts.
- */
-static void verify_irn_class_cast_state(ir_node *n, void *env)
-{
-       ccs_env             *ccs = (ccs_env *)env;
-       ir_class_cast_state this_state = ir_class_casts_any;
-       ir_type             *fromtype, *totype;
-       int                 ref_depth = 0;
-
-       if (!is_Cast(n)) return;
-
-       fromtype = get_irn_typeinfo_type(get_Cast_op(n));
-       totype   = get_Cast_type(n);
-
-       while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
-               totype   = get_pointer_points_to_type(totype);
-               fromtype = get_pointer_points_to_type(fromtype);
-               ref_depth++;
-       }
-
-       if (!is_Class_type(totype)) return;
-
-       if (is_SubClass_of(totype, fromtype) ||
-               is_SubClass_of(fromtype, totype)) {
-               this_state = ir_class_casts_transitive;
-               if ((get_class_supertype_index(totype, fromtype) != (size_t)-1) ||
-                   (get_class_supertype_index(fromtype, totype) != (size_t)-1) ||
-                   fromtype == totype) {
-                       this_state = ir_class_casts_normalized;
-               }
-       }
-
-       if (!(this_state >= ccs->expected_state)) {
-               ir_printf("  Node is %+F\n", n);
-               ir_printf("    totype   %+F\n", totype);
-               ir_printf("    fromtype %+F\n", fromtype);
-               ir_printf("    this_state: %s, exp. state: %s\n",
-                       get_class_cast_state_string(this_state),
-                       get_class_cast_state_string(ccs->expected_state));
-               assert(this_state >= ccs->expected_state &&
-                       "invalid state class cast state setting in graph");
-       }
-
-       if (this_state < ccs->worst_situation)
-               ccs->worst_situation = this_state;
-}
-
-/** Verify that the graph meets requirements of state set. */
-void verify_irg_class_cast_state(ir_graph *irg)
-{
-       ccs_env env;
-
-       FIRM_DBG_REGISTER(dbg, "firm.tr.inheritance");
-
-       env.expected_state  = get_irg_class_cast_state(irg);
-       env.worst_situation = ir_class_casts_normalized;
-
-       irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
-
-       if ((env.worst_situation > env.expected_state)) {
-               DB((dbg, LEVEL_1, "Note:  class cast state is set lower than reqired "
-                   "in graph \n\t%+F\n", irg));
-               DB((dbg, LEVEL_1, "       state is %s, reqired is %s\n",
-                       get_class_cast_state_string(env.expected_state),
-                       get_class_cast_state_string(env.worst_situation)));
-       }
-}