* source.c: Make global variables current_source_symtab and
[platform/upstream/binutils.git] / gdb / linespec.c
1 /* Parser for linespec for the GNU debugger, GDB.
2    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3    1996, 1997, 1998, 1999, 2000, 2001
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "command.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "source.h"
30 #include "demangle.h"
31 #include "value.h"
32 #include "completer.h"
33 #include "cp-abi.h"
34
35 /* Prototype for one function in parser-defs.h,
36    instead of including that entire file. */
37
38 extern char *find_template_name_end (char *);
39
40 /* We share this one with symtab.c, but it is not exported widely. */
41
42 extern char *operator_chars (char *, char **);
43
44 /* Prototypes for local functions */
45
46 static void cplusplus_error (const char *name, const char *fmt, ...) ATTR_FORMAT (printf, 2, 3);
47
48 static int total_number_of_methods (struct type *type);
49
50 static int find_methods (struct type *, char *, struct symbol **);
51
52 static void build_canonical_line_spec (struct symtab_and_line *,
53                                        char *, char ***);
54
55 static char *find_toplevel_char (char *s, char c);
56
57 static struct symtabs_and_lines decode_line_2 (struct symbol *[],
58                                                int, int, char ***);
59
60 /* Helper functions. */
61
62 /* Issue a helpful hint on using the command completion feature on
63    single quoted demangled C++ symbols as part of the completion
64    error.  */
65
66 static void
67 cplusplus_error (const char *name, const char *fmt, ...)
68 {
69   struct ui_file *tmp_stream;
70   tmp_stream = mem_fileopen ();
71   make_cleanup_ui_file_delete (tmp_stream);
72
73   {
74     va_list args;
75     va_start (args, fmt);
76     vfprintf_unfiltered (tmp_stream, fmt, args);
77     va_end (args);
78   }
79
80   while (*name == '\'')
81     name++;
82   fprintf_unfiltered (tmp_stream,
83                       ("Hint: try '%s<TAB> or '%s<ESC-?>\n"
84                        "(Note leading single quote.)"),
85                       name, name);
86   error_stream (tmp_stream);
87 }
88
89 /* Return the number of methods described for TYPE, including the
90    methods from types it derives from. This can't be done in the symbol
91    reader because the type of the baseclass might still be stubbed
92    when the definition of the derived class is parsed.  */
93
94 static int
95 total_number_of_methods (struct type *type)
96 {
97   int n;
98   int count;
99
100   CHECK_TYPEDEF (type);
101   if (TYPE_CPLUS_SPECIFIC (type) == NULL)
102     return 0;
103   count = TYPE_NFN_FIELDS_TOTAL (type);
104
105   for (n = 0; n < TYPE_N_BASECLASSES (type); n++)
106     count += total_number_of_methods (TYPE_BASECLASS (type, n));
107
108   return count;
109 }
110
111 /* Recursive helper function for decode_line_1.
112    Look for methods named NAME in type T.
113    Return number of matches.
114    Put matches in SYM_ARR, which should have been allocated with
115    a size of total_number_of_methods (T) * sizeof (struct symbol *).
116    Note that this function is g++ specific.  */
117
118 static int
119 find_methods (struct type *t, char *name, struct symbol **sym_arr)
120 {
121   int i1 = 0;
122   int ibase;
123   char *class_name = type_name_no_tag (t);
124
125   /* Ignore this class if it doesn't have a name.  This is ugly, but
126      unless we figure out how to get the physname without the name of
127      the class, then the loop can't do any good.  */
128   if (class_name
129       && (lookup_symbol (class_name, (struct block *) NULL,
130                          STRUCT_NAMESPACE, (int *) NULL,
131                          (struct symtab **) NULL)))
132     {
133       int method_counter;
134       int name_len = strlen (name);
135
136       CHECK_TYPEDEF (t);
137
138       /* Loop over each method name.  At this level, all overloads of a name
139          are counted as a single name.  There is an inner loop which loops over
140          each overload.  */
141
142       for (method_counter = TYPE_NFN_FIELDS (t) - 1;
143            method_counter >= 0;
144            --method_counter)
145         {
146           int field_counter;
147           char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
148           char dem_opname[64];
149
150           if (strncmp (method_name, "__", 2) == 0 ||
151               strncmp (method_name, "op", 2) == 0 ||
152               strncmp (method_name, "type", 4) == 0)
153             {
154               if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
155                 method_name = dem_opname;
156               else if (cplus_demangle_opname (method_name, dem_opname, 0))
157                 method_name = dem_opname;
158             }
159
160           if (strcmp_iw (name, method_name) == 0)
161             /* Find all the overloaded methods with that name.  */
162             for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
163                  field_counter >= 0;
164                  --field_counter)
165               {
166                 struct fn_field *f;
167                 char *phys_name;
168
169                 f = TYPE_FN_FIELDLIST1 (t, method_counter);
170
171                 if (TYPE_FN_FIELD_STUB (f, field_counter))
172                   {
173                     char *tmp_name;
174
175                     tmp_name = gdb_mangle_name (t,
176                                                 method_counter,
177                                                 field_counter);
178                     phys_name = alloca (strlen (tmp_name) + 1);
179                     strcpy (phys_name, tmp_name);
180                     xfree (tmp_name);
181                   }
182                 else
183                   phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
184                 
185                 /* Destructor is handled by caller, dont add it to the list */
186                 if (is_destructor_name (phys_name) != 0)
187                   continue;
188
189                 sym_arr[i1] = lookup_symbol (phys_name,
190                                              NULL, VAR_NAMESPACE,
191                                              (int *) NULL,
192                                              (struct symtab **) NULL);
193                 if (sym_arr[i1])
194                   i1++;
195                 else
196                   {
197                     /* This error message gets printed, but the method
198                        still seems to be found
199                        fputs_filtered("(Cannot find method ", gdb_stdout);
200                        fprintf_symbol_filtered (gdb_stdout, phys_name,
201                        language_cplus,
202                        DMGL_PARAMS | DMGL_ANSI);
203                        fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
204                      */
205                   }
206               }
207           else if (strncmp (class_name, name, name_len) == 0
208                    && (class_name[name_len] == '\0'
209                        || class_name[name_len] == '<'))
210             {
211               /* For GCC 3.x and stabs, constructors and destructors have names
212                  like __base_ctor and __complete_dtor.  Check the physname for now
213                  if we're looking for a constructor.  */
214               for (field_counter
215                      = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
216                    field_counter >= 0;
217                    --field_counter)
218                 {
219                   struct fn_field *f;
220                   char *phys_name;
221                   
222                   f = TYPE_FN_FIELDLIST1 (t, method_counter);
223
224                   /* GCC 3.x will never produce stabs stub methods, so we don't need
225                      to handle this case.  */
226                   if (TYPE_FN_FIELD_STUB (f, field_counter))
227                     continue;
228                   phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
229                   if (! is_constructor_name (phys_name))
230                     continue;
231
232                   /* If this method is actually defined, include it in the
233                      list.  */
234                   sym_arr[i1] = lookup_symbol (phys_name,
235                                                NULL, VAR_NAMESPACE,
236                                                (int *) NULL,
237                                                (struct symtab **) NULL);
238                   if (sym_arr[i1])
239                     i1++;
240                 }
241             }
242         }
243     }
244
245   /* Only search baseclasses if there is no match yet, since names in
246      derived classes override those in baseclasses.
247
248      FIXME: The above is not true; it is only true of member functions
249      if they have the same number of arguments (??? - section 13.1 of the
250      ARM says the function members are not in the same scope but doesn't
251      really spell out the rules in a way I understand.  In any case, if
252      the number of arguments differ this is a case in which we can overload
253      rather than hiding without any problem, and gcc 2.4.5 does overload
254      rather than hiding in this case).  */
255
256   if (i1 == 0)
257     for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
258       i1 += find_methods (TYPE_BASECLASS (t, ibase), name, sym_arr + i1);
259
260   return i1;
261 }
262
263 /* Helper function for decode_line_1.
264    Build a canonical line spec in CANONICAL if it is non-NULL and if
265    the SAL has a symtab.
266    If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
267    If SYMNAME is NULL the line number from SAL is used and the canonical
268    line spec is `filename:linenum'.  */
269
270 static void
271 build_canonical_line_spec (struct symtab_and_line *sal, char *symname,
272                            char ***canonical)
273 {
274   char **canonical_arr;
275   char *canonical_name;
276   char *filename;
277   struct symtab *s = sal->symtab;
278
279   if (s == (struct symtab *) NULL
280       || s->filename == (char *) NULL
281       || canonical == (char ***) NULL)
282     return;
283
284   canonical_arr = (char **) xmalloc (sizeof (char *));
285   *canonical = canonical_arr;
286
287   filename = s->filename;
288   if (symname != NULL)
289     {
290       canonical_name = xmalloc (strlen (filename) + strlen (symname) + 2);
291       sprintf (canonical_name, "%s:%s", filename, symname);
292     }
293   else
294     {
295       canonical_name = xmalloc (strlen (filename) + 30);
296       sprintf (canonical_name, "%s:%d", filename, sal->line);
297     }
298   canonical_arr[0] = canonical_name;
299 }
300
301
302
303 /* Find an instance of the character C in the string S that is outside
304    of all parenthesis pairs, single-quoted strings, and double-quoted
305    strings.  Also, ignore the char within a template name, like a ','
306    within foo<int, int>.  */
307
308 static char *
309 find_toplevel_char (char *s, char c)
310 {
311   int quoted = 0;               /* zero if we're not in quotes;
312                                    '"' if we're in a double-quoted string;
313                                    '\'' if we're in a single-quoted string.  */
314   int depth = 0;                /* number of unclosed parens we've seen */
315   char *scan;
316
317   for (scan = s; *scan; scan++)
318     {
319       if (quoted)
320         {
321           if (*scan == quoted)
322             quoted = 0;
323           else if (*scan == '\\' && *(scan + 1))
324             scan++;
325         }
326       else if (*scan == c && ! quoted && depth == 0)
327         return scan;
328       else if (*scan == '"' || *scan == '\'')
329         quoted = *scan;
330       else if (*scan == '(' || *scan == '<')
331         depth++;
332       else if ((*scan == ')' || *scan == '>') && depth > 0)
333         depth--;
334     }
335
336   return 0;
337 }
338
339 /* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
340    operate on (ask user if necessary).
341    If CANONICAL is non-NULL return a corresponding array of mangled names
342    as canonical line specs there.  */
343
344 static struct symtabs_and_lines
345 decode_line_2 (struct symbol *sym_arr[], int nelts, int funfirstline,
346                char ***canonical)
347 {
348   struct symtabs_and_lines values, return_values;
349   char *args, *arg1;
350   int i;
351   char *prompt;
352   char *symname;
353   struct cleanup *old_chain;
354   char **canonical_arr = (char **) NULL;
355
356   values.sals = (struct symtab_and_line *)
357     alloca (nelts * sizeof (struct symtab_and_line));
358   return_values.sals = (struct symtab_and_line *)
359     xmalloc (nelts * sizeof (struct symtab_and_line));
360   old_chain = make_cleanup (xfree, return_values.sals);
361
362   if (canonical)
363     {
364       canonical_arr = (char **) xmalloc (nelts * sizeof (char *));
365       make_cleanup (xfree, canonical_arr);
366       memset (canonical_arr, 0, nelts * sizeof (char *));
367       *canonical = canonical_arr;
368     }
369
370   i = 0;
371   printf_unfiltered ("[0] cancel\n[1] all\n");
372   while (i < nelts)
373     {
374       INIT_SAL (&return_values.sals[i]);        /* initialize to zeroes */
375       INIT_SAL (&values.sals[i]);
376       if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
377         {
378           values.sals[i] = find_function_start_sal (sym_arr[i], funfirstline);
379           printf_unfiltered ("[%d] %s at %s:%d\n",
380                              (i + 2),
381                              SYMBOL_SOURCE_NAME (sym_arr[i]),
382                              values.sals[i].symtab->filename,
383                              values.sals[i].line);
384         }
385       else
386         printf_unfiltered ("?HERE\n");
387       i++;
388     }
389
390   if ((prompt = getenv ("PS2")) == NULL)
391     {
392       prompt = "> ";
393     }
394   args = command_line_input (prompt, 0, "overload-choice");
395
396   if (args == 0 || *args == 0)
397     error_no_arg ("one or more choice numbers");
398
399   i = 0;
400   while (*args)
401     {
402       int num;
403
404       arg1 = args;
405       while (*arg1 >= '0' && *arg1 <= '9')
406         arg1++;
407       if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
408         error ("Arguments must be choice numbers.");
409
410       num = atoi (args);
411
412       if (num == 0)
413         error ("canceled");
414       else if (num == 1)
415         {
416           if (canonical_arr)
417             {
418               for (i = 0; i < nelts; i++)
419                 {
420                   if (canonical_arr[i] == NULL)
421                     {
422                       symname = SYMBOL_NAME (sym_arr[i]);
423                       canonical_arr[i] = savestring (symname, strlen (symname));
424                     }
425                 }
426             }
427           memcpy (return_values.sals, values.sals,
428                   (nelts * sizeof (struct symtab_and_line)));
429           return_values.nelts = nelts;
430           discard_cleanups (old_chain);
431           return return_values;
432         }
433
434       if (num >= nelts + 2)
435         {
436           printf_unfiltered ("No choice number %d.\n", num);
437         }
438       else
439         {
440           num -= 2;
441           if (values.sals[num].pc)
442             {
443               if (canonical_arr)
444                 {
445                   symname = SYMBOL_NAME (sym_arr[num]);
446                   make_cleanup (xfree, symname);
447                   canonical_arr[i] = savestring (symname, strlen (symname));
448                 }
449               return_values.sals[i++] = values.sals[num];
450               values.sals[num].pc = 0;
451             }
452           else
453             {
454               printf_unfiltered ("duplicate request for %d ignored.\n", num);
455             }
456         }
457
458       args = arg1;
459       while (*args == ' ' || *args == '\t')
460         args++;
461     }
462   return_values.nelts = i;
463   discard_cleanups (old_chain);
464   return return_values;
465 }
466 \f
467 /* The parser of linespec itself. */
468
469 /* Parse a string that specifies a line number.
470    Pass the address of a char * variable; that variable will be
471    advanced over the characters actually parsed.
472
473    The string can be:
474
475    LINENUM -- that line number in current file.  PC returned is 0.
476    FILE:LINENUM -- that line in that file.  PC returned is 0.
477    FUNCTION -- line number of openbrace of that function.
478    PC returned is the start of the function.
479    VARIABLE -- line number of definition of that variable.
480    PC returned is 0.
481    FILE:FUNCTION -- likewise, but prefer functions in that file.
482    *EXPR -- line in which address EXPR appears.
483
484    This may all be followed by an "if EXPR", which we ignore.
485
486    FUNCTION may be an undebuggable function found in minimal symbol table.
487
488    If the argument FUNFIRSTLINE is nonzero, we want the first line
489    of real code inside a function when a function is specified, and it is
490    not OK to specify a variable or type to get its line number.
491
492    DEFAULT_SYMTAB specifies the file to use if none is specified.
493    It defaults to current_source_symtab.
494    DEFAULT_LINE specifies the line number to use for relative
495    line numbers (that start with signs).  Defaults to current_source_line.
496    If CANONICAL is non-NULL, store an array of strings containing the canonical
497    line specs there if necessary. Currently overloaded member functions and
498    line numbers or static functions without a filename yield a canonical
499    line spec. The array and the line spec strings are allocated on the heap,
500    it is the callers responsibility to free them.
501
502    Note that it is possible to return zero for the symtab
503    if no file is validly specified.  Callers must check that.
504    Also, the line number returned may be invalid.  */
505
506 /* We allow single quotes in various places.  This is a hideous
507    kludge, which exists because the completer can't yet deal with the
508    lack of single quotes.  FIXME: write a linespec_completer which we
509    can use as appropriate instead of make_symbol_completion_list.  */
510
511 struct symtabs_and_lines
512 decode_line_1 (char **argptr, int funfirstline, struct symtab *default_symtab,
513                int default_line, char ***canonical)
514 {
515   struct symtabs_and_lines values;
516   struct symtab_and_line val;
517   register char *p, *p1;
518   char *q, *pp, *ii, *p2;
519 #if 0
520   char *q1;
521 #endif
522   register struct symtab *s;
523
524   register struct symbol *sym;
525   /* The symtab that SYM was found in.  */
526   struct symtab *sym_symtab;
527
528   register CORE_ADDR pc;
529   register struct minimal_symbol *msymbol;
530   char *copy;
531   struct symbol *sym_class;
532   int i1;
533   int is_quoted;
534   int is_quote_enclosed;
535   int has_parens;
536   int has_if = 0;
537   int has_comma = 0;
538   struct symbol **sym_arr;
539   struct type *t;
540   char *saved_arg = *argptr;
541   extern char *gdb_completer_quote_characters;
542
543   INIT_SAL (&val);              /* initialize to zeroes */
544
545   /* Defaults have defaults.  */
546
547   if (default_symtab == 0)
548     {
549       /* Use whatever we have for the default source line.  We don't use
550          get_current_or_default_symtab_and_line as it can recurse and call
551          us back! */
552       struct symtab_and_line cursal = 
553                         get_current_source_symtab_and_line ();
554       
555       default_symtab = cursal.symtab;
556       default_line = cursal.line;
557     }
558
559   /* See if arg is *PC */
560
561   if (**argptr == '*')
562     {
563       (*argptr)++;
564       pc = parse_and_eval_address_1 (argptr);
565
566       values.sals = (struct symtab_and_line *)
567         xmalloc (sizeof (struct symtab_and_line));
568
569       values.nelts = 1;
570       values.sals[0] = find_pc_line (pc, 0);
571       values.sals[0].pc = pc;
572       values.sals[0].section = find_pc_overlay (pc);
573
574       return values;
575     }
576
577   /* 'has_if' is for the syntax:
578    *     (gdb) break foo if (a==b)
579    */
580   if ((ii = strstr (*argptr, " if ")) != NULL ||
581       (ii = strstr (*argptr, "\tif ")) != NULL ||
582       (ii = strstr (*argptr, " if\t")) != NULL ||
583       (ii = strstr (*argptr, "\tif\t")) != NULL ||
584       (ii = strstr (*argptr, " if(")) != NULL ||
585       (ii = strstr (*argptr, "\tif( ")) != NULL)
586     has_if = 1;
587   /* Temporarily zap out "if (condition)" to not
588    * confuse the parenthesis-checking code below.
589    * This is undone below. Do not change ii!!
590    */
591   if (has_if)
592     {
593       *ii = '\0';
594     }
595
596   /* Set various flags.
597    * 'has_parens' is important for overload checking, where
598    * we allow things like: 
599    *     (gdb) break c::f(int)
600    */
601
602   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
603
604   is_quoted = (**argptr
605                && strchr (get_gdb_completer_quote_characters (),
606                           **argptr) != NULL);
607
608   has_parens = ((pp = strchr (*argptr, '(')) != NULL
609                 && (pp = strrchr (pp, ')')) != NULL);
610
611   /* Now that we're safely past the has_parens check,
612    * put back " if (condition)" so outer layers can see it 
613    */
614   if (has_if)
615     *ii = ' ';
616
617   /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
618      and we must isolate the first half.  Outer layers will call again later
619      for the second half.
620
621      Don't count commas that appear in argument lists of overloaded
622      functions, or in quoted strings.  It's stupid to go to this much
623      trouble when the rest of the function is such an obvious roach hotel.  */
624   ii = find_toplevel_char (*argptr, ',');
625   has_comma = (ii != 0);
626
627   /* Temporarily zap out second half to not
628    * confuse the code below.
629    * This is undone below. Do not change ii!!
630    */
631   if (has_comma)
632     {
633       *ii = '\0';
634     }
635
636   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
637   /* May also be CLASS::MEMBER, or NAMESPACE::NAME */
638   /* Look for ':', but ignore inside of <> */
639
640   s = NULL;
641   p = *argptr;
642   if (p[0] == '"')
643     {
644       is_quote_enclosed = 1;
645       (*argptr)++;
646       p++;
647     }
648   else
649     is_quote_enclosed = 0;
650   for (; *p; p++)
651     {
652       if (p[0] == '<')
653         {
654           char *temp_end = find_template_name_end (p);
655           if (!temp_end)
656             error ("malformed template specification in command");
657           p = temp_end;
658         }
659       /* Check for the end of the first half of the linespec.  End of line,
660          a tab, a double colon or the last single colon, or a space.  But
661          if enclosed in double quotes we do not break on enclosed spaces */
662       if (!*p
663           || p[0] == '\t'
664           || ((p[0] == ':')
665               && ((p[1] == ':') || (strchr (p + 1, ':') == NULL)))
666           || ((p[0] == ' ') && !is_quote_enclosed))
667         break;
668       if (p[0] == '.' && strchr (p, ':') == NULL)       /* Java qualified method. */
669         {
670           /* Find the *last* '.', since the others are package qualifiers. */
671           for (p1 = p; *p1; p1++)
672             {
673               if (*p1 == '.')
674                 p = p1;
675             }
676           break;
677         }
678     }
679   while (p[0] == ' ' || p[0] == '\t')
680     p++;
681
682   /* if the closing double quote was left at the end, remove it */
683   if (is_quote_enclosed)
684     {
685       char *closing_quote = strchr (p - 1, '"');
686       if (closing_quote && closing_quote[1] == '\0')
687         *closing_quote = '\0';
688     }
689
690   /* Now that we've safely parsed the first half,
691    * put back ',' so outer layers can see it 
692    */
693   if (has_comma)
694     *ii = ',';
695
696   if ((p[0] == ':' || p[0] == '.') && !has_parens)
697     {
698       /*  C++ */
699       /*  ... or Java */
700       if (is_quoted)
701         *argptr = *argptr + 1;
702       if (p[0] == '.' || p[1] == ':')
703         {
704           char *saved_arg2 = *argptr;
705           char *temp_end;
706           /* First check for "global" namespace specification,
707              of the form "::foo". If found, skip over the colons
708              and jump to normal symbol processing */
709           if (p[0] == ':' 
710               && ((*argptr == p) || (p[-1] == ' ') || (p[-1] == '\t')))
711             saved_arg2 += 2;
712
713           /* We have what looks like a class or namespace
714              scope specification (A::B), possibly with many
715              levels of namespaces or classes (A::B::C::D).
716
717              Some versions of the HP ANSI C++ compiler (as also possibly
718              other compilers) generate class/function/member names with
719              embedded double-colons if they are inside namespaces. To
720              handle this, we loop a few times, considering larger and
721              larger prefixes of the string as though they were single
722              symbols.  So, if the initially supplied string is
723              A::B::C::D::foo, we have to look up "A", then "A::B",
724              then "A::B::C", then "A::B::C::D", and finally
725              "A::B::C::D::foo" as single, monolithic symbols, because
726              A, B, C or D may be namespaces.
727
728              Note that namespaces can nest only inside other
729              namespaces, and not inside classes.  So we need only
730              consider *prefixes* of the string; there is no need to look up
731              "B::C" separately as a symbol in the previous example. */
732
733           p2 = p;               /* save for restart */
734           while (1)
735             {
736               /* Extract the class name.  */
737               p1 = p;
738               while (p != *argptr && p[-1] == ' ')
739                 --p;
740               copy = (char *) alloca (p - *argptr + 1);
741               memcpy (copy, *argptr, p - *argptr);
742               copy[p - *argptr] = 0;
743
744               /* Discard the class name from the arg.  */
745               p = p1 + (p1[0] == ':' ? 2 : 1);
746               while (*p == ' ' || *p == '\t')
747                 p++;
748               *argptr = p;
749
750               sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0,
751                                          (struct symtab **) NULL);
752
753               if (sym_class &&
754                   (t = check_typedef (SYMBOL_TYPE (sym_class)),
755                    (TYPE_CODE (t) == TYPE_CODE_STRUCT
756                     || TYPE_CODE (t) == TYPE_CODE_UNION)))
757                 {
758                   /* Arg token is not digits => try it as a function name
759                      Find the next token(everything up to end or next blank). */
760                   if (**argptr
761                       && strchr (get_gdb_completer_quote_characters (),
762                                  **argptr) != NULL)
763                     {
764                       p = skip_quoted (*argptr);
765                       *argptr = *argptr + 1;
766                     }
767                   else
768                     {
769                       p = *argptr;
770                       while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p != ':')
771                         p++;
772                     }
773 /*
774    q = operator_chars (*argptr, &q1);
775    if (q1 - q)
776    {
777    char *opname;
778    char *tmp = alloca (q1 - q + 1);
779    memcpy (tmp, q, q1 - q);
780    tmp[q1 - q] = '\0';
781    opname = cplus_mangle_opname (tmp, DMGL_ANSI);
782    if (opname == NULL)
783    {
784    cplusplus_error (saved_arg, "no mangling for \"%s\"\n", tmp);
785    }
786    copy = (char*) alloca (3 + strlen(opname));
787    sprintf (copy, "__%s", opname);
788    p = q1;
789    }
790    else
791  */
792                   {
793                     copy = (char *) alloca (p - *argptr + 1);
794                     memcpy (copy, *argptr, p - *argptr);
795                     copy[p - *argptr] = '\0';
796                     if (p != *argptr
797                         && copy[p - *argptr - 1]
798                         && strchr (get_gdb_completer_quote_characters (),
799                                    copy[p - *argptr - 1]) != NULL)
800                       copy[p - *argptr - 1] = '\0';
801                   }
802
803                   /* no line number may be specified */
804                   while (*p == ' ' || *p == '\t')
805                     p++;
806                   *argptr = p;
807
808                   sym = 0;
809                   i1 = 0;       /*  counter for the symbol array */
810                   sym_arr = (struct symbol **) alloca (total_number_of_methods (t)
811                                                 * sizeof (struct symbol *));
812
813                   if (destructor_name_p (copy, t))
814                     {
815                       /* Destructors are a special case.  */
816                       int m_index, f_index;
817
818                       if (get_destructor_fn_field (t, &m_index, &f_index))
819                         {
820                           struct fn_field *f = TYPE_FN_FIELDLIST1 (t, m_index);
821
822                           sym_arr[i1] =
823                             lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, f_index),
824                                            NULL, VAR_NAMESPACE, (int *) NULL,
825                                            (struct symtab **) NULL);
826                           if (sym_arr[i1])
827                             i1++;
828                         }
829                     }
830                   else
831                     i1 = find_methods (t, copy, sym_arr);
832                   if (i1 == 1)
833                     {
834                       /* There is exactly one field with that name.  */
835                       sym = sym_arr[0];
836
837                       if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
838                         {
839                           values.sals = (struct symtab_and_line *)
840                             xmalloc (sizeof (struct symtab_and_line));
841                           values.nelts = 1;
842                           values.sals[0] = find_function_start_sal (sym,
843                                                               funfirstline);
844                         }
845                       else
846                         {
847                           values.nelts = 0;
848                         }
849                       return values;
850                     }
851                   if (i1 > 0)
852                     {
853                       /* There is more than one field with that name
854                          (overloaded).  Ask the user which one to use.  */
855                       return decode_line_2 (sym_arr, i1, funfirstline, canonical);
856                     }
857                   else
858                     {
859                       char *tmp;
860
861                       if (is_operator_name (copy))
862                         {
863                           tmp = (char *) alloca (strlen (copy + 3) + 9);
864                           strcpy (tmp, "operator ");
865                           strcat (tmp, copy + 3);
866                         }
867                       else
868                         tmp = copy;
869                       if (tmp[0] == '~')
870                         cplusplus_error (saved_arg,
871                                          "the class `%s' does not have destructor defined\n",
872                                          SYMBOL_SOURCE_NAME (sym_class));
873                       else
874                         cplusplus_error (saved_arg,
875                                          "the class %s does not have any method named %s\n",
876                                          SYMBOL_SOURCE_NAME (sym_class), tmp);
877                     }
878                 }
879
880               /* Move pointer up to next possible class/namespace token */
881               p = p2 + 1;       /* restart with old value +1 */
882               /* Move pointer ahead to next double-colon */
883               while (*p && (p[0] != ' ') && (p[0] != '\t') && (p[0] != '\''))
884                 {
885                   if (p[0] == '<')
886                     {
887                       temp_end = find_template_name_end (p);
888                       if (!temp_end)
889                         error ("malformed template specification in command");
890                       p = temp_end;
891                     }
892                   else if ((p[0] == ':') && (p[1] == ':'))
893                     break;      /* found double-colon */
894                   else
895                     p++;
896                 }
897
898               if (*p != ':')
899                 break;          /* out of the while (1) */
900
901               p2 = p;           /* save restart for next time around */
902               *argptr = saved_arg2;     /* restore argptr */
903             }                   /* while (1) */
904
905           /* Last chance attempt -- check entire name as a symbol */
906           /* Use "copy" in preparation for jumping out of this block,
907              to be consistent with usage following the jump target */
908           copy = (char *) alloca (p - saved_arg2 + 1);
909           memcpy (copy, saved_arg2, p - saved_arg2);
910           /* Note: if is_quoted should be true, we snuff out quote here anyway */
911           copy[p - saved_arg2] = '\000';
912           /* Set argptr to skip over the name */
913           *argptr = (*p == '\'') ? p + 1 : p;
914           /* Look up entire name */
915           sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
916           s = (struct symtab *) 0;
917           /* Prepare to jump: restore the " if (condition)" so outer layers see it */
918           /* Symbol was found --> jump to normal symbol processing.
919              Code following "symbol_found" expects "copy" to have the
920              symbol name, "sym" to have the symbol pointer, "s" to be
921              a specified file's symtab, and sym_symtab to be the symbol's
922              symtab. */
923           /* By jumping there we avoid falling through the FILE:LINE and
924              FILE:FUNC processing stuff below */
925           if (sym)
926             goto symbol_found;
927
928           /* Couldn't find any interpretation as classes/namespaces, so give up */
929           /* The quotes are important if copy is empty.  */
930           cplusplus_error (saved_arg,
931                            "Can't find member of namespace, class, struct, or union named \"%s\"\n",
932                            copy);
933         }
934       /*  end of C++  */
935
936
937       /* Extract the file name.  */
938       p1 = p;
939       while (p != *argptr && p[-1] == ' ')
940         --p;
941       if ((*p == '"') && is_quote_enclosed)
942         --p;
943       copy = (char *) alloca (p - *argptr + 1);
944       memcpy (copy, *argptr, p - *argptr);
945       /* It may have the ending quote right after the file name */
946       if (is_quote_enclosed && copy[p - *argptr - 1] == '"')
947         copy[p - *argptr - 1] = 0;
948       else
949         copy[p - *argptr] = 0;
950
951       /* Find that file's data.  */
952       s = lookup_symtab (copy);
953       if (s == 0)
954         {
955           if (!have_full_symbols () && !have_partial_symbols ())
956             error ("No symbol table is loaded.  Use the \"file\" command.");
957           error ("No source file named %s.", copy);
958         }
959
960       /* Discard the file name from the arg.  */
961       p = p1 + 1;
962       while (*p == ' ' || *p == '\t')
963         p++;
964       *argptr = p;
965     }
966 #if 0
967   /* No one really seems to know why this was added. It certainly
968      breaks the command line, though, whenever the passed
969      name is of the form ClassName::Method. This bit of code
970      singles out the class name, and if funfirstline is set (for
971      example, you are setting a breakpoint at this function),
972      you get an error. This did not occur with earlier
973      verions, so I am ifdef'ing this out. 3/29/99 */
974   else
975     {
976       /* Check if what we have till now is a symbol name */
977
978       /* We may be looking at a template instantiation such
979          as "foo<int>".  Check here whether we know about it,
980          instead of falling through to the code below which
981          handles ordinary function names, because that code
982          doesn't like seeing '<' and '>' in a name -- the
983          skip_quoted call doesn't go past them.  So see if we
984          can figure it out right now. */
985
986       copy = (char *) alloca (p - *argptr + 1);
987       memcpy (copy, *argptr, p - *argptr);
988       copy[p - *argptr] = '\000';
989       sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
990       if (sym)
991         {
992           /* Yes, we have a symbol; jump to symbol processing */
993           /* Code after symbol_found expects S, SYM_SYMTAB, SYM, 
994              and COPY to be set correctly */
995           *argptr = (*p == '\'') ? p + 1 : p;
996           s = (struct symtab *) 0;
997           goto symbol_found;
998         }
999       /* Otherwise fall out from here and go to file/line spec
1000          processing, etc. */
1001     }
1002 #endif
1003
1004   /* S is specified file's symtab, or 0 if no file specified.
1005      arg no longer contains the file name.  */
1006
1007   /* Check whether arg is all digits (and sign) */
1008
1009   q = *argptr;
1010   if (*q == '-' || *q == '+')
1011     q++;
1012   while (*q >= '0' && *q <= '9')
1013     q++;
1014
1015   if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ','))
1016     {
1017       /* We found a token consisting of all digits -- at least one digit.  */
1018       enum sign
1019         {
1020           none, plus, minus
1021         }
1022       sign = none;
1023
1024       /* We might need a canonical line spec if no file was specified.  */
1025       int need_canonical = (s == 0) ? 1 : 0;
1026
1027       /* This is where we need to make sure that we have good defaults.
1028          We must guarantee that this section of code is never executed
1029          when we are called with just a function name, since
1030          get_current_or_default_source_symtab_and_line uses
1031          select_source_symtab that calls us with such an argument  */
1032
1033       if (s == 0 && default_symtab == 0)
1034         {
1035           struct symtab_and_line cursal =
1036                   get_current_or_default_source_symtab_and_line ();
1037       
1038           default_symtab = cursal.symtab;
1039           default_line = cursal.line;
1040         }
1041
1042       if (**argptr == '+')
1043         sign = plus, (*argptr)++;
1044       else if (**argptr == '-')
1045         sign = minus, (*argptr)++;
1046       val.line = atoi (*argptr);
1047       switch (sign)
1048         {
1049         case plus:
1050           if (q == *argptr)
1051             val.line = 5;
1052           if (s == 0)
1053             val.line = default_line + val.line;
1054           break;
1055         case minus:
1056           if (q == *argptr)
1057             val.line = 15;
1058           if (s == 0)
1059             val.line = default_line - val.line;
1060           else
1061             val.line = 1;
1062           break;
1063         case none:
1064           break;                /* No need to adjust val.line.  */
1065         }
1066
1067       while (*q == ' ' || *q == '\t')
1068         q++;
1069       *argptr = q;
1070       if (s == 0)
1071         s = default_symtab;
1072
1073       /* It is possible that this source file has more than one symtab, 
1074          and that the new line number specification has moved us from the
1075          default (in s) to a new one.  */
1076       val.symtab = find_line_symtab (s, val.line, NULL, NULL);
1077       if (val.symtab == 0)
1078         val.symtab = s;
1079
1080       val.pc = 0;
1081       values.sals = (struct symtab_and_line *)
1082         xmalloc (sizeof (struct symtab_and_line));
1083       values.sals[0] = val;
1084       values.nelts = 1;
1085       if (need_canonical)
1086         build_canonical_line_spec (values.sals, NULL, canonical);
1087       return values;
1088     }
1089
1090   /* Arg token is not digits => try it as a variable name
1091      Find the next token (everything up to end or next whitespace).  */
1092
1093   if (**argptr == '$')          /* May be a convenience variable */
1094     p = skip_quoted (*argptr + (((*argptr)[1] == '$') ? 2 : 1));        /* One or two $ chars possible */
1095   else if (is_quoted)
1096     {
1097       p = skip_quoted (*argptr);
1098       if (p[-1] != '\'')
1099         error ("Unmatched single quote.");
1100     }
1101   else if (has_parens)
1102     {
1103       p = pp + 1;
1104     }
1105   else
1106     {
1107       p = skip_quoted (*argptr);
1108     }
1109
1110   copy = (char *) alloca (p - *argptr + 1);
1111   memcpy (copy, *argptr, p - *argptr);
1112   copy[p - *argptr] = '\0';
1113   if (p != *argptr
1114       && copy[0]
1115       && copy[0] == copy[p - *argptr - 1]
1116       && strchr (get_gdb_completer_quote_characters (), copy[0]) != NULL)
1117     {
1118       copy[p - *argptr - 1] = '\0';
1119       copy++;
1120     }
1121   while (*p == ' ' || *p == '\t')
1122     p++;
1123   *argptr = p;
1124
1125   /* If it starts with $: may be a legitimate variable or routine name
1126      (e.g. HP-UX millicode routines such as $$dyncall), or it may
1127      be history value, or it may be a convenience variable */
1128
1129   if (*copy == '$')
1130     {
1131       struct value *valx;
1132       int index = 0;
1133       int need_canonical = 0;
1134
1135       p = (copy[1] == '$') ? copy + 2 : copy + 1;
1136       while (*p >= '0' && *p <= '9')
1137         p++;
1138       if (!*p)                  /* reached end of token without hitting non-digit */
1139         {
1140           /* We have a value history reference */
1141           sscanf ((copy[1] == '$') ? copy + 2 : copy + 1, "%d", &index);
1142           valx = access_value_history ((copy[1] == '$') ? -index : index);
1143           if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
1144             error ("History values used in line specs must have integer values.");
1145         }
1146       else
1147         {
1148           /* Not all digits -- may be user variable/function or a
1149              convenience variable */
1150
1151           /* Look up entire name as a symbol first */
1152           sym = lookup_symbol (copy, 0, VAR_NAMESPACE, 0, &sym_symtab);
1153           s = (struct symtab *) 0;
1154           need_canonical = 1;
1155           /* Symbol was found --> jump to normal symbol processing.
1156              Code following "symbol_found" expects "copy" to have the
1157              symbol name, "sym" to have the symbol pointer, "s" to be
1158              a specified file's symtab, and sym_symtab to be the symbol's
1159              symtab. */
1160           if (sym)
1161             goto symbol_found;
1162
1163           /* If symbol was not found, look in minimal symbol tables */
1164           msymbol = lookup_minimal_symbol (copy, NULL, NULL);
1165           /* Min symbol was found --> jump to minsym processing. */
1166           if (msymbol)
1167             goto minimal_symbol_found;
1168
1169           /* Not a user variable or function -- must be convenience variable */
1170           need_canonical = (s == 0) ? 1 : 0;
1171           valx = value_of_internalvar (lookup_internalvar (copy + 1));
1172           if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
1173             error ("Convenience variables used in line specs must have integer values.");
1174         }
1175
1176       /* Either history value or convenience value from above, in valx */
1177       val.symtab = s ? s : default_symtab;
1178       val.line = value_as_long (valx);
1179       val.pc = 0;
1180
1181       values.sals = (struct symtab_and_line *) xmalloc (sizeof val);
1182       values.sals[0] = val;
1183       values.nelts = 1;
1184
1185       if (need_canonical)
1186         build_canonical_line_spec (values.sals, NULL, canonical);
1187
1188       return values;
1189     }
1190
1191
1192   /* Look up that token as a variable.
1193      If file specified, use that file's per-file block to start with.  */
1194
1195   sym = lookup_symbol (copy,
1196                        (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
1197                         : get_selected_block (0)),
1198                        VAR_NAMESPACE, 0, &sym_symtab);
1199
1200 symbol_found:                   /* We also jump here from inside the C++ class/namespace 
1201                                    code on finding a symbol of the form "A::B::C" */
1202
1203   if (sym != NULL)
1204     {
1205       if (SYMBOL_CLASS (sym) == LOC_BLOCK)
1206         {
1207           /* Arg is the name of a function */
1208           values.sals = (struct symtab_and_line *)
1209             xmalloc (sizeof (struct symtab_and_line));
1210           values.sals[0] = find_function_start_sal (sym, funfirstline);
1211           values.nelts = 1;
1212
1213           /* Don't use the SYMBOL_LINE; if used at all it points to
1214              the line containing the parameters or thereabouts, not
1215              the first line of code.  */
1216
1217           /* We might need a canonical line spec if it is a static
1218              function.  */
1219           if (s == 0)
1220             {
1221               struct blockvector *bv = BLOCKVECTOR (sym_symtab);
1222               struct block *b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1223               if (lookup_block_symbol (b, copy, NULL, VAR_NAMESPACE) != NULL)
1224                 build_canonical_line_spec (values.sals, copy, canonical);
1225             }
1226           return values;
1227         }
1228       else
1229         {
1230           if (funfirstline)
1231             error ("\"%s\" is not a function", copy);
1232           else if (SYMBOL_LINE (sym) != 0)
1233             {
1234               /* We know its line number.  */
1235               values.sals = (struct symtab_and_line *)
1236                 xmalloc (sizeof (struct symtab_and_line));
1237               values.nelts = 1;
1238               memset (&values.sals[0], 0, sizeof (values.sals[0]));
1239               values.sals[0].symtab = sym_symtab;
1240               values.sals[0].line = SYMBOL_LINE (sym);
1241               return values;
1242             }
1243           else
1244             /* This can happen if it is compiled with a compiler which doesn't
1245                put out line numbers for variables.  */
1246             /* FIXME: Shouldn't we just set .line and .symtab to zero
1247                and return?  For example, "info line foo" could print
1248                the address.  */
1249             error ("Line number not known for symbol \"%s\"", copy);
1250         }
1251     }
1252
1253   msymbol = lookup_minimal_symbol (copy, NULL, NULL);
1254
1255 minimal_symbol_found:           /* We also jump here from the case for variables
1256                                    that begin with '$' */
1257
1258   if (msymbol != NULL)
1259     {
1260       values.sals = (struct symtab_and_line *)
1261         xmalloc (sizeof (struct symtab_and_line));
1262       values.sals[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol),
1263                                           (struct sec *) 0, 0);
1264       values.sals[0].section = SYMBOL_BFD_SECTION (msymbol);
1265       if (funfirstline)
1266         {
1267           values.sals[0].pc += FUNCTION_START_OFFSET;
1268           values.sals[0].pc = SKIP_PROLOGUE (values.sals[0].pc);
1269         }
1270       values.nelts = 1;
1271       return values;
1272     }
1273
1274   if (!have_full_symbols () &&
1275       !have_partial_symbols () && !have_minimal_symbols ())
1276     error ("No symbol table is loaded.  Use the \"file\" command.");
1277
1278   error ("Function \"%s\" not defined.", copy);
1279   return values;                /* for lint */
1280 }