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