edb5835c70d4d9242a126fa1c52ae5418a301875
[binutils-gdb.git] / binutils / debug.c
1 /* debug.c -- Handle generic debugging information.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
4
5 This file is part of GNU Binutils.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* This file implements a generic debugging format. We may eventually
23 have readers which convert different formats into this generic
24 format, and writers which write it out. The initial impetus for
25 this was writing a convertor from stabs to HP IEEE-695 debugging
26 format. */
27
28 #include <stdio.h>
29 #include <assert.h>
30
31 #include "bfd.h"
32 #include "bucomm.h"
33 #include "libiberty.h"
34 #include "debug.h"
35
36 /* Global information we keep for debugging. A pointer to this
37 structure is the debugging handle passed to all the routines. */
38
39 struct debug_handle
40 {
41 /* A linked list of compilation units. */
42 struct debug_unit *units;
43 /* The current compilation unit. */
44 struct debug_unit *current_unit;
45 /* The current source file. */
46 struct debug_file *current_file;
47 /* The current function. */
48 struct debug_function *current_function;
49 /* The current block. */
50 struct debug_block *current_block;
51 /* The current line number information for the current unit. */
52 struct debug_lineno *current_lineno;
53 /* Mark. This is used by debug_write. */
54 unsigned int mark;
55 /* Another mark used by debug_write. */
56 unsigned int class_mark;
57 /* A struct/class ID used by debug_write. */
58 unsigned int class_id;
59 /* The base for class_id for this call to debug_write. */
60 unsigned int base_id;
61 };
62
63 /* Information we keep for a single compilation unit. */
64
65 struct debug_unit
66 {
67 /* The next compilation unit. */
68 struct debug_unit *next;
69 /* A list of files included in this compilation unit. The first
70 file is always the main one, and that is where the main file name
71 is stored. */
72 struct debug_file *files;
73 /* Line number information for this compilation unit. This is not
74 stored by function, because assembler code may have line number
75 information without function information. */
76 struct debug_lineno *linenos;
77 };
78
79 /* Information kept for a single source file. */
80
81 struct debug_file
82 {
83 /* The next source file in this compilation unit. */
84 struct debug_file *next;
85 /* The name of the source file. */
86 const char *filename;
87 /* Global functions, variables, types, etc. */
88 struct debug_namespace *globals;
89 };
90
91 /* A type. */
92
93 struct debug_type
94 {
95 /* Kind of type. */
96 enum debug_type_kind kind;
97 /* Size of type (0 if not known). */
98 unsigned int size;
99 /* Type which is a pointer to this type. */
100 debug_type pointer;
101 /* Tagged union with additional information about the type. */
102 union
103 {
104 /* DEBUG_KIND_INDIRECT. */
105 struct debug_indirect_type *kindirect;
106 /* DEBUG_KIND_INT. */
107 /* Whether the integer is unsigned. */
108 boolean kint;
109 /* DEBUG_KIND_STRUCT, DEBUG_KIND_UNION, DEBUG_KIND_CLASS,
110 DEBUG_KIND_UNION_CLASS. */
111 struct debug_class_type *kclass;
112 /* DEBUG_KIND_ENUM. */
113 struct debug_enum_type *kenum;
114 /* DEBUG_KIND_POINTER. */
115 struct debug_type *kpointer;
116 /* DEBUG_KIND_FUNCTION. */
117 struct debug_function_type *kfunction;
118 /* DEBUG_KIND_REFERENCE. */
119 struct debug_type *kreference;
120 /* DEBUG_KIND_RANGE. */
121 struct debug_range_type *krange;
122 /* DEBUG_KIND_ARRAY. */
123 struct debug_array_type *karray;
124 /* DEBUG_KIND_SET. */
125 struct debug_set_type *kset;
126 /* DEBUG_KIND_OFFSET. */
127 struct debug_offset_type *koffset;
128 /* DEBUG_KIND_METHOD. */
129 struct debug_method_type *kmethod;
130 /* DEBUG_KIND_CONST. */
131 struct debug_type *kconst;
132 /* DEBUG_KIND_VOLATILE. */
133 struct debug_type *kvolatile;
134 /* DEBUG_KIND_NAMED, DEBUG_KIND_TAGGED. */
135 struct debug_named_type *knamed;
136 } u;
137 };
138
139 /* Information kept for an indirect type. */
140
141 struct debug_indirect_type
142 {
143 /* Slot where the final type will appear. */
144 debug_type *slot;
145 /* Tag. */
146 const char *tag;
147 };
148
149 /* Information kept for a struct, union, or class. */
150
151 struct debug_class_type
152 {
153 /* NULL terminated array of fields. */
154 debug_field *fields;
155 /* A mark field used to avoid recursively printing out structs. */
156 unsigned int mark;
157 /* This is used to uniquely identify unnamed structs when printing. */
158 unsigned int id;
159 /* The remaining fields are only used for DEBUG_KIND_CLASS and
160 DEBUG_KIND_UNION_CLASS. */
161 /* NULL terminated array of base classes. */
162 debug_baseclass *baseclasses;
163 /* NULL terminated array of methods. */
164 debug_method *methods;
165 /* The type of the class providing the virtual function table for
166 this class. This may point to the type itself. */
167 debug_type vptrbase;
168 };
169
170 /* Information kept for an enum. */
171
172 struct debug_enum_type
173 {
174 /* NULL terminated array of names. */
175 const char **names;
176 /* Array of corresponding values. */
177 bfd_signed_vma *values;
178 };
179
180 /* Information kept for a function. FIXME: We should be able to
181 record the parameter types. */
182
183 struct debug_function_type
184 {
185 /* Return type. */
186 debug_type return_type;
187 };
188
189 /* Information kept for a range. */
190
191 struct debug_range_type
192 {
193 /* Range base type. */
194 debug_type type;
195 /* Lower bound. */
196 bfd_signed_vma lower;
197 /* Upper bound. */
198 bfd_signed_vma upper;
199 };
200
201 /* Information kept for an array. */
202
203 struct debug_array_type
204 {
205 /* Element type. */
206 debug_type element_type;
207 /* Range type. */
208 debug_type range_type;
209 /* Lower bound. */
210 bfd_signed_vma lower;
211 /* Upper bound. */
212 bfd_signed_vma upper;
213 /* Whether this array is really a string. */
214 boolean stringp;
215 };
216
217 /* Information kept for a set. */
218
219 struct debug_set_type
220 {
221 /* Base type. */
222 debug_type type;
223 /* Whether this set is really a bitstring. */
224 boolean bitstringp;
225 };
226
227 /* Information kept for an offset type (a based pointer). */
228
229 struct debug_offset_type
230 {
231 /* The type the pointer is an offset from. */
232 debug_type base_type;
233 /* The type the pointer points to. */
234 debug_type target_type;
235 };
236
237 /* Information kept for a method type. */
238
239 struct debug_method_type
240 {
241 /* The return type. */
242 debug_type return_type;
243 /* The object type which this method is for. */
244 debug_type domain_type;
245 /* A NULL terminated array of argument types. */
246 debug_type *arg_types;
247 };
248
249 /* Information kept for a named type. */
250
251 struct debug_named_type
252 {
253 /* Name. */
254 struct debug_name *name;
255 /* Real type. */
256 debug_type type;
257 };
258
259 /* A field in a struct or union. */
260
261 struct debug_field
262 {
263 /* Name of the field. */
264 const char *name;
265 /* Type of the field. */
266 struct debug_type *type;
267 /* Visibility of the field. */
268 enum debug_visibility visibility;
269 /* Whether this is a static member. */
270 boolean static_member;
271 union
272 {
273 /* If static_member is false. */
274 struct
275 {
276 /* Bit position of the field in the struct. */
277 unsigned int bitpos;
278 /* Size of the field in bits. */
279 unsigned int bitsize;
280 } f;
281 /* If static_member is true. */
282 struct
283 {
284 const char *physname;
285 } s;
286 } u;
287 };
288
289 /* A base class for an object. */
290
291 struct debug_baseclass
292 {
293 /* Type of the base class. */
294 struct debug_type *type;
295 /* Bit position of the base class in the object. */
296 unsigned int bitpos;
297 /* Whether the base class is virtual. */
298 boolean virtual;
299 /* Visibility of the base class. */
300 enum debug_visibility visibility;
301 };
302
303 /* A method of an object. */
304
305 struct debug_method
306 {
307 /* The name of the method. */
308 const char *name;
309 /* A NULL terminated array of different types of variants. */
310 struct debug_method_variant **variants;
311 };
312
313 /* The variants of a method function of an object. These indicate
314 which method to run. */
315
316 struct debug_method_variant
317 {
318 /* The physical name of the function. */
319 const char *physname;
320 /* The type of the function. */
321 struct debug_type *type;
322 /* The visibility of the function. */
323 enum debug_visibility visibility;
324 /* Whether the function is const. */
325 boolean constp;
326 /* Whether the function is volatile. */
327 boolean volatilep;
328 /* The offset to the function in the virtual function table. */
329 bfd_vma voffset;
330 /* If voffset is VOFFSET_STATIC_METHOD, this is a static method. */
331 #define VOFFSET_STATIC_METHOD (1)
332 /* Context of a virtual method function. */
333 struct debug_type *context;
334 };
335
336 /* A variable. This is the information we keep for a variable object.
337 This has no name; a name is associated with a variable in a
338 debug_name structure. */
339
340 struct debug_variable
341 {
342 /* Kind of variable. */
343 enum debug_var_kind kind;
344 /* Type. */
345 debug_type type;
346 /* Value. The interpretation of the value depends upon kind. */
347 bfd_vma val;
348 };
349
350 /* A function. This has no name; a name is associated with a function
351 in a debug_name structure. */
352
353 struct debug_function
354 {
355 /* Return type. */
356 debug_type return_type;
357 /* Parameter information. */
358 struct debug_parameter *parameters;
359 /* Block information. The first structure on the list is the main
360 block of the function, and describes function local variables. */
361 struct debug_block *blocks;
362 };
363
364 /* A function parameter. */
365
366 struct debug_parameter
367 {
368 /* Next parameter. */
369 struct debug_parameter *next;
370 /* Name. */
371 const char *name;
372 /* Type. */
373 debug_type type;
374 /* Kind. */
375 enum debug_parm_kind kind;
376 /* Value (meaning depends upon kind). */
377 bfd_vma val;
378 };
379
380 /* A typed constant. */
381
382 struct debug_typed_constant
383 {
384 /* Type. */
385 debug_type type;
386 /* Value. FIXME: We may eventually need to support non-integral
387 values. */
388 bfd_vma val;
389 };
390
391 /* Information about a block within a function. */
392
393 struct debug_block
394 {
395 /* Next block with the same parent. */
396 struct debug_block *next;
397 /* Parent block. */
398 struct debug_block *parent;
399 /* List of child blocks. */
400 struct debug_block *children;
401 /* Start address of the block. */
402 bfd_vma start;
403 /* End address of the block. */
404 bfd_vma end;
405 /* Local variables. */
406 struct debug_namespace *locals;
407 };
408
409 /* Line number information we keep for a compilation unit. FIXME:
410 This structure is easy to create, but can be very space
411 inefficient. */
412
413 struct debug_lineno
414 {
415 /* More line number information for this block. */
416 struct debug_lineno *next;
417 /* Source file. */
418 struct debug_file *file;
419 /* Line numbers, terminated by a -1 or the end of the array. */
420 #define DEBUG_LINENO_COUNT 10
421 unsigned long linenos[DEBUG_LINENO_COUNT];
422 /* Addresses for the line numbers. */
423 bfd_vma addrs[DEBUG_LINENO_COUNT];
424 };
425
426 /* A namespace. This is a mapping from names to objects. FIXME: This
427 should be implemented as a hash table. */
428
429 struct debug_namespace
430 {
431 /* List of items in this namespace. */
432 struct debug_name *list;
433 /* Pointer to where the next item in this namespace should go. */
434 struct debug_name **tail;
435 };
436
437 /* Kinds of objects that appear in a namespace. */
438
439 enum debug_object_kind
440 {
441 /* A type. */
442 DEBUG_OBJECT_TYPE,
443 /* A tagged type (really a different sort of namespace). */
444 DEBUG_OBJECT_TAG,
445 /* A variable. */
446 DEBUG_OBJECT_VARIABLE,
447 /* A function. */
448 DEBUG_OBJECT_FUNCTION,
449 /* An integer constant. */
450 DEBUG_OBJECT_INT_CONSTANT,
451 /* A floating point constant. */
452 DEBUG_OBJECT_FLOAT_CONSTANT,
453 /* A typed constant. */
454 DEBUG_OBJECT_TYPED_CONSTANT
455 };
456
457 /* Linkage of an object that appears in a namespace. */
458
459 enum debug_object_linkage
460 {
461 /* Local variable. */
462 DEBUG_LINKAGE_AUTOMATIC,
463 /* Static--either file static or function static, depending upon the
464 namespace is. */
465 DEBUG_LINKAGE_STATIC,
466 /* Global. */
467 DEBUG_LINKAGE_GLOBAL,
468 /* No linkage. */
469 DEBUG_LINKAGE_NONE
470 };
471
472 /* A name in a namespace. */
473
474 struct debug_name
475 {
476 /* Next name in this namespace. */
477 struct debug_name *next;
478 /* Name. */
479 const char *name;
480 /* Mark. This is used by debug_write. */
481 unsigned int mark;
482 /* Kind of object. */
483 enum debug_object_kind kind;
484 /* Linkage of object. */
485 enum debug_object_linkage linkage;
486 /* Tagged union with additional information about the object. */
487 union
488 {
489 /* DEBUG_OBJECT_TYPE. */
490 struct debug_type *type;
491 /* DEBUG_OBJECT_TAG. */
492 struct debug_type *tag;
493 /* DEBUG_OBJECT_VARIABLE. */
494 struct debug_variable *variable;
495 /* DEBUG_OBJECT_FUNCTION. */
496 struct debug_function *function;
497 /* DEBUG_OBJECT_INT_CONSTANT. */
498 bfd_vma int_constant;
499 /* DEBUG_OBJECT_FLOAT_CONSTANT. */
500 double float_constant;
501 /* DEBUG_OBJECT_TYPED_CONSTANT. */
502 struct debug_typed_constant *typed_constant;
503 } u;
504 };
505
506 /* This variable is an ellipsis type. The contents are not used; its
507 address is returned by debug_make_ellipsis_type, and anything which
508 needs to know whether it is dealing with an ellipsis compares
509 addresses. */
510
511 static const struct debug_type debug_ellipsis_type;
512
513 #define ELLIPSIS_P(t) ((t) == &debug_ellipsis_type)
514
515 /* Local functions. */
516
517 static void debug_error PARAMS ((const char *));
518 static struct debug_name *debug_add_to_namespace
519 PARAMS ((struct debug_handle *, struct debug_namespace **, const char *,
520 enum debug_object_kind, enum debug_object_linkage));
521 static struct debug_name *debug_add_to_current_namespace
522 PARAMS ((struct debug_handle *, const char *, enum debug_object_kind,
523 enum debug_object_linkage));
524 static struct debug_type *debug_make_type
525 PARAMS ((struct debug_handle *, enum debug_type_kind, unsigned int));
526 static struct debug_type *debug_get_real_type PARAMS ((PTR, debug_type));
527 static boolean debug_write_name
528 PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
529 struct debug_name *));
530 static boolean debug_write_type
531 PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
532 struct debug_type *, struct debug_name *));
533 static boolean debug_write_class_type
534 PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
535 struct debug_type *, const char *));
536 static boolean debug_write_function
537 PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
538 const char *, enum debug_object_linkage, struct debug_function *));
539 static boolean debug_write_block
540 PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
541 struct debug_block *));
542 \f
543 /* Issue an error message. */
544
545 static void
546 debug_error (message)
547 const char *message;
548 {
549 fprintf (stderr, "%s\n", message);
550 }
551
552 /* Add an object to a namespace. */
553
554 static struct debug_name *
555 debug_add_to_namespace (info, nsp, name, kind, linkage)
556 struct debug_handle *info;
557 struct debug_namespace **nsp;
558 const char *name;
559 enum debug_object_kind kind;
560 enum debug_object_linkage linkage;
561 {
562 struct debug_name *n;
563 struct debug_namespace *ns;
564
565 n = (struct debug_name *) xmalloc (sizeof *n);
566 memset (n, 0, sizeof *n);
567
568 n->name = name;
569 n->kind = kind;
570 n->linkage = linkage;
571
572 ns = *nsp;
573 if (ns == NULL)
574 {
575 ns = (struct debug_namespace *) xmalloc (sizeof *ns);
576 memset (ns, 0, sizeof *ns);
577
578 ns->tail = &ns->list;
579
580 *nsp = ns;
581 }
582
583 *ns->tail = n;
584 ns->tail = &n->next;
585
586 return n;
587 }
588
589 /* Add an object to the current namespace. */
590
591 static struct debug_name *
592 debug_add_to_current_namespace (info, name, kind, linkage)
593 struct debug_handle *info;
594 const char *name;
595 enum debug_object_kind kind;
596 enum debug_object_linkage linkage;
597 {
598 struct debug_namespace **nsp;
599
600 if (info->current_unit == NULL
601 || info->current_file == NULL)
602 {
603 debug_error ("debug_add_to_current_namespace: no current file");
604 return NULL;
605 }
606
607 if (info->current_block != NULL)
608 nsp = &info->current_block->locals;
609 else
610 nsp = &info->current_file->globals;
611
612 return debug_add_to_namespace (info, nsp, name, kind, linkage);
613 }
614 \f
615 /* Return a handle for debugging information. */
616
617 PTR
618 debug_init ()
619 {
620 struct debug_handle *ret;
621
622 ret = (struct debug_handle *) xmalloc (sizeof *ret);
623 memset (ret, 0, sizeof *ret);
624 return (PTR) ret;
625 }
626
627 /* Set the source filename. This implicitly starts a new compilation
628 unit. */
629
630 boolean
631 debug_set_filename (handle, name)
632 PTR handle;
633 const char *name;
634 {
635 struct debug_handle *info = (struct debug_handle *) handle;
636 struct debug_file *nfile;
637 struct debug_unit *nunit;
638
639 if (name == NULL)
640 name = "";
641
642 nfile = (struct debug_file *) xmalloc (sizeof *nfile);
643 memset (nfile, 0, sizeof *nfile);
644
645 nfile->filename = name;
646
647 nunit = (struct debug_unit *) xmalloc (sizeof *nunit);
648 memset (nunit, 0, sizeof *nunit);
649
650 nunit->files = nfile;
651 info->current_file = nfile;
652
653 if (info->current_unit != NULL)
654 info->current_unit->next = nunit;
655 else
656 {
657 assert (info->units == NULL);
658 info->units = nunit;
659 }
660
661 info->current_unit = nunit;
662
663 info->current_function = NULL;
664 info->current_block = NULL;
665 info->current_lineno = NULL;
666
667 return true;
668 }
669
670 /* Append a string to the source filename. */
671
672 boolean
673 debug_append_filename (handle, string)
674 PTR handle;
675 const char *string;
676 {
677 struct debug_handle *info = (struct debug_handle *) handle;
678 char *n;
679
680 if (string == NULL)
681 string = "";
682
683 if (info->current_unit == NULL)
684 {
685 debug_error ("debug_append_filename: no current file");
686 return false;
687 }
688
689 n = (char *) xmalloc (strlen (info->current_unit->files->filename)
690 + strlen (string)
691 + 1);
692 sprintf (n, "%s%s", info->current_unit->files->filename, string);
693 info->current_unit->files->filename = n;
694
695 return true;
696 }
697
698 /* Change source files to the given file name. This is used for
699 include files in a single compilation unit. */
700
701 boolean
702 debug_start_source (handle, name)
703 PTR handle;
704 const char *name;
705 {
706 struct debug_handle *info = (struct debug_handle *) handle;
707 struct debug_file *f, **pf;
708
709 if (name == NULL)
710 name = "";
711
712 if (info->current_unit == NULL)
713 {
714 debug_error ("debug_start_source: no debug_set_filename call");
715 return false;
716 }
717
718 for (f = info->current_unit->files; f != NULL; f = f->next)
719 {
720 if (f->filename[0] == name[0]
721 && f->filename[1] == name[1]
722 && strcmp (f->filename, name) == 0)
723 {
724 info->current_file = f;
725 return true;
726 }
727 }
728
729 f = (struct debug_file *) xmalloc (sizeof *f);
730 memset (f, 0, sizeof *f);
731
732 f->filename = name;
733
734 for (pf = &info->current_file->next;
735 *pf != NULL;
736 pf = &(*pf)->next)
737 ;
738 *pf = f;
739
740 info->current_file = f;
741
742 return true;
743 }
744
745 /* Record a function definition. This implicitly starts a function
746 block. The debug_type argument is the type of the return value.
747 The boolean indicates whether the function is globally visible.
748 The bfd_vma is the address of the start of the function. Currently
749 the parameter types are specified by calls to
750 debug_record_parameter. FIXME: There is no way to specify nested
751 functions. FIXME: I don't think there is any way to record where a
752 function ends. */
753
754 boolean
755 debug_record_function (handle, name, return_type, global, addr)
756 PTR handle;
757 const char *name;
758 debug_type return_type;
759 boolean global;
760 bfd_vma addr;
761 {
762 struct debug_handle *info = (struct debug_handle *) handle;
763 struct debug_function *f;
764 struct debug_block *b;
765 struct debug_name *n;
766
767 if (name == NULL)
768 name = "";
769 if (return_type == NULL)
770 return false;
771
772 if (info->current_unit == NULL)
773 {
774 debug_error ("debug_record_function: no debug_set_filename call");
775 return false;
776 }
777
778 f = (struct debug_function *) xmalloc (sizeof *f);
779 memset (f, 0, sizeof *f);
780
781 f->return_type = return_type;
782
783 b = (struct debug_block *) xmalloc (sizeof *b);
784 memset (b, 0, sizeof *b);
785
786 b->start = addr;
787 b->end = (bfd_vma) -1;
788
789 f->blocks = b;
790
791 info->current_function = f;
792 info->current_block = b;
793
794 /* FIXME: If we could handle nested functions, this would be the
795 place: we would want to use a different namespace. */
796 n = debug_add_to_namespace (info,
797 &info->current_file->globals,
798 name,
799 DEBUG_OBJECT_FUNCTION,
800 (global
801 ? DEBUG_LINKAGE_GLOBAL
802 : DEBUG_LINKAGE_STATIC));
803 if (n == NULL)
804 return false;
805
806 n->u.function = f;
807
808 return true;
809 }
810
811 /* Record a parameter for the current function. */
812
813 boolean
814 debug_record_parameter (handle, name, type, kind, val)
815 PTR handle;
816 const char *name;
817 debug_type type;
818 enum debug_parm_kind kind;
819 bfd_vma val;
820 {
821 struct debug_handle *info = (struct debug_handle *) handle;
822 struct debug_parameter *p, **pp;
823
824 if (name == NULL || type == NULL)
825 return false;
826
827 if (info->current_unit == NULL
828 || info->current_function == NULL)
829 {
830 debug_error ("debug_record_parameter: no current function");
831 return false;
832 }
833
834 p = (struct debug_parameter *) xmalloc (sizeof *p);
835 memset (p, 0, sizeof *p);
836
837 p->name = name;
838 p->type = type;
839 p->kind = kind;
840 p->val = val;
841
842 for (pp = &info->current_function->parameters;
843 *pp != NULL;
844 pp = &(*pp)->next)
845 ;
846 *pp = p;
847
848 return true;
849 }
850
851 /* End a function. FIXME: This should handle function nesting. */
852
853 boolean
854 debug_end_function (handle, addr)
855 PTR handle;
856 bfd_vma addr;
857 {
858 struct debug_handle *info = (struct debug_handle *) handle;
859
860 if (info->current_unit == NULL
861 || info->current_block == NULL
862 || info->current_function == NULL)
863 {
864 debug_error ("debug_end_function: no current function");
865 return false;
866 }
867
868 if (info->current_block->parent != NULL)
869 {
870 debug_error ("debug_end_function: some blocks were not closed");
871 return false;
872 }
873
874 info->current_block->end = addr;
875
876 info->current_function = NULL;
877 info->current_block = NULL;
878
879 return true;
880 }
881
882 /* Start a block in a function. All local information will be
883 recorded in this block, until the matching call to debug_end_block.
884 debug_start_block and debug_end_block may be nested. The bfd_vma
885 argument is the address at which this block starts. */
886
887 boolean
888 debug_start_block (handle, addr)
889 PTR handle;
890 bfd_vma addr;
891 {
892 struct debug_handle *info = (struct debug_handle *) handle;
893 struct debug_block *b, **pb;
894
895 /* We must always have a current block: debug_record_function sets
896 one up. */
897 if (info->current_unit == NULL
898 || info->current_block == NULL)
899 {
900 debug_error ("debug_start_block: no current block");
901 return false;
902 }
903
904 b = (struct debug_block *) xmalloc (sizeof *b);
905 memset (b, 0, sizeof *b);
906
907 b->parent = info->current_block;
908 b->start = addr;
909 b->end = (bfd_vma) -1;
910
911 /* This new block is a child of the current block. */
912 for (pb = &info->current_block->children;
913 *pb != NULL;
914 pb = &(*pb)->next)
915 ;
916 *pb = b;
917
918 info->current_block = b;
919
920 return true;
921 }
922
923 /* Finish a block in a function. This matches the call to
924 debug_start_block. The argument is the address at which this block
925 ends. */
926
927 boolean
928 debug_end_block (handle, addr)
929 PTR handle;
930 bfd_vma addr;
931 {
932 struct debug_handle *info = (struct debug_handle *) handle;
933 struct debug_block *parent;
934
935 if (info->current_unit == NULL
936 || info->current_block == NULL)
937 {
938 debug_error ("debug_end_block: no current block");
939 return false;
940 }
941
942 parent = info->current_block->parent;
943 if (parent == NULL)
944 {
945 debug_error ("debug_end_block: attempt to close top level block");
946 return false;
947 }
948
949 info->current_block->end = addr;
950
951 info->current_block = parent;
952
953 return true;
954 }
955
956 /* Associate a line number in the current source file and function
957 with a given address. */
958
959 boolean
960 debug_record_line (handle, lineno, addr)
961 PTR handle;
962 unsigned long lineno;
963 bfd_vma addr;
964 {
965 struct debug_handle *info = (struct debug_handle *) handle;
966 struct debug_lineno *l;
967 unsigned int i;
968
969 if (info->current_unit == NULL)
970 {
971 debug_error ("debug_record_line: no current unit");
972 return false;
973 }
974
975 l = info->current_lineno;
976 if (l != NULL && l->file == info->current_file)
977 {
978 for (i = 0; i < DEBUG_LINENO_COUNT; i++)
979 {
980 if (l->linenos[i] == (unsigned long) -1)
981 {
982 l->linenos[i] = lineno;
983 l->addrs[i] = addr;
984 return true;
985 }
986 }
987 }
988
989 /* If we get here, then either 1) there is no current_lineno
990 structure, which means this is the first line number in this
991 compilation unit, 2) the current_lineno structure is for a
992 different file, or 3) the current_lineno structure is full.
993 Regardless, we want to allocate a new debug_lineno structure, put
994 it in the right place, and make it the new current_lineno
995 structure. */
996
997 l = (struct debug_lineno *) xmalloc (sizeof *l);
998 memset (l, 0, sizeof *l);
999
1000 l->file = info->current_file;
1001 l->linenos[0] = lineno;
1002 l->addrs[0] = addr;
1003 for (i = 1; i < DEBUG_LINENO_COUNT; i++)
1004 l->linenos[i] = (unsigned long) -1;
1005
1006 if (info->current_lineno != NULL)
1007 info->current_lineno->next = l;
1008 else
1009 info->current_unit->linenos = l;
1010
1011 info->current_lineno = l;
1012
1013 return true;
1014 }
1015
1016 /* Start a named common block. This is a block of variables that may
1017 move in memory. */
1018
1019 boolean
1020 debug_start_common_block (handle, name)
1021 PTR handle;
1022 const char *name;
1023 {
1024 /* FIXME */
1025 debug_error ("debug_start_common_block: not implemented");
1026 return false;
1027 }
1028
1029 /* End a named common block. */
1030
1031 boolean
1032 debug_end_common_block (handle, name)
1033 PTR handle;
1034 const char *name;
1035 {
1036 /* FIXME */
1037 debug_error ("debug_end_common_block: not implemented");
1038 return false;
1039 }
1040
1041 /* Record a named integer constant. */
1042
1043 boolean
1044 debug_record_int_const (handle, name, val)
1045 PTR handle;
1046 const char *name;
1047 bfd_vma val;
1048 {
1049 struct debug_handle *info = (struct debug_handle *) handle;
1050 struct debug_name *n;
1051
1052 if (name == NULL)
1053 return false;
1054
1055 n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_INT_CONSTANT,
1056 DEBUG_LINKAGE_NONE);
1057 if (n == NULL)
1058 return false;
1059
1060 n->u.int_constant = val;
1061
1062 return true;
1063 }
1064
1065 /* Record a named floating point constant. */
1066
1067 boolean
1068 debug_record_float_const (handle, name, val)
1069 PTR handle;
1070 const char *name;
1071 double val;
1072 {
1073 struct debug_handle *info = (struct debug_handle *) handle;
1074 struct debug_name *n;
1075
1076 if (name == NULL)
1077 return false;
1078
1079 n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_FLOAT_CONSTANT,
1080 DEBUG_LINKAGE_NONE);
1081 if (n == NULL)
1082 return false;
1083
1084 n->u.float_constant = val;
1085
1086 return true;
1087 }
1088
1089 /* Record a typed constant with an integral value. */
1090
1091 boolean
1092 debug_record_typed_const (handle, name, type, val)
1093 PTR handle;
1094 const char *name;
1095 debug_type type;
1096 bfd_vma val;
1097 {
1098 struct debug_handle *info = (struct debug_handle *) handle;
1099 struct debug_name *n;
1100 struct debug_typed_constant *tc;
1101
1102 if (name == NULL || type == NULL)
1103 return false;
1104
1105 n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_TYPED_CONSTANT,
1106 DEBUG_LINKAGE_NONE);
1107 if (n == NULL)
1108 return false;
1109
1110 tc = (struct debug_typed_constant *) xmalloc (sizeof *tc);
1111 memset (tc, 0, sizeof *tc);
1112
1113 tc->type = type;
1114 tc->val = val;
1115
1116 n->u.typed_constant = tc;
1117
1118 return true;
1119 }
1120
1121 /* Record a label. */
1122
1123 boolean
1124 debug_record_label (handle, name, type, addr)
1125 PTR handle;
1126 const char *name;
1127 debug_type type;
1128 bfd_vma addr;
1129 {
1130 /* FIXME. */
1131 debug_error ("debug_record_label not implemented");
1132 return false;
1133 }
1134
1135 /* Record a variable. */
1136
1137 boolean
1138 debug_record_variable (handle, name, type, kind, val)
1139 PTR handle;
1140 const char *name;
1141 debug_type type;
1142 enum debug_var_kind kind;
1143 bfd_vma val;
1144 {
1145 struct debug_handle *info = (struct debug_handle *) handle;
1146 struct debug_namespace **nsp;
1147 enum debug_object_linkage linkage;
1148 struct debug_name *n;
1149 struct debug_variable *v;
1150
1151 if (name == NULL || type == NULL)
1152 return false;
1153
1154 if (info->current_unit == NULL
1155 || info->current_file == NULL)
1156 {
1157 debug_error ("debug_record_variable: no current file");
1158 return false;
1159 }
1160
1161 if (kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
1162 {
1163 nsp = &info->current_file->globals;
1164 if (kind == DEBUG_GLOBAL)
1165 linkage = DEBUG_LINKAGE_GLOBAL;
1166 else
1167 linkage = DEBUG_LINKAGE_STATIC;
1168 }
1169 else
1170 {
1171 if (info->current_block == NULL)
1172 {
1173 debug_error ("debug_record_variable: no current block");
1174 return false;
1175 }
1176 nsp = &info->current_block->locals;
1177 linkage = DEBUG_LINKAGE_AUTOMATIC;
1178 }
1179
1180 n = debug_add_to_namespace (info, nsp, name, DEBUG_OBJECT_VARIABLE, linkage);
1181 if (n == NULL)
1182 return false;
1183
1184 v = (struct debug_variable *) xmalloc (sizeof *v);
1185 memset (v, 0, sizeof *v);
1186
1187 v->kind = kind;
1188 v->type = type;
1189 v->val = val;
1190
1191 n->u.variable = v;
1192
1193 return true;
1194 }
1195
1196 /* Make a type with a given kind and size. */
1197
1198 /*ARGSUSED*/
1199 static struct debug_type *
1200 debug_make_type (info, kind, size)
1201 struct debug_handle *info;
1202 enum debug_type_kind kind;
1203 unsigned int size;
1204 {
1205 struct debug_type *t;
1206
1207 t = (struct debug_type *) xmalloc (sizeof *t);
1208 memset (t, 0, sizeof *t);
1209
1210 t->kind = kind;
1211 t->size = size;
1212
1213 return t;
1214 }
1215
1216 /* Make an indirect type which may be used as a placeholder for a type
1217 which is referenced before it is defined. */
1218
1219 debug_type
1220 debug_make_indirect_type (handle, slot, tag)
1221 PTR handle;
1222 debug_type *slot;
1223 const char *tag;
1224 {
1225 struct debug_handle *info = (struct debug_handle *) handle;
1226 struct debug_type *t;
1227 struct debug_indirect_type *i;
1228
1229 t = debug_make_type (info, DEBUG_KIND_INDIRECT, 0);
1230 if (t == NULL)
1231 return DEBUG_TYPE_NULL;
1232
1233 i = (struct debug_indirect_type *) xmalloc (sizeof *i);
1234 memset (i, 0, sizeof *i);
1235
1236 i->slot = slot;
1237 i->tag = tag;
1238
1239 t->u.kindirect = i;
1240
1241 return t;
1242 }
1243
1244 /* Make an ellipsis type. This is not a type at all, but is a marker
1245 suitable for appearing in the list of argument types passed to
1246 debug_make_method_type. It should be used to indicate a method
1247 which takes a variable number of arguments. */
1248
1249 debug_type
1250 debug_make_ellipsis_type (handle)
1251 PTR handle;
1252 {
1253 return (debug_type) &debug_ellipsis_type;
1254 }
1255
1256 /* Make a void type. There is only one of these. */
1257
1258 debug_type
1259 debug_make_void_type (handle)
1260 PTR handle;
1261 {
1262 struct debug_handle *info = (struct debug_handle *) handle;
1263
1264 return debug_make_type (info, DEBUG_KIND_VOID, 0);
1265 }
1266
1267 /* Make an integer type of a given size. The boolean argument is true
1268 if the integer is unsigned. */
1269
1270 debug_type
1271 debug_make_int_type (handle, size, unsignedp)
1272 PTR handle;
1273 unsigned int size;
1274 boolean unsignedp;
1275 {
1276 struct debug_handle *info = (struct debug_handle *) handle;
1277 struct debug_type *t;
1278
1279 t = debug_make_type (info, DEBUG_KIND_INT, size);
1280 if (t == NULL)
1281 return DEBUG_TYPE_NULL;
1282
1283 t->u.kint = unsignedp;
1284
1285 return t;
1286 }
1287
1288 /* Make a floating point type of a given size. FIXME: On some
1289 platforms, like an Alpha, you probably need to be able to specify
1290 the format. */
1291
1292 debug_type
1293 debug_make_float_type (handle, size)
1294 PTR handle;
1295 unsigned int size;
1296 {
1297 struct debug_handle *info = (struct debug_handle *) handle;
1298
1299 return debug_make_type (info, DEBUG_KIND_FLOAT, size);
1300 }
1301
1302 /* Make a boolean type of a given size. */
1303
1304 debug_type
1305 debug_make_bool_type (handle, size)
1306 PTR handle;
1307 unsigned int size;
1308 {
1309 struct debug_handle *info = (struct debug_handle *) handle;
1310
1311 return debug_make_type (info, DEBUG_KIND_BOOL, size);
1312 }
1313
1314 /* Make a complex type of a given size. */
1315
1316 debug_type
1317 debug_make_complex_type (handle, size)
1318 PTR handle;
1319 unsigned int size;
1320 {
1321 struct debug_handle *info = (struct debug_handle *) handle;
1322
1323 return debug_make_type (info, DEBUG_KIND_COMPLEX, size);
1324 }
1325
1326 /* Make a structure type. The second argument is true for a struct,
1327 false for a union. The third argument is the size of the struct.
1328 The fourth argument is a NULL terminated array of fields. */
1329
1330 debug_type
1331 debug_make_struct_type (handle, structp, size, fields)
1332 PTR handle;
1333 boolean structp;
1334 bfd_vma size;
1335 debug_field *fields;
1336 {
1337 struct debug_handle *info = (struct debug_handle *) handle;
1338 struct debug_type *t;
1339 struct debug_class_type *c;
1340
1341 t = debug_make_type (info,
1342 structp ? DEBUG_KIND_STRUCT : DEBUG_KIND_UNION,
1343 size);
1344 if (t == NULL)
1345 return DEBUG_TYPE_NULL;
1346
1347 c = (struct debug_class_type *) xmalloc (sizeof *c);
1348 memset (c, 0, sizeof *c);
1349
1350 c->fields = fields;
1351
1352 t->u.kclass = c;
1353
1354 return t;
1355 }
1356
1357 /* Make an object type. The first three arguments after the handle
1358 are the same as for debug_make_struct_type. The next arguments are
1359 a NULL terminated array of base classes, a NULL terminated array of
1360 methods, the type of the object holding the virtual function table
1361 if it is not this object, and a boolean which is true if this
1362 object has its own virtual function table. */
1363
1364 debug_type
1365 debug_make_object_type (handle, structp, size, fields, baseclasses,
1366 methods, vptrbase, ownvptr)
1367 PTR handle;
1368 boolean structp;
1369 bfd_vma size;
1370 debug_field *fields;
1371 debug_baseclass *baseclasses;
1372 debug_method *methods;
1373 debug_type vptrbase;
1374 boolean ownvptr;
1375 {
1376 struct debug_handle *info = (struct debug_handle *) handle;
1377 struct debug_type *t;
1378 struct debug_class_type *c;
1379
1380 t = debug_make_type (info,
1381 structp ? DEBUG_KIND_CLASS : DEBUG_KIND_UNION_CLASS,
1382 size);
1383 if (t == NULL)
1384 return DEBUG_TYPE_NULL;
1385
1386 c = (struct debug_class_type *) xmalloc (sizeof *c);
1387 memset (c, 0, sizeof *c);
1388
1389 c->fields = fields;
1390 c->baseclasses = baseclasses;
1391 c->methods = methods;
1392 if (ownvptr)
1393 c->vptrbase = t;
1394 else
1395 c->vptrbase = vptrbase;
1396
1397 t->u.kclass = c;
1398
1399 return t;
1400 }
1401
1402 /* Make an enumeration type. The arguments are a null terminated
1403 array of strings, and an array of corresponding values. */
1404
1405 debug_type
1406 debug_make_enum_type (handle, names, values)
1407 PTR handle;
1408 const char **names;
1409 bfd_signed_vma *values;
1410 {
1411 struct debug_handle *info = (struct debug_handle *) handle;
1412 struct debug_type *t;
1413 struct debug_enum_type *e;
1414
1415 t = debug_make_type (info, DEBUG_KIND_ENUM, 0);
1416 if (t == NULL)
1417 return DEBUG_TYPE_NULL;
1418
1419 e = (struct debug_enum_type *) xmalloc (sizeof *e);
1420 memset (e, 0, sizeof *e);
1421
1422 e->names = names;
1423 e->values = values;
1424
1425 t->u.kenum = e;
1426
1427 return t;
1428 }
1429
1430 /* Make a pointer to a given type. */
1431
1432 debug_type
1433 debug_make_pointer_type (handle, type)
1434 PTR handle;
1435 debug_type type;
1436 {
1437 struct debug_handle *info = (struct debug_handle *) handle;
1438 struct debug_type *t;
1439
1440 if (type == NULL)
1441 return DEBUG_TYPE_NULL;
1442
1443 if (type->pointer != DEBUG_TYPE_NULL)
1444 return type->pointer;
1445
1446 t = debug_make_type (info, DEBUG_KIND_POINTER, 0);
1447 if (t == NULL)
1448 return DEBUG_TYPE_NULL;
1449
1450 t->u.kpointer = type;
1451
1452 type->pointer = t;
1453
1454 return t;
1455 }
1456
1457 /* Make a function returning a given type. FIXME: We should be able
1458 to record the parameter types. */
1459
1460 debug_type
1461 debug_make_function_type (handle, type)
1462 PTR handle;
1463 debug_type type;
1464 {
1465 struct debug_handle *info = (struct debug_handle *) handle;
1466 struct debug_type *t;
1467 struct debug_function_type *f;
1468
1469 if (type == NULL)
1470 return DEBUG_TYPE_NULL;
1471
1472 t = debug_make_type (info, DEBUG_KIND_FUNCTION, 0);
1473 if (t == NULL)
1474 return DEBUG_TYPE_NULL;
1475
1476 f = (struct debug_function_type *) xmalloc (sizeof *f);
1477 memset (f, 0, sizeof *f);
1478
1479 f->return_type = type;
1480
1481 t->u.kfunction = f;
1482
1483 return t;
1484 }
1485
1486 /* Make a reference to a given type. */
1487
1488 debug_type
1489 debug_make_reference_type (handle, type)
1490 PTR handle;
1491 debug_type type;
1492 {
1493 struct debug_handle *info = (struct debug_handle *) handle;
1494 struct debug_type *t;
1495
1496 if (type == NULL)
1497 return DEBUG_TYPE_NULL;
1498
1499 t = debug_make_type (info, DEBUG_KIND_REFERENCE, 0);
1500 if (t == NULL)
1501 return DEBUG_TYPE_NULL;
1502
1503 t->u.kreference = type;
1504
1505 return t;
1506 }
1507
1508 /* Make a range of a given type from a lower to an upper bound. */
1509
1510 debug_type
1511 debug_make_range_type (handle, type, lower, upper)
1512 PTR handle;
1513 debug_type type;
1514 bfd_signed_vma lower;
1515 bfd_signed_vma upper;
1516 {
1517 struct debug_handle *info = (struct debug_handle *) handle;
1518 struct debug_type *t;
1519 struct debug_range_type *r;
1520
1521 if (type == NULL)
1522 return DEBUG_TYPE_NULL;
1523
1524 t = debug_make_type (info, DEBUG_KIND_RANGE, 0);
1525 if (t == NULL)
1526 return DEBUG_TYPE_NULL;
1527
1528 r = (struct debug_range_type *) xmalloc (sizeof *r);
1529 memset (r, 0, sizeof *r);
1530
1531 r->type = type;
1532 r->lower = lower;
1533 r->upper = upper;
1534
1535 t->u.krange = r;
1536
1537 return t;
1538 }
1539
1540 /* Make an array type. The second argument is the type of an element
1541 of the array. The third argument is the type of a range of the
1542 array. The fourth and fifth argument are the lower and upper
1543 bounds, respectively. The sixth argument is true if this array is
1544 actually a string, as in C. */
1545
1546 debug_type
1547 debug_make_array_type (handle, element_type, range_type, lower, upper,
1548 stringp)
1549 PTR handle;
1550 debug_type element_type;
1551 debug_type range_type;
1552 bfd_signed_vma lower;
1553 bfd_signed_vma upper;
1554 boolean stringp;
1555 {
1556 struct debug_handle *info = (struct debug_handle *) handle;
1557 struct debug_type *t;
1558 struct debug_array_type *a;
1559
1560 if (element_type == NULL || range_type == NULL)
1561 return DEBUG_TYPE_NULL;
1562
1563 t = debug_make_type (info, DEBUG_KIND_ARRAY, 0);
1564 if (t == NULL)
1565 return DEBUG_TYPE_NULL;
1566
1567 a = (struct debug_array_type *) xmalloc (sizeof *a);
1568 memset (a, 0, sizeof *a);
1569
1570 a->element_type = element_type;
1571 a->range_type = range_type;
1572 a->lower = lower;
1573 a->upper = upper;
1574 a->stringp = stringp;
1575
1576 t->u.karray = a;
1577
1578 return t;
1579 }
1580
1581 /* Make a set of a given type. For example, a Pascal set type. The
1582 boolean argument is true if this set is actually a bitstring, as in
1583 CHILL. */
1584
1585 debug_type
1586 debug_make_set_type (handle, type, bitstringp)
1587 PTR handle;
1588 debug_type type;
1589 boolean bitstringp;
1590 {
1591 struct debug_handle *info = (struct debug_handle *) handle;
1592 struct debug_type *t;
1593 struct debug_set_type *s;
1594
1595 if (type == NULL)
1596 return DEBUG_TYPE_NULL;
1597
1598 t = debug_make_type (info, DEBUG_KIND_SET, 0);
1599 if (t == NULL)
1600 return DEBUG_TYPE_NULL;
1601
1602 s = (struct debug_set_type *) xmalloc (sizeof *s);
1603 memset (s, 0, sizeof *s);
1604
1605 s->type = type;
1606 s->bitstringp = bitstringp;
1607
1608 t->u.kset = s;
1609
1610 return t;
1611 }
1612
1613 /* Make a type for a pointer which is relative to an object. The
1614 second argument is the type of the object to which the pointer is
1615 relative. The third argument is the type that the pointer points
1616 to. */
1617
1618 debug_type
1619 debug_make_offset_type (handle, base_type, target_type)
1620 PTR handle;
1621 debug_type base_type;
1622 debug_type target_type;
1623 {
1624 struct debug_handle *info = (struct debug_handle *) handle;
1625 struct debug_type *t;
1626 struct debug_offset_type *o;
1627
1628 if (base_type == NULL || target_type == NULL)
1629 return DEBUG_TYPE_NULL;
1630
1631 t = debug_make_type (info, DEBUG_KIND_OFFSET, 0);
1632 if (t == NULL)
1633 return DEBUG_TYPE_NULL;
1634
1635 o = (struct debug_offset_type *) xmalloc (sizeof *o);
1636 memset (o, 0, sizeof *o);
1637
1638 o->base_type = base_type;
1639 o->target_type = target_type;
1640
1641 t->u.koffset = o;
1642
1643 return t;
1644 }
1645
1646 /* Make a type for a method function. The second argument is the
1647 return type, the third argument is the domain, and the fourth
1648 argument is a NULL terminated array of argument types. */
1649
1650 debug_type
1651 debug_make_method_type (handle, return_type, domain_type, arg_types)
1652 PTR handle;
1653 debug_type return_type;
1654 debug_type domain_type;
1655 debug_type *arg_types;
1656 {
1657 struct debug_handle *info = (struct debug_handle *) handle;
1658 struct debug_type *t;
1659 struct debug_method_type *m;
1660
1661 if (return_type == NULL)
1662 return DEBUG_TYPE_NULL;
1663
1664 t = debug_make_type (info, DEBUG_KIND_METHOD, 0);
1665 if (t == NULL)
1666 return DEBUG_TYPE_NULL;
1667
1668 m = (struct debug_method_type *) xmalloc (sizeof *m);
1669 memset (m, 0, sizeof *m);
1670
1671 m->return_type = return_type;
1672 m->domain_type = domain_type;
1673 m->arg_types = arg_types;
1674
1675 t->u.kmethod = m;
1676
1677 return t;
1678 }
1679
1680 /* Make a const qualified version of a given type. */
1681
1682 debug_type
1683 debug_make_const_type (handle, type)
1684 PTR handle;
1685 debug_type type;
1686 {
1687 struct debug_handle *info = (struct debug_handle *) handle;
1688 struct debug_type *t;
1689
1690 if (type == NULL)
1691 return DEBUG_TYPE_NULL;
1692
1693 t = debug_make_type (info, DEBUG_KIND_CONST, 0);
1694 if (t == NULL)
1695 return DEBUG_TYPE_NULL;
1696
1697 t->u.kconst = type;
1698
1699 return t;
1700 }
1701
1702 /* Make a volatile qualified version of a given type. */
1703
1704 debug_type
1705 debug_make_volatile_type (handle, type)
1706 PTR handle;
1707 debug_type type;
1708 {
1709 struct debug_handle *info = (struct debug_handle *) handle;
1710 struct debug_type *t;
1711
1712 if (type == NULL)
1713 return DEBUG_TYPE_NULL;
1714
1715 t = debug_make_type (info, DEBUG_KIND_VOLATILE, 0);
1716 if (t == NULL)
1717 return DEBUG_TYPE_NULL;
1718
1719 t->u.kvolatile = type;
1720
1721 return t;
1722 }
1723
1724 /* Make an undefined tagged type. For example, a struct which has
1725 been mentioned, but not defined. */
1726
1727 debug_type
1728 debug_make_undefined_tagged_type (handle, name, kind)
1729 PTR handle;
1730 const char *name;
1731 enum debug_type_kind kind;
1732 {
1733 struct debug_handle *info = (struct debug_handle *) handle;
1734 struct debug_type *t;
1735
1736 if (name == NULL)
1737 return DEBUG_TYPE_NULL;
1738
1739 switch (kind)
1740 {
1741 case DEBUG_KIND_STRUCT:
1742 case DEBUG_KIND_UNION:
1743 case DEBUG_KIND_CLASS:
1744 case DEBUG_KIND_UNION_CLASS:
1745 case DEBUG_KIND_ENUM:
1746 break;
1747
1748 default:
1749 debug_error ("debug_make_undefined_type: unsupported kind");
1750 return DEBUG_TYPE_NULL;
1751 }
1752
1753 t = debug_make_type (info, kind, 0);
1754 if (t == NULL)
1755 return DEBUG_TYPE_NULL;
1756
1757 return debug_tag_type (handle, name, t);
1758 }
1759
1760 /* Make a base class for an object. The second argument is the base
1761 class type. The third argument is the bit position of this base
1762 class in the object (always 0 unless doing multiple inheritance).
1763 The fourth argument is whether this is a virtual class. The fifth
1764 argument is the visibility of the base class. */
1765
1766 /*ARGSUSED*/
1767 debug_baseclass
1768 debug_make_baseclass (handle, type, bitpos, virtual, visibility)
1769 PTR handle;
1770 debug_type type;
1771 bfd_vma bitpos;
1772 boolean virtual;
1773 enum debug_visibility visibility;
1774 {
1775 struct debug_baseclass *b;
1776
1777 b = (struct debug_baseclass *) xmalloc (sizeof *b);
1778 memset (b, 0, sizeof *b);
1779
1780 b->type = type;
1781 b->bitpos = bitpos;
1782 b->virtual = virtual;
1783 b->visibility = visibility;
1784
1785 return b;
1786 }
1787
1788 /* Make a field for a struct. The second argument is the name. The
1789 third argument is the type of the field. The fourth argument is
1790 the bit position of the field. The fifth argument is the size of
1791 the field (it may be zero). The sixth argument is the visibility
1792 of the field. */
1793
1794 /*ARGSUSED*/
1795 debug_field
1796 debug_make_field (handle, name, type, bitpos, bitsize, visibility)
1797 PTR handle;
1798 const char *name;
1799 debug_type type;
1800 bfd_vma bitpos;
1801 bfd_vma bitsize;
1802 enum debug_visibility visibility;
1803 {
1804 struct debug_field *f;
1805
1806 f = (struct debug_field *) xmalloc (sizeof *f);
1807 memset (f, 0, sizeof *f);
1808
1809 f->name = name;
1810 f->type = type;
1811 f->static_member = false;
1812 f->u.f.bitpos = bitpos;
1813 f->u.f.bitsize = bitsize;
1814 f->visibility = visibility;
1815
1816 return f;
1817 }
1818
1819 /* Make a static member of an object. The second argument is the
1820 name. The third argument is the type of the member. The fourth
1821 argument is the physical name of the member (i.e., the name as a
1822 global variable). The fifth argument is the visibility of the
1823 member. */
1824
1825 /*ARGSUSED*/
1826 debug_field
1827 debug_make_static_member (handle, name, type, physname, visibility)
1828 PTR handle;
1829 const char *name;
1830 debug_type type;
1831 const char *physname;
1832 enum debug_visibility visibility;
1833 {
1834 struct debug_field *f;
1835
1836 f = (struct debug_field *) xmalloc (sizeof *f);
1837 memset (f, 0, sizeof *f);
1838
1839 f->name = name;
1840 f->type = type;
1841 f->static_member = true;
1842 f->u.s.physname = physname;
1843 f->visibility = visibility;
1844
1845 return f;
1846 }
1847
1848 /* Make a method. The second argument is the name, and the third
1849 argument is a NULL terminated array of method variants. */
1850
1851 /*ARGSUSED*/
1852 debug_method
1853 debug_make_method (handle, name, variants)
1854 PTR handle;
1855 const char *name;
1856 debug_method_variant *variants;
1857 {
1858 struct debug_method *m;
1859
1860 m = (struct debug_method *) xmalloc (sizeof *m);
1861 memset (m, 0, sizeof *m);
1862
1863 m->name = name;
1864 m->variants = variants;
1865
1866 return m;
1867 }
1868
1869 /* Make a method argument. The second argument is the real name of
1870 the function. The third argument is the type of the function. The
1871 fourth argument is the visibility. The fifth argument is whether
1872 this is a const function. The sixth argument is whether this is a
1873 volatile function. The seventh argument is the offset in the
1874 virtual function table, if any. The eighth argument is the virtual
1875 function context. FIXME: Are the const and volatile arguments
1876 necessary? Could we just use debug_make_const_type? */
1877
1878 /*ARGSUSED*/
1879 debug_method_variant
1880 debug_make_method_variant (handle, physname, type, visibility, constp,
1881 volatilep, voffset, context)
1882 PTR handle;
1883 const char *physname;
1884 debug_type type;
1885 enum debug_visibility visibility;
1886 boolean constp;
1887 boolean volatilep;
1888 bfd_vma voffset;
1889 debug_type context;
1890 {
1891 struct debug_method_variant *m;
1892
1893 m = (struct debug_method_variant *) xmalloc (sizeof *m);
1894 memset (m, 0, sizeof *m);
1895
1896 m->physname = physname;
1897 m->type = type;
1898 m->visibility = visibility;
1899 m->constp = constp;
1900 m->volatilep = volatilep;
1901 m->voffset = voffset;
1902 m->context = context;
1903
1904 return m;
1905 }
1906
1907 /* Make a static method argument. The arguments are the same as for
1908 debug_make_method_variant, except that the last two are omitted
1909 since a static method can not also be virtual. */
1910
1911 debug_method_variant
1912 debug_make_static_method_variant (handle, physname, type, visibility,
1913 constp, volatilep)
1914 PTR handle;
1915 const char *physname;
1916 debug_type type;
1917 enum debug_visibility visibility;
1918 boolean constp;
1919 boolean volatilep;
1920 {
1921 struct debug_method_variant *m;
1922
1923 m = (struct debug_method_variant *) xmalloc (sizeof *m);
1924 memset (m, 0, sizeof *m);
1925
1926 m->physname = physname;
1927 m->type = type;
1928 m->visibility = visibility;
1929 m->constp = constp;
1930 m->volatilep = volatilep;
1931 m->voffset = VOFFSET_STATIC_METHOD;
1932
1933 return m;
1934 }
1935
1936 /* Name a type. */
1937
1938 debug_type
1939 debug_name_type (handle, name, type)
1940 PTR handle;
1941 const char *name;
1942 debug_type type;
1943 {
1944 struct debug_handle *info = (struct debug_handle *) handle;
1945 struct debug_type *t;
1946 struct debug_named_type *n;
1947 struct debug_name *nm;
1948
1949 if (name == NULL || type == NULL)
1950 return DEBUG_TYPE_NULL;
1951
1952 if (info->current_unit == NULL
1953 || info->current_file == NULL)
1954 {
1955 debug_error ("debug_record_variable: no current file");
1956 return false;
1957 }
1958
1959 t = debug_make_type (info, DEBUG_KIND_NAMED, 0);
1960 if (t == NULL)
1961 return DEBUG_TYPE_NULL;
1962
1963 n = (struct debug_named_type *) xmalloc (sizeof *n);
1964 memset (n, 0, sizeof *n);
1965
1966 n->type = type;
1967
1968 t->u.knamed = n;
1969
1970 /* We always add the name to the global namespace. This is probably
1971 wrong in some cases, but it seems to be right for stabs. FIXME. */
1972
1973 nm = debug_add_to_namespace (info, &info->current_file->globals, name,
1974 DEBUG_OBJECT_TYPE, DEBUG_LINKAGE_NONE);
1975 if (nm == NULL)
1976 return false;
1977
1978 nm->u.type = t;
1979
1980 n->name = nm;
1981
1982 return t;
1983 }
1984
1985 /* Tag a type. */
1986
1987 debug_type
1988 debug_tag_type (handle, name, type)
1989 PTR handle;
1990 const char *name;
1991 debug_type type;
1992 {
1993 struct debug_handle *info = (struct debug_handle *) handle;
1994 struct debug_type *t;
1995 struct debug_named_type *n;
1996 struct debug_name *nm;
1997
1998 if (name == NULL || type == NULL)
1999 return DEBUG_TYPE_NULL;
2000
2001 if (info->current_file == NULL)
2002 {
2003 debug_error ("debug_tag_type: no current file");
2004 return DEBUG_TYPE_NULL;
2005 }
2006
2007 if (type->kind == DEBUG_KIND_TAGGED)
2008 {
2009 if (strcmp (type->u.knamed->name->name, name) == 0)
2010 return type;
2011 debug_error ("debug_tag_type: extra tag attempted");
2012 return DEBUG_TYPE_NULL;
2013 }
2014
2015 t = debug_make_type (info, DEBUG_KIND_TAGGED, 0);
2016 if (t == NULL)
2017 return DEBUG_TYPE_NULL;
2018
2019 n = (struct debug_named_type *) xmalloc (sizeof *n);
2020 memset (n, 0, sizeof *n);
2021
2022 n->type = type;
2023
2024 t->u.knamed = n;
2025
2026 /* We keep a global namespace of tags for each compilation unit. I
2027 don't know if that is the right thing to do. */
2028
2029 nm = debug_add_to_namespace (info, &info->current_file->globals, name,
2030 DEBUG_OBJECT_TAG, DEBUG_LINKAGE_NONE);
2031 if (nm == NULL)
2032 return false;
2033
2034 nm->u.tag = t;
2035
2036 n->name = nm;
2037
2038 return t;
2039 }
2040
2041 /* Record the size of a given type. */
2042
2043 /*ARGSUSED*/
2044 boolean
2045 debug_record_type_size (handle, type, size)
2046 PTR handle;
2047 debug_type type;
2048 unsigned int size;
2049 {
2050 if (type->size != 0 && type->size != size)
2051 fprintf (stderr, "Warning: changing type size from %d to %d\n",
2052 type->size, size);
2053
2054 type->size = size;
2055
2056 return true;
2057 }
2058
2059 /* Find a named type. */
2060
2061 debug_type
2062 debug_find_named_type (handle, name)
2063 PTR handle;
2064 const char *name;
2065 {
2066 struct debug_handle *info = (struct debug_handle *) handle;
2067 struct debug_block *b;
2068 struct debug_file *f;
2069
2070 /* We only search the current compilation unit. I don't know if
2071 this is right or not. */
2072
2073 if (info->current_unit == NULL)
2074 {
2075 debug_error ("debug_find_named_type: no current compilation unit");
2076 return DEBUG_TYPE_NULL;
2077 }
2078
2079 for (b = info->current_block; b != NULL; b = b->parent)
2080 {
2081 if (b->locals != NULL)
2082 {
2083 struct debug_name *n;
2084
2085 for (n = b->locals->list; n != NULL; n = n->next)
2086 {
2087 if (n->kind == DEBUG_OBJECT_TYPE
2088 && n->name[0] == name[0]
2089 && strcmp (n->name, name) == 0)
2090 return n->u.type;
2091 }
2092 }
2093 }
2094
2095 for (f = info->current_unit->files; f != NULL; f = f->next)
2096 {
2097 if (f->globals != NULL)
2098 {
2099 struct debug_name *n;
2100
2101 for (n = f->globals->list; n != NULL; n = n->next)
2102 {
2103 if (n->kind == DEBUG_OBJECT_TYPE
2104 && n->name[0] == name[0]
2105 && strcmp (n->name, name) == 0)
2106 return n->u.type;
2107 }
2108 }
2109 }
2110
2111 return DEBUG_TYPE_NULL;
2112 }
2113
2114 /* Find a tagged type. */
2115
2116 debug_type
2117 debug_find_tagged_type (handle, name, kind)
2118 PTR handle;
2119 const char *name;
2120 enum debug_type_kind kind;
2121 {
2122 struct debug_handle *info = (struct debug_handle *) handle;
2123 struct debug_unit *u;
2124
2125 /* We search the globals of all the compilation units. I don't know
2126 if this is correct or not. It would be easy to change. */
2127
2128 for (u = info->units; u != NULL; u = u->next)
2129 {
2130 struct debug_file *f;
2131
2132 for (f = u->files; f != NULL; f = f->next)
2133 {
2134 struct debug_name *n;
2135
2136 if (f->globals != NULL)
2137 {
2138 for (n = f->globals->list; n != NULL; n = n->next)
2139 {
2140 if (n->kind == DEBUG_OBJECT_TAG
2141 && (kind == DEBUG_KIND_ILLEGAL
2142 || n->u.tag->kind == kind)
2143 && n->name[0] == name[0]
2144 && strcmp (n->name, name) == 0)
2145 return n->u.tag;
2146 }
2147 }
2148 }
2149 }
2150
2151 return DEBUG_TYPE_NULL;
2152 }
2153
2154 /* Get a base type. */
2155
2156 static struct debug_type *
2157 debug_get_real_type (handle, type)
2158 PTR handle;
2159 debug_type type;
2160 {
2161 switch (type->kind)
2162 {
2163 default:
2164 return type;
2165 case DEBUG_KIND_INDIRECT:
2166 if (*type->u.kindirect->slot != NULL)
2167 return debug_get_real_type (handle, *type->u.kindirect->slot);
2168 return type;
2169 case DEBUG_KIND_NAMED:
2170 return debug_get_real_type (handle, type->u.knamed->type);
2171 }
2172 /*NOTREACHED*/
2173 }
2174
2175 /* Get the kind of a type. */
2176
2177 enum debug_type_kind
2178 debug_get_type_kind (handle, type)
2179 PTR handle;
2180 debug_type type;
2181 {
2182 if (type == NULL)
2183 return DEBUG_KIND_ILLEGAL;
2184 type = debug_get_real_type (handle, type);
2185 return type->kind;
2186 }
2187
2188 /* Get the name of a type. */
2189
2190 const char *
2191 debug_get_type_name (handle, type)
2192 PTR handle;
2193 debug_type type;
2194 {
2195 if (type->kind == DEBUG_KIND_INDIRECT)
2196 {
2197 if (*type->u.kindirect->slot != NULL)
2198 return debug_get_type_name (handle, *type->u.kindirect->slot);
2199 return type->u.kindirect->tag;
2200 }
2201 if (type->kind == DEBUG_KIND_NAMED
2202 || type->kind == DEBUG_KIND_TAGGED)
2203 return type->u.knamed->name->name;
2204 return NULL;
2205 }
2206
2207 /* Get the return type of a function or method type. */
2208
2209 debug_type
2210 debug_get_return_type (handle, type)
2211 PTR handle;
2212 debug_type type;
2213 {
2214 if (type == NULL)
2215 return DEBUG_TYPE_NULL;
2216 type = debug_get_real_type (handle, type);
2217 switch (type->kind)
2218 {
2219 default:
2220 return DEBUG_TYPE_NULL;
2221 case DEBUG_KIND_FUNCTION:
2222 return type->u.kfunction->return_type;
2223 case DEBUG_KIND_METHOD:
2224 return type->u.kmethod->return_type;
2225 }
2226 /*NOTREACHED*/
2227 }
2228
2229 /* Get the parameter types of a function or method type (except that
2230 we don't currently store the parameter types of a function). */
2231
2232 const debug_type *
2233 debug_get_parameter_types (handle, type)
2234 PTR handle;
2235 debug_type type;
2236 {
2237 if (type == NULL)
2238 return NULL;
2239 type = debug_get_real_type (handle, type);
2240 switch (type->kind)
2241 {
2242 default:
2243 return NULL;
2244 case DEBUG_KIND_METHOD:
2245 return type->u.kmethod->arg_types;
2246 }
2247 /*NOTREACHED*/
2248 }
2249
2250 /* Get the NULL terminated array of fields for a struct, union, or
2251 class. */
2252
2253 const debug_field *
2254 debug_get_fields (handle, type)
2255 PTR handle;
2256 debug_type type;
2257 {
2258 if (type == NULL)
2259 return NULL;
2260 type = debug_get_real_type (handle, type);
2261 switch (type->kind)
2262 {
2263 default:
2264 return NULL;
2265 case DEBUG_KIND_STRUCT:
2266 case DEBUG_KIND_UNION:
2267 case DEBUG_KIND_CLASS:
2268 case DEBUG_KIND_UNION_CLASS:
2269 return type->u.kclass->fields;
2270 }
2271 /*NOTREACHED*/
2272 }
2273
2274 /* Get the type of a field. */
2275
2276 /*ARGSUSED*/
2277 debug_type
2278 debug_get_field_type (handle, field)
2279 PTR handle;
2280 debug_field field;
2281 {
2282 if (field == NULL)
2283 return NULL;
2284 return field->type;
2285 }
2286 \f
2287 /* Write out the debugging information. This is given a handle to
2288 debugging information, and a set of function pointers to call. */
2289
2290 boolean
2291 debug_write (handle, fns, fhandle)
2292 PTR handle;
2293 const struct debug_write_fns *fns;
2294 PTR fhandle;
2295 {
2296 struct debug_handle *info = (struct debug_handle *) handle;
2297 struct debug_unit *u;
2298
2299 /* We use a mark to tell whether we have already written out a
2300 particular name. We use an integer, so that we don't have to
2301 clear the mark fields if we happen to write out the same
2302 information more than once. */
2303 ++info->mark;
2304
2305 /* The base_id field holds an ID value which will never be used, so
2306 that we can tell whether we have assigned an ID during this call
2307 to debug_write. */
2308 info->base_id = info->class_id;
2309
2310 for (u = info->units; u != NULL; u = u->next)
2311 {
2312 struct debug_file *f;
2313 boolean first_file;
2314 struct debug_lineno *l;
2315
2316 if (! (*fns->start_compilation_unit) (fhandle, u->files->filename))
2317 return false;
2318
2319 first_file = true;
2320 for (f = u->files; f != NULL; f = f->next)
2321 {
2322 struct debug_name *n;
2323
2324 if (first_file)
2325 first_file = false;
2326 else
2327 {
2328 if (! (*fns->start_source) (fhandle, f->filename))
2329 return false;
2330 }
2331
2332 if (f->globals != NULL)
2333 {
2334 for (n = f->globals->list; n != NULL; n = n->next)
2335 {
2336 if (! debug_write_name (info, fns, fhandle, n))
2337 return false;
2338 }
2339 }
2340 }
2341
2342 for (l = u->linenos; l != NULL; l = l->next)
2343 {
2344 unsigned int i;
2345
2346 for (i = 0; i < DEBUG_LINENO_COUNT; i++)
2347 {
2348 if (l->linenos[i] == (unsigned long) -1)
2349 break;
2350 if (! (*fns->lineno) (fhandle, l->file->filename, l->linenos[i],
2351 l->addrs[i]))
2352 return false;
2353 }
2354 }
2355 }
2356
2357 return true;
2358 }
2359
2360 /* Write out an element in a namespace. */
2361
2362 static boolean
2363 debug_write_name (info, fns, fhandle, n)
2364 struct debug_handle *info;
2365 const struct debug_write_fns *fns;
2366 PTR fhandle;
2367 struct debug_name *n;
2368 {
2369 /* The class_mark field is used to prevent recursively outputting a
2370 struct or class. */
2371 ++info->class_mark;
2372
2373 switch (n->kind)
2374 {
2375 case DEBUG_OBJECT_TYPE:
2376 if (! debug_write_type (info, fns, fhandle, n->u.type, n)
2377 || ! (*fns->typdef) (fhandle, n->name))
2378 return false;
2379 return true;
2380 case DEBUG_OBJECT_TAG:
2381 if (! debug_write_type (info, fns, fhandle, n->u.tag, n))
2382 return false;
2383 return (*fns->tag) (fhandle, n->name);
2384 case DEBUG_OBJECT_VARIABLE:
2385 if (! debug_write_type (info, fns, fhandle, n->u.variable->type,
2386 (struct debug_name *) NULL))
2387 return false;
2388 return (*fns->variable) (fhandle, n->name, n->u.variable->kind,
2389 n->u.variable->val);
2390 case DEBUG_OBJECT_FUNCTION:
2391 return debug_write_function (info, fns, fhandle, n->name,
2392 n->linkage, n->u.function);
2393 case DEBUG_OBJECT_INT_CONSTANT:
2394 return (*fns->int_constant) (fhandle, n->name, n->u.int_constant);
2395 case DEBUG_OBJECT_FLOAT_CONSTANT:
2396 return (*fns->float_constant) (fhandle, n->name, n->u.float_constant);
2397 case DEBUG_OBJECT_TYPED_CONSTANT:
2398 if (! debug_write_type (info, fns, fhandle, n->u.typed_constant->type,
2399 (struct debug_name *) NULL))
2400 return false;
2401 return (*fns->typed_constant) (fhandle, n->name,
2402 n->u.typed_constant->val);
2403 default:
2404 abort ();
2405 return false;
2406 }
2407 /*NOTREACHED*/
2408 }
2409
2410 /* Write out a type. If the type is DEBUG_KIND_NAMED or
2411 DEBUG_KIND_TAGGED, then the name argument is the name for which we
2412 are about to call typedef or tag. If the type is anything else,
2413 then the name argument is a tag from a DEBUG_KIND_TAGGED type which
2414 points to this one. */
2415
2416 static boolean
2417 debug_write_type (info, fns, fhandle, type, name)
2418 struct debug_handle *info;
2419 const struct debug_write_fns *fns;
2420 PTR fhandle;
2421 struct debug_type *type;
2422 struct debug_name *name;
2423 {
2424 unsigned int i;
2425 const char *tag;
2426
2427 /* If we have a name for this type, just output it. We only output
2428 typedef names after they have been defined. We output type tags
2429 whenever we are not actually defining them. */
2430 if ((type->kind == DEBUG_KIND_NAMED
2431 || type->kind == DEBUG_KIND_TAGGED)
2432 && (type->u.knamed->name->mark == info->mark
2433 || (type->kind == DEBUG_KIND_TAGGED
2434 && type->u.knamed->name != name)))
2435 {
2436 if (type->kind == DEBUG_KIND_NAMED)
2437 return (*fns->typedef_type) (fhandle, type->u.knamed->name->name);
2438 else
2439 return (*fns->tag_type) (fhandle, type->u.knamed->name->name, 0,
2440 type->u.knamed->type->kind);
2441 }
2442
2443 /* Mark the name after we have already looked for a known name, so
2444 that we don't just define a type in terms of itself. We need to
2445 mark the name here so that a struct containing a pointer to
2446 itself will work. */
2447 if (name != NULL)
2448 name->mark = info->mark;
2449
2450 tag = NULL;
2451 if (name != NULL
2452 && type->kind != DEBUG_KIND_NAMED
2453 && type->kind != DEBUG_KIND_TAGGED)
2454 {
2455 assert (name->kind == DEBUG_OBJECT_TAG);
2456 tag = name->name;
2457 }
2458
2459 switch (type->kind)
2460 {
2461 case DEBUG_KIND_ILLEGAL:
2462 debug_error ("debug_write_type: illegal type encountered");
2463 return false;
2464 case DEBUG_KIND_INDIRECT:
2465 if (*type->u.kindirect->slot == DEBUG_TYPE_NULL)
2466 return (*fns->empty_type) (fhandle);
2467 return debug_write_type (info, fns, fhandle, *type->u.kindirect->slot,
2468 (struct debug_name *) NULL);
2469 case DEBUG_KIND_VOID:
2470 return (*fns->void_type) (fhandle);
2471 case DEBUG_KIND_INT:
2472 return (*fns->int_type) (fhandle, type->size, type->u.kint);
2473 case DEBUG_KIND_FLOAT:
2474 return (*fns->float_type) (fhandle, type->size);
2475 case DEBUG_KIND_COMPLEX:
2476 return (*fns->complex_type) (fhandle, type->size);
2477 case DEBUG_KIND_BOOL:
2478 return (*fns->bool_type) (fhandle, type->size);
2479 case DEBUG_KIND_STRUCT:
2480 case DEBUG_KIND_UNION:
2481 if (type->u.kclass != NULL)
2482 {
2483 if (info->class_mark == type->u.kclass->mark
2484 || type->u.kclass->id > info->base_id)
2485 {
2486 /* We are currently outputting this struct, or we have
2487 already output it. I don't know if this can happen,
2488 but it can happen for a class. */
2489 assert (type->u.kclass->id > info->base_id);
2490 return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2491 type->kind);
2492 }
2493 type->u.kclass->mark = info->class_mark;
2494 ++info->class_id;
2495 type->u.kclass->id = info->class_id;
2496 }
2497
2498 if (! (*fns->start_struct_type) (fhandle, tag,
2499 (type->u.kclass != NULL
2500 ? type->u.kclass->id
2501 : 0),
2502 type->kind == DEBUG_KIND_STRUCT,
2503 type->size))
2504 return false;
2505 if (type->u.kclass != NULL
2506 && type->u.kclass->fields != NULL)
2507 {
2508 for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2509 {
2510 struct debug_field *f;
2511
2512 f = type->u.kclass->fields[i];
2513 if (! debug_write_type (info, fns, fhandle, f->type,
2514 (struct debug_name *) NULL)
2515 || ! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2516 f->u.f.bitsize, f->visibility))
2517 return false;
2518 }
2519 }
2520 return (*fns->end_struct_type) (fhandle);
2521 case DEBUG_KIND_CLASS:
2522 case DEBUG_KIND_UNION_CLASS:
2523 return debug_write_class_type (info, fns, fhandle, type, tag);
2524 case DEBUG_KIND_ENUM:
2525 if (type->u.kenum == NULL)
2526 return (*fns->enum_type) (fhandle, tag, (const char **) NULL,
2527 (bfd_signed_vma *) NULL);
2528 return (*fns->enum_type) (fhandle, tag, type->u.kenum->names,
2529 type->u.kenum->values);
2530 case DEBUG_KIND_POINTER:
2531 if (! debug_write_type (info, fns, fhandle, type->u.kpointer,
2532 (struct debug_name *) NULL))
2533 return false;
2534 return (*fns->pointer_type) (fhandle);
2535 case DEBUG_KIND_FUNCTION:
2536 if (! debug_write_type (info, fns, fhandle,
2537 type->u.kfunction->return_type,
2538 (struct debug_name *) NULL))
2539 return false;
2540 return (*fns->function_type) (fhandle);
2541 case DEBUG_KIND_REFERENCE:
2542 if (! debug_write_type (info, fns, fhandle, type->u.kreference,
2543 (struct debug_name *) NULL))
2544 return false;
2545 return (*fns->reference_type) (fhandle);
2546 case DEBUG_KIND_RANGE:
2547 if (! debug_write_type (info, fns, fhandle, type->u.krange->type,
2548 (struct debug_name *) NULL))
2549 return false;
2550 return (*fns->range_type) (fhandle, type->u.krange->lower,
2551 type->u.krange->upper);
2552 case DEBUG_KIND_ARRAY:
2553 if (! debug_write_type (info, fns, fhandle, type->u.karray->element_type,
2554 (struct debug_name *) NULL)
2555 || ! debug_write_type (info, fns, fhandle,
2556 type->u.karray->range_type,
2557 (struct debug_name *) NULL))
2558 return false;
2559 return (*fns->array_type) (fhandle, type->u.karray->lower,
2560 type->u.karray->upper,
2561 type->u.karray->stringp);
2562 case DEBUG_KIND_SET:
2563 if (! debug_write_type (info, fns, fhandle, type->u.kset->type,
2564 (struct debug_name *) NULL))
2565 return false;
2566 return (*fns->set_type) (fhandle, type->u.kset->bitstringp);
2567 case DEBUG_KIND_OFFSET:
2568 if (! debug_write_type (info, fns, fhandle, type->u.koffset->base_type,
2569 (struct debug_name *) NULL)
2570 || ! debug_write_type (info, fns, fhandle,
2571 type->u.koffset->target_type,
2572 (struct debug_name *) NULL))
2573 return false;
2574 return (*fns->offset_type) (fhandle);
2575 case DEBUG_KIND_METHOD:
2576 if (! debug_write_type (info, fns, fhandle,
2577 type->u.kmethod->return_type,
2578 (struct debug_name *) NULL))
2579 return false;
2580 if (type->u.kmethod->arg_types == NULL)
2581 i = -1;
2582 else
2583 {
2584 for (i = 0; type->u.kmethod->arg_types[i] != NULL; i++)
2585 {
2586 if (ELLIPSIS_P (type->u.kmethod->arg_types[i]))
2587 {
2588 if (! (*fns->ellipsis_type) (fhandle))
2589 return false;
2590 }
2591 else
2592 {
2593 if (! debug_write_type (info, fns, fhandle,
2594 type->u.kmethod->arg_types[i],
2595 (struct debug_name *) NULL))
2596 return false;
2597 }
2598 }
2599 }
2600 if (type->u.kmethod->domain_type != NULL)
2601 {
2602 if (! debug_write_type (info, fns, fhandle,
2603 type->u.kmethod->domain_type,
2604 (struct debug_name *) NULL))
2605 return false;
2606 }
2607 return (*fns->method_type) (fhandle,
2608 type->u.kmethod->domain_type != NULL,
2609 i);
2610 case DEBUG_KIND_CONST:
2611 if (! debug_write_type (info, fns, fhandle, type->u.kconst,
2612 (struct debug_name *) NULL))
2613 return false;
2614 return (*fns->const_type) (fhandle);
2615 case DEBUG_KIND_VOLATILE:
2616 if (! debug_write_type (info, fns, fhandle, type->u.kvolatile,
2617 (struct debug_name *) NULL))
2618 return false;
2619 return (*fns->volatile_type) (fhandle);
2620 case DEBUG_KIND_NAMED:
2621 return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2622 (struct debug_name *) NULL);
2623 case DEBUG_KIND_TAGGED:
2624 return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2625 type->u.knamed->name);
2626 default:
2627 abort ();
2628 return false;
2629 }
2630 }
2631
2632 /* Write out a class type. */
2633
2634 static boolean
2635 debug_write_class_type (info, fns, fhandle, type, tag)
2636 struct debug_handle *info;
2637 const struct debug_write_fns *fns;
2638 PTR fhandle;
2639 struct debug_type *type;
2640 const char *tag;
2641 {
2642 unsigned int i;
2643 unsigned int id;
2644 struct debug_type *vptrbase;
2645
2646 if (type->u.kclass == NULL)
2647 {
2648 id = 0;
2649 vptrbase = NULL;
2650 }
2651 else
2652 {
2653 if (info->class_mark == type->u.kclass->mark
2654 || type->u.kclass->id > info->base_id)
2655 {
2656 /* We are currently outputting this class, or we have
2657 already output it. This can happen when there are
2658 methods for an anonymous class. */
2659 assert (type->u.kclass->id > info->base_id);
2660 return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2661 type->kind);
2662 }
2663 type->u.kclass->mark = info->class_mark;
2664 ++info->class_id;
2665 id = info->class_id;
2666 type->u.kclass->id = id;
2667
2668 vptrbase = type->u.kclass->vptrbase;
2669 if (vptrbase != NULL && vptrbase != type)
2670 {
2671 if (! debug_write_type (info, fns, fhandle, vptrbase,
2672 (struct debug_name *) NULL))
2673 return false;
2674 }
2675 }
2676
2677 if (! (*fns->start_class_type) (fhandle, tag, id,
2678 type->kind == DEBUG_KIND_CLASS,
2679 type->size,
2680 vptrbase != NULL,
2681 vptrbase == type))
2682 return false;
2683
2684 if (type->u.kclass != NULL)
2685 {
2686 if (type->u.kclass->fields != NULL)
2687 {
2688 for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2689 {
2690 struct debug_field *f;
2691
2692 f = type->u.kclass->fields[i];
2693 if (! debug_write_type (info, fns, fhandle, f->type,
2694 (struct debug_name *) NULL))
2695 return false;
2696 if (f->static_member)
2697 {
2698 if (! (*fns->class_static_member) (fhandle, f->name,
2699 f->u.s.physname,
2700 f->visibility))
2701 return false;
2702 }
2703 else
2704 {
2705 if (! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2706 f->u.f.bitsize, f->visibility))
2707 return false;
2708 }
2709 }
2710 }
2711
2712 if (type->u.kclass->baseclasses != NULL)
2713 {
2714 for (i = 0; type->u.kclass->baseclasses[i] != NULL; i++)
2715 {
2716 struct debug_baseclass *b;
2717
2718 b = type->u.kclass->baseclasses[i];
2719 if (! debug_write_type (info, fns, fhandle, b->type,
2720 (struct debug_name *) NULL))
2721 return false;
2722 if (! (*fns->class_baseclass) (fhandle, b->bitpos, b->virtual,
2723 b->visibility))
2724 return false;
2725 }
2726 }
2727
2728 if (type->u.kclass->methods != NULL)
2729 {
2730 for (i = 0; type->u.kclass->methods[i] != NULL; i++)
2731 {
2732 struct debug_method *m;
2733 unsigned int j;
2734
2735 m = type->u.kclass->methods[i];
2736 if (! (*fns->class_start_method) (fhandle, m->name))
2737 return false;
2738 for (j = 0; m->variants[j] != NULL; j++)
2739 {
2740 struct debug_method_variant *v;
2741
2742 v = m->variants[j];
2743 if (v->context != NULL)
2744 {
2745 if (! debug_write_type (info, fns, fhandle, v->context,
2746 (struct debug_name *) NULL))
2747 return false;
2748 }
2749 if (! debug_write_type (info, fns, fhandle, v->type,
2750 (struct debug_name *) NULL))
2751 return false;
2752 if (v->voffset != VOFFSET_STATIC_METHOD)
2753 {
2754 if (! (*fns->class_method_variant) (fhandle, v->physname,
2755 v->visibility,
2756 v->constp,
2757 v->volatilep,
2758 v->voffset,
2759 v->context != NULL))
2760 return false;
2761 }
2762 else
2763 {
2764 if (! (*fns->class_static_method_variant) (fhandle,
2765 v->physname,
2766 v->visibility,
2767 v->constp,
2768 v->volatilep))
2769 return false;
2770 }
2771 }
2772 if (! (*fns->class_end_method) (fhandle))
2773 return false;
2774 }
2775 }
2776 }
2777
2778 return (*fns->end_class_type) (fhandle);
2779 }
2780
2781 /* Write out information for a function. */
2782
2783 static boolean
2784 debug_write_function (info, fns, fhandle, name, linkage, function)
2785 struct debug_handle *info;
2786 const struct debug_write_fns *fns;
2787 PTR fhandle;
2788 const char *name;
2789 enum debug_object_linkage linkage;
2790 struct debug_function *function;
2791 {
2792 struct debug_parameter *p;
2793 struct debug_block *b;
2794
2795 if (! debug_write_type (info, fns, fhandle, function->return_type,
2796 (struct debug_name *) NULL))
2797 return false;
2798
2799 if (! (*fns->start_function) (fhandle, name,
2800 linkage == DEBUG_LINKAGE_GLOBAL))
2801 return false;
2802
2803 for (p = function->parameters; p != NULL; p = p->next)
2804 {
2805 if (! debug_write_type (info, fns, fhandle, p->type,
2806 (struct debug_name *) NULL)
2807 || ! (*fns->function_parameter) (fhandle, p->name, p->kind, p->val))
2808 return false;
2809 }
2810
2811 for (b = function->blocks; b != NULL; b = b->next)
2812 {
2813 if (! debug_write_block (info, fns, fhandle, b))
2814 return false;
2815 }
2816
2817 return (*fns->end_function) (fhandle);
2818 }
2819
2820 /* Write out information for a block. */
2821
2822 static boolean
2823 debug_write_block (info, fns, fhandle, block)
2824 struct debug_handle *info;
2825 const struct debug_write_fns *fns;
2826 PTR fhandle;
2827 struct debug_block *block;
2828 {
2829 struct debug_name *n;
2830 struct debug_block *b;
2831
2832 if (! (*fns->start_block) (fhandle, block->start))
2833 return false;
2834
2835 if (block->locals != NULL)
2836 {
2837 for (n = block->locals->list; n != NULL; n = n->next)
2838 {
2839 if (! debug_write_name (info, fns, fhandle, n))
2840 return false;
2841 }
2842 }
2843
2844 for (b = block->children; b != NULL; b = b->next)
2845 {
2846 if (! debug_write_block (info, fns, fhandle, b))
2847 return false;
2848 }
2849
2850 return (*fns->end_block) (fhandle, block->end);
2851 }