home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Atari / Gnu / gdb36p4s.zoo / symtab.c < prev    next >
C/C++ Source or Header  |  1993-08-13  |  72KB  |  2,583 lines

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