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