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