Various fixes to improve g++ debugging. See ChangeLog.
[binutils-gdb.git] / gdb / symtab.c
1 /* Symbol table lookup for the GNU debugger, GDB.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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"
30 #include "regex.h"
31 #include "language.h"
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
41 extern char *getenv ();
42
43 extern char *cplus_demangle ();
44 extern char *cplus_mangle_opname ();
45 extern struct value *value_of_this ();
46 extern void break_command ();
47 extern void select_source_symtab ();
48
49 /* Functions this file defines */
50 static int find_line_common ();
51 struct partial_symtab *lookup_partial_symtab ();
52 static struct partial_symbol *lookup_partial_symbol ();
53 static struct partial_symbol *lookup_demangled_partial_symbol ();
54 static struct symbol *lookup_demangled_block_symbol ();
55
56 /* The single non-language-specific builtin type */
57 struct 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. */
62 struct block *block_found;
63
64 char 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
70 static struct symtab *
71 lookup_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)
87 error ("Internal: readin pst for `%s' found when no symtab found.", name);
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)
110 error ("Internal: readin pst for `%s' found when no symtab found.", name);
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
121 struct symtab *
122 lookup_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
147 struct partial_symtab *
148 lookup_partial_symtab (name)
149 char *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. */
163 char *
164 type_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
196 don't waste too much time in future. (FIXME, this doesn't seem
197 to be happening...)
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
203 struct complaint stub_noname_complaint =
204 {"stub type has NULL name", 0, 0};
205
206 void
207 check_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 {
216 complain (&stub_noname_complaint, 0);
217 return;
218 }
219 sym = lookup_symbol (name, 0, STRUCT_NAMESPACE, 0,
220 (struct symtab **)NULL);
221 if (sym)
222 bcopy (SYMBOL_TYPE(sym), type, sizeof (struct type));
223 }
224 }
225
226 /* Demangle a GDB method stub type. */
227 char *
228 gdb_mangle_name (type, i, j)
229 struct type *type;
230 int i, j;
231 {
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);
237
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
265 {
266 mangled_name = (char *)xmalloc (mangled_name_len);
267 strcpy (mangled_name, TYPE_FN_FIELDLIST_NAME (type, i));
268 }
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;
274 }
275
276 /* Lookup a primitive type named NAME.
277 Return zero if NAME is not a primitive type.*/
278
279 struct type *
280 lookup_primitive_typename (name)
281 char *name;
282 {
283 struct type ** const *p;
284
285 for (p = current_language->la_builtin_type_vector; *p; p++)
286 if(!strcmp((**p)->name, name))
287 return **p;
288 return 0;
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
295 struct type *
296 lookup_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);
307 if(tmp)
308 return tmp;
309 else if (!tmp && noerr)
310 return 0;
311 else
312 error ("No type named %s.", name);
313 }
314 return SYMBOL_TYPE (sym);
315 }
316
317 struct type *
318 lookup_unsigned_typename (name)
319 char *name;
320 {
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);
326 }
327
328 /* Lookup a structure type named "struct NAME",
329 visible in lexical block BLOCK. */
330
331 struct type *
332 lookup_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
349 struct type *
350 lookup_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
367 struct type *
368 lookup_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
382 NAME.
383 If NOERR is nonzero, return zero if NAME is not suitably defined. */
384
385 struct type *
386 lookup_struct_elt_type (type, name, noerr)
387 struct type *type;
388 char *name;
389 int noerr;
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
403 check_stub_type (type);
404
405 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
406 {
407 char *t_field_name = TYPE_FIELD_NAME (type, i);
408
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
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
440 struct type *
441 lookup_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! */
462 /* FIXME: This confuses host<->target data representations, and is a
463 poor assumption besides. */
464 TYPE_LENGTH (ptype) = sizeof (char *);
465 TYPE_CODE (ptype) = TYPE_CODE_PTR;
466 return ptype;
467 }
468
469 struct type *
470 lookup_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
502 struct type *
503 lookup_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
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. */
561 struct type *
562 allocate_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
575 /* Lookup a method type belonging to class DOMAIN, returning type TYPE,
576 and taking a list of arguments ARGS.
577 If one is not found, allocate a new one. */
578
579 struct type *
580 lookup_method_type (domain, type, args)
581 struct type *domain, *type, **args;
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. */
650 struct type *
651 lookup_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
713 struct type *
714 lookup_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
748 struct type *
749 create_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));
755 struct type *range_type;
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));
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;
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
794 void
795 smash_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
811 void
812 smash_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
830 struct partial_symtab *
831 find_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. */
845 struct partial_symbol *
846 find_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
889 struct symbol *
890 lookup_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);
918 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
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
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
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);
984 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
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);
1028 /* If S is zero, there are no debug symbols for this file.
1029 Skip this stuff and check for matching static symbols below. */
1030 if (s)
1031 {
1032 bv = BLOCKVECTOR (s);
1033 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
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);
1061 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1062 sym = lookup_block_symbol (block, name, namespace);
1063 if (!sym)
1064 error ("Internal: global symbol `%s' found in psymtab but not in symtab", name);
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);
1077 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
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);
1093 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1094 sym = lookup_block_symbol (block, name, namespace);
1095 if (!sym)
1096 error ("Internal: static symbol `%s' found in psymtab but not in symtab", name);
1097 if (symtab != NULL)
1098 *symtab = s;
1099 return sym;
1100 }
1101
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)
1129 error ("Internal: mangled static symbol `%s' found in psymtab but not in symtab", name);
1130 if (symtab != NULL)
1131 *symtab = s;
1132 return sym;
1133 }
1134 }
1135
1136 if (symtab != NULL)
1137 *symtab = NULL;
1138 return 0;
1139 }
1140
1141 /* Look for a static demangled symbol in block BLOCK. */
1142
1143 static struct symbol *
1144 lookup_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
1178 static struct partial_symbol *
1179 lookup_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
1210 /* Look, in partial_symtab PST, for symbol NAME. Check the global
1211 symbols if GLOBAL, the static symbols if not */
1212
1213 static struct partial_symbol *
1214 lookup_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
1273 /* Look for a symbol in block BLOCK. */
1274
1275 struct symbol *
1276 lookup_block_symbol (block, name, namespace)
1277 register struct block *block;
1278 char *name;
1279 enum namespace namespace;
1280 {
1281 register int bot, top, inc;
1282 register struct symbol *sym, *parameter_sym;
1283
1284 top = BLOCK_NSYMS (block);
1285 bot = 0;
1286
1287 /* If the blocks's symbols were sorted, start with a binary search. */
1288
1289 if (BLOCK_SHOULD_SORT (block))
1290 {
1291 /* First, advance BOT to not far before
1292 the first symbol whose name is NAME. */
1293
1294 while (1)
1295 {
1296 inc = (top - bot + 1);
1297 /* No need to keep binary searching for the last few bits worth. */
1298 if (inc < 4)
1299 break;
1300 inc = (inc >> 1) + bot;
1301 sym = BLOCK_SYM (block, inc);
1302 if (SYMBOL_NAME (sym)[0] < name[0])
1303 bot = inc;
1304 else if (SYMBOL_NAME (sym)[0] > name[0])
1305 top = inc;
1306 else if (strcmp (SYMBOL_NAME (sym), name) < 0)
1307 bot = inc;
1308 else
1309 top = inc;
1310 }
1311
1312 /* Now scan forward until we run out of symbols,
1313 find one whose name is greater than NAME,
1314 or find one we want.
1315 If there is more than one symbol with the right name and namespace,
1316 we return the first one. dbxread.c is careful to make sure
1317 that if one is a register then it comes first. */
1318
1319 top = BLOCK_NSYMS (block);
1320 while (bot < top)
1321 {
1322 sym = BLOCK_SYM (block, bot);
1323 inc = SYMBOL_NAME (sym)[0] - name[0];
1324 if (inc == 0)
1325 inc = strcmp (SYMBOL_NAME (sym), name);
1326 if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
1327 return sym;
1328 if (inc > 0)
1329 return 0;
1330 bot++;
1331 }
1332 return 0;
1333 }
1334
1335 /* Here if block isn't sorted.
1336 This loop is equivalent to the loop above,
1337 but hacked greatly for speed.
1338
1339 Note that parameter symbols do not always show up last in the
1340 list; this loop makes sure to take anything else other than
1341 parameter symbols first; it only uses parameter symbols as a
1342 last resort. Note that this only takes up extra computation
1343 time on a match. */
1344
1345 parameter_sym = (struct symbol *) 0;
1346 top = BLOCK_NSYMS (block);
1347 inc = name[0];
1348 while (bot < top)
1349 {
1350 sym = BLOCK_SYM (block, bot);
1351 if (SYMBOL_NAME (sym)[0] == inc
1352 && !strcmp (SYMBOL_NAME (sym), name)
1353 && SYMBOL_NAMESPACE (sym) == namespace)
1354 {
1355 if (SYMBOL_CLASS (sym) == LOC_ARG
1356 || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG
1357 || SYMBOL_CLASS (sym) == LOC_REF_ARG
1358 || SYMBOL_CLASS (sym) == LOC_REGPARM)
1359 parameter_sym = sym;
1360 else
1361 return sym;
1362 }
1363 bot++;
1364 }
1365 return parameter_sym; /* Will be 0 if not found. */
1366 }
1367 \f
1368 /* Return the symbol for the function which contains a specified
1369 lexical block, described by a struct block BL. */
1370
1371 struct symbol *
1372 block_function (bl)
1373 struct block *bl;
1374 {
1375 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
1376 bl = BLOCK_SUPERBLOCK (bl);
1377
1378 return BLOCK_FUNCTION (bl);
1379 }
1380
1381 /* Subroutine of find_pc_line */
1382
1383 struct symtab *
1384 find_pc_symtab (pc)
1385 register CORE_ADDR pc;
1386 {
1387 register struct block *b;
1388 struct blockvector *bv;
1389 register struct symtab *s;
1390 register struct partial_symtab *ps;
1391
1392 /* Search all symtabs for one whose file contains our pc */
1393
1394 for (s = symtab_list; s; s = s->next)
1395 {
1396 bv = BLOCKVECTOR (s);
1397 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1398 if (BLOCK_START (b) <= pc
1399 && BLOCK_END (b) > pc)
1400 break;
1401 }
1402
1403 if (!s)
1404 {
1405 ps = find_pc_psymtab (pc);
1406 if (ps && ps->readin)
1407 printf_filtered (
1408 "(Internal error: pc 0x%x in read in psymtab, but not in symtab.)\n", pc);
1409
1410 if (ps)
1411 s = PSYMTAB_TO_SYMTAB (ps);
1412 }
1413
1414 return s;
1415 }
1416
1417 /* Find the source file and line number for a given PC value.
1418 Return a structure containing a symtab pointer, a line number,
1419 and a pc range for the entire source line.
1420 The value's .pc field is NOT the specified pc.
1421 NOTCURRENT nonzero means, if specified pc is on a line boundary,
1422 use the line that ends there. Otherwise, in that case, the line
1423 that begins there is used. */
1424
1425 struct symtab_and_line
1426 find_pc_line (pc, notcurrent)
1427 CORE_ADDR pc;
1428 int notcurrent;
1429 {
1430 struct symtab *s;
1431 register struct linetable *l;
1432 register int len;
1433 register int i;
1434 register struct linetable_entry *item;
1435 struct symtab_and_line val;
1436 struct blockvector *bv;
1437
1438 /* Info on best line seen so far, and where it starts, and its file. */
1439
1440 int best_line = 0;
1441 CORE_ADDR best_pc = 0;
1442 CORE_ADDR best_end = 0;
1443 struct symtab *best_symtab = 0;
1444
1445 /* Store here the first line number
1446 of a file which contains the line at the smallest pc after PC.
1447 If we don't find a line whose range contains PC,
1448 we will use a line one less than this,
1449 with a range from the start of that file to the first line's pc. */
1450 int alt_line = 0;
1451 CORE_ADDR alt_pc = 0;
1452 struct symtab *alt_symtab = 0;
1453
1454 /* Info on best line seen in this file. */
1455
1456 int prev_line;
1457 CORE_ADDR prev_pc;
1458
1459 /* Info on first line of this file. */
1460
1461 int first_line;
1462 CORE_ADDR first_pc;
1463
1464 /* If this pc is not from the current frame,
1465 it is the address of the end of a call instruction.
1466 Quite likely that is the start of the following statement.
1467 But what we want is the statement containing the instruction.
1468 Fudge the pc to make sure we get that. */
1469
1470 if (notcurrent) pc -= 1;
1471
1472 s = find_pc_symtab (pc);
1473 if (s == 0)
1474 {
1475 val.symtab = 0;
1476 val.line = 0;
1477 val.pc = pc;
1478 val.end = 0;
1479 return val;
1480 }
1481
1482 bv = BLOCKVECTOR (s);
1483
1484 /* Look at all the symtabs that share this blockvector.
1485 They all have the same apriori range, that we found was right;
1486 but they have different line tables. */
1487
1488 for (; s && BLOCKVECTOR (s) == bv; s = s->next)
1489 {
1490 /* Find the best line in this symtab. */
1491 l = LINETABLE (s);
1492 len = l->nitems;
1493 prev_line = -1;
1494 first_line = -1;
1495 for (i = 0; i < len; i++)
1496 {
1497 item = &(l->item[i]);
1498
1499 if (first_line < 0)
1500 {
1501 first_line = item->line;
1502 first_pc = item->pc;
1503 }
1504 /* Return the last line that did not start after PC. */
1505 if (pc >= item->pc)
1506 {
1507 prev_line = item->line;
1508 prev_pc = item->pc;
1509 }
1510 else
1511 break;
1512 }
1513
1514 /* Is this file's best line closer than the best in the other files?
1515 If so, record this file, and its best line, as best so far. */
1516 if (prev_line >= 0 && prev_pc > best_pc)
1517 {
1518 best_pc = prev_pc;
1519 best_line = prev_line;
1520 best_symtab = s;
1521 if (i < len)
1522 best_end = item->pc;
1523 else
1524 best_end = 0;
1525 }
1526 /* Is this file's first line closer than the first lines of other files?
1527 If so, record this file, and its first line, as best alternate. */
1528 if (first_line >= 0 && first_pc > pc
1529 && (alt_pc == 0 || first_pc < alt_pc))
1530 {
1531 alt_pc = first_pc;
1532 alt_line = first_line;
1533 alt_symtab = s;
1534 }
1535 }
1536 if (best_symtab == 0)
1537 {
1538 val.symtab = alt_symtab;
1539 val.line = alt_line - 1;
1540 val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
1541 val.end = alt_pc;
1542 }
1543 else
1544 {
1545 val.symtab = best_symtab;
1546 val.line = best_line;
1547 val.pc = best_pc;
1548 val.end = (best_end ? best_end
1549 : (alt_pc ? alt_pc
1550 : BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))));
1551 }
1552 return val;
1553 }
1554 \f
1555 /* Find the PC value for a given source file and line number.
1556 Returns zero for invalid line number.
1557 The source file is specified with a struct symtab. */
1558
1559 CORE_ADDR
1560 find_line_pc (symtab, line)
1561 struct symtab *symtab;
1562 int line;
1563 {
1564 register struct linetable *l;
1565 register int ind;
1566 int dummy;
1567
1568 if (symtab == 0)
1569 return 0;
1570 l = LINETABLE (symtab);
1571 ind = find_line_common(l, line, &dummy);
1572 return (ind >= 0) ? l->item[ind].pc : 0;
1573 }
1574
1575 /* Find the range of pc values in a line.
1576 Store the starting pc of the line into *STARTPTR
1577 and the ending pc (start of next line) into *ENDPTR.
1578 Returns 1 to indicate success.
1579 Returns 0 if could not find the specified line. */
1580
1581 int
1582 find_line_pc_range (symtab, thisline, startptr, endptr)
1583 struct symtab *symtab;
1584 int thisline;
1585 CORE_ADDR *startptr, *endptr;
1586 {
1587 register struct linetable *l;
1588 register int ind;
1589 int exact_match; /* did we get an exact linenumber match */
1590
1591 if (symtab == 0)
1592 return 0;
1593
1594 l = LINETABLE (symtab);
1595 ind = find_line_common (l, thisline, &exact_match);
1596 if (ind >= 0)
1597 {
1598 *startptr = l->item[ind].pc;
1599 /* If we have not seen an entry for the specified line,
1600 assume that means the specified line has zero bytes. */
1601 if (!exact_match || ind == l->nitems-1)
1602 *endptr = *startptr;
1603 else
1604 /* Perhaps the following entry is for the following line.
1605 It's worth a try. */
1606 if (ind+1 < l->nitems
1607 && l->item[ind+1].line == thisline + 1)
1608 *endptr = l->item[ind+1].pc;
1609 else
1610 *endptr = find_line_pc (symtab, thisline+1);
1611 return 1;
1612 }
1613
1614 return 0;
1615 }
1616
1617 /* Given a line table and a line number, return the index into the line
1618 table for the pc of the nearest line whose number is >= the specified one.
1619 Return -1 if none is found. The value is >= 0 if it is an index.
1620
1621 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
1622
1623 static int
1624 find_line_common (l, lineno, exact_match)
1625 register struct linetable *l;
1626 register int lineno;
1627 int *exact_match;
1628 {
1629 register int i;
1630 register int len;
1631
1632 /* BEST is the smallest linenumber > LINENO so far seen,
1633 or 0 if none has been seen so far.
1634 BEST_INDEX identifies the item for it. */
1635
1636 int best_index = -1;
1637 int best = 0;
1638
1639 if (lineno <= 0)
1640 return -1;
1641
1642 len = l->nitems;
1643 for (i = 0; i < len; i++)
1644 {
1645 register struct linetable_entry *item = &(l->item[i]);
1646
1647 if (item->line == lineno)
1648 {
1649 *exact_match = 1;
1650 return i;
1651 }
1652
1653 if (item->line > lineno && (best == 0 || item->line < best))
1654 {
1655 best = item->line;
1656 best_index = i;
1657 }
1658 }
1659
1660 /* If we got here, we didn't get an exact match. */
1661
1662 *exact_match = 0;
1663 return best_index;
1664 }
1665
1666 int
1667 find_pc_line_pc_range (pc, startptr, endptr)
1668 CORE_ADDR pc;
1669 CORE_ADDR *startptr, *endptr;
1670 {
1671 struct symtab_and_line sal;
1672 sal = find_pc_line (pc, 0);
1673 *startptr = sal.pc;
1674 *endptr = sal.end;
1675 return sal.symtab != 0;
1676 }
1677 \f
1678 /* If P is of the form "operator[ \t]+..." where `...' is
1679 some legitimate operator text, return a pointer to the
1680 beginning of the substring of the operator text.
1681 Otherwise, return "". */
1682 static char *
1683 operator_chars (p, end)
1684 char *p;
1685 char **end;
1686 {
1687 *end = "";
1688 if (strncmp (p, "operator", 8))
1689 return *end;
1690 p += 8;
1691
1692 /* Don't get faked out by `operator' being part of a longer
1693 identifier. */
1694 if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')
1695 || *p == '_' || *p == '$' || *p == '\0')
1696 return *end;
1697
1698 /* Allow some whitespace between `operator' and the operator symbol. */
1699 while (*p == ' ' || *p == '\t')
1700 p++;
1701
1702 switch (*p)
1703 {
1704 case '!':
1705 case '=':
1706 case '*':
1707 case '/':
1708 case '%':
1709 case '^':
1710 if (p[1] == '=')
1711 *end = p+2;
1712 else
1713 *end = p+1;
1714 return p;
1715 case '<':
1716 case '>':
1717 case '+':
1718 case '-':
1719 case '&':
1720 case '|':
1721 if (p[1] == '=' || p[1] == p[0])
1722 *end = p+2;
1723 else
1724 *end = p+1;
1725 return p;
1726 case '~':
1727 case ',':
1728 *end = p+1;
1729 return p;
1730 case '(':
1731 if (p[1] != ')')
1732 error ("`operator ()' must be specified without whitespace in `()'");
1733 *end = p+2;
1734 return p;
1735 case '?':
1736 if (p[1] != ':')
1737 error ("`operator ?:' must be specified without whitespace in `?:'");
1738 *end = p+2;
1739 return p;
1740 case '[':
1741 if (p[1] != ']')
1742 error ("`operator []' must be specified without whitespace in `[]'");
1743 *end = p+2;
1744 return p;
1745 default:
1746 error ("`operator %s' not supported", p);
1747 break;
1748 }
1749 *end = "";
1750 return *end;
1751 }
1752
1753 /* Recursive helper function for decode_line_1.
1754 * Look for methods named NAME in type T.
1755 * Return number of matches.
1756 * Put matches in PHYSNAMES and SYM_ARR (which better be big enough!).
1757 * These allocations seem to define "big enough":
1758 * sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
1759 * physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
1760 */
1761
1762 int
1763 find_methods(t, name, physnames, sym_arr)
1764 struct type *t;
1765 char *name;
1766 char **physnames;
1767 struct symbol **sym_arr;
1768 {
1769 int i1 = 0;
1770 int ibase;
1771 struct symbol *sym_class;
1772 char *class_name = type_name_no_tag (t);
1773 /* Ignore this class if it doesn't have a name.
1774 This prevents core dumps, but is just a workaround
1775 because we might not find the function in
1776 certain cases, such as
1777 struct D {virtual int f();}
1778 struct C : D {virtual int g();}
1779 (in this case g++ 1.35.1- does not put out a name
1780 for D as such, it defines type 19 (for example) in
1781 the same stab as C, and then does a
1782 .stabs "D:T19" and a .stabs "D:t19".
1783 Thus
1784 "break C::f" should not be looking for field f in
1785 the class named D,
1786 but just for the field f in the baseclasses of C
1787 (no matter what their names).
1788
1789 However, I don't know how to replace the code below
1790 that depends on knowing the name of D. */
1791 if (class_name
1792 && (sym_class = lookup_symbol (class_name,
1793 (struct block *)NULL,
1794 STRUCT_NAMESPACE,
1795 (int *)NULL,
1796 (struct symtab **)NULL)))
1797 {
1798 int method_counter;
1799 t = SYMBOL_TYPE (sym_class);
1800 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1801 method_counter >= 0;
1802 --method_counter)
1803 {
1804 int field_counter;
1805 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, method_counter);
1806
1807 char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
1808 if (!strcmp (name, method_name))
1809 /* Find all the fields with that name. */
1810 for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
1811 field_counter >= 0;
1812 --field_counter)
1813 {
1814 char *phys_name;
1815 if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, field_counter)) & TYPE_FLAG_STUB)
1816 check_stub_method (t, method_counter, field_counter);
1817 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1818 physnames[i1] = (char*) alloca (strlen (phys_name) + 1);
1819 strcpy (physnames[i1], phys_name);
1820 sym_arr[i1] = lookup_symbol (phys_name,
1821 SYMBOL_BLOCK_VALUE (sym_class),
1822 VAR_NAMESPACE,
1823 (int *) NULL,
1824 (struct symtab **) NULL);
1825 if (sym_arr[i1]) i1++;
1826 }
1827 }
1828 }
1829 /* Only search baseclasses if there is no match yet,
1830 * since names in derived classes override those in baseclasses.
1831 */
1832 if (i1)
1833 return i1;
1834 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
1835 i1 += find_methods(TYPE_BASECLASS(t, ibase), name,
1836 physnames + i1, sym_arr + i1);
1837 return i1;
1838 }
1839
1840 /* Parse a string that specifies a line number.
1841 Pass the address of a char * variable; that variable will be
1842 advanced over the characters actually parsed.
1843
1844 The string can be:
1845
1846 LINENUM -- that line number in current file. PC returned is 0.
1847 FILE:LINENUM -- that line in that file. PC returned is 0.
1848 FUNCTION -- line number of openbrace of that function.
1849 PC returned is the start of the function.
1850 VARIABLE -- line number of definition of that variable.
1851 PC returned is 0.
1852 FILE:FUNCTION -- likewise, but prefer functions in that file.
1853 *EXPR -- line in which address EXPR appears.
1854
1855 FUNCTION may be an undebuggable function found in misc_function_vector.
1856
1857 If the argument FUNFIRSTLINE is nonzero, we want the first line
1858 of real code inside a function when a function is specified.
1859
1860 DEFAULT_SYMTAB specifies the file to use if none is specified.
1861 It defaults to current_source_symtab.
1862 DEFAULT_LINE specifies the line number to use for relative
1863 line numbers (that start with signs). Defaults to current_source_line.
1864
1865 Note that it is possible to return zero for the symtab
1866 if no file is validly specified. Callers must check that.
1867 Also, the line number returned may be invalid. */
1868
1869 struct symtabs_and_lines
1870 decode_line_1 (argptr, funfirstline, default_symtab, default_line)
1871 char **argptr;
1872 int funfirstline;
1873 struct symtab *default_symtab;
1874 int default_line;
1875 {
1876 struct symtabs_and_lines decode_line_2 ();
1877 struct symtabs_and_lines values;
1878 struct symtab_and_line val;
1879 register char *p, *p1;
1880 char *q, *q1;
1881 register struct symtab *s;
1882
1883 register struct symbol *sym;
1884 /* The symtab that SYM was found in. */
1885 struct symtab *sym_symtab;
1886
1887 register CORE_ADDR pc;
1888 register int i;
1889 char *copy;
1890 struct symbol *sym_class;
1891 int i1;
1892 struct symbol **sym_arr;
1893 struct type *t;
1894 char **physnames;
1895
1896 /* Defaults have defaults. */
1897
1898 if (default_symtab == 0)
1899 {
1900 default_symtab = current_source_symtab;
1901 default_line = current_source_line;
1902 }
1903
1904 /* See if arg is *PC */
1905
1906 if (**argptr == '*')
1907 {
1908 (*argptr)++;
1909 pc = parse_and_eval_address_1 (argptr);
1910 values.sals = (struct symtab_and_line *)
1911 xmalloc (sizeof (struct symtab_and_line));
1912 values.nelts = 1;
1913 values.sals[0] = find_pc_line (pc, 0);
1914 values.sals[0].pc = pc;
1915 return values;
1916 }
1917
1918 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
1919
1920 s = 0;
1921
1922 for (p = *argptr; *p; p++)
1923 {
1924 if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
1925 break;
1926 }
1927 while (p[0] == ' ' || p[0] == '\t') p++;
1928
1929 q = operator_chars (*argptr, &q1);
1930 if (p[0] == ':')
1931 {
1932
1933 /* C++ */
1934 if (p[1] ==':')
1935 {
1936 /* Extract the class name. */
1937 p1 = p;
1938 while (p != *argptr && p[-1] == ' ') --p;
1939 copy = (char *) alloca (p - *argptr + 1);
1940 bcopy (*argptr, copy, p - *argptr);
1941 copy[p - *argptr] = 0;
1942
1943 /* Discard the class name from the arg. */
1944 p = p1 + 2;
1945 while (*p == ' ' || *p == '\t') p++;
1946 *argptr = p;
1947
1948 sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0,
1949 (struct symtab **)NULL);
1950
1951 if (sym_class &&
1952 (TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_STRUCT
1953 || TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_UNION))
1954 {
1955 /* Arg token is not digits => try it as a function name
1956 Find the next token (everything up to end or next whitespace). */
1957 p = *argptr;
1958 while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p !=':') p++;
1959 q = operator_chars (*argptr, &q1);
1960
1961 copy = (char *) alloca (p - *argptr + 1 + (q1 - q));
1962 if (q1 - q)
1963 {
1964 copy[0] = 'o';
1965 copy[1] = 'p';
1966 copy[2] = CPLUS_MARKER;
1967 bcopy (q, copy + 3, q1 - q);
1968 copy[3 + (q1 - q)] = '\0';
1969 p = q1;
1970 }
1971 else
1972 {
1973 bcopy (*argptr, copy, p - *argptr);
1974 copy[p - *argptr] = '\0';
1975 }
1976
1977 /* no line number may be specified */
1978 while (*p == ' ' || *p == '\t') p++;
1979 *argptr = p;
1980
1981 sym = 0;
1982 i1 = 0; /* counter for the symbol array */
1983 t = SYMBOL_TYPE (sym_class);
1984 sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
1985 physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
1986
1987 if (destructor_name_p (copy, t))
1988 {
1989 /* destructors are a special case. */
1990 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, 0);
1991 int len = TYPE_FN_FIELDLIST_LENGTH (t, 0) - 1;
1992 char *phys_name = TYPE_FN_FIELD_PHYSNAME (f, len);
1993 physnames[i1] = (char *)alloca (strlen (phys_name) + 1);
1994 strcpy (physnames[i1], phys_name);
1995 sym_arr[i1] =
1996 lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class),
1997 VAR_NAMESPACE, 0, (struct symtab **)NULL);
1998 if (sym_arr[i1]) i1++;
1999 }
2000 else
2001 i1 = find_methods (t, copy, physnames, sym_arr);
2002 if (i1 == 1)
2003 {
2004 /* There is exactly one field with that name. */
2005 sym = sym_arr[0];
2006
2007 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2008 {
2009 /* Arg is the name of a function */
2010 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
2011 if (funfirstline)
2012 SKIP_PROLOGUE (pc);
2013 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2014 values.nelts = 1;
2015 values.sals[0] = find_pc_line (pc, 0);
2016 values.sals[0].pc = (values.sals[0].end && values.sals[0].pc != pc) ? values.sals[0].end : pc;
2017 }
2018 else
2019 {
2020 values.nelts = 0;
2021 }
2022 return values;
2023 }
2024 if (i1 > 0)
2025 {
2026 /* There is more than one field with that name
2027 (overloaded). Ask the user which one to use. */
2028 return decode_line_2 (sym_arr, i1, funfirstline);
2029 }
2030 else
2031 {
2032 char *tmp;
2033
2034 if (OPNAME_PREFIX_P (copy))
2035 {
2036 tmp = (char *)alloca (strlen (copy+3) + 9);
2037 strcpy (tmp, "operator ");
2038 strcat (tmp, copy+3);
2039 }
2040 else
2041 tmp = copy;
2042 error ("that class does not have any method named %s", tmp);
2043 }
2044 }
2045 else
2046 /* The quotes are important if copy is empty. */
2047 error("No class, struct, or union named \"%s\"", copy );
2048 }
2049 /* end of C++ */
2050
2051
2052 /* Extract the file name. */
2053 p1 = p;
2054 while (p != *argptr && p[-1] == ' ') --p;
2055 copy = (char *) alloca (p - *argptr + 1 + (q1 - q));
2056 if (q1 - q)
2057 {
2058 copy[0] = 'o';
2059 copy[1] = 'p';
2060 copy[2] = CPLUS_MARKER;
2061 bcopy (q, copy + 3, q1-q);
2062 copy[3 + (q1-q)] = 0;
2063 p = q1;
2064 }
2065 else
2066 {
2067 bcopy (*argptr, copy, p - *argptr);
2068 copy[p - *argptr] = 0;
2069 }
2070
2071 /* Find that file's data. */
2072 s = lookup_symtab (copy);
2073 if (s == 0)
2074 {
2075 if (symtab_list == 0 && partial_symtab_list == 0)
2076 error (no_symtab_msg);
2077 error ("No source file named %s.", copy);
2078 }
2079
2080 /* Discard the file name from the arg. */
2081 p = p1 + 1;
2082 while (*p == ' ' || *p == '\t') p++;
2083 *argptr = p;
2084 }
2085
2086 /* S is specified file's symtab, or 0 if no file specified.
2087 arg no longer contains the file name. */
2088
2089 /* Check whether arg is all digits (and sign) */
2090
2091 p = *argptr;
2092 if (*p == '-' || *p == '+') p++;
2093 while (*p >= '0' && *p <= '9')
2094 p++;
2095
2096 if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ','))
2097 {
2098 /* We found a token consisting of all digits -- at least one digit. */
2099 enum sign {none, plus, minus} sign = none;
2100
2101 /* This is where we need to make sure that we have good defaults.
2102 We must guarantee that this section of code is never executed
2103 when we are called with just a function name, since
2104 select_source_symtab calls us with such an argument */
2105
2106 if (s == 0 && default_symtab == 0)
2107 {
2108 select_source_symtab (0);
2109 default_symtab = current_source_symtab;
2110 default_line = current_source_line;
2111 }
2112
2113 if (**argptr == '+')
2114 sign = plus, (*argptr)++;
2115 else if (**argptr == '-')
2116 sign = minus, (*argptr)++;
2117 val.line = atoi (*argptr);
2118 switch (sign)
2119 {
2120 case plus:
2121 if (p == *argptr)
2122 val.line = 5;
2123 if (s == 0)
2124 val.line = default_line + val.line;
2125 break;
2126 case minus:
2127 if (p == *argptr)
2128 val.line = 15;
2129 if (s == 0)
2130 val.line = default_line - val.line;
2131 else
2132 val.line = 1;
2133 break;
2134 case none:
2135 break; /* No need to adjust val.line. */
2136 }
2137
2138 while (*p == ' ' || *p == '\t') p++;
2139 *argptr = p;
2140 if (s == 0)
2141 s = default_symtab;
2142 val.symtab = s;
2143 val.pc = 0;
2144 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2145 values.sals[0] = val;
2146 values.nelts = 1;
2147 return values;
2148 }
2149
2150 /* Arg token is not digits => try it as a variable name
2151 Find the next token (everything up to end or next whitespace). */
2152 p = *argptr;
2153 while (*p && *p != ' ' && *p != '\t' && *p != ',') p++;
2154 copy = (char *) alloca (p - *argptr + 1);
2155 bcopy (*argptr, copy, p - *argptr);
2156 copy[p - *argptr] = 0;
2157 while (*p == ' ' || *p == '\t') p++;
2158 *argptr = p;
2159
2160 /* Look up that token as a variable.
2161 If file specified, use that file's per-file block to start with. */
2162
2163 sym = lookup_symbol (copy,
2164 (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
2165 : get_selected_block ()),
2166 VAR_NAMESPACE, 0, &sym_symtab);
2167
2168 if (sym != NULL)
2169 {
2170 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
2171 {
2172 /* Arg is the name of a function */
2173 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
2174 if (funfirstline)
2175 SKIP_PROLOGUE (pc);
2176 val = find_pc_line (pc, 0);
2177 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
2178 /* Convex: no need to suppress code on first line, if any */
2179 val.pc = pc;
2180 #else
2181 val.pc = (val.end && val.pc != pc) ? val.end : pc;
2182 #endif
2183 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2184 values.sals[0] = val;
2185 values.nelts = 1;
2186
2187 /* I think this is always the same as the line that
2188 we calculate above, but the general principle is
2189 "trust the symbols more than stuff like
2190 SKIP_PROLOGUE". */
2191 if (SYMBOL_LINE (sym) != 0)
2192 values.sals[0].line = SYMBOL_LINE (sym);
2193
2194 return values;
2195 }
2196 else if (SYMBOL_LINE (sym) != 0)
2197 {
2198 /* We know its line number. */
2199 values.sals = (struct symtab_and_line *)
2200 xmalloc (sizeof (struct symtab_and_line));
2201 values.nelts = 1;
2202 bzero (&values.sals[0], sizeof (values.sals[0]));
2203 values.sals[0].symtab = sym_symtab;
2204 values.sals[0].line = SYMBOL_LINE (sym);
2205 return values;
2206 }
2207 else
2208 /* This can happen if it is compiled with a compiler which doesn't
2209 put out line numbers for variables. */
2210 error ("Line number not known for symbol \"%s\"", copy);
2211 }
2212
2213 if ((i = lookup_misc_func (copy)) >= 0)
2214 {
2215 val.symtab = 0;
2216 val.line = 0;
2217 val.pc = misc_function_vector[i].address + FUNCTION_START_OFFSET;
2218 if (funfirstline)
2219 SKIP_PROLOGUE (val.pc);
2220 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2221 values.sals[0] = val;
2222 values.nelts = 1;
2223 return values;
2224 }
2225
2226 if (symtab_list == 0 && partial_symtab_list == 0 && misc_function_count == 0)
2227 error (no_symtab_msg);
2228
2229 error ("Function %s not defined.", copy);
2230 return values; /* for lint */
2231 }
2232
2233 struct symtabs_and_lines
2234 decode_line_spec (string, funfirstline)
2235 char *string;
2236 int funfirstline;
2237 {
2238 struct symtabs_and_lines sals;
2239 if (string == 0)
2240 error ("Empty line specification.");
2241 sals = decode_line_1 (&string, funfirstline,
2242 current_source_symtab, current_source_line);
2243 if (*string)
2244 error ("Junk at end of line specification: %s", string);
2245 return sals;
2246 }
2247
2248 /* Given a list of NELTS symbols in sym_arr (with corresponding
2249 mangled names in physnames), return a list of lines to operate on
2250 (ask user if necessary). */
2251 struct symtabs_and_lines
2252 decode_line_2 (sym_arr, nelts, funfirstline)
2253 struct symbol *sym_arr[];
2254 int nelts;
2255 int funfirstline;
2256 {
2257 struct symtabs_and_lines values, return_values;
2258 register CORE_ADDR pc;
2259 char *args, *arg1, *command_line_input ();
2260 int i;
2261 char *prompt;
2262
2263 values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line));
2264 return_values.sals = (struct symtab_and_line *) xmalloc (nelts * sizeof(struct symtab_and_line));
2265
2266 i = 0;
2267 printf("[0] cancel\n[1] all\n");
2268 while (i < nelts)
2269 {
2270 if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
2271 {
2272 /* Arg is the name of a function */
2273 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr[i]))
2274 + FUNCTION_START_OFFSET;
2275 if (funfirstline)
2276 SKIP_PROLOGUE (pc);
2277 values.sals[i] = find_pc_line (pc, 0);
2278 values.sals[i].pc = (values.sals[i].end && values.sals[i].pc != pc) ?
2279 values.sals[i].end : pc;
2280 printf("[%d] file:%s; line number:%d\n",
2281 (i+2), values.sals[i].symtab->filename, values.sals[i].line);
2282 }
2283 else printf ("?HERE\n");
2284 i++;
2285 }
2286
2287 if ((prompt = getenv ("PS2")) == NULL)
2288 {
2289 prompt = ">";
2290 }
2291 printf("%s ",prompt);
2292 fflush(stdout);
2293
2294 args = command_line_input (0, 0);
2295
2296 if (args == 0)
2297 error_no_arg ("one or more choice numbers");
2298
2299 i = 0;
2300 while (*args)
2301 {
2302 int num;
2303
2304 arg1 = args;
2305 while (*arg1 >= '0' && *arg1 <= '9') arg1++;
2306 if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
2307 error ("Arguments must be choice numbers.");
2308
2309 num = atoi (args);
2310
2311 if (num == 0)
2312 error ("cancelled");
2313 else if (num == 1)
2314 {
2315 bcopy (values.sals, return_values.sals, (nelts * sizeof(struct symtab_and_line)));
2316 return_values.nelts = nelts;
2317 return return_values;
2318 }
2319
2320 if (num > nelts + 2)
2321 {
2322 printf ("No choice number %d.\n", num);
2323 }
2324 else
2325 {
2326 num -= 2;
2327 if (values.sals[num].pc)
2328 {
2329 return_values.sals[i++] = values.sals[num];
2330 values.sals[num].pc = 0;
2331 }
2332 else
2333 {
2334 printf ("duplicate request for %d ignored.\n", num);
2335 }
2336 }
2337
2338 args = arg1;
2339 while (*args == ' ' || *args == '\t') args++;
2340 }
2341 return_values.nelts = i;
2342 return return_values;
2343 }
2344
2345 /* Return the index of misc function named NAME. */
2346
2347 int
2348 lookup_misc_func (name)
2349 register char *name;
2350 {
2351 register int i;
2352
2353 for (i = 0; i < misc_function_count; i++)
2354 if (!strcmp (misc_function_vector[i].name, name))
2355 return i;
2356 return -1; /* not found */
2357 }
2358 \f
2359 /* Slave routine for sources_info. Force line breaks at ,'s.
2360 NAME is the name to print and *FIRST is nonzero if this is the first
2361 name printed. Set *FIRST to zero. */
2362 static void
2363 output_source_filename (name, first)
2364 char *name;
2365 int *first;
2366 {
2367 static int column;
2368 /* Table of files printed so far. Since a single source file can
2369 result in several partial symbol tables, we need to avoid printing
2370 it more than once. Note: if some of the psymtabs are read in and
2371 some are not, it gets printed both under "Source files for which
2372 symbols have been read" and "Source files for which symbols will
2373 be read in on demand". I consider this a reasonable way to deal
2374 with the situation. I'm not sure whether this can also happen for
2375 symtabs; it doesn't hurt to check. */
2376 static char **tab = NULL;
2377 /* Allocated size of tab in elements.
2378 Start with one 256-byte block (when using GNU malloc.c).
2379 24 is the malloc overhead when range checking is in effect. */
2380 static int tab_alloc_size = (256 - 24) / sizeof (char *);
2381 /* Current size of tab in elements. */
2382 static int tab_cur_size;
2383
2384 char **p;
2385
2386 if (*first)
2387 {
2388 if (tab == NULL)
2389 tab = (char **) xmalloc (tab_alloc_size * sizeof (*tab));
2390 tab_cur_size = 0;
2391 }
2392
2393 /* Is NAME in tab? */
2394 for (p = tab; p < tab + tab_cur_size; p++)
2395 if (strcmp (*p, name) == 0)
2396 /* Yes; don't print it again. */
2397 return;
2398 /* No; add it to tab. */
2399 if (tab_cur_size == tab_alloc_size)
2400 {
2401 tab_alloc_size *= 2;
2402 tab = (char **) xrealloc (tab, tab_alloc_size * sizeof (*tab));
2403 }
2404 tab[tab_cur_size++] = name;
2405
2406 if (*first)
2407 {
2408 column = 0;
2409 *first = 0;
2410 }
2411 else
2412 {
2413 printf_filtered (",");
2414 column++;
2415 }
2416
2417 if (column != 0 && column + strlen (name) >= 70)
2418 {
2419 printf_filtered ("\n");
2420 column = 0;
2421 }
2422 else if (column != 0)
2423 {
2424 printf_filtered (" ");
2425 column++;
2426 }
2427 fputs_filtered (name, stdout);
2428 column += strlen (name);
2429 }
2430
2431 static void
2432 sources_info ()
2433 {
2434 register struct symtab *s;
2435 register struct partial_symtab *ps;
2436 int first;
2437
2438 if (symtab_list == 0 && partial_symtab_list == 0)
2439 {
2440 printf (no_symtab_msg);
2441 return;
2442 }
2443
2444 printf_filtered ("Source files for which symbols have been read in:\n\n");
2445
2446 first = 1;
2447 for (s = symtab_list; s; s = s->next)
2448 output_source_filename (s->filename, &first);
2449 printf_filtered ("\n\n");
2450
2451 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2452
2453 first = 1;
2454 for (ps = partial_symtab_list; ps; ps = ps->next)
2455 if (!ps->readin)
2456 output_source_filename (ps->filename, &first);
2457 printf_filtered ("\n");
2458 }
2459
2460 /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
2461 If CLASS is zero, list all symbols except functions and type names.
2462 If CLASS is 1, list only functions.
2463 If CLASS is 2, list only type names.
2464 If CLASS is 3, list only method names.
2465
2466 BPT is non-zero if we should set a breakpoint at the functions
2467 we find. */
2468
2469 static void
2470 list_symbols (regexp, class, bpt)
2471 char *regexp;
2472 int class;
2473 int bpt;
2474 {
2475 register struct symtab *s;
2476 register struct partial_symtab *ps;
2477 register struct blockvector *bv;
2478 struct blockvector *prev_bv = 0;
2479 register struct block *b;
2480 register int i, j;
2481 register struct symbol *sym;
2482 struct partial_symbol *psym;
2483 char *val;
2484 static char *classnames[]
2485 = {"variable", "function", "type", "method"};
2486 int found_in_file = 0;
2487 int found_misc = 0;
2488 static enum misc_function_type types[]
2489 = {mf_data, mf_text, mf_abs, mf_unknown};
2490 static enum misc_function_type types2[]
2491 = {mf_bss, mf_text, mf_abs, mf_unknown};
2492 enum misc_function_type ourtype = types[class];
2493 enum misc_function_type ourtype2 = types2[class];
2494
2495 if (regexp)
2496 if (0 != (val = re_comp (regexp)))
2497 error ("Invalid regexp (%s): %s", val, regexp);
2498
2499 /* Search through the partial_symtab_list *first* for all symbols
2500 matching the regexp. That way we don't have to reproduce all of
2501 the machinery below. */
2502 for (ps = partial_symtab_list; ps; ps = ps->next)
2503 {
2504 struct partial_symbol *bound, *gbound, *sbound;
2505 int keep_going = 1;
2506
2507 if (ps->readin) continue;
2508
2509 gbound = global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2510 sbound = static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2511 bound = gbound;
2512
2513 /* Go through all of the symbols stored in a partial
2514 symtab in one loop. */
2515 psym = global_psymbols.list + ps->globals_offset;
2516 while (keep_going)
2517 {
2518 if (psym >= bound)
2519 {
2520 if (bound == gbound && ps->n_static_syms != 0)
2521 {
2522 psym = static_psymbols.list + ps->statics_offset;
2523 bound = sbound;
2524 }
2525 else
2526 keep_going = 0;
2527 continue;
2528 }
2529 else
2530 {
2531 QUIT;
2532
2533 /* If it would match (logic taken from loop below)
2534 load the file and go on to the next one */
2535 if ((regexp == 0 || re_exec (SYMBOL_NAME (psym)))
2536 && ((class == 0 && SYMBOL_CLASS (psym) != LOC_TYPEDEF
2537 && SYMBOL_CLASS (psym) != LOC_BLOCK)
2538 || (class == 1 && SYMBOL_CLASS (psym) == LOC_BLOCK)
2539 || (class == 2 && SYMBOL_CLASS (psym) == LOC_TYPEDEF)
2540 || (class == 3 && SYMBOL_CLASS (psym) == LOC_BLOCK)))
2541 {
2542 (void) PSYMTAB_TO_SYMTAB(ps);
2543 keep_going = 0;
2544 }
2545 }
2546 psym++;
2547 }
2548 }
2549
2550 /* Here, we search through the misc function vector for functions that
2551 match, and call find_pc_symtab on them to force their symbols to
2552 be read. The symbol will then be found during the scan of symtabs
2553 below. If find_pc_symtab fails, set found_misc so that we will
2554 rescan to print any matching symbols without debug info. */
2555
2556 if (class == 1) {
2557 for (i = 0; i < misc_function_count; i++) {
2558 if (misc_function_vector[i].type != ourtype
2559 && misc_function_vector[i].type != ourtype2)
2560 continue;
2561 if (regexp == 0 || re_exec (misc_function_vector[i].name)) {
2562 if (0 == find_pc_symtab (misc_function_vector[i].address))
2563 found_misc = 1;
2564 }
2565 }
2566 }
2567
2568 /* Printout here so as to get after the "Reading in symbols"
2569 messages which will be generated above. */
2570 if (!bpt)
2571 printf_filtered (regexp
2572 ? "All %ss matching regular expression \"%s\":\n"
2573 : "All defined %ss:\n",
2574 classnames[class],
2575 regexp);
2576
2577 for (s = symtab_list; s; s = s->next)
2578 {
2579 found_in_file = 0;
2580 bv = BLOCKVECTOR (s);
2581 /* Often many files share a blockvector.
2582 Scan each blockvector only once so that
2583 we don't get every symbol many times.
2584 It happens that the first symtab in the list
2585 for any given blockvector is the main file. */
2586 if (bv != prev_bv)
2587 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
2588 {
2589 b = BLOCKVECTOR_BLOCK (bv, i);
2590 /* Skip the sort if this block is always sorted. */
2591 if (!BLOCK_SHOULD_SORT (b))
2592 sort_block_syms (b);
2593 for (j = 0; j < BLOCK_NSYMS (b); j++)
2594 {
2595 QUIT;
2596 sym = BLOCK_SYM (b, j);
2597 if ((regexp == 0 || re_exec (SYMBOL_NAME (sym)))
2598 && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
2599 && SYMBOL_CLASS (sym) != LOC_BLOCK)
2600 || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
2601 || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2602 || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK)))
2603 {
2604 if (bpt)
2605 {
2606 /* Set a breakpoint here, if it's a function */
2607 if (class == 1)
2608 break_command (SYMBOL_NAME(sym), 0);
2609 }
2610 else if (!found_in_file)
2611 {
2612 fputs_filtered ("\nFile ", stdout);
2613 fputs_filtered (s->filename, stdout);
2614 fputs_filtered (":\n", stdout);
2615 }
2616 found_in_file = 1;
2617
2618 if (class != 2 && i == STATIC_BLOCK)
2619 printf_filtered ("static ");
2620
2621 /* Typedef that is not a C++ class */
2622 if (class == 2
2623 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
2624 typedef_print (SYMBOL_TYPE(sym), sym, stdout);
2625 /* variable, func, or typedef-that-is-c++-class */
2626 else if (class < 2 ||
2627 (class == 2 &&
2628 SYMBOL_NAMESPACE(sym) == STRUCT_NAMESPACE))
2629 {
2630 type_print (SYMBOL_TYPE (sym),
2631 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
2632 ? "" : SYMBOL_NAME (sym)),
2633 stdout, 0);
2634
2635 printf_filtered (";\n");
2636 }
2637 else
2638 {
2639 # if 0
2640 char buf[1024];
2641 type_print_base (TYPE_FN_FIELD_TYPE(t, i), stdout, 0, 0);
2642 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i), stdout, 0);
2643 sprintf (buf, " %s::", type_name_no_tag (t));
2644 type_print_method_args (TYPE_FN_FIELD_ARGS (t, i), buf, name, stdout);
2645 # endif
2646 }
2647 }
2648 }
2649 }
2650 prev_bv = bv;
2651 }
2652
2653
2654 /* If there are no eyes, avoid all contact. I mean, if there are
2655 no debug symbols, then print directly from the misc_function_vector. */
2656
2657 if (found_misc || class != 1) {
2658 found_in_file = 0;
2659 for (i = 0; i < misc_function_count; i++) {
2660 if (misc_function_vector[i].type != ourtype
2661 && misc_function_vector[i].type != ourtype2)
2662 continue;
2663 if (regexp == 0 || re_exec (misc_function_vector[i].name)) {
2664 /* Functions: Look up by address. */
2665 if (class == 1)
2666 if (0 != find_pc_symtab (misc_function_vector[i].address))
2667 continue;
2668 /* Variables/Absolutes: Look up by name */
2669 if (0 != lookup_symbol (misc_function_vector[i].name,
2670 (struct block *)0, VAR_NAMESPACE, 0, (struct symtab **)0))
2671 continue;
2672 if (!found_in_file) {
2673 printf_filtered ("\nNon-debugging symbols:\n");
2674 found_in_file = 1;
2675 }
2676 printf_filtered (" %08x %s\n",
2677 misc_function_vector[i].address,
2678 misc_function_vector[i].name);
2679 }
2680 }
2681 }
2682 }
2683
2684 static void
2685 variables_info (regexp)
2686 char *regexp;
2687 {
2688 list_symbols (regexp, 0, 0);
2689 }
2690
2691 static void
2692 functions_info (regexp)
2693 char *regexp;
2694 {
2695 list_symbols (regexp, 1, 0);
2696 }
2697
2698 static void
2699 types_info (regexp)
2700 char *regexp;
2701 {
2702 list_symbols (regexp, 2, 0);
2703 }
2704
2705 #if 0
2706 /* Tiemann says: "info methods was never implemented." */
2707 static void
2708 methods_info (regexp)
2709 char *regexp;
2710 {
2711 list_symbols (regexp, 3, 0);
2712 }
2713 #endif /* 0 */
2714
2715 /* Breakpoint all functions matching regular expression. */
2716 static void
2717 rbreak_command (regexp)
2718 char *regexp;
2719 {
2720 list_symbols (regexp, 1, 1);
2721 }
2722 \f
2723 /* Helper function to initialize the standard scalar types. */
2724
2725 struct type *
2726 init_type (code, length, uns, name)
2727 enum type_code code;
2728 int length, uns;
2729 char *name;
2730 {
2731 register struct type *type;
2732
2733 type = (struct type *) xmalloc (sizeof (struct type));
2734 bzero (type, sizeof *type);
2735 TYPE_MAIN_VARIANT (type) = type;
2736 TYPE_CODE (type) = code;
2737 TYPE_LENGTH (type) = length;
2738 TYPE_FLAGS (type) = uns ? TYPE_FLAG_UNSIGNED : 0;
2739 TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
2740 TYPE_NFIELDS (type) = 0;
2741 TYPE_NAME (type) = name;
2742
2743 /* C++ fancies. */
2744 TYPE_NFN_FIELDS (type) = 0;
2745 TYPE_N_BASECLASSES (type) = 0;
2746 return type;
2747 }
2748
2749 /* Return Nonzero if block a is lexically nested within block b,
2750 or if a and b have the same pc range.
2751 Return zero otherwise. */
2752 int
2753 contained_in (a, b)
2754 struct block *a, *b;
2755 {
2756 if (!a || !b)
2757 return 0;
2758 return BLOCK_START (a) >= BLOCK_START (b)
2759 && BLOCK_END (a) <= BLOCK_END (b);
2760 }
2761
2762 \f
2763 /* Helper routine for make_symbol_completion_list. */
2764
2765 int return_val_size, return_val_index;
2766 char **return_val;
2767
2768 void
2769 completion_list_add_symbol (symname)
2770 char *symname;
2771 {
2772 if (return_val_index + 3 > return_val_size)
2773 return_val =
2774 (char **)xrealloc (return_val,
2775 (return_val_size *= 2) * sizeof (char *));
2776
2777 return_val[return_val_index] =
2778 (char *)xmalloc (1 + strlen (symname));
2779
2780 strcpy (return_val[return_val_index], symname);
2781
2782 return_val[++return_val_index] = (char *)NULL;
2783 }
2784
2785 /* Return a NULL terminated array of all symbols (regardless of class) which
2786 begin by matching TEXT. If the answer is no symbols, then the return value
2787 is an array which contains only a NULL pointer.
2788
2789 Problem: All of the symbols have to be copied because readline
2790 frees them. I'm not going to worry about this; hopefully there
2791 won't be that many. */
2792
2793 char **
2794 make_symbol_completion_list (text)
2795 char *text;
2796 {
2797 register struct symtab *s;
2798 register struct partial_symtab *ps;
2799 register struct block *b, *surrounding_static_block = 0;
2800 extern struct block *get_selected_block ();
2801 register int i, j;
2802 struct partial_symbol *psym;
2803
2804 int text_len = strlen (text);
2805 return_val_size = 100;
2806 return_val_index = 0;
2807 return_val =
2808 (char **)xmalloc ((1 + return_val_size) *sizeof (char *));
2809 return_val[0] = (char *)NULL;
2810
2811 /* Look through the partial symtabs for all symbols which begin
2812 by matching TEXT. Add each one that you find to the list. */
2813
2814 for (ps = partial_symtab_list; ps; ps = ps->next)
2815 {
2816 /* If the psymtab's been read in we'll get it when we search
2817 through the blockvector. */
2818 if (ps->readin) continue;
2819
2820 for (psym = global_psymbols.list + ps->globals_offset;
2821 psym < (global_psymbols.list + ps->globals_offset
2822 + ps->n_global_syms);
2823 psym++)
2824 {
2825 QUIT; /* If interrupted, then quit. */
2826 if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0))
2827 completion_list_add_symbol (SYMBOL_NAME (psym));
2828 }
2829
2830 for (psym = static_psymbols.list + ps->statics_offset;
2831 psym < (static_psymbols.list + ps->statics_offset
2832 + ps->n_static_syms);
2833 psym++)
2834 {
2835 QUIT;
2836 if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0))
2837 completion_list_add_symbol (SYMBOL_NAME (psym));
2838 }
2839 }
2840
2841 /* At this point scan through the misc function vector and add each
2842 symbol you find to the list. Eventually we want to ignore
2843 anything that isn't a text symbol (everything else will be
2844 handled by the psymtab code above). */
2845
2846 for (i = 0; i < misc_function_count; i++)
2847 if (!strncmp (text, misc_function_vector[i].name, text_len))
2848 completion_list_add_symbol (misc_function_vector[i].name);
2849
2850 /* Search upwards from currently selected frame (so that we can
2851 complete on local vars. */
2852 for (b = get_selected_block (); b; b = BLOCK_SUPERBLOCK (b))
2853 {
2854 if (!BLOCK_SUPERBLOCK (b))
2855 surrounding_static_block = b; /* For elmin of dups */
2856
2857 /* Also catch fields of types defined in this places which
2858 match our text string. Only complete on types visible
2859 from current context. */
2860 for (i = 0; i < BLOCK_NSYMS (b); i++)
2861 {
2862 register struct symbol *sym = BLOCK_SYM (b, i);
2863
2864 if (!strncmp (SYMBOL_NAME (sym), text, text_len))
2865 completion_list_add_symbol (SYMBOL_NAME (sym));
2866
2867 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2868 {
2869 struct type *t = SYMBOL_TYPE (sym);
2870 enum type_code c = TYPE_CODE (t);
2871
2872 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
2873 for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
2874 if (TYPE_FIELD_NAME (t, j) &&
2875 !strncmp (TYPE_FIELD_NAME (t, j), text, text_len))
2876 completion_list_add_symbol (TYPE_FIELD_NAME (t, j));
2877 }
2878 }
2879 }
2880
2881 /* Go through the symtabs and check the externs and statics for
2882 symbols which match. */
2883
2884 for (s = symtab_list; s; s = s->next)
2885 {
2886 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
2887
2888 for (i = 0; i < BLOCK_NSYMS (b); i++)
2889 if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len))
2890 completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i)));
2891 }
2892
2893 for (s = symtab_list; s; s = s->next)
2894 {
2895 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
2896
2897 /* Don't do this block twice. */
2898 if (b == surrounding_static_block) continue;
2899
2900 for (i = 0; i < BLOCK_NSYMS (b); i++)
2901 if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len))
2902 completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i)));
2903 }
2904
2905 return (return_val);
2906 }
2907 \f
2908 #if 0
2909 /* Add the type of the symbol sym to the type of the current
2910 function whose block we are in (assumed). The type of
2911 this current function is contained in *TYPE.
2912
2913 This basically works as follows: When we find a function
2914 symbol (N_FUNC with a 'f' or 'F' in the symbol name), we record
2915 a pointer to its type in the global in_function_type. Every
2916 time we come across a parameter symbol ('p' in its name), then
2917 this procedure adds the name and type of that parameter
2918 to the function type pointed to by *TYPE. (Which should correspond
2919 to in_function_type if it was called correctly).
2920
2921 Note that since we are modifying a type, the result of
2922 lookup_function_type() should be bcopy()ed before calling
2923 this. When not in strict typing mode, the expression
2924 evaluator can choose to ignore this.
2925
2926 Assumption: All of a function's parameter symbols will
2927 appear before another function symbol is found. The parameters
2928 appear in the same order in the argument list as they do in the
2929 symbol table. */
2930
2931 void
2932 add_param_to_type (type,sym)
2933 struct type **type;
2934 struct symbol *sym;
2935 {
2936 int num = ++(TYPE_NFIELDS(*type));
2937
2938 if(TYPE_NFIELDS(*type)-1)
2939 TYPE_FIELDS(*type) =
2940 (struct field *)xrealloc((char *)(TYPE_FIELDS(*type)),
2941 num*sizeof(struct field));
2942 else
2943 TYPE_FIELDS(*type) =
2944 (struct field *)xmalloc(num*sizeof(struct field));
2945
2946 TYPE_FIELD_BITPOS(*type,num-1) = num-1;
2947 TYPE_FIELD_BITSIZE(*type,num-1) = 0;
2948 TYPE_FIELD_TYPE(*type,num-1) = SYMBOL_TYPE(sym);
2949 TYPE_FIELD_NAME(*type,num-1) = SYMBOL_NAME(sym);
2950 }
2951 #endif
2952 \f
2953 void
2954 _initialize_symtab ()
2955 {
2956 add_info ("variables", variables_info,
2957 "All global and static variable names, or those matching REGEXP.");
2958 add_info ("functions", functions_info,
2959 "All function names, or those matching REGEXP.");
2960
2961 /* FIXME: This command has at least the following problems:
2962 1. It prints builtin types (in a very strange and confusing fashion).
2963 2. It doesn't print right, e.g. with
2964 typedef struct foo *FOO
2965 type_print prints "FOO" when we want to make it (in this situation)
2966 print "struct foo *".
2967 I also think "ptype" or "whatis" is more likely to be useful (but if
2968 there is much disagreement "info types" can be fixed). */
2969 add_info ("types", types_info,
2970 "All types names, or those matching REGEXP.");
2971
2972 #if 0
2973 add_info ("methods", methods_info,
2974 "All method names, or those matching REGEXP::REGEXP.\n\
2975 If the class qualifier is ommited, it is assumed to be the current scope.\n\
2976 If the first REGEXP is ommited, then all methods matching the second REGEXP\n\
2977 are listed.");
2978 #endif
2979 add_info ("sources", sources_info,
2980 "Source files in the program.");
2981
2982 add_com ("rbreak", no_class, rbreak_command,
2983 "Set a breakpoint for all functions matching REGEXP.");
2984
2985 /* Initialize the one built-in type that isn't language dependent... */
2986 builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>");
2987 }