gdb-3.3
[deliverable/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 while (*class_name++ != ' ');
1571
1572 sym_class = lookup_symbol (class_name, 0, STRUCT_NAMESPACE, 0);
1573 for (method_counter = TYPE_NFN_FIELDS (SYMBOL_TYPE (sym_class)) - 1;
1574 method_counter >= 0;
1575 --method_counter)
1576 {
1577 int field_counter;
1578 struct fn_field *f =
1579 TYPE_FN_FIELDLIST1 (SYMBOL_TYPE (sym_class), method_counter);
1580
1581 method_name = TYPE_FN_FIELDLIST_NAME (SYMBOL_TYPE (sym_class), method_counter);
1582 if (!strcmp (copy, method_name))
1583 /* Find all the fields with that name. */
1584 for (field_counter = TYPE_FN_FIELDLIST_LENGTH (SYMBOL_TYPE (sym_class), method_counter) - 1;
1585 field_counter >= 0;
1586 --field_counter)
1587 {
1588 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1589 physnames[i1] = (char*) alloca (strlen (phys_name) + 1);
1590 strcpy (physnames[i1], phys_name);
1591 sym_arr[i1] = lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class), VAR_NAMESPACE, 0);
1592 if (sym_arr[i1]) i1++;
1593 }
1594 }
1595 }
1596 if (TYPE_N_BASECLASSES (t))
1597 t = TYPE_BASECLASS(t, 1);
1598 else
1599 break;
1600 }
1601
1602 if (i1 == 1)
1603 {
1604 /* There is exactly one field with that name. */
1605 sym = sym_arr[0];
1606
1607 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1608 {
1609 /* Arg is the name of a function */
1610 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
1611 if (funfirstline)
1612 SKIP_PROLOGUE (pc);
1613 values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1614 values.nelts = 1;
1615 values.sals[0] = find_pc_line (pc, 0);
1616 values.sals[0].pc = (values.sals[0].end && values.sals[0].pc != pc) ? values.sals[0].end : pc;
1617 }
1618 else
1619 {
1620 values.nelts = 0;
1621 }
1622 return values;
1623 }
1624 if (i1 > 0)
1625 {
1626 /* There is more than one field with that name
1627 (overloaded). Ask the user which one to use. */
1628 return decode_line_2 (argptr, sym_arr, physnames,
1629 i1, funfirstline);
1630 }
1631 else
1632 error ("that class does not have any method named %s",copy);
1633 }
1634 else
1635 error("no class, struct, or union named %s", copy );
1636 }
1637 /* end of C++ */
1638
1639
1640 /* Extract the file name. */
1641 p1 = p;
1642 while (p != *argptr && p[-1] == ' ') --p;
1643 copy = (char *) alloca (p - *argptr + 1);
1644 bcopy (*argptr, copy, p - *argptr);
1645 copy[p - *argptr] = 0;
1646
1647 /* Find that file's data. */
1648 s = lookup_symtab (copy);
1649 if (s == 0)
1650 {
1651 if (symtab_list == 0 && partial_symtab_list == 0)
1652 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
1653 error ("No source file named %s.", copy);
1654 }
1655
1656 /* Discard the file name from the arg. */
1657 p = p1 + 1;
1658 while (*p == ' ' || *p == '\t') p++;
1659 *argptr = p;
1660 }
1661
1662 /* S is specified file's symtab, or 0 if no file specified.
1663 arg no longer contains the file name. */
1664
1665 /* Check whether arg is all digits (and sign) */
1666
1667 p = *argptr;
1668 if (*p == '-' || *p == '+') p++;
1669 while (*p >= '0' && *p <= '9')
1670 p++;
1671
1672 if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ','))
1673 {
1674 /* We found a token consisting of all digits -- at least one digit. */
1675 enum sign {none, plus, minus} sign = none;
1676
1677 /* This is where we need to make sure that we have good defaults.
1678 We must guarrantee that this section of code is never executed
1679 when we are called with just a function name, since
1680 select_source_symtab calls us with such an argument */
1681
1682 if (s == 0 && default_symtab == 0)
1683 {
1684 if (symtab_list == 0 && partial_symtab_list == 0)
1685 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
1686 select_source_symtab (0);
1687 default_symtab = current_source_symtab;
1688 default_line = current_source_line;
1689 }
1690
1691 if (**argptr == '+')
1692 sign = plus, (*argptr)++;
1693 else if (**argptr == '-')
1694 sign = minus, (*argptr)++;
1695 value.line = atoi (*argptr);
1696 switch (sign)
1697 {
1698 case plus:
1699 if (p == *argptr)
1700 value.line = 5;
1701 if (s == 0)
1702 value.line = default_line + value.line;
1703 break;
1704 case minus:
1705 if (p == *argptr)
1706 value.line = 15;
1707 if (s == 0)
1708 value.line = default_line - value.line;
1709 else
1710 value.line = 1;
1711 break;
1712 }
1713
1714 while (*p == ' ' || *p == '\t') p++;
1715 *argptr = p;
1716 if (s == 0)
1717 s = default_symtab;
1718 value.symtab = s;
1719 value.pc = 0;
1720 values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1721 values.sals[0] = value;
1722 values.nelts = 1;
1723 return values;
1724 }
1725
1726 /* Arg token is not digits => try it as a function name
1727 Find the next token (everything up to end or next whitespace). */
1728 p = *argptr;
1729 while (*p && *p != ' ' && *p != '\t' && *p != ',') p++;
1730 copy = (char *) alloca (p - *argptr + 1);
1731 bcopy (*argptr, copy, p - *argptr);
1732 copy[p - *argptr] = 0;
1733 while (*p == ' ' || *p == '\t') p++;
1734 *argptr = p;
1735
1736 /* Look up that token as a function.
1737 If file specified, use that file's per-file block to start with. */
1738
1739 sym = lookup_symbol (copy, s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1) : 0,
1740 VAR_NAMESPACE, 0);
1741
1742 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1743 {
1744 /* Arg is the name of a function */
1745 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
1746 if (funfirstline)
1747 SKIP_PROLOGUE (pc);
1748 value = find_pc_line (pc, 0);
1749 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
1750 /* Convex: no need to suppress code on first line, if any */
1751 value.pc = pc;
1752 #else
1753 value.pc = (value.end && value.pc != pc) ? value.end : pc;
1754 #endif
1755 values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1756 values.sals[0] = value;
1757 values.nelts = 1;
1758 return values;
1759 }
1760
1761 if (sym)
1762 error ("%s is not a function.", copy);
1763
1764 if ((i = lookup_misc_func (copy)) < 0)
1765 error ("Function %s not defined.", copy);
1766 else
1767 {
1768 value.symtab = 0;
1769 value.line = 0;
1770 value.pc = misc_function_vector[i].address + FUNCTION_START_OFFSET;
1771 if (funfirstline)
1772 SKIP_PROLOGUE (value.pc);
1773 values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1774 values.sals[0] = value;
1775 values.nelts = 1;
1776 return values;
1777 }
1778
1779 if (symtab_list == 0 && partial_symtab_list == 0)
1780 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
1781 error ("Function %s not defined.", copy);
1782 }
1783
1784 struct symtabs_and_lines
1785 decode_line_spec (string, funfirstline)
1786 char *string;
1787 int funfirstline;
1788 {
1789 struct symtabs_and_lines sals;
1790 if (string == 0)
1791 error ("Empty line specification.");
1792 sals = decode_line_1 (&string, funfirstline,
1793 current_source_symtab, current_source_line);
1794 if (*string)
1795 error ("Junk at end of line specification: %s", string);
1796 return sals;
1797 }
1798
1799 struct symtabs_and_lines
1800 decode_line_2 (argptr, sym_arr, physnames, nelts, funfirstline)
1801 char **argptr;
1802 struct symbol *sym_arr[];
1803 char *physnames[];
1804 int nelts;
1805 int funfirstline;
1806 {
1807 char *getenv();
1808 struct symtabs_and_lines values, return_values;
1809 register CORE_ADDR pc;
1810 char *args, *arg1, *command_line_input ();
1811 int i;
1812 char *prompt;
1813
1814 values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line));
1815 return_values.sals = (struct symtab_and_line *) malloc (nelts * sizeof(struct symtab_and_line));
1816
1817 i = 0;
1818 printf("[0] cancel\n[1] all\n");
1819 while (i < nelts)
1820 {
1821 if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
1822 {
1823 /* Arg is the name of a function */
1824 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr[i]))
1825 + FUNCTION_START_OFFSET;
1826 if (funfirstline)
1827 SKIP_PROLOGUE (pc);
1828 values.sals[i] = find_pc_line (pc, 0);
1829 printf("[%d] file:%s; line number:%d\n",
1830 (i+2), values.sals[i].symtab->filename, values.sals[i].line);
1831 }
1832 else printf ("?HERE\n");
1833 i++;
1834 }
1835
1836 if ((prompt = getenv ("PS2")) == NULL)
1837 {
1838 prompt = ">";
1839 }
1840 printf("%s ",prompt);
1841 fflush(stdout);
1842
1843 args = command_line_input (0, 0);
1844
1845 if (args == 0)
1846 error_no_arg ("one or more choice numbers");
1847
1848 i = 0;
1849 while (*args)
1850 {
1851 int num;
1852
1853 arg1 = args;
1854 while (*arg1 >= '0' && *arg1 <= '9') arg1++;
1855 if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
1856 error ("Arguments must be choice numbers.");
1857
1858 num = atoi (args);
1859
1860 if (num == 0)
1861 error ("cancelled");
1862 else if (num == 1)
1863 {
1864 bcopy (values.sals, return_values.sals, (nelts * sizeof(struct symtab_and_line)));
1865 return_values.nelts = nelts;
1866 return return_values;
1867 }
1868
1869 if (num > nelts + 2)
1870 {
1871 printf ("No choice number %d.\n", num);
1872 }
1873 else
1874 {
1875 num -= 2;
1876 if (values.sals[num].pc)
1877 {
1878 return_values.sals[i++] = values.sals[num];
1879 values.sals[num].pc = 0;
1880 }
1881 else
1882 {
1883 printf ("duplicate request for %d ignored.\n", num);
1884 }
1885 }
1886
1887 args = arg1;
1888 while (*args == ' ' || *args == '\t') args++;
1889 }
1890 return_values.nelts = i;
1891 return return_values;
1892 }
1893
1894 /* Return the index of misc function named NAME. */
1895
1896 static int
1897 lookup_misc_func (name)
1898 register char *name;
1899 {
1900 register int i;
1901
1902 for (i = 0; i < misc_function_count; i++)
1903 if (!strcmp (misc_function_vector[i].name, name))
1904 return i;
1905 return -1; /* not found */
1906 }
1907 \f
1908 /*
1909 * Slave routine for sources_info. Force line breaks at ,'s.
1910 */
1911 static void
1912 output_source_filename (name, next)
1913 char *name;
1914 int next;
1915 {
1916 static int column = 0;
1917
1918 if (column != 0 && column + strlen (name) >= 70)
1919 {
1920 printf_filtered ("\n");
1921 column = 0;
1922 }
1923 else if (column != 0)
1924 {
1925 printf_filtered (" ");
1926 column++;
1927 }
1928 printf_filtered ("%s", name);
1929 column += strlen (name);
1930 if (next)
1931 {
1932 printf_filtered (",");
1933 column++;
1934 }
1935
1936 if (!next) column = 0;
1937 }
1938
1939 static void
1940 sources_info ()
1941 {
1942 register struct symtab *s;
1943 register struct partial_symtab *ps;
1944 register int column = 0;
1945
1946 if (symtab_list == 0 && partial_symtab_list == 0)
1947 {
1948 printf ("No symbol table is loaded.\n");
1949 return;
1950 }
1951
1952 printf_filtered ("Source files for which symbols have been read in:\n\n");
1953
1954 for (s = symtab_list; s; s = s->next)
1955 output_source_filename (s->filename, s->next);
1956 printf_filtered ("\n\n");
1957
1958 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
1959
1960 for (ps = partial_symtab_list; ps; ps = ps->next)
1961 if (!ps->readin)
1962 output_source_filename (ps->filename, ps->next);
1963 printf_filtered ("\n");
1964 }
1965
1966 /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
1967 If CLASS is zero, list all symbols except functions and type names.
1968 If CLASS is 1, list only functions.
1969 If CLASS is 2, list only type names. */
1970
1971 static void sort_block_syms ();
1972
1973 static void
1974 list_symbols (regexp, class)
1975 char *regexp;
1976 int class;
1977 {
1978 register struct symtab *s;
1979 register struct partial_symtab *ps;
1980 register struct blockvector *bv;
1981 struct blockvector *prev_bv = 0;
1982 register struct block *b;
1983 register int i, j;
1984 register struct symbol *sym;
1985 struct partial_symbol *psym;
1986 char *val;
1987 static char *classnames[]
1988 = {"variable", "function", "type", "method"};
1989 int print_count = 0;
1990 int found_in_file = 0;
1991
1992 if (regexp)
1993 if (val = (char *) re_comp (regexp))
1994 error ("Invalid regexp: %s", val);
1995
1996 /* Search through the partial_symtab_list *first* for all symbols
1997 matching the regexp. That way we don't have to reproduce all of
1998 the machinery below. */
1999 for (ps = partial_symtab_list; ps; ps = ps->next)
2000 {
2001 struct partial_symbol *bound, *gbound, *sbound;
2002 int keep_going = 1;
2003
2004 if (ps->readin) continue;
2005
2006 gbound = global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2007 sbound = static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2008 bound = gbound;
2009
2010 /* Go through all of the symbols stored in a partial
2011 symtab in one loop. */
2012 psym = global_psymbols.list + ps->globals_offset;
2013 while (keep_going)
2014 {
2015 if (psym >= bound)
2016 {
2017 if (bound == gbound && ps->n_static_syms != 0)
2018 {
2019 psym = static_psymbols.list + ps->statics_offset;
2020 bound = sbound;
2021 }
2022 else
2023 keep_going = 0;
2024 }
2025 else
2026 {
2027 QUIT;
2028
2029 /* If it would match (logic taken from loop below)
2030 load the file and go on to the next one */
2031 if ((regexp == 0 || re_exec (SYMBOL_NAME (psym)))
2032 && ((class == 0 && SYMBOL_CLASS (psym) != LOC_TYPEDEF
2033 && SYMBOL_CLASS (psym) != LOC_BLOCK)
2034 || (class == 1 && SYMBOL_CLASS (psym) == LOC_BLOCK)
2035 || (class == 2 && SYMBOL_CLASS (psym) == LOC_TYPEDEF)
2036 || (class == 3 && SYMBOL_CLASS (psym) == LOC_BLOCK)))
2037 {
2038 psymtab_to_symtab(ps);
2039 keep_going = 0;
2040 }
2041 }
2042 psym++;
2043 }
2044 }
2045
2046 /* Printout here so as to get after the "Reading in symbols"
2047 messages which will be generated above. */
2048 printf_filtered (regexp
2049 ? "All %ss matching regular expression \"%s\":\n"
2050 : "All defined %ss:\n",
2051 classnames[class],
2052 regexp);
2053
2054 /* Here, *if* the class is correct (function only, right now), we
2055 should search through the misc function vector for symbols that
2056 match and call find_pc_psymtab on them. If find_pc_psymtab returns
2057 0, don't worry about it (already read in or no debugging info). */
2058
2059 if (class == 1)
2060 {
2061 for (i = 0; i < misc_function_count; i++)
2062 if (regexp == 0 || re_exec (misc_function_vector[i].name))
2063 {
2064 ps = find_pc_psymtab (misc_function_vector[i].address);
2065 if (ps && !ps->readin)
2066 psymtab_to_symtab (ps);
2067 }
2068 }
2069
2070 for (s = symtab_list; s; s = s->next)
2071 {
2072 found_in_file = 0;
2073 bv = BLOCKVECTOR (s);
2074 /* Often many files share a blockvector.
2075 Scan each blockvector only once so that
2076 we don't get every symbol many times.
2077 It happens that the first symtab in the list
2078 for any given blockvector is the main file. */
2079 if (bv != prev_bv)
2080 for (i = 0; i < 2; i++)
2081 {
2082 b = BLOCKVECTOR_BLOCK (bv, i);
2083 /* Skip the sort if this block is always sorted. */
2084 if (!BLOCK_SHOULD_SORT (b))
2085 sort_block_syms (b);
2086 for (j = 0; j < BLOCK_NSYMS (b); j++)
2087 {
2088 QUIT;
2089 sym = BLOCK_SYM (b, j);
2090 if ((regexp == 0 || re_exec (SYMBOL_NAME (sym)))
2091 && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
2092 && SYMBOL_CLASS (sym) != LOC_BLOCK)
2093 || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
2094 || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2095 || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK)))
2096 {
2097 if (!found_in_file)
2098 {
2099 printf_filtered ("\nFile %s:\n", s->filename);
2100 print_count += 2;
2101 }
2102 found_in_file = 1;
2103 if (class != 2 && i == 1)
2104 printf_filtered ("static ");
2105 if (class == 2
2106 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
2107 printf_filtered ("typedef ");
2108
2109 if (class < 3)
2110 {
2111 type_print (SYMBOL_TYPE (sym),
2112 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
2113 ? "" : SYMBOL_NAME (sym)),
2114 stdout, 0);
2115
2116 if (class == 2
2117 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE
2118 && (TYPE_NAME ((SYMBOL_TYPE (sym))) == 0
2119 || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (sym))),
2120 SYMBOL_NAME (sym))))
2121 printf_filtered (" %s", SYMBOL_NAME (sym));
2122
2123 printf_filtered (";\n");
2124 }
2125 else
2126 {
2127 char buf[1024];
2128 # if 0
2129 type_print_base (TYPE_FN_FIELD_TYPE(t, i), stdout, 0, 0);
2130 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i), stdout, 0);
2131 sprintf (buf, " %s::", TYPE_NAME (t));
2132 type_print_method_args (TYPE_FN_FIELD_ARGS (t, i), buf, name, stdout);
2133 # endif
2134 }
2135 }
2136 }
2137 }
2138 prev_bv = bv;
2139 }
2140 }
2141
2142 static void
2143 variables_info (regexp)
2144 char *regexp;
2145 {
2146 list_symbols (regexp, 0);
2147 }
2148
2149 static void
2150 functions_info (regexp)
2151 char *regexp;
2152 {
2153 list_symbols (regexp, 1);
2154 }
2155
2156 static void
2157 types_info (regexp)
2158 char *regexp;
2159 {
2160 list_symbols (regexp, 2);
2161 }
2162
2163 static void
2164 methods_info (regexp)
2165 char *regexp;
2166 {
2167 list_symbols (regexp, 3);
2168 }
2169 \f
2170 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
2171
2172 static int
2173 compare_symbols (s1, s2)
2174 struct symbol **s1, **s2;
2175 {
2176 /* Names that are less should come first. */
2177 register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
2178 if (namediff != 0) return namediff;
2179 /* For symbols of the same name, registers should come first. */
2180 return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
2181 - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
2182 }
2183
2184 static void
2185 sort_block_syms (b)
2186 register struct block *b;
2187 {
2188 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
2189 sizeof (struct symbol *), compare_symbols);
2190 }
2191 \f
2192 /* Initialize the standard C scalar types. */
2193
2194 static
2195 struct type *
2196 init_type (code, length, uns, name)
2197 enum type_code code;
2198 int length, uns;
2199 char *name;
2200 {
2201 register struct type *type;
2202
2203 type = (struct type *) xmalloc (sizeof (struct type));
2204 bzero (type, sizeof *type);
2205 TYPE_MAIN_VARIANT (type) = type;
2206 TYPE_CODE (type) = code;
2207 TYPE_LENGTH (type) = length;
2208 TYPE_FLAGS (type) = uns ? TYPE_FLAG_UNSIGNED : 0;
2209 TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
2210 TYPE_NFIELDS (type) = 0;
2211 TYPE_NAME (type) = name;
2212
2213 /* C++ fancies. */
2214 TYPE_NFN_FIELDS (type) = 0;
2215 TYPE_N_BASECLASSES (type) = 0;
2216 TYPE_BASECLASSES (type) = 0;
2217 return type;
2218 }
2219
2220 /* Return Nonzero if block a is lexically nested within block b,
2221 or if a and b have the same pc range.
2222 Return zero otherwise. */
2223 int
2224 contained_in (a, b)
2225 struct block *a, *b;
2226 {
2227 if (!a || !b)
2228 return 0;
2229 return a->startaddr >= b->startaddr && a->endaddr <= b->endaddr;
2230 }
2231
2232 \f
2233 /* Helper routine for make_symbol_completion_list. */
2234
2235 int return_val_size, return_val_index;
2236 char **return_val;
2237
2238 void
2239 completion_list_add_symbol (symname)
2240 char *symname;
2241 {
2242 if (return_val_index + 3 > return_val_size)
2243 return_val =
2244 (char **)xrealloc (return_val,
2245 (return_val_size *= 2) * sizeof (char *));
2246
2247 return_val[return_val_index] =
2248 (char *)xmalloc (1 + strlen (symname));
2249
2250 strcpy (return_val[return_val_index], symname);
2251
2252 return_val[++return_val_index] = (char *)NULL;
2253 }
2254
2255 /* Return a NULL terminated array of all symbols (regardless of class) which
2256 begin by matching TEXT. If the answer is no symbols, then the return value
2257 is an array which contains only a NULL pointer.
2258
2259 Problem: All of the symbols have to be copied because readline
2260 frees them. I'm not going to worry about this; hopefully there
2261 won't be that many. */
2262
2263 char **
2264 make_symbol_completion_list (text)
2265 char *text;
2266 {
2267 register struct symtab *s;
2268 register struct partial_symtab *ps;
2269 register struct blockvector *bv;
2270 struct blockvector *prev_bv = 0;
2271 register struct block *b, *surrounding_static_block;
2272 extern struct block *get_selected_block ();
2273 register int i, j;
2274 register struct symbol *sym;
2275 struct partial_symbol *psym;
2276
2277 int text_len = strlen (text);
2278 return_val_size = 100;
2279 return_val_index = 0;
2280 return_val =
2281 (char **)xmalloc ((1 + return_val_size) *sizeof (char *));
2282 return_val[0] = (char *)NULL;
2283
2284 /* Look through the partial symtabs for all symbols which begin
2285 by matching TEXT. Add each one that you find to the list. */
2286
2287 for (ps = partial_symtab_list; ps; ps = ps->next)
2288 {
2289 /* If the psymtab's been read in we'll get it when we search
2290 through the blockvector. */
2291 if (ps->readin) continue;
2292
2293 for (psym = global_psymbols.list + ps->globals_offset;
2294 psym < (global_psymbols.list + ps->globals_offset
2295 + ps->n_global_syms);
2296 psym++)
2297 {
2298 QUIT; /* If interrupted, then quit. */
2299 if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0))
2300 completion_list_add_symbol (SYMBOL_NAME (psym));
2301 }
2302
2303 for (psym = static_psymbols.list + ps->statics_offset;
2304 psym < (static_psymbols.list + ps->statics_offset
2305 + ps->n_static_syms);
2306 psym++)
2307 {
2308 QUIT;
2309 if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0))
2310 completion_list_add_symbol (SYMBOL_NAME (psym));
2311 }
2312 }
2313
2314 /* At this point scan through the misc function vector and add each
2315 symbol you find to the list. Eventually we want to ignore
2316 anything that isn't a text symbol (everything else will be
2317 handled by the psymtab code above). */
2318
2319 for (i = 0; i < misc_function_count; i++)
2320 if (!strncmp (text, misc_function_vector[i].name, text_len))
2321 completion_list_add_symbol (misc_function_vector[i].name);
2322
2323 /* Search upwards from currently selected frame (so that we can
2324 complete on local vars. */
2325 for (b = get_selected_block (); b; b = BLOCK_SUPERBLOCK (b))
2326 {
2327 if (!BLOCK_SUPERBLOCK (b))
2328 surrounding_static_block = b; /* For elmin of dups */
2329
2330 /* Also catch fields of types defined in this places which
2331 match our text string. Only complete on types visible
2332 from current context. */
2333 for (i = 0; i < BLOCK_NSYMS (b); i++)
2334 {
2335 register struct symbol *sym = BLOCK_SYM (b, i);
2336
2337 if (!strncmp (SYMBOL_NAME (sym), text, text_len))
2338 completion_list_add_symbol (SYMBOL_NAME (sym));
2339
2340 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2341 {
2342 struct type *t = SYMBOL_TYPE (sym);
2343 enum type_code c = TYPE_CODE (t);
2344
2345 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
2346 for (j = 0; j < TYPE_NFIELDS (t); j++)
2347 if (TYPE_FIELD_NAME (t, j) &&
2348 !strncmp (TYPE_FIELD_NAME (t, j), text, text_len))
2349 completion_list_add_symbol (TYPE_FIELD_NAME (t, j));
2350 }
2351 }
2352 }
2353
2354 /* Go through the symtabs and check the externs and statics for
2355 symbols which match. */
2356
2357 for (s = symtab_list; s; s = s->next)
2358 {
2359 struct block *b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 0);
2360
2361 for (i = 0; i < BLOCK_NSYMS (b); i++)
2362 if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len))
2363 completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i)));
2364 }
2365
2366 for (s = symtab_list; s; s = s->next)
2367 {
2368 struct block *b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1);
2369
2370 /* Don't do this block twice. */
2371 if (b == surrounding_static_block) continue;
2372
2373 for (i = 0; i < BLOCK_NSYMS (b); i++)
2374 if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len))
2375 completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i)));
2376 }
2377
2378 return (return_val);
2379 }
2380 \f
2381 void
2382 _initialize_symtab ()
2383 {
2384 add_info ("variables", variables_info,
2385 "All global and static variable names, or those matching REGEXP.");
2386 add_info ("functions", functions_info,
2387 "All function names, or those matching REGEXP.");
2388 add_info ("types", types_info,
2389 "All types names, or those matching REGEXP.");
2390 add_info ("methods", methods_info,
2391 "All method names, or those matching REGEXP::REGEXP.\n\
2392 If the class qualifier is ommited, it is assumed to be the current scope.\n\
2393 If the first REGEXP is ommited, then all methods matching the second REGEXP\n\
2394 are listed.");
2395 add_info ("sources", sources_info,
2396 "Source files in the program.");
2397
2398 obstack_init (symbol_obstack);
2399 obstack_init (psymbol_obstack);
2400
2401 builtin_type_void = init_type (TYPE_CODE_VOID, 1, 0, "void");
2402
2403 builtin_type_float = init_type (TYPE_CODE_FLT, sizeof (float), 0, "float");
2404 builtin_type_double = init_type (TYPE_CODE_FLT, sizeof (double), 0, "double");
2405
2406 builtin_type_char = init_type (TYPE_CODE_INT, sizeof (char), 0, "char");
2407 builtin_type_short = init_type (TYPE_CODE_INT, sizeof (short), 0, "short");
2408 builtin_type_long = init_type (TYPE_CODE_INT, sizeof (long), 0, "long");
2409 builtin_type_int = init_type (TYPE_CODE_INT, sizeof (int), 0, "int");
2410
2411 builtin_type_unsigned_char = init_type (TYPE_CODE_INT, sizeof (char), 1, "unsigned char");
2412 builtin_type_unsigned_short = init_type (TYPE_CODE_INT, sizeof (short), 1, "unsigned short");
2413 builtin_type_unsigned_long = init_type (TYPE_CODE_INT, sizeof (long), 1, "unsigned long");
2414 builtin_type_unsigned_int = init_type (TYPE_CODE_INT, sizeof (int), 1, "unsigned int");
2415 #ifdef LONG_LONG
2416 builtin_type_long_long =
2417 init_type (TYPE_CODE_INT, sizeof (long long), 0, "long long");
2418 builtin_type_unsigned_long_long =
2419 init_type (TYPE_CODE_INT, sizeof (long long), 1, "unsigned long long");
2420 #endif
2421 }
2422
This page took 0.086229 seconds and 4 git commands to generate.