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