gdb-2.8.1
[platform/upstream/binutils.git] / gdb / symtab.c
1 /* Symbol table lookup for the GNU debugger, GDB.
2    Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
3
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY.  No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
9
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License.  A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities.  It
14 should be in a file named COPYING.  Among other things, the copyright
15 notice and this notice must be preserved on all copies.
16
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther.  Help stamp out software hoarding!
19 */
20
21 #include "defs.h"
22 #include "initialize.h"
23 #include "symtab.h"
24 #include "param.h"
25
26 #include <stdio.h>
27 #include <obstack.h>
28
29 START_FILE
30
31 static int find_line_common();
32 static int lookup_misc_func();
33
34 /* Allocate an obstack to hold objects that should be freed
35    when we load a new symbol table.
36    This includes the symbols made by dbxread
37    and the types that are not permanent.  */
38
39 struct obstack obstack1;
40
41 struct obstack *symbol_obstack = &obstack1;
42
43 /* These variables point to the objects
44    representing the predefined C data types.  */
45
46 struct type *builtin_type_void;
47 struct type *builtin_type_char;
48 struct type *builtin_type_short;
49 struct type *builtin_type_int;
50 struct type *builtin_type_long;
51 struct type *builtin_type_unsigned_char;
52 struct type *builtin_type_unsigned_short;
53 struct type *builtin_type_unsigned_int;
54 struct type *builtin_type_unsigned_long;
55 struct type *builtin_type_float;
56 struct type *builtin_type_double;
57
58 /* Lookup the symbol table of a source file named NAME.  */
59
60 struct symtab *
61 lookup_symtab (name)
62      char *name;
63 {
64   register struct symtab *s;
65   register char *copy;
66
67   for (s = symtab_list; s; s = s->next)
68     if (!strcmp (name, s->filename))
69       return s;
70
71   /* If name not found as specified, see if adding ".c" helps.  */
72
73   copy = (char *) alloca (strlen (name) + 3);
74   strcpy (copy, name);
75   strcat (copy, ".c");
76   for (s = symtab_list; s; s = s->next)
77     if (!strcmp (copy, s->filename))
78       return s;
79
80   return 0;
81 }
82 \f
83 /* Lookup a typedef or primitive type named NAME,
84    visible in lexical block BLOCK.
85    If NOERR is nonzero, return zero if NAME is not suitably defined.  */
86
87 struct type *
88 lookup_typename (name, block, noerr)
89      char *name;
90      struct block *block;
91      int noerr;
92 {
93   register struct symbol *sym = lookup_symbol (name, block, VAR_NAMESPACE);
94   if (sym == 0 || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
95     {
96       if (!strcmp (name, "int"))
97         return builtin_type_int;
98       if (!strcmp (name, "long"))
99         return builtin_type_long;
100       if (!strcmp (name, "short"))
101         return builtin_type_short;
102       if (!strcmp (name, "char"))
103         return builtin_type_char;
104       if (!strcmp (name, "float"))
105         return builtin_type_float;
106       if (!strcmp (name, "double"))
107         return builtin_type_double;
108       if (!strcmp (name, "void"))
109         return builtin_type_void;
110
111       if (noerr)
112         return 0;
113       error ("No type named %s.", name);
114     }
115   return SYMBOL_TYPE (sym);
116 }
117
118 struct type *
119 lookup_unsigned_typename (name)
120      char *name;
121 {
122   if (!strcmp (name, "int"))
123     return builtin_type_unsigned_int;
124   if (!strcmp (name, "long"))
125     return builtin_type_unsigned_long;
126   if (!strcmp (name, "short"))
127     return builtin_type_unsigned_short;
128   if (!strcmp (name, "char"))
129     return builtin_type_unsigned_char;
130   error ("No type named unsigned %s.", name);
131 }
132
133 /* Lookup a structure type named "struct NAME",
134    visible in lexical block BLOCK.  */
135
136 struct type *
137 lookup_struct (name, block)
138      char *name;
139      struct block *block;
140 {
141   register struct symbol *sym = lookup_symbol (name, block, STRUCT_NAMESPACE);
142   if (sym == 0)
143     error ("No struct type named %s.", name);
144   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
145     error ("This context has union or enum %s, not a struct.", name);
146   return SYMBOL_TYPE (sym);
147 }
148
149 /* Lookup a union type named "union NAME",
150    visible in lexical block BLOCK.  */
151
152 struct type *
153 lookup_union (name, block)
154      char *name;
155      struct block *block;
156 {
157   register struct symbol *sym = lookup_symbol (name, block, STRUCT_NAMESPACE);
158   if (sym == 0)
159     error ("No union type named %s.", name);
160   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
161     error ("This context has struct or enum %s, not a union.", name);
162   return SYMBOL_TYPE (sym);
163 }
164
165 /* Lookup an enum type named "enum NAME",
166    visible in lexical block BLOCK.  */
167
168 struct type *
169 lookup_enum (name, block)
170      char *name;
171      struct block *block;
172 {
173   register struct symbol *sym = lookup_symbol (name, block, STRUCT_NAMESPACE);
174   if (sym == 0)
175     error ("No enum type named %s.", name);
176   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
177     error ("This context has struct or union %s, not an enum.", name);
178   return SYMBOL_TYPE (sym);
179 }
180
181 /* Given a type TYPE, return a type of pointers to that type.
182    May need to construct such a type if this is the first use.
183
184    C++: use TYPE_MAIN_VARIANT and TYPE_CHAIN to keep pointer
185    to member types under control.  */
186
187 struct type *
188 lookup_pointer_type (type)
189      struct type *type;
190 {
191   register struct type *ptype = TYPE_POINTER_TYPE (type);
192   if (ptype) return TYPE_MAIN_VARIANT (ptype);
193
194   /* This is the first time anyone wanted a pointer to a TYPE.  */
195   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
196     ptype  = (struct type *) xmalloc (sizeof (struct type));
197   else
198     ptype  = (struct type *) obstack_alloc (symbol_obstack,
199                                             sizeof (struct type));
200
201   bzero (ptype, sizeof (struct type));
202   TYPE_MAIN_VARIANT (ptype) = ptype;
203   TYPE_TARGET_TYPE (ptype) = type;
204   TYPE_POINTER_TYPE (type) = ptype;
205   /* New type is permanent if type pointed to is permanent.  */
206   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
207     TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
208   /* We assume the machine has only one representation for pointers!  */
209   TYPE_LENGTH (ptype) = sizeof (char *);
210   TYPE_CODE (ptype) = TYPE_CODE_PTR;
211   return ptype;
212 }
213
214 struct type *
215 lookup_reference_type (type)
216      struct type *type;
217 {
218   register struct type *rtype = TYPE_REFERENCE_TYPE (type);
219   if (rtype) return TYPE_MAIN_VARIANT (rtype);
220
221   /* This is the first time anyone wanted a pointer to a TYPE.  */
222   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
223     rtype  = (struct type *) xmalloc (sizeof (struct type));
224   else
225     rtype  = (struct type *) obstack_alloc (symbol_obstack,
226                                             sizeof (struct type));
227
228   bzero (rtype, sizeof (struct type));
229   TYPE_MAIN_VARIANT (rtype) = rtype;
230   TYPE_TARGET_TYPE (rtype) = type;
231   TYPE_REFERENCE_TYPE (type) = rtype;
232   /* New type is permanent if type pointed to is permanent.  */
233   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
234     TYPE_FLAGS (rtype) |= TYPE_FLAG_PERM;
235   /* We assume the machine has only one representation for pointers!  */
236   TYPE_LENGTH (rtype) = sizeof (char *);
237   TYPE_CODE (rtype) = TYPE_CODE_REF;
238   return rtype;
239 }
240
241 /* Implement direct support for MEMBER_TYPE in GNU C++.
242    May need to construct such a type if this is the first use.
243    The TYPE is the type of the member.  The DOMAIN is the type
244    of the aggregate that the member belongs to.  */
245
246 struct type *
247 lookup_member_type (domain, type)
248      struct type *domain, *type;
249 {
250   register struct type *mtype = TYPE_MAIN_VARIANT (type);
251   struct type *main_type;
252
253   main_type = mtype;
254   while (mtype)
255     {
256       if (TYPE_DOMAIN_TYPE (mtype) == domain)
257         return mtype;
258       mtype = TYPE_NEXT_VARIANT (mtype);
259     }
260
261   /* This is the first time anyone wanted this member type.  */
262   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
263     mtype  = (struct type *) xmalloc (sizeof (struct type));
264   else
265     mtype  = (struct type *) obstack_alloc (symbol_obstack,
266                                             sizeof (struct type));
267
268   bzero (mtype, sizeof (struct type));
269   if (main_type == 0) main_type = mtype;
270   else
271     {
272       TYPE_NEXT_VARIANT (mtype) = TYPE_NEXT_VARIANT (main_type);
273       TYPE_NEXT_VARIANT (main_type) = mtype;
274     }
275   TYPE_MAIN_VARIANT (mtype) = main_type;
276   TYPE_TARGET_TYPE (mtype) = type;
277   TYPE_DOMAIN_TYPE (mtype) = domain;
278   /* New type is permanent if type pointed to is permanent.  */
279   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
280     TYPE_FLAGS (mtype) |= TYPE_FLAG_PERM;
281
282   /* In practice, this is never used.  */
283   TYPE_LENGTH (mtype) = 1;
284   TYPE_CODE (mtype) = TYPE_CODE_MEMBER;
285
286   return mtype;
287 }
288
289 /* Given a type TYPE, return a type which has offset OFFSET,
290    via_virtual VIA_VIRTUAL, and via_public VIA_PUBLIC.
291    May need to construct such a type if none exists.  */
292 struct type *
293 lookup_basetype_type (type, offset, via_virtual, via_public)
294      struct type *type;
295      int offset;
296      int via_virtual, via_public;
297 {
298   register struct type *btype = TYPE_MAIN_VARIANT (type);
299   struct type *main_type;
300
301   if (offset != 0)
302     {
303       printf ("type offset non-zero in lookup_basetype_type");
304       offset = 0;
305     }
306
307   main_type = btype;
308   while (btype)
309     {
310       if (/* TYPE_OFFSET (btype) == offset
311           && */ TYPE_VIA_PUBLIC (btype) == via_public
312           && TYPE_VIA_VIRTUAL (btype) == via_virtual)
313         return btype;
314       btype = TYPE_NEXT_VARIANT (btype);
315     }
316
317   /* This is the first time anyone wanted this member type.  */
318   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
319     btype  = (struct type *) xmalloc (sizeof (struct type));
320   else
321     btype  = (struct type *) obstack_alloc (symbol_obstack,
322                                             sizeof (struct type));
323
324   if (main_type == 0)
325     {
326       main_type = btype;
327       bzero (btype, sizeof (struct type));
328       TYPE_MAIN_VARIANT (btype) = main_type;
329     }
330   else
331     {
332       bcopy (main_type, btype, sizeof (struct type));
333       TYPE_NEXT_VARIANT (main_type) = btype;
334     }
335 /* TYPE_OFFSET (btype) = offset; */
336   if (via_public)
337     TYPE_FLAGS (btype) |= TYPE_FLAG_VIA_PUBLIC;
338   if (via_virtual)
339     TYPE_FLAGS (btype) |= TYPE_FLAG_VIA_VIRTUAL;
340   /* New type is permanent if type pointed to is permanent.  */
341   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
342     TYPE_FLAGS (btype) |= TYPE_FLAG_PERM;
343
344   /* In practice, this is never used.  */
345   TYPE_LENGTH (btype) = 1;
346   TYPE_CODE (btype) = TYPE_CODE_STRUCT;
347
348   return btype;
349 }
350
351 /* Given a type TYPE, return a type of functions that return that type.
352    May need to construct such a type if this is the first use.  */
353
354 struct type *
355 lookup_function_type (type, argtypes)
356      struct type *type;
357      struct type **argtypes;
358 {
359   register struct type *ptype = TYPE_FUNCTION_TYPE (type);
360   if (ptype) return ptype;
361
362   /* This is the first time anyone wanted a function returning a TYPE.  */
363   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
364     ptype  = (struct type *) xmalloc (sizeof (struct type));
365   else
366     ptype  = (struct type *) obstack_alloc (symbol_obstack,
367                                             sizeof (struct type));
368
369   bzero (ptype, sizeof (struct type));
370   TYPE_TARGET_TYPE (ptype) = type;
371   TYPE_FUNCTION_TYPE (type) = ptype;
372   /* New type is permanent if type returned is permanent.  */
373   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
374     TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
375   TYPE_LENGTH (ptype) = 1;
376   TYPE_CODE (ptype) = TYPE_CODE_FUNC;
377   TYPE_NFIELDS (ptype) = 0;
378   return ptype;
379 }
380 \f
381 /* Smash TYPE to be a type of pointers to TO_TYPE.
382    If TO_TYPE is not permanent and has no pointer-type yet,
383    record TYPE as its pointer-type.  */
384
385 void
386 smash_to_pointer_type (type, to_type)
387      struct type *type, *to_type;
388 {
389   bzero (type, sizeof (struct type));
390   TYPE_TARGET_TYPE (type) = to_type;
391   /* We assume the machine has only one representation for pointers!  */
392   TYPE_LENGTH (type) = sizeof (char *);
393   TYPE_CODE (type) = TYPE_CODE_PTR;
394
395   TYPE_MAIN_VARIANT (type) = type;
396
397   if (TYPE_POINTER_TYPE (to_type) == 0
398       && !(TYPE_FLAGS (type) & TYPE_FLAG_PERM))
399     {
400       TYPE_POINTER_TYPE (to_type) = type;
401     }
402 }
403
404 /* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE.  */
405
406 void
407 smash_to_member_type (type, domain, to_type)
408      struct type *type, *domain, *to_type;
409 {
410   bzero (type, sizeof (struct type));
411   TYPE_TARGET_TYPE (type) = to_type;
412   TYPE_DOMAIN_TYPE (type) = domain;
413
414   /* In practice, this is never needed.  */
415   TYPE_LENGTH (type) = 1;
416   TYPE_CODE (type) = TYPE_CODE_MEMBER;
417
418   TYPE_MAIN_VARIANT (type) = lookup_member_type (domain, to_type);
419 }
420
421 /* Smash TYPE to be a type of reference to TO_TYPE.
422    If TO_TYPE is not permanent and has no pointer-type yet,
423    record TYPE as its pointer-type.  */
424
425 void
426 smash_to_reference_type (type, to_type)
427      struct type *type, *to_type;
428 {
429   bzero (type, sizeof (struct type));
430   TYPE_TARGET_TYPE (type) = to_type;
431   /* We assume the machine has only one representation for pointers!  */
432   TYPE_LENGTH (type) = sizeof (char *);
433   TYPE_CODE (type) = TYPE_CODE_REF;
434
435   TYPE_MAIN_VARIANT (type) = type;
436
437   if (TYPE_REFERENCE_TYPE (to_type) == 0
438       && !(TYPE_FLAGS (type) & TYPE_FLAG_PERM))
439     {
440       TYPE_REFERENCE_TYPE (to_type) = type;
441     }
442 }
443
444 /* Smash TYPE to be a type of functions returning TO_TYPE.
445    If TO_TYPE is not permanent and has no function-type yet,
446    record TYPE as its function-type.  */
447
448 void
449 smash_to_function_type (type, to_type)
450      struct type *type, *to_type;
451 {
452   bzero (type, sizeof (struct type));
453   TYPE_TARGET_TYPE (type) = to_type;
454   TYPE_LENGTH (type) = 1;
455   TYPE_CODE (type) = TYPE_CODE_FUNC;
456   TYPE_NFIELDS (type) = 0;
457
458   if (TYPE_FUNCTION_TYPE (to_type) == 0
459       && !(TYPE_FLAGS (type) & TYPE_FLAG_PERM))
460     {
461       TYPE_FUNCTION_TYPE (to_type) = type;
462     }
463 }
464 \f
465 static struct symbol *lookup_block_symbol ();
466
467 /* Find the definition for a specified symbol name NAME
468    in namespace NAMESPACE, visible from lexical block BLOCK.
469    Returns the struct symbol pointer, or zero if no symbol is found.  */
470
471 struct symbol *
472 lookup_symbol_1 (name, block, namespace)
473      char *name;
474      register struct block *block;
475      enum namespace namespace;
476 {
477   register int i, n;
478   register struct symbol *sym;
479   register struct symtab *s;
480   struct blockvector *bv;
481
482   /* Search specified block and its superiors.  */
483
484   while (block != 0)
485     {
486       sym = lookup_block_symbol (block, name, namespace);
487       if (sym) return sym;
488       block = BLOCK_SUPERBLOCK (block);
489     }
490   return 0;
491 }
492
493 struct symbol *
494 lookup_symbol_2 (name, block, namespace)
495      char *name;
496      register struct block *block; /* ignored as parameter */
497      enum namespace namespace;
498 {
499   register int i, n;
500   register struct symbol *sym;
501   register struct symtab *s;
502   struct blockvector *bv;
503
504   /* Now search all symtabs' global blocks.  */
505
506   for (s = symtab_list; s; s = s->next)
507     {
508       bv = BLOCKVECTOR (s);
509       block = BLOCKVECTOR_BLOCK (bv, 0);
510       sym = lookup_block_symbol (block, name, namespace);
511       if (sym) return sym;
512     }
513
514   /* Now search all symtabs' per-file blocks.
515      Not strictly correct, but more useful than an error.  */
516
517   for (s = symtab_list; s; s = s->next)
518     {
519       bv = BLOCKVECTOR (s);
520       block = BLOCKVECTOR_BLOCK (bv, 1);
521       sym = lookup_block_symbol (block, name, namespace);
522       if (sym) return sym;
523     }
524   return 0;
525 }
526
527 struct symbol *
528 lookup_symbol (name, block, namespace)
529      char *name;
530      register struct block *block;
531      enum namespace namespace;
532 {
533   register int i, n;
534   register struct symbol *sym;
535   register struct symtab *s;
536   struct blockvector *bv;
537
538   /* Search specified block and its superiors.  */
539
540   while (block != 0)
541     {
542       sym = lookup_block_symbol (block, name, namespace);
543       if (sym) return sym;
544       block = BLOCK_SUPERBLOCK (block);
545     }
546
547   /* Now search all symtabs' global blocks.  */
548
549   for (s = symtab_list; s; s = s->next)
550     {
551       bv = BLOCKVECTOR (s);
552       block = BLOCKVECTOR_BLOCK (bv, 0);
553       sym = lookup_block_symbol (block, name, namespace);
554       if (sym) return sym;
555     }
556
557   /* Now search all symtabs' per-file blocks.
558      Not strictly correct, but more useful than an error.  */
559
560   for (s = symtab_list; s; s = s->next)
561     {
562       bv = BLOCKVECTOR (s);
563       block = BLOCKVECTOR_BLOCK (bv, 1);
564       sym = lookup_block_symbol (block, name, namespace);
565       if (sym) return sym;
566     }
567   return 0;
568 }
569
570 /* Look for a symbol in block BLOCK.  */
571
572 static struct symbol *
573 lookup_block_symbol (block, name, namespace)
574      register struct block *block;
575      char *name;
576      enum namespace namespace;
577 {
578   register int bot, top, inc;
579   register struct symbol *sym;
580
581   top = BLOCK_NSYMS (block);
582   bot = 0;
583
584   /* If the blocks's symbols were sorted, start with a binary search.  */
585
586   if (BLOCK_SHOULD_SORT (block))
587     {
588       /* First, advance BOT to not far before
589          the first symbol whose name is NAME.  */
590
591       while (1)
592         {
593           inc = (top - bot + 1);
594           /* No need to keep binary searching for the last few bits worth.  */
595           if (inc < 4)
596             break;
597           inc = (inc >> 1) + bot;
598           sym = BLOCK_SYM (block, inc);
599           if (SYMBOL_NAME (sym)[0] < name[0])
600             bot = inc;
601           else if (SYMBOL_NAME (sym)[0] > name[0])
602             top = inc;
603           else if (strcmp (SYMBOL_NAME (sym), name) < 0)
604             bot = inc;
605           else
606             top = inc;
607         }
608
609       /* Now scan forward until we run out of symbols,
610          find one whose name is greater than NAME,
611          or find one we want.
612          If there is more than one symbol with the right name and namespace,
613          we return the first one.  dbxread.c is careful to make sure
614          that if one is a register then it comes first.  */
615
616       top = BLOCK_NSYMS (block);
617       while (bot < top)
618         {
619           sym = BLOCK_SYM (block, bot);
620           inc = SYMBOL_NAME (sym)[0] - name[0];
621           if (inc == 0)
622             inc = strcmp (SYMBOL_NAME (sym), name);
623           if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
624             return sym;
625           if (inc > 0)
626             return 0;
627           bot++;
628         }
629       return 0;
630     }
631
632   /* Here if block isn't sorted.
633      This loop is equivalent to the loop above,
634      but hacked greatly for speed.  */
635
636   top = BLOCK_NSYMS (block);
637   inc = name[0];
638   while (bot < top)
639     {
640       sym = BLOCK_SYM (block, bot);
641       if (SYMBOL_NAME (sym)[0] == inc
642           && !strcmp (SYMBOL_NAME (sym), name)
643           && SYMBOL_NAMESPACE (sym) == namespace)
644         return sym;
645       bot++;
646     }
647   return 0;
648 }
649 \f
650 /* Return the symbol for the function which contains a specified
651    lexical block, described by a struct block BL.  */
652
653 struct symbol *
654 block_function (bl)
655      struct block *bl;
656 {
657   while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
658     bl = BLOCK_SUPERBLOCK (bl);
659
660   return BLOCK_FUNCTION (bl);
661 }
662
663 /* Subroutine of find_pc_line */
664
665 static struct symtab *
666 find_pc_symtab (pc)
667      register CORE_ADDR pc;
668 {
669   register struct block *b;
670   struct blockvector *bv;
671   register struct symtab *s;
672
673   /* Search all symtabs for one whose file contains our pc */
674
675   for (s = symtab_list; s; s = s->next)
676     {
677       bv = BLOCKVECTOR (s);
678       b = BLOCKVECTOR_BLOCK (bv, 0);
679       if (BLOCK_START (b) <= pc
680           && BLOCK_END (b) > pc)
681         break;
682     }
683
684   return s;
685 }
686
687 /* Find the source file and line number for a given PC value.
688    Return a structure containing a symtab pointer, a line number,
689    and a pc range for the entire source line.
690    The value's .pc field is NOT the specified pc.
691    NOTCURRENT nonzero means, if specified pc is on a line boundary,
692    use the line that ends there.  Otherwise, in that case, the line
693    that begins there is used.  */
694
695 struct symtab_and_line
696 find_pc_line (pc, notcurrent)
697      CORE_ADDR pc;
698      int notcurrent;
699 {
700   struct symtab *s;
701   register struct linetable *l;
702   register int len;
703   register int i, item;
704   int line;
705   struct symtab_and_line value;
706   struct blockvector *bv;
707
708   /* Info on best line seen so far, and where it starts, and its file.  */
709
710   int best_line = 0;
711   CORE_ADDR best_pc = 0;
712   CORE_ADDR best_end = 0;
713   struct symtab *best_symtab = 0;
714
715   /* Store here the first line number
716      of a file which contains the line at the smallest pc after PC.
717      If we don't find a line whose range contains PC,
718      we will use a line one less than this,
719      with a range from the start of that file to the first line's pc.  */
720   int alt_line = 0;
721   CORE_ADDR alt_pc = 0;
722   struct symtab *alt_symtab = 0;
723
724   /* Info on best line seen in this file.  */
725
726   int prev_line;
727   CORE_ADDR prev_pc;
728
729   /* Info on first line of this file.  */
730
731   int first_line;
732   CORE_ADDR first_pc;
733
734   /* If this pc is not from the current frame,
735      it is the address of the end of a call instruction.
736      Quite likely that is the start of the following statement.
737      But what we want is the statement containing the instruction.
738      Fudge the pc to make sure we get that.  */
739
740   if (notcurrent) pc -= 1;
741
742   s = find_pc_symtab (pc);
743   if (s == 0)
744     {
745       value.symtab = 0;
746       value.line = 0;
747       value.pc = pc;
748       return value;
749     }
750
751   bv = BLOCKVECTOR (s);
752
753   /* Look at all the symtabs that share this blockvector.
754      They all have the same apriori range, that we found was right;
755      but they have different line tables.  */
756
757   for (; s && BLOCKVECTOR (s) == bv; s = s->next)
758     {
759       /* Find the best line in this symtab.  */
760       l = LINETABLE (s);
761       len = l->nitems;
762       prev_line = -1;
763       first_line = -1;
764       for (i = 0; i < len; i++)
765         {
766           item = l->item[i];
767           if (item < 0)
768             line = - item - 1;
769           else
770             {
771               line++;
772               if (first_line < 0)
773                 {
774                   first_line = line;
775                   first_pc = item;
776                 }
777               /* Return the last line that did not start after PC.  */
778               if (pc >= item)
779                 {
780                   prev_line = line;
781                   prev_pc = item;
782                 }
783               else
784                 break;
785             }
786         }
787
788       /* Is this file's best line closer than the best in the other files?
789          If so, record this file, and its best line, as best so far.  */
790       if (prev_line >= 0 && prev_pc > best_pc)
791         {
792           best_pc = prev_pc;
793           best_line = prev_line;
794           best_symtab = s;
795           if (i < len)
796             best_end = item;
797           else
798             best_end = 0;
799         }
800       /* Is this file's first line closer than the first lines of other files?
801          If so, record this file, and its first line, as best alternate.  */
802       if (first_line >= 0 && first_pc > pc
803           && (alt_pc == 0 || first_pc < alt_pc))
804         {
805           alt_pc = first_pc;
806           alt_line = first_line;
807           alt_symtab = s;
808         }
809     }
810   if (best_symtab == 0)
811     {
812       value.symtab = alt_symtab;
813       value.line = alt_line - 1;
814       value.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, 0));
815       value.end = alt_pc;
816     }
817   else
818     {
819       value.symtab = best_symtab;
820       value.line = best_line;
821       value.pc = best_pc;
822       value.end = (best_end ? best_end
823                    : (alt_pc ? alt_pc
824                       : BLOCK_END (BLOCKVECTOR_BLOCK (bv, 0))));
825     }
826   return value;
827 }
828 \f
829 /* Find the PC value for a given source file and line number.
830    Returns zero for invalid line number.
831    The source file is specified with a struct symtab.  */
832
833 CORE_ADDR
834 find_line_pc (symtab, line)
835      struct symtab *symtab;
836      int line;
837 {
838   register struct linetable *l;
839   register int index;
840   int dummy;
841
842   if (symtab == 0)
843     return 0;
844   l = LINETABLE (symtab);
845   index = find_line_common(l, line, &dummy);
846   return index ? l->item[index] : 0;
847 }
848
849 /* Find the range of pc values in a line.
850    Store the starting pc of the line into *STARTPTR
851    and the ending pc (start of next line) into *ENDPTR.
852    Returns 1 to indicate success.
853    Returns 0 if could not find the specified line.  */
854
855 int
856 find_line_pc_range (symtab, thisline, startptr, endptr)
857      struct symtab *symtab;
858      int thisline;
859      CORE_ADDR *startptr, *endptr;
860 {
861   register struct linetable *l;
862   register int index;
863   int exact_match;              /* did we get an exact linenumber match */
864   register CORE_ADDR prev_pc;
865   CORE_ADDR last_pc;
866
867   if (symtab == 0)
868     return 0;
869
870   l = LINETABLE (symtab);
871   index = find_line_common (l, thisline, &exact_match);
872   if (index)
873     {
874       *startptr = l->item[index];
875       /* If we have not seen an entry for the specified line,
876          assume that means the specified line has zero bytes.  */
877       if (!exact_match || index == l->nitems-1)
878         *endptr = *startptr;
879       else
880         /* Perhaps the following entry is for the following line.
881            It's worth a try.  */
882         if (l->item[index+1] > 0)
883           *endptr = l->item[index+1];
884         else
885           *endptr = find_line_pc (symtab, thisline+1);
886       return 1;
887     }
888
889   return 0;
890 }
891
892 /* Given a line table and a line number, return the index into the line
893    table for the pc of the nearest line whose number is >= the specified one.
894    Return 0 if none is found.  The value is never zero is it is an index.
895
896    Set *EXACT_MATCH nonzero if the value returned is an exact match.  */
897
898 static int
899 find_line_common (l, lineno, exact_match)
900      register struct linetable *l;
901      register int lineno;
902      int *exact_match;
903 {
904   register int i;
905   register int len;
906
907   /* BEST is the smallest linenumber > LINENO so far seen,
908      or 0 if none has been seen so far.
909      BEST_INDEX identifies the item for it.  */
910
911   int best_index = 0;
912   int best = 0;
913
914   int nextline = -1;
915
916   if (lineno <= 0)
917     return 0;
918
919   len = l->nitems;
920   for (i = 0; i < len; i++)
921     {
922       register int item = l->item[i];
923
924       if (item < 0)
925         nextline = - item - 1;
926       else
927         {
928           nextline++;
929           if (nextline == lineno)
930             {
931               *exact_match = 1;
932               return i;
933             }
934
935           if (nextline > lineno && (best == 0 || nextline < best))
936             {
937               best = lineno;
938               best_index = i;
939             }
940         }
941     }
942
943   /* If we got here, we didn't get an exact match.  */
944
945   *exact_match = 0;
946   return best_index;
947 }
948
949 int
950 find_pc_line_pc_range (pc, startptr, endptr)
951      CORE_ADDR pc;
952      CORE_ADDR *startptr, *endptr;
953 {
954   struct symtab_and_line sal;
955   sal = find_pc_line (pc, 0);
956   *startptr = sal.pc;
957   *endptr = sal.end;
958   return sal.symtab != 0;
959 }
960 \f
961 /* Parse a string that specifies a line number.
962    Pass the address of a char * variable; that variable will be
963    advanced over the characters actually parsed.
964
965    The string can be:
966
967    LINENUM -- that line number in current file.  PC returned is 0.
968    FILE:LINENUM -- that line in that file.  PC returned is 0.
969    FUNCTION -- line number of openbrace of that function.
970       PC returned is the start of the function.
971    FILE:FUNCTION -- likewise, but prefer functions in that file.
972    *EXPR -- line in which address EXPR appears.
973
974    FUNCTION may be an undebuggable function found in misc_function_vector.
975
976    If the argument FUNFIRSTLINE is nonzero, we want the first line
977    of real code inside a function when a function is specified.
978
979    DEFAULT_SYMTAB specifies the file to use if none is specified.
980    It defaults to current_source_symtab.
981    DEFAULT_LINE specifies the line number to use for relative
982    line numbers (that start with signs).  Defaults to current_source_line.
983
984    Note that it is possible to return zero for the symtab
985    if no file is validly specified.  Callers must check that.
986    Also, the line number returned may be invalid.  */
987
988 struct symtabs_and_lines
989 decode_line_1 (argptr, funfirstline, default_symtab, default_line)
990      char **argptr;
991      int funfirstline;
992      struct symtab *default_symtab;
993      int default_line;
994 {
995   struct symtabs_and_lines decode_line_2 ();
996   struct symtabs_and_lines values;
997   struct symtab_and_line value;
998   register char *p, *p1;
999   register struct symtab *s;
1000   register struct symbol *sym;
1001   register CORE_ADDR pc;
1002   register int i;
1003   char *copy;
1004   struct symbol *sym_class;
1005   char *class_name, *method_name, *phys_name;
1006   int method_counter;
1007   int i1;
1008   struct symbol **sym_arr;
1009   struct type *t, *field;
1010   char **physnames;
1011
1012   /* Defaults have defaults.  */
1013
1014   if (default_symtab == 0)
1015     {
1016       default_symtab = current_source_symtab;
1017       default_line = current_source_line;
1018     }
1019
1020   /* See if arg is *PC */
1021
1022   if (**argptr == '*')
1023     {
1024       (*argptr)++;
1025       pc = parse_and_eval_address_1 (argptr);
1026       values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1027       values.nelts = 1;
1028       values.sals[0] = find_pc_line (pc, 0);
1029       values.sals[0].pc = pc;
1030       return values;
1031     }
1032
1033   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
1034
1035   s = 0;
1036
1037   for (p = *argptr; *p; p++)
1038     {
1039       if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
1040         break;
1041     }
1042   while (p[0] == ' ' || p[0] == '\t') p++;
1043
1044   if (p[0] == ':')
1045     {
1046       /*  C++  */
1047       if (p[1] ==':')
1048         {
1049           /* Extract the class name.  */
1050           p1 = p;
1051           while (p != *argptr && p[-1] == ' ') --p;
1052           copy = (char *) alloca (p - *argptr + 1);
1053           bcopy (*argptr, copy, p - *argptr);
1054           copy[p - *argptr] = 0;
1055
1056           /* Discard the class name from the arg.  */
1057           p = p1 + 2;
1058           while (*p == ' ' || *p == '\t') p++;
1059           *argptr = p;
1060
1061           sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE);
1062        
1063           if (sym_class &&
1064               (TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_STRUCT
1065                || TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_UNION))
1066             {
1067               /* Arg token is not digits => try it as a function name
1068                  Find the next token (everything up to end or next whitespace). */
1069               p = *argptr;
1070               while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p !=':') p++;
1071               copy = (char *) alloca (p - *argptr + 1);
1072               bcopy (*argptr, copy, p - *argptr);
1073               copy[p - *argptr] = '\0';
1074
1075               /* no line number may be specified */
1076               while (*p == ' ' || *p == '\t') p++;
1077               *argptr = p;
1078
1079               sym = 0;
1080               i1 = 0;           /*  counter for the symbol array */
1081               t = SYMBOL_TYPE (sym_class);
1082               sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
1083               physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
1084
1085               if (destructor_name_p (copy, t))
1086                 {
1087                   /* destructors are a special case.  */
1088                   struct fn_field *f = TYPE_FN_FIELDLIST1 (t, 0);
1089                   int len = TYPE_FN_FIELDLIST_LENGTH (t, 0) - 1;
1090                   phys_name = TYPE_FN_FIELD_PHYSNAME (f, len);
1091                   physnames[i1] = (char *)alloca (strlen (phys_name) + 1);
1092                   strcpy (physnames[i1], phys_name);
1093                   sym_arr[i1] = lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class), VAR_NAMESPACE);
1094                   if (sym_arr[i1]) i1++;
1095                 }
1096               else while (t)
1097                 {
1098                   class_name = TYPE_NAME (t);
1099                   while (*class_name++ != ' ');
1100
1101                   sym_class = lookup_symbol (class_name, 0, STRUCT_NAMESPACE);
1102                   for (method_counter = TYPE_NFN_FIELDS (SYMBOL_TYPE (sym_class)) - 1;
1103                        method_counter >= 0;
1104                        --method_counter)
1105                     {
1106                       int field_counter;
1107                       struct fn_field *f =
1108                         TYPE_FN_FIELDLIST1 (SYMBOL_TYPE (sym_class), method_counter);
1109
1110                       method_name = TYPE_FN_FIELDLIST_NAME (SYMBOL_TYPE (sym_class), method_counter);
1111                       if (!strcmp (copy, method_name))
1112                         for (field_counter = TYPE_FN_FIELDLIST_LENGTH (SYMBOL_TYPE (sym_class), method_counter) - 1;
1113                              field_counter >= 0;
1114                              --field_counter)
1115                           {
1116                             phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1117                             physnames[i1] = (char*) alloca (strlen (phys_name) + 1);
1118                             strcpy (physnames[i1], phys_name);
1119                             sym_arr[i1] = lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class), VAR_NAMESPACE);
1120                             if (sym_arr[i1]) i1++;
1121                           }
1122                     }
1123                   if (TYPE_N_BASECLASSES (t))
1124                     t = TYPE_BASECLASS(t, 1);
1125                   else break;
1126                 }
1127
1128               if (i1 == 1)
1129                 {
1130                   sym = sym_arr[0];
1131
1132                   if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1133                     {
1134                       /* Arg is the name of a function */
1135                       pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
1136                       if (funfirstline)
1137                         SKIP_PROLOGUE (pc);
1138                       values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1139                       values.nelts = 1;
1140                       values.sals[0] = find_pc_line (pc, 0);
1141                       values.sals[0].pc = (values.sals[0].end && values.sals[0].pc != pc) ? values.sals[0].end : pc;
1142                     }
1143                   else
1144                     {
1145                       values.nelts = 0;
1146                     }
1147                   return values;
1148                 }
1149               if (i1 > 0)
1150                 {
1151                   return decode_line_2 (argptr, sym_arr, physnames, i1, funfirstline);
1152                 }
1153               else
1154                 error ("that class does not have any method named %s",copy);
1155             }
1156           else
1157             error("no class, struct, or union named %s", copy );
1158         }
1159       /*  end of C++  */
1160
1161       /* Extract the file name.  */
1162       p1 = p;
1163       while (p != *argptr && p[-1] == ' ') --p;
1164       copy = (char *) alloca (p - *argptr + 1);
1165       bcopy (*argptr, copy, p - *argptr);
1166       copy[p - *argptr] = 0;
1167
1168       /* Find that file's data.  */
1169       s = lookup_symtab (copy);
1170       if (s == 0)
1171         {
1172           if (symtab_list == 0)
1173             error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
1174           error ("No source file named %s.", copy);
1175         }
1176
1177       /* Discard the file name from the arg.  */
1178       p = p1 + 1;
1179       while (*p == ' ' || *p == '\t') p++;
1180       *argptr = p;
1181     }
1182
1183   /* S is specified file's symtab, or 0 if no file specified.
1184      arg no longer contains the file name.  */
1185
1186   /* Check whether arg is all digits (and sign) */
1187
1188   p = *argptr;
1189   if (*p == '-' || *p == '+') p++;
1190   while (*p >= '0' && *p <= '9')
1191     p++;
1192
1193   if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ','))
1194     {
1195       /* We found a token consisting of all digits -- at least one digit.  */
1196       enum sign {none, plus, minus} sign = none;
1197
1198       if (**argptr == '+')
1199         sign = plus, (*argptr)++;
1200       else if (**argptr == '-')
1201         sign = minus, (*argptr)++;
1202       value.line = atoi (*argptr);
1203       switch (sign)
1204         {
1205         case plus:
1206           if (p == *argptr)
1207             value.line = 5;
1208           if (s == 0)
1209             value.line = default_line + value.line;
1210           break;
1211         case minus:
1212           if (p == *argptr)
1213             value.line = 15;
1214           if (s == 0)
1215             value.line = default_line - value.line;
1216           else
1217             value.line = 1;
1218           break;
1219         }
1220
1221       while (*p == ' ' || *p == '\t') p++;
1222       *argptr = p;
1223       if (s == 0)
1224         s = default_symtab;
1225       value.symtab = s;
1226       value.pc = 0;
1227       values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1228       values.sals[0] = value;
1229       values.nelts = 1;
1230       return values;
1231     }
1232
1233   /* Arg token is not digits => try it as a function name
1234      Find the next token (everything up to end or next whitespace).  */
1235   p = *argptr;
1236   while (*p && *p != ' ' && *p != '\t' && *p != ',') p++;
1237   copy = (char *) alloca (p - *argptr + 1);
1238   bcopy (*argptr, copy, p - *argptr);
1239   copy[p - *argptr] = 0;
1240   while (*p == ' ' || *p == '\t') p++;
1241   *argptr = p;
1242
1243   /* Look up that token as a function.
1244      If file specified, use that file's per-file block to start with.  */
1245
1246   sym = lookup_symbol (copy, s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1) : 0,
1247                        VAR_NAMESPACE);
1248
1249   if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1250     {
1251       /* Arg is the name of a function */
1252       pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
1253       if (funfirstline)
1254         SKIP_PROLOGUE (pc);
1255       value = find_pc_line (pc, 0);
1256       value.pc = (value.end && value.pc != pc) ? value.end : pc;
1257       values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1258       values.sals[0] = value;
1259       values.nelts = 1;
1260       return values;
1261     }
1262
1263   if (sym)
1264     error ("%s is not a function.", copy);
1265
1266   if ((i = lookup_misc_func (copy)) < 0)
1267     error ("Function %s not defined.", copy);
1268   else
1269     {
1270       value.symtab = 0;
1271       value.line = 0;
1272       value.pc = misc_function_vector[i].address + FUNCTION_START_OFFSET;
1273       if (funfirstline)
1274         SKIP_PROLOGUE (value.pc);
1275       values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line));
1276       values.sals[0] = value;
1277       values.nelts = 1;
1278       return values;
1279     }
1280
1281   if (symtab_list == 0)
1282     error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
1283   error ("Function %s not defined.", copy);
1284 }
1285
1286 struct symtabs_and_lines
1287 decode_line_spec (string, funfirstline)
1288      char *string;
1289      int funfirstline;
1290 {
1291   struct symtabs_and_lines sals;
1292   if (string == 0)
1293     error ("Empty line specification.");
1294   sals = decode_line_1 (&string, funfirstline,
1295                         current_source_symtab, current_source_line);
1296   if (*string)
1297     error ("Junk at end of line specification: %s", string);
1298   return sals;
1299 }
1300
1301 struct symtabs_and_lines
1302 decode_line_2 (argptr, sym_arr, physnames, nelts, funfirstline)
1303      char **argptr;
1304      struct symbol *sym_arr[];
1305      char *physnames[];
1306      int nelts;
1307      int funfirstline;
1308 {
1309   char *getenv();
1310   struct symtabs_and_lines values, return_values;
1311   register CORE_ADDR pc;
1312   char *args, *arg1, *read_line ();
1313   int i;
1314   char *prompt;
1315
1316   values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line));
1317   return_values.sals = (struct symtab_and_line *) malloc (nelts * sizeof(struct symtab_and_line));
1318
1319   i = 0;
1320   printf("[0] cancel\n[1] all\n");
1321   while (i < nelts)
1322     {
1323       if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
1324         {
1325           /* Arg is the name of a function */
1326           pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr[i])) 
1327                + FUNCTION_START_OFFSET;
1328           if (funfirstline)
1329             SKIP_PROLOGUE (pc);
1330           values.sals[i] = find_pc_line (pc, 0);
1331           printf("[%d] file:%s; line number:%d\n",
1332                  (i+2), values.sals[i].symtab->filename, values.sals[i].line);
1333         }
1334       else printf ("?HERE\n");
1335       i++;
1336     }
1337   
1338   if ((prompt = getenv ("PS2")) == NULL)
1339     {
1340       prompt = ">";
1341     }
1342   printf("%s ",prompt);
1343   fflush(stdout);
1344
1345   args = read_line (0);
1346   
1347   if (args == 0)
1348     error_no_arg ("one or more choice numbers");
1349
1350   i = 0;
1351   while (*args)
1352     {
1353       int num;
1354
1355       arg1 = args;
1356       while (*arg1 >= '0' && *arg1 <= '9') arg1++;
1357       if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
1358         error ("Arguments must be choice numbers.");
1359
1360       num = atoi (args);
1361
1362       if (num == 0)
1363         error ("cancelled");
1364       else if (num == 1)
1365         {
1366           bcopy (values.sals, return_values.sals, (nelts * sizeof(struct symtab_and_line)));
1367           return_values.nelts = nelts;
1368           return return_values;
1369         }
1370
1371       if (num > nelts + 2)
1372         {
1373           printf ("No choice number %d.\n", num);
1374         }
1375       else
1376         {
1377           num -= 2;
1378           if (values.sals[num].pc)
1379             {
1380               return_values.sals[i++] = values.sals[num];
1381               values.sals[num].pc = 0;
1382             }
1383           else
1384             {
1385               printf ("duplicate request for %d ignored.\n", num);
1386             }
1387         }
1388
1389       args = arg1;
1390       while (*args == ' ' || *args == '\t') args++;
1391     }
1392   return_values.nelts = i;
1393   return return_values;
1394 }
1395
1396 /* Return the index of misc function named NAME.  */
1397
1398 static
1399 lookup_misc_func (name)
1400      register char *name;
1401 {
1402   register int i;
1403
1404   for (i = 0; i < misc_function_count; i++)
1405     if (!strcmp (misc_function_vector[i].name, name))
1406       return i;
1407   return -1;            /* not found */
1408 }
1409 \f
1410 static void
1411 sources_info ()
1412 {
1413   register struct symtab *s;
1414   register int column = 0;
1415
1416   if (symtab_list == 0)
1417     {
1418       printf ("No symbol table is loaded.\n");
1419       return;
1420     }
1421   printf ("Source files for which symbol table is known:\n");
1422   for (s = symtab_list; s; s = s->next)
1423     {
1424       if (column != 0 && column + strlen (s->filename) >= 70)
1425         {
1426           printf ("\n");
1427           column = 0;
1428         }
1429       else if (column != 0)
1430         {
1431           printf (" ");
1432           column++;
1433         }
1434       printf ("%s", s->filename);
1435       column += strlen (s->filename);
1436       if (s->next)
1437         {
1438           printf (",");
1439           column++;
1440         }
1441     }
1442   printf ("\n");
1443 }
1444
1445 /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
1446    If CLASS is zero, list all symbols except functions and type names.
1447    If CLASS is 1, list only functions.
1448    If CLASS is 2, list only type names.  */
1449
1450 #define MORE  \
1451 { print_count++;                \
1452   if (print_count >= 21)        \
1453     { printf ("--Type Return to print more--"); \
1454       print_count = 0;          \
1455       fflush (stdout);          \
1456       read_line (); } }
1457
1458 static void sort_block_syms ();
1459
1460 static void
1461 list_symbols (regexp, class)
1462      char *regexp;
1463      int class;
1464 {
1465   register struct symtab *s;
1466   register struct blockvector *bv;
1467   struct blockvector *prev_bv = 0;
1468   register struct block *b;
1469   register int i, j;
1470   register struct symbol *sym;
1471   char *val;
1472   int found_in_file;
1473   static char *classnames[]
1474     = {"variable", "function", "type", "method"};
1475   int print_count = 0;
1476
1477   if (regexp)
1478     if (val = (char *) re_comp (regexp))
1479       error ("Invalid regexp: %s", val);
1480
1481   printf (regexp
1482           ? "All %ss matching regular expression \"%s\":\n"
1483           : "All defined %ss:\n",
1484           classnames[class],
1485           regexp);
1486
1487   for (s = symtab_list; s; s = s->next)
1488     {
1489       found_in_file = 0;
1490       bv = BLOCKVECTOR (s);
1491       /* Often many files share a blockvector.
1492          Scan each blockvector only once so that
1493          we don't get every symbol many times.
1494          It happens that the first symtab in the list
1495          for any given blockvector is the main file.  */
1496       if (bv != prev_bv)
1497         for (i = 0; i < 2; i++)
1498           {
1499             b = BLOCKVECTOR_BLOCK (bv, i);
1500             /* Skip the sort if this block is always sorted.  */
1501             if (!BLOCK_SHOULD_SORT (b))
1502               sort_block_syms (b);
1503             for (j = 0; j < BLOCK_NSYMS (b); j++)
1504               {
1505                 QUIT;
1506                 sym = BLOCK_SYM (b, j);
1507                 if ((regexp == 0 || re_exec (SYMBOL_NAME (sym)))
1508                     && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
1509                          && SYMBOL_CLASS (sym) != LOC_BLOCK)
1510                         || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
1511                         || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1512                         || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK)))
1513                   {
1514                     if (!found_in_file)
1515                       {
1516                         printf ("\nFile %s:\n", s->filename);
1517                         print_count += 2;
1518                       }
1519                     found_in_file = 1;
1520                     MORE;
1521                     if (class != 2 && i == 1)
1522                       printf ("static ");
1523                     if (class == 2
1524                         && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
1525                       printf ("typedef ");
1526
1527                     if (class < 3)
1528                       {
1529                         type_print (SYMBOL_TYPE (sym),
1530                                     (SYMBOL_CLASS (sym) == LOC_TYPEDEF
1531                                      ? "" : SYMBOL_NAME (sym)),
1532                                     stdout, 0);
1533                         printf (";\n");
1534                       }
1535                     else
1536                       {
1537                         char buf[1024];
1538 # if 0
1539                         type_print_base (TYPE_FN_FIELD_TYPE(t, i), stdout, 0, 0); 
1540                         type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i), stdout, 0); 
1541                         sprintf (buf, " %s::", TYPE_NAME (t));
1542                         type_print_method_args (TYPE_FN_FIELD_ARGS (t, i), buf, name, stdout);
1543 # endif
1544                       }
1545                     if (class == 2
1546                         && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE
1547                         && (TYPE_NAME ((SYMBOL_TYPE (sym))) == 0
1548                             || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (sym))),
1549                                             SYMBOL_NAME (sym))))
1550                       printf (" %s", SYMBOL_NAME (sym));
1551                   }
1552               }
1553           }
1554       prev_bv = bv;
1555     }
1556 }
1557
1558 static void
1559 variables_info (regexp)
1560      char *regexp;
1561 {
1562   list_symbols (regexp, 0);
1563 }
1564
1565 static void
1566 functions_info (regexp)
1567      char *regexp;
1568 {
1569   list_symbols (regexp, 1);
1570 }
1571
1572 static void
1573 types_info (regexp)
1574      char *regexp;
1575 {
1576   list_symbols (regexp, 2);
1577 }
1578
1579 static void
1580 methods_info (regexp)
1581      char *regexp;
1582 {
1583   list_symbols (regexp, 3);
1584 }
1585 \f
1586 /* Call sort_block_syms to sort alphabetically the symbols of one block.  */
1587
1588 static int
1589 compare_symbols (s1, s2)
1590      struct symbol **s1, **s2;
1591 {
1592   /* Names that are less should come first.  */
1593   register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
1594   if (namediff != 0) return namediff;
1595   /* For symbols of the same name, registers should come first.  */
1596   return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
1597           - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
1598 }
1599
1600 static void
1601 sort_block_syms (b)
1602      register struct block *b;
1603 {
1604   qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
1605          sizeof (struct symbol *), compare_symbols);
1606 }
1607 \f
1608 /* Initialize the standard C scalar types.  */
1609
1610 static
1611 struct type *
1612 init_type (code, length, uns, name)
1613      enum type_code code;
1614      int length, uns;
1615      char *name;
1616 {
1617   register struct type *type;
1618
1619   type = (struct type *) xmalloc (sizeof (struct type));
1620   bzero (type, sizeof *type);
1621   TYPE_MAIN_VARIANT (type) = type;
1622   TYPE_CODE (type) = code;
1623   TYPE_LENGTH (type) = length;
1624   TYPE_FLAGS (type) = uns ? TYPE_FLAG_UNSIGNED : 0;
1625   TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
1626   TYPE_NFIELDS (type) = 0;
1627   TYPE_NAME (type) = name;
1628
1629   /* C++ fancies.  */
1630   TYPE_NFN_FIELDS (type) = 0;
1631   TYPE_N_BASECLASSES (type) = 0;
1632   TYPE_BASECLASSES (type) = 0;
1633   return type;
1634 }
1635
1636 static
1637 initialize ()
1638 {
1639   add_info ("variables", variables_info,
1640             "All global and static variable names, or those matching REGEXP.");
1641   add_info ("functions", functions_info,
1642             "All function names, or those matching REGEXP.");
1643   add_info ("types", types_info,
1644             "All types names, or those matching REGEXP.");
1645   add_info ("methods", methods_info,
1646             "All method names, or those matching REGEXP::REGEXP.\n\
1647 If the class qualifier is ommited, it is assumed to be the current scope.\n\
1648 If the first REGEXP is ommited, then all methods matching the second REGEXP\n\
1649 are listed.");
1650   add_info ("sources", sources_info,
1651             "Source files in the program.");
1652
1653   obstack_init (symbol_obstack);
1654
1655   builtin_type_void = init_type (TYPE_CODE_VOID, 0, 0, "void");
1656
1657   builtin_type_float = init_type (TYPE_CODE_FLT, sizeof (float), 0, "float");
1658   builtin_type_double = init_type (TYPE_CODE_FLT, sizeof (double), 0, "double");
1659
1660   builtin_type_char = init_type (TYPE_CODE_INT, sizeof (char), 0, "char");
1661   builtin_type_short = init_type (TYPE_CODE_INT, sizeof (short), 0, "short");
1662   builtin_type_long = init_type (TYPE_CODE_INT, sizeof (long), 0, "long");
1663   builtin_type_int = init_type (TYPE_CODE_INT, sizeof (int), 0, "int");
1664
1665   builtin_type_unsigned_char = init_type (TYPE_CODE_INT, sizeof (char), 1, "unsigned char");
1666   builtin_type_unsigned_short = init_type (TYPE_CODE_INT, sizeof (short), 1, "unsigned short");
1667   builtin_type_unsigned_long = init_type (TYPE_CODE_INT, sizeof (long), 1, "unsigned long");
1668   builtin_type_unsigned_int = init_type (TYPE_CODE_INT, sizeof (int), 1, "unsigned int");
1669 }
1670
1671 END_FILE