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