Fixes related to handling of C++ methods (handle destructors
[deliverable/binutils-gdb.git] / gdb / symtab.c
CommitLineData
bd5635a1 1/* Symbol table lookup for the GNU debugger, GDB.
997a978c 2 Copyright 1986, 1987, 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
997a978c 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
997a978c
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
997a978c 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
997a978c
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1
RP
19
20#include <stdio.h>
21#include "defs.h"
22#include "symtab.h"
23#include "param.h"
24#include "gdbcore.h"
25#include "frame.h"
26#include "target.h"
27#include "value.h"
28#include "symfile.h"
29#include "gdbcmd.h"
5594d534 30#include "regex.h"
997a978c 31#include "language.h"
bd5635a1
RP
32
33#include <obstack.h>
34#include <assert.h>
35
36#include <sys/types.h>
37#include <fcntl.h>
38#include <string.h>
39#include <sys/stat.h>
40
bd5635a1
RP
41extern char *getenv ();
42
43extern char *cplus_demangle ();
bcccec8c 44extern char *cplus_mangle_opname ();
bd5635a1
RP
45extern struct value *value_of_this ();
46extern void break_command ();
47extern void select_source_symtab ();
48
49/* Functions this file defines */
50static int find_line_common ();
51struct partial_symtab *lookup_partial_symtab ();
52static struct partial_symbol *lookup_partial_symbol ();
b039ac3a
JK
53static struct partial_symbol *lookup_demangled_partial_symbol ();
54static struct symbol *lookup_demangled_block_symbol ();
bd5635a1 55
997a978c 56/* The single non-language-specific builtin type */
bd5635a1
RP
57struct type *builtin_type_error;
58
59/* Block in which the most recently searched-for symbol was found.
60 Might be better to make this a parameter to lookup_symbol and
61 value_of_this. */
62struct block *block_found;
63
64char no_symtab_msg[] = "No symbol table is loaded. Use the \"file\" command.";
65
66/* Check for a symtab of a specific name; first in symtabs, then in
67 psymtabs. *If* there is no '/' in the name, a match after a '/'
68 in the symtab filename will also work. */
69
70static struct symtab *
71lookup_symtab_1 (name)
72 char *name;
73{
74 register struct symtab *s;
75 register struct partial_symtab *ps;
76 register char *slash = strchr (name, '/');
77 register int len = strlen (name);
78
79 for (s = symtab_list; s; s = s->next)
80 if (!strcmp (name, s->filename))
81 return s;
82
83 for (ps = partial_symtab_list; ps; ps = ps->next)
84 if (!strcmp (name, ps->filename))
85 {
86 if (ps->readin)
eaa1ef1d 87 error ("Internal: readin pst for `%s' found when no symtab found.", name);
bd5635a1
RP
88 return PSYMTAB_TO_SYMTAB (ps);
89 }
90
91 if (!slash)
92 {
93 for (s = symtab_list; s; s = s->next)
94 {
95 int l = strlen (s->filename);
96
97 if (s->filename[l - len -1] == '/'
98 && !strcmp (s->filename + l - len, name))
99 return s;
100 }
101
102 for (ps = partial_symtab_list; ps; ps = ps->next)
103 {
104 int l = strlen (ps->filename);
105
106 if (ps->filename[l - len - 1] == '/'
107 && !strcmp (ps->filename + l - len, name))
108 {
109 if (ps->readin)
eaa1ef1d 110 error ("Internal: readin pst for `%s' found when no symtab found.", name);
bd5635a1
RP
111 return PSYMTAB_TO_SYMTAB (ps);
112 }
113 }
114 }
115 return 0;
116}
117
118/* Lookup the symbol table of a source file named NAME. Try a couple
119 of variations if the first lookup doesn't work. */
120
121struct symtab *
122lookup_symtab (name)
123 char *name;
124{
125 register struct symtab *s;
126 register char *copy;
127
128 s = lookup_symtab_1 (name);
129 if (s) return s;
130
131 /* If name not found as specified, see if adding ".c" helps. */
132
133 copy = (char *) alloca (strlen (name) + 3);
134 strcpy (copy, name);
135 strcat (copy, ".c");
136 s = lookup_symtab_1 (copy);
137 if (s) return s;
138
139 /* We didn't find anything; die. */
140 return 0;
141}
142
143/* Lookup the partial symbol table of a source file named NAME. This
144 only returns true on an exact match (ie. this semantics are
145 different from lookup_symtab. */
146
147struct partial_symtab *
148lookup_partial_symtab (name)
149char *name;
150{
151 register struct partial_symtab *s;
152
153 for (s = partial_symtab_list; s; s = s->next)
154 if (!strcmp (name, s->filename))
155 return s;
156
157 return 0;
158}
159\f
160/* Return a typename for a struct/union/enum type
161 without the tag qualifier. If the type has a NULL name,
162 NULL is returned. */
163char *
164type_name_no_tag (type)
165 register struct type *type;
166{
167 register char *name = TYPE_NAME (type);
168 char *strchr ();
169 if (name == 0)
170 return 0;
171
172#if 0
173 switch (TYPE_CODE (type))
174 {
175 case TYPE_CODE_STRUCT:
176 return name + 7;
177 case TYPE_CODE_UNION:
178 return name + 6;
179 case TYPE_CODE_ENUM:
180 return name + 5;
181 }
182#endif
183
184 name = strchr (name, ' ');
185 if (name)
186 return name + 1;
187
188 return TYPE_NAME (type);
189}
190
191/* Added by Bryan Boreham, Kewill, Sun Sep 17 18:07:17 1989.
192
193 If this is a stubbed struct (i.e. declared as struct foo *), see if
194 we can find a full definition in some other file. If so, copy this
195 definition, so we can use it in future. If not, set a flag so we
c2536607
JG
196 don't waste too much time in future. (FIXME, this doesn't seem
197 to be happening...)
bd5635a1
RP
198
199 This used to be coded as a macro, but I don't think it is called
200 often enough to merit such treatment.
201*/
202
203struct complaint stub_noname_complaint =
204 {"stub type has NULL name", 0, 0};
205
206void
207check_stub_type(type)
208 struct type *type;
209{
210 if (TYPE_FLAGS(type) & TYPE_FLAG_STUB)
211 {
212 char* name= type_name_no_tag (type);
213 struct symbol *sym;
214 if (name == 0)
215 {
e1ce8aa5 216 complain (&stub_noname_complaint, 0);
bd5635a1
RP
217 return;
218 }
5594d534
JG
219 sym = lookup_symbol (name, 0, STRUCT_NAMESPACE, 0,
220 (struct symtab **)NULL);
221 if (sym)
bd5635a1
RP
222 bcopy (SYMBOL_TYPE(sym), type, sizeof (struct type));
223 }
224}
225
226/* Demangle a GDB method stub type. */
227char *
bcccec8c 228gdb_mangle_name (type, i, j)
bd5635a1 229 struct type *type;
bcccec8c 230 int i, j;
bd5635a1 231{
bcccec8c
PB
232 int mangled_name_len;
233 char *mangled_name;
234 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
235 struct fn_field *method = &f[j];
236 char *field_name = TYPE_FN_FIELDLIST_NAME (type, i);
bd5635a1 237
bcccec8c
PB
238 /* Need a new type prefix. */
239 char *strchr ();
240 char *const_prefix = method->is_const ? "C" : "";
241 char *volatile_prefix = method->is_volatile ? "V" : "";
242 char *newname = type_name_no_tag (type);
243 char buf[20];
244 int len = strlen (newname);
245
246 sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
247 mangled_name_len = (strlen (field_name)
248 + strlen (buf) + len
249 + strlen (TYPE_FN_FIELD_PHYSNAME (f, j))
250 + 1);
251
252 if (OPNAME_PREFIX_P (field_name))
253 {
254 char *opname = cplus_mangle_opname (field_name + 3);
255 if (opname == NULL)
256 error ("No mangling for \"%s\"", field_name);
257 mangled_name_len += strlen (opname);
258 mangled_name = (char *)xmalloc (mangled_name_len);
259
260 strncpy (mangled_name, field_name, 3);
261 mangled_name[3] = '\0';
262 strcat (mangled_name, opname);
263 }
264 else
bd5635a1 265 {
bcccec8c
PB
266 mangled_name = (char *)xmalloc (mangled_name_len);
267 strcpy (mangled_name, TYPE_FN_FIELDLIST_NAME (type, i));
bd5635a1 268 }
bcccec8c
PB
269 strcat (mangled_name, buf);
270 strcat (mangled_name, newname);
271 strcat (mangled_name, TYPE_FN_FIELD_PHYSNAME (f, j));
272
273 return mangled_name;
bd5635a1
RP
274}
275
276/* Lookup a primitive type named NAME.
277 Return zero if NAME is not a primitive type.*/
278
279struct type *
280lookup_primitive_typename (name)
281 char *name;
282{
c2536607 283 struct type ** const *p;
997a978c
JG
284
285 for (p = current_language->la_builtin_type_vector; *p; p++)
286 if(!strcmp((**p)->name, name))
287 return **p;
288 return 0;
bd5635a1
RP
289}
290
291/* Lookup a typedef or primitive type named NAME,
292 visible in lexical block BLOCK.
293 If NOERR is nonzero, return zero if NAME is not suitably defined. */
294
295struct type *
296lookup_typename (name, block, noerr)
297 char *name;
298 struct block *block;
299 int noerr;
300{
301 register struct symbol *sym =
302 lookup_symbol (name, block, VAR_NAMESPACE, 0, (struct symtab **)NULL);
303 if (sym == 0 || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
304 {
305 struct type *tmp;
306 tmp = lookup_primitive_typename (name);
997a978c
JG
307 if(tmp)
308 return tmp;
309 else if (!tmp && noerr)
bd5635a1 310 return 0;
997a978c
JG
311 else
312 error ("No type named %s.", name);
bd5635a1
RP
313 }
314 return SYMBOL_TYPE (sym);
315}
316
317struct type *
318lookup_unsigned_typename (name)
319 char *name;
320{
997a978c
JG
321 char *uns = alloca (strlen(name) + 10);
322
323 strcpy (uns, "unsigned ");
324 strcpy (uns+9, name);
325 return lookup_typename (uns, (struct block *)0, 0);
bd5635a1
RP
326}
327
328/* Lookup a structure type named "struct NAME",
329 visible in lexical block BLOCK. */
330
331struct type *
332lookup_struct (name, block)
333 char *name;
334 struct block *block;
335{
336 register struct symbol *sym
337 = lookup_symbol (name, block, STRUCT_NAMESPACE, 0, (struct symtab **)NULL);
338
339 if (sym == 0)
340 error ("No struct type named %s.", name);
341 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
342 error ("This context has class, union or enum %s, not a struct.", name);
343 return SYMBOL_TYPE (sym);
344}
345
346/* Lookup a union type named "union NAME",
347 visible in lexical block BLOCK. */
348
349struct type *
350lookup_union (name, block)
351 char *name;
352 struct block *block;
353{
354 register struct symbol *sym
355 = lookup_symbol (name, block, STRUCT_NAMESPACE, 0, (struct symtab **)NULL);
356
357 if (sym == 0)
358 error ("No union type named %s.", name);
359 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
360 error ("This context has class, struct or enum %s, not a union.", name);
361 return SYMBOL_TYPE (sym);
362}
363
364/* Lookup an enum type named "enum NAME",
365 visible in lexical block BLOCK. */
366
367struct type *
368lookup_enum (name, block)
369 char *name;
370 struct block *block;
371{
372 register struct symbol *sym
373 = lookup_symbol (name, block, STRUCT_NAMESPACE, 0, (struct symtab **)NULL);
374 if (sym == 0)
375 error ("No enum type named %s.", name);
376 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
377 error ("This context has class, struct or union %s, not an enum.", name);
378 return SYMBOL_TYPE (sym);
379}
380
381/* Given a type TYPE, lookup the type of the component of type named
d96b54ea
JK
382 NAME.
383 If NOERR is nonzero, return zero if NAME is not suitably defined. */
bd5635a1
RP
384
385struct type *
d96b54ea 386lookup_struct_elt_type (type, name, noerr)
bd5635a1
RP
387 struct type *type;
388 char *name;
d96b54ea 389 int noerr;
bd5635a1
RP
390{
391 int i;
392
393 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
394 && TYPE_CODE (type) != TYPE_CODE_UNION)
395 {
396 target_terminal_ours ();
397 fflush (stdout);
398 fprintf (stderr, "Type ");
399 type_print (type, "", stderr, -1);
400 error (" is not a structure or union type.");
401 }
402
d96b54ea
JK
403 check_stub_type (type);
404
bd5635a1 405 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
d96b54ea
JK
406 {
407 char *t_field_name = TYPE_FIELD_NAME (type, i);
bd5635a1 408
d96b54ea
JK
409 if (t_field_name && !strcmp (t_field_name, name))
410 return TYPE_FIELD_TYPE (type, i);
411 }
412 /* OK, it's not in this class. Recursively check the baseclasses. */
413 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
414 {
415 struct type *t = lookup_struct_elt_type (TYPE_BASECLASS (type, i),
416 name, 0);
417 if (t != NULL)
418 return t;
419 }
420
421 if (noerr)
422 return NULL;
423
bd5635a1
RP
424 target_terminal_ours ();
425 fflush (stdout);
426 fprintf (stderr, "Type ");
427 type_print (type, "", stderr, -1);
428 fprintf (stderr, " has no component named ");
429 fputs_filtered (name, stderr);
430 error (".");
431 return (struct type *)-1; /* For lint */
432}
433
434/* Given a type TYPE, return a type of pointers to that type.
435 May need to construct such a type if this is the first use.
436
437 C++: use TYPE_MAIN_VARIANT and TYPE_CHAIN to keep pointer
438 to member types under control. */
439
440struct type *
441lookup_pointer_type (type)
442 struct type *type;
443{
444 register struct type *ptype = TYPE_POINTER_TYPE (type);
445 if (ptype) return TYPE_MAIN_VARIANT (ptype);
446
447 /* This is the first time anyone wanted a pointer to a TYPE. */
448 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
449 ptype = (struct type *) xmalloc (sizeof (struct type));
450 else
451 ptype = (struct type *) obstack_alloc (symbol_obstack,
452 sizeof (struct type));
453
454 bzero (ptype, sizeof (struct type));
455 TYPE_MAIN_VARIANT (ptype) = ptype;
456 TYPE_TARGET_TYPE (ptype) = type;
457 TYPE_POINTER_TYPE (type) = ptype;
458 /* New type is permanent if type pointed to is permanent. */
459 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
460 TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
461 /* We assume the machine has only one representation for pointers! */
dc9894c8
JG
462 /* FIXME: This confuses host<->target data representations, and is a
463 poor assumption besides. */
bd5635a1
RP
464 TYPE_LENGTH (ptype) = sizeof (char *);
465 TYPE_CODE (ptype) = TYPE_CODE_PTR;
466 return ptype;
467}
468
469struct type *
470lookup_reference_type (type)
471 struct type *type;
472{
473 register struct type *rtype = TYPE_REFERENCE_TYPE (type);
474 if (rtype) return TYPE_MAIN_VARIANT (rtype);
475
476 /* This is the first time anyone wanted a pointer to a TYPE. */
477 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
478 rtype = (struct type *) xmalloc (sizeof (struct type));
479 else
480 rtype = (struct type *) obstack_alloc (symbol_obstack,
481 sizeof (struct type));
482
483 bzero (rtype, sizeof (struct type));
484 TYPE_MAIN_VARIANT (rtype) = rtype;
485 TYPE_TARGET_TYPE (rtype) = type;
486 TYPE_REFERENCE_TYPE (type) = rtype;
487 /* New type is permanent if type pointed to is permanent. */
488 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
489 TYPE_FLAGS (rtype) |= TYPE_FLAG_PERM;
490 /* We assume the machine has only one representation for pointers! */
491 TYPE_LENGTH (rtype) = sizeof (char *);
492 TYPE_CODE (rtype) = TYPE_CODE_REF;
493 return rtype;
494}
495
496
497/* Implement direct support for MEMBER_TYPE in GNU C++.
498 May need to construct such a type if this is the first use.
499 The TYPE is the type of the member. The DOMAIN is the type
500 of the aggregate that the member belongs to. */
501
502struct type *
503lookup_member_type (type, domain)
504 struct type *type, *domain;
505{
506 register struct type *mtype = TYPE_MAIN_VARIANT (type);
507 struct type *main_type;
508
509 main_type = mtype;
510 while (mtype)
511 {
512 if (TYPE_DOMAIN_TYPE (mtype) == domain)
513 return mtype;
514 mtype = TYPE_NEXT_VARIANT (mtype);
515 }
516
517 /* This is the first time anyone wanted this member type. */
518 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
519 mtype = (struct type *) xmalloc (sizeof (struct type));
520 else
521 mtype = (struct type *) obstack_alloc (symbol_obstack,
522 sizeof (struct type));
523
524 bzero (mtype, sizeof (struct type));
525 if (main_type == 0)
526 main_type = mtype;
527 else
528 {
529 TYPE_NEXT_VARIANT (mtype) = TYPE_NEXT_VARIANT (main_type);
530 TYPE_NEXT_VARIANT (main_type) = mtype;
531 }
532 TYPE_MAIN_VARIANT (mtype) = main_type;
533 TYPE_TARGET_TYPE (mtype) = type;
534 TYPE_DOMAIN_TYPE (mtype) = domain;
535 /* New type is permanent if type pointed to is permanent. */
536 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
537 TYPE_FLAGS (mtype) |= TYPE_FLAG_PERM;
538
539 /* In practice, this is never used. */
540 TYPE_LENGTH (mtype) = 1;
541 TYPE_CODE (mtype) = TYPE_CODE_MEMBER;
542
543#if 0
544 /* Now splice in the new member pointer type. */
545 if (main_type)
546 {
547 /* This type was not "smashed". */
548 TYPE_CHAIN (mtype) = TYPE_CHAIN (main_type);
549 TYPE_CHAIN (main_type) = mtype;
550 }
551#endif
552
553 return mtype;
554}
555
d96b54ea
JK
556/* Allocate a stub method whose return type is
557 TYPE. We will fill in arguments later. This always
558 returns a fresh type. If we unify this type with
559 an existing type later, the storage allocated
560 here can be freed. */
561struct type *
562allocate_stub_method (type)
563 struct type *type;
564{
565 struct type *mtype = (struct type *)xmalloc (sizeof (struct type));
566 bzero (mtype, sizeof (struct type));
567 TYPE_MAIN_VARIANT (mtype) = mtype;
568 TYPE_TARGET_TYPE (mtype) = type;
569 TYPE_FLAGS (mtype) = TYPE_FLAG_STUB;
570 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
571 TYPE_LENGTH (mtype) = 1;
572 return mtype;
573}
574
dc9894c8
JG
575/* Lookup a method type belonging to class DOMAIN, returning type TYPE,
576 and taking a list of arguments ARGS.
d96b54ea
JK
577 If one is not found, allocate a new one. */
578
bd5635a1 579struct type *
dc9894c8
JG
580lookup_method_type (domain, type, args)
581 struct type *domain, *type, **args;
bd5635a1
RP
582{
583 register struct type *mtype = TYPE_MAIN_VARIANT (type);
584 struct type *main_type;
585
586 main_type = mtype;
587 while (mtype)
588 {
589 if (TYPE_DOMAIN_TYPE (mtype) == domain)
590 {
591 struct type **t1 = args;
592 struct type **t2 = TYPE_ARG_TYPES (mtype);
593 if (t2)
594 {
595 int i;
596 for (i = 0; t1[i] != 0 && t1[i]->code != TYPE_CODE_VOID; i++)
597 if (t1[i] != t2[i])
598 break;
599 if (t1[i] == t2[i])
600 return mtype;
601 }
602 }
603 mtype = TYPE_NEXT_VARIANT (mtype);
604 }
605
606 /* This is the first time anyone wanted this member type. */
607 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
608 mtype = (struct type *) xmalloc (sizeof (struct type));
609 else
610 mtype = (struct type *) obstack_alloc (symbol_obstack,
611 sizeof (struct type));
612
613 bzero (mtype, sizeof (struct type));
614 if (main_type == 0)
615 main_type = mtype;
616 else
617 {
618 TYPE_NEXT_VARIANT (mtype) = TYPE_NEXT_VARIANT (main_type);
619 TYPE_NEXT_VARIANT (main_type) = mtype;
620 }
621 TYPE_MAIN_VARIANT (mtype) = main_type;
622 TYPE_TARGET_TYPE (mtype) = type;
623 TYPE_DOMAIN_TYPE (mtype) = domain;
624 TYPE_ARG_TYPES (mtype) = args;
625 /* New type is permanent if type pointed to is permanent. */
626 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
627 TYPE_FLAGS (mtype) |= TYPE_FLAG_PERM;
628
629 /* In practice, this is never used. */
630 TYPE_LENGTH (mtype) = 1;
631 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
632
633#if 0
634 /* Now splice in the new member pointer type. */
635 if (main_type)
636 {
637 /* This type was not "smashed". */
638 TYPE_CHAIN (mtype) = TYPE_CHAIN (main_type);
639 TYPE_CHAIN (main_type) = mtype;
640 }
641#endif
642
643 return mtype;
644}
645
646#if 0
647/* Given a type TYPE, return a type which has offset OFFSET,
648 via_virtual VIA_VIRTUAL, and via_public VIA_PUBLIC.
649 May need to construct such a type if none exists. */
650struct type *
651lookup_basetype_type (type, offset, via_virtual, via_public)
652 struct type *type;
653 int offset;
654 int via_virtual, via_public;
655{
656 register struct type *btype = TYPE_MAIN_VARIANT (type);
657 struct type *main_type;
658
659 if (offset != 0)
660 {
661 printf ("Internal error: type offset non-zero in lookup_basetype_type");
662 offset = 0;
663 }
664
665 main_type = btype;
666 while (btype)
667 {
668 if (/* TYPE_OFFSET (btype) == offset
669 && */ TYPE_VIA_PUBLIC (btype) == via_public
670 && TYPE_VIA_VIRTUAL (btype) == via_virtual)
671 return btype;
672 btype = TYPE_NEXT_VARIANT (btype);
673 }
674
675 /* This is the first time anyone wanted this member type. */
676 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
677 btype = (struct type *) xmalloc (sizeof (struct type));
678 else
679 btype = (struct type *) obstack_alloc (symbol_obstack,
680 sizeof (struct type));
681
682 if (main_type == 0)
683 {
684 main_type = btype;
685 bzero (btype, sizeof (struct type));
686 TYPE_MAIN_VARIANT (btype) = main_type;
687 }
688 else
689 {
690 bcopy (main_type, btype, sizeof (struct type));
691 TYPE_NEXT_VARIANT (main_type) = btype;
692 }
693/* TYPE_OFFSET (btype) = offset; */
694 if (via_public)
695 TYPE_FLAGS (btype) |= TYPE_FLAG_VIA_PUBLIC;
696 if (via_virtual)
697 TYPE_FLAGS (btype) |= TYPE_FLAG_VIA_VIRTUAL;
698 /* New type is permanent if type pointed to is permanent. */
699 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
700 TYPE_FLAGS (btype) |= TYPE_FLAG_PERM;
701
702 /* In practice, this is never used. */
703 TYPE_LENGTH (btype) = 1;
704 TYPE_CODE (btype) = TYPE_CODE_STRUCT;
705
706 return btype;
707}
708#endif
709
710/* Given a type TYPE, return a type of functions that return that type.
711 May need to construct such a type if this is the first use. */
712
713struct type *
714lookup_function_type (type)
715 struct type *type;
716{
717 register struct type *ptype = TYPE_FUNCTION_TYPE (type);
718 if (ptype) return ptype;
719
720 /* This is the first time anyone wanted a function returning a TYPE. */
721 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
722 ptype = (struct type *) xmalloc (sizeof (struct type));
723 else
724 ptype = (struct type *) obstack_alloc (symbol_obstack,
725 sizeof (struct type));
726
727 bzero (ptype, sizeof (struct type));
728 TYPE_TARGET_TYPE (ptype) = type;
729 TYPE_FUNCTION_TYPE (type) = ptype;
730 /* New type is permanent if type returned is permanent. */
731 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
732 TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
733 TYPE_LENGTH (ptype) = 1;
734 TYPE_CODE (ptype) = TYPE_CODE_FUNC;
735 TYPE_NFIELDS (ptype) = 0;
736 return ptype;
737}
738\f
739/* Create an array type. Elements will be of type TYPE, and there will
740 be NUM of them.
741
742 Eventually this should be extended to take two more arguments which
743 specify the bounds of the array and the type of the index.
744 It should also be changed to be a "lookup" function, with the
745 appropriate data structures added to the type field.
746 Then read array type should call here. */
747
748struct type *
749create_array_type (element_type, number)
750 struct type *element_type;
751 int number;
752{
753 struct type *result_type = (struct type *)
754 obstack_alloc (symbol_obstack, sizeof (struct type));
997a978c 755 struct type *range_type;
bd5635a1
RP
756
757 bzero (result_type, sizeof (struct type));
758
759 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
760 TYPE_TARGET_TYPE (result_type) = element_type;
761 TYPE_LENGTH (result_type) = number * TYPE_LENGTH (element_type);
762 TYPE_NFIELDS (result_type) = 1;
763 TYPE_FIELDS (result_type) =
764 (struct field *) obstack_alloc (symbol_obstack, sizeof (struct field));
997a978c
JG
765
766 {
767 /* Create range type. */
768 range_type = (struct type *) obstack_alloc (symbol_obstack,
769 sizeof (struct type));
770 TYPE_CODE (range_type) = TYPE_CODE_RANGE;
771 TYPE_TARGET_TYPE (range_type) = builtin_type_int; /* FIXME */
772
773 /* This should never be needed. */
774 TYPE_LENGTH (range_type) = sizeof (int);
775
776 TYPE_NFIELDS (range_type) = 2;
777 TYPE_FIELDS (range_type) =
778 (struct field *) obstack_alloc (symbol_obstack,
779 2 * sizeof (struct field));
780 TYPE_FIELD_BITPOS (range_type, 0) = 0; /* FIXME */
781 TYPE_FIELD_BITPOS (range_type, 1) = number-1; /* FIXME */
782 TYPE_FIELD_TYPE (range_type, 0) = builtin_type_int; /* FIXME */
783 TYPE_FIELD_TYPE (range_type, 1) = builtin_type_int; /* FIXME */
784 }
785 TYPE_FIELD_TYPE(result_type,0)=range_type;
bd5635a1
RP
786 TYPE_VPTR_FIELDNO (result_type) = -1;
787
788 return result_type;
789}
790
791\f
792/* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE. */
793
794void
795smash_to_member_type (type, domain, to_type)
796 struct type *type, *domain, *to_type;
797{
798 bzero (type, sizeof (struct type));
799 TYPE_TARGET_TYPE (type) = to_type;
800 TYPE_DOMAIN_TYPE (type) = domain;
801
802 /* In practice, this is never needed. */
803 TYPE_LENGTH (type) = 1;
804 TYPE_CODE (type) = TYPE_CODE_MEMBER;
805
806 TYPE_MAIN_VARIANT (type) = lookup_member_type (domain, to_type);
807}
808
809/* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE. */
810
811void
812smash_to_method_type (type, domain, to_type, args)
813 struct type *type, *domain, *to_type, **args;
814{
815 bzero (type, sizeof (struct type));
816 TYPE_TARGET_TYPE (type) = to_type;
817 TYPE_DOMAIN_TYPE (type) = domain;
818 TYPE_ARG_TYPES (type) = args;
819
820 /* In practice, this is never needed. */
821 TYPE_LENGTH (type) = 1;
822 TYPE_CODE (type) = TYPE_CODE_METHOD;
823
824 TYPE_MAIN_VARIANT (type) = lookup_method_type (domain, to_type, args);
825}
826\f
827/* Find which partial symtab on the partial_symtab_list contains
828 PC. Return 0 if none. */
829
830struct partial_symtab *
831find_pc_psymtab (pc)
832 register CORE_ADDR pc;
833{
834 register struct partial_symtab *ps;
835
836 for (ps = partial_symtab_list; ps; ps = ps->next)
837 if (pc >= ps->textlow && pc < ps->texthigh)
838 return ps;
839
840 return 0;
841}
842
843/* Find which partial symbol within a psymtab contains PC. Return 0
844 if none. Check all psymtabs if PSYMTAB is 0. */
845struct partial_symbol *
846find_pc_psymbol (psymtab, pc)
847 struct partial_symtab *psymtab;
848 CORE_ADDR pc;
849{
850 struct partial_symbol *best, *p;
851 CORE_ADDR best_pc;
852
853 if (!psymtab)
854 psymtab = find_pc_psymtab (pc);
855 if (!psymtab)
856 return 0;
857
858 best_pc = psymtab->textlow - 1;
859
860 for (p = static_psymbols.list + psymtab->statics_offset;
861 (p - (static_psymbols.list + psymtab->statics_offset)
862 < psymtab->n_static_syms);
863 p++)
864 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
865 && SYMBOL_CLASS (p) == LOC_BLOCK
866 && pc >= SYMBOL_VALUE_ADDRESS (p)
867 && SYMBOL_VALUE_ADDRESS (p) > best_pc)
868 {
869 best_pc = SYMBOL_VALUE_ADDRESS (p);
870 best = p;
871 }
872 if (best_pc == psymtab->textlow - 1)
873 return 0;
874 return best;
875}
876
877\f
878/* Find the definition for a specified symbol name NAME
879 in namespace NAMESPACE, visible from lexical block BLOCK.
880 Returns the struct symbol pointer, or zero if no symbol is found.
881 If SYMTAB is non-NULL, store the symbol table in which the
882 symbol was found there, or NULL if not found.
883 C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
884 NAME is a field of the current implied argument `this'. If so set
885 *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
886 BLOCK_FOUND is set to the block in which NAME is found (in the case of
887 a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
888
889struct symbol *
890lookup_symbol (name, block, namespace, is_a_field_of_this, symtab)
891 char *name;
892 register struct block *block;
893 enum namespace namespace;
894 int *is_a_field_of_this;
895 struct symtab **symtab;
896{
897 register struct symbol *sym;
898 register struct symtab *s;
899 register struct partial_symtab *ps;
900 struct blockvector *bv;
901
902 /* Search specified block and its superiors. */
903
904 while (block != 0)
905 {
906 sym = lookup_block_symbol (block, name, namespace);
907 if (sym)
908 {
909 block_found = block;
910 if (symtab != NULL)
911 {
912 /* Search the list of symtabs for one which contains the
913 address of the start of this block. */
914 struct block *b;
915 for (s = symtab_list; s; s = s->next)
916 {
917 bv = BLOCKVECTOR (s);
3ba6a043 918 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
bd5635a1
RP
919 if (BLOCK_START (b) <= BLOCK_START (block)
920 && BLOCK_END (b) > BLOCK_START (block))
921 break;
922 }
923 *symtab = s;
924 }
925
926 return sym;
927 }
928 block = BLOCK_SUPERBLOCK (block);
929 }
930
b039ac3a
JK
931 /* But that doesn't do any demangling for the STATIC_BLOCK.
932 I'm not sure whether demangling is needed in the case of
933 nested function in inner blocks; if so this needs to be changed.
934
935 Don't need to mess with the psymtabs; if we have a block,
936 that file is read in. If we don't, then we deal later with
937 all the psymtab stuff that needs checking. */
938 if (namespace == VAR_NAMESPACE && block != NULL)
939 {
940 struct block *b;
941 /* Find the right symtab. */
942 for (s = symtab_list; s; s = s->next)
943 {
944 bv = BLOCKVECTOR (s);
945 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
946 if (BLOCK_START (b) <= BLOCK_START (block)
947 && BLOCK_END (b) > BLOCK_START (block))
948 {
949 sym = lookup_demangled_block_symbol (b, name);
950 if (sym)
951 {
952 block_found = b;
953 if (symtab != NULL)
954 *symtab = s;
955 return sym;
956 }
957 }
958 }
959 }
960
961
bd5635a1
RP
962 /* C++: If requested to do so by the caller,
963 check to see if NAME is a field of `this'. */
964 if (is_a_field_of_this)
965 {
966 struct value *v = value_of_this (0);
967
968 *is_a_field_of_this = 0;
969 if (v && check_field (v, name))
970 {
971 *is_a_field_of_this = 1;
972 if (symtab != NULL)
973 *symtab = NULL;
974 return 0;
975 }
976 }
977
978 /* Now search all global blocks. Do the symtab's first, then
979 check the psymtab's */
980
981 for (s = symtab_list; s; s = s->next)
982 {
983 bv = BLOCKVECTOR (s);
3ba6a043 984 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
bd5635a1
RP
985 sym = lookup_block_symbol (block, name, namespace);
986 if (sym)
987 {
988 block_found = block;
989 if (symtab != NULL)
990 *symtab = s;
991 return sym;
992 }
993 }
994
995 /* Check for the possibility of the symbol being a global function
996 that is stored on the misc function vector. Eventually, all
997 global symbols might be resolved in this way. */
998
999 if (namespace == VAR_NAMESPACE)
1000 {
1001 int ind = lookup_misc_func (name);
1002
1003 /* Look for a mangled C++ name for NAME. */
1004 if (ind == -1)
1005 {
1006 int name_len = strlen (name);
1007
1008 for (ind = misc_function_count; --ind >= 0; )
1009 /* Assume orginal name is prefix of mangled name. */
1010 if (!strncmp (misc_function_vector[ind].name, name, name_len))
1011 {
1012 char *demangled =
1013 cplus_demangle(misc_function_vector[ind].name, -1);
1014 if (demangled != NULL)
1015 {
1016 int cond = strcmp (demangled, name);
1017 free (demangled);
1018 if (!cond)
1019 break;
1020 }
1021 }
1022 /* Loop terminates on no match with ind == -1. */
1023 }
1024
1025 if (ind != -1)
1026 {
1027 s = find_pc_symtab (misc_function_vector[ind].address);
997a978c
JG
1028 /* If S is zero, there are no debug symbols for this file.
1029 Skip this stuff and check for matching static symbols below. */
bd5635a1
RP
1030 if (s)
1031 {
1032 bv = BLOCKVECTOR (s);
3ba6a043 1033 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
bd5635a1
RP
1034 sym = lookup_block_symbol (block, misc_function_vector[ind].name,
1035 namespace);
1036 /* sym == 0 if symbol was found in the misc_function_vector
1037 but not in the symtab.
1038 Return 0 to use the misc_function definition of "foo_".
1039
1040 This happens for Fortran "foo_" symbols,
1041 which are "foo" in the symtab.
1042
1043 This can also happen if "asm" is used to make a
1044 regular symbol but not a debugging symbol, e.g.
1045 asm(".globl _main");
1046 asm("_main:");
1047 */
1048
1049 if (symtab != NULL)
1050 *symtab = s;
1051 return sym;
1052 }
1053 }
1054 }
1055
1056 for (ps = partial_symtab_list; ps; ps = ps->next)
1057 if (!ps->readin && lookup_partial_symbol (ps, name, 1, namespace))
1058 {
1059 s = PSYMTAB_TO_SYMTAB(ps);
1060 bv = BLOCKVECTOR (s);
3ba6a043 1061 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
bd5635a1
RP
1062 sym = lookup_block_symbol (block, name, namespace);
1063 if (!sym)
eaa1ef1d 1064 error ("Internal: global symbol `%s' found in psymtab but not in symtab", name);
bd5635a1
RP
1065 if (symtab != NULL)
1066 *symtab = s;
1067 return sym;
1068 }
1069
1070 /* Now search all per-file blocks.
1071 Not strictly correct, but more useful than an error.
1072 Do the symtabs first, then check the psymtabs */
1073
1074 for (s = symtab_list; s; s = s->next)
1075 {
1076 bv = BLOCKVECTOR (s);
3ba6a043 1077 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
bd5635a1
RP
1078 sym = lookup_block_symbol (block, name, namespace);
1079 if (sym)
1080 {
1081 block_found = block;
1082 if (symtab != NULL)
1083 *symtab = s;
1084 return sym;
1085 }
1086 }
1087
1088 for (ps = partial_symtab_list; ps; ps = ps->next)
1089 if (!ps->readin && lookup_partial_symbol (ps, name, 0, namespace))
1090 {
1091 s = PSYMTAB_TO_SYMTAB(ps);
1092 bv = BLOCKVECTOR (s);
3ba6a043 1093 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
bd5635a1
RP
1094 sym = lookup_block_symbol (block, name, namespace);
1095 if (!sym)
eaa1ef1d 1096 error ("Internal: static symbol `%s' found in psymtab but not in symtab", name);
bd5635a1
RP
1097 if (symtab != NULL)
1098 *symtab = s;
1099 return sym;
1100 }
1101
b039ac3a
JK
1102 /* Now search all per-file blocks for static mangled symbols.
1103 Do the symtabs first, then check the psymtabs. */
1104
1105 if (namespace == VAR_NAMESPACE)
1106 {
1107 for (s = symtab_list; s; s = s->next)
1108 {
1109 bv = BLOCKVECTOR (s);
1110 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1111 sym = lookup_demangled_block_symbol (block, name);
1112 if (sym)
1113 {
1114 block_found = block;
1115 if (symtab != NULL)
1116 *symtab = s;
1117 return sym;
1118 }
1119 }
1120
1121 for (ps = partial_symtab_list; ps; ps = ps->next)
1122 if (!ps->readin && lookup_demangled_partial_symbol (ps, name))
1123 {
1124 s = PSYMTAB_TO_SYMTAB(ps);
1125 bv = BLOCKVECTOR (s);
1126 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1127 sym = lookup_demangled_block_symbol (block, name);
1128 if (!sym)
eaa1ef1d 1129 error ("Internal: mangled static symbol `%s' found in psymtab but not in symtab", name);
b039ac3a
JK
1130 if (symtab != NULL)
1131 *symtab = s;
1132 return sym;
1133 }
1134 }
1135
bd5635a1
RP
1136 if (symtab != NULL)
1137 *symtab = NULL;
1138 return 0;
1139}
1140
b039ac3a
JK
1141/* Look for a static demangled symbol in block BLOCK. */
1142
1143static struct symbol *
1144lookup_demangled_block_symbol (block, name)
1145 register struct block *block;
1146 char *name;
1147{
1148 register int bot, top, inc;
1149 register struct symbol *sym;
1150
1151 bot = 0;
1152 top = BLOCK_NSYMS (block);
1153 inc = name[0];
1154
1155 while (bot < top)
1156 {
1157 sym = BLOCK_SYM (block, bot);
1158 if (SYMBOL_NAME (sym)[0] == inc
1159 && SYMBOL_NAMESPACE (sym) == VAR_NAMESPACE)
1160 {
1161 char *demangled = cplus_demangle(SYMBOL_NAME (sym), -1);
1162 if (demangled != NULL)
1163 {
1164 int cond = strcmp (demangled, name);
1165 free (demangled);
1166 if (!cond)
1167 return sym;
1168 }
1169 }
1170 bot++;
1171 }
1172
1173 return 0;
1174}
1175
1176/* Look, in partial_symtab PST, for static mangled symbol NAME. */
1177
1178static struct partial_symbol *
1179lookup_demangled_partial_symbol (pst, name)
1180 struct partial_symtab *pst;
1181 char *name;
1182{
1183 struct partial_symbol *start, *psym;
1184 int length = pst->n_static_syms;
1185 register int inc = name[0];
1186
1187 if (!length)
1188 return (struct partial_symbol *) 0;
1189
1190 start = static_psymbols.list + pst->statics_offset;
1191 for (psym = start; psym < start + length; psym++)
1192 {
1193 if (SYMBOL_NAME (psym)[0] == inc
1194 && SYMBOL_NAMESPACE (psym) == VAR_NAMESPACE)
1195 {
1196 char *demangled = cplus_demangle(SYMBOL_NAME (psym), -1);
1197 if (demangled != NULL)
1198 {
1199 int cond = strcmp (demangled, name);
1200 free (demangled);
1201 if (!cond)
1202 return psym;
1203 }
1204 }
1205 }
1206
1207 return (struct partial_symbol *) 0;
1208}
1209
bd5635a1
RP
1210/* Look, in partial_symtab PST, for symbol NAME. Check the global
1211 symbols if GLOBAL, the static symbols if not */
1212
1213static struct partial_symbol *
1214lookup_partial_symbol (pst, name, global, namespace)
1215 struct partial_symtab *pst;
1216 char *name;
1217 int global;
1218 enum namespace namespace;
1219{
1220 struct partial_symbol *start, *psym;
1221 int length = (global ? pst->n_global_syms : pst->n_static_syms);
1222
1223 if (!length)
1224 return (struct partial_symbol *) 0;
1225
1226 start = (global ?
1227 global_psymbols.list + pst->globals_offset :
1228 static_psymbols.list + pst->statics_offset );
1229
1230 if (global) /* This means we can use a binary */
1231 /* search. */
1232 {
1233 struct partial_symbol *top, *bottom, *center;
1234
1235 /* Binary search. This search is guaranteed to end with center
1236 pointing at the earliest partial symbol with the correct
1237 name. At that point *all* partial symbols with that name
1238 will be checked against the correct namespace. */
1239 bottom = start;
1240 top = start + length - 1;
1241 while (top > bottom)
1242 {
1243 center = bottom + (top - bottom) / 2;
1244
1245 assert (center < top);
1246
1247 if (strcmp (SYMBOL_NAME (center), name) >= 0)
1248 top = center;
1249 else
1250 bottom = center + 1;
1251 }
1252 assert (top == bottom);
1253
1254 while (!strcmp (SYMBOL_NAME (top), name))
1255 {
1256 if (SYMBOL_NAMESPACE (top) == namespace)
1257 return top;
1258 top ++;
1259 }
1260 }
1261 else
1262 {
1263 /* Can't use a binary search */
1264 for (psym = start; psym < start + length; psym++)
1265 if (namespace == SYMBOL_NAMESPACE (psym)
1266 && !strcmp (name, SYMBOL_NAME (psym)))
1267 return psym;
1268 }
1269
1270 return (struct partial_symbol *) 0;
1271}
1272
0e2a896c
PB
1273/* Find the psymtab containing main(). */
1274
1275struct partial_symtab *
1276find_main_psymtab ()
1277{
1278 register struct partial_symtab *pst;
1279 for (pst = partial_symtab_list; pst; pst = pst->next)
1280 if (lookup_partial_symbol (pst, "main", 1, VAR_NAMESPACE))
1281 return pst;
1282 return NULL;
1283}
1284
bd5635a1
RP
1285/* Look for a symbol in block BLOCK. */
1286
1287struct symbol *
1288lookup_block_symbol (block, name, namespace)
1289 register struct block *block;
1290 char *name;
1291 enum namespace namespace;
1292{
1293 register int bot, top, inc;
1294 register struct symbol *sym, *parameter_sym;
1295
1296 top = BLOCK_NSYMS (block);
1297 bot = 0;
1298
1299 /* If the blocks's symbols were sorted, start with a binary search. */
1300
1301 if (BLOCK_SHOULD_SORT (block))
1302 {
1303 /* First, advance BOT to not far before
1304 the first symbol whose name is NAME. */
1305
1306 while (1)
1307 {
1308 inc = (top - bot + 1);
1309 /* No need to keep binary searching for the last few bits worth. */
1310 if (inc < 4)
1311 break;
1312 inc = (inc >> 1) + bot;
1313 sym = BLOCK_SYM (block, inc);
1314 if (SYMBOL_NAME (sym)[0] < name[0])
1315 bot = inc;
1316 else if (SYMBOL_NAME (sym)[0] > name[0])
1317 top = inc;
1318 else if (strcmp (SYMBOL_NAME (sym), name) < 0)
1319 bot = inc;
1320 else
1321 top = inc;
1322 }
1323
1324 /* Now scan forward until we run out of symbols,
1325 find one whose name is greater than NAME,
1326 or find one we want.
1327 If there is more than one symbol with the right name and namespace,
1328 we return the first one. dbxread.c is careful to make sure
1329 that if one is a register then it comes first. */
1330
1331 top = BLOCK_NSYMS (block);
1332 while (bot < top)
1333 {
1334 sym = BLOCK_SYM (block, bot);
1335 inc = SYMBOL_NAME (sym)[0] - name[0];
1336 if (inc == 0)
1337 inc = strcmp (SYMBOL_NAME (sym), name);
1338 if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
1339 return sym;
1340 if (inc > 0)
1341 return 0;
1342 bot++;
1343 }
1344 return 0;
1345 }
1346
1347 /* Here if block isn't sorted.
1348 This loop is equivalent to the loop above,
1349 but hacked greatly for speed.
1350
1351 Note that parameter symbols do not always show up last in the
1352 list; this loop makes sure to take anything else other than
1353 parameter symbols first; it only uses parameter symbols as a
1354 last resort. Note that this only takes up extra computation
1355 time on a match. */
1356
1357 parameter_sym = (struct symbol *) 0;
1358 top = BLOCK_NSYMS (block);
1359 inc = name[0];
1360 while (bot < top)
1361 {
1362 sym = BLOCK_SYM (block, bot);
1363 if (SYMBOL_NAME (sym)[0] == inc
1364 && !strcmp (SYMBOL_NAME (sym), name)
1365 && SYMBOL_NAMESPACE (sym) == namespace)
1366 {
1367 if (SYMBOL_CLASS (sym) == LOC_ARG
1368 || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG
1369 || SYMBOL_CLASS (sym) == LOC_REF_ARG
1370 || SYMBOL_CLASS (sym) == LOC_REGPARM)
1371 parameter_sym = sym;
1372 else
1373 return sym;
1374 }
1375 bot++;
1376 }
1377 return parameter_sym; /* Will be 0 if not found. */
1378}
1379\f
1380/* Return the symbol for the function which contains a specified
1381 lexical block, described by a struct block BL. */
1382
1383struct symbol *
1384block_function (bl)
1385 struct block *bl;
1386{
1387 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
1388 bl = BLOCK_SUPERBLOCK (bl);
1389
1390 return BLOCK_FUNCTION (bl);
1391}
1392
1393/* Subroutine of find_pc_line */
1394
1395struct symtab *
1396find_pc_symtab (pc)
1397 register CORE_ADDR pc;
1398{
1399 register struct block *b;
1400 struct blockvector *bv;
1401 register struct symtab *s;
1402 register struct partial_symtab *ps;
1403
1404 /* Search all symtabs for one whose file contains our pc */
1405
1406 for (s = symtab_list; s; s = s->next)
1407 {
1408 bv = BLOCKVECTOR (s);
3ba6a043 1409 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
bd5635a1
RP
1410 if (BLOCK_START (b) <= pc
1411 && BLOCK_END (b) > pc)
1412 break;
1413 }
1414
1415 if (!s)
1416 {
1417 ps = find_pc_psymtab (pc);
1418 if (ps && ps->readin)
997a978c 1419 printf_filtered (
eaa1ef1d 1420 "(Internal error: pc 0x%x in read in psymtab, but not in symtab.)\n", pc);
bd5635a1
RP
1421
1422 if (ps)
1423 s = PSYMTAB_TO_SYMTAB (ps);
1424 }
1425
1426 return s;
1427}
1428
1429/* Find the source file and line number for a given PC value.
1430 Return a structure containing a symtab pointer, a line number,
1431 and a pc range for the entire source line.
1432 The value's .pc field is NOT the specified pc.
1433 NOTCURRENT nonzero means, if specified pc is on a line boundary,
1434 use the line that ends there. Otherwise, in that case, the line
1435 that begins there is used. */
1436
1437struct symtab_and_line
1438find_pc_line (pc, notcurrent)
1439 CORE_ADDR pc;
1440 int notcurrent;
1441{
1442 struct symtab *s;
1443 register struct linetable *l;
1444 register int len;
1445 register int i;
1446 register struct linetable_entry *item;
1447 struct symtab_and_line val;
1448 struct blockvector *bv;
1449
1450 /* Info on best line seen so far, and where it starts, and its file. */
1451
1452 int best_line = 0;
1453 CORE_ADDR best_pc = 0;
1454 CORE_ADDR best_end = 0;
1455 struct symtab *best_symtab = 0;
1456
1457 /* Store here the first line number
1458 of a file which contains the line at the smallest pc after PC.
1459 If we don't find a line whose range contains PC,
1460 we will use a line one less than this,
1461 with a range from the start of that file to the first line's pc. */
1462 int alt_line = 0;
1463 CORE_ADDR alt_pc = 0;
1464 struct symtab *alt_symtab = 0;
1465
1466 /* Info on best line seen in this file. */
1467
1468 int prev_line;
1469 CORE_ADDR prev_pc;
1470
1471 /* Info on first line of this file. */
1472
1473 int first_line;
1474 CORE_ADDR first_pc;
1475
1476 /* If this pc is not from the current frame,
1477 it is the address of the end of a call instruction.
1478 Quite likely that is the start of the following statement.
1479 But what we want is the statement containing the instruction.
1480 Fudge the pc to make sure we get that. */
1481
1482 if (notcurrent) pc -= 1;
1483
1484 s = find_pc_symtab (pc);
1485 if (s == 0)
1486 {
1487 val.symtab = 0;
1488 val.line = 0;
1489 val.pc = pc;
1490 val.end = 0;
1491 return val;
1492 }
1493
1494 bv = BLOCKVECTOR (s);
1495
1496 /* Look at all the symtabs that share this blockvector.
1497 They all have the same apriori range, that we found was right;
1498 but they have different line tables. */
1499
1500 for (; s && BLOCKVECTOR (s) == bv; s = s->next)
1501 {
1502 /* Find the best line in this symtab. */
1503 l = LINETABLE (s);
4137c5fc
JG
1504 if (!l)
1505 continue;
bd5635a1
RP
1506 len = l->nitems;
1507 prev_line = -1;
1508 first_line = -1;
1509 for (i = 0; i < len; i++)
1510 {
1511 item = &(l->item[i]);
1512
1513 if (first_line < 0)
1514 {
1515 first_line = item->line;
1516 first_pc = item->pc;
1517 }
1518 /* Return the last line that did not start after PC. */
1519 if (pc >= item->pc)
1520 {
1521 prev_line = item->line;
1522 prev_pc = item->pc;
1523 }
1524 else
1525 break;
1526 }
1527
1528 /* Is this file's best line closer than the best in the other files?
1529 If so, record this file, and its best line, as best so far. */
1530 if (prev_line >= 0 && prev_pc > best_pc)
1531 {
1532 best_pc = prev_pc;
1533 best_line = prev_line;
1534 best_symtab = s;
1535 if (i < len)
1536 best_end = item->pc;
1537 else
1538 best_end = 0;
1539 }
1540 /* Is this file's first line closer than the first lines of other files?
1541 If so, record this file, and its first line, as best alternate. */
1542 if (first_line >= 0 && first_pc > pc
1543 && (alt_pc == 0 || first_pc < alt_pc))
1544 {
1545 alt_pc = first_pc;
1546 alt_line = first_line;
1547 alt_symtab = s;
1548 }
1549 }
1550 if (best_symtab == 0)
1551 {
1552 val.symtab = alt_symtab;
1553 val.line = alt_line - 1;
3ba6a043 1554 val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
bd5635a1
RP
1555 val.end = alt_pc;
1556 }
1557 else
1558 {
1559 val.symtab = best_symtab;
1560 val.line = best_line;
1561 val.pc = best_pc;
1562 val.end = (best_end ? best_end
1563 : (alt_pc ? alt_pc
3ba6a043 1564 : BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))));
bd5635a1
RP
1565 }
1566 return val;
1567}
1568\f
1569/* Find the PC value for a given source file and line number.
1570 Returns zero for invalid line number.
1571 The source file is specified with a struct symtab. */
1572
1573CORE_ADDR
1574find_line_pc (symtab, line)
1575 struct symtab *symtab;
1576 int line;
1577{
1578 register struct linetable *l;
1579 register int ind;
1580 int dummy;
1581
1582 if (symtab == 0)
1583 return 0;
1584 l = LINETABLE (symtab);
1585 ind = find_line_common(l, line, &dummy);
b203fc18 1586 return (ind >= 0) ? l->item[ind].pc : 0;
bd5635a1
RP
1587}
1588
1589/* Find the range of pc values in a line.
1590 Store the starting pc of the line into *STARTPTR
1591 and the ending pc (start of next line) into *ENDPTR.
1592 Returns 1 to indicate success.
1593 Returns 0 if could not find the specified line. */
1594
1595int
1596find_line_pc_range (symtab, thisline, startptr, endptr)
1597 struct symtab *symtab;
1598 int thisline;
1599 CORE_ADDR *startptr, *endptr;
1600{
1601 register struct linetable *l;
1602 register int ind;
1603 int exact_match; /* did we get an exact linenumber match */
1604
1605 if (symtab == 0)
1606 return 0;
1607
1608 l = LINETABLE (symtab);
1609 ind = find_line_common (l, thisline, &exact_match);
b203fc18 1610 if (ind >= 0)
bd5635a1
RP
1611 {
1612 *startptr = l->item[ind].pc;
1613 /* If we have not seen an entry for the specified line,
1614 assume that means the specified line has zero bytes. */
1615 if (!exact_match || ind == l->nitems-1)
1616 *endptr = *startptr;
1617 else
1618 /* Perhaps the following entry is for the following line.
1619 It's worth a try. */
1620 if (ind+1 < l->nitems
1621 && l->item[ind+1].line == thisline + 1)
1622 *endptr = l->item[ind+1].pc;
1623 else
1624 *endptr = find_line_pc (symtab, thisline+1);
1625 return 1;
1626 }
1627
1628 return 0;
1629}
1630
1631/* Given a line table and a line number, return the index into the line
1632 table for the pc of the nearest line whose number is >= the specified one.
b203fc18 1633 Return -1 if none is found. The value is >= 0 if it is an index.
bd5635a1
RP
1634
1635 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
1636
1637static int
1638find_line_common (l, lineno, exact_match)
1639 register struct linetable *l;
1640 register int lineno;
1641 int *exact_match;
1642{
1643 register int i;
1644 register int len;
1645
1646 /* BEST is the smallest linenumber > LINENO so far seen,
1647 or 0 if none has been seen so far.
1648 BEST_INDEX identifies the item for it. */
1649
b203fc18 1650 int best_index = -1;
bd5635a1
RP
1651 int best = 0;
1652
1653 if (lineno <= 0)
b203fc18 1654 return -1;
4137c5fc
JG
1655 if (l == 0)
1656 return -1;
bd5635a1
RP
1657
1658 len = l->nitems;
1659 for (i = 0; i < len; i++)
1660 {
1661 register struct linetable_entry *item = &(l->item[i]);
1662
1663 if (item->line == lineno)
1664 {
1665 *exact_match = 1;
1666 return i;
1667 }
1668
1669 if (item->line > lineno && (best == 0 || item->line < best))
1670 {
1671 best = item->line;
1672 best_index = i;
1673 }
1674 }
1675
1676 /* If we got here, we didn't get an exact match. */
1677
1678 *exact_match = 0;
1679 return best_index;
1680}
1681
1682int
1683find_pc_line_pc_range (pc, startptr, endptr)
1684 CORE_ADDR pc;
1685 CORE_ADDR *startptr, *endptr;
1686{
1687 struct symtab_and_line sal;
1688 sal = find_pc_line (pc, 0);
1689 *startptr = sal.pc;
1690 *endptr = sal.end;
1691 return sal.symtab != 0;
1692}
1693\f
d96b54ea
JK
1694/* If P is of the form "operator[ \t]+..." where `...' is
1695 some legitimate operator text, return a pointer to the
1696 beginning of the substring of the operator text.
1697 Otherwise, return "". */
1698static char *
1699operator_chars (p, end)
1700 char *p;
1701 char **end;
1702{
1703 *end = "";
1704 if (strncmp (p, "operator", 8))
1705 return *end;
1706 p += 8;
1707
1708 /* Don't get faked out by `operator' being part of a longer
1709 identifier. */
1710 if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')
1711 || *p == '_' || *p == '$' || *p == '\0')
1712 return *end;
1713
1714 /* Allow some whitespace between `operator' and the operator symbol. */
1715 while (*p == ' ' || *p == '\t')
1716 p++;
1717
1718 switch (*p)
1719 {
1720 case '!':
1721 case '=':
1722 case '*':
1723 case '/':
1724 case '%':
1725 case '^':
1726 if (p[1] == '=')
1727 *end = p+2;
1728 else
1729 *end = p+1;
1730 return p;
1731 case '<':
1732 case '>':
1733 case '+':
1734 case '-':
1735 case '&':
1736 case '|':
1737 if (p[1] == '=' || p[1] == p[0])
1738 *end = p+2;
1739 else
1740 *end = p+1;
1741 return p;
1742 case '~':
1743 case ',':
1744 *end = p+1;
1745 return p;
1746 case '(':
1747 if (p[1] != ')')
1748 error ("`operator ()' must be specified without whitespace in `()'");
1749 *end = p+2;
1750 return p;
1751 case '?':
1752 if (p[1] != ':')
1753 error ("`operator ?:' must be specified without whitespace in `?:'");
1754 *end = p+2;
1755 return p;
1756 case '[':
1757 if (p[1] != ']')
1758 error ("`operator []' must be specified without whitespace in `[]'");
1759 *end = p+2;
1760 return p;
1761 default:
1762 error ("`operator %s' not supported", p);
1763 break;
1764 }
1765 *end = "";
1766 return *end;
1767}
1768
bd5635a1
RP
1769/* Recursive helper function for decode_line_1.
1770 * Look for methods named NAME in type T.
1771 * Return number of matches.
1772 * Put matches in PHYSNAMES and SYM_ARR (which better be big enough!).
1773 * These allocations seem to define "big enough":
1774 * sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
1775 * physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
1776 */
1777
1778int
1779find_methods(t, name, physnames, sym_arr)
1780 struct type *t;
1781 char *name;
1782 char **physnames;
1783 struct symbol **sym_arr;
1784{
1785 int i1 = 0;
1786 int ibase;
1787 struct symbol *sym_class;
1788 char *class_name = type_name_no_tag (t);
1789 /* Ignore this class if it doesn't have a name.
1790 This prevents core dumps, but is just a workaround
1791 because we might not find the function in
1792 certain cases, such as
1793 struct D {virtual int f();}
1794 struct C : D {virtual int g();}
1795 (in this case g++ 1.35.1- does not put out a name
1796 for D as such, it defines type 19 (for example) in
1797 the same stab as C, and then does a
1798 .stabs "D:T19" and a .stabs "D:t19".
1799 Thus
1800 "break C::f" should not be looking for field f in
1801 the class named D,
1802 but just for the field f in the baseclasses of C
1803 (no matter what their names).
1804
1805 However, I don't know how to replace the code below
1806 that depends on knowing the name of D. */
1807 if (class_name
1808 && (sym_class = lookup_symbol (class_name,
1809 (struct block *)NULL,
1810 STRUCT_NAMESPACE,
1811 (int *)NULL,
1812 (struct symtab **)NULL)))
1813 {
1814 int method_counter;
1815 t = SYMBOL_TYPE (sym_class);
1816 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1817 method_counter >= 0;
1818 --method_counter)
1819 {
1820 int field_counter;
1821 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, method_counter);
1822
1823 char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
1824 if (!strcmp (name, method_name))
1825 /* Find all the fields with that name. */
1826 for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
1827 field_counter >= 0;
1828 --field_counter)
1829 {
1830 char *phys_name;
1831 if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, field_counter)) & TYPE_FLAG_STUB)
1832 check_stub_method (t, method_counter, field_counter);
1833 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1834 physnames[i1] = (char*) alloca (strlen (phys_name) + 1);
1835 strcpy (physnames[i1], phys_name);
1836 sym_arr[i1] = lookup_symbol (phys_name,
1837 SYMBOL_BLOCK_VALUE (sym_class),
1838 VAR_NAMESPACE,
1839 (int *) NULL,
1840 (struct symtab **) NULL);
1841 if (sym_arr[i1]) i1++;
1842 }
1843 }
1844 }
1845 /* Only search baseclasses if there is no match yet,
1846 * since names in derived classes override those in baseclasses.
1847 */
1848 if (i1)
1849 return i1;
1850 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
1851 i1 += find_methods(TYPE_BASECLASS(t, ibase), name,
1852 physnames + i1, sym_arr + i1);
1853 return i1;
1854}
1855
1856/* Parse a string that specifies a line number.
1857 Pass the address of a char * variable; that variable will be
1858 advanced over the characters actually parsed.
1859
1860 The string can be:
1861
1862 LINENUM -- that line number in current file. PC returned is 0.
1863 FILE:LINENUM -- that line in that file. PC returned is 0.
1864 FUNCTION -- line number of openbrace of that function.
1865 PC returned is the start of the function.
1866 VARIABLE -- line number of definition of that variable.
1867 PC returned is 0.
1868 FILE:FUNCTION -- likewise, but prefer functions in that file.
1869 *EXPR -- line in which address EXPR appears.
1870
1871 FUNCTION may be an undebuggable function found in misc_function_vector.
1872
1873 If the argument FUNFIRSTLINE is nonzero, we want the first line
1874 of real code inside a function when a function is specified.
1875
1876 DEFAULT_SYMTAB specifies the file to use if none is specified.
1877 It defaults to current_source_symtab.
1878 DEFAULT_LINE specifies the line number to use for relative
1879 line numbers (that start with signs). Defaults to current_source_line.
1880
1881 Note that it is possible to return zero for the symtab
1882 if no file is validly specified. Callers must check that.
1883 Also, the line number returned may be invalid. */
1884
1885struct symtabs_and_lines
1886decode_line_1 (argptr, funfirstline, default_symtab, default_line)
1887 char **argptr;
1888 int funfirstline;
1889 struct symtab *default_symtab;
1890 int default_line;
1891{
1892 struct symtabs_and_lines decode_line_2 ();
1893 struct symtabs_and_lines values;
1894 struct symtab_and_line val;
1895 register char *p, *p1;
d96b54ea 1896 char *q, *q1;
bd5635a1
RP
1897 register struct symtab *s;
1898
1899 register struct symbol *sym;
1900 /* The symtab that SYM was found in. */
1901 struct symtab *sym_symtab;
1902
1903 register CORE_ADDR pc;
1904 register int i;
1905 char *copy;
1906 struct symbol *sym_class;
1907 int i1;
1908 struct symbol **sym_arr;
1909 struct type *t;
1910 char **physnames;
1911
1912 /* Defaults have defaults. */
1913
1914 if (default_symtab == 0)
1915 {
1916 default_symtab = current_source_symtab;
1917 default_line = current_source_line;
1918 }
1919
1920 /* See if arg is *PC */
1921
1922 if (**argptr == '*')
1923 {
1924 (*argptr)++;
1925 pc = parse_and_eval_address_1 (argptr);
1926 values.sals = (struct symtab_and_line *)
1927 xmalloc (sizeof (struct symtab_and_line));
1928 values.nelts = 1;
1929 values.sals[0] = find_pc_line (pc, 0);
1930 values.sals[0].pc = pc;
1931 return values;
1932 }
1933
1934 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
1935
1936 s = 0;
1937
1938 for (p = *argptr; *p; p++)
1939 {
1940 if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
1941 break;
1942 }
1943 while (p[0] == ' ' || p[0] == '\t') p++;
1944
1945 if (p[0] == ':')
1946 {
1947
1948 /* C++ */
1949 if (p[1] ==':')
1950 {
1951 /* Extract the class name. */
1952 p1 = p;
1953 while (p != *argptr && p[-1] == ' ') --p;
1954 copy = (char *) alloca (p - *argptr + 1);
1955 bcopy (*argptr, copy, p - *argptr);
1956 copy[p - *argptr] = 0;
1957
1958 /* Discard the class name from the arg. */
1959 p = p1 + 2;
1960 while (*p == ' ' || *p == '\t') p++;
1961 *argptr = p;
1962
1963 sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0,
1964 (struct symtab **)NULL);
1965
1966 if (sym_class &&
1967 (TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_STRUCT
1968 || TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_UNION))
1969 {
1970 /* Arg token is not digits => try it as a function name
1971 Find the next token (everything up to end or next whitespace). */
1972 p = *argptr;
1973 while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p !=':') p++;
d96b54ea 1974 q = operator_chars (*argptr, &q1);
aec4cb91 1975
d96b54ea
JK
1976 copy = (char *) alloca (p - *argptr + 1 + (q1 - q));
1977 if (q1 - q)
1978 {
1979 copy[0] = 'o';
1980 copy[1] = 'p';
1981 copy[2] = CPLUS_MARKER;
1982 bcopy (q, copy + 3, q1 - q);
1983 copy[3 + (q1 - q)] = '\0';
1984 p = q1;
1985 }
1986 else
1987 {
1988 bcopy (*argptr, copy, p - *argptr);
1989 copy[p - *argptr] = '\0';
1990 }
bd5635a1
RP
1991
1992 /* no line number may be specified */
1993 while (*p == ' ' || *p == '\t') p++;
1994 *argptr = p;
1995
1996 sym = 0;
1997 i1 = 0; /* counter for the symbol array */
1998 t = SYMBOL_TYPE (sym_class);
1999 sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
2000 physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
2001
2002 if (destructor_name_p (copy, t))
2003 {
2004 /* destructors are a special case. */
2005 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, 0);
2006 int len = TYPE_FN_FIELDLIST_LENGTH (t, 0) - 1;
2007 char *phys_name = TYPE_FN_FIELD_PHYSNAME (f, len);
2008 physnames[i1] = (char *)alloca (strlen (phys_name) + 1);
2009 strcpy (physnames[i1], phys_name);
2010 sym_arr[i1] =
2011 lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class),
2012 VAR_NAMESPACE, 0, (struct symtab **)NULL);
2013 if (sym_arr[i1]) i1++;
2014 }
2015 else
2016 i1 = find_methods (t, copy, physnames, sym_arr);
2017 if (i1 == 1)
2018 {
2019 /* There is exactly one field with that name. */
2020 sym = sym_arr[0];
2021
2022 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2023 {
2024 /* Arg is the name of a function */
2025 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
2026 if (funfirstline)
2027 SKIP_PROLOGUE (pc);
2028 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2029 values.nelts = 1;
2030 values.sals[0] = find_pc_line (pc, 0);
2031 values.sals[0].pc = (values.sals[0].end && values.sals[0].pc != pc) ? values.sals[0].end : pc;
2032 }
2033 else
2034 {
2035 values.nelts = 0;
2036 }
2037 return values;
2038 }
2039 if (i1 > 0)
2040 {
2041 /* There is more than one field with that name
2042 (overloaded). Ask the user which one to use. */
2043 return decode_line_2 (sym_arr, i1, funfirstline);
2044 }
2045 else
d96b54ea
JK
2046 {
2047 char *tmp;
2048
2049 if (OPNAME_PREFIX_P (copy))
2050 {
2051 tmp = (char *)alloca (strlen (copy+3) + 9);
2052 strcpy (tmp, "operator ");
2053 strcat (tmp, copy+3);
2054 }
2055 else
2056 tmp = copy;
0e2a896c
PB
2057 if (tmp[0] == '~')
2058 error ("The class `%s' does not have destructor defined",
2059 sym_class->name);
2060 else
2061 error ("The class %s does not have any method named %s",
2062 sym_class->name, tmp);
d96b54ea 2063 }
bd5635a1
RP
2064 }
2065 else
2066 /* The quotes are important if copy is empty. */
2067 error("No class, struct, or union named \"%s\"", copy );
2068 }
2069 /* end of C++ */
2070
2071
2072 /* Extract the file name. */
2073 p1 = p;
2074 while (p != *argptr && p[-1] == ' ') --p;
d96b54ea
JK
2075 copy = (char *) alloca (p - *argptr + 1 + (q1 - q));
2076 if (q1 - q)
2077 {
2078 copy[0] = 'o';
2079 copy[1] = 'p';
2080 copy[2] = CPLUS_MARKER;
2081 bcopy (q, copy + 3, q1-q);
2082 copy[3 + (q1-q)] = 0;
2083 p = q1;
2084 }
2085 else
2086 {
2087 bcopy (*argptr, copy, p - *argptr);
2088 copy[p - *argptr] = 0;
2089 }
bd5635a1
RP
2090
2091 /* Find that file's data. */
2092 s = lookup_symtab (copy);
2093 if (s == 0)
2094 {
2095 if (symtab_list == 0 && partial_symtab_list == 0)
2096 error (no_symtab_msg);
2097 error ("No source file named %s.", copy);
2098 }
2099
2100 /* Discard the file name from the arg. */
2101 p = p1 + 1;
2102 while (*p == ' ' || *p == '\t') p++;
2103 *argptr = p;
2104 }
2105
2106 /* S is specified file's symtab, or 0 if no file specified.
2107 arg no longer contains the file name. */
2108
2109 /* Check whether arg is all digits (and sign) */
2110
2111 p = *argptr;
2112 if (*p == '-' || *p == '+') p++;
2113 while (*p >= '0' && *p <= '9')
2114 p++;
2115
2116 if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ','))
2117 {
2118 /* We found a token consisting of all digits -- at least one digit. */
2119 enum sign {none, plus, minus} sign = none;
2120
2121 /* This is where we need to make sure that we have good defaults.
2122 We must guarantee that this section of code is never executed
2123 when we are called with just a function name, since
2124 select_source_symtab calls us with such an argument */
2125
2126 if (s == 0 && default_symtab == 0)
2127 {
bd5635a1
RP
2128 select_source_symtab (0);
2129 default_symtab = current_source_symtab;
2130 default_line = current_source_line;
2131 }
2132
2133 if (**argptr == '+')
2134 sign = plus, (*argptr)++;
2135 else if (**argptr == '-')
2136 sign = minus, (*argptr)++;
2137 val.line = atoi (*argptr);
2138 switch (sign)
2139 {
2140 case plus:
2141 if (p == *argptr)
2142 val.line = 5;
2143 if (s == 0)
2144 val.line = default_line + val.line;
2145 break;
2146 case minus:
2147 if (p == *argptr)
2148 val.line = 15;
2149 if (s == 0)
2150 val.line = default_line - val.line;
2151 else
2152 val.line = 1;
2153 break;
2154 case none:
2155 break; /* No need to adjust val.line. */
2156 }
2157
2158 while (*p == ' ' || *p == '\t') p++;
2159 *argptr = p;
2160 if (s == 0)
2161 s = default_symtab;
2162 val.symtab = s;
2163 val.pc = 0;
2164 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2165 values.sals[0] = val;
2166 values.nelts = 1;
2167 return values;
2168 }
2169
2170 /* Arg token is not digits => try it as a variable name
2171 Find the next token (everything up to end or next whitespace). */
2172 p = *argptr;
2173 while (*p && *p != ' ' && *p != '\t' && *p != ',') p++;
2174 copy = (char *) alloca (p - *argptr + 1);
2175 bcopy (*argptr, copy, p - *argptr);
2176 copy[p - *argptr] = 0;
2177 while (*p == ' ' || *p == '\t') p++;
2178 *argptr = p;
2179
2180 /* Look up that token as a variable.
2181 If file specified, use that file's per-file block to start with. */
2182
2183 sym = lookup_symbol (copy,
3ba6a043 2184 (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
bd5635a1
RP
2185 : get_selected_block ()),
2186 VAR_NAMESPACE, 0, &sym_symtab);
2187
2188 if (sym != NULL)
2189 {
2190 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
2191 {
2192 /* Arg is the name of a function */
2193 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
2194 if (funfirstline)
2195 SKIP_PROLOGUE (pc);
2196 val = find_pc_line (pc, 0);
2197#ifdef PROLOGUE_FIRSTLINE_OVERLAP
2198 /* Convex: no need to suppress code on first line, if any */
2199 val.pc = pc;
2200#else
2201 val.pc = (val.end && val.pc != pc) ? val.end : pc;
2202#endif
2203 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2204 values.sals[0] = val;
2205 values.nelts = 1;
2206
2207 /* I think this is always the same as the line that
2208 we calculate above, but the general principle is
2209 "trust the symbols more than stuff like
2210 SKIP_PROLOGUE". */
2211 if (SYMBOL_LINE (sym) != 0)
2212 values.sals[0].line = SYMBOL_LINE (sym);
2213
2214 return values;
2215 }
2216 else if (SYMBOL_LINE (sym) != 0)
2217 {
2218 /* We know its line number. */
2219 values.sals = (struct symtab_and_line *)
2220 xmalloc (sizeof (struct symtab_and_line));
2221 values.nelts = 1;
2222 bzero (&values.sals[0], sizeof (values.sals[0]));
2223 values.sals[0].symtab = sym_symtab;
2224 values.sals[0].line = SYMBOL_LINE (sym);
2225 return values;
2226 }
2227 else
2228 /* This can happen if it is compiled with a compiler which doesn't
2229 put out line numbers for variables. */
2230 error ("Line number not known for symbol \"%s\"", copy);
2231 }
2232
bd5635a1
RP
2233 if ((i = lookup_misc_func (copy)) >= 0)
2234 {
2235 val.symtab = 0;
2236 val.line = 0;
2237 val.pc = misc_function_vector[i].address + FUNCTION_START_OFFSET;
2238 if (funfirstline)
2239 SKIP_PROLOGUE (val.pc);
2240 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2241 values.sals[0] = val;
2242 values.nelts = 1;
2243 return values;
2244 }
2245
997a978c
JG
2246 if (symtab_list == 0 && partial_symtab_list == 0 && misc_function_count == 0)
2247 error (no_symtab_msg);
2248
bd5635a1
RP
2249 error ("Function %s not defined.", copy);
2250 return values; /* for lint */
2251}
2252
2253struct symtabs_and_lines
2254decode_line_spec (string, funfirstline)
2255 char *string;
2256 int funfirstline;
2257{
2258 struct symtabs_and_lines sals;
2259 if (string == 0)
2260 error ("Empty line specification.");
2261 sals = decode_line_1 (&string, funfirstline,
2262 current_source_symtab, current_source_line);
2263 if (*string)
2264 error ("Junk at end of line specification: %s", string);
2265 return sals;
2266}
2267
2268/* Given a list of NELTS symbols in sym_arr (with corresponding
2269 mangled names in physnames), return a list of lines to operate on
2270 (ask user if necessary). */
2271struct symtabs_and_lines
2272decode_line_2 (sym_arr, nelts, funfirstline)
2273 struct symbol *sym_arr[];
2274 int nelts;
2275 int funfirstline;
2276{
bd5635a1
RP
2277 struct symtabs_and_lines values, return_values;
2278 register CORE_ADDR pc;
2279 char *args, *arg1, *command_line_input ();
2280 int i;
2281 char *prompt;
2282
2283 values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line));
2284 return_values.sals = (struct symtab_and_line *) xmalloc (nelts * sizeof(struct symtab_and_line));
2285
2286 i = 0;
2287 printf("[0] cancel\n[1] all\n");
2288 while (i < nelts)
2289 {
2290 if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
2291 {
2292 /* Arg is the name of a function */
2293 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr[i]))
2294 + FUNCTION_START_OFFSET;
2295 if (funfirstline)
2296 SKIP_PROLOGUE (pc);
2297 values.sals[i] = find_pc_line (pc, 0);
2298 values.sals[i].pc = (values.sals[i].end && values.sals[i].pc != pc) ?
2299 values.sals[i].end : pc;
2300 printf("[%d] file:%s; line number:%d\n",
2301 (i+2), values.sals[i].symtab->filename, values.sals[i].line);
2302 }
2303 else printf ("?HERE\n");
2304 i++;
2305 }
2306
2307 if ((prompt = getenv ("PS2")) == NULL)
2308 {
2309 prompt = ">";
2310 }
2311 printf("%s ",prompt);
2312 fflush(stdout);
2313
2314 args = command_line_input (0, 0);
2315
2316 if (args == 0)
2317 error_no_arg ("one or more choice numbers");
2318
2319 i = 0;
2320 while (*args)
2321 {
2322 int num;
2323
2324 arg1 = args;
2325 while (*arg1 >= '0' && *arg1 <= '9') arg1++;
2326 if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
2327 error ("Arguments must be choice numbers.");
2328
2329 num = atoi (args);
2330
2331 if (num == 0)
2332 error ("cancelled");
2333 else if (num == 1)
2334 {
2335 bcopy (values.sals, return_values.sals, (nelts * sizeof(struct symtab_and_line)));
2336 return_values.nelts = nelts;
2337 return return_values;
2338 }
2339
2340 if (num > nelts + 2)
2341 {
2342 printf ("No choice number %d.\n", num);
2343 }
2344 else
2345 {
2346 num -= 2;
2347 if (values.sals[num].pc)
2348 {
2349 return_values.sals[i++] = values.sals[num];
2350 values.sals[num].pc = 0;
2351 }
2352 else
2353 {
2354 printf ("duplicate request for %d ignored.\n", num);
2355 }
2356 }
2357
2358 args = arg1;
2359 while (*args == ' ' || *args == '\t') args++;
2360 }
2361 return_values.nelts = i;
2362 return return_values;
2363}
2364
2365/* Return the index of misc function named NAME. */
2366
2367int
2368lookup_misc_func (name)
2369 register char *name;
2370{
2371 register int i;
2372
2373 for (i = 0; i < misc_function_count; i++)
2374 if (!strcmp (misc_function_vector[i].name, name))
2375 return i;
2376 return -1; /* not found */
2377}
2378\f
2379/* Slave routine for sources_info. Force line breaks at ,'s.
2380 NAME is the name to print and *FIRST is nonzero if this is the first
2381 name printed. Set *FIRST to zero. */
2382static void
2383output_source_filename (name, first)
2384 char *name;
2385 int *first;
2386{
2387 static int column;
2388 /* Table of files printed so far. Since a single source file can
2389 result in several partial symbol tables, we need to avoid printing
2390 it more than once. Note: if some of the psymtabs are read in and
2391 some are not, it gets printed both under "Source files for which
2392 symbols have been read" and "Source files for which symbols will
2393 be read in on demand". I consider this a reasonable way to deal
2394 with the situation. I'm not sure whether this can also happen for
2395 symtabs; it doesn't hurt to check. */
2396 static char **tab = NULL;
2397 /* Allocated size of tab in elements.
2398 Start with one 256-byte block (when using GNU malloc.c).
2399 24 is the malloc overhead when range checking is in effect. */
2400 static int tab_alloc_size = (256 - 24) / sizeof (char *);
2401 /* Current size of tab in elements. */
2402 static int tab_cur_size;
2403
2404 char **p;
2405
2406 if (*first)
2407 {
2408 if (tab == NULL)
2409 tab = (char **) xmalloc (tab_alloc_size * sizeof (*tab));
2410 tab_cur_size = 0;
2411 }
2412
2413 /* Is NAME in tab? */
2414 for (p = tab; p < tab + tab_cur_size; p++)
2415 if (strcmp (*p, name) == 0)
2416 /* Yes; don't print it again. */
2417 return;
2418 /* No; add it to tab. */
2419 if (tab_cur_size == tab_alloc_size)
2420 {
2421 tab_alloc_size *= 2;
2422 tab = (char **) xrealloc (tab, tab_alloc_size * sizeof (*tab));
2423 }
2424 tab[tab_cur_size++] = name;
2425
2426 if (*first)
2427 {
2428 column = 0;
2429 *first = 0;
2430 }
2431 else
2432 {
2433 printf_filtered (",");
2434 column++;
2435 }
2436
2437 if (column != 0 && column + strlen (name) >= 70)
2438 {
2439 printf_filtered ("\n");
2440 column = 0;
2441 }
2442 else if (column != 0)
2443 {
2444 printf_filtered (" ");
2445 column++;
2446 }
2447 fputs_filtered (name, stdout);
2448 column += strlen (name);
2449}
2450
2451static void
2452sources_info ()
2453{
2454 register struct symtab *s;
2455 register struct partial_symtab *ps;
2456 int first;
2457
2458 if (symtab_list == 0 && partial_symtab_list == 0)
2459 {
2460 printf (no_symtab_msg);
2461 return;
2462 }
2463
2464 printf_filtered ("Source files for which symbols have been read in:\n\n");
2465
2466 first = 1;
2467 for (s = symtab_list; s; s = s->next)
2468 output_source_filename (s->filename, &first);
2469 printf_filtered ("\n\n");
2470
2471 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2472
2473 first = 1;
2474 for (ps = partial_symtab_list; ps; ps = ps->next)
2475 if (!ps->readin)
2476 output_source_filename (ps->filename, &first);
2477 printf_filtered ("\n");
2478}
2479
2480/* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
2481 If CLASS is zero, list all symbols except functions and type names.
2482 If CLASS is 1, list only functions.
2483 If CLASS is 2, list only type names.
997a978c 2484 If CLASS is 3, list only method names.
bd5635a1
RP
2485
2486 BPT is non-zero if we should set a breakpoint at the functions
2487 we find. */
2488
2489static void
2490list_symbols (regexp, class, bpt)
2491 char *regexp;
2492 int class;
2493 int bpt;
2494{
2495 register struct symtab *s;
2496 register struct partial_symtab *ps;
2497 register struct blockvector *bv;
2498 struct blockvector *prev_bv = 0;
2499 register struct block *b;
2500 register int i, j;
2501 register struct symbol *sym;
2502 struct partial_symbol *psym;
2503 char *val;
2504 static char *classnames[]
2505 = {"variable", "function", "type", "method"};
2506 int found_in_file = 0;
997a978c
JG
2507 int found_misc = 0;
2508 static enum misc_function_type types[]
2509 = {mf_data, mf_text, mf_abs, mf_unknown};
2510 static enum misc_function_type types2[]
2511 = {mf_bss, mf_text, mf_abs, mf_unknown};
2512 enum misc_function_type ourtype = types[class];
2513 enum misc_function_type ourtype2 = types2[class];
bd5635a1
RP
2514
2515 if (regexp)
d11c44f1 2516 if (0 != (val = re_comp (regexp)))
bd5635a1
RP
2517 error ("Invalid regexp (%s): %s", val, regexp);
2518
2519 /* Search through the partial_symtab_list *first* for all symbols
2520 matching the regexp. That way we don't have to reproduce all of
2521 the machinery below. */
2522 for (ps = partial_symtab_list; ps; ps = ps->next)
2523 {
2524 struct partial_symbol *bound, *gbound, *sbound;
2525 int keep_going = 1;
2526
2527 if (ps->readin) continue;
2528
2529 gbound = global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2530 sbound = static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2531 bound = gbound;
2532
2533 /* Go through all of the symbols stored in a partial
2534 symtab in one loop. */
2535 psym = global_psymbols.list + ps->globals_offset;
2536 while (keep_going)
2537 {
2538 if (psym >= bound)
2539 {
2540 if (bound == gbound && ps->n_static_syms != 0)
2541 {
2542 psym = static_psymbols.list + ps->statics_offset;
2543 bound = sbound;
2544 }
2545 else
2546 keep_going = 0;
3ba6a043 2547 continue;
bd5635a1
RP
2548 }
2549 else
2550 {
2551 QUIT;
2552
2553 /* If it would match (logic taken from loop below)
2554 load the file and go on to the next one */
2555 if ((regexp == 0 || re_exec (SYMBOL_NAME (psym)))
2556 && ((class == 0 && SYMBOL_CLASS (psym) != LOC_TYPEDEF
997a978c 2557 && SYMBOL_CLASS (psym) != LOC_BLOCK)
bd5635a1
RP
2558 || (class == 1 && SYMBOL_CLASS (psym) == LOC_BLOCK)
2559 || (class == 2 && SYMBOL_CLASS (psym) == LOC_TYPEDEF)
2560 || (class == 3 && SYMBOL_CLASS (psym) == LOC_BLOCK)))
2561 {
2562 (void) PSYMTAB_TO_SYMTAB(ps);
2563 keep_going = 0;
2564 }
2565 }
2566 psym++;
2567 }
2568 }
2569
997a978c 2570 /* Here, we search through the misc function vector for functions that
bd5635a1
RP
2571 match, and call find_pc_symtab on them to force their symbols to
2572 be read. The symbol will then be found during the scan of symtabs
997a978c
JG
2573 below. If find_pc_symtab fails, set found_misc so that we will
2574 rescan to print any matching symbols without debug info. */
2575
2576 if (class == 1) {
2577 for (i = 0; i < misc_function_count; i++) {
2578 if (misc_function_vector[i].type != ourtype
2579 && misc_function_vector[i].type != ourtype2)
2580 continue;
2581 if (regexp == 0 || re_exec (misc_function_vector[i].name)) {
2582 if (0 == find_pc_symtab (misc_function_vector[i].address))
2583 found_misc = 1;
2584 }
bd5635a1 2585 }
997a978c 2586 }
bd5635a1
RP
2587
2588 /* Printout here so as to get after the "Reading in symbols"
2589 messages which will be generated above. */
2590 if (!bpt)
2591 printf_filtered (regexp
2592 ? "All %ss matching regular expression \"%s\":\n"
2593 : "All defined %ss:\n",
2594 classnames[class],
2595 regexp);
2596
2597 for (s = symtab_list; s; s = s->next)
2598 {
2599 found_in_file = 0;
2600 bv = BLOCKVECTOR (s);
2601 /* Often many files share a blockvector.
2602 Scan each blockvector only once so that
2603 we don't get every symbol many times.
2604 It happens that the first symtab in the list
2605 for any given blockvector is the main file. */
2606 if (bv != prev_bv)
3ba6a043 2607 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
bd5635a1
RP
2608 {
2609 b = BLOCKVECTOR_BLOCK (bv, i);
2610 /* Skip the sort if this block is always sorted. */
2611 if (!BLOCK_SHOULD_SORT (b))
2612 sort_block_syms (b);
2613 for (j = 0; j < BLOCK_NSYMS (b); j++)
2614 {
2615 QUIT;
2616 sym = BLOCK_SYM (b, j);
2617 if ((regexp == 0 || re_exec (SYMBOL_NAME (sym)))
2618 && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
997a978c 2619 && SYMBOL_CLASS (sym) != LOC_BLOCK)
bd5635a1
RP
2620 || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
2621 || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2622 || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK)))
2623 {
2624 if (bpt)
2625 {
2626 /* Set a breakpoint here, if it's a function */
2627 if (class == 1)
2628 break_command (SYMBOL_NAME(sym), 0);
2629 }
2630 else if (!found_in_file)
2631 {
2632 fputs_filtered ("\nFile ", stdout);
2633 fputs_filtered (s->filename, stdout);
2634 fputs_filtered (":\n", stdout);
2635 }
2636 found_in_file = 1;
2637
3ba6a043 2638 if (class != 2 && i == STATIC_BLOCK)
bd5635a1 2639 printf_filtered ("static ");
997a978c
JG
2640
2641 /* Typedef that is not a C++ class */
bd5635a1
RP
2642 if (class == 2
2643 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
dc9894c8 2644 typedef_print (SYMBOL_TYPE(sym), sym, stdout);
997a978c
JG
2645 /* variable, func, or typedef-that-is-c++-class */
2646 else if (class < 2 ||
2647 (class == 2 &&
2648 SYMBOL_NAMESPACE(sym) == STRUCT_NAMESPACE))
bd5635a1 2649 {
997a978c
JG
2650 type_print (SYMBOL_TYPE (sym),
2651 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
2652 ? "" : SYMBOL_NAME (sym)),
2653 stdout, 0);
2654
2655 printf_filtered (";\n");
bd5635a1
RP
2656 }
2657 else
2658 {
2659# if 0
2660 char buf[1024];
2661 type_print_base (TYPE_FN_FIELD_TYPE(t, i), stdout, 0, 0);
2662 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i), stdout, 0);
2663 sprintf (buf, " %s::", type_name_no_tag (t));
2664 type_print_method_args (TYPE_FN_FIELD_ARGS (t, i), buf, name, stdout);
2665# endif
2666 }
2667 }
2668 }
2669 }
2670 prev_bv = bv;
2671 }
997a978c
JG
2672
2673
2674 /* If there are no eyes, avoid all contact. I mean, if there are
2675 no debug symbols, then print directly from the misc_function_vector. */
2676
2677 if (found_misc || class != 1) {
2678 found_in_file = 0;
2679 for (i = 0; i < misc_function_count; i++) {
2680 if (misc_function_vector[i].type != ourtype
2681 && misc_function_vector[i].type != ourtype2)
2682 continue;
2683 if (regexp == 0 || re_exec (misc_function_vector[i].name)) {
2684 /* Functions: Look up by address. */
2685 if (class == 1)
2686 if (0 != find_pc_symtab (misc_function_vector[i].address))
2687 continue;
2688 /* Variables/Absolutes: Look up by name */
2689 if (0 != lookup_symbol (misc_function_vector[i].name,
2690 (struct block *)0, VAR_NAMESPACE, 0, (struct symtab **)0))
2691 continue;
2692 if (!found_in_file) {
2693 printf_filtered ("\nNon-debugging symbols:\n");
2694 found_in_file = 1;
2695 }
2696 printf_filtered (" %08x %s\n",
2697 misc_function_vector[i].address,
2698 misc_function_vector[i].name);
2699 }
2700 }
2701 }
bd5635a1
RP
2702}
2703
2704static void
2705variables_info (regexp)
2706 char *regexp;
2707{
2708 list_symbols (regexp, 0, 0);
2709}
2710
2711static void
2712functions_info (regexp)
2713 char *regexp;
2714{
2715 list_symbols (regexp, 1, 0);
2716}
2717
bd5635a1
RP
2718static void
2719types_info (regexp)
2720 char *regexp;
2721{
2722 list_symbols (regexp, 2, 0);
2723}
bd5635a1
RP
2724
2725#if 0
2726/* Tiemann says: "info methods was never implemented." */
2727static void
2728methods_info (regexp)
2729 char *regexp;
2730{
2731 list_symbols (regexp, 3, 0);
2732}
2733#endif /* 0 */
2734
2735/* Breakpoint all functions matching regular expression. */
2736static void
2737rbreak_command (regexp)
2738 char *regexp;
2739{
2740 list_symbols (regexp, 1, 1);
2741}
2742\f
997a978c 2743/* Helper function to initialize the standard scalar types. */
bd5635a1 2744
bd5635a1
RP
2745struct type *
2746init_type (code, length, uns, name)
2747 enum type_code code;
2748 int length, uns;
2749 char *name;
2750{
2751 register struct type *type;
2752
2753 type = (struct type *) xmalloc (sizeof (struct type));
2754 bzero (type, sizeof *type);
2755 TYPE_MAIN_VARIANT (type) = type;
2756 TYPE_CODE (type) = code;
2757 TYPE_LENGTH (type) = length;
2758 TYPE_FLAGS (type) = uns ? TYPE_FLAG_UNSIGNED : 0;
2759 TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
2760 TYPE_NFIELDS (type) = 0;
2761 TYPE_NAME (type) = name;
2762
2763 /* C++ fancies. */
2764 TYPE_NFN_FIELDS (type) = 0;
2765 TYPE_N_BASECLASSES (type) = 0;
2766 return type;
2767}
2768
2769/* Return Nonzero if block a is lexically nested within block b,
2770 or if a and b have the same pc range.
2771 Return zero otherwise. */
2772int
2773contained_in (a, b)
2774 struct block *a, *b;
2775{
2776 if (!a || !b)
2777 return 0;
2778 return BLOCK_START (a) >= BLOCK_START (b)
2779 && BLOCK_END (a) <= BLOCK_END (b);
2780}
2781
2782\f
2783/* Helper routine for make_symbol_completion_list. */
2784
2785int return_val_size, return_val_index;
2786char **return_val;
2787
2788void
2789completion_list_add_symbol (symname)
2790 char *symname;
2791{
2792 if (return_val_index + 3 > return_val_size)
2793 return_val =
2794 (char **)xrealloc (return_val,
2795 (return_val_size *= 2) * sizeof (char *));
2796
2797 return_val[return_val_index] =
2798 (char *)xmalloc (1 + strlen (symname));
2799
2800 strcpy (return_val[return_val_index], symname);
2801
2802 return_val[++return_val_index] = (char *)NULL;
2803}
2804
2805/* Return a NULL terminated array of all symbols (regardless of class) which
2806 begin by matching TEXT. If the answer is no symbols, then the return value
2807 is an array which contains only a NULL pointer.
2808
2809 Problem: All of the symbols have to be copied because readline
2810 frees them. I'm not going to worry about this; hopefully there
2811 won't be that many. */
2812
2813char **
2814make_symbol_completion_list (text)
2815 char *text;
2816{
2817 register struct symtab *s;
2818 register struct partial_symtab *ps;
2819 register struct block *b, *surrounding_static_block = 0;
2820 extern struct block *get_selected_block ();
2821 register int i, j;
2822 struct partial_symbol *psym;
2823
2824 int text_len = strlen (text);
2825 return_val_size = 100;
2826 return_val_index = 0;
2827 return_val =
2828 (char **)xmalloc ((1 + return_val_size) *sizeof (char *));
2829 return_val[0] = (char *)NULL;
2830
2831 /* Look through the partial symtabs for all symbols which begin
2832 by matching TEXT. Add each one that you find to the list. */
2833
2834 for (ps = partial_symtab_list; ps; ps = ps->next)
2835 {
2836 /* If the psymtab's been read in we'll get it when we search
2837 through the blockvector. */
2838 if (ps->readin) continue;
2839
2840 for (psym = global_psymbols.list + ps->globals_offset;
2841 psym < (global_psymbols.list + ps->globals_offset
2842 + ps->n_global_syms);
2843 psym++)
2844 {
2845 QUIT; /* If interrupted, then quit. */
2846 if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0))
2847 completion_list_add_symbol (SYMBOL_NAME (psym));
2848 }
2849
2850 for (psym = static_psymbols.list + ps->statics_offset;
2851 psym < (static_psymbols.list + ps->statics_offset
2852 + ps->n_static_syms);
2853 psym++)
2854 {
2855 QUIT;
2856 if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0))
2857 completion_list_add_symbol (SYMBOL_NAME (psym));
2858 }
2859 }
2860
2861 /* At this point scan through the misc function vector and add each
2862 symbol you find to the list. Eventually we want to ignore
2863 anything that isn't a text symbol (everything else will be
2864 handled by the psymtab code above). */
2865
2866 for (i = 0; i < misc_function_count; i++)
2867 if (!strncmp (text, misc_function_vector[i].name, text_len))
2868 completion_list_add_symbol (misc_function_vector[i].name);
2869
2870 /* Search upwards from currently selected frame (so that we can
2871 complete on local vars. */
2872 for (b = get_selected_block (); b; b = BLOCK_SUPERBLOCK (b))
2873 {
2874 if (!BLOCK_SUPERBLOCK (b))
2875 surrounding_static_block = b; /* For elmin of dups */
2876
2877 /* Also catch fields of types defined in this places which
2878 match our text string. Only complete on types visible
2879 from current context. */
2880 for (i = 0; i < BLOCK_NSYMS (b); i++)
2881 {
2882 register struct symbol *sym = BLOCK_SYM (b, i);
2883
2884 if (!strncmp (SYMBOL_NAME (sym), text, text_len))
2885 completion_list_add_symbol (SYMBOL_NAME (sym));
2886
2887 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2888 {
2889 struct type *t = SYMBOL_TYPE (sym);
2890 enum type_code c = TYPE_CODE (t);
2891
2892 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
2893 for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
2894 if (TYPE_FIELD_NAME (t, j) &&
2895 !strncmp (TYPE_FIELD_NAME (t, j), text, text_len))
2896 completion_list_add_symbol (TYPE_FIELD_NAME (t, j));
2897 }
2898 }
2899 }
2900
2901 /* Go through the symtabs and check the externs and statics for
2902 symbols which match. */
2903
2904 for (s = symtab_list; s; s = s->next)
2905 {
3ba6a043 2906 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
bd5635a1
RP
2907
2908 for (i = 0; i < BLOCK_NSYMS (b); i++)
2909 if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len))
2910 completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i)));
2911 }
2912
2913 for (s = symtab_list; s; s = s->next)
2914 {
3ba6a043 2915 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
bd5635a1
RP
2916
2917 /* Don't do this block twice. */
2918 if (b == surrounding_static_block) continue;
2919
2920 for (i = 0; i < BLOCK_NSYMS (b); i++)
2921 if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len))
2922 completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i)));
2923 }
2924
2925 return (return_val);
2926}
2927\f
997a978c
JG
2928#if 0
2929/* Add the type of the symbol sym to the type of the current
2930 function whose block we are in (assumed). The type of
2931 this current function is contained in *TYPE.
2932
2933 This basically works as follows: When we find a function
2934 symbol (N_FUNC with a 'f' or 'F' in the symbol name), we record
2935 a pointer to its type in the global in_function_type. Every
2936 time we come across a parameter symbol ('p' in its name), then
2937 this procedure adds the name and type of that parameter
2938 to the function type pointed to by *TYPE. (Which should correspond
2939 to in_function_type if it was called correctly).
2940
2941 Note that since we are modifying a type, the result of
2942 lookup_function_type() should be bcopy()ed before calling
2943 this. When not in strict typing mode, the expression
2944 evaluator can choose to ignore this.
2945
2946 Assumption: All of a function's parameter symbols will
2947 appear before another function symbol is found. The parameters
2948 appear in the same order in the argument list as they do in the
2949 symbol table. */
2950
2951void
2952add_param_to_type (type,sym)
2953 struct type **type;
2954 struct symbol *sym;
2955{
2956 int num = ++(TYPE_NFIELDS(*type));
2957
2958 if(TYPE_NFIELDS(*type)-1)
2959 TYPE_FIELDS(*type) =
2960 (struct field *)xrealloc((char *)(TYPE_FIELDS(*type)),
2961 num*sizeof(struct field));
2962 else
2963 TYPE_FIELDS(*type) =
2964 (struct field *)xmalloc(num*sizeof(struct field));
2965
2966 TYPE_FIELD_BITPOS(*type,num-1) = num-1;
2967 TYPE_FIELD_BITSIZE(*type,num-1) = 0;
2968 TYPE_FIELD_TYPE(*type,num-1) = SYMBOL_TYPE(sym);
2969 TYPE_FIELD_NAME(*type,num-1) = SYMBOL_NAME(sym);
2970}
2971#endif
2972\f
bd5635a1
RP
2973void
2974_initialize_symtab ()
2975{
2976 add_info ("variables", variables_info,
2977 "All global and static variable names, or those matching REGEXP.");
2978 add_info ("functions", functions_info,
2979 "All function names, or those matching REGEXP.");
3ba6a043
JG
2980
2981 /* FIXME: This command has at least the following problems:
bd5635a1
RP
2982 1. It prints builtin types (in a very strange and confusing fashion).
2983 2. It doesn't print right, e.g. with
2984 typedef struct foo *FOO
2985 type_print prints "FOO" when we want to make it (in this situation)
2986 print "struct foo *".
2987 I also think "ptype" or "whatis" is more likely to be useful (but if
2988 there is much disagreement "info types" can be fixed). */
2989 add_info ("types", types_info,
2990 "All types names, or those matching REGEXP.");
3ba6a043 2991
bd5635a1
RP
2992#if 0
2993 add_info ("methods", methods_info,
2994 "All method names, or those matching REGEXP::REGEXP.\n\
2995If the class qualifier is ommited, it is assumed to be the current scope.\n\
2996If the first REGEXP is ommited, then all methods matching the second REGEXP\n\
2997are listed.");
2998#endif
2999 add_info ("sources", sources_info,
3000 "Source files in the program.");
3001
3002 add_com ("rbreak", no_class, rbreak_command,
3003 "Set a breakpoint for all functions matching REGEXP.");
3004
997a978c 3005 /* Initialize the one built-in type that isn't language dependent... */
bd5635a1
RP
3006 builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>");
3007}
This page took 0.160042 seconds and 4 git commands to generate.