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