comments, freeing routine
[libfirm] / ir / tr / type.c
index 4776a43..19bb13e 100644 (file)
@@ -86,9 +86,10 @@ void init_type(void) {
 }
 
 unsigned long type_visited;
-void set_master_type_visited(unsigned long val) { type_visited = val; }
-unsigned long get_master_type_visited() { return type_visited; }
-void inc_master_type_visited() { type_visited++; }
+
+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(); }
 
 
 type *
@@ -162,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) {
@@ -189,14 +186,12 @@ 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) {
@@ -219,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 */
-long
-get_type_nr(type *tp) {
-  assert(tp);
-#ifdef DEBUG_libfirm
-  return tp->nr;
-#else
-  return (long)tp;
-#endif
+long (get_type_nr)(type *tp) {
+  return __get_type_nr(tp);
 }
 
 const char* get_type_name(type *tp) {
@@ -245,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
@@ -260,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
@@ -320,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;
@@ -432,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;
     }
@@ -524,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;
@@ -600,9 +589,9 @@ bool smaller_type (type *st, type *lt) {
   return true;
 }
 
-/*******************************************************************/
-/** TYPE_CLASS                                                    **/
-/*******************************************************************/
+/*-----------------------------------------------------------------*/
+/* TYPE_CLASS                                                      */
+/*-----------------------------------------------------------------*/
 
 /* create a new class type */
 type   *new_type_class (ident *name) {
@@ -643,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));
@@ -655,11 +646,13 @@ 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));
@@ -810,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) {
@@ -829,9 +821,9 @@ bool is_subclass_of(type *low, type *high) {
   return false;
 }
 
-/*******************************************************************/
-/** TYPE_STRUCT                                                   **/
-/*******************************************************************/
+/*----------------------------------------------------------------**/
+/* TYPE_STRUCT                                                     */
+/*----------------------------------------------------------------**/
 
 /* create a new type struct */
 type   *new_type_struct (ident *name) {
@@ -861,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));
@@ -891,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;
@@ -919,17 +930,19 @@ build_value_type(ident *name, int len, type **tps) {
    N_param is the number of parameters, n_res the number of results.  */
 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;
 }
@@ -943,6 +956,7 @@ type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
 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));
@@ -963,6 +977,7 @@ 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));
@@ -971,6 +986,7 @@ type *get_method_param_type(type *method, int 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));
@@ -981,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) {
@@ -996,16 +1013,20 @@ entity *get_method_value_param_ent(type *method, int pos) {
   return get_struct_member(method->attr.ma.value_params, pos);
 }
 
-type *get_method_value_res_type(type *method) {
+/*
+ * 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));
@@ -1014,6 +1035,7 @@ type *get_method_res_type(type *method, int 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));
@@ -1025,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) {
@@ -1039,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)
 {
@@ -1064,15 +1095,46 @@ 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 */
 type  *new_type_union (ident *name) {
@@ -1161,14 +1223,13 @@ 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 */
@@ -1198,6 +1259,7 @@ 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);
@@ -1208,6 +1270,7 @@ type *new_d_type_array (ident *name, int n_dimensions,
 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);
@@ -1314,14 +1377,13 @@ 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 */
 type   *new_type_enumeration    (ident *name, int n_enums) {
@@ -1381,14 +1443,13 @@ 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 */
 type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
@@ -1423,9 +1484,8 @@ 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.
@@ -1443,9 +1503,9 @@ type *find_pointer_type_to_type (type *tp) {
 
 
 
-/*******************************************************************/
-/** TYPE_PRIMITIVE                                                **/
-/*******************************************************************/
+/*-----------------------------------------------------------------*/
+/* TYPE_PRIMITIVE                                                  */
+/*-----------------------------------------------------------------*/
 
 /* create a new type primitive */
 type *new_type_primitive (ident *name, ir_mode *mode) {
@@ -1470,20 +1530,17 @@ void free_primitive_attrs (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                                            */
+/*-----------------------------------------------------------------*/
 
 
-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);
 }
 
 /*
@@ -1535,10 +1592,7 @@ int is_compound_type(type *tp) {
 }
 
 
-
-
-
-#if 1 || DEBUG_libfirm
+#ifdef DEBUG_libfirm
 int dump_node_opcode(FILE *F, ir_node *n); /* from irdump.c */
 
 void dump_type (type *tp) {
@@ -1548,14 +1602,14 @@ void dump_type (type *tp) {
 
   switch (get_type_tpop_code(tp)) {
 
-  case tpo_class: {
+  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  suptertypes: ");
+    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));
@@ -1567,10 +1621,26 @@ void dump_type (type *tp) {
     }
 
     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("not implemented\n");
+    printf(": details not implemented\n");
   }
   printf("\n\n");
 }