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