gdb-3.4
[binutils-gdb.git] / gdb / symtab.c
1 /* Symbol table lookup for the GNU debugger, GDB.
2 Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 GDB 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 1, or (at your option)
9 any later version.
10
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "param.h"
23
24 #include <stdio.h>
25 #include <obstack.h>
26 #include <assert.h>
27
28 char *index ();
29
30 /* Allocate an obstack to hold objects that should be freed
31 when we load a new symbol table.
32 This includes the symbols made by dbxread
33 and the types that are not permanent. */
34
35 struct obstack obstack1;
36
37 struct obstack *symbol_obstack = &obstack1;
38
39 /* This obstack will be used for partial_symbol objects. It can
40 probably actually be the same as the symbol_obstack above, but I'd
41 like to keep them seperate for now. If I want to later, I'll
42 replace one with the other. */
43
44 struct obstack obstack2;
45
46 struct obstack *psymbol_obstack = &obstack2;
47
48 /* These variables point to the objects
49 representing the predefined C data types. */
50
51 struct type *builtin_type_void;
52 struct type *builtin_type_char;
53 struct type *builtin_type_short;
54 struct type *builtin_type_int;
55 struct type *builtin_type_long;
56 #ifdef LONG_LONG
57 struct type *builtin_type_long_long;
58 #endif
59 struct type *builtin_type_unsigned_char;
60 struct type *builtin_type_unsigned_short;
61 struct type *builtin_type_unsigned_int;
62 struct type *builtin_type_unsigned_long;
63 #ifdef LONG_LONG
64 struct type *builtin_type_unsigned_long_long;
65 #endif
66 struct type *builtin_type_float;
67 struct type *builtin_type_double;
68
69 /* Block in which the most recently searched-for symbol was found.
70 Might be better to make this a parameter to lookup_symbol and
71 value_of_this. */
72 struct block *block_found;
73
74 /* Functions */
75 static int find_line_common ();
76 static int lookup_misc_func ();
77 struct partial_symtab *lookup_partial_symtab ();
78 struct symtab *psymtab_to_symtab ();
79 static struct partial_symbol *lookup_partial_symbol ();
80
81 /* Check for a symtab of a specific name; first in symtabs, then in
82 psymtabs. *If* there is no '/' in the name, a match after a '/'
83 in the symtab filename will also work. */
84
85 static struct symtab *
86 lookup_symtab_1 (name)
87 char *name;
88 {
89 register struct symtab *s;
90 register struct partial_symtab *ps;
91 register char *slash = index (name, '/');
92 register int len = strlen (name);
93
94 for (s = symtab_list; s; s = s->next)
95 if (!strcmp (name, s->filename))
96 return s;
97
98 for (ps = partial_symtab_list; ps; ps = ps->next)
99 if (!strcmp (name, ps->filename))
100 {
101 if (ps->readin)
102 fatal ("Internal: readin pst found when no symtab found.");
103 s = psymtab_to_symtab (ps);
104 return s;
105 }
106
107 if (!slash)
108 {
109 for (s = symtab_list; s; s = s->next)
110 {
111 int l = strlen (s->filename);
112
113 if (s->filename[l - len -1] == '/'
114 && !strcmp (s->filename + l - len, name))
115 return s;
116 }
117
118 for (ps = partial_symtab_list; ps; ps = ps->next)
119 {
120 int l = strlen (ps->filename);
121
122 if (ps->filename[l - len - 1] == '/'
123 && !strcmp (ps->filename + l - len, name))
124 {
125 if (ps->readin)
126 fatal ("Internal: readin pst found when no symtab found.");
127 s = psymtab_to_symtab (ps);
128 return s;
129 }
130 }
131 }
132 return 0;
133 }
134
135 /* Lookup the symbol table of a source file named NAME. Try a couple
136 of variations if the first lookup doesn't work. */
137
138 struct symtab *
139 lookup_symtab (name)
140 char *name;
141 {
142 register struct symtab *s;
143 register char *copy;
144
145 s = lookup_symtab_1 (name);
146 if (s) return s;
147
148 /* If name not found as specified, see if adding ".c" helps. */
149
150 copy = (char *) alloca (strlen (name) + 3);
151 strcpy (copy, name);
152 strcat (copy, ".c");
153 s = lookup_symtab_1 (copy);
154 if (s) return s;
155
156 /* We didn't find anything; die. */
157 return 0;
158 }
159
160 /* Lookup the partial symbol table of a source file named NAME. This
161 only returns true on an exact match (ie. this semantics are
162 different from lookup_symtab. */
163
164 struct partial_symtab *
165 lookup_partial_symtab (name)
166 char *name;
167 {
168 register struct partial_symtab *s;
169 register char *copy;
170
171 for (s = partial_symtab_list; s; s = s->next)
172 if (!strcmp (name, s->filename))
173 return s;
174
175 return 0;
176 }
177 \f
178 /* Lookup a typedef or primitive type named NAME,
179 visible in lexical block BLOCK.
180 If NOERR is nonzero, return zero if NAME is not suitably defined. */
181
182 struct type *
183 lookup_typename (name, block, noerr)
184 char *name;
185 struct block *block;
186 int noerr;
187 {
188 register struct symbol *sym = lookup_symbol (name, block, VAR_NAMESPACE, 0);
189 if (sym == 0 || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
190 {
191 if (!strcmp (name, "int"))
192 return builtin_type_int;
193 if (!strcmp (name, "long"))
194 return builtin_type_long;
195 if (!strcmp (name, "short"))
196 return builtin_type_short;
197 if (!strcmp (name, "char"))
198 return builtin_type_char;
199 if (!strcmp (name, "float"))
200 return builtin_type_float;
201 if (!strcmp (name, "double"))
202 return builtin_type_double;
203 if (!strcmp (name, "void"))
204 return builtin_type_void;
205
206 if (noerr)
207 return 0;
208 error ("No type named %s.", name);
209 }
210 return SYMBOL_TYPE (sym);
211 }
212
213 struct type *
214 lookup_unsigned_typename (name)
215 char *name;
216 {
217 if (!strcmp (name, "int"))
218 return builtin_type_unsigned_int;
219 if (!strcmp (name, "long"))
220 return builtin_type_unsigned_long;
221 if (!strcmp (name, "short"))
222 return builtin_type_unsigned_short;
223 if (!strcmp (name, "char"))
224 return builtin_type_unsigned_char;
225 error ("No type named unsigned %s.", name);
226 }
227
228 /* Lookup a structure type named "struct NAME",
229 visible in lexical block BLOCK. */
230
231 struct type *
232 lookup_struct (name, block)
233 char *name;
234 struct block *block;
235 {
236 register struct symbol *sym
237 = lookup_symbol (name, block, STRUCT_NAMESPACE, 0);
238
239 if (sym == 0)
240 error ("No struct type named %s.", name);
241 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
242 error ("This context has class, union or enum %s, not a struct.", name);
243 return SYMBOL_TYPE (sym);
244 }
245
246 /* Lookup a union type named "union NAME",
247 visible in lexical block BLOCK. */
248
249 struct type *
250 lookup_union (name, block)
251 char *name;
252 struct block *block;
253 {
254 register struct symbol *sym
255 = lookup_symbol (name, block, STRUCT_NAMESPACE, 0);
256
257 if (sym == 0)
258 error ("No union type named %s.", name);
259 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
260 error ("This context has class, struct or enum %s, not a union.", name);
261 return SYMBOL_TYPE (sym);
262 }
263
264 /* Lookup an enum type named "enum NAME",
265 visible in lexical block BLOCK. */
266
267 struct type *
268 lookup_enum (name, block)
269 char *name;
270 struct block *block;
271 {
272 register struct symbol *sym
273 = lookup_symbol (name, block, STRUCT_NAMESPACE, 0);
274 if (sym == 0)
275 error ("No enum type named %s.", name);
276 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
277 error ("This context has class, struct or union %s, not an enum.", name);
278 return SYMBOL_TYPE (sym);
279 }
280
281 /* Given a type TYPE, lookup the type of the component of type named
282 NAME. */
283
284 struct type *
285 lookup_struct_elt_type (type, name)
286 struct type *type;
287 char *name;
288 {
289 struct type *t;
290 int i;
291 char *errmsg;
292
293 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
294 && TYPE_CODE (type) != TYPE_CODE_UNION)
295 {
296 terminal_ours ();
297 fflush (stdout);
298 fprintf (stderr, "Type ");
299 type_print (type, "", stderr, -1);
300 fprintf (stderr, " is not a structure or union type.\n");
301 return_to_top_level ();
302 }
303
304 for (i = TYPE_NFIELDS (type) - 1; i >= 0; i--)
305 if (!strcmp (TYPE_FIELD_NAME (type, i), name))
306 return TYPE_FIELD_TYPE (type, i);
307
308 terminal_ours ();
309 fflush (stdout);
310 fprintf (stderr, "Type ");
311 type_print (type, "", stderr, -1);
312 fprintf (stderr, " has no component named %s\n", name);
313 return_to_top_level ();
314 }
315
316 /* Given a type TYPE, return a type of pointers to that type.
317 May need to construct such a type if this is the first use.
318
319 C++: use TYPE_MAIN_VARIANT and TYPE_CHAIN to keep pointer
320 to member types under control. */
321
322 struct type *
323 lookup_pointer_type (type)
324 struct type *type;
325 {
326 register struct type *ptype = TYPE_POINTER_TYPE (type);
327 if (ptype) return TYPE_MAIN_VARIANT (ptype);
328
329 /* This is the first time anyone wanted a pointer to a TYPE. */
330 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
331 ptype = (struct type *) xmalloc (sizeof (struct type));
332 else
333 ptype = (struct type *) obstack_alloc (symbol_obstack,
334 sizeof (struct type));
335
336 bzero (ptype, sizeof (struct type));
337 TYPE_MAIN_VARIANT (ptype) = ptype;
338 TYPE_TARGET_TYPE (ptype) = type;
339 TYPE_POINTER_TYPE (type) = ptype;
340 /* New type is permanent if type pointed to is permanent. */
341 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
342 TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
343 /* We assume the machine has only one representation for pointers! */
344 TYPE_LENGTH (ptype) = sizeof (char *);
345 TYPE_CODE (ptype) = TYPE_CODE_PTR;
346 return ptype;
347 }
348
349 struct type *
350 lookup_reference_type (type)
351 struct type *type;
352 {
353 register struct type *rtype = TYPE_REFERENCE_TYPE (type);
354 if (rtype) return TYPE_MAIN_VARIANT (rtype);
355
356 /* This is the first time anyone wanted a pointer to a TYPE. */
357 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
358 rtype = (struct type *) xmalloc (sizeof (struct type));
359 else
360 rtype = (struct type *) obstack_alloc (symbol_obstack,
361 sizeof (struct type));
362
363 bzero (rtype, sizeof (struct type));
364 TYPE_MAIN_VARIANT (rtype) = rtype;
365 TYPE_TARGET_TYPE (rtype) = type;
366 TYPE_REFERENCE_TYPE (type) = rtype;
367 /* New type is permanent if type pointed to is permanent. */
368 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
369 TYPE_FLAGS (rtype) |= TYPE_FLAG_PERM;
370 /* We assume the machine has only one representation for pointers! */
371 TYPE_LENGTH (rtype) = sizeof (char *);
372 TYPE_CODE (rtype) = TYPE_CODE_REF;
373 return rtype;
374 }
375
376
377 /* Implement direct support for MEMBER_TYPE in GNU C++.
378 May need to construct such a type if this is the first use.
379 The TYPE is the type of the member. The DOMAIN is the type
380 of the aggregate that the member belongs to. */
381
382 struct type *
383 lookup_member_type (type, domain)
384 struct type *type, *domain;
385 {
386 register struct type *mtype = TYPE_MAIN_VARIANT (type);
387 struct type *main_type;
388
389 main_type = mtype;
390 while (mtype)
391 {
392 if (TYPE_DOMAIN_TYPE (mtype) == domain)
393 return mtype;
394 mtype = TYPE_NEXT_VARIANT (mtype);
395 }
396
397 /* This is the first time anyone wanted this member type. */
398 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
399 mtype = (struct type *) xmalloc (sizeof (struct type));
400 else
401 mtype = (struct type *) obstack_alloc (symbol_obstack,
402 sizeof (struct type));
403
404 bzero (mtype, sizeof (struct type));
405 if (main_type == 0)
406 main_type = mtype;
407 else
408 {
409 TYPE_NEXT_VARIANT (mtype) = TYPE_NEXT_VARIANT (main_type);
410 TYPE_NEXT_VARIANT (main_type) = mtype;
411 }
412 TYPE_MAIN_VARIANT (mtype) = main_type;
413 TYPE_TARGET_TYPE (mtype) = type;
414 TYPE_DOMAIN_TYPE (mtype) = domain;
415 /* New type is permanent if type pointed to is permanent. */
416 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
417 TYPE_FLAGS (mtype) |= TYPE_FLAG_PERM;
418
419 /* In practice, this is never used. */
420 TYPE_LENGTH (mtype) = 1;
421 TYPE_CODE (mtype) = TYPE_CODE_MEMBER;
422
423 #if 0
424 /* Now splice in the new member pointer type. */
425 if (main_type)
426 {
427 /* This type was not "smashed". */
428 TYPE_CHAIN (mtype) = TYPE_CHAIN (main_type);
429 TYPE_CHAIN (main_type) = mtype;
430 }
431 #endif
432
433 return mtype;
434 }
435
436 struct type *
437 lookup_method_type (type, domain, args)
438 struct type *type, *domain, **args;
439 {
440 register struct type *mtype = TYPE_MAIN_VARIANT (type);
441 struct type *main_type;
442
443 main_type = mtype;
444 while (mtype)
445 {
446 if (TYPE_DOMAIN_TYPE (mtype) == domain)
447 {
448 struct type **t1 = args;
449 struct type **t2 = TYPE_ARG_TYPES (mtype);
450 if (t2)
451 {
452 int i;
453 for (i = 0; t1[i] != 0 && t1[i]->code != TYPE_CODE_VOID; i++)
454 if (t1[i] != t2[i])
455 break;
456 if (t1[i] == t2[i])
457 return mtype;
458 }
459 }
460 mtype = TYPE_NEXT_VARIANT (mtype);
461 }
462
463 /* This is the first time anyone wanted this member type. */
464 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
465 mtype = (struct type *) xmalloc (sizeof (struct type));
466 else
467 mtype = (struct type *) obstack_alloc (symbol_obstack,
468 sizeof (struct type));
469
470 bzero (mtype, sizeof (struct type));
471 if (main_type == 0)
472 main_type = mtype;
473 else
474 {
475 TYPE_NEXT_VARIANT (mtype) = TYPE_NEXT_VARIANT (main_type);
476 TYPE_NEXT_VARIANT (main_type) = mtype;
477 }
478 TYPE_MAIN_VARIANT (mtype) = main_type;
479 TYPE_TARGET_TYPE (mtype) = type;
480 TYPE_DOMAIN_TYPE (mtype) = domain;
481 TYPE_ARG_TYPES (mtype) = args;
482 /* New type is permanent if type pointed to is permanent. */
483 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
484 TYPE_FLAGS (mtype) |= TYPE_FLAG_PERM;
485
486 /* In practice, this is never used. */
487 TYPE_LENGTH (mtype) = 1;
488 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
489
490 #if 0
491 /* Now splice in the new member pointer type. */
492 if (main_type)
493 {
494 /* This type was not "smashed". */
495 TYPE_CHAIN (mtype) = TYPE_CHAIN (main_type);
496 TYPE_CHAIN (main_type) = mtype;
497 }
498 #endif
499
500 return mtype;
501 }
502
503 /* Given a type TYPE, return a type which has offset OFFSET,
504 via_virtual VIA_VIRTUAL, and via_public VIA_PUBLIC.
505 May need to construct such a type if none exists. */
506 struct type *
507 lookup_basetype_type (type, offset, via_virtual, via_public)
508 struct type *type;
509 int offset;
510 int via_virtual, via_public;
511 {
512 register struct type *btype = TYPE_MAIN_VARIANT (type);
513 struct type *main_type;
514
515 if (offset != 0)
516 {
517 printf ("Internal error: type offset non-zero in lookup_basetype_type");
518 offset = 0;
519 }
520
521 main_type = btype;
522 while (btype)
523 {
524 if (/* TYPE_OFFSET (btype) == offset
525 && */ TYPE_VIA_PUBLIC (btype) == via_public
526 && TYPE_VIA_VIRTUAL (btype) == via_virtual)
527 return btype;
528 btype = TYPE_NEXT_VARIANT (btype);
529 }
530
531 /* This is the first time anyone wanted this member type. */
532 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
533 btype = (struct type *) xmalloc (sizeof (struct type));
534 else
535 btype = (struct type *) obstack_alloc (symbol_obstack,
536 sizeof (struct type));
537
538 if (main_type == 0)
539 {
540 main_type = btype;
541 bzero (btype, sizeof (struct type));
542 TYPE_MAIN_VARIANT (btype) = main_type;
543 }
544 else
545 {
546 bcopy (main_type, btype, sizeof (struct type));
547 TYPE_NEXT_VARIANT (main_type) = btype;
548 }
549 /* TYPE_OFFSET (btype) = offset; */
550 if (via_public)
551 TYPE_FLAGS (btype) |= TYPE_FLAG_VIA_PUBLIC;
552 if (via_virtual)
553 TYPE_FLAGS (btype) |= TYPE_FLAG_VIA_VIRTUAL;
554 /* New type is permanent if type pointed to is permanent. */
555 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
556 TYPE_FLAGS (btype) |= TYPE_FLAG_PERM;
557
558 /* In practice, this is never used. */
559 TYPE_LENGTH (btype) = 1;
560 TYPE_CODE (btype) = TYPE_CODE_STRUCT;
561
562 return btype;
563 }
564
565 /* Given a type TYPE, return a type of functions that return that type.
566 May need to construct such a type if this is the first use. */
567
568 struct type *
569 lookup_function_type (type)
570 struct type *type;
571 {
572 register struct type *ptype = TYPE_FUNCTION_TYPE (type);
573 if (ptype) return ptype;
574
575 /* This is the first time anyone wanted a function returning a TYPE. */
576 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
577 ptype = (struct type *) xmalloc (sizeof (struct type));
578 else
579 ptype = (struct type *) obstack_alloc (symbol_obstack,
580 sizeof (struct type));
581
582 bzero (ptype, sizeof (struct type));
583 TYPE_TARGET_TYPE (ptype) = type;
584 TYPE_FUNCTION_TYPE (type) = ptype;
585 /* New type is permanent if type returned is permanent. */
586 if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
587 TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
588 TYPE_LENGTH (ptype) = 1;
589 TYPE_CODE (ptype) = TYPE_CODE_FUNC;
590 TYPE_NFIELDS (ptype) = 0;
591 return ptype;
592 }
593 \f
594 /* Create an array type. Elements will be of type TYPE, and there will
595 be NUM of them.
596
597 Eventually this should be extended to take two more arguments which
598 specify the bounds of the array and the type of the index.
599 It should also be changed to be a "lookup" function, with the
600 appropriate data structures added to the type field.
601 Then read array type should call here. */
602
603 struct type *
604 create_array_type (element_type, number)
605 struct type *element_type;
606 int number;
607 {
608 struct type *result_type = (struct type *)
609 obstack_alloc (symbol_obstack, sizeof (struct type));
610
611 bzero (result_type, sizeof (struct type));
612
613 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
614 TYPE_TARGET_TYPE (result_type) = element_type;
615 TYPE_LENGTH (result_type) = number * TYPE_LENGTH (element_type);
616 TYPE_NFIELDS (result_type) = 1;
617 TYPE_FIELDS (result_type) =
618 (struct field *) obstack_alloc (symbol_obstack, sizeof (struct field));
619 TYPE_FIELD_TYPE (result_type, 0) = builtin_type_int;
620 TYPE_VPTR_FIELDNO (result_type) = -1;
621
622 return result_type;
623 }
624
625 \f
626 /* Smash TYPE to be a type of pointers to TO_TYPE.
627 If TO_TYPE is not permanent and has no pointer-type yet,
628 record TYPE as its pointer-type. */
629
630 void
631 smash_to_pointer_type (type, to_type)
632 struct type *type, *to_type;
633 {
634 int type_permanent = (TYPE_FLAGS (type) & TYPE_FLAG_PERM);
635
636 bzero (type, sizeof (struct type));
637 TYPE_TARGET_TYPE (type) = to_type;
638 /* We assume the machine has only one representation for pointers! */
639 TYPE_LENGTH (type) = sizeof (char *);
640 TYPE_CODE (type) = TYPE_CODE_PTR;
641
642 TYPE_MAIN_VARIANT (type) = type;
643
644 if (type_permanent)
645 TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
646
647 if (TYPE_POINTER_TYPE (to_type) == 0
648 && (!(TYPE_FLAGS (to_type) & TYPE_FLAG_PERM)
649 || type_permanent))
650 {
651 TYPE_POINTER_TYPE (to_type) = type;
652 }
653 }
654
655 /* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE. */
656
657 void
658 smash_to_member_type (type, domain, to_type)
659 struct type *type, *domain, *to_type;
660 {
661 bzero (type, sizeof (struct type));
662 TYPE_TARGET_TYPE (type) = to_type;
663 TYPE_DOMAIN_TYPE (type) = domain;
664
665 /* In practice, this is never needed. */
666 TYPE_LENGTH (type) = 1;
667 TYPE_CODE (type) = TYPE_CODE_MEMBER;
668
669 TYPE_MAIN_VARIANT (type) = lookup_member_type (domain, to_type);
670 }
671
672 /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE. */
673
674 void
675 smash_to_method_type (type, domain, to_type, args)
676 struct type *type, *domain, *to_type, **args;
677 {
678 bzero (type, sizeof (struct type));
679 TYPE_TARGET_TYPE (type) = to_type;
680 TYPE_DOMAIN_TYPE (type) = domain;
681 TYPE_ARG_TYPES (type) = args;
682
683 /* In practice, this is never needed. */
684 TYPE_LENGTH (type) = 1;
685 TYPE_CODE (type) = TYPE_CODE_METHOD;
686
687 TYPE_MAIN_VARIANT (type) = lookup_method_type (domain, to_type, args);
688 }
689
690 /* Smash TYPE to be a type of reference to TO_TYPE.
691 If TO_TYPE is not permanent and has no pointer-type yet,
692 record TYPE as its pointer-type. */
693
694 void
695 smash_to_reference_type (type, to_type)
696 struct type *type, *to_type;
697 {
698 int type_permanent = (TYPE_FLAGS (type) & TYPE_FLAG_PERM);
699
700 bzero (type, sizeof (struct type));
701 TYPE_TARGET_TYPE (type) = to_type;
702 /* We assume the machine has only one representation for pointers! */
703 TYPE_LENGTH (type) = sizeof (char *);
704 TYPE_CODE (type) = TYPE_CODE_REF;
705
706 TYPE_MAIN_VARIANT (type) = type;
707
708 if (type_permanent)
709 TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
710
711 if (TYPE_REFERENCE_TYPE (to_type) == 0
712 && (!(TYPE_FLAGS (to_type) & TYPE_FLAG_PERM)
713 || type_permanent))
714 {
715 TYPE_REFERENCE_TYPE (to_type) = type;
716 }
717 }
718
719 /* Smash TYPE to be a type of functions returning TO_TYPE.
720 If TO_TYPE is not permanent and has no function-type yet,
721 record TYPE as its function-type. */
722
723 void
724 smash_to_function_type (type, to_type)
725 struct type *type, *to_type;
726 {
727 int type_permanent = (TYPE_FLAGS (type) & TYPE_FLAG_PERM);
728
729 bzero (type, sizeof (struct type));
730 TYPE_TARGET_TYPE (type) = to_type;
731 TYPE_LENGTH (type) = 1;
732 TYPE_CODE (type) = TYPE_CODE_FUNC;
733 TYPE_NFIELDS (type) = 0;
734
735 if (type_permanent)
736 TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
737
738 if (TYPE_FUNCTION_TYPE (to_type) == 0
739 && (!(TYPE_FLAGS (to_type) & TYPE_FLAG_PERM)
740 || type_permanent))
741 {
742 TYPE_FUNCTION_TYPE (to_type) = type;
743 }
744 }
745 \f
746 /* Find which partial symtab on the partial_symtab_list contains
747 PC. Return 0 if none. */
748
749 struct partial_symtab *
750 find_pc_psymtab (pc)
751 register CORE_ADDR pc;
752 {
753 register struct partial_symtab *ps;
754
755 for (ps = partial_symtab_list; ps; ps = ps->next)
756 if (pc >= ps->textlow && pc < ps->texthigh)
757 return ps;
758
759 return 0;
760 }
761
762 /* Find which partial symbol within a psymtab contains PC. Return 0
763 if none. Check all psymtabs if PSYMTAB is 0. */
764 struct partial_symbol *
765 find_pc_psymbol (psymtab, pc)
766 struct partial_symtab *psymtab;
767 CORE_ADDR pc;
768 {
769 struct partial_symbol *best, *p;
770 int best_pc;
771
772 if (!psymtab)
773 psymtab = find_pc_psymtab (pc);
774 if (!psymtab)
775 return 0;
776
777 best_pc = psymtab->textlow - 1;
778
779 for (p = static_psymbols.list + psymtab->statics_offset;
780 (p - (static_psymbols.list + psymtab->statics_offset)
781 < psymtab->n_static_syms);
782 p++)
783 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
784 && SYMBOL_CLASS (p) == LOC_BLOCK
785 && pc >= SYMBOL_VALUE (p)
786 && SYMBOL_VALUE (p) > best_pc)
787 {
788 best_pc = SYMBOL_VALUE (p);
789 best = p;
790 }
791 if (best_pc == psymtab->textlow - 1)
792 return 0;
793 return best;
794 }
795
796 \f
797 static struct symbol *lookup_block_symbol ();
798
799 /* Find the definition for a specified symbol name NAME
800 in namespace NAMESPACE, visible from lexical block BLOCK.
801 Returns the struct symbol pointer, or zero if no symbol is found.
802 C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
803 NAME is a field of the current implied argument `this'. If so set
804 *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
805 BLOCK_FOUND is set to the block in which NAME is found (in the case of
806 a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
807
808 struct symbol *
809 lookup_symbol (name, block, namespace, is_a_field_of_this)
810 char *name;
811 register struct block *block;
812 enum namespace namespace;
813 int *is_a_field_of_this;
814 {
815 register int i, n;
816 register struct symbol *sym;
817 register struct symtab *s;
818 register struct partial_symtab *ps;
819 struct blockvector *bv;
820
821 /* Search specified block and its superiors. */
822
823 while (block != 0)
824 {
825 sym = lookup_block_symbol (block, name, namespace);
826 if (sym)
827 {
828 block_found = block;
829 return sym;
830 }
831 block = BLOCK_SUPERBLOCK (block);
832 }
833
834 /* C++: If requested to do so by the caller,
835 check to see if NAME is a field of `this'. */
836 if (is_a_field_of_this)
837 {
838 int v = (int) value_of_this (0);
839
840 *is_a_field_of_this = 0;
841 if (v && check_field (v, name))
842 {
843 *is_a_field_of_this = 1;
844 return 0;
845 }
846 }
847
848 /* Now search all global blocks. Do the symtab's first, then
849 check the psymtab's */
850
851 for (s = symtab_list; s; s = s->next)
852 {
853 bv = BLOCKVECTOR (s);
854 block = BLOCKVECTOR_BLOCK (bv, 0);
855 sym = lookup_block_symbol (block, name, namespace);
856 if (sym)
857 {
858 block_found = block;
859 return sym;
860 }
861 }
862
863 /* Check for the possibility of the symbol being a global function
864 that is stored on the misc function vector. Eventually, all
865 global symbols might be resolved in this way. */
866
867 if (namespace == VAR_NAMESPACE)
868 {
869 int index = lookup_misc_func (name);
870
871 if (index != -1)
872 {
873 ps = find_pc_psymtab (misc_function_vector[index].address);
874 if (ps && !ps->readin)
875 {
876 s = psymtab_to_symtab (ps);
877 bv = BLOCKVECTOR (s);
878 block = BLOCKVECTOR_BLOCK (bv, 0);
879 sym = lookup_block_symbol (block, name, namespace);
880 /* sym == 0 if symbol was found in the psymtab but not
881 in the symtab.
882 Return 0 to use the misc_function definition of "foo_".
883
884 This happens for Fortran "foo_" symbols,
885 which are "foo" in the symtab.
886
887 This can also happen if "asm" is used to make a
888 regular symbol but not a debugging symbol, e.g.
889 asm(".globl _main");
890 asm("_main:");
891 */
892
893 return sym;
894 }
895 }
896 }
897
898 for (ps = partial_symtab_list; ps; ps = ps->next)
899 if (!ps->readin && lookup_partial_symbol (ps, name, 1, namespace))
900 {
901 s = psymtab_to_symtab(ps);
902 bv = BLOCKVECTOR (s);
903 block = BLOCKVECTOR_BLOCK (bv, 0);
904 sym = lookup_block_symbol (block, name, namespace);
905 if (!sym)
906 fatal ("Internal: global symbol found in psymtab but not in symtab");
907 return sym;
908 }
909
910 /* Now search all per-file blocks.
911 Not strictly correct, but more useful than an error.
912 Do the symtabs first, then check the psymtabs */
913
914 for (s = symtab_list; s; s = s->next)
915 {
916 bv = BLOCKVECTOR (s);
917 block = BLOCKVECTOR_BLOCK (bv, 1);
918 sym = lookup_block_symbol (block, name, namespace);
919 if (sym)
920 {
921 block_found = block;
922 return sym;
923 }
924 }
925
926 for (ps = partial_symtab_list; ps; ps = ps->next)
927 if (!ps->readin && lookup_partial_symbol (ps, name, 0, namespace))
928 {
929 s = psymtab_to_symtab(ps);
930 bv = BLOCKVECTOR (s);
931 block = BLOCKVECTOR_BLOCK (bv, 1);
932 sym = lookup_block_symbol (block, name, namespace);
933 if (!sym)
934 fatal ("Internal: static symbol found in psymtab but not in symtab");
935 return sym;
936 }
937
938 return 0;
939 }
940
941 /* Look, in partial_symtab PST, for symbol NAME. Check the global
942 symbols if GLOBAL, the static symbols if not */
943
944 static struct partial_symbol *
945 lookup_partial_symbol (pst, name, global, namespace)
946 struct partial_symtab *pst;
947 char *name;
948 int global;
949 enum namespace namespace;
950 {
951 struct partial_symbol *start, *psym;
952 int length = (global ? pst->n_global_syms : pst->n_static_syms);
953
954 start = (global ?
955 global_psymbols.list + pst->globals_offset :
956 static_psymbols.list + pst->statics_offset );
957
958 if (!length)
959 return (struct partial_symbol *) 0;
960
961 if (global) /* This means we can use a binary */
962 /* search. */
963 {
964 struct partial_symbol *top, *bottom, *center;
965
966 /* Binary search. This search is guarranteed to end with center
967 pointing at the earliest partial symbol with the correct
968 name. At that point *all* partial symbols with that name
969 will be checked against the correct namespace. */
970 bottom = start;
971 top = start + length - 1;
972 while (top > bottom)
973 {
974 center = bottom + (top - bottom) / 2;
975
976 assert (center < top);
977
978 if (strcmp (SYMBOL_NAME (center), name) >= 0)
979 top = center;
980 else
981 bottom = center + 1;
982 }
983 assert (top == bottom);
984
985 while (!strcmp (SYMBOL_NAME (top), name))
986 {
987 if (SYMBOL_NAMESPACE (top) == namespace)
988 return top;
989 top ++;
990 }
991 }
992 else
993 {
994 /* Can't use a binary search */
995 for (psym = start; psym < start + length; psym++)
996 if (namespace == SYMBOL_NAMESPACE (psym)
997 && !strcmp (name, SYMBOL_NAME (psym)))
998 return psym;
999 }
1000
1001 return (struct partial_symbol *) 0;
1002 }
1003
1004 /* Look for a symbol in block BLOCK. */
1005
1006 static struct symbol *
1007 lookup_block_symbol (block, name, namespace)
1008 register struct block *block;
1009 char *name;
1010 enum namespace namespace;
1011 {
1012 register int bot, top, inc;
1013 register struct symbol *sym, *parameter_sym;
1014
1015 top = BLOCK_NSYMS (block);
1016 bot = 0;
1017
1018 /* If the blocks's symbols were sorted, start with a binary search. */
1019
1020 if (BLOCK_SHOULD_SORT (block))
1021 {
1022 /* First, advance BOT to not far before
1023 the first symbol whose name is NAME. */
1024
1025 while (1)
1026 {
1027 inc = (top - bot + 1);
1028 /* No need to keep binary searching for the last few bits worth. */
1029 if (inc < 4)
1030 break;
1031 inc = (inc >> 1) + bot;
1032 sym = BLOCK_SYM (block, inc);
1033 if (SYMBOL_NAME (sym)[0] < name[0])
1034 bot = inc;
1035 else if (SYMBOL_NAME (sym)[0] > name[0])
1036 top = inc;
1037 else if (strcmp (SYMBOL_NAME (sym), name) < 0)
1038 bot = inc;
1039 else
1040 top = inc;
1041 }
1042
1043 /* Now scan forward until we run out of symbols,
1044 find one whose name is greater than NAME,
1045 or find one we want.
1046 If there is more than one symbol with the right name and namespace,
1047 we return the first one. dbxread.c is careful to make sure
1048 that if one is a register then it comes first. */
1049
1050 top = BLOCK_NSYMS (block);
1051 while (bot < top)
1052 {
1053 sym = BLOCK_SYM (block, bot);
1054 inc = SYMBOL_NAME (sym)[0] - name[0];
1055 if (inc == 0)
1056 inc = strcmp (SYMBOL_NAME (sym), name);
1057 if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
1058 return sym;
1059 if (inc > 0)
1060 return 0;
1061 bot++;
1062 }
1063 return 0;
1064 }
1065
1066 /* Here if block isn't sorted.
1067 This loop is equivalent to the loop above,
1068 but hacked greatly for speed.
1069
1070 Note that parameter symbols do not always show up last in the
1071 list; this loop makes sure to take anything else other than
1072 parameter symbols first; it only uses parameter symbols as a
1073 last resort. Note that this only takes up extra computation
1074 time on a match. */
1075
1076 parameter_sym = (struct symbol *) 0;
1077 top = BLOCK_NSYMS (block);
1078 inc = name[0];
1079 while (bot < top)
1080 {
1081 sym = BLOCK_SYM (block, bot);
1082 if (SYMBOL_NAME (sym)[0] == inc
1083 && !strcmp (SYMBOL_NAME (sym), name)
1084 && SYMBOL_NAMESPACE (sym) == namespace)
1085 {
1086 if (SYMBOL_CLASS (sym) == LOC_ARG
1087 || SYMBOL_CLASS (sym) == LOC_REF_ARG
1088 || SYMBOL_CLASS (sym) == LOC_REGPARM)
1089 parameter_sym = sym;
1090 else
1091 return sym;
1092 }
1093 bot++;
1094 }
1095 return parameter_sym; /* Will be 0 if not found. */
1096 }
1097 \f
1098 /* Return the symbol for the function which contains a specified
1099 lexical block, described by a struct block BL. */
1100
1101 struct symbol *
1102 block_function (bl)
1103 struct block *bl;
1104 {
1105 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
1106 bl = BLOCK_SUPERBLOCK (bl);
1107
1108 return BLOCK_FUNCTION (bl);
1109 }
1110
1111 /* Subroutine of find_pc_line */
1112
1113 struct symtab *
1114 find_pc_symtab (pc)
1115 register CORE_ADDR pc;
1116 {
1117 register struct block *b;
1118 struct blockvector *bv;
1119 register struct symtab *s;
1120 register struct partial_symtab *ps;
1121
1122 /* Search all symtabs for one whose file contains our pc */
1123
1124 for (s = symtab_list; s; s = s->next)
1125 {
1126 bv = BLOCKVECTOR (s);
1127 b = BLOCKVECTOR_BLOCK (bv, 0);
1128 if (BLOCK_START (b) <= pc
1129 && BLOCK_END (b) > pc)
1130 break;
1131 }
1132
1133 if (!s)
1134 {
1135 ps = find_pc_psymtab (pc);
1136 if (ps && ps->readin)
1137 fatal ("Internal error: pc in read in psymtab, but not in symtab.");
1138
1139 if (ps)
1140 s = psymtab_to_symtab (ps);
1141 }
1142
1143 return s;
1144 }
1145
1146 /* Find the source file and line number for a given PC value.
1147 Return a structure containing a symtab pointer, a line number,
1148 and a pc range for the entire source line.
1149 The value's .pc field is NOT the specified pc.
1150 NOTCURRENT nonzero means, if specified pc is on a line boundary,
1151 use the line that ends there. Otherwise, in that case, the line
1152 that begins there is used. */
1153
1154 struct symtab_and_line
1155 find_pc_line (pc, notcurrent)
1156 CORE_ADDR pc;
1157 int notcurrent;
1158 {
1159 struct symtab *s;
1160 register struct linetable *l;
1161 register int len;
1162 register int i;
1163 register struct linetable_entry *item;
1164 struct symtab_and_line value;
1165 struct blockvector *bv;
1166
1167 /* Info on best line seen so far, and where it starts, and its file. */
1168
1169 int best_line = 0;
1170 CORE_ADDR best_pc = 0;
1171 CORE_ADDR best_end = 0;
1172 struct symtab *best_symtab = 0;
1173
1174 /* Store here the first line number
1175 of a file which contains the line at the smallest pc after PC.
1176 If we don't find a line whose range contains PC,
1177 we will use a line one less than this,
1178 with a range from the start of that file to the first line's pc. */
1179 int alt_line = 0;
1180 CORE_ADDR alt_pc = 0;
1181 struct symtab *alt_symtab = 0;
1182
1183 /* Info on best line seen in this file. */
1184
1185 int prev_line;
1186 CORE_ADDR prev_pc;
1187
1188 /* Info on first line of this file. */
1189
1190 int first_line;
1191 CORE_ADDR first_pc;
1192
1193 /* If this pc is not from the current frame,
1194 it is the address of the end of a call instruction.
1195 Quite likely that is the start of the following statement.
1196 But what we want is the statement containing the instruction.
1197 Fudge the pc to make sure we get that. */
1198
1199 if (notcurrent) pc -= 1;
1200
1201 s = find_pc_symtab (pc);
1202 if (s == 0)
1203 {
1204 value.symtab = 0;
1205 value.line = 0;
1206 value.pc = pc;
1207 value.end = 0;
1208 return value;
1209 }
1210
1211 bv = BLOCKVECTOR (s);
1212
1213 /* Look at all the symtabs that share this blockvector.
1214 They all have the same apriori range, that we found was right;
1215 but they have different line tables. */
1216
1217 for (; s && BLOCKVECTOR (s) == bv; s = s->next)
1218 {
1219 /* Find the best line in this symtab. */
1220 l = LINETABLE (s);
1221 len = l->nitems;
1222 prev_line = -1;
1223 first_line = -1;
1224 for (i = 0; i < len; i++)
1225 {
1226 item = &(l->item[i]);
1227
1228 if (first_line < 0)
1229 {
1230 first_line = item->line;
1231 first_pc = item->pc;
1232 }
1233 /* Return the last line that did not start after PC. */
1234 if (pc >= item->pc)
1235 {
1236 prev_line = item->line;
1237 prev_pc = item->pc;
1238 }
1239 else
1240 break;
1241 }
1242
1243 /* Is this file's best line closer than the best in the other files?
1244 If so, record this file, and its best line, as best so far. */
1245 if (prev_line >= 0 && prev_pc > best_pc)
1246 {
1247 best_pc = prev_pc;
1248 best_line = prev_line;
1249 best_symtab = s;
1250 if (i < len)
1251 best_end = item->pc;
1252 else
1253 best_end = 0;
1254 }
1255 /* Is this file's first line closer than the first lines of other files?
1256 If so, record this file, and its first line, as best alternate. */
1257 if (first_line >= 0 && first_pc > pc
1258 && (alt_pc == 0 || first_pc < alt_pc))
1259 {
1260 alt_pc = first_pc;
1261 alt_line = first_line;
1262 alt_symtab = s;
1263 }
1264 }
1265 if (best_symtab == 0)
1266 {
1267 value.symtab = alt_symtab;
1268 value.line = alt_line - 1;
1269 value.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, 0));
1270 value.end = alt_pc;
1271 }
1272 else
1273 {
1274 value.symtab = best_symtab;
1275 value.line = best_line;
1276 value.pc = best_pc;
1277 value.end = (best_end ? best_end
1278 : (alt_pc ? alt_pc
1279 : BLOCK_END (BLOCKVECTOR_BLOCK (bv, 0))));
1280 }
1281 return value;
1282 }
1283 \f
1284 /* Find the PC value for a given source file and line number.
1285 Returns zero for invalid line number.
1286 The source file is specified with a struct symtab. */
1287
1288 CORE_ADDR
1289 find_line_pc (symtab, line)
1290 struct symtab *symtab;
1291 int line;
1292 {
1293 register struct linetable *l;
1294 register int index;
1295 int dummy;
1296
1297 if (symtab == 0)
1298 return 0;
1299 l = LINETABLE (symtab);
1300 index = find_line_common(l, line, &dummy);
1301 return index ? l->item[index].pc : 0;
1302 }
1303
1304 /* Find the range of pc values in a line.
1305 Store the starting pc of the line into *STARTPTR
1306 and the ending pc (start of next line) into *ENDPTR.
1307 Returns 1 to indicate success.
1308 Returns 0 if could not find the specified line. */
1309
1310 int
1311 find_line_pc_range (symtab, thisline, startptr, endptr)
1312 struct symtab *symtab;
1313 int thisline;
1314 CORE_ADDR *startptr, *endptr;
1315 {
1316 register struct linetable *l;
1317 register int index;
1318 int exact_match; /* did we get an exact linenumber match */
1319 register CORE_ADDR prev_pc;
1320 CORE_ADDR last_pc;
1321
1322 if (symtab == 0)
1323 return 0;
1324
1325 l = LINETABLE (symtab);
1326 index = find_line_common (l, thisline, &exact_match);
1327 if (index)
1328 {
1329 *startptr = l->item[index].pc;
1330 /* If we have not seen an entry for the specified line,
1331 assume that means the specified line has zero bytes. */
1332 if (!exact_match || index == l->nitems-1)
1333 *endptr = *startptr;
1334 else
1335 /* Perhaps the following entry is for the following line.
1336 It's worth a try. */
1337 if (l->item[index+1].line == thisline + 1)
1338 *endptr = l->item[index+1].pc;
1339 else
1340 *endptr = find_line_pc (symtab, thisline+1);
1341 return 1;
1342 }
1343
1344 return 0;
1345 }
1346
1347 /* Given a line table and a line number, return the index into the line
1348 table for the pc of the nearest line whose number is >= the specified one.
1349 Return 0 if none is found. The value is never zero is it is an index.
1350
1351 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
1352
1353 static int
1354 find_line_common (l, lineno, exact_match)
1355 register struct linetable *l;
1356 register int lineno;
1357 int *exact_match;
1358 {
1359 register int i;
1360 register int len;
1361
1362 /* BEST is the smallest linenumber > LINENO so far seen,
1363 or 0 if none has been seen so far.
1364 BEST_INDEX identifies the item for it. */
1365
1366 int best_index = 0;
1367 int best = 0;
1368
1369 int nextline = -1;
1370
1371 if (lineno <= 0)
1372 return 0;
1373
1374 len = l->nitems;
1375 for (i = 0; i < len; i++)
1376 {
1377 register struct linetable_entry *item = &(l->item[i]);
1378
1379 if (item->line == lineno)
1380 {
1381 *exact_match = 1;
1382 return i;
1383 }
1384
1385 if (item->line > lineno && (best == 0 || item->line < best))
1386 {
1387 best = item->line;
1388 best_index = i;
1389 }
1390 }
1391
1392 /* If we got here, we didn't get an exact match. */
1393
1394 *exact_match = 0;
1395 return best_index;
1396 }
1397
1398 int
1399 find_pc_line_pc_range (pc, startptr, endptr)
1400 CORE_ADDR pc;
1401 CORE_ADDR *startptr, *endptr;
1402 {
1403 struct symtab_and_line sal;
1404 sal = find_pc_line (pc, 0);
1405 *startptr = sal.pc;
1406 *endptr = sal.end;
1407 return sal.symtab != 0;
1408 }
1409 \f
1410 /* Parse a string that specifies a line number.
1411 Pass the address of a char * variable; that variable will be
1412 advanced over the characters actually parsed.
1413
1414 The string can be:
1415
1416 LINENUM -- that line number in current file. PC returned is 0.
1417 FILE:LINENUM -- that line in that file. PC returned is 0.
1418 FUNCTION -- line number of openbrace of that function.
1419 PC returned is the start of the function.
1420 FILE:FUNCTION -- likewise, but prefer functions in that file.
1421 *EXPR -- line in which address EXPR appears.
1422
1423 FUNCTION may be an undebuggable function found in misc_function_vector.
1424
1425 If the argument FUNFIRSTLINE is nonzero, we want the first line
1426 of real code inside a function when a function is specified.
1427
1428 DEFAULT_SYMTAB specifies the file to use if none is specified.
1429 It defaults to current_source_symtab.
1430 DEFAULT_LINE specifies the line number to use for relative
1431 line numbers (that start with signs). Defaults to current_source_line.
1432
1433 Note that it is possible to return zero for the symtab
1434 if no file is validly specified. Callers must check that.
1435 Also, the line number returned may be invalid. */
1436
1437 struct symtabs_and_lines
1438 decode_line_1 (argptr, funfirstline, default_symtab, default_line)
1439 char **argptr;
1440 int funfirstline;
1441 struct symtab *default_symtab;
1442 int default_line;
1443 {
1444 struct symtabs_and_lines decode_line_2 ();
1445 struct symtabs_and_lines values;
1446 struct symtab_and_line value;
1447 register char *p, *p1;
1448 register struct symtab *s;
1449 register struct symbol *sym;
1450 register CORE_ADDR pc;
1451 register int i;
1452 char *copy;
1453 struct symbol *sym_class;
1454 char *class_name, *method_name, *phys_name;
1455 int method_counter;
1456 int i1;
1457 struct symbol **sym_arr;
1458 struct type *t, *field;
1459 char **physnames;
1460
1461 /* Defaults have defaults. */
1462
1463 if (default_symtab == 0)
1464 {
1465 default_symtab = current_source_symtab;
1466 default_line = current_source_line;
1467 }
1468
1469 /* See if arg is *PC */
1470
1471 if (**argptr == '*')
1472 {
1473 (*argptr)++;
1474 pc = parse_and_eval_address_1 (argptr);
1475 values.sals = (struct symtab_and_line *)
1476 malloc (sizeof (struct symtab_and_line));
1477 values.nelts = 1;
1478 values.sals[0] = find_pc_line (pc, 0);
1479 values.sals[0].pc = pc;
1480 return values;
1481 }
1482
1483 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
1484
1485 s = 0;
1486
1487 for (p = *argptr; *p; p++)
1488 {
1489 if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
1490 break;
1491 }
1492 while (p[0] == ' ' || p[0] == '\t') p++;
1493
1494 if (p[0] == ':')
1495 {
1496
1497 /* C++ */
1498 if (p[1] ==':')
1499 {
1500 /* Extract the class name. */
1501 p1 = p;
1502 while (p != *argptr && p[-1] == ' ') --p;
1503 copy = (char *) alloca (p - *argptr + 1);
1504 bcopy (*argptr, copy, p - *argptr);
1505 copy[p - *argptr] = 0;
1506
1507 /* Discard the class name from the arg. */
1508 p = p1 + 2;
1509 while (*p == ' ' || *p == '\t') p++;
1510 *argptr = p;
1511
1512 sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0);
1513
1514 if (sym_class &&
1515 (TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_STRUCT
1516 || TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_UNION))
1517 {
1518 /* Arg token is not digits => try it as a function name
1519 Find the next token (everything up to end or next whitespace). */
1520 p = *argptr;
1521 while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p !=':') p++;
1522 copy = (char *) alloca (p - *argptr + 1);
1523 bcopy (*argptr, copy, p - *argptr);
1524 copy[p - *argptr] = '\0';
1525
1526 /* no line number may be specified */
1527 while (*p == ' ' || *p == '\t') p++;
1528 *argptr = p;
1529
1530 sym = 0;
1531 i1 = 0; /* counter for the symbol array */
1532 t = SYMBOL_TYPE (sym_class);
1533 sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
1534 physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
1535
1536 if (destructor_name_p (copy, t))
1537 {
1538 /* destructors are a special case. */
1539 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, 0);
1540 int len = TYPE_FN_FIELDLIST_LENGTH (t, 0) - 1;
1541 phys_name = TYPE_FN_FIELD_PHYSNAME (f, len);
1542 physnames[i1] = (char *)alloca (strlen (phys_name) + 1);
1543 strcpy (physnames[i1], phys_name);
1544 sym_arr[i1] = lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class), VAR_NAMESPACE, 0);
1545 if (sym_arr[i1]) i1++;
1546 }
1547 else while (t)
1548 {
1549 class_name = TYPE_NAME (t);
1550 /* Ignore this class if it doesn't have a name.
1551 This prevents core dumps, but is just a workaround
1552 because we might not find the function in
1553 certain cases, such as
1554 struct D {virtual int f();}
1555 struct C : D {virtual int g();}
1556 (in this case g++ 1.35.1- does not put out a name
1557 for D as such, it defines type 19 (for example) in
1558 the same stab as C, and then does a
1559 .stabs "D:T19" and a .stabs "D:t19".
1560 Thus
1561 "break C::f" should not be looking for field f in
1562 the class named D,
1563 but just for the field f in the baseclasses of C
1564 (no matter what their names).
1565
1566 However, I don't know how to replace the code below
1567 that depends on knowing the name of D. */
1568 if (class_name)
1569 {
1570 /* We just want the class name. In the context
1571 of C++, stripping off "struct " is always
1572 sensible. */
1573 if (strncmp("struct ", class_name, 7) == 0)
1574 class_name += 7;
1575 if (strncmp("union ", class_name, 6) == 0)
1576 class_name += 6;
1577
1578 sym_class = lookup_symbol (class_name, 0, STRUCT_NAMESPACE, 0);
1579 for (method_counter = TYPE_NFN_FIELDS (SYMBOL_TYPE (sym_class)) - 1;
1580 method_counter >= 0;
1581 --method_counter)
1582 {
1583 int field_counter;
1584 struct fn_field *f =
1585 TYPE_FN_FIELDLIST1 (SYMBOL_TYPE (sym_class), method_counter);
1586
1587 method_name = TYPE_FN_FIELDLIST_NAME (SYMBOL_TYPE (sym_class), method_counter);
1588 if (!strcmp (copy, method_name))
1589 /* Find all the fields with that name. */
1590 for (field_counter = TYPE_FN_FIELDLIST_LENGTH (SYMBOL_TYPE (sym_class), method_counter) - 1;
1591 field_counter >= 0;
1592 --field_counter)
1593 {
1594 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1595 physnames[i1] = (char*) alloca (strlen (phys_name) + 1);
1596 strcpy (physnames[i1], phys_name);
1597 sym_arr[i1] = lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class), VAR_NAMESPACE, 0);
1598 if (sym_arr[i1]) i1++;
1599 }
1600 }
1601 }
1602 if (TYPE_N_BASECLASSES (t))
1603 t = TYPE_BASECLASS(t, 1);
1604 else
1605 break;
1606 }
1607
1608 if (i1 == 1)
1609 {
1610 /* There is exactly one field with that name. */
1611 sym = sym_arr[0];
1612
1613 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1614 {
1615 /* Arg is the name of a function */
1616 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
1617 if (funfirstline)
1618 SKIP_PROLOGUE (pc);
1619 values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1620 values.nelts = 1;
1621 values.sals[0] = find_pc_line (pc, 0);
1622 values.sals[0].pc = (values.sals[0].end && values.sals[0].pc != pc) ? values.sals[0].end : pc;
1623 }
1624 else
1625 {
1626 values.nelts = 0;
1627 }
1628 return values;
1629 }
1630 if (i1 > 0)
1631 {
1632 /* There is more than one field with that name
1633 (overloaded). Ask the user which one to use. */
1634 return decode_line_2 (argptr, sym_arr, physnames,
1635 i1, funfirstline);
1636 }
1637 else
1638 error ("that class does not have any method named %s",copy);
1639 }
1640 else
1641 error("no class, struct, or union named %s", copy );
1642 }
1643 /* end of C++ */
1644
1645
1646 /* Extract the file name. */
1647 p1 = p;
1648 while (p != *argptr && p[-1] == ' ') --p;
1649 copy = (char *) alloca (p - *argptr + 1);
1650 bcopy (*argptr, copy, p - *argptr);
1651 copy[p - *argptr] = 0;
1652
1653 /* Find that file's data. */
1654 s = lookup_symtab (copy);
1655 if (s == 0)
1656 {
1657 if (symtab_list == 0 && partial_symtab_list == 0)
1658 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
1659 error ("No source file named %s.", copy);
1660 }
1661
1662 /* Discard the file name from the arg. */
1663 p = p1 + 1;
1664 while (*p == ' ' || *p == '\t') p++;
1665 *argptr = p;
1666 }
1667
1668 /* S is specified file's symtab, or 0 if no file specified.
1669 arg no longer contains the file name. */
1670
1671 /* Check whether arg is all digits (and sign) */
1672
1673 p = *argptr;
1674 if (*p == '-' || *p == '+') p++;
1675 while (*p >= '0' && *p <= '9')
1676 p++;
1677
1678 if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ','))
1679 {
1680 /* We found a token consisting of all digits -- at least one digit. */
1681 enum sign {none, plus, minus} sign = none;
1682
1683 /* This is where we need to make sure that we have good defaults.
1684 We must guarrantee that this section of code is never executed
1685 when we are called with just a function name, since
1686 select_source_symtab calls us with such an argument */
1687
1688 if (s == 0 && default_symtab == 0)
1689 {
1690 if (symtab_list == 0 && partial_symtab_list == 0)
1691 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
1692 select_source_symtab (0);
1693 default_symtab = current_source_symtab;
1694 default_line = current_source_line;
1695 }
1696
1697 if (**argptr == '+')
1698 sign = plus, (*argptr)++;
1699 else if (**argptr == '-')
1700 sign = minus, (*argptr)++;
1701 value.line = atoi (*argptr);
1702 switch (sign)
1703 {
1704 case plus:
1705 if (p == *argptr)
1706 value.line = 5;
1707 if (s == 0)
1708 value.line = default_line + value.line;
1709 break;
1710 case minus:
1711 if (p == *argptr)
1712 value.line = 15;
1713 if (s == 0)
1714 value.line = default_line - value.line;
1715 else
1716 value.line = 1;
1717 break;
1718 }
1719
1720 while (*p == ' ' || *p == '\t') p++;
1721 *argptr = p;
1722 if (s == 0)
1723 s = default_symtab;
1724 value.symtab = s;
1725 value.pc = 0;
1726 values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1727 values.sals[0] = value;
1728 values.nelts = 1;
1729 return values;
1730 }
1731
1732 /* Arg token is not digits => try it as a function name
1733 Find the next token (everything up to end or next whitespace). */
1734 p = *argptr;
1735 while (*p && *p != ' ' && *p != '\t' && *p != ',') p++;
1736 copy = (char *) alloca (p - *argptr + 1);
1737 bcopy (*argptr, copy, p - *argptr);
1738 copy[p - *argptr] = 0;
1739 while (*p == ' ' || *p == '\t') p++;
1740 *argptr = p;
1741
1742 /* Look up that token as a function.
1743 If file specified, use that file's per-file block to start with. */
1744
1745 sym = lookup_symbol (copy, s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1) : 0,
1746 VAR_NAMESPACE, 0);
1747
1748 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1749 {
1750 /* Arg is the name of a function */
1751 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
1752 if (funfirstline)
1753 SKIP_PROLOGUE (pc);
1754 value = find_pc_line (pc, 0);
1755 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
1756 /* Convex: no need to suppress code on first line, if any */
1757 value.pc = pc;
1758 #else
1759 value.pc = (value.end && value.pc != pc) ? value.end : pc;
1760 #endif
1761 values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1762 values.sals[0] = value;
1763 values.nelts = 1;
1764 return values;
1765 }
1766
1767 if (sym)
1768 error ("%s is not a function.", copy);
1769
1770 if ((i = lookup_misc_func (copy)) < 0)
1771 error ("Function %s not defined.", copy);
1772 else
1773 {
1774 value.symtab = 0;
1775 value.line = 0;
1776 value.pc = misc_function_vector[i].address + FUNCTION_START_OFFSET;
1777 if (funfirstline)
1778 SKIP_PROLOGUE (value.pc);
1779 values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1780 values.sals[0] = value;
1781 values.nelts = 1;
1782 return values;
1783 }
1784
1785 if (symtab_list == 0 && partial_symtab_list == 0)
1786 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
1787 error ("Function %s not defined.", copy);
1788 }
1789
1790 struct symtabs_and_lines
1791 decode_line_spec (string, funfirstline)
1792 char *string;
1793 int funfirstline;
1794 {
1795 struct symtabs_and_lines sals;
1796 if (string == 0)
1797 error ("Empty line specification.");
1798 sals = decode_line_1 (&string, funfirstline,
1799 current_source_symtab, current_source_line);
1800 if (*string)
1801 error ("Junk at end of line specification: %s", string);
1802 return sals;
1803 }
1804
1805 struct symtabs_and_lines
1806 decode_line_2 (argptr, sym_arr, physnames, nelts, funfirstline)
1807 char **argptr;
1808 struct symbol *sym_arr[];
1809 char *physnames[];
1810 int nelts;
1811 int funfirstline;
1812 {
1813 char *getenv();
1814 struct symtabs_and_lines values, return_values;
1815 register CORE_ADDR pc;
1816 char *args, *arg1, *command_line_input ();
1817 int i;
1818 char *prompt;
1819
1820 values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line));
1821 return_values.sals = (struct symtab_and_line *) malloc (nelts * sizeof(struct symtab_and_line));
1822
1823 i = 0;
1824 printf("[0] cancel\n[1] all\n");
1825 while (i < nelts)
1826 {
1827 if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
1828 {
1829 /* Arg is the name of a function */
1830 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr[i]))
1831 + FUNCTION_START_OFFSET;
1832 if (funfirstline)
1833 SKIP_PROLOGUE (pc);
1834 values.sals[i] = find_pc_line (pc, 0);
1835 printf("[%d] file:%s; line number:%d\n",
1836 (i+2), values.sals[i].symtab->filename, values.sals[i].line);
1837 }
1838 else printf ("?HERE\n");
1839 i++;
1840 }
1841
1842 if ((prompt = getenv ("PS2")) == NULL)
1843 {
1844 prompt = ">";
1845 }
1846 printf("%s ",prompt);
1847 fflush(stdout);
1848
1849 args = command_line_input (0, 0);
1850
1851 if (args == 0)
1852 error_no_arg ("one or more choice numbers");
1853
1854 i = 0;
1855 while (*args)
1856 {
1857 int num;
1858
1859 arg1 = args;
1860 while (*arg1 >= '0' && *arg1 <= '9') arg1++;
1861 if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
1862 error ("Arguments must be choice numbers.");
1863
1864 num = atoi (args);
1865
1866 if (num == 0)
1867 error ("cancelled");
1868 else if (num == 1)
1869 {
1870 bcopy (values.sals, return_values.sals, (nelts * sizeof(struct symtab_and_line)));
1871 return_values.nelts = nelts;
1872 return return_values;
1873 }
1874
1875 if (num > nelts + 2)
1876 {
1877 printf ("No choice number %d.\n", num);
1878 }
1879 else
1880 {
1881 num -= 2;
1882 if (values.sals[num].pc)
1883 {
1884 return_values.sals[i++] = values.sals[num];
1885 values.sals[num].pc = 0;
1886 }
1887 else
1888 {
1889 printf ("duplicate request for %d ignored.\n", num);
1890 }
1891 }
1892
1893 args = arg1;
1894 while (*args == ' ' || *args == '\t') args++;
1895 }
1896 return_values.nelts = i;
1897 return return_values;
1898 }
1899
1900 /* Return the index of misc function named NAME. */
1901
1902 static int
1903 lookup_misc_func (name)
1904 register char *name;
1905 {
1906 register int i;
1907
1908 for (i = 0; i < misc_function_count; i++)
1909 if (!strcmp (misc_function_vector[i].name, name))
1910 return i;
1911 return -1; /* not found */
1912 }
1913 \f
1914 /*
1915 * Slave routine for sources_info. Force line breaks at ,'s.
1916 */
1917 static void
1918 output_source_filename (name, next)
1919 char *name;
1920 int next;
1921 {
1922 static int column = 0;
1923
1924 if (column != 0 && column + strlen (name) >= 70)
1925 {
1926 printf_filtered ("\n");
1927 column = 0;
1928 }
1929 else if (column != 0)
1930 {
1931 printf_filtered (" ");
1932 column++;
1933 }
1934 printf_filtered ("%s", name);
1935 column += strlen (name);
1936 if (next)
1937 {
1938 printf_filtered (",");
1939 column++;
1940 }
1941
1942 if (!next) column = 0;
1943 }
1944
1945 static void
1946 sources_info ()
1947 {
1948 register struct symtab *s;
1949 register struct partial_symtab *ps;
1950 register int column = 0;
1951
1952 if (symtab_list == 0 && partial_symtab_list == 0)
1953 {
1954 printf ("No symbol table is loaded.\n");
1955 return;
1956 }
1957
1958 printf_filtered ("Source files for which symbols have been read in:\n\n");
1959
1960 for (s = symtab_list; s; s = s->next)
1961 output_source_filename (s->filename, s->next);
1962 printf_filtered ("\n\n");
1963
1964 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
1965
1966 for (ps = partial_symtab_list; ps; ps = ps->next)
1967 if (!ps->readin)
1968 output_source_filename (ps->filename, ps->next);
1969 printf_filtered ("\n");
1970 }
1971
1972 /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
1973 If CLASS is zero, list all symbols except functions and type names.
1974 If CLASS is 1, list only functions.
1975 If CLASS is 2, list only type names. */
1976
1977 static void sort_block_syms ();
1978
1979 static void
1980 list_symbols (regexp, class)
1981 char *regexp;
1982 int class;
1983 {
1984 register struct symtab *s;
1985 register struct partial_symtab *ps;
1986 register struct blockvector *bv;
1987 struct blockvector *prev_bv = 0;
1988 register struct block *b;
1989 register int i, j;
1990 register struct symbol *sym;
1991 struct partial_symbol *psym;
1992 char *val;
1993 static char *classnames[]
1994 = {"variable", "function", "type", "method"};
1995 int print_count = 0;
1996 int found_in_file = 0;
1997
1998 if (regexp)
1999 if (val = (char *) re_comp (regexp))
2000 error ("Invalid regexp: %s", val);
2001
2002 /* Search through the partial_symtab_list *first* for all symbols
2003 matching the regexp. That way we don't have to reproduce all of
2004 the machinery below. */
2005 for (ps = partial_symtab_list; ps; ps = ps->next)
2006 {
2007 struct partial_symbol *bound, *gbound, *sbound;
2008 int keep_going = 1;
2009
2010 if (ps->readin) continue;
2011
2012 gbound = global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2013 sbound = static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2014 bound = gbound;
2015
2016 /* Go through all of the symbols stored in a partial
2017 symtab in one loop. */
2018 psym = global_psymbols.list + ps->globals_offset;
2019 while (keep_going)
2020 {
2021 if (psym >= bound)
2022 {
2023 if (bound == gbound && ps->n_static_syms != 0)
2024 {
2025 psym = static_psymbols.list + ps->statics_offset;
2026 bound = sbound;
2027 }
2028 else
2029 keep_going = 0;
2030 }
2031 else
2032 {
2033 QUIT;
2034
2035 /* If it would match (logic taken from loop below)
2036 load the file and go on to the next one */
2037 if ((regexp == 0 || re_exec (SYMBOL_NAME (psym)))
2038 && ((class == 0 && SYMBOL_CLASS (psym) != LOC_TYPEDEF
2039 && SYMBOL_CLASS (psym) != LOC_BLOCK)
2040 || (class == 1 && SYMBOL_CLASS (psym) == LOC_BLOCK)
2041 || (class == 2 && SYMBOL_CLASS (psym) == LOC_TYPEDEF)
2042 || (class == 3 && SYMBOL_CLASS (psym) == LOC_BLOCK)))
2043 {
2044 psymtab_to_symtab(ps);
2045 keep_going = 0;
2046 }
2047 }
2048 psym++;
2049 }
2050 }
2051
2052 /* Printout here so as to get after the "Reading in symbols"
2053 messages which will be generated above. */
2054 printf_filtered (regexp
2055 ? "All %ss matching regular expression \"%s\":\n"
2056 : "All defined %ss:\n",
2057 classnames[class],
2058 regexp);
2059
2060 /* Here, *if* the class is correct (function only, right now), we
2061 should search through the misc function vector for symbols that
2062 match and call find_pc_psymtab on them. If find_pc_psymtab returns
2063 0, don't worry about it (already read in or no debugging info). */
2064
2065 if (class == 1)
2066 {
2067 for (i = 0; i < misc_function_count; i++)
2068 if (regexp == 0 || re_exec (misc_function_vector[i].name))
2069 {
2070 ps = find_pc_psymtab (misc_function_vector[i].address);
2071 if (ps && !ps->readin)
2072 psymtab_to_symtab (ps);
2073 }
2074 }
2075
2076 for (s = symtab_list; s; s = s->next)
2077 {
2078 found_in_file = 0;
2079 bv = BLOCKVECTOR (s);
2080 /* Often many files share a blockvector.
2081 Scan each blockvector only once so that
2082 we don't get every symbol many times.
2083 It happens that the first symtab in the list
2084 for any given blockvector is the main file. */
2085 if (bv != prev_bv)
2086 for (i = 0; i < 2; i++)
2087 {
2088 b = BLOCKVECTOR_BLOCK (bv, i);
2089 /* Skip the sort if this block is always sorted. */
2090 if (!BLOCK_SHOULD_SORT (b))
2091 sort_block_syms (b);
2092 for (j = 0; j < BLOCK_NSYMS (b); j++)
2093 {
2094 QUIT;
2095 sym = BLOCK_SYM (b, j);
2096 if ((regexp == 0 || re_exec (SYMBOL_NAME (sym)))
2097 && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
2098 && SYMBOL_CLASS (sym) != LOC_BLOCK)
2099 || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
2100 || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2101 || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK)))
2102 {
2103 if (!found_in_file)
2104 {
2105 printf_filtered ("\nFile %s:\n", s->filename);
2106 print_count += 2;
2107 }
2108 found_in_file = 1;
2109 if (class != 2 && i == 1)
2110 printf_filtered ("static ");
2111 if (class == 2
2112 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
2113 printf_filtered ("typedef ");
2114
2115 if (class < 3)
2116 {
2117 type_print (SYMBOL_TYPE (sym),
2118 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
2119 ? "" : SYMBOL_NAME (sym)),
2120 stdout, 0);
2121
2122 if (class == 2
2123 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE
2124 && (TYPE_NAME ((SYMBOL_TYPE (sym))) == 0
2125 || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (sym))),
2126 SYMBOL_NAME (sym))))
2127 printf_filtered (" %s", SYMBOL_NAME (sym));
2128
2129 printf_filtered (";\n");
2130 }
2131 else
2132 {
2133 char buf[1024];
2134 # if 0
2135 type_print_base (TYPE_FN_FIELD_TYPE(t, i), stdout, 0, 0);
2136 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i), stdout, 0);
2137 sprintf (buf, " %s::", TYPE_NAME (t));
2138 type_print_method_args (TYPE_FN_FIELD_ARGS (t, i), buf, name, stdout);
2139 # endif
2140 }
2141 }
2142 }
2143 }
2144 prev_bv = bv;
2145 }
2146 }
2147
2148 static void
2149 variables_info (regexp)
2150 char *regexp;
2151 {
2152 list_symbols (regexp, 0);
2153 }
2154
2155 static void
2156 functions_info (regexp)
2157 char *regexp;
2158 {
2159 list_symbols (regexp, 1);
2160 }
2161
2162 static void
2163 types_info (regexp)
2164 char *regexp;
2165 {
2166 list_symbols (regexp, 2);
2167 }
2168
2169 static void
2170 methods_info (regexp)
2171 char *regexp;
2172 {
2173 list_symbols (regexp, 3);
2174 }
2175 \f
2176 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
2177
2178 static int
2179 compare_symbols (s1, s2)
2180 struct symbol **s1, **s2;
2181 {
2182 /* Names that are less should come first. */
2183 register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
2184 if (namediff != 0) return namediff;
2185 /* For symbols of the same name, registers should come first. */
2186 return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
2187 - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
2188 }
2189
2190 static void
2191 sort_block_syms (b)
2192 register struct block *b;
2193 {
2194 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
2195 sizeof (struct symbol *), compare_symbols);
2196 }
2197 \f
2198 /* Initialize the standard C scalar types. */
2199
2200 static
2201 struct type *
2202 init_type (code, length, uns, name)
2203 enum type_code code;
2204 int length, uns;
2205 char *name;
2206 {
2207 register struct type *type;
2208
2209 type = (struct type *) xmalloc (sizeof (struct type));
2210 bzero (type, sizeof *type);
2211 TYPE_MAIN_VARIANT (type) = type;
2212 TYPE_CODE (type) = code;
2213 TYPE_LENGTH (type) = length;
2214 TYPE_FLAGS (type) = uns ? TYPE_FLAG_UNSIGNED : 0;
2215 TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
2216 TYPE_NFIELDS (type) = 0;
2217 TYPE_NAME (type) = name;
2218
2219 /* C++ fancies. */
2220 TYPE_NFN_FIELDS (type) = 0;
2221 TYPE_N_BASECLASSES (type) = 0;
2222 TYPE_BASECLASSES (type) = 0;
2223 return type;
2224 }
2225
2226 /* Return Nonzero if block a is lexically nested within block b,
2227 or if a and b have the same pc range.
2228 Return zero otherwise. */
2229 int
2230 contained_in (a, b)
2231 struct block *a, *b;
2232 {
2233 if (!a || !b)
2234 return 0;
2235 return a->startaddr >= b->startaddr && a->endaddr <= b->endaddr;
2236 }
2237
2238 \f
2239 /* Helper routine for make_symbol_completion_list. */
2240
2241 int return_val_size, return_val_index;
2242 char **return_val;
2243
2244 void
2245 completion_list_add_symbol (symname)
2246 char *symname;
2247 {
2248 if (return_val_index + 3 > return_val_size)
2249 return_val =
2250 (char **)xrealloc (return_val,
2251 (return_val_size *= 2) * sizeof (char *));
2252
2253 return_val[return_val_index] =
2254 (char *)xmalloc (1 + strlen (symname));
2255
2256 strcpy (return_val[return_val_index], symname);
2257
2258 return_val[++return_val_index] = (char *)NULL;
2259 }
2260
2261 /* Return a NULL terminated array of all symbols (regardless of class) which
2262 begin by matching TEXT. If the answer is no symbols, then the return value
2263 is an array which contains only a NULL pointer.
2264
2265 Problem: All of the symbols have to be copied because readline
2266 frees them. I'm not going to worry about this; hopefully there
2267 won't be that many. */
2268
2269 char **
2270 make_symbol_completion_list (text)
2271 char *text;
2272 {
2273 register struct symtab *s;
2274 register struct partial_symtab *ps;
2275 register struct blockvector *bv;
2276 struct blockvector *prev_bv = 0;
2277 register struct block *b, *surrounding_static_block;
2278 extern struct block *get_selected_block ();
2279 register int i, j;
2280 register struct symbol *sym;
2281 struct partial_symbol *psym;
2282
2283 int text_len = strlen (text);
2284 return_val_size = 100;
2285 return_val_index = 0;
2286 return_val =
2287 (char **)xmalloc ((1 + return_val_size) *sizeof (char *));
2288 return_val[0] = (char *)NULL;
2289
2290 /* Look through the partial symtabs for all symbols which begin
2291 by matching TEXT. Add each one that you find to the list. */
2292
2293 for (ps = partial_symtab_list; ps; ps = ps->next)
2294 {
2295 /* If the psymtab's been read in we'll get it when we search
2296 through the blockvector. */
2297 if (ps->readin) continue;
2298
2299 for (psym = global_psymbols.list + ps->globals_offset;
2300 psym < (global_psymbols.list + ps->globals_offset
2301 + ps->n_global_syms);
2302 psym++)
2303 {
2304 QUIT; /* If interrupted, then quit. */
2305 if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0))
2306 completion_list_add_symbol (SYMBOL_NAME (psym));
2307 }
2308
2309 for (psym = static_psymbols.list + ps->statics_offset;
2310 psym < (static_psymbols.list + ps->statics_offset
2311 + ps->n_static_syms);
2312 psym++)
2313 {
2314 QUIT;
2315 if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0))
2316 completion_list_add_symbol (SYMBOL_NAME (psym));
2317 }
2318 }
2319
2320 /* At this point scan through the misc function vector and add each
2321 symbol you find to the list. Eventually we want to ignore
2322 anything that isn't a text symbol (everything else will be
2323 handled by the psymtab code above). */
2324
2325 for (i = 0; i < misc_function_count; i++)
2326 if (!strncmp (text, misc_function_vector[i].name, text_len))
2327 completion_list_add_symbol (misc_function_vector[i].name);
2328
2329 /* Search upwards from currently selected frame (so that we can
2330 complete on local vars. */
2331 for (b = get_selected_block (); b; b = BLOCK_SUPERBLOCK (b))
2332 {
2333 if (!BLOCK_SUPERBLOCK (b))
2334 surrounding_static_block = b; /* For elmin of dups */
2335
2336 /* Also catch fields of types defined in this places which
2337 match our text string. Only complete on types visible
2338 from current context. */
2339 for (i = 0; i < BLOCK_NSYMS (b); i++)
2340 {
2341 register struct symbol *sym = BLOCK_SYM (b, i);
2342
2343 if (!strncmp (SYMBOL_NAME (sym), text, text_len))
2344 completion_list_add_symbol (SYMBOL_NAME (sym));
2345
2346 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2347 {
2348 struct type *t = SYMBOL_TYPE (sym);
2349 enum type_code c = TYPE_CODE (t);
2350
2351 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
2352 for (j = 0; j < TYPE_NFIELDS (t); j++)
2353 if (TYPE_FIELD_NAME (t, j) &&
2354 !strncmp (TYPE_FIELD_NAME (t, j), text, text_len))
2355 completion_list_add_symbol (TYPE_FIELD_NAME (t, j));
2356 }
2357 }
2358 }
2359
2360 /* Go through the symtabs and check the externs and statics for
2361 symbols which match. */
2362
2363 for (s = symtab_list; s; s = s->next)
2364 {
2365 struct block *b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 0);
2366
2367 for (i = 0; i < BLOCK_NSYMS (b); i++)
2368 if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len))
2369 completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i)));
2370 }
2371
2372 for (s = symtab_list; s; s = s->next)
2373 {
2374 struct block *b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1);
2375
2376 /* Don't do this block twice. */
2377 if (b == surrounding_static_block) continue;
2378
2379 for (i = 0; i < BLOCK_NSYMS (b); i++)
2380 if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len))
2381 completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i)));
2382 }
2383
2384 return (return_val);
2385 }
2386 \f
2387 void
2388 _initialize_symtab ()
2389 {
2390 add_info ("variables", variables_info,
2391 "All global and static variable names, or those matching REGEXP.");
2392 add_info ("functions", functions_info,
2393 "All function names, or those matching REGEXP.");
2394 add_info ("types", types_info,
2395 "All types names, or those matching REGEXP.");
2396 add_info ("methods", methods_info,
2397 "All method names, or those matching REGEXP::REGEXP.\n\
2398 If the class qualifier is ommited, it is assumed to be the current scope.\n\
2399 If the first REGEXP is ommited, then all methods matching the second REGEXP\n\
2400 are listed.");
2401 add_info ("sources", sources_info,
2402 "Source files in the program.");
2403
2404 obstack_init (symbol_obstack);
2405 obstack_init (psymbol_obstack);
2406
2407 builtin_type_void = init_type (TYPE_CODE_VOID, 1, 0, "void");
2408
2409 builtin_type_float = init_type (TYPE_CODE_FLT, sizeof (float), 0, "float");
2410 builtin_type_double = init_type (TYPE_CODE_FLT, sizeof (double), 0, "double");
2411
2412 builtin_type_char = init_type (TYPE_CODE_INT, sizeof (char), 0, "char");
2413 builtin_type_short = init_type (TYPE_CODE_INT, sizeof (short), 0, "short");
2414 builtin_type_long = init_type (TYPE_CODE_INT, sizeof (long), 0, "long");
2415 builtin_type_int = init_type (TYPE_CODE_INT, sizeof (int), 0, "int");
2416
2417 builtin_type_unsigned_char = init_type (TYPE_CODE_INT, sizeof (char), 1, "unsigned char");
2418 builtin_type_unsigned_short = init_type (TYPE_CODE_INT, sizeof (short), 1, "unsigned short");
2419 builtin_type_unsigned_long = init_type (TYPE_CODE_INT, sizeof (long), 1, "unsigned long");
2420 builtin_type_unsigned_int = init_type (TYPE_CODE_INT, sizeof (int), 1, "unsigned int");
2421 #ifdef LONG_LONG
2422 builtin_type_long_long =
2423 init_type (TYPE_CODE_INT, sizeof (long long), 0, "long long");
2424 builtin_type_unsigned_long_long =
2425 init_type (TYPE_CODE_INT, sizeof (long long), 1, "unsigned long long");
2426 #endif
2427 }
2428