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