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