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