stabs.c revision 33965
1/* stabs.c -- Parse stabs 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 contains code which parses stabs debugging information.
23   The organization of this code is based on the gdb stabs reading
24   code.  The job it does is somewhat different, because it is not
25   trying to identify the correct address for anything.  */
26
27#include <stdio.h>
28#include <ctype.h>
29
30#include "bfd.h"
31#include "bucomm.h"
32#include "libiberty.h"
33#include "demangle.h"
34#include "debug.h"
35#include "budbg.h"
36
37/* Meaningless definition needs by aout64.h.  FIXME.  */
38#define BYTES_IN_WORD 4
39
40#include "aout/aout64.h"
41#include "aout/stab_gnu.h"
42
43/* The number of predefined XCOFF types.  */
44
45#define XCOFF_TYPE_COUNT 34
46
47/* This structure is used as a handle so that the stab parsing doesn't
48   need to use any static variables.  */
49
50struct stab_handle
51{
52  /* The BFD.  */
53  bfd *abfd;
54  /* True if this is stabs in sections.  */
55  boolean sections;
56  /* The symbol table.  */
57  asymbol **syms;
58  /* The number of symbols.  */
59  long symcount;
60  /* The accumulated file name string.  */
61  char *so_string;
62  /* The value of the last N_SO symbol.  */
63  bfd_vma so_value;
64  /* The value of the start of the file, so that we can handle file
65     relative N_LBRAC and N_RBRAC symbols.  */
66  bfd_vma file_start_offset;
67  /* The offset of the start of the function, so that we can handle
68     function relative N_LBRAC and N_RBRAC symbols.  */
69  bfd_vma function_start_offset;
70  /* The version number of gcc which compiled the current compilation
71     unit, 0 if not compiled by gcc.  */
72  int gcc_compiled;
73  /* Whether an N_OPT symbol was seen that was not generated by gcc,
74     so that we can detect the SunPRO compiler.  */
75  boolean n_opt_found;
76  /* The main file name.  */
77  char *main_filename;
78  /* A stack of unfinished N_BINCL files.  */
79  struct bincl_file *bincl_stack;
80  /* A list of finished N_BINCL files.  */
81  struct bincl_file *bincl_list;
82  /* Whether we are inside a function or not.  */
83  boolean within_function;
84  /* The address of the end of the function, used if we have seen an
85     N_FUN symbol while in a function.  This is -1 if we have not seen
86     an N_FUN (the normal case).  */
87  bfd_vma function_end;
88  /* The depth of block nesting.  */
89  int block_depth;
90  /* List of pending variable definitions.  */
91  struct stab_pending_var *pending;
92  /* Number of files for which we have types.  */
93  unsigned int files;
94  /* Lists of types per file.  */
95  struct stab_types **file_types;
96  /* Predefined XCOFF types.  */
97  debug_type xcoff_types[XCOFF_TYPE_COUNT];
98  /* Undefined tags.  */
99  struct stab_tag *tags;
100};
101
102/* A list of these structures is used to hold pending variable
103   definitions seen before the N_LBRAC of a block.  */
104
105struct stab_pending_var
106{
107  /* Next pending variable definition.  */
108  struct stab_pending_var *next;
109  /* Name.  */
110  const char *name;
111  /* Type.  */
112  debug_type type;
113  /* Kind.  */
114  enum debug_var_kind kind;
115  /* Value.  */
116  bfd_vma val;
117};
118
119/* A list of these structures is used to hold the types for a single
120   file.  */
121
122struct stab_types
123{
124  /* Next set of slots for this file.  */
125  struct stab_types *next;
126  /* Types indexed by type number.  */
127#define STAB_TYPES_SLOTS (16)
128  debug_type types[STAB_TYPES_SLOTS];
129};
130
131/* We keep a list of undefined tags that we encounter, so that we can
132   fill them in if the tag is later defined.  */
133
134struct stab_tag
135{
136  /* Next undefined tag.  */
137  struct stab_tag *next;
138  /* Tag name.  */
139  const char *name;
140  /* Type kind.  */
141  enum debug_type_kind kind;
142  /* Slot to hold real type when we discover it.  If we don't, we fill
143     in an undefined tag type.  */
144  debug_type slot;
145  /* Indirect type we have created to point at slot.  */
146  debug_type type;
147};
148
149static char *savestring PARAMS ((const char *, int));
150static bfd_vma parse_number PARAMS ((const char **, boolean *));
151static void bad_stab PARAMS ((const char *));
152static void warn_stab PARAMS ((const char *, const char *));
153static boolean parse_stab_string
154  PARAMS ((PTR, struct stab_handle *, int, int, bfd_vma, const char *));
155static debug_type parse_stab_type
156  PARAMS ((PTR, struct stab_handle *, const char *, const char **,
157	   debug_type **));
158static boolean parse_stab_type_number
159  PARAMS ((const char **, int *));
160static debug_type parse_stab_range_type
161  PARAMS ((PTR, struct stab_handle *, const char *, const char **,
162	   const int *));
163static debug_type parse_stab_sun_builtin_type PARAMS ((PTR, const char **));
164static debug_type parse_stab_sun_floating_type
165  PARAMS ((PTR, const char **));
166static debug_type parse_stab_enum_type PARAMS ((PTR, const char **));
167static debug_type parse_stab_struct_type
168  PARAMS ((PTR, struct stab_handle *, const char *, const char **, boolean,
169	   const int *));
170static boolean parse_stab_baseclasses
171  PARAMS ((PTR, struct stab_handle *, const char **, debug_baseclass **));
172static boolean parse_stab_struct_fields
173  PARAMS ((PTR, struct stab_handle *, const char **, debug_field **,
174	   boolean *));
175static boolean parse_stab_cpp_abbrev
176  PARAMS ((PTR, struct stab_handle *, const char **, debug_field *));
177static boolean parse_stab_one_struct_field
178  PARAMS ((PTR, struct stab_handle *, const char **, const char *,
179	   debug_field *, boolean *));
180static boolean parse_stab_members
181  PARAMS ((PTR, struct stab_handle *, const char *, const char **,
182	   const int *, debug_method **));
183static debug_type parse_stab_argtypes
184  PARAMS ((PTR, struct stab_handle *, debug_type, const char *, const char *,
185	   debug_type, const char *, boolean, boolean, const char **));
186static boolean parse_stab_tilde_field
187  PARAMS ((PTR, struct stab_handle *, const char **, const int *,
188	   debug_type *, boolean *));
189static debug_type parse_stab_array_type
190  PARAMS ((PTR, struct stab_handle *, const char **, boolean));
191static void push_bincl PARAMS ((struct stab_handle *, const char *, bfd_vma));
192static const char *pop_bincl PARAMS ((struct stab_handle *));
193static boolean find_excl
194  PARAMS ((struct stab_handle *, const char *, bfd_vma));
195static boolean stab_record_variable
196  PARAMS ((PTR, struct stab_handle *, const char *, debug_type,
197	   enum debug_var_kind, bfd_vma));
198static boolean stab_emit_pending_vars PARAMS ((PTR, struct stab_handle *));
199static debug_type *stab_find_slot
200  PARAMS ((struct stab_handle *, const int *));
201static debug_type stab_find_type
202  PARAMS ((PTR, struct stab_handle *, const int *));
203static boolean stab_record_type
204  PARAMS ((PTR, struct stab_handle *, const int *, debug_type));
205static debug_type stab_xcoff_builtin_type
206  PARAMS ((PTR, struct stab_handle *, int));
207static debug_type stab_find_tagged_type
208  PARAMS ((PTR, struct stab_handle *, const char *, int,
209	   enum debug_type_kind));
210static debug_type *stab_demangle_argtypes
211  PARAMS ((PTR, struct stab_handle *, const char *, boolean *));
212
213/* Save a string in memory.  */
214
215static char *
216savestring (start, len)
217     const char *start;
218     int len;
219{
220  char *ret;
221
222  ret = (char *) xmalloc (len + 1);
223  memcpy (ret, start, len);
224  ret[len] = '\0';
225  return ret;
226}
227
228/* Read a number from a string.  */
229
230static bfd_vma
231parse_number (pp, poverflow)
232     const char **pp;
233     boolean *poverflow;
234{
235  unsigned long ul;
236  const char *orig;
237
238  if (poverflow != NULL)
239    *poverflow = false;
240
241  orig = *pp;
242
243  errno = 0;
244  ul = strtoul (*pp, (char **) pp, 0);
245  if (ul + 1 != 0 || errno == 0)
246    return (bfd_vma) ul;
247
248  /* Note that even though strtoul overflowed, it should have set *pp
249     to the end of the number, which is where we want it.  */
250
251  if (sizeof (bfd_vma) > sizeof (unsigned long))
252    {
253      const char *p;
254      boolean neg;
255      int base;
256      bfd_vma over, lastdig;
257      boolean overflow;
258      bfd_vma v;
259
260      /* Our own version of strtoul, for a bfd_vma.  */
261
262      p = orig;
263
264      neg = false;
265      if (*p == '+')
266	++p;
267      else if (*p == '-')
268	{
269	  neg = true;
270	  ++p;
271	}
272
273      base = 10;
274      if (*p == '0')
275	{
276	  if (p[1] == 'x' || p[1] == 'X')
277	    {
278	      base = 16;
279	      p += 2;
280	    }
281	  else
282	    {
283	      base = 8;
284	      ++p;
285	    }
286	}
287
288      over = ((bfd_vma) (bfd_signed_vma) -1) / (bfd_vma) base;
289      lastdig = ((bfd_vma) (bfd_signed_vma) -1) % (bfd_vma) base;
290
291      overflow = false;
292      v = 0;
293      while (1)
294	{
295	  int d;
296
297	  d = *p++;
298	  if (isdigit ((unsigned char) d))
299	    d -= '0';
300	  else if (isupper ((unsigned char) d))
301	    d -= 'A';
302	  else if (islower ((unsigned char) d))
303	    d -= 'a';
304	  else
305	    break;
306
307	  if (d >= base)
308	    break;
309
310	  if (v > over || (v == over && (bfd_vma) d > lastdig))
311	    {
312	      overflow = true;
313	      break;
314	    }
315	}
316
317      if (! overflow)
318	{
319	  if (neg)
320	    v = - v;
321	  return v;
322	}
323    }
324
325  /* If we get here, the number is too large to represent in a
326     bfd_vma.  */
327
328  if (poverflow != NULL)
329    *poverflow = true;
330  else
331    warn_stab (orig, "numeric overflow");
332
333  return 0;
334}
335
336/* Give an error for a bad stab string.  */
337
338static void
339bad_stab (p)
340     const char *p;
341{
342  fprintf (stderr, "Bad stab: %s\n", p);
343}
344
345/* Warn about something in a stab string.  */
346
347static void
348warn_stab (p, err)
349     const char *p;
350     const char *err;
351{
352  fprintf (stderr, "Warning: %s: %s\n", err, p);
353}
354
355/* Create a handle to parse stabs symbols with.  */
356
357/*ARGSUSED*/
358PTR
359start_stab (dhandle, abfd, sections, syms, symcount)
360     PTR dhandle;
361     bfd *abfd;
362     boolean sections;
363     asymbol **syms;
364     long symcount;
365{
366  struct stab_handle *ret;
367
368  ret = (struct stab_handle *) xmalloc (sizeof *ret);
369  memset (ret, 0, sizeof *ret);
370  ret->abfd = abfd;
371  ret->sections = sections;
372  ret->syms = syms;
373  ret->symcount = symcount;
374  ret->files = 1;
375  ret->file_types = (struct stab_types **) xmalloc (sizeof *ret->file_types);
376  ret->file_types[0] = NULL;
377  ret->function_end = (bfd_vma) -1;
378  return (PTR) ret;
379}
380
381/* When we have processed all the stabs information, we need to go
382   through and fill in all the undefined tags.  */
383
384boolean
385finish_stab (dhandle, handle)
386     PTR dhandle;
387     PTR handle;
388{
389  struct stab_handle *info = (struct stab_handle *) handle;
390  struct stab_tag *st;
391
392  if (info->within_function)
393    {
394      if (! stab_emit_pending_vars (dhandle, info)
395	  || ! debug_end_function (dhandle, info->function_end))
396	return false;
397      info->within_function = false;
398      info->function_end = (bfd_vma) -1;
399    }
400
401  for (st = info->tags; st != NULL; st = st->next)
402    {
403      enum debug_type_kind kind;
404
405      kind = st->kind;
406      if (kind == DEBUG_KIND_ILLEGAL)
407	kind = DEBUG_KIND_STRUCT;
408      st->slot = debug_make_undefined_tagged_type (dhandle, st->name, kind);
409      if (st->slot == DEBUG_TYPE_NULL)
410	return false;
411    }
412
413  return true;
414}
415
416/* Handle a single stabs symbol.  */
417
418boolean
419parse_stab (dhandle, handle, type, desc, value, string)
420     PTR dhandle;
421     PTR handle;
422     int type;
423     int desc;
424     bfd_vma value;
425     const char *string;
426{
427  struct stab_handle *info = (struct stab_handle *) handle;
428
429  /* gcc will emit two N_SO strings per compilation unit, one for the
430     directory name and one for the file name.  We just collect N_SO
431     strings as we see them, and start the new compilation unit when
432     we see a non N_SO symbol.  */
433  if (info->so_string != NULL
434      && (type != N_SO || *string == '\0' || value != info->so_value))
435    {
436      if (! debug_set_filename (dhandle, info->so_string))
437	return false;
438      info->main_filename = info->so_string;
439
440      info->gcc_compiled = 0;
441      info->n_opt_found = false;
442
443      /* Generally, for stabs in the symbol table, the N_LBRAC and
444	 N_RBRAC symbols are relative to the N_SO symbol value.  */
445      if (! info->sections)
446	info->file_start_offset = info->so_value;
447
448      /* We need to reset the mapping from type numbers to types.  We
449	 can't free the old mapping, because of the use of
450	 debug_make_indirect_type.  */
451      info->files = 1;
452      info->file_types = ((struct stab_types **)
453			  xmalloc (sizeof *info->file_types));
454      info->file_types[0] = NULL;
455
456      info->so_string = NULL;
457
458      /* Now process whatever type we just got.  */
459    }
460
461  switch (type)
462    {
463    case N_FN:
464    case N_FN_SEQ:
465      break;
466
467    case N_LBRAC:
468      /* Ignore extra outermost context from SunPRO cc and acc.  */
469      if (info->n_opt_found && desc == 1)
470	break;
471
472      if (! info->within_function)
473	{
474	  fprintf (stderr, "N_LBRAC not within function\n");
475	  return false;
476	}
477
478      /* Start an inner lexical block.  */
479      if (! debug_start_block (dhandle,
480			       (value
481				+ info->file_start_offset
482				+ info->function_start_offset)))
483	return false;
484
485      /* Emit any pending variable definitions.  */
486      if (! stab_emit_pending_vars (dhandle, info))
487	return false;
488
489      ++info->block_depth;
490      break;
491
492    case N_RBRAC:
493      /* Ignore extra outermost context from SunPRO cc and acc.  */
494      if (info->n_opt_found && desc == 1)
495	break;
496
497      /* We shouldn't have any pending variable definitions here, but,
498         if we do, we probably need to emit them before closing the
499         block.  */
500      if (! stab_emit_pending_vars (dhandle, info))
501	return false;
502
503      /* End an inner lexical block.  */
504      if (! debug_end_block (dhandle,
505			     (value
506			      + info->file_start_offset
507			      + info->function_start_offset)))
508	return false;
509
510      --info->block_depth;
511      if (info->block_depth < 0)
512	{
513	  fprintf (stderr, "Too many N_RBRACs\n");
514	  return false;
515	}
516      break;
517
518    case N_SO:
519      /* This always ends a function.  */
520      if (info->within_function)
521	{
522	  bfd_vma endval;
523
524	  endval = value;
525	  if (*string != '\0'
526	      && info->function_end != (bfd_vma) -1
527	      && info->function_end < endval)
528	    endval = info->function_end;
529	  if (! stab_emit_pending_vars (dhandle, info)
530	      || ! debug_end_function (dhandle, endval))
531	    return false;
532	  info->within_function = false;
533	  info->function_end = (bfd_vma) -1;
534	}
535
536      /* An empty string is emitted by gcc at the end of a compilation
537         unit.  */
538      if (*string == '\0')
539	return true;
540
541      /* Just accumulate strings until we see a non N_SO symbol.  If
542         the string starts with '/', we discard the previously
543         accumulated strings.  */
544      if (info->so_string == NULL)
545	info->so_string = xstrdup (string);
546      else
547	{
548	  char *f;
549
550	  f = info->so_string;
551	  if (*string == '/')
552	    info->so_string = xstrdup (string);
553	  else
554	    info->so_string = concat (info->so_string, string,
555				      (const char *) NULL);
556	  free (f);
557	}
558
559      info->so_value = value;
560
561      break;
562
563    case N_SOL:
564      /* Start an include file.  */
565      if (! debug_start_source (dhandle, string))
566	return false;
567      break;
568
569    case N_BINCL:
570      /* Start an include file which may be replaced.  */
571      push_bincl (info, string, value);
572      if (! debug_start_source (dhandle, string))
573	return false;
574      break;
575
576    case N_EINCL:
577      /* End an N_BINCL include.  */
578      if (! debug_start_source (dhandle, pop_bincl (info)))
579	return false;
580      break;
581
582    case N_EXCL:
583      /* This is a duplicate of a header file named by N_BINCL which
584         was eliminated by the linker.  */
585      if (! find_excl (info, string, value))
586	return false;
587      break;
588
589    case N_SLINE:
590      if (! debug_record_line (dhandle, desc,
591			       value + info->function_start_offset))
592	return false;
593      break;
594
595    case N_BCOMM:
596      if (! debug_start_common_block (dhandle, string))
597	return false;
598      break;
599
600    case N_ECOMM:
601      if (! debug_end_common_block (dhandle, string))
602	return false;
603      break;
604
605    case N_FUN:
606      if (*string == '\0')
607	{
608	  if (info->within_function)
609	    {
610	      /* This always marks the end of a function; we don't
611                 need to worry about info->function_end.  */
612	      if (info->sections)
613		value += info->function_start_offset;
614	      if (! stab_emit_pending_vars (dhandle, info)
615		  || ! debug_end_function (dhandle, value))
616		return false;
617	      info->within_function = false;
618	      info->function_end = (bfd_vma) -1;
619	    }
620	  break;
621	}
622
623      /* A const static symbol in the .text section will have an N_FUN
624         entry.  We need to use these to mark the end of the function,
625         in case we are looking at gcc output before it was changed to
626         always emit an empty N_FUN.  We can't call debug_end_function
627         here, because it might be a local static symbol.  */
628      if (info->within_function
629	  && (info->function_end == (bfd_vma) -1
630	      || value < info->function_end))
631	info->function_end = value;
632
633      /* Fall through.  */
634      /* FIXME: gdb checks the string for N_STSYM, N_LCSYM or N_ROSYM
635         symbols, and if it does not start with :S, gdb relocates the
636         value to the start of the section.  gcc always seems to use
637         :S, so we don't worry about this.  */
638      /* Fall through.  */
639    default:
640      {
641	const char *colon;
642
643	colon = strchr (string, ':');
644	if (colon != NULL
645	    && (colon[1] == 'f' || colon[1] == 'F'))
646	  {
647	    if (info->within_function)
648	      {
649		bfd_vma endval;
650
651		endval = value;
652		if (info->function_end != (bfd_vma) -1
653		    && info->function_end < endval)
654		  endval = info->function_end;
655		if (! stab_emit_pending_vars (dhandle, info)
656		    || ! debug_end_function (dhandle, endval))
657		  return false;
658		info->function_end = (bfd_vma) -1;
659	      }
660	    /* For stabs in sections, line numbers and block addresses
661               are offsets from the start of the function.  */
662	    if (info->sections)
663	      info->function_start_offset = value;
664	    info->within_function = true;
665	  }
666
667	if (! parse_stab_string (dhandle, info, type, desc, value, string))
668	  return false;
669      }
670      break;
671
672    case N_OPT:
673      if (string != NULL && strcmp (string, "gcc2_compiled.") == 0)
674	info->gcc_compiled = 2;
675      else if (string != NULL && strcmp (string, "gcc_compiled.") == 0)
676	info->gcc_compiled = 1;
677      else
678	info->n_opt_found = true;
679      break;
680
681    case N_OBJ:
682    case N_ENDM:
683    case N_MAIN:
684      break;
685    }
686
687  return true;
688}
689
690/* Parse the stabs string.  */
691
692static boolean
693parse_stab_string (dhandle, info, stabtype, desc, value, string)
694     PTR dhandle;
695     struct stab_handle *info;
696     int stabtype;
697     int desc;
698     bfd_vma value;
699     const char *string;
700{
701  const char *p;
702  char *name;
703  int type;
704  debug_type dtype;
705  boolean synonym;
706  unsigned int lineno;
707  debug_type *slot;
708
709  p = strchr (string, ':');
710  if (p == NULL)
711    return true;
712
713  while (p[1] == ':')
714    {
715      p += 2;
716      p = strchr (p, ':');
717      if (p == NULL)
718	{
719	  bad_stab (string);
720	  return false;
721	}
722    }
723
724  /* GCC 2.x puts the line number in desc.  SunOS apparently puts in
725     the number of bytes occupied by a type or object, which we
726     ignore.  */
727  if (info->gcc_compiled >= 2)
728    lineno = desc;
729  else
730    lineno = 0;
731
732  /* FIXME: Sometimes the special C++ names start with '.'.  */
733  name = NULL;
734  if (string[0] == '$')
735    {
736      switch (string[1])
737	{
738	case 't':
739	  name = "this";
740	  break;
741	case 'v':
742	  /* Was: name = "vptr"; */
743	  break;
744	case 'e':
745	  name = "eh_throw";
746	  break;
747	case '_':
748	  /* This was an anonymous type that was never fixed up.  */
749	  break;
750	case 'X':
751	  /* SunPRO (3.0 at least) static variable encoding.  */
752	  break;
753	default:
754	  warn_stab (string, "unknown C++ encoded name");
755	  break;
756	}
757    }
758
759  if (name == NULL)
760    {
761      if (p == string || (string[0] == ' ' && p == string + 1))
762	name = NULL;
763      else
764	name = savestring (string, p - string);
765    }
766
767  ++p;
768  if (isdigit ((unsigned char) *p) || *p == '(' || *p == '-')
769    type = 'l';
770  else
771    type = *p++;
772
773  switch (type)
774    {
775    case 'c':
776      /* c is a special case, not followed by a type-number.
777	 SYMBOL:c=iVALUE for an integer constant symbol.
778	 SYMBOL:c=rVALUE for a floating constant symbol.
779	 SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
780	 e.g. "b:c=e6,0" for "const b = blob1"
781	 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
782      if (*p != '=')
783	{
784	  bad_stab (string);
785	  return false;
786	}
787      ++p;
788      switch (*p++)
789	{
790	case 'r':
791	  /* Floating point constant.  */
792	  if (! debug_record_float_const (dhandle, name, atof (p)))
793	    return false;
794	  break;
795	case 'i':
796	  /* Integer constant.  */
797	  /* Defining integer constants this way is kind of silly,
798	     since 'e' constants allows the compiler to give not only
799	     the value, but the type as well.  C has at least int,
800	     long, unsigned int, and long long as constant types;
801	     other languages probably should have at least unsigned as
802	     well as signed constants.  */
803	  if (! debug_record_int_const (dhandle, name, atoi (p)))
804	    return false;
805	  break;
806	case 'e':
807	  /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
808	     can be represented as integral.
809	     e.g. "b:c=e6,0" for "const b = blob1"
810	     (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
811	  dtype = parse_stab_type (dhandle, info, (const char *) NULL,
812				   &p, (debug_type **) NULL);
813	  if (dtype == DEBUG_TYPE_NULL)
814	    return false;
815	  if (*p != ',')
816	    {
817	      bad_stab (string);
818	      return false;
819	    }
820	  if (! debug_record_typed_const (dhandle, name, dtype, atoi (p)))
821	    return false;
822	  break;
823	default:
824	  bad_stab (string);
825	  return false;
826	}
827
828      break;
829
830    case 'C':
831      /* The name of a caught exception.  */
832      dtype = parse_stab_type (dhandle, info, (const char *) NULL,
833			       &p, (debug_type **) NULL);
834      if (dtype == DEBUG_TYPE_NULL)
835	return false;
836      if (! debug_record_label (dhandle, name, dtype, value))
837	return false;
838      break;
839
840    case 'f':
841    case 'F':
842      /* A function definition.  */
843      dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
844			       (debug_type **) NULL);
845      if (dtype == DEBUG_TYPE_NULL)
846	return false;
847      if (! debug_record_function (dhandle, name, dtype, type == 'F', value))
848	return false;
849
850      /* Sun acc puts declared types of arguments here.  We don't care
851	 about their actual types (FIXME -- we should remember the whole
852	 function prototype), but the list may define some new types
853	 that we have to remember, so we must scan it now.  */
854      while (*p == ';')
855	{
856	  ++p;
857	  if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
858			       (debug_type **) NULL)
859	      == DEBUG_TYPE_NULL)
860	    return false;
861	}
862
863      break;
864
865    case 'G':
866      {
867	char leading;
868	long c;
869	asymbol **ps;
870
871	/* A global symbol.  The value must be extracted from the
872	   symbol table.  */
873	dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
874				 (debug_type **) NULL);
875	if (dtype == DEBUG_TYPE_NULL)
876	  return false;
877	leading = bfd_get_symbol_leading_char (info->abfd);
878	for (c = info->symcount, ps = info->syms; c > 0; --c, ++ps)
879	  {
880	    const char *n;
881
882	    n = bfd_asymbol_name (*ps);
883	    if (leading != '\0' && *n == leading)
884	      ++n;
885	    if (*n == *name && strcmp (n, name) == 0)
886	      break;
887	  }
888	if (c > 0)
889	  value = bfd_asymbol_value (*ps);
890	if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_GLOBAL,
891				    value))
892	  return false;
893      }
894      break;
895
896      /* This case is faked by a conditional above, when there is no
897	 code letter in the dbx data.  Dbx data never actually
898	 contains 'l'.  */
899    case 'l':
900    case 's':
901      dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
902			       (debug_type **) NULL);
903      if (dtype == DEBUG_TYPE_NULL)
904	return false;
905      if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
906				  value))
907	return false;
908      break;
909
910    case 'p':
911      /* A function parameter.  */
912      if (*p != 'F')
913	dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
914				 (debug_type **) NULL);
915      else
916	{
917	/* pF is a two-letter code that means a function parameter in
918	   Fortran.  The type-number specifies the type of the return
919	   value.  Translate it into a pointer-to-function type.  */
920	  ++p;
921	  dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
922				   (debug_type **) NULL);
923	  if (dtype != DEBUG_TYPE_NULL)
924	    {
925	      debug_type ftype;
926
927	      ftype = debug_make_function_type (dhandle, dtype,
928						(debug_type *) NULL, false);
929	      dtype = debug_make_pointer_type (dhandle, ftype);
930	    }
931	}
932      if (dtype == DEBUG_TYPE_NULL)
933	return false;
934      if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_STACK,
935				    value))
936	return false;
937
938      /* FIXME: At this point gdb considers rearranging the parameter
939	 address on a big endian machine if it is smaller than an int.
940	 We have no way to do that, since we don't really know much
941	 about the target.  */
942
943      break;
944
945    case 'P':
946      if (stabtype == N_FUN)
947	{
948	  /* Prototype of a function referenced by this file.  */
949	  while (*p == ';')
950	    {
951	      ++p;
952	      if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
953				   (debug_type **) NULL)
954		  == DEBUG_TYPE_NULL)
955		return false;
956	    }
957	  break;
958	}
959      /* Fall through.  */
960    case 'R':
961      /* Parameter which is in a register.  */
962      dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
963			       (debug_type **) NULL);
964      if (dtype == DEBUG_TYPE_NULL)
965	return false;
966      if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REG,
967				    value))
968	return false;
969      break;
970
971    case 'r':
972      /* Register variable (either global or local).  */
973      dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
974			       (debug_type **) NULL);
975      if (dtype == DEBUG_TYPE_NULL)
976	return false;
977      if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_REGISTER,
978				  value))
979	return false;
980
981      /* FIXME: At this point gdb checks to combine pairs of 'p' and
982	 'r' stabs into a single 'P' stab.  */
983
984      break;
985
986    case 'S':
987      /* Static symbol at top level of file */
988      dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
989			       (debug_type **) NULL);
990      if (dtype == DEBUG_TYPE_NULL)
991	return false;
992      if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_STATIC,
993				  value))
994	return false;
995      break;
996
997    case 't':
998      /* A typedef.  */
999      dtype = parse_stab_type (dhandle, info, name, &p, &slot);
1000      if (dtype == DEBUG_TYPE_NULL)
1001	return false;
1002      if (name == NULL)
1003	{
1004	  /* A nameless type.  Nothing to do.  */
1005	  return true;
1006	}
1007
1008      dtype = debug_name_type (dhandle, name, dtype);
1009      if (dtype == DEBUG_TYPE_NULL)
1010	return false;
1011
1012      if (slot != NULL)
1013	*slot = dtype;
1014
1015      break;
1016
1017    case 'T':
1018      /* Struct, union, or enum tag.  For GNU C++, this can be be followed
1019	 by 't' which means we are typedef'ing it as well.  */
1020      if (*p != 't')
1021	{
1022	  synonym = false;
1023	  /* FIXME: gdb sets synonym to true if the current language
1024             is C++.  */
1025	}
1026      else
1027	{
1028	  synonym = true;
1029	  ++p;
1030	}
1031
1032      dtype = parse_stab_type (dhandle, info, name, &p, &slot);
1033      if (dtype == DEBUG_TYPE_NULL)
1034	return false;
1035      if (name == NULL)
1036	return true;
1037
1038      dtype = debug_tag_type (dhandle, name, dtype);
1039      if (dtype == DEBUG_TYPE_NULL)
1040	return false;
1041      if (slot != NULL)
1042	*slot = dtype;
1043
1044      /* See if we have a cross reference to this tag which we can now
1045         fill in.  */
1046      {
1047	register struct stab_tag **pst;
1048
1049	for (pst = &info->tags; *pst != NULL; pst = &(*pst)->next)
1050	  {
1051	    if ((*pst)->name[0] == name[0]
1052		&& strcmp ((*pst)->name, name) == 0)
1053	      {
1054		(*pst)->slot = dtype;
1055		*pst = (*pst)->next;
1056		break;
1057	      }
1058	  }
1059      }
1060
1061      if (synonym)
1062	{
1063	  dtype = debug_name_type (dhandle, name, dtype);
1064	  if (dtype == DEBUG_TYPE_NULL)
1065	    return false;
1066
1067	  if (slot != NULL)
1068	    *slot = dtype;
1069	}
1070
1071      break;
1072
1073    case 'V':
1074      /* Static symbol of local scope */
1075      dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1076			       (debug_type **) NULL);
1077      if (dtype == DEBUG_TYPE_NULL)
1078	return false;
1079      /* FIXME: gdb checks os9k_stabs here.  */
1080      if (! stab_record_variable (dhandle, info, name, dtype,
1081				  DEBUG_LOCAL_STATIC, value))
1082	return false;
1083      break;
1084
1085    case 'v':
1086      /* Reference parameter.  */
1087      dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1088			       (debug_type **) NULL);
1089      if (dtype == DEBUG_TYPE_NULL)
1090	return false;
1091      if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REFERENCE,
1092				    value))
1093	return false;
1094      break;
1095
1096    case 'a':
1097      /* Reference parameter which is in a register.  */
1098      dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1099			       (debug_type **) NULL);
1100      if (dtype == DEBUG_TYPE_NULL)
1101	return false;
1102      if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REF_REG,
1103				    value))
1104	return false;
1105      break;
1106
1107    case 'X':
1108      /* This is used by Sun FORTRAN for "function result value".
1109	 Sun claims ("dbx and dbxtool interfaces", 2nd ed)
1110	 that Pascal uses it too, but when I tried it Pascal used
1111	 "x:3" (local symbol) instead.  */
1112      dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1113			       (debug_type **) NULL);
1114      if (dtype == DEBUG_TYPE_NULL)
1115	return false;
1116      if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
1117				  value))
1118	return false;
1119      break;
1120
1121    default:
1122      bad_stab (string);
1123      return false;
1124    }
1125
1126  /* FIXME: gdb converts structure values to structure pointers in a
1127     couple of cases, depending upon the target.  */
1128
1129  return true;
1130}
1131
1132/* Parse a stabs type.  The typename argument is non-NULL if this is a
1133   typedef or a tag definition.  The pp argument points to the stab
1134   string, and is updated.  The slotp argument points to a place to
1135   store the slot used if the type is being defined.  */
1136
1137static debug_type
1138parse_stab_type (dhandle, info, typename, pp, slotp)
1139     PTR dhandle;
1140     struct stab_handle *info;
1141     const char *typename;
1142     const char **pp;
1143     debug_type **slotp;
1144{
1145  const char *orig;
1146  int typenums[2];
1147  int size;
1148  boolean stringp;
1149  int descriptor;
1150  debug_type dtype;
1151
1152  if (slotp != NULL)
1153    *slotp = NULL;
1154
1155  orig = *pp;
1156
1157  size = -1;
1158  stringp = false;
1159
1160  /* Read type number if present.  The type number may be omitted.
1161     for instance in a two-dimensional array declared with type
1162     "ar1;1;10;ar1;1;10;4".  */
1163  if (! isdigit ((unsigned char) **pp) && **pp != '(' && **pp != '-')
1164    {
1165      /* 'typenums=' not present, type is anonymous.  Read and return
1166	 the definition, but don't put it in the type vector.  */
1167      typenums[0] = typenums[1] = -1;
1168    }
1169  else
1170    {
1171      if (! parse_stab_type_number (pp, typenums))
1172	return DEBUG_TYPE_NULL;
1173
1174      if (**pp != '=')
1175	{
1176	  /* Type is not being defined here.  Either it already
1177	     exists, or this is a forward reference to it.  */
1178	  return stab_find_type (dhandle, info, typenums);
1179	}
1180
1181      /* Only set the slot if the type is being defined.  This means
1182         that the mapping from type numbers to types will only record
1183         the name of the typedef which defines a type.  If we don't do
1184         this, then something like
1185	     typedef int foo;
1186	     int i;
1187	 will record that i is of type foo.  Unfortunately, stabs
1188	 information is ambiguous about variable types.  For this code,
1189	     typedef int foo;
1190	     int i;
1191	     foo j;
1192	 the stabs information records both i and j as having the same
1193	 type.  This could be fixed by patching the compiler.  */
1194      if (slotp != NULL && typenums[0] >= 0 && typenums[1] >= 0)
1195	*slotp = stab_find_slot (info, typenums);
1196
1197      /* Type is being defined here.  */
1198      /* Skip the '='.  */
1199      ++*pp;
1200
1201      while (**pp == '@')
1202	{
1203	  const char *p = *pp + 1;
1204	  const char *attr;
1205
1206	  if (isdigit ((unsigned char) *p) || *p == '(' || *p == '-')
1207	    {
1208	      /* Member type.  */
1209	      break;
1210	    }
1211
1212	  /* Type attributes.  */
1213	  attr = p;
1214
1215	  for (; *p != ';'; ++p)
1216	    {
1217	      if (*p == '\0')
1218		{
1219		  bad_stab (orig);
1220		  return DEBUG_TYPE_NULL;
1221		}
1222	    }
1223	  *pp = p + 1;
1224
1225	  switch (*attr)
1226	    {
1227	    case 's':
1228	      size = atoi (attr + 1);
1229	      if (size <= 0)
1230		size = -1;
1231	      break;
1232
1233	    case 'S':
1234	      stringp = true;
1235	      break;
1236
1237	    default:
1238	      /* Ignore unrecognized type attributes, so future
1239		 compilers can invent new ones.  */
1240	      break;
1241	    }
1242	}
1243    }
1244
1245  descriptor = **pp;
1246  ++*pp;
1247
1248  switch (descriptor)
1249    {
1250    case 'x':
1251      {
1252	enum debug_type_kind code;
1253	const char *q1, *q2, *p;
1254
1255	/* A cross reference to another type.  */
1256
1257	switch (**pp)
1258	  {
1259	  case 's':
1260	    code = DEBUG_KIND_STRUCT;
1261	    break;
1262	  case 'u':
1263	    code = DEBUG_KIND_UNION;
1264	    break;
1265	  case 'e':
1266	    code = DEBUG_KIND_ENUM;
1267	    break;
1268	  default:
1269	    /* Complain and keep going, so compilers can invent new
1270	       cross-reference types.  */
1271	    warn_stab (orig, "unrecognized cross reference type");
1272	    code = DEBUG_KIND_STRUCT;
1273	    break;
1274	  }
1275	++*pp;
1276
1277	q1 = strchr (*pp, '<');
1278	p = strchr (*pp, ':');
1279	if (p == NULL)
1280	  {
1281	    bad_stab (orig);
1282	    return DEBUG_TYPE_NULL;
1283	  }
1284	while (q1 != NULL && p > q1 && p[1] == ':')
1285	  {
1286	    q2 = strchr (q1, '>');
1287	    if (q2 == NULL || q2 < p)
1288	      break;
1289	    p += 2;
1290	    p = strchr (p, ':');
1291	    if (p == NULL)
1292	      {
1293		bad_stab (orig);
1294		return DEBUG_TYPE_NULL;
1295	      }
1296	  }
1297
1298	dtype = stab_find_tagged_type (dhandle, info, *pp, p - *pp, code);
1299
1300	*pp = p + 1;
1301      }
1302      break;
1303
1304    case '-':
1305    case '0':
1306    case '1':
1307    case '2':
1308    case '3':
1309    case '4':
1310    case '5':
1311    case '6':
1312    case '7':
1313    case '8':
1314    case '9':
1315    case '(':
1316      {
1317	const char *hold;
1318	int xtypenums[2];
1319
1320	/* This type is defined as another type.  */
1321
1322	(*pp)--;
1323	hold = *pp;
1324
1325	/* Peek ahead at the number to detect void.  */
1326	if (! parse_stab_type_number (pp, xtypenums))
1327	  return DEBUG_TYPE_NULL;
1328
1329	if (typenums[0] == xtypenums[0] && typenums[1] == xtypenums[1])
1330	  {
1331	    /* This type is being defined as itself, which means that
1332               it is void.  */
1333	    dtype = debug_make_void_type (dhandle);
1334	  }
1335	else
1336	  {
1337	    *pp = hold;
1338
1339	    /* Go back to the number and have parse_stab_type get it.
1340	       This means that we can deal with something like
1341	       t(1,2)=(3,4)=... which the Lucid compiler uses.  */
1342	    dtype = parse_stab_type (dhandle, info, (const char *) NULL,
1343				     pp, (debug_type **) NULL);
1344	    if (dtype == DEBUG_TYPE_NULL)
1345	      return DEBUG_TYPE_NULL;
1346	  }
1347
1348	if (typenums[0] != -1)
1349	  {
1350	    if (! stab_record_type (dhandle, info, typenums, dtype))
1351	      return DEBUG_TYPE_NULL;
1352	  }
1353
1354	break;
1355      }
1356
1357    case '*':
1358      dtype = debug_make_pointer_type (dhandle,
1359				       parse_stab_type (dhandle, info,
1360							(const char *) NULL,
1361							pp,
1362							(debug_type **) NULL));
1363      break;
1364
1365    case '&':
1366      /* Reference to another type.  */
1367      dtype = (debug_make_reference_type
1368	       (dhandle,
1369		parse_stab_type (dhandle, info, (const char *) NULL, pp,
1370				 (debug_type **) NULL)));
1371      break;
1372
1373    case 'f':
1374      /* Function returning another type.  */
1375      /* FIXME: gdb checks os9k_stabs here.  */
1376      dtype = (debug_make_function_type
1377	       (dhandle,
1378		parse_stab_type (dhandle, info, (const char *) NULL, pp,
1379				 (debug_type **) NULL),
1380		(debug_type *) NULL, false));
1381      break;
1382
1383    case 'k':
1384      /* Const qualifier on some type (Sun).  */
1385      /* FIXME: gdb accepts 'c' here if os9k_stabs.  */
1386      dtype = debug_make_const_type (dhandle,
1387				     parse_stab_type (dhandle, info,
1388						      (const char *) NULL,
1389						      pp,
1390						      (debug_type **) NULL));
1391      break;
1392
1393    case 'B':
1394      /* Volatile qual on some type (Sun).  */
1395      /* FIXME: gdb accepts 'i' here if os9k_stabs.  */
1396      dtype = (debug_make_volatile_type
1397	       (dhandle,
1398		parse_stab_type (dhandle, info, (const char *) NULL, pp,
1399				 (debug_type **) NULL)));
1400      break;
1401
1402    case '@':
1403      /* Offset (class & variable) type.  This is used for a pointer
1404         relative to an object.  */
1405      {
1406	debug_type domain;
1407	debug_type memtype;
1408
1409	/* Member type.  */
1410
1411	domain = parse_stab_type (dhandle, info, (const char *) NULL, pp,
1412				  (debug_type **) NULL);
1413	if (domain == DEBUG_TYPE_NULL)
1414	  return DEBUG_TYPE_NULL;
1415
1416	if (**pp != ',')
1417	  {
1418	    bad_stab (orig);
1419	    return DEBUG_TYPE_NULL;
1420	  }
1421	++*pp;
1422
1423	memtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
1424				   (debug_type **) NULL);
1425	if (memtype == DEBUG_TYPE_NULL)
1426	  return DEBUG_TYPE_NULL;
1427
1428	dtype = debug_make_offset_type (dhandle, domain, memtype);
1429      }
1430      break;
1431
1432    case '#':
1433      /* Method (class & fn) type.  */
1434      if (**pp == '#')
1435	{
1436	  debug_type return_type;
1437
1438	  ++*pp;
1439	  return_type = parse_stab_type (dhandle, info, (const char *) NULL,
1440					 pp, (debug_type **) NULL);
1441	  if (return_type == DEBUG_TYPE_NULL)
1442	    return DEBUG_TYPE_NULL;
1443	  if (**pp != ';')
1444	    {
1445	      bad_stab (orig);
1446	      return DEBUG_TYPE_NULL;
1447	    }
1448	  ++*pp;
1449	  dtype = debug_make_method_type (dhandle, return_type,
1450					  DEBUG_TYPE_NULL,
1451					  (debug_type *) NULL, false);
1452	}
1453      else
1454	{
1455	  debug_type domain;
1456	  debug_type return_type;
1457	  debug_type *args;
1458	  unsigned int n;
1459	  unsigned int alloc;
1460	  boolean varargs;
1461
1462	  domain = parse_stab_type (dhandle, info, (const char *) NULL,
1463				    pp, (debug_type **) NULL);
1464	  if (domain == DEBUG_TYPE_NULL)
1465	    return DEBUG_TYPE_NULL;
1466
1467	  if (**pp != ',')
1468	    {
1469	      bad_stab (orig);
1470	      return DEBUG_TYPE_NULL;
1471	    }
1472	  ++*pp;
1473
1474	  return_type = parse_stab_type (dhandle, info, (const char *) NULL,
1475					 pp, (debug_type **) NULL);
1476	  if (return_type == DEBUG_TYPE_NULL)
1477	    return DEBUG_TYPE_NULL;
1478
1479	  alloc = 10;
1480	  args = (debug_type *) xmalloc (alloc * sizeof *args);
1481	  n = 0;
1482	  while (**pp != ';')
1483	    {
1484	      if (**pp != ',')
1485		{
1486		  bad_stab (orig);
1487		  return DEBUG_TYPE_NULL;
1488		}
1489	      ++*pp;
1490
1491	      if (n + 1 >= alloc)
1492		{
1493		  alloc += 10;
1494		  args = ((debug_type *)
1495			  xrealloc ((PTR) args, alloc * sizeof *args));
1496		}
1497
1498	      args[n] = parse_stab_type (dhandle, info, (const char *) NULL,
1499					 pp, (debug_type **) NULL);
1500	      if (args[n] == DEBUG_TYPE_NULL)
1501		return DEBUG_TYPE_NULL;
1502	      ++n;
1503	    }
1504	  ++*pp;
1505
1506	  /* If the last type is not void, then this function takes a
1507	     variable number of arguments.  Otherwise, we must strip
1508	     the void type.  */
1509	  if (n == 0
1510	      || debug_get_type_kind (dhandle, args[n - 1]) != DEBUG_KIND_VOID)
1511	    varargs = true;
1512	  else
1513	    {
1514	      --n;
1515	      varargs = false;
1516	    }
1517
1518	  args[n] = DEBUG_TYPE_NULL;
1519
1520	  dtype = debug_make_method_type (dhandle, return_type, domain, args,
1521					  varargs);
1522	}
1523      break;
1524
1525    case 'r':
1526      /* Range type.  */
1527      dtype = parse_stab_range_type (dhandle, info, typename, pp, typenums);
1528      break;
1529
1530    case 'b':
1531      /* FIXME: gdb checks os9k_stabs here.  */
1532      /* Sun ACC builtin int type.  */
1533      dtype = parse_stab_sun_builtin_type (dhandle, pp);
1534      break;
1535
1536    case 'R':
1537      /* Sun ACC builtin float type.  */
1538      dtype = parse_stab_sun_floating_type (dhandle, pp);
1539      break;
1540
1541    case 'e':
1542      /* Enumeration type.  */
1543      dtype = parse_stab_enum_type (dhandle, pp);
1544      break;
1545
1546    case 's':
1547    case 'u':
1548      /* Struct or union type.  */
1549      dtype = parse_stab_struct_type (dhandle, info, typename, pp,
1550				      descriptor == 's', typenums);
1551      break;
1552
1553    case 'a':
1554      /* Array type.  */
1555      if (**pp != 'r')
1556	{
1557	  bad_stab (orig);
1558	  return DEBUG_TYPE_NULL;
1559	}
1560      ++*pp;
1561
1562      dtype = parse_stab_array_type (dhandle, info, pp, stringp);
1563      break;
1564
1565    case 'S':
1566      dtype = debug_make_set_type (dhandle,
1567				   parse_stab_type (dhandle, info,
1568						    (const char *) NULL,
1569						    pp,
1570						    (debug_type **) NULL),
1571				   stringp);
1572      break;
1573
1574    default:
1575      bad_stab (orig);
1576      return DEBUG_TYPE_NULL;
1577    }
1578
1579  if (dtype == DEBUG_TYPE_NULL)
1580    return DEBUG_TYPE_NULL;
1581
1582  if (typenums[0] != -1)
1583    {
1584      if (! stab_record_type (dhandle, info, typenums, dtype))
1585	return DEBUG_TYPE_NULL;
1586    }
1587
1588  if (size != -1)
1589    {
1590      if (! debug_record_type_size (dhandle, dtype, (unsigned int) size))
1591	return false;
1592    }
1593
1594  return dtype;
1595}
1596
1597/* Read a number by which a type is referred to in dbx data, or
1598   perhaps read a pair (FILENUM, TYPENUM) in parentheses.  Just a
1599   single number N is equivalent to (0,N).  Return the two numbers by
1600   storing them in the vector TYPENUMS.  */
1601
1602static boolean
1603parse_stab_type_number (pp, typenums)
1604     const char **pp;
1605     int *typenums;
1606{
1607  const char *orig;
1608
1609  orig = *pp;
1610
1611  if (**pp != '(')
1612    {
1613      typenums[0] = 0;
1614      typenums[1] = (int) parse_number (pp, (boolean *) NULL);
1615    }
1616  else
1617    {
1618      ++*pp;
1619      typenums[0] = (int) parse_number (pp, (boolean *) NULL);
1620      if (**pp != ',')
1621	{
1622	  bad_stab (orig);
1623	  return false;
1624	}
1625      ++*pp;
1626      typenums[1] = (int) parse_number (pp, (boolean *) NULL);
1627      if (**pp != ')')
1628	{
1629	  bad_stab (orig);
1630	  return false;
1631	}
1632      ++*pp;
1633    }
1634
1635  return true;
1636}
1637
1638/* Parse a range type.  */
1639
1640static debug_type
1641parse_stab_range_type (dhandle, info, typename, pp, typenums)
1642     PTR dhandle;
1643     struct stab_handle *info;
1644     const char *typename;
1645     const char **pp;
1646     const int *typenums;
1647{
1648  const char *orig;
1649  int rangenums[2];
1650  boolean self_subrange;
1651  debug_type index_type;
1652  const char *s2, *s3;
1653  bfd_signed_vma n2, n3;
1654  boolean ov2, ov3;
1655
1656  orig = *pp;
1657
1658  index_type = DEBUG_TYPE_NULL;
1659
1660  /* First comes a type we are a subrange of.
1661     In C it is usually 0, 1 or the type being defined.  */
1662  if (! parse_stab_type_number (pp, rangenums))
1663    return DEBUG_TYPE_NULL;
1664
1665  self_subrange = (rangenums[0] == typenums[0]
1666		   && rangenums[1] == typenums[1]);
1667
1668  if (**pp == '=')
1669    {
1670      *pp = orig;
1671      index_type = parse_stab_type (dhandle, info, (const char *) NULL,
1672				    pp, (debug_type **) NULL);
1673      if (index_type == DEBUG_TYPE_NULL)
1674	return DEBUG_TYPE_NULL;
1675    }
1676
1677  if (**pp == ';')
1678    ++*pp;
1679
1680  /* The remaining two operands are usually lower and upper bounds of
1681     the range.  But in some special cases they mean something else.  */
1682  s2 = *pp;
1683  n2 = parse_number (pp, &ov2);
1684  if (**pp != ';')
1685    {
1686      bad_stab (orig);
1687      return DEBUG_TYPE_NULL;
1688    }
1689  ++*pp;
1690
1691  s3 = *pp;
1692  n3 = parse_number (pp, &ov3);
1693  if (**pp != ';')
1694    {
1695      bad_stab (orig);
1696      return DEBUG_TYPE_NULL;
1697    }
1698  ++*pp;
1699
1700  if (ov2 || ov3)
1701    {
1702      /* gcc will emit range stabs for long long types.  Handle this
1703         as a special case.  FIXME: This needs to be more general.  */
1704#define LLLOW  "01000000000000000000000;"
1705#define LLHIGH "0777777777777777777777;"
1706#define ULLHIGH "01777777777777777777777;"
1707      if (index_type == DEBUG_TYPE_NULL)
1708	{
1709	  if (strncmp (s2, LLLOW, sizeof LLLOW - 1) == 0
1710	      && strncmp (s3, LLHIGH, sizeof LLHIGH - 1) == 0)
1711	    return debug_make_int_type (dhandle, 8, false);
1712	  if (! ov2
1713	      && n2 == 0
1714	      && strncmp (s3, ULLHIGH, sizeof ULLHIGH - 1) == 0)
1715	    return debug_make_int_type (dhandle, 8, true);
1716	}
1717
1718      warn_stab (orig, "numeric overflow");
1719    }
1720
1721  if (index_type == DEBUG_TYPE_NULL)
1722    {
1723      /* A type defined as a subrange of itself, with both bounds 0,
1724         is void.  */
1725      if (self_subrange && n2 == 0 && n3 == 0)
1726	return debug_make_void_type (dhandle);
1727
1728      /* A type defined as a subrange of itself, with n2 positive and
1729	 n3 zero, is a complex type, and n2 is the number of bytes.  */
1730      if (self_subrange && n3 == 0 && n2 > 0)
1731	return debug_make_complex_type (dhandle, n2);
1732
1733      /* If n3 is zero and n2 is positive, this is a floating point
1734         type, and n2 is the number of bytes.  */
1735      if (n3 == 0 && n2 > 0)
1736	return debug_make_float_type (dhandle, n2);
1737
1738      /* If the upper bound is -1, this is an unsigned int.  */
1739      if (n2 == 0 && n3 == -1)
1740	{
1741	  /* When gcc is used with -gstabs, but not -gstabs+, it will emit
1742	         long long int:t6=r1;0;-1;
1743		 long long unsigned int:t7=r1;0;-1;
1744	     We hack here to handle this reasonably.  */
1745	  if (typename != NULL)
1746	    {
1747	      if (strcmp (typename, "long long int") == 0)
1748		return debug_make_int_type (dhandle, 8, false);
1749	      else if (strcmp (typename, "long long unsigned int") == 0)
1750		return debug_make_int_type (dhandle, 8, true);
1751	    }
1752	  /* FIXME: The size here really depends upon the target.  */
1753	  return debug_make_int_type (dhandle, 4, true);
1754	}
1755
1756      /* A range of 0 to 127 is char.  */
1757      if (self_subrange && n2 == 0 && n3 == 127)
1758	return debug_make_int_type (dhandle, 1, false);
1759
1760      /* FIXME: gdb checks for the language CHILL here.  */
1761
1762      if (n2 == 0)
1763	{
1764	  if (n3 < 0)
1765	    return debug_make_int_type (dhandle, - n3, true);
1766	  else if (n3 == 0xff)
1767	    return debug_make_int_type (dhandle, 1, true);
1768	  else if (n3 == 0xffff)
1769	    return debug_make_int_type (dhandle, 2, true);
1770	  /* -1 is used for the upper bound of (4 byte) "unsigned int"
1771	     and "unsigned long", and we already checked for that, so
1772	     don't need to test for it here.  */
1773	}
1774      else if (n3 == 0
1775	       && n2 < 0
1776	       && (self_subrange || n2 == -8))
1777	return debug_make_int_type (dhandle, - n2, true);
1778      else if (n2 == - n3 - 1)
1779	{
1780	  if (n3 == 0x7f)
1781	    return debug_make_int_type (dhandle, 1, false);
1782	  else if (n3 == 0x7fff)
1783	    return debug_make_int_type (dhandle, 2, false);
1784	  else if (n3 == 0x7fffffff)
1785	    return debug_make_int_type (dhandle, 4, false);
1786	}
1787    }
1788
1789  /* At this point I don't have the faintest idea how to deal with a
1790     self_subrange type; I'm going to assume that this is used as an
1791     idiom, and that all of them are special cases.  So . . .  */
1792  if (self_subrange)
1793    {
1794      bad_stab (orig);
1795      return DEBUG_TYPE_NULL;
1796    }
1797
1798  index_type = stab_find_type (dhandle, info, rangenums);
1799  if (index_type == DEBUG_TYPE_NULL)
1800    {
1801      /* Does this actually ever happen?  Is that why we are worrying
1802         about dealing with it rather than just calling error_type?  */
1803      warn_stab (orig, "missing index type");
1804      index_type = debug_make_int_type (dhandle, 4, false);
1805    }
1806
1807  return debug_make_range_type (dhandle, index_type, n2, n3);
1808}
1809
1810/* Sun's ACC uses a somewhat saner method for specifying the builtin
1811   typedefs in every file (for int, long, etc):
1812
1813	type = b <signed> <width>; <offset>; <nbits>
1814	signed = u or s.  Possible c in addition to u or s (for char?).
1815	offset = offset from high order bit to start bit of type.
1816	width is # bytes in object of this type, nbits is # bits in type.
1817
1818   The width/offset stuff appears to be for small objects stored in
1819   larger ones (e.g. `shorts' in `int' registers).  We ignore it for now,
1820   FIXME.  */
1821
1822static debug_type
1823parse_stab_sun_builtin_type (dhandle, pp)
1824     PTR dhandle;
1825     const char **pp;
1826{
1827  const char *orig;
1828  boolean unsignedp;
1829  bfd_vma bits;
1830
1831  orig = *pp;
1832
1833  switch (**pp)
1834    {
1835    case 's':
1836      unsignedp = false;
1837      break;
1838    case 'u':
1839      unsignedp = true;
1840      break;
1841    default:
1842      bad_stab (orig);
1843      return DEBUG_TYPE_NULL;
1844    }
1845  ++*pp;
1846
1847  /* For some odd reason, all forms of char put a c here.  This is strange
1848     because no other type has this honor.  We can safely ignore this because
1849     we actually determine 'char'acterness by the number of bits specified in
1850     the descriptor.  */
1851  if (**pp == 'c')
1852    ++*pp;
1853
1854  /* The first number appears to be the number of bytes occupied
1855     by this type, except that unsigned short is 4 instead of 2.
1856     Since this information is redundant with the third number,
1857     we will ignore it.  */
1858  (void) parse_number (pp, (boolean *) NULL);
1859  if (**pp != ';')
1860    {
1861      bad_stab (orig);
1862      return DEBUG_TYPE_NULL;
1863    }
1864  ++*pp;
1865
1866  /* The second number is always 0, so ignore it too. */
1867  (void) parse_number (pp, (boolean *) NULL);
1868  if (**pp != ';')
1869    {
1870      bad_stab (orig);
1871      return DEBUG_TYPE_NULL;
1872    }
1873  ++*pp;
1874
1875  /* The third number is the number of bits for this type. */
1876  bits = parse_number (pp, (boolean *) NULL);
1877
1878  /* The type *should* end with a semicolon.  If it are embedded
1879     in a larger type the semicolon may be the only way to know where
1880     the type ends.  If this type is at the end of the stabstring we
1881     can deal with the omitted semicolon (but we don't have to like
1882     it).  Don't bother to complain(), Sun's compiler omits the semicolon
1883     for "void".  */
1884  if (**pp == ';')
1885    ++*pp;
1886
1887  if (bits == 0)
1888    return debug_make_void_type (dhandle);
1889
1890  return debug_make_int_type (dhandle, bits / 8, unsignedp);
1891}
1892
1893/* Parse a builtin floating type generated by the Sun compiler.  */
1894
1895static debug_type
1896parse_stab_sun_floating_type (dhandle, pp)
1897     PTR dhandle;
1898     const char **pp;
1899{
1900  const char *orig;
1901  bfd_vma details;
1902  bfd_vma bytes;
1903
1904  orig = *pp;
1905
1906  /* The first number has more details about the type, for example
1907     FN_COMPLEX.  */
1908  details = parse_number (pp, (boolean *) NULL);
1909  if (**pp != ';')
1910    {
1911      bad_stab (orig);
1912      return DEBUG_TYPE_NULL;
1913    }
1914
1915  /* The second number is the number of bytes occupied by this type */
1916  bytes = parse_number (pp, (boolean *) NULL);
1917  if (**pp != ';')
1918    {
1919      bad_stab (orig);
1920      return DEBUG_TYPE_NULL;
1921    }
1922
1923  if (details == NF_COMPLEX
1924      || details == NF_COMPLEX16
1925      || details == NF_COMPLEX32)
1926    return debug_make_complex_type (dhandle, bytes);
1927
1928  return debug_make_float_type (dhandle, bytes);
1929}
1930
1931/* Handle an enum type.  */
1932
1933static debug_type
1934parse_stab_enum_type (dhandle, pp)
1935     PTR dhandle;
1936     const char **pp;
1937{
1938  const char *orig;
1939  const char **names;
1940  bfd_signed_vma *values;
1941  unsigned int n;
1942  unsigned int alloc;
1943
1944  orig = *pp;
1945
1946  /* FIXME: gdb checks os9k_stabs here.  */
1947
1948  /* The aix4 compiler emits an extra field before the enum members;
1949     my guess is it's a type of some sort.  Just ignore it.  */
1950  if (**pp == '-')
1951    {
1952      while (**pp != ':')
1953	++*pp;
1954      ++*pp;
1955    }
1956
1957  /* Read the value-names and their values.
1958     The input syntax is NAME:VALUE,NAME:VALUE, and so on.
1959     A semicolon or comma instead of a NAME means the end.  */
1960  alloc = 10;
1961  names = (const char **) xmalloc (alloc * sizeof *names);
1962  values = (bfd_signed_vma *) xmalloc (alloc * sizeof *values);
1963  n = 0;
1964  while (**pp != '\0' && **pp != ';' && **pp != ',')
1965    {
1966      const char *p;
1967      char *name;
1968      bfd_signed_vma val;
1969
1970      p = *pp;
1971      while (*p != ':')
1972	++p;
1973
1974      name = savestring (*pp, p - *pp);
1975
1976      *pp = p + 1;
1977      val = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
1978      if (**pp != ',')
1979	{
1980	  bad_stab (orig);
1981	  return DEBUG_TYPE_NULL;
1982	}
1983      ++*pp;
1984
1985      if (n + 1 >= alloc)
1986	{
1987	  alloc += 10;
1988	  names = ((const char **)
1989		   xrealloc ((PTR) names, alloc * sizeof *names));
1990	  values = ((bfd_signed_vma *)
1991		    xrealloc ((PTR) values, alloc * sizeof *values));
1992	}
1993
1994      names[n] = name;
1995      values[n] = val;
1996      ++n;
1997    }
1998
1999  names[n] = NULL;
2000  values[n] = 0;
2001
2002  if (**pp == ';')
2003    ++*pp;
2004
2005  return debug_make_enum_type (dhandle, names, values);
2006}
2007
2008/* Read the description of a structure (or union type) and return an object
2009   describing the type.
2010
2011   PP points to a character pointer that points to the next unconsumed token
2012   in the the stabs string.  For example, given stabs "A:T4=s4a:1,0,32;;",
2013   *PP will point to "4a:1,0,32;;".  */
2014
2015static debug_type
2016parse_stab_struct_type (dhandle, info, tagname, pp, structp, typenums)
2017     PTR dhandle;
2018     struct stab_handle *info;
2019     const char *tagname;
2020     const char **pp;
2021     boolean structp;
2022     const int *typenums;
2023{
2024  const char *orig;
2025  bfd_vma size;
2026  debug_baseclass *baseclasses;
2027  debug_field *fields;
2028  boolean statics;
2029  debug_method *methods;
2030  debug_type vptrbase;
2031  boolean ownvptr;
2032
2033  orig = *pp;
2034
2035  /* Get the size.  */
2036  size = parse_number (pp, (boolean *) NULL);
2037
2038  /* Get the other information.  */
2039  if (! parse_stab_baseclasses (dhandle, info, pp, &baseclasses)
2040      || ! parse_stab_struct_fields (dhandle, info, pp, &fields, &statics)
2041      || ! parse_stab_members (dhandle, info, tagname, pp, typenums, &methods)
2042      || ! parse_stab_tilde_field (dhandle, info, pp, typenums, &vptrbase,
2043				   &ownvptr))
2044    return DEBUG_TYPE_NULL;
2045
2046  if (! statics
2047      && baseclasses == NULL
2048      && methods == NULL
2049      && vptrbase == DEBUG_TYPE_NULL
2050      && ! ownvptr)
2051    return debug_make_struct_type (dhandle, structp, size, fields);
2052
2053  return debug_make_object_type (dhandle, structp, size, fields, baseclasses,
2054				 methods, vptrbase, ownvptr);
2055}
2056
2057/* The stabs for C++ derived classes contain baseclass information which
2058   is marked by a '!' character after the total size.  This function is
2059   called when we encounter the baseclass marker, and slurps up all the
2060   baseclass information.
2061
2062   Immediately following the '!' marker is the number of base classes that
2063   the class is derived from, followed by information for each base class.
2064   For each base class, there are two visibility specifiers, a bit offset
2065   to the base class information within the derived class, a reference to
2066   the type for the base class, and a terminating semicolon.
2067
2068   A typical example, with two base classes, would be "!2,020,19;0264,21;".
2069   						       ^^ ^ ^ ^  ^ ^  ^
2070	Baseclass information marker __________________|| | | |  | |  |
2071	Number of baseclasses __________________________| | | |  | |  |
2072	Visibility specifiers (2) ________________________| | |  | |  |
2073	Offset in bits from start of class _________________| |  | |  |
2074	Type number for base class ___________________________|  | |  |
2075	Visibility specifiers (2) _______________________________| |  |
2076	Offset in bits from start of class ________________________|  |
2077	Type number of base class ____________________________________|
2078
2079  Return true for success, false for failure.  */
2080
2081static boolean
2082parse_stab_baseclasses (dhandle, info, pp, retp)
2083     PTR dhandle;
2084     struct stab_handle *info;
2085     const char **pp;
2086     debug_baseclass **retp;
2087{
2088  const char *orig;
2089  unsigned int c, i;
2090  debug_baseclass *classes;
2091
2092  *retp = NULL;
2093
2094  orig = *pp;
2095
2096  if (**pp != '!')
2097    {
2098      /* No base classes.  */
2099      return true;
2100    }
2101  ++*pp;
2102
2103  c = (unsigned int) parse_number (pp, (boolean *) NULL);
2104
2105  if (**pp != ',')
2106    {
2107      bad_stab (orig);
2108      return false;
2109    }
2110  ++*pp;
2111
2112  classes = (debug_baseclass *) xmalloc ((c + 1) * sizeof (**retp));
2113
2114  for (i = 0; i < c; i++)
2115    {
2116      boolean virtual;
2117      enum debug_visibility visibility;
2118      bfd_vma bitpos;
2119      debug_type type;
2120
2121      switch (**pp)
2122	{
2123	case '0':
2124	  virtual = false;
2125	  break;
2126	case '1':
2127	  virtual = true;
2128	  break;
2129	default:
2130	  warn_stab (orig, "unknown virtual character for baseclass");
2131	  virtual = false;
2132	  break;
2133	}
2134      ++*pp;
2135
2136      switch (**pp)
2137	{
2138	case '0':
2139	  visibility = DEBUG_VISIBILITY_PRIVATE;
2140	  break;
2141	case '1':
2142	  visibility = DEBUG_VISIBILITY_PROTECTED;
2143	  break;
2144	case '2':
2145	  visibility = DEBUG_VISIBILITY_PUBLIC;
2146	  break;
2147	default:
2148	  warn_stab (orig, "unknown visibility character for baseclass");
2149	  visibility = DEBUG_VISIBILITY_PUBLIC;
2150	  break;
2151	}
2152      ++*pp;
2153
2154      /* The remaining value is the bit offset of the portion of the
2155	 object corresponding to this baseclass.  Always zero in the
2156	 absence of multiple inheritance.  */
2157      bitpos = parse_number (pp, (boolean *) NULL);
2158      if (**pp != ',')
2159	{
2160	  bad_stab (orig);
2161	  return false;
2162	}
2163      ++*pp;
2164
2165      type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2166			      (debug_type **) NULL);
2167      if (type == DEBUG_TYPE_NULL)
2168	return false;
2169
2170      classes[i] = debug_make_baseclass (dhandle, type, bitpos, virtual,
2171					 visibility);
2172      if (classes[i] == DEBUG_BASECLASS_NULL)
2173	return false;
2174
2175      if (**pp != ';')
2176	return false;
2177      ++*pp;
2178    }
2179
2180  classes[i] = DEBUG_BASECLASS_NULL;
2181
2182  *retp = classes;
2183
2184  return true;
2185}
2186
2187/* Read struct or class data fields.  They have the form:
2188
2189   	NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
2190
2191   At the end, we see a semicolon instead of a field.
2192
2193   In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2194   a static field.
2195
2196   The optional VISIBILITY is one of:
2197
2198   	'/0'	(VISIBILITY_PRIVATE)
2199	'/1'	(VISIBILITY_PROTECTED)
2200	'/2'	(VISIBILITY_PUBLIC)
2201	'/9'	(VISIBILITY_IGNORE)
2202
2203   or nothing, for C style fields with public visibility.
2204
2205   Returns 1 for success, 0 for failure.  */
2206
2207static boolean
2208parse_stab_struct_fields (dhandle, info, pp, retp, staticsp)
2209     PTR dhandle;
2210     struct stab_handle *info;
2211     const char **pp;
2212     debug_field **retp;
2213     boolean *staticsp;
2214{
2215  const char *orig;
2216  const char *p;
2217  debug_field *fields;
2218  unsigned int c;
2219  unsigned int alloc;
2220
2221  *retp = NULL;
2222  *staticsp = false;
2223
2224  orig = *pp;
2225
2226  c = 0;
2227  alloc = 10;
2228  fields = (debug_field *) xmalloc (alloc * sizeof *fields);
2229  while (**pp != ';')
2230    {
2231      /* FIXME: gdb checks os9k_stabs here.  */
2232
2233      p = *pp;
2234
2235      /* Add 1 to c to leave room for NULL pointer at end.  */
2236      if (c + 1 >= alloc)
2237	{
2238	  alloc += 10;
2239	  fields = ((debug_field *)
2240		    xrealloc ((PTR) fields, alloc * sizeof *fields));
2241	}
2242
2243      /* If it starts with CPLUS_MARKER it is a special abbreviation,
2244	 unless the CPLUS_MARKER is followed by an underscore, in
2245	 which case it is just the name of an anonymous type, which we
2246	 should handle like any other type name.  We accept either '$'
2247	 or '.', because a field name can never contain one of these
2248	 characters except as a CPLUS_MARKER.  */
2249
2250      if ((*p == '$' || *p == '.') && p[1] != '_')
2251	{
2252	  ++*pp;
2253	  if (! parse_stab_cpp_abbrev (dhandle, info, pp, fields + c))
2254	    return false;
2255	  ++c;
2256	  continue;
2257	}
2258
2259      /* Look for the ':' that separates the field name from the field
2260	 values.  Data members are delimited by a single ':', while member
2261	 functions are delimited by a pair of ':'s.  When we hit the member
2262	 functions (if any), terminate scan loop and return. */
2263
2264      p = strchr (p, ':');
2265      if (p == NULL)
2266	{
2267	  bad_stab (orig);
2268	  return false;
2269	}
2270
2271      if (p[1] == ':')
2272	break;
2273
2274      if (! parse_stab_one_struct_field (dhandle, info, pp, p, fields + c,
2275					 staticsp))
2276	return false;
2277
2278      ++c;
2279    }
2280
2281  fields[c] = DEBUG_FIELD_NULL;
2282
2283  *retp = fields;
2284
2285  return true;
2286}
2287
2288/* Special GNU C++ name.  */
2289
2290static boolean
2291parse_stab_cpp_abbrev (dhandle, info, pp, retp)
2292     PTR dhandle;
2293     struct stab_handle *info;
2294     const char **pp;
2295     debug_field *retp;
2296{
2297  const char *orig;
2298  int cpp_abbrev;
2299  debug_type context;
2300  const char *name;
2301  const char *typename;
2302  debug_type type;
2303  bfd_vma bitpos;
2304
2305  *retp = DEBUG_FIELD_NULL;
2306
2307  orig = *pp;
2308
2309  if (**pp != 'v')
2310    {
2311      bad_stab (*pp);
2312      return false;
2313    }
2314  ++*pp;
2315
2316  cpp_abbrev = **pp;
2317  ++*pp;
2318
2319  /* At this point, *pp points to something like "22:23=*22...", where
2320     the type number before the ':' is the "context" and everything
2321     after is a regular type definition.  Lookup the type, find it's
2322     name, and construct the field name.  */
2323
2324  context = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2325			     (debug_type **) NULL);
2326  if (context == DEBUG_TYPE_NULL)
2327    return false;
2328
2329  switch (cpp_abbrev)
2330    {
2331    case 'f':
2332      /* $vf -- a virtual function table pointer.  */
2333      name = "_vptr$";
2334      break;
2335    case 'b':
2336      /* $vb -- a virtual bsomethingorother */
2337      typename = debug_get_type_name (dhandle, context);
2338      if (typename == NULL)
2339	{
2340	  warn_stab (orig, "unnamed $vb type");
2341	  typename = "FOO";
2342	}
2343      name = concat ("_vb$", typename, (const char *) NULL);
2344      break;
2345    default:
2346      warn_stab (orig, "unrecognized C++ abbreviation");
2347      name = "INVALID_CPLUSPLUS_ABBREV";
2348      break;
2349    }
2350
2351  if (**pp != ':')
2352    {
2353      bad_stab (orig);
2354      return false;
2355    }
2356  ++*pp;
2357
2358  type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2359			  (debug_type **) NULL);
2360  if (**pp != ',')
2361    {
2362      bad_stab (orig);
2363      return false;
2364    }
2365  ++*pp;
2366
2367  bitpos = parse_number (pp, (boolean *) NULL);
2368  if (**pp != ';')
2369    {
2370      bad_stab (orig);
2371      return false;
2372    }
2373  ++*pp;
2374
2375  *retp = debug_make_field (dhandle, name, type, bitpos, 0,
2376			    DEBUG_VISIBILITY_PRIVATE);
2377  if (*retp == DEBUG_FIELD_NULL)
2378    return false;
2379
2380  return true;
2381}
2382
2383/* Parse a single field in a struct or union.  */
2384
2385static boolean
2386parse_stab_one_struct_field (dhandle, info, pp, p, retp, staticsp)
2387     PTR dhandle;
2388     struct stab_handle *info;
2389     const char **pp;
2390     const char *p;
2391     debug_field *retp;
2392     boolean *staticsp;
2393{
2394  const char *orig;
2395  char *name;
2396  enum debug_visibility visibility;
2397  debug_type type;
2398  bfd_vma bitpos;
2399  bfd_vma bitsize;
2400
2401  orig = *pp;
2402
2403  /* FIXME: gdb checks ARM_DEMANGLING here.  */
2404
2405  name = savestring (*pp, p - *pp);
2406
2407  *pp = p + 1;
2408
2409  if (**pp != '/')
2410    visibility = DEBUG_VISIBILITY_PUBLIC;
2411  else
2412    {
2413      ++*pp;
2414      switch (**pp)
2415	{
2416	case '0':
2417	  visibility = DEBUG_VISIBILITY_PRIVATE;
2418	  break;
2419	case '1':
2420	  visibility = DEBUG_VISIBILITY_PROTECTED;
2421	  break;
2422	case '2':
2423	  visibility = DEBUG_VISIBILITY_PUBLIC;
2424	  break;
2425	default:
2426	  warn_stab (orig, "unknown visibility character for field");
2427	  visibility = DEBUG_VISIBILITY_PUBLIC;
2428	  break;
2429	}
2430      ++*pp;
2431    }
2432
2433  type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2434			  (debug_type **) NULL);
2435  if (type == DEBUG_TYPE_NULL)
2436    return false;
2437
2438  if (**pp == ':')
2439    {
2440      char *varname;
2441
2442      /* This is a static class member.  */
2443      ++*pp;
2444      p = strchr (*pp, ';');
2445      if (p == NULL)
2446	{
2447	  bad_stab (orig);
2448	  return false;
2449	}
2450
2451      varname = savestring (*pp, p - *pp);
2452
2453      *pp = p + 1;
2454
2455      *retp = debug_make_static_member (dhandle, name, type, varname,
2456					visibility);
2457      *staticsp = true;
2458
2459      return true;
2460    }
2461
2462  if (**pp != ',')
2463    {
2464      bad_stab (orig);
2465      return false;
2466    }
2467  ++*pp;
2468
2469  bitpos = parse_number (pp, (boolean *) NULL);
2470  if (**pp != ',')
2471    {
2472      bad_stab (orig);
2473      return false;
2474    }
2475  ++*pp;
2476
2477  bitsize = parse_number (pp, (boolean *) NULL);
2478  if (**pp != ';')
2479    {
2480      bad_stab (orig);
2481      return false;
2482    }
2483  ++*pp;
2484
2485  if (bitpos == 0 && bitsize == 0)
2486    {
2487      /* This can happen in two cases: (1) at least for gcc 2.4.5 or
2488	 so, it is a field which has been optimized out.  The correct
2489	 stab for this case is to use VISIBILITY_IGNORE, but that is a
2490	 recent invention.  (2) It is a 0-size array.  For example
2491	 union { int num; char str[0]; } foo.  Printing "<no value>"
2492	 for str in "p foo" is OK, since foo.str (and thus foo.str[3])
2493	 will continue to work, and a 0-size array as a whole doesn't
2494	 have any contents to print.
2495
2496	 I suspect this probably could also happen with gcc -gstabs
2497	 (not -gstabs+) for static fields, and perhaps other C++
2498	 extensions.  Hopefully few people use -gstabs with gdb, since
2499	 it is intended for dbx compatibility.  */
2500      visibility = DEBUG_VISIBILITY_IGNORE;
2501    }
2502
2503  /* FIXME: gdb does some stuff here to mark fields as unpacked.  */
2504
2505  *retp = debug_make_field (dhandle, name, type, bitpos, bitsize, visibility);
2506
2507  return true;
2508}
2509
2510/* Read member function stabs info for C++ classes.  The form of each member
2511   function data is:
2512
2513	NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
2514
2515   An example with two member functions is:
2516
2517	afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
2518
2519   For the case of overloaded operators, the format is op$::*.funcs, where
2520   $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
2521   name (such as `+=') and `.' marks the end of the operator name.  */
2522
2523static boolean
2524parse_stab_members (dhandle, info, tagname, pp, typenums, retp)
2525     PTR dhandle;
2526     struct stab_handle *info;
2527     const char *tagname;
2528     const char **pp;
2529     const int *typenums;
2530     debug_method **retp;
2531{
2532  const char *orig;
2533  debug_method *methods;
2534  unsigned int c;
2535  unsigned int alloc;
2536
2537  *retp = NULL;
2538
2539  orig = *pp;
2540
2541  alloc = 0;
2542  methods = NULL;
2543  c = 0;
2544
2545  while (**pp != ';')
2546    {
2547      const char *p;
2548      char *name;
2549      debug_method_variant *variants;
2550      unsigned int cvars;
2551      unsigned int allocvars;
2552      debug_type look_ahead_type;
2553
2554      p = strchr (*pp, ':');
2555      if (p == NULL || p[1] != ':')
2556	break;
2557
2558      /* FIXME: Some systems use something other than '$' here.  */
2559      if ((*pp)[0] != 'o' || (*pp)[1] != 'p' || (*pp)[2] != '$')
2560	{
2561	  name = savestring (*pp, p - *pp);
2562	  *pp = p + 2;
2563	}
2564      else
2565	{
2566	  /* This is a completely wierd case.  In order to stuff in the
2567	     names that might contain colons (the usual name delimiter),
2568	     Mike Tiemann defined a different name format which is
2569	     signalled if the identifier is "op$".  In that case, the
2570	     format is "op$::XXXX." where XXXX is the name.  This is
2571	     used for names like "+" or "=".  YUUUUUUUK!  FIXME!  */
2572	  *pp = p + 2;
2573	  for (p = *pp; *p != '.' && *p != '\0'; p++)
2574	    ;
2575	  if (*p != '.')
2576	    {
2577	      bad_stab (orig);
2578	      return false;
2579	    }
2580	  name = savestring (*pp, p - *pp);
2581	  *pp = p + 1;
2582	}
2583
2584      allocvars = 10;
2585      variants = ((debug_method_variant *)
2586		  xmalloc (allocvars * sizeof *variants));
2587      cvars = 0;
2588
2589      look_ahead_type = DEBUG_TYPE_NULL;
2590
2591      do
2592	{
2593	  debug_type type;
2594	  boolean stub;
2595	  char *argtypes;
2596	  enum debug_visibility visibility;
2597	  boolean constp, volatilep, staticp;
2598	  bfd_vma voffset;
2599	  debug_type context;
2600	  const char *physname;
2601	  boolean varargs;
2602
2603	  if (look_ahead_type != DEBUG_TYPE_NULL)
2604	    {
2605	      /* g++ version 1 kludge */
2606	      type = look_ahead_type;
2607	      look_ahead_type = DEBUG_TYPE_NULL;
2608	    }
2609	  else
2610	    {
2611	      type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2612				      (debug_type **) NULL);
2613	      if (type == DEBUG_TYPE_NULL)
2614		return false;
2615	      if (**pp != ':')
2616		{
2617		  bad_stab (orig);
2618		  return false;
2619		}
2620	    }
2621
2622	  ++*pp;
2623	  p = strchr (*pp, ';');
2624	  if (p == NULL)
2625	    {
2626	      bad_stab (orig);
2627	      return false;
2628	    }
2629
2630	  stub = false;
2631	  if (debug_get_type_kind (dhandle, type) == DEBUG_KIND_METHOD
2632	      && debug_get_parameter_types (dhandle, type, &varargs) == NULL)
2633	    stub = true;
2634
2635	  argtypes = savestring (*pp, p - *pp);
2636	  *pp = p + 1;
2637
2638	  switch (**pp)
2639	    {
2640	    case '0':
2641	      visibility = DEBUG_VISIBILITY_PRIVATE;
2642	      break;
2643	    case '1':
2644	      visibility = DEBUG_VISIBILITY_PROTECTED;
2645	      break;
2646	    default:
2647	      visibility = DEBUG_VISIBILITY_PUBLIC;
2648	      break;
2649	    }
2650	  ++*pp;
2651
2652	  constp = false;
2653	  volatilep = false;
2654	  switch (**pp)
2655	    {
2656	    case 'A':
2657	      /* Normal function.  */
2658	      ++*pp;
2659	      break;
2660	    case 'B':
2661	      /* const member function.  */
2662	      constp = true;
2663	      ++*pp;
2664	      break;
2665	    case 'C':
2666	      /* volatile member function.  */
2667	      volatilep = true;
2668	      ++*pp;
2669	      break;
2670	    case 'D':
2671	      /* const volatile member function.  */
2672	      constp = true;
2673	      volatilep = true;
2674	      ++*pp;
2675	      break;
2676	    case '*':
2677	    case '?':
2678	    case '.':
2679	      /* File compiled with g++ version 1; no information.  */
2680	      break;
2681	    default:
2682	      warn_stab (orig, "const/volatile indicator missing");
2683	      break;
2684	    }
2685
2686	  staticp = false;
2687	  switch (**pp)
2688	    {
2689	    case '*':
2690	      /* virtual member function, followed by index.  The sign
2691		 bit is supposedly set to distinguish
2692		 pointers-to-methods from virtual function indicies.  */
2693	      ++*pp;
2694	      voffset = parse_number (pp, (boolean *) NULL);
2695	      if (**pp != ';')
2696		{
2697		  bad_stab (orig);
2698		  return false;
2699		}
2700	      ++*pp;
2701	      voffset &= 0x7fffffff;
2702
2703	      if (**pp == ';' || *pp == '\0')
2704		{
2705		  /* Must be g++ version 1.  */
2706		  context = DEBUG_TYPE_NULL;
2707		}
2708	      else
2709		{
2710		  /* Figure out from whence this virtual function
2711		     came.  It may belong to virtual function table of
2712		     one of its baseclasses.  */
2713		    look_ahead_type = parse_stab_type (dhandle, info,
2714						       (const char *) NULL,
2715						       pp,
2716						       (debug_type **) NULL);
2717		    if (**pp == ':')
2718		      {
2719			/* g++ version 1 overloaded methods.  */
2720			context = DEBUG_TYPE_NULL;
2721		      }
2722		    else
2723		      {
2724			context = look_ahead_type;
2725			look_ahead_type = DEBUG_TYPE_NULL;
2726			if (**pp != ';')
2727			  {
2728			    bad_stab (orig);
2729			    return false;
2730			  }
2731			++*pp;
2732		      }
2733		  }
2734	      break;
2735
2736	    case '?':
2737	      /* static member function.  */
2738	      ++*pp;
2739	      staticp = true;
2740	      voffset = 0;
2741	      context = DEBUG_TYPE_NULL;
2742	      if (strncmp (argtypes, name, strlen (name)) != 0)
2743		stub = true;
2744	      break;
2745
2746	    default:
2747	      warn_stab (orig, "member function type missing");
2748	      voffset = 0;
2749	      context = DEBUG_TYPE_NULL;
2750	      break;
2751
2752	    case '.':
2753	      ++*pp;
2754	      voffset = 0;
2755	      context = DEBUG_TYPE_NULL;
2756	      break;
2757	    }
2758
2759	  /* If the type is not a stub, then the argtypes string is
2760             the physical name of the function.  Otherwise the
2761             argtypes string is the mangled form of the argument
2762             types, and the full type and the physical name must be
2763             extracted from them.  */
2764	  if (! stub)
2765	    physname = argtypes;
2766	  else
2767	    {
2768	      debug_type class_type, return_type;
2769
2770	      class_type = stab_find_type (dhandle, info, typenums);
2771	      if (class_type == DEBUG_TYPE_NULL)
2772		return false;
2773	      return_type = debug_get_return_type (dhandle, type);
2774	      if (return_type == DEBUG_TYPE_NULL)
2775		{
2776		  bad_stab (orig);
2777		  return false;
2778		}
2779	      type = parse_stab_argtypes (dhandle, info, class_type, name,
2780					  tagname, return_type, argtypes,
2781					  constp, volatilep, &physname);
2782	      if (type == DEBUG_TYPE_NULL)
2783		return false;
2784	    }
2785
2786	  if (cvars + 1 >= allocvars)
2787	    {
2788	      allocvars += 10;
2789	      variants = ((debug_method_variant *)
2790			  xrealloc ((PTR) variants,
2791				    allocvars * sizeof *variants));
2792	    }
2793
2794	  if (! staticp)
2795	    variants[cvars] = debug_make_method_variant (dhandle, physname,
2796							 type, visibility,
2797							 constp, volatilep,
2798							 voffset, context);
2799	  else
2800	    variants[cvars] = debug_make_static_method_variant (dhandle,
2801								physname,
2802								type,
2803								visibility,
2804								constp,
2805								volatilep);
2806	  if (variants[cvars] == DEBUG_METHOD_VARIANT_NULL)
2807	    return false;
2808
2809	  ++cvars;
2810	}
2811      while (**pp != ';' && **pp != '\0');
2812
2813      variants[cvars] = DEBUG_METHOD_VARIANT_NULL;
2814
2815      if (**pp != '\0')
2816	++*pp;
2817
2818      if (c + 1 >= alloc)
2819	{
2820	  alloc += 10;
2821	  methods = ((debug_method *)
2822		     xrealloc ((PTR) methods, alloc * sizeof *methods));
2823	}
2824
2825      methods[c] = debug_make_method (dhandle, name, variants);
2826
2827      ++c;
2828    }
2829
2830  if (methods != NULL)
2831    methods[c] = DEBUG_METHOD_NULL;
2832
2833  *retp = methods;
2834
2835  return true;
2836}
2837
2838/* Parse a string representing argument types for a method.  Stabs
2839   tries to save space by packing argument types into a mangled
2840   string.  This string should give us enough information to extract
2841   both argument types and the physical name of the function, given
2842   the tag name.  */
2843
2844static debug_type
2845parse_stab_argtypes (dhandle, info, class_type, fieldname, tagname,
2846		     return_type, argtypes, constp, volatilep, pphysname)
2847     PTR dhandle;
2848     struct stab_handle *info;
2849     debug_type class_type;
2850     const char *fieldname;
2851     const char *tagname;
2852     debug_type return_type;
2853     const char *argtypes;
2854     boolean constp;
2855     boolean volatilep;
2856     const char **pphysname;
2857{
2858  boolean is_full_physname_constructor;
2859  boolean is_constructor;
2860  boolean is_destructor;
2861  debug_type *args;
2862  boolean varargs;
2863
2864  /* Constructors are sometimes handled specially.  */
2865  is_full_physname_constructor = ((argtypes[0] == '_'
2866				   && argtypes[1] == '_'
2867				   && (isdigit ((unsigned char) argtypes[2])
2868				       || argtypes[2] == 'Q'
2869				       || argtypes[2] == 't'))
2870				  || strncmp (argtypes, "__ct", 4) == 0);
2871
2872  is_constructor = (is_full_physname_constructor
2873		    || (tagname != NULL
2874			&& strcmp (fieldname, tagname) == 0));
2875  is_destructor = ((argtypes[0] == '_'
2876		    && (argtypes[1] == '$' || argtypes[1] == '.')
2877		    && argtypes[2] == '_')
2878		   || strncmp (argtypes, "__dt", 4) == 0);
2879
2880  if (is_destructor || is_full_physname_constructor)
2881    *pphysname = argtypes;
2882  else
2883    {
2884      unsigned int len;
2885      const char *const_prefix;
2886      const char *volatile_prefix;
2887      char buf[20];
2888      unsigned int mangled_name_len;
2889      char *physname;
2890
2891      len = tagname == NULL ? 0 : strlen (tagname);
2892      const_prefix = constp ? "C" : "";
2893      volatile_prefix = volatilep ? "V" : "";
2894
2895      if (len == 0)
2896	sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
2897      else if (tagname != NULL && strchr (tagname, '<') != NULL)
2898	{
2899	  /* Template methods are fully mangled.  */
2900	  sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
2901	  tagname = NULL;
2902	  len = 0;
2903	}
2904      else
2905	sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
2906
2907      mangled_name_len = ((is_constructor ? 0 : strlen (fieldname))
2908			  + strlen (buf)
2909			  + len
2910			  + strlen (argtypes)
2911			  + 1);
2912
2913      if (fieldname[0] == 'o'
2914	  && fieldname[1] == 'p'
2915	  && (fieldname[2] == '$' || fieldname[2] == '.'))
2916	{
2917	  const char *opname;
2918
2919	  opname = cplus_mangle_opname (fieldname + 3, 0);
2920	  if (opname == NULL)
2921	    {
2922	      fprintf (stderr, "No mangling for \"%s\"\n", fieldname);
2923	      return DEBUG_TYPE_NULL;
2924	    }
2925	  mangled_name_len += strlen (opname);
2926	  physname = (char *) xmalloc (mangled_name_len);
2927	  strncpy (physname, fieldname, 3);
2928	  strcpy (physname + 3, opname);
2929	}
2930      else
2931	{
2932	  physname = (char *) xmalloc (mangled_name_len);
2933	  if (is_constructor)
2934	    physname[0] = '\0';
2935	  else
2936	    strcpy (physname, fieldname);
2937	}
2938
2939      strcat (physname, buf);
2940      if (tagname != NULL)
2941	strcat (physname, tagname);
2942      strcat (physname, argtypes);
2943
2944      *pphysname = physname;
2945    }
2946
2947  if (*argtypes == '\0')
2948    {
2949      args = (debug_type *) xmalloc (sizeof *args);
2950      *args = NULL;
2951      return debug_make_method_type (dhandle, return_type, class_type, args,
2952				     false);
2953    }
2954
2955  args = stab_demangle_argtypes (dhandle, info, *pphysname, &varargs);
2956  if (args == NULL)
2957    return DEBUG_TYPE_NULL;
2958
2959  return debug_make_method_type (dhandle, return_type, class_type, args,
2960				 varargs);
2961}
2962
2963/* The tail end of stabs for C++ classes that contain a virtual function
2964   pointer contains a tilde, a %, and a type number.
2965   The type number refers to the base class (possibly this class itself) which
2966   contains the vtable pointer for the current class.
2967
2968   This function is called when we have parsed all the method declarations,
2969   so we can look for the vptr base class info.  */
2970
2971static boolean
2972parse_stab_tilde_field (dhandle, info, pp, typenums, retvptrbase, retownvptr)
2973     PTR dhandle;
2974     struct stab_handle *info;
2975     const char **pp;
2976     const int *typenums;
2977     debug_type *retvptrbase;
2978     boolean *retownvptr;
2979{
2980  const char *orig;
2981  const char *hold;
2982  int vtypenums[2];
2983
2984  *retvptrbase = DEBUG_TYPE_NULL;
2985  *retownvptr = false;
2986
2987  orig = *pp;
2988
2989  /* If we are positioned at a ';', then skip it. */
2990  if (**pp == ';')
2991    ++*pp;
2992
2993  if (**pp != '~')
2994    return true;
2995
2996  ++*pp;
2997
2998  if (**pp == '=' || **pp == '+' || **pp == '-')
2999    {
3000      /* Obsolete flags that used to indicate the presence of
3001	 constructors and/or destructors. */
3002      ++*pp;
3003    }
3004
3005  if (**pp != '%')
3006    return true;
3007
3008  ++*pp;
3009
3010  hold = *pp;
3011
3012  /* The next number is the type number of the base class (possibly
3013     our own class) which supplies the vtable for this class.  */
3014  if (! parse_stab_type_number (pp, vtypenums))
3015    return false;
3016
3017  if (vtypenums[0] == typenums[0]
3018      && vtypenums[1] == typenums[1])
3019    *retownvptr = true;
3020  else
3021    {
3022      debug_type vtype;
3023      const char *p;
3024
3025      *pp = hold;
3026
3027      vtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3028			       (debug_type **) NULL);
3029      for (p = *pp; *p != ';' && *p != '\0'; p++)
3030	;
3031      if (*p != ';')
3032	{
3033	  bad_stab (orig);
3034	  return false;
3035	}
3036
3037      *retvptrbase = vtype;
3038
3039      *pp = p + 1;
3040    }
3041
3042  return true;
3043}
3044
3045/* Read a definition of an array type.  */
3046
3047static debug_type
3048parse_stab_array_type (dhandle, info, pp, stringp)
3049     PTR dhandle;
3050     struct stab_handle *info;
3051     const char **pp;
3052     boolean stringp;
3053{
3054  const char *orig;
3055  const char *p;
3056  int typenums[2];
3057  debug_type index_type;
3058  boolean adjustable;
3059  bfd_signed_vma lower, upper;
3060  debug_type element_type;
3061
3062  /* Format of an array type:
3063     "ar<index type>;lower;upper;<array_contents_type>".
3064     OS9000: "arlower,upper;<array_contents_type>".
3065
3066     Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
3067     for these, produce a type like float[][].  */
3068
3069  orig = *pp;
3070
3071  /* FIXME: gdb checks os9k_stabs here.  */
3072
3073  /* If the index type is type 0, we take it as int.  */
3074  p = *pp;
3075  if (! parse_stab_type_number (&p, typenums))
3076    return false;
3077  if (typenums[0] == 0 && typenums[1] == 0 && **pp != '=')
3078    {
3079      index_type = debug_find_named_type (dhandle, "int");
3080      if (index_type == DEBUG_TYPE_NULL)
3081	{
3082	  index_type = debug_make_int_type (dhandle, 4, false);
3083	  if (index_type == DEBUG_TYPE_NULL)
3084	    return false;
3085	}
3086      *pp = p;
3087    }
3088  else
3089    {
3090      index_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3091				    (debug_type **) NULL);
3092    }
3093
3094  if (**pp != ';')
3095    {
3096      bad_stab (orig);
3097      return DEBUG_TYPE_NULL;
3098    }
3099  ++*pp;
3100
3101  adjustable = false;
3102
3103  if (! isdigit ((unsigned char) **pp) && **pp != '-')
3104    {
3105      ++*pp;
3106      adjustable = true;
3107    }
3108
3109  lower = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
3110  if (**pp != ';')
3111    {
3112      bad_stab (orig);
3113      return false;
3114    }
3115  ++*pp;
3116
3117  if (! isdigit ((unsigned char) **pp) && **pp != '-')
3118    {
3119      ++*pp;
3120      adjustable = true;
3121    }
3122
3123  upper = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
3124  if (**pp != ';')
3125    {
3126      bad_stab (orig);
3127      return false;
3128    }
3129  ++*pp;
3130
3131  element_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3132				  (debug_type **) NULL);
3133  if (element_type == DEBUG_TYPE_NULL)
3134    return false;
3135
3136  if (adjustable)
3137    {
3138      lower = 0;
3139      upper = -1;
3140    }
3141
3142  return debug_make_array_type (dhandle, element_type, index_type, lower,
3143				upper, stringp);
3144}
3145
3146/* This struct holds information about files we have seen using
3147   N_BINCL.  */
3148
3149struct bincl_file
3150{
3151  /* The next N_BINCL file.  */
3152  struct bincl_file *next;
3153  /* The next N_BINCL on the stack.  */
3154  struct bincl_file *next_stack;
3155  /* The file name.  */
3156  const char *name;
3157  /* The hash value.  */
3158  bfd_vma hash;
3159  /* The file index.  */
3160  unsigned int file;
3161  /* The list of types defined in this file.  */
3162  struct stab_types *file_types;
3163};
3164
3165/* Start a new N_BINCL file, pushing it onto the stack.  */
3166
3167static void
3168push_bincl (info, name, hash)
3169     struct stab_handle *info;
3170     const char *name;
3171     bfd_vma hash;
3172{
3173  struct bincl_file *n;
3174
3175  n = (struct bincl_file *) xmalloc (sizeof *n);
3176  n->next = info->bincl_list;
3177  n->next_stack = info->bincl_stack;
3178  n->name = name;
3179  n->hash = hash;
3180  n->file = info->files;
3181  n->file_types = NULL;
3182  info->bincl_list = n;
3183  info->bincl_stack = n;
3184
3185  ++info->files;
3186  info->file_types = ((struct stab_types **)
3187		      xrealloc ((PTR) info->file_types,
3188				(info->files
3189				 * sizeof *info->file_types)));
3190  info->file_types[n->file] = NULL;
3191}
3192
3193/* Finish an N_BINCL file, at an N_EINCL, popping the name off the
3194   stack.  */
3195
3196static const char *
3197pop_bincl (info)
3198     struct stab_handle *info;
3199{
3200  struct bincl_file *o;
3201
3202  o = info->bincl_stack;
3203  if (o == NULL)
3204    return info->main_filename;
3205  info->bincl_stack = o->next_stack;
3206
3207  o->file_types = info->file_types[o->file];
3208
3209  if (info->bincl_stack == NULL)
3210    return info->main_filename;
3211  return info->bincl_stack->name;
3212}
3213
3214/* Handle an N_EXCL: get the types from the corresponding N_BINCL.  */
3215
3216static boolean
3217find_excl (info, name, hash)
3218     struct stab_handle *info;
3219     const char *name;
3220     bfd_vma hash;
3221{
3222  struct bincl_file *l;
3223
3224  ++info->files;
3225  info->file_types = ((struct stab_types **)
3226		      xrealloc ((PTR) info->file_types,
3227				(info->files
3228				 * sizeof *info->file_types)));
3229
3230  for (l = info->bincl_list; l != NULL; l = l->next)
3231    if (l->hash == hash && strcmp (l->name, name) == 0)
3232      break;
3233  if (l == NULL)
3234    {
3235      warn_stab (name, "Undefined N_EXCL");
3236      info->file_types[info->files - 1] = NULL;
3237      return true;
3238    }
3239
3240  info->file_types[info->files - 1] = l->file_types;
3241
3242  return true;
3243}
3244
3245/* Handle a variable definition.  gcc emits variable definitions for a
3246   block before the N_LBRAC, so we must hold onto them until we see
3247   it.  The SunPRO compiler emits variable definitions after the
3248   N_LBRAC, so we can call debug_record_variable immediately.  */
3249
3250static boolean
3251stab_record_variable (dhandle, info, name, type, kind, val)
3252     PTR dhandle;
3253     struct stab_handle *info;
3254     const char *name;
3255     debug_type type;
3256     enum debug_var_kind kind;
3257     bfd_vma val;
3258{
3259  struct stab_pending_var *v;
3260
3261  if ((kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
3262      || ! info->within_function
3263      || (info->gcc_compiled == 0 && info->n_opt_found))
3264    return debug_record_variable (dhandle, name, type, kind, val);
3265
3266  v = (struct stab_pending_var *) xmalloc (sizeof *v);
3267  memset (v, 0, sizeof *v);
3268
3269  v->next = info->pending;
3270  v->name = name;
3271  v->type = type;
3272  v->kind = kind;
3273  v->val = val;
3274  info->pending = v;
3275
3276  return true;
3277}
3278
3279/* Emit pending variable definitions.  This is called after we see the
3280   N_LBRAC that starts the block.  */
3281
3282static boolean
3283stab_emit_pending_vars (dhandle, info)
3284     PTR dhandle;
3285     struct stab_handle *info;
3286{
3287  struct stab_pending_var *v;
3288
3289  v = info->pending;
3290  while (v != NULL)
3291    {
3292      struct stab_pending_var *next;
3293
3294      if (! debug_record_variable (dhandle, v->name, v->type, v->kind, v->val))
3295	return false;
3296
3297      next = v->next;
3298      free (v);
3299      v = next;
3300    }
3301
3302  info->pending = NULL;
3303
3304  return true;
3305}
3306
3307/* Find the slot for a type in the database.  */
3308
3309static debug_type *
3310stab_find_slot (info, typenums)
3311     struct stab_handle *info;
3312     const int *typenums;
3313{
3314  int filenum;
3315  int index;
3316  struct stab_types **ps;
3317
3318  filenum = typenums[0];
3319  index = typenums[1];
3320
3321  if (filenum < 0 || (unsigned int) filenum >= info->files)
3322    {
3323      fprintf (stderr, "Type file number %d out of range\n", filenum);
3324      return NULL;
3325    }
3326  if (index < 0)
3327    {
3328      fprintf (stderr, "Type index number %d out of range\n", index);
3329      return NULL;
3330    }
3331
3332  ps = info->file_types + filenum;
3333
3334  while (index >= STAB_TYPES_SLOTS)
3335    {
3336      if (*ps == NULL)
3337	{
3338	  *ps = (struct stab_types *) xmalloc (sizeof **ps);
3339	  memset (*ps, 0, sizeof **ps);
3340	}
3341      ps = &(*ps)->next;
3342      index -= STAB_TYPES_SLOTS;
3343    }
3344  if (*ps == NULL)
3345    {
3346      *ps = (struct stab_types *) xmalloc (sizeof **ps);
3347      memset (*ps, 0, sizeof **ps);
3348    }
3349
3350  return (*ps)->types + index;
3351}
3352
3353/* Find a type given a type number.  If the type has not been
3354   allocated yet, create an indirect type.  */
3355
3356static debug_type
3357stab_find_type (dhandle, info, typenums)
3358     PTR dhandle;
3359     struct stab_handle *info;
3360     const int *typenums;
3361{
3362  debug_type *slot;
3363
3364  if (typenums[0] == 0 && typenums[1] < 0)
3365    {
3366      /* A negative type number indicates an XCOFF builtin type.  */
3367      return stab_xcoff_builtin_type (dhandle, info, typenums[1]);
3368    }
3369
3370  slot = stab_find_slot (info, typenums);
3371  if (slot == NULL)
3372    return DEBUG_TYPE_NULL;
3373
3374  if (*slot == DEBUG_TYPE_NULL)
3375    return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
3376
3377  return *slot;
3378}
3379
3380/* Record that a given type number refers to a given type.  */
3381
3382static boolean
3383stab_record_type (dhandle, info, typenums, type)
3384     PTR dhandle;
3385     struct stab_handle *info;
3386     const int *typenums;
3387     debug_type type;
3388{
3389  debug_type *slot;
3390
3391  slot = stab_find_slot (info, typenums);
3392  if (slot == NULL)
3393    return false;
3394
3395  /* gdb appears to ignore type redefinitions, so we do as well.  */
3396
3397  *slot = type;
3398
3399  return true;
3400}
3401
3402/* Return an XCOFF builtin type.  */
3403
3404static debug_type
3405stab_xcoff_builtin_type (dhandle, info, typenum)
3406     PTR dhandle;
3407     struct stab_handle *info;
3408     int typenum;
3409{
3410  debug_type rettype;
3411  const char *name;
3412
3413  if (typenum >= 0 || typenum < -XCOFF_TYPE_COUNT)
3414    {
3415      fprintf (stderr, "Unrecognized XCOFF type %d\n", typenum);
3416      return DEBUG_TYPE_NULL;
3417    }
3418  if (info->xcoff_types[-typenum] != NULL)
3419    return info->xcoff_types[-typenum];
3420
3421  switch (-typenum)
3422    {
3423    case 1:
3424      /* The size of this and all the other types are fixed, defined
3425	 by the debugging format.  */
3426      name = "int";
3427      rettype = debug_make_int_type (dhandle, 4, false);
3428      break;
3429    case 2:
3430      name = "char";
3431      rettype = debug_make_int_type (dhandle, 1, false);
3432      break;
3433    case 3:
3434      name = "short";
3435      rettype = debug_make_int_type (dhandle, 2, false);
3436      break;
3437    case 4:
3438      name = "long";
3439      rettype = debug_make_int_type (dhandle, 4, false);
3440      break;
3441    case 5:
3442      name = "unsigned char";
3443      rettype = debug_make_int_type (dhandle, 1, true);
3444      break;
3445    case 6:
3446      name = "signed char";
3447      rettype = debug_make_int_type (dhandle, 1, false);
3448      break;
3449    case 7:
3450      name = "unsigned short";
3451      rettype = debug_make_int_type (dhandle, 2, true);
3452      break;
3453    case 8:
3454      name = "unsigned int";
3455      rettype = debug_make_int_type (dhandle, 4, true);
3456      break;
3457    case 9:
3458      name = "unsigned";
3459      rettype = debug_make_int_type (dhandle, 4, true);
3460    case 10:
3461      name = "unsigned long";
3462      rettype = debug_make_int_type (dhandle, 4, true);
3463      break;
3464    case 11:
3465      name = "void";
3466      rettype = debug_make_void_type (dhandle);
3467      break;
3468    case 12:
3469      /* IEEE single precision (32 bit).  */
3470      name = "float";
3471      rettype = debug_make_float_type (dhandle, 4);
3472      break;
3473    case 13:
3474      /* IEEE double precision (64 bit).  */
3475      name = "double";
3476      rettype = debug_make_float_type (dhandle, 8);
3477      break;
3478    case 14:
3479      /* This is an IEEE double on the RS/6000, and different machines
3480	 with different sizes for "long double" should use different
3481	 negative type numbers.  See stabs.texinfo.  */
3482      name = "long double";
3483      rettype = debug_make_float_type (dhandle, 8);
3484      break;
3485    case 15:
3486      name = "integer";
3487      rettype = debug_make_int_type (dhandle, 4, false);
3488      break;
3489    case 16:
3490      name = "boolean";
3491      rettype = debug_make_bool_type (dhandle, 4);
3492      break;
3493    case 17:
3494      name = "short real";
3495      rettype = debug_make_float_type (dhandle, 4);
3496      break;
3497    case 18:
3498      name = "real";
3499      rettype = debug_make_float_type (dhandle, 8);
3500      break;
3501    case 19:
3502      /* FIXME */
3503      name = "stringptr";
3504      rettype = NULL;
3505      break;
3506    case 20:
3507      /* FIXME */
3508      name = "character";
3509      rettype = debug_make_int_type (dhandle, 1, true);
3510      break;
3511    case 21:
3512      name = "logical*1";
3513      rettype = debug_make_bool_type (dhandle, 1);
3514      break;
3515    case 22:
3516      name = "logical*2";
3517      rettype = debug_make_bool_type (dhandle, 2);
3518      break;
3519    case 23:
3520      name = "logical*4";
3521      rettype = debug_make_bool_type (dhandle, 4);
3522      break;
3523    case 24:
3524      name = "logical";
3525      rettype = debug_make_bool_type (dhandle, 4);
3526      break;
3527    case 25:
3528      /* Complex type consisting of two IEEE single precision values.  */
3529      name = "complex";
3530      rettype = debug_make_complex_type (dhandle, 8);
3531      break;
3532    case 26:
3533      /* Complex type consisting of two IEEE double precision values.  */
3534      name = "double complex";
3535      rettype = debug_make_complex_type (dhandle, 16);
3536      break;
3537    case 27:
3538      name = "integer*1";
3539      rettype = debug_make_int_type (dhandle, 1, false);
3540      break;
3541    case 28:
3542      name = "integer*2";
3543      rettype = debug_make_int_type (dhandle, 2, false);
3544      break;
3545    case 29:
3546      name = "integer*4";
3547      rettype = debug_make_int_type (dhandle, 4, false);
3548      break;
3549    case 30:
3550      /* FIXME */
3551      name = "wchar";
3552      rettype = debug_make_int_type (dhandle, 2, false);
3553      break;
3554    case 31:
3555      name = "long long";
3556      rettype = debug_make_int_type (dhandle, 8, false);
3557      break;
3558    case 32:
3559      name = "unsigned long long";
3560      rettype = debug_make_int_type (dhandle, 8, true);
3561      break;
3562    case 33:
3563      name = "logical*8";
3564      rettype = debug_make_bool_type (dhandle, 8);
3565      break;
3566    case 34:
3567      name = "integer*8";
3568      rettype = debug_make_int_type (dhandle, 8, false);
3569      break;
3570    default:
3571      abort ();
3572    }
3573
3574  rettype = debug_name_type (dhandle, name, rettype);
3575
3576  info->xcoff_types[-typenum] = rettype;
3577
3578  return rettype;
3579}
3580
3581/* Find or create a tagged type.  */
3582
3583static debug_type
3584stab_find_tagged_type (dhandle, info, p, len, kind)
3585     PTR dhandle;
3586     struct stab_handle *info;
3587     const char *p;
3588     int len;
3589     enum debug_type_kind kind;
3590{
3591  char *name;
3592  debug_type dtype;
3593  struct stab_tag *st;
3594
3595  name = savestring (p, len);
3596
3597  /* We pass DEBUG_KIND_ILLEGAL because we want all tags in the same
3598     namespace.  This is right for C, and I don't know how to handle
3599     other languages.  FIXME.  */
3600  dtype = debug_find_tagged_type (dhandle, name, DEBUG_KIND_ILLEGAL);
3601  if (dtype != DEBUG_TYPE_NULL)
3602    {
3603      free (name);
3604      return dtype;
3605    }
3606
3607  /* We need to allocate an entry on the undefined tag list.  */
3608  for (st = info->tags; st != NULL; st = st->next)
3609    {
3610      if (st->name[0] == name[0]
3611	  && strcmp (st->name, name) == 0)
3612	{
3613	  if (st->kind == DEBUG_KIND_ILLEGAL)
3614	    st->kind = kind;
3615	  free (name);
3616	  break;
3617	}
3618    }
3619  if (st == NULL)
3620    {
3621      st = (struct stab_tag *) xmalloc (sizeof *st);
3622      memset (st, 0, sizeof *st);
3623
3624      st->next = info->tags;
3625      st->name = name;
3626      st->kind = kind;
3627      st->slot = DEBUG_TYPE_NULL;
3628      st->type = debug_make_indirect_type (dhandle, &st->slot, name);
3629      info->tags = st;
3630    }
3631
3632  return st->type;
3633}
3634
3635/* In order to get the correct argument types for a stubbed method, we
3636   need to extract the argument types from a C++ mangled string.
3637   Since the argument types can refer back to the return type, this
3638   means that we must demangle the entire physical name.  In gdb this
3639   is done by calling cplus_demangle and running the results back
3640   through the C++ expression parser.  Since we have no expression
3641   parser, we must duplicate much of the work of cplus_demangle here.
3642
3643   We assume that GNU style demangling is used, since this is only
3644   done for method stubs, and only g++ should output that form of
3645   debugging information.  */
3646
3647/* This structure is used to hold a pointer to type information which
3648   demangling a string.  */
3649
3650struct stab_demangle_typestring
3651{
3652  /* The start of the type.  This is not null terminated.  */
3653  const char *typestring;
3654  /* The length of the type.  */
3655  unsigned int len;
3656};
3657
3658/* This structure is used to hold information while demangling a
3659   string.  */
3660
3661struct stab_demangle_info
3662{
3663  /* The debugging information handle.  */
3664  PTR dhandle;
3665  /* The stab information handle.  */
3666  struct stab_handle *info;
3667  /* The array of arguments we are building.  */
3668  debug_type *args;
3669  /* Whether the method takes a variable number of arguments.  */
3670  boolean varargs;
3671  /* The array of types we have remembered.  */
3672  struct stab_demangle_typestring *typestrings;
3673  /* The number of typestrings.  */
3674  unsigned int typestring_count;
3675  /* The number of typestring slots we have allocated.  */
3676  unsigned int typestring_alloc;
3677};
3678
3679static void stab_bad_demangle PARAMS ((const char *));
3680static unsigned int stab_demangle_count PARAMS ((const char **));
3681static boolean stab_demangle_get_count
3682  PARAMS ((const char **, unsigned int *));
3683static boolean stab_demangle_prefix
3684  PARAMS ((struct stab_demangle_info *, const char **));
3685static boolean stab_demangle_function_name
3686  PARAMS ((struct stab_demangle_info *, const char **, const char *));
3687static boolean stab_demangle_signature
3688  PARAMS ((struct stab_demangle_info *, const char **));
3689static boolean stab_demangle_qualified
3690  PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3691static boolean stab_demangle_template
3692  PARAMS ((struct stab_demangle_info *, const char **));
3693static boolean stab_demangle_class
3694  PARAMS ((struct stab_demangle_info *, const char **, const char **));
3695static boolean stab_demangle_args
3696  PARAMS ((struct stab_demangle_info *, const char **, debug_type **,
3697	   boolean *));
3698static boolean stab_demangle_arg
3699  PARAMS ((struct stab_demangle_info *, const char **, debug_type **,
3700	   unsigned int *, unsigned int *));
3701static boolean stab_demangle_type
3702  PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3703static boolean stab_demangle_fund_type
3704  PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3705static boolean stab_demangle_remember_type
3706  PARAMS ((struct stab_demangle_info *, const char *, int));
3707
3708/* Warn about a bad demangling.  */
3709
3710static void
3711stab_bad_demangle (s)
3712     const char *s;
3713{
3714  fprintf (stderr, "bad mangled name `%s'\n", s);
3715}
3716
3717/* Get a count from a stab string.  */
3718
3719static unsigned int
3720stab_demangle_count (pp)
3721     const char **pp;
3722{
3723  unsigned int count;
3724
3725  count = 0;
3726  while (isdigit ((unsigned char) **pp))
3727    {
3728      count *= 10;
3729      count += **pp - '0';
3730      ++*pp;
3731    }
3732  return count;
3733}
3734
3735/* Require a count in a string.  The count may be multiple digits, in
3736   which case it must end in an underscore.  */
3737
3738static boolean
3739stab_demangle_get_count (pp, pi)
3740     const char **pp;
3741     unsigned int *pi;
3742{
3743  if (! isdigit ((unsigned char) **pp))
3744    return false;
3745
3746  *pi = **pp - '0';
3747  ++*pp;
3748  if (isdigit ((unsigned char) **pp))
3749    {
3750      unsigned int count;
3751      const char *p;
3752
3753      count = *pi;
3754      p = *pp;
3755      do
3756	{
3757	  count *= 10;
3758	  count += *p - '0';
3759	  ++p;
3760	}
3761      while (isdigit ((unsigned char) *p));
3762      if (*p == '_')
3763	{
3764	  *pp = p + 1;
3765	  *pi = count;
3766	}
3767    }
3768
3769  return true;
3770}
3771
3772/* This function demangles a physical name, returning a NULL
3773   terminated array of argument types.  */
3774
3775static debug_type *
3776stab_demangle_argtypes (dhandle, info, physname, pvarargs)
3777     PTR dhandle;
3778     struct stab_handle *info;
3779     const char *physname;
3780     boolean *pvarargs;
3781{
3782  struct stab_demangle_info minfo;
3783
3784  minfo.dhandle = dhandle;
3785  minfo.info = info;
3786  minfo.args = NULL;
3787  minfo.varargs = false;
3788  minfo.typestring_alloc = 10;
3789  minfo.typestrings = ((struct stab_demangle_typestring *)
3790		       xmalloc (minfo.typestring_alloc
3791				* sizeof *minfo.typestrings));
3792  minfo.typestring_count = 0;
3793
3794  /* cplus_demangle checks for special GNU mangled forms, but we can't
3795     see any of them in mangled method argument types.  */
3796
3797  if (! stab_demangle_prefix (&minfo, &physname))
3798    goto error_return;
3799
3800  if (*physname != '\0')
3801    {
3802      if (! stab_demangle_signature (&minfo, &physname))
3803	goto error_return;
3804    }
3805
3806  free (minfo.typestrings);
3807  minfo.typestrings = NULL;
3808
3809  if (minfo.args == NULL)
3810    fprintf (stderr, "no argument types in mangled string\n");
3811
3812  *pvarargs = minfo.varargs;
3813  return minfo.args;
3814
3815 error_return:
3816  if (minfo.typestrings != NULL)
3817    free (minfo.typestrings);
3818  return NULL;
3819}
3820
3821/* Demangle the prefix of the mangled name.  */
3822
3823static boolean
3824stab_demangle_prefix (minfo, pp)
3825     struct stab_demangle_info *minfo;
3826     const char **pp;
3827{
3828  const char *scan;
3829  unsigned int i;
3830
3831  /* cplus_demangle checks for global constructors and destructors,
3832     but we can't see them in mangled argument types.  */
3833
3834  /* Look for `__'.  */
3835  scan = *pp;
3836  do
3837    {
3838      scan = strchr (scan, '_');
3839    }
3840  while (scan != NULL && *++scan != '_');
3841
3842  if (scan == NULL)
3843    {
3844      stab_bad_demangle (*pp);
3845      return false;
3846    }
3847
3848  --scan;
3849
3850  /* We found `__'; move ahead to the last contiguous `__' pair.  */
3851  i = strspn (scan, "_");
3852  if (i > 2)
3853    scan += i - 2;
3854
3855  if (scan == *pp
3856      && (isdigit ((unsigned char) scan[2])
3857	  || scan[2] == 'Q'
3858	  || scan[2] == 't'))
3859    {
3860      /* This is a GNU style constructor name.  */
3861      *pp = scan + 2;
3862      return true;
3863    }
3864  else if (scan == *pp
3865	   && ! isdigit ((unsigned char) scan[2])
3866	   && scan[2] != 't')
3867    {
3868      /* Look for the `__' that separates the prefix from the
3869         signature.  */
3870      while (*scan == '_')
3871	++scan;
3872      scan = strstr (scan, "__");
3873      if (scan == NULL || scan[2] == '\0')
3874	{
3875	  stab_bad_demangle (*pp);
3876	  return false;
3877	}
3878
3879      return stab_demangle_function_name (minfo, pp, scan);
3880    }
3881  else if (scan[2] != '\0')
3882    {
3883      /* The name doesn't start with `__', but it does contain `__'.  */
3884      return stab_demangle_function_name (minfo, pp, scan);
3885    }
3886  else
3887    {
3888      stab_bad_demangle (*pp);
3889      return false;
3890    }
3891  /*NOTREACHED*/
3892}
3893
3894/* Demangle a function name prefix.  The scan argument points to the
3895   double underscore which separates the function name from the
3896   signature.  */
3897
3898static boolean
3899stab_demangle_function_name (minfo, pp, scan)
3900     struct stab_demangle_info *minfo;
3901     const char **pp;
3902     const char *scan;
3903{
3904  const char *name;
3905
3906  /* The string from *pp to scan is the name of the function.  We
3907     don't care about the name, since we just looking for argument
3908     types.  However, for conversion operators, the name may include a
3909     type which we must remember in order to handle backreferences.  */
3910
3911  name = *pp;
3912  *pp = scan + 2;
3913
3914  if (*pp - name >= 5
3915	   && strncmp (name, "type", 4) == 0
3916	   && (name[4] == '$' || name[4] == '.'))
3917    {
3918      const char *tem;
3919
3920      /* This is a type conversion operator.  */
3921      tem = name + 5;
3922      if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
3923	return false;
3924    }
3925  else if (name[0] == '_'
3926	   && name[1] == '_'
3927	   && name[2] == 'o'
3928	   && name[3] == 'p')
3929    {
3930      const char *tem;
3931
3932      /* This is a type conversion operator.  */
3933      tem = name + 4;
3934      if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
3935	return false;
3936    }
3937
3938  return true;
3939}
3940
3941/* Demangle the signature.  This is where the argument types are
3942   found.  */
3943
3944static boolean
3945stab_demangle_signature (minfo, pp)
3946     struct stab_demangle_info *minfo;
3947     const char **pp;
3948{
3949  const char *orig;
3950  boolean expect_func, func_done;
3951  const char *hold;
3952
3953  orig = *pp;
3954
3955  expect_func = false;
3956  func_done = false;
3957  hold = NULL;
3958
3959  while (**pp != '\0')
3960    {
3961      switch (**pp)
3962	{
3963	case 'Q':
3964	  hold = *pp;
3965	  if (! stab_demangle_qualified (minfo, pp, (debug_type *) NULL)
3966	      || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
3967	    return false;
3968	  expect_func = true;
3969	  hold = NULL;
3970	  break;
3971
3972	case 'S':
3973	  /* Static member function.  FIXME: Can this happen?  */
3974	  if (hold == NULL)
3975	    hold = *pp;
3976	  ++*pp;
3977	  break;
3978
3979	case 'C':
3980	  /* Const member function.  */
3981	  if (hold == NULL)
3982	    hold = *pp;
3983	  ++*pp;
3984	  break;
3985
3986	case '0': case '1': case '2': case '3': case '4':
3987	case '5': case '6': case '7': case '8': case '9':
3988	  if (hold == NULL)
3989	    hold = *pp;
3990	  if (! stab_demangle_class (minfo, pp, (const char **) NULL)
3991	      || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
3992	    return false;
3993	  expect_func = true;
3994	  hold = NULL;
3995	  break;
3996
3997	case 'F':
3998	  /* Function.  I don't know if this actually happens with g++
3999             output.  */
4000	  hold = NULL;
4001	  func_done = true;
4002	  ++*pp;
4003	  if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4004	    return false;
4005	  break;
4006
4007	case 't':
4008	  /* Template.  */
4009	  if (hold == NULL)
4010	    hold = *pp;
4011	  if (! stab_demangle_template (minfo, pp)
4012	      || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4013	    return false;
4014	  hold = NULL;
4015	  expect_func = true;
4016	  break;
4017
4018	case '_':
4019	  /* At the outermost level, we cannot have a return type
4020	     specified, so if we run into another '_' at this point we
4021	     are dealing with a mangled name that is either bogus, or
4022	     has been mangled by some algorithm we don't know how to
4023	     deal with.  So just reject the entire demangling.  */
4024	  stab_bad_demangle (orig);
4025	  return false;
4026
4027	default:
4028	  /* Assume we have stumbled onto the first outermost function
4029	     argument token, and start processing args.  */
4030	  func_done = true;
4031	  if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4032	    return false;
4033	  break;
4034	}
4035
4036      if (expect_func)
4037	{
4038	  func_done = true;
4039	  if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4040	    return false;
4041	}
4042    }
4043
4044  if (! func_done)
4045    {
4046      /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
4047	 bar__3fooi is 'foo::bar(int)'.  We get here when we find the
4048	 first case, and need to ensure that the '(void)' gets added
4049	 to the current declp.  */
4050      if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4051	return false;
4052    }
4053
4054  return true;
4055}
4056
4057/* Demangle a qualified name, such as "Q25Outer5Inner" which is the
4058   mangled form of "Outer::Inner".  */
4059
4060static boolean
4061stab_demangle_qualified (minfo, pp, ptype)
4062     struct stab_demangle_info *minfo;
4063     const char **pp;
4064     debug_type *ptype;
4065{
4066  const char *orig;
4067  const char *p;
4068  unsigned int qualifiers;
4069  debug_type context;
4070
4071  orig = *pp;
4072
4073  switch ((*pp)[1])
4074    {
4075    case '_':
4076      /* GNU mangled name with more than 9 classes.  The count is
4077	 preceded by an underscore (to distinguish it from the <= 9
4078	 case) and followed by an underscore.  */
4079      p = *pp + 2;
4080      if (! isdigit ((unsigned char) *p) || *p == '0')
4081	{
4082	  stab_bad_demangle (orig);
4083	  return false;
4084	}
4085      qualifiers = atoi (p);
4086      while (isdigit ((unsigned char) *p))
4087	++p;
4088      if (*p != '_')
4089	{
4090	  stab_bad_demangle (orig);
4091	  return false;
4092	}
4093      *pp = p + 1;
4094      break;
4095
4096    case '1': case '2': case '3': case '4': case '5':
4097    case '6': case '7': case '8': case '9':
4098      qualifiers = (*pp)[1] - '0';
4099      /* Skip an optional underscore after the count.  */
4100      if ((*pp)[2] == '_')
4101	++*pp;
4102      *pp += 2;
4103      break;
4104
4105    case '0':
4106    default:
4107      stab_bad_demangle (orig);
4108      return false;
4109    }
4110
4111  context = DEBUG_TYPE_NULL;
4112
4113  /* Pick off the names.  */
4114  while (qualifiers-- > 0)
4115    {
4116      if (**pp == '_')
4117	++*pp;
4118      if (**pp == 't')
4119	{
4120	  /* FIXME: I don't know how to handle the ptype != NULL case
4121             here.  */
4122	  if (! stab_demangle_template (minfo, pp))
4123	    return false;
4124	}
4125      else
4126	{
4127	  unsigned int len;
4128
4129	  len = stab_demangle_count (pp);
4130	  if (strlen (*pp) < len)
4131	    {
4132	      stab_bad_demangle (orig);
4133	      return false;
4134	    }
4135
4136	  if (ptype != NULL)
4137	    {
4138	      const debug_field *fields;
4139
4140	      fields = NULL;
4141	      if (context != DEBUG_TYPE_NULL)
4142		fields = debug_get_fields (minfo->dhandle, context);
4143
4144	      context = DEBUG_TYPE_NULL;
4145
4146	      if (fields != NULL)
4147		{
4148		  char *name;
4149
4150		  /* Try to find the type by looking through the
4151                     fields of context until we find a field with the
4152                     same type.  This ought to work for a class
4153                     defined within a class, but it won't work for,
4154                     e.g., an enum defined within a class.  stabs does
4155                     not give us enough information to figure out the
4156                     latter case.  */
4157
4158		  name = savestring (*pp, len);
4159
4160		  for (; *fields != DEBUG_FIELD_NULL; fields++)
4161		    {
4162		      debug_type ft;
4163		      const char *dn;
4164
4165		      ft = debug_get_field_type (minfo->dhandle, *fields);
4166		      if (ft == NULL)
4167			return false;
4168		      dn = debug_get_type_name (minfo->dhandle, ft);
4169		      if (dn != NULL && strcmp (dn, name) == 0)
4170			{
4171			  context = ft;
4172			  break;
4173			}
4174		    }
4175
4176		  free (name);
4177		}
4178
4179	      if (context == DEBUG_TYPE_NULL)
4180		{
4181	  /* We have to fall back on finding the type by name.
4182                     If there are more types to come, then this must
4183                     be a class.  Otherwise, it could be anything.  */
4184
4185		  if (qualifiers == 0)
4186		    {
4187		      char *name;
4188
4189		      name = savestring (*pp, len);
4190		      context = debug_find_named_type (minfo->dhandle,
4191						       name);
4192		      free (name);
4193		    }
4194
4195		  if (context == DEBUG_TYPE_NULL)
4196		    {
4197		      context = stab_find_tagged_type (minfo->dhandle,
4198						       minfo->info,
4199						       *pp, len,
4200						       (qualifiers == 0
4201							? DEBUG_KIND_ILLEGAL
4202							: DEBUG_KIND_CLASS));
4203		      if (context == DEBUG_TYPE_NULL)
4204			return false;
4205		    }
4206		}
4207	    }
4208
4209	  *pp += len;
4210	}
4211    }
4212
4213  if (ptype != NULL)
4214    *ptype = context;
4215
4216  return true;
4217}
4218
4219/* Demangle a template.  */
4220
4221static boolean
4222stab_demangle_template (minfo, pp)
4223     struct stab_demangle_info *minfo;
4224     const char **pp;
4225{
4226  const char *orig;
4227  unsigned int r, i;
4228
4229  orig = *pp;
4230
4231  ++*pp;
4232
4233  /* Skip the template name.  */
4234  r = stab_demangle_count (pp);
4235  if (r == 0 || strlen (*pp) < r)
4236    {
4237      stab_bad_demangle (orig);
4238      return false;
4239    }
4240  *pp += r;
4241
4242  /* Get the size of the parameter list.  */
4243  if (stab_demangle_get_count (pp, &r) == 0)
4244    {
4245      stab_bad_demangle (orig);
4246      return false;
4247    }
4248
4249  for (i = 0; i < r; i++)
4250    {
4251      if (**pp == 'Z')
4252	{
4253	  /* This is a type parameter.  */
4254	  ++*pp;
4255	  if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4256	    return false;
4257	}
4258      else
4259	{
4260	  const char *old_p;
4261	  boolean pointerp, realp, integralp, charp, boolp;
4262	  boolean done;
4263
4264	  old_p = *pp;
4265	  pointerp = false;
4266	  realp = false;
4267	  integralp = false;
4268	  charp = false;
4269	  boolp = false;
4270	  done = false;
4271
4272	  /* This is a value parameter.  */
4273
4274	  if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4275	    return false;
4276
4277	  while (*old_p != '\0' && ! done)
4278	    {
4279	      switch (*old_p)
4280		{
4281		case 'P':
4282		case 'p':
4283		case 'R':
4284		  pointerp = true;
4285		  done = true;
4286		  break;
4287		case 'C':	/* Const.  */
4288		case 'S':	/* Signed.  */
4289		case 'U':	/* Unsigned.  */
4290		case 'V':	/* Volatile.  */
4291		case 'F':	/* Function.  */
4292		case 'M':	/* Member function.  */
4293		case 'O':	/* ??? */
4294		  ++old_p;
4295		  break;
4296		case 'Q':	/* Qualified name.  */
4297		  integralp = true;
4298		  done = true;
4299		  break;
4300		case 'T':	/* Remembered type.  */
4301		  abort ();
4302		case 'v':	/* Void.  */
4303		  abort ();
4304		case 'x':	/* Long long.  */
4305		case 'l':	/* Long.  */
4306		case 'i':	/* Int.  */
4307		case 's':	/* Short.  */
4308		case 'w':	/* Wchar_t.  */
4309		  integralp = true;
4310		  done = true;
4311		  break;
4312		case 'b':	/* Bool.  */
4313		  boolp = true;
4314		  done = true;
4315		  break;
4316		case 'c':	/* Char.  */
4317		  charp = true;
4318		  done = true;
4319		  break;
4320		case 'r':	/* Long double.  */
4321		case 'd':	/* Double.  */
4322		case 'f':	/* Float.  */
4323		  realp = true;
4324		  done = true;
4325		  break;
4326		default:
4327		  /* Assume it's a user defined integral type.  */
4328		  integralp = true;
4329		  done = true;
4330		  break;
4331		}
4332	    }
4333
4334	  if (integralp)
4335	    {
4336	      if (**pp == 'm')
4337		++*pp;
4338	      while (isdigit ((unsigned char) **pp))
4339		++*pp;
4340	    }
4341	  else if (charp)
4342	    {
4343	      unsigned int val;
4344
4345	      if (**pp == 'm')
4346		++*pp;
4347	      val = stab_demangle_count (pp);
4348	      if (val == 0)
4349		{
4350		  stab_bad_demangle (orig);
4351		  return false;
4352		}
4353	    }
4354	  else if (boolp)
4355	    {
4356	      unsigned int val;
4357
4358	      val = stab_demangle_count (pp);
4359	      if (val != 0 && val != 1)
4360		{
4361		  stab_bad_demangle (orig);
4362		  return false;
4363		}
4364	    }
4365	  else if (realp)
4366	    {
4367	      if (**pp == 'm')
4368		++*pp;
4369	      while (isdigit ((unsigned char) **pp))
4370		++*pp;
4371	      if (**pp == '.')
4372		{
4373		  ++*pp;
4374		  while (isdigit ((unsigned char) **pp))
4375		    ++*pp;
4376		}
4377	      if (**pp == 'e')
4378		{
4379		  ++*pp;
4380		  while (isdigit ((unsigned char) **pp))
4381		    ++*pp;
4382		}
4383	    }
4384	  else if (pointerp)
4385	    {
4386	      unsigned int len;
4387
4388	      if (! stab_demangle_get_count (pp, &len))
4389		{
4390		  stab_bad_demangle (orig);
4391		  return false;
4392		}
4393	      *pp += len;
4394	    }
4395	}
4396    }
4397
4398  return true;
4399}
4400
4401/* Demangle a class name.  */
4402
4403static boolean
4404stab_demangle_class (minfo, pp, pstart)
4405     struct stab_demangle_info *minfo;
4406     const char **pp;
4407     const char **pstart;
4408{
4409  const char *orig;
4410  unsigned int n;
4411
4412  orig = *pp;
4413
4414  n = stab_demangle_count (pp);
4415  if (strlen (*pp) < n)
4416    {
4417      stab_bad_demangle (orig);
4418      return false;
4419    }
4420
4421  if (pstart != NULL)
4422    *pstart = *pp;
4423
4424  *pp += n;
4425
4426  return true;
4427}
4428
4429/* Demangle function arguments.  If the pargs argument is not NULL, it
4430   is set to a NULL terminated array holding the arguments.  */
4431
4432static boolean
4433stab_demangle_args (minfo, pp, pargs, pvarargs)
4434     struct stab_demangle_info *minfo;
4435     const char **pp;
4436     debug_type **pargs;
4437     boolean *pvarargs;
4438{
4439  const char *orig;
4440  unsigned int alloc, count;
4441
4442  orig = *pp;
4443
4444  alloc = 10;
4445  if (pargs != NULL)
4446    {
4447      *pargs = (debug_type *) xmalloc (alloc * sizeof **pargs);
4448      *pvarargs = false;
4449    }
4450  count = 0;
4451
4452  while (**pp != '_' && **pp != '\0' && **pp != 'e')
4453    {
4454      if (**pp == 'N' || **pp == 'T')
4455	{
4456	  char temptype;
4457	  unsigned int r, t;
4458
4459	  temptype = **pp;
4460	  ++*pp;
4461
4462	  if (temptype == 'T')
4463	    r = 1;
4464	  else
4465	    {
4466	      if (! stab_demangle_get_count (pp, &r))
4467		{
4468		  stab_bad_demangle (orig);
4469		  return false;
4470		}
4471	    }
4472
4473	  if (! stab_demangle_get_count (pp, &t))
4474	    {
4475	      stab_bad_demangle (orig);
4476	      return false;
4477	    }
4478
4479	  if (t >= minfo->typestring_count)
4480	    {
4481	      stab_bad_demangle (orig);
4482	      return false;
4483	    }
4484	  while (r-- > 0)
4485	    {
4486	      const char *tem;
4487
4488	      tem = minfo->typestrings[t].typestring;
4489	      if (! stab_demangle_arg (minfo, &tem, pargs, &count, &alloc))
4490		return false;
4491	    }
4492	}
4493      else
4494	{
4495	  if (! stab_demangle_arg (minfo, pp, pargs, &count, &alloc))
4496	    return false;
4497	}
4498    }
4499
4500  if (pargs != NULL)
4501    (*pargs)[count] = DEBUG_TYPE_NULL;
4502
4503  if (**pp == 'e')
4504    {
4505      if (pargs != NULL)
4506	*pvarargs = true;
4507      ++*pp;
4508    }
4509
4510  return true;
4511}
4512
4513/* Demangle a single argument.  */
4514
4515static boolean
4516stab_demangle_arg (minfo, pp, pargs, pcount, palloc)
4517     struct stab_demangle_info *minfo;
4518     const char **pp;
4519     debug_type **pargs;
4520     unsigned int *pcount;
4521     unsigned int *palloc;
4522{
4523  const char *start;
4524  debug_type type;
4525
4526  start = *pp;
4527  if (! stab_demangle_type (minfo, pp,
4528			    pargs == NULL ? (debug_type *) NULL : &type)
4529      || ! stab_demangle_remember_type (minfo, start, *pp - start))
4530    return false;
4531
4532  if (pargs != NULL)
4533    {
4534      if (type == DEBUG_TYPE_NULL)
4535	return false;
4536
4537      if (*pcount + 1 >= *palloc)
4538	{
4539	  *palloc += 10;
4540	  *pargs = ((debug_type *)
4541		    xrealloc (*pargs, *palloc * sizeof **pargs));
4542	}
4543      (*pargs)[*pcount] = type;
4544      ++*pcount;
4545    }
4546
4547  return true;
4548}
4549
4550/* Demangle a type.  If the ptype argument is not NULL, *ptype is set
4551   to the newly allocated type.  */
4552
4553static boolean
4554stab_demangle_type (minfo, pp, ptype)
4555     struct stab_demangle_info *minfo;
4556     const char **pp;
4557     debug_type *ptype;
4558{
4559  const char *orig;
4560
4561  orig = *pp;
4562
4563  switch (**pp)
4564    {
4565    case 'P':
4566    case 'p':
4567      /* A pointer type.  */
4568      ++*pp;
4569      if (! stab_demangle_type (minfo, pp, ptype))
4570	return false;
4571      if (ptype != NULL)
4572	*ptype = debug_make_pointer_type (minfo->dhandle, *ptype);
4573      break;
4574
4575    case 'R':
4576      /* A reference type.  */
4577      ++*pp;
4578      if (! stab_demangle_type (minfo, pp, ptype))
4579	return false;
4580      if (ptype != NULL)
4581	*ptype = debug_make_reference_type (minfo->dhandle, *ptype);
4582      break;
4583
4584    case 'A':
4585      /* An array.  */
4586      {
4587	unsigned long high;
4588
4589	++*pp;
4590	high = 0;
4591	while (**pp != '\0' && **pp != '_')
4592	  {
4593	    if (! isdigit ((unsigned char) **pp))
4594	      {
4595		stab_bad_demangle (orig);
4596		return false;
4597	      }
4598	    high *= 10;
4599	    high += **pp - '0';
4600	    ++*pp;
4601	  }
4602	if (**pp != '_')
4603	  {
4604	    stab_bad_demangle (orig);
4605	    return false;
4606	  }
4607	++*pp;
4608
4609	if (! stab_demangle_type (minfo, pp, ptype))
4610	  return false;
4611	if (ptype != NULL)
4612	  {
4613	    debug_type int_type;
4614
4615	    int_type = debug_find_named_type (minfo->dhandle, "int");
4616	    if (int_type == NULL)
4617	      int_type = debug_make_int_type (minfo->dhandle, 4, false);
4618	    *ptype = debug_make_array_type (minfo->dhandle, *ptype, int_type,
4619					    0, high, false);
4620	  }
4621      }
4622      break;
4623
4624    case 'T':
4625      /* A back reference to a remembered type.  */
4626      {
4627	unsigned int i;
4628	const char *p;
4629
4630	++*pp;
4631	if (! stab_demangle_get_count (pp, &i))
4632	  {
4633	    stab_bad_demangle (orig);
4634	    return false;
4635	  }
4636	if (i >= minfo->typestring_count)
4637	  {
4638	    stab_bad_demangle (orig);
4639	    return false;
4640	  }
4641	p = minfo->typestrings[i].typestring;
4642	if (! stab_demangle_type (minfo, &p, ptype))
4643	  return false;
4644      }
4645      break;
4646
4647    case 'F':
4648      /* A function.  */
4649      {
4650	debug_type *args;
4651	boolean varargs;
4652
4653	++*pp;
4654	if (! stab_demangle_args (minfo, pp,
4655				  (ptype == NULL
4656				   ? (debug_type **) NULL
4657				   : &args),
4658				  (ptype == NULL
4659				   ? (boolean *) NULL
4660				   : &varargs)))
4661	  return false;
4662	if (**pp != '_')
4663	  {
4664	    /* cplus_demangle will accept a function without a return
4665	       type, but I don't know when that will happen, or what
4666	       to do if it does.  */
4667	    stab_bad_demangle (orig);
4668	    return false;
4669	  }
4670	++*pp;
4671	if (! stab_demangle_type (minfo, pp, ptype))
4672	  return false;
4673	if (ptype != NULL)
4674	  *ptype = debug_make_function_type (minfo->dhandle, *ptype, args,
4675					     varargs);
4676
4677      }
4678      break;
4679
4680    case 'M':
4681    case 'O':
4682      {
4683	boolean memberp, constp, volatilep;
4684	debug_type *args;
4685	boolean varargs;
4686	unsigned int n;
4687	const char *name;
4688
4689	memberp = **pp == 'M';
4690	constp = false;
4691	volatilep = false;
4692	args = NULL;
4693	varargs = false;
4694
4695	++*pp;
4696	if (! isdigit ((unsigned char) **pp))
4697	  {
4698	    stab_bad_demangle (orig);
4699	    return false;
4700	  }
4701	n = stab_demangle_count (pp);
4702	if (strlen (*pp) < n)
4703	  {
4704	    stab_bad_demangle (orig);
4705	    return false;
4706	  }
4707	name = *pp;
4708	*pp += n;
4709
4710	if (memberp)
4711	  {
4712	    if (**pp == 'C')
4713	      {
4714		constp = true;
4715		++*pp;
4716	      }
4717	    else if (**pp == 'V')
4718	      {
4719		volatilep = true;
4720		++*pp;
4721	      }
4722	    if (**pp != 'F')
4723	      {
4724		stab_bad_demangle (orig);
4725		return false;
4726	      }
4727	    ++*pp;
4728	    if (! stab_demangle_args (minfo, pp,
4729				      (ptype == NULL
4730				       ? (debug_type **) NULL
4731				       : &args),
4732				      (ptype == NULL
4733				       ? (boolean *) NULL
4734				       : &varargs)))
4735	      return false;
4736	  }
4737
4738	if (**pp != '_')
4739	  {
4740	    stab_bad_demangle (orig);
4741	    return false;
4742	  }
4743	++*pp;
4744
4745	if (! stab_demangle_type (minfo, pp, ptype))
4746	  return false;
4747
4748	if (ptype != NULL)
4749	  {
4750	    debug_type class_type;
4751
4752	    class_type = stab_find_tagged_type (minfo->dhandle, minfo->info,
4753						name, (int) n,
4754						DEBUG_KIND_CLASS);
4755	    if (class_type == DEBUG_TYPE_NULL)
4756	      return false;
4757
4758	    if (! memberp)
4759	      *ptype = debug_make_offset_type (minfo->dhandle, class_type,
4760					       *ptype);
4761	    else
4762	      {
4763		/* FIXME: We have no way to record constp or
4764                   volatilep.  */
4765		*ptype = debug_make_method_type (minfo->dhandle, *ptype,
4766						 class_type, args, varargs);
4767	      }
4768	  }
4769      }
4770      break;
4771
4772    case 'G':
4773      ++*pp;
4774      if (! stab_demangle_type (minfo, pp, ptype))
4775	return false;
4776      break;
4777
4778    case 'C':
4779      ++*pp;
4780      if (! stab_demangle_type (minfo, pp, ptype))
4781	return false;
4782      if (ptype != NULL)
4783	*ptype = debug_make_const_type (minfo->dhandle, *ptype);
4784      break;
4785
4786    case 'Q':
4787      {
4788	const char *hold;
4789
4790	hold = *pp;
4791	if (! stab_demangle_qualified (minfo, pp, ptype))
4792	  return false;
4793      }
4794      break;
4795
4796    default:
4797      if (! stab_demangle_fund_type (minfo, pp, ptype))
4798	return false;
4799      break;
4800    }
4801
4802  return true;
4803}
4804
4805/* Demangle a fundamental type.  If the ptype argument is not NULL,
4806   *ptype is set to the newly allocated type.  */
4807
4808static boolean
4809stab_demangle_fund_type (minfo, pp, ptype)
4810     struct stab_demangle_info *minfo;
4811     const char **pp;
4812     debug_type *ptype;
4813{
4814  const char *orig;
4815  boolean constp, volatilep, unsignedp, signedp;
4816  boolean done;
4817
4818  orig = *pp;
4819
4820  constp = false;
4821  volatilep = false;
4822  unsignedp = false;
4823  signedp = false;
4824
4825  done = false;
4826  while (! done)
4827    {
4828      switch (**pp)
4829	{
4830	case 'C':
4831	  constp = true;
4832	  ++*pp;
4833	  break;
4834
4835	case 'U':
4836	  unsignedp = true;
4837	  ++*pp;
4838	  break;
4839
4840	case 'S':
4841	  signedp = true;
4842	  ++*pp;
4843	  break;
4844
4845	case 'V':
4846	  volatilep = true;
4847	  ++*pp;
4848	  break;
4849
4850	default:
4851	  done = true;
4852	  break;
4853	}
4854    }
4855
4856  switch (**pp)
4857    {
4858    case '\0':
4859    case '_':
4860      /* cplus_demangle permits this, but I don't know what it means.  */
4861      stab_bad_demangle (orig);
4862      break;
4863
4864    case 'v': /* void */
4865      if (ptype != NULL)
4866	{
4867	  *ptype = debug_find_named_type (minfo->dhandle, "void");
4868	  if (*ptype == DEBUG_TYPE_NULL)
4869	    *ptype = debug_make_void_type (minfo->dhandle);
4870	}
4871      ++*pp;
4872      break;
4873
4874    case 'x': /* long long */
4875      if (ptype != NULL)
4876	{
4877	  *ptype = debug_find_named_type (minfo->dhandle,
4878					  (unsignedp
4879					   ? "long long unsigned int"
4880					   : "long long int"));
4881	  if (*ptype == DEBUG_TYPE_NULL)
4882	    *ptype = debug_make_int_type (minfo->dhandle, 8, unsignedp);
4883	}
4884      ++*pp;
4885      break;
4886
4887    case 'l': /* long */
4888      if (ptype != NULL)
4889	{
4890	  *ptype = debug_find_named_type (minfo->dhandle,
4891					  (unsignedp
4892					   ? "long unsigned int"
4893					   : "long int"));
4894	  if (*ptype == DEBUG_TYPE_NULL)
4895	    *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
4896	}
4897      ++*pp;
4898      break;
4899
4900    case 'i': /* int */
4901      if (ptype != NULL)
4902	{
4903	  *ptype = debug_find_named_type (minfo->dhandle,
4904					  (unsignedp
4905					   ? "unsigned int"
4906					   : "int"));
4907	  if (*ptype == DEBUG_TYPE_NULL)
4908	    *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
4909	}
4910      ++*pp;
4911      break;
4912
4913    case 's': /* short */
4914      if (ptype != NULL)
4915	{
4916	  *ptype = debug_find_named_type (minfo->dhandle,
4917					  (unsignedp
4918					   ? "short unsigned int"
4919					   : "short int"));
4920	  if (*ptype == DEBUG_TYPE_NULL)
4921	    *ptype = debug_make_int_type (minfo->dhandle, 2, unsignedp);
4922	}
4923      ++*pp;
4924      break;
4925
4926    case 'b': /* bool */
4927      if (ptype != NULL)
4928	{
4929	  *ptype = debug_find_named_type (minfo->dhandle, "bool");
4930	  if (*ptype == DEBUG_TYPE_NULL)
4931	    *ptype = debug_make_bool_type (minfo->dhandle, 4);
4932	}
4933      ++*pp;
4934      break;
4935
4936    case 'c': /* char */
4937      if (ptype != NULL)
4938	{
4939	  *ptype = debug_find_named_type (minfo->dhandle,
4940					  (unsignedp
4941					   ? "unsigned char"
4942					   : (signedp
4943					      ? "signed char"
4944					      : "char")));
4945	  if (*ptype == DEBUG_TYPE_NULL)
4946	    *ptype = debug_make_int_type (minfo->dhandle, 1, unsignedp);
4947	}
4948      ++*pp;
4949      break;
4950
4951    case 'w': /* wchar_t */
4952      if (ptype != NULL)
4953	{
4954	  *ptype = debug_find_named_type (minfo->dhandle, "__wchar_t");
4955	  if (*ptype == DEBUG_TYPE_NULL)
4956	    *ptype = debug_make_int_type (minfo->dhandle, 2, true);
4957	}
4958      ++*pp;
4959      break;
4960
4961    case 'r': /* long double */
4962      if (ptype != NULL)
4963	{
4964	  *ptype = debug_find_named_type (minfo->dhandle, "long double");
4965	  if (*ptype == DEBUG_TYPE_NULL)
4966	    *ptype = debug_make_float_type (minfo->dhandle, 8);
4967	}
4968      ++*pp;
4969      break;
4970
4971    case 'd': /* double */
4972      if (ptype != NULL)
4973	{
4974	  *ptype = debug_find_named_type (minfo->dhandle, "double");
4975	  if (*ptype == DEBUG_TYPE_NULL)
4976	    *ptype = debug_make_float_type (minfo->dhandle, 8);
4977	}
4978      ++*pp;
4979      break;
4980
4981    case 'f': /* float */
4982      if (ptype != NULL)
4983	{
4984	  *ptype = debug_find_named_type (minfo->dhandle, "float");
4985	  if (*ptype == DEBUG_TYPE_NULL)
4986	    *ptype = debug_make_float_type (minfo->dhandle, 4);
4987	}
4988      ++*pp;
4989      break;
4990
4991    case 'G':
4992      ++*pp;
4993      if (! isdigit ((unsigned char) **pp))
4994	{
4995	  stab_bad_demangle (orig);
4996	  return false;
4997	}
4998      /* Fall through.  */
4999    case '0': case '1': case '2': case '3': case '4':
5000    case '5': case '6': case '7': case '8': case '9':
5001      {
5002	const char *hold;
5003
5004	if (! stab_demangle_class (minfo, pp, &hold))
5005	  return false;
5006	if (ptype != NULL)
5007	  {
5008	    char *name;
5009
5010	    name = savestring (hold, *pp - hold);
5011	    *ptype = debug_find_named_type (minfo->dhandle, name);
5012	    if (*ptype == DEBUG_TYPE_NULL)
5013	      {
5014		/* FIXME: It is probably incorrect to assume that
5015                   undefined types are tagged types.  */
5016		*ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
5017						hold, *pp - hold,
5018						DEBUG_KIND_ILLEGAL);
5019	      }
5020	    free (name);
5021	  }
5022      }
5023      break;
5024
5025    case 't':
5026      if (! stab_demangle_template (minfo, pp))
5027	return false;
5028      if (ptype != NULL)
5029	{
5030	  debug_type t;
5031
5032	  /* FIXME: I really don't know how a template should be
5033             represented in the current type system.  Perhaps the
5034             template should be demangled into a string, and the type
5035             should be represented as a named type.  However, I don't
5036             know what the base type of the named type should be.  */
5037	  t = debug_make_void_type (minfo->dhandle);
5038	  t = debug_make_pointer_type (minfo->dhandle, t);
5039	  t = debug_name_type (minfo->dhandle, "TEMPLATE", t);
5040	  *ptype = t;
5041	}
5042      break;
5043
5044    default:
5045      stab_bad_demangle (orig);
5046      return false;
5047    }
5048
5049  if (ptype != NULL)
5050    {
5051      if (constp)
5052	*ptype = debug_make_const_type (minfo->dhandle, *ptype);
5053      if (volatilep)
5054	*ptype = debug_make_volatile_type (minfo->dhandle, *ptype);
5055    }
5056
5057  return true;
5058}
5059
5060/* Remember a type string in a demangled string.  */
5061
5062static boolean
5063stab_demangle_remember_type (minfo, p, len)
5064     struct stab_demangle_info *minfo;
5065     const char *p;
5066     int len;
5067{
5068  if (minfo->typestring_count >= minfo->typestring_alloc)
5069    {
5070      minfo->typestring_alloc += 10;
5071      minfo->typestrings = ((struct stab_demangle_typestring *)
5072			    xrealloc (minfo->typestrings,
5073				      (minfo->typestring_alloc
5074				       * sizeof *minfo->typestrings)));
5075    }
5076
5077  minfo->typestrings[minfo->typestring_count].typestring = p;
5078  minfo->typestrings[minfo->typestring_count].len = (unsigned int) len;
5079  ++minfo->typestring_count;
5080
5081  return true;
5082}
5083