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