do not call decode_compound with Ada expressions.
[platform/upstream/binutils.git] / gdb / linespec.c
1 /* Parser for linespec for the GNU debugger, GDB.
2
3    Copyright (C) 1986-2005, 2007-2012 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "frame.h"
23 #include "command.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "source.h"
27 #include "demangle.h"
28 #include "value.h"
29 #include "completer.h"
30 #include "cp-abi.h"
31 #include "cp-support.h"
32 #include "parser-defs.h"
33 #include "block.h"
34 #include "objc-lang.h"
35 #include "linespec.h"
36 #include "exceptions.h"
37 #include "language.h"
38 #include "interps.h"
39 #include "mi/mi-cmds.h"
40 #include "target.h"
41 #include "arch-utils.h"
42 #include <ctype.h>
43 #include "cli/cli-utils.h"
44 #include "filenames.h"
45 #include "ada-lang.h"
46
47 typedef struct symtab *symtab_p;
48 DEF_VEC_P (symtab_p);
49
50 typedef struct symbol *symbolp;
51 DEF_VEC_P (symbolp);
52
53 typedef struct type *typep;
54 DEF_VEC_P (typep);
55
56 /* An address entry is used to ensure that any given location is only
57    added to the result a single time.  It holds an address and the
58    program space from which the address came.  */
59
60 struct address_entry
61 {
62   struct program_space *pspace;
63   CORE_ADDR addr;
64 };
65
66 /* An instance of this is used to keep all state while linespec
67    operates.  This instance is passed around as a 'this' pointer to
68    the various implementation methods.  */
69
70 struct linespec_state
71 {
72   /* The program space as seen when the module was entered.  */
73   struct program_space *program_space;
74
75   /* The default symtab to use, if no other symtab is specified.  */
76   struct symtab *default_symtab;
77
78   /* The default line to use.  */
79   int default_line;
80
81   /* If the linespec started with "FILE:", this holds all the matching
82      symtabs.  Otherwise, it will hold a single NULL entry, meaning
83      that the default symtab should be used.  */
84   VEC (symtab_p) *file_symtabs;
85
86   /* If the linespec started with "FILE:", this holds an xmalloc'd
87      copy of "FILE".  */
88   char *user_filename;
89
90   /* If the linespec is "FUNCTION:LABEL", this holds an xmalloc'd copy
91      of "FUNCTION".  */
92   char *user_function;
93
94   /* The 'funfirstline' value that was passed in to decode_line_1 or
95      decode_line_full.  */
96   int funfirstline;
97
98   /* Nonzero if we are running in 'list' mode; see decode_line_list.  */
99   int list_mode;
100
101   /* The 'canonical' value passed to decode_line_full, or NULL.  */
102   struct linespec_result *canonical;
103
104   /* Canonical strings that mirror the symtabs_and_lines result.  */
105   char **canonical_names;
106
107   /* This is a set of address_entry objects which is used to prevent
108      duplicate symbols from being entered into the result.  */
109   htab_t addr_set;
110 };
111
112 /* This is a helper object that is used when collecting symbols into a
113    result.  */
114
115 struct collect_info
116 {
117   /* The linespec object in use.  */
118   struct linespec_state *state;
119
120   /* The result being accumulated.  */
121   struct symtabs_and_lines result;
122 };
123
124 /* Prototypes for local functions.  */
125
126 static void initialize_defaults (struct symtab **default_symtab,
127                                  int *default_line);
128
129 static struct symtabs_and_lines decode_indirect (struct linespec_state *self,
130                                                  char **argptr);
131
132 static char *locate_first_half (char **argptr, int *is_quote_enclosed);
133
134 static struct symtabs_and_lines decode_objc (struct linespec_state *self,
135                                              char **argptr);
136
137 static struct symtabs_and_lines decode_compound (struct linespec_state *self,
138                                                  char **argptr,
139                                                  char *saved_arg,
140                                                  char *p);
141
142 static VEC (symbolp) *lookup_prefix_sym (char **argptr, char *p,
143                                          VEC (symtab_p) *,
144                                          char **);
145
146 static struct symtabs_and_lines find_method (struct linespec_state *self,
147                                              char *saved_arg,
148                                              char *copy,
149                                              const char *class_name,
150                                              VEC (symbolp) *sym_classes);
151
152 static void cplusplus_error (const char *name, const char *fmt, ...)
153      ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 3);
154
155 static char *find_toplevel_char (char *s, char c);
156
157 static int is_objc_method_format (const char *s);
158
159 static VEC (symtab_p) *symtabs_from_filename (char **argptr,
160                                               char *p, int is_quote_enclosed,
161                                               char **user_filename);
162
163 static VEC (symbolp) *find_function_symbols (char **argptr, char *p,
164                                              int is_quote_enclosed,
165                                              char **user_function);
166
167 static struct symtabs_and_lines decode_all_digits (struct linespec_state *self,
168                                                    char **argptr,
169                                                    char *q);
170
171 static struct symtabs_and_lines decode_dollar (struct linespec_state *self,
172                                                char *copy);
173
174 static int decode_label (struct linespec_state *self,
175                          VEC (symbolp) *function_symbols,
176                          char *copy,
177                          struct symtabs_and_lines *result);
178
179 static struct symtabs_and_lines decode_variable (struct linespec_state *self,
180                                                  char *copy);
181
182 static int symbol_to_sal (struct symtab_and_line *result,
183                           int funfirstline, struct symbol *sym);
184
185 static void add_matching_symbols_to_info (const char *name,
186                                           struct collect_info *info,
187                                           struct program_space *pspace);
188
189 static void add_all_symbol_names_from_pspace (struct collect_info *info,
190                                               struct program_space *pspace,
191                                               VEC (const_char_ptr) *names);
192
193 /* Helper functions.  */
194
195 /* Add SAL to SALS.  */
196
197 static void
198 add_sal_to_sals_basic (struct symtabs_and_lines *sals,
199                        struct symtab_and_line *sal)
200 {
201   ++sals->nelts;
202   sals->sals = xrealloc (sals->sals, sals->nelts * sizeof (sals->sals[0]));
203   sals->sals[sals->nelts - 1] = *sal;
204 }
205
206 /* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
207    the new sal, if needed.  If not NULL, SYMNAME is the name of the
208    symbol to use when constructing the new canonical name.  */
209
210 static void
211 add_sal_to_sals (struct linespec_state *self,
212                  struct symtabs_and_lines *sals,
213                  struct symtab_and_line *sal,
214                  const char *symname)
215 {
216   add_sal_to_sals_basic (sals, sal);
217
218   if (self->canonical)
219     {
220       char *canonical_name = NULL;
221
222       self->canonical_names = xrealloc (self->canonical_names,
223                                         sals->nelts * sizeof (char *));
224       if (sal->symtab && sal->symtab->filename)
225         {
226           char *filename = sal->symtab->filename;
227
228           /* Note that the filter doesn't have to be a valid linespec
229              input.  We only apply the ":LINE" treatment to Ada for
230              the time being.  */
231           if (symname != NULL && sal->line != 0
232               && current_language->la_language == language_ada)
233             canonical_name = xstrprintf ("%s:%s:%d", filename, symname,
234                                          sal->line);
235           else if (symname != NULL)
236             canonical_name = xstrprintf ("%s:%s", filename, symname);
237           else
238             canonical_name = xstrprintf ("%s:%d", filename, sal->line);
239         }
240
241       self->canonical_names[sals->nelts - 1] = canonical_name;
242     }
243 }
244
245 /* A hash function for address_entry.  */
246
247 static hashval_t
248 hash_address_entry (const void *p)
249 {
250   const struct address_entry *aep = p;
251   hashval_t hash;
252
253   hash = iterative_hash_object (aep->pspace, 0);
254   return iterative_hash_object (aep->addr, hash);
255 }
256
257 /* An equality function for address_entry.  */
258
259 static int
260 eq_address_entry (const void *a, const void *b)
261 {
262   const struct address_entry *aea = a;
263   const struct address_entry *aeb = b;
264
265   return aea->pspace == aeb->pspace && aea->addr == aeb->addr;
266 }
267
268 /* Check whether the address, represented by PSPACE and ADDR, is
269    already in the set.  If so, return 0.  Otherwise, add it and return
270    1.  */
271
272 static int
273 maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr)
274 {
275   struct address_entry e, *p;
276   void **slot;
277
278   e.pspace = pspace;
279   e.addr = addr;
280   slot = htab_find_slot (set, &e, INSERT);
281   if (*slot)
282     return 0;
283
284   p = XNEW (struct address_entry);
285   memcpy (p, &e, sizeof (struct address_entry));
286   *slot = p;
287
288   return 1;
289 }
290
291 /* Issue a helpful hint on using the command completion feature on
292    single quoted demangled C++ symbols as part of the completion
293    error.  */
294
295 static void
296 cplusplus_error (const char *name, const char *fmt, ...)
297 {
298   struct ui_file *tmp_stream;
299   char *message;
300
301   tmp_stream = mem_fileopen ();
302   make_cleanup_ui_file_delete (tmp_stream);
303
304   {
305     va_list args;
306
307     va_start (args, fmt);
308     vfprintf_unfiltered (tmp_stream, fmt, args);
309     va_end (args);
310   }
311
312   while (*name == '\'')
313     name++;
314   fprintf_unfiltered (tmp_stream,
315                       ("Hint: try '%s<TAB> or '%s<ESC-?>\n"
316                        "(Note leading single quote.)"),
317                       name, name);
318
319   message = ui_file_xstrdup (tmp_stream, NULL);
320   make_cleanup (xfree, message);
321   throw_error (NOT_FOUND_ERROR, "%s", message);
322 }
323
324 /* A helper for iterate_over_all_matching_symtabs that is passed as a
325    callback to the expand_symtabs_matching method.  */
326
327 static int
328 iterate_name_matcher (const struct language_defn *language,
329                       const char *name, void *d)
330 {
331   const char **dname = d;
332
333   if (language->la_symbol_name_compare (name, *dname) == 0)
334     return 1;
335   return 0;
336 }
337
338 /* A helper that walks over all matching symtabs in all objfiles and
339    calls CALLBACK for each symbol matching NAME.  If SEARCH_PSPACE is
340    not NULL, then the search is restricted to just that program
341    space.  */
342
343 static void
344 iterate_over_all_matching_symtabs (const char *name,
345                                    const domain_enum domain,
346                                    int (*callback) (struct symbol *, void *),
347                                    void *data,
348                                    struct program_space *search_pspace)
349 {
350   struct objfile *objfile;
351   struct program_space *pspace;
352
353   ALL_PSPACES (pspace)
354   {
355     if (search_pspace != NULL && search_pspace != pspace)
356       continue;
357     if (pspace->executing_startup)
358       continue;
359
360     set_current_program_space (pspace);
361
362     ALL_OBJFILES (objfile)
363     {
364       struct symtab *symtab;
365
366       if (objfile->sf)
367         objfile->sf->qf->expand_symtabs_matching (objfile, NULL,
368                                                   iterate_name_matcher,
369                                                   ALL_DOMAIN,
370                                                   &name);
371
372       ALL_OBJFILE_SYMTABS (objfile, symtab)
373         {
374           if (symtab->primary)
375             {
376               struct block *block;
377
378               block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
379               LA_ITERATE_OVER_SYMBOLS (block, name, domain, callback, data);
380             }
381         }
382     }
383   }
384 }
385
386 /* Returns the block to be used for symbol searches for the given SYMTAB,
387    which may be NULL.  */
388
389 static struct block *
390 get_search_block (struct symtab *symtab)
391 {
392   struct block *block;
393
394   if (symtab != NULL)
395     block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
396   else
397     {
398       enum language save_language;
399
400       /* get_selected_block can change the current language when there is
401          no selected frame yet.  */
402       save_language = current_language->la_language;
403       block = get_selected_block (0);
404       set_language (save_language);
405     }
406
407   return block;
408 }
409
410 /* A helper for find_method.  This finds all methods in type T which
411    match NAME.  It adds resulting symbol names to RESULT_NAMES, and
412    adds T's direct superclasses to SUPERCLASSES.  */
413
414 static void
415 find_methods (struct type *t, const char *name,
416               VEC (const_char_ptr) **result_names,
417               VEC (typep) **superclasses)
418 {
419   int i1 = 0;
420   int ibase;
421   char *class_name = type_name_no_tag (t);
422   char *canon;
423
424   /* Ignore this class if it doesn't have a name.  This is ugly, but
425      unless we figure out how to get the physname without the name of
426      the class, then the loop can't do any good.  */
427   if (class_name)
428     {
429       int method_counter;
430       int name_len = strlen (name);
431
432       CHECK_TYPEDEF (t);
433
434       /* Loop over each method name.  At this level, all overloads of a name
435          are counted as a single name.  There is an inner loop which loops over
436          each overload.  */
437
438       for (method_counter = TYPE_NFN_FIELDS (t) - 1;
439            method_counter >= 0;
440            --method_counter)
441         {
442           char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
443           char dem_opname[64];
444
445           if (strncmp (method_name, "__", 2) == 0 ||
446               strncmp (method_name, "op", 2) == 0 ||
447               strncmp (method_name, "type", 4) == 0)
448             {
449               if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
450                 method_name = dem_opname;
451               else if (cplus_demangle_opname (method_name, dem_opname, 0))
452                 method_name = dem_opname;
453             }
454
455           if (strcmp_iw (method_name, name) == 0)
456             {
457               int field_counter;
458
459               for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
460                                     - 1);
461                    field_counter >= 0;
462                    --field_counter)
463                 {
464                   struct fn_field *f;
465                   const char *phys_name;
466
467                   f = TYPE_FN_FIELDLIST1 (t, method_counter);
468                   if (TYPE_FN_FIELD_STUB (f, field_counter))
469                     continue;
470                   phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
471                   VEC_safe_push (const_char_ptr, *result_names, phys_name);
472                 }
473             }
474         }
475     }
476
477   for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
478     VEC_safe_push (typep, *superclasses, TYPE_BASECLASS (t, ibase));
479 }
480
481 /* Find an instance of the character C in the string S that is outside
482    of all parenthesis pairs, single-quoted strings, and double-quoted
483    strings.  Also, ignore the char within a template name, like a ','
484    within foo<int, int>.  */
485
486 static char *
487 find_toplevel_char (char *s, char c)
488 {
489   int quoted = 0;               /* zero if we're not in quotes;
490                                    '"' if we're in a double-quoted string;
491                                    '\'' if we're in a single-quoted string.  */
492   int depth = 0;                /* Number of unclosed parens we've seen.  */
493   char *scan;
494
495   for (scan = s; *scan; scan++)
496     {
497       if (quoted)
498         {
499           if (*scan == quoted)
500             quoted = 0;
501           else if (*scan == '\\' && *(scan + 1))
502             scan++;
503         }
504       else if (*scan == c && ! quoted && depth == 0)
505         return scan;
506       else if (*scan == '"' || *scan == '\'')
507         quoted = *scan;
508       else if (*scan == '(' || *scan == '<')
509         depth++;
510       else if ((*scan == ')' || *scan == '>') && depth > 0)
511         depth--;
512     }
513
514   return 0;
515 }
516
517 /* Determines if the gives string corresponds to an Objective-C method
518    representation, such as -[Foo bar:] or +[Foo bar].  Objective-C symbols
519    are allowed to have spaces and parentheses in them.  */
520
521 static int 
522 is_objc_method_format (const char *s)
523 {
524   if (s == NULL || *s == '\0')
525     return 0;
526   /* Handle arguments with the format FILENAME:SYMBOL.  */
527   if ((s[0] == ':') && (strchr ("+-", s[1]) != NULL) 
528       && (s[2] == '[') && strchr(s, ']'))
529     return 1;
530   /* Handle arguments that are just SYMBOL.  */
531   else if ((strchr ("+-", s[0]) != NULL) && (s[1] == '[') && strchr(s, ']'))
532     return 1;
533   return 0;
534 }
535
536 /* Given FILTERS, a list of canonical names, filter the sals in RESULT
537    and store the result in SELF->CANONICAL.  */
538
539 static void
540 filter_results (struct linespec_state *self,
541                 struct symtabs_and_lines *result,
542                 VEC (const_char_ptr) *filters)
543 {
544   int i;
545   const char *name;
546
547   for (i = 0; VEC_iterate (const_char_ptr, filters, i, name); ++i)
548     {
549       struct linespec_sals lsal;
550       int j;
551
552       memset (&lsal, 0, sizeof (lsal));
553
554       for (j = 0; j < result->nelts; ++j)
555         {
556           if (strcmp (name, self->canonical_names[j]) == 0)
557             add_sal_to_sals_basic (&lsal.sals, &result->sals[j]);
558         }
559
560       if (lsal.sals.nelts > 0)
561         {
562           lsal.canonical = xstrdup (name);
563           VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
564         }
565     }
566
567   self->canonical->pre_expanded = 0;
568 }
569
570 /* Store RESULT into SELF->CANONICAL.  */
571
572 static void
573 convert_results_to_lsals (struct linespec_state *self,
574                           struct symtabs_and_lines *result)
575 {
576   struct linespec_sals lsal;
577
578   lsal.canonical = NULL;
579   lsal.sals = *result;
580   VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
581 }
582
583 /* Handle multiple results in RESULT depending on SELECT_MODE.  This
584    will either return normally, throw an exception on multiple
585    results, or present a menu to the user.  On return, the SALS vector
586    in SELF->CANONICAL is set up properly.  */
587
588 static void
589 decode_line_2 (struct linespec_state *self,
590                struct symtabs_and_lines *result,
591                const char *select_mode)
592 {
593   const char *iter;
594   char *args, *prompt;
595   int i;
596   struct cleanup *old_chain;
597   VEC (const_char_ptr) *item_names = NULL, *filters = NULL;
598   struct get_number_or_range_state state;
599
600   gdb_assert (select_mode != multiple_symbols_all);
601   gdb_assert (self->canonical != NULL);
602
603   old_chain = make_cleanup (VEC_cleanup (const_char_ptr), &item_names);
604   make_cleanup (VEC_cleanup (const_char_ptr), &filters);
605   for (i = 0; i < result->nelts; ++i)
606     {
607       int j, found = 0;
608       const char *iter;
609
610       gdb_assert (self->canonical_names[i] != NULL);
611       for (j = 0; VEC_iterate (const_char_ptr, item_names, j, iter); ++j)
612         {
613           if (strcmp (iter, self->canonical_names[i]) == 0)
614             {
615               found = 1;
616               break;
617             }
618         }
619
620       if (!found)
621         VEC_safe_push (const_char_ptr, item_names, self->canonical_names[i]);
622     }
623
624   if (select_mode == multiple_symbols_cancel
625       && VEC_length (const_char_ptr, item_names) > 1)
626     error (_("canceled because the command is ambiguous\n"
627              "See set/show multiple-symbol."));
628   
629   if (select_mode == multiple_symbols_all
630       || VEC_length (const_char_ptr, item_names) == 1)
631     {
632       do_cleanups (old_chain);
633       convert_results_to_lsals (self, result);
634       return;
635     }
636
637   printf_unfiltered (_("[0] cancel\n[1] all\n"));
638   for (i = 0; VEC_iterate (const_char_ptr, item_names, i, iter); ++i)
639     printf_unfiltered ("[%d] %s\n", i + 2, iter);
640
641   prompt = getenv ("PS2");
642   if (prompt == NULL)
643     {
644       prompt = "> ";
645     }
646   args = command_line_input (prompt, 0, "overload-choice");
647
648   if (args == 0 || *args == 0)
649     error_no_arg (_("one or more choice numbers"));
650
651   init_number_or_range (&state, args);
652   while (!state.finished)
653     {
654       int num;
655
656       num = get_number_or_range (&state);
657
658       if (num == 0)
659         error (_("canceled"));
660       else if (num == 1)
661         {
662           /* We intentionally make this result in a single breakpoint,
663              contrary to what older versions of gdb did.  The
664              rationale is that this lets a user get the
665              multiple_symbols_all behavior even with the 'ask'
666              setting; and he can get separate breakpoints by entering
667              "2-57" at the query.  */
668           do_cleanups (old_chain);
669           convert_results_to_lsals (self, result);
670           return;
671         }
672
673       num -= 2;
674       if (num >= VEC_length (const_char_ptr, item_names))
675         printf_unfiltered (_("No choice number %d.\n"), num);
676       else
677         {
678           const char *elt = VEC_index (const_char_ptr, item_names, num);
679
680           if (elt != NULL)
681             {
682               VEC_safe_push (const_char_ptr, filters, elt);
683               VEC_replace (const_char_ptr, item_names, num, NULL);
684             }
685           else
686             {
687               printf_unfiltered (_("duplicate request for %d ignored.\n"),
688                                  num);
689             }
690         }
691     }
692
693   filter_results (self, result, filters);
694   do_cleanups (old_chain);
695 }
696
697 /* Valid delimiters for linespec keywords "if", "thread" or "task".  */
698
699 static int
700 is_linespec_boundary (char c)
701 {
702   return c == ' ' || c == '\t' || c == '\0' || c == ',';
703 }
704
705 /* A helper function for decode_line_1 and friends which skips P
706    past any method overload information at the beginning of P, e.g.,
707    "(const struct foo *)".
708
709    This function assumes that P has already been validated to contain
710    overload information, and it will assert if *P != '('.  */
711 static char *
712 find_method_overload_end (char *p)
713 {
714   int depth = 0;
715
716   gdb_assert (*p == '(');
717
718   while (*p)
719     {
720       if (*p == '(')
721         ++depth;
722       else if (*p == ')')
723         {
724           if (--depth == 0)
725             {
726               ++p;
727               break;
728             }
729         }
730       ++p;
731     }
732
733   return p;
734 }
735
736 /* Keep important information used when looking up a name.  This includes
737    template parameters, overload information, and important keywords, including
738    the possible Java trailing type.  */
739
740 static char *
741 keep_name_info (char *p, int on_boundary)
742 {
743   const char *quotes = get_gdb_completer_quote_characters ();
744   char *saved_p = p;
745   int nest = 0;
746
747   while (*p)
748     {
749       if (strchr (quotes, *p))
750         break;
751
752       if (*p == ',' && !nest)
753         break;
754
755       if (on_boundary && !nest)
756         {
757           const char *const words[] = { "if", "thread", "task" };
758           int wordi;
759
760           for (wordi = 0; wordi < ARRAY_SIZE (words); wordi++)
761             if (strncmp (p, words[wordi], strlen (words[wordi])) == 0
762                 && is_linespec_boundary (p[strlen (words[wordi])]))
763               break;
764           if (wordi < ARRAY_SIZE (words))
765             break;
766         }
767
768       if (*p == '(' || *p == '<' || *p == '[')
769         nest++;
770       else if ((*p == ')' || *p == '>' || *p == ']') && nest > 0)
771         nest--;
772
773       p++;
774
775       /* The ',' check could fail on "operator ,".  */
776       p += cp_validate_operator (p);
777
778       on_boundary = is_linespec_boundary (p[-1]);
779     }
780
781   while (p > saved_p && is_linespec_boundary (p[-1]))
782     p--;
783
784   return p;
785 }
786
787 \f
788 /* The parser of linespec itself.  */
789
790 /* Parse a string that specifies a line number.
791    Pass the address of a char * variable; that variable will be
792    advanced over the characters actually parsed.
793
794    The string can be:
795
796    LINENUM -- that line number in current file.  PC returned is 0.
797    FILE:LINENUM -- that line in that file.  PC returned is 0.
798    FUNCTION -- line number of openbrace of that function.
799    PC returned is the start of the function.
800    LABEL -- a label in the current scope
801    VARIABLE -- line number of definition of that variable.
802    PC returned is 0.
803    FILE:FUNCTION -- likewise, but prefer functions in that file.
804    *EXPR -- line in which address EXPR appears.
805
806    This may all be followed by an "if EXPR", which we ignore.
807
808    FUNCTION may be an undebuggable function found in minimal symbol table.
809
810    If the argument FUNFIRSTLINE is nonzero, we want the first line
811    of real code inside a function when a function is specified, and it is
812    not OK to specify a variable or type to get its line number.
813
814    DEFAULT_SYMTAB specifies the file to use if none is specified.
815    It defaults to current_source_symtab.
816    DEFAULT_LINE specifies the line number to use for relative
817    line numbers (that start with signs).  Defaults to current_source_line.
818    If CANONICAL is non-NULL, store an array of strings containing the canonical
819    line specs there if necessary.  Currently overloaded member functions and
820    line numbers or static functions without a filename yield a canonical
821    line spec.  The array and the line spec strings are allocated on the heap,
822    it is the callers responsibility to free them.
823
824    Note that it is possible to return zero for the symtab
825    if no file is validly specified.  Callers must check that.
826    Also, the line number returned may be invalid.  */
827
828 /* We allow single quotes in various places.  This is a hideous
829    kludge, which exists because the completer can't yet deal with the
830    lack of single quotes.  FIXME: write a linespec_completer which we
831    can use as appropriate instead of make_symbol_completion_list.  */
832
833 static struct symtabs_and_lines
834 decode_line_internal (struct linespec_state *self, char **argptr)
835 {
836   char *p;
837   char *q;
838
839   char *copy;
840   /* This says whether or not something in *ARGPTR is quoted with
841      completer_quotes (i.e. with single quotes).  */
842   int is_quoted;
843   /* Is *ARGPTR enclosed in double quotes?  */
844   int is_quote_enclosed;
845   int is_objc_method = 0;
846   char *saved_arg = *argptr;
847   /* If IS_QUOTED, the end of the quoted bit.  */
848   char *end_quote = NULL;
849   /* Is *ARGPTR enclosed in single quotes?  */
850   int is_squote_enclosed = 0;
851   /* The "first half" of the linespec.  */
852   char *first_half;
853
854   /* If we are parsing `function:label', this holds the symbols
855      matching the function name.  */
856   VEC (symbolp) *function_symbols = NULL;
857   /* If FUNCTION_SYMBOLS is not NULL, then this is the exception that
858      was thrown when trying to parse a filename.  */
859   volatile struct gdb_exception file_exception;
860
861   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
862
863   /* Defaults have defaults.  */
864
865   initialize_defaults (&self->default_symtab, &self->default_line);
866   
867   /* See if arg is *PC.  */
868
869   if (**argptr == '*')
870     {
871       do_cleanups (cleanup);
872       return decode_indirect (self, argptr);
873     }
874
875   is_quoted = (strchr (get_gdb_completer_quote_characters (),
876                        **argptr) != NULL);
877
878   if (is_quoted)
879     {
880       end_quote = skip_quoted (*argptr);
881       if (*end_quote == '\0')
882         is_squote_enclosed = 1;
883     }
884
885   /* Check to see if it's a multipart linespec (with colons or
886      periods).  */
887
888   /* Locate the end of the first half of the linespec.
889      After the call, for instance, if the argptr string is "foo.c:123"
890      p will point at "123".  If there is only one part, like "foo", p
891      will point to "".  If this is a C++ name, like "A::B::foo", p will
892      point to "::B::foo".  Argptr is not changed by this call.  */
893
894   first_half = p = locate_first_half (argptr, &is_quote_enclosed);
895
896   /* First things first: if ARGPTR starts with a filename, get its
897      symtab and strip the filename from ARGPTR.  */
898   TRY_CATCH (file_exception, RETURN_MASK_ERROR)
899     {
900       self->file_symtabs = symtabs_from_filename (argptr, p, is_quote_enclosed,
901                                                   &self->user_filename);
902     }
903
904   if (VEC_empty (symtab_p, self->file_symtabs))
905     {
906       /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB.  */
907       VEC_safe_push (symtab_p, self->file_symtabs, NULL);
908     }
909
910   if (file_exception.reason >= 0)
911     {
912       /* Check for single quotes on the non-filename part.  */
913       is_quoted = (**argptr
914                    && strchr (get_gdb_completer_quote_characters (),
915                               **argptr) != NULL);
916       if (is_quoted)
917         end_quote = skip_quoted (*argptr);
918
919       /* Locate the next "half" of the linespec.  */
920       first_half = p = locate_first_half (argptr, &is_quote_enclosed);
921     }
922
923   /* Check if this is an Objective-C method (anything that starts with
924      a '+' or '-' and a '[').  */
925   if (is_objc_method_format (p))
926     is_objc_method = 1;
927
928   /* Check if the symbol could be an Objective-C selector.  */
929
930   {
931     struct symtabs_and_lines values;
932
933     values = decode_objc (self, argptr);
934     if (values.sals != NULL)
935       {
936         do_cleanups (cleanup);
937         return values;
938       }
939   }
940
941   /* Does it look like there actually were two parts?  */
942
943   if (p[0] == ':' || p[0] == '.')
944     {
945       /* Is it a C++ or Java compound data structure?
946          The check on p[1] == ':' is capturing the case of "::",
947          since p[0]==':' was checked above.
948          Note that the call to decode_compound does everything
949          for us, including the lookup on the symbol table, so we
950          can return now.  */
951         
952       if (p[0] == '.' || p[1] == ':')
953         {
954          /* We only perform this check for the languages where it might
955             make sense.  For instance, Ada does not use this type of
956             syntax, and trying to apply this logic on an Ada linespec
957             may trigger a spurious error (for instance, decode_compound
958             does not like expressions such as `ops."<"', which is a
959             valid function name in Ada).  */
960           if (current_language->la_language == language_c
961               || current_language->la_language == language_cplus
962               || current_language->la_language == language_java)
963             {
964               struct symtabs_and_lines values;
965               volatile struct gdb_exception ex;
966               char *saved_argptr = *argptr;
967
968               if (is_quote_enclosed)
969                 ++saved_arg;
970
971               /* Initialize it just to avoid a GCC false warning.  */
972               memset (&values, 0, sizeof (values));
973
974               TRY_CATCH (ex, RETURN_MASK_ERROR)
975                 {
976                   values = decode_compound (self, argptr, saved_arg, p);
977                 }
978               if ((is_quoted || is_squote_enclosed) && **argptr == '\'')
979                 *argptr = *argptr + 1;
980
981               if (ex.reason >= 0)
982                 {
983                   do_cleanups (cleanup);
984                   return values;
985                 }
986
987               if (ex.error != NOT_FOUND_ERROR)
988                 throw_exception (ex);
989
990               *argptr = saved_argptr;
991             }
992         }
993       else
994         {
995           /* If there was an exception looking up a specified filename earlier,
996              then check whether we were really given `function:label'.   */
997           if (file_exception.reason < 0)
998             {
999               function_symbols = find_function_symbols (argptr, p,
1000                                                         is_quote_enclosed,
1001                                                         &self->user_function);
1002
1003               /* If we did not find a function, re-throw the original
1004                  exception.  */
1005               if (!function_symbols)
1006                 throw_exception (file_exception);
1007
1008               make_cleanup (VEC_cleanup (symbolp), &function_symbols);
1009             }
1010
1011           /* Check for single quotes on the non-filename part.  */
1012           if (!is_quoted)
1013             {
1014               is_quoted = (**argptr
1015                            && strchr (get_gdb_completer_quote_characters (),
1016                                       **argptr) != NULL);
1017               if (is_quoted)
1018                 end_quote = skip_quoted (*argptr);
1019             }
1020         }
1021     }
1022
1023   /* self->file_symtabs holds the  specified file symtabs, or 0 if no file
1024      specified.
1025      If we are parsing `function:symbol', then FUNCTION_SYMBOLS holds the
1026      functions before the `:'.
1027      arg no longer contains the file name.  */
1028
1029   /* If the filename was quoted, we must re-check the quotation.  */
1030
1031   if (end_quote == first_half && *end_quote!= '\0')
1032     {
1033       is_quoted = (**argptr
1034                    && strchr (get_gdb_completer_quote_characters (),
1035                               **argptr) != NULL);
1036       if (is_quoted)
1037         end_quote = skip_quoted (*argptr);
1038     }
1039
1040   /* Check whether arg is all digits (and sign).  */
1041
1042   q = *argptr;
1043   if (*q == '-' || *q == '+')
1044     q++;
1045   while (*q >= '0' && *q <= '9')
1046     q++;
1047
1048   if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ',')
1049       && function_symbols == NULL)
1050     {
1051       struct symtabs_and_lines values;
1052
1053       /* We found a token consisting of all digits -- at least one digit.  */
1054       values = decode_all_digits (self, argptr, q);
1055       do_cleanups (cleanup);
1056       return values;
1057     }
1058
1059   /* Arg token is not digits => try it as a variable name
1060      Find the next token (everything up to end or next whitespace).  */
1061
1062   if (**argptr == '$')          /* May be a convenience variable.  */
1063     /* One or two $ chars possible.  */
1064     p = skip_quoted (*argptr + (((*argptr)[1] == '$') ? 2 : 1));
1065   else if (is_quoted || is_squote_enclosed)
1066     {
1067       p = end_quote;
1068       if (p[-1] != '\'')
1069         error (_("Unmatched single quote."));
1070     }
1071   else if (is_objc_method)
1072     {
1073       /* allow word separators in method names for Obj-C.  */
1074       p = skip_quoted_chars (*argptr, NULL, "");
1075     }
1076   else
1077     {
1078       p = skip_quoted (*argptr);
1079     }
1080
1081   /* Keep any important naming information.  */
1082   p = keep_name_info (p, p == saved_arg || is_linespec_boundary (p[-1]));
1083
1084   copy = (char *) alloca (p - *argptr + 1);
1085   memcpy (copy, *argptr, p - *argptr);
1086   copy[p - *argptr] = '\0';
1087   if (p != *argptr
1088       && copy[0]
1089       && copy[0] == copy[p - *argptr - 1]
1090       && strchr (get_gdb_completer_quote_characters (), copy[0]) != NULL)
1091     {
1092       copy[p - *argptr - 1] = '\0';
1093       copy++;
1094     }
1095   else if (is_quoted || is_squote_enclosed)
1096     copy[p - *argptr - 1] = '\0';
1097   
1098   *argptr = skip_spaces (p);
1099
1100   /* If it starts with $: may be a legitimate variable or routine name
1101      (e.g. HP-UX millicode routines such as $$dyncall), or it may
1102      be history value, or it may be a convenience variable.  */
1103
1104   if (*copy == '$' && function_symbols == NULL)
1105     {
1106       struct symtabs_and_lines values;
1107
1108       values = decode_dollar (self, copy);
1109       do_cleanups (cleanup);
1110       return values;
1111     }
1112
1113   /* Try the token as a label, but only if no file was specified,
1114      because we can only really find labels in the current scope.  */
1115
1116   if (VEC_length (symtab_p, self->file_symtabs) == 1
1117       && VEC_index (symtab_p, self->file_symtabs, 0) == NULL)
1118     {
1119       struct symtabs_and_lines label_result;
1120       if (decode_label (self, function_symbols, copy, &label_result))
1121         {
1122           do_cleanups (cleanup);
1123           return label_result;
1124         }
1125     }
1126
1127   if (function_symbols)
1128     throw_exception (file_exception);
1129
1130   /* Look up that token as a variable.
1131      If file specified, use that file's per-file block to start with.  */
1132
1133   {
1134     struct symtabs_and_lines values;
1135
1136     values = decode_variable (self, copy);
1137     do_cleanups (cleanup);
1138     return values;
1139   }
1140 }
1141
1142 /* A constructor for linespec_state.  */
1143
1144 static void
1145 linespec_state_constructor (struct linespec_state *self,
1146                             int flags,
1147                             struct symtab *default_symtab,
1148                             int default_line,
1149                             struct linespec_result *canonical)
1150 {
1151   memset (self, 0, sizeof (*self));
1152   self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
1153   self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
1154   self->default_symtab = default_symtab;
1155   self->default_line = default_line;
1156   self->canonical = canonical;
1157   self->program_space = current_program_space;
1158   self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
1159                                       xfree, xcalloc, xfree);
1160 }
1161
1162 /* A destructor for linespec_state.  */
1163
1164 static void
1165 linespec_state_destructor (void *arg)
1166 {
1167   struct linespec_state *self = arg;
1168
1169   xfree (self->user_filename);
1170   xfree (self->user_function);
1171   VEC_free (symtab_p, self->file_symtabs);
1172   htab_delete (self->addr_set);
1173 }
1174
1175 /* See linespec.h.  */
1176
1177 void
1178 decode_line_full (char **argptr, int flags,
1179                   struct symtab *default_symtab,
1180                   int default_line, struct linespec_result *canonical,
1181                   const char *select_mode,
1182                   const char *filter)
1183 {
1184   struct symtabs_and_lines result;
1185   struct linespec_state state;
1186   struct cleanup *cleanups;
1187   char *arg_start = *argptr;
1188   VEC (const_char_ptr) *filters = NULL;
1189
1190   gdb_assert (canonical != NULL);
1191   /* The filter only makes sense for 'all'.  */
1192   gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
1193   gdb_assert (select_mode == NULL
1194               || select_mode == multiple_symbols_all
1195               || select_mode == multiple_symbols_ask
1196               || select_mode == multiple_symbols_cancel);
1197   gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);
1198
1199   linespec_state_constructor (&state, flags,
1200                               default_symtab, default_line, canonical);
1201   cleanups = make_cleanup (linespec_state_destructor, &state);
1202   save_current_program_space ();
1203
1204   result = decode_line_internal (&state, argptr);
1205
1206   gdb_assert (result.nelts == 1 || canonical->pre_expanded);
1207   gdb_assert (canonical->addr_string != NULL);
1208   canonical->pre_expanded = 1;
1209
1210   /* Fill in the missing canonical names.  */
1211   if (result.nelts > 0)
1212     {
1213       int i;
1214
1215       if (state.canonical_names == NULL)
1216         state.canonical_names = xcalloc (result.nelts, sizeof (char *));
1217       make_cleanup (xfree, state.canonical_names);
1218       for (i = 0; i < result.nelts; ++i)
1219         {
1220           if (state.canonical_names[i] == NULL)
1221             state.canonical_names[i] = savestring (arg_start,
1222                                                    *argptr - arg_start);
1223           make_cleanup (xfree, state.canonical_names[i]);
1224         }
1225     }
1226
1227   if (select_mode == NULL)
1228     {
1229       if (ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ())))
1230         select_mode = multiple_symbols_all;
1231       else
1232         select_mode = multiple_symbols_select_mode ();
1233     }
1234
1235   if (select_mode == multiple_symbols_all)
1236     {
1237       if (filter != NULL)
1238         {
1239           make_cleanup (VEC_cleanup (const_char_ptr), &filters);
1240           VEC_safe_push (const_char_ptr, filters, filter);
1241           filter_results (&state, &result, filters);
1242         }
1243       else
1244         convert_results_to_lsals (&state, &result);
1245     }
1246   else
1247     decode_line_2 (&state, &result, select_mode);
1248
1249   do_cleanups (cleanups);
1250 }
1251
1252 struct symtabs_and_lines
1253 decode_line_1 (char **argptr, int flags,
1254                struct symtab *default_symtab,
1255                int default_line)
1256 {
1257   struct symtabs_and_lines result;
1258   struct linespec_state state;
1259   struct cleanup *cleanups;
1260
1261   linespec_state_constructor (&state, flags,
1262                               default_symtab, default_line, NULL);
1263   cleanups = make_cleanup (linespec_state_destructor, &state);
1264   save_current_program_space ();
1265
1266   result = decode_line_internal (&state, argptr);
1267   do_cleanups (cleanups);
1268   return result;
1269 }
1270
1271 \f
1272
1273 /* First, some functions to initialize stuff at the beggining of the
1274    function.  */
1275
1276 static void
1277 initialize_defaults (struct symtab **default_symtab, int *default_line)
1278 {
1279   if (*default_symtab == 0)
1280     {
1281       /* Use whatever we have for the default source line.  We don't use
1282          get_current_or_default_symtab_and_line as it can recurse and call
1283          us back!  */
1284       struct symtab_and_line cursal = 
1285         get_current_source_symtab_and_line ();
1286       
1287       *default_symtab = cursal.symtab;
1288       *default_line = cursal.line;
1289     }
1290 }
1291
1292 \f
1293
1294 /* Decode arg of the form *PC.  */
1295
1296 static struct symtabs_and_lines
1297 decode_indirect (struct linespec_state *self, char **argptr)
1298 {
1299   struct symtabs_and_lines values;
1300   CORE_ADDR pc;
1301   char *initial = *argptr;
1302   
1303   if (current_program_space->executing_startup)
1304     /* The error message doesn't really matter, because this case
1305        should only hit during breakpoint reset.  */
1306     throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
1307                                     "program space is in startup"));
1308
1309   (*argptr)++;
1310   pc = value_as_address (parse_to_comma_and_eval (argptr));
1311
1312   values.sals = (struct symtab_and_line *)
1313     xmalloc (sizeof (struct symtab_and_line));
1314
1315   values.nelts = 1;
1316   values.sals[0] = find_pc_line (pc, 0);
1317   values.sals[0].pc = pc;
1318   values.sals[0].section = find_pc_overlay (pc);
1319   values.sals[0].explicit_pc = 1;
1320
1321   if (self->canonical)
1322     self->canonical->addr_string = savestring (initial, *argptr - initial);
1323
1324   return values;
1325 }
1326
1327 \f
1328
1329 /* Locate the first half of the linespec, ending in a colon, period,
1330    or whitespace.  (More or less.)  Also, check to see if *ARGPTR is
1331    enclosed in double quotes; if so, set is_quote_enclosed, advance
1332    ARGPTR past that and zero out the trailing double quote.
1333    If ARGPTR is just a simple name like "main", p will point to ""
1334    at the end.  */
1335
1336 static char *
1337 locate_first_half (char **argptr, int *is_quote_enclosed)
1338 {
1339   char *ii;
1340   char *p, *p1;
1341   int has_comma;
1342
1343   /* Check if the linespec starts with an Ada operator (such as "+",
1344      or ">", for instance).  */
1345   p = *argptr;
1346   if (p[0] == '"'
1347       && current_language->la_language == language_ada)
1348     {
1349       const struct ada_opname_map *op;
1350
1351       for (op = ada_opname_table; op->encoded != NULL; op++)
1352         if (strncmp (op->decoded, p, strlen (op->decoded)) == 0)
1353           break;
1354       if (op->encoded != NULL)
1355         {
1356           *is_quote_enclosed = 0;
1357           return p + strlen (op->decoded);
1358         }
1359     }
1360
1361   /* Maybe we were called with a line range FILENAME:LINENUM,FILENAME:LINENUM
1362      and we must isolate the first half.  Outer layers will call again later
1363      for the second half.
1364
1365      Don't count commas that appear in argument lists of overloaded
1366      functions, or in quoted strings.  It's stupid to go to this much
1367      trouble when the rest of the function is such an obvious roach hotel.  */
1368   ii = find_toplevel_char (*argptr, ',');
1369   has_comma = (ii != 0);
1370
1371   /* Temporarily zap out second half to not confuse the code below.
1372      This is undone below.  Do not change ii!!  */
1373   if (has_comma)
1374     {
1375       *ii = '\0';
1376     }
1377
1378   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION.  May also be
1379      CLASS::MEMBER, or NAMESPACE::NAME.  Look for ':', but ignore
1380      inside of <>.  */
1381
1382   p = *argptr;
1383   if (p[0] == '"')
1384     {
1385       *is_quote_enclosed = 1;
1386       (*argptr)++;
1387       p++;
1388     }
1389   else
1390     {
1391       *is_quote_enclosed = 0;
1392       if (strchr (get_gdb_completer_quote_characters (), *p))
1393         {
1394           ++(*argptr);
1395           ++p;
1396         }
1397     }
1398
1399
1400   /* Check for a drive letter in the filename.  This is done on all hosts
1401      to capture cross-compilation environments.  On Unixen, directory
1402      separators are illegal in filenames, so if the user enters "e:/foo.c",
1403      he is referring to a directory named "e:" and a source file named
1404      "foo.c", and we still want to keep these two pieces together.  */
1405   if (isalpha (p[0]) && p[1] == ':' && IS_DIR_SEPARATOR (p[2]))
1406     p += 3;
1407
1408   for (; *p; p++)
1409     {
1410       if (p[0] == '<')
1411         {
1412           char *temp_end = find_template_name_end (p);
1413
1414           if (!temp_end)
1415             error (_("malformed template specification in command"));
1416           p = temp_end;
1417         }
1418
1419       if (p[0] == '(')
1420         p = find_method_overload_end (p);
1421
1422       /* Check for a colon and a plus or minus and a [ (which
1423          indicates an Objective-C method).  */
1424       if (is_objc_method_format (p))
1425         {
1426           break;
1427         }
1428       /* Check for the end of the first half of the linespec.  End of
1429          line, a tab, a colon or a space.  But if enclosed in double
1430          quotes we do not break on enclosed spaces.  */
1431       if (!*p
1432           || p[0] == '\t'
1433           || (p[0] == ':')
1434           || ((p[0] == ' ') && !*is_quote_enclosed))
1435         break;
1436       if (p[0] == '.' && strchr (p, ':') == NULL)
1437         {
1438           /* Java qualified method.  Find the *last* '.', since the
1439              others are package qualifiers.  Stop at any open parenthesis
1440              which might provide overload information.  */
1441           for (p1 = p; *p1 && *p1 != '('; p1++)
1442             {
1443               if (*p1 == '.')
1444                 p = p1;
1445             }
1446           break;
1447         }
1448     }
1449   p = skip_spaces (p);
1450
1451   /* If the closing double quote was left at the end, remove it.  */
1452   if (*is_quote_enclosed)
1453     {
1454       char *closing_quote = strchr (p - 1, '"');
1455
1456       if (closing_quote && closing_quote[1] == '\0')
1457         *closing_quote = '\0';
1458     }
1459
1460   /* Now that we've safely parsed the first half, put back ',' so
1461      outer layers can see it.  */
1462   if (has_comma)
1463     *ii = ',';
1464
1465   return p;
1466 }
1467
1468 \f
1469
1470 /* Here's where we recognise an Objective-C Selector.  An Objective C
1471    selector may be implemented by more than one class, therefore it
1472    may represent more than one method/function.  This gives us a
1473    situation somewhat analogous to C++ overloading.  If there's more
1474    than one method that could represent the selector, then use some of
1475    the existing C++ code to let the user choose one.  */
1476
1477 static struct symtabs_and_lines
1478 decode_objc (struct linespec_state *self, char **argptr)
1479 {
1480   struct collect_info info;
1481   VEC (const_char_ptr) *symbol_names = NULL;
1482   char *new_argptr;
1483   struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
1484                                           &symbol_names);
1485
1486   info.state = self;
1487   info.result.sals = NULL;
1488   info.result.nelts = 0;
1489
1490   new_argptr = find_imps (*argptr, &symbol_names); 
1491   if (VEC_empty (const_char_ptr, symbol_names))
1492     {
1493       do_cleanups (cleanup);
1494       return info.result;
1495     }
1496
1497   add_all_symbol_names_from_pspace (&info, NULL, symbol_names);
1498
1499   if (info.result.nelts > 0)
1500     {
1501       char *saved_arg;
1502
1503       saved_arg = alloca (new_argptr - *argptr + 1);
1504       memcpy (saved_arg, *argptr, new_argptr - *argptr);
1505       saved_arg[new_argptr - *argptr] = '\0';
1506
1507       if (self->canonical)
1508         {
1509           self->canonical->pre_expanded = 1;
1510           if (self->user_filename)
1511             self->canonical->addr_string
1512               = xstrprintf ("%s:%s", self->user_filename, saved_arg);
1513           else
1514             self->canonical->addr_string = xstrdup (saved_arg);
1515         }
1516     }
1517
1518   *argptr = new_argptr;
1519
1520   do_cleanups (cleanup);
1521   return info.result;
1522 }
1523
1524 /* This handles C++ and Java compound data structures.  P should point
1525    at the first component separator, i.e. double-colon or period.  As
1526    an example, on entrance to this function we could have ARGPTR
1527    pointing to "AAA::inA::fun" and P pointing to "::inA::fun".  */
1528
1529 static struct symtabs_and_lines
1530 decode_compound (struct linespec_state *self,
1531                  char **argptr, char *the_real_saved_arg, char *p)
1532 {
1533   struct symtabs_and_lines values;
1534   char *p2;
1535   char *saved_arg2 = *argptr;
1536   char *temp_end;
1537   struct symbol *sym;
1538   char *copy;
1539   VEC (symbolp) *sym_classes;
1540   char *saved_arg, *class_name;
1541   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
1542
1543   /* If the user specified any completer quote characters in the input,
1544      strip them.  They are superfluous.  */
1545   saved_arg = alloca (strlen (the_real_saved_arg) + 1);
1546   {
1547     char *dst = saved_arg;
1548     char *src = the_real_saved_arg;
1549     char *quotes = get_gdb_completer_quote_characters ();
1550     while (*src != '\0')
1551       {
1552         if (strchr (quotes, *src) == NULL)
1553           *dst++ = *src;
1554         ++src;
1555       }
1556     *dst = '\0';
1557   }
1558
1559   /* First check for "global" namespace specification, of the form
1560      "::foo".  If found, skip over the colons and jump to normal
1561      symbol processing.  I.e. the whole line specification starts with
1562      "::" (note the condition that *argptr == p).  */
1563   if (p[0] == ':' 
1564       && ((*argptr == p) || (p[-1] == ' ') || (p[-1] == '\t')))
1565     saved_arg2 += 2;
1566
1567   /* Given our example "AAA::inA::fun", we have two cases to consider:
1568
1569      1) AAA::inA is the name of a class.  In that case, presumably it
1570         has a method called "fun"; we then look up that method using
1571         find_method.
1572
1573      2) AAA::inA isn't the name of a class.  In that case, either the
1574         user made a typo, AAA::inA is the name of a namespace, or it is
1575         the name of a minimal symbol.
1576         In this case we just delegate to decode_variable.
1577
1578      Thus, our first task is to find everything before the last set of
1579      double-colons and figure out if it's the name of a class.  So we
1580      first loop through all of the double-colons.  */
1581
1582   p2 = p;               /* Save for restart.  */
1583
1584   /* This is very messy.  Following the example above we have now the
1585      following pointers:
1586      p -> "::inA::fun"
1587      argptr -> "AAA::inA::fun
1588      saved_arg -> "AAA::inA::fun
1589      saved_arg2 -> "AAA::inA::fun
1590      p2 -> "::inA::fun".  */
1591
1592   /* In the loop below, with these strings, we'll make 2 passes, each
1593      is marked in comments.  */
1594
1595   while (1)
1596     {
1597       static char *break_characters = " \t(";
1598
1599       /* Move pointer up to next possible class/namespace token.  */
1600
1601       p = p2 + 1;       /* Restart with old value +1.  */
1602
1603       /* PASS1: at this point p2->"::inA::fun", so p->":inA::fun",
1604          i.e. if there is a double-colon, p will now point to the
1605          second colon.  */
1606       /* PASS2: p2->"::fun", p->":fun" */
1607
1608       /* Move pointer ahead to next double-colon.  */
1609       while (*p
1610              && strchr (break_characters, *p) == NULL
1611              && strchr (get_gdb_completer_quote_characters (), *p) == NULL)
1612         {
1613           if (current_language->la_language == language_cplus)
1614             p += cp_validate_operator (p);
1615
1616           if (p[0] == '<')
1617             {
1618               temp_end = find_template_name_end (p);
1619               if (!temp_end)
1620                 error (_("malformed template specification in command"));
1621               p = temp_end;
1622             }
1623           /* Note that, since, at the start of this loop, p would be
1624              pointing to the second colon in a double-colon, we only
1625              satisfy the condition below if there is another
1626              double-colon to the right (after).  I.e. there is another
1627              component that can be a class or a namespace.  I.e, if at
1628              the beginning of this loop (PASS1), we had
1629              p->":inA::fun", we'll trigger this when p has been
1630              advanced to point to "::fun".  */
1631           /* PASS2: we will not trigger this.  */
1632           else if ((p[0] == ':') && (p[1] == ':'))
1633             break;      /* Found double-colon.  */
1634           else
1635             {
1636               /* PASS2: We'll keep getting here, until P points to one of the
1637                  break characters, at which point we exit this loop.  */
1638               if (*p)
1639                 {
1640                   if (p[1] == '('
1641                       && strncmp (&p[1], CP_ANONYMOUS_NAMESPACE_STR,
1642                                   CP_ANONYMOUS_NAMESPACE_LEN) == 0)
1643                     p += CP_ANONYMOUS_NAMESPACE_LEN;
1644                   else if (strchr (break_characters, *p) == NULL)
1645                     ++p;
1646                 }
1647             }
1648         }
1649
1650       if (*p != ':')
1651         break;          /* Out of the while (1).  This would happen
1652                            for instance if we have looked up
1653                            unsuccessfully all the components of the
1654                            string, and p->""(PASS2).  */
1655
1656       /* We get here if p points to one of the break characters or "" (i.e.,
1657          string ended).  */
1658       /* Save restart for next time around.  */
1659       p2 = p;
1660       /* Restore argptr as it was on entry to this function.  */
1661       *argptr = saved_arg2;
1662       /* PASS1: at this point p->"::fun" argptr->"AAA::inA::fun",
1663          p2->"::fun".  */
1664
1665       /* All ready for next pass through the loop.  */
1666     }                   /* while (1) */
1667
1668
1669   /* Start of lookup in the symbol tables.  */
1670
1671   /* Lookup in the symbol table the substring between argptr and
1672      p.  Note, this call changes the value of argptr.  */
1673   /* Before the call, argptr->"AAA::inA::fun",
1674      p->"", p2->"::fun".  After the call: argptr->"fun", p, p2
1675      unchanged.  */
1676   sym_classes = lookup_prefix_sym (argptr, p2, self->file_symtabs,
1677                                    &class_name);
1678   make_cleanup (VEC_cleanup (symbolp), &sym_classes);
1679   make_cleanup (xfree, class_name);
1680
1681   /* If a class has been found, then we're in case 1 above.  So we
1682      look up "fun" as a method of those classes.  */
1683   if (!VEC_empty (symbolp, sym_classes))
1684     {
1685       /* Arg token is not digits => try it as a function name.
1686          Find the next token (everything up to end or next
1687          blank).  */
1688       if (**argptr
1689           && strchr (get_gdb_completer_quote_characters (),
1690                      **argptr) != NULL)
1691         {
1692           p = skip_quoted (*argptr);
1693           *argptr = *argptr + 1;
1694         }
1695       else
1696         {
1697           /* At this point argptr->"fun".  */
1698           char *a;
1699
1700           p = *argptr;
1701           while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p != ':'
1702                  && *p != '(')
1703             p++;
1704           /* At this point p->"".  String ended.  */
1705           /* Nope, C++ operators could have spaces in them
1706              ("foo::operator <" or "foo::operator delete []").
1707              I apologize, this is a bit hacky...  */
1708           if (current_language->la_language == language_cplus
1709               && *p == ' ' && p - 8 - *argptr + 1 > 0)
1710             {
1711               /* The above loop has already swallowed "operator".  */
1712               p += cp_validate_operator (p - 8) - 8;
1713             }
1714
1715           /* Keep any important naming information.  */
1716           p = keep_name_info (p, 1);
1717         }
1718
1719       /* Allocate our own copy of the substring between argptr and
1720          p.  */
1721       copy = (char *) alloca (p - *argptr + 1);
1722       memcpy (copy, *argptr, p - *argptr);
1723       copy[p - *argptr] = '\0';
1724       if (p != *argptr
1725           && copy[p - *argptr - 1]
1726           && strchr (get_gdb_completer_quote_characters (),
1727                      copy[p - *argptr - 1]) != NULL)
1728         copy[p - *argptr - 1] = '\0';
1729
1730       /* At this point copy->"fun", p->"".  */
1731
1732       /* No line number may be specified.  */
1733       *argptr = skip_spaces (p);
1734       /* At this point arptr->"".  */
1735
1736       /* Look for copy as a method of sym_class.  */
1737       /* At this point copy->"fun", sym_class is "AAA:inA",
1738          saved_arg->"AAA::inA::fun".  This concludes the scanning of
1739          the string for possible components matches.  If we find it
1740          here, we return.  If not, and we are at the and of the string,
1741          we'll lookup the whole string in the symbol tables.  */
1742
1743       values = find_method (self, saved_arg, copy, class_name, sym_classes);
1744
1745       do_cleanups (cleanup);
1746       return values;
1747     } /* End if symbol found.  */
1748
1749
1750   /* We couldn't find a class, so we're in case 2 above.  We check the
1751      entire name as a symbol instead.  The simplest way to do this is
1752      to just throw an exception and let our caller fall through to
1753      decode_variable.  */
1754
1755   throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
1756 }
1757
1758 /* An instance of this type is used when collecting prefix symbols for
1759    decode_compound.  */
1760
1761 struct decode_compound_collector
1762 {
1763   /* The result vector.  */
1764   VEC (symbolp) *symbols;
1765
1766   /* A hash table of all symbols we found.  We use this to avoid
1767      adding any symbol more than once.  */
1768   htab_t unique_syms;
1769 };
1770
1771 /* A callback for iterate_over_symbols that is used by
1772    lookup_prefix_sym to collect type symbols.  */
1773
1774 static int
1775 collect_one_symbol (struct symbol *sym, void *d)
1776 {
1777   struct decode_compound_collector *collector = d;
1778   void **slot;
1779   struct type *t;
1780
1781   if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1782     return 1;
1783
1784   t = SYMBOL_TYPE (sym);
1785   CHECK_TYPEDEF (t);
1786   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1787       && TYPE_CODE (t) != TYPE_CODE_UNION
1788       && TYPE_CODE (t) != TYPE_CODE_NAMESPACE)
1789     return 1;
1790
1791   slot = htab_find_slot (collector->unique_syms, sym, INSERT);
1792   if (!*slot)
1793     {
1794       *slot = sym;
1795       VEC_safe_push (symbolp, collector->symbols, sym);
1796     }
1797
1798   return 1;
1799 }
1800
1801 /* Return the symbol corresponding to the substring of *ARGPTR ending
1802    at P, allowing whitespace.  Also, advance *ARGPTR past the symbol
1803    name in question, the compound object separator ("::" or "."), and
1804    whitespace.  Note that *ARGPTR is changed whether or not the
1805    this call finds anything (i.e we return NULL).  As an
1806    example, say ARGPTR is "AAA::inA::fun" and P is "::inA::fun".  */
1807
1808 static VEC (symbolp) *
1809 lookup_prefix_sym (char **argptr, char *p, VEC (symtab_p) *file_symtabs,
1810                    char **class_name)
1811 {
1812   char *p1;
1813   char *copy;
1814   int ix;
1815   struct symtab *elt;
1816   struct decode_compound_collector collector;
1817   struct cleanup *outer;
1818   struct cleanup *cleanup;
1819   struct block *search_block;
1820
1821   /* Extract the class name.  */
1822   p1 = p;
1823   while (p != *argptr && p[-1] == ' ')
1824     --p;
1825   copy = (char *) xmalloc (p - *argptr + 1);
1826   memcpy (copy, *argptr, p - *argptr);
1827   copy[p - *argptr] = 0;
1828   *class_name = copy;
1829   outer = make_cleanup (xfree, copy);
1830
1831   /* Discard the class name from the argptr.  */
1832   p = p1 + (p1[0] == ':' ? 2 : 1);
1833   p = skip_spaces (p);
1834   *argptr = p;
1835
1836   /* At this point p1->"::inA::fun", p->"inA::fun" copy->"AAA",
1837      argptr->"inA::fun".  */
1838
1839   collector.symbols = NULL;
1840   make_cleanup (VEC_cleanup (symbolp), &collector.symbols);
1841
1842   collector.unique_syms = htab_create_alloc (1, htab_hash_pointer,
1843                                              htab_eq_pointer, NULL,
1844                                              xcalloc, xfree);
1845   cleanup = make_cleanup_htab_delete (collector.unique_syms);
1846
1847   for (ix = 0; VEC_iterate (symtab_p, file_symtabs, ix, elt); ++ix)
1848     {
1849       if (elt == NULL)
1850         {
1851           iterate_over_all_matching_symtabs (copy, STRUCT_DOMAIN,
1852                                              collect_one_symbol, &collector,
1853                                              NULL);
1854           iterate_over_all_matching_symtabs (copy, VAR_DOMAIN,
1855                                              collect_one_symbol, &collector,
1856                                              NULL);
1857         }
1858       else
1859         {
1860           struct block *search_block;
1861
1862           /* Program spaces that are executing startup should have
1863              been filtered out earlier.  */
1864           gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
1865           set_current_program_space (SYMTAB_PSPACE (elt));
1866           search_block = get_search_block (elt);
1867           LA_ITERATE_OVER_SYMBOLS (search_block, copy, STRUCT_DOMAIN,
1868                                    collect_one_symbol, &collector);
1869           LA_ITERATE_OVER_SYMBOLS (search_block, copy, VAR_DOMAIN,
1870                                    collect_one_symbol, &collector);
1871         }
1872     }
1873
1874   do_cleanups (cleanup);
1875   discard_cleanups (outer);
1876   return collector.symbols;
1877 }
1878
1879 /* A qsort comparison function for symbols.  The resulting order does
1880    not actually matter; we just need to be able to sort them so that
1881    symbols with the same program space end up next to each other.  */
1882
1883 static int
1884 compare_symbols (const void *a, const void *b)
1885 {
1886   struct symbol * const *sa = a;
1887   struct symbol * const *sb = b;
1888   uintptr_t uia, uib;
1889
1890   uia = (uintptr_t) SYMTAB_PSPACE (SYMBOL_SYMTAB (*sa));
1891   uib = (uintptr_t) SYMTAB_PSPACE (SYMBOL_SYMTAB (*sb));
1892
1893   if (uia < uib)
1894     return -1;
1895   if (uia > uib)
1896     return 1;
1897
1898   uia = (uintptr_t) *sa;
1899   uib = (uintptr_t) *sb;
1900
1901   if (uia < uib)
1902     return -1;
1903   if (uia > uib)
1904     return 1;
1905
1906   return 0;
1907 }
1908
1909 /* Look for all the matching instances of each symbol in NAMES.  Only
1910    instances from PSPACE are considered; other program spaces are
1911    handled by our caller.  If PSPACE is NULL, then all program spaces
1912    are considered.  Results are stored into INFO.  */
1913
1914 static void
1915 add_all_symbol_names_from_pspace (struct collect_info *info,
1916                                   struct program_space *pspace,
1917                                   VEC (const_char_ptr) *names)
1918 {
1919   int ix;
1920   const char *iter;
1921
1922   for (ix = 0; VEC_iterate (const_char_ptr, names, ix, iter); ++ix)
1923     add_matching_symbols_to_info (iter, info, pspace);
1924 }
1925
1926 static void
1927 find_superclass_methods (VEC (typep) *superclasses,
1928                          const char *name,
1929                          VEC (const_char_ptr) **result_names)
1930 {
1931   int old_len = VEC_length (const_char_ptr, *result_names);
1932   VEC (typep) *iter_classes;
1933   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
1934
1935   iter_classes = superclasses;
1936   while (1)
1937     {
1938       VEC (typep) *new_supers = NULL;
1939       int ix;
1940       struct type *t;
1941
1942       make_cleanup (VEC_cleanup (typep), &new_supers);
1943       for (ix = 0; VEC_iterate (typep, iter_classes, ix, t); ++ix)
1944         find_methods (t, name, result_names, &new_supers);
1945
1946       if (VEC_length (const_char_ptr, *result_names) != old_len
1947           || VEC_empty (typep, new_supers))
1948         break;
1949
1950       iter_classes = new_supers;
1951     }
1952
1953   do_cleanups (cleanup);
1954 }
1955
1956 /* This finds the method COPY in the class whose type is given by one
1957    of the symbols in SYM_CLASSES.  */
1958
1959 static struct symtabs_and_lines
1960 find_method (struct linespec_state *self, char *saved_arg,
1961              char *copy, const char *class_name, VEC (symbolp) *sym_classes)
1962 {
1963   char *canon;
1964   struct symbol *sym;
1965   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
1966   int ix;
1967   int last_result_len;
1968   VEC (typep) *superclass_vec;
1969   VEC (const_char_ptr) *result_names;
1970   struct collect_info info;
1971   char *name_iter;
1972
1973   /* NAME is typed by the user: it needs to be canonicalized before
1974      searching the symbol tables.  */
1975   canon = cp_canonicalize_string_no_typedefs (copy);
1976   if (canon != NULL)
1977     {
1978       copy = canon;
1979       make_cleanup (xfree, copy);
1980     }
1981
1982   /* Sort symbols so that symbols with the same program space are next
1983      to each other.  */
1984   qsort (VEC_address (symbolp, sym_classes),
1985          VEC_length (symbolp, sym_classes),
1986          sizeof (symbolp),
1987          compare_symbols);
1988
1989   info.state = self;
1990   info.result.sals = NULL;
1991   info.result.nelts = 0;
1992
1993   /* Iterate over all the types, looking for the names of existing
1994      methods matching COPY.  If we cannot find a direct method in a
1995      given program space, then we consider inherited methods; this is
1996      not ideal (ideal would be to respect C++ hiding rules), but it
1997      seems good enough and is what GDB has historically done.  We only
1998      need to collect the names because later we find all symbols with
1999      those names.  This loop is written in a somewhat funny way
2000      because we collect data across the program space before deciding
2001      what to do.  */
2002   superclass_vec = NULL;
2003   make_cleanup (VEC_cleanup (typep), &superclass_vec);
2004   result_names = NULL;
2005   make_cleanup (VEC_cleanup (const_char_ptr), &result_names);
2006   last_result_len = 0;
2007   for (ix = 0; VEC_iterate (symbolp, sym_classes, ix, sym); ++ix)
2008     {
2009       struct type *t;
2010       struct program_space *pspace;
2011
2012       /* Program spaces that are executing startup should have
2013          been filtered out earlier.  */
2014       gdb_assert (!SYMTAB_PSPACE (SYMBOL_SYMTAB (sym))->executing_startup);
2015       pspace = SYMTAB_PSPACE (SYMBOL_SYMTAB (sym));
2016       set_current_program_space (pspace);
2017       t = check_typedef (SYMBOL_TYPE (sym));
2018       find_methods (t, copy, &result_names, &superclass_vec);
2019
2020       /* Handle all items from a single program space at once; and be
2021          sure not to miss the last batch.  */
2022       if (ix == VEC_length (symbolp, sym_classes) - 1
2023           || (pspace
2024               != SYMTAB_PSPACE (SYMBOL_SYMTAB (VEC_index (symbolp, sym_classes,
2025                                                           ix + 1)))))
2026         {
2027           /* If we did not find a direct implementation anywhere in
2028              this program space, consider superclasses.  */
2029           if (VEC_length (const_char_ptr, result_names) == last_result_len)
2030             find_superclass_methods (superclass_vec, copy, &result_names);
2031
2032           /* We have a list of candidate symbol names, so now we
2033              iterate over the symbol tables looking for all
2034              matches in this pspace.  */
2035           add_all_symbol_names_from_pspace (&info, pspace, result_names);
2036
2037           VEC_truncate (typep, superclass_vec, 0);
2038           last_result_len = VEC_length (const_char_ptr, result_names);
2039         }
2040     }
2041
2042   if (info.result.nelts > 0)
2043     {
2044       if (self->canonical)
2045         {
2046           self->canonical->pre_expanded = 1;
2047           if (self->user_filename)
2048             self->canonical->addr_string
2049               = xstrprintf ("%s:%s", self->user_filename, saved_arg);
2050           else
2051             self->canonical->addr_string = xstrdup (saved_arg);
2052         }
2053
2054       do_cleanups (cleanup);
2055
2056       return info.result;
2057     }
2058
2059   if (copy[0] == '~')
2060     cplusplus_error (saved_arg,
2061                      "the class `%s' does not have destructor defined\n",
2062                      class_name);
2063   else
2064     cplusplus_error (saved_arg,
2065                      "the class %s does not have any method named %s\n",
2066                      class_name, copy);
2067 }
2068
2069 \f
2070
2071 /* This object is used when collecting all matching symtabs.  */
2072
2073 struct symtab_collector
2074 {
2075   /* The result vector of symtabs.  */
2076   VEC (symtab_p) *symtabs;
2077
2078   /* This is used to ensure the symtabs are unique.  */
2079   htab_t symtab_table;
2080 };
2081
2082 /* Callback for iterate_over_symtabs.  */
2083
2084 static int
2085 add_symtabs_to_list (struct symtab *symtab, void *d)
2086 {
2087   struct symtab_collector *data = d;
2088   void **slot;
2089
2090   slot = htab_find_slot (data->symtab_table, symtab, INSERT);
2091   if (!*slot)
2092     {
2093       *slot = symtab;
2094       VEC_safe_push (symtab_p, data->symtabs, symtab);
2095     }
2096
2097   return 0;
2098 }
2099
2100 /* Given a file name, return a VEC of all matching symtabs.  */
2101
2102 static VEC (symtab_p) *
2103 collect_symtabs_from_filename (const char *file)
2104 {
2105   struct symtab_collector collector;
2106   struct cleanup *cleanups;
2107   struct program_space *pspace;
2108
2109   collector.symtabs = NULL;
2110   collector.symtab_table = htab_create (1, htab_hash_pointer, htab_eq_pointer,
2111                                         NULL);
2112   cleanups = make_cleanup_htab_delete (collector.symtab_table);
2113
2114   /* Find that file's data.  */
2115   ALL_PSPACES (pspace)
2116   {
2117     if (pspace->executing_startup)
2118       continue;
2119
2120     set_current_program_space (pspace);
2121     iterate_over_symtabs (file, add_symtabs_to_list, &collector);
2122   }
2123
2124   do_cleanups (cleanups);
2125   return collector.symtabs;
2126 }
2127
2128 /* Return all the symtabs associated to the filename given by the
2129    substring of *ARGPTR ending at P, and advance ARGPTR past that
2130    filename.  */
2131
2132 static VEC (symtab_p) *
2133 symtabs_from_filename (char **argptr, char *p, int is_quote_enclosed,
2134                        char **user_filename)
2135 {
2136   char *p1;
2137   char *copy;
2138   struct cleanup *outer;
2139   VEC (symtab_p) *result;
2140   
2141   p1 = p;
2142   while (p != *argptr && p[-1] == ' ')
2143     --p;
2144   if ((*p == '"') && is_quote_enclosed)
2145     --p;
2146   copy = xmalloc (p - *argptr + 1);
2147   outer = make_cleanup (xfree, copy);
2148   memcpy (copy, *argptr, p - *argptr);
2149   /* It may have the ending quote right after the file name.  */
2150   if ((is_quote_enclosed && copy[p - *argptr - 1] == '"')
2151       || copy[p - *argptr - 1] == '\'')
2152     copy[p - *argptr - 1] = 0;
2153   else
2154     copy[p - *argptr] = 0;
2155
2156   result = collect_symtabs_from_filename (copy);
2157
2158   if (VEC_empty (symtab_p, result))
2159     {
2160       if (!have_full_symbols () && !have_partial_symbols ())
2161         throw_error (NOT_FOUND_ERROR,
2162                      _("No symbol table is loaded.  "
2163                        "Use the \"file\" command."));
2164       throw_error (NOT_FOUND_ERROR, _("No source file named %s."), copy);
2165     }
2166
2167   /* Discard the file name from the arg.  */
2168   if (*p1 == '\0')
2169     *argptr = p1;
2170   else
2171     *argptr = skip_spaces (p1 + 1);
2172
2173   discard_cleanups (outer);
2174   *user_filename = copy;
2175   return result;
2176 }
2177
2178 /* A callback used by iterate_over_all_matching_symtabs that collects
2179    symbols for find_function_symbols.  */
2180
2181 static int
2182 collect_function_symbols (struct symbol *sym, void *arg)
2183 {
2184   VEC (symbolp) **syms = arg;
2185
2186   if (SYMBOL_CLASS (sym) == LOC_BLOCK)
2187     VEC_safe_push (symbolp, *syms, sym);
2188
2189   return 1;
2190 }
2191
2192 /* Look up a function symbol in *ARGPTR.  If found, advance *ARGPTR
2193    and return the symbol.  If not found, return NULL.  */
2194
2195 static VEC (symbolp) *
2196 find_function_symbols (char **argptr, char *p, int is_quote_enclosed,
2197                        char **user_function)
2198 {
2199   char *p1;
2200   char *copy;
2201   VEC (symbolp) *result = NULL;
2202
2203   p1 = p;
2204   while (p != *argptr && p[-1] == ' ')
2205     --p;
2206   if ((*p == '"') && is_quote_enclosed)
2207     --p;
2208   copy = (char *) xmalloc (p - *argptr + 1);
2209   *user_function = copy;
2210   memcpy (copy, *argptr, p - *argptr);
2211   /* It may have the ending quote right after the file name.  */
2212   if ((is_quote_enclosed && copy[p - *argptr - 1] == '"')
2213       || copy[p - *argptr - 1] == '\'')
2214     copy[p - *argptr - 1] = 0;
2215   else
2216     copy[p - *argptr] = 0;
2217
2218   iterate_over_all_matching_symtabs (copy, VAR_DOMAIN,
2219                                      collect_function_symbols, &result, NULL);
2220
2221   if (VEC_empty (symbolp, result))
2222     VEC_free (symbolp, result);
2223   else
2224     {
2225       /* Discard the file name from the arg.  */
2226       *argptr = skip_spaces (p1 + 1);
2227     }
2228
2229   return result;
2230 }
2231
2232 \f
2233
2234 /* A helper for decode_all_digits that handles the 'list_mode' case.  */
2235
2236 static void
2237 decode_digits_list_mode (struct linespec_state *self,
2238                          struct symtabs_and_lines *values,
2239                          struct symtab_and_line val)
2240 {
2241   int ix;
2242   struct symtab *elt;
2243
2244   gdb_assert (self->list_mode);
2245
2246   for (ix = 0; VEC_iterate (symtab_p, self->file_symtabs, ix, elt); ++ix)
2247     {
2248       /* The logic above should ensure this.  */
2249       gdb_assert (elt != NULL);
2250
2251       set_current_program_space (SYMTAB_PSPACE (elt));
2252
2253       /* Simplistic search just for the list command.  */
2254       val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
2255       if (val.symtab == NULL)
2256         val.symtab = elt;
2257       val.pspace = SYMTAB_PSPACE (elt);
2258       val.pc = 0;
2259       val.explicit_line = 1;
2260
2261       add_sal_to_sals (self, values, &val, NULL);
2262     }
2263 }
2264
2265 /* A helper for decode_all_digits that iterates over the symtabs,
2266    adding lines to the VEC.  */
2267
2268 static void
2269 decode_digits_ordinary (struct linespec_state *self,
2270                         int line,
2271                         struct symtabs_and_lines *sals,
2272                         struct linetable_entry **best_entry)
2273 {
2274   int ix;
2275   struct symtab *elt;
2276
2277   for (ix = 0; VEC_iterate (symtab_p, self->file_symtabs, ix, elt); ++ix)
2278     {
2279       int i;
2280       VEC (CORE_ADDR) *pcs;
2281       CORE_ADDR pc;
2282
2283       /* The logic above should ensure this.  */
2284       gdb_assert (elt != NULL);
2285
2286       set_current_program_space (SYMTAB_PSPACE (elt));
2287
2288       pcs = find_pcs_for_symtab_line (elt, line, best_entry);
2289       for (i = 0; VEC_iterate (CORE_ADDR, pcs, i, pc); ++i)
2290         {
2291           struct symtab_and_line sal;
2292
2293           init_sal (&sal);
2294           sal.pspace = SYMTAB_PSPACE (elt);
2295           sal.symtab = elt;
2296           sal.line = line;
2297           sal.pc = pc;
2298           add_sal_to_sals_basic (sals, &sal);
2299         }
2300
2301       VEC_free (CORE_ADDR, pcs);
2302     }
2303 }
2304
2305 /* This decodes a line where the argument is all digits (possibly
2306    preceded by a sign).  Q should point to the end of those digits;
2307    the other arguments are as usual.  */
2308
2309 static struct symtabs_and_lines
2310 decode_all_digits (struct linespec_state *self,
2311                    char **argptr,
2312                    char *q)
2313 {
2314   struct symtabs_and_lines values;
2315   struct symtab_and_line val;
2316   int use_default = 0;
2317   char *saved_arg = *argptr;
2318
2319   enum sign
2320     {
2321       none, plus, minus
2322     }
2323   sign = none;
2324
2325   init_sal (&val);
2326   values.sals = NULL;
2327   values.nelts = 0;
2328
2329   /* This is where we need to make sure that we have good defaults.
2330      We must guarantee that this section of code is never executed
2331      when we are called with just a function name, since
2332      set_default_source_symtab_and_line uses
2333      select_source_symtab that calls us with such an argument.  */
2334
2335   if (VEC_length (symtab_p, self->file_symtabs) == 1
2336       && VEC_index (symtab_p, self->file_symtabs, 0) == NULL)
2337     {
2338       set_current_program_space (self->program_space);
2339
2340       /* Make sure we have at least a default source file.  */
2341       set_default_source_symtab_and_line ();
2342       initialize_defaults (&self->default_symtab, &self->default_line);
2343       VEC_pop (symtab_p, self->file_symtabs);
2344       VEC_free (symtab_p, self->file_symtabs);
2345       self->file_symtabs
2346         = collect_symtabs_from_filename (self->default_symtab->filename);
2347       use_default = 1;
2348     }
2349
2350   if (**argptr == '+')
2351     sign = plus, (*argptr)++;
2352   else if (**argptr == '-')
2353     sign = minus, (*argptr)++;
2354   val.line = atoi (*argptr);
2355   switch (sign)
2356     {
2357     case plus:
2358       if (q == *argptr)
2359         val.line = 5;
2360       if (use_default)
2361         val.line = self->default_line + val.line;
2362       break;
2363     case minus:
2364       if (q == *argptr)
2365         val.line = 15;
2366       if (use_default)
2367         val.line = self->default_line - val.line;
2368       else
2369         val.line = 1;
2370       break;
2371     case none:
2372       break;            /* No need to adjust val.line.  */
2373     }
2374
2375   *argptr = skip_spaces (q);
2376
2377   if (self->list_mode)
2378     decode_digits_list_mode (self, &values, val);
2379   else
2380     {
2381       struct linetable_entry *best_entry = NULL;
2382       int *filter;
2383       struct block **blocks;
2384       struct cleanup *cleanup;
2385       struct symtabs_and_lines intermediate_results;
2386       int i, j;
2387
2388       intermediate_results.sals = NULL;
2389       intermediate_results.nelts = 0;
2390
2391       decode_digits_ordinary (self, val.line, &intermediate_results,
2392                               &best_entry);
2393       if (intermediate_results.nelts == 0 && best_entry != NULL)
2394         decode_digits_ordinary (self, best_entry->line, &intermediate_results,
2395                                 &best_entry);
2396
2397       cleanup = make_cleanup (xfree, intermediate_results.sals);
2398
2399       /* For optimized code, compiler can scatter one source line
2400          accross disjoint ranges of PC values, even when no duplicate
2401          functions or inline functions are involved.  For example,
2402          'for (;;)' inside non-template non-inline non-ctor-or-dtor
2403          function can result in two PC ranges.  In this case, we don't
2404          want to set breakpoint on first PC of each range.  To filter
2405          such cases, we use containing blocks -- for each PC found
2406          above we see if there are other PCs that are in the same
2407          block.  If yes, the other PCs are filtered out.  */
2408
2409       filter = xmalloc (intermediate_results.nelts * sizeof (int));
2410       make_cleanup (xfree, filter);
2411       blocks = xmalloc (intermediate_results.nelts * sizeof (struct block *));
2412       make_cleanup (xfree, blocks);
2413
2414       for (i = 0; i < intermediate_results.nelts; ++i)
2415         {
2416           set_current_program_space (intermediate_results.sals[i].pspace);
2417
2418           filter[i] = 1;
2419           blocks[i] = block_for_pc_sect (intermediate_results.sals[i].pc,
2420                                          intermediate_results.sals[i].section);
2421         }
2422
2423       for (i = 0; i < intermediate_results.nelts; ++i)
2424         {
2425           if (blocks[i] != NULL)
2426             for (j = i + 1; j < intermediate_results.nelts; ++j)
2427               {
2428                 if (blocks[j] == blocks[i])
2429                   {
2430                     filter[j] = 0;
2431                     break;
2432                   }
2433               }
2434         }
2435
2436       for (i = 0; i < intermediate_results.nelts; ++i)
2437         if (filter[i])
2438           {
2439             struct symbol *sym = (blocks[i]
2440                                   ? block_containing_function (blocks[i])
2441                                   : NULL);
2442
2443             if (self->funfirstline)
2444               skip_prologue_sal (&intermediate_results.sals[i]);
2445             /* Make sure the line matches the request, not what was
2446                found.  */
2447             intermediate_results.sals[i].line = val.line;
2448             add_sal_to_sals (self, &values, &intermediate_results.sals[i],
2449                              sym ? SYMBOL_NATURAL_NAME (sym) : NULL);
2450           }
2451
2452       do_cleanups (cleanup);
2453     }
2454
2455   if (values.nelts == 0)
2456     {
2457       if (self->user_filename)
2458         throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
2459                      val.line, self->user_filename);
2460       else
2461         throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
2462                      val.line);
2463     }
2464
2465   if (self->canonical)
2466     {
2467       char *copy = savestring (saved_arg, q - saved_arg);
2468
2469       self->canonical->pre_expanded = 1;
2470       gdb_assert (self->user_filename || use_default);
2471       self->canonical->addr_string
2472         = xstrprintf ("%s:%s", (self->user_filename
2473                                 ? self->user_filename
2474                                 : self->default_symtab->filename),
2475                       copy);
2476       xfree (copy);
2477     }
2478
2479   return values;
2480 }
2481
2482 \f
2483
2484 /* Decode a linespec starting with a dollar sign.  */
2485
2486 static struct symtabs_and_lines
2487 decode_dollar (struct linespec_state *self, char *copy)
2488 {
2489   LONGEST valx;
2490   int index = 0;
2491   struct symtabs_and_lines values;
2492   struct symtab_and_line val;
2493   char *p;
2494   struct symbol *sym;
2495   struct minimal_symbol *msymbol;
2496   int ix;
2497   struct symtab *elt;
2498
2499   p = (copy[1] == '$') ? copy + 2 : copy + 1;
2500   while (*p >= '0' && *p <= '9')
2501     p++;
2502   if (!*p)              /* Reached end of token without hitting non-digit.  */
2503     {
2504       /* We have a value history reference.  */
2505       struct value *val_history;
2506
2507       sscanf ((copy[1] == '$') ? copy + 2 : copy + 1, "%d", &index);
2508       val_history = access_value_history ((copy[1] == '$') ? -index : index);
2509       if (TYPE_CODE (value_type (val_history)) != TYPE_CODE_INT)
2510         error (_("History values used in line "
2511                  "specs must have integer values."));
2512       valx = value_as_long (val_history);
2513     }
2514   else
2515     {
2516       /* Not all digits -- may be user variable/function or a
2517          convenience variable.  */
2518
2519       volatile struct gdb_exception exc;
2520
2521       /* Avoid "may be used uninitialized" warning.  */
2522       values.sals = NULL;
2523       values.nelts = 0;
2524
2525       TRY_CATCH (exc, RETURN_MASK_ERROR)
2526         {
2527           values = decode_variable (self, copy);
2528         }
2529
2530       if (exc.reason == 0)
2531         return values;
2532
2533       if (exc.error != NOT_FOUND_ERROR)
2534         throw_exception (exc);
2535
2536       /* Not a user variable or function -- must be convenience variable.  */
2537       if (!get_internalvar_integer (lookup_internalvar (copy + 1), &valx))
2538         error (_("Convenience variables used in line "
2539                  "specs must have integer values."));
2540     }
2541
2542   init_sal (&val);
2543
2544   values.sals = NULL;
2545   values.nelts = 0;
2546
2547   for (ix = 0; VEC_iterate (symtab_p, self->file_symtabs, ix, elt); ++ix)
2548     {
2549       if (elt == NULL)
2550         {
2551           elt = self->default_symtab;
2552           set_current_program_space (self->program_space);
2553         }
2554       else
2555         set_current_program_space (SYMTAB_PSPACE (elt));
2556
2557       /* Either history value or convenience value from above, in valx.  */
2558       val.symtab = elt;
2559       val.line = valx;
2560       val.pc = 0;
2561       val.pspace = elt ? SYMTAB_PSPACE (elt) : current_program_space;
2562
2563       add_sal_to_sals (self, &values, &val, NULL);
2564     }
2565
2566   if (self->canonical)
2567     {
2568       self->canonical->pre_expanded = 1;
2569       if (self->user_filename)
2570         self->canonical->addr_string = xstrprintf ("%s:%s",
2571                                                    self->user_filename, copy);
2572       else
2573         self->canonical->addr_string = xstrdup (copy);
2574     }
2575
2576   return values;
2577 }
2578
2579 \f
2580
2581 /* A helper for decode_line_1 that tries to find a label.  The label
2582    is searched for in the current block.
2583    FUNCTION_SYMBOLS is a list of the enclosing functions; or NULL if none
2584    specified.
2585    COPY is the name of the label to find.
2586    CANONICAL is the same as the "canonical" argument to decode_line_1.
2587    RESULT is a pointer to a symtabs_and_lines structure which will be
2588    filled in on success.
2589    This function returns 1 if a label was found, 0 otherwise.  */
2590
2591 static int
2592 decode_label (struct linespec_state *self,
2593               VEC (symbolp) *function_symbols, char *copy,
2594               struct symtabs_and_lines *result)
2595 {
2596   struct symbol *fn_sym;
2597   int ix;
2598
2599   if (function_symbols == NULL)
2600     {
2601       struct block *block;
2602       struct symbol *sym;
2603       struct symtab_and_line sal;
2604       struct symtabs_and_lines values;
2605
2606       values.nelts = 0;
2607       values.sals = NULL;
2608
2609       set_current_program_space (self->program_space);
2610       block = get_search_block (NULL);
2611
2612       for (;
2613            block && !BLOCK_FUNCTION (block);
2614            block = BLOCK_SUPERBLOCK (block))
2615         ;
2616       if (!block)
2617         return 0;
2618       fn_sym = BLOCK_FUNCTION (block);
2619
2620       sym = lookup_symbol (copy, block, LABEL_DOMAIN, 0);
2621
2622       if (sym == NULL)
2623         return 0;
2624
2625       symbol_to_sal (&sal, self->funfirstline, sym);
2626       add_sal_to_sals (self, &values, &sal,
2627                        SYMBOL_NATURAL_NAME (fn_sym));
2628
2629       if (self->canonical)
2630         {
2631           self->canonical->special_display = 1;
2632           self->canonical->addr_string
2633             = xstrprintf ("%s:%s", SYMBOL_NATURAL_NAME (fn_sym),
2634                           copy);
2635         }
2636
2637       *result = values;
2638
2639       return 1;
2640     }
2641
2642   result->sals = NULL;
2643   result->nelts = 0;
2644
2645   for (ix = 0; VEC_iterate (symbolp, function_symbols, ix, fn_sym); ++ix)
2646     {
2647       struct block *block;
2648       struct symbol *sym;
2649
2650       set_current_program_space (SYMTAB_PSPACE (SYMBOL_SYMTAB (fn_sym)));
2651       block = SYMBOL_BLOCK_VALUE (fn_sym);
2652       sym = lookup_symbol (copy, block, LABEL_DOMAIN, 0);
2653
2654       if (sym != NULL)
2655         {
2656           struct symtab_and_line sal;
2657           char *symname;
2658
2659           symbol_to_sal (&sal, self->funfirstline, sym);
2660           symname = xstrprintf ("%s:%s",
2661                                 SYMBOL_NATURAL_NAME (fn_sym),
2662                                 SYMBOL_NATURAL_NAME (sym));
2663           add_sal_to_sals (self, result, &sal, symname);
2664           xfree (symname);
2665         }
2666     }
2667
2668   if (self->canonical && result->nelts > 0)
2669     {
2670       self->canonical->pre_expanded = 1;
2671       self->canonical->special_display = 1;
2672
2673       gdb_assert (self->user_function);
2674       self->canonical->addr_string
2675         = xstrprintf ("%s:%s", self->user_function, copy);
2676     }
2677
2678   return result->nelts > 0;
2679 }
2680
2681 /* A callback used to possibly add a symbol to the results.  */
2682
2683 static int
2684 collect_symbols (struct symbol *sym, void *data)
2685 {
2686   struct collect_info *info = data;
2687   struct symtab_and_line sal;
2688
2689   if (symbol_to_sal (&sal, info->state->funfirstline, sym)
2690       && maybe_add_address (info->state->addr_set,
2691                             SYMTAB_PSPACE (SYMBOL_SYMTAB (sym)),
2692                             sal.pc))
2693     add_sal_to_sals (info->state, &info->result, &sal,
2694                      SYMBOL_NATURAL_NAME (sym));
2695
2696   return 1;
2697 }
2698
2699 /* We've found a minimal symbol MSYMBOL to associate with our
2700    linespec; add it to the result symtabs_and_lines.  */
2701
2702 static void
2703 minsym_found (struct linespec_state *self, struct objfile *objfile,
2704               struct minimal_symbol *msymbol,
2705               struct symtabs_and_lines *result)
2706 {
2707   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2708   CORE_ADDR pc;
2709   struct symtab_and_line sal;
2710
2711   sal = find_pc_sect_line (SYMBOL_VALUE_ADDRESS (msymbol),
2712                            (struct obj_section *) 0, 0);
2713   sal.section = SYMBOL_OBJ_SECTION (msymbol);
2714
2715   /* The minimal symbol might point to a function descriptor;
2716      resolve it to the actual code address instead.  */
2717   pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, &current_target);
2718   if (pc != sal.pc)
2719     sal = find_pc_sect_line (pc, NULL, 0);
2720
2721   if (self->funfirstline)
2722     skip_prologue_sal (&sal);
2723
2724   if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
2725     add_sal_to_sals (self, result, &sal, SYMBOL_NATURAL_NAME (msymbol));
2726 }
2727
2728 /* A helper struct which just holds a minimal symbol and the object
2729    file from which it came.  */
2730
2731 typedef struct minsym_and_objfile
2732 {
2733   struct minimal_symbol *minsym;
2734   struct objfile *objfile;
2735 } minsym_and_objfile_d;
2736
2737 DEF_VEC_O (minsym_and_objfile_d);
2738
2739 /* A helper struct to pass some data through
2740    iterate_over_minimal_symbols.  */
2741
2742 struct collect_minsyms
2743 {
2744   /* The objfile we're examining.  */
2745   struct objfile *objfile;
2746
2747   /* The funfirstline setting from the initial call.  */
2748   int funfirstline;
2749
2750   /* The list_mode setting from the initial call.  */
2751   int list_mode;
2752
2753   /* The resulting symbols.  */
2754   VEC (minsym_and_objfile_d) *msyms;
2755 };
2756
2757 /* A helper function to classify a minimal_symbol_type according to
2758    priority.  */
2759
2760 static int
2761 classify_mtype (enum minimal_symbol_type t)
2762 {
2763   switch (t)
2764     {
2765     case mst_file_text:
2766     case mst_file_data:
2767     case mst_file_bss:
2768       /* Intermediate priority.  */
2769       return 1;
2770
2771     case mst_solib_trampoline:
2772       /* Lowest priority.  */
2773       return 2;
2774
2775     default:
2776       /* Highest priority.  */
2777       return 0;
2778     }
2779 }
2780
2781 /* Callback for qsort that sorts symbols by priority.  */
2782
2783 static int
2784 compare_msyms (const void *a, const void *b)
2785 {
2786   const minsym_and_objfile_d *moa = a;
2787   const minsym_and_objfile_d *mob = b;
2788   enum minimal_symbol_type ta = MSYMBOL_TYPE (moa->minsym);
2789   enum minimal_symbol_type tb = MSYMBOL_TYPE (mob->minsym);
2790
2791   return classify_mtype (ta) - classify_mtype (tb);
2792 }
2793
2794 /* Callback for iterate_over_minimal_symbols that adds the symbol to
2795    the result.  */
2796
2797 static void
2798 add_minsym (struct minimal_symbol *minsym, void *d)
2799 {
2800   struct collect_minsyms *info = d;
2801   minsym_and_objfile_d mo;
2802
2803   /* Exclude data symbols when looking for breakpoint locations.   */
2804   if (!info->list_mode)
2805     switch (minsym->type)
2806       {
2807         case mst_slot_got_plt:
2808         case mst_data:
2809         case mst_bss:
2810         case mst_abs:
2811         case mst_file_data:
2812         case mst_file_bss:
2813           {
2814             /* Make sure this minsym is not a function descriptor
2815                before we decide to discard it.  */
2816             struct gdbarch *gdbarch = info->objfile->gdbarch;
2817             CORE_ADDR addr = gdbarch_convert_from_func_ptr_addr
2818                                (gdbarch, SYMBOL_VALUE_ADDRESS (minsym),
2819                                 &current_target);
2820
2821             if (addr == SYMBOL_VALUE_ADDRESS (minsym))
2822               return;
2823           }
2824       }
2825
2826   mo.minsym = minsym;
2827   mo.objfile = info->objfile;
2828   VEC_safe_push (minsym_and_objfile_d, info->msyms, &mo);
2829 }
2830
2831 /* Search minimal symbols in all objfiles for NAME.  If SEARCH_PSPACE
2832    is not NULL, the search is restricted to just that program
2833    space.  */
2834
2835 static void
2836 search_minsyms_for_name (struct collect_info *info, const char *name,
2837                          struct program_space *search_pspace)
2838 {
2839   struct objfile *objfile;
2840   struct program_space *pspace;
2841
2842   ALL_PSPACES (pspace)
2843   {
2844     struct collect_minsyms local;
2845     struct cleanup *cleanup;
2846
2847     if (search_pspace != NULL && search_pspace != pspace)
2848       continue;
2849     if (pspace->executing_startup)
2850       continue;
2851
2852     set_current_program_space (pspace);
2853
2854     memset (&local, 0, sizeof (local));
2855     local.funfirstline = info->state->funfirstline;
2856     local.list_mode = info->state->list_mode;
2857
2858     cleanup = make_cleanup (VEC_cleanup (minsym_and_objfile_d),
2859                             &local.msyms);
2860
2861     ALL_OBJFILES (objfile)
2862     {
2863       local.objfile = objfile;
2864       iterate_over_minimal_symbols (objfile, name, add_minsym, &local);
2865     }
2866
2867     if (!VEC_empty (minsym_and_objfile_d, local.msyms))
2868       {
2869         int classification;
2870         int ix;
2871         minsym_and_objfile_d *item;
2872
2873         qsort (VEC_address (minsym_and_objfile_d, local.msyms),
2874                VEC_length (minsym_and_objfile_d, local.msyms),
2875                sizeof (minsym_and_objfile_d),
2876                compare_msyms);
2877
2878         /* Now the minsyms are in classification order.  So, we walk
2879            over them and process just the minsyms with the same
2880            classification as the very first minsym in the list.  */
2881         item = VEC_index (minsym_and_objfile_d, local.msyms, 0);
2882         classification = classify_mtype (MSYMBOL_TYPE (item->minsym));
2883
2884         for (ix = 0;
2885              VEC_iterate (minsym_and_objfile_d, local.msyms, ix, item);
2886              ++ix)
2887           {
2888             if (classify_mtype (MSYMBOL_TYPE (item->minsym)) != classification)
2889               break;
2890
2891             minsym_found (info->state, item->objfile, item->minsym,
2892                           &info->result);
2893           }
2894       }
2895
2896     do_cleanups (cleanup);
2897   }
2898 }
2899
2900 /* A helper function to add all symbols matching NAME to INFO.  If
2901    PSPACE is not NULL, the search is restricted to just that program
2902    space.  */
2903
2904 static void
2905 add_matching_symbols_to_info (const char *name,
2906                               struct collect_info *info,
2907                               struct program_space *pspace)
2908 {
2909   int ix;
2910   struct symtab *elt;
2911
2912   for (ix = 0; VEC_iterate (symtab_p, info->state->file_symtabs, ix, elt); ++ix)
2913     {
2914       struct symbol *sym;
2915
2916       if (elt == NULL)
2917         {
2918           iterate_over_all_matching_symtabs (name, VAR_DOMAIN,
2919                                              collect_symbols, info,
2920                                              pspace);
2921           search_minsyms_for_name (info, name, pspace);
2922         }
2923       else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt))
2924         {
2925           /* Program spaces that are executing startup should have
2926              been filtered out earlier.  */
2927           gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
2928           set_current_program_space (SYMTAB_PSPACE (elt));
2929           LA_ITERATE_OVER_SYMBOLS (get_search_block (elt), name,
2930                                    VAR_DOMAIN, collect_symbols,
2931                                    info);
2932         }
2933     }
2934 }
2935
2936 /* Decode a linespec that's a variable.  If FILE_SYMTAB is non-NULL,
2937    look in that symtab's static variables first.  */ 
2938
2939 static struct symtabs_and_lines
2940 decode_variable (struct linespec_state *self, char *copy)
2941 {
2942   struct collect_info info;
2943   const char *lookup_name;
2944   char *canon;
2945   struct cleanup *cleanup;
2946
2947   info.state = self;
2948   info.result.sals = NULL;
2949   info.result.nelts = 0;
2950
2951   cleanup = demangle_for_lookup (copy, current_language->la_language,
2952                                  &lookup_name);
2953   if (current_language->la_language == language_ada)
2954     {
2955       /* In Ada, the symbol lookups are performed using the encoded
2956          name rather than the demangled name.  */
2957       lookup_name = ada_name_for_lookup (copy);
2958       make_cleanup (xfree, (void *) lookup_name);
2959     }
2960
2961   canon = cp_canonicalize_string_no_typedefs (lookup_name);
2962   if (canon != NULL)
2963     {
2964       make_cleanup (xfree, canon);
2965       lookup_name = canon;
2966     }
2967
2968   add_matching_symbols_to_info (lookup_name, &info, NULL);
2969
2970   if (info.result.nelts > 0)
2971     {
2972       if (self->canonical)
2973         {
2974           self->canonical->pre_expanded = 1;
2975           if (self->user_filename)
2976             self->canonical->addr_string
2977               = xstrprintf ("%s:%s", self->user_filename, copy);
2978           else
2979             self->canonical->addr_string = xstrdup (copy);
2980         }
2981       return info.result;
2982     }
2983
2984   if (!have_full_symbols ()
2985       && !have_partial_symbols ()
2986       && !have_minimal_symbols ())
2987     throw_error (NOT_FOUND_ERROR,
2988                  _("No symbol table is loaded.  Use the \"file\" command."));
2989   if (self->user_filename)
2990     throw_error (NOT_FOUND_ERROR, _("Function \"%s\" not defined in \"%s\"."),
2991                  copy, self->user_filename);
2992   else
2993     throw_error (NOT_FOUND_ERROR, _("Function \"%s\" not defined."), copy);
2994 }
2995
2996
2997 \f
2998
2999 /* Now come some functions that are called from multiple places within
3000    decode_line_1.  */
3001
3002 static int
3003 symbol_to_sal (struct symtab_and_line *result,
3004                int funfirstline, struct symbol *sym)
3005 {
3006   if (SYMBOL_CLASS (sym) == LOC_BLOCK)
3007     {
3008       *result = find_function_start_sal (sym, funfirstline);
3009       return 1;
3010     }
3011   else
3012     {
3013       if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
3014         {
3015           init_sal (result);
3016           result->symtab = SYMBOL_SYMTAB (sym);
3017           result->line = SYMBOL_LINE (sym);
3018           result->pc = SYMBOL_VALUE_ADDRESS (sym);
3019           result->pspace = SYMTAB_PSPACE (SYMBOL_SYMTAB (sym));
3020           result->explicit_pc = 1;
3021           return 1;
3022         }
3023       else if (funfirstline)
3024         {
3025           /* Nothing.  */
3026         }
3027       else if (SYMBOL_LINE (sym) != 0)
3028         {
3029           /* We know its line number.  */
3030           init_sal (result);
3031           result->symtab = SYMBOL_SYMTAB (sym);
3032           result->line = SYMBOL_LINE (sym);
3033           result->pspace = SYMTAB_PSPACE (SYMBOL_SYMTAB (sym));
3034           return 1;
3035         }
3036     }
3037
3038   return 0;
3039 }
3040
3041 /* See the comment in linespec.h.  */
3042
3043 void
3044 init_linespec_result (struct linespec_result *lr)
3045 {
3046   memset (lr, 0, sizeof (*lr));
3047 }
3048
3049 /* See the comment in linespec.h.  */
3050
3051 void
3052 destroy_linespec_result (struct linespec_result *ls)
3053 {
3054   int i;
3055   struct linespec_sals *lsal;
3056
3057   xfree (ls->addr_string);
3058   for (i = 0; VEC_iterate (linespec_sals, ls->sals, i, lsal); ++i)
3059     {
3060       xfree (lsal->canonical);
3061       xfree (lsal->sals.sals);
3062     }
3063   VEC_free (linespec_sals, ls->sals);
3064 }
3065
3066 /* Cleanup function for a linespec_result.  */
3067
3068 static void
3069 cleanup_linespec_result (void *a)
3070 {
3071   destroy_linespec_result (a);
3072 }
3073
3074 /* See the comment in linespec.h.  */
3075
3076 struct cleanup *
3077 make_cleanup_destroy_linespec_result (struct linespec_result *ls)
3078 {
3079   return make_cleanup (cleanup_linespec_result, ls);
3080 }