1 /* Parser for linespec for the GNU debugger, GDB.
2 
3    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
5    Free Software Foundation, Inc.
6 
7    This file is part of GDB.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23 
24 #include "defs.h"
25 #include "symtab.h"
26 #include "frame.h"
27 #include "command.h"
28 #include "symfile.h"
29 #include "objfiles.h"
30 #include "source.h"
31 #include "demangle.h"
32 #include "value.h"
33 #include "completer.h"
34 #include "cp-abi.h"
35 #include "parser-defs.h"
36 #include "block.h"
37 #include "objc-lang.h"
38 #include "linespec.h"
39 #include "exceptions.h"
40 
41 /* We share this one with symtab.c, but it is not exported widely. */
42 
43 extern char *operator_chars (char *, char **);
44 
45 /* Prototypes for local functions */
46 
47 static void initialize_defaults (struct symtab **default_symtab,
48 				 int *default_line);
49 
50 static void set_flags (char *arg, int *is_quoted, char **paren_pointer);
51 
52 static struct symtabs_and_lines decode_indirect (char **argptr);
53 
54 static char *locate_first_half (char **argptr, int *is_quote_enclosed);
55 
56 static struct symtabs_and_lines decode_objc (char **argptr,
57 					     int funfirstline,
58 					     struct symtab *file_symtab,
59 					     char ***canonical,
60 					     char *saved_arg);
61 
62 static struct symtabs_and_lines decode_compound (char **argptr,
63 						 int funfirstline,
64 						 char ***canonical,
65 						 char *saved_arg,
66 						 char *p);
67 
68 static struct symbol *lookup_prefix_sym (char **argptr, char *p);
69 
70 static struct symtabs_and_lines find_method (int funfirstline,
71 					     char ***canonical,
72 					     char *saved_arg,
73 					     char *copy,
74 					     struct type *t,
75 					     struct symbol *sym_class);
76 
77 static int collect_methods (char *copy, struct type *t,
78 			    struct symbol **sym_arr);
79 
80 static NORETURN void cplusplus_error (const char *name,
81 				      const char *fmt, ...)
82      ATTR_NORETURN ATTR_FORMAT (printf, 2, 3);
83 
84 static int total_number_of_methods (struct type *type);
85 
86 static int find_methods (struct type *, char *, struct symbol **);
87 
88 static int add_matching_methods (int method_counter, struct type *t,
89 				 struct symbol **sym_arr);
90 
91 static int add_constructors (int method_counter, struct type *t,
92 			     struct symbol **sym_arr);
93 
94 static void build_canonical_line_spec (struct symtab_and_line *,
95 				       char *, char ***);
96 
97 static char *find_toplevel_char (char *s, char c);
98 
99 static int is_objc_method_format (const char *s);
100 
101 static struct symtabs_and_lines decode_line_2 (struct symbol *[],
102 					       int, int, char ***);
103 
104 static struct symtab *symtab_from_filename (char **argptr,
105 					    char *p, int is_quote_enclosed,
106 					    int *not_found_ptr);
107 
108 static struct
109 symtabs_and_lines decode_all_digits (char **argptr,
110 				     struct symtab *default_symtab,
111 				     int default_line,
112 				     char ***canonical,
113 				     struct symtab *file_symtab,
114 				     char *q);
115 
116 static struct symtabs_and_lines decode_dollar (char *copy,
117 					       int funfirstline,
118 					       struct symtab *default_symtab,
119 					       char ***canonical,
120 					       struct symtab *file_symtab);
121 
122 static struct symtabs_and_lines decode_variable (char *copy,
123 						 int funfirstline,
124 						 char ***canonical,
125 						 struct symtab *file_symtab,
126 						 int *not_found_ptr);
127 
128 static struct
129 symtabs_and_lines symbol_found (int funfirstline,
130 				char ***canonical,
131 				char *copy,
132 				struct symbol *sym,
133 				struct symtab *file_symtab,
134 				struct symtab *sym_symtab);
135 
136 static struct
137 symtabs_and_lines minsym_found (int funfirstline,
138 				struct minimal_symbol *msymbol);
139 
140 /* Helper functions. */
141 
142 /* Issue a helpful hint on using the command completion feature on
143    single quoted demangled C++ symbols as part of the completion
144    error.  */
145 
146 static NORETURN void
cplusplus_error(const char * name,const char * fmt,...)147 cplusplus_error (const char *name, const char *fmt, ...)
148 {
149   struct ui_file *tmp_stream;
150   tmp_stream = mem_fileopen ();
151   make_cleanup_ui_file_delete (tmp_stream);
152 
153   {
154     va_list args;
155     va_start (args, fmt);
156     vfprintf_unfiltered (tmp_stream, fmt, args);
157     va_end (args);
158   }
159 
160   while (*name == '\'')
161     name++;
162   fprintf_unfiltered (tmp_stream,
163 		      ("Hint: try '%s<TAB> or '%s<ESC-?>\n"
164 		       "(Note leading single quote.)"),
165 		      name, name);
166   error_stream (tmp_stream);
167 }
168 
169 /* Return the number of methods described for TYPE, including the
170    methods from types it derives from. This can't be done in the symbol
171    reader because the type of the baseclass might still be stubbed
172    when the definition of the derived class is parsed.  */
173 
174 static int
total_number_of_methods(struct type * type)175 total_number_of_methods (struct type *type)
176 {
177   int n;
178   int count;
179 
180   CHECK_TYPEDEF (type);
181   if (TYPE_CPLUS_SPECIFIC (type) == NULL)
182     return 0;
183   count = TYPE_NFN_FIELDS_TOTAL (type);
184 
185   for (n = 0; n < TYPE_N_BASECLASSES (type); n++)
186     count += total_number_of_methods (TYPE_BASECLASS (type, n));
187 
188   return count;
189 }
190 
191 /* Recursive helper function for decode_line_1.
192    Look for methods named NAME in type T.
193    Return number of matches.
194    Put matches in SYM_ARR, which should have been allocated with
195    a size of total_number_of_methods (T) * sizeof (struct symbol *).
196    Note that this function is g++ specific.  */
197 
198 static int
find_methods(struct type * t,char * name,struct symbol ** sym_arr)199 find_methods (struct type *t, char *name, struct symbol **sym_arr)
200 {
201   int i1 = 0;
202   int ibase;
203   char *class_name = type_name_no_tag (t);
204 
205   /* Ignore this class if it doesn't have a name.  This is ugly, but
206      unless we figure out how to get the physname without the name of
207      the class, then the loop can't do any good.  */
208   if (class_name
209       && (lookup_symbol (class_name, (struct block *) NULL,
210 			 STRUCT_DOMAIN, (int *) NULL,
211 			 (struct symtab **) NULL)))
212     {
213       int method_counter;
214       int name_len = strlen (name);
215 
216       CHECK_TYPEDEF (t);
217 
218       /* Loop over each method name.  At this level, all overloads of a name
219          are counted as a single name.  There is an inner loop which loops over
220          each overload.  */
221 
222       for (method_counter = TYPE_NFN_FIELDS (t) - 1;
223 	   method_counter >= 0;
224 	   --method_counter)
225 	{
226 	  char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
227 	  char dem_opname[64];
228 
229 	  if (strncmp (method_name, "__", 2) == 0 ||
230 	      strncmp (method_name, "op", 2) == 0 ||
231 	      strncmp (method_name, "type", 4) == 0)
232 	    {
233 	      if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI, sizeof(dem_opname)))
234 		method_name = dem_opname;
235 	      else if (cplus_demangle_opname (method_name, dem_opname, 0, sizeof(dem_opname)))
236 		method_name = dem_opname;
237 	    }
238 
239 	  if (strcmp_iw (name, method_name) == 0)
240 	    /* Find all the overloaded methods with that name.  */
241 	    i1 += add_matching_methods (method_counter, t,
242 					sym_arr + i1);
243 	  else if (strncmp (class_name, name, name_len) == 0
244 		   && (class_name[name_len] == '\0'
245 		       || class_name[name_len] == '<'))
246 	    i1 += add_constructors (method_counter, t,
247 				    sym_arr + i1);
248 	}
249     }
250 
251   /* Only search baseclasses if there is no match yet, since names in
252      derived classes override those in baseclasses.
253 
254      FIXME: The above is not true; it is only true of member functions
255      if they have the same number of arguments (??? - section 13.1 of the
256      ARM says the function members are not in the same scope but doesn't
257      really spell out the rules in a way I understand.  In any case, if
258      the number of arguments differ this is a case in which we can overload
259      rather than hiding without any problem, and gcc 2.4.5 does overload
260      rather than hiding in this case).  */
261 
262   if (i1 == 0)
263     for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
264       i1 += find_methods (TYPE_BASECLASS (t, ibase), name, sym_arr + i1);
265 
266   return i1;
267 }
268 
269 /* Add the symbols associated to methods of the class whose type is T
270    and whose name matches the method indexed by METHOD_COUNTER in the
271    array SYM_ARR.  Return the number of methods added.  */
272 
273 static int
add_matching_methods(int method_counter,struct type * t,struct symbol ** sym_arr)274 add_matching_methods (int method_counter, struct type *t,
275 		      struct symbol **sym_arr)
276 {
277   int field_counter;
278   int i1 = 0;
279 
280   for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
281        field_counter >= 0;
282        --field_counter)
283     {
284       struct fn_field *f;
285       char *phys_name;
286 
287       f = TYPE_FN_FIELDLIST1 (t, method_counter);
288 
289       if (TYPE_FN_FIELD_STUB (f, field_counter))
290 	{
291 	  char *tmp_name;
292 
293 	  tmp_name = gdb_mangle_name (t,
294 				      method_counter,
295 				      field_counter);
296 	  phys_name = alloca (strlen (tmp_name) + 1);
297 	  strcpy (phys_name, tmp_name);
298 	  xfree (tmp_name);
299 	}
300       else
301 	phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
302 
303       /* Destructor is handled by caller, don't add it to
304 	 the list.  */
305       if (is_destructor_name (phys_name) != 0)
306 	continue;
307 
308       sym_arr[i1] = lookup_symbol (phys_name,
309 				   NULL, VAR_DOMAIN,
310 				   (int *) NULL,
311 				   (struct symtab **) NULL);
312       if (sym_arr[i1])
313 	i1++;
314       else
315 	{
316 	  /* This error message gets printed, but the method
317 	     still seems to be found
318 	     fputs_filtered("(Cannot find method ", gdb_stdout);
319 	     fprintf_symbol_filtered (gdb_stdout, phys_name,
320 	     language_cplus,
321 	     DMGL_PARAMS | DMGL_ANSI);
322 	     fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
323 	  */
324 	}
325     }
326 
327   return i1;
328 }
329 
330 /* Add the symbols associated to constructors of the class whose type
331    is CLASS_TYPE and which are indexed by by METHOD_COUNTER to the
332    array SYM_ARR.  Return the number of methods added.  */
333 
334 static int
add_constructors(int method_counter,struct type * t,struct symbol ** sym_arr)335 add_constructors (int method_counter, struct type *t,
336 		  struct symbol **sym_arr)
337 {
338   int field_counter;
339   int i1 = 0;
340 
341   /* For GCC 3.x and stabs, constructors and destructors
342      have names like __base_ctor and __complete_dtor.
343      Check the physname for now if we're looking for a
344      constructor.  */
345   for (field_counter
346 	 = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
347        field_counter >= 0;
348        --field_counter)
349     {
350       struct fn_field *f;
351       char *phys_name;
352 
353       f = TYPE_FN_FIELDLIST1 (t, method_counter);
354 
355       /* GCC 3.x will never produce stabs stub methods, so
356 	 we don't need to handle this case.  */
357       if (TYPE_FN_FIELD_STUB (f, field_counter))
358 	continue;
359       phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
360       if (! is_constructor_name (phys_name))
361 	continue;
362 
363       /* If this method is actually defined, include it in the
364 	 list.  */
365       sym_arr[i1] = lookup_symbol (phys_name,
366 				   NULL, VAR_DOMAIN,
367 				   (int *) NULL,
368 				   (struct symtab **) NULL);
369       if (sym_arr[i1])
370 	i1++;
371     }
372 
373   return i1;
374 }
375 
376 /* Helper function for decode_line_1.
377    Build a canonical line spec in CANONICAL if it is non-NULL and if
378    the SAL has a symtab.
379    If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
380    If SYMNAME is NULL the line number from SAL is used and the canonical
381    line spec is `filename:linenum'.  */
382 
383 static void
build_canonical_line_spec(struct symtab_and_line * sal,char * symname,char *** canonical)384 build_canonical_line_spec (struct symtab_and_line *sal, char *symname,
385 			   char ***canonical)
386 {
387   char **canonical_arr;
388   char *canonical_name;
389   char *filename;
390   struct symtab *s = sal->symtab;
391 
392   if (s == (struct symtab *) NULL
393       || s->filename == (char *) NULL
394       || canonical == (char ***) NULL)
395     return;
396 
397   canonical_arr = (char **) xmalloc (sizeof (char *));
398   *canonical = canonical_arr;
399 
400   filename = s->filename;
401   if (symname != NULL)
402     {
403       canonical_name = xmalloc (strlen (filename) + strlen (symname) + 2);
404       sprintf (canonical_name, "%s:%s", filename, symname);
405     }
406   else
407     {
408       canonical_name = xmalloc (strlen (filename) + 30);
409       sprintf (canonical_name, "%s:%d", filename, sal->line);
410     }
411   canonical_arr[0] = canonical_name;
412 }
413 
414 
415 
416 /* Find an instance of the character C in the string S that is outside
417    of all parenthesis pairs, single-quoted strings, and double-quoted
418    strings.  Also, ignore the char within a template name, like a ','
419    within foo<int, int>.  */
420 
421 static char *
find_toplevel_char(char * s,char c)422 find_toplevel_char (char *s, char c)
423 {
424   int quoted = 0;		/* zero if we're not in quotes;
425 				   '"' if we're in a double-quoted string;
426 				   '\'' if we're in a single-quoted string.  */
427   int depth = 0;		/* Number of unclosed parens we've seen.  */
428   char *scan;
429 
430   for (scan = s; *scan; scan++)
431     {
432       if (quoted)
433 	{
434 	  if (*scan == quoted)
435 	    quoted = 0;
436 	  else if (*scan == '\\' && *(scan + 1))
437 	    scan++;
438 	}
439       else if (*scan == c && ! quoted && depth == 0)
440 	return scan;
441       else if (*scan == '"' || *scan == '\'')
442 	quoted = *scan;
443       else if (*scan == '(' || *scan == '<')
444 	depth++;
445       else if ((*scan == ')' || *scan == '>') && depth > 0)
446 	depth--;
447     }
448 
449   return 0;
450 }
451 
452 /* Determines if the gives string corresponds to an Objective-C method
453    representation, such as -[Foo bar:] or +[Foo bar]. Objective-C symbols
454    are allowed to have spaces and parentheses in them.  */
455 
456 static int
is_objc_method_format(const char * s)457 is_objc_method_format (const char *s)
458 {
459   if (s == NULL || *s == '\0')
460     return 0;
461   /* Handle arguments with the format FILENAME:SYMBOL.  */
462   if ((s[0] == ':') && (strchr ("+-", s[1]) != NULL)
463       && (s[2] == '[') && strchr(s, ']'))
464     return 1;
465   /* Handle arguments that are just SYMBOL.  */
466   else if ((strchr ("+-", s[0]) != NULL) && (s[1] == '[') && strchr(s, ']'))
467     return 1;
468   return 0;
469 }
470 
471 /* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
472    operate on (ask user if necessary).
473    If CANONICAL is non-NULL return a corresponding array of mangled names
474    as canonical line specs there.  */
475 
476 static struct symtabs_and_lines
decode_line_2(struct symbol * sym_arr[],int nelts,int funfirstline,char *** canonical)477 decode_line_2 (struct symbol *sym_arr[], int nelts, int funfirstline,
478 	       char ***canonical)
479 {
480   struct symtabs_and_lines values, return_values;
481   char *args, *arg1;
482   int i;
483   char *prompt;
484   char *symname;
485   struct cleanup *old_chain;
486   char **canonical_arr = (char **) NULL;
487 
488   values.sals = (struct symtab_and_line *)
489     alloca (nelts * sizeof (struct symtab_and_line));
490   return_values.sals = (struct symtab_and_line *)
491     xmalloc (nelts * sizeof (struct symtab_and_line));
492   old_chain = make_cleanup (xfree, return_values.sals);
493 
494   if (canonical)
495     {
496       canonical_arr = (char **) xmalloc (nelts * sizeof (char *));
497       make_cleanup (xfree, canonical_arr);
498       memset (canonical_arr, 0, nelts * sizeof (char *));
499       *canonical = canonical_arr;
500     }
501 
502   i = 0;
503   printf_unfiltered (_("[0] cancel\n[1] all\n"));
504   while (i < nelts)
505     {
506       init_sal (&return_values.sals[i]);	/* Initialize to zeroes.  */
507       init_sal (&values.sals[i]);
508       if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
509 	{
510 	  values.sals[i] = find_function_start_sal (sym_arr[i], funfirstline);
511 	  if (values.sals[i].symtab)
512 	    printf_unfiltered ("[%d] %s at %s:%d\n",
513 			       (i + 2),
514 			       SYMBOL_PRINT_NAME (sym_arr[i]),
515 			       values.sals[i].symtab->filename,
516 			       values.sals[i].line);
517 	  else
518 	    printf_unfiltered (_("[%d] %s at ?FILE:%d [No symtab? Probably broken debug info...]\n"),
519 			       (i + 2),
520 			       SYMBOL_PRINT_NAME (sym_arr[i]),
521 			       values.sals[i].line);
522 
523 	}
524       else
525 	printf_unfiltered (_("?HERE\n"));
526       i++;
527     }
528 
529   prompt = getenv ("PS2");
530   if (prompt == NULL)
531     {
532       prompt = "> ";
533     }
534   args = command_line_input (prompt, 0, "overload-choice");
535 
536   if (args == 0 || *args == 0)
537     error_no_arg (_("one or more choice numbers"));
538 
539   i = 0;
540   while (*args)
541     {
542       int num;
543 
544       arg1 = args;
545       while (*arg1 >= '0' && *arg1 <= '9')
546 	arg1++;
547       if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
548 	error (_("Arguments must be choice numbers."));
549 
550       num = atoi (args);
551 
552       if (num == 0)
553 	error (_("canceled"));
554       else if (num == 1)
555 	{
556 	  if (canonical_arr)
557 	    {
558 	      for (i = 0; i < nelts; i++)
559 		{
560 		  if (canonical_arr[i] == NULL)
561 		    {
562 		      symname = DEPRECATED_SYMBOL_NAME (sym_arr[i]);
563 		      canonical_arr[i] = savestring (symname, strlen (symname));
564 		    }
565 		}
566 	    }
567 	  memcpy (return_values.sals, values.sals,
568 		  (nelts * sizeof (struct symtab_and_line)));
569 	  return_values.nelts = nelts;
570 	  discard_cleanups (old_chain);
571 	  return return_values;
572 	}
573 
574       if (num >= nelts + 2)
575 	{
576 	  printf_unfiltered (_("No choice number %d.\n"), num);
577 	}
578       else
579 	{
580 	  num -= 2;
581 	  if (values.sals[num].pc)
582 	    {
583 	      if (canonical_arr)
584 		{
585 		  symname = DEPRECATED_SYMBOL_NAME (sym_arr[num]);
586 		  make_cleanup (xfree, symname);
587 		  canonical_arr[i] = savestring (symname, strlen (symname));
588 		}
589 	      return_values.sals[i++] = values.sals[num];
590 	      values.sals[num].pc = 0;
591 	    }
592 	  else
593 	    {
594 	      printf_unfiltered (_("duplicate request for %d ignored.\n"), num);
595 	    }
596 	}
597 
598       args = arg1;
599       while (*args == ' ' || *args == '\t')
600 	args++;
601     }
602   return_values.nelts = i;
603   discard_cleanups (old_chain);
604   return return_values;
605 }
606 
607 /* The parser of linespec itself. */
608 
609 /* Parse a string that specifies a line number.
610    Pass the address of a char * variable; that variable will be
611    advanced over the characters actually parsed.
612 
613    The string can be:
614 
615    LINENUM -- that line number in current file.  PC returned is 0.
616    FILE:LINENUM -- that line in that file.  PC returned is 0.
617    FUNCTION -- line number of openbrace of that function.
618    PC returned is the start of the function.
619    VARIABLE -- line number of definition of that variable.
620    PC returned is 0.
621    FILE:FUNCTION -- likewise, but prefer functions in that file.
622    *EXPR -- line in which address EXPR appears.
623 
624    This may all be followed by an "if EXPR", which we ignore.
625 
626    FUNCTION may be an undebuggable function found in minimal symbol table.
627 
628    If the argument FUNFIRSTLINE is nonzero, we want the first line
629    of real code inside a function when a function is specified, and it is
630    not OK to specify a variable or type to get its line number.
631 
632    DEFAULT_SYMTAB specifies the file to use if none is specified.
633    It defaults to current_source_symtab.
634    DEFAULT_LINE specifies the line number to use for relative
635    line numbers (that start with signs).  Defaults to current_source_line.
636    If CANONICAL is non-NULL, store an array of strings containing the canonical
637    line specs there if necessary. Currently overloaded member functions and
638    line numbers or static functions without a filename yield a canonical
639    line spec. The array and the line spec strings are allocated on the heap,
640    it is the callers responsibility to free them.
641 
642    Note that it is possible to return zero for the symtab
643    if no file is validly specified.  Callers must check that.
644    Also, the line number returned may be invalid.
645 
646    If NOT_FOUND_PTR is not null, store a boolean true/false value at the location, based
647    on whether or not failure occurs due to an unknown function or file.  In the case
648    where failure does occur due to an unknown function or file, do not issue an error
649    message.  */
650 
651 /* We allow single quotes in various places.  This is a hideous
652    kludge, which exists because the completer can't yet deal with the
653    lack of single quotes.  FIXME: write a linespec_completer which we
654    can use as appropriate instead of make_symbol_completion_list.  */
655 
656 struct symtabs_and_lines
decode_line_1(char ** argptr,int funfirstline,struct symtab * default_symtab,int default_line,char *** canonical,int * not_found_ptr)657 decode_line_1 (char **argptr, int funfirstline, struct symtab *default_symtab,
658 	       int default_line, char ***canonical, int *not_found_ptr)
659 {
660   char *p;
661   char *q;
662   /* If a file name is specified, this is its symtab.  */
663   struct symtab *file_symtab = NULL;
664 
665   char *copy;
666   /* This is NULL if there are no parens in *ARGPTR, or a pointer to
667      the closing parenthesis if there are parens.  */
668   char *paren_pointer;
669   /* This says whether or not something in *ARGPTR is quoted with
670      completer_quotes (i.e. with single quotes).  */
671   int is_quoted;
672   /* Is part of *ARGPTR is enclosed in double quotes?  */
673   int is_quote_enclosed;
674   int is_objc_method = 0;
675   char *saved_arg = *argptr;
676 
677   if (not_found_ptr)
678     *not_found_ptr = 0;
679 
680   /* Defaults have defaults.  */
681 
682   initialize_defaults (&default_symtab, &default_line);
683 
684   /* See if arg is *PC.  */
685 
686   if (**argptr == '*')
687     return decode_indirect (argptr);
688 
689   /* Set various flags.  'paren_pointer' is important for overload
690      checking, where we allow things like:
691         (gdb) break c::f(int)
692   */
693 
694   set_flags (*argptr, &is_quoted, &paren_pointer);
695 
696   /* Check to see if it's a multipart linespec (with colons or
697      periods).  */
698 
699   /* Locate the end of the first half of the linespec.
700      After the call, for instance, if the argptr string is "foo.c:123"
701      p will point at "123".  If there is only one part, like "foo", p
702      will point to "". If this is a C++ name, like "A::B::foo", p will
703      point to "::B::foo". Argptr is not changed by this call.  */
704 
705   p = locate_first_half (argptr, &is_quote_enclosed);
706 
707   /* Check if this is an Objective-C method (anything that starts with
708      a '+' or '-' and a '[').  */
709   if (is_objc_method_format (p))
710     {
711       is_objc_method = 1;
712       paren_pointer  = NULL; /* Just a category name.  Ignore it.  */
713     }
714 
715   /* Check if the symbol could be an Objective-C selector.  */
716 
717   {
718     struct symtabs_and_lines values;
719     values = decode_objc (argptr, funfirstline, NULL,
720 			  canonical, saved_arg);
721     if (values.sals != NULL)
722       return values;
723   }
724 
725   /* Does it look like there actually were two parts?  */
726 
727   if ((p[0] == ':' || p[0] == '.') && paren_pointer == NULL)
728     {
729       if (is_quoted)
730 	*argptr = *argptr + 1;
731 
732       /* Is it a C++ or Java compound data structure?
733 	 The check on p[1] == ':' is capturing the case of "::",
734 	 since p[0]==':' was checked above.
735 	 Note that the call to decode_compound does everything
736 	 for us, including the lookup on the symbol table, so we
737 	 can return now. */
738 
739       if (p[0] == '.' || p[1] == ':')
740 	return decode_compound (argptr, funfirstline, canonical,
741 				saved_arg, p);
742 
743       /* No, the first part is a filename; set s to be that file's
744 	 symtab.  Also, move argptr past the filename.  */
745 
746       file_symtab = symtab_from_filename (argptr, p, is_quote_enclosed,
747 		      			  not_found_ptr);
748     }
749 #if 0
750   /* No one really seems to know why this was added. It certainly
751      breaks the command line, though, whenever the passed
752      name is of the form ClassName::Method. This bit of code
753      singles out the class name, and if funfirstline is set (for
754      example, you are setting a breakpoint at this function),
755      you get an error. This did not occur with earlier
756      verions, so I am ifdef'ing this out. 3/29/99 */
757   else
758     {
759       /* Check if what we have till now is a symbol name */
760 
761       /* We may be looking at a template instantiation such
762          as "foo<int>".  Check here whether we know about it,
763          instead of falling through to the code below which
764          handles ordinary function names, because that code
765          doesn't like seeing '<' and '>' in a name -- the
766          skip_quoted call doesn't go past them.  So see if we
767          can figure it out right now. */
768 
769       copy = (char *) alloca (p - *argptr + 1);
770       memcpy (copy, *argptr, p - *argptr);
771       copy[p - *argptr] = '\000';
772       sym = lookup_symbol (copy, 0, VAR_DOMAIN, 0, &sym_symtab);
773       if (sym)
774 	{
775 	  *argptr = (*p == '\'') ? p + 1 : p;
776 	  return symbol_found (funfirstline, canonical, copy, sym,
777 			       NULL, sym_symtab);
778 	}
779       /* Otherwise fall out from here and go to file/line spec
780          processing, etc. */
781     }
782 #endif
783 
784   /* S is specified file's symtab, or 0 if no file specified.
785      arg no longer contains the file name.  */
786 
787   /* Check whether arg is all digits (and sign).  */
788 
789   q = *argptr;
790   if (*q == '-' || *q == '+')
791     q++;
792   while (*q >= '0' && *q <= '9')
793     q++;
794 
795   if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ','))
796     /* We found a token consisting of all digits -- at least one digit.  */
797     return decode_all_digits (argptr, default_symtab, default_line,
798 			      canonical, file_symtab, q);
799 
800   /* Arg token is not digits => try it as a variable name
801      Find the next token (everything up to end or next whitespace).  */
802 
803   if (**argptr == '$')		/* May be a convenience variable.  */
804     /* One or two $ chars possible.  */
805     p = skip_quoted (*argptr + (((*argptr)[1] == '$') ? 2 : 1));
806   else if (is_quoted)
807     {
808       p = skip_quoted (*argptr);
809       if (p[-1] != '\'')
810 	error (_("Unmatched single quote."));
811     }
812   else if (is_objc_method)
813     {
814       /* allow word separators in method names for Obj-C */
815       p = skip_quoted_chars (*argptr, NULL, "");
816     }
817   else if (paren_pointer != NULL)
818     {
819       p = paren_pointer + 1;
820     }
821   else
822     {
823       p = skip_quoted (*argptr);
824     }
825 
826   copy = (char *) alloca (p - *argptr + 1);
827   memcpy (copy, *argptr, p - *argptr);
828   copy[p - *argptr] = '\0';
829   if (p != *argptr
830       && copy[0]
831       && copy[0] == copy[p - *argptr - 1]
832       && strchr (get_gdb_completer_quote_characters (), copy[0]) != NULL)
833     {
834       copy[p - *argptr - 1] = '\0';
835       copy++;
836     }
837   while (*p == ' ' || *p == '\t')
838     p++;
839   *argptr = p;
840 
841   /* If it starts with $: may be a legitimate variable or routine name
842      (e.g. HP-UX millicode routines such as $$dyncall), or it may
843      be history value, or it may be a convenience variable.  */
844 
845   if (*copy == '$')
846     return decode_dollar (copy, funfirstline, default_symtab,
847 			  canonical, file_symtab);
848 
849   /* Look up that token as a variable.
850      If file specified, use that file's per-file block to start with.  */
851 
852   return decode_variable (copy, funfirstline, canonical,
853 			  file_symtab, not_found_ptr);
854 }
855 
856 
857 
858 /* Now, more helper functions for decode_line_1.  Some conventions
859    that these functions follow:
860 
861    Decode_line_1 typically passes along some of its arguments or local
862    variables to the subfunctions.  It passes the variables by
863    reference if they are modified by the subfunction, and by value
864    otherwise.
865 
866    Some of the functions have side effects that don't arise from
867    variables that are passed by reference.  In particular, if a
868    function is passed ARGPTR as an argument, it modifies what ARGPTR
869    points to; typically, it advances *ARGPTR past whatever substring
870    it has just looked at.  (If it doesn't modify *ARGPTR, then the
871    function gets passed *ARGPTR instead, which is then called ARG: see
872    set_flags, for example.)  Also, functions that return a struct
873    symtabs_and_lines may modify CANONICAL, as in the description of
874    decode_line_1.
875 
876    If a function returns a struct symtabs_and_lines, then that struct
877    will immediately make its way up the call chain to be returned by
878    decode_line_1.  In particular, all of the functions decode_XXX
879    calculate the appropriate struct symtabs_and_lines, under the
880    assumption that their argument is of the form XXX.  */
881 
882 /* First, some functions to initialize stuff at the beggining of the
883    function.  */
884 
885 static void
initialize_defaults(struct symtab ** default_symtab,int * default_line)886 initialize_defaults (struct symtab **default_symtab, int *default_line)
887 {
888   if (*default_symtab == 0)
889     {
890       /* Use whatever we have for the default source line.  We don't use
891          get_current_or_default_symtab_and_line as it can recurse and call
892 	 us back! */
893       struct symtab_and_line cursal =
894 	get_current_source_symtab_and_line ();
895 
896       *default_symtab = cursal.symtab;
897       *default_line = cursal.line;
898     }
899 }
900 
901 static void
set_flags(char * arg,int * is_quoted,char ** paren_pointer)902 set_flags (char *arg, int *is_quoted, char **paren_pointer)
903 {
904   char *ii;
905   int has_if = 0;
906 
907   /* 'has_if' is for the syntax:
908         (gdb) break foo if (a==b)
909   */
910   if ((ii = strstr (arg, " if ")) != NULL ||
911       (ii = strstr (arg, "\tif ")) != NULL ||
912       (ii = strstr (arg, " if\t")) != NULL ||
913       (ii = strstr (arg, "\tif\t")) != NULL ||
914       (ii = strstr (arg, " if(")) != NULL ||
915       (ii = strstr (arg, "\tif( ")) != NULL)
916     has_if = 1;
917   /* Temporarily zap out "if (condition)" to not confuse the
918      parenthesis-checking code below.  This is undone below. Do not
919      change ii!!  */
920   if (has_if)
921     {
922       *ii = '\0';
923     }
924 
925   *is_quoted = (*arg
926 		&& strchr (get_gdb_completer_quote_characters (),
927 			   *arg) != NULL);
928 
929   *paren_pointer = strchr (arg, '(');
930   if (*paren_pointer != NULL)
931     *paren_pointer = strrchr (*paren_pointer, ')');
932 
933   /* Now that we're safely past the paren_pointer check, put back " if
934      (condition)" so outer layers can see it.  */
935   if (has_if)
936     *ii = ' ';
937 }
938 
939 
940 
941 /* Decode arg of the form *PC.  */
942 
943 static struct symtabs_and_lines
decode_indirect(char ** argptr)944 decode_indirect (char **argptr)
945 {
946   struct symtabs_and_lines values;
947   CORE_ADDR pc;
948 
949   (*argptr)++;
950   pc = parse_and_eval_address_1 (argptr);
951 
952   values.sals = (struct symtab_and_line *)
953     xmalloc (sizeof (struct symtab_and_line));
954 
955   values.nelts = 1;
956   values.sals[0] = find_pc_line (pc, 0);
957   values.sals[0].pc = pc;
958   values.sals[0].section = find_pc_overlay (pc);
959 
960   return values;
961 }
962 
963 
964 
965 /* Locate the first half of the linespec, ending in a colon, period,
966    or whitespace.  (More or less.)  Also, check to see if *ARGPTR is
967    enclosed in double quotes; if so, set is_quote_enclosed, advance
968    ARGPTR past that and zero out the trailing double quote.
969    If ARGPTR is just a simple name like "main", p will point to ""
970    at the end.  */
971 
972 static char *
locate_first_half(char ** argptr,int * is_quote_enclosed)973 locate_first_half (char **argptr, int *is_quote_enclosed)
974 {
975   char *ii;
976   char *p, *p1;
977   int has_comma;
978 
979   /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
980      and we must isolate the first half.  Outer layers will call again later
981      for the second half.
982 
983      Don't count commas that appear in argument lists of overloaded
984      functions, or in quoted strings.  It's stupid to go to this much
985      trouble when the rest of the function is such an obvious roach hotel.  */
986   ii = find_toplevel_char (*argptr, ',');
987   has_comma = (ii != 0);
988 
989   /* Temporarily zap out second half to not confuse the code below.
990      This is undone below. Do not change ii!!  */
991   if (has_comma)
992     {
993       *ii = '\0';
994     }
995 
996   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION.  May also be
997      CLASS::MEMBER, or NAMESPACE::NAME.  Look for ':', but ignore
998      inside of <>.  */
999 
1000   p = *argptr;
1001   if (p[0] == '"')
1002     {
1003       *is_quote_enclosed = 1;
1004       (*argptr)++;
1005       p++;
1006     }
1007   else
1008     *is_quote_enclosed = 0;
1009   for (; *p; p++)
1010     {
1011       if (p[0] == '<')
1012 	{
1013 	  char *temp_end = find_template_name_end (p);
1014 	  if (!temp_end)
1015 	    error (_("malformed template specification in command"));
1016 	  p = temp_end;
1017 	}
1018       /* Check for a colon and a plus or minus and a [ (which
1019          indicates an Objective-C method) */
1020       if (is_objc_method_format (p))
1021 	{
1022 	  break;
1023 	}
1024       /* Check for the end of the first half of the linespec.  End of
1025          line, a tab, a double colon or the last single colon, or a
1026          space.  But if enclosed in double quotes we do not break on
1027          enclosed spaces.  */
1028       if (!*p
1029 	  || p[0] == '\t'
1030 	  || ((p[0] == ':')
1031 	      && ((p[1] == ':') || (strchr (p + 1, ':') == NULL)))
1032 	  || ((p[0] == ' ') && !*is_quote_enclosed))
1033 	break;
1034       if (p[0] == '.' && strchr (p, ':') == NULL)
1035 	{
1036 	  /* Java qualified method.  Find the *last* '.', since the
1037 	     others are package qualifiers.  */
1038 	  for (p1 = p; *p1; p1++)
1039 	    {
1040 	      if (*p1 == '.')
1041 		p = p1;
1042 	    }
1043 	  break;
1044 	}
1045     }
1046   while (p[0] == ' ' || p[0] == '\t')
1047     p++;
1048 
1049   /* If the closing double quote was left at the end, remove it.  */
1050   if (*is_quote_enclosed)
1051     {
1052       char *closing_quote = strchr (p - 1, '"');
1053       if (closing_quote && closing_quote[1] == '\0')
1054 	*closing_quote = '\0';
1055     }
1056 
1057   /* Now that we've safely parsed the first half, put back ',' so
1058      outer layers can see it.  */
1059   if (has_comma)
1060     *ii = ',';
1061 
1062   return p;
1063 }
1064 
1065 
1066 
1067 /* Here's where we recognise an Objective-C Selector.  An Objective C
1068    selector may be implemented by more than one class, therefore it
1069    may represent more than one method/function.  This gives us a
1070    situation somewhat analogous to C++ overloading.  If there's more
1071    than one method that could represent the selector, then use some of
1072    the existing C++ code to let the user choose one.  */
1073 
1074 struct symtabs_and_lines
decode_objc(char ** argptr,int funfirstline,struct symtab * file_symtab,char *** canonical,char * saved_arg)1075 decode_objc (char **argptr, int funfirstline, struct symtab *file_symtab,
1076 	     char ***canonical, char *saved_arg)
1077 {
1078   struct symtabs_and_lines values;
1079   struct symbol **sym_arr = NULL;
1080   struct symbol *sym = NULL;
1081   char *copy = NULL;
1082   struct block *block = NULL;
1083   int i1 = 0;
1084   int i2 = 0;
1085 
1086   values.sals = NULL;
1087   values.nelts = 0;
1088 
1089   if (file_symtab != NULL)
1090     block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (file_symtab), STATIC_BLOCK);
1091   else
1092     block = get_selected_block (0);
1093 
1094   copy = find_imps (file_symtab, block, *argptr, NULL, &i1, &i2);
1095 
1096   if (i1 > 0)
1097     {
1098       sym_arr = (struct symbol **) alloca ((i1 + 1) * sizeof (struct symbol *));
1099       sym_arr[i1] = 0;
1100 
1101       copy = find_imps (file_symtab, block, *argptr, sym_arr, &i1, &i2);
1102       *argptr = copy;
1103     }
1104 
1105   /* i1 now represents the TOTAL number of matches found.
1106      i2 represents how many HIGH-LEVEL (struct symbol) matches,
1107      which will come first in the sym_arr array.  Any low-level
1108      (minimal_symbol) matches will follow those.  */
1109 
1110   if (i1 == 1)
1111     {
1112       if (i2 > 0)
1113 	{
1114 	  /* Already a struct symbol.  */
1115 	  sym = sym_arr[0];
1116 	}
1117       else
1118 	{
1119 	  sym = find_pc_function (SYMBOL_VALUE_ADDRESS (sym_arr[0]));
1120 	  if ((sym != NULL) && strcmp (SYMBOL_LINKAGE_NAME (sym_arr[0]), SYMBOL_LINKAGE_NAME (sym)) != 0)
1121 	    {
1122 	      warning (_("debugging symbol \"%s\" does not match selector; ignoring"), SYMBOL_LINKAGE_NAME (sym));
1123 	      sym = NULL;
1124 	    }
1125 	}
1126 
1127       values.sals = (struct symtab_and_line *) xmalloc (sizeof (struct symtab_and_line));
1128       values.nelts = 1;
1129 
1130       if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1131 	{
1132 	  /* Canonicalize this, so it remains resolved for dylib loads.  */
1133 	  values.sals[0] = find_function_start_sal (sym, funfirstline);
1134 	  build_canonical_line_spec (values.sals, SYMBOL_NATURAL_NAME (sym), canonical);
1135 	}
1136       else
1137 	{
1138 	  /* The only match was a non-debuggable symbol.  */
1139 	  values.sals[0].symtab = 0;
1140 	  values.sals[0].line = 0;
1141 	  values.sals[0].end = 0;
1142 	  values.sals[0].pc = SYMBOL_VALUE_ADDRESS (sym_arr[0]);
1143 	}
1144       return values;
1145     }
1146 
1147   if (i1 > 1)
1148     {
1149       /* More than one match. The user must choose one or more.  */
1150       return decode_line_2 (sym_arr, i2, funfirstline, canonical);
1151     }
1152 
1153   return values;
1154 }
1155 
1156 /* This handles C++ and Java compound data structures.  P should point
1157    at the first component separator, i.e. double-colon or period.  As
1158    an example, on entrance to this function we could have ARGPTR
1159    pointing to "AAA::inA::fun" and P pointing to "::inA::fun".  */
1160 
1161 static struct symtabs_and_lines
decode_compound(char ** argptr,int funfirstline,char *** canonical,char * saved_arg,char * p)1162 decode_compound (char **argptr, int funfirstline, char ***canonical,
1163 		 char *saved_arg, char *p)
1164 {
1165   struct symtabs_and_lines values;
1166   char *p2;
1167   char *saved_arg2 = *argptr;
1168   char *temp_end;
1169   struct symbol *sym;
1170   /* The symtab that SYM was found in.  */
1171   struct symtab *sym_symtab;
1172   char *copy;
1173   struct symbol *sym_class;
1174   struct symbol **sym_arr;
1175   struct type *t;
1176 
1177   /* First check for "global" namespace specification, of the form
1178      "::foo".  If found, skip over the colons and jump to normal
1179      symbol processing.  I.e. the whole line specification starts with
1180      "::" (note the condition that *argptr == p). */
1181   if (p[0] == ':'
1182       && ((*argptr == p) || (p[-1] == ' ') || (p[-1] == '\t')))
1183     saved_arg2 += 2;
1184 
1185   /* Given our example "AAA::inA::fun", we have two cases to consider:
1186 
1187      1) AAA::inA is the name of a class.  In that case, presumably it
1188         has a method called "fun"; we then look up that method using
1189         find_method.
1190 
1191      2) AAA::inA isn't the name of a class.  In that case, either the
1192         user made a typo or AAA::inA is the name of a namespace.
1193         Either way, we just look up AAA::inA::fun with lookup_symbol.
1194 
1195      Thus, our first task is to find everything before the last set of
1196      double-colons and figure out if it's the name of a class.  So we
1197      first loop through all of the double-colons.  */
1198 
1199   p2 = p;		/* Save for restart.  */
1200 
1201   /* This is very messy. Following the example above we have now the
1202      following pointers:
1203      p -> "::inA::fun"
1204      argptr -> "AAA::inA::fun
1205      saved_arg -> "AAA::inA::fun
1206      saved_arg2 -> "AAA::inA::fun
1207      p2 -> "::inA::fun". */
1208 
1209   /* In the loop below, with these strings, we'll make 2 passes, each
1210      is marked in comments.*/
1211 
1212   while (1)
1213     {
1214       /* Move pointer up to next possible class/namespace token.  */
1215 
1216       p = p2 + 1;	/* Restart with old value +1.  */
1217 
1218       /* PASS1: at this point p2->"::inA::fun", so p->":inA::fun",
1219 	 i.e. if there is a double-colon, p will now point to the
1220 	 second colon. */
1221       /* PASS2: p2->"::fun", p->":fun" */
1222 
1223       /* Move pointer ahead to next double-colon.  */
1224       while (*p && (p[0] != ' ') && (p[0] != '\t') && (p[0] != '\''))
1225 	{
1226 	  if (p[0] == '<')
1227 	    {
1228 	      temp_end = find_template_name_end (p);
1229 	      if (!temp_end)
1230 		error (_("malformed template specification in command"));
1231 	      p = temp_end;
1232 	    }
1233 	  /* Note that, since, at the start of this loop, p would be
1234 	     pointing to the second colon in a double-colon, we only
1235 	     satisfy the condition below if there is another
1236 	     double-colon to the right (after). I.e. there is another
1237 	     component that can be a class or a namespace. I.e, if at
1238 	     the beginning of this loop (PASS1), we had
1239 	     p->":inA::fun", we'll trigger this when p has been
1240 	     advanced to point to "::fun".  */
1241 	  /* PASS2: we will not trigger this. */
1242 	  else if ((p[0] == ':') && (p[1] == ':'))
1243 	    break;	/* Found double-colon.  */
1244 	  else
1245 	    /* PASS2: We'll keep getting here, until p->"", at which point
1246 	       we exit this loop.  */
1247 	    p++;
1248 	}
1249 
1250       if (*p != ':')
1251 	break;		/* Out of the while (1).  This would happen
1252 			   for instance if we have looked up
1253 			   unsuccessfully all the components of the
1254 			   string, and p->""(PASS2)  */
1255 
1256       /* We get here if p points to ' ', '\t', '\'', "::" or ""(i.e
1257 	 string ended). */
1258       /* Save restart for next time around.  */
1259       p2 = p;
1260       /* Restore argptr as it was on entry to this function.  */
1261       *argptr = saved_arg2;
1262       /* PASS1: at this point p->"::fun" argptr->"AAA::inA::fun",
1263 	 p2->"::fun".  */
1264 
1265       /* All ready for next pass through the loop.  */
1266     }			/* while (1) */
1267 
1268 
1269   /* Start of lookup in the symbol tables. */
1270 
1271   /* Lookup in the symbol table the substring between argptr and
1272      p. Note, this call changes the value of argptr.  */
1273   /* Before the call, argptr->"AAA::inA::fun",
1274      p->"", p2->"::fun".  After the call: argptr->"fun", p, p2
1275      unchanged.  */
1276   sym_class = lookup_prefix_sym (argptr, p2);
1277 
1278   /* If sym_class has been found, and if "AAA::inA" is a class, then
1279      we're in case 1 above.  So we look up "fun" as a method of that
1280      class.  */
1281   if (sym_class &&
1282       (t = check_typedef (SYMBOL_TYPE (sym_class)),
1283        (TYPE_CODE (t) == TYPE_CODE_STRUCT
1284 	|| TYPE_CODE (t) == TYPE_CODE_UNION)))
1285     {
1286       /* Arg token is not digits => try it as a function name.
1287 	 Find the next token (everything up to end or next
1288 	 blank).  */
1289       if (**argptr
1290 	  && strchr (get_gdb_completer_quote_characters (),
1291 		     **argptr) != NULL)
1292 	{
1293 	  p = skip_quoted (*argptr);
1294 	  *argptr = *argptr + 1;
1295 	}
1296       else
1297 	{
1298 	  /* At this point argptr->"fun".  */
1299 	  p = *argptr;
1300 	  while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p != ':')
1301 	    p++;
1302 	  /* At this point p->"".  String ended.  */
1303 	}
1304 
1305       /* Allocate our own copy of the substring between argptr and
1306 	 p. */
1307       copy = (char *) alloca (p - *argptr + 1);
1308       memcpy (copy, *argptr, p - *argptr);
1309       copy[p - *argptr] = '\0';
1310       if (p != *argptr
1311 	  && copy[p - *argptr - 1]
1312 	  && strchr (get_gdb_completer_quote_characters (),
1313 		     copy[p - *argptr - 1]) != NULL)
1314 	copy[p - *argptr - 1] = '\0';
1315 
1316       /* At this point copy->"fun", p->"" */
1317 
1318       /* No line number may be specified.  */
1319       while (*p == ' ' || *p == '\t')
1320 	p++;
1321       *argptr = p;
1322       /* At this point arptr->"".  */
1323 
1324       /* Look for copy as a method of sym_class. */
1325       /* At this point copy->"fun", sym_class is "AAA:inA",
1326 	 saved_arg->"AAA::inA::fun".  This concludes the scanning of
1327 	 the string for possible components matches.  If we find it
1328 	 here, we return. If not, and we are at the and of the string,
1329 	 we'll lookup the whole string in the symbol tables.  */
1330 
1331       return find_method (funfirstline, canonical, saved_arg,
1332 			  copy, t, sym_class);
1333 
1334     } /* End if symbol found */
1335 
1336 
1337   /* We couldn't find a class, so we're in case 2 above.  We check the
1338      entire name as a symbol instead.  */
1339 
1340   copy = (char *) alloca (p - saved_arg2 + 1);
1341   memcpy (copy, saved_arg2, p - saved_arg2);
1342   /* Note: if is_quoted should be true, we snuff out quote here
1343      anyway.  */
1344   copy[p - saved_arg2] = '\000';
1345   /* Set argptr to skip over the name.  */
1346   *argptr = (*p == '\'') ? p + 1 : p;
1347 
1348   /* Look up entire name */
1349   sym = lookup_symbol (copy, 0, VAR_DOMAIN, 0, &sym_symtab);
1350   if (sym)
1351     return symbol_found (funfirstline, canonical, copy, sym,
1352 			 NULL, sym_symtab);
1353 
1354   /* Couldn't find any interpretation as classes/namespaces, so give
1355      up.  The quotes are important if copy is empty.  */
1356   cplusplus_error (saved_arg,
1357 		   "Can't find member of namespace, class, struct, or union named \"%s\"\n",
1358 		   copy);
1359 }
1360 
1361 /* Next come some helper functions for decode_compound.  */
1362 
1363 /* Return the symbol corresponding to the substring of *ARGPTR ending
1364    at P, allowing whitespace.  Also, advance *ARGPTR past the symbol
1365    name in question, the compound object separator ("::" or "."), and
1366    whitespace.  Note that *ARGPTR is changed whether or not the
1367    lookup_symbol call finds anything (i.e we return NULL).  As an
1368    example, say ARGPTR is "AAA::inA::fun" and P is "::inA::fun".  */
1369 
1370 static struct symbol *
lookup_prefix_sym(char ** argptr,char * p)1371 lookup_prefix_sym (char **argptr, char *p)
1372 {
1373   char *p1;
1374   char *copy;
1375 
1376   /* Extract the class name.  */
1377   p1 = p;
1378   while (p != *argptr && p[-1] == ' ')
1379     --p;
1380   copy = (char *) alloca (p - *argptr + 1);
1381   memcpy (copy, *argptr, p - *argptr);
1382   copy[p - *argptr] = 0;
1383 
1384   /* Discard the class name from the argptr.  */
1385   p = p1 + (p1[0] == ':' ? 2 : 1);
1386   while (*p == ' ' || *p == '\t')
1387     p++;
1388   *argptr = p;
1389 
1390   /* At this point p1->"::inA::fun", p->"inA::fun" copy->"AAA",
1391      argptr->"inA::fun" */
1392 
1393   return lookup_symbol (copy, 0, STRUCT_DOMAIN, 0,
1394 			(struct symtab **) NULL);
1395 }
1396 
1397 /* This finds the method COPY in the class whose type is T and whose
1398    symbol is SYM_CLASS.  */
1399 
1400 static struct symtabs_and_lines
find_method(int funfirstline,char *** canonical,char * saved_arg,char * copy,struct type * t,struct symbol * sym_class)1401 find_method (int funfirstline, char ***canonical, char *saved_arg,
1402 	     char *copy, struct type *t, struct symbol *sym_class)
1403 {
1404   struct symtabs_and_lines values;
1405   struct symbol *sym = 0;
1406   int i1;	/*  Counter for the symbol array.  */
1407   struct symbol **sym_arr =  alloca (total_number_of_methods (t)
1408 				     * sizeof (struct symbol *));
1409 
1410   /* Find all methods with a matching name, and put them in
1411      sym_arr.  */
1412 
1413   i1 = collect_methods (copy, t, sym_arr);
1414 
1415   if (i1 == 1)
1416     {
1417       /* There is exactly one field with that name.  */
1418       sym = sym_arr[0];
1419 
1420       if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1421 	{
1422 	  values.sals = (struct symtab_and_line *)
1423 	    xmalloc (sizeof (struct symtab_and_line));
1424 	  values.nelts = 1;
1425 	  values.sals[0] = find_function_start_sal (sym,
1426 						    funfirstline);
1427 	}
1428       else
1429 	{
1430 	  values.nelts = 0;
1431 	}
1432       return values;
1433     }
1434   if (i1 > 0)
1435     {
1436       /* There is more than one field with that name
1437 	 (overloaded).  Ask the user which one to use.  */
1438       return decode_line_2 (sym_arr, i1, funfirstline, canonical);
1439     }
1440   else
1441     {
1442       char *tmp;
1443 
1444       if (is_operator_name (copy))
1445 	{
1446 	  tmp = (char *) alloca (strlen (copy + 3) + 9);
1447 	  strcpy (tmp, "operator ");
1448 	  strcat (tmp, copy + 3);
1449 	}
1450       else
1451 	tmp = copy;
1452       if (tmp[0] == '~')
1453 	cplusplus_error (saved_arg,
1454 			 "the class `%s' does not have destructor defined\n",
1455 			 SYMBOL_PRINT_NAME (sym_class));
1456       else
1457 	cplusplus_error (saved_arg,
1458 			 "the class %s does not have any method named %s\n",
1459 			 SYMBOL_PRINT_NAME (sym_class), tmp);
1460     }
1461 }
1462 
1463 /* Find all methods named COPY in the class whose type is T, and put
1464    them in SYM_ARR.  Return the number of methods found.  */
1465 
1466 static int
collect_methods(char * copy,struct type * t,struct symbol ** sym_arr)1467 collect_methods (char *copy, struct type *t,
1468 		 struct symbol **sym_arr)
1469 {
1470   int i1 = 0;	/*  Counter for the symbol array.  */
1471 
1472   if (destructor_name_p (copy, t))
1473     {
1474       /* Destructors are a special case.  */
1475       int m_index, f_index;
1476 
1477       if (get_destructor_fn_field (t, &m_index, &f_index))
1478 	{
1479 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (t, m_index);
1480 
1481 	  sym_arr[i1] =
1482 	    lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, f_index),
1483 			   NULL, VAR_DOMAIN, (int *) NULL,
1484 			   (struct symtab **) NULL);
1485 	  if (sym_arr[i1])
1486 	    i1++;
1487 	}
1488     }
1489   else
1490     i1 = find_methods (t, copy, sym_arr);
1491 
1492   return i1;
1493 }
1494 
1495 
1496 
1497 /* Return the symtab associated to the filename given by the substring
1498    of *ARGPTR ending at P, and advance ARGPTR past that filename.  If
1499    NOT_FOUND_PTR is not null and the source file is not found, store
1500    boolean true at the location pointed to and do not issue an
1501    error message.  */
1502 
1503 static struct symtab *
symtab_from_filename(char ** argptr,char * p,int is_quote_enclosed,int * not_found_ptr)1504 symtab_from_filename (char **argptr, char *p, int is_quote_enclosed,
1505 		      int *not_found_ptr)
1506 {
1507   char *p1;
1508   char *copy;
1509   struct symtab *file_symtab;
1510 
1511   p1 = p;
1512   while (p != *argptr && p[-1] == ' ')
1513     --p;
1514   if ((*p == '"') && is_quote_enclosed)
1515     --p;
1516   copy = (char *) alloca (p - *argptr + 1);
1517   memcpy (copy, *argptr, p - *argptr);
1518   /* It may have the ending quote right after the file name.  */
1519   if (is_quote_enclosed && copy[p - *argptr - 1] == '"')
1520     copy[p - *argptr - 1] = 0;
1521   else
1522     copy[p - *argptr] = 0;
1523 
1524   /* Find that file's data.  */
1525   file_symtab = lookup_symtab (copy);
1526   if (file_symtab == 0)
1527     {
1528       if (!have_full_symbols () && !have_partial_symbols ())
1529 	error (_("No symbol table is loaded.  Use the \"file\" command."));
1530       if (not_found_ptr)
1531 	*not_found_ptr = 1;
1532       throw_error (NOT_FOUND_ERROR, _("No source file named %s."), copy);
1533     }
1534 
1535   /* Discard the file name from the arg.  */
1536   p = p1 + 1;
1537   while (*p == ' ' || *p == '\t')
1538     p++;
1539   *argptr = p;
1540 
1541   return file_symtab;
1542 }
1543 
1544 
1545 
1546 /* This decodes a line where the argument is all digits (possibly
1547    preceded by a sign).  Q should point to the end of those digits;
1548    the other arguments are as usual.  */
1549 
1550 static struct symtabs_and_lines
decode_all_digits(char ** argptr,struct symtab * default_symtab,int default_line,char *** canonical,struct symtab * file_symtab,char * q)1551 decode_all_digits (char **argptr, struct symtab *default_symtab,
1552 		   int default_line, char ***canonical,
1553 		   struct symtab *file_symtab, char *q)
1554 
1555 {
1556   struct symtabs_and_lines values;
1557   struct symtab_and_line val;
1558 
1559   enum sign
1560     {
1561       none, plus, minus
1562     }
1563   sign = none;
1564 
1565   /* We might need a canonical line spec if no file was specified.  */
1566   int need_canonical = (file_symtab == 0) ? 1 : 0;
1567 
1568   init_sal (&val);
1569 
1570   /* This is where we need to make sure that we have good defaults.
1571      We must guarantee that this section of code is never executed
1572      when we are called with just a function name, since
1573      set_default_source_symtab_and_line uses
1574      select_source_symtab that calls us with such an argument.  */
1575 
1576   if (file_symtab == 0 && default_symtab == 0)
1577     {
1578       /* Make sure we have at least a default source file.  */
1579       set_default_source_symtab_and_line ();
1580       initialize_defaults (&default_symtab, &default_line);
1581     }
1582 
1583   if (**argptr == '+')
1584     sign = plus, (*argptr)++;
1585   else if (**argptr == '-')
1586     sign = minus, (*argptr)++;
1587   val.line = atoi (*argptr);
1588   switch (sign)
1589     {
1590     case plus:
1591       if (q == *argptr)
1592 	val.line = 5;
1593       if (file_symtab == 0)
1594 	val.line = default_line + val.line;
1595       break;
1596     case minus:
1597       if (q == *argptr)
1598 	val.line = 15;
1599       if (file_symtab == 0)
1600 	val.line = default_line - val.line;
1601       else
1602 	val.line = 1;
1603       break;
1604     case none:
1605       break;		/* No need to adjust val.line.  */
1606     }
1607 
1608   while (*q == ' ' || *q == '\t')
1609     q++;
1610   *argptr = q;
1611   if (file_symtab == 0)
1612     file_symtab = default_symtab;
1613 
1614   /* It is possible that this source file has more than one symtab,
1615      and that the new line number specification has moved us from the
1616      default (in file_symtab) to a new one.  */
1617   val.symtab = find_line_symtab (file_symtab, val.line, NULL, NULL);
1618   if (val.symtab == 0)
1619     val.symtab = file_symtab;
1620 
1621   val.pc = 0;
1622   values.sals = (struct symtab_and_line *)
1623     xmalloc (sizeof (struct symtab_and_line));
1624   values.sals[0] = val;
1625   values.nelts = 1;
1626   if (need_canonical)
1627     build_canonical_line_spec (values.sals, NULL, canonical);
1628   return values;
1629 }
1630 
1631 
1632 
1633 /* Decode a linespec starting with a dollar sign.  */
1634 
1635 static struct symtabs_and_lines
decode_dollar(char * copy,int funfirstline,struct symtab * default_symtab,char *** canonical,struct symtab * file_symtab)1636 decode_dollar (char *copy, int funfirstline, struct symtab *default_symtab,
1637 	       char ***canonical, struct symtab *file_symtab)
1638 {
1639   struct value *valx;
1640   int index = 0;
1641   int need_canonical = 0;
1642   struct symtabs_and_lines values;
1643   struct symtab_and_line val;
1644   char *p;
1645   struct symbol *sym;
1646   /* The symtab that SYM was found in.  */
1647   struct symtab *sym_symtab;
1648   struct minimal_symbol *msymbol;
1649 
1650   p = (copy[1] == '$') ? copy + 2 : copy + 1;
1651   while (*p >= '0' && *p <= '9')
1652     p++;
1653   if (!*p)		/* Reached end of token without hitting non-digit.  */
1654     {
1655       /* We have a value history reference.  */
1656       sscanf ((copy[1] == '$') ? copy + 2 : copy + 1, "%d", &index);
1657       valx = access_value_history ((copy[1] == '$') ? -index : index);
1658       if (TYPE_CODE (value_type (valx)) != TYPE_CODE_INT)
1659 	error (_("History values used in line specs must have integer values."));
1660     }
1661   else
1662     {
1663       /* Not all digits -- may be user variable/function or a
1664 	 convenience variable.  */
1665 
1666       /* Look up entire name as a symbol first.  */
1667       sym = lookup_symbol (copy, 0, VAR_DOMAIN, 0, &sym_symtab);
1668       file_symtab = (struct symtab *) 0;
1669       need_canonical = 1;
1670       /* Symbol was found --> jump to normal symbol processing.  */
1671       if (sym)
1672 	return symbol_found (funfirstline, canonical, copy, sym,
1673 			     NULL, sym_symtab);
1674 
1675       /* If symbol was not found, look in minimal symbol tables.  */
1676       msymbol = lookup_minimal_symbol (copy, NULL, NULL);
1677       /* Min symbol was found --> jump to minsym processing.  */
1678       if (msymbol)
1679 	return minsym_found (funfirstline, msymbol);
1680 
1681       /* Not a user variable or function -- must be convenience variable.  */
1682       need_canonical = (file_symtab == 0) ? 1 : 0;
1683       valx = value_of_internalvar (lookup_internalvar (copy + 1));
1684       if (TYPE_CODE (value_type (valx)) != TYPE_CODE_INT)
1685 	error (_("Convenience variables used in line specs must have integer values."));
1686     }
1687 
1688   init_sal (&val);
1689 
1690   /* Either history value or convenience value from above, in valx.  */
1691   val.symtab = file_symtab ? file_symtab : default_symtab;
1692   val.line = value_as_long (valx);
1693   val.pc = 0;
1694 
1695   values.sals = (struct symtab_and_line *) xmalloc (sizeof val);
1696   values.sals[0] = val;
1697   values.nelts = 1;
1698 
1699   if (need_canonical)
1700     build_canonical_line_spec (values.sals, NULL, canonical);
1701 
1702   return values;
1703 }
1704 
1705 
1706 
1707 /* Decode a linespec that's a variable.  If FILE_SYMTAB is non-NULL,
1708    look in that symtab's static variables first.  If NOT_FOUND_PTR is not NULL and
1709    the function cannot be found, store boolean true in the location pointed to
1710    and do not issue an error message.  */
1711 
1712 static struct symtabs_and_lines
decode_variable(char * copy,int funfirstline,char *** canonical,struct symtab * file_symtab,int * not_found_ptr)1713 decode_variable (char *copy, int funfirstline, char ***canonical,
1714 		 struct symtab *file_symtab, int *not_found_ptr)
1715 {
1716   struct symbol *sym;
1717   /* The symtab that SYM was found in.  */
1718   struct symtab *sym_symtab;
1719 
1720   struct minimal_symbol *msymbol;
1721 
1722   sym = lookup_symbol (copy,
1723 		       (file_symtab
1724 			? BLOCKVECTOR_BLOCK (BLOCKVECTOR (file_symtab),
1725 					     STATIC_BLOCK)
1726 			: get_selected_block (0)),
1727 		       VAR_DOMAIN, 0, &sym_symtab);
1728 
1729   if (sym != NULL)
1730     return symbol_found (funfirstline, canonical, copy, sym,
1731 			 file_symtab, sym_symtab);
1732 
1733   msymbol = lookup_minimal_symbol (copy, NULL, NULL);
1734 
1735   if (msymbol != NULL)
1736     return minsym_found (funfirstline, msymbol);
1737 
1738   if (!have_full_symbols () &&
1739       !have_partial_symbols () && !have_minimal_symbols ())
1740     error (_("No symbol table is loaded.  Use the \"file\" command."));
1741 
1742   if (not_found_ptr)
1743     *not_found_ptr = 1;
1744   throw_error (NOT_FOUND_ERROR, _("Function \"%s\" not defined."), copy);
1745 }
1746 
1747 
1748 
1749 
1750 /* Now come some functions that are called from multiple places within
1751    decode_line_1.  */
1752 
1753 /* We've found a symbol SYM to associate with our linespec; build a
1754    corresponding struct symtabs_and_lines.  */
1755 
1756 static struct symtabs_and_lines
symbol_found(int funfirstline,char *** canonical,char * copy,struct symbol * sym,struct symtab * file_symtab,struct symtab * sym_symtab)1757 symbol_found (int funfirstline, char ***canonical, char *copy,
1758 	      struct symbol *sym, struct symtab *file_symtab,
1759 	      struct symtab *sym_symtab)
1760 {
1761   struct symtabs_and_lines values;
1762 
1763   if (SYMBOL_CLASS (sym) == LOC_BLOCK)
1764     {
1765       /* Arg is the name of a function */
1766       values.sals = (struct symtab_and_line *)
1767 	xmalloc (sizeof (struct symtab_and_line));
1768       values.sals[0] = find_function_start_sal (sym, funfirstline);
1769       values.nelts = 1;
1770 
1771       /* Don't use the SYMBOL_LINE; if used at all it points to
1772 	 the line containing the parameters or thereabouts, not
1773 	 the first line of code.  */
1774 
1775       /* We might need a canonical line spec if it is a static
1776 	 function.  */
1777       if (file_symtab == 0)
1778 	{
1779 	  struct blockvector *bv = BLOCKVECTOR (sym_symtab);
1780 	  struct block *b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1781 	  if (lookup_block_symbol (b, copy, NULL, VAR_DOMAIN) != NULL)
1782 	    build_canonical_line_spec (values.sals, copy, canonical);
1783 	}
1784       return values;
1785     }
1786   else
1787     {
1788       if (funfirstline)
1789 	error (_("\"%s\" is not a function"), copy);
1790       else if (SYMBOL_LINE (sym) != 0)
1791 	{
1792 	  /* We know its line number.  */
1793 	  values.sals = (struct symtab_and_line *)
1794 	    xmalloc (sizeof (struct symtab_and_line));
1795 	  values.nelts = 1;
1796 	  memset (&values.sals[0], 0, sizeof (values.sals[0]));
1797 	  values.sals[0].symtab = sym_symtab;
1798 	  values.sals[0].line = SYMBOL_LINE (sym);
1799 	  return values;
1800 	}
1801       else
1802 	/* This can happen if it is compiled with a compiler which doesn't
1803 	   put out line numbers for variables.  */
1804 	/* FIXME: Shouldn't we just set .line and .symtab to zero
1805 	   and return?  For example, "info line foo" could print
1806 	   the address.  */
1807 	error (_("Line number not known for symbol \"%s\""), copy);
1808     }
1809 }
1810 
1811 /* We've found a minimal symbol MSYMBOL to associate with our
1812    linespec; build a corresponding struct symtabs_and_lines.  */
1813 
1814 static struct symtabs_and_lines
minsym_found(int funfirstline,struct minimal_symbol * msymbol)1815 minsym_found (int funfirstline, struct minimal_symbol *msymbol)
1816 {
1817   struct symtabs_and_lines values;
1818 
1819   values.sals = (struct symtab_and_line *)
1820     xmalloc (sizeof (struct symtab_and_line));
1821   values.sals[0] = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol),
1822 				      (struct bfd_section *) 0, 0);
1823   values.sals[0].section = SYMBOL_BFD_SECTION (msymbol);
1824   if (funfirstline)
1825     {
1826       values.sals[0].pc += DEPRECATED_FUNCTION_START_OFFSET;
1827       values.sals[0].pc = SKIP_PROLOGUE (values.sals[0].pc);
1828     }
1829   values.nelts = 1;
1830   return values;
1831 }
1832