198944Sobrien/* Parser for linespec for the GNU debugger, GDB.
298944Sobrien   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3130803Smarcel   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
498944Sobrien   Free Software Foundation, Inc.
598944Sobrien
698944Sobrien   This file is part of GDB.
798944Sobrien
898944Sobrien   This program is free software; you can redistribute it and/or modify
998944Sobrien   it under the terms of the GNU General Public License as published by
1098944Sobrien   the Free Software Foundation; either version 2 of the License, or
1198944Sobrien   (at your option) any later version.
1298944Sobrien
1398944Sobrien   This program is distributed in the hope that it will be useful,
1498944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1598944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1698944Sobrien   GNU General Public License for more details.
1798944Sobrien
1898944Sobrien   You should have received a copy of the GNU General Public License
1998944Sobrien   along with this program; if not, write to the Free Software
2098944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2198944Sobrien   Boston, MA 02111-1307, USA.  */
2298944Sobrien
2398944Sobrien#include "defs.h"
2498944Sobrien#include "symtab.h"
2598944Sobrien#include "frame.h"
2698944Sobrien#include "command.h"
2798944Sobrien#include "symfile.h"
2898944Sobrien#include "objfiles.h"
29130803Smarcel#include "source.h"
3098944Sobrien#include "demangle.h"
3198944Sobrien#include "value.h"
3298944Sobrien#include "completer.h"
3398944Sobrien#include "cp-abi.h"
34130803Smarcel#include "parser-defs.h"
35130803Smarcel#include "block.h"
36130803Smarcel#include "objc-lang.h"
37130803Smarcel#include "linespec.h"
3898944Sobrien
3998944Sobrien/* We share this one with symtab.c, but it is not exported widely. */
4098944Sobrien
4198944Sobrienextern char *operator_chars (char *, char **);
4298944Sobrien
4398944Sobrien/* Prototypes for local functions */
4498944Sobrien
45130803Smarcelstatic void initialize_defaults (struct symtab **default_symtab,
46130803Smarcel				 int *default_line);
4798944Sobrien
48130803Smarcelstatic void set_flags (char *arg, int *is_quoted, char **paren_pointer);
49130803Smarcel
50130803Smarcelstatic struct symtabs_and_lines decode_indirect (char **argptr);
51130803Smarcel
52130803Smarcelstatic char *locate_first_half (char **argptr, int *is_quote_enclosed);
53130803Smarcel
54130803Smarcelstatic struct symtabs_and_lines decode_objc (char **argptr,
55130803Smarcel					     int funfirstline,
56130803Smarcel					     struct symtab *file_symtab,
57130803Smarcel					     char ***canonical,
58130803Smarcel					     char *saved_arg);
59130803Smarcel
60130803Smarcelstatic struct symtabs_and_lines decode_compound (char **argptr,
61130803Smarcel						 int funfirstline,
62130803Smarcel						 char ***canonical,
63130803Smarcel						 char *saved_arg,
64130803Smarcel						 char *p);
65130803Smarcel
66130803Smarcelstatic struct symbol *lookup_prefix_sym (char **argptr, char *p);
67130803Smarcel
68130803Smarcelstatic struct symtabs_and_lines find_method (int funfirstline,
69130803Smarcel					     char ***canonical,
70130803Smarcel					     char *saved_arg,
71130803Smarcel					     char *copy,
72130803Smarcel					     struct type *t,
73130803Smarcel					     struct symbol *sym_class);
74130803Smarcel
75130803Smarcelstatic int collect_methods (char *copy, struct type *t,
76130803Smarcel			    struct symbol **sym_arr);
77130803Smarcel
78130803Smarcelstatic NORETURN void cplusplus_error (const char *name,
79130803Smarcel				      const char *fmt, ...)
80130803Smarcel     ATTR_NORETURN ATTR_FORMAT (printf, 2, 3);
81130803Smarcel
8298944Sobrienstatic int total_number_of_methods (struct type *type);
8398944Sobrien
8498944Sobrienstatic int find_methods (struct type *, char *, struct symbol **);
8598944Sobrien
86130803Smarcelstatic int add_matching_methods (int method_counter, struct type *t,
87130803Smarcel				 struct symbol **sym_arr);
88130803Smarcel
89130803Smarcelstatic int add_constructors (int method_counter, struct type *t,
90130803Smarcel			     struct symbol **sym_arr);
91130803Smarcel
9298944Sobrienstatic void build_canonical_line_spec (struct symtab_and_line *,
9398944Sobrien				       char *, char ***);
9498944Sobrien
9598944Sobrienstatic char *find_toplevel_char (char *s, char c);
9698944Sobrien
97130803Smarcelstatic int is_objc_method_format (const char *s);
98130803Smarcel
9998944Sobrienstatic struct symtabs_and_lines decode_line_2 (struct symbol *[],
10098944Sobrien					       int, int, char ***);
10198944Sobrien
102130803Smarcelstatic struct symtab *symtab_from_filename (char **argptr,
103130803Smarcel					    char *p, int is_quote_enclosed,
104130803Smarcel					    int *not_found_ptr);
105130803Smarcel
106130803Smarcelstatic struct
107130803Smarcelsymtabs_and_lines decode_all_digits (char **argptr,
108130803Smarcel				     struct symtab *default_symtab,
109130803Smarcel				     int default_line,
110130803Smarcel				     char ***canonical,
111130803Smarcel				     struct symtab *file_symtab,
112130803Smarcel				     char *q);
113130803Smarcel
114130803Smarcelstatic struct symtabs_and_lines decode_dollar (char *copy,
115130803Smarcel					       int funfirstline,
116130803Smarcel					       struct symtab *default_symtab,
117130803Smarcel					       char ***canonical,
118130803Smarcel					       struct symtab *file_symtab);
119130803Smarcel
120130803Smarcelstatic struct symtabs_and_lines decode_variable (char *copy,
121130803Smarcel						 int funfirstline,
122130803Smarcel						 char ***canonical,
123130803Smarcel						 struct symtab *file_symtab,
124130803Smarcel						 int *not_found_ptr);
125130803Smarcel
126130803Smarcelstatic struct
127130803Smarcelsymtabs_and_lines symbol_found (int funfirstline,
128130803Smarcel				char ***canonical,
129130803Smarcel				char *copy,
130130803Smarcel				struct symbol *sym,
131130803Smarcel				struct symtab *file_symtab,
132130803Smarcel				struct symtab *sym_symtab);
133130803Smarcel
134130803Smarcelstatic struct
135130803Smarcelsymtabs_and_lines minsym_found (int funfirstline,
136130803Smarcel				struct minimal_symbol *msymbol);
137130803Smarcel
13898944Sobrien/* Helper functions. */
13998944Sobrien
14098944Sobrien/* Issue a helpful hint on using the command completion feature on
14198944Sobrien   single quoted demangled C++ symbols as part of the completion
14298944Sobrien   error.  */
14398944Sobrien
144130803Smarcelstatic NORETURN void
14598944Sobriencplusplus_error (const char *name, const char *fmt, ...)
14698944Sobrien{
14798944Sobrien  struct ui_file *tmp_stream;
14898944Sobrien  tmp_stream = mem_fileopen ();
14998944Sobrien  make_cleanup_ui_file_delete (tmp_stream);
15098944Sobrien
15198944Sobrien  {
15298944Sobrien    va_list args;
15398944Sobrien    va_start (args, fmt);
15498944Sobrien    vfprintf_unfiltered (tmp_stream, fmt, args);
15598944Sobrien    va_end (args);
15698944Sobrien  }
15798944Sobrien
15898944Sobrien  while (*name == '\'')
15998944Sobrien    name++;
16098944Sobrien  fprintf_unfiltered (tmp_stream,
16198944Sobrien		      ("Hint: try '%s<TAB> or '%s<ESC-?>\n"
16298944Sobrien		       "(Note leading single quote.)"),
16398944Sobrien		      name, name);
16498944Sobrien  error_stream (tmp_stream);
16598944Sobrien}
16698944Sobrien
16798944Sobrien/* Return the number of methods described for TYPE, including the
16898944Sobrien   methods from types it derives from. This can't be done in the symbol
16998944Sobrien   reader because the type of the baseclass might still be stubbed
17098944Sobrien   when the definition of the derived class is parsed.  */
17198944Sobrien
17298944Sobrienstatic int
17398944Sobrientotal_number_of_methods (struct type *type)
17498944Sobrien{
17598944Sobrien  int n;
17698944Sobrien  int count;
17798944Sobrien
17898944Sobrien  CHECK_TYPEDEF (type);
17998944Sobrien  if (TYPE_CPLUS_SPECIFIC (type) == NULL)
18098944Sobrien    return 0;
18198944Sobrien  count = TYPE_NFN_FIELDS_TOTAL (type);
18298944Sobrien
18398944Sobrien  for (n = 0; n < TYPE_N_BASECLASSES (type); n++)
18498944Sobrien    count += total_number_of_methods (TYPE_BASECLASS (type, n));
18598944Sobrien
18698944Sobrien  return count;
18798944Sobrien}
18898944Sobrien
18998944Sobrien/* Recursive helper function for decode_line_1.
19098944Sobrien   Look for methods named NAME in type T.
19198944Sobrien   Return number of matches.
19298944Sobrien   Put matches in SYM_ARR, which should have been allocated with
19398944Sobrien   a size of total_number_of_methods (T) * sizeof (struct symbol *).
19498944Sobrien   Note that this function is g++ specific.  */
19598944Sobrien
19698944Sobrienstatic int
19798944Sobrienfind_methods (struct type *t, char *name, struct symbol **sym_arr)
19898944Sobrien{
19998944Sobrien  int i1 = 0;
20098944Sobrien  int ibase;
20198944Sobrien  char *class_name = type_name_no_tag (t);
20298944Sobrien
20398944Sobrien  /* Ignore this class if it doesn't have a name.  This is ugly, but
20498944Sobrien     unless we figure out how to get the physname without the name of
20598944Sobrien     the class, then the loop can't do any good.  */
20698944Sobrien  if (class_name
20798944Sobrien      && (lookup_symbol (class_name, (struct block *) NULL,
208130803Smarcel			 STRUCT_DOMAIN, (int *) NULL,
20998944Sobrien			 (struct symtab **) NULL)))
21098944Sobrien    {
21198944Sobrien      int method_counter;
212130803Smarcel      int name_len = strlen (name);
21398944Sobrien
21498944Sobrien      CHECK_TYPEDEF (t);
21598944Sobrien
21698944Sobrien      /* Loop over each method name.  At this level, all overloads of a name
21798944Sobrien         are counted as a single name.  There is an inner loop which loops over
21898944Sobrien         each overload.  */
21998944Sobrien
22098944Sobrien      for (method_counter = TYPE_NFN_FIELDS (t) - 1;
22198944Sobrien	   method_counter >= 0;
22298944Sobrien	   --method_counter)
22398944Sobrien	{
22498944Sobrien	  char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
22598944Sobrien	  char dem_opname[64];
22698944Sobrien
22798944Sobrien	  if (strncmp (method_name, "__", 2) == 0 ||
22898944Sobrien	      strncmp (method_name, "op", 2) == 0 ||
22998944Sobrien	      strncmp (method_name, "type", 4) == 0)
23098944Sobrien	    {
23198944Sobrien	      if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
23298944Sobrien		method_name = dem_opname;
23398944Sobrien	      else if (cplus_demangle_opname (method_name, dem_opname, 0))
23498944Sobrien		method_name = dem_opname;
23598944Sobrien	    }
23698944Sobrien
23798944Sobrien	  if (strcmp_iw (name, method_name) == 0)
23898944Sobrien	    /* Find all the overloaded methods with that name.  */
239130803Smarcel	    i1 += add_matching_methods (method_counter, t,
240130803Smarcel					sym_arr + i1);
241130803Smarcel	  else if (strncmp (class_name, name, name_len) == 0
242130803Smarcel		   && (class_name[name_len] == '\0'
243130803Smarcel		       || class_name[name_len] == '<'))
244130803Smarcel	    i1 += add_constructors (method_counter, t,
245130803Smarcel				    sym_arr + i1);
24698944Sobrien	}
24798944Sobrien    }
24898944Sobrien
24998944Sobrien  /* Only search baseclasses if there is no match yet, since names in
25098944Sobrien     derived classes override those in baseclasses.
25198944Sobrien
25298944Sobrien     FIXME: The above is not true; it is only true of member functions
25398944Sobrien     if they have the same number of arguments (??? - section 13.1 of the
25498944Sobrien     ARM says the function members are not in the same scope but doesn't
25598944Sobrien     really spell out the rules in a way I understand.  In any case, if
25698944Sobrien     the number of arguments differ this is a case in which we can overload
25798944Sobrien     rather than hiding without any problem, and gcc 2.4.5 does overload
25898944Sobrien     rather than hiding in this case).  */
25998944Sobrien
26098944Sobrien  if (i1 == 0)
26198944Sobrien    for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
26298944Sobrien      i1 += find_methods (TYPE_BASECLASS (t, ibase), name, sym_arr + i1);
26398944Sobrien
26498944Sobrien  return i1;
26598944Sobrien}
26698944Sobrien
267130803Smarcel/* Add the symbols associated to methods of the class whose type is T
268130803Smarcel   and whose name matches the method indexed by METHOD_COUNTER in the
269130803Smarcel   array SYM_ARR.  Return the number of methods added.  */
270130803Smarcel
271130803Smarcelstatic int
272130803Smarceladd_matching_methods (int method_counter, struct type *t,
273130803Smarcel		      struct symbol **sym_arr)
274130803Smarcel{
275130803Smarcel  int field_counter;
276130803Smarcel  int i1 = 0;
277130803Smarcel
278130803Smarcel  for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
279130803Smarcel       field_counter >= 0;
280130803Smarcel       --field_counter)
281130803Smarcel    {
282130803Smarcel      struct fn_field *f;
283130803Smarcel      char *phys_name;
284130803Smarcel
285130803Smarcel      f = TYPE_FN_FIELDLIST1 (t, method_counter);
286130803Smarcel
287130803Smarcel      if (TYPE_FN_FIELD_STUB (f, field_counter))
288130803Smarcel	{
289130803Smarcel	  char *tmp_name;
290130803Smarcel
291130803Smarcel	  tmp_name = gdb_mangle_name (t,
292130803Smarcel				      method_counter,
293130803Smarcel				      field_counter);
294130803Smarcel	  phys_name = alloca (strlen (tmp_name) + 1);
295130803Smarcel	  strcpy (phys_name, tmp_name);
296130803Smarcel	  xfree (tmp_name);
297130803Smarcel	}
298130803Smarcel      else
299130803Smarcel	phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
300130803Smarcel
301130803Smarcel      /* Destructor is handled by caller, don't add it to
302130803Smarcel	 the list.  */
303130803Smarcel      if (is_destructor_name (phys_name) != 0)
304130803Smarcel	continue;
305130803Smarcel
306130803Smarcel      sym_arr[i1] = lookup_symbol (phys_name,
307130803Smarcel				   NULL, VAR_DOMAIN,
308130803Smarcel				   (int *) NULL,
309130803Smarcel				   (struct symtab **) NULL);
310130803Smarcel      if (sym_arr[i1])
311130803Smarcel	i1++;
312130803Smarcel      else
313130803Smarcel	{
314130803Smarcel	  /* This error message gets printed, but the method
315130803Smarcel	     still seems to be found
316130803Smarcel	     fputs_filtered("(Cannot find method ", gdb_stdout);
317130803Smarcel	     fprintf_symbol_filtered (gdb_stdout, phys_name,
318130803Smarcel	     language_cplus,
319130803Smarcel	     DMGL_PARAMS | DMGL_ANSI);
320130803Smarcel	     fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
321130803Smarcel	  */
322130803Smarcel	}
323130803Smarcel    }
324130803Smarcel
325130803Smarcel  return i1;
326130803Smarcel}
327130803Smarcel
328130803Smarcel/* Add the symbols associated to constructors of the class whose type
329130803Smarcel   is CLASS_TYPE and which are indexed by by METHOD_COUNTER to the
330130803Smarcel   array SYM_ARR.  Return the number of methods added.  */
331130803Smarcel
332130803Smarcelstatic int
333130803Smarceladd_constructors (int method_counter, struct type *t,
334130803Smarcel		  struct symbol **sym_arr)
335130803Smarcel{
336130803Smarcel  int field_counter;
337130803Smarcel  int i1 = 0;
338130803Smarcel
339130803Smarcel  /* For GCC 3.x and stabs, constructors and destructors
340130803Smarcel     have names like __base_ctor and __complete_dtor.
341130803Smarcel     Check the physname for now if we're looking for a
342130803Smarcel     constructor.  */
343130803Smarcel  for (field_counter
344130803Smarcel	 = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
345130803Smarcel       field_counter >= 0;
346130803Smarcel       --field_counter)
347130803Smarcel    {
348130803Smarcel      struct fn_field *f;
349130803Smarcel      char *phys_name;
350130803Smarcel
351130803Smarcel      f = TYPE_FN_FIELDLIST1 (t, method_counter);
352130803Smarcel
353130803Smarcel      /* GCC 3.x will never produce stabs stub methods, so
354130803Smarcel	 we don't need to handle this case.  */
355130803Smarcel      if (TYPE_FN_FIELD_STUB (f, field_counter))
356130803Smarcel	continue;
357130803Smarcel      phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
358130803Smarcel      if (! is_constructor_name (phys_name))
359130803Smarcel	continue;
360130803Smarcel
361130803Smarcel      /* If this method is actually defined, include it in the
362130803Smarcel	 list.  */
363130803Smarcel      sym_arr[i1] = lookup_symbol (phys_name,
364130803Smarcel				   NULL, VAR_DOMAIN,
365130803Smarcel				   (int *) NULL,
366130803Smarcel				   (struct symtab **) NULL);
367130803Smarcel      if (sym_arr[i1])
368130803Smarcel	i1++;
369130803Smarcel    }
370130803Smarcel
371130803Smarcel  return i1;
372130803Smarcel}
373130803Smarcel
37498944Sobrien/* Helper function for decode_line_1.
37598944Sobrien   Build a canonical line spec in CANONICAL if it is non-NULL and if
37698944Sobrien   the SAL has a symtab.
37798944Sobrien   If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
37898944Sobrien   If SYMNAME is NULL the line number from SAL is used and the canonical
37998944Sobrien   line spec is `filename:linenum'.  */
38098944Sobrien
38198944Sobrienstatic void
38298944Sobrienbuild_canonical_line_spec (struct symtab_and_line *sal, char *symname,
38398944Sobrien			   char ***canonical)
38498944Sobrien{
38598944Sobrien  char **canonical_arr;
38698944Sobrien  char *canonical_name;
38798944Sobrien  char *filename;
38898944Sobrien  struct symtab *s = sal->symtab;
38998944Sobrien
39098944Sobrien  if (s == (struct symtab *) NULL
39198944Sobrien      || s->filename == (char *) NULL
39298944Sobrien      || canonical == (char ***) NULL)
39398944Sobrien    return;
39498944Sobrien
39598944Sobrien  canonical_arr = (char **) xmalloc (sizeof (char *));
39698944Sobrien  *canonical = canonical_arr;
39798944Sobrien
39898944Sobrien  filename = s->filename;
39998944Sobrien  if (symname != NULL)
40098944Sobrien    {
40198944Sobrien      canonical_name = xmalloc (strlen (filename) + strlen (symname) + 2);
40298944Sobrien      sprintf (canonical_name, "%s:%s", filename, symname);
40398944Sobrien    }
40498944Sobrien  else
40598944Sobrien    {
40698944Sobrien      canonical_name = xmalloc (strlen (filename) + 30);
40798944Sobrien      sprintf (canonical_name, "%s:%d", filename, sal->line);
40898944Sobrien    }
40998944Sobrien  canonical_arr[0] = canonical_name;
41098944Sobrien}
41198944Sobrien
41298944Sobrien
41398944Sobrien
41498944Sobrien/* Find an instance of the character C in the string S that is outside
41598944Sobrien   of all parenthesis pairs, single-quoted strings, and double-quoted
416130803Smarcel   strings.  Also, ignore the char within a template name, like a ','
417130803Smarcel   within foo<int, int>.  */
418130803Smarcel
41998944Sobrienstatic char *
42098944Sobrienfind_toplevel_char (char *s, char c)
42198944Sobrien{
42298944Sobrien  int quoted = 0;		/* zero if we're not in quotes;
42398944Sobrien				   '"' if we're in a double-quoted string;
42498944Sobrien				   '\'' if we're in a single-quoted string.  */
425130803Smarcel  int depth = 0;		/* Number of unclosed parens we've seen.  */
42698944Sobrien  char *scan;
42798944Sobrien
42898944Sobrien  for (scan = s; *scan; scan++)
42998944Sobrien    {
43098944Sobrien      if (quoted)
43198944Sobrien	{
43298944Sobrien	  if (*scan == quoted)
43398944Sobrien	    quoted = 0;
43498944Sobrien	  else if (*scan == '\\' && *(scan + 1))
43598944Sobrien	    scan++;
43698944Sobrien	}
43798944Sobrien      else if (*scan == c && ! quoted && depth == 0)
43898944Sobrien	return scan;
43998944Sobrien      else if (*scan == '"' || *scan == '\'')
44098944Sobrien	quoted = *scan;
441130803Smarcel      else if (*scan == '(' || *scan == '<')
44298944Sobrien	depth++;
443130803Smarcel      else if ((*scan == ')' || *scan == '>') && depth > 0)
44498944Sobrien	depth--;
44598944Sobrien    }
44698944Sobrien
44798944Sobrien  return 0;
44898944Sobrien}
44998944Sobrien
450130803Smarcel/* Determines if the gives string corresponds to an Objective-C method
451130803Smarcel   representation, such as -[Foo bar:] or +[Foo bar]. Objective-C symbols
452130803Smarcel   are allowed to have spaces and parentheses in them.  */
453130803Smarcel
454130803Smarcelstatic int
455130803Smarcelis_objc_method_format (const char *s)
456130803Smarcel{
457130803Smarcel  if (s == NULL || *s == '\0')
458130803Smarcel    return 0;
459130803Smarcel  /* Handle arguments with the format FILENAME:SYMBOL.  */
460130803Smarcel  if ((s[0] == ':') && (strchr ("+-", s[1]) != NULL)
461130803Smarcel      && (s[2] == '[') && strchr(s, ']'))
462130803Smarcel    return 1;
463130803Smarcel  /* Handle arguments that are just SYMBOL.  */
464130803Smarcel  else if ((strchr ("+-", s[0]) != NULL) && (s[1] == '[') && strchr(s, ']'))
465130803Smarcel    return 1;
466130803Smarcel  return 0;
467130803Smarcel}
468130803Smarcel
46998944Sobrien/* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
47098944Sobrien   operate on (ask user if necessary).
47198944Sobrien   If CANONICAL is non-NULL return a corresponding array of mangled names
47298944Sobrien   as canonical line specs there.  */
47398944Sobrien
47498944Sobrienstatic struct symtabs_and_lines
47598944Sobriendecode_line_2 (struct symbol *sym_arr[], int nelts, int funfirstline,
47698944Sobrien	       char ***canonical)
47798944Sobrien{
47898944Sobrien  struct symtabs_and_lines values, return_values;
47998944Sobrien  char *args, *arg1;
48098944Sobrien  int i;
48198944Sobrien  char *prompt;
48298944Sobrien  char *symname;
48398944Sobrien  struct cleanup *old_chain;
48498944Sobrien  char **canonical_arr = (char **) NULL;
48598944Sobrien
48698944Sobrien  values.sals = (struct symtab_and_line *)
48798944Sobrien    alloca (nelts * sizeof (struct symtab_and_line));
48898944Sobrien  return_values.sals = (struct symtab_and_line *)
48998944Sobrien    xmalloc (nelts * sizeof (struct symtab_and_line));
49098944Sobrien  old_chain = make_cleanup (xfree, return_values.sals);
49198944Sobrien
49298944Sobrien  if (canonical)
49398944Sobrien    {
49498944Sobrien      canonical_arr = (char **) xmalloc (nelts * sizeof (char *));
49598944Sobrien      make_cleanup (xfree, canonical_arr);
49698944Sobrien      memset (canonical_arr, 0, nelts * sizeof (char *));
49798944Sobrien      *canonical = canonical_arr;
49898944Sobrien    }
49998944Sobrien
50098944Sobrien  i = 0;
50198944Sobrien  printf_unfiltered ("[0] cancel\n[1] all\n");
50298944Sobrien  while (i < nelts)
50398944Sobrien    {
504130803Smarcel      init_sal (&return_values.sals[i]);	/* Initialize to zeroes.  */
505130803Smarcel      init_sal (&values.sals[i]);
50698944Sobrien      if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
50798944Sobrien	{
50898944Sobrien	  values.sals[i] = find_function_start_sal (sym_arr[i], funfirstline);
509130803Smarcel	  if (values.sals[i].symtab)
510130803Smarcel	    printf_unfiltered ("[%d] %s at %s:%d\n",
511130803Smarcel			       (i + 2),
512130803Smarcel			       SYMBOL_PRINT_NAME (sym_arr[i]),
513130803Smarcel			       values.sals[i].symtab->filename,
514130803Smarcel			       values.sals[i].line);
515130803Smarcel	  else
516130803Smarcel	    printf_unfiltered ("[%d] %s at ?FILE:%d [No symtab? Probably broken debug info...]\n",
517130803Smarcel			       (i + 2),
518130803Smarcel			       SYMBOL_PRINT_NAME (sym_arr[i]),
519130803Smarcel			       values.sals[i].line);
520130803Smarcel
52198944Sobrien	}
52298944Sobrien      else
52398944Sobrien	printf_unfiltered ("?HERE\n");
52498944Sobrien      i++;
52598944Sobrien    }
52698944Sobrien
527130803Smarcel  prompt = getenv ("PS2");
528130803Smarcel  if (prompt == NULL)
52998944Sobrien    {
53098944Sobrien      prompt = "> ";
53198944Sobrien    }
53298944Sobrien  args = command_line_input (prompt, 0, "overload-choice");
53398944Sobrien
53498944Sobrien  if (args == 0 || *args == 0)
53598944Sobrien    error_no_arg ("one or more choice numbers");
53698944Sobrien
53798944Sobrien  i = 0;
53898944Sobrien  while (*args)
53998944Sobrien    {
54098944Sobrien      int num;
54198944Sobrien
54298944Sobrien      arg1 = args;
54398944Sobrien      while (*arg1 >= '0' && *arg1 <= '9')
54498944Sobrien	arg1++;
54598944Sobrien      if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
54698944Sobrien	error ("Arguments must be choice numbers.");
54798944Sobrien
54898944Sobrien      num = atoi (args);
54998944Sobrien
55098944Sobrien      if (num == 0)
55198944Sobrien	error ("canceled");
55298944Sobrien      else if (num == 1)
55398944Sobrien	{
55498944Sobrien	  if (canonical_arr)
55598944Sobrien	    {
55698944Sobrien	      for (i = 0; i < nelts; i++)
55798944Sobrien		{
55898944Sobrien		  if (canonical_arr[i] == NULL)
55998944Sobrien		    {
560130803Smarcel		      symname = DEPRECATED_SYMBOL_NAME (sym_arr[i]);
56198944Sobrien		      canonical_arr[i] = savestring (symname, strlen (symname));
56298944Sobrien		    }
56398944Sobrien		}
56498944Sobrien	    }
56598944Sobrien	  memcpy (return_values.sals, values.sals,
56698944Sobrien		  (nelts * sizeof (struct symtab_and_line)));
56798944Sobrien	  return_values.nelts = nelts;
56898944Sobrien	  discard_cleanups (old_chain);
56998944Sobrien	  return return_values;
57098944Sobrien	}
57198944Sobrien
57298944Sobrien      if (num >= nelts + 2)
57398944Sobrien	{
57498944Sobrien	  printf_unfiltered ("No choice number %d.\n", num);
57598944Sobrien	}
57698944Sobrien      else
57798944Sobrien	{
57898944Sobrien	  num -= 2;
57998944Sobrien	  if (values.sals[num].pc)
58098944Sobrien	    {
58198944Sobrien	      if (canonical_arr)
58298944Sobrien		{
583130803Smarcel		  symname = DEPRECATED_SYMBOL_NAME (sym_arr[num]);
58498944Sobrien		  make_cleanup (xfree, symname);
58598944Sobrien		  canonical_arr[i] = savestring (symname, strlen (symname));
58698944Sobrien		}
58798944Sobrien	      return_values.sals[i++] = values.sals[num];
58898944Sobrien	      values.sals[num].pc = 0;
58998944Sobrien	    }
59098944Sobrien	  else
59198944Sobrien	    {
59298944Sobrien	      printf_unfiltered ("duplicate request for %d ignored.\n", num);
59398944Sobrien	    }
59498944Sobrien	}
59598944Sobrien
59698944Sobrien      args = arg1;
59798944Sobrien      while (*args == ' ' || *args == '\t')
59898944Sobrien	args++;
59998944Sobrien    }
60098944Sobrien  return_values.nelts = i;
60198944Sobrien  discard_cleanups (old_chain);
60298944Sobrien  return return_values;
60398944Sobrien}
60498944Sobrien
60598944Sobrien/* The parser of linespec itself. */
60698944Sobrien
60798944Sobrien/* Parse a string that specifies a line number.
60898944Sobrien   Pass the address of a char * variable; that variable will be
60998944Sobrien   advanced over the characters actually parsed.
61098944Sobrien
61198944Sobrien   The string can be:
61298944Sobrien
61398944Sobrien   LINENUM -- that line number in current file.  PC returned is 0.
61498944Sobrien   FILE:LINENUM -- that line in that file.  PC returned is 0.
61598944Sobrien   FUNCTION -- line number of openbrace of that function.
61698944Sobrien   PC returned is the start of the function.
61798944Sobrien   VARIABLE -- line number of definition of that variable.
61898944Sobrien   PC returned is 0.
61998944Sobrien   FILE:FUNCTION -- likewise, but prefer functions in that file.
62098944Sobrien   *EXPR -- line in which address EXPR appears.
62198944Sobrien
62298944Sobrien   This may all be followed by an "if EXPR", which we ignore.
62398944Sobrien
62498944Sobrien   FUNCTION may be an undebuggable function found in minimal symbol table.
62598944Sobrien
62698944Sobrien   If the argument FUNFIRSTLINE is nonzero, we want the first line
62798944Sobrien   of real code inside a function when a function is specified, and it is
62898944Sobrien   not OK to specify a variable or type to get its line number.
62998944Sobrien
63098944Sobrien   DEFAULT_SYMTAB specifies the file to use if none is specified.
63198944Sobrien   It defaults to current_source_symtab.
63298944Sobrien   DEFAULT_LINE specifies the line number to use for relative
63398944Sobrien   line numbers (that start with signs).  Defaults to current_source_line.
63498944Sobrien   If CANONICAL is non-NULL, store an array of strings containing the canonical
63598944Sobrien   line specs there if necessary. Currently overloaded member functions and
63698944Sobrien   line numbers or static functions without a filename yield a canonical
63798944Sobrien   line spec. The array and the line spec strings are allocated on the heap,
63898944Sobrien   it is the callers responsibility to free them.
63998944Sobrien
64098944Sobrien   Note that it is possible to return zero for the symtab
64198944Sobrien   if no file is validly specified.  Callers must check that.
642130803Smarcel   Also, the line number returned may be invalid.
643130803Smarcel
644130803Smarcel   If NOT_FOUND_PTR is not null, store a boolean true/false value at the location, based
645130803Smarcel   on whether or not failure occurs due to an unknown function or file.  In the case
646130803Smarcel   where failure does occur due to an unknown function or file, do not issue an error
647130803Smarcel   message.  */
64898944Sobrien
64998944Sobrien/* We allow single quotes in various places.  This is a hideous
65098944Sobrien   kludge, which exists because the completer can't yet deal with the
65198944Sobrien   lack of single quotes.  FIXME: write a linespec_completer which we
65298944Sobrien   can use as appropriate instead of make_symbol_completion_list.  */
65398944Sobrien
65498944Sobrienstruct symtabs_and_lines
65598944Sobriendecode_line_1 (char **argptr, int funfirstline, struct symtab *default_symtab,
656130803Smarcel	       int default_line, char ***canonical, int *not_found_ptr)
65798944Sobrien{
658130803Smarcel  char *p;
659130803Smarcel  char *q;
660130803Smarcel  /* If a file name is specified, this is its symtab.  */
661130803Smarcel  struct symtab *file_symtab = NULL;
66298944Sobrien
66398944Sobrien  char *copy;
664130803Smarcel  /* This is NULL if there are no parens in *ARGPTR, or a pointer to
665130803Smarcel     the closing parenthesis if there are parens.  */
666130803Smarcel  char *paren_pointer;
667130803Smarcel  /* This says whether or not something in *ARGPTR is quoted with
668130803Smarcel     completer_quotes (i.e. with single quotes).  */
66998944Sobrien  int is_quoted;
670130803Smarcel  /* Is part of *ARGPTR is enclosed in double quotes?  */
67198944Sobrien  int is_quote_enclosed;
672130803Smarcel  int is_objc_method = 0;
67398944Sobrien  char *saved_arg = *argptr;
67498944Sobrien
675130803Smarcel  if (not_found_ptr)
676130803Smarcel    *not_found_ptr = 0;
67798944Sobrien
67898944Sobrien  /* Defaults have defaults.  */
67998944Sobrien
680130803Smarcel  initialize_defaults (&default_symtab, &default_line);
681130803Smarcel
682130803Smarcel  /* See if arg is *PC.  */
683130803Smarcel
684130803Smarcel  if (**argptr == '*')
685130803Smarcel    return decode_indirect (argptr);
686130803Smarcel
687130803Smarcel  /* Set various flags.  'paren_pointer' is important for overload
688130803Smarcel     checking, where we allow things like:
689130803Smarcel        (gdb) break c::f(int)
690130803Smarcel  */
691130803Smarcel
692130803Smarcel  set_flags (*argptr, &is_quoted, &paren_pointer);
693130803Smarcel
694130803Smarcel  /* Check to see if it's a multipart linespec (with colons or
695130803Smarcel     periods).  */
696130803Smarcel
697130803Smarcel  /* Locate the end of the first half of the linespec.
698130803Smarcel     After the call, for instance, if the argptr string is "foo.c:123"
699130803Smarcel     p will point at "123".  If there is only one part, like "foo", p
700130803Smarcel     will point to "". If this is a C++ name, like "A::B::foo", p will
701130803Smarcel     point to "::B::foo". Argptr is not changed by this call.  */
702130803Smarcel
703130803Smarcel  p = locate_first_half (argptr, &is_quote_enclosed);
704130803Smarcel
705130803Smarcel  /* Check if this is an Objective-C method (anything that starts with
706130803Smarcel     a '+' or '-' and a '[').  */
707130803Smarcel  if (is_objc_method_format (p))
70898944Sobrien    {
709130803Smarcel      is_objc_method = 1;
710130803Smarcel      paren_pointer  = NULL; /* Just a category name.  Ignore it.  */
71198944Sobrien    }
71298944Sobrien
713130803Smarcel  /* Check if the symbol could be an Objective-C selector.  */
71498944Sobrien
715130803Smarcel  {
716130803Smarcel    struct symtabs_and_lines values;
717130803Smarcel    values = decode_objc (argptr, funfirstline, NULL,
718130803Smarcel			  canonical, saved_arg);
719130803Smarcel    if (values.sals != NULL)
720130803Smarcel      return values;
721130803Smarcel  }
722130803Smarcel
723130803Smarcel  /* Does it look like there actually were two parts?  */
724130803Smarcel
725130803Smarcel  if ((p[0] == ':' || p[0] == '.') && paren_pointer == NULL)
72698944Sobrien    {
727130803Smarcel      if (is_quoted)
728130803Smarcel	*argptr = *argptr + 1;
729130803Smarcel
730130803Smarcel      /* Is it a C++ or Java compound data structure?
731130803Smarcel	 The check on p[1] == ':' is capturing the case of "::",
732130803Smarcel	 since p[0]==':' was checked above.
733130803Smarcel	 Note that the call to decode_compound does everything
734130803Smarcel	 for us, including the lookup on the symbol table, so we
735130803Smarcel	 can return now. */
736130803Smarcel
737130803Smarcel      if (p[0] == '.' || p[1] == ':')
738130803Smarcel	return decode_compound (argptr, funfirstline, canonical,
739130803Smarcel				saved_arg, p);
74098944Sobrien
741130803Smarcel      /* No, the first part is a filename; set s to be that file's
742130803Smarcel	 symtab.  Also, move argptr past the filename.  */
74398944Sobrien
744130803Smarcel      file_symtab = symtab_from_filename (argptr, p, is_quote_enclosed,
745130803Smarcel		      			  not_found_ptr);
746130803Smarcel    }
747130803Smarcel#if 0
748130803Smarcel  /* No one really seems to know why this was added. It certainly
749130803Smarcel     breaks the command line, though, whenever the passed
750130803Smarcel     name is of the form ClassName::Method. This bit of code
751130803Smarcel     singles out the class name, and if funfirstline is set (for
752130803Smarcel     example, you are setting a breakpoint at this function),
753130803Smarcel     you get an error. This did not occur with earlier
754130803Smarcel     verions, so I am ifdef'ing this out. 3/29/99 */
755130803Smarcel  else
756130803Smarcel    {
757130803Smarcel      /* Check if what we have till now is a symbol name */
75898944Sobrien
759130803Smarcel      /* We may be looking at a template instantiation such
760130803Smarcel         as "foo<int>".  Check here whether we know about it,
761130803Smarcel         instead of falling through to the code below which
762130803Smarcel         handles ordinary function names, because that code
763130803Smarcel         doesn't like seeing '<' and '>' in a name -- the
764130803Smarcel         skip_quoted call doesn't go past them.  So see if we
765130803Smarcel         can figure it out right now. */
766130803Smarcel
767130803Smarcel      copy = (char *) alloca (p - *argptr + 1);
768130803Smarcel      memcpy (copy, *argptr, p - *argptr);
769130803Smarcel      copy[p - *argptr] = '\000';
770130803Smarcel      sym = lookup_symbol (copy, 0, VAR_DOMAIN, 0, &sym_symtab);
771130803Smarcel      if (sym)
772130803Smarcel	{
773130803Smarcel	  *argptr = (*p == '\'') ? p + 1 : p;
774130803Smarcel	  return symbol_found (funfirstline, canonical, copy, sym,
775130803Smarcel			       NULL, sym_symtab);
776130803Smarcel	}
777130803Smarcel      /* Otherwise fall out from here and go to file/line spec
778130803Smarcel         processing, etc. */
77998944Sobrien    }
780130803Smarcel#endif
78198944Sobrien
782130803Smarcel  /* S is specified file's symtab, or 0 if no file specified.
783130803Smarcel     arg no longer contains the file name.  */
784130803Smarcel
785130803Smarcel  /* Check whether arg is all digits (and sign).  */
786130803Smarcel
787130803Smarcel  q = *argptr;
788130803Smarcel  if (*q == '-' || *q == '+')
789130803Smarcel    q++;
790130803Smarcel  while (*q >= '0' && *q <= '9')
791130803Smarcel    q++;
792130803Smarcel
793130803Smarcel  if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ','))
794130803Smarcel    /* We found a token consisting of all digits -- at least one digit.  */
795130803Smarcel    return decode_all_digits (argptr, default_symtab, default_line,
796130803Smarcel			      canonical, file_symtab, q);
797130803Smarcel
798130803Smarcel  /* Arg token is not digits => try it as a variable name
799130803Smarcel     Find the next token (everything up to end or next whitespace).  */
800130803Smarcel
801130803Smarcel  if (**argptr == '$')		/* May be a convenience variable.  */
802130803Smarcel    /* One or two $ chars possible.  */
803130803Smarcel    p = skip_quoted (*argptr + (((*argptr)[1] == '$') ? 2 : 1));
804130803Smarcel  else if (is_quoted)
805130803Smarcel    {
806130803Smarcel      p = skip_quoted (*argptr);
807130803Smarcel      if (p[-1] != '\'')
808130803Smarcel	error ("Unmatched single quote.");
809130803Smarcel    }
810130803Smarcel  else if (is_objc_method)
811130803Smarcel    {
812130803Smarcel      /* allow word separators in method names for Obj-C */
813130803Smarcel      p = skip_quoted_chars (*argptr, NULL, "");
814130803Smarcel    }
815130803Smarcel  else if (paren_pointer != NULL)
816130803Smarcel    {
817130803Smarcel      p = paren_pointer + 1;
818130803Smarcel    }
819130803Smarcel  else
820130803Smarcel    {
821130803Smarcel      p = skip_quoted (*argptr);
822130803Smarcel    }
823130803Smarcel
824130803Smarcel  copy = (char *) alloca (p - *argptr + 1);
825130803Smarcel  memcpy (copy, *argptr, p - *argptr);
826130803Smarcel  copy[p - *argptr] = '\0';
827130803Smarcel  if (p != *argptr
828130803Smarcel      && copy[0]
829130803Smarcel      && copy[0] == copy[p - *argptr - 1]
830130803Smarcel      && strchr (get_gdb_completer_quote_characters (), copy[0]) != NULL)
831130803Smarcel    {
832130803Smarcel      copy[p - *argptr - 1] = '\0';
833130803Smarcel      copy++;
834130803Smarcel    }
835130803Smarcel  while (*p == ' ' || *p == '\t')
836130803Smarcel    p++;
837130803Smarcel  *argptr = p;
838130803Smarcel
839130803Smarcel  /* If it starts with $: may be a legitimate variable or routine name
840130803Smarcel     (e.g. HP-UX millicode routines such as $$dyncall), or it may
841130803Smarcel     be history value, or it may be a convenience variable.  */
842130803Smarcel
843130803Smarcel  if (*copy == '$')
844130803Smarcel    return decode_dollar (copy, funfirstline, default_symtab,
845130803Smarcel			  canonical, file_symtab);
846130803Smarcel
847130803Smarcel  /* Look up that token as a variable.
848130803Smarcel     If file specified, use that file's per-file block to start with.  */
849130803Smarcel
850130803Smarcel  return decode_variable (copy, funfirstline, canonical,
851130803Smarcel			  file_symtab, not_found_ptr);
852130803Smarcel}
853130803Smarcel
854130803Smarcel
855130803Smarcel
856130803Smarcel/* Now, more helper functions for decode_line_1.  Some conventions
857130803Smarcel   that these functions follow:
858130803Smarcel
859130803Smarcel   Decode_line_1 typically passes along some of its arguments or local
860130803Smarcel   variables to the subfunctions.  It passes the variables by
861130803Smarcel   reference if they are modified by the subfunction, and by value
862130803Smarcel   otherwise.
863130803Smarcel
864130803Smarcel   Some of the functions have side effects that don't arise from
865130803Smarcel   variables that are passed by reference.  In particular, if a
866130803Smarcel   function is passed ARGPTR as an argument, it modifies what ARGPTR
867130803Smarcel   points to; typically, it advances *ARGPTR past whatever substring
868130803Smarcel   it has just looked at.  (If it doesn't modify *ARGPTR, then the
869130803Smarcel   function gets passed *ARGPTR instead, which is then called ARG: see
870130803Smarcel   set_flags, for example.)  Also, functions that return a struct
871130803Smarcel   symtabs_and_lines may modify CANONICAL, as in the description of
872130803Smarcel   decode_line_1.
873130803Smarcel
874130803Smarcel   If a function returns a struct symtabs_and_lines, then that struct
875130803Smarcel   will immediately make its way up the call chain to be returned by
876130803Smarcel   decode_line_1.  In particular, all of the functions decode_XXX
877130803Smarcel   calculate the appropriate struct symtabs_and_lines, under the
878130803Smarcel   assumption that their argument is of the form XXX.  */
879130803Smarcel
880130803Smarcel/* First, some functions to initialize stuff at the beggining of the
881130803Smarcel   function.  */
882130803Smarcel
883130803Smarcelstatic void
884130803Smarcelinitialize_defaults (struct symtab **default_symtab, int *default_line)
885130803Smarcel{
886130803Smarcel  if (*default_symtab == 0)
887130803Smarcel    {
888130803Smarcel      /* Use whatever we have for the default source line.  We don't use
889130803Smarcel         get_current_or_default_symtab_and_line as it can recurse and call
890130803Smarcel	 us back! */
891130803Smarcel      struct symtab_and_line cursal =
892130803Smarcel	get_current_source_symtab_and_line ();
893130803Smarcel
894130803Smarcel      *default_symtab = cursal.symtab;
895130803Smarcel      *default_line = cursal.line;
896130803Smarcel    }
897130803Smarcel}
898130803Smarcel
899130803Smarcelstatic void
900130803Smarcelset_flags (char *arg, int *is_quoted, char **paren_pointer)
901130803Smarcel{
902130803Smarcel  char *ii;
903130803Smarcel  int has_if = 0;
904130803Smarcel
90598944Sobrien  /* 'has_if' is for the syntax:
906130803Smarcel        (gdb) break foo if (a==b)
907130803Smarcel  */
908130803Smarcel  if ((ii = strstr (arg, " if ")) != NULL ||
909130803Smarcel      (ii = strstr (arg, "\tif ")) != NULL ||
910130803Smarcel      (ii = strstr (arg, " if\t")) != NULL ||
911130803Smarcel      (ii = strstr (arg, "\tif\t")) != NULL ||
912130803Smarcel      (ii = strstr (arg, " if(")) != NULL ||
913130803Smarcel      (ii = strstr (arg, "\tif( ")) != NULL)
91498944Sobrien    has_if = 1;
915130803Smarcel  /* Temporarily zap out "if (condition)" to not confuse the
916130803Smarcel     parenthesis-checking code below.  This is undone below. Do not
917130803Smarcel     change ii!!  */
91898944Sobrien  if (has_if)
91998944Sobrien    {
92098944Sobrien      *ii = '\0';
92198944Sobrien    }
92298944Sobrien
923130803Smarcel  *is_quoted = (*arg
924130803Smarcel		&& strchr (get_gdb_completer_quote_characters (),
925130803Smarcel			   *arg) != NULL);
92698944Sobrien
927130803Smarcel  *paren_pointer = strchr (arg, '(');
928130803Smarcel  if (*paren_pointer != NULL)
929130803Smarcel    *paren_pointer = strrchr (*paren_pointer, ')');
93098944Sobrien
931130803Smarcel  /* Now that we're safely past the paren_pointer check, put back " if
932130803Smarcel     (condition)" so outer layers can see it.  */
93398944Sobrien  if (has_if)
93498944Sobrien    *ii = ' ';
935130803Smarcel}
93698944Sobrien
937130803Smarcel
938130803Smarcel
939130803Smarcel/* Decode arg of the form *PC.  */
940130803Smarcel
941130803Smarcelstatic struct symtabs_and_lines
942130803Smarceldecode_indirect (char **argptr)
943130803Smarcel{
944130803Smarcel  struct symtabs_and_lines values;
945130803Smarcel  CORE_ADDR pc;
946130803Smarcel
947130803Smarcel  (*argptr)++;
948130803Smarcel  pc = parse_and_eval_address_1 (argptr);
949130803Smarcel
950130803Smarcel  values.sals = (struct symtab_and_line *)
951130803Smarcel    xmalloc (sizeof (struct symtab_and_line));
952130803Smarcel
953130803Smarcel  values.nelts = 1;
954130803Smarcel  values.sals[0] = find_pc_line (pc, 0);
955130803Smarcel  values.sals[0].pc = pc;
956130803Smarcel  values.sals[0].section = find_pc_overlay (pc);
957130803Smarcel
958130803Smarcel  return values;
959130803Smarcel}
960130803Smarcel
961130803Smarcel
962130803Smarcel
963130803Smarcel/* Locate the first half of the linespec, ending in a colon, period,
964130803Smarcel   or whitespace.  (More or less.)  Also, check to see if *ARGPTR is
965130803Smarcel   enclosed in double quotes; if so, set is_quote_enclosed, advance
966130803Smarcel   ARGPTR past that and zero out the trailing double quote.
967130803Smarcel   If ARGPTR is just a simple name like "main", p will point to ""
968130803Smarcel   at the end.  */
969130803Smarcel
970130803Smarcelstatic char *
971130803Smarcellocate_first_half (char **argptr, int *is_quote_enclosed)
972130803Smarcel{
973130803Smarcel  char *ii;
974130803Smarcel  char *p, *p1;
975130803Smarcel  int has_comma;
976130803Smarcel
97798944Sobrien  /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
97898944Sobrien     and we must isolate the first half.  Outer layers will call again later
97998944Sobrien     for the second half.
98098944Sobrien
98198944Sobrien     Don't count commas that appear in argument lists of overloaded
98298944Sobrien     functions, or in quoted strings.  It's stupid to go to this much
98398944Sobrien     trouble when the rest of the function is such an obvious roach hotel.  */
98498944Sobrien  ii = find_toplevel_char (*argptr, ',');
98598944Sobrien  has_comma = (ii != 0);
98698944Sobrien
987130803Smarcel  /* Temporarily zap out second half to not confuse the code below.
988130803Smarcel     This is undone below. Do not change ii!!  */
98998944Sobrien  if (has_comma)
99098944Sobrien    {
99198944Sobrien      *ii = '\0';
99298944Sobrien    }
99398944Sobrien
994130803Smarcel  /* Maybe arg is FILE : LINENUM or FILE : FUNCTION.  May also be
995130803Smarcel     CLASS::MEMBER, or NAMESPACE::NAME.  Look for ':', but ignore
996130803Smarcel     inside of <>.  */
99798944Sobrien
99898944Sobrien  p = *argptr;
99998944Sobrien  if (p[0] == '"')
100098944Sobrien    {
1001130803Smarcel      *is_quote_enclosed = 1;
100298944Sobrien      (*argptr)++;
100398944Sobrien      p++;
100498944Sobrien    }
100598944Sobrien  else
1006130803Smarcel    *is_quote_enclosed = 0;
100798944Sobrien  for (; *p; p++)
100898944Sobrien    {
100998944Sobrien      if (p[0] == '<')
101098944Sobrien	{
101198944Sobrien	  char *temp_end = find_template_name_end (p);
101298944Sobrien	  if (!temp_end)
101398944Sobrien	    error ("malformed template specification in command");
101498944Sobrien	  p = temp_end;
101598944Sobrien	}
1016130803Smarcel      /* Check for a colon and a plus or minus and a [ (which
1017130803Smarcel         indicates an Objective-C method) */
1018130803Smarcel      if (is_objc_method_format (p))
1019130803Smarcel	{
1020130803Smarcel	  break;
1021130803Smarcel	}
1022130803Smarcel      /* Check for the end of the first half of the linespec.  End of
1023130803Smarcel         line, a tab, a double colon or the last single colon, or a
1024130803Smarcel         space.  But if enclosed in double quotes we do not break on
1025130803Smarcel         enclosed spaces.  */
102698944Sobrien      if (!*p
102798944Sobrien	  || p[0] == '\t'
102898944Sobrien	  || ((p[0] == ':')
102998944Sobrien	      && ((p[1] == ':') || (strchr (p + 1, ':') == NULL)))
1030130803Smarcel	  || ((p[0] == ' ') && !*is_quote_enclosed))
103198944Sobrien	break;
1032130803Smarcel      if (p[0] == '.' && strchr (p, ':') == NULL)
103398944Sobrien	{
1034130803Smarcel	  /* Java qualified method.  Find the *last* '.', since the
1035130803Smarcel	     others are package qualifiers.  */
103698944Sobrien	  for (p1 = p; *p1; p1++)
103798944Sobrien	    {
103898944Sobrien	      if (*p1 == '.')
103998944Sobrien		p = p1;
104098944Sobrien	    }
104198944Sobrien	  break;
104298944Sobrien	}
104398944Sobrien    }
104498944Sobrien  while (p[0] == ' ' || p[0] == '\t')
104598944Sobrien    p++;
104698944Sobrien
1047130803Smarcel  /* If the closing double quote was left at the end, remove it.  */
1048130803Smarcel  if (*is_quote_enclosed)
104998944Sobrien    {
105098944Sobrien      char *closing_quote = strchr (p - 1, '"');
105198944Sobrien      if (closing_quote && closing_quote[1] == '\0')
105298944Sobrien	*closing_quote = '\0';
105398944Sobrien    }
105498944Sobrien
1055130803Smarcel  /* Now that we've safely parsed the first half, put back ',' so
1056130803Smarcel     outer layers can see it.  */
105798944Sobrien  if (has_comma)
105898944Sobrien    *ii = ',';
105998944Sobrien
1060130803Smarcel  return p;
1061130803Smarcel}
106298944Sobrien
1063130803Smarcel
106498944Sobrien
1065130803Smarcel/* Here's where we recognise an Objective-C Selector.  An Objective C
1066130803Smarcel   selector may be implemented by more than one class, therefore it
1067130803Smarcel   may represent more than one method/function.  This gives us a
1068130803Smarcel   situation somewhat analogous to C++ overloading.  If there's more
1069130803Smarcel   than one method that could represent the selector, then use some of
1070130803Smarcel   the existing C++ code to let the user choose one.  */
107198944Sobrien
1072130803Smarcelstruct symtabs_and_lines
1073130803Smarceldecode_objc (char **argptr, int funfirstline, struct symtab *file_symtab,
1074130803Smarcel	     char ***canonical, char *saved_arg)
1075130803Smarcel{
1076130803Smarcel  struct symtabs_and_lines values;
1077130803Smarcel  struct symbol **sym_arr = NULL;
1078130803Smarcel  struct symbol *sym = NULL;
1079130803Smarcel  char *copy = NULL;
1080130803Smarcel  struct block *block = NULL;
1081130803Smarcel  int i1 = 0;
1082130803Smarcel  int i2 = 0;
108398944Sobrien
1084130803Smarcel  values.sals = NULL;
1085130803Smarcel  values.nelts = 0;
1086130803Smarcel
1087130803Smarcel  if (file_symtab != NULL)
1088130803Smarcel    block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (file_symtab), STATIC_BLOCK);
1089130803Smarcel  else
1090130803Smarcel    block = get_selected_block (0);
1091130803Smarcel
1092130803Smarcel  copy = find_imps (file_symtab, block, *argptr, NULL, &i1, &i2);
1093130803Smarcel
1094130803Smarcel  if (i1 > 0)
1095130803Smarcel    {
1096130803Smarcel      sym_arr = (struct symbol **) alloca ((i1 + 1) * sizeof (struct symbol *));
1097130803Smarcel      sym_arr[i1] = 0;
1098130803Smarcel
1099130803Smarcel      copy = find_imps (file_symtab, block, *argptr, sym_arr, &i1, &i2);
1100130803Smarcel      *argptr = copy;
1101130803Smarcel    }
1102130803Smarcel
1103130803Smarcel  /* i1 now represents the TOTAL number of matches found.
1104130803Smarcel     i2 represents how many HIGH-LEVEL (struct symbol) matches,
1105130803Smarcel     which will come first in the sym_arr array.  Any low-level
1106130803Smarcel     (minimal_symbol) matches will follow those.  */
1107130803Smarcel
1108130803Smarcel  if (i1 == 1)
1109130803Smarcel    {
1110130803Smarcel      if (i2 > 0)
1111130803Smarcel	{
1112130803Smarcel	  /* Already a struct symbol.  */
1113130803Smarcel	  sym = sym_arr[0];
1114130803Smarcel	}
1115130803Smarcel      else
1116130803Smarcel	{
1117130803Smarcel	  sym = find_pc_function (SYMBOL_VALUE_ADDRESS (sym_arr[0]));
1118130803Smarcel	  if ((sym != NULL) && strcmp (SYMBOL_LINKAGE_NAME (sym_arr[0]), SYMBOL_LINKAGE_NAME (sym)) != 0)
111998944Sobrien	    {
1120130803Smarcel	      warning ("debugging symbol \"%s\" does not match selector; ignoring", SYMBOL_LINKAGE_NAME (sym));
1121130803Smarcel	      sym = NULL;
1122130803Smarcel	    }
1123130803Smarcel	}
1124130803Smarcel
1125130803Smarcel      values.sals = (struct symtab_and_line *) xmalloc (sizeof (struct symtab_and_line));
1126130803Smarcel      values.nelts = 1;
1127130803Smarcel
1128130803Smarcel      if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1129130803Smarcel	{
1130130803Smarcel	  /* Canonicalize this, so it remains resolved for dylib loads.  */
1131130803Smarcel	  values.sals[0] = find_function_start_sal (sym, funfirstline);
1132130803Smarcel	  build_canonical_line_spec (values.sals, SYMBOL_NATURAL_NAME (sym), canonical);
1133130803Smarcel	}
1134130803Smarcel      else
1135130803Smarcel	{
1136130803Smarcel	  /* The only match was a non-debuggable symbol.  */
1137130803Smarcel	  values.sals[0].symtab = 0;
1138130803Smarcel	  values.sals[0].line = 0;
1139130803Smarcel	  values.sals[0].end = 0;
1140130803Smarcel	  values.sals[0].pc = SYMBOL_VALUE_ADDRESS (sym_arr[0]);
1141130803Smarcel	}
1142130803Smarcel      return values;
1143130803Smarcel    }
114498944Sobrien
1145130803Smarcel  if (i1 > 1)
1146130803Smarcel    {
1147130803Smarcel      /* More than one match. The user must choose one or more.  */
1148130803Smarcel      return decode_line_2 (sym_arr, i2, funfirstline, canonical);
1149130803Smarcel    }
115098944Sobrien
1151130803Smarcel  return values;
1152130803Smarcel}
115398944Sobrien
1154130803Smarcel/* This handles C++ and Java compound data structures.  P should point
1155130803Smarcel   at the first component separator, i.e. double-colon or period.  As
1156130803Smarcel   an example, on entrance to this function we could have ARGPTR
1157130803Smarcel   pointing to "AAA::inA::fun" and P pointing to "::inA::fun".  */
115898944Sobrien
1159130803Smarcelstatic struct symtabs_and_lines
1160130803Smarceldecode_compound (char **argptr, int funfirstline, char ***canonical,
1161130803Smarcel		 char *saved_arg, char *p)
1162130803Smarcel{
1163130803Smarcel  struct symtabs_and_lines values;
1164130803Smarcel  char *p2;
1165130803Smarcel  char *saved_arg2 = *argptr;
1166130803Smarcel  char *temp_end;
1167130803Smarcel  struct symbol *sym;
1168130803Smarcel  /* The symtab that SYM was found in.  */
1169130803Smarcel  struct symtab *sym_symtab;
1170130803Smarcel  char *copy;
1171130803Smarcel  struct symbol *sym_class;
1172130803Smarcel  struct symbol **sym_arr;
1173130803Smarcel  struct type *t;
117498944Sobrien
1175130803Smarcel  /* First check for "global" namespace specification, of the form
1176130803Smarcel     "::foo".  If found, skip over the colons and jump to normal
1177130803Smarcel     symbol processing.  I.e. the whole line specification starts with
1178130803Smarcel     "::" (note the condition that *argptr == p). */
1179130803Smarcel  if (p[0] == ':'
1180130803Smarcel      && ((*argptr == p) || (p[-1] == ' ') || (p[-1] == '\t')))
1181130803Smarcel    saved_arg2 += 2;
118298944Sobrien
1183130803Smarcel  /* Given our example "AAA::inA::fun", we have two cases to consider:
118498944Sobrien
1185130803Smarcel     1) AAA::inA is the name of a class.  In that case, presumably it
1186130803Smarcel        has a method called "fun"; we then look up that method using
1187130803Smarcel        find_method.
118898944Sobrien
1189130803Smarcel     2) AAA::inA isn't the name of a class.  In that case, either the
1190130803Smarcel        user made a typo or AAA::inA is the name of a namespace.
1191130803Smarcel        Either way, we just look up AAA::inA::fun with lookup_symbol.
119298944Sobrien
1193130803Smarcel     Thus, our first task is to find everything before the last set of
1194130803Smarcel     double-colons and figure out if it's the name of a class.  So we
1195130803Smarcel     first loop through all of the double-colons.  */
119698944Sobrien
1197130803Smarcel  p2 = p;		/* Save for restart.  */
119898944Sobrien
1199130803Smarcel  /* This is very messy. Following the example above we have now the
1200130803Smarcel     following pointers:
1201130803Smarcel     p -> "::inA::fun"
1202130803Smarcel     argptr -> "AAA::inA::fun
1203130803Smarcel     saved_arg -> "AAA::inA::fun
1204130803Smarcel     saved_arg2 -> "AAA::inA::fun
1205130803Smarcel     p2 -> "::inA::fun". */
120698944Sobrien
1207130803Smarcel  /* In the loop below, with these strings, we'll make 2 passes, each
1208130803Smarcel     is marked in comments.*/
120998944Sobrien
1210130803Smarcel  while (1)
1211130803Smarcel    {
1212130803Smarcel      /* Move pointer up to next possible class/namespace token.  */
121398944Sobrien
1214130803Smarcel      p = p2 + 1;	/* Restart with old value +1.  */
121598944Sobrien
1216130803Smarcel      /* PASS1: at this point p2->"::inA::fun", so p->":inA::fun",
1217130803Smarcel	 i.e. if there is a double-colon, p will now point to the
1218130803Smarcel	 second colon. */
1219130803Smarcel      /* PASS2: p2->"::fun", p->":fun" */
1220130803Smarcel
1221130803Smarcel      /* Move pointer ahead to next double-colon.  */
1222130803Smarcel      while (*p && (p[0] != ' ') && (p[0] != '\t') && (p[0] != '\''))
1223130803Smarcel	{
1224130803Smarcel	  if (p[0] == '<')
1225130803Smarcel	    {
1226130803Smarcel	      temp_end = find_template_name_end (p);
1227130803Smarcel	      if (!temp_end)
1228130803Smarcel		error ("malformed template specification in command");
1229130803Smarcel	      p = temp_end;
1230130803Smarcel	    }
1231130803Smarcel	  /* Note that, since, at the start of this loop, p would be
1232130803Smarcel	     pointing to the second colon in a double-colon, we only
1233130803Smarcel	     satisfy the condition below if there is another
1234130803Smarcel	     double-colon to the right (after). I.e. there is another
1235130803Smarcel	     component that can be a class or a namespace. I.e, if at
1236130803Smarcel	     the beginning of this loop (PASS1), we had
1237130803Smarcel	     p->":inA::fun", we'll trigger this when p has been
1238130803Smarcel	     advanced to point to "::fun".  */
1239130803Smarcel	  /* PASS2: we will not trigger this. */
1240130803Smarcel	  else if ((p[0] == ':') && (p[1] == ':'))
1241130803Smarcel	    break;	/* Found double-colon.  */
1242130803Smarcel	  else
1243130803Smarcel	    /* PASS2: We'll keep getting here, until p->"", at which point
1244130803Smarcel	       we exit this loop.  */
1245130803Smarcel	    p++;
124698944Sobrien	}
124798944Sobrien
1248130803Smarcel      if (*p != ':')
1249130803Smarcel	break;		/* Out of the while (1).  This would happen
1250130803Smarcel			   for instance if we have looked up
1251130803Smarcel			   unsuccessfully all the components of the
1252130803Smarcel			   string, and p->""(PASS2)  */
125398944Sobrien
1254130803Smarcel      /* We get here if p points to ' ', '\t', '\'', "::" or ""(i.e
1255130803Smarcel	 string ended). */
1256130803Smarcel      /* Save restart for next time around.  */
1257130803Smarcel      p2 = p;
1258130803Smarcel      /* Restore argptr as it was on entry to this function.  */
1259130803Smarcel      *argptr = saved_arg2;
1260130803Smarcel      /* PASS1: at this point p->"::fun" argptr->"AAA::inA::fun",
1261130803Smarcel	 p2->"::fun".  */
1262130803Smarcel
1263130803Smarcel      /* All ready for next pass through the loop.  */
1264130803Smarcel    }			/* while (1) */
1265130803Smarcel
1266130803Smarcel
1267130803Smarcel  /* Start of lookup in the symbol tables. */
1268130803Smarcel
1269130803Smarcel  /* Lookup in the symbol table the substring between argptr and
1270130803Smarcel     p. Note, this call changes the value of argptr.  */
1271130803Smarcel  /* Before the call, argptr->"AAA::inA::fun",
1272130803Smarcel     p->"", p2->"::fun".  After the call: argptr->"fun", p, p2
1273130803Smarcel     unchanged.  */
1274130803Smarcel  sym_class = lookup_prefix_sym (argptr, p2);
1275130803Smarcel
1276130803Smarcel  /* If sym_class has been found, and if "AAA::inA" is a class, then
1277130803Smarcel     we're in case 1 above.  So we look up "fun" as a method of that
1278130803Smarcel     class.  */
1279130803Smarcel  if (sym_class &&
1280130803Smarcel      (t = check_typedef (SYMBOL_TYPE (sym_class)),
1281130803Smarcel       (TYPE_CODE (t) == TYPE_CODE_STRUCT
1282130803Smarcel	|| TYPE_CODE (t) == TYPE_CODE_UNION)))
1283130803Smarcel    {
1284130803Smarcel      /* Arg token is not digits => try it as a function name.
1285130803Smarcel	 Find the next token (everything up to end or next
1286130803Smarcel	 blank).  */
1287130803Smarcel      if (**argptr
1288130803Smarcel	  && strchr (get_gdb_completer_quote_characters (),
1289130803Smarcel		     **argptr) != NULL)
129098944Sobrien	{
1291130803Smarcel	  p = skip_quoted (*argptr);
1292130803Smarcel	  *argptr = *argptr + 1;
129398944Sobrien	}
129498944Sobrien      else
129598944Sobrien	{
1296130803Smarcel	  /* At this point argptr->"fun".  */
1297130803Smarcel	  p = *argptr;
1298130803Smarcel	  while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p != ':')
1299130803Smarcel	    p++;
1300130803Smarcel	  /* At this point p->"".  String ended.  */
130198944Sobrien	}
130298944Sobrien
1303130803Smarcel      /* Allocate our own copy of the substring between argptr and
1304130803Smarcel	 p. */
1305130803Smarcel      copy = (char *) alloca (p - *argptr + 1);
1306130803Smarcel      memcpy (copy, *argptr, p - *argptr);
1307130803Smarcel      copy[p - *argptr] = '\0';
1308130803Smarcel      if (p != *argptr
1309130803Smarcel	  && copy[p - *argptr - 1]
1310130803Smarcel	  && strchr (get_gdb_completer_quote_characters (),
1311130803Smarcel		     copy[p - *argptr - 1]) != NULL)
1312130803Smarcel	copy[p - *argptr - 1] = '\0';
131398944Sobrien
1314130803Smarcel      /* At this point copy->"fun", p->"" */
1315130803Smarcel
1316130803Smarcel      /* No line number may be specified.  */
131798944Sobrien      while (*p == ' ' || *p == '\t')
131898944Sobrien	p++;
131998944Sobrien      *argptr = p;
1320130803Smarcel      /* At this point arptr->"".  */
132198944Sobrien
1322130803Smarcel      /* Look for copy as a method of sym_class. */
1323130803Smarcel      /* At this point copy->"fun", sym_class is "AAA:inA",
1324130803Smarcel	 saved_arg->"AAA::inA::fun".  This concludes the scanning of
1325130803Smarcel	 the string for possible components matches.  If we find it
1326130803Smarcel	 here, we return. If not, and we are at the and of the string,
1327130803Smarcel	 we'll lookup the whole string in the symbol tables.  */
132898944Sobrien
1329130803Smarcel      return find_method (funfirstline, canonical, saved_arg,
1330130803Smarcel			  copy, t, sym_class);
133198944Sobrien
1332130803Smarcel    } /* End if symbol found */
133398944Sobrien
133498944Sobrien
1335130803Smarcel  /* We couldn't find a class, so we're in case 2 above.  We check the
1336130803Smarcel     entire name as a symbol instead.  */
133798944Sobrien
1338130803Smarcel  copy = (char *) alloca (p - saved_arg2 + 1);
1339130803Smarcel  memcpy (copy, saved_arg2, p - saved_arg2);
1340130803Smarcel  /* Note: if is_quoted should be true, we snuff out quote here
1341130803Smarcel     anyway.  */
1342130803Smarcel  copy[p - saved_arg2] = '\000';
1343130803Smarcel  /* Set argptr to skip over the name.  */
1344130803Smarcel  *argptr = (*p == '\'') ? p + 1 : p;
134598944Sobrien
1346130803Smarcel  /* Look up entire name */
1347130803Smarcel  sym = lookup_symbol (copy, 0, VAR_DOMAIN, 0, &sym_symtab);
1348130803Smarcel  if (sym)
1349130803Smarcel    return symbol_found (funfirstline, canonical, copy, sym,
1350130803Smarcel			 NULL, sym_symtab);
135198944Sobrien
1352130803Smarcel  /* Couldn't find any interpretation as classes/namespaces, so give
1353130803Smarcel     up.  The quotes are important if copy is empty.  */
1354130803Smarcel  cplusplus_error (saved_arg,
1355130803Smarcel		   "Can't find member of namespace, class, struct, or union named \"%s\"\n",
1356130803Smarcel		   copy);
1357130803Smarcel}
135898944Sobrien
1359130803Smarcel/* Next come some helper functions for decode_compound.  */
136098944Sobrien
1361130803Smarcel/* Return the symbol corresponding to the substring of *ARGPTR ending
1362130803Smarcel   at P, allowing whitespace.  Also, advance *ARGPTR past the symbol
1363130803Smarcel   name in question, the compound object separator ("::" or "."), and
1364130803Smarcel   whitespace.  Note that *ARGPTR is changed whether or not the
1365130803Smarcel   lookup_symbol call finds anything (i.e we return NULL).  As an
1366130803Smarcel   example, say ARGPTR is "AAA::inA::fun" and P is "::inA::fun".  */
136798944Sobrien
1368130803Smarcelstatic struct symbol *
1369130803Smarcellookup_prefix_sym (char **argptr, char *p)
1370130803Smarcel{
1371130803Smarcel  char *p1;
1372130803Smarcel  char *copy;
137398944Sobrien
1374130803Smarcel  /* Extract the class name.  */
1375130803Smarcel  p1 = p;
1376130803Smarcel  while (p != *argptr && p[-1] == ' ')
1377130803Smarcel    --p;
1378130803Smarcel  copy = (char *) alloca (p - *argptr + 1);
1379130803Smarcel  memcpy (copy, *argptr, p - *argptr);
1380130803Smarcel  copy[p - *argptr] = 0;
138198944Sobrien
1382130803Smarcel  /* Discard the class name from the argptr.  */
1383130803Smarcel  p = p1 + (p1[0] == ':' ? 2 : 1);
1384130803Smarcel  while (*p == ' ' || *p == '\t')
1385130803Smarcel    p++;
1386130803Smarcel  *argptr = p;
138798944Sobrien
1388130803Smarcel  /* At this point p1->"::inA::fun", p->"inA::fun" copy->"AAA",
1389130803Smarcel     argptr->"inA::fun" */
139098944Sobrien
1391130803Smarcel  return lookup_symbol (copy, 0, STRUCT_DOMAIN, 0,
1392130803Smarcel			(struct symtab **) NULL);
1393130803Smarcel}
1394130803Smarcel
1395130803Smarcel/* This finds the method COPY in the class whose type is T and whose
1396130803Smarcel   symbol is SYM_CLASS.  */
1397130803Smarcel
1398130803Smarcelstatic struct symtabs_and_lines
1399130803Smarcelfind_method (int funfirstline, char ***canonical, char *saved_arg,
1400130803Smarcel	     char *copy, struct type *t, struct symbol *sym_class)
1401130803Smarcel{
1402130803Smarcel  struct symtabs_and_lines values;
1403130803Smarcel  struct symbol *sym = 0;
1404130803Smarcel  int i1;	/*  Counter for the symbol array.  */
1405130803Smarcel  struct symbol **sym_arr =  alloca (total_number_of_methods (t)
1406130803Smarcel				     * sizeof (struct symbol *));
1407130803Smarcel
1408130803Smarcel  /* Find all methods with a matching name, and put them in
1409130803Smarcel     sym_arr.  */
1410130803Smarcel
1411130803Smarcel  i1 = collect_methods (copy, t, sym_arr);
1412130803Smarcel
1413130803Smarcel  if (i1 == 1)
141498944Sobrien    {
1415130803Smarcel      /* There is exactly one field with that name.  */
1416130803Smarcel      sym = sym_arr[0];
1417130803Smarcel
1418130803Smarcel      if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1419130803Smarcel	{
1420130803Smarcel	  values.sals = (struct symtab_and_line *)
1421130803Smarcel	    xmalloc (sizeof (struct symtab_and_line));
1422130803Smarcel	  values.nelts = 1;
1423130803Smarcel	  values.sals[0] = find_function_start_sal (sym,
1424130803Smarcel						    funfirstline);
1425130803Smarcel	}
1426130803Smarcel      else
1427130803Smarcel	{
1428130803Smarcel	  values.nelts = 0;
1429130803Smarcel	}
1430130803Smarcel      return values;
143198944Sobrien    }
1432130803Smarcel  if (i1 > 0)
143398944Sobrien    {
1434130803Smarcel      /* There is more than one field with that name
1435130803Smarcel	 (overloaded).  Ask the user which one to use.  */
1436130803Smarcel      return decode_line_2 (sym_arr, i1, funfirstline, canonical);
143798944Sobrien    }
143898944Sobrien  else
143998944Sobrien    {
1440130803Smarcel      char *tmp;
1441130803Smarcel
1442130803Smarcel      if (is_operator_name (copy))
1443130803Smarcel	{
1444130803Smarcel	  tmp = (char *) alloca (strlen (copy + 3) + 9);
1445130803Smarcel	  strcpy (tmp, "operator ");
1446130803Smarcel	  strcat (tmp, copy + 3);
1447130803Smarcel	}
1448130803Smarcel      else
1449130803Smarcel	tmp = copy;
1450130803Smarcel      if (tmp[0] == '~')
1451130803Smarcel	cplusplus_error (saved_arg,
1452130803Smarcel			 "the class `%s' does not have destructor defined\n",
1453130803Smarcel			 SYMBOL_PRINT_NAME (sym_class));
1454130803Smarcel      else
1455130803Smarcel	cplusplus_error (saved_arg,
1456130803Smarcel			 "the class %s does not have any method named %s\n",
1457130803Smarcel			 SYMBOL_PRINT_NAME (sym_class), tmp);
145898944Sobrien    }
1459130803Smarcel}
146098944Sobrien
1461130803Smarcel/* Find all methods named COPY in the class whose type is T, and put
1462130803Smarcel   them in SYM_ARR.  Return the number of methods found.  */
1463130803Smarcel
1464130803Smarcelstatic int
1465130803Smarcelcollect_methods (char *copy, struct type *t,
1466130803Smarcel		 struct symbol **sym_arr)
1467130803Smarcel{
1468130803Smarcel  int i1 = 0;	/*  Counter for the symbol array.  */
1469130803Smarcel
1470130803Smarcel  if (destructor_name_p (copy, t))
1471130803Smarcel    {
1472130803Smarcel      /* Destructors are a special case.  */
1473130803Smarcel      int m_index, f_index;
1474130803Smarcel
1475130803Smarcel      if (get_destructor_fn_field (t, &m_index, &f_index))
1476130803Smarcel	{
1477130803Smarcel	  struct fn_field *f = TYPE_FN_FIELDLIST1 (t, m_index);
1478130803Smarcel
1479130803Smarcel	  sym_arr[i1] =
1480130803Smarcel	    lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, f_index),
1481130803Smarcel			   NULL, VAR_DOMAIN, (int *) NULL,
1482130803Smarcel			   (struct symtab **) NULL);
1483130803Smarcel	  if (sym_arr[i1])
1484130803Smarcel	    i1++;
1485130803Smarcel	}
1486130803Smarcel    }
1487130803Smarcel  else
1488130803Smarcel    i1 = find_methods (t, copy, sym_arr);
1489130803Smarcel
1490130803Smarcel  return i1;
1491130803Smarcel}
1492130803Smarcel
1493130803Smarcel
1494130803Smarcel
1495130803Smarcel/* Return the symtab associated to the filename given by the substring
1496130803Smarcel   of *ARGPTR ending at P, and advance ARGPTR past that filename.  If
1497130803Smarcel   NOT_FOUND_PTR is not null and the source file is not found, store
1498130803Smarcel   boolean true at the location pointed to and do not issue an
1499130803Smarcel   error message.  */
1500130803Smarcel
1501130803Smarcelstatic struct symtab *
1502130803Smarcelsymtab_from_filename (char **argptr, char *p, int is_quote_enclosed,
1503130803Smarcel		      int *not_found_ptr)
1504130803Smarcel{
1505130803Smarcel  char *p1;
1506130803Smarcel  char *copy;
1507130803Smarcel  struct symtab *file_symtab;
1508130803Smarcel
1509130803Smarcel  p1 = p;
1510130803Smarcel  while (p != *argptr && p[-1] == ' ')
1511130803Smarcel    --p;
1512130803Smarcel  if ((*p == '"') && is_quote_enclosed)
1513130803Smarcel    --p;
151498944Sobrien  copy = (char *) alloca (p - *argptr + 1);
151598944Sobrien  memcpy (copy, *argptr, p - *argptr);
1516130803Smarcel  /* It may have the ending quote right after the file name.  */
1517130803Smarcel  if (is_quote_enclosed && copy[p - *argptr - 1] == '"')
1518130803Smarcel    copy[p - *argptr - 1] = 0;
1519130803Smarcel  else
1520130803Smarcel    copy[p - *argptr] = 0;
1521130803Smarcel
1522130803Smarcel  /* Find that file's data.  */
1523130803Smarcel  file_symtab = lookup_symtab (copy);
1524130803Smarcel  if (file_symtab == 0)
152598944Sobrien    {
1526130803Smarcel      if (!have_full_symbols () && !have_partial_symbols ())
1527130803Smarcel	error ("No symbol table is loaded.  Use the \"file\" command.");
1528130803Smarcel      if (not_found_ptr)
1529130803Smarcel	{
1530130803Smarcel	  *not_found_ptr = 1;
1531130803Smarcel	  /* The caller has indicated that it wishes quiet notification of any
1532130803Smarcel	     error where the function or file is not found.  A call to
1533130803Smarcel	     error_silent causes an error to occur, but it does not issue
1534130803Smarcel	     the supplied message.  The message can be manually output by
1535130803Smarcel	     the caller, if desired.  This is used, for example, when
1536130803Smarcel	     attempting to set breakpoints for functions in shared libraries
1537130803Smarcel	     that have not yet been loaded.  */
1538130803Smarcel	  error_silent ("No source file named %s.", copy);
1539130803Smarcel	}
1540130803Smarcel      error ("No source file named %s.", copy);
154198944Sobrien    }
1542130803Smarcel
1543130803Smarcel  /* Discard the file name from the arg.  */
1544130803Smarcel  p = p1 + 1;
154598944Sobrien  while (*p == ' ' || *p == '\t')
154698944Sobrien    p++;
154798944Sobrien  *argptr = p;
154898944Sobrien
1549130803Smarcel  return file_symtab;
1550130803Smarcel}
155198944Sobrien
1552130803Smarcel
1553130803Smarcel
1554130803Smarcel/* This decodes a line where the argument is all digits (possibly
1555130803Smarcel   preceded by a sign).  Q should point to the end of those digits;
1556130803Smarcel   the other arguments are as usual.  */
1557130803Smarcel
1558130803Smarcelstatic struct symtabs_and_lines
1559130803Smarceldecode_all_digits (char **argptr, struct symtab *default_symtab,
1560130803Smarcel		   int default_line, char ***canonical,
1561130803Smarcel		   struct symtab *file_symtab, char *q)
1562130803Smarcel
1563130803Smarcel{
1564130803Smarcel  struct symtabs_and_lines values;
1565130803Smarcel  struct symtab_and_line val;
1566130803Smarcel
1567130803Smarcel  enum sign
156898944Sobrien    {
1569130803Smarcel      none, plus, minus
1570130803Smarcel    }
1571130803Smarcel  sign = none;
157298944Sobrien
1573130803Smarcel  /* We might need a canonical line spec if no file was specified.  */
1574130803Smarcel  int need_canonical = (file_symtab == 0) ? 1 : 0;
1575130803Smarcel
1576130803Smarcel  init_sal (&val);
1577130803Smarcel
1578130803Smarcel  /* This is where we need to make sure that we have good defaults.
1579130803Smarcel     We must guarantee that this section of code is never executed
1580130803Smarcel     when we are called with just a function name, since
1581130803Smarcel     set_default_source_symtab_and_line uses
1582130803Smarcel     select_source_symtab that calls us with such an argument.  */
1583130803Smarcel
1584130803Smarcel  if (file_symtab == 0 && default_symtab == 0)
1585130803Smarcel    {
1586130803Smarcel      /* Make sure we have at least a default source file.  */
1587130803Smarcel      set_default_source_symtab_and_line ();
1588130803Smarcel      initialize_defaults (&default_symtab, &default_line);
1589130803Smarcel    }
1590130803Smarcel
1591130803Smarcel  if (**argptr == '+')
1592130803Smarcel    sign = plus, (*argptr)++;
1593130803Smarcel  else if (**argptr == '-')
1594130803Smarcel    sign = minus, (*argptr)++;
1595130803Smarcel  val.line = atoi (*argptr);
1596130803Smarcel  switch (sign)
1597130803Smarcel    {
1598130803Smarcel    case plus:
1599130803Smarcel      if (q == *argptr)
1600130803Smarcel	val.line = 5;
1601130803Smarcel      if (file_symtab == 0)
1602130803Smarcel	val.line = default_line + val.line;
1603130803Smarcel      break;
1604130803Smarcel    case minus:
1605130803Smarcel      if (q == *argptr)
1606130803Smarcel	val.line = 15;
1607130803Smarcel      if (file_symtab == 0)
1608130803Smarcel	val.line = default_line - val.line;
160998944Sobrien      else
1610130803Smarcel	val.line = 1;
1611130803Smarcel      break;
1612130803Smarcel    case none:
1613130803Smarcel      break;		/* No need to adjust val.line.  */
1614130803Smarcel    }
161598944Sobrien
1616130803Smarcel  while (*q == ' ' || *q == '\t')
1617130803Smarcel    q++;
1618130803Smarcel  *argptr = q;
1619130803Smarcel  if (file_symtab == 0)
1620130803Smarcel    file_symtab = default_symtab;
162198944Sobrien
1622130803Smarcel  /* It is possible that this source file has more than one symtab,
1623130803Smarcel     and that the new line number specification has moved us from the
1624130803Smarcel     default (in file_symtab) to a new one.  */
1625130803Smarcel  val.symtab = find_line_symtab (file_symtab, val.line, NULL, NULL);
1626130803Smarcel  if (val.symtab == 0)
1627130803Smarcel    val.symtab = file_symtab;
162898944Sobrien
1629130803Smarcel  val.pc = 0;
1630130803Smarcel  values.sals = (struct symtab_and_line *)
1631130803Smarcel    xmalloc (sizeof (struct symtab_and_line));
1632130803Smarcel  values.sals[0] = val;
1633130803Smarcel  values.nelts = 1;
1634130803Smarcel  if (need_canonical)
1635130803Smarcel    build_canonical_line_spec (values.sals, NULL, canonical);
1636130803Smarcel  return values;
1637130803Smarcel}
163898944Sobrien
1639130803Smarcel
164098944Sobrien
1641130803Smarcel/* Decode a linespec starting with a dollar sign.  */
164298944Sobrien
1643130803Smarcelstatic struct symtabs_and_lines
1644130803Smarceldecode_dollar (char *copy, int funfirstline, struct symtab *default_symtab,
1645130803Smarcel	       char ***canonical, struct symtab *file_symtab)
1646130803Smarcel{
1647130803Smarcel  struct value *valx;
1648130803Smarcel  int index = 0;
1649130803Smarcel  int need_canonical = 0;
1650130803Smarcel  struct symtabs_and_lines values;
1651130803Smarcel  struct symtab_and_line val;
1652130803Smarcel  char *p;
1653130803Smarcel  struct symbol *sym;
1654130803Smarcel  /* The symtab that SYM was found in.  */
1655130803Smarcel  struct symtab *sym_symtab;
1656130803Smarcel  struct minimal_symbol *msymbol;
165798944Sobrien
1658130803Smarcel  p = (copy[1] == '$') ? copy + 2 : copy + 1;
1659130803Smarcel  while (*p >= '0' && *p <= '9')
1660130803Smarcel    p++;
1661130803Smarcel  if (!*p)		/* Reached end of token without hitting non-digit.  */
1662130803Smarcel    {
1663130803Smarcel      /* We have a value history reference.  */
1664130803Smarcel      sscanf ((copy[1] == '$') ? copy + 2 : copy + 1, "%d", &index);
1665130803Smarcel      valx = access_value_history ((copy[1] == '$') ? -index : index);
1666130803Smarcel      if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
1667130803Smarcel	error ("History values used in line specs must have integer values.");
166898944Sobrien    }
1669130803Smarcel  else
1670130803Smarcel    {
1671130803Smarcel      /* Not all digits -- may be user variable/function or a
1672130803Smarcel	 convenience variable.  */
167398944Sobrien
1674130803Smarcel      /* Look up entire name as a symbol first.  */
1675130803Smarcel      sym = lookup_symbol (copy, 0, VAR_DOMAIN, 0, &sym_symtab);
1676130803Smarcel      file_symtab = (struct symtab *) 0;
1677130803Smarcel      need_canonical = 1;
1678130803Smarcel      /* Symbol was found --> jump to normal symbol processing.  */
1679130803Smarcel      if (sym)
1680130803Smarcel	return symbol_found (funfirstline, canonical, copy, sym,
1681130803Smarcel			     NULL, sym_symtab);
168298944Sobrien
1683130803Smarcel      /* If symbol was not found, look in minimal symbol tables.  */
1684130803Smarcel      msymbol = lookup_minimal_symbol (copy, NULL, NULL);
1685130803Smarcel      /* Min symbol was found --> jump to minsym processing.  */
1686130803Smarcel      if (msymbol)
1687130803Smarcel	return minsym_found (funfirstline, msymbol);
168898944Sobrien
1689130803Smarcel      /* Not a user variable or function -- must be convenience variable.  */
1690130803Smarcel      need_canonical = (file_symtab == 0) ? 1 : 0;
1691130803Smarcel      valx = value_of_internalvar (lookup_internalvar (copy + 1));
1692130803Smarcel      if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
1693130803Smarcel	error ("Convenience variables used in line specs must have integer values.");
1694130803Smarcel    }
1695130803Smarcel
1696130803Smarcel  init_sal (&val);
1697130803Smarcel
1698130803Smarcel  /* Either history value or convenience value from above, in valx.  */
1699130803Smarcel  val.symtab = file_symtab ? file_symtab : default_symtab;
1700130803Smarcel  val.line = value_as_long (valx);
1701130803Smarcel  val.pc = 0;
1702130803Smarcel
1703130803Smarcel  values.sals = (struct symtab_and_line *) xmalloc (sizeof val);
1704130803Smarcel  values.sals[0] = val;
1705130803Smarcel  values.nelts = 1;
1706130803Smarcel
1707130803Smarcel  if (need_canonical)
1708130803Smarcel    build_canonical_line_spec (values.sals, NULL, canonical);
1709130803Smarcel
1710130803Smarcel  return values;
1711130803Smarcel}
1712130803Smarcel
1713130803Smarcel
1714130803Smarcel
1715130803Smarcel/* Decode a linespec that's a variable.  If FILE_SYMTAB is non-NULL,
1716130803Smarcel   look in that symtab's static variables first.  If NOT_FOUND_PTR is not NULL and
1717130803Smarcel   the function cannot be found, store boolean true in the location pointed to
1718130803Smarcel   and do not issue an error message.  */
1719130803Smarcel
1720130803Smarcelstatic struct symtabs_and_lines
1721130803Smarceldecode_variable (char *copy, int funfirstline, char ***canonical,
1722130803Smarcel		 struct symtab *file_symtab, int *not_found_ptr)
1723130803Smarcel{
1724130803Smarcel  struct symbol *sym;
1725130803Smarcel  /* The symtab that SYM was found in.  */
1726130803Smarcel  struct symtab *sym_symtab;
1727130803Smarcel
1728130803Smarcel  struct minimal_symbol *msymbol;
1729130803Smarcel
173098944Sobrien  sym = lookup_symbol (copy,
1731130803Smarcel		       (file_symtab
1732130803Smarcel			? BLOCKVECTOR_BLOCK (BLOCKVECTOR (file_symtab),
1733130803Smarcel					     STATIC_BLOCK)
1734130803Smarcel			: get_selected_block (0)),
1735130803Smarcel		       VAR_DOMAIN, 0, &sym_symtab);
173698944Sobrien
173798944Sobrien  if (sym != NULL)
1738130803Smarcel    return symbol_found (funfirstline, canonical, copy, sym,
1739130803Smarcel			 file_symtab, sym_symtab);
174098944Sobrien
1741130803Smarcel  msymbol = lookup_minimal_symbol (copy, NULL, NULL);
174298944Sobrien
1743130803Smarcel  if (msymbol != NULL)
1744130803Smarcel    return minsym_found (funfirstline, msymbol);
1745130803Smarcel
1746130803Smarcel  if (!have_full_symbols () &&
1747130803Smarcel      !have_partial_symbols () && !have_minimal_symbols ())
1748130803Smarcel    error ("No symbol table is loaded.  Use the \"file\" command.");
1749130803Smarcel
1750130803Smarcel  if (not_found_ptr)
1751130803Smarcel    {
1752130803Smarcel      *not_found_ptr = 1;
1753130803Smarcel      /* The caller has indicated that it wishes quiet notification of any
1754130803Smarcel	 error where the function or file is not found.  A call to
1755130803Smarcel	 error_silent causes an error to occur, but it does not issue
1756130803Smarcel	 the supplied message.  The message can be manually output by
1757130803Smarcel	 the caller, if desired.  This is used, for example, when
1758130803Smarcel	 attempting to set breakpoints for functions in shared libraries
1759130803Smarcel	 that have not yet been loaded.  */
1760130803Smarcel      error_silent ("Function \"%s\" not defined.", copy);
176198944Sobrien    }
1762130803Smarcel
1763130803Smarcel  error ("Function \"%s\" not defined.", copy);
1764130803Smarcel}
176598944Sobrien
176698944Sobrien
1767130803Smarcel
176898944Sobrien
1769130803Smarcel/* Now come some functions that are called from multiple places within
1770130803Smarcel   decode_line_1.  */
1771130803Smarcel
1772130803Smarcel/* We've found a symbol SYM to associate with our linespec; build a
1773130803Smarcel   corresponding struct symtabs_and_lines.  */
1774130803Smarcel
1775130803Smarcelstatic struct symtabs_and_lines
1776130803Smarcelsymbol_found (int funfirstline, char ***canonical, char *copy,
1777130803Smarcel	      struct symbol *sym, struct symtab *file_symtab,
1778130803Smarcel	      struct symtab *sym_symtab)
1779130803Smarcel{
1780130803Smarcel  struct symtabs_and_lines values;
1781130803Smarcel
1782130803Smarcel  if (SYMBOL_CLASS (sym) == LOC_BLOCK)
178398944Sobrien    {
1784130803Smarcel      /* Arg is the name of a function */
178598944Sobrien      values.sals = (struct symtab_and_line *)
178698944Sobrien	xmalloc (sizeof (struct symtab_and_line));
1787130803Smarcel      values.sals[0] = find_function_start_sal (sym, funfirstline);
1788130803Smarcel      values.nelts = 1;
1789130803Smarcel
1790130803Smarcel      /* Don't use the SYMBOL_LINE; if used at all it points to
1791130803Smarcel	 the line containing the parameters or thereabouts, not
1792130803Smarcel	 the first line of code.  */
1793130803Smarcel
1794130803Smarcel      /* We might need a canonical line spec if it is a static
1795130803Smarcel	 function.  */
1796130803Smarcel      if (file_symtab == 0)
179798944Sobrien	{
1798130803Smarcel	  struct blockvector *bv = BLOCKVECTOR (sym_symtab);
1799130803Smarcel	  struct block *b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1800130803Smarcel	  if (lookup_block_symbol (b, copy, NULL, VAR_DOMAIN) != NULL)
1801130803Smarcel	    build_canonical_line_spec (values.sals, copy, canonical);
180298944Sobrien	}
180398944Sobrien      return values;
180498944Sobrien    }
1805130803Smarcel  else
1806130803Smarcel    {
1807130803Smarcel      if (funfirstline)
1808130803Smarcel	error ("\"%s\" is not a function", copy);
1809130803Smarcel      else if (SYMBOL_LINE (sym) != 0)
1810130803Smarcel	{
1811130803Smarcel	  /* We know its line number.  */
1812130803Smarcel	  values.sals = (struct symtab_and_line *)
1813130803Smarcel	    xmalloc (sizeof (struct symtab_and_line));
1814130803Smarcel	  values.nelts = 1;
1815130803Smarcel	  memset (&values.sals[0], 0, sizeof (values.sals[0]));
1816130803Smarcel	  values.sals[0].symtab = sym_symtab;
1817130803Smarcel	  values.sals[0].line = SYMBOL_LINE (sym);
1818130803Smarcel	  return values;
1819130803Smarcel	}
1820130803Smarcel      else
1821130803Smarcel	/* This can happen if it is compiled with a compiler which doesn't
1822130803Smarcel	   put out line numbers for variables.  */
1823130803Smarcel	/* FIXME: Shouldn't we just set .line and .symtab to zero
1824130803Smarcel	   and return?  For example, "info line foo" could print
1825130803Smarcel	   the address.  */
1826130803Smarcel	error ("Line number not known for symbol \"%s\"", copy);
1827130803Smarcel    }
1828130803Smarcel}
182998944Sobrien
1830130803Smarcel/* We've found a minimal symbol MSYMBOL to associate with our
1831130803Smarcel   linespec; build a corresponding struct symtabs_and_lines.  */
183298944Sobrien
1833130803Smarcelstatic struct symtabs_and_lines
1834130803Smarcelminsym_found (int funfirstline, struct minimal_symbol *msymbol)
1835130803Smarcel{
1836130803Smarcel  struct symtabs_and_lines values;
1837130803Smarcel
1838130803Smarcel  values.sals = (struct symtab_and_line *)
1839130803Smarcel    xmalloc (sizeof (struct symtab_and_line));
1840130803Smarcel  values.sals[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol),
1841130803Smarcel				      (struct bfd_section *) 0, 0);
1842130803Smarcel  values.sals[0].section = SYMBOL_BFD_SECTION (msymbol);
1843130803Smarcel  if (funfirstline)
1844130803Smarcel    {
1845130803Smarcel      values.sals[0].pc += FUNCTION_START_OFFSET;
1846130803Smarcel      values.sals[0].pc = SKIP_PROLOGUE (values.sals[0].pc);
1847130803Smarcel    }
1848130803Smarcel  values.nelts = 1;
1849130803Smarcel  return values;
185098944Sobrien}
1851