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