Back to index

cell-binutils  2.17cvs20070401
debug.c
Go to the documentation of this file.
00001 /* debug.c -- Handle generic debugging information.
00002    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003
00003    Free Software Foundation, Inc.
00004    Written by Ian Lance Taylor <ian@cygnus.com>.
00005 
00006    This file is part of GNU Binutils.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
00021    02110-1301, USA.  */
00022 
00023 /* This file implements a generic debugging format.  We may eventually
00024    have readers which convert different formats into this generic
00025    format, and writers which write it out.  The initial impetus for
00026    this was writing a converter from stabs to HP IEEE-695 debugging
00027    format.  */
00028 
00029 #include <stdio.h>
00030 #include <assert.h>
00031 
00032 #include "bfd.h"
00033 #include "bucomm.h"
00034 #include "libiberty.h"
00035 #include "debug.h"
00036 
00037 /* Global information we keep for debugging.  A pointer to this
00038    structure is the debugging handle passed to all the routines.  */
00039 
00040 struct debug_handle
00041 {
00042   /* A linked list of compilation units.  */
00043   struct debug_unit *units;
00044   /* The current compilation unit.  */
00045   struct debug_unit *current_unit;
00046   /* The current source file.  */
00047   struct debug_file *current_file;
00048   /* The current function.  */
00049   struct debug_function *current_function;
00050   /* The current block.  */
00051   struct debug_block *current_block;
00052   /* The current line number information for the current unit.  */
00053   struct debug_lineno *current_lineno;
00054   /* Mark.  This is used by debug_write.  */
00055   unsigned int mark;
00056   /* A struct/class ID used by debug_write.  */
00057   unsigned int class_id;
00058   /* The base for class_id for this call to debug_write.  */
00059   unsigned int base_id;
00060   /* The current line number in debug_write.  */
00061   struct debug_lineno *current_write_lineno;
00062   unsigned int current_write_lineno_index;
00063   /* A list of classes which have assigned ID's during debug_write.
00064      This is linked through the next_id field of debug_class_type.  */
00065   struct debug_class_id *id_list;
00066   /* A list used to avoid recursion during debug_type_samep.  */
00067   struct debug_type_compare_list *compare_list;
00068 };
00069 
00070 /* Information we keep for a single compilation unit.  */
00071 
00072 struct debug_unit
00073 {
00074   /* The next compilation unit.  */
00075   struct debug_unit *next;
00076   /* A list of files included in this compilation unit.  The first
00077      file is always the main one, and that is where the main file name
00078      is stored.  */
00079   struct debug_file *files;
00080   /* Line number information for this compilation unit.  This is not
00081      stored by function, because assembler code may have line number
00082      information without function information.  */
00083   struct debug_lineno *linenos;
00084 };
00085 
00086 /* Information kept for a single source file.  */
00087 
00088 struct debug_file
00089 {
00090   /* The next source file in this compilation unit.  */
00091   struct debug_file *next;
00092   /* The name of the source file.  */
00093   const char *filename;
00094   /* Global functions, variables, types, etc.  */
00095   struct debug_namespace *globals;
00096 };
00097 
00098 /* A type.  */
00099 
00100 struct debug_type
00101 {
00102   /* Kind of type.  */
00103   enum debug_type_kind kind;
00104   /* Size of type (0 if not known).  */
00105   unsigned int size;
00106   /* Type which is a pointer to this type.  */
00107   debug_type pointer;
00108   /* Tagged union with additional information about the type.  */
00109   union
00110     {
00111       /* DEBUG_KIND_INDIRECT.  */
00112       struct debug_indirect_type *kindirect;
00113       /* DEBUG_KIND_INT.  */
00114       /* Whether the integer is unsigned.  */
00115       bfd_boolean kint;
00116       /* DEBUG_KIND_STRUCT, DEBUG_KIND_UNION, DEBUG_KIND_CLASS,
00117          DEBUG_KIND_UNION_CLASS.  */
00118       struct debug_class_type *kclass;
00119       /* DEBUG_KIND_ENUM.  */
00120       struct debug_enum_type *kenum;
00121       /* DEBUG_KIND_POINTER.  */
00122       struct debug_type *kpointer;
00123       /* DEBUG_KIND_FUNCTION.  */
00124       struct debug_function_type *kfunction;
00125       /* DEBUG_KIND_REFERENCE.  */
00126       struct debug_type *kreference;
00127       /* DEBUG_KIND_RANGE.  */
00128       struct debug_range_type *krange;
00129       /* DEBUG_KIND_ARRAY.  */
00130       struct debug_array_type *karray;
00131       /* DEBUG_KIND_SET.  */
00132       struct debug_set_type *kset;
00133       /* DEBUG_KIND_OFFSET.  */
00134       struct debug_offset_type *koffset;
00135       /* DEBUG_KIND_METHOD.  */
00136       struct debug_method_type *kmethod;
00137       /* DEBUG_KIND_CONST.  */
00138       struct debug_type *kconst;
00139       /* DEBUG_KIND_VOLATILE.  */
00140       struct debug_type *kvolatile;
00141       /* DEBUG_KIND_NAMED, DEBUG_KIND_TAGGED.  */
00142       struct debug_named_type *knamed;
00143     } u;
00144 };
00145 
00146 /* Information kept for an indirect type.  */
00147 
00148 struct debug_indirect_type
00149 {
00150   /* Slot where the final type will appear.  */
00151   debug_type *slot;
00152   /* Tag.  */
00153   const char *tag;
00154 };
00155 
00156 /* Information kept for a struct, union, or class.  */
00157 
00158 struct debug_class_type
00159 {
00160   /* NULL terminated array of fields.  */
00161   debug_field *fields;
00162   /* A mark field which indicates whether the struct has already been
00163      printed.  */
00164   unsigned int mark;
00165   /* This is used to uniquely identify unnamed structs when printing.  */
00166   unsigned int id;
00167   /* The remaining fields are only used for DEBUG_KIND_CLASS and
00168      DEBUG_KIND_UNION_CLASS.  */
00169   /* NULL terminated array of base classes.  */
00170   debug_baseclass *baseclasses;
00171   /* NULL terminated array of methods.  */
00172   debug_method *methods;
00173   /* The type of the class providing the virtual function table for
00174      this class.  This may point to the type itself.  */
00175   debug_type vptrbase;
00176 };
00177 
00178 /* Information kept for an enum.  */
00179 
00180 struct debug_enum_type
00181 {
00182   /* NULL terminated array of names.  */
00183   const char **names;
00184   /* Array of corresponding values.  */
00185   bfd_signed_vma *values;
00186 };
00187 
00188 /* Information kept for a function.  FIXME: We should be able to
00189    record the parameter types.  */
00190 
00191 struct debug_function_type
00192 {
00193   /* Return type.  */
00194   debug_type return_type;
00195   /* NULL terminated array of argument types.  */
00196   debug_type *arg_types;
00197   /* Whether the function takes a variable number of arguments.  */
00198   bfd_boolean varargs;
00199 };
00200 
00201 /* Information kept for a range.  */
00202 
00203 struct debug_range_type
00204 {
00205   /* Range base type.  */
00206   debug_type type;
00207   /* Lower bound.  */
00208   bfd_signed_vma lower;
00209   /* Upper bound.  */
00210   bfd_signed_vma upper;
00211 };
00212 
00213 /* Information kept for an array.  */
00214 
00215 struct debug_array_type
00216 {
00217   /* Element type.  */
00218   debug_type element_type;
00219   /* Range type.  */
00220   debug_type range_type;
00221   /* Lower bound.  */
00222   bfd_signed_vma lower;
00223   /* Upper bound.  */
00224   bfd_signed_vma upper;
00225   /* Whether this array is really a string.  */
00226   bfd_boolean stringp;
00227 };
00228 
00229 /* Information kept for a set.  */
00230 
00231 struct debug_set_type
00232 {
00233   /* Base type.  */
00234   debug_type type;
00235   /* Whether this set is really a bitstring.  */
00236   bfd_boolean bitstringp;
00237 };
00238 
00239 /* Information kept for an offset type (a based pointer).  */
00240 
00241 struct debug_offset_type
00242 {
00243   /* The type the pointer is an offset from.  */
00244   debug_type base_type;
00245   /* The type the pointer points to.  */
00246   debug_type target_type;
00247 };
00248 
00249 /* Information kept for a method type.  */
00250 
00251 struct debug_method_type
00252 {
00253   /* The return type.  */
00254   debug_type return_type;
00255   /* The object type which this method is for.  */
00256   debug_type domain_type;
00257   /* A NULL terminated array of argument types.  */
00258   debug_type *arg_types;
00259   /* Whether the method takes a variable number of arguments.  */
00260   bfd_boolean varargs;
00261 };
00262 
00263 /* Information kept for a named type.  */
00264 
00265 struct debug_named_type
00266 {
00267   /* Name.  */
00268   struct debug_name *name;
00269   /* Real type.  */
00270   debug_type type;
00271 };
00272 
00273 /* A field in a struct or union.  */
00274 
00275 struct debug_field
00276 {
00277   /* Name of the field.  */
00278   const char *name;
00279   /* Type of the field.  */
00280   struct debug_type *type;
00281   /* Visibility of the field.  */
00282   enum debug_visibility visibility;
00283   /* Whether this is a static member.  */
00284   bfd_boolean static_member;
00285   union
00286     {
00287       /* If static_member is false.  */
00288       struct
00289        {
00290          /* Bit position of the field in the struct.  */
00291          unsigned int bitpos;
00292          /* Size of the field in bits.  */
00293          unsigned int bitsize;
00294        } f;
00295       /* If static_member is true.  */
00296       struct
00297        {
00298          const char *physname;
00299        } s;
00300     } u;
00301 };
00302 
00303 /* A base class for an object.  */
00304 
00305 struct debug_baseclass
00306 {
00307   /* Type of the base class.  */
00308   struct debug_type *type;
00309   /* Bit position of the base class in the object.  */
00310   unsigned int bitpos;
00311   /* Whether the base class is virtual.  */
00312   bfd_boolean virtual;
00313   /* Visibility of the base class.  */
00314   enum debug_visibility visibility;
00315 };
00316 
00317 /* A method of an object.  */
00318 
00319 struct debug_method
00320 {
00321   /* The name of the method.  */
00322   const char *name;
00323   /* A NULL terminated array of different types of variants.  */
00324   struct debug_method_variant **variants;
00325 };
00326 
00327 /* The variants of a method function of an object.  These indicate
00328    which method to run.  */
00329 
00330 struct debug_method_variant
00331 {
00332   /* The physical name of the function.  */
00333   const char *physname;
00334   /* The type of the function.  */
00335   struct debug_type *type;
00336   /* The visibility of the function.  */
00337   enum debug_visibility visibility;
00338   /* Whether the function is const.  */
00339   bfd_boolean constp;
00340   /* Whether the function is volatile.  */
00341   bfd_boolean volatilep;
00342   /* The offset to the function in the virtual function table.  */
00343   bfd_vma voffset;
00344   /* If voffset is VOFFSET_STATIC_METHOD, this is a static method.  */
00345 #define VOFFSET_STATIC_METHOD ((bfd_vma) -1)
00346   /* Context of a virtual method function.  */
00347   struct debug_type *context;
00348 };
00349 
00350 /* A variable.  This is the information we keep for a variable object.
00351    This has no name; a name is associated with a variable in a
00352    debug_name structure.  */
00353 
00354 struct debug_variable
00355 {
00356   /* Kind of variable.  */
00357   enum debug_var_kind kind;
00358   /* Type.  */
00359   debug_type type;
00360   /* Value.  The interpretation of the value depends upon kind.  */
00361   bfd_vma val;
00362 };
00363 
00364 /* A function.  This has no name; a name is associated with a function
00365    in a debug_name structure.  */
00366 
00367 struct debug_function
00368 {
00369   /* Return type.  */
00370   debug_type return_type;
00371   /* Parameter information.  */
00372   struct debug_parameter *parameters;
00373   /* Block information.  The first structure on the list is the main
00374      block of the function, and describes function local variables.  */
00375   struct debug_block *blocks;
00376 };
00377 
00378 /* A function parameter.  */
00379 
00380 struct debug_parameter
00381 {
00382   /* Next parameter.  */
00383   struct debug_parameter *next;
00384   /* Name.  */
00385   const char *name;
00386   /* Type.  */
00387   debug_type type;
00388   /* Kind.  */
00389   enum debug_parm_kind kind;
00390   /* Value (meaning depends upon kind).  */
00391   bfd_vma val;
00392 };
00393 
00394 /* A typed constant.  */
00395 
00396 struct debug_typed_constant
00397 {
00398   /* Type.  */
00399   debug_type type;
00400   /* Value.  FIXME: We may eventually need to support non-integral
00401      values.  */
00402   bfd_vma val;
00403 };
00404 
00405 /* Information about a block within a function.  */
00406 
00407 struct debug_block
00408 {
00409   /* Next block with the same parent.  */
00410   struct debug_block *next;
00411   /* Parent block.  */
00412   struct debug_block *parent;
00413   /* List of child blocks.  */
00414   struct debug_block *children;
00415   /* Start address of the block.  */
00416   bfd_vma start;
00417   /* End address of the block.  */
00418   bfd_vma end;
00419   /* Local variables.  */
00420   struct debug_namespace *locals;
00421 };
00422 
00423 /* Line number information we keep for a compilation unit.  FIXME:
00424    This structure is easy to create, but can be very space
00425    inefficient.  */
00426 
00427 struct debug_lineno
00428 {
00429   /* More line number information for this block.  */
00430   struct debug_lineno *next;
00431   /* Source file.  */
00432   struct debug_file *file;
00433   /* Line numbers, terminated by a -1 or the end of the array.  */
00434 #define DEBUG_LINENO_COUNT 10
00435   unsigned long linenos[DEBUG_LINENO_COUNT];
00436   /* Addresses for the line numbers.  */
00437   bfd_vma addrs[DEBUG_LINENO_COUNT];
00438 };
00439 
00440 /* A namespace.  This is a mapping from names to objects.  FIXME: This
00441    should be implemented as a hash table.  */
00442 
00443 struct debug_namespace
00444 {
00445   /* List of items in this namespace.  */
00446   struct debug_name *list;
00447   /* Pointer to where the next item in this namespace should go.  */
00448   struct debug_name **tail;
00449 };
00450 
00451 /* Kinds of objects that appear in a namespace.  */
00452 
00453 enum debug_object_kind
00454 {
00455   /* A type.  */
00456   DEBUG_OBJECT_TYPE,
00457   /* A tagged type (really a different sort of namespace).  */
00458   DEBUG_OBJECT_TAG,
00459   /* A variable.  */
00460   DEBUG_OBJECT_VARIABLE,
00461   /* A function.  */
00462   DEBUG_OBJECT_FUNCTION,
00463   /* An integer constant.  */
00464   DEBUG_OBJECT_INT_CONSTANT,
00465   /* A floating point constant.  */
00466   DEBUG_OBJECT_FLOAT_CONSTANT,
00467   /* A typed constant.  */
00468   DEBUG_OBJECT_TYPED_CONSTANT
00469 };
00470 
00471 /* Linkage of an object that appears in a namespace.  */
00472 
00473 enum debug_object_linkage
00474 {
00475   /* Local variable.  */
00476   DEBUG_LINKAGE_AUTOMATIC,
00477   /* Static--either file static or function static, depending upon the
00478      namespace is.  */
00479   DEBUG_LINKAGE_STATIC,
00480   /* Global.  */
00481   DEBUG_LINKAGE_GLOBAL,
00482   /* No linkage.  */
00483   DEBUG_LINKAGE_NONE
00484 };
00485 
00486 /* A name in a namespace.  */
00487 
00488 struct debug_name
00489 {
00490   /* Next name in this namespace.  */
00491   struct debug_name *next;
00492   /* Name.  */
00493   const char *name;
00494   /* Mark.  This is used by debug_write.  */
00495   unsigned int mark;
00496   /* Kind of object.  */
00497   enum debug_object_kind kind;
00498   /* Linkage of object.  */
00499   enum debug_object_linkage linkage;
00500   /* Tagged union with additional information about the object.  */
00501   union
00502     {
00503       /* DEBUG_OBJECT_TYPE.  */
00504       struct debug_type *type;
00505       /* DEBUG_OBJECT_TAG.  */
00506       struct debug_type *tag;
00507       /* DEBUG_OBJECT_VARIABLE.  */
00508       struct debug_variable *variable;
00509       /* DEBUG_OBJECT_FUNCTION.  */
00510       struct debug_function *function;
00511       /* DEBUG_OBJECT_INT_CONSTANT.  */
00512       bfd_vma int_constant;
00513       /* DEBUG_OBJECT_FLOAT_CONSTANT.  */
00514       double float_constant;
00515       /* DEBUG_OBJECT_TYPED_CONSTANT.  */
00516       struct debug_typed_constant *typed_constant;
00517     } u;
00518 };
00519 
00520 /* During debug_write, a linked list of these structures is used to
00521    keep track of ID numbers that have been assigned to classes.  */
00522 
00523 struct debug_class_id
00524 {
00525   /* Next ID number.  */
00526   struct debug_class_id *next;
00527   /* The type with the ID.  */
00528   struct debug_type *type;
00529   /* The tag; NULL if no tag.  */
00530   const char *tag;
00531 };
00532 
00533 /* During debug_type_samep, a linked list of these structures is kept
00534    on the stack to avoid infinite recursion.  */
00535 
00536 struct debug_type_compare_list
00537 {
00538   /* Next type on list.  */
00539   struct debug_type_compare_list *next;
00540   /* The types we are comparing.  */
00541   struct debug_type *t1;
00542   struct debug_type *t2;
00543 };
00544 
00545 /* During debug_get_real_type, a linked list of these structures is
00546    kept on the stack to avoid infinite recursion.  */
00547 
00548 struct debug_type_real_list
00549 {
00550   /* Next type on list.  */
00551   struct debug_type_real_list *next;
00552   /* The type we are checking.  */
00553   struct debug_type *t;
00554 };
00555 
00556 /* Local functions.  */
00557 
00558 static void debug_error (const char *);
00559 static struct debug_name *debug_add_to_namespace
00560   (struct debug_handle *, struct debug_namespace **, const char *,
00561    enum debug_object_kind, enum debug_object_linkage);
00562 static struct debug_name *debug_add_to_current_namespace
00563   (struct debug_handle *, const char *, enum debug_object_kind,
00564    enum debug_object_linkage);
00565 static struct debug_type *debug_make_type
00566   (struct debug_handle *, enum debug_type_kind, unsigned int);
00567 static struct debug_type *debug_get_real_type
00568   (void *, debug_type, struct debug_type_real_list *);
00569 static bfd_boolean debug_write_name
00570   (struct debug_handle *, const struct debug_write_fns *, void *,
00571    struct debug_name *);
00572 static bfd_boolean debug_write_type
00573   (struct debug_handle *, const struct debug_write_fns *, void *,
00574    struct debug_type *, struct debug_name *);
00575 static bfd_boolean debug_write_class_type
00576   (struct debug_handle *, const struct debug_write_fns *, void *,
00577    struct debug_type *, const char *);
00578 static bfd_boolean debug_write_function
00579   (struct debug_handle *, const struct debug_write_fns *, void *,
00580    const char *, enum debug_object_linkage, struct debug_function *);
00581 static bfd_boolean debug_write_block
00582   (struct debug_handle *, const struct debug_write_fns *, void *,
00583    struct debug_block *);
00584 static bfd_boolean debug_write_linenos
00585   (struct debug_handle *, const struct debug_write_fns *, void *, bfd_vma);
00586 static bfd_boolean debug_set_class_id
00587   (struct debug_handle *, const char *, struct debug_type *);
00588 static bfd_boolean debug_type_samep
00589   (struct debug_handle *, struct debug_type *, struct debug_type *);
00590 static bfd_boolean debug_class_type_samep
00591   (struct debug_handle *, struct debug_type *, struct debug_type *);
00592 
00593 /* Issue an error message.  */
00594 
00595 static void
00596 debug_error (const char *message)
00597 {
00598   fprintf (stderr, "%s\n", message);
00599 }
00600 
00601 /* Add an object to a namespace.  */
00602 
00603 static struct debug_name *
00604 debug_add_to_namespace (struct debug_handle *info ATTRIBUTE_UNUSED,
00605                      struct debug_namespace **nsp, const char *name,
00606                      enum debug_object_kind kind,
00607                      enum debug_object_linkage linkage)
00608 {
00609   struct debug_name *n;
00610   struct debug_namespace *ns;
00611 
00612   n = (struct debug_name *) xmalloc (sizeof *n);
00613   memset (n, 0, sizeof *n);
00614 
00615   n->name = name;
00616   n->kind = kind;
00617   n->linkage = linkage;
00618 
00619   ns = *nsp;
00620   if (ns == NULL)
00621     {
00622       ns = (struct debug_namespace *) xmalloc (sizeof *ns);
00623       memset (ns, 0, sizeof *ns);
00624 
00625       ns->tail = &ns->list;
00626 
00627       *nsp = ns;
00628     }
00629 
00630   *ns->tail = n;
00631   ns->tail = &n->next;
00632 
00633   return n;
00634 }
00635 
00636 /* Add an object to the current namespace.  */
00637 
00638 static struct debug_name *
00639 debug_add_to_current_namespace (struct debug_handle *info, const char *name,
00640                             enum debug_object_kind kind,
00641                             enum debug_object_linkage linkage)
00642 {
00643   struct debug_namespace **nsp;
00644 
00645   if (info->current_unit == NULL
00646       || info->current_file == NULL)
00647     {
00648       debug_error (_("debug_add_to_current_namespace: no current file"));
00649       return NULL;
00650     }
00651 
00652   if (info->current_block != NULL)
00653     nsp = &info->current_block->locals;
00654   else
00655     nsp = &info->current_file->globals;
00656 
00657   return debug_add_to_namespace (info, nsp, name, kind, linkage);
00658 }
00659 
00660 /* Return a handle for debugging information.  */
00661 
00662 void *
00663 debug_init (void)
00664 {
00665   struct debug_handle *ret;
00666 
00667   ret = (struct debug_handle *) xmalloc (sizeof *ret);
00668   memset (ret, 0, sizeof *ret);
00669   return (void *) ret;
00670 }
00671 
00672 /* Set the source filename.  This implicitly starts a new compilation
00673    unit.  */
00674 
00675 bfd_boolean
00676 debug_set_filename (void *handle, const char *name)
00677 {
00678   struct debug_handle *info = (struct debug_handle *) handle;
00679   struct debug_file *nfile;
00680   struct debug_unit *nunit;
00681 
00682   if (name == NULL)
00683     name = "";
00684 
00685   nfile = (struct debug_file *) xmalloc (sizeof *nfile);
00686   memset (nfile, 0, sizeof *nfile);
00687 
00688   nfile->filename = name;
00689 
00690   nunit = (struct debug_unit *) xmalloc (sizeof *nunit);
00691   memset (nunit, 0, sizeof *nunit);
00692 
00693   nunit->files = nfile;
00694   info->current_file = nfile;
00695 
00696   if (info->current_unit != NULL)
00697     info->current_unit->next = nunit;
00698   else
00699     {
00700       assert (info->units == NULL);
00701       info->units = nunit;
00702     }
00703 
00704   info->current_unit = nunit;
00705 
00706   info->current_function = NULL;
00707   info->current_block = NULL;
00708   info->current_lineno = NULL;
00709 
00710   return TRUE;
00711 }
00712 
00713 /* Change source files to the given file name.  This is used for
00714    include files in a single compilation unit.  */
00715 
00716 bfd_boolean
00717 debug_start_source (void *handle, const char *name)
00718 {
00719   struct debug_handle *info = (struct debug_handle *) handle;
00720   struct debug_file *f, **pf;
00721 
00722   if (name == NULL)
00723     name = "";
00724 
00725   if (info->current_unit == NULL)
00726     {
00727       debug_error (_("debug_start_source: no debug_set_filename call"));
00728       return FALSE;
00729     }
00730 
00731   for (f = info->current_unit->files; f != NULL; f = f->next)
00732     {
00733       if (f->filename[0] == name[0]
00734          && f->filename[1] == name[1]
00735          && strcmp (f->filename, name) == 0)
00736        {
00737          info->current_file = f;
00738          return TRUE;
00739        }
00740     }
00741 
00742   f = (struct debug_file *) xmalloc (sizeof *f);
00743   memset (f, 0, sizeof *f);
00744 
00745   f->filename = name;
00746 
00747   for (pf = &info->current_file->next;
00748        *pf != NULL;
00749        pf = &(*pf)->next)
00750     ;
00751   *pf = f;
00752 
00753   info->current_file = f;
00754 
00755   return TRUE;
00756 }
00757 
00758 /* Record a function definition.  This implicitly starts a function
00759    block.  The debug_type argument is the type of the return value.
00760    The boolean indicates whether the function is globally visible.
00761    The bfd_vma is the address of the start of the function.  Currently
00762    the parameter types are specified by calls to
00763    debug_record_parameter.  FIXME: There is no way to specify nested
00764    functions.  */
00765 
00766 bfd_boolean
00767 debug_record_function (void *handle, const char *name,
00768                      debug_type return_type, bfd_boolean global,
00769                      bfd_vma addr)
00770 {
00771   struct debug_handle *info = (struct debug_handle *) handle;
00772   struct debug_function *f;
00773   struct debug_block *b;
00774   struct debug_name *n;
00775 
00776   if (name == NULL)
00777     name = "";
00778   if (return_type == NULL)
00779     return FALSE;
00780 
00781   if (info->current_unit == NULL)
00782     {
00783       debug_error (_("debug_record_function: no debug_set_filename call"));
00784       return FALSE;
00785     }
00786 
00787   f = (struct debug_function *) xmalloc (sizeof *f);
00788   memset (f, 0, sizeof *f);
00789 
00790   f->return_type = return_type;
00791 
00792   b = (struct debug_block *) xmalloc (sizeof *b);
00793   memset (b, 0, sizeof *b);
00794 
00795   b->start = addr;
00796   b->end = (bfd_vma) -1;
00797 
00798   f->blocks = b;
00799 
00800   info->current_function = f;
00801   info->current_block = b;
00802 
00803   /* FIXME: If we could handle nested functions, this would be the
00804      place: we would want to use a different namespace.  */
00805   n = debug_add_to_namespace (info,
00806                            &info->current_file->globals,
00807                            name,
00808                            DEBUG_OBJECT_FUNCTION,
00809                            (global
00810                             ? DEBUG_LINKAGE_GLOBAL
00811                             : DEBUG_LINKAGE_STATIC));
00812   if (n == NULL)
00813     return FALSE;
00814 
00815   n->u.function = f;
00816 
00817   return TRUE;
00818 }
00819 
00820 /* Record a parameter for the current function.  */
00821 
00822 bfd_boolean
00823 debug_record_parameter (void *handle, const char *name, debug_type type,
00824                      enum debug_parm_kind kind, bfd_vma val)
00825 {
00826   struct debug_handle *info = (struct debug_handle *) handle;
00827   struct debug_parameter *p, **pp;
00828 
00829   if (name == NULL || type == NULL)
00830     return FALSE;
00831 
00832   if (info->current_unit == NULL
00833       || info->current_function == NULL)
00834     {
00835       debug_error (_("debug_record_parameter: no current function"));
00836       return FALSE;
00837     }
00838 
00839   p = (struct debug_parameter *) xmalloc (sizeof *p);
00840   memset (p, 0, sizeof *p);
00841 
00842   p->name = name;
00843   p->type = type;
00844   p->kind = kind;
00845   p->val = val;
00846 
00847   for (pp = &info->current_function->parameters;
00848        *pp != NULL;
00849        pp = &(*pp)->next)
00850     ;
00851   *pp = p;
00852 
00853   return TRUE;
00854 }
00855 
00856 /* End a function.  FIXME: This should handle function nesting.  */
00857 
00858 bfd_boolean
00859 debug_end_function (void *handle, bfd_vma addr)
00860 {
00861   struct debug_handle *info = (struct debug_handle *) handle;
00862 
00863   if (info->current_unit == NULL
00864       || info->current_block == NULL
00865       || info->current_function == NULL)
00866     {
00867       debug_error (_("debug_end_function: no current function"));
00868       return FALSE;
00869     }
00870 
00871   if (info->current_block->parent != NULL)
00872     {
00873       debug_error (_("debug_end_function: some blocks were not closed"));
00874       return FALSE;
00875     }
00876 
00877   info->current_block->end = addr;
00878 
00879   info->current_function = NULL;
00880   info->current_block = NULL;
00881 
00882   return TRUE;
00883 }
00884 
00885 /* Start a block in a function.  All local information will be
00886    recorded in this block, until the matching call to debug_end_block.
00887    debug_start_block and debug_end_block may be nested.  The bfd_vma
00888    argument is the address at which this block starts.  */
00889 
00890 bfd_boolean
00891 debug_start_block (void *handle, bfd_vma addr)
00892 {
00893   struct debug_handle *info = (struct debug_handle *) handle;
00894   struct debug_block *b, **pb;
00895 
00896   /* We must always have a current block: debug_record_function sets
00897      one up.  */
00898   if (info->current_unit == NULL
00899       || info->current_block == NULL)
00900     {
00901       debug_error (_("debug_start_block: no current block"));
00902       return FALSE;
00903     }
00904 
00905   b = (struct debug_block *) xmalloc (sizeof *b);
00906   memset (b, 0, sizeof *b);
00907 
00908   b->parent = info->current_block;
00909   b->start = addr;
00910   b->end = (bfd_vma) -1;
00911 
00912   /* This new block is a child of the current block.  */
00913   for (pb = &info->current_block->children;
00914        *pb != NULL;
00915        pb = &(*pb)->next)
00916     ;
00917   *pb = b;
00918 
00919   info->current_block = b;
00920 
00921   return TRUE;
00922 }
00923 
00924 /* Finish a block in a function.  This matches the call to
00925    debug_start_block.  The argument is the address at which this block
00926    ends.  */
00927 
00928 bfd_boolean
00929 debug_end_block (void *handle, bfd_vma addr)
00930 {
00931   struct debug_handle *info = (struct debug_handle *) handle;
00932   struct debug_block *parent;
00933 
00934   if (info->current_unit == NULL
00935       || info->current_block == NULL)
00936     {
00937       debug_error (_("debug_end_block: no current block"));
00938       return FALSE;
00939     }
00940 
00941   parent = info->current_block->parent;
00942   if (parent == NULL)
00943     {
00944       debug_error (_("debug_end_block: attempt to close top level block"));
00945       return FALSE;
00946     }
00947 
00948   info->current_block->end = addr;
00949 
00950   info->current_block = parent;
00951 
00952   return TRUE;
00953 }
00954 
00955 /* Associate a line number in the current source file and function
00956    with a given address.  */
00957 
00958 bfd_boolean
00959 debug_record_line (void *handle, unsigned long lineno, bfd_vma addr)
00960 {
00961   struct debug_handle *info = (struct debug_handle *) handle;
00962   struct debug_lineno *l;
00963   unsigned int i;
00964 
00965   if (info->current_unit == NULL)
00966     {
00967       debug_error (_("debug_record_line: no current unit"));
00968       return FALSE;
00969     }
00970 
00971   l = info->current_lineno;
00972   if (l != NULL && l->file == info->current_file)
00973     {
00974       for (i = 0; i < DEBUG_LINENO_COUNT; i++)
00975        {
00976          if (l->linenos[i] == (unsigned long) -1)
00977            {
00978              l->linenos[i] = lineno;
00979              l->addrs[i] = addr;
00980              return TRUE;
00981            }
00982        }
00983     }
00984 
00985   /* If we get here, then either 1) there is no current_lineno
00986      structure, which means this is the first line number in this
00987      compilation unit, 2) the current_lineno structure is for a
00988      different file, or 3) the current_lineno structure is full.
00989      Regardless, we want to allocate a new debug_lineno structure, put
00990      it in the right place, and make it the new current_lineno
00991      structure.  */
00992 
00993   l = (struct debug_lineno *) xmalloc (sizeof *l);
00994   memset (l, 0, sizeof *l);
00995 
00996   l->file = info->current_file;
00997   l->linenos[0] = lineno;
00998   l->addrs[0] = addr;
00999   for (i = 1; i < DEBUG_LINENO_COUNT; i++)
01000     l->linenos[i] = (unsigned long) -1;
01001 
01002   if (info->current_lineno != NULL)
01003     info->current_lineno->next = l;
01004   else
01005     info->current_unit->linenos = l;
01006 
01007   info->current_lineno = l;
01008 
01009   return TRUE;
01010 }
01011 
01012 /* Start a named common block.  This is a block of variables that may
01013    move in memory.  */
01014 
01015 bfd_boolean
01016 debug_start_common_block (void *handle ATTRIBUTE_UNUSED,
01017                        const char *name ATTRIBUTE_UNUSED)
01018 {
01019   /* FIXME */
01020   debug_error (_("debug_start_common_block: not implemented"));
01021   return FALSE;
01022 }
01023 
01024 /* End a named common block.  */
01025 
01026 bfd_boolean
01027 debug_end_common_block (void *handle ATTRIBUTE_UNUSED,
01028                      const char *name ATTRIBUTE_UNUSED)
01029 {
01030   /* FIXME */
01031   debug_error (_("debug_end_common_block: not implemented"));
01032   return FALSE;
01033 }
01034 
01035 /* Record a named integer constant.  */
01036 
01037 bfd_boolean
01038 debug_record_int_const (void *handle, const char *name, bfd_vma val)
01039 {
01040   struct debug_handle *info = (struct debug_handle *) handle;
01041   struct debug_name *n;
01042 
01043   if (name == NULL)
01044     return FALSE;
01045 
01046   n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_INT_CONSTANT,
01047                                   DEBUG_LINKAGE_NONE);
01048   if (n == NULL)
01049     return FALSE;
01050 
01051   n->u.int_constant = val;
01052 
01053   return TRUE;
01054 }
01055 
01056 /* Record a named floating point constant.  */
01057 
01058 bfd_boolean
01059 debug_record_float_const (void *handle, const char *name, double val)
01060 {
01061   struct debug_handle *info = (struct debug_handle *) handle;
01062   struct debug_name *n;
01063 
01064   if (name == NULL)
01065     return FALSE;
01066 
01067   n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_FLOAT_CONSTANT,
01068                                   DEBUG_LINKAGE_NONE);
01069   if (n == NULL)
01070     return FALSE;
01071 
01072   n->u.float_constant = val;
01073 
01074   return TRUE;
01075 }
01076 
01077 /* Record a typed constant with an integral value.  */
01078 
01079 bfd_boolean
01080 debug_record_typed_const (void *handle, const char *name, debug_type type,
01081                        bfd_vma val)
01082 {
01083   struct debug_handle *info = (struct debug_handle *) handle;
01084   struct debug_name *n;
01085   struct debug_typed_constant *tc;
01086 
01087   if (name == NULL || type == NULL)
01088     return FALSE;
01089 
01090   n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_TYPED_CONSTANT,
01091                                   DEBUG_LINKAGE_NONE);
01092   if (n == NULL)
01093     return FALSE;
01094 
01095   tc = (struct debug_typed_constant *) xmalloc (sizeof *tc);
01096   memset (tc, 0, sizeof *tc);
01097 
01098   tc->type = type;
01099   tc->val = val;
01100 
01101   n->u.typed_constant = tc;
01102 
01103   return TRUE;
01104 }
01105 
01106 /* Record a label.  */
01107 
01108 bfd_boolean
01109 debug_record_label (void *handle ATTRIBUTE_UNUSED,
01110                   const char *name ATTRIBUTE_UNUSED,
01111                   debug_type type ATTRIBUTE_UNUSED,
01112                   bfd_vma addr ATTRIBUTE_UNUSED)
01113 {
01114   /* FIXME.  */
01115   debug_error (_("debug_record_label: not implemented"));
01116   return FALSE;
01117 }
01118 
01119 /* Record a variable.  */
01120 
01121 bfd_boolean
01122 debug_record_variable (void *handle, const char *name, debug_type type,
01123                      enum debug_var_kind kind, bfd_vma val)
01124 {
01125   struct debug_handle *info = (struct debug_handle *) handle;
01126   struct debug_namespace **nsp;
01127   enum debug_object_linkage linkage;
01128   struct debug_name *n;
01129   struct debug_variable *v;
01130 
01131   if (name == NULL || type == NULL)
01132     return FALSE;
01133 
01134   if (info->current_unit == NULL
01135       || info->current_file == NULL)
01136     {
01137       debug_error (_("debug_record_variable: no current file"));
01138       return FALSE;
01139     }
01140 
01141   if (kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
01142     {
01143       nsp = &info->current_file->globals;
01144       if (kind == DEBUG_GLOBAL)
01145        linkage = DEBUG_LINKAGE_GLOBAL;
01146       else
01147        linkage = DEBUG_LINKAGE_STATIC;
01148     }
01149   else
01150     {
01151       if (info->current_block == NULL)
01152        nsp = &info->current_file->globals;
01153       else
01154        nsp = &info->current_block->locals;
01155       linkage = DEBUG_LINKAGE_AUTOMATIC;
01156     }
01157 
01158   n = debug_add_to_namespace (info, nsp, name, DEBUG_OBJECT_VARIABLE, linkage);
01159   if (n == NULL)
01160     return FALSE;
01161 
01162   v = (struct debug_variable *) xmalloc (sizeof *v);
01163   memset (v, 0, sizeof *v);
01164 
01165   v->kind = kind;
01166   v->type = type;
01167   v->val = val;
01168 
01169   n->u.variable = v;
01170 
01171   return TRUE;
01172 }
01173 
01174 /* Make a type with a given kind and size.  */
01175 
01176 static struct debug_type *
01177 debug_make_type (struct debug_handle *info ATTRIBUTE_UNUSED,
01178                enum debug_type_kind kind, unsigned int size)
01179 {
01180   struct debug_type *t;
01181 
01182   t = (struct debug_type *) xmalloc (sizeof *t);
01183   memset (t, 0, sizeof *t);
01184 
01185   t->kind = kind;
01186   t->size = size;
01187 
01188   return t;
01189 }
01190 
01191 /* Make an indirect type which may be used as a placeholder for a type
01192    which is referenced before it is defined.  */
01193 
01194 debug_type
01195 debug_make_indirect_type (void *handle, debug_type *slot, const char *tag)
01196 {
01197   struct debug_handle *info = (struct debug_handle *) handle;
01198   struct debug_type *t;
01199   struct debug_indirect_type *i;
01200 
01201   t = debug_make_type (info, DEBUG_KIND_INDIRECT, 0);
01202   if (t == NULL)
01203     return DEBUG_TYPE_NULL;
01204 
01205   i = (struct debug_indirect_type *) xmalloc (sizeof *i);
01206   memset (i, 0, sizeof *i);
01207 
01208   i->slot = slot;
01209   i->tag = tag;
01210 
01211   t->u.kindirect = i;
01212 
01213   return t;
01214 }
01215 
01216 /* Make a void type.  There is only one of these.  */
01217 
01218 debug_type
01219 debug_make_void_type (void *handle)
01220 {
01221   struct debug_handle *info = (struct debug_handle *) handle;
01222 
01223   return debug_make_type (info, DEBUG_KIND_VOID, 0);
01224 }
01225 
01226 /* Make an integer type of a given size.  The boolean argument is true
01227    if the integer is unsigned.  */
01228 
01229 debug_type
01230 debug_make_int_type (void *handle, unsigned int size, bfd_boolean unsignedp)
01231 {
01232   struct debug_handle *info = (struct debug_handle *) handle;
01233   struct debug_type *t;
01234 
01235   t = debug_make_type (info, DEBUG_KIND_INT, size);
01236   if (t == NULL)
01237     return DEBUG_TYPE_NULL;
01238 
01239   t->u.kint = unsignedp;
01240 
01241   return t;
01242 }
01243 
01244 /* Make a floating point type of a given size.  FIXME: On some
01245    platforms, like an Alpha, you probably need to be able to specify
01246    the format.  */
01247 
01248 debug_type
01249 debug_make_float_type (void *handle, unsigned int size)
01250 {
01251   struct debug_handle *info = (struct debug_handle *) handle;
01252 
01253   return debug_make_type (info, DEBUG_KIND_FLOAT, size);
01254 }
01255 
01256 /* Make a boolean type of a given size.  */
01257 
01258 debug_type
01259 debug_make_bool_type (void *handle, unsigned int size)
01260 {
01261   struct debug_handle *info = (struct debug_handle *) handle;
01262 
01263   return debug_make_type (info, DEBUG_KIND_BOOL, size);
01264 }
01265 
01266 /* Make a complex type of a given size.  */
01267 
01268 debug_type
01269 debug_make_complex_type (void *handle, unsigned int size)
01270 {
01271   struct debug_handle *info = (struct debug_handle *) handle;
01272 
01273   return debug_make_type (info, DEBUG_KIND_COMPLEX, size);
01274 }
01275 
01276 /* Make a structure type.  The second argument is true for a struct,
01277    false for a union.  The third argument is the size of the struct.
01278    The fourth argument is a NULL terminated array of fields.  */
01279 
01280 debug_type
01281 debug_make_struct_type (void *handle, bfd_boolean structp, bfd_vma size,
01282                      debug_field *fields)
01283 {
01284   struct debug_handle *info = (struct debug_handle *) handle;
01285   struct debug_type *t;
01286   struct debug_class_type *c;
01287 
01288   t = debug_make_type (info,
01289                      structp ? DEBUG_KIND_STRUCT : DEBUG_KIND_UNION,
01290                      size);
01291   if (t == NULL)
01292     return DEBUG_TYPE_NULL;
01293 
01294   c = (struct debug_class_type *) xmalloc (sizeof *c);
01295   memset (c, 0, sizeof *c);
01296 
01297   c->fields = fields;
01298 
01299   t->u.kclass = c;
01300 
01301   return t;
01302 }
01303 
01304 /* Make an object type.  The first three arguments after the handle
01305    are the same as for debug_make_struct_type.  The next arguments are
01306    a NULL terminated array of base classes, a NULL terminated array of
01307    methods, the type of the object holding the virtual function table
01308    if it is not this object, and a boolean which is true if this
01309    object has its own virtual function table.  */
01310 
01311 debug_type
01312 debug_make_object_type (void *handle, bfd_boolean structp, bfd_vma size,
01313                      debug_field *fields, debug_baseclass *baseclasses,
01314                      debug_method *methods, debug_type vptrbase,
01315                      bfd_boolean ownvptr)
01316 {
01317   struct debug_handle *info = (struct debug_handle *) handle;
01318   struct debug_type *t;
01319   struct debug_class_type *c;
01320 
01321   t = debug_make_type (info,
01322                      structp ? DEBUG_KIND_CLASS : DEBUG_KIND_UNION_CLASS,
01323                      size);
01324   if (t == NULL)
01325     return DEBUG_TYPE_NULL;
01326 
01327   c = (struct debug_class_type *) xmalloc (sizeof *c);
01328   memset (c, 0, sizeof *c);
01329 
01330   c->fields = fields;
01331   c->baseclasses = baseclasses;
01332   c->methods = methods;
01333   if (ownvptr)
01334     c->vptrbase = t;
01335   else
01336     c->vptrbase = vptrbase;
01337 
01338   t->u.kclass = c;
01339 
01340   return t;
01341 }
01342 
01343 /* Make an enumeration type.  The arguments are a null terminated
01344    array of strings, and an array of corresponding values.  */
01345 
01346 debug_type
01347 debug_make_enum_type (void *handle, const char **names,
01348                     bfd_signed_vma *values)
01349 {
01350   struct debug_handle *info = (struct debug_handle *) handle;
01351   struct debug_type *t;
01352   struct debug_enum_type *e;
01353 
01354   t = debug_make_type (info, DEBUG_KIND_ENUM, 0);
01355   if (t == NULL)
01356     return DEBUG_TYPE_NULL;
01357 
01358   e = (struct debug_enum_type *) xmalloc (sizeof *e);
01359   memset (e, 0, sizeof *e);
01360 
01361   e->names = names;
01362   e->values = values;
01363 
01364   t->u.kenum = e;
01365 
01366   return t;
01367 }
01368 
01369 /* Make a pointer to a given type.  */
01370 
01371 debug_type
01372 debug_make_pointer_type (void *handle, debug_type type)
01373 {
01374   struct debug_handle *info = (struct debug_handle *) handle;
01375   struct debug_type *t;
01376 
01377   if (type == NULL)
01378     return DEBUG_TYPE_NULL;
01379 
01380   if (type->pointer != DEBUG_TYPE_NULL)
01381     return type->pointer;
01382 
01383   t = debug_make_type (info, DEBUG_KIND_POINTER, 0);
01384   if (t == NULL)
01385     return DEBUG_TYPE_NULL;
01386 
01387   t->u.kpointer = type;
01388 
01389   type->pointer = t;
01390 
01391   return t;
01392 }
01393 
01394 /* Make a function returning a given type.  FIXME: We should be able
01395    to record the parameter types.  */
01396 
01397 debug_type
01398 debug_make_function_type (void *handle, debug_type type,
01399                        debug_type *arg_types, bfd_boolean varargs)
01400 {
01401   struct debug_handle *info = (struct debug_handle *) handle;
01402   struct debug_type *t;
01403   struct debug_function_type *f;
01404 
01405   if (type == NULL)
01406     return DEBUG_TYPE_NULL;
01407 
01408   t = debug_make_type (info, DEBUG_KIND_FUNCTION, 0);
01409   if (t == NULL)
01410     return DEBUG_TYPE_NULL;
01411 
01412   f = (struct debug_function_type *) xmalloc (sizeof *f);
01413   memset (f, 0, sizeof *f);
01414 
01415   f->return_type = type;
01416   f->arg_types = arg_types;
01417   f->varargs = varargs;
01418 
01419   t->u.kfunction = f;
01420 
01421   return t;
01422 }
01423 
01424 /* Make a reference to a given type.  */
01425 
01426 debug_type
01427 debug_make_reference_type (void *handle, debug_type type)
01428 {
01429   struct debug_handle *info = (struct debug_handle *) handle;
01430   struct debug_type *t;
01431 
01432   if (type == NULL)
01433     return DEBUG_TYPE_NULL;
01434 
01435   t = debug_make_type (info, DEBUG_KIND_REFERENCE, 0);
01436   if (t == NULL)
01437     return DEBUG_TYPE_NULL;
01438 
01439   t->u.kreference = type;
01440 
01441   return t;
01442 }
01443 
01444 /* Make a range of a given type from a lower to an upper bound.  */
01445 
01446 debug_type
01447 debug_make_range_type (void *handle, debug_type type, bfd_signed_vma lower,
01448                      bfd_signed_vma upper)
01449 {
01450   struct debug_handle *info = (struct debug_handle *) handle;
01451   struct debug_type *t;
01452   struct debug_range_type *r;
01453 
01454   if (type == NULL)
01455     return DEBUG_TYPE_NULL;
01456 
01457   t = debug_make_type (info, DEBUG_KIND_RANGE, 0);
01458   if (t == NULL)
01459     return DEBUG_TYPE_NULL;
01460 
01461   r = (struct debug_range_type *) xmalloc (sizeof *r);
01462   memset (r, 0, sizeof *r);
01463 
01464   r->type = type;
01465   r->lower = lower;
01466   r->upper = upper;
01467 
01468   t->u.krange = r;
01469 
01470   return t;
01471 }
01472 
01473 /* Make an array type.  The second argument is the type of an element
01474    of the array.  The third argument is the type of a range of the
01475    array.  The fourth and fifth argument are the lower and upper
01476    bounds, respectively.  The sixth argument is true if this array is
01477    actually a string, as in C.  */
01478 
01479 debug_type
01480 debug_make_array_type (void *handle, debug_type element_type,
01481                      debug_type range_type, bfd_signed_vma lower,
01482                      bfd_signed_vma upper, bfd_boolean stringp)
01483 {
01484   struct debug_handle *info = (struct debug_handle *) handle;
01485   struct debug_type *t;
01486   struct debug_array_type *a;
01487 
01488   if (element_type == NULL || range_type == NULL)
01489     return DEBUG_TYPE_NULL;
01490 
01491   t = debug_make_type (info, DEBUG_KIND_ARRAY, 0);
01492   if (t == NULL)
01493     return DEBUG_TYPE_NULL;
01494 
01495   a = (struct debug_array_type *) xmalloc (sizeof *a);
01496   memset (a, 0, sizeof *a);
01497 
01498   a->element_type = element_type;
01499   a->range_type = range_type;
01500   a->lower = lower;
01501   a->upper = upper;
01502   a->stringp = stringp;
01503 
01504   t->u.karray = a;
01505 
01506   return t;
01507 }
01508 
01509 /* Make a set of a given type.  For example, a Pascal set type.  The
01510    boolean argument is true if this set is actually a bitstring, as in
01511    CHILL.  */
01512 
01513 debug_type
01514 debug_make_set_type (void *handle, debug_type type, bfd_boolean bitstringp)
01515 {
01516   struct debug_handle *info = (struct debug_handle *) handle;
01517   struct debug_type *t;
01518   struct debug_set_type *s;
01519 
01520   if (type == NULL)
01521     return DEBUG_TYPE_NULL;
01522 
01523   t = debug_make_type (info, DEBUG_KIND_SET, 0);
01524   if (t == NULL)
01525     return DEBUG_TYPE_NULL;
01526 
01527   s = (struct debug_set_type *) xmalloc (sizeof *s);
01528   memset (s, 0, sizeof *s);
01529 
01530   s->type = type;
01531   s->bitstringp = bitstringp;
01532 
01533   t->u.kset = s;
01534 
01535   return t;
01536 }
01537 
01538 /* Make a type for a pointer which is relative to an object.  The
01539    second argument is the type of the object to which the pointer is
01540    relative.  The third argument is the type that the pointer points
01541    to.  */
01542 
01543 debug_type
01544 debug_make_offset_type (void *handle, debug_type base_type,
01545                      debug_type target_type)
01546 {
01547   struct debug_handle *info = (struct debug_handle *) handle;
01548   struct debug_type *t;
01549   struct debug_offset_type *o;
01550 
01551   if (base_type == NULL || target_type == NULL)
01552     return DEBUG_TYPE_NULL;
01553 
01554   t = debug_make_type (info, DEBUG_KIND_OFFSET, 0);
01555   if (t == NULL)
01556     return DEBUG_TYPE_NULL;
01557 
01558   o = (struct debug_offset_type *) xmalloc (sizeof *o);
01559   memset (o, 0, sizeof *o);
01560 
01561   o->base_type = base_type;
01562   o->target_type = target_type;
01563 
01564   t->u.koffset = o;
01565 
01566   return t;
01567 }
01568 
01569 /* Make a type for a method function.  The second argument is the
01570    return type, the third argument is the domain, and the fourth
01571    argument is a NULL terminated array of argument types.  */
01572 
01573 debug_type
01574 debug_make_method_type (void *handle, debug_type return_type,
01575                      debug_type domain_type, debug_type *arg_types,
01576                      bfd_boolean varargs)
01577 {
01578   struct debug_handle *info = (struct debug_handle *) handle;
01579   struct debug_type *t;
01580   struct debug_method_type *m;
01581 
01582   if (return_type == NULL)
01583     return DEBUG_TYPE_NULL;
01584 
01585   t = debug_make_type (info, DEBUG_KIND_METHOD, 0);
01586   if (t == NULL)
01587     return DEBUG_TYPE_NULL;
01588 
01589   m = (struct debug_method_type *) xmalloc (sizeof *m);
01590   memset (m, 0, sizeof *m);
01591 
01592   m->return_type = return_type;
01593   m->domain_type = domain_type;
01594   m->arg_types = arg_types;
01595   m->varargs = varargs;
01596 
01597   t->u.kmethod = m;
01598 
01599   return t;
01600 }
01601 
01602 /* Make a const qualified version of a given type.  */
01603 
01604 debug_type
01605 debug_make_const_type (void *handle, debug_type type)
01606 {
01607   struct debug_handle *info = (struct debug_handle *) handle;
01608   struct debug_type *t;
01609 
01610   if (type == NULL)
01611     return DEBUG_TYPE_NULL;
01612 
01613   t = debug_make_type (info, DEBUG_KIND_CONST, 0);
01614   if (t == NULL)
01615     return DEBUG_TYPE_NULL;
01616 
01617   t->u.kconst = type;
01618 
01619   return t;
01620 }
01621 
01622 /* Make a volatile qualified version of a given type.  */
01623 
01624 debug_type
01625 debug_make_volatile_type (void *handle, debug_type type)
01626 {
01627   struct debug_handle *info = (struct debug_handle *) handle;
01628   struct debug_type *t;
01629 
01630   if (type == NULL)
01631     return DEBUG_TYPE_NULL;
01632 
01633   t = debug_make_type (info, DEBUG_KIND_VOLATILE, 0);
01634   if (t == NULL)
01635     return DEBUG_TYPE_NULL;
01636 
01637   t->u.kvolatile = type;
01638 
01639   return t;
01640 }
01641 
01642 /* Make an undefined tagged type.  For example, a struct which has
01643    been mentioned, but not defined.  */
01644 
01645 debug_type
01646 debug_make_undefined_tagged_type (void *handle, const char *name,
01647                               enum debug_type_kind kind)
01648 {
01649   struct debug_handle *info = (struct debug_handle *) handle;
01650   struct debug_type *t;
01651 
01652   if (name == NULL)
01653     return DEBUG_TYPE_NULL;
01654 
01655   switch (kind)
01656     {
01657     case DEBUG_KIND_STRUCT:
01658     case DEBUG_KIND_UNION:
01659     case DEBUG_KIND_CLASS:
01660     case DEBUG_KIND_UNION_CLASS:
01661     case DEBUG_KIND_ENUM:
01662       break;
01663 
01664     default:
01665       debug_error (_("debug_make_undefined_type: unsupported kind"));
01666       return DEBUG_TYPE_NULL;
01667     }
01668 
01669   t = debug_make_type (info, kind, 0);
01670   if (t == NULL)
01671     return DEBUG_TYPE_NULL;
01672 
01673   return debug_tag_type (handle, name, t);
01674 }
01675 
01676 /* Make a base class for an object.  The second argument is the base
01677    class type.  The third argument is the bit position of this base
01678    class in the object (always 0 unless doing multiple inheritance).
01679    The fourth argument is whether this is a virtual class.  The fifth
01680    argument is the visibility of the base class.  */
01681 
01682 debug_baseclass
01683 debug_make_baseclass (void *handle ATTRIBUTE_UNUSED, debug_type type,
01684                     bfd_vma bitpos, bfd_boolean virtual,
01685                     enum debug_visibility visibility)
01686 {
01687   struct debug_baseclass *b;
01688 
01689   b = (struct debug_baseclass *) xmalloc (sizeof *b);
01690   memset (b, 0, sizeof *b);
01691 
01692   b->type = type;
01693   b->bitpos = bitpos;
01694   b->virtual = virtual;
01695   b->visibility = visibility;
01696 
01697   return b;
01698 }
01699 
01700 /* Make a field for a struct.  The second argument is the name.  The
01701    third argument is the type of the field.  The fourth argument is
01702    the bit position of the field.  The fifth argument is the size of
01703    the field (it may be zero).  The sixth argument is the visibility
01704    of the field.  */
01705 
01706 debug_field
01707 debug_make_field (void *handle ATTRIBUTE_UNUSED, const char *name,
01708                 debug_type type, bfd_vma bitpos, bfd_vma bitsize,
01709                 enum debug_visibility visibility)
01710 {
01711   struct debug_field *f;
01712 
01713   f = (struct debug_field *) xmalloc (sizeof *f);
01714   memset (f, 0, sizeof *f);
01715 
01716   f->name = name;
01717   f->type = type;
01718   f->static_member = FALSE;
01719   f->u.f.bitpos = bitpos;
01720   f->u.f.bitsize = bitsize;
01721   f->visibility = visibility;
01722 
01723   return f;
01724 }
01725 
01726 /* Make a static member of an object.  The second argument is the
01727    name.  The third argument is the type of the member.  The fourth
01728    argument is the physical name of the member (i.e., the name as a
01729    global variable).  The fifth argument is the visibility of the
01730    member.  */
01731 
01732 debug_field
01733 debug_make_static_member (void *handle ATTRIBUTE_UNUSED, const char *name,
01734                        debug_type type, const char *physname,
01735                        enum debug_visibility visibility)
01736 {
01737   struct debug_field *f;
01738 
01739   f = (struct debug_field *) xmalloc (sizeof *f);
01740   memset (f, 0, sizeof *f);
01741 
01742   f->name = name;
01743   f->type = type;
01744   f->static_member = TRUE;
01745   f->u.s.physname = physname;
01746   f->visibility = visibility;
01747 
01748   return f;
01749 }
01750 
01751 /* Make a method.  The second argument is the name, and the third
01752    argument is a NULL terminated array of method variants.  */
01753 
01754 debug_method
01755 debug_make_method (void *handle ATTRIBUTE_UNUSED, const char *name,
01756                  debug_method_variant *variants)
01757 {
01758   struct debug_method *m;
01759 
01760   m = (struct debug_method *) xmalloc (sizeof *m);
01761   memset (m, 0, sizeof *m);
01762 
01763   m->name = name;
01764   m->variants = variants;
01765 
01766   return m;
01767 }
01768 
01769 /* Make a method argument.  The second argument is the real name of
01770    the function.  The third argument is the type of the function.  The
01771    fourth argument is the visibility.  The fifth argument is whether
01772    this is a const function.  The sixth argument is whether this is a
01773    volatile function.  The seventh argument is the offset in the
01774    virtual function table, if any.  The eighth argument is the virtual
01775    function context.  FIXME: Are the const and volatile arguments
01776    necessary?  Could we just use debug_make_const_type?  */
01777 
01778 debug_method_variant
01779 debug_make_method_variant (void *handle ATTRIBUTE_UNUSED,
01780                         const char *physname, debug_type type,
01781                         enum debug_visibility visibility,
01782                         bfd_boolean constp, bfd_boolean volatilep,
01783                         bfd_vma voffset, debug_type context)
01784 {
01785   struct debug_method_variant *m;
01786 
01787   m = (struct debug_method_variant *) xmalloc (sizeof *m);
01788   memset (m, 0, sizeof *m);
01789 
01790   m->physname = physname;
01791   m->type = type;
01792   m->visibility = visibility;
01793   m->constp = constp;
01794   m->volatilep = volatilep;
01795   m->voffset = voffset;
01796   m->context = context;
01797 
01798   return m;
01799 }
01800 
01801 /* Make a static method argument.  The arguments are the same as for
01802    debug_make_method_variant, except that the last two are omitted
01803    since a static method can not also be virtual.  */
01804 
01805 debug_method_variant
01806 debug_make_static_method_variant (void *handle ATTRIBUTE_UNUSED,
01807                               const char *physname, debug_type type,
01808                               enum debug_visibility visibility,
01809                               bfd_boolean constp, bfd_boolean volatilep)
01810 {
01811   struct debug_method_variant *m;
01812 
01813   m = (struct debug_method_variant *) xmalloc (sizeof *m);
01814   memset (m, 0, sizeof *m);
01815 
01816   m->physname = physname;
01817   m->type = type;
01818   m->visibility = visibility;
01819   m->constp = constp;
01820   m->volatilep = volatilep;
01821   m->voffset = VOFFSET_STATIC_METHOD;
01822 
01823   return m;
01824 }
01825 
01826 /* Name a type.  */
01827 
01828 debug_type
01829 debug_name_type (void *handle, const char *name, debug_type type)
01830 {
01831   struct debug_handle *info = (struct debug_handle *) handle;
01832   struct debug_type *t;
01833   struct debug_named_type *n;
01834   struct debug_name *nm;
01835 
01836   if (name == NULL || type == NULL)
01837     return DEBUG_TYPE_NULL;
01838 
01839   if (info->current_unit == NULL
01840       || info->current_file == NULL)
01841     {
01842       debug_error (_("debug_name_type: no current file"));
01843       return DEBUG_TYPE_NULL;
01844     }
01845 
01846   t = debug_make_type (info, DEBUG_KIND_NAMED, 0);
01847   if (t == NULL)
01848     return DEBUG_TYPE_NULL;
01849 
01850   n = (struct debug_named_type *) xmalloc (sizeof *n);
01851   memset (n, 0, sizeof *n);
01852 
01853   n->type = type;
01854 
01855   t->u.knamed = n;
01856 
01857   /* We always add the name to the global namespace.  This is probably
01858      wrong in some cases, but it seems to be right for stabs.  FIXME.  */
01859 
01860   nm = debug_add_to_namespace (info, &info->current_file->globals, name,
01861                             DEBUG_OBJECT_TYPE, DEBUG_LINKAGE_NONE);
01862   if (nm == NULL)
01863     return DEBUG_TYPE_NULL;
01864 
01865   nm->u.type = t;
01866 
01867   n->name = nm;
01868 
01869   return t;
01870 }
01871 
01872 /* Tag a type.  */
01873 
01874 debug_type
01875 debug_tag_type (void *handle, const char *name, debug_type type)
01876 {
01877   struct debug_handle *info = (struct debug_handle *) handle;
01878   struct debug_type *t;
01879   struct debug_named_type *n;
01880   struct debug_name *nm;
01881 
01882   if (name == NULL || type == NULL)
01883     return DEBUG_TYPE_NULL;
01884 
01885   if (info->current_file == NULL)
01886     {
01887       debug_error (_("debug_tag_type: no current file"));
01888       return DEBUG_TYPE_NULL;
01889     }
01890 
01891   if (type->kind == DEBUG_KIND_TAGGED)
01892     {
01893       if (strcmp (type->u.knamed->name->name, name) == 0)
01894        return type;
01895       debug_error (_("debug_tag_type: extra tag attempted"));
01896       return DEBUG_TYPE_NULL;
01897     }
01898 
01899   t = debug_make_type (info, DEBUG_KIND_TAGGED, 0);
01900   if (t == NULL)
01901     return DEBUG_TYPE_NULL;
01902 
01903   n = (struct debug_named_type *) xmalloc (sizeof *n);
01904   memset (n, 0, sizeof *n);
01905 
01906   n->type = type;
01907 
01908   t->u.knamed = n;
01909 
01910   /* We keep a global namespace of tags for each compilation unit.  I
01911      don't know if that is the right thing to do.  */
01912 
01913   nm = debug_add_to_namespace (info, &info->current_file->globals, name,
01914                             DEBUG_OBJECT_TAG, DEBUG_LINKAGE_NONE);
01915   if (nm == NULL)
01916     return DEBUG_TYPE_NULL;
01917 
01918   nm->u.tag = t;
01919 
01920   n->name = nm;
01921 
01922   return t;
01923 }
01924 
01925 /* Record the size of a given type.  */
01926 
01927 bfd_boolean
01928 debug_record_type_size (void *handle ATTRIBUTE_UNUSED, debug_type type,
01929                      unsigned int size)
01930 {
01931   if (type->size != 0 && type->size != size)
01932     fprintf (stderr, _("Warning: changing type size from %d to %d\n"),
01933             type->size, size);
01934 
01935   type->size = size;
01936 
01937   return TRUE;
01938 }
01939 
01940 /* Find a named type.  */
01941 
01942 debug_type
01943 debug_find_named_type (void *handle, const char *name)
01944 {
01945   struct debug_handle *info = (struct debug_handle *) handle;
01946   struct debug_block *b;
01947   struct debug_file *f;
01948 
01949   /* We only search the current compilation unit.  I don't know if
01950      this is right or not.  */
01951 
01952   if (info->current_unit == NULL)
01953     {
01954       debug_error (_("debug_find_named_type: no current compilation unit"));
01955       return DEBUG_TYPE_NULL;
01956     }
01957 
01958   for (b = info->current_block; b != NULL; b = b->parent)
01959     {
01960       if (b->locals != NULL)
01961        {
01962          struct debug_name *n;
01963 
01964          for (n = b->locals->list; n != NULL; n = n->next)
01965            {
01966              if (n->kind == DEBUG_OBJECT_TYPE
01967                 && n->name[0] == name[0]
01968                 && strcmp (n->name, name) == 0)
01969               return n->u.type;
01970            }
01971        }
01972     }
01973 
01974   for (f = info->current_unit->files; f != NULL; f = f->next)
01975     {
01976       if (f->globals != NULL)
01977        {
01978          struct debug_name *n;
01979 
01980          for (n = f->globals->list; n != NULL; n = n->next)
01981            {
01982              if (n->kind == DEBUG_OBJECT_TYPE
01983                 && n->name[0] == name[0]
01984                 && strcmp (n->name, name) == 0)
01985               return n->u.type;
01986            }
01987        }
01988     }
01989 
01990   return DEBUG_TYPE_NULL;
01991 }
01992 
01993 /* Find a tagged type.  */
01994 
01995 debug_type
01996 debug_find_tagged_type (void *handle, const char *name,
01997                      enum debug_type_kind kind)
01998 {
01999   struct debug_handle *info = (struct debug_handle *) handle;
02000   struct debug_unit *u;
02001 
02002   /* We search the globals of all the compilation units.  I don't know
02003      if this is correct or not.  It would be easy to change.  */
02004 
02005   for (u = info->units; u != NULL; u = u->next)
02006     {
02007       struct debug_file *f;
02008 
02009       for (f = u->files; f != NULL; f = f->next)
02010        {
02011          struct debug_name *n;
02012 
02013          if (f->globals != NULL)
02014            {
02015              for (n = f->globals->list; n != NULL; n = n->next)
02016               {
02017                 if (n->kind == DEBUG_OBJECT_TAG
02018                     && (kind == DEBUG_KIND_ILLEGAL
02019                        || n->u.tag->kind == kind)
02020                     && n->name[0] == name[0]
02021                     && strcmp (n->name, name) == 0)
02022                   return n->u.tag;
02023               }
02024            }
02025        }
02026     }
02027 
02028   return DEBUG_TYPE_NULL;
02029 }
02030 
02031 /* Get a base type.  We build a linked list on the stack to avoid
02032    crashing if the type is defined circularly.  */
02033 
02034 static struct debug_type *
02035 debug_get_real_type (void *handle, debug_type type,
02036                    struct debug_type_real_list *list)
02037 {
02038   struct debug_type_real_list *l;
02039   struct debug_type_real_list rl;
02040 
02041   switch (type->kind)
02042     {
02043     default:
02044       return type;
02045 
02046     case DEBUG_KIND_INDIRECT:
02047     case DEBUG_KIND_NAMED:
02048     case DEBUG_KIND_TAGGED:
02049       break;
02050     }
02051 
02052   for (l = list; l != NULL; l = l->next)
02053     {
02054       if (l->t == type || l == l->next)
02055        {
02056          fprintf (stderr,
02057                  _("debug_get_real_type: circular debug information for %s\n"),
02058                  debug_get_type_name (handle, type));
02059          return NULL;
02060        }
02061     }
02062 
02063   rl.next = list;
02064   rl.t = type;
02065 
02066   switch (type->kind)
02067     {
02068       /* The default case is just here to avoid warnings.  */
02069     default:
02070     case DEBUG_KIND_INDIRECT:
02071       if (*type->u.kindirect->slot != NULL)
02072        return debug_get_real_type (handle, *type->u.kindirect->slot, &rl);
02073       return type;
02074     case DEBUG_KIND_NAMED:
02075     case DEBUG_KIND_TAGGED:
02076       return debug_get_real_type (handle, type->u.knamed->type, &rl);
02077     }
02078   /*NOTREACHED*/
02079 }
02080 
02081 /* Get the kind of a type.  */
02082 
02083 enum debug_type_kind
02084 debug_get_type_kind (void *handle, debug_type type)
02085 {
02086   if (type == NULL)
02087     return DEBUG_KIND_ILLEGAL;
02088   type = debug_get_real_type (handle, type, NULL);
02089   if (type == NULL)
02090     return DEBUG_KIND_ILLEGAL;
02091   return type->kind;
02092 }
02093 
02094 /* Get the name of a type.  */
02095 
02096 const char *
02097 debug_get_type_name (void *handle, debug_type type)
02098 {
02099   if (type->kind == DEBUG_KIND_INDIRECT)
02100     {
02101       if (*type->u.kindirect->slot != NULL)
02102        return debug_get_type_name (handle, *type->u.kindirect->slot);
02103       return type->u.kindirect->tag;
02104     }
02105   if (type->kind == DEBUG_KIND_NAMED
02106       || type->kind == DEBUG_KIND_TAGGED)
02107     return type->u.knamed->name->name;
02108   return NULL;
02109 }
02110 
02111 /* Get the size of a type.  */
02112 
02113 bfd_vma
02114 debug_get_type_size (void *handle, debug_type type)
02115 {
02116   if (type == NULL)
02117     return 0;
02118 
02119   /* We don't call debug_get_real_type, because somebody might have
02120      called debug_record_type_size on a named or indirect type.  */
02121 
02122   if (type->size != 0)
02123     return type->size;
02124 
02125   switch (type->kind)
02126     {
02127     default:
02128       return 0;
02129     case DEBUG_KIND_INDIRECT:
02130       if (*type->u.kindirect->slot != NULL)
02131        return debug_get_type_size (handle, *type->u.kindirect->slot);
02132       return 0;
02133     case DEBUG_KIND_NAMED:
02134     case DEBUG_KIND_TAGGED:
02135       return debug_get_type_size (handle, type->u.knamed->type);
02136     }
02137   /*NOTREACHED*/
02138 }
02139 
02140 /* Get the return type of a function or method type.  */
02141 
02142 debug_type
02143 debug_get_return_type (void *handle, debug_type type)
02144 {
02145   if (type == NULL)
02146     return DEBUG_TYPE_NULL;
02147 
02148   type = debug_get_real_type (handle, type, NULL);
02149   if (type == NULL)
02150     return DEBUG_TYPE_NULL;
02151 
02152   switch (type->kind)
02153     {
02154     default:
02155       return DEBUG_TYPE_NULL;
02156     case DEBUG_KIND_FUNCTION:
02157       return type->u.kfunction->return_type;
02158     case DEBUG_KIND_METHOD:
02159       return type->u.kmethod->return_type;
02160     }
02161   /*NOTREACHED*/
02162 }
02163 
02164 /* Get the parameter types of a function or method type (except that
02165    we don't currently store the parameter types of a function).  */
02166 
02167 const debug_type *
02168 debug_get_parameter_types (void *handle, debug_type type,
02169                         bfd_boolean *pvarargs)
02170 {
02171   if (type == NULL)
02172     return NULL;
02173 
02174   type = debug_get_real_type (handle, type, NULL);
02175   if (type == NULL)
02176     return NULL;
02177 
02178   switch (type->kind)
02179     {
02180     default:
02181       return NULL;
02182     case DEBUG_KIND_FUNCTION:
02183       *pvarargs = type->u.kfunction->varargs;
02184       return type->u.kfunction->arg_types;
02185     case DEBUG_KIND_METHOD:
02186       *pvarargs = type->u.kmethod->varargs;
02187       return type->u.kmethod->arg_types;
02188     }
02189   /*NOTREACHED*/
02190 }
02191 
02192 /* Get the target type of a type.  */
02193 
02194 debug_type
02195 debug_get_target_type (void *handle, debug_type type)
02196 {
02197   if (type == NULL)
02198     return NULL;
02199 
02200   type = debug_get_real_type (handle, type, NULL);
02201   if (type == NULL)
02202     return NULL;
02203 
02204   switch (type->kind)
02205     {
02206     default:
02207       return NULL;
02208     case DEBUG_KIND_POINTER:
02209       return type->u.kpointer;
02210     case DEBUG_KIND_REFERENCE:
02211       return type->u.kreference;
02212     case DEBUG_KIND_CONST:
02213       return type->u.kconst;
02214     case DEBUG_KIND_VOLATILE:
02215       return type->u.kvolatile;
02216     }
02217   /*NOTREACHED*/
02218 }
02219 
02220 /* Get the NULL terminated array of fields for a struct, union, or
02221    class.  */
02222 
02223 const debug_field *
02224 debug_get_fields (void *handle, debug_type type)
02225 {
02226   if (type == NULL)
02227     return NULL;
02228 
02229   type = debug_get_real_type (handle, type, NULL);
02230   if (type == NULL)
02231     return NULL;
02232 
02233   switch (type->kind)
02234     {
02235     default:
02236       return NULL;
02237     case DEBUG_KIND_STRUCT:
02238     case DEBUG_KIND_UNION:
02239     case DEBUG_KIND_CLASS:
02240     case DEBUG_KIND_UNION_CLASS:
02241       return type->u.kclass->fields;
02242     }
02243   /*NOTREACHED*/
02244 }
02245 
02246 /* Get the type of a field.  */
02247 
02248 debug_type
02249 debug_get_field_type (void *handle ATTRIBUTE_UNUSED, debug_field field)
02250 {
02251   if (field == NULL)
02252     return NULL;
02253   return field->type;
02254 }
02255 
02256 /* Get the name of a field.  */
02257 
02258 const char *
02259 debug_get_field_name (void *handle ATTRIBUTE_UNUSED, debug_field field)
02260 {
02261   if (field == NULL)
02262     return NULL;
02263   return field->name;
02264 }
02265 
02266 /* Get the bit position of a field.  */
02267 
02268 bfd_vma
02269 debug_get_field_bitpos (void *handle ATTRIBUTE_UNUSED, debug_field field)
02270 {
02271   if (field == NULL || field->static_member)
02272     return (bfd_vma) -1;
02273   return field->u.f.bitpos;
02274 }
02275 
02276 /* Get the bit size of a field.  */
02277 
02278 bfd_vma
02279 debug_get_field_bitsize (void *handle ATTRIBUTE_UNUSED, debug_field field)
02280 {
02281   if (field == NULL || field->static_member)
02282     return (bfd_vma) -1;
02283   return field->u.f.bitsize;
02284 }
02285 
02286 /* Get the visibility of a field.  */
02287 
02288 enum debug_visibility
02289 debug_get_field_visibility (void *handle ATTRIBUTE_UNUSED, debug_field field)
02290 {
02291   if (field == NULL)
02292     return DEBUG_VISIBILITY_IGNORE;
02293   return field->visibility;
02294 }
02295 
02296 /* Get the physical name of a field.  */
02297 
02298 const char *
02299 debug_get_field_physname (void *handle ATTRIBUTE_UNUSED, debug_field field)
02300 {
02301   if (field == NULL || ! field->static_member)
02302     return NULL;
02303   return field->u.s.physname;
02304 }
02305 
02306 /* Write out the debugging information.  This is given a handle to
02307    debugging information, and a set of function pointers to call.  */
02308 
02309 bfd_boolean
02310 debug_write (void *handle, const struct debug_write_fns *fns, void *fhandle)
02311 {
02312   struct debug_handle *info = (struct debug_handle *) handle;
02313   struct debug_unit *u;
02314 
02315   /* We use a mark to tell whether we have already written out a
02316      particular name.  We use an integer, so that we don't have to
02317      clear the mark fields if we happen to write out the same
02318      information more than once.  */
02319   ++info->mark;
02320 
02321   /* The base_id field holds an ID value which will never be used, so
02322      that we can tell whether we have assigned an ID during this call
02323      to debug_write.  */
02324   info->base_id = info->class_id;
02325 
02326   /* We keep a linked list of classes for which was have assigned ID's
02327      during this call to debug_write.  */
02328   info->id_list = NULL;
02329 
02330   for (u = info->units; u != NULL; u = u->next)
02331     {
02332       struct debug_file *f;
02333       bfd_boolean first_file;
02334 
02335       info->current_write_lineno = u->linenos;
02336       info->current_write_lineno_index = 0;
02337 
02338       if (! (*fns->start_compilation_unit) (fhandle, u->files->filename))
02339        return FALSE;
02340 
02341       first_file = TRUE;
02342       for (f = u->files; f != NULL; f = f->next)
02343        {
02344          struct debug_name *n;
02345 
02346          if (first_file)
02347            first_file = FALSE;
02348          else if (! (*fns->start_source) (fhandle, f->filename))
02349            return FALSE;
02350 
02351          if (f->globals != NULL)
02352            for (n = f->globals->list; n != NULL; n = n->next)
02353              if (! debug_write_name (info, fns, fhandle, n))
02354               return FALSE;
02355        }
02356 
02357       /* Output any line number information which hasn't already been
02358          handled.  */
02359       if (! debug_write_linenos (info, fns, fhandle, (bfd_vma) -1))
02360        return FALSE;
02361     }
02362 
02363   return TRUE;
02364 }
02365 
02366 /* Write out an element in a namespace.  */
02367 
02368 static bfd_boolean
02369 debug_write_name (struct debug_handle *info,
02370                 const struct debug_write_fns *fns, void *fhandle,
02371                 struct debug_name *n)
02372 {
02373   switch (n->kind)
02374     {
02375     case DEBUG_OBJECT_TYPE:
02376       if (! debug_write_type (info, fns, fhandle, n->u.type, n)
02377          || ! (*fns->typdef) (fhandle, n->name))
02378        return FALSE;
02379       return TRUE;
02380     case DEBUG_OBJECT_TAG:
02381       if (! debug_write_type (info, fns, fhandle, n->u.tag, n))
02382        return FALSE;
02383       return (*fns->tag) (fhandle, n->name);
02384     case DEBUG_OBJECT_VARIABLE:
02385       if (! debug_write_type (info, fns, fhandle, n->u.variable->type,
02386                            (struct debug_name *) NULL))
02387        return FALSE;
02388       return (*fns->variable) (fhandle, n->name, n->u.variable->kind,
02389                             n->u.variable->val);
02390     case DEBUG_OBJECT_FUNCTION:
02391       return debug_write_function (info, fns, fhandle, n->name,
02392                                n->linkage, n->u.function);
02393     case DEBUG_OBJECT_INT_CONSTANT:
02394       return (*fns->int_constant) (fhandle, n->name, n->u.int_constant);
02395     case DEBUG_OBJECT_FLOAT_CONSTANT:
02396       return (*fns->float_constant) (fhandle, n->name, n->u.float_constant);
02397     case DEBUG_OBJECT_TYPED_CONSTANT:
02398       if (! debug_write_type (info, fns, fhandle, n->u.typed_constant->type,
02399                            (struct debug_name *) NULL))
02400        return FALSE;
02401       return (*fns->typed_constant) (fhandle, n->name,
02402                                  n->u.typed_constant->val);
02403     default:
02404       abort ();
02405       return FALSE;
02406     }
02407   /*NOTREACHED*/
02408 }
02409 
02410 /* Write out a type.  If the type is DEBUG_KIND_NAMED or
02411    DEBUG_KIND_TAGGED, then the name argument is the name for which we
02412    are about to call typedef or tag.  If the type is anything else,
02413    then the name argument is a tag from a DEBUG_KIND_TAGGED type which
02414    points to this one.  */
02415 
02416 static bfd_boolean
02417 debug_write_type (struct debug_handle *info,
02418                 const struct debug_write_fns *fns, void *fhandle,
02419                 struct debug_type *type, struct debug_name *name)
02420 {
02421   unsigned int i;
02422   int is;
02423   const char *tag = NULL;
02424 
02425   /* If we have a name for this type, just output it.  We only output
02426      typedef names after they have been defined.  We output type tags
02427      whenever we are not actually defining them.  */
02428   if ((type->kind == DEBUG_KIND_NAMED
02429        || type->kind == DEBUG_KIND_TAGGED)
02430       && (type->u.knamed->name->mark == info->mark
02431          || (type->kind == DEBUG_KIND_TAGGED
02432              && type->u.knamed->name != name)))
02433     {
02434       if (type->kind == DEBUG_KIND_NAMED)
02435        return (*fns->typedef_type) (fhandle, type->u.knamed->name->name);
02436       else
02437        {
02438          struct debug_type *real;
02439          unsigned int id;
02440 
02441          real = debug_get_real_type ((void *) info, type, NULL);
02442          if (real == NULL)
02443            return (*fns->empty_type) (fhandle);
02444          id = 0;
02445          if ((real->kind == DEBUG_KIND_STRUCT
02446               || real->kind == DEBUG_KIND_UNION
02447               || real->kind == DEBUG_KIND_CLASS
02448               || real->kind == DEBUG_KIND_UNION_CLASS)
02449              && real->u.kclass != NULL)
02450            {
02451              if (real->u.kclass->id <= info->base_id)
02452               {
02453                 if (! debug_set_class_id (info,
02454                                        type->u.knamed->name->name,
02455                                        real))
02456                   return FALSE;
02457               }
02458              id = real->u.kclass->id;
02459            }
02460 
02461          return (*fns->tag_type) (fhandle, type->u.knamed->name->name, id,
02462                                real->kind);
02463        }
02464     }
02465 
02466   /* Mark the name after we have already looked for a known name, so
02467      that we don't just define a type in terms of itself.  We need to
02468      mark the name here so that a struct containing a pointer to
02469      itself will work.  */
02470   if (name != NULL)
02471     name->mark = info->mark;
02472 
02473   if (name != NULL
02474       && type->kind != DEBUG_KIND_NAMED
02475       && type->kind != DEBUG_KIND_TAGGED)
02476     {
02477       assert (name->kind == DEBUG_OBJECT_TAG);
02478       tag = name->name;
02479     }
02480 
02481   switch (type->kind)
02482     {
02483     case DEBUG_KIND_ILLEGAL:
02484       debug_error (_("debug_write_type: illegal type encountered"));
02485       return FALSE;
02486     case DEBUG_KIND_INDIRECT:
02487       if (*type->u.kindirect->slot == DEBUG_TYPE_NULL)
02488        return (*fns->empty_type) (fhandle);
02489       return debug_write_type (info, fns, fhandle, *type->u.kindirect->slot,
02490                             name);
02491     case DEBUG_KIND_VOID:
02492       return (*fns->void_type) (fhandle);
02493     case DEBUG_KIND_INT:
02494       return (*fns->int_type) (fhandle, type->size, type->u.kint);
02495     case DEBUG_KIND_FLOAT:
02496       return (*fns->float_type) (fhandle, type->size);
02497     case DEBUG_KIND_COMPLEX:
02498       return (*fns->complex_type) (fhandle, type->size);
02499     case DEBUG_KIND_BOOL:
02500       return (*fns->bool_type) (fhandle, type->size);
02501     case DEBUG_KIND_STRUCT:
02502     case DEBUG_KIND_UNION:
02503       if (type->u.kclass != NULL)
02504        {
02505          if (type->u.kclass->id <= info->base_id)
02506            {
02507              if (! debug_set_class_id (info, tag, type))
02508               return FALSE;
02509            }
02510 
02511          if (info->mark == type->u.kclass->mark)
02512            {
02513              /* We are currently outputting this struct, or we have
02514                already output it.  I don't know if this can happen,
02515                but it can happen for a class.  */
02516              assert (type->u.kclass->id > info->base_id);
02517              return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
02518                                    type->kind);
02519            }
02520          type->u.kclass->mark = info->mark;
02521        }
02522 
02523       if (! (*fns->start_struct_type) (fhandle, tag,
02524                                    (type->u.kclass != NULL
02525                                    ? type->u.kclass->id
02526                                    : 0),
02527                                    type->kind == DEBUG_KIND_STRUCT,
02528                                    type->size))
02529        return FALSE;
02530       if (type->u.kclass != NULL
02531          && type->u.kclass->fields != NULL)
02532        {
02533          for (i = 0; type->u.kclass->fields[i] != NULL; i++)
02534            {
02535              struct debug_field *f;
02536 
02537              f = type->u.kclass->fields[i];
02538              if (! debug_write_type (info, fns, fhandle, f->type,
02539                                   (struct debug_name *) NULL)
02540                 || ! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
02541                                         f->u.f.bitsize, f->visibility))
02542               return FALSE;
02543            }
02544        }
02545       return (*fns->end_struct_type) (fhandle);
02546     case DEBUG_KIND_CLASS:
02547     case DEBUG_KIND_UNION_CLASS:
02548       return debug_write_class_type (info, fns, fhandle, type, tag);
02549     case DEBUG_KIND_ENUM:
02550       if (type->u.kenum == NULL)
02551        return (*fns->enum_type) (fhandle, tag, (const char **) NULL,
02552                               (bfd_signed_vma *) NULL);
02553       return (*fns->enum_type) (fhandle, tag, type->u.kenum->names,
02554                             type->u.kenum->values);
02555     case DEBUG_KIND_POINTER:
02556       if (! debug_write_type (info, fns, fhandle, type->u.kpointer,
02557                            (struct debug_name *) NULL))
02558        return FALSE;
02559       return (*fns->pointer_type) (fhandle);
02560     case DEBUG_KIND_FUNCTION:
02561       if (! debug_write_type (info, fns, fhandle,
02562                            type->u.kfunction->return_type,
02563                            (struct debug_name *) NULL))
02564        return FALSE;
02565       if (type->u.kfunction->arg_types == NULL)
02566        is = -1;
02567       else
02568        {
02569          for (is = 0; type->u.kfunction->arg_types[is] != NULL; is++)
02570            if (! debug_write_type (info, fns, fhandle,
02571                                 type->u.kfunction->arg_types[is],
02572                                 (struct debug_name *) NULL))
02573              return FALSE;
02574        }
02575       return (*fns->function_type) (fhandle, is,
02576                                 type->u.kfunction->varargs);
02577     case DEBUG_KIND_REFERENCE:
02578       if (! debug_write_type (info, fns, fhandle, type->u.kreference,
02579                            (struct debug_name *) NULL))
02580        return FALSE;
02581       return (*fns->reference_type) (fhandle);
02582     case DEBUG_KIND_RANGE:
02583       if (! debug_write_type (info, fns, fhandle, type->u.krange->type,
02584                            (struct debug_name *) NULL))
02585        return FALSE;
02586       return (*fns->range_type) (fhandle, type->u.krange->lower,
02587                              type->u.krange->upper);
02588     case DEBUG_KIND_ARRAY:
02589       if (! debug_write_type (info, fns, fhandle, type->u.karray->element_type,
02590                            (struct debug_name *) NULL)
02591          || ! debug_write_type (info, fns, fhandle,
02592                              type->u.karray->range_type,
02593                              (struct debug_name *) NULL))
02594        return FALSE;
02595       return (*fns->array_type) (fhandle, type->u.karray->lower,
02596                              type->u.karray->upper,
02597                              type->u.karray->stringp);
02598     case DEBUG_KIND_SET:
02599       if (! debug_write_type (info, fns, fhandle, type->u.kset->type,
02600                            (struct debug_name *) NULL))
02601        return FALSE;
02602       return (*fns->set_type) (fhandle, type->u.kset->bitstringp);
02603     case DEBUG_KIND_OFFSET:
02604       if (! debug_write_type (info, fns, fhandle, type->u.koffset->base_type,
02605                            (struct debug_name *) NULL)
02606          || ! debug_write_type (info, fns, fhandle,
02607                              type->u.koffset->target_type,
02608                              (struct debug_name *) NULL))
02609        return FALSE;
02610       return (*fns->offset_type) (fhandle);
02611     case DEBUG_KIND_METHOD:
02612       if (! debug_write_type (info, fns, fhandle,
02613                            type->u.kmethod->return_type,
02614                            (struct debug_name *) NULL))
02615        return FALSE;
02616       if (type->u.kmethod->arg_types == NULL)
02617        is = -1;
02618       else
02619        {
02620          for (is = 0; type->u.kmethod->arg_types[is] != NULL; is++)
02621            if (! debug_write_type (info, fns, fhandle,
02622                                 type->u.kmethod->arg_types[is],
02623                                 (struct debug_name *) NULL))
02624              return FALSE;
02625        }
02626       if (type->u.kmethod->domain_type != NULL)
02627        {
02628          if (! debug_write_type (info, fns, fhandle,
02629                               type->u.kmethod->domain_type,
02630                               (struct debug_name *) NULL))
02631            return FALSE;
02632        }
02633       return (*fns->method_type) (fhandle,
02634                               type->u.kmethod->domain_type != NULL,
02635                               is,
02636                               type->u.kmethod->varargs);
02637     case DEBUG_KIND_CONST:
02638       if (! debug_write_type (info, fns, fhandle, type->u.kconst,
02639                            (struct debug_name *) NULL))
02640        return FALSE;
02641       return (*fns->const_type) (fhandle);
02642     case DEBUG_KIND_VOLATILE:
02643       if (! debug_write_type (info, fns, fhandle, type->u.kvolatile,
02644                            (struct debug_name *) NULL))
02645        return FALSE;
02646       return (*fns->volatile_type) (fhandle);
02647     case DEBUG_KIND_NAMED:
02648       return debug_write_type (info, fns, fhandle, type->u.knamed->type,
02649                             (struct debug_name *) NULL);
02650     case DEBUG_KIND_TAGGED:
02651       return debug_write_type (info, fns, fhandle, type->u.knamed->type,
02652                             type->u.knamed->name);
02653     default:
02654       abort ();
02655       return FALSE;
02656     }
02657 }
02658 
02659 /* Write out a class type.  */
02660 
02661 static bfd_boolean
02662 debug_write_class_type (struct debug_handle *info,
02663                      const struct debug_write_fns *fns, void *fhandle,
02664                      struct debug_type *type, const char *tag)
02665 {
02666   unsigned int i;
02667   unsigned int id;
02668   struct debug_type *vptrbase;
02669 
02670   if (type->u.kclass == NULL)
02671     {
02672       id = 0;
02673       vptrbase = NULL;
02674     }
02675   else
02676     {
02677       if (type->u.kclass->id <= info->base_id)
02678        {
02679          if (! debug_set_class_id (info, tag, type))
02680            return FALSE;
02681        }
02682 
02683       if (info->mark == type->u.kclass->mark)
02684        {
02685          /* We are currently outputting this class, or we have
02686             already output it.  This can happen when there are
02687             methods for an anonymous class.  */
02688          assert (type->u.kclass->id > info->base_id);
02689          return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
02690                                type->kind);
02691        }
02692       type->u.kclass->mark = info->mark;
02693       id = type->u.kclass->id;
02694 
02695       vptrbase = type->u.kclass->vptrbase;
02696       if (vptrbase != NULL && vptrbase != type)
02697        {
02698          if (! debug_write_type (info, fns, fhandle, vptrbase,
02699                               (struct debug_name *) NULL))
02700            return FALSE;
02701        }
02702     }
02703 
02704   if (! (*fns->start_class_type) (fhandle, tag, id,
02705                               type->kind == DEBUG_KIND_CLASS,
02706                               type->size,
02707                               vptrbase != NULL,
02708                               vptrbase == type))
02709     return FALSE;
02710 
02711   if (type->u.kclass != NULL)
02712     {
02713       if (type->u.kclass->fields != NULL)
02714        {
02715          for (i = 0; type->u.kclass->fields[i] != NULL; i++)
02716            {
02717              struct debug_field *f;
02718 
02719              f = type->u.kclass->fields[i];
02720              if (! debug_write_type (info, fns, fhandle, f->type,
02721                                   (struct debug_name *) NULL))
02722               return FALSE;
02723              if (f->static_member)
02724               {
02725                 if (! (*fns->class_static_member) (fhandle, f->name,
02726                                                f->u.s.physname,
02727                                                f->visibility))
02728                   return FALSE;
02729               }
02730              else
02731               {
02732                 if (! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
02733                                          f->u.f.bitsize, f->visibility))
02734                   return FALSE;
02735               }
02736            }
02737        }
02738 
02739       if (type->u.kclass->baseclasses != NULL)
02740        {
02741          for (i = 0; type->u.kclass->baseclasses[i] != NULL; i++)
02742            {
02743              struct debug_baseclass *b;
02744 
02745              b = type->u.kclass->baseclasses[i];
02746              if (! debug_write_type (info, fns, fhandle, b->type,
02747                                   (struct debug_name *) NULL))
02748               return FALSE;
02749              if (! (*fns->class_baseclass) (fhandle, b->bitpos, b->virtual,
02750                                         b->visibility))
02751               return FALSE;
02752            }
02753        }
02754 
02755       if (type->u.kclass->methods != NULL)
02756        {
02757          for (i = 0; type->u.kclass->methods[i] != NULL; i++)
02758            {
02759              struct debug_method *m;
02760              unsigned int j;
02761 
02762              m = type->u.kclass->methods[i];
02763              if (! (*fns->class_start_method) (fhandle, m->name))
02764               return FALSE;
02765              for (j = 0; m->variants[j] != NULL; j++)
02766               {
02767                 struct debug_method_variant *v;
02768 
02769                 v = m->variants[j];
02770                 if (v->context != NULL)
02771                   {
02772                     if (! debug_write_type (info, fns, fhandle, v->context,
02773                                          (struct debug_name *) NULL))
02774                      return FALSE;
02775                   }
02776                 if (! debug_write_type (info, fns, fhandle, v->type,
02777                                      (struct debug_name *) NULL))
02778                   return FALSE;
02779                 if (v->voffset != VOFFSET_STATIC_METHOD)
02780                   {
02781                     if (! (*fns->class_method_variant) (fhandle, v->physname,
02782                                                    v->visibility,
02783                                                    v->constp,
02784                                                    v->volatilep,
02785                                                    v->voffset,
02786                                                    v->context != NULL))
02787                      return FALSE;
02788                   }
02789                 else
02790                   {
02791                     if (! (*fns->class_static_method_variant) (fhandle,
02792                                                          v->physname,
02793                                                          v->visibility,
02794                                                          v->constp,
02795                                                          v->volatilep))
02796                      return FALSE;
02797                   }
02798               }
02799              if (! (*fns->class_end_method) (fhandle))
02800               return FALSE;
02801            }
02802        }
02803     }
02804 
02805   return (*fns->end_class_type) (fhandle);
02806 }
02807 
02808 /* Write out information for a function.  */
02809 
02810 static bfd_boolean
02811 debug_write_function (struct debug_handle *info,
02812                     const struct debug_write_fns *fns, void *fhandle,
02813                     const char *name, enum debug_object_linkage linkage,
02814                     struct debug_function *function)
02815 {
02816   struct debug_parameter *p;
02817   struct debug_block *b;
02818 
02819   if (! debug_write_linenos (info, fns, fhandle, function->blocks->start))
02820     return FALSE;
02821 
02822   if (! debug_write_type (info, fns, fhandle, function->return_type,
02823                        (struct debug_name *) NULL))
02824     return FALSE;
02825 
02826   if (! (*fns->start_function) (fhandle, name,
02827                             linkage == DEBUG_LINKAGE_GLOBAL))
02828     return FALSE;
02829 
02830   for (p = function->parameters; p != NULL; p = p->next)
02831     {
02832       if (! debug_write_type (info, fns, fhandle, p->type,
02833                            (struct debug_name *) NULL)
02834          || ! (*fns->function_parameter) (fhandle, p->name, p->kind, p->val))
02835        return FALSE;
02836     }
02837 
02838   for (b = function->blocks; b != NULL; b = b->next)
02839     {
02840       if (! debug_write_block (info, fns, fhandle, b))
02841        return FALSE;
02842     }
02843 
02844   return (*fns->end_function) (fhandle);
02845 }
02846 
02847 /* Write out information for a block.  */
02848 
02849 static bfd_boolean
02850 debug_write_block (struct debug_handle *info,
02851                  const struct debug_write_fns *fns, void *fhandle,
02852                  struct debug_block *block)
02853 {
02854   struct debug_name *n;
02855   struct debug_block *b;
02856 
02857   if (! debug_write_linenos (info, fns, fhandle, block->start))
02858     return FALSE;
02859 
02860   /* I can't see any point to writing out a block with no local
02861      variables, so we don't bother, except for the top level block.  */
02862   if (block->locals != NULL || block->parent == NULL)
02863     {
02864       if (! (*fns->start_block) (fhandle, block->start))
02865        return FALSE;
02866     }
02867 
02868   if (block->locals != NULL)
02869     {
02870       for (n = block->locals->list; n != NULL; n = n->next)
02871        {
02872          if (! debug_write_name (info, fns, fhandle, n))
02873            return FALSE;
02874        }
02875     }
02876 
02877   for (b = block->children; b != NULL; b = b->next)
02878     {
02879       if (! debug_write_block (info, fns, fhandle, b))
02880        return FALSE;
02881     }
02882 
02883   if (! debug_write_linenos (info, fns, fhandle, block->end))
02884     return FALSE;
02885 
02886   if (block->locals != NULL || block->parent == NULL)
02887     {
02888       if (! (*fns->end_block) (fhandle, block->end))
02889        return FALSE;
02890     }
02891 
02892   return TRUE;
02893 }
02894 
02895 /* Write out line number information up to ADDRESS.  */
02896 
02897 static bfd_boolean
02898 debug_write_linenos (struct debug_handle *info,
02899                    const struct debug_write_fns *fns, void *fhandle,
02900                    bfd_vma address)
02901 {
02902   while (info->current_write_lineno != NULL)
02903     {
02904       struct debug_lineno *l;
02905 
02906       l = info->current_write_lineno;
02907 
02908       while (info->current_write_lineno_index < DEBUG_LINENO_COUNT)
02909        {
02910          if (l->linenos[info->current_write_lineno_index]
02911              == (unsigned long) -1)
02912            break;
02913 
02914          if (l->addrs[info->current_write_lineno_index] >= address)
02915            return TRUE;
02916 
02917          if (! (*fns->lineno) (fhandle, l->file->filename,
02918                             l->linenos[info->current_write_lineno_index],
02919                             l->addrs[info->current_write_lineno_index]))
02920            return FALSE;
02921 
02922          ++info->current_write_lineno_index;
02923        }
02924 
02925       info->current_write_lineno = l->next;
02926       info->current_write_lineno_index = 0;
02927     }
02928 
02929   return TRUE;
02930 }
02931 
02932 /* Get the ID number for a class.  If during the same call to
02933    debug_write we find a struct with the same definition with the same
02934    name, we use the same ID.  This type of things happens because the
02935    same struct will be defined by multiple compilation units.  */
02936 
02937 static bfd_boolean
02938 debug_set_class_id (struct debug_handle *info, const char *tag,
02939                   struct debug_type *type)
02940 {
02941   struct debug_class_type *c;
02942   struct debug_class_id *l;
02943 
02944   assert (type->kind == DEBUG_KIND_STRUCT
02945          || type->kind == DEBUG_KIND_UNION
02946          || type->kind == DEBUG_KIND_CLASS
02947          || type->kind == DEBUG_KIND_UNION_CLASS);
02948 
02949   c = type->u.kclass;
02950 
02951   if (c->id > info->base_id)
02952     return TRUE;
02953 
02954   for (l = info->id_list; l != NULL; l = l->next)
02955     {
02956       if (l->type->kind != type->kind)
02957        continue;
02958 
02959       if (tag == NULL)
02960        {
02961          if (l->tag != NULL)
02962            continue;
02963        }
02964       else
02965        {
02966          if (l->tag == NULL
02967              || l->tag[0] != tag[0]
02968              || strcmp (l->tag, tag) != 0)
02969            continue;
02970        }
02971 
02972       if (debug_type_samep (info, l->type, type))
02973        {
02974          c->id = l->type->u.kclass->id;
02975          return TRUE;
02976        }
02977     }
02978 
02979   /* There are no identical types.  Use a new ID, and add it to the
02980      list.  */
02981   ++info->class_id;
02982   c->id = info->class_id;
02983 
02984   l = (struct debug_class_id *) xmalloc (sizeof *l);
02985   memset (l, 0, sizeof *l);
02986 
02987   l->type = type;
02988   l->tag = tag;
02989 
02990   l->next = info->id_list;
02991   info->id_list = l;
02992 
02993   return TRUE;
02994 }
02995 
02996 /* See if two types are the same.  At this point, we don't care about
02997    tags and the like.  */
02998 
02999 static bfd_boolean
03000 debug_type_samep (struct debug_handle *info, struct debug_type *t1,
03001                 struct debug_type *t2)
03002 {
03003   struct debug_type_compare_list *l;
03004   struct debug_type_compare_list top;
03005   bfd_boolean ret;
03006 
03007   if (t1 == NULL)
03008     return t2 == NULL;
03009   if (t2 == NULL)
03010     return FALSE;
03011 
03012   while (t1->kind == DEBUG_KIND_INDIRECT)
03013     {
03014       t1 = *t1->u.kindirect->slot;
03015       if (t1 == NULL)
03016        return FALSE;
03017     }
03018   while (t2->kind == DEBUG_KIND_INDIRECT)
03019     {
03020       t2 = *t2->u.kindirect->slot;
03021       if (t2 == NULL)
03022        return FALSE;
03023     }
03024 
03025   if (t1 == t2)
03026     return TRUE;
03027 
03028   /* As a special case, permit a typedef to match a tag, since C++
03029      debugging output will sometimes add a typedef where C debugging
03030      output will not.  */
03031   if (t1->kind == DEBUG_KIND_NAMED
03032       && t2->kind == DEBUG_KIND_TAGGED)
03033     return debug_type_samep (info, t1->u.knamed->type, t2);
03034   else if (t1->kind == DEBUG_KIND_TAGGED
03035           && t2->kind == DEBUG_KIND_NAMED)
03036     return debug_type_samep (info, t1, t2->u.knamed->type);
03037 
03038   if (t1->kind != t2->kind
03039       || t1->size != t2->size)
03040     return FALSE;
03041 
03042   /* Get rid of the trivial cases first.  */
03043   switch (t1->kind)
03044     {
03045     default:
03046       break;
03047     case DEBUG_KIND_VOID:
03048     case DEBUG_KIND_FLOAT:
03049     case DEBUG_KIND_COMPLEX:
03050     case DEBUG_KIND_BOOL:
03051       return TRUE;
03052     case DEBUG_KIND_INT:
03053       return t1->u.kint == t2->u.kint;
03054     }
03055 
03056   /* We have to avoid an infinite recursion.  We do this by keeping a
03057      list of types which we are comparing.  We just keep the list on
03058      the stack.  If we encounter a pair of types we are currently
03059      comparing, we just assume that they are equal.  */
03060   for (l = info->compare_list; l != NULL; l = l->next)
03061     {
03062       if (l->t1 == t1 && l->t2 == t2)
03063        return TRUE;
03064     }
03065 
03066   top.t1 = t1;
03067   top.t2 = t2;
03068   top.next = info->compare_list;
03069   info->compare_list = &top;
03070 
03071   switch (t1->kind)
03072     {
03073     default:
03074       abort ();
03075       ret = FALSE;
03076       break;
03077 
03078     case DEBUG_KIND_STRUCT:
03079     case DEBUG_KIND_UNION:
03080     case DEBUG_KIND_CLASS:
03081     case DEBUG_KIND_UNION_CLASS:
03082       if (t1->u.kclass == NULL)
03083        ret = t2->u.kclass == NULL;
03084       else if (t2->u.kclass == NULL)
03085        ret = FALSE;
03086       else if (t1->u.kclass->id > info->base_id
03087               && t1->u.kclass->id == t2->u.kclass->id)
03088        ret = TRUE;
03089       else
03090        ret = debug_class_type_samep (info, t1, t2);
03091       break;
03092 
03093     case DEBUG_KIND_ENUM:
03094       if (t1->u.kenum == NULL)
03095        ret = t2->u.kenum == NULL;
03096       else if (t2->u.kenum == NULL)
03097        ret = FALSE;
03098       else
03099        {
03100          const char **pn1, **pn2;
03101          bfd_signed_vma *pv1, *pv2;
03102 
03103          pn1 = t1->u.kenum->names;
03104          pn2 = t2->u.kenum->names;
03105          pv1 = t1->u.kenum->values;
03106          pv2 = t2->u.kenum->values;
03107          while (*pn1 != NULL && *pn2 != NULL)
03108            {
03109              if (**pn1 != **pn2
03110                 || *pv1 != *pv2
03111                 || strcmp (*pn1, *pn2) != 0)
03112               break;
03113              ++pn1;
03114              ++pn2;
03115              ++pv1;
03116              ++pv2;
03117            }
03118          ret = *pn1 == NULL && *pn2 == NULL;
03119        }
03120       break;
03121 
03122     case DEBUG_KIND_POINTER:
03123       ret = debug_type_samep (info, t1->u.kpointer, t2->u.kpointer);
03124       break;
03125 
03126     case DEBUG_KIND_FUNCTION:
03127       if (t1->u.kfunction->varargs != t2->u.kfunction->varargs
03128          || ! debug_type_samep (info, t1->u.kfunction->return_type,
03129                              t2->u.kfunction->return_type)
03130          || ((t1->u.kfunction->arg_types == NULL)
03131              != (t2->u.kfunction->arg_types == NULL)))
03132        ret = FALSE;
03133       else if (t1->u.kfunction->arg_types == NULL)
03134        ret = TRUE;
03135       else
03136        {
03137          struct debug_type **a1, **a2;
03138 
03139          a1 = t1->u.kfunction->arg_types;
03140          a2 = t2->u.kfunction->arg_types;
03141          while (*a1 != NULL && *a2 != NULL)
03142            {
03143              if (! debug_type_samep (info, *a1, *a2))
03144               break;
03145              ++a1;
03146              ++a2;
03147            }
03148          ret = *a1 == NULL && *a2 == NULL;
03149        }
03150       break;
03151 
03152     case DEBUG_KIND_REFERENCE:
03153       ret = debug_type_samep (info, t1->u.kreference, t2->u.kreference);
03154       break;
03155 
03156     case DEBUG_KIND_RANGE:
03157       ret = (t1->u.krange->lower == t2->u.krange->lower
03158             && t1->u.krange->upper == t2->u.krange->upper
03159             && debug_type_samep (info, t1->u.krange->type,
03160                               t2->u.krange->type));
03161 
03162     case DEBUG_KIND_ARRAY:
03163       ret = (t1->u.karray->lower == t2->u.karray->lower
03164             && t1->u.karray->upper == t2->u.karray->upper
03165             && t1->u.karray->stringp == t2->u.karray->stringp
03166             && debug_type_samep (info, t1->u.karray->element_type,
03167                               t2->u.karray->element_type));
03168       break;
03169 
03170     case DEBUG_KIND_SET:
03171       ret = (t1->u.kset->bitstringp == t2->u.kset->bitstringp
03172             && debug_type_samep (info, t1->u.kset->type, t2->u.kset->type));
03173       break;
03174 
03175     case DEBUG_KIND_OFFSET:
03176       ret = (debug_type_samep (info, t1->u.koffset->base_type,
03177                             t2->u.koffset->base_type)
03178             && debug_type_samep (info, t1->u.koffset->target_type,
03179                               t2->u.koffset->target_type));
03180       break;
03181 
03182     case DEBUG_KIND_METHOD:
03183       if (t1->u.kmethod->varargs != t2->u.kmethod->varargs
03184          || ! debug_type_samep (info, t1->u.kmethod->return_type,
03185                              t2->u.kmethod->return_type)
03186          || ! debug_type_samep (info, t1->u.kmethod->domain_type,
03187                              t2->u.kmethod->domain_type)
03188          || ((t1->u.kmethod->arg_types == NULL)
03189              != (t2->u.kmethod->arg_types == NULL)))
03190        ret = FALSE;
03191       else if (t1->u.kmethod->arg_types == NULL)
03192        ret = TRUE;
03193       else
03194        {
03195          struct debug_type **a1, **a2;
03196 
03197          a1 = t1->u.kmethod->arg_types;
03198          a2 = t2->u.kmethod->arg_types;
03199          while (*a1 != NULL && *a2 != NULL)
03200            {
03201              if (! debug_type_samep (info, *a1, *a2))
03202               break;
03203              ++a1;
03204              ++a2;
03205            }
03206          ret = *a1 == NULL && *a2 == NULL;
03207        }
03208       break;
03209 
03210     case DEBUG_KIND_CONST:
03211       ret = debug_type_samep (info, t1->u.kconst, t2->u.kconst);
03212       break;
03213 
03214     case DEBUG_KIND_VOLATILE:
03215       ret = debug_type_samep (info, t1->u.kvolatile, t2->u.kvolatile);
03216       break;
03217 
03218     case DEBUG_KIND_NAMED:
03219     case DEBUG_KIND_TAGGED:
03220       ret = (strcmp (t1->u.knamed->name->name, t2->u.knamed->name->name) == 0
03221             && debug_type_samep (info, t1->u.knamed->type,
03222                               t2->u.knamed->type));
03223       break;
03224     }
03225 
03226   info->compare_list = top.next;
03227 
03228   return ret;
03229 }
03230 
03231 /* See if two classes are the same.  This is a subroutine of
03232    debug_type_samep.  */
03233 
03234 static bfd_boolean
03235 debug_class_type_samep (struct debug_handle *info, struct debug_type *t1,
03236                      struct debug_type *t2)
03237 {
03238   struct debug_class_type *c1, *c2;
03239 
03240   c1 = t1->u.kclass;
03241   c2 = t2->u.kclass;
03242 
03243   if ((c1->fields == NULL) != (c2->fields == NULL)
03244       || (c1->baseclasses == NULL) != (c2->baseclasses == NULL)
03245       || (c1->methods == NULL) != (c2->methods == NULL)
03246       || (c1->vptrbase == NULL) != (c2->vptrbase == NULL))
03247     return FALSE;
03248 
03249   if (c1->fields != NULL)
03250     {
03251       struct debug_field **pf1, **pf2;
03252 
03253       for (pf1 = c1->fields, pf2 = c2->fields;
03254           *pf1 != NULL && *pf2 != NULL;
03255           pf1++, pf2++)
03256        {
03257          struct debug_field *f1, *f2;
03258 
03259          f1 = *pf1;
03260          f2 = *pf2;
03261          if (f1->name[0] != f2->name[0]
03262              || f1->visibility != f2->visibility
03263              || f1->static_member != f2->static_member)
03264            return FALSE;
03265          if (f1->static_member)
03266            {
03267              if (strcmp (f1->u.s.physname, f2->u.s.physname) != 0)
03268               return FALSE;
03269            }
03270          else
03271            {
03272              if (f1->u.f.bitpos != f2->u.f.bitpos
03273                 || f1->u.f.bitsize != f2->u.f.bitsize)
03274               return FALSE;
03275            }
03276          /* We do the checks which require function calls last.  We
03277              don't require that the types of fields have the same
03278              names, since that sometimes fails in the presence of
03279              typedefs and we really don't care.  */
03280          if (strcmp (f1->name, f2->name) != 0
03281              || ! debug_type_samep (info,
03282                                  debug_get_real_type ((void *) info,
03283                                                    f1->type, NULL),
03284                                  debug_get_real_type ((void *) info,
03285                                                    f2->type, NULL)))
03286            return FALSE;
03287        }
03288       if (*pf1 != NULL || *pf2 != NULL)
03289        return FALSE;
03290     }
03291 
03292   if (c1->vptrbase != NULL)
03293     {
03294       if (! debug_type_samep (info, c1->vptrbase, c2->vptrbase))
03295        return FALSE;
03296     }
03297 
03298   if (c1->baseclasses != NULL)
03299     {
03300       struct debug_baseclass **pb1, **pb2;
03301 
03302       for (pb1 = c1->baseclasses, pb2 = c2->baseclasses;
03303           *pb1 != NULL && *pb2 != NULL;
03304           ++pb1, ++pb2)
03305        {
03306          struct debug_baseclass *b1, *b2;
03307 
03308          b1 = *pb1;
03309          b2 = *pb2;
03310          if (b1->bitpos != b2->bitpos
03311              || b1->virtual != b2->virtual
03312              || b1->visibility != b2->visibility
03313              || ! debug_type_samep (info, b1->type, b2->type))
03314            return FALSE;
03315        }
03316       if (*pb1 != NULL || *pb2 != NULL)
03317        return FALSE;
03318     }
03319 
03320   if (c1->methods != NULL)
03321     {
03322       struct debug_method **pm1, **pm2;
03323 
03324       for (pm1 = c1->methods, pm2 = c2->methods;
03325           *pm1 != NULL && *pm2 != NULL;
03326           ++pm1, ++pm2)
03327        {
03328          struct debug_method *m1, *m2;
03329 
03330          m1 = *pm1;
03331          m2 = *pm2;
03332          if (m1->name[0] != m2->name[0]
03333              || strcmp (m1->name, m2->name) != 0
03334              || (m1->variants == NULL) != (m2->variants == NULL))
03335            return FALSE;
03336          if (m1->variants == NULL)
03337            {
03338              struct debug_method_variant **pv1, **pv2;
03339 
03340              for (pv1 = m1->variants, pv2 = m2->variants;
03341                  *pv1 != NULL && *pv2 != NULL;
03342                  ++pv1, ++pv2)
03343               {
03344                 struct debug_method_variant *v1, *v2;
03345 
03346                 v1 = *pv1;
03347                 v2 = *pv2;
03348                 if (v1->physname[0] != v2->physname[0]
03349                     || v1->visibility != v2->visibility
03350                     || v1->constp != v2->constp
03351                     || v1->volatilep != v2->volatilep
03352                     || v1->voffset != v2->voffset
03353                     || (v1->context == NULL) != (v2->context == NULL)
03354                     || strcmp (v1->physname, v2->physname) != 0
03355                     || ! debug_type_samep (info, v1->type, v2->type))
03356                   return FALSE;
03357                 if (v1->context != NULL)
03358                   {
03359                     if (! debug_type_samep (info, v1->context,
03360                                          v2->context))
03361                      return FALSE;
03362                   }
03363               }
03364              if (*pv1 != NULL || *pv2 != NULL)
03365               return FALSE;
03366            }
03367        }
03368       if (*pm1 != NULL || *pm2 != NULL)
03369        return FALSE;
03370     }
03371 
03372   return TRUE;
03373 }