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