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