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