]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/stabsread.c
2002-05-09 Michael Snyder <msnyder@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / stabsread.c
CommitLineData
c906108c 1/* Support routines for decoding "stabs" debugging information format.
b6ba6518 2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
fedbd091 3 1996, 1997, 1998, 1999, 2000, 2001, 2002
c5aa993b 4 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b
JM
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,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23/* Support routines for reading and decoding debugging information in
24 the "stabs" format. This format is used with many systems that use
25 the a.out object file format, as well as some systems that use
26 COFF or ELF where the stabs data is placed in a special section.
27 Avoid placing any object file format specific code in this file. */
28
29#include "defs.h"
30#include "gdb_string.h"
31#include "bfd.h"
32#include "obstack.h"
33#include "symtab.h"
34#include "gdbtypes.h"
35#include "expression.h"
36#include "symfile.h"
37#include "objfiles.h"
38#include "aout/stab_gnu.h" /* We always use GNU stabs, not native */
39#include "libaout.h"
40#include "aout/aout64.h"
41#include "gdb-stabs.h"
42#include "buildsym.h"
43#include "complaints.h"
44#include "demangle.h"
45#include "language.h"
d16aafd8 46#include "doublest.h"
c906108c
SS
47
48#include <ctype.h>
49
50/* Ask stabsread.h to define the vars it normally declares `extern'. */
c5aa993b
JM
51#define EXTERN
52/**/
c906108c
SS
53#include "stabsread.h" /* Our own declarations */
54#undef EXTERN
55
a14ed312 56extern void _initialize_stabsread (void);
392a587b 57
c906108c
SS
58/* The routines that read and process a complete stabs for a C struct or
59 C++ class pass lists of data member fields and lists of member function
60 fields in an instance of a field_info structure, as defined below.
61 This is part of some reorganization of low level C++ support and is
62 expected to eventually go away... (FIXME) */
63
64struct field_info
c5aa993b
JM
65 {
66 struct nextfield
67 {
68 struct nextfield *next;
c906108c 69
c5aa993b
JM
70 /* This is the raw visibility from the stab. It is not checked
71 for being one of the visibilities we recognize, so code which
72 examines this field better be able to deal. */
73 int visibility;
c906108c 74
c5aa993b
JM
75 struct field field;
76 }
77 *list;
78 struct next_fnfieldlist
79 {
80 struct next_fnfieldlist *next;
81 struct fn_fieldlist fn_fieldlist;
82 }
83 *fnlist;
84 };
c906108c
SS
85
86static void
a14ed312
KB
87read_one_struct_field (struct field_info *, char **, char *,
88 struct type *, struct objfile *);
c906108c 89
a14ed312 90static char *get_substring (char **, int);
c906108c 91
a14ed312 92static struct type *dbx_alloc_type (int[2], struct objfile *);
c906108c 93
a14ed312 94static long read_huge_number (char **, int, int *);
c906108c 95
a14ed312 96static struct type *error_type (char **, struct objfile *);
c906108c
SS
97
98static void
a14ed312
KB
99patch_block_stabs (struct pending *, struct pending_stabs *,
100 struct objfile *);
c906108c 101
a14ed312 102static void fix_common_block (struct symbol *, int);
c906108c 103
a14ed312 104static int read_type_number (char **, int *);
c906108c 105
a14ed312 106static struct type *read_range_type (char **, int[2], struct objfile *);
c906108c 107
a14ed312 108static struct type *read_sun_builtin_type (char **, int[2], struct objfile *);
c906108c 109
a14ed312
KB
110static struct type *read_sun_floating_type (char **, int[2],
111 struct objfile *);
c906108c 112
a14ed312 113static struct type *read_enum_type (char **, struct type *, struct objfile *);
c906108c 114
a14ed312 115static struct type *rs6000_builtin_type (int);
c906108c
SS
116
117static int
a14ed312
KB
118read_member_functions (struct field_info *, char **, struct type *,
119 struct objfile *);
c906108c
SS
120
121static int
a14ed312
KB
122read_struct_fields (struct field_info *, char **, struct type *,
123 struct objfile *);
c906108c
SS
124
125static int
a14ed312
KB
126read_baseclasses (struct field_info *, char **, struct type *,
127 struct objfile *);
c906108c
SS
128
129static int
a14ed312
KB
130read_tilde_fields (struct field_info *, char **, struct type *,
131 struct objfile *);
c906108c 132
a14ed312 133static int attach_fn_fields_to_type (struct field_info *, struct type *);
c906108c
SS
134
135static int
a14ed312 136attach_fields_to_type (struct field_info *, struct type *, struct objfile *);
c906108c 137
a14ed312 138static struct type *read_struct_type (char **, struct type *,
2ae1c2d2 139 enum type_code,
a14ed312 140 struct objfile *);
c906108c 141
a14ed312
KB
142static struct type *read_array_type (char **, struct type *,
143 struct objfile *);
c906108c 144
a14ed312 145static struct type **read_args (char **, int, struct objfile *);
c906108c
SS
146
147static int
a14ed312
KB
148read_cpp_abbrev (struct field_info *, char **, struct type *,
149 struct objfile *);
c906108c
SS
150
151/* new functions added for cfront support */
152
153static int
a14ed312
KB
154copy_cfront_struct_fields (struct field_info *, struct type *,
155 struct objfile *);
c906108c 156
a14ed312 157static char *get_cfront_method_physname (char *);
c906108c
SS
158
159static int
a14ed312
KB
160read_cfront_baseclasses (struct field_info *, char **,
161 struct type *, struct objfile *);
c906108c
SS
162
163static int
a14ed312
KB
164read_cfront_static_fields (struct field_info *, char **,
165 struct type *, struct objfile *);
c906108c 166static int
a14ed312
KB
167read_cfront_member_functions (struct field_info *, char **,
168 struct type *, struct objfile *);
c906108c
SS
169
170/* end new functions added for cfront support */
171
172static void
a14ed312 173add_live_range (struct objfile *, struct symbol *, CORE_ADDR, CORE_ADDR);
c906108c 174
a14ed312 175static int resolve_live_range (struct objfile *, struct symbol *, char *);
c906108c 176
a14ed312 177static int process_reference (char **string);
c906108c 178
a14ed312 179static CORE_ADDR ref_search_value (int refnum);
c906108c
SS
180
181static int
a14ed312 182resolve_symbol_reference (struct objfile *, struct symbol *, char *);
c906108c 183
a14ed312 184void stabsread_clear_cache (void);
7be570e7 185
c5aa993b
JM
186static const char vptr_name[] =
187{'_', 'v', 'p', 't', 'r', CPLUS_MARKER, '\0'};
188static const char vb_name[] =
189{'_', 'v', 'b', CPLUS_MARKER, '\0'};
c906108c
SS
190
191/* Define this as 1 if a pcc declaration of a char or short argument
192 gives the correct address. Otherwise assume pcc gives the
193 address of the corresponding int, which is not the same on a
194 big-endian machine. */
195
7a292a7a 196#if !defined (BELIEVE_PCC_PROMOTION)
c906108c
SS
197#define BELIEVE_PCC_PROMOTION 0
198#endif
7a292a7a
SS
199#if !defined (BELIEVE_PCC_PROMOTION_TYPE)
200#define BELIEVE_PCC_PROMOTION_TYPE 0
201#endif
c906108c
SS
202
203static struct complaint invalid_cpp_abbrev_complaint =
c5aa993b 204{"invalid C++ abbreviation `%s'", 0, 0};
c906108c
SS
205
206static struct complaint invalid_cpp_type_complaint =
c5aa993b 207{"C++ abbreviated type name unknown at symtab pos %d", 0, 0};
c906108c
SS
208
209static struct complaint member_fn_complaint =
c5aa993b 210{"member function type missing, got '%c'", 0, 0};
c906108c
SS
211
212static struct complaint const_vol_complaint =
c5aa993b 213{"const/volatile indicator missing, got '%c'", 0, 0};
c906108c
SS
214
215static struct complaint error_type_complaint =
0f5881fa 216{"couldn't parse type; debugger out of date?", 0, 0};
c906108c
SS
217
218static struct complaint invalid_member_complaint =
c5aa993b 219{"invalid (minimal) member type data format at symtab pos %d.", 0, 0};
c906108c
SS
220
221static struct complaint range_type_base_complaint =
c5aa993b 222{"base type %d of range type is not defined", 0, 0};
c906108c
SS
223
224static struct complaint reg_value_complaint =
c5aa993b 225{"register number %d too large (max %d) in symbol %s", 0, 0};
c906108c
SS
226
227static struct complaint vtbl_notfound_complaint =
c5aa993b 228{"virtual function table pointer not found when defining class `%s'", 0, 0};
c906108c
SS
229
230static struct complaint unrecognized_cplus_name_complaint =
c5aa993b 231{"Unknown C++ symbol name `%s'", 0, 0};
c906108c
SS
232
233static struct complaint rs6000_builtin_complaint =
c5aa993b 234{"Unknown builtin type %d", 0, 0};
c906108c
SS
235
236static struct complaint unresolved_sym_chain_complaint =
c5aa993b 237{"%s: common block `%s' from global_sym_chain unresolved", 0, 0};
c906108c
SS
238
239static struct complaint stabs_general_complaint =
c5aa993b 240{"%s", 0, 0};
c906108c
SS
241
242static struct complaint lrs_general_complaint =
c5aa993b 243{"%s", 0, 0};
c906108c
SS
244
245/* Make a list of forward references which haven't been defined. */
246
247static struct type **undef_types;
248static int undef_types_allocated;
249static int undef_types_length;
250static struct symbol *current_symbol = NULL;
251
252/* Check for and handle cretinous stabs symbol name continuation! */
253#define STABS_CONTINUE(pp,objfile) \
254 do { \
255 if (**(pp) == '\\' || (**(pp) == '?' && (*(pp))[1] == '\0')) \
256 *(pp) = next_symbol_text (objfile); \
257 } while (0)
258\f
259/* FIXME: These probably should be our own types (like rs6000_builtin_type
260 has its own types) rather than builtin_type_*. */
c5aa993b
JM
261static struct type **os9k_type_vector[] =
262{
263 0,
264 &builtin_type_int,
265 &builtin_type_char,
266 &builtin_type_long,
267 &builtin_type_short,
268 &builtin_type_unsigned_char,
269 &builtin_type_unsigned_short,
270 &builtin_type_unsigned_long,
271 &builtin_type_unsigned_int,
272 &builtin_type_float,
273 &builtin_type_double,
274 &builtin_type_void,
275 &builtin_type_long_double
c906108c
SS
276};
277
a14ed312 278static void os9k_init_type_vector (struct type **);
c906108c
SS
279
280static void
fba45db2 281os9k_init_type_vector (struct type **tv)
c906108c 282{
745b8ca0 283 unsigned int i;
c5aa993b 284 for (i = 0; i < sizeof (os9k_type_vector) / sizeof (struct type **); i++)
c906108c
SS
285 tv[i] = (os9k_type_vector[i] == 0 ? 0 : *(os9k_type_vector[i]));
286}
287
288/* Look up a dbx type-number pair. Return the address of the slot
289 where the type for that number-pair is stored.
290 The number-pair is in TYPENUMS.
291
292 This can be used for finding the type associated with that pair
293 or for associating a new type with the pair. */
294
295struct type **
35a2f538 296dbx_lookup_type (int typenums[2])
c906108c
SS
297{
298 register int filenum = typenums[0];
299 register int index = typenums[1];
300 unsigned old_len;
301 register int real_filenum;
302 register struct header_file *f;
303 int f_orig_length;
304
305 if (filenum == -1) /* -1,-1 is for temporary types. */
306 return 0;
307
308 if (filenum < 0 || filenum >= n_this_object_header_files)
309 {
c5aa993b
JM
310 static struct complaint msg =
311 {"\
c906108c 312Invalid symbol data: type number (%d,%d) out of range at symtab pos %d.",
c5aa993b 313 0, 0};
c906108c
SS
314 complain (&msg, filenum, index, symnum);
315 goto error_return;
316 }
317
318 if (filenum == 0)
319 {
320 if (index < 0)
321 {
322 /* Caller wants address of address of type. We think
323 that negative (rs6k builtin) types will never appear as
324 "lvalues", (nor should they), so we stuff the real type
325 pointer into a temp, and return its address. If referenced,
326 this will do the right thing. */
327 static struct type *temp_type;
328
c5aa993b 329 temp_type = rs6000_builtin_type (index);
c906108c
SS
330 return &temp_type;
331 }
332
333 /* Type is defined outside of header files.
c5aa993b 334 Find it in this object file's type vector. */
c906108c
SS
335 if (index >= type_vector_length)
336 {
337 old_len = type_vector_length;
338 if (old_len == 0)
339 {
340 type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
341 type_vector = (struct type **)
342 xmalloc (type_vector_length * sizeof (struct type *));
343 }
344 while (index >= type_vector_length)
345 {
346 type_vector_length *= 2;
347 }
348 type_vector = (struct type **)
349 xrealloc ((char *) type_vector,
350 (type_vector_length * sizeof (struct type *)));
351 memset (&type_vector[old_len], 0,
352 (type_vector_length - old_len) * sizeof (struct type *));
353
354 if (os9k_stabs)
355 /* Deal with OS9000 fundamental types. */
356 os9k_init_type_vector (type_vector);
357 }
358 return (&type_vector[index]);
359 }
360 else
361 {
362 real_filenum = this_object_header_files[filenum];
363
364 if (real_filenum >= N_HEADER_FILES (current_objfile))
365 {
366 struct type *temp_type;
367 struct type **temp_type_p;
368
369 warning ("GDB internal error: bad real_filenum");
370
371 error_return:
372 temp_type = init_type (TYPE_CODE_ERROR, 0, 0, NULL, NULL);
373 temp_type_p = (struct type **) xmalloc (sizeof (struct type *));
374 *temp_type_p = temp_type;
375 return temp_type_p;
376 }
377
378 f = HEADER_FILES (current_objfile) + real_filenum;
379
380 f_orig_length = f->length;
381 if (index >= f_orig_length)
382 {
383 while (index >= f->length)
384 {
385 f->length *= 2;
386 }
387 f->vector = (struct type **)
388 xrealloc ((char *) f->vector, f->length * sizeof (struct type *));
389 memset (&f->vector[f_orig_length], 0,
390 (f->length - f_orig_length) * sizeof (struct type *));
391 }
392 return (&f->vector[index]);
393 }
394}
395
396/* Make sure there is a type allocated for type numbers TYPENUMS
397 and return the type object.
398 This can create an empty (zeroed) type object.
399 TYPENUMS may be (-1, -1) to return a new type object that is not
400 put into the type vector, and so may not be referred to by number. */
401
402static struct type *
35a2f538 403dbx_alloc_type (int typenums[2], struct objfile *objfile)
c906108c
SS
404{
405 register struct type **type_addr;
406
407 if (typenums[0] == -1)
408 {
409 return (alloc_type (objfile));
410 }
411
412 type_addr = dbx_lookup_type (typenums);
413
414 /* If we are referring to a type not known at all yet,
415 allocate an empty type for it.
416 We will fill it in later if we find out how. */
417 if (*type_addr == 0)
418 {
419 *type_addr = alloc_type (objfile);
420 }
421
422 return (*type_addr);
423}
424
425/* for all the stabs in a given stab vector, build appropriate types
426 and fix their symbols in given symbol vector. */
427
428static void
fba45db2
KB
429patch_block_stabs (struct pending *symbols, struct pending_stabs *stabs,
430 struct objfile *objfile)
c906108c
SS
431{
432 int ii;
433 char *name;
434 char *pp;
435 struct symbol *sym;
436
437 if (stabs)
438 {
c5aa993b 439
c906108c 440 /* for all the stab entries, find their corresponding symbols and
c5aa993b
JM
441 patch their types! */
442
c906108c
SS
443 for (ii = 0; ii < stabs->count; ++ii)
444 {
445 name = stabs->stab[ii];
c5aa993b 446 pp = (char *) strchr (name, ':');
c906108c
SS
447 while (pp[1] == ':')
448 {
c5aa993b
JM
449 pp += 2;
450 pp = (char *) strchr (pp, ':');
c906108c 451 }
c5aa993b 452 sym = find_symbol_in_list (symbols, name, pp - name);
c906108c
SS
453 if (!sym)
454 {
455 /* FIXME-maybe: it would be nice if we noticed whether
c5aa993b
JM
456 the variable was defined *anywhere*, not just whether
457 it is defined in this compilation unit. But neither
458 xlc or GCC seem to need such a definition, and until
459 we do psymtabs (so that the minimal symbols from all
460 compilation units are available now), I'm not sure
461 how to get the information. */
c906108c
SS
462
463 /* On xcoff, if a global is defined and never referenced,
c5aa993b
JM
464 ld will remove it from the executable. There is then
465 a N_GSYM stab for it, but no regular (C_EXT) symbol. */
c906108c
SS
466 sym = (struct symbol *)
467 obstack_alloc (&objfile->symbol_obstack,
468 sizeof (struct symbol));
469
470 memset (sym, 0, sizeof (struct symbol));
471 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
472 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
473 SYMBOL_NAME (sym) =
474 obsavestring (name, pp - name, &objfile->symbol_obstack);
475 pp += 2;
c5aa993b 476 if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
c906108c
SS
477 {
478 /* I don't think the linker does this with functions,
479 so as far as I know this is never executed.
480 But it doesn't hurt to check. */
481 SYMBOL_TYPE (sym) =
482 lookup_function_type (read_type (&pp, objfile));
483 }
484 else
485 {
486 SYMBOL_TYPE (sym) = read_type (&pp, objfile);
487 }
488 add_symbol_to_list (sym, &global_symbols);
489 }
490 else
491 {
492 pp += 2;
c5aa993b 493 if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
c906108c
SS
494 {
495 SYMBOL_TYPE (sym) =
496 lookup_function_type (read_type (&pp, objfile));
497 }
498 else
499 {
500 SYMBOL_TYPE (sym) = read_type (&pp, objfile);
501 }
502 }
503 }
504 }
505}
c906108c 506\f
c5aa993b 507
c906108c
SS
508/* Read a number by which a type is referred to in dbx data,
509 or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
510 Just a single number N is equivalent to (0,N).
511 Return the two numbers by storing them in the vector TYPENUMS.
512 TYPENUMS will then be used as an argument to dbx_lookup_type.
513
514 Returns 0 for success, -1 for error. */
515
516static int
fba45db2 517read_type_number (register char **pp, register int *typenums)
c906108c
SS
518{
519 int nbits;
520 if (**pp == '(')
521 {
522 (*pp)++;
523 typenums[0] = read_huge_number (pp, ',', &nbits);
c5aa993b
JM
524 if (nbits != 0)
525 return -1;
c906108c 526 typenums[1] = read_huge_number (pp, ')', &nbits);
c5aa993b
JM
527 if (nbits != 0)
528 return -1;
c906108c
SS
529 }
530 else
531 {
532 typenums[0] = 0;
533 typenums[1] = read_huge_number (pp, 0, &nbits);
c5aa993b
JM
534 if (nbits != 0)
535 return -1;
c906108c
SS
536 }
537 return 0;
538}
c906108c 539\f
c5aa993b 540
c906108c
SS
541#define VISIBILITY_PRIVATE '0' /* Stabs character for private field */
542#define VISIBILITY_PROTECTED '1' /* Stabs character for protected fld */
543#define VISIBILITY_PUBLIC '2' /* Stabs character for public field */
544#define VISIBILITY_IGNORE '9' /* Optimized out or zero length */
545
546#define CFRONT_VISIBILITY_PRIVATE '2' /* Stabs character for private field */
547#define CFRONT_VISIBILITY_PUBLIC '1' /* Stabs character for public field */
548
549/* This code added to support parsing of ARM/Cfront stabs strings */
550
551/* Get substring from string up to char c, advance string pointer past
552 suibstring. */
553
c5aa993b 554static char *
fba45db2 555get_substring (char **p, int c)
c906108c
SS
556{
557 char *str;
558 str = *p;
559 *p = strchr (*p, c);
c5aa993b 560 if (*p)
c906108c
SS
561 {
562 **p = 0;
563 (*p)++;
564 }
c5aa993b 565 else
c906108c
SS
566 str = 0;
567 return str;
568}
569
570/* Physname gets strcat'd onto sname in order to recreate the mangled
571 name (see funtion gdb_mangle_name in gdbtypes.c). For cfront, make
572 the physname look like that of g++ - take out the initial mangling
573 eg: for sname="a" and fname="foo__1aFPFs_i" return "FPFs_i" */
574
c5aa993b 575static char *
fba45db2 576get_cfront_method_physname (char *fname)
c906108c
SS
577{
578 int len = 0;
579 /* FIXME would like to make this generic for g++ too, but
580 that is already handled in read_member_funcctions */
c5aa993b 581 char *p = fname;
c906108c
SS
582
583 /* search ahead to find the start of the mangled suffix */
c5aa993b
JM
584 if (*p == '_' && *(p + 1) == '_') /* compiler generated; probably a ctor/dtor */
585 p += 2;
586 while (p && (unsigned) ((p + 1) - fname) < strlen (fname) && *(p + 1) != '_')
c906108c 587 p = strchr (p, '_');
c5aa993b
JM
588 if (!(p && *p == '_' && *(p + 1) == '_'))
589 error ("Invalid mangled function name %s", fname);
590 p += 2; /* advance past '__' */
c906108c
SS
591
592 /* struct name length and name of type should come next; advance past it */
593 while (isdigit (*p))
594 {
595 len = len * 10 + (*p - '0');
596 p++;
597 }
598 p += len;
599
600 return p;
601}
602
603/* Read base classes within cfront class definition.
604 eg: A:ZcA;1@Bpub v2@Bvirpri;__ct__1AFv func__1AFv *sfunc__1AFv ;as__1A ;;
c5aa993b 605 ^^^^^^^^^^^^^^^^^^
c906108c 606
c5aa993b
JM
607 A:ZcA;;foopri__1AFv foopro__1AFv __ct__1AFv __ct__1AFRC1A foopub__1AFv ;;;
608 ^
609 */
c906108c
SS
610
611static int
fba45db2
KB
612read_cfront_baseclasses (struct field_info *fip, char **pp, struct type *type,
613 struct objfile *objfile)
c906108c 614{
c5aa993b
JM
615 static struct complaint msg_unknown =
616 {"\
c906108c 617 Unsupported token in stabs string %s.\n",
c5aa993b
JM
618 0, 0};
619 static struct complaint msg_notfound =
620 {"\
c906108c 621 Unable to find base type for %s.\n",
c5aa993b 622 0, 0};
c906108c 623 int bnum = 0;
c5aa993b 624 char *p;
c906108c
SS
625 int i;
626 struct nextfield *new;
627
628 if (**pp == ';') /* no base classes; return */
629 {
630 ++(*pp);
631 return 1;
632 }
633
634 /* first count base classes so we can allocate space before parsing */
635 for (p = *pp; p && *p && *p != ';'; p++)
636 {
637 if (*p == ' ')
638 bnum++;
639 }
c5aa993b 640 bnum++; /* add one more for last one */
c906108c
SS
641
642 /* now parse the base classes until we get to the start of the methods
643 (code extracted and munged from read_baseclasses) */
644 ALLOCATE_CPLUS_STRUCT_TYPE (type);
c5aa993b 645 TYPE_N_BASECLASSES (type) = bnum;
c906108c
SS
646
647 /* allocate space */
648 {
649 int num_bytes = B_BYTES (TYPE_N_BASECLASSES (type));
650 char *pointer;
651
652 pointer = (char *) TYPE_ALLOC (type, num_bytes);
653 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
654 }
655 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
656
657 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
658 {
659 new = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 660 make_cleanup (xfree, new);
c906108c 661 memset (new, 0, sizeof (struct nextfield));
c5aa993b
JM
662 new->next = fip->list;
663 fip->list = new;
664 FIELD_BITSIZE (new->field) = 0; /* this should be an unpacked field! */
c906108c
SS
665
666 STABS_CONTINUE (pp, objfile);
667
668 /* virtual? eg: v2@Bvir */
c5aa993b
JM
669 if (**pp == 'v')
670 {
671 SET_TYPE_FIELD_VIRTUAL (type, i);
672 ++(*pp);
c906108c
SS
673 }
674
675 /* access? eg: 2@Bvir */
c5aa993b 676 /* Note: protected inheritance not supported in cfront */
c906108c 677 switch (*(*pp)++)
c5aa993b
JM
678 {
679 case CFRONT_VISIBILITY_PRIVATE:
680 new->visibility = VISIBILITY_PRIVATE;
681 break;
682 case CFRONT_VISIBILITY_PUBLIC:
683 new->visibility = VISIBILITY_PUBLIC;
684 break;
685 default:
686 /* Bad visibility format. Complain and treat it as
687 public. */
688 {
689 static struct complaint msg =
690 {
691 "Unknown visibility `%c' for baseclass", 0, 0};
692 complain (&msg, new->visibility);
693 new->visibility = VISIBILITY_PUBLIC;
694 }
695 }
c906108c
SS
696
697 /* "@" comes next - eg: @Bvir */
c5aa993b
JM
698 if (**pp != '@')
699 {
700 complain (&msg_unknown, *pp);
701 return 1;
c906108c
SS
702 }
703 ++(*pp);
704
705
c5aa993b
JM
706 /* Set the bit offset of the portion of the object corresponding
707 to this baseclass. Always zero in the absence of
708 multiple inheritance. */
709 /* Unable to read bit position from stabs;
710 Assuming no multiple inheritance for now FIXME! */
711 /* We may have read this in the structure definition;
712 now we should fixup the members to be the actual base classes */
713 FIELD_BITPOS (new->field) = 0;
c906108c 714
c5aa993b
JM
715 /* Get the base class name and type */
716 {
717 char *bname; /* base class name */
718 struct symbol *bsym; /* base class */
719 char *p1, *p2;
720 p1 = strchr (*pp, ' ');
721 p2 = strchr (*pp, ';');
722 if (p1 < p2)
723 bname = get_substring (pp, ' ');
724 else
725 bname = get_substring (pp, ';');
726 if (!bname || !*bname)
c906108c 727 {
c5aa993b
JM
728 complain (&msg_unknown, *pp);
729 return 1;
730 }
731 /* FIXME! attach base info to type */
732 bsym = lookup_symbol (bname, 0, STRUCT_NAMESPACE, 0, 0); /*demangled_name */
733 if (bsym)
734 {
735 new->field.type = SYMBOL_TYPE (bsym);
736 new->field.name = type_name_no_tag (new->field.type);
c906108c 737 }
c5aa993b
JM
738 else
739 {
740 complain (&msg_notfound, *pp);
741 return 1;
742 }
743 }
c906108c
SS
744
745 /* If more base classes to parse, loop again.
746 We ate the last ' ' or ';' in get_substring,
747 so on exit we will have skipped the trailing ';' */
748 /* if invalid, return 0; add code to detect - FIXME! */
749 }
750 return 1;
751}
752
753/* read cfront member functions.
754 pp points to string starting with list of functions
755 eg: A:ZcA;1@Bpub v2@Bvirpri;__ct__1AFv func__1AFv *sfunc__1AFv ;as__1A ;;
c5aa993b
JM
756 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
757 A:ZcA;;foopri__1AFv foopro__1AFv __ct__1AFv __ct__1AFRC1A foopub__1AFv ;;;
758 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
759 */
c906108c
SS
760
761static int
fba45db2
KB
762read_cfront_member_functions (struct field_info *fip, char **pp,
763 struct type *type, struct objfile *objfile)
c906108c
SS
764{
765 /* This code extracted from read_member_functions
766 so as to do the similar thing for our funcs */
767
768 int nfn_fields = 0;
769 int length = 0;
770 /* Total number of member functions defined in this class. If the class
771 defines two `f' functions, and one `g' function, then this will have
772 the value 3. */
773 int total_length = 0;
774 int i;
775 struct next_fnfield
776 {
777 struct next_fnfield *next;
778 struct fn_field fn_field;
c5aa993b
JM
779 }
780 *sublist;
c906108c
SS
781 struct type *look_ahead_type;
782 struct next_fnfieldlist *new_fnlist;
783 struct next_fnfield *new_sublist;
784 char *main_fn_name;
c5aa993b
JM
785 char *fname;
786 struct symbol *ref_func = 0;
787
c906108c
SS
788 /* Process each list until we find the end of the member functions.
789 eg: p = "__ct__1AFv foo__1AFv ;;;" */
790
c5aa993b 791 STABS_CONTINUE (pp, objfile); /* handle \\ */
c906108c 792
c5aa993b 793 while (**pp != ';' && (fname = get_substring (pp, ' '), fname))
c906108c
SS
794 {
795 int is_static = 0;
796 int sublist_count = 0;
c5aa993b
JM
797 char *pname;
798 if (fname[0] == '*') /* static member */
799 {
800 is_static = 1;
801 sublist_count++;
802 fname++;
803 }
804 ref_func = lookup_symbol (fname, 0, VAR_NAMESPACE, 0, 0); /* demangled name */
805 if (!ref_func)
806 {
807 static struct complaint msg =
808 {"\
c906108c 809 Unable to find function symbol for %s\n",
c5aa993b 810 0, 0};
c906108c
SS
811 complain (&msg, fname);
812 continue;
813 }
814 sublist = NULL;
815 look_ahead_type = NULL;
816 length = 0;
c5aa993b 817
c906108c 818 new_fnlist = (struct next_fnfieldlist *)
c5aa993b 819 xmalloc (sizeof (struct next_fnfieldlist));
b8c9b27d 820 make_cleanup (xfree, new_fnlist);
c906108c 821 memset (new_fnlist, 0, sizeof (struct next_fnfieldlist));
c5aa993b 822
c906108c
SS
823 /* The following is code to work around cfront generated stabs.
824 The stabs contains full mangled name for each field.
825 We try to demangle the name and extract the field name out of it. */
826 {
c5aa993b
JM
827 char *dem, *dem_p, *dem_args;
828 int dem_len;
829 dem = cplus_demangle (fname, DMGL_ANSI | DMGL_PARAMS);
830 if (dem != NULL)
831 {
832 dem_p = strrchr (dem, ':');
833 if (dem_p != 0 && *(dem_p - 1) == ':')
834 dem_p++;
c906108c 835 /* get rid of args */
c5aa993b 836 dem_args = strchr (dem_p, '(');
c906108c
SS
837 if (dem_args == NULL)
838 dem_len = strlen (dem_p);
839 else
c5aa993b
JM
840 dem_len = dem_args - dem_p;
841 main_fn_name =
842 obsavestring (dem_p, dem_len, &objfile->type_obstack);
843 }
844 else
845 {
846 main_fn_name =
847 obsavestring (fname, strlen (fname), &objfile->type_obstack);
848 }
849 } /* end of code for cfront work around */
850
851 new_fnlist->fn_fieldlist.name = main_fn_name;
852
853/*-------------------------------------------------*/
854 /* Set up the sublists
855 Sublists are stuff like args, static, visibility, etc.
856 so in ARM, we have to set that info some other way.
857 Multiple sublists happen if overloading
858 eg: foo::26=##1;:;2A.;
859 In g++, we'd loop here thru all the sublists... */
860
861 new_sublist =
862 (struct next_fnfield *) xmalloc (sizeof (struct next_fnfield));
b8c9b27d 863 make_cleanup (xfree, new_sublist);
c5aa993b
JM
864 memset (new_sublist, 0, sizeof (struct next_fnfield));
865
866 /* eat 1; from :;2A.; */
867 new_sublist->fn_field.type = SYMBOL_TYPE (ref_func); /* normally takes a read_type */
868 /* Make this type look like a method stub for gdb */
869 TYPE_FLAGS (new_sublist->fn_field.type) |= TYPE_FLAG_STUB;
870 TYPE_CODE (new_sublist->fn_field.type) = TYPE_CODE_METHOD;
871
872 /* If this is just a stub, then we don't have the real name here. */
74a9bb82 873 if (TYPE_STUB (new_sublist->fn_field.type))
c5aa993b
JM
874 {
875 if (!TYPE_DOMAIN_TYPE (new_sublist->fn_field.type))
876 TYPE_DOMAIN_TYPE (new_sublist->fn_field.type) = type;
877 new_sublist->fn_field.is_stub = 1;
878 }
c906108c 879
c5aa993b
JM
880 /* physname used later in mangling; eg PFs_i,5 for foo__1aFPFs_i
881 physname gets strcat'd in order to recreate the onto mangled name */
882 pname = get_cfront_method_physname (fname);
883 new_sublist->fn_field.physname = savestring (pname, strlen (pname));
c906108c 884
c5aa993b
JM
885
886 /* Set this member function's visibility fields.
887 Unable to distinguish access from stabs definition!
c906108c 888 Assuming public for now. FIXME!
c5aa993b
JM
889 (for private, set new_sublist->fn_field.is_private = 1,
890 for public, set new_sublist->fn_field.is_protected = 1) */
891
892 /* Unable to distinguish const/volatile from stabs definition!
893 Assuming normal for now. FIXME! */
894
895 new_sublist->fn_field.is_const = 0;
896 new_sublist->fn_field.is_volatile = 0; /* volatile not implemented in cfront */
897
898 /* Set virtual/static function info
899 How to get vtable offsets ?
900 Assuming normal for now FIXME!!
901 For vtables, figure out from whence this virtual function came.
902 It may belong to virtual function table of
903 one of its baseclasses.
904 set:
905 new_sublist -> fn_field.voffset = vtable offset,
906 new_sublist -> fn_field.fcontext = look_ahead_type;
907 where look_ahead_type is type of baseclass */
908 if (is_static)
909 new_sublist->fn_field.voffset = VOFFSET_STATIC;
910 else /* normal member function. */
911 new_sublist->fn_field.voffset = 0;
912 new_sublist->fn_field.fcontext = 0;
913
914
915 /* Prepare new sublist */
916 new_sublist->next = sublist;
917 sublist = new_sublist;
918 length++;
919
920 /* In g++, we loop thu sublists - now we set from functions. */
921 new_fnlist->fn_fieldlist.fn_fields = (struct fn_field *)
922 obstack_alloc (&objfile->type_obstack,
923 sizeof (struct fn_field) * length);
924 memset (new_fnlist->fn_fieldlist.fn_fields, 0,
925 sizeof (struct fn_field) * length);
926 for (i = length; (i--, sublist); sublist = sublist->next)
927 {
928 new_fnlist->fn_fieldlist.fn_fields[i] = sublist->fn_field;
929 }
930
931 new_fnlist->fn_fieldlist.length = length;
932 new_fnlist->next = fip->fnlist;
933 fip->fnlist = new_fnlist;
934 nfn_fields++;
935 total_length += length;
936 STABS_CONTINUE (pp, objfile); /* handle \\ */
937 } /* end of loop */
c906108c
SS
938
939 if (nfn_fields)
940 {
941 /* type should already have space */
942 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
c5aa993b 943 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * nfn_fields);
c906108c 944 memset (TYPE_FN_FIELDLISTS (type), 0,
c5aa993b 945 sizeof (struct fn_fieldlist) * nfn_fields);
c906108c
SS
946 TYPE_NFN_FIELDS (type) = nfn_fields;
947 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
948 }
949
950 /* end of scope for reading member func */
951
952 /* eg: ";;" */
953
954 /* Skip trailing ';' and bump count of number of fields seen */
955 if (**pp == ';')
956 (*pp)++;
957 else
958 return 0;
959 return 1;
960}
961
962/* This routine fixes up partial cfront types that were created
963 while parsing the stabs. The main need for this function is
964 to add information such as methods to classes.
965 Examples of "p": "sA;;__ct__1AFv foo__1AFv ;;;" */
966int
fba45db2
KB
967resolve_cfront_continuation (struct objfile *objfile, struct symbol *sym,
968 char *p)
c906108c 969{
c5aa993b
JM
970 struct symbol *ref_sym = 0;
971 char *sname;
c906108c
SS
972 /* snarfed from read_struct_type */
973 struct field_info fi;
974 struct type *type;
975 struct cleanup *back_to;
976
977 /* Need to make sure that fi isn't gunna conflict with struct
978 in case struct already had some fnfs */
979 fi.list = NULL;
c5aa993b 980 fi.fnlist = NULL;
c906108c
SS
981 back_to = make_cleanup (null_cleanup, 0);
982
983 /* We only accept structs, classes and unions at the moment.
984 Other continuation types include t (typedef), r (long dbl), ...
985 We may want to add support for them as well;
986 right now they are handled by duplicating the symbol information
987 into the type information (see define_symbol) */
c5aa993b
JM
988 if (*p != 's' /* structs */
989 && *p != 'c' /* class */
990 && *p != 'u') /* union */
991 return 0; /* only handle C++ types */
992 p++;
c906108c
SS
993
994 /* Get symbol typs name and validate
995 eg: p = "A;;__ct__1AFv foo__1AFv ;;;" */
996 sname = get_substring (&p, ';');
c5aa993b 997 if (!sname || strcmp (sname, SYMBOL_NAME (sym)))
c906108c
SS
998 error ("Internal error: base symbol type name does not match\n");
999
1000 /* Find symbol's internal gdb reference using demangled_name.
1001 This is the real sym that we want;
1002 sym was a temp hack to make debugger happy */
c5aa993b
JM
1003 ref_sym = lookup_symbol (SYMBOL_NAME (sym), 0, STRUCT_NAMESPACE, 0, 0);
1004 type = SYMBOL_TYPE (ref_sym);
c906108c
SS
1005
1006
1007 /* Now read the baseclasses, if any, read the regular C struct or C++
1008 class member fields, attach the fields to the type, read the C++
1009 member functions, attach them to the type, and then read any tilde
1010 field (baseclass specifier for the class holding the main vtable). */
1011
1012 if (!read_cfront_baseclasses (&fi, &p, type, objfile)
c5aa993b
JM
1013 /* g++ does this next, but cfront already did this:
1014 || !read_struct_fields (&fi, &p, type, objfile) */
c906108c
SS
1015 || !copy_cfront_struct_fields (&fi, type, objfile)
1016 || !read_cfront_member_functions (&fi, &p, type, objfile)
1017 || !read_cfront_static_fields (&fi, &p, type, objfile)
1018 || !attach_fields_to_type (&fi, type, objfile)
1019 || !attach_fn_fields_to_type (&fi, type)
c5aa993b
JM
1020 /* g++ does this next, but cfront doesn't seem to have this:
1021 || !read_tilde_fields (&fi, &p, type, objfile) */
1022 )
c906108c
SS
1023 {
1024 type = error_type (&p, objfile);
1025 }
1026
1027 do_cleanups (back_to);
c5aa993b 1028 return 0;
c906108c
SS
1029}
1030/* End of code added to support parsing of ARM/Cfront stabs strings */
1031
1032
1033/* This routine fixes up symbol references/aliases to point to the original
1034 symbol definition. Returns 0 on failure, non-zero on success. */
1035
1036static int
fba45db2 1037resolve_symbol_reference (struct objfile *objfile, struct symbol *sym, char *p)
c906108c
SS
1038{
1039 int refnum;
c5aa993b 1040 struct symbol *ref_sym = 0;
c906108c
SS
1041 struct alias_list *alias;
1042
1043 /* If this is not a symbol reference return now. */
1044 if (*p != '#')
c5aa993b 1045 return 0;
c906108c
SS
1046
1047 /* Use "#<num>" as the name; we'll fix the name later.
1048 We stored the original symbol name as "#<id>=<name>"
1049 so we can now search for "#<id>" to resolving the reference.
1050 We'll fix the names later by removing the "#<id>" or "#<id>=" */
1051
c5aa993b 1052/*---------------------------------------------------------*/
c906108c
SS
1053 /* Get the reference id number, and
1054 advance p past the names so we can parse the rest.
c5aa993b
JM
1055 eg: id=2 for p : "2=", "2=z:r(0,1)" "2:r(0,1);l(#5,#6),l(#7,#4)" */
1056/*---------------------------------------------------------*/
c906108c
SS
1057
1058 /* This gets reference name from string. sym may not have a name. */
1059
1060 /* Get the reference number associated with the reference id in the
1061 gdb stab string. From that reference number, get the main/primary
1062 symbol for this alias. */
1063 refnum = process_reference (&p);
1064 ref_sym = ref_search (refnum);
1065 if (!ref_sym)
1066 {
1067 complain (&lrs_general_complaint, "symbol for reference not found");
1068 return 0;
1069 }
1070
1071 /* Parse the stab of the referencing symbol
1072 now that we have the referenced symbol.
1073 Add it as a new symbol and a link back to the referenced symbol.
1074 eg: p : "=", "=z:r(0,1)" ":r(0,1);l(#5,#6),l(#7,#4)" */
1075
1076
1077 /* If the stab symbol table and string contain:
c5aa993b
JM
1078 RSYM 0 5 00000000 868 #15=z:r(0,1)
1079 LBRAC 0 0 00000000 899 #5=
1080 SLINE 0 16 00000003 923 #6=
c906108c 1081 Then the same symbols can be later referenced by:
c5aa993b 1082 RSYM 0 5 00000000 927 #15:r(0,1);l(#5,#6)
c906108c
SS
1083 This is used in live range splitting to:
1084 1) specify that a symbol (#15) is actually just a new storage
c5aa993b 1085 class for a symbol (#15=z) which was previously defined.
c906108c 1086 2) specify that the beginning and ending ranges for a symbol
c5aa993b
JM
1087 (#15) are the values of the beginning (#5) and ending (#6)
1088 symbols. */
1089
1090 /* Read number as reference id.
1091 eg: p : "=", "=z:r(0,1)" ":r(0,1);l(#5,#6),l(#7,#4)" */
1092 /* FIXME! Might I want to use SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
1093 in case of "l(0,0)"? */
1094
1095/*--------------------------------------------------*/
1096 /* Add this symbol to the reference list. */
1097/*--------------------------------------------------*/
c906108c
SS
1098
1099 alias = (struct alias_list *) obstack_alloc (&objfile->type_obstack,
1100 sizeof (struct alias_list));
1101 if (!alias)
1102 {
1103 complain (&lrs_general_complaint, "Unable to allocate alias list memory");
1104 return 0;
1105 }
1106
1107 alias->next = 0;
1108 alias->sym = sym;
1109
1110 if (!SYMBOL_ALIASES (ref_sym))
1111 {
1112 SYMBOL_ALIASES (ref_sym) = alias;
1113 }
1114 else
1115 {
1116 struct alias_list *temp;
1117
1118 /* Get to the end of the list. */
1119 for (temp = SYMBOL_ALIASES (ref_sym);
1120 temp->next;
1121 temp = temp->next)
1122 ;
1123 temp->next = alias;
1124 }
1125
c5aa993b
JM
1126 /* Want to fix up name so that other functions (eg. valops)
1127 will correctly print the name.
1128 Don't add_symbol_to_list so that lookup_symbol won't find it.
1129 nope... needed for fixups. */
1130 SYMBOL_NAME (sym) = SYMBOL_NAME (ref_sym);
c906108c
SS
1131
1132 /* Done! */
1133 return 1;
1134}
1135
1136/* Structure for storing pointers to reference definitions for fast lookup
1137 during "process_later". */
1138
1139struct ref_map
1140{
1141 char *stabs;
1142 CORE_ADDR value;
1143 struct symbol *sym;
1144};
1145
1146#define MAX_CHUNK_REFS 100
1147#define REF_CHUNK_SIZE (MAX_CHUNK_REFS * sizeof (struct ref_map))
1148#define REF_MAP_SIZE(ref_chunk) ((ref_chunk) * REF_CHUNK_SIZE)
1149
c5aa993b 1150static struct ref_map *ref_map;
c906108c
SS
1151
1152/* Ptr to free cell in chunk's linked list. */
c5aa993b 1153static int ref_count = 0;
c906108c
SS
1154
1155/* Number of chunks malloced. */
1156static int ref_chunk = 0;
1157
7be570e7
JM
1158/* This file maintains a cache of stabs aliases found in the symbol
1159 table. If the symbol table changes, this cache must be cleared
1160 or we are left holding onto data in invalid obstacks. */
1161void
fba45db2 1162stabsread_clear_cache (void)
7be570e7
JM
1163{
1164 ref_count = 0;
1165 ref_chunk = 0;
1166}
1167
c906108c
SS
1168/* Create array of pointers mapping refids to symbols and stab strings.
1169 Add pointers to reference definition symbols and/or their values as we
1170 find them, using their reference numbers as our index.
1171 These will be used later when we resolve references. */
1172void
fba45db2 1173ref_add (int refnum, struct symbol *sym, char *stabs, CORE_ADDR value)
c906108c
SS
1174{
1175 if (ref_count == 0)
1176 ref_chunk = 0;
1177 if (refnum >= ref_count)
1178 ref_count = refnum + 1;
1179 if (ref_count > ref_chunk * MAX_CHUNK_REFS)
1180 {
c5aa993b 1181 int new_slots = ref_count - ref_chunk * MAX_CHUNK_REFS;
c906108c
SS
1182 int new_chunks = new_slots / MAX_CHUNK_REFS + 1;
1183 ref_map = (struct ref_map *)
1184 xrealloc (ref_map, REF_MAP_SIZE (ref_chunk + new_chunks));
1185 memset (ref_map + ref_chunk * MAX_CHUNK_REFS, 0, new_chunks * REF_CHUNK_SIZE);
1186 ref_chunk += new_chunks;
1187 }
1188 ref_map[refnum].stabs = stabs;
1189 ref_map[refnum].sym = sym;
1190 ref_map[refnum].value = value;
1191}
1192
1193/* Return defined sym for the reference REFNUM. */
1194struct symbol *
fba45db2 1195ref_search (int refnum)
c906108c
SS
1196{
1197 if (refnum < 0 || refnum > ref_count)
1198 return 0;
1199 return ref_map[refnum].sym;
1200}
1201
1202/* Return value for the reference REFNUM. */
1203
1204static CORE_ADDR
fba45db2 1205ref_search_value (int refnum)
c906108c
SS
1206{
1207 if (refnum < 0 || refnum > ref_count)
1208 return 0;
1209 return ref_map[refnum].value;
1210}
c5aa993b 1211
c906108c
SS
1212/* Parse a reference id in STRING and return the resulting
1213 reference number. Move STRING beyond the reference id. */
1214
c5aa993b 1215static int
fba45db2 1216process_reference (char **string)
c906108c
SS
1217{
1218 char *p;
1219 int refnum = 0;
1220
c5aa993b
JM
1221 if (**string != '#')
1222 return 0;
1223
c906108c
SS
1224 /* Advance beyond the initial '#'. */
1225 p = *string + 1;
1226
1227 /* Read number as reference id. */
1228 while (*p && isdigit (*p))
1229 {
1230 refnum = refnum * 10 + *p - '0';
1231 p++;
1232 }
1233 *string = p;
1234 return refnum;
1235}
1236
1237/* If STRING defines a reference, store away a pointer to the reference
1238 definition for later use. Return the reference number. */
1239
1240int
fba45db2 1241symbol_reference_defined (char **string)
c906108c
SS
1242{
1243 char *p = *string;
1244 int refnum = 0;
1245
1246 refnum = process_reference (&p);
1247
1248 /* Defining symbols end in '=' */
c5aa993b 1249 if (*p == '=')
c906108c 1250 {
c5aa993b 1251 /* Symbol is being defined here. */
c906108c
SS
1252 *string = p + 1;
1253 return refnum;
1254 }
1255 else
1256 {
1257 /* Must be a reference. Either the symbol has already been defined,
1258 or this is a forward reference to it. */
1259 *string = p;
1260 return -1;
1261 }
1262}
1263
1264/* ARGSUSED */
1265struct symbol *
fba45db2
KB
1266define_symbol (CORE_ADDR valu, char *string, int desc, int type,
1267 struct objfile *objfile)
c906108c
SS
1268{
1269 register struct symbol *sym;
1270 char *p = (char *) strchr (string, ':');
1271 int deftype;
1272 int synonym = 0;
1273 register int i;
1274
1275 /* We would like to eliminate nameless symbols, but keep their types.
1276 E.g. stab entry ":t10=*2" should produce a type 10, which is a pointer
1277 to type 2, but, should not create a symbol to address that type. Since
1278 the symbol will be nameless, there is no way any user can refer to it. */
1279
1280 int nameless;
1281
1282 /* Ignore syms with empty names. */
1283 if (string[0] == 0)
1284 return 0;
1285
1286 /* Ignore old-style symbols from cc -go */
1287 if (p == 0)
1288 return 0;
1289
1290 while (p[1] == ':')
1291 {
c5aa993b
JM
1292 p += 2;
1293 p = strchr (p, ':');
c906108c
SS
1294 }
1295
1296 /* If a nameless stab entry, all we need is the type, not the symbol.
1297 e.g. ":t10=*2" or a nameless enum like " :T16=ered:0,green:1,blue:2,;" */
1298 nameless = (p == string || ((string[0] == ' ') && (string[1] == ':')));
1299
c5aa993b
JM
1300 current_symbol = sym = (struct symbol *)
1301 obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol));
c906108c
SS
1302 memset (sym, 0, sizeof (struct symbol));
1303
1304 switch (type & N_TYPE)
1305 {
1306 case N_TEXT:
b8fbeb18 1307 SYMBOL_SECTION (sym) = SECT_OFF_TEXT (objfile);
c906108c
SS
1308 break;
1309 case N_DATA:
b8fbeb18 1310 SYMBOL_SECTION (sym) = SECT_OFF_DATA (objfile);
c906108c
SS
1311 break;
1312 case N_BSS:
b8fbeb18 1313 SYMBOL_SECTION (sym) = SECT_OFF_BSS (objfile);
c906108c
SS
1314 break;
1315 }
1316
1317 if (processing_gcc_compilation)
1318 {
1319 /* GCC 2.x puts the line number in desc. SunOS apparently puts in the
c5aa993b
JM
1320 number of bytes occupied by a type or object, which we ignore. */
1321 SYMBOL_LINE (sym) = desc;
c906108c
SS
1322 }
1323 else
1324 {
c5aa993b 1325 SYMBOL_LINE (sym) = 0; /* unknown */
c906108c
SS
1326 }
1327
1328 if (is_cplus_marker (string[0]))
1329 {
1330 /* Special GNU C++ names. */
1331 switch (string[1])
1332 {
c5aa993b
JM
1333 case 't':
1334 SYMBOL_NAME (sym) = obsavestring ("this", strlen ("this"),
1335 &objfile->symbol_obstack);
1336 break;
c906108c 1337
c5aa993b
JM
1338 case 'v': /* $vtbl_ptr_type */
1339 /* Was: SYMBOL_NAME (sym) = "vptr"; */
1340 goto normal;
c906108c 1341
c5aa993b
JM
1342 case 'e':
1343 SYMBOL_NAME (sym) = obsavestring ("eh_throw", strlen ("eh_throw"),
1344 &objfile->symbol_obstack);
1345 break;
c906108c 1346
c5aa993b
JM
1347 case '_':
1348 /* This was an anonymous type that was never fixed up. */
1349 goto normal;
c906108c
SS
1350
1351#ifdef STATIC_TRANSFORM_NAME
c5aa993b
JM
1352 case 'X':
1353 /* SunPRO (3.0 at least) static variable encoding. */
1354 goto normal;
c906108c
SS
1355#endif
1356
c5aa993b
JM
1357 default:
1358 complain (&unrecognized_cplus_name_complaint, string);
1359 goto normal; /* Do *something* with it */
c906108c
SS
1360 }
1361 }
1362 else if (string[0] == '#')
1363 {
1364 /* Special GNU C extension for referencing symbols. */
1365 char *s;
1366 int refnum, nlen;
1367
1368 /* If STRING defines a new reference id, then add it to the
c5aa993b
JM
1369 reference map. Else it must be referring to a previously
1370 defined symbol, so add it to the alias list of the previously
1371 defined symbol. */
c906108c
SS
1372 s = string;
1373 refnum = symbol_reference_defined (&s);
1374 if (refnum >= 0)
c5aa993b
JM
1375 ref_add (refnum, sym, string, SYMBOL_VALUE (sym));
1376 else if (!resolve_symbol_reference (objfile, sym, string))
1377 return NULL;
c906108c
SS
1378
1379 /* S..P contains the name of the symbol. We need to store
c5aa993b 1380 the correct name into SYMBOL_NAME. */
c906108c
SS
1381 nlen = p - s;
1382 if (refnum >= 0)
1383 {
1384 if (nlen > 0)
1385 {
1386 SYMBOL_NAME (sym) = (char *)
c5aa993b 1387 obstack_alloc (&objfile->symbol_obstack, nlen);
c906108c
SS
1388 strncpy (SYMBOL_NAME (sym), s, nlen);
1389 SYMBOL_NAME (sym)[nlen] = '\0';
1390 SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
1391 }
1392 else
1393 /* FIXME! Want SYMBOL_NAME (sym) = 0;
1394 Get error if leave name 0. So give it something. */
1395 {
1396 nlen = p - string;
c5aa993b
JM
1397 SYMBOL_NAME (sym) = (char *)
1398 obstack_alloc (&objfile->symbol_obstack, nlen);
c906108c
SS
1399 strncpy (SYMBOL_NAME (sym), string, nlen);
1400 SYMBOL_NAME (sym)[nlen] = '\0';
1401 SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
1402 }
1403 }
1404 /* Advance STRING beyond the reference id. */
1405 string = s;
1406 }
1407 else
1408 {
1409 normal:
c5aa993b
JM
1410 SYMBOL_LANGUAGE (sym) = current_subfile->language;
1411 SYMBOL_NAME (sym) = (char *)
1412 obstack_alloc (&objfile->symbol_obstack, ((p - string) + 1));
c906108c
SS
1413 /* Open-coded memcpy--saves function call time. */
1414 /* FIXME: Does it really? Try replacing with simple strcpy and
c5aa993b 1415 try it on an executable with a large symbol table. */
c906108c 1416 /* FIXME: considering that gcc can open code memcpy anyway, I
c5aa993b 1417 doubt it. xoxorich. */
c906108c
SS
1418 {
1419 register char *p1 = string;
1420 register char *p2 = SYMBOL_NAME (sym);
1421 while (p1 != p)
1422 {
1423 *p2++ = *p1++;
1424 }
1425 *p2++ = '\0';
1426 }
1427
1428 /* If this symbol is from a C++ compilation, then attempt to cache the
c5aa993b
JM
1429 demangled form for future reference. This is a typical time versus
1430 space tradeoff, that was decided in favor of time because it sped up
1431 C++ symbol lookups by a factor of about 20. */
c906108c
SS
1432
1433 SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
1434 }
1435 p++;
1436
1437 /* Determine the type of name being defined. */
1438#if 0
1439 /* Getting GDB to correctly skip the symbol on an undefined symbol
1440 descriptor and not ever dump core is a very dodgy proposition if
1441 we do things this way. I say the acorn RISC machine can just
1442 fix their compiler. */
1443 /* The Acorn RISC machine's compiler can put out locals that don't
1444 start with "234=" or "(3,4)=", so assume anything other than the
1445 deftypes we know how to handle is a local. */
1446 if (!strchr ("cfFGpPrStTvVXCR", *p))
1447#else
1448 if (isdigit (*p) || *p == '(' || *p == '-')
1449#endif
1450 deftype = 'l';
1451 else
1452 deftype = *p++;
1453
1454 switch (deftype)
1455 {
1456 case 'c':
1457 /* c is a special case, not followed by a type-number.
c5aa993b
JM
1458 SYMBOL:c=iVALUE for an integer constant symbol.
1459 SYMBOL:c=rVALUE for a floating constant symbol.
1460 SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
1461 e.g. "b:c=e6,0" for "const b = blob1"
1462 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
c906108c
SS
1463 if (*p != '=')
1464 {
1465 SYMBOL_CLASS (sym) = LOC_CONST;
1466 SYMBOL_TYPE (sym) = error_type (&p, objfile);
1467 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1468 add_symbol_to_list (sym, &file_symbols);
1469 return sym;
1470 }
1471 ++p;
1472 switch (*p++)
1473 {
1474 case 'r':
1475 {
1476 double d = atof (p);
1477 char *dbl_valu;
1478
1479 /* FIXME-if-picky-about-floating-accuracy: Should be using
1480 target arithmetic to get the value. real.c in GCC
1481 probably has the necessary code. */
1482
1483 /* FIXME: lookup_fundamental_type is a hack. We should be
1484 creating a type especially for the type of float constants.
1485 Problem is, what type should it be?
1486
1487 Also, what should the name of this type be? Should we
1488 be using 'S' constants (see stabs.texinfo) instead? */
1489
1490 SYMBOL_TYPE (sym) = lookup_fundamental_type (objfile,
1491 FT_DBL_PREC_FLOAT);
1492 dbl_valu = (char *)
c5aa993b 1493 obstack_alloc (&objfile->symbol_obstack,
c906108c 1494 TYPE_LENGTH (SYMBOL_TYPE (sym)));
96d2f608 1495 store_typed_floating (dbl_valu, SYMBOL_TYPE (sym), d);
c906108c
SS
1496 SYMBOL_VALUE_BYTES (sym) = dbl_valu;
1497 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
1498 }
1499 break;
1500 case 'i':
1501 {
1502 /* Defining integer constants this way is kind of silly,
1503 since 'e' constants allows the compiler to give not
1504 only the value, but the type as well. C has at least
1505 int, long, unsigned int, and long long as constant
1506 types; other languages probably should have at least
1507 unsigned as well as signed constants. */
1508
1509 /* We just need one int constant type for all objfiles.
1510 It doesn't depend on languages or anything (arguably its
1511 name should be a language-specific name for a type of
1512 that size, but I'm inclined to say that if the compiler
1513 wants a nice name for the type, it can use 'e'). */
1514 static struct type *int_const_type;
1515
1516 /* Yes, this is as long as a *host* int. That is because we
1517 use atoi. */
1518 if (int_const_type == NULL)
1519 int_const_type =
1520 init_type (TYPE_CODE_INT,
1521 sizeof (int) * HOST_CHAR_BIT / TARGET_CHAR_BIT, 0,
1522 "integer constant",
c5aa993b 1523 (struct objfile *) NULL);
c906108c
SS
1524 SYMBOL_TYPE (sym) = int_const_type;
1525 SYMBOL_VALUE (sym) = atoi (p);
1526 SYMBOL_CLASS (sym) = LOC_CONST;
1527 }
1528 break;
1529 case 'e':
1530 /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
1531 can be represented as integral.
1532 e.g. "b:c=e6,0" for "const b = blob1"
1533 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
1534 {
1535 SYMBOL_CLASS (sym) = LOC_CONST;
1536 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1537
1538 if (*p != ',')
1539 {
1540 SYMBOL_TYPE (sym) = error_type (&p, objfile);
1541 break;
1542 }
1543 ++p;
1544
1545 /* If the value is too big to fit in an int (perhaps because
1546 it is unsigned), or something like that, we silently get
1547 a bogus value. The type and everything else about it is
1548 correct. Ideally, we should be using whatever we have
1549 available for parsing unsigned and long long values,
1550 however. */
1551 SYMBOL_VALUE (sym) = atoi (p);
1552 }
1553 break;
1554 default:
1555 {
1556 SYMBOL_CLASS (sym) = LOC_CONST;
1557 SYMBOL_TYPE (sym) = error_type (&p, objfile);
1558 }
1559 }
1560 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1561 add_symbol_to_list (sym, &file_symbols);
1562 return sym;
1563
1564 case 'C':
1565 /* The name of a caught exception. */
1566 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1567 SYMBOL_CLASS (sym) = LOC_LABEL;
1568 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1569 SYMBOL_VALUE_ADDRESS (sym) = valu;
1570 add_symbol_to_list (sym, &local_symbols);
1571 break;
1572
1573 case 'f':
1574 /* A static function definition. */
1575 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1576 SYMBOL_CLASS (sym) = LOC_BLOCK;
1577 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1578 add_symbol_to_list (sym, &file_symbols);
1579 /* fall into process_function_types. */
1580
1581 process_function_types:
1582 /* Function result types are described as the result type in stabs.
c5aa993b
JM
1583 We need to convert this to the function-returning-type-X type
1584 in GDB. E.g. "int" is converted to "function returning int". */
c906108c
SS
1585 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_FUNC)
1586 SYMBOL_TYPE (sym) = lookup_function_type (SYMBOL_TYPE (sym));
1587
1588 /* All functions in C++ have prototypes. */
1589 if (SYMBOL_LANGUAGE (sym) == language_cplus)
1590 TYPE_FLAGS (SYMBOL_TYPE (sym)) |= TYPE_FLAG_PROTOTYPED;
1591
1592 /* fall into process_prototype_types */
1593
1594 process_prototype_types:
1595 /* Sun acc puts declared types of arguments here. */
1596 if (*p == ';')
1597 {
1598 struct type *ftype = SYMBOL_TYPE (sym);
1599 int nsemi = 0;
1600 int nparams = 0;
1601 char *p1 = p;
1602
1603 /* Obtain a worst case guess for the number of arguments
1604 by counting the semicolons. */
1605 while (*p1)
1606 {
1607 if (*p1++ == ';')
1608 nsemi++;
1609 }
1610
1611 /* Allocate parameter information fields and fill them in. */
1612 TYPE_FIELDS (ftype) = (struct field *)
1613 TYPE_ALLOC (ftype, nsemi * sizeof (struct field));
1614 while (*p++ == ';')
1615 {
1616 struct type *ptype;
1617
1618 /* A type number of zero indicates the start of varargs.
c5aa993b 1619 FIXME: GDB currently ignores vararg functions. */
c906108c
SS
1620 if (p[0] == '0' && p[1] == '\0')
1621 break;
1622 ptype = read_type (&p, objfile);
1623
1624 /* The Sun compilers mark integer arguments, which should
c5aa993b
JM
1625 be promoted to the width of the calling conventions, with
1626 a type which references itself. This type is turned into
1627 a TYPE_CODE_VOID type by read_type, and we have to turn
1628 it back into builtin_type_int here.
1629 FIXME: Do we need a new builtin_type_promoted_int_arg ? */
c906108c
SS
1630 if (TYPE_CODE (ptype) == TYPE_CODE_VOID)
1631 ptype = builtin_type_int;
8176bb6d
DJ
1632 TYPE_FIELD_TYPE (ftype, nparams) = ptype;
1633 TYPE_FIELD_ARTIFICIAL (ftype, nparams++) = 0;
c906108c
SS
1634 }
1635 TYPE_NFIELDS (ftype) = nparams;
1636 TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
1637 }
1638 break;
1639
1640 case 'F':
1641 /* A global function definition. */
1642 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1643 SYMBOL_CLASS (sym) = LOC_BLOCK;
1644 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1645 add_symbol_to_list (sym, &global_symbols);
1646 goto process_function_types;
1647
1648 case 'G':
1649 /* For a class G (global) symbol, it appears that the
c5aa993b
JM
1650 value is not correct. It is necessary to search for the
1651 corresponding linker definition to find the value.
1652 These definitions appear at the end of the namelist. */
c906108c
SS
1653 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1654 SYMBOL_CLASS (sym) = LOC_STATIC;
1655 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1656 /* Don't add symbol references to global_sym_chain.
c5aa993b
JM
1657 Symbol references don't have valid names and wont't match up with
1658 minimal symbols when the global_sym_chain is relocated.
1659 We'll fixup symbol references when we fixup the defining symbol. */
c906108c
SS
1660 if (SYMBOL_NAME (sym) && SYMBOL_NAME (sym)[0] != '#')
1661 {
c5aa993b
JM
1662 i = hashname (SYMBOL_NAME (sym));
1663 SYMBOL_VALUE_CHAIN (sym) = global_sym_chain[i];
1664 global_sym_chain[i] = sym;
c906108c
SS
1665 }
1666 add_symbol_to_list (sym, &global_symbols);
1667 break;
1668
1669 /* This case is faked by a conditional above,
c5aa993b
JM
1670 when there is no code letter in the dbx data.
1671 Dbx data never actually contains 'l'. */
c906108c
SS
1672 case 's':
1673 case 'l':
1674 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1675 SYMBOL_CLASS (sym) = LOC_LOCAL;
1676 SYMBOL_VALUE (sym) = valu;
1677 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1678 add_symbol_to_list (sym, &local_symbols);
1679 break;
1680
1681 case 'p':
1682 if (*p == 'F')
1683 /* pF is a two-letter code that means a function parameter in Fortran.
1684 The type-number specifies the type of the return value.
1685 Translate it into a pointer-to-function type. */
1686 {
1687 p++;
1688 SYMBOL_TYPE (sym)
1689 = lookup_pointer_type
c5aa993b 1690 (lookup_function_type (read_type (&p, objfile)));
c906108c
SS
1691 }
1692 else
1693 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1694
1695 /* Normally this is a parameter, a LOC_ARG. On the i960, it
c5aa993b 1696 can also be a LOC_LOCAL_ARG depending on symbol type. */
c906108c
SS
1697#ifndef DBX_PARM_SYMBOL_CLASS
1698#define DBX_PARM_SYMBOL_CLASS(type) LOC_ARG
1699#endif
1700
1701 SYMBOL_CLASS (sym) = DBX_PARM_SYMBOL_CLASS (type);
1702 SYMBOL_VALUE (sym) = valu;
1703 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1704 add_symbol_to_list (sym, &local_symbols);
1705
d7449b42 1706 if (TARGET_BYTE_ORDER != BFD_ENDIAN_BIG)
c906108c
SS
1707 {
1708 /* On little-endian machines, this crud is never necessary,
1709 and, if the extra bytes contain garbage, is harmful. */
1710 break;
1711 }
1712
1713 /* If it's gcc-compiled, if it says `short', believe it. */
1714 if (processing_gcc_compilation || BELIEVE_PCC_PROMOTION)
1715 break;
1716
7a292a7a
SS
1717 if (!BELIEVE_PCC_PROMOTION)
1718 {
1719 /* This is the signed type which arguments get promoted to. */
1720 static struct type *pcc_promotion_type;
1721 /* This is the unsigned type which arguments get promoted to. */
1722 static struct type *pcc_unsigned_promotion_type;
c5aa993b 1723
7a292a7a
SS
1724 /* Call it "int" because this is mainly C lossage. */
1725 if (pcc_promotion_type == NULL)
1726 pcc_promotion_type =
1727 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1728 0, "int", NULL);
c5aa993b 1729
7a292a7a
SS
1730 if (pcc_unsigned_promotion_type == NULL)
1731 pcc_unsigned_promotion_type =
1732 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1733 TYPE_FLAG_UNSIGNED, "unsigned int", NULL);
c5aa993b 1734
7a292a7a
SS
1735 if (BELIEVE_PCC_PROMOTION_TYPE)
1736 {
1737 /* This is defined on machines (e.g. sparc) where we
c5aa993b
JM
1738 should believe the type of a PCC 'short' argument,
1739 but shouldn't believe the address (the address is the
1740 address of the corresponding int).
1741
1742 My guess is that this correction, as opposed to
1743 changing the parameter to an 'int' (as done below,
1744 for PCC on most machines), is the right thing to do
1745 on all machines, but I don't want to risk breaking
1746 something that already works. On most PCC machines,
1747 the sparc problem doesn't come up because the calling
1748 function has to zero the top bytes (not knowing
1749 whether the called function wants an int or a short),
1750 so there is little practical difference between an
1751 int and a short (except perhaps what happens when the
1752 GDB user types "print short_arg = 0x10000;").
1753
1754 Hacked for SunOS 4.1 by gnu@cygnus.com. In 4.1, the
1755 compiler actually produces the correct address (we
1756 don't need to fix it up). I made this code adapt so
1757 that it will offset the symbol if it was pointing at
1758 an int-aligned location and not otherwise. This way
1759 you can use the same gdb for 4.0.x and 4.1 systems.
1760
1761 If the parameter is shorter than an int, and is
1762 integral (e.g. char, short, or unsigned equivalent),
1763 and is claimed to be passed on an integer boundary,
1764 don't believe it! Offset the parameter's address to
1765 the tail-end of that integer. */
1766
7a292a7a
SS
1767 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (pcc_promotion_type)
1768 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT
c5aa993b 1769 && 0 == SYMBOL_VALUE (sym) % TYPE_LENGTH (pcc_promotion_type))
7a292a7a
SS
1770 {
1771 SYMBOL_VALUE (sym) += TYPE_LENGTH (pcc_promotion_type)
1772 - TYPE_LENGTH (SYMBOL_TYPE (sym));
1773 }
1774 break;
1775 }
1776 else
1777 {
1778 /* If PCC says a parameter is a short or a char,
c5aa993b 1779 it is really an int. */
7a292a7a
SS
1780 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (pcc_promotion_type)
1781 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
1782 {
1783 SYMBOL_TYPE (sym) =
1784 TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1785 ? pcc_unsigned_promotion_type
1786 : pcc_promotion_type;
1787 }
1788 break;
1789 }
1790 }
c906108c
SS
1791
1792 case 'P':
1793 /* acc seems to use P to declare the prototypes of functions that
1794 are referenced by this file. gdb is not prepared to deal
1795 with this extra information. FIXME, it ought to. */
1796 if (type == N_FUN)
1797 {
1798 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1799 goto process_prototype_types;
1800 }
c5aa993b 1801 /*FALLTHROUGH */
c906108c
SS
1802
1803 case 'R':
1804 /* Parameter which is in a register. */
1805 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1806 SYMBOL_CLASS (sym) = LOC_REGPARM;
1807 SYMBOL_VALUE (sym) = STAB_REG_TO_REGNUM (valu);
64485362 1808 if (SYMBOL_VALUE (sym) >= NUM_REGS + NUM_PSEUDO_REGS)
c906108c 1809 {
64485362
AC
1810 complain (&reg_value_complaint, SYMBOL_VALUE (sym),
1811 NUM_REGS + NUM_PSEUDO_REGS,
c906108c 1812 SYMBOL_SOURCE_NAME (sym));
c5aa993b 1813 SYMBOL_VALUE (sym) = SP_REGNUM; /* Known safe, though useless */
c906108c
SS
1814 }
1815 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1816 add_symbol_to_list (sym, &local_symbols);
1817 break;
1818
1819 case 'r':
1820 /* Register variable (either global or local). */
1821 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1822 SYMBOL_CLASS (sym) = LOC_REGISTER;
1823 SYMBOL_VALUE (sym) = STAB_REG_TO_REGNUM (valu);
64485362 1824 if (SYMBOL_VALUE (sym) >= NUM_REGS + NUM_PSEUDO_REGS)
c906108c 1825 {
64485362
AC
1826 complain (&reg_value_complaint, SYMBOL_VALUE (sym),
1827 NUM_REGS + NUM_PSEUDO_REGS,
c906108c 1828 SYMBOL_SOURCE_NAME (sym));
c5aa993b 1829 SYMBOL_VALUE (sym) = SP_REGNUM; /* Known safe, though useless */
c906108c
SS
1830 }
1831 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1832 if (within_function)
1833 {
1834 /* Sun cc uses a pair of symbols, one 'p' and one 'r' with the same
1835 name to represent an argument passed in a register.
1836 GCC uses 'P' for the same case. So if we find such a symbol pair
1837 we combine it into one 'P' symbol. For Sun cc we need to do this
1838 regardless of REG_STRUCT_HAS_ADDR, because the compiler puts out
1839 the 'p' symbol even if it never saves the argument onto the stack.
1840
1841 On most machines, we want to preserve both symbols, so that
1842 we can still get information about what is going on with the
1843 stack (VAX for computing args_printed, using stack slots instead
1844 of saved registers in backtraces, etc.).
1845
1846 Note that this code illegally combines
c5aa993b 1847 main(argc) struct foo argc; { register struct foo argc; }
c906108c
SS
1848 but this case is considered pathological and causes a warning
1849 from a decent compiler. */
1850
1851 if (local_symbols
1852 && local_symbols->nsyms > 0
1853#ifndef USE_REGISTER_NOT_ARG
d03e67c9 1854 && REG_STRUCT_HAS_ADDR_P ()
c906108c
SS
1855 && REG_STRUCT_HAS_ADDR (processing_gcc_compilation,
1856 SYMBOL_TYPE (sym))
1857 && (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
1858 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_UNION
1859 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_SET
1860 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_BITSTRING)
1861#endif
c5aa993b 1862 )
c906108c
SS
1863 {
1864 struct symbol *prev_sym;
1865 prev_sym = local_symbols->symbol[local_symbols->nsyms - 1];
1866 if ((SYMBOL_CLASS (prev_sym) == LOC_REF_ARG
1867 || SYMBOL_CLASS (prev_sym) == LOC_ARG)
c5aa993b 1868 && STREQ (SYMBOL_NAME (prev_sym), SYMBOL_NAME (sym)))
c906108c
SS
1869 {
1870 SYMBOL_CLASS (prev_sym) = LOC_REGPARM;
1871 /* Use the type from the LOC_REGISTER; that is the type
1872 that is actually in that register. */
1873 SYMBOL_TYPE (prev_sym) = SYMBOL_TYPE (sym);
1874 SYMBOL_VALUE (prev_sym) = SYMBOL_VALUE (sym);
1875 sym = prev_sym;
1876 break;
1877 }
1878 }
c5aa993b 1879 add_symbol_to_list (sym, &local_symbols);
c906108c
SS
1880 }
1881 else
c5aa993b 1882 add_symbol_to_list (sym, &file_symbols);
c906108c
SS
1883 break;
1884
1885 case 'S':
1886 /* Static symbol at top level of file */
1887 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1888 SYMBOL_CLASS (sym) = LOC_STATIC;
1889 SYMBOL_VALUE_ADDRESS (sym) = valu;
1890#ifdef STATIC_TRANSFORM_NAME
1891 if (IS_STATIC_TRANSFORM_NAME (SYMBOL_NAME (sym)))
c5aa993b
JM
1892 {
1893 struct minimal_symbol *msym;
1894 msym = lookup_minimal_symbol (SYMBOL_NAME (sym), NULL, objfile);
1895 if (msym != NULL)
1896 {
1897 SYMBOL_NAME (sym) = STATIC_TRANSFORM_NAME (SYMBOL_NAME (sym));
1898 SYMBOL_VALUE_ADDRESS (sym) = SYMBOL_VALUE_ADDRESS (msym);
1899 }
1900 }
c906108c
SS
1901#endif
1902 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1903 add_symbol_to_list (sym, &file_symbols);
1904 break;
1905
1906 case 't':
1907 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1908
1909 /* For a nameless type, we don't want a create a symbol, thus we
c5aa993b
JM
1910 did not use `sym'. Return without further processing. */
1911 if (nameless)
1912 return NULL;
c906108c
SS
1913
1914 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1915 SYMBOL_VALUE (sym) = valu;
1916 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1917 /* C++ vagaries: we may have a type which is derived from
c5aa993b
JM
1918 a base type which did not have its name defined when the
1919 derived class was output. We fill in the derived class's
1920 base part member's name here in that case. */
c906108c
SS
1921 if (TYPE_NAME (SYMBOL_TYPE (sym)) != NULL)
1922 if ((TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
1923 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_UNION)
1924 && TYPE_N_BASECLASSES (SYMBOL_TYPE (sym)))
1925 {
1926 int j;
1927 for (j = TYPE_N_BASECLASSES (SYMBOL_TYPE (sym)) - 1; j >= 0; j--)
1928 if (TYPE_BASECLASS_NAME (SYMBOL_TYPE (sym), j) == 0)
1929 TYPE_BASECLASS_NAME (SYMBOL_TYPE (sym), j) =
1930 type_name_no_tag (TYPE_BASECLASS (SYMBOL_TYPE (sym), j));
1931 }
1932
1933 if (TYPE_NAME (SYMBOL_TYPE (sym)) == NULL)
1934 {
1935 /* gcc-2.6 or later (when using -fvtable-thunks)
1936 emits a unique named type for a vtable entry.
1937 Some gdb code depends on that specific name. */
1938 extern const char vtbl_ptr_name[];
1939
1940 if ((TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1941 && strcmp (SYMBOL_NAME (sym), vtbl_ptr_name))
1942 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
1943 {
1944 /* If we are giving a name to a type such as "pointer to
c5aa993b
JM
1945 foo" or "function returning foo", we better not set
1946 the TYPE_NAME. If the program contains "typedef char
1947 *caddr_t;", we don't want all variables of type char
1948 * to print as caddr_t. This is not just a
1949 consequence of GDB's type management; PCC and GCC (at
1950 least through version 2.4) both output variables of
1951 either type char * or caddr_t with the type number
1952 defined in the 't' symbol for caddr_t. If a future
1953 compiler cleans this up it GDB is not ready for it
1954 yet, but if it becomes ready we somehow need to
1955 disable this check (without breaking the PCC/GCC2.4
1956 case).
1957
1958 Sigh.
1959
1960 Fortunately, this check seems not to be necessary
1961 for anything except pointers or functions. */
49d97c60
EZ
1962 /* ezannoni: 2000-10-26. This seems to apply for
1963 versions of gcc older than 2.8. This was the original
1964 problem: with the following code gdb would tell that
1965 the type for name1 is caddr_t, and func is char()
1966 typedef char *caddr_t;
1967 char *name2;
1968 struct x
1969 {
1970 char *name1;
1971 } xx;
1972 char *func()
1973 {
1974 }
1975 main () {}
1976 */
1977
1978 /* Pascal accepts names for pointer types. */
1979 if (current_subfile->language == language_pascal)
1980 {
1981 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_NAME (sym);
1982 }
c906108c
SS
1983 }
1984 else
1985 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_NAME (sym);
1986 }
1987
1988 add_symbol_to_list (sym, &file_symbols);
1989 break;
1990
1991 case 'T':
1992 /* Struct, union, or enum tag. For GNU C++, this can be be followed
c5aa993b 1993 by 't' which means we are typedef'ing it as well. */
c906108c
SS
1994 synonym = *p == 't';
1995
1996 if (synonym)
1997 p++;
1998 /* The semantics of C++ state that "struct foo { ... }" also defines
c5aa993b
JM
1999 a typedef for "foo". Unfortunately, cfront never makes the typedef
2000 when translating C++ into C. We make the typedef here so that
2001 "ptype foo" works as expected for cfront translated code. */
c906108c
SS
2002 else if (current_subfile->language == language_cplus)
2003 synonym = 1;
2004
2005 SYMBOL_TYPE (sym) = read_type (&p, objfile);
2006
2007 /* For a nameless type, we don't want a create a symbol, thus we
c5aa993b
JM
2008 did not use `sym'. Return without further processing. */
2009 if (nameless)
2010 return NULL;
c906108c
SS
2011
2012 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
2013 SYMBOL_VALUE (sym) = valu;
2014 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
2015 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
2016 TYPE_TAG_NAME (SYMBOL_TYPE (sym))
c5aa993b 2017 = obconcat (&objfile->type_obstack, "", "", SYMBOL_NAME (sym));
c906108c
SS
2018 add_symbol_to_list (sym, &file_symbols);
2019
2020 if (synonym)
2021 {
2022 /* Clone the sym and then modify it. */
2023 register struct symbol *typedef_sym = (struct symbol *)
c5aa993b 2024 obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol));
c906108c
SS
2025 *typedef_sym = *sym;
2026 SYMBOL_CLASS (typedef_sym) = LOC_TYPEDEF;
2027 SYMBOL_VALUE (typedef_sym) = valu;
2028 SYMBOL_NAMESPACE (typedef_sym) = VAR_NAMESPACE;
2029 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
2030 TYPE_NAME (SYMBOL_TYPE (sym))
c5aa993b 2031 = obconcat (&objfile->type_obstack, "", "", SYMBOL_NAME (sym));
c906108c
SS
2032 add_symbol_to_list (typedef_sym, &file_symbols);
2033 }
2034 break;
2035
2036 case 'V':
2037 /* Static symbol of local scope */
2038 SYMBOL_TYPE (sym) = read_type (&p, objfile);
2039 SYMBOL_CLASS (sym) = LOC_STATIC;
2040 SYMBOL_VALUE_ADDRESS (sym) = valu;
2041#ifdef STATIC_TRANSFORM_NAME
2042 if (IS_STATIC_TRANSFORM_NAME (SYMBOL_NAME (sym)))
c5aa993b
JM
2043 {
2044 struct minimal_symbol *msym;
2045 msym = lookup_minimal_symbol (SYMBOL_NAME (sym), NULL, objfile);
2046 if (msym != NULL)
2047 {
2048 SYMBOL_NAME (sym) = STATIC_TRANSFORM_NAME (SYMBOL_NAME (sym));
2049 SYMBOL_VALUE_ADDRESS (sym) = SYMBOL_VALUE_ADDRESS (msym);
2050 }
2051 }
c906108c
SS
2052#endif
2053 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2054 if (os9k_stabs)
2055 add_symbol_to_list (sym, &global_symbols);
2056 else
2057 add_symbol_to_list (sym, &local_symbols);
2058 break;
2059
2060 case 'v':
2061 /* Reference parameter */
2062 SYMBOL_TYPE (sym) = read_type (&p, objfile);
2063 SYMBOL_CLASS (sym) = LOC_REF_ARG;
2064 SYMBOL_VALUE (sym) = valu;
2065 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2066 add_symbol_to_list (sym, &local_symbols);
2067 break;
2068
2069 case 'a':
2070 /* Reference parameter which is in a register. */
2071 SYMBOL_TYPE (sym) = read_type (&p, objfile);
2072 SYMBOL_CLASS (sym) = LOC_REGPARM_ADDR;
2073 SYMBOL_VALUE (sym) = STAB_REG_TO_REGNUM (valu);
64485362 2074 if (SYMBOL_VALUE (sym) >= NUM_REGS + NUM_PSEUDO_REGS)
c906108c 2075 {
64485362
AC
2076 complain (&reg_value_complaint, SYMBOL_VALUE (sym),
2077 NUM_REGS + NUM_PSEUDO_REGS,
c906108c 2078 SYMBOL_SOURCE_NAME (sym));
c5aa993b 2079 SYMBOL_VALUE (sym) = SP_REGNUM; /* Known safe, though useless */
c906108c
SS
2080 }
2081 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2082 add_symbol_to_list (sym, &local_symbols);
2083 break;
2084
2085 case 'X':
2086 /* This is used by Sun FORTRAN for "function result value".
c5aa993b
JM
2087 Sun claims ("dbx and dbxtool interfaces", 2nd ed)
2088 that Pascal uses it too, but when I tried it Pascal used
2089 "x:3" (local symbol) instead. */
c906108c
SS
2090 SYMBOL_TYPE (sym) = read_type (&p, objfile);
2091 SYMBOL_CLASS (sym) = LOC_LOCAL;
2092 SYMBOL_VALUE (sym) = valu;
2093 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2094 add_symbol_to_list (sym, &local_symbols);
2095 break;
2096
c5aa993b
JM
2097 /* New code added to support cfront stabs strings.
2098 Note: case 'P' already handled above */
c906108c
SS
2099 case 'Z':
2100 /* Cfront type continuation coming up!
c5aa993b
JM
2101 Find the original definition and add to it.
2102 We'll have to do this for the typedef too,
2103 since we cloned the symbol to define a type in read_type.
2104 Stabs info examples:
2105 __1C :Ztl
2106 foo__1CFv :ZtF (first def foo__1CFv:F(0,3);(0,24))
2107 C:ZsC;;__ct__1CFv func1__1CFv func2__1CFv ... ;;;
2108 where C is the name of the class.
2109 Unfortunately, we can't lookup the original symbol yet 'cuz
2110 we haven't finished reading all the symbols.
2111 Instead, we save it for processing later */
c906108c 2112 process_later (sym, p, resolve_cfront_continuation);
c5aa993b
JM
2113 SYMBOL_TYPE (sym) = error_type (&p, objfile); /* FIXME! change later */
2114 SYMBOL_CLASS (sym) = LOC_CONST;
2115 SYMBOL_VALUE (sym) = 0;
c906108c
SS
2116 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2117 /* Don't add to list - we'll delete it later when
2118 we add the continuation to the real sym */
2119 return sym;
2120 /* End of new code added to support cfront stabs strings */
2121
2122 default:
2123 SYMBOL_TYPE (sym) = error_type (&p, objfile);
2124 SYMBOL_CLASS (sym) = LOC_CONST;
2125 SYMBOL_VALUE (sym) = 0;
2126 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2127 add_symbol_to_list (sym, &file_symbols);
2128 break;
2129 }
2130
2131 /* When passing structures to a function, some systems sometimes pass
2132 the address in a register, not the structure itself. */
2133
d03e67c9
AC
2134 if (REG_STRUCT_HAS_ADDR_P ()
2135 && REG_STRUCT_HAS_ADDR (processing_gcc_compilation, SYMBOL_TYPE (sym))
2136 && (SYMBOL_CLASS (sym) == LOC_REGPARM || SYMBOL_CLASS (sym) == LOC_ARG))
c906108c
SS
2137 {
2138 struct type *symbol_type = check_typedef (SYMBOL_TYPE (sym));
2139
2140 if ((TYPE_CODE (symbol_type) == TYPE_CODE_STRUCT)
2141 || (TYPE_CODE (symbol_type) == TYPE_CODE_UNION)
2142 || (TYPE_CODE (symbol_type) == TYPE_CODE_BITSTRING)
2143 || (TYPE_CODE (symbol_type) == TYPE_CODE_SET))
2144 {
2145 /* If REG_STRUCT_HAS_ADDR yields non-zero we have to convert
2146 LOC_REGPARM to LOC_REGPARM_ADDR for structures and unions. */
2147 if (SYMBOL_CLASS (sym) == LOC_REGPARM)
2148 SYMBOL_CLASS (sym) = LOC_REGPARM_ADDR;
2149 /* Likewise for converting LOC_ARG to LOC_REF_ARG (for the 7th
2150 and subsequent arguments on the sparc, for example). */
2151 else if (SYMBOL_CLASS (sym) == LOC_ARG)
2152 SYMBOL_CLASS (sym) = LOC_REF_ARG;
2153 }
2154 }
2155
2156 /* Is there more to parse? For example LRS/alias information? */
2157 while (*p && *p == ';')
2158 {
2159 p++;
7a292a7a 2160 if (*p && p[0] == 'l' && p[1] == '(')
c5aa993b
JM
2161 {
2162 /* GNU extensions for live range splitting may be appended to
2163 the end of the stab string. eg. "l(#1,#2);l(#3,#5)" */
c906108c
SS
2164
2165 /* Resolve the live range and add it to SYM's live range list. */
2166 if (!resolve_live_range (objfile, sym, p))
2167 return NULL;
2168
2169 /* Find end of live range info. */
2170 p = strchr (p, ')');
c5aa993b 2171 if (!*p || *p != ')')
c906108c
SS
2172 {
2173 complain (&lrs_general_complaint, "live range format not recognized");
2174 return NULL;
2175 }
c5aa993b
JM
2176 p++;
2177 }
c906108c
SS
2178 }
2179 return sym;
2180}
2181
2182/* Add the live range found in P to the symbol SYM in objfile OBJFILE. Returns
2183 non-zero on success, zero otherwise. */
2184
2185static int
fba45db2 2186resolve_live_range (struct objfile *objfile, struct symbol *sym, char *p)
c906108c
SS
2187{
2188 int refnum;
2189 CORE_ADDR start, end;
2190
2191 /* Sanity check the beginning of the stabs string. */
2192 if (!*p || *p != 'l')
2193 {
2194 complain (&lrs_general_complaint, "live range string 1");
2195 return 0;
2196 }
2197 p++;
2198
2199 if (!*p || *p != '(')
2200 {
2201 complain (&lrs_general_complaint, "live range string 2");
2202 return 0;
2203 }
2204 p++;
c5aa993b 2205
c906108c
SS
2206 /* Get starting value of range and advance P past the reference id.
2207
2208 ?!? In theory, the process_reference should never fail, but we should
2209 catch that case just in case the compiler scrogged the stabs. */
2210 refnum = process_reference (&p);
2211 start = ref_search_value (refnum);
2212 if (!start)
2213 {
2214 complain (&lrs_general_complaint, "Live range symbol not found 1");
2215 return 0;
2216 }
2217
2218 if (!*p || *p != ',')
2219 {
2220 complain (&lrs_general_complaint, "live range string 3");
2221 return 0;
2222 }
2223 p++;
2224
2225 /* Get ending value of range and advance P past the reference id.
2226
2227 ?!? In theory, the process_reference should never fail, but we should
2228 catch that case just in case the compiler scrogged the stabs. */
2229 refnum = process_reference (&p);
2230 end = ref_search_value (refnum);
2231 if (!end)
2232 {
2233 complain (&lrs_general_complaint, "Live range symbol not found 2");
2234 return 0;
2235 }
2236
2237 if (!*p || *p != ')')
2238 {
2239 complain (&lrs_general_complaint, "live range string 4");
2240 return 0;
2241 }
2242
2243 /* Now that we know the bounds of the range, add it to the
2244 symbol. */
2245 add_live_range (objfile, sym, start, end);
2246
2247 return 1;
2248}
2249
2250/* Add a new live range defined by START and END to the symbol SYM
2251 in objfile OBJFILE. */
2252
2253static void
fba45db2
KB
2254add_live_range (struct objfile *objfile, struct symbol *sym, CORE_ADDR start,
2255 CORE_ADDR end)
c906108c
SS
2256{
2257 struct range_list *r, *rs;
2258
2259 if (start >= end)
2260 {
2261 complain (&lrs_general_complaint, "end of live range follows start");
2262 return;
2263 }
2264
2265 /* Alloc new live range structure. */
2266 r = (struct range_list *)
c5aa993b 2267 obstack_alloc (&objfile->type_obstack,
c906108c
SS
2268 sizeof (struct range_list));
2269 r->start = start;
2270 r->end = end;
2271 r->next = 0;
2272
2273 /* Append this range to the symbol's range list. */
2274 if (!SYMBOL_RANGES (sym))
2275 SYMBOL_RANGES (sym) = r;
2276 else
2277 {
2278 /* Get the last range for the symbol. */
2279 for (rs = SYMBOL_RANGES (sym); rs->next; rs = rs->next)
2280 ;
2281 rs->next = r;
2282 }
2283}
c906108c 2284\f
c5aa993b 2285
c906108c
SS
2286/* Skip rest of this symbol and return an error type.
2287
2288 General notes on error recovery: error_type always skips to the
2289 end of the symbol (modulo cretinous dbx symbol name continuation).
2290 Thus code like this:
2291
2292 if (*(*pp)++ != ';')
c5aa993b 2293 return error_type (pp, objfile);
c906108c
SS
2294
2295 is wrong because if *pp starts out pointing at '\0' (typically as the
2296 result of an earlier error), it will be incremented to point to the
2297 start of the next symbol, which might produce strange results, at least
2298 if you run off the end of the string table. Instead use
2299
2300 if (**pp != ';')
c5aa993b 2301 return error_type (pp, objfile);
c906108c
SS
2302 ++*pp;
2303
2304 or
2305
2306 if (**pp != ';')
c5aa993b 2307 foo = error_type (pp, objfile);
c906108c 2308 else
c5aa993b 2309 ++*pp;
c906108c
SS
2310
2311 And in case it isn't obvious, the point of all this hair is so the compiler
2312 can define new types and new syntaxes, and old versions of the
2313 debugger will be able to read the new symbol tables. */
2314
2315static struct type *
fba45db2 2316error_type (char **pp, struct objfile *objfile)
c906108c
SS
2317{
2318 complain (&error_type_complaint);
2319 while (1)
2320 {
2321 /* Skip to end of symbol. */
2322 while (**pp != '\0')
2323 {
2324 (*pp)++;
2325 }
2326
2327 /* Check for and handle cretinous dbx symbol name continuation! */
2328 if ((*pp)[-1] == '\\' || (*pp)[-1] == '?')
2329 {
2330 *pp = next_symbol_text (objfile);
2331 }
2332 else
2333 {
2334 break;
2335 }
2336 }
2337 return (builtin_type_error);
2338}
c906108c 2339\f
c5aa993b 2340
c906108c
SS
2341/* Read type information or a type definition; return the type. Even
2342 though this routine accepts either type information or a type
2343 definition, the distinction is relevant--some parts of stabsread.c
2344 assume that type information starts with a digit, '-', or '(' in
2345 deciding whether to call read_type. */
2346
2347struct type *
fba45db2 2348read_type (register char **pp, struct objfile *objfile)
c906108c
SS
2349{
2350 register struct type *type = 0;
2351 struct type *type1;
2352 int typenums[2];
2353 char type_descriptor;
2354
2355 /* Size in bits of type if specified by a type attribute, or -1 if
2356 there is no size attribute. */
2357 int type_size = -1;
2358
2359 /* Used to distinguish string and bitstring from char-array and set. */
2360 int is_string = 0;
2361
2362 /* Read type number if present. The type number may be omitted.
2363 for instance in a two-dimensional array declared with type
2364 "ar1;1;10;ar1;1;10;4". */
2365 if ((**pp >= '0' && **pp <= '9')
2366 || **pp == '('
2367 || **pp == '-')
2368 {
2369 if (read_type_number (pp, typenums) != 0)
2370 return error_type (pp, objfile);
c5aa993b 2371
c906108c 2372 /* Type is not being defined here. Either it already exists,
c5aa993b
JM
2373 or this is a forward reference to it. dbx_alloc_type handles
2374 both cases. */
c906108c
SS
2375 if (**pp != '=')
2376 return dbx_alloc_type (typenums, objfile);
2377
2378 /* Type is being defined here. */
2379 /* Skip the '='.
c5aa993b
JM
2380 Also skip the type descriptor - we get it below with (*pp)[-1]. */
2381 (*pp) += 2;
c906108c
SS
2382 }
2383 else
2384 {
2385 /* 'typenums=' not present, type is anonymous. Read and return
c5aa993b 2386 the definition, but don't put it in the type vector. */
c906108c
SS
2387 typenums[0] = typenums[1] = -1;
2388 (*pp)++;
2389 }
2390
c5aa993b 2391again:
c906108c
SS
2392 type_descriptor = (*pp)[-1];
2393 switch (type_descriptor)
2394 {
2395 case 'x':
2396 {
2397 enum type_code code;
2398
2399 /* Used to index through file_symbols. */
2400 struct pending *ppt;
2401 int i;
c5aa993b 2402
c906108c
SS
2403 /* Name including "struct", etc. */
2404 char *type_name;
c5aa993b 2405
c906108c
SS
2406 {
2407 char *from, *to, *p, *q1, *q2;
c5aa993b 2408
c906108c
SS
2409 /* Set the type code according to the following letter. */
2410 switch ((*pp)[0])
2411 {
2412 case 's':
2413 code = TYPE_CODE_STRUCT;
2414 break;
2415 case 'u':
2416 code = TYPE_CODE_UNION;
2417 break;
2418 case 'e':
2419 code = TYPE_CODE_ENUM;
2420 break;
2421 default:
2422 {
2423 /* Complain and keep going, so compilers can invent new
2424 cross-reference types. */
2425 static struct complaint msg =
c5aa993b 2426 {"Unrecognized cross-reference type `%c'", 0, 0};
c906108c
SS
2427 complain (&msg, (*pp)[0]);
2428 code = TYPE_CODE_STRUCT;
2429 break;
2430 }
2431 }
c5aa993b 2432
c906108c
SS
2433 q1 = strchr (*pp, '<');
2434 p = strchr (*pp, ':');
2435 if (p == NULL)
2436 return error_type (pp, objfile);
2437 if (q1 && p > q1 && p[1] == ':')
2438 {
2439 int nesting_level = 0;
2440 for (q2 = q1; *q2; q2++)
2441 {
2442 if (*q2 == '<')
2443 nesting_level++;
2444 else if (*q2 == '>')
2445 nesting_level--;
2446 else if (*q2 == ':' && nesting_level == 0)
2447 break;
2448 }
2449 p = q2;
2450 if (*p != ':')
2451 return error_type (pp, objfile);
2452 }
c5aa993b
JM
2453 to = type_name =
2454 (char *) obstack_alloc (&objfile->type_obstack, p - *pp + 1);
2455
c906108c
SS
2456 /* Copy the name. */
2457 from = *pp + 1;
c5aa993b 2458 while (from < p)
c906108c
SS
2459 *to++ = *from++;
2460 *to = '\0';
c5aa993b 2461
c906108c
SS
2462 /* Set the pointer ahead of the name which we just read, and
2463 the colon. */
2464 *pp = from + 1;
2465 }
2466
2467 /* Now check to see whether the type has already been
2468 declared. This was written for arrays of cross-referenced
2469 types before we had TYPE_CODE_TARGET_STUBBED, so I'm pretty
2470 sure it is not necessary anymore. But it might be a good
2471 idea, to save a little memory. */
2472
2473 for (ppt = file_symbols; ppt; ppt = ppt->next)
2474 for (i = 0; i < ppt->nsyms; i++)
2475 {
2476 struct symbol *sym = ppt->symbol[i];
2477
2478 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF
2479 && SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE
2480 && (TYPE_CODE (SYMBOL_TYPE (sym)) == code)
2481 && STREQ (SYMBOL_NAME (sym), type_name))
2482 {
c5aa993b 2483 obstack_free (&objfile->type_obstack, type_name);
c906108c
SS
2484 type = SYMBOL_TYPE (sym);
2485 return type;
2486 }
2487 }
2488
2489 /* Didn't find the type to which this refers, so we must
2490 be dealing with a forward reference. Allocate a type
2491 structure for it, and keep track of it so we can
2492 fill in the rest of the fields when we get the full
2493 type. */
2494 type = dbx_alloc_type (typenums, objfile);
2495 TYPE_CODE (type) = code;
2496 TYPE_TAG_NAME (type) = type_name;
c5aa993b 2497 INIT_CPLUS_SPECIFIC (type);
c906108c
SS
2498 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
2499
2500 add_undefined_type (type);
2501 return type;
2502 }
2503
c5aa993b 2504 case '-': /* RS/6000 built-in type */
c906108c
SS
2505 case '0':
2506 case '1':
2507 case '2':
2508 case '3':
2509 case '4':
2510 case '5':
2511 case '6':
2512 case '7':
2513 case '8':
2514 case '9':
2515 case '(':
2516 (*pp)--;
2517
2518 /* We deal with something like t(1,2)=(3,4)=... which
c5aa993b 2519 the Lucid compiler and recent gcc versions (post 2.7.3) use. */
c906108c
SS
2520
2521 /* Allocate and enter the typedef type first.
c5aa993b 2522 This handles recursive types. */
c906108c
SS
2523 type = dbx_alloc_type (typenums, objfile);
2524 TYPE_CODE (type) = TYPE_CODE_TYPEDEF;
c5aa993b
JM
2525 {
2526 struct type *xtype = read_type (pp, objfile);
c906108c
SS
2527 if (type == xtype)
2528 {
2529 /* It's being defined as itself. That means it is "void". */
2530 TYPE_CODE (type) = TYPE_CODE_VOID;
2531 TYPE_LENGTH (type) = 1;
2532 }
2533 else if (type_size >= 0 || is_string)
2534 {
dd6bda65
DJ
2535 /* This is the absolute wrong way to construct types. Every
2536 other debug format has found a way around this problem and
2537 the related problems with unnecessarily stubbed types;
2538 someone motivated should attempt to clean up the issue
2539 here as well. Once a type pointed to has been created it
13a393b0
JB
2540 should not be modified.
2541
2542 Well, it's not *absolutely* wrong. Constructing recursive
2543 types (trees, linked lists) necessarily entails modifying
2544 types after creating them. Constructing any loop structure
2545 entails side effects. The Dwarf 2 reader does handle this
2546 more gracefully (it never constructs more than once
2547 instance of a type object, so it doesn't have to copy type
2548 objects wholesale), but it still mutates type objects after
2549 other folks have references to them.
2550
2551 Keep in mind that this circularity/mutation issue shows up
2552 at the source language level, too: C's "incomplete types",
2553 for example. So the proper cleanup, I think, would be to
2554 limit GDB's type smashing to match exactly those required
2555 by the source language. So GDB could have a
2556 "complete_this_type" function, but never create unnecessary
2557 copies of a type otherwise. */
dd6bda65 2558 replace_type (type, xtype);
c906108c
SS
2559 TYPE_NAME (type) = NULL;
2560 TYPE_TAG_NAME (type) = NULL;
2561 }
2562 else
2563 {
2564 TYPE_FLAGS (type) |= TYPE_FLAG_TARGET_STUB;
2565 TYPE_TARGET_TYPE (type) = xtype;
2566 }
2567 }
2568 break;
2569
c5aa993b
JM
2570 /* In the following types, we must be sure to overwrite any existing
2571 type that the typenums refer to, rather than allocating a new one
2572 and making the typenums point to the new one. This is because there
2573 may already be pointers to the existing type (if it had been
2574 forward-referenced), and we must change it to a pointer, function,
2575 reference, or whatever, *in-place*. */
c906108c
SS
2576
2577 case '*':
2578 type1 = read_type (pp, objfile);
2579 type = make_pointer_type (type1, dbx_lookup_type (typenums));
2580 break;
2581
c5aa993b 2582 case '&': /* Reference to another type */
c906108c
SS
2583 type1 = read_type (pp, objfile);
2584 type = make_reference_type (type1, dbx_lookup_type (typenums));
2585 break;
2586
c5aa993b 2587 case 'f': /* Function returning another type */
c906108c
SS
2588 if (os9k_stabs && **pp == '(')
2589 {
2590 /* Function prototype; parse it.
2591 We must conditionalize this on os9k_stabs because otherwise
2592 it could be confused with a Sun-style (1,3) typenumber
2593 (I think). */
2594 struct type *t;
2595 ++*pp;
2596 while (**pp != ')')
c5aa993b
JM
2597 {
2598 t = read_type (pp, objfile);
2599 if (**pp == ',')
2600 ++ * pp;
2601 }
c906108c
SS
2602 }
2603 type1 = read_type (pp, objfile);
2604 type = make_function_type (type1, dbx_lookup_type (typenums));
2605 break;
2606
da966255
JB
2607 case 'g': /* Prototyped function. (Sun) */
2608 {
2609 /* Unresolved questions:
2610
2611 - According to Sun's ``STABS Interface Manual'', for 'f'
2612 and 'F' symbol descriptors, a `0' in the argument type list
2613 indicates a varargs function. But it doesn't say how 'g'
2614 type descriptors represent that info. Someone with access
2615 to Sun's toolchain should try it out.
2616
2617 - According to the comment in define_symbol (search for
2618 `process_prototype_types:'), Sun emits integer arguments as
2619 types which ref themselves --- like `void' types. Do we
2620 have to deal with that here, too? Again, someone with
2621 access to Sun's toolchain should try it out and let us
2622 know. */
2623
2624 const char *type_start = (*pp) - 1;
2625 struct type *return_type = read_type (pp, objfile);
2626 struct type *func_type
2627 = make_function_type (return_type, dbx_lookup_type (typenums));
2628 struct type_list {
2629 struct type *type;
2630 struct type_list *next;
2631 } *arg_types = 0;
2632 int num_args = 0;
2633
2634 while (**pp && **pp != '#')
2635 {
2636 struct type *arg_type = read_type (pp, objfile);
2637 struct type_list *new = alloca (sizeof (*new));
2638 new->type = arg_type;
2639 new->next = arg_types;
2640 arg_types = new;
2641 num_args++;
2642 }
2643 if (**pp == '#')
2644 ++*pp;
2645 else
2646 {
2647 static struct complaint msg = {
2648 "Prototyped function type didn't end arguments with `#':\n%s",
2649 0, 0
2650 };
2651 complain (&msg, type_start);
2652 }
2653
2654 /* If there is just one argument whose type is `void', then
2655 that's just an empty argument list. */
2656 if (arg_types
2657 && ! arg_types->next
2658 && TYPE_CODE (arg_types->type) == TYPE_CODE_VOID)
2659 num_args = 0;
2660
2661 TYPE_FIELDS (func_type)
2662 = (struct field *) TYPE_ALLOC (func_type,
2663 num_args * sizeof (struct field));
2664 memset (TYPE_FIELDS (func_type), 0, num_args * sizeof (struct field));
2665 {
2666 int i;
2667 struct type_list *t;
2668
2669 /* We stuck each argument type onto the front of the list
2670 when we read it, so the list is reversed. Build the
2671 fields array right-to-left. */
2672 for (t = arg_types, i = num_args - 1; t; t = t->next, i--)
2673 TYPE_FIELD_TYPE (func_type, i) = t->type;
2674 }
2675 TYPE_NFIELDS (func_type) = num_args;
2676 TYPE_FLAGS (func_type) |= TYPE_FLAG_PROTOTYPED;
2677
2678 type = func_type;
2679 break;
2680 }
2681
c5aa993b
JM
2682 case 'k': /* Const qualifier on some type (Sun) */
2683 case 'c': /* Const qualifier on some type (OS9000) */
c906108c 2684 /* Because 'c' means other things to AIX and 'k' is perfectly good,
c5aa993b 2685 only accept 'c' in the os9k_stabs case. */
c906108c
SS
2686 if (type_descriptor == 'c' && !os9k_stabs)
2687 return error_type (pp, objfile);
2688 type = read_type (pp, objfile);
d7242108
DJ
2689 type = make_cv_type (1, TYPE_VOLATILE (type), type,
2690 dbx_lookup_type (typenums));
c906108c
SS
2691 break;
2692
c5aa993b
JM
2693 case 'B': /* Volatile qual on some type (Sun) */
2694 case 'i': /* Volatile qual on some type (OS9000) */
c906108c 2695 /* Because 'i' means other things to AIX and 'B' is perfectly good,
c5aa993b 2696 only accept 'i' in the os9k_stabs case. */
c906108c
SS
2697 if (type_descriptor == 'i' && !os9k_stabs)
2698 return error_type (pp, objfile);
2699 type = read_type (pp, objfile);
d7242108
DJ
2700 type = make_cv_type (TYPE_CONST (type), 1, type,
2701 dbx_lookup_type (typenums));
c906108c
SS
2702 break;
2703
2704 case '@':
c5aa993b
JM
2705 if (isdigit (**pp) || **pp == '(' || **pp == '-')
2706 { /* Member (class & variable) type */
c906108c
SS
2707 /* FIXME -- we should be doing smash_to_XXX types here. */
2708
2709 struct type *domain = read_type (pp, objfile);
2710 struct type *memtype;
2711
2712 if (**pp != ',')
2713 /* Invalid member type data format. */
2714 return error_type (pp, objfile);
2715 ++*pp;
2716
2717 memtype = read_type (pp, objfile);
2718 type = dbx_alloc_type (typenums, objfile);
2719 smash_to_member_type (type, domain, memtype);
2720 }
c5aa993b
JM
2721 else
2722 /* type attribute */
c906108c
SS
2723 {
2724 char *attr = *pp;
2725 /* Skip to the semicolon. */
2726 while (**pp != ';' && **pp != '\0')
2727 ++(*pp);
2728 if (**pp == '\0')
2729 return error_type (pp, objfile);
2730 else
c5aa993b 2731 ++ * pp; /* Skip the semicolon. */
c906108c
SS
2732
2733 switch (*attr)
2734 {
2735 case 's':
2736 type_size = atoi (attr + 1);
2737 if (type_size <= 0)
2738 type_size = -1;
2739 break;
2740
2741 case 'S':
2742 is_string = 1;
2743 break;
2744
2745 default:
2746 /* Ignore unrecognized type attributes, so future compilers
c5aa993b 2747 can invent new ones. */
c906108c
SS
2748 break;
2749 }
2750 ++*pp;
2751 goto again;
2752 }
2753 break;
2754
c5aa993b 2755 case '#': /* Method (class & fn) type */
c906108c
SS
2756 if ((*pp)[0] == '#')
2757 {
2758 /* We'll get the parameter types from the name. */
2759 struct type *return_type;
2760
2761 (*pp)++;
2762 return_type = read_type (pp, objfile);
2763 if (*(*pp)++ != ';')
2764 complain (&invalid_member_complaint, symnum);
2765 type = allocate_stub_method (return_type);
2766 if (typenums[0] != -1)
2767 *dbx_lookup_type (typenums) = type;
2768 }
2769 else
2770 {
2771 struct type *domain = read_type (pp, objfile);
2772 struct type *return_type;
2773 struct type **args;
2774
2775 if (**pp != ',')
2776 /* Invalid member type data format. */
2777 return error_type (pp, objfile);
2778 else
2779 ++(*pp);
2780
2781 return_type = read_type (pp, objfile);
2782 args = read_args (pp, ';', objfile);
2783 type = dbx_alloc_type (typenums, objfile);
2784 smash_to_method_type (type, domain, return_type, args);
2785 }
2786 break;
2787
c5aa993b 2788 case 'r': /* Range type */
c906108c
SS
2789 type = read_range_type (pp, typenums, objfile);
2790 if (typenums[0] != -1)
2791 *dbx_lookup_type (typenums) = type;
2792 break;
2793
2794 case 'b':
2795 if (os9k_stabs)
2796 /* Const and volatile qualified type. */
2797 type = read_type (pp, objfile);
2798 else
2799 {
2800 /* Sun ACC builtin int type */
2801 type = read_sun_builtin_type (pp, typenums, objfile);
2802 if (typenums[0] != -1)
2803 *dbx_lookup_type (typenums) = type;
2804 }
2805 break;
2806
c5aa993b 2807 case 'R': /* Sun ACC builtin float type */
c906108c
SS
2808 type = read_sun_floating_type (pp, typenums, objfile);
2809 if (typenums[0] != -1)
2810 *dbx_lookup_type (typenums) = type;
2811 break;
c5aa993b
JM
2812
2813 case 'e': /* Enumeration type */
c906108c
SS
2814 type = dbx_alloc_type (typenums, objfile);
2815 type = read_enum_type (pp, type, objfile);
2816 if (typenums[0] != -1)
2817 *dbx_lookup_type (typenums) = type;
2818 break;
2819
c5aa993b
JM
2820 case 's': /* Struct type */
2821 case 'u': /* Union type */
2ae1c2d2
JB
2822 {
2823 enum type_code type_code = TYPE_CODE_UNDEF;
2824 type = dbx_alloc_type (typenums, objfile);
2825 switch (type_descriptor)
2826 {
2827 case 's':
2828 type_code = TYPE_CODE_STRUCT;
2829 break;
2830 case 'u':
2831 type_code = TYPE_CODE_UNION;
2832 break;
2833 }
2834 type = read_struct_type (pp, type, type_code, objfile);
2835 break;
2836 }
c906108c 2837
c5aa993b 2838 case 'a': /* Array type */
c906108c
SS
2839 if (**pp != 'r')
2840 return error_type (pp, objfile);
2841 ++*pp;
c5aa993b 2842
c906108c
SS
2843 type = dbx_alloc_type (typenums, objfile);
2844 type = read_array_type (pp, type, objfile);
2845 if (is_string)
2846 TYPE_CODE (type) = TYPE_CODE_STRING;
2847 break;
2848
2849 case 'S':
2850 type1 = read_type (pp, objfile);
c5aa993b 2851 type = create_set_type ((struct type *) NULL, type1);
c906108c
SS
2852 if (is_string)
2853 TYPE_CODE (type) = TYPE_CODE_BITSTRING;
2854 if (typenums[0] != -1)
2855 *dbx_lookup_type (typenums) = type;
2856 break;
2857
2858 default:
2859 --*pp; /* Go back to the symbol in error */
c5aa993b 2860 /* Particularly important if it was \0! */
c906108c
SS
2861 return error_type (pp, objfile);
2862 }
2863
2864 if (type == 0)
2865 {
2866 warning ("GDB internal error, type is NULL in stabsread.c\n");
2867 return error_type (pp, objfile);
2868 }
2869
2870 /* Size specified in a type attribute overrides any other size. */
2871 if (type_size != -1)
2872 TYPE_LENGTH (type) = (type_size + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
2873
2874 return type;
2875}
2876\f
2877/* RS/6000 xlc/dbx combination uses a set of builtin types, starting from -1.
2878 Return the proper type node for a given builtin type number. */
2879
2880static struct type *
fba45db2 2881rs6000_builtin_type (int typenum)
c906108c
SS
2882{
2883 /* We recognize types numbered from -NUMBER_RECOGNIZED to -1. */
2884#define NUMBER_RECOGNIZED 34
2885 /* This includes an empty slot for type number -0. */
2886 static struct type *negative_types[NUMBER_RECOGNIZED + 1];
2887 struct type *rettype = NULL;
2888
2889 if (typenum >= 0 || typenum < -NUMBER_RECOGNIZED)
2890 {
2891 complain (&rs6000_builtin_complaint, typenum);
2892 return builtin_type_error;
2893 }
2894 if (negative_types[-typenum] != NULL)
2895 return negative_types[-typenum];
2896
2897#if TARGET_CHAR_BIT != 8
c5aa993b 2898#error This code wrong for TARGET_CHAR_BIT not 8
c906108c
SS
2899 /* These definitions all assume that TARGET_CHAR_BIT is 8. I think
2900 that if that ever becomes not true, the correct fix will be to
2901 make the size in the struct type to be in bits, not in units of
2902 TARGET_CHAR_BIT. */
2903#endif
2904
2905 switch (-typenum)
2906 {
2907 case 1:
2908 /* The size of this and all the other types are fixed, defined
c5aa993b
JM
2909 by the debugging format. If there is a type called "int" which
2910 is other than 32 bits, then it should use a new negative type
2911 number (or avoid negative type numbers for that case).
2912 See stabs.texinfo. */
c906108c
SS
2913 rettype = init_type (TYPE_CODE_INT, 4, 0, "int", NULL);
2914 break;
2915 case 2:
2916 rettype = init_type (TYPE_CODE_INT, 1, 0, "char", NULL);
2917 break;
2918 case 3:
2919 rettype = init_type (TYPE_CODE_INT, 2, 0, "short", NULL);
2920 break;
2921 case 4:
2922 rettype = init_type (TYPE_CODE_INT, 4, 0, "long", NULL);
2923 break;
2924 case 5:
2925 rettype = init_type (TYPE_CODE_INT, 1, TYPE_FLAG_UNSIGNED,
2926 "unsigned char", NULL);
2927 break;
2928 case 6:
2929 rettype = init_type (TYPE_CODE_INT, 1, 0, "signed char", NULL);
2930 break;
2931 case 7:
2932 rettype = init_type (TYPE_CODE_INT, 2, TYPE_FLAG_UNSIGNED,
2933 "unsigned short", NULL);
2934 break;
2935 case 8:
2936 rettype = init_type (TYPE_CODE_INT, 4, TYPE_FLAG_UNSIGNED,
2937 "unsigned int", NULL);
2938 break;
2939 case 9:
2940 rettype = init_type (TYPE_CODE_INT, 4, TYPE_FLAG_UNSIGNED,
2941 "unsigned", NULL);
2942 case 10:
2943 rettype = init_type (TYPE_CODE_INT, 4, TYPE_FLAG_UNSIGNED,
2944 "unsigned long", NULL);
2945 break;
2946 case 11:
2947 rettype = init_type (TYPE_CODE_VOID, 1, 0, "void", NULL);
2948 break;
2949 case 12:
2950 /* IEEE single precision (32 bit). */
2951 rettype = init_type (TYPE_CODE_FLT, 4, 0, "float", NULL);
2952 break;
2953 case 13:
2954 /* IEEE double precision (64 bit). */
2955 rettype = init_type (TYPE_CODE_FLT, 8, 0, "double", NULL);
2956 break;
2957 case 14:
2958 /* This is an IEEE double on the RS/6000, and different machines with
c5aa993b
JM
2959 different sizes for "long double" should use different negative
2960 type numbers. See stabs.texinfo. */
c906108c
SS
2961 rettype = init_type (TYPE_CODE_FLT, 8, 0, "long double", NULL);
2962 break;
2963 case 15:
2964 rettype = init_type (TYPE_CODE_INT, 4, 0, "integer", NULL);
2965 break;
2966 case 16:
2967 rettype = init_type (TYPE_CODE_BOOL, 4, TYPE_FLAG_UNSIGNED,
2968 "boolean", NULL);
2969 break;
2970 case 17:
2971 rettype = init_type (TYPE_CODE_FLT, 4, 0, "short real", NULL);
2972 break;
2973 case 18:
2974 rettype = init_type (TYPE_CODE_FLT, 8, 0, "real", NULL);
2975 break;
2976 case 19:
2977 rettype = init_type (TYPE_CODE_ERROR, 0, 0, "stringptr", NULL);
2978 break;
2979 case 20:
2980 rettype = init_type (TYPE_CODE_CHAR, 1, TYPE_FLAG_UNSIGNED,
2981 "character", NULL);
2982 break;
2983 case 21:
2984 rettype = init_type (TYPE_CODE_BOOL, 1, TYPE_FLAG_UNSIGNED,
2985 "logical*1", NULL);
2986 break;
2987 case 22:
2988 rettype = init_type (TYPE_CODE_BOOL, 2, TYPE_FLAG_UNSIGNED,
2989 "logical*2", NULL);
2990 break;
2991 case 23:
2992 rettype = init_type (TYPE_CODE_BOOL, 4, TYPE_FLAG_UNSIGNED,
2993 "logical*4", NULL);
2994 break;
2995 case 24:
2996 rettype = init_type (TYPE_CODE_BOOL, 4, TYPE_FLAG_UNSIGNED,
2997 "logical", NULL);
2998 break;
2999 case 25:
3000 /* Complex type consisting of two IEEE single precision values. */
3001 rettype = init_type (TYPE_CODE_COMPLEX, 8, 0, "complex", NULL);
f65ca430
DJ
3002 TYPE_TARGET_TYPE (rettype) = init_type (TYPE_CODE_FLT, 4, 0, "float",
3003 NULL);
c906108c
SS
3004 break;
3005 case 26:
3006 /* Complex type consisting of two IEEE double precision values. */
3007 rettype = init_type (TYPE_CODE_COMPLEX, 16, 0, "double complex", NULL);
f65ca430
DJ
3008 TYPE_TARGET_TYPE (rettype) = init_type (TYPE_CODE_FLT, 8, 0, "double",
3009 NULL);
c906108c
SS
3010 break;
3011 case 27:
3012 rettype = init_type (TYPE_CODE_INT, 1, 0, "integer*1", NULL);
3013 break;
3014 case 28:
3015 rettype = init_type (TYPE_CODE_INT, 2, 0, "integer*2", NULL);
3016 break;
3017 case 29:
3018 rettype = init_type (TYPE_CODE_INT, 4, 0, "integer*4", NULL);
3019 break;
3020 case 30:
3021 rettype = init_type (TYPE_CODE_CHAR, 2, 0, "wchar", NULL);
3022 break;
3023 case 31:
3024 rettype = init_type (TYPE_CODE_INT, 8, 0, "long long", NULL);
3025 break;
3026 case 32:
3027 rettype = init_type (TYPE_CODE_INT, 8, TYPE_FLAG_UNSIGNED,
3028 "unsigned long long", NULL);
3029 break;
3030 case 33:
3031 rettype = init_type (TYPE_CODE_INT, 8, TYPE_FLAG_UNSIGNED,
3032 "logical*8", NULL);
3033 break;
3034 case 34:
3035 rettype = init_type (TYPE_CODE_INT, 8, 0, "integer*8", NULL);
3036 break;
3037 }
3038 negative_types[-typenum] = rettype;
3039 return rettype;
3040}
3041\f
3042/* This page contains subroutines of read_type. */
3043
3044/* Read member function stabs info for C++ classes. The form of each member
3045 function data is:
3046
c5aa993b 3047 NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
c906108c
SS
3048
3049 An example with two member functions is:
3050
c5aa993b 3051 afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
c906108c
SS
3052
3053 For the case of overloaded operators, the format is op$::*.funcs, where
3054 $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
3055 name (such as `+=') and `.' marks the end of the operator name.
3056
3057 Returns 1 for success, 0 for failure. */
3058
3059static int
fba45db2
KB
3060read_member_functions (struct field_info *fip, char **pp, struct type *type,
3061 struct objfile *objfile)
c906108c
SS
3062{
3063 int nfn_fields = 0;
3064 int length = 0;
3065 /* Total number of member functions defined in this class. If the class
3066 defines two `f' functions, and one `g' function, then this will have
3067 the value 3. */
3068 int total_length = 0;
3069 int i;
3070 struct next_fnfield
3071 {
3072 struct next_fnfield *next;
3073 struct fn_field fn_field;
c5aa993b
JM
3074 }
3075 *sublist;
c906108c
SS
3076 struct type *look_ahead_type;
3077 struct next_fnfieldlist *new_fnlist;
3078 struct next_fnfield *new_sublist;
3079 char *main_fn_name;
3080 register char *p;
c5aa993b 3081
c906108c
SS
3082 /* Process each list until we find something that is not a member function
3083 or find the end of the functions. */
3084
3085 while (**pp != ';')
3086 {
3087 /* We should be positioned at the start of the function name.
c5aa993b
JM
3088 Scan forward to find the first ':' and if it is not the
3089 first of a "::" delimiter, then this is not a member function. */
c906108c
SS
3090 p = *pp;
3091 while (*p != ':')
3092 {
3093 p++;
3094 }
3095 if (p[1] != ':')
3096 {
3097 break;
3098 }
3099
3100 sublist = NULL;
3101 look_ahead_type = NULL;
3102 length = 0;
c5aa993b 3103
c906108c
SS
3104 new_fnlist = (struct next_fnfieldlist *)
3105 xmalloc (sizeof (struct next_fnfieldlist));
b8c9b27d 3106 make_cleanup (xfree, new_fnlist);
c906108c 3107 memset (new_fnlist, 0, sizeof (struct next_fnfieldlist));
c5aa993b 3108
c906108c
SS
3109 if ((*pp)[0] == 'o' && (*pp)[1] == 'p' && is_cplus_marker ((*pp)[2]))
3110 {
3111 /* This is a completely wierd case. In order to stuff in the
3112 names that might contain colons (the usual name delimiter),
3113 Mike Tiemann defined a different name format which is
3114 signalled if the identifier is "op$". In that case, the
3115 format is "op$::XXXX." where XXXX is the name. This is
3116 used for names like "+" or "=". YUUUUUUUK! FIXME! */
3117 /* This lets the user type "break operator+".
3118 We could just put in "+" as the name, but that wouldn't
3119 work for "*". */
c5aa993b
JM
3120 static char opname[32] =
3121 {'o', 'p', CPLUS_MARKER};
c906108c 3122 char *o = opname + 3;
c5aa993b 3123
c906108c
SS
3124 /* Skip past '::'. */
3125 *pp = p + 2;
3126
3127 STABS_CONTINUE (pp, objfile);
3128 p = *pp;
3129 while (*p != '.')
3130 {
3131 *o++ = *p++;
3132 }
3133 main_fn_name = savestring (opname, o - opname);
3134 /* Skip past '.' */
3135 *pp = p + 1;
3136 }
3137 else
3138 {
3139 main_fn_name = savestring (*pp, p - *pp);
3140 /* Skip past '::'. */
3141 *pp = p + 2;
3142 }
c5aa993b
JM
3143 new_fnlist->fn_fieldlist.name = main_fn_name;
3144
c906108c
SS
3145 do
3146 {
3147 new_sublist =
3148 (struct next_fnfield *) xmalloc (sizeof (struct next_fnfield));
b8c9b27d 3149 make_cleanup (xfree, new_sublist);
c906108c 3150 memset (new_sublist, 0, sizeof (struct next_fnfield));
c5aa993b 3151
c906108c
SS
3152 /* Check for and handle cretinous dbx symbol name continuation! */
3153 if (look_ahead_type == NULL)
3154 {
3155 /* Normal case. */
3156 STABS_CONTINUE (pp, objfile);
c5aa993b
JM
3157
3158 new_sublist->fn_field.type = read_type (pp, objfile);
c906108c
SS
3159 if (**pp != ':')
3160 {
3161 /* Invalid symtab info for member function. */
3162 return 0;
3163 }
3164 }
3165 else
3166 {
3167 /* g++ version 1 kludge */
c5aa993b 3168 new_sublist->fn_field.type = look_ahead_type;
c906108c
SS
3169 look_ahead_type = NULL;
3170 }
c5aa993b 3171
c906108c
SS
3172 (*pp)++;
3173 p = *pp;
3174 while (*p != ';')
3175 {
3176 p++;
3177 }
c5aa993b 3178
c906108c
SS
3179 /* If this is just a stub, then we don't have the real name here. */
3180
74a9bb82 3181 if (TYPE_STUB (new_sublist->fn_field.type))
c906108c 3182 {
c5aa993b
JM
3183 if (!TYPE_DOMAIN_TYPE (new_sublist->fn_field.type))
3184 TYPE_DOMAIN_TYPE (new_sublist->fn_field.type) = type;
3185 new_sublist->fn_field.is_stub = 1;
c906108c 3186 }
c5aa993b 3187 new_sublist->fn_field.physname = savestring (*pp, p - *pp);
c906108c 3188 *pp = p + 1;
c5aa993b 3189
c906108c
SS
3190 /* Set this member function's visibility fields. */
3191 switch (*(*pp)++)
3192 {
c5aa993b
JM
3193 case VISIBILITY_PRIVATE:
3194 new_sublist->fn_field.is_private = 1;
3195 break;
3196 case VISIBILITY_PROTECTED:
3197 new_sublist->fn_field.is_protected = 1;
3198 break;
c906108c 3199 }
c5aa993b 3200
c906108c
SS
3201 STABS_CONTINUE (pp, objfile);
3202 switch (**pp)
3203 {
c5aa993b
JM
3204 case 'A': /* Normal functions. */
3205 new_sublist->fn_field.is_const = 0;
3206 new_sublist->fn_field.is_volatile = 0;
3207 (*pp)++;
3208 break;
3209 case 'B': /* `const' member functions. */
3210 new_sublist->fn_field.is_const = 1;
3211 new_sublist->fn_field.is_volatile = 0;
3212 (*pp)++;
3213 break;
3214 case 'C': /* `volatile' member function. */
3215 new_sublist->fn_field.is_const = 0;
3216 new_sublist->fn_field.is_volatile = 1;
3217 (*pp)++;
3218 break;
3219 case 'D': /* `const volatile' member function. */
3220 new_sublist->fn_field.is_const = 1;
3221 new_sublist->fn_field.is_volatile = 1;
3222 (*pp)++;
3223 break;
3224 case '*': /* File compiled with g++ version 1 -- no info */
3225 case '?':
3226 case '.':
3227 break;
3228 default:
3229 complain (&const_vol_complaint, **pp);
3230 break;
c906108c 3231 }
c5aa993b 3232
c906108c
SS
3233 switch (*(*pp)++)
3234 {
c5aa993b 3235 case '*':
c906108c
SS
3236 {
3237 int nbits;
c5aa993b 3238 /* virtual member function, followed by index.
c906108c
SS
3239 The sign bit is set to distinguish pointers-to-methods
3240 from virtual function indicies. Since the array is
3241 in words, the quantity must be shifted left by 1
3242 on 16 bit machine, and by 2 on 32 bit machine, forcing
3243 the sign bit out, and usable as a valid index into
3244 the array. Remove the sign bit here. */
c5aa993b 3245 new_sublist->fn_field.voffset =
c906108c
SS
3246 (0x7fffffff & read_huge_number (pp, ';', &nbits)) + 2;
3247 if (nbits != 0)
3248 return 0;
c5aa993b 3249
c906108c
SS
3250 STABS_CONTINUE (pp, objfile);
3251 if (**pp == ';' || **pp == '\0')
3252 {
3253 /* Must be g++ version 1. */
c5aa993b 3254 new_sublist->fn_field.fcontext = 0;
c906108c
SS
3255 }
3256 else
3257 {
3258 /* Figure out from whence this virtual function came.
3259 It may belong to virtual function table of
3260 one of its baseclasses. */
3261 look_ahead_type = read_type (pp, objfile);
3262 if (**pp == ':')
3263 {
3264 /* g++ version 1 overloaded methods. */
3265 }
3266 else
3267 {
c5aa993b 3268 new_sublist->fn_field.fcontext = look_ahead_type;
c906108c
SS
3269 if (**pp != ';')
3270 {
3271 return 0;
3272 }
3273 else
3274 {
3275 ++*pp;
3276 }
3277 look_ahead_type = NULL;
3278 }
3279 }
3280 break;
3281 }
c5aa993b
JM
3282 case '?':
3283 /* static member function. */
4ea09c10
PS
3284 {
3285 int slen = strlen (main_fn_name);
3286
3287 new_sublist->fn_field.voffset = VOFFSET_STATIC;
3288
3289 /* For static member functions, we can't tell if they
3290 are stubbed, as they are put out as functions, and not as
3291 methods.
3292 GCC v2 emits the fully mangled name if
3293 dbxout.c:flag_minimal_debug is not set, so we have to
3294 detect a fully mangled physname here and set is_stub
3295 accordingly. Fully mangled physnames in v2 start with
3296 the member function name, followed by two underscores.
3297 GCC v3 currently always emits stubbed member functions,
3298 but with fully mangled physnames, which start with _Z. */
3299 if (!(strncmp (new_sublist->fn_field.physname,
3300 main_fn_name, slen) == 0
3301 && new_sublist->fn_field.physname[slen] == '_'
3302 && new_sublist->fn_field.physname[slen + 1] == '_'))
3303 {
3304 new_sublist->fn_field.is_stub = 1;
3305 }
3306 break;
3307 }
c5aa993b
JM
3308
3309 default:
3310 /* error */
3311 complain (&member_fn_complaint, (*pp)[-1]);
3312 /* Fall through into normal member function. */
3313
3314 case '.':
3315 /* normal member function. */
3316 new_sublist->fn_field.voffset = 0;
3317 new_sublist->fn_field.fcontext = 0;
3318 break;
c906108c 3319 }
c5aa993b
JM
3320
3321 new_sublist->next = sublist;
c906108c
SS
3322 sublist = new_sublist;
3323 length++;
3324 STABS_CONTINUE (pp, objfile);
3325 }
3326 while (**pp != ';' && **pp != '\0');
c5aa993b 3327
c906108c 3328 (*pp)++;
0c867556 3329 STABS_CONTINUE (pp, objfile);
c5aa993b 3330
0c867556
PS
3331 /* Skip GCC 3.X member functions which are duplicates of the callable
3332 constructor/destructor. */
3333 if (strcmp (main_fn_name, "__base_ctor") == 0
3334 || strcmp (main_fn_name, "__base_dtor") == 0
3335 || strcmp (main_fn_name, "__deleting_dtor") == 0)
c906108c 3336 {
0c867556 3337 xfree (main_fn_name);
c906108c 3338 }
0c867556
PS
3339 else
3340 {
3341 new_fnlist->fn_fieldlist.fn_fields = (struct fn_field *)
3342 obstack_alloc (&objfile->type_obstack,
3343 sizeof (struct fn_field) * length);
3344 memset (new_fnlist->fn_fieldlist.fn_fields, 0,
3345 sizeof (struct fn_field) * length);
3346 for (i = length; (i--, sublist); sublist = sublist->next)
3347 {
3348 new_fnlist->fn_fieldlist.fn_fields[i] = sublist->fn_field;
3349 }
c5aa993b 3350
0c867556
PS
3351 new_fnlist->fn_fieldlist.length = length;
3352 new_fnlist->next = fip->fnlist;
3353 fip->fnlist = new_fnlist;
3354 nfn_fields++;
3355 total_length += length;
3356 }
c906108c
SS
3357 }
3358
3359 if (nfn_fields)
3360 {
3361 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3362 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
3363 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * nfn_fields);
3364 memset (TYPE_FN_FIELDLISTS (type), 0,
3365 sizeof (struct fn_fieldlist) * nfn_fields);
3366 TYPE_NFN_FIELDS (type) = nfn_fields;
3367 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
3368 }
3369
3370 return 1;
3371}
3372
3373/* Special GNU C++ name.
3374
3375 Returns 1 for success, 0 for failure. "failure" means that we can't
3376 keep parsing and it's time for error_type(). */
3377
3378static int
fba45db2
KB
3379read_cpp_abbrev (struct field_info *fip, char **pp, struct type *type,
3380 struct objfile *objfile)
c906108c
SS
3381{
3382 register char *p;
3383 char *name;
3384 char cpp_abbrev;
3385 struct type *context;
3386
3387 p = *pp;
3388 if (*++p == 'v')
3389 {
3390 name = NULL;
3391 cpp_abbrev = *++p;
3392
3393 *pp = p + 1;
3394
3395 /* At this point, *pp points to something like "22:23=*22...",
c5aa993b
JM
3396 where the type number before the ':' is the "context" and
3397 everything after is a regular type definition. Lookup the
3398 type, find it's name, and construct the field name. */
c906108c
SS
3399
3400 context = read_type (pp, objfile);
3401
3402 switch (cpp_abbrev)
3403 {
c5aa993b 3404 case 'f': /* $vf -- a virtual function table pointer */
c2bd2ed9
JB
3405 name = type_name_no_tag (context);
3406 if (name == NULL)
3407 {
3408 name = "";
3409 }
c5aa993b 3410 fip->list->field.name =
c2bd2ed9 3411 obconcat (&objfile->type_obstack, vptr_name, name, "");
c5aa993b 3412 break;
c906108c 3413
c5aa993b
JM
3414 case 'b': /* $vb -- a virtual bsomethingorother */
3415 name = type_name_no_tag (context);
3416 if (name == NULL)
3417 {
3418 complain (&invalid_cpp_type_complaint, symnum);
3419 name = "FOO";
3420 }
3421 fip->list->field.name =
3422 obconcat (&objfile->type_obstack, vb_name, name, "");
3423 break;
c906108c 3424
c5aa993b
JM
3425 default:
3426 complain (&invalid_cpp_abbrev_complaint, *pp);
3427 fip->list->field.name =
3428 obconcat (&objfile->type_obstack,
3429 "INVALID_CPLUSPLUS_ABBREV", "", "");
3430 break;
c906108c
SS
3431 }
3432
3433 /* At this point, *pp points to the ':'. Skip it and read the
c5aa993b 3434 field type. */
c906108c
SS
3435
3436 p = ++(*pp);
3437 if (p[-1] != ':')
3438 {
3439 complain (&invalid_cpp_abbrev_complaint, *pp);
3440 return 0;
3441 }
3442 fip->list->field.type = read_type (pp, objfile);
3443 if (**pp == ',')
c5aa993b 3444 (*pp)++; /* Skip the comma. */
c906108c
SS
3445 else
3446 return 0;
3447
3448 {
3449 int nbits;
3450 FIELD_BITPOS (fip->list->field) = read_huge_number (pp, ';', &nbits);
3451 if (nbits != 0)
3452 return 0;
3453 }
3454 /* This field is unpacked. */
3455 FIELD_BITSIZE (fip->list->field) = 0;
3456 fip->list->visibility = VISIBILITY_PRIVATE;
3457 }
3458 else
3459 {
3460 complain (&invalid_cpp_abbrev_complaint, *pp);
3461 /* We have no idea what syntax an unrecognized abbrev would have, so
c5aa993b
JM
3462 better return 0. If we returned 1, we would need to at least advance
3463 *pp to avoid an infinite loop. */
c906108c
SS
3464 return 0;
3465 }
3466 return 1;
3467}
3468
3469static void
fba45db2
KB
3470read_one_struct_field (struct field_info *fip, char **pp, char *p,
3471 struct type *type, struct objfile *objfile)
c906108c
SS
3472{
3473 /* The following is code to work around cfront generated stabs.
3474 The stabs contains full mangled name for each field.
3475 We try to demangle the name and extract the field name out of it.
c5aa993b 3476 */
c906108c
SS
3477 if (ARM_DEMANGLING && current_subfile->language == language_cplus)
3478 {
3479 char save_p;
3480 char *dem, *dem_p;
3481 save_p = *p;
3482 *p = '\0';
3483 dem = cplus_demangle (*pp, DMGL_ANSI | DMGL_PARAMS);
3484 if (dem != NULL)
c5aa993b
JM
3485 {
3486 dem_p = strrchr (dem, ':');
3487 if (dem_p != 0 && *(dem_p - 1) == ':')
3488 dem_p++;
3489 FIELD_NAME (fip->list->field) =
3490 obsavestring (dem_p, strlen (dem_p), &objfile->type_obstack);
3491 }
c906108c 3492 else
c5aa993b
JM
3493 {
3494 FIELD_NAME (fip->list->field) =
3495 obsavestring (*pp, p - *pp, &objfile->type_obstack);
3496 }
c906108c
SS
3497 *p = save_p;
3498 }
3499 /* end of code for cfront work around */
3500
3501 else
c5aa993b
JM
3502 fip->list->field.name =
3503 obsavestring (*pp, p - *pp, &objfile->type_obstack);
c906108c
SS
3504 *pp = p + 1;
3505
3506 /* This means we have a visibility for a field coming. */
3507 if (**pp == '/')
3508 {
3509 (*pp)++;
c5aa993b 3510 fip->list->visibility = *(*pp)++;
c906108c
SS
3511 }
3512 else
3513 {
3514 /* normal dbx-style format, no explicit visibility */
c5aa993b 3515 fip->list->visibility = VISIBILITY_PUBLIC;
c906108c
SS
3516 }
3517
c5aa993b 3518 fip->list->field.type = read_type (pp, objfile);
c906108c
SS
3519 if (**pp == ':')
3520 {
3521 p = ++(*pp);
3522#if 0
3523 /* Possible future hook for nested types. */
3524 if (**pp == '!')
3525 {
c5aa993b 3526 fip->list->field.bitpos = (long) -2; /* nested type */
c906108c
SS
3527 p = ++(*pp);
3528 }
c5aa993b
JM
3529 else
3530 ...;
c906108c 3531#endif
c5aa993b 3532 while (*p != ';')
c906108c
SS
3533 {
3534 p++;
3535 }
3536 /* Static class member. */
3537 SET_FIELD_PHYSNAME (fip->list->field, savestring (*pp, p - *pp));
3538 *pp = p + 1;
3539 return;
3540 }
3541 else if (**pp != ',')
3542 {
3543 /* Bad structure-type format. */
3544 complain (&stabs_general_complaint, "bad structure-type format");
3545 return;
3546 }
3547
3548 (*pp)++; /* Skip the comma. */
3549
3550 {
3551 int nbits;
3552 FIELD_BITPOS (fip->list->field) = read_huge_number (pp, ',', &nbits);
3553 if (nbits != 0)
3554 {
3555 complain (&stabs_general_complaint, "bad structure-type format");
3556 return;
3557 }
3558 FIELD_BITSIZE (fip->list->field) = read_huge_number (pp, ';', &nbits);
3559 if (nbits != 0)
3560 {
3561 complain (&stabs_general_complaint, "bad structure-type format");
3562 return;
3563 }
3564 }
3565
3566 if (FIELD_BITPOS (fip->list->field) == 0
3567 && FIELD_BITSIZE (fip->list->field) == 0)
3568 {
3569 /* This can happen in two cases: (1) at least for gcc 2.4.5 or so,
c5aa993b
JM
3570 it is a field which has been optimized out. The correct stab for
3571 this case is to use VISIBILITY_IGNORE, but that is a recent
3572 invention. (2) It is a 0-size array. For example
3573 union { int num; char str[0]; } foo. Printing "<no value>" for
3574 str in "p foo" is OK, since foo.str (and thus foo.str[3])
3575 will continue to work, and a 0-size array as a whole doesn't
3576 have any contents to print.
3577
3578 I suspect this probably could also happen with gcc -gstabs (not
3579 -gstabs+) for static fields, and perhaps other C++ extensions.
3580 Hopefully few people use -gstabs with gdb, since it is intended
3581 for dbx compatibility. */
c906108c
SS
3582
3583 /* Ignore this field. */
c5aa993b 3584 fip->list->visibility = VISIBILITY_IGNORE;
c906108c
SS
3585 }
3586 else
3587 {
3588 /* Detect an unpacked field and mark it as such.
c5aa993b
JM
3589 dbx gives a bit size for all fields.
3590 Note that forward refs cannot be packed,
3591 and treat enums as if they had the width of ints. */
c906108c
SS
3592
3593 struct type *field_type = check_typedef (FIELD_TYPE (fip->list->field));
3594
3595 if (TYPE_CODE (field_type) != TYPE_CODE_INT
3596 && TYPE_CODE (field_type) != TYPE_CODE_RANGE
3597 && TYPE_CODE (field_type) != TYPE_CODE_BOOL
3598 && TYPE_CODE (field_type) != TYPE_CODE_ENUM)
3599 {
3600 FIELD_BITSIZE (fip->list->field) = 0;
3601 }
c5aa993b 3602 if ((FIELD_BITSIZE (fip->list->field)
c906108c
SS
3603 == TARGET_CHAR_BIT * TYPE_LENGTH (field_type)
3604 || (TYPE_CODE (field_type) == TYPE_CODE_ENUM
c5aa993b
JM
3605 && FIELD_BITSIZE (fip->list->field) == TARGET_INT_BIT)
3606 )
c906108c
SS
3607 &&
3608 FIELD_BITPOS (fip->list->field) % 8 == 0)
3609 {
3610 FIELD_BITSIZE (fip->list->field) = 0;
3611 }
3612 }
3613}
3614
3615
3616/* Read struct or class data fields. They have the form:
3617
c5aa993b 3618 NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
c906108c
SS
3619
3620 At the end, we see a semicolon instead of a field.
3621
3622 In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
3623 a static field.
3624
3625 The optional VISIBILITY is one of:
3626
c5aa993b
JM
3627 '/0' (VISIBILITY_PRIVATE)
3628 '/1' (VISIBILITY_PROTECTED)
3629 '/2' (VISIBILITY_PUBLIC)
3630 '/9' (VISIBILITY_IGNORE)
c906108c
SS
3631
3632 or nothing, for C style fields with public visibility.
3633
3634 Returns 1 for success, 0 for failure. */
3635
3636static int
fba45db2
KB
3637read_struct_fields (struct field_info *fip, char **pp, struct type *type,
3638 struct objfile *objfile)
c906108c
SS
3639{
3640 register char *p;
3641 struct nextfield *new;
3642
3643 /* We better set p right now, in case there are no fields at all... */
3644
3645 p = *pp;
3646
3647 /* Read each data member type until we find the terminating ';' at the end of
3648 the data member list, or break for some other reason such as finding the
3649 start of the member function list. */
fedbd091
EZ
3650 /* Stab string for structure/union does not end with two ';' in
3651 SUN C compiler 5.3 i.e. F6U2, hence check for end of string. */
c906108c 3652
fedbd091 3653 while (**pp != ';' && **pp != '\0')
c906108c 3654 {
c5aa993b
JM
3655 if (os9k_stabs && **pp == ',')
3656 break;
c906108c
SS
3657 STABS_CONTINUE (pp, objfile);
3658 /* Get space to record the next field's data. */
3659 new = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 3660 make_cleanup (xfree, new);
c906108c 3661 memset (new, 0, sizeof (struct nextfield));
c5aa993b
JM
3662 new->next = fip->list;
3663 fip->list = new;
c906108c
SS
3664
3665 /* Get the field name. */
3666 p = *pp;
3667
3668 /* If is starts with CPLUS_MARKER it is a special abbreviation,
c5aa993b
JM
3669 unless the CPLUS_MARKER is followed by an underscore, in
3670 which case it is just the name of an anonymous type, which we
3671 should handle like any other type name. */
c906108c
SS
3672
3673 if (is_cplus_marker (p[0]) && p[1] != '_')
3674 {
3675 if (!read_cpp_abbrev (fip, pp, type, objfile))
3676 return 0;
3677 continue;
3678 }
3679
3680 /* Look for the ':' that separates the field name from the field
c5aa993b
JM
3681 values. Data members are delimited by a single ':', while member
3682 functions are delimited by a pair of ':'s. When we hit the member
3683 functions (if any), terminate scan loop and return. */
c906108c 3684
c5aa993b 3685 while (*p != ':' && *p != '\0')
c906108c
SS
3686 {
3687 p++;
3688 }
3689 if (*p == '\0')
3690 return 0;
3691
3692 /* Check to see if we have hit the member functions yet. */
3693 if (p[1] == ':')
3694 {
3695 break;
3696 }
3697 read_one_struct_field (fip, pp, p, type, objfile);
3698 }
3699 if (p[0] == ':' && p[1] == ':')
3700 {
3701 /* chill the list of fields: the last entry (at the head) is a
c5aa993b
JM
3702 partially constructed entry which we now scrub. */
3703 fip->list = fip->list->next;
c906108c
SS
3704 }
3705 return 1;
3706}
9846de1b 3707/* *INDENT-OFF* */
c906108c
SS
3708/* The stabs for C++ derived classes contain baseclass information which
3709 is marked by a '!' character after the total size. This function is
3710 called when we encounter the baseclass marker, and slurps up all the
3711 baseclass information.
3712
3713 Immediately following the '!' marker is the number of base classes that
3714 the class is derived from, followed by information for each base class.
3715 For each base class, there are two visibility specifiers, a bit offset
3716 to the base class information within the derived class, a reference to
3717 the type for the base class, and a terminating semicolon.
3718
3719 A typical example, with two base classes, would be "!2,020,19;0264,21;".
3720 ^^ ^ ^ ^ ^ ^ ^
3721 Baseclass information marker __________________|| | | | | | |
3722 Number of baseclasses __________________________| | | | | | |
3723 Visibility specifiers (2) ________________________| | | | | |
3724 Offset in bits from start of class _________________| | | | |
3725 Type number for base class ___________________________| | | |
3726 Visibility specifiers (2) _______________________________| | |
3727 Offset in bits from start of class ________________________| |
3728 Type number of base class ____________________________________|
3729
3730 Return 1 for success, 0 for (error-type-inducing) failure. */
9846de1b 3731/* *INDENT-ON* */
c906108c 3732
c5aa993b
JM
3733
3734
c906108c 3735static int
fba45db2
KB
3736read_baseclasses (struct field_info *fip, char **pp, struct type *type,
3737 struct objfile *objfile)
c906108c
SS
3738{
3739 int i;
3740 struct nextfield *new;
3741
3742 if (**pp != '!')
3743 {
3744 return 1;
3745 }
3746 else
3747 {
3748 /* Skip the '!' baseclass information marker. */
3749 (*pp)++;
3750 }
3751
3752 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3753 {
3754 int nbits;
3755 TYPE_N_BASECLASSES (type) = read_huge_number (pp, ',', &nbits);
3756 if (nbits != 0)
3757 return 0;
3758 }
3759
3760#if 0
3761 /* Some stupid compilers have trouble with the following, so break
3762 it up into simpler expressions. */
3763 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
3764 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
3765#else
3766 {
3767 int num_bytes = B_BYTES (TYPE_N_BASECLASSES (type));
3768 char *pointer;
3769
3770 pointer = (char *) TYPE_ALLOC (type, num_bytes);
3771 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
3772 }
3773#endif /* 0 */
3774
3775 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
3776
3777 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
3778 {
3779 new = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 3780 make_cleanup (xfree, new);
c906108c 3781 memset (new, 0, sizeof (struct nextfield));
c5aa993b
JM
3782 new->next = fip->list;
3783 fip->list = new;
c906108c
SS
3784 FIELD_BITSIZE (new->field) = 0; /* this should be an unpacked field! */
3785
3786 STABS_CONTINUE (pp, objfile);
3787 switch (**pp)
3788 {
c5aa993b
JM
3789 case '0':
3790 /* Nothing to do. */
3791 break;
3792 case '1':
3793 SET_TYPE_FIELD_VIRTUAL (type, i);
3794 break;
3795 default:
3796 /* Unknown character. Complain and treat it as non-virtual. */
3797 {
3798 static struct complaint msg =
c906108c 3799 {
c5aa993b
JM
3800 "Unknown virtual character `%c' for baseclass", 0, 0};
3801 complain (&msg, **pp);
3802 }
c906108c
SS
3803 }
3804 ++(*pp);
3805
c5aa993b
JM
3806 new->visibility = *(*pp)++;
3807 switch (new->visibility)
c906108c 3808 {
c5aa993b
JM
3809 case VISIBILITY_PRIVATE:
3810 case VISIBILITY_PROTECTED:
3811 case VISIBILITY_PUBLIC:
3812 break;
3813 default:
3814 /* Bad visibility format. Complain and treat it as
3815 public. */
3816 {
3817 static struct complaint msg =
c906108c 3818 {
53a5351d
JM
3819 "Unknown visibility `%c' for baseclass", 0, 0
3820 };
c5aa993b
JM
3821 complain (&msg, new->visibility);
3822 new->visibility = VISIBILITY_PUBLIC;
3823 }
c906108c
SS
3824 }
3825
3826 {
3827 int nbits;
c5aa993b 3828
c906108c
SS
3829 /* The remaining value is the bit offset of the portion of the object
3830 corresponding to this baseclass. Always zero in the absence of
3831 multiple inheritance. */
3832
3833 FIELD_BITPOS (new->field) = read_huge_number (pp, ',', &nbits);
3834 if (nbits != 0)
3835 return 0;
3836 }
3837
3838 /* The last piece of baseclass information is the type of the
c5aa993b
JM
3839 base class. Read it, and remember it's type name as this
3840 field's name. */
c906108c 3841
c5aa993b
JM
3842 new->field.type = read_type (pp, objfile);
3843 new->field.name = type_name_no_tag (new->field.type);
c906108c
SS
3844
3845 /* skip trailing ';' and bump count of number of fields seen */
3846 if (**pp == ';')
3847 (*pp)++;
3848 else
3849 return 0;
3850 }
3851 return 1;
3852}
3853
3854/* The tail end of stabs for C++ classes that contain a virtual function
3855 pointer contains a tilde, a %, and a type number.
3856 The type number refers to the base class (possibly this class itself) which
3857 contains the vtable pointer for the current class.
3858
3859 This function is called when we have parsed all the method declarations,
3860 so we can look for the vptr base class info. */
3861
3862static int
fba45db2
KB
3863read_tilde_fields (struct field_info *fip, char **pp, struct type *type,
3864 struct objfile *objfile)
c906108c
SS
3865{
3866 register char *p;
3867
3868 STABS_CONTINUE (pp, objfile);
3869
3870 /* If we are positioned at a ';', then skip it. */
3871 if (**pp == ';')
3872 {
3873 (*pp)++;
3874 }
3875
3876 if (**pp == '~')
3877 {
3878 (*pp)++;
3879
3880 if (**pp == '=' || **pp == '+' || **pp == '-')
3881 {
3882 /* Obsolete flags that used to indicate the presence
3883 of constructors and/or destructors. */
3884 (*pp)++;
3885 }
3886
3887 /* Read either a '%' or the final ';'. */
3888 if (*(*pp)++ == '%')
3889 {
3890 /* The next number is the type number of the base class
3891 (possibly our own class) which supplies the vtable for
3892 this class. Parse it out, and search that class to find
3893 its vtable pointer, and install those into TYPE_VPTR_BASETYPE
3894 and TYPE_VPTR_FIELDNO. */
3895
3896 struct type *t;
3897 int i;
3898
3899 t = read_type (pp, objfile);
3900 p = (*pp)++;
3901 while (*p != '\0' && *p != ';')
3902 {
3903 p++;
3904 }
3905 if (*p == '\0')
3906 {
3907 /* Premature end of symbol. */
3908 return 0;
3909 }
c5aa993b 3910
c906108c 3911 TYPE_VPTR_BASETYPE (type) = t;
c5aa993b 3912 if (type == t) /* Our own class provides vtbl ptr */
c906108c
SS
3913 {
3914 for (i = TYPE_NFIELDS (t) - 1;
3915 i >= TYPE_N_BASECLASSES (t);
3916 --i)
3917 {
c5aa993b
JM
3918 if (!strncmp (TYPE_FIELD_NAME (t, i), vptr_name,
3919 sizeof (vptr_name) - 1))
c906108c
SS
3920 {
3921 TYPE_VPTR_FIELDNO (type) = i;
3922 goto gotit;
3923 }
3924 }
3925 /* Virtual function table field not found. */
3926 complain (&vtbl_notfound_complaint, TYPE_NAME (type));
3927 return 0;
3928 }
3929 else
3930 {
3931 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
3932 }
3933
c5aa993b 3934 gotit:
c906108c
SS
3935 *pp = p + 1;
3936 }
3937 }
3938 return 1;
3939}
3940
3941static int
fba45db2 3942attach_fn_fields_to_type (struct field_info *fip, register struct type *type)
c906108c
SS
3943{
3944 register int n;
3945
3946 for (n = TYPE_NFN_FIELDS (type);
c5aa993b
JM
3947 fip->fnlist != NULL;
3948 fip->fnlist = fip->fnlist->next)
c906108c 3949 {
c5aa993b
JM
3950 --n; /* Circumvent Sun3 compiler bug */
3951 TYPE_FN_FIELDLISTS (type)[n] = fip->fnlist->fn_fieldlist;
c906108c
SS
3952 }
3953 return 1;
3954}
3955
3956/* read cfront class static data.
3957 pp points to string starting with the list of static data
3958 eg: A:ZcA;1@Bpub v2@Bvirpri;__ct__1AFv func__1AFv *sfunc__1AFv ;as__1A ;;
c5aa993b 3959 ^^^^^^^^
c906108c 3960
c5aa993b
JM
3961 A:ZcA;;foopri__1AFv foopro__1AFv __ct__1AFv __ct__1AFRC1A foopub__1AFv ;;;
3962 ^
3963 */
c906108c
SS
3964
3965static int
fba45db2
KB
3966read_cfront_static_fields (struct field_info *fip, char **pp, struct type *type,
3967 struct objfile *objfile)
c906108c 3968{
c5aa993b 3969 struct nextfield *new;
c906108c 3970 struct type *stype;
c5aa993b
JM
3971 char *sname;
3972 struct symbol *ref_static = 0;
3973
3974 if (**pp == ';') /* no static data; return */
c906108c
SS
3975 {
3976 ++(*pp);
3977 return 1;
3978 }
3979
3980 /* Process each field in the list until we find the terminating ";" */
3981
3982 /* eg: p = "as__1A ;;;" */
c5aa993b
JM
3983 STABS_CONTINUE (pp, objfile); /* handle \\ */
3984 while (**pp != ';' && (sname = get_substring (pp, ' '), sname))
c906108c 3985 {
c5aa993b
JM
3986 ref_static = lookup_symbol (sname, 0, VAR_NAMESPACE, 0, 0); /*demangled_name */
3987 if (!ref_static)
3988 {
3989 static struct complaint msg =
3990 {"\
c906108c 3991 Unable to find symbol for static data field %s\n",
c5aa993b 3992 0, 0};
c906108c
SS
3993 complain (&msg, sname);
3994 continue;
3995 }
c5aa993b 3996 stype = SYMBOL_TYPE (ref_static);
c906108c
SS
3997
3998 /* allocate a new fip */
3999 new = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 4000 make_cleanup (xfree, new);
c906108c 4001 memset (new, 0, sizeof (struct nextfield));
c5aa993b
JM
4002 new->next = fip->list;
4003 fip->list = new;
c906108c
SS
4004
4005 /* set visibility */
4006 /* FIXME! no way to tell visibility from stabs??? */
c5aa993b 4007 new->visibility = VISIBILITY_PUBLIC;
c906108c
SS
4008
4009 /* set field info into fip */
c5aa993b 4010 fip->list->field.type = stype;
c906108c
SS
4011
4012 /* set bitpos & bitsize */
4013 SET_FIELD_PHYSNAME (fip->list->field, savestring (sname, strlen (sname)));
4014
4015 /* set name field */
4016 /* The following is code to work around cfront generated stabs.
4017 The stabs contains full mangled name for each field.
4018 We try to demangle the name and extract the field name out of it.
c5aa993b 4019 */
c906108c 4020 if (ARM_DEMANGLING)
c5aa993b
JM
4021 {
4022 char *dem, *dem_p;
4023 dem = cplus_demangle (sname, DMGL_ANSI | DMGL_PARAMS);
4024 if (dem != NULL)
4025 {
4026 dem_p = strrchr (dem, ':');
4027 if (dem_p != 0 && *(dem_p - 1) == ':')
4028 dem_p++;
4029 fip->list->field.name =
4030 obsavestring (dem_p, strlen (dem_p), &objfile->type_obstack);
c906108c 4031 }
c5aa993b
JM
4032 else
4033 {
4034 fip->list->field.name =
4035 obsavestring (sname, strlen (sname), &objfile->type_obstack);
4036 }
4037 } /* end of code for cfront work around */
4038 } /* loop again for next static field */
c906108c
SS
4039 return 1;
4040}
4041
4042/* Copy structure fields to fip so attach_fields_to_type will work.
4043 type has already been created with the initial instance data fields.
4044 Now we want to be able to add the other members to the class,
4045 so we want to add them back to the fip and reattach them again
4046 once we have collected all the class members. */
4047
4048static int
fba45db2
KB
4049copy_cfront_struct_fields (struct field_info *fip, struct type *type,
4050 struct objfile *objfile)
c906108c 4051{
c5aa993b 4052 int nfields = TYPE_NFIELDS (type);
c906108c 4053 int i;
c5aa993b 4054 struct nextfield *new;
c906108c
SS
4055
4056 /* Copy the fields into the list of fips and reset the types
4057 to remove the old fields */
4058
c5aa993b 4059 for (i = 0; i < nfields; i++)
c906108c
SS
4060 {
4061 /* allocate a new fip */
4062 new = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 4063 make_cleanup (xfree, new);
c906108c 4064 memset (new, 0, sizeof (struct nextfield));
c5aa993b
JM
4065 new->next = fip->list;
4066 fip->list = new;
c906108c
SS
4067
4068 /* copy field info into fip */
c5aa993b 4069 new->field = TYPE_FIELD (type, i);
c906108c
SS
4070 /* set visibility */
4071 if (TYPE_FIELD_PROTECTED (type, i))
c5aa993b 4072 new->visibility = VISIBILITY_PROTECTED;
c906108c 4073 else if (TYPE_FIELD_PRIVATE (type, i))
c5aa993b 4074 new->visibility = VISIBILITY_PRIVATE;
c906108c 4075 else
c5aa993b 4076 new->visibility = VISIBILITY_PUBLIC;
c906108c
SS
4077 }
4078 /* Now delete the fields from the type since we will be
4079 allocing new space once we get the rest of the fields
4080 in attach_fields_to_type.
4081 The pointer TYPE_FIELDS(type) is left dangling but should
4082 be freed later by objstack_free */
c5aa993b 4083 TYPE_FIELDS (type) = 0;
c906108c
SS
4084 TYPE_NFIELDS (type) = 0;
4085
4086 return 1;
4087}
4088
4089/* Create the vector of fields, and record how big it is.
4090 We need this info to record proper virtual function table information
4091 for this class's virtual functions. */
4092
4093static int
fba45db2
KB
4094attach_fields_to_type (struct field_info *fip, register struct type *type,
4095 struct objfile *objfile)
c906108c
SS
4096{
4097 register int nfields = 0;
4098 register int non_public_fields = 0;
4099 register struct nextfield *scan;
4100
4101 /* Count up the number of fields that we have, as well as taking note of
4102 whether or not there are any non-public fields, which requires us to
4103 allocate and build the private_field_bits and protected_field_bits
4104 bitfields. */
4105
c5aa993b 4106 for (scan = fip->list; scan != NULL; scan = scan->next)
c906108c
SS
4107 {
4108 nfields++;
c5aa993b 4109 if (scan->visibility != VISIBILITY_PUBLIC)
c906108c
SS
4110 {
4111 non_public_fields++;
4112 }
4113 }
4114
4115 /* Now we know how many fields there are, and whether or not there are any
4116 non-public fields. Record the field count, allocate space for the
4117 array of fields, and create blank visibility bitfields if necessary. */
4118
4119 TYPE_NFIELDS (type) = nfields;
4120 TYPE_FIELDS (type) = (struct field *)
4121 TYPE_ALLOC (type, sizeof (struct field) * nfields);
4122 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
4123
4124 if (non_public_fields)
4125 {
4126 ALLOCATE_CPLUS_STRUCT_TYPE (type);
4127
4128 TYPE_FIELD_PRIVATE_BITS (type) =
4129 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4130 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
4131
4132 TYPE_FIELD_PROTECTED_BITS (type) =
4133 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4134 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
4135
4136 TYPE_FIELD_IGNORE_BITS (type) =
4137 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4138 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
4139 }
4140
4141 /* Copy the saved-up fields into the field vector. Start from the head
4142 of the list, adding to the tail of the field array, so that they end
4143 up in the same order in the array in which they were added to the list. */
4144
4145 while (nfields-- > 0)
4146 {
c5aa993b
JM
4147 TYPE_FIELD (type, nfields) = fip->list->field;
4148 switch (fip->list->visibility)
c906108c 4149 {
c5aa993b
JM
4150 case VISIBILITY_PRIVATE:
4151 SET_TYPE_FIELD_PRIVATE (type, nfields);
4152 break;
c906108c 4153
c5aa993b
JM
4154 case VISIBILITY_PROTECTED:
4155 SET_TYPE_FIELD_PROTECTED (type, nfields);
4156 break;
c906108c 4157
c5aa993b
JM
4158 case VISIBILITY_IGNORE:
4159 SET_TYPE_FIELD_IGNORE (type, nfields);
4160 break;
c906108c 4161
c5aa993b
JM
4162 case VISIBILITY_PUBLIC:
4163 break;
c906108c 4164
c5aa993b
JM
4165 default:
4166 /* Unknown visibility. Complain and treat it as public. */
4167 {
4168 static struct complaint msg =
c906108c 4169 {
c5aa993b
JM
4170 "Unknown visibility `%c' for field", 0, 0};
4171 complain (&msg, fip->list->visibility);
4172 }
4173 break;
c906108c 4174 }
c5aa993b 4175 fip->list = fip->list->next;
c906108c
SS
4176 }
4177 return 1;
4178}
4179
2ae1c2d2
JB
4180
4181static struct complaint multiply_defined_struct =
4182{"struct/union type gets multiply defined: %s%s", 0, 0};
4183
4184
4185/* Complain that the compiler has emitted more than one definition for the
4186 structure type TYPE. */
4187static void
4188complain_about_struct_wipeout (struct type *type)
4189{
4190 char *name = "";
4191 char *kind = "";
4192
4193 if (TYPE_TAG_NAME (type))
4194 {
4195 name = TYPE_TAG_NAME (type);
4196 switch (TYPE_CODE (type))
4197 {
4198 case TYPE_CODE_STRUCT: kind = "struct "; break;
4199 case TYPE_CODE_UNION: kind = "union "; break;
4200 case TYPE_CODE_ENUM: kind = "enum "; break;
4201 default: kind = "";
4202 }
4203 }
4204 else if (TYPE_NAME (type))
4205 {
4206 name = TYPE_NAME (type);
4207 kind = "";
4208 }
4209 else
4210 {
4211 name = "<unknown>";
4212 kind = "";
4213 }
4214
4215 complain (&multiply_defined_struct, kind, name);
4216}
4217
4218
c906108c
SS
4219/* Read the description of a structure (or union type) and return an object
4220 describing the type.
4221
4222 PP points to a character pointer that points to the next unconsumed token
4223 in the the stabs string. For example, given stabs "A:T4=s4a:1,0,32;;",
4224 *PP will point to "4a:1,0,32;;".
4225
4226 TYPE points to an incomplete type that needs to be filled in.
4227
4228 OBJFILE points to the current objfile from which the stabs information is
4229 being read. (Note that it is redundant in that TYPE also contains a pointer
4230 to this same objfile, so it might be a good idea to eliminate it. FIXME).
c5aa993b 4231 */
c906108c
SS
4232
4233static struct type *
2ae1c2d2
JB
4234read_struct_type (char **pp, struct type *type, enum type_code type_code,
4235 struct objfile *objfile)
c906108c
SS
4236{
4237 struct cleanup *back_to;
4238 struct field_info fi;
4239
4240 fi.list = NULL;
4241 fi.fnlist = NULL;
4242
2ae1c2d2
JB
4243 /* When describing struct/union/class types in stabs, G++ always drops
4244 all qualifications from the name. So if you've got:
4245 struct A { ... struct B { ... }; ... };
4246 then G++ will emit stabs for `struct A::B' that call it simply
4247 `struct B'. Obviously, if you've got a real top-level definition for
4248 `struct B', or other nested definitions, this is going to cause
4249 problems.
4250
4251 Obviously, GDB can't fix this by itself, but it can at least avoid
4252 scribbling on existing structure type objects when new definitions
4253 appear. */
4254 if (! (TYPE_CODE (type) == TYPE_CODE_UNDEF
4255 || TYPE_STUB (type)))
4256 {
4257 complain_about_struct_wipeout (type);
4258
4259 /* It's probably best to return the type unchanged. */
4260 return type;
4261 }
4262
c906108c
SS
4263 back_to = make_cleanup (null_cleanup, 0);
4264
4265 INIT_CPLUS_SPECIFIC (type);
2ae1c2d2 4266 TYPE_CODE (type) = type_code;
c906108c
SS
4267 TYPE_FLAGS (type) &= ~TYPE_FLAG_STUB;
4268
4269 /* First comes the total size in bytes. */
4270
4271 {
4272 int nbits;
4273 TYPE_LENGTH (type) = read_huge_number (pp, 0, &nbits);
4274 if (nbits != 0)
4275 return error_type (pp, objfile);
4276 }
4277
4278 /* Now read the baseclasses, if any, read the regular C struct or C++
4279 class member fields, attach the fields to the type, read the C++
4280 member functions, attach them to the type, and then read any tilde
4281 field (baseclass specifier for the class holding the main vtable). */
4282
4283 if (!read_baseclasses (&fi, pp, type, objfile)
4284 || !read_struct_fields (&fi, pp, type, objfile)
4285 || !attach_fields_to_type (&fi, type, objfile)
4286 || !read_member_functions (&fi, pp, type, objfile)
4287 || !attach_fn_fields_to_type (&fi, type)
4288 || !read_tilde_fields (&fi, pp, type, objfile))
4289 {
4290 type = error_type (pp, objfile);
4291 }
4292
a02fd225
DJ
4293 /* Fix up any cv-qualified versions of this type. */
4294 finish_cv_type (type);
c906108c
SS
4295 do_cleanups (back_to);
4296 return (type);
4297}
4298
4299/* Read a definition of an array type,
4300 and create and return a suitable type object.
4301 Also creates a range type which represents the bounds of that
4302 array. */
4303
4304static struct type *
fba45db2
KB
4305read_array_type (register char **pp, register struct type *type,
4306 struct objfile *objfile)
c906108c
SS
4307{
4308 struct type *index_type, *element_type, *range_type;
4309 int lower, upper;
4310 int adjustable = 0;
4311 int nbits;
4312
4313 /* Format of an array type:
4314 "ar<index type>;lower;upper;<array_contents_type>".
4315 OS9000: "arlower,upper;<array_contents_type>".
4316
4317 Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
4318 for these, produce a type like float[][]. */
4319
4320 if (os9k_stabs)
4321 index_type = builtin_type_int;
4322 else
4323 {
4324 index_type = read_type (pp, objfile);
4325 if (**pp != ';')
4326 /* Improper format of array type decl. */
4327 return error_type (pp, objfile);
4328 ++*pp;
4329 }
4330
4331 if (!(**pp >= '0' && **pp <= '9') && **pp != '-')
4332 {
4333 (*pp)++;
4334 adjustable = 1;
4335 }
4336 lower = read_huge_number (pp, os9k_stabs ? ',' : ';', &nbits);
4337 if (nbits != 0)
4338 return error_type (pp, objfile);
4339
4340 if (!(**pp >= '0' && **pp <= '9') && **pp != '-')
4341 {
4342 (*pp)++;
4343 adjustable = 1;
4344 }
4345 upper = read_huge_number (pp, ';', &nbits);
4346 if (nbits != 0)
4347 return error_type (pp, objfile);
c5aa993b 4348
c906108c
SS
4349 element_type = read_type (pp, objfile);
4350
4351 if (adjustable)
4352 {
4353 lower = 0;
4354 upper = -1;
4355 }
4356
4357 range_type =
4358 create_range_type ((struct type *) NULL, index_type, lower, upper);
4359 type = create_array_type (type, element_type, range_type);
4360
4361 return type;
4362}
4363
4364
4365/* Read a definition of an enumeration type,
4366 and create and return a suitable type object.
4367 Also defines the symbols that represent the values of the type. */
4368
4369static struct type *
fba45db2
KB
4370read_enum_type (register char **pp, register struct type *type,
4371 struct objfile *objfile)
c906108c
SS
4372{
4373 register char *p;
4374 char *name;
4375 register long n;
4376 register struct symbol *sym;
4377 int nsyms = 0;
4378 struct pending **symlist;
4379 struct pending *osyms, *syms;
4380 int o_nsyms;
4381 int nbits;
4382 int unsigned_enum = 1;
4383
4384#if 0
4385 /* FIXME! The stabs produced by Sun CC merrily define things that ought
4386 to be file-scope, between N_FN entries, using N_LSYM. What's a mother
4387 to do? For now, force all enum values to file scope. */
4388 if (within_function)
4389 symlist = &local_symbols;
4390 else
4391#endif
4392 symlist = &file_symbols;
4393 osyms = *symlist;
4394 o_nsyms = osyms ? osyms->nsyms : 0;
4395
4396 if (os9k_stabs)
4397 {
4398 /* Size. Perhaps this does not have to be conditionalized on
c5aa993b
JM
4399 os9k_stabs (assuming the name of an enum constant can't start
4400 with a digit). */
c906108c
SS
4401 read_huge_number (pp, 0, &nbits);
4402 if (nbits != 0)
4403 return error_type (pp, objfile);
4404 }
4405
4406 /* The aix4 compiler emits an extra field before the enum members;
4407 my guess is it's a type of some sort. Just ignore it. */
4408 if (**pp == '-')
4409 {
4410 /* Skip over the type. */
4411 while (**pp != ':')
c5aa993b 4412 (*pp)++;
c906108c
SS
4413
4414 /* Skip over the colon. */
4415 (*pp)++;
4416 }
4417
4418 /* Read the value-names and their values.
4419 The input syntax is NAME:VALUE,NAME:VALUE, and so on.
4420 A semicolon or comma instead of a NAME means the end. */
4421 while (**pp && **pp != ';' && **pp != ',')
4422 {
4423 STABS_CONTINUE (pp, objfile);
4424 p = *pp;
c5aa993b
JM
4425 while (*p != ':')
4426 p++;
4427 name = obsavestring (*pp, p - *pp, &objfile->symbol_obstack);
c906108c
SS
4428 *pp = p + 1;
4429 n = read_huge_number (pp, ',', &nbits);
4430 if (nbits != 0)
4431 return error_type (pp, objfile);
4432
4433 sym = (struct symbol *)
c5aa993b 4434 obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol));
c906108c
SS
4435 memset (sym, 0, sizeof (struct symbol));
4436 SYMBOL_NAME (sym) = name;
c5aa993b 4437 SYMBOL_LANGUAGE (sym) = current_subfile->language;
c906108c
SS
4438 SYMBOL_CLASS (sym) = LOC_CONST;
4439 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4440 SYMBOL_VALUE (sym) = n;
4441 if (n < 0)
4442 unsigned_enum = 0;
4443 add_symbol_to_list (sym, symlist);
4444 nsyms++;
4445 }
4446
4447 if (**pp == ';')
4448 (*pp)++; /* Skip the semicolon. */
4449
4450 /* Now fill in the fields of the type-structure. */
4451
4452 TYPE_LENGTH (type) = TARGET_INT_BIT / HOST_CHAR_BIT;
4453 TYPE_CODE (type) = TYPE_CODE_ENUM;
4454 TYPE_FLAGS (type) &= ~TYPE_FLAG_STUB;
4455 if (unsigned_enum)
4456 TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
4457 TYPE_NFIELDS (type) = nsyms;
4458 TYPE_FIELDS (type) = (struct field *)
4459 TYPE_ALLOC (type, sizeof (struct field) * nsyms);
4460 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nsyms);
4461
4462 /* Find the symbols for the values and put them into the type.
4463 The symbols can be found in the symlist that we put them on
4464 to cause them to be defined. osyms contains the old value
4465 of that symlist; everything up to there was defined by us. */
4466 /* Note that we preserve the order of the enum constants, so
4467 that in something like "enum {FOO, LAST_THING=FOO}" we print
4468 FOO, not LAST_THING. */
4469
4470 for (syms = *symlist, n = nsyms - 1; syms; syms = syms->next)
4471 {
4472 int last = syms == osyms ? o_nsyms : 0;
4473 int j = syms->nsyms;
4474 for (; --j >= last; --n)
4475 {
4476 struct symbol *xsym = syms->symbol[j];
4477 SYMBOL_TYPE (xsym) = type;
4478 TYPE_FIELD_NAME (type, n) = SYMBOL_NAME (xsym);
4479 TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (xsym);
4480 TYPE_FIELD_BITSIZE (type, n) = 0;
4481 }
4482 if (syms == osyms)
4483 break;
4484 }
4485
4486 return type;
4487}
4488
4489/* Sun's ACC uses a somewhat saner method for specifying the builtin
4490 typedefs in every file (for int, long, etc):
4491
c5aa993b
JM
4492 type = b <signed> <width> <format type>; <offset>; <nbits>
4493 signed = u or s.
4494 optional format type = c or b for char or boolean.
4495 offset = offset from high order bit to start bit of type.
4496 width is # bytes in object of this type, nbits is # bits in type.
c906108c
SS
4497
4498 The width/offset stuff appears to be for small objects stored in
4499 larger ones (e.g. `shorts' in `int' registers). We ignore it for now,
4500 FIXME. */
4501
4502static struct type *
35a2f538 4503read_sun_builtin_type (char **pp, int typenums[2], struct objfile *objfile)
c906108c
SS
4504{
4505 int type_bits;
4506 int nbits;
4507 int signed_type;
4508 enum type_code code = TYPE_CODE_INT;
4509
4510 switch (**pp)
4511 {
c5aa993b
JM
4512 case 's':
4513 signed_type = 1;
4514 break;
4515 case 'u':
4516 signed_type = 0;
4517 break;
4518 default:
4519 return error_type (pp, objfile);
c906108c
SS
4520 }
4521 (*pp)++;
4522
4523 /* For some odd reason, all forms of char put a c here. This is strange
4524 because no other type has this honor. We can safely ignore this because
4525 we actually determine 'char'acterness by the number of bits specified in
4526 the descriptor.
4527 Boolean forms, e.g Fortran logical*X, put a b here. */
4528
4529 if (**pp == 'c')
4530 (*pp)++;
4531 else if (**pp == 'b')
4532 {
4533 code = TYPE_CODE_BOOL;
4534 (*pp)++;
4535 }
4536
4537 /* The first number appears to be the number of bytes occupied
4538 by this type, except that unsigned short is 4 instead of 2.
4539 Since this information is redundant with the third number,
4540 we will ignore it. */
4541 read_huge_number (pp, ';', &nbits);
4542 if (nbits != 0)
4543 return error_type (pp, objfile);
4544
4545 /* The second number is always 0, so ignore it too. */
4546 read_huge_number (pp, ';', &nbits);
4547 if (nbits != 0)
4548 return error_type (pp, objfile);
4549
4550 /* The third number is the number of bits for this type. */
4551 type_bits = read_huge_number (pp, 0, &nbits);
4552 if (nbits != 0)
4553 return error_type (pp, objfile);
4554 /* The type *should* end with a semicolon. If it are embedded
4555 in a larger type the semicolon may be the only way to know where
4556 the type ends. If this type is at the end of the stabstring we
4557 can deal with the omitted semicolon (but we don't have to like
4558 it). Don't bother to complain(), Sun's compiler omits the semicolon
4559 for "void". */
4560 if (**pp == ';')
4561 ++(*pp);
4562
4563 if (type_bits == 0)
4564 return init_type (TYPE_CODE_VOID, 1,
c5aa993b 4565 signed_type ? 0 : TYPE_FLAG_UNSIGNED, (char *) NULL,
c906108c
SS
4566 objfile);
4567 else
4568 return init_type (code,
4569 type_bits / TARGET_CHAR_BIT,
c5aa993b 4570 signed_type ? 0 : TYPE_FLAG_UNSIGNED, (char *) NULL,
c906108c
SS
4571 objfile);
4572}
4573
4574static struct type *
35a2f538 4575read_sun_floating_type (char **pp, int typenums[2], struct objfile *objfile)
c906108c
SS
4576{
4577 int nbits;
4578 int details;
4579 int nbytes;
f65ca430 4580 struct type *rettype;
c906108c
SS
4581
4582 /* The first number has more details about the type, for example
4583 FN_COMPLEX. */
4584 details = read_huge_number (pp, ';', &nbits);
4585 if (nbits != 0)
4586 return error_type (pp, objfile);
4587
4588 /* The second number is the number of bytes occupied by this type */
4589 nbytes = read_huge_number (pp, ';', &nbits);
4590 if (nbits != 0)
4591 return error_type (pp, objfile);
4592
4593 if (details == NF_COMPLEX || details == NF_COMPLEX16
4594 || details == NF_COMPLEX32)
f65ca430
DJ
4595 {
4596 rettype = init_type (TYPE_CODE_COMPLEX, nbytes, 0, NULL, objfile);
4597 TYPE_TARGET_TYPE (rettype)
4598 = init_type (TYPE_CODE_FLT, nbytes / 2, 0, NULL, objfile);
4599 return rettype;
4600 }
c906108c
SS
4601
4602 return init_type (TYPE_CODE_FLT, nbytes, 0, NULL, objfile);
4603}
4604
4605/* Read a number from the string pointed to by *PP.
4606 The value of *PP is advanced over the number.
4607 If END is nonzero, the character that ends the
4608 number must match END, or an error happens;
4609 and that character is skipped if it does match.
4610 If END is zero, *PP is left pointing to that character.
4611
4612 If the number fits in a long, set *BITS to 0 and return the value.
4613 If not, set *BITS to be the number of bits in the number and return 0.
4614
4615 If encounter garbage, set *BITS to -1 and return 0. */
4616
c2d11a7d 4617static long
fba45db2 4618read_huge_number (char **pp, int end, int *bits)
c906108c
SS
4619{
4620 char *p = *pp;
4621 int sign = 1;
c2d11a7d 4622 long n = 0;
c906108c
SS
4623 int radix = 10;
4624 char overflow = 0;
4625 int nbits = 0;
4626 int c;
c2d11a7d 4627 long upper_limit;
c5aa993b 4628
c906108c
SS
4629 if (*p == '-')
4630 {
4631 sign = -1;
4632 p++;
4633 }
4634
4635 /* Leading zero means octal. GCC uses this to output values larger
4636 than an int (because that would be hard in decimal). */
4637 if (*p == '0')
4638 {
4639 radix = 8;
4640 p++;
4641 }
4642
4643 if (os9k_stabs)
c2d11a7d 4644 upper_limit = ULONG_MAX / radix;
c906108c 4645 else
c2d11a7d 4646 upper_limit = LONG_MAX / radix;
c906108c
SS
4647
4648 while ((c = *p++) >= '0' && c < ('0' + radix))
4649 {
4650 if (n <= upper_limit)
4651 {
4652 n *= radix;
4653 n += c - '0'; /* FIXME this overflows anyway */
4654 }
4655 else
4656 overflow = 1;
c5aa993b 4657
c906108c 4658 /* This depends on large values being output in octal, which is
c5aa993b 4659 what GCC does. */
c906108c
SS
4660 if (radix == 8)
4661 {
4662 if (nbits == 0)
4663 {
4664 if (c == '0')
4665 /* Ignore leading zeroes. */
4666 ;
4667 else if (c == '1')
4668 nbits = 1;
4669 else if (c == '2' || c == '3')
4670 nbits = 2;
4671 else
4672 nbits = 3;
4673 }
4674 else
4675 nbits += 3;
4676 }
4677 }
4678 if (end)
4679 {
4680 if (c && c != end)
4681 {
4682 if (bits != NULL)
4683 *bits = -1;
4684 return 0;
4685 }
4686 }
4687 else
4688 --p;
4689
4690 *pp = p;
4691 if (overflow)
4692 {
4693 if (nbits == 0)
4694 {
4695 /* Large decimal constants are an error (because it is hard to
4696 count how many bits are in them). */
4697 if (bits != NULL)
4698 *bits = -1;
4699 return 0;
4700 }
c5aa993b 4701
c906108c 4702 /* -0x7f is the same as 0x80. So deal with it by adding one to
c5aa993b 4703 the number of bits. */
c906108c
SS
4704 if (sign == -1)
4705 ++nbits;
4706 if (bits)
4707 *bits = nbits;
4708 }
4709 else
4710 {
4711 if (bits)
4712 *bits = 0;
4713 return n * sign;
4714 }
4715 /* It's *BITS which has the interesting information. */
4716 return 0;
4717}
4718
4719static struct type *
35a2f538 4720read_range_type (char **pp, int typenums[2], struct objfile *objfile)
c906108c
SS
4721{
4722 char *orig_pp = *pp;
4723 int rangenums[2];
c2d11a7d 4724 long n2, n3;
c906108c
SS
4725 int n2bits, n3bits;
4726 int self_subrange;
4727 struct type *result_type;
4728 struct type *index_type = NULL;
4729
4730 /* First comes a type we are a subrange of.
4731 In C it is usually 0, 1 or the type being defined. */
4732 if (read_type_number (pp, rangenums) != 0)
4733 return error_type (pp, objfile);
4734 self_subrange = (rangenums[0] == typenums[0] &&
4735 rangenums[1] == typenums[1]);
4736
4737 if (**pp == '=')
4738 {
4739 *pp = orig_pp;
4740 index_type = read_type (pp, objfile);
4741 }
4742
4743 /* A semicolon should now follow; skip it. */
4744 if (**pp == ';')
4745 (*pp)++;
4746
4747 /* The remaining two operands are usually lower and upper bounds
4748 of the range. But in some special cases they mean something else. */
4749 n2 = read_huge_number (pp, ';', &n2bits);
4750 n3 = read_huge_number (pp, ';', &n3bits);
4751
4752 if (n2bits == -1 || n3bits == -1)
4753 return error_type (pp, objfile);
4754
4755 if (index_type)
4756 goto handle_true_range;
4757
4758 /* If limits are huge, must be large integral type. */
4759 if (n2bits != 0 || n3bits != 0)
4760 {
4761 char got_signed = 0;
4762 char got_unsigned = 0;
4763 /* Number of bits in the type. */
4764 int nbits = 0;
4765
4766 /* Range from 0 to <large number> is an unsigned large integral type. */
4767 if ((n2bits == 0 && n2 == 0) && n3bits != 0)
4768 {
4769 got_unsigned = 1;
4770 nbits = n3bits;
4771 }
4772 /* Range from <large number> to <large number>-1 is a large signed
c5aa993b
JM
4773 integral type. Take care of the case where <large number> doesn't
4774 fit in a long but <large number>-1 does. */
c906108c
SS
4775 else if ((n2bits != 0 && n3bits != 0 && n2bits == n3bits + 1)
4776 || (n2bits != 0 && n3bits == 0
c2d11a7d
JM
4777 && (n2bits == sizeof (long) * HOST_CHAR_BIT)
4778 && n3 == LONG_MAX))
c906108c
SS
4779 {
4780 got_signed = 1;
4781 nbits = n2bits;
4782 }
4783
4784 if (got_signed || got_unsigned)
4785 {
4786 return init_type (TYPE_CODE_INT, nbits / TARGET_CHAR_BIT,
4787 got_unsigned ? TYPE_FLAG_UNSIGNED : 0, NULL,
4788 objfile);
4789 }
4790 else
4791 return error_type (pp, objfile);
4792 }
4793
4794 /* A type defined as a subrange of itself, with bounds both 0, is void. */
4795 if (self_subrange && n2 == 0 && n3 == 0)
4796 return init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
4797
4798 /* If n3 is zero and n2 is positive, we want a floating type, and n2
4799 is the width in bytes.
4800
4801 Fortran programs appear to use this for complex types also. To
4802 distinguish between floats and complex, g77 (and others?) seem
4803 to use self-subranges for the complexes, and subranges of int for
4804 the floats.
4805
4806 Also note that for complexes, g77 sets n2 to the size of one of
4807 the member floats, not the whole complex beast. My guess is that
4808 this was to work well with pre-COMPLEX versions of gdb. */
4809
4810 if (n3 == 0 && n2 > 0)
4811 {
1300f5dd
JB
4812 struct type *float_type
4813 = init_type (TYPE_CODE_FLT, n2, 0, NULL, objfile);
4814
c906108c
SS
4815 if (self_subrange)
4816 {
1300f5dd
JB
4817 struct type *complex_type =
4818 init_type (TYPE_CODE_COMPLEX, 2 * n2, 0, NULL, objfile);
4819 TYPE_TARGET_TYPE (complex_type) = float_type;
4820 return complex_type;
c906108c
SS
4821 }
4822 else
1300f5dd 4823 return float_type;
c906108c
SS
4824 }
4825
4826 /* If the upper bound is -1, it must really be an unsigned int. */
4827
4828 else if (n2 == 0 && n3 == -1)
4829 {
4830 /* It is unsigned int or unsigned long. */
4831 /* GCC 2.3.3 uses this for long long too, but that is just a GDB 3.5
c5aa993b 4832 compatibility hack. */
c906108c
SS
4833 return init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
4834 TYPE_FLAG_UNSIGNED, NULL, objfile);
4835 }
4836
4837 /* Special case: char is defined (Who knows why) as a subrange of
4838 itself with range 0-127. */
4839 else if (self_subrange && n2 == 0 && n3 == 127)
4840 return init_type (TYPE_CODE_INT, 1, 0, NULL, objfile);
4841
4842 else if (current_symbol && SYMBOL_LANGUAGE (current_symbol) == language_chill
4843 && !self_subrange)
4844 goto handle_true_range;
4845
4846 /* We used to do this only for subrange of self or subrange of int. */
4847 else if (n2 == 0)
4848 {
a0b3c4fd
JM
4849 /* -1 is used for the upper bound of (4 byte) "unsigned int" and
4850 "unsigned long", and we already checked for that,
4851 so don't need to test for it here. */
4852
c906108c
SS
4853 if (n3 < 0)
4854 /* n3 actually gives the size. */
c5aa993b 4855 return init_type (TYPE_CODE_INT, -n3, TYPE_FLAG_UNSIGNED,
c906108c 4856 NULL, objfile);
c906108c 4857
7be570e7 4858 /* Is n3 == 2**(8n)-1 for some integer n? Then it's an
a0b3c4fd
JM
4859 unsigned n-byte integer. But do require n to be a power of
4860 two; we don't want 3- and 5-byte integers flying around. */
4861 {
4862 int bytes;
4863 unsigned long bits;
4864
4865 bits = n3;
4866 for (bytes = 0; (bits & 0xff) == 0xff; bytes++)
4867 bits >>= 8;
4868 if (bits == 0
4869 && ((bytes - 1) & bytes) == 0) /* "bytes is a power of two" */
4870 return init_type (TYPE_CODE_INT, bytes, TYPE_FLAG_UNSIGNED, NULL,
4871 objfile);
4872 }
c906108c
SS
4873 }
4874 /* I think this is for Convex "long long". Since I don't know whether
4875 Convex sets self_subrange, I also accept that particular size regardless
4876 of self_subrange. */
4877 else if (n3 == 0 && n2 < 0
4878 && (self_subrange
c5aa993b
JM
4879 || n2 == -TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT))
4880 return init_type (TYPE_CODE_INT, -n2, 0, NULL, objfile);
4881 else if (n2 == -n3 - 1)
c906108c
SS
4882 {
4883 if (n3 == 0x7f)
4884 return init_type (TYPE_CODE_INT, 1, 0, NULL, objfile);
4885 if (n3 == 0x7fff)
4886 return init_type (TYPE_CODE_INT, 2, 0, NULL, objfile);
4887 if (n3 == 0x7fffffff)
4888 return init_type (TYPE_CODE_INT, 4, 0, NULL, objfile);
4889 }
4890
4891 /* We have a real range type on our hands. Allocate space and
4892 return a real pointer. */
c5aa993b 4893handle_true_range:
c906108c
SS
4894
4895 if (self_subrange)
4896 index_type = builtin_type_int;
4897 else
4898 index_type = *dbx_lookup_type (rangenums);
4899 if (index_type == NULL)
4900 {
4901 /* Does this actually ever happen? Is that why we are worrying
4902 about dealing with it rather than just calling error_type? */
4903
4904 static struct type *range_type_index;
4905
4906 complain (&range_type_base_complaint, rangenums[1]);
4907 if (range_type_index == NULL)
4908 range_type_index =
4909 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
4910 0, "range type index type", NULL);
4911 index_type = range_type_index;
4912 }
4913
4914 result_type = create_range_type ((struct type *) NULL, index_type, n2, n3);
4915 return (result_type);
4916}
4917
4918/* Read in an argument list. This is a list of types, separated by commas
4919 and terminated with END. Return the list of types read in, or (struct type
4920 **)-1 if there is an error. */
4921
4922static struct type **
fba45db2 4923read_args (char **pp, int end, struct objfile *objfile)
c906108c
SS
4924{
4925 /* FIXME! Remove this arbitrary limit! */
c5aa993b 4926 struct type *types[1024], **rval; /* allow for fns of 1023 parameters */
c906108c
SS
4927 int n = 0;
4928
4929 while (**pp != end)
4930 {
4931 if (**pp != ',')
4932 /* Invalid argument list: no ','. */
c5aa993b 4933 return (struct type **) -1;
c906108c
SS
4934 (*pp)++;
4935 STABS_CONTINUE (pp, objfile);
4936 types[n++] = read_type (pp, objfile);
4937 }
4938 (*pp)++; /* get past `end' (the ':' character) */
4939
4940 if (n == 1)
4941 {
4942 rval = (struct type **) xmalloc (2 * sizeof (struct type *));
4943 }
c5aa993b 4944 else if (TYPE_CODE (types[n - 1]) != TYPE_CODE_VOID)
c906108c
SS
4945 {
4946 rval = (struct type **) xmalloc ((n + 1) * sizeof (struct type *));
4947 memset (rval + n, 0, sizeof (struct type *));
4948 }
4949 else
4950 {
4951 rval = (struct type **) xmalloc (n * sizeof (struct type *));
4952 }
4953 memcpy (rval, types, n * sizeof (struct type *));
4954 return rval;
4955}
4956\f
4957/* Common block handling. */
4958
4959/* List of symbols declared since the last BCOMM. This list is a tail
4960 of local_symbols. When ECOMM is seen, the symbols on the list
4961 are noted so their proper addresses can be filled in later,
4962 using the common block base address gotten from the assembler
4963 stabs. */
4964
4965static struct pending *common_block;
4966static int common_block_i;
4967
4968/* Name of the current common block. We get it from the BCOMM instead of the
4969 ECOMM to match IBM documentation (even though IBM puts the name both places
4970 like everyone else). */
4971static char *common_block_name;
4972
4973/* Process a N_BCOMM symbol. The storage for NAME is not guaranteed
4974 to remain after this function returns. */
4975
4976void
fba45db2 4977common_block_start (char *name, struct objfile *objfile)
c906108c
SS
4978{
4979 if (common_block_name != NULL)
4980 {
c5aa993b
JM
4981 static struct complaint msg =
4982 {
c906108c
SS
4983 "Invalid symbol data: common block within common block",
4984 0, 0};
4985 complain (&msg);
4986 }
4987 common_block = local_symbols;
4988 common_block_i = local_symbols ? local_symbols->nsyms : 0;
4989 common_block_name = obsavestring (name, strlen (name),
c5aa993b 4990 &objfile->symbol_obstack);
c906108c
SS
4991}
4992
4993/* Process a N_ECOMM symbol. */
4994
4995void
fba45db2 4996common_block_end (struct objfile *objfile)
c906108c
SS
4997{
4998 /* Symbols declared since the BCOMM are to have the common block
4999 start address added in when we know it. common_block and
5000 common_block_i point to the first symbol after the BCOMM in
5001 the local_symbols list; copy the list and hang it off the
5002 symbol for the common block name for later fixup. */
5003 int i;
5004 struct symbol *sym;
5005 struct pending *new = 0;
5006 struct pending *next;
5007 int j;
5008
5009 if (common_block_name == NULL)
5010 {
c5aa993b
JM
5011 static struct complaint msg =
5012 {"ECOMM symbol unmatched by BCOMM", 0, 0};
c906108c
SS
5013 complain (&msg);
5014 return;
5015 }
5016
c5aa993b
JM
5017 sym = (struct symbol *)
5018 obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol));
c906108c
SS
5019 memset (sym, 0, sizeof (struct symbol));
5020 /* Note: common_block_name already saved on symbol_obstack */
5021 SYMBOL_NAME (sym) = common_block_name;
5022 SYMBOL_CLASS (sym) = LOC_BLOCK;
5023
5024 /* Now we copy all the symbols which have been defined since the BCOMM. */
5025
5026 /* Copy all the struct pendings before common_block. */
5027 for (next = local_symbols;
5028 next != NULL && next != common_block;
5029 next = next->next)
5030 {
5031 for (j = 0; j < next->nsyms; j++)
5032 add_symbol_to_list (next->symbol[j], &new);
5033 }
5034
5035 /* Copy however much of COMMON_BLOCK we need. If COMMON_BLOCK is
5036 NULL, it means copy all the local symbols (which we already did
5037 above). */
5038
5039 if (common_block != NULL)
5040 for (j = common_block_i; j < common_block->nsyms; j++)
5041 add_symbol_to_list (common_block->symbol[j], &new);
5042
5043 SYMBOL_TYPE (sym) = (struct type *) new;
5044
5045 /* Should we be putting local_symbols back to what it was?
5046 Does it matter? */
5047
5048 i = hashname (SYMBOL_NAME (sym));
5049 SYMBOL_VALUE_CHAIN (sym) = global_sym_chain[i];
5050 global_sym_chain[i] = sym;
5051 common_block_name = NULL;
5052}
5053
5054/* Add a common block's start address to the offset of each symbol
5055 declared to be in it (by being between a BCOMM/ECOMM pair that uses
5056 the common block name). */
5057
5058static void
fba45db2 5059fix_common_block (struct symbol *sym, int valu)
c906108c
SS
5060{
5061 struct pending *next = (struct pending *) SYMBOL_TYPE (sym);
c5aa993b 5062 for (; next; next = next->next)
c906108c
SS
5063 {
5064 register int j;
5065 for (j = next->nsyms - 1; j >= 0; j--)
5066 SYMBOL_VALUE_ADDRESS (next->symbol[j]) += valu;
5067 }
5068}
c5aa993b 5069\f
c906108c
SS
5070
5071
c906108c
SS
5072/* What about types defined as forward references inside of a small lexical
5073 scope? */
5074/* Add a type to the list of undefined types to be checked through
5075 once this file has been read in. */
5076
5077void
fba45db2 5078add_undefined_type (struct type *type)
c906108c
SS
5079{
5080 if (undef_types_length == undef_types_allocated)
5081 {
5082 undef_types_allocated *= 2;
5083 undef_types = (struct type **)
5084 xrealloc ((char *) undef_types,
5085 undef_types_allocated * sizeof (struct type *));
5086 }
5087 undef_types[undef_types_length++] = type;
5088}
5089
5090/* Go through each undefined type, see if it's still undefined, and fix it
5091 up if possible. We have two kinds of undefined types:
5092
5093 TYPE_CODE_ARRAY: Array whose target type wasn't defined yet.
c5aa993b
JM
5094 Fix: update array length using the element bounds
5095 and the target type's length.
c906108c 5096 TYPE_CODE_STRUCT, TYPE_CODE_UNION: Structure whose fields were not
c5aa993b
JM
5097 yet defined at the time a pointer to it was made.
5098 Fix: Do a full lookup on the struct/union tag. */
c906108c 5099void
fba45db2 5100cleanup_undefined_types (void)
c906108c
SS
5101{
5102 struct type **type;
5103
5104 for (type = undef_types; type < undef_types + undef_types_length; type++)
5105 {
5106 switch (TYPE_CODE (*type))
5107 {
5108
c5aa993b
JM
5109 case TYPE_CODE_STRUCT:
5110 case TYPE_CODE_UNION:
5111 case TYPE_CODE_ENUM:
c906108c
SS
5112 {
5113 /* Check if it has been defined since. Need to do this here
5114 as well as in check_typedef to deal with the (legitimate in
5115 C though not C++) case of several types with the same name
5116 in different source files. */
74a9bb82 5117 if (TYPE_STUB (*type))
c906108c
SS
5118 {
5119 struct pending *ppt;
5120 int i;
5121 /* Name of the type, without "struct" or "union" */
5122 char *typename = TYPE_TAG_NAME (*type);
5123
5124 if (typename == NULL)
5125 {
c5aa993b
JM
5126 static struct complaint msg =
5127 {"need a type name", 0, 0};
c906108c
SS
5128 complain (&msg);
5129 break;
5130 }
5131 for (ppt = file_symbols; ppt; ppt = ppt->next)
5132 {
5133 for (i = 0; i < ppt->nsyms; i++)
5134 {
5135 struct symbol *sym = ppt->symbol[i];
c5aa993b 5136
c906108c
SS
5137 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF
5138 && SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE
5139 && (TYPE_CODE (SYMBOL_TYPE (sym)) ==
5140 TYPE_CODE (*type))
5141 && STREQ (SYMBOL_NAME (sym), typename))
13a393b0 5142 replace_type (*type, SYMBOL_TYPE (sym));
c906108c
SS
5143 }
5144 }
5145 }
5146 }
5147 break;
5148
5149 default:
5150 {
c5aa993b
JM
5151 static struct complaint msg =
5152 {"\
c906108c
SS
5153GDB internal error. cleanup_undefined_types with bad type %d.", 0, 0};
5154 complain (&msg, TYPE_CODE (*type));
5155 }
5156 break;
5157 }
5158 }
5159
5160 undef_types_length = 0;
5161}
5162
5163/* Scan through all of the global symbols defined in the object file,
5164 assigning values to the debugging symbols that need to be assigned
5165 to. Get these symbols from the minimal symbol table. */
5166
5167void
fba45db2 5168scan_file_globals (struct objfile *objfile)
c906108c
SS
5169{
5170 int hash;
5171 struct minimal_symbol *msymbol;
5172 struct symbol *sym, *prev, *rsym;
5173 struct objfile *resolve_objfile;
5174
5175 /* SVR4 based linkers copy referenced global symbols from shared
5176 libraries to the main executable.
5177 If we are scanning the symbols for a shared library, try to resolve
5178 them from the minimal symbols of the main executable first. */
5179
5180 if (symfile_objfile && objfile != symfile_objfile)
5181 resolve_objfile = symfile_objfile;
5182 else
5183 resolve_objfile = objfile;
5184
5185 while (1)
5186 {
5187 /* Avoid expensive loop through all minimal symbols if there are
c5aa993b 5188 no unresolved symbols. */
c906108c
SS
5189 for (hash = 0; hash < HASHSIZE; hash++)
5190 {
5191 if (global_sym_chain[hash])
5192 break;
5193 }
5194 if (hash >= HASHSIZE)
5195 return;
5196
c5aa993b 5197 for (msymbol = resolve_objfile->msymbols;
c906108c
SS
5198 msymbol && SYMBOL_NAME (msymbol) != NULL;
5199 msymbol++)
5200 {
5201 QUIT;
5202
5203 /* Skip static symbols. */
5204 switch (MSYMBOL_TYPE (msymbol))
5205 {
5206 case mst_file_text:
5207 case mst_file_data:
5208 case mst_file_bss:
5209 continue;
5210 default:
5211 break;
5212 }
5213
5214 prev = NULL;
5215
5216 /* Get the hash index and check all the symbols
5217 under that hash index. */
5218
5219 hash = hashname (SYMBOL_NAME (msymbol));
5220
5221 for (sym = global_sym_chain[hash]; sym;)
5222 {
5223 if (SYMBOL_NAME (msymbol)[0] == SYMBOL_NAME (sym)[0] &&
c5aa993b 5224 STREQ (SYMBOL_NAME (msymbol) + 1, SYMBOL_NAME (sym) + 1))
c906108c
SS
5225 {
5226
5227 struct alias_list *aliases;
5228
5229 /* Splice this symbol out of the hash chain and
5230 assign the value we have to it. */
5231 if (prev)
5232 {
5233 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
5234 }
5235 else
5236 {
5237 global_sym_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
5238 }
c5aa993b 5239
c906108c
SS
5240 /* Check to see whether we need to fix up a common block. */
5241 /* Note: this code might be executed several times for
5242 the same symbol if there are multiple references. */
5243
5244 /* If symbol has aliases, do minimal symbol fixups for each.
5245 These live aliases/references weren't added to
5246 global_sym_chain hash but may also need to be fixed up. */
c5aa993b 5247 /* FIXME: Maybe should have added aliases to the global chain, resolved symbol name, then treated aliases as normal
c906108c
SS
5248 symbols? Still, we wouldn't want to add_to_list. */
5249 /* Now do the same for each alias of this symbol */
5250 rsym = sym;
5251 aliases = SYMBOL_ALIASES (sym);
5252 while (rsym)
5253 {
5254 if (SYMBOL_CLASS (rsym) == LOC_BLOCK)
5255 {
5256 fix_common_block (rsym,
5257 SYMBOL_VALUE_ADDRESS (msymbol));
5258 }
5259 else
5260 {
5261 SYMBOL_VALUE_ADDRESS (rsym)
5262 = SYMBOL_VALUE_ADDRESS (msymbol);
5263 }
5264 SYMBOL_SECTION (rsym) = SYMBOL_SECTION (msymbol);
5265 if (aliases)
5266 {
5267 rsym = aliases->sym;
5268 aliases = aliases->next;
5269 }
5270 else
5271 rsym = NULL;
5272 }
5273
c5aa993b 5274
c906108c
SS
5275 if (prev)
5276 {
5277 sym = SYMBOL_VALUE_CHAIN (prev);
5278 }
5279 else
5280 {
5281 sym = global_sym_chain[hash];
5282 }
5283 }
5284 else
5285 {
5286 prev = sym;
5287 sym = SYMBOL_VALUE_CHAIN (sym);
5288 }
5289 }
5290 }
5291 if (resolve_objfile == objfile)
5292 break;
5293 resolve_objfile = objfile;
5294 }
5295
5296 /* Change the storage class of any remaining unresolved globals to
5297 LOC_UNRESOLVED and remove them from the chain. */
5298 for (hash = 0; hash < HASHSIZE; hash++)
5299 {
5300 sym = global_sym_chain[hash];
5301 while (sym)
5302 {
5303 prev = sym;
5304 sym = SYMBOL_VALUE_CHAIN (sym);
5305
5306 /* Change the symbol address from the misleading chain value
5307 to address zero. */
5308 SYMBOL_VALUE_ADDRESS (prev) = 0;
5309
5310 /* Complain about unresolved common block symbols. */
5311 if (SYMBOL_CLASS (prev) == LOC_STATIC)
5312 SYMBOL_CLASS (prev) = LOC_UNRESOLVED;
5313 else
5314 complain (&unresolved_sym_chain_complaint,
c5aa993b 5315 objfile->name, SYMBOL_NAME (prev));
c906108c
SS
5316 }
5317 }
5318 memset (global_sym_chain, 0, sizeof (global_sym_chain));
5319}
5320
5321/* Initialize anything that needs initializing when starting to read
5322 a fresh piece of a symbol file, e.g. reading in the stuff corresponding
5323 to a psymtab. */
5324
5325void
fba45db2 5326stabsread_init (void)
c906108c
SS
5327{
5328}
5329
5330/* Initialize anything that needs initializing when a completely new
5331 symbol file is specified (not just adding some symbols from another
5332 file, e.g. a shared library). */
5333
5334void
fba45db2 5335stabsread_new_init (void)
c906108c
SS
5336{
5337 /* Empty the hash table of global syms looking for values. */
5338 memset (global_sym_chain, 0, sizeof (global_sym_chain));
5339}
5340
5341/* Initialize anything that needs initializing at the same time as
5342 start_symtab() is called. */
5343
c5aa993b 5344void
fba45db2 5345start_stabs (void)
c906108c
SS
5346{
5347 global_stabs = NULL; /* AIX COFF */
5348 /* Leave FILENUM of 0 free for builtin types and this file's types. */
5349 n_this_object_header_files = 1;
5350 type_vector_length = 0;
5351 type_vector = (struct type **) 0;
5352
5353 /* FIXME: If common_block_name is not already NULL, we should complain(). */
5354 common_block_name = NULL;
5355
5356 os9k_stabs = 0;
5357}
5358
5359/* Call after end_symtab() */
5360
c5aa993b 5361void
fba45db2 5362end_stabs (void)
c906108c
SS
5363{
5364 if (type_vector)
5365 {
b8c9b27d 5366 xfree (type_vector);
c906108c
SS
5367 }
5368 type_vector = 0;
5369 type_vector_length = 0;
5370 previous_stab_code = 0;
5371}
5372
5373void
fba45db2 5374finish_global_stabs (struct objfile *objfile)
c906108c
SS
5375{
5376 if (global_stabs)
5377 {
5378 patch_block_stabs (global_symbols, global_stabs, objfile);
b8c9b27d 5379 xfree (global_stabs);
c906108c
SS
5380 global_stabs = NULL;
5381 }
5382}
5383
5384/* Initializer for this module */
5385
5386void
fba45db2 5387_initialize_stabsread (void)
c906108c
SS
5388{
5389 undef_types_allocated = 20;
5390 undef_types_length = 0;
5391 undef_types = (struct type **)
5392 xmalloc (undef_types_allocated * sizeof (struct type *));
5393}