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