comments, freeing routine
[libfirm] / ir / tr / type.c
index 865e855..19bb13e 100644 (file)
 /** TYPE                                                          **/
 /*******************************************************************/
 
+type *none_type;    type *get_none_type(void)    { return none_type;    }
+type *unknown_type; type *get_unknown_type(void) { return unknown_type; }
+
+
 #ifdef DEBUG_libfirm
 /** Returns a new, unique number to number nodes or the like. */
 int get_irp_new_node_nr(void);
@@ -67,28 +71,28 @@ static ident *value_params_suffix = NULL;
 static ident *value_ress_suffix = NULL;
 
 void init_type(void) {
-  value_params_suffix = id_from_str(VALUE_PARAMS_SUFFIX, strlen(VALUE_PARAMS_SUFFIX));
-  value_ress_suffix   = id_from_str(VALUE_RESS_SUFFIX,   strlen(VALUE_RESS_SUFFIX));
+  value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
+  value_ress_suffix   = new_id_from_str(VALUE_RESS_SUFFIX);
+
+  /* construct none and unknown type. */
+  none_type    = new_type(tpop_none,    mode_BAD, new_id_from_str("type_none"));
+  set_type_size  (none_type, 0);
+  set_type_state (none_type, layout_fixed);
+  remove_irp_type(none_type);
+  unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"));
+  set_type_size  (unknown_type, 0);
+  set_type_state (unknown_type, layout_fixed);
+  remove_irp_type(unknown_type);
 }
 
 unsigned long type_visited;
-INLINE void set_master_type_visited(unsigned long val) { type_visited = val; }
-INLINE unsigned long get_master_type_visited() { return type_visited; }
-INLINE void inc_master_type_visited() { type_visited++; }
 
-void        free_type(type *tp) {
-  /* Remove from list of all types */
-  remove_irp_type(tp);
-  /* Free the attributes of the type. */
-  free_type_attrs(tp);
-  /* Free entities automatically allocated with the type */
-  if (is_array_type(tp))
-    free_entity(get_array_element_entity(tp));
-  /* And now the type itself... */
-  free(tp);
-}
+void (set_master_type_visited)(unsigned long val) { __set_master_type_visited(val); }
+unsigned long (get_master_type_visited)(void)     { return __get_master_type_visited(); }
+void (inc_master_type_visited)(void)              { __inc_master_type_visited(); }
+
 
-INLINE type *
+type *
 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
   type *res;
   int node_size ;
@@ -115,6 +119,35 @@ new_type(tp_op *type_op, ir_mode *mode, ident* name) {
   return res;
 }
 
+void        free_type(type *tp) {
+  if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
+    return;
+  /* Remove from list of all types */
+  remove_irp_type(tp);
+  /* Free the attributes of the type. */
+  free_type_attrs(tp);
+  /* Free entities automatically allocated with the type */
+  if (is_array_type(tp))
+    free_entity(get_array_element_entity(tp));
+  /* And now the type itself... */
+  tp->kind = k_BAD;
+  free(tp);
+}
+
+void free_type_entities(type *tp) {
+  switch(get_type_tpop_code(tp)) {
+  case tpo_class:       { free_class_entities(tp);       } break;
+  case tpo_struct:      { free_struct_entities(tp);      } break;
+  case tpo_method:      { free_method_entities(tp);      } break;
+  case tpo_union:       { free_union_entities(tp);       } break;
+  case tpo_array:       { free_array_entities(tp);       } break;
+  case tpo_enumeration: { free_enumeration_entities(tp); } break;
+  case tpo_pointer:     { free_pointer_entities(tp);     } break;
+  case tpo_primitive:   { free_primitive_entities(tp);   } break;
+  default: break;
+  }
+}
+
 void free_type_attrs(type *tp) {
   switch(get_type_tpop_code(tp)) {
   case tpo_class:       { free_class_attrs(tp);       } break;
@@ -130,26 +163,22 @@ void free_type_attrs(type *tp) {
 }
 
 /* set/get the link field */
-void *get_type_link(type *tp)
+void *(get_type_link)(type *tp)
 {
-  assert(tp && tp->kind == k_type);
-  return(tp -> link);
+  return __get_type_link(tp);
 }
 
-void set_type_link(type *tp, void *l)
+void (set_type_link)(type *tp, void *l)
 {
-  assert(tp && tp->kind == k_type);
-  tp -> link = l;
+  __set_type_link(tp, l);
 }
 
-tp_op*      get_type_tpop(type *tp) {
-  assert(tp && tp->kind == k_type);
-  return tp->type_op;
+tp_op *(get_type_tpop)(type *tp) {
+  return __get_type_tpop(tp);
 }
 
-ident*      get_type_tpop_nameid(type *tp) {
-  assert(tp && tp->kind == k_type);
-  return tp->type_op->name;
+ident *(get_type_tpop_nameid)(type *tp) {
+  return __get_type_tpop_nameid(tp);
 }
 
 const char* get_type_tpop_name(type *tp) {
@@ -157,25 +186,27 @@ const char* get_type_tpop_name(type *tp) {
   return get_id_str(tp->type_op->name);
 }
 
-tp_opcode    get_type_tpop_code(type *tp) {
-  assert(tp && tp->kind == k_type);
-  return tp->type_op->code;
+tp_opcode (get_type_tpop_code)(type *tp) {
+  return __get_type_tpop_code(tp);
 }
 
-ir_mode*    get_type_mode(type *tp) {
-  assert(tp && tp->kind == k_type);
-  return tp->mode;
+ir_mode *(get_type_mode)(type *tp) {
+  return __get_type_mode(tp);
 }
 
 void        set_type_mode(type *tp, ir_mode* m) {
   assert(tp && tp->kind == k_type);
 
-  assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
+  assert(((tp->type_op != type_primitive)   || mode_is_data(m))     &&
         /* Modes of primitives must be data */
-        ((tp->type_op != type_enumeration) || mode_is_int(m)));
+        ((tp->type_op != type_enumeration) || mode_is_int(m))      &&
          /* Modes of enumerations must be integers */
+        ((tp->type_op != type_pointer)     || mode_is_reference(m))   );
+        /* Modes of pointers must be references. */
 
-  if ((tp->type_op == type_primitive) || (tp->type_op == type_enumeration)) {
+  if ((tp->type_op == type_primitive)   ||
+      (tp->type_op == type_enumeration) ||
+      (tp->type_op == type_pointer)       ) {
     /* For pointer, primitive and enumeration size depends on the mode. */
     assert((get_mode_size_bytes(m) != -1) && "unorthodox modes not implemented");
     tp->size = get_mode_size_bytes(m);
@@ -183,25 +214,17 @@ void        set_type_mode(type *tp, ir_mode* m) {
   }
 }
 
-ident*      get_type_ident(type *tp) {
-  assert(tp && tp->kind == k_type);
-  return tp->name;
+ident *(get_type_ident)(type *tp) {
+  return __get_type_ident(tp);
 }
 
-void        set_type_ident(type *tp, ident* id) {
-  assert(tp && tp->kind == k_type);
-  tp->name = id;
+void (set_type_ident)(type *tp, ident* id) {
+  __set_type_ident(tp, id);
 }
 
 /* Outputs a unique number for this node */
-INLINE long
-get_type_nr(type *tp) {
-  assert(tp);
-#ifdef DEBUG_libfirm
-  return tp->nr;
-#else
-  return 0;
-#endif
+long (get_type_nr)(type *tp) {
+  return __get_type_nr(tp);
 }
 
 const char* get_type_name(type *tp) {
@@ -209,9 +232,8 @@ const char* get_type_name(type *tp) {
   return (get_id_str(tp->name));
 }
 
-int         get_type_size(type *tp) {
-  assert(tp && tp->kind == k_type);
-  return tp->size;
+int (get_type_size)(type *tp) {
+  return __get_type_size(tp);
 }
 
 void
@@ -224,10 +246,8 @@ set_type_size(type *tp, int size) {
     tp->size = size;
 }
 
-type_state
-get_type_state(type *tp) {
-  assert(tp && tp->kind == k_type);
-  return tp->state;
+type_state (get_type_state)(type *tp) {
+  return __get_type_state(tp);
 }
 
 void
@@ -250,8 +270,10 @@ set_type_state(type *tp, type_state state) {
            if (get_entity_offset(get_class_member(tp, i)) <= -1)
              { DDMT(tp); DDME(get_class_member(tp, i)); }
            assert(get_entity_offset(get_class_member(tp, i)) > -1);
+            /* TR ??
            assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
                   (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
+                   */
          }
       } break;
     case tpo_struct:
@@ -282,40 +304,31 @@ set_type_state(type *tp, type_state state) {
   tp->state = state;
 }
 
-unsigned long get_type_visited(type *tp) {
-  assert(tp && tp->kind == k_type);
-  return tp->visit;
+unsigned long (get_type_visited)(type *tp) {
+  return __get_type_visited(tp);
 }
 
-void        set_type_visited(type *tp, unsigned long num) {
-  assert(tp && tp->kind == k_type);
-  tp->visit = num;
+void (set_type_visited)(type *tp, unsigned long num) {
+  __set_type_visited(tp, num);
 }
+
 /* Sets visited field in type to type_visited. */
-void        mark_type_visited(type *tp) {
-  assert(tp && tp->kind == k_type);
-  assert(tp->visit < type_visited);
-  tp->visit = type_visited;
-}
-/* @@@ name clash with master flag
-bool          type_visited(type *tp) {
-  assert(tp && tp->kind == k_type);
-  return tp->visit >= type_visited;
-  }*/
-bool          type_not_visited(type *tp) {
-  assert(tp && tp->kind == k_type);
-  return tp->visit  < type_visited;
+void (mark_type_visited)(type *tp) {
+  __mark_type_visited(tp);
 }
 
+/* @@@ name clash with master flag
+int (type_visited)(type *tp) {
+  return __type_visited(tp);
+}*/
 
-int is_type            (void *thing) {
-  assert(thing);
-  if (get_kind(thing) == k_type)
-    return 1;
-  else
-    return 0;
+int (type_not_visited)(type *tp) {
+  return __type_not_visited(tp);
 }
 
+int (is_type)(void *thing) {
+  return __is_type(thing);
+}
 
 bool equal_type(type *typ1, type *typ2) {
   entity **m;
@@ -394,10 +407,23 @@ bool equal_type(type *typ1, type *typ2) {
     }
   } break;
   case tpo_method:      {
+    int n_param1, n_param2;
+
     if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
-    if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
-    if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
-    for (i = 0; i < get_method_n_params(typ1); i++) {
+    if (get_method_n_ress(typ1)      != get_method_n_ress(typ2)) return false;
+
+    if (get_method_variadicity(typ1) == variadicity_non_variadic) {
+      n_param1 = get_method_n_params(typ1);
+      n_param2 = get_method_n_params(typ2);
+    }
+    else {
+      n_param1 = get_method_first_variadic_param_index(typ1);
+      n_param2 = get_method_first_variadic_param_index(typ2);
+    }
+
+    if (n_param1 != n_param2) return false;
+
+    for (i = 0; i < n_param1; i++) {
       if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
        return false;
     }
@@ -486,6 +512,7 @@ bool smaller_type (type *st, type *lt) {
     }
   } break;
   case tpo_method:      {
+    /** FIXME: is this still true? */
     if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
     if (get_method_n_params(st) != get_method_n_params(lt)) return false;
     if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
@@ -562,12 +589,12 @@ bool smaller_type (type *st, type *lt) {
   return true;
 }
 
-/*******************************************************************/
-/** TYPE_CLASS                                                    **/
-/*******************************************************************/
+/*-----------------------------------------------------------------*/
+/* TYPE_CLASS                                                      */
+/*-----------------------------------------------------------------*/
 
 /* create a new class type */
-INLINE type   *new_type_class (ident *name) {
+type   *new_type_class (ident *name) {
   type *res;
 
   res = new_type(type_class, NULL, name);
@@ -585,7 +612,15 @@ type   *new_d_type_class (ident *name, dbg_info* db) {
   set_type_dbg_info(res, db);
   return res;
 }
-INLINE void free_class_attrs(type *clss) {
+
+void free_class_entities(type *clss) {
+  int i;
+  assert(clss && (clss->type_op == type_class));
+  for (i = get_class_n_members(clss)-1; i >= 0; --i)
+    free_entity(get_class_member(clss, i));
+}
+
+void free_class_attrs(type *clss) {
   assert(clss && (clss->type_op == type_class));
   DEL_ARR_F(clss->attr.ca.members);
   DEL_ARR_F(clss->attr.ca.subtypes);
@@ -597,10 +632,12 @@ void    add_class_member   (type *clss, entity *member) {
   assert(clss && (clss->type_op == type_class));
   ARR_APP1 (entity *, clss->attr.ca.members, member);
 }
+
 int     get_class_n_members (type *clss) {
   assert(clss && (clss->type_op == type_class));
   return (ARR_LEN (clss->attr.ca.members))-1;
 }
+
 int     get_class_member_index(type *clss, entity *mem) {
   int i;
   assert(clss && (clss->type_op == type_class));
@@ -609,11 +646,24 @@ int     get_class_member_index(type *clss, entity *mem) {
       return i;
   return -1;
 }
+
 entity *get_class_member   (type *clss, int pos) {
   assert(clss && (clss->type_op == type_class));
   assert(pos >= 0 && pos < get_class_n_members(clss));
   return clss->attr.ca.members[pos+1];
 }
+
+entity *get_class_member_by_name(type *clss, ident *name) {
+  int i, n_mem;
+  assert(clss && (clss->type_op == type_class));
+  n_mem = get_class_n_members(clss);
+  for (i = 0; i < n_mem; ++i) {
+    entity *mem = get_class_member(clss, i);
+    if (get_entity_ident(mem) == name) return mem;
+  }
+  return NULL;
+}
+
 void    set_class_member   (type *clss, entity *member, int pos) {
   assert(clss && (clss->type_op == type_class));
   assert(pos >= 0 && pos < get_class_n_members(clss));
@@ -724,11 +774,19 @@ void    remove_class_supertype(type *clss, type *supertype) {
     }
 }
 
-INLINE peculiarity get_class_peculiarity (type *clss) {
+char *get_peculiarity_string(peculiarity p) {
+  if (p == peculiarity_description)
+    return "peculiarity_description";
+  if (p == peculiarity_inherited)
+    return "peculiarity_inherited";
+  return "peculiarity_existent";
+}
+
+peculiarity get_class_peculiarity (type *clss) {
   assert(clss && (clss->type_op == type_class));
   return clss->attr.ca.peculiarity;
 }
-INLINE void        set_class_peculiarity (type *clss, peculiarity pec) {
+void        set_class_peculiarity (type *clss, peculiarity pec) {
   assert(clss && (clss->type_op == type_class));
   assert(pec != peculiarity_inherited);  /* There is no inheritance of types in libFirm. */
   clss->attr.ca.peculiarity = pec;
@@ -745,9 +803,8 @@ int get_class_dfn (type *clss)
 }
 
 /* typecheck */
-bool    is_class_type(type *clss) {
-  assert(clss);
-  if (clss->type_op == type_class) return 1; else return 0;
+int (is_class_type)(type *clss) {
+  return __is_class_type(clss);
 }
 
 bool is_subclass_of(type *low, type *high) {
@@ -764,12 +821,12 @@ bool is_subclass_of(type *low, type *high) {
   return false;
 }
 
-/*******************************************************************/
-/** TYPE_STRUCT                                                   **/
-/*******************************************************************/
+/*----------------------------------------------------------------**/
+/* TYPE_STRUCT                                                     */
+/*----------------------------------------------------------------**/
 
 /* create a new type struct */
-INLINE type   *new_type_struct (ident *name) {
+type   *new_type_struct (ident *name) {
   type *res;
   res = new_type(type_struct, NULL, name);
   res->attr.sa.members = NEW_ARR_F (entity *, 1);
@@ -780,7 +837,13 @@ type   *new_d_type_struct (ident *name, dbg_info* db) {
   set_type_dbg_info(res, db);
   return res;
 }
-INLINE void free_struct_attrs (type *strct) {
+void free_struct_entities (type *strct) {
+  int i;
+  assert(strct && (strct->type_op == type_struct));
+  for (i = get_struct_n_members(strct)-1; i >= 0; --i)
+    free_entity(get_struct_member(strct, i));
+}
+void free_struct_attrs (type *strct) {
   assert(strct && (strct->type_op == type_struct));
   DEL_ARR_F(strct->attr.sa.members);
 }
@@ -790,17 +853,29 @@ int     get_struct_n_members (type *strct) {
   assert(strct && (strct->type_op == type_struct));
   return (ARR_LEN (strct->attr.sa.members))-1;
 }
+
 void    add_struct_member   (type *strct, entity *member) {
   assert(strct && (strct->type_op == type_struct));
   assert(get_type_tpop(get_entity_type(member)) != type_method);
     /*    @@@ lowerfirm geht nicht durch */
   ARR_APP1 (entity *, strct->attr.sa.members, member);
 }
+
 entity *get_struct_member   (type *strct, int pos) {
   assert(strct && (strct->type_op == type_struct));
   assert(pos >= 0 && pos < get_struct_n_members(strct));
   return strct->attr.sa.members[pos+1];
 }
+
+int     get_struct_member_index(type *strct, entity *mem) {
+  int i;
+  assert(strct && (strct->type_op == type_struct));
+  for (i = 0; i < get_struct_n_members(strct); i++)
+    if (get_struct_member(strct, i) == mem)
+      return i;
+  return -1;
+}
+
 void    set_struct_member   (type *strct, int pos, entity *member) {
   assert(strct && (strct->type_op == type_struct));
   assert(pos >= 0 && pos < get_struct_n_members(strct));
@@ -820,16 +895,23 @@ void    remove_struct_member(type *strct, entity *member) {
 }
 
 /* typecheck */
-bool    is_struct_type(type *strct) {
-  assert(strct);
-  if (strct->type_op == type_struct) return 1; else return 0;
+int (is_struct_type)(type *strct) {
+  return __is_struct_type(strct);
 }
 
 /*******************************************************************/
 /** TYPE_METHOD                                                   **/
 /*******************************************************************/
 
-/* Lazy construction of value argument / result representation. */
+/**
+ * Lazy construction of value argument / result representation.
+ * Constructs a struct type and its member.  The types of the members
+ * are passed in the argument list.
+ *
+ * @param name    name of the type constructed
+ * @param len     number of fields
+ * @param tps     array of field types with length len
+ */
 static INLINE type *
 build_value_type(ident *name, int len, type **tps) {
   int i;
@@ -846,19 +928,21 @@ build_value_type(ident *name, int len, type **tps) {
 
 /* Create a new method type.
    N_param is the number of parameters, n_res the number of results.  */
-INLINE type *new_type_method (ident *name, int n_param, int n_res) {
+type *new_type_method (ident *name, int n_param, int n_res) {
   type *res;
-  res = new_type(type_method, mode_P_mach, name);
-  res->state = layout_fixed;
+
   assert((get_mode_size_bytes(mode_P_mach) != -1) && "unorthodox modes not implemented");
-  res->size = get_mode_size_bytes(mode_P_mach);
-  res->attr.ma.n_params     = n_param;
-  res->attr.ma.param_type   = (type **) xmalloc (sizeof (type *) * n_param);
-  res->attr.ma.value_params = NULL;
-  res->attr.ma.n_res        = n_res;
-  res->attr.ma.res_type     = (type **) xmalloc (sizeof (type *) * n_res);
-  res->attr.ma.value_ress   = NULL;
-  res->attr.ma.variadicity  = variadicity_non_variadic;
+  res = new_type(type_method, mode_P_mach, name);
+  res->state                        = layout_fixed;
+  res->size                         = get_mode_size_bytes(mode_P_mach);
+  res->attr.ma.n_params             = n_param;
+  res->attr.ma.param_type           = (type **) xmalloc (sizeof (type *) * n_param);
+  res->attr.ma.value_params         = NULL;
+  res->attr.ma.n_res                = n_res;
+  res->attr.ma.res_type             = (type **) xmalloc (sizeof (type *) * n_res);
+  res->attr.ma.value_ress           = NULL;
+  res->attr.ma.variadicity          = variadicity_non_variadic;
+  res->attr.ma.first_variadic_param = -1;
 
   return res;
 }
@@ -869,10 +953,23 @@ type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
   return res;
 }
 
-INLINE void free_method_attrs(type *method) {
+void free_method_entities(type *method) {
+  assert(method && (method->type_op == type_method));
+}
+
+/* Attention: also frees entities in value parameter subtypes! */
+void free_method_attrs(type *method) {
   assert(method && (method->type_op == type_method));
   free(method->attr.ma.param_type);
   free(method->attr.ma.res_type);
+  if (method->attr.ma.value_params) {
+    free_type_entities(method->attr.ma.value_params);
+    free_type(method->attr.ma.value_params);
+  }
+  if (method->attr.ma.value_ress) {
+    free_type_entities(method->attr.ma.value_ress);
+    free_type(method->attr.ma.value_ress);
+  }
 }
 
 /* manipulate private fields of method. */
@@ -880,11 +977,16 @@ int   get_method_n_params  (type *method) {
   assert(method && (method->type_op == type_method));
   return method->attr.ma.n_params;
 }
+
 type *get_method_param_type(type *method, int pos) {
+  type *res;
   assert(method && (method->type_op == type_method));
   assert(pos >= 0 && pos < get_method_n_params(method));
-  return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
+  res = method->attr.ma.param_type[pos];
+  assert(res != NULL && "empty method param type");
+  return method->attr.ma.param_type[pos] = skip_tid(res);
 }
+
 void  set_method_param_type(type *method, int pos, type* tp) {
   assert(method && (method->type_op == type_method));
   assert(pos >= 0 && pos < get_method_n_params(method));
@@ -895,6 +997,7 @@ void  set_method_param_type(type *method, int pos, type* tp) {
     set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
   }
 }
+
 /* Returns an entity that represents the copied value argument.  Only necessary
    for compounds passed by value. */
 entity *get_method_value_param_ent(type *method, int pos) {
@@ -904,20 +1007,35 @@ entity *get_method_value_param_ent(type *method, int pos) {
     method->attr.ma.value_params
       = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
                         get_method_n_params(method), method->attr.ma.param_type);
-  assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos)) != method->attr.ma.value_params)
+  assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos))
+         != method->attr.ma.value_params)
         && "param type not yet set");
   return get_struct_member(method->attr.ma.value_params, pos);
 }
 
+/*
+ * Returns a type that represents the copied value arguments.
+ */
+type *get_method_value_param_type(type *method)
+{
+  assert(method && (method->type_op == type_method));
+  return method->attr.ma.value_params;
+}
+
 int   get_method_n_ress   (type *method) {
   assert(method && (method->type_op == type_method));
   return method->attr.ma.n_res;
 }
+
 type *get_method_res_type(type *method, int pos) {
+  type *res;
   assert(method && (method->type_op == type_method));
   assert(pos >= 0 && pos < get_method_n_ress(method));
-  return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
+  res = method->attr.ma.res_type[pos];
+  assert(res != NULL && "empty method return type");
+  return method->attr.ma.res_type[pos] = skip_tid(res);
 }
+
 void  set_method_res_type(type *method, int pos, type* tp) {
   assert(method && (method->type_op == type_method));
   assert(pos >= 0 && pos < get_method_n_ress(method));
@@ -929,6 +1047,7 @@ void  set_method_res_type(type *method, int pos, type* tp) {
     set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
   }
 }
+
 /* Returns an entity that represents the copied value result.  Only necessary
    for compounds passed by value. */
 entity *get_method_value_res_ent(type *method, int pos) {
@@ -943,6 +1062,14 @@ entity *get_method_value_res_ent(type *method, int pos) {
   return get_struct_member(method->attr.ma.value_ress, pos);
 }
 
+/*
+ * Returns a type that represents the copied value results.
+ */
+type *get_method_value_res_type(type *method) {
+  assert(method && (method->type_op == type_method));
+  return method->attr.ma.value_ress;
+}
+
 /* Returns the null-terminated name of this variadicity. */
 const char *get_variadicity_name(variadicity vari)
 {
@@ -968,18 +1095,49 @@ void set_method_variadicity(type *method, variadicity vari)
   method->attr.ma.variadicity = vari;
 }
 
+/*
+ * Returns the first variadic parameter index of a type.
+ * If this index was NOT set, the index of the last parameter
+ * of the method type plus one is returned for variadic functions.
+ * Non-variadic function types always return -1 here.
+ */
+int get_method_first_variadic_param_index(type *method)
+{
+  assert(method && (method->type_op == type_method));
+
+  if (method->attr.ma.variadicity == variadicity_non_variadic)
+    return -1;
+
+  if (method->attr.ma.first_variadic_param == -1)
+    return get_method_n_params(method);
+  return method->attr.ma.first_variadic_param;
+}
+
+/*
+ * Sets the first variadic parameter index. This allows to specify
+ * a complete call type (containing the type of all parameters)
+ * but still have the knowledge, which parameter must be passed as
+ * variadic one.
+ */
+void set_method_first_variadic_param_index(type *method, int index)
+{
+  assert(method && (method->type_op == type_method));
+  assert(index >= 0 && index <= get_method_n_params(method));
+
+  method->attr.ma.first_variadic_param = index;
+}
+
 /* typecheck */
-bool  is_method_type     (type *method) {
-  assert(method);
-  if (method->type_op == type_method) return 1; else return 0;
+int (is_method_type)(type *method) {
+  return __is_method_type(method);
 }
 
-/*******************************************************************/
-/** TYPE_UNION                                                    **/
-/*******************************************************************/
+/*-----------------------------------------------------------------*/
+/* TYPE_UNION                                                      */
+/*-----------------------------------------------------------------*/
 
 /* create a new type uni */
-INLINE type  *new_type_union (ident *name) {
+type  *new_type_union (ident *name) {
   type *res;
   res = new_type(type_union, NULL, name);
   /*res->attr.ua.unioned_type = (type **)  xmalloc (sizeof (type *)  * n_types);
@@ -992,7 +1150,13 @@ type  *new_d_type_union (ident *name, dbg_info* db) {
   set_type_dbg_info(res, db);
   return res;
 }
-INLINE void free_union_attrs (type *uni) {
+void free_union_entities (type *uni) {
+  int i;
+  assert(uni && (uni->type_op == type_union));
+  for (i = get_union_n_members(uni)-1; i >= 0; --i)
+    free_entity(get_union_member(uni, i));
+}
+void free_union_attrs (type *uni) {
   assert(uni && (uni->type_op == type_union));
   DEL_ARR_F(uni->attr.ua.members);
 }
@@ -1059,18 +1223,17 @@ void   remove_union_member(type *uni, entity *member) {
 }
 
 /* typecheck */
-bool   is_union_type         (type *uni) {
-  assert(uni);
-  if (uni->type_op == type_union) return 1; else return 0;
+int (is_union_type)(type *uni) {
+  return __is_union_type(uni);
 }
 
-/*******************************************************************/
-/** TYPE_ARRAY                                                    **/
-/*******************************************************************/
+/*-----------------------------------------------------------------*/
+/* TYPE_ARRAY                                                      */
+/*-----------------------------------------------------------------*/
 
 
 /* create a new type array -- set dimension sizes independently */
-INLINE type *new_type_array         (ident *name, int n_dimensions,
+type *new_type_array         (ident *name, int n_dimensions,
                              type *element_type) {
   type *res;
   int i;
@@ -1085,8 +1248,8 @@ INLINE type *new_type_array         (ident *name, int n_dimensions,
 
   current_ir_graph = get_const_code_irg();
   for (i = 0; i < n_dimensions; i++) {
-    res->attr.aa.lower_bound[i]  = new_Unknown();
-    res->attr.aa.upper_bound[i]  = new_Unknown();
+    res->attr.aa.lower_bound[i]  = new_Unknown(mode_Iu);
+    res->attr.aa.upper_bound[i]  = new_Unknown(mode_Iu);
     res->attr.aa.order[i] = i;
   }
   current_ir_graph = rem;
@@ -1096,6 +1259,7 @@ INLINE type *new_type_array         (ident *name, int n_dimensions,
 
   return res;
 }
+
 type *new_d_type_array (ident *name, int n_dimensions,
                        type *element_type, dbg_info* db) {
   type *res = new_type_array (name, n_dimensions, element_type);
@@ -1103,7 +1267,11 @@ type *new_d_type_array (ident *name, int n_dimensions,
   return res;
 }
 
-INLINE void free_array_attrs (type *array) {
+void free_array_entities (type *array) {
+  assert(array && (array->type_op == type_array));
+}
+
+void free_array_attrs (type *array) {
   assert(array && (array->type_op == type_array));
   free(array->attr.aa.lower_bound);
   free(array->attr.aa.upper_bound);
@@ -1115,7 +1283,7 @@ int   get_array_n_dimensions (type *array) {
   return array->attr.aa.n_dimensions;
 }
 
-INLINE void
+void
 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
                  ir_node * upper_bound) {
   assert(array && (array->type_op == type_array));
@@ -1135,7 +1303,7 @@ set_array_bounds_int (type *array, int dimension, int lower_bound,
                    new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
   current_ir_graph = rem;
 }
-INLINE void
+void
 set_array_lower_bound  (type *array, int dimension, ir_node * lower_bound) {
   assert(array && (array->type_op == type_array));
   assert(lower_bound && "lower_bound node may not be NULL.");
@@ -1148,7 +1316,7 @@ void  set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
                          new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
   current_ir_graph = rem;
 }
-INLINE void
+void
 set_array_upper_bound  (type *array, int dimension, ir_node * upper_bound) {
   assert(array && (array->type_op == type_array));
   assert(upper_bound && "upper_bound node may not be NULL.");
@@ -1209,27 +1377,23 @@ entity *get_array_element_entity (type *array) {
 }
 
 /* typecheck */
-bool   is_array_type         (type *array) {
-  assert(array);
-  if (array->type_op == type_array) return 1; else return 0;
+int (is_array_type)(type *array) {
+  return __is_array_type(array);
 }
 
-/*******************************************************************/
-/** TYPE_ENUMERATION                                              **/
-/*******************************************************************/
+/*-----------------------------------------------------------------*/
+/* TYPE_ENUMERATION                                                */
+/*-----------------------------------------------------------------*/
 
 /* create a new type enumeration -- set the enumerators independently */
-INLINE type   *new_type_enumeration    (ident *name, int n_enums) {
+type   *new_type_enumeration    (ident *name, int n_enums) {
   type *res;
-  int i;
   res = new_type(type_enumeration, NULL, name);
   res->attr.ea.n_enums     = n_enums;
-  res->attr.ea.enumer      = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
-  res->attr.ea.enum_nameid = (ident  **) xmalloc (sizeof (ident  *) * n_enums);
-  for (i = 0; i < n_enums; i++) {
-    res->attr.ea.enumer[i] = NULL;
-    res->attr.ea.enum_nameid  = NULL;
-  }
+  res->attr.ea.enumer      = (tarval **)xmalloc(sizeof(res->attr.ea.enumer[0]) * n_enums);
+  res->attr.ea.enum_nameid = (ident  **)xmalloc(sizeof(res->attr.ea.enum_nameid[0]) * n_enums);
+  memset(res->attr.ea.enumer,      0, sizeof(res->attr.ea.enumer[0])      * n_enums);
+  memset(res->attr.ea.enum_nameid, 0, sizeof(res->attr.ea.enum_nameid[0]) * n_enums);
   return res;
 }
 type   *new_d_type_enumeration    (ident *name, int n_enums, dbg_info* db) {
@@ -1238,7 +1402,10 @@ type   *new_d_type_enumeration    (ident *name, int n_enums, dbg_info* db) {
   return res;
 }
 
-INLINE void free_enumeration_attrs(type *enumeration) {
+void free_enumeration_entities(type *enumeration) {
+  assert(enumeration && (enumeration->type_op == type_enumeration));
+}
+void free_enumeration_attrs(type *enumeration) {
   assert(enumeration && (enumeration->type_op == type_enumeration));
   free(enumeration->attr.ea.enumer);
   free(enumeration->attr.ea.enum_nameid);
@@ -1276,17 +1443,16 @@ const char *get_enumeration_name(type *enumeration, int pos) {
 }
 
 /* typecheck */
-bool    is_enumeration_type     (type *enumeration) {
-  assert(enumeration);
-  if (enumeration->type_op == type_enumeration) return 1; else return 0;
+int (is_enumeration_type)(type *enumeration) {
+  return __is_enumeration_type(enumeration);
 }
 
-/*******************************************************************/
-/** TYPE_POINTER                                                  **/
-/*******************************************************************/
+/*-----------------------------------------------------------------*/
+/* TYPE_POINTER                                                    */
+/*-----------------------------------------------------------------*/
 
 /* Create a new type pointer */
-INLINE type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
+type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
   type *res;
   assert(mode_is_reference(ptr_mode));
   res = new_type(type_pointer, ptr_mode, name);
@@ -1301,7 +1467,10 @@ type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_i
   set_type_dbg_info(res, db);
   return res;
 }
-INLINE void free_pointer_attrs (type *pointer) {
+void free_pointer_entities (type *pointer) {
+  assert(pointer && (pointer->type_op == type_pointer));
+}
+void free_pointer_attrs (type *pointer) {
   assert(pointer && (pointer->type_op == type_pointer));
 }
 /* manipulate fields of type_pointer */
@@ -1315,18 +1484,31 @@ type *get_pointer_points_to_type (type *pointer) {
 }
 
 /* typecheck */
-bool  is_pointer_type            (type *pointer) {
-  assert(pointer);
-  if (pointer->type_op == type_pointer) return 1; else return 0;
+int (is_pointer_type)(type *pointer) {
+  return __is_pointer_type(pointer);
+}
+
+/* Returns the first pointer type that has as points_to tp.
+ *  Not efficient: O(#types).
+ *  If not found returns unknown_type. */
+type *find_pointer_type_to_type (type *tp) {
+  int i;
+  for (i = 0; i < get_irp_n_types(); ++i) {
+    type *found = get_irp_type(i);
+    if (is_pointer_type(found) && get_pointer_points_to_type(found) == tp)
+      return (found);
+  }
+  return unknown_type;
 }
 
 
-/*******************************************************************/
-/** TYPE_PRIMITIVE                                                **/
-/*******************************************************************/
+
+/*-----------------------------------------------------------------*/
+/* TYPE_PRIMITIVE                                                  */
+/*-----------------------------------------------------------------*/
 
 /* create a new type primitive */
-INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
+type *new_type_primitive (ident *name, ir_mode *mode) {
   type *res;
   /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
   res = new_type(type_primitive, mode, name);
@@ -1340,25 +1522,25 @@ type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
   set_type_dbg_info(res, db);
   return res;
 }
-INLINE void free_primitive_attrs (type *primitive) {
+void free_primitive_entities (type *primitive) {
+  assert(primitive && (primitive->type_op == type_primitive));
+}
+void free_primitive_attrs (type *primitive) {
   assert(primitive && (primitive->type_op == type_primitive));
 }
 
 /* typecheck */
-bool  is_primitive_type  (type *primitive) {
-  assert(primitive && primitive->kind == k_type);
-  if (primitive->type_op == type_primitive) return 1; else return 0;
+int (is_primitive_type)(type *primitive) {
+  return __is_primitive_type(primitive);
 }
 
-/*******************************************************************/
-/** common functionality                                          **/
-/*******************************************************************/
+/*-----------------------------------------------------------------*/
+/* common functionality                                            */
+/*-----------------------------------------------------------------*/
 
 
-INLINE int is_atomic_type(type *tp) {
-  assert(tp && tp->kind == k_type);
-  return (is_primitive_type(tp) || is_pointer_type(tp) ||
-         is_enumeration_type(tp));
+int (is_atomic_type)(type *tp) {
+  return __is_atomic_type(tp);
 }
 
 /*
@@ -1403,8 +1585,65 @@ entity *get_compound_member(type *tp, int pos)
 }
 
 
-INLINE int is_compound_type(type *tp) {
+int is_compound_type(type *tp) {
   assert(tp && tp->kind == k_type);
   return (is_class_type(tp) || is_struct_type(tp) ||
          is_array_type(tp) || is_union_type(tp));
 }
+
+
+#ifdef DEBUG_libfirm
+int dump_node_opcode(FILE *F, ir_node *n); /* from irdump.c */
+
+void dump_type (type *tp) {
+  int i;
+
+  printf("%s type %s (%ld)", get_tpop_name(get_type_tpop(tp)), get_type_name(tp), get_type_nr(tp));
+
+  switch (get_type_tpop_code(tp)) {
+
+  case tpo_class:
+    printf("\n  members: ");
+    for (i = 0; i < get_class_n_members(tp); ++i) {
+      entity *mem = get_class_member(tp, i);
+      printf("\n    (%2d) %s:\t %s",
+            get_entity_offset(mem), get_type_name(get_entity_type(mem)), get_entity_name(mem));
+    }
+    printf("\n  supertypes: ");
+    for (i = 0; i < get_class_n_supertypes(tp); ++i) {
+      type *stp = get_class_supertype(tp, i);
+      printf("\n    %s", get_type_name(stp));
+    }
+    printf("\n  subtypes: ");
+    for (i = 0; i < get_class_n_subtypes(tp); ++i) {
+      type *stp = get_class_subtype(tp, i);
+      printf("\n    %s", get_type_name(stp));
+    }
+
+    printf("\n  peculiarity: %s", get_peculiarity_string(get_class_peculiarity(tp)));
+    break;
+
+  case tpo_union:
+  case tpo_struct:
+    printf("\n  members: ");
+    for (i = 0; i < get_compound_n_members(tp); ++i) {
+      entity *mem = get_compound_member(tp, i);
+      printf("\n    (%2d) %s:\t %s",
+            get_entity_offset(mem), get_type_name(get_entity_type(mem)), get_entity_name(mem));
+    }
+    break;
+
+  case tpo_pointer: {
+    type *tt = get_pointer_points_to_type(tp);
+
+    printf("\n  points to %s (%ld)", get_type_name(tt), get_type_nr(tt));
+  } break;
+
+  default:
+    printf(": details not implemented\n");
+  }
+  printf("\n\n");
+}
+#else  /* DEBUG_libfirm */
+void dump_type (type *tp) {}
+#endif /* DEBUG_libfirm */