typerep: freeing a type frees contained entities
authorMatthias Braun <matze@braunis.de>
Tue, 20 Dec 2011 16:16:03 +0000 (17:16 +0100)
committerMatthias Braun <matze@braunis.de>
Wed, 21 Dec 2011 17:21:21 +0000 (18:21 +0100)
Types "own" entities, so they should also free them when they get freed.

include/libfirm/typerep.h
ir/ir/irprog.c
ir/tr/type.c
ir/tr/type_t.h

index 59e61b9..c409f04 100644 (file)
@@ -1070,20 +1070,11 @@ FIRM_API int check_type(ir_type *tp);
  */
 FIRM_API int tr_verify(void);
 
-/** Frees all entities associated with a type.
- *  Does not free the array entity.
- *  Warning: ensure these entities are not referenced anywhere else.
- */
-FIRM_API void free_type_entities(ir_type *tp);
-
-/** Frees the memory used by the type.
+/**
+ * Frees the memory used by the type.
  *
- * Removes the type from the type list. Does not free the entities
- * belonging to the type, except for the array element entity.  Does
- * not free if tp is "none" or "unknown".  Frees entities in value
- * param subtypes of method types!!! Make sure these are not
- * referenced any more.  Further make sure there is no pointer type
- * that refers to this type.
+ * Removes the type from the type list and frees all entities
+ * belonging to the type.
  */
 FIRM_API void free_type(ir_type *tp);
 
index 1b26a7e..bd5a73c 100644 (file)
@@ -131,8 +131,8 @@ void free_ir_prog(void)
        for (i = get_irp_n_irgs(); i > 0;)
                free_ir_graph(get_irp_irg(--i));
 
-       free_type_entities(get_glob_type());
-       /* must iterate backwards here */
+       /* free entities first to avoid entity types being destroyed before
+        * the entities using them */
        for (i = get_irp_n_types(); i > 0;)
                free_type_entities(get_irp_type(--i));
 
index f28e687..4e08126 100644 (file)
@@ -145,10 +145,26 @@ ir_type *new_type(const tp_op *type_op, ir_mode *mode, type_dbg_info *db)
        return res;
 }
 
+void free_type_entities(ir_type *tp)
+{
+       const tp_op *op = get_type_tpop(tp);
+       if (op->ops.free_entities != NULL)
+               op->ops.free_entities(tp);
+}
+
+static void free_type_attrs(ir_type *tp)
+{
+       const tp_op *tpop = get_type_tpop(tp);
+
+       if (tpop->ops.free_attrs)
+               tpop->ops.free_attrs(tp);
+}
+
 void free_type(ir_type *tp)
 {
        const tp_op *op = get_type_tpop(tp);
 
+       free_type_entities(tp);
        /* Remove from list of all types */
        remove_irp_type(tp);
        /* Free the attributes of the type. */
@@ -163,22 +179,6 @@ void free_type(ir_type *tp)
        free(tp);
 }
 
-void free_type_entities(ir_type *tp)
-{
-       const tp_op *tpop = get_type_tpop(tp);
-
-       if (tpop->ops.free_entities)
-               tpop->ops.free_entities(tp);
-}
-
-void free_type_attrs(ir_type *tp)
-{
-       const tp_op *tpop = get_type_tpop(tp);
-
-       if (tpop->ops.free_attrs)
-               tpop->ops.free_attrs(tp);
-}
-
 void *(get_type_link)(const ir_type *tp)
 {
        return _get_type_link(tp);
index 19685bd..b5f3fd7 100644 (file)
@@ -196,7 +196,8 @@ struct ir_type {
  *           initialized.  The type is in state layout_undefined.
  */
 ir_type *new_type(const tp_op *type_op, ir_mode *mode, type_dbg_info *db);
-void free_type_attrs(ir_type *tp);
+
+void free_type_entities(ir_type *tp);
 
 void free_class_entities      (ir_type *clss);
 void free_struct_entities     (ir_type *strct);