Add missing changelog entries of last commit
[deliverable/binutils-gdb.git] / gdb / linespec.c
CommitLineData
50641945 1/* Parser for linespec for the GNU debugger, GDB.
05ff989b 2
32d0add0 3 Copyright (C) 1986-2015 Free Software Foundation, Inc.
50641945
FN
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
50641945
FN
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
50641945
FN
19
20#include "defs.h"
21#include "symtab.h"
c5f0f3d0
FN
22#include "frame.h"
23#include "command.h"
50641945
FN
24#include "symfile.h"
25#include "objfiles.h"
0378c332 26#include "source.h"
50641945 27#include "demangle.h"
c5f0f3d0
FN
28#include "value.h"
29#include "completer.h"
015a42b4 30#include "cp-abi.h"
12907978 31#include "cp-support.h"
c38da1af 32#include "parser-defs.h"
fe898f56 33#include "block.h"
d2630e69 34#include "objc-lang.h"
b9362cc7 35#include "linespec.h"
53c5240f 36#include "language.h"
dc67126b
NR
37#include "interps.h"
38#include "mi/mi-cmds.h"
bccdca4a 39#include "target.h"
94af9270 40#include "arch-utils.h"
c00f8484
KS
41#include <ctype.h>
42#include "cli/cli-utils.h"
731971ed 43#include "filenames.h"
f8eba3c6 44#include "ada-lang.h"
39cf75f7 45#include "stack.h"
f8eba3c6 46
f8eba3c6
TT
47typedef struct symbol *symbolp;
48DEF_VEC_P (symbolp);
49
50typedef struct type *typep;
51DEF_VEC_P (typep);
52
53/* An address entry is used to ensure that any given location is only
54 added to the result a single time. It holds an address and the
55 program space from which the address came. */
56
57struct address_entry
58{
59 struct program_space *pspace;
60 CORE_ADDR addr;
61};
62
f60e2d5c 63typedef struct bound_minimal_symbol bound_minimal_symbol_d;
40e084e1 64
f60e2d5c 65DEF_VEC_O (bound_minimal_symbol_d);
40e084e1
KS
66
67/* An enumeration of possible signs for a line offset. */
68enum offset_relative_sign
69{
70 /* No sign */
71 LINE_OFFSET_NONE,
72
73 /* A plus sign ("+") */
74 LINE_OFFSET_PLUS,
75
76 /* A minus sign ("-") */
77 LINE_OFFSET_MINUS,
78
79 /* A special "sign" for unspecified offset. */
80 LINE_OFFSET_UNKNOWN
81};
82
83/* A line offset in a linespec. */
84
85struct line_offset
86{
87 /* Line offset and any specified sign. */
88 int offset;
89 enum offset_relative_sign sign;
90};
91
92/* A linespec. Elements of this structure are filled in by a parser
93 (either parse_linespec or some other function). The structure is
94 then converted into SALs by convert_linespec_to_sals. */
95
96struct linespec
97{
98 /* An expression and the resulting PC. Specifying an expression
99 currently precludes the use of other members. */
100
101 /* The expression entered by the user. */
5d94e27b 102 const char *expression;
40e084e1
KS
103
104 /* The resulting PC expression derived from evaluating EXPRESSION. */
105 CORE_ADDR expr_pc;
106
107 /* Any specified file symtabs. */
108
109 /* The user-supplied source filename or NULL if none was specified. */
5d94e27b 110 const char *source_filename;
40e084e1
KS
111
112 /* The list of symtabs to search to which to limit the search. May not
113 be NULL. If SOURCE_FILENAME is NULL (no user-specified filename),
114 FILE_SYMTABS should contain one single NULL member. This will
115 cause the code to use the default symtab. */
ec94af83 116 VEC (symtab_ptr) *file_symtabs;
40e084e1
KS
117
118 /* The name of a function or method and any matching symbols. */
119
120 /* The user-specified function name. If no function name was
121 supplied, this may be NULL. */
5d94e27b 122 const char *function_name;
40e084e1
KS
123
124 /* A list of matching function symbols and minimal symbols. Both lists
125 may be NULL if no matching symbols were found. */
126 VEC (symbolp) *function_symbols;
f60e2d5c 127 VEC (bound_minimal_symbol_d) *minimal_symbols;
40e084e1
KS
128
129 /* The name of a label and matching symbols. */
130
131 /* The user-specified label name. */
5d94e27b 132 const char *label_name;
40e084e1
KS
133
134 /* A structure of matching label symbols and the corresponding
135 function symbol in which the label was found. Both may be NULL
136 or both must be non-NULL. */
137 struct
138 {
139 VEC (symbolp) *label_symbols;
140 VEC (symbolp) *function_symbols;
141 } labels;
142
143 /* Line offset. It may be LINE_OFFSET_UNKNOWN, meaning that no
144 offset was specified. */
145 struct line_offset line_offset;
146};
147typedef struct linespec *linespec_p;
148
33f448b1
JK
149/* A canonical linespec represented as a symtab-related string.
150
151 Each entry represents the "SYMTAB:SUFFIX" linespec string.
152 SYMTAB can be converted for example by symtab_to_fullname or
153 symtab_to_filename_for_display as needed. */
154
155struct linespec_canonical_name
156{
157 /* Remaining text part of the linespec string. */
158 char *suffix;
159
160 /* If NULL then SUFFIX is the whole linespec string. */
161 struct symtab *symtab;
162};
163
f8eba3c6
TT
164/* An instance of this is used to keep all state while linespec
165 operates. This instance is passed around as a 'this' pointer to
166 the various implementation methods. */
167
168struct linespec_state
169{
40e084e1
KS
170 /* The language in use during linespec processing. */
171 const struct language_defn *language;
172
f8eba3c6
TT
173 /* The program space as seen when the module was entered. */
174 struct program_space *program_space;
175
176 /* The default symtab to use, if no other symtab is specified. */
177 struct symtab *default_symtab;
178
179 /* The default line to use. */
180 int default_line;
181
f8eba3c6
TT
182 /* The 'funfirstline' value that was passed in to decode_line_1 or
183 decode_line_full. */
184 int funfirstline;
185
186 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
187 int list_mode;
188
189 /* The 'canonical' value passed to decode_line_full, or NULL. */
190 struct linespec_result *canonical;
191
192 /* Canonical strings that mirror the symtabs_and_lines result. */
33f448b1 193 struct linespec_canonical_name *canonical_names;
f8eba3c6
TT
194
195 /* This is a set of address_entry objects which is used to prevent
196 duplicate symbols from being entered into the result. */
197 htab_t addr_set;
198};
199
200/* This is a helper object that is used when collecting symbols into a
201 result. */
202
203struct collect_info
204{
205 /* The linespec object in use. */
206 struct linespec_state *state;
207
40e084e1 208 /* A list of symtabs to which to restrict matches. */
ec94af83 209 VEC (symtab_ptr) *file_symtabs;
40e084e1 210
f8eba3c6 211 /* The result being accumulated. */
40e084e1
KS
212 struct
213 {
214 VEC (symbolp) *symbols;
f60e2d5c 215 VEC (bound_minimal_symbol_d) *minimal_symbols;
40e084e1 216 } result;
f8eba3c6 217};
50641945 218
40e084e1 219/* Token types */
50641945 220
40e084e1
KS
221enum ls_token_type
222{
223 /* A keyword */
224 LSTOKEN_KEYWORD = 0,
44fe14ab 225
40e084e1
KS
226 /* A colon "separator" */
227 LSTOKEN_COLON,
44fe14ab 228
40e084e1
KS
229 /* A string */
230 LSTOKEN_STRING,
0960f083 231
40e084e1
KS
232 /* A number */
233 LSTOKEN_NUMBER,
234
235 /* A comma */
236 LSTOKEN_COMMA,
237
238 /* EOI (end of input) */
239 LSTOKEN_EOI,
240
241 /* Consumed token */
242 LSTOKEN_CONSUMED
243};
244typedef enum ls_token_type linespec_token_type;
245
246/* List of keywords */
247
248static const char * const linespec_keywords[] = { "if", "thread", "task" };
0578b14e 249#define IF_KEYWORD_INDEX 0
40e084e1
KS
250
251/* A token of the linespec lexer */
252
253struct ls_token
254{
255 /* The type of the token */
256 linespec_token_type type;
257
258 /* Data for the token */
259 union
260 {
261 /* A string, given as a stoken */
262 struct stoken string;
263
264 /* A keyword */
265 const char *keyword;
266 } data;
267};
268typedef struct ls_token linespec_token;
269
270#define LS_TOKEN_STOKEN(TOK) (TOK).data.string
271#define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
272
273/* An instance of the linespec parser. */
274
275struct ls_parser
276{
277 /* Lexer internal data */
278 struct
279 {
280 /* Save head of input stream. */
d7561cbb 281 const char *saved_arg;
d2630e69 282
40e084e1 283 /* Head of the input stream. */
d7561cbb 284 const char **stream;
40e084e1 285#define PARSER_STREAM(P) (*(P)->lexer.stream)
614b3b14 286
40e084e1
KS
287 /* The current token. */
288 linespec_token current;
289 } lexer;
93d91629 290
40e084e1
KS
291 /* Is the entire linespec quote-enclosed? */
292 int is_quote_enclosed;
293
294 /* The state of the parse. */
295 struct linespec_state state;
296#define PARSER_STATE(PPTR) (&(PPTR)->state)
4224873a 297
40e084e1
KS
298 /* The result of the parse. */
299 struct linespec result;
300#define PARSER_RESULT(PPTR) (&(PPTR)->result)
301};
302typedef struct ls_parser linespec_parser;
50641945 303
40e084e1 304/* Prototypes for local functions. */
50641945 305
4eeaa230
DE
306static void iterate_over_file_blocks (struct symtab *symtab,
307 const char *name, domain_enum domain,
308 symbol_found_callback_ftype *callback,
309 void *data);
310
40e084e1
KS
311static void initialize_defaults (struct symtab **default_symtab,
312 int *default_line);
50641945 313
bbc13ae3 314static CORE_ADDR linespec_expression_to_pc (const char **exp_ptr);
aee8d8ba 315
40e084e1
KS
316static struct symtabs_and_lines decode_objc (struct linespec_state *self,
317 linespec_p ls,
d7561cbb 318 const char **argptr);
aee8d8ba 319
ec94af83 320static VEC (symtab_ptr) *symtabs_from_filename (const char *);
50641945 321
40e084e1
KS
322static VEC (symbolp) *find_label_symbols (struct linespec_state *self,
323 VEC (symbolp) *function_symbols,
324 VEC (symbolp) **label_funcs_ret,
325 const char *name);
50641945 326
b1ae631a 327static void find_linespec_symbols (struct linespec_state *self,
ec94af83 328 VEC (symtab_ptr) *file_symtabs,
b1ae631a
DE
329 const char *name,
330 VEC (symbolp) **symbols,
f60e2d5c 331 VEC (bound_minimal_symbol_d) **minsyms);
f8eba3c6 332
40e084e1
KS
333static struct line_offset
334 linespec_parse_variable (struct linespec_state *self,
335 const char *variable);
889f28e2 336
f8eba3c6
TT
337static int symbol_to_sal (struct symtab_and_line *result,
338 int funfirstline, struct symbol *sym);
50641945 339
f8eba3c6
TT
340static void add_matching_symbols_to_info (const char *name,
341 struct collect_info *info,
342 struct program_space *pspace);
f3c39e76 343
f8eba3c6
TT
344static void add_all_symbol_names_from_pspace (struct collect_info *info,
345 struct program_space *pspace,
346 VEC (const_char_ptr) *names);
9ef07c8c 347
ec94af83 348static VEC (symtab_ptr) *collect_symtabs_from_filename (const char *file);
84fba31b 349
40e084e1
KS
350static void decode_digits_ordinary (struct linespec_state *self,
351 linespec_p ls,
352 int line,
353 struct symtabs_and_lines *sals,
354 struct linetable_entry **best_entry);
14e91ac5 355
40e084e1
KS
356static void decode_digits_list_mode (struct linespec_state *self,
357 linespec_p ls,
358 struct symtabs_and_lines *values,
359 struct symtab_and_line val);
0f5238ed 360
40e084e1
KS
361static void minsym_found (struct linespec_state *self, struct objfile *objfile,
362 struct minimal_symbol *msymbol,
363 struct symtabs_and_lines *result);
bca02a8a 364
40e084e1 365static int compare_symbols (const void *a, const void *b);
413dad4d 366
40e084e1 367static int compare_msymbols (const void *a, const void *b);
413dad4d 368
40e084e1 369static const char *find_toplevel_char (const char *s, char c);
f8eba3c6 370
40e084e1
KS
371/* Permitted quote characters for the parser. This is different from the
372 completer's quote characters to allow backward compatibility with the
373 previous parser. */
374static const char *const linespec_quote_characters = "\"\'";
f8eba3c6 375
40e084e1
KS
376/* Lexer functions. */
377
378/* Lex a number from the input in PARSER. This only supports
dd3818c8
KS
379 decimal numbers.
380
d7cbec71 381 Return true if input is decimal numbers. Return false if not. */
40e084e1 382
d7cbec71
HZ
383static int
384linespec_lexer_lex_number (linespec_parser *parser, linespec_token *tokenp)
40e084e1 385{
d7cbec71
HZ
386 tokenp->type = LSTOKEN_NUMBER;
387 LS_TOKEN_STOKEN (*tokenp).length = 0;
388 LS_TOKEN_STOKEN (*tokenp).ptr = PARSER_STREAM (parser);
40e084e1
KS
389
390 /* Keep any sign at the start of the stream. */
391 if (*PARSER_STREAM (parser) == '+' || *PARSER_STREAM (parser) == '-')
392 {
d7cbec71 393 ++LS_TOKEN_STOKEN (*tokenp).length;
40e084e1
KS
394 ++(PARSER_STREAM (parser));
395 }
396
397 while (isdigit (*PARSER_STREAM (parser)))
398 {
d7cbec71 399 ++LS_TOKEN_STOKEN (*tokenp).length;
40e084e1 400 ++(PARSER_STREAM (parser));
f8eba3c6 401 }
40e084e1 402
dd3818c8 403 /* If the next character in the input buffer is not a space, comma,
eff9c3e6 404 quote, or colon, this input does not represent a number. */
dd3818c8
KS
405 if (*PARSER_STREAM (parser) != '\0'
406 && !isspace (*PARSER_STREAM (parser)) && *PARSER_STREAM (parser) != ','
eff9c3e6
KS
407 && *PARSER_STREAM (parser) != ':'
408 && !strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
d7cbec71
HZ
409 {
410 PARSER_STREAM (parser) = LS_TOKEN_STOKEN (*tokenp).ptr;
411 return 0;
412 }
413
414 return 1;
f8eba3c6
TT
415}
416
40e084e1
KS
417/* Does P represent one of the keywords? If so, return
418 the keyword. If not, return NULL. */
f8eba3c6 419
0578b14e 420const char *
40e084e1 421linespec_lexer_lex_keyword (const char *p)
f8eba3c6 422{
40e084e1 423 int i;
f8eba3c6 424
40e084e1
KS
425 if (p != NULL)
426 {
427 for (i = 0; i < ARRAY_SIZE (linespec_keywords); ++i)
428 {
429 int len = strlen (linespec_keywords[i]);
430
431 /* If P begins with one of the keywords and the next
0578b14e
KS
432 character is whitespace, we may have found a keyword.
433 It is only a keyword if it is not followed by another
434 keyword. */
40e084e1 435 if (strncmp (p, linespec_keywords[i], len) == 0
0578b14e
KS
436 && isspace (p[len]))
437 {
438 int j;
439
440 /* Special case: "if" ALWAYS stops the lexer, since it
441 is not possible to predict what is going to appear in
442 the condition, which can only be parsed after SaLs have
443 been found. */
444 if (i != IF_KEYWORD_INDEX)
445 {
446 p += len;
447 p = skip_spaces_const (p);
448 for (j = 0; j < ARRAY_SIZE (linespec_keywords); ++j)
449 {
450 int nextlen = strlen (linespec_keywords[j]);
451
452 if (strncmp (p, linespec_keywords[j], nextlen) == 0
453 && isspace (p[nextlen]))
454 return NULL;
455 }
456 }
457
458 return linespec_keywords[i];
459 }
40e084e1
KS
460 }
461 }
462
463 return NULL;
f8eba3c6
TT
464}
465
40e084e1
KS
466/* Does STRING represent an Ada operator? If so, return the length
467 of the decoded operator name. If not, return 0. */
f8eba3c6
TT
468
469static int
40e084e1 470is_ada_operator (const char *string)
f8eba3c6 471{
40e084e1 472 const struct ada_opname_map *mapping;
f8eba3c6 473
40e084e1
KS
474 for (mapping = ada_opname_table;
475 mapping->encoded != NULL
61012eef 476 && !startswith (string, mapping->decoded); ++mapping)
40e084e1
KS
477 ;
478
479 return mapping->decoded == NULL ? 0 : strlen (mapping->decoded);
f8eba3c6
TT
480}
481
40e084e1
KS
482/* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
483 the location of QUOTE_CHAR, or NULL if not found. */
f8eba3c6 484
40e084e1
KS
485static const char *
486skip_quote_char (const char *string, char quote_char)
f8eba3c6 487{
40e084e1 488 const char *p, *last;
f8eba3c6 489
40e084e1
KS
490 p = last = find_toplevel_char (string, quote_char);
491 while (p && *p != '\0' && *p != ':')
492 {
493 p = find_toplevel_char (p, quote_char);
494 if (p != NULL)
495 last = p++;
496 }
f8eba3c6 497
40e084e1 498 return last;
f8eba3c6 499}
50641945 500
40e084e1
KS
501/* Make a writable copy of the string given in TOKEN, trimming
502 any trailing whitespace. */
50641945 503
40e084e1
KS
504static char *
505copy_token_string (linespec_token token)
50641945 506{
40e084e1 507 char *str, *s;
e0881a8e 508
40e084e1
KS
509 if (token.type == LSTOKEN_KEYWORD)
510 return xstrdup (LS_TOKEN_KEYWORD (token));
255e7dbf 511
40e084e1
KS
512 str = savestring (LS_TOKEN_STOKEN (token).ptr,
513 LS_TOKEN_STOKEN (token).length);
514 s = remove_trailing_whitespace (str, str + LS_TOKEN_STOKEN (token).length);
515 *s = '\0';
e0881a8e 516
40e084e1
KS
517 return str;
518}
255e7dbf 519
40e084e1 520/* Does P represent the end of a quote-enclosed linespec? */
f3a5f1de 521
40e084e1
KS
522static int
523is_closing_quote_enclosed (const char *p)
524{
525 if (strchr (linespec_quote_characters, *p))
526 ++p;
527 p = skip_spaces ((char *) p);
528 return (*p == '\0' || linespec_lexer_lex_keyword (p));
50641945
FN
529}
530
40e084e1
KS
531/* Find the end of the parameter list that starts with *INPUT.
532 This helper function assists with lexing string segments
533 which might contain valid (non-terminating) commas. */
481860b3 534
d7561cbb
KS
535static const char *
536find_parameter_list_end (const char *input)
481860b3 537{
40e084e1
KS
538 char end_char, start_char;
539 int depth;
d7561cbb 540 const char *p;
481860b3 541
40e084e1
KS
542 start_char = *input;
543 if (start_char == '(')
544 end_char = ')';
545 else if (start_char == '<')
546 end_char = '>';
547 else
548 return NULL;
481860b3 549
40e084e1
KS
550 p = input;
551 depth = 0;
552 while (*p)
481860b3 553 {
40e084e1
KS
554 if (*p == start_char)
555 ++depth;
556 else if (*p == end_char)
557 {
558 if (--depth == 0)
559 {
560 ++p;
561 break;
562 }
563 }
564 ++p;
481860b3 565 }
40e084e1
KS
566
567 return p;
481860b3
GB
568}
569
74ccd7f5 570
40e084e1
KS
571/* Lex a string from the input in PARSER. */
572
573static linespec_token
574linespec_lexer_lex_string (linespec_parser *parser)
74ccd7f5 575{
40e084e1 576 linespec_token token;
d7561cbb 577 const char *start = PARSER_STREAM (parser);
74ccd7f5 578
40e084e1 579 token.type = LSTOKEN_STRING;
74ccd7f5 580
40e084e1
KS
581 /* If the input stream starts with a quote character, skip to the next
582 quote character, regardless of the content. */
583 if (strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
584 {
585 const char *end;
586 char quote_char = *PARSER_STREAM (parser);
50641945 587
40e084e1
KS
588 /* Special case: Ada operators. */
589 if (PARSER_STATE (parser)->language->la_language == language_ada
590 && quote_char == '\"')
591 {
592 int len = is_ada_operator (PARSER_STREAM (parser));
50641945 593
40e084e1
KS
594 if (len != 0)
595 {
596 /* The input is an Ada operator. Return the quoted string
597 as-is. */
598 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
599 LS_TOKEN_STOKEN (token).length = len;
600 PARSER_STREAM (parser) += len;
601 return token;
602 }
f8eba3c6 603
40e084e1
KS
604 /* The input does not represent an Ada operator -- fall through
605 to normal quoted string handling. */
606 }
f8eba3c6 607
40e084e1
KS
608 /* Skip past the beginning quote. */
609 ++(PARSER_STREAM (parser));
74ccd7f5 610
40e084e1
KS
611 /* Mark the start of the string. */
612 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
f8eba3c6 613
40e084e1
KS
614 /* Skip to the ending quote. */
615 end = skip_quote_char (PARSER_STREAM (parser), quote_char);
616
617 /* Error if the input did not terminate properly. */
618 if (end == NULL)
619 error (_("unmatched quote"));
620
621 /* Skip over the ending quote and mark the length of the string. */
622 PARSER_STREAM (parser) = (char *) ++end;
623 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 2 - start;
624 }
625 else
626 {
d7561cbb 627 const char *p;
40e084e1
KS
628
629 /* Otherwise, only identifier characters are permitted.
630 Spaces are the exception. In general, we keep spaces,
631 but only if the next characters in the input do not resolve
632 to one of the keywords.
633
634 This allows users to forgo quoting CV-qualifiers, template arguments,
635 and similar common language constructs. */
636
637 while (1)
638 {
639 if (isspace (*PARSER_STREAM (parser)))
640 {
d7561cbb 641 p = skip_spaces_const (PARSER_STREAM (parser));
7c09e5a0
DE
642 /* When we get here we know we've found something followed by
643 a space (we skip over parens and templates below).
644 So if we find a keyword now, we know it is a keyword and not,
645 say, a function name. */
40e084e1
KS
646 if (linespec_lexer_lex_keyword (p) != NULL)
647 {
648 LS_TOKEN_STOKEN (token).ptr = start;
649 LS_TOKEN_STOKEN (token).length
650 = PARSER_STREAM (parser) - start;
651 return token;
652 }
653
654 /* Advance past the whitespace. */
655 PARSER_STREAM (parser) = p;
656 }
657
658 /* If the next character is EOI or (single) ':', the
659 string is complete; return the token. */
660 if (*PARSER_STREAM (parser) == 0)
661 {
662 LS_TOKEN_STOKEN (token).ptr = start;
663 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
664 return token;
665 }
666 else if (PARSER_STREAM (parser)[0] == ':')
667 {
668 /* Do not tokenize the C++ scope operator. */
669 if (PARSER_STREAM (parser)[1] == ':')
670 ++(PARSER_STREAM (parser));
671
672 /* Do not tokenify if the input length so far is one
673 (i.e, a single-letter drive name) and the next character
674 is a directory separator. This allows Windows-style
675 paths to be recognized as filenames without quoting it. */
676 else if ((PARSER_STREAM (parser) - start) != 1
677 || !IS_DIR_SEPARATOR (PARSER_STREAM (parser)[1]))
678 {
679 LS_TOKEN_STOKEN (token).ptr = start;
680 LS_TOKEN_STOKEN (token).length
681 = PARSER_STREAM (parser) - start;
682 return token;
683 }
684 }
685 /* Special case: permit quote-enclosed linespecs. */
686 else if (parser->is_quote_enclosed
687 && strchr (linespec_quote_characters,
688 *PARSER_STREAM (parser))
689 && is_closing_quote_enclosed (PARSER_STREAM (parser)))
690 {
691 LS_TOKEN_STOKEN (token).ptr = start;
692 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
693 return token;
694 }
695 /* Because commas may terminate a linespec and appear in
696 the middle of valid string input, special cases for
697 '<' and '(' are necessary. */
698 else if (*PARSER_STREAM (parser) == '<'
699 || *PARSER_STREAM (parser) == '(')
700 {
d7561cbb 701 const char *p;
40e084e1
KS
702
703 p = find_parameter_list_end (PARSER_STREAM (parser));
704 if (p != NULL)
705 {
706 PARSER_STREAM (parser) = p;
707 continue;
708 }
709 }
710 /* Commas are terminators, but not if they are part of an
711 operator name. */
712 else if (*PARSER_STREAM (parser) == ',')
713 {
714 if ((PARSER_STATE (parser)->language->la_language
715 == language_cplus)
716 && (PARSER_STREAM (parser) - start) > 8
717 /* strlen ("operator") */)
718 {
719 char *p = strstr (start, "operator");
720
721 if (p != NULL && is_operator_name (p))
722 {
723 /* This is an operator name. Keep going. */
724 ++(PARSER_STREAM (parser));
725 continue;
726 }
727 }
728
729 /* Comma terminates the string. */
730 LS_TOKEN_STOKEN (token).ptr = start;
731 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
732 return token;
733 }
734
735 /* Advance the stream. */
736 ++(PARSER_STREAM (parser));
737 }
738 }
739
740 return token;
741}
742
743/* Lex a single linespec token from PARSER. */
744
745static linespec_token
746linespec_lexer_lex_one (linespec_parser *parser)
747{
748 const char *keyword;
749
750 if (parser->lexer.current.type == LSTOKEN_CONSUMED)
751 {
752 /* Skip any whitespace. */
d7561cbb 753 PARSER_STREAM (parser) = skip_spaces_const (PARSER_STREAM (parser));
40e084e1 754
7c09e5a0 755 /* Check for a keyword, they end the linespec. */
0578b14e 756 keyword = linespec_lexer_lex_keyword (PARSER_STREAM (parser));
40e084e1
KS
757 if (keyword != NULL)
758 {
759 parser->lexer.current.type = LSTOKEN_KEYWORD;
760 LS_TOKEN_KEYWORD (parser->lexer.current) = keyword;
0578b14e
KS
761 /* We do not advance the stream here intentionally:
762 we would like lexing to stop when a keyword is seen.
763
764 PARSER_STREAM (parser) += strlen (keyword); */
765
40e084e1
KS
766 return parser->lexer.current;
767 }
768
769 /* Handle other tokens. */
770 switch (*PARSER_STREAM (parser))
771 {
772 case 0:
773 parser->lexer.current.type = LSTOKEN_EOI;
774 break;
775
776 case '+': case '-':
777 case '0': case '1': case '2': case '3': case '4':
778 case '5': case '6': case '7': case '8': case '9':
d7cbec71
HZ
779 if (!linespec_lexer_lex_number (parser, &(parser->lexer.current)))
780 parser->lexer.current = linespec_lexer_lex_string (parser);
40e084e1
KS
781 break;
782
783 case ':':
784 /* If we have a scope operator, lex the input as a string.
785 Otherwise, return LSTOKEN_COLON. */
786 if (PARSER_STREAM (parser)[1] == ':')
787 parser->lexer.current = linespec_lexer_lex_string (parser);
788 else
789 {
790 parser->lexer.current.type = LSTOKEN_COLON;
791 ++(PARSER_STREAM (parser));
792 }
793 break;
794
795 case '\'': case '\"':
796 /* Special case: permit quote-enclosed linespecs. */
797 if (parser->is_quote_enclosed
798 && is_closing_quote_enclosed (PARSER_STREAM (parser)))
799 {
800 ++(PARSER_STREAM (parser));
801 parser->lexer.current.type = LSTOKEN_EOI;
802 }
803 else
804 parser->lexer.current = linespec_lexer_lex_string (parser);
805 break;
806
807 case ',':
808 parser->lexer.current.type = LSTOKEN_COMMA;
809 LS_TOKEN_STOKEN (parser->lexer.current).ptr
810 = PARSER_STREAM (parser);
811 LS_TOKEN_STOKEN (parser->lexer.current).length = 1;
812 ++(PARSER_STREAM (parser));
813 break;
814
815 default:
816 /* If the input is not a number, it must be a string.
817 [Keywords were already considered above.] */
818 parser->lexer.current = linespec_lexer_lex_string (parser);
819 break;
820 }
821 }
822
823 return parser->lexer.current;
824}
825
826/* Consume the current token and return the next token in PARSER's
827 input stream. */
828
829static linespec_token
830linespec_lexer_consume_token (linespec_parser *parser)
831{
832 parser->lexer.current.type = LSTOKEN_CONSUMED;
833 return linespec_lexer_lex_one (parser);
834}
835
836/* Return the next token without consuming the current token. */
837
838static linespec_token
839linespec_lexer_peek_token (linespec_parser *parser)
840{
841 linespec_token next;
d7561cbb 842 const char *saved_stream = PARSER_STREAM (parser);
40e084e1
KS
843 linespec_token saved_token = parser->lexer.current;
844
845 next = linespec_lexer_consume_token (parser);
846 PARSER_STREAM (parser) = saved_stream;
847 parser->lexer.current = saved_token;
848 return next;
849}
850
851/* Helper functions. */
852
853/* Add SAL to SALS. */
854
855static void
856add_sal_to_sals_basic (struct symtabs_and_lines *sals,
857 struct symtab_and_line *sal)
858{
859 ++sals->nelts;
860 sals->sals = xrealloc (sals->sals, sals->nelts * sizeof (sals->sals[0]));
861 sals->sals[sals->nelts - 1] = *sal;
862}
863
864/* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
865 the new sal, if needed. If not NULL, SYMNAME is the name of the
66f1999b
KS
866 symbol to use when constructing the new canonical name.
867
868 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
869 canonical name for the SAL. */
40e084e1
KS
870
871static void
872add_sal_to_sals (struct linespec_state *self,
873 struct symtabs_and_lines *sals,
874 struct symtab_and_line *sal,
66f1999b 875 const char *symname, int literal_canonical)
40e084e1
KS
876{
877 add_sal_to_sals_basic (sals, sal);
878
879 if (self->canonical)
880 {
33f448b1 881 struct linespec_canonical_name *canonical;
40e084e1
KS
882
883 self->canonical_names = xrealloc (self->canonical_names,
33f448b1
JK
884 (sals->nelts
885 * sizeof (*self->canonical_names)));
886 canonical = &self->canonical_names[sals->nelts - 1];
4e04028d 887 if (!literal_canonical && sal->symtab)
40e084e1 888 {
05cba821 889 const char *fullname = symtab_to_fullname (sal->symtab);
40e084e1
KS
890
891 /* Note that the filter doesn't have to be a valid linespec
892 input. We only apply the ":LINE" treatment to Ada for
893 the time being. */
894 if (symname != NULL && sal->line != 0
895 && self->language->la_language == language_ada)
33f448b1 896 canonical->suffix = xstrprintf ("%s:%d", symname, sal->line);
40e084e1 897 else if (symname != NULL)
33f448b1 898 canonical->suffix = xstrdup (symname);
40e084e1 899 else
33f448b1
JK
900 canonical->suffix = xstrprintf ("%d", sal->line);
901 canonical->symtab = sal->symtab;
902 }
903 else
904 {
905 if (symname != NULL)
906 canonical->suffix = xstrdup (symname);
907 else
e617b069 908 canonical->suffix = xstrdup ("<unknown>");
33f448b1 909 canonical->symtab = NULL;
40e084e1 910 }
40e084e1
KS
911 }
912}
913
914/* A hash function for address_entry. */
915
916static hashval_t
917hash_address_entry (const void *p)
918{
919 const struct address_entry *aep = p;
920 hashval_t hash;
921
922 hash = iterative_hash_object (aep->pspace, 0);
923 return iterative_hash_object (aep->addr, hash);
924}
925
926/* An equality function for address_entry. */
927
928static int
929eq_address_entry (const void *a, const void *b)
930{
931 const struct address_entry *aea = a;
932 const struct address_entry *aeb = b;
933
934 return aea->pspace == aeb->pspace && aea->addr == aeb->addr;
935}
936
937/* Check whether the address, represented by PSPACE and ADDR, is
938 already in the set. If so, return 0. Otherwise, add it and return
939 1. */
940
941static int
942maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr)
943{
944 struct address_entry e, *p;
945 void **slot;
946
947 e.pspace = pspace;
948 e.addr = addr;
949 slot = htab_find_slot (set, &e, INSERT);
950 if (*slot)
951 return 0;
952
953 p = XNEW (struct address_entry);
954 memcpy (p, &e, sizeof (struct address_entry));
955 *slot = p;
956
957 return 1;
958}
959
960/* A callback function and the additional data to call it with. */
961
962struct symbol_and_data_callback
963{
964 /* The callback to use. */
965 symbol_found_callback_ftype *callback;
966
967 /* Data to be passed to the callback. */
968 void *data;
969};
970
971/* A helper for iterate_over_all_matching_symtabs that is used to
972 restrict calls to another callback to symbols representing inline
973 symbols only. */
974
975static int
976iterate_inline_only (struct symbol *sym, void *d)
977{
978 if (SYMBOL_INLINED (sym))
979 {
980 struct symbol_and_data_callback *cad = d;
981
982 return cad->callback (sym, cad->data);
983 }
984 return 1; /* Continue iterating. */
985}
986
987/* Some data for the expand_symtabs_matching callback. */
988
989struct symbol_matcher_data
990{
991 /* The lookup name against which symbol name should be compared. */
992 const char *lookup_name;
993
994 /* The routine to be used for comparison. */
995 symbol_name_cmp_ftype symbol_name_cmp;
996};
997
998/* A helper for iterate_over_all_matching_symtabs that is passed as a
999 callback to the expand_symtabs_matching method. */
1000
1001static int
1002iterate_name_matcher (const char *name, void *d)
1003{
1004 const struct symbol_matcher_data *data = d;
1005
642a8d80 1006 if (data->symbol_name_cmp (name, data->lookup_name) == 0)
40e084e1
KS
1007 return 1; /* Expand this symbol's symbol table. */
1008 return 0; /* Skip this symbol. */
1009}
1010
1011/* A helper that walks over all matching symtabs in all objfiles and
1012 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
1013 not NULL, then the search is restricted to just that program
1014 space. If INCLUDE_INLINE is nonzero then symbols representing
1015 inlined instances of functions will be included in the result. */
1016
1017static void
1018iterate_over_all_matching_symtabs (struct linespec_state *state,
1019 const char *name,
1020 const domain_enum domain,
1021 symbol_found_callback_ftype *callback,
1022 void *data,
1023 struct program_space *search_pspace,
1024 int include_inline)
1025{
1026 struct objfile *objfile;
1027 struct program_space *pspace;
1028 struct symbol_matcher_data matcher_data;
1029
1030 matcher_data.lookup_name = name;
1031 matcher_data.symbol_name_cmp =
1032 state->language->la_get_symbol_name_cmp != NULL
1033 ? state->language->la_get_symbol_name_cmp (name)
1034 : strcmp_iw;
1035
1036 ALL_PSPACES (pspace)
1037 {
1038 if (search_pspace != NULL && search_pspace != pspace)
1039 continue;
1040 if (pspace->executing_startup)
f8eba3c6
TT
1041 continue;
1042
1043 set_current_program_space (pspace);
50641945 1044
f8eba3c6
TT
1045 ALL_OBJFILES (objfile)
1046 {
43f3e411 1047 struct compunit_symtab *cu;
f8eba3c6
TT
1048
1049 if (objfile->sf)
1050 objfile->sf->qf->expand_symtabs_matching (objfile, NULL,
1051 iterate_name_matcher,
276d885b 1052 NULL, ALL_DOMAIN,
74ccd7f5 1053 &matcher_data);
f8eba3c6 1054
43f3e411 1055 ALL_OBJFILE_COMPUNITS (objfile, cu)
f8eba3c6 1056 {
43f3e411
DE
1057 struct symtab *symtab = COMPUNIT_FILETABS (cu);
1058
4eeaa230 1059 iterate_over_file_blocks (symtab, name, domain, callback, data);
481860b3 1060
d790cf0a
DE
1061 if (include_inline)
1062 {
1063 struct symbol_and_data_callback cad = { callback, data };
4eeaa230 1064 struct block *block;
d790cf0a 1065 int i;
481860b3 1066
d790cf0a 1067 for (i = FIRST_LOCAL_BLOCK;
439247b6
DE
1068 i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab));
1069 i++)
d790cf0a 1070 {
439247b6 1071 block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i);
4ae24af0
JB
1072 state->language->la_iterate_over_symbols
1073 (block, name, domain, iterate_inline_only, &cad);
481860b3 1074 }
f8eba3c6
TT
1075 }
1076 }
1077 }
1078 }
50641945
FN
1079}
1080
4eeaa230
DE
1081/* Returns the block to be used for symbol searches from
1082 the current location. */
e8eb7bc5 1083
3977b71f 1084static const struct block *
e482a1a7 1085get_current_search_block (void)
e8eb7bc5 1086{
3977b71f 1087 const struct block *block;
4eeaa230 1088 enum language save_language;
e8eb7bc5 1089
4eeaa230
DE
1090 /* get_selected_block can change the current language when there is
1091 no selected frame yet. */
1092 save_language = current_language->la_language;
1093 block = get_selected_block (0);
1094 set_language (save_language);
e8eb7bc5
KS
1095
1096 return block;
1097}
1098
4eeaa230
DE
1099/* Iterate over static and global blocks. */
1100
1101static void
1102iterate_over_file_blocks (struct symtab *symtab,
1103 const char *name, domain_enum domain,
1104 symbol_found_callback_ftype *callback, void *data)
1105{
1106 struct block *block;
1107
439247b6 1108 for (block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK);
4eeaa230
DE
1109 block != NULL;
1110 block = BLOCK_SUPERBLOCK (block))
1111 LA_ITERATE_OVER_SYMBOLS (block, name, domain, callback, data);
1112}
1113
f8eba3c6 1114/* A helper for find_method. This finds all methods in type T which
40e084e1 1115 match NAME. It adds matching symbol names to RESULT_NAMES, and
f8eba3c6 1116 adds T's direct superclasses to SUPERCLASSES. */
50641945 1117
f8eba3c6
TT
1118static void
1119find_methods (struct type *t, const char *name,
1120 VEC (const_char_ptr) **result_names,
1121 VEC (typep) **superclasses)
50641945 1122{
50641945 1123 int ibase;
0d5cff50 1124 const char *class_name = type_name_no_tag (t);
c00f8484 1125
50641945
FN
1126 /* Ignore this class if it doesn't have a name. This is ugly, but
1127 unless we figure out how to get the physname without the name of
1128 the class, then the loop can't do any good. */
f8eba3c6 1129 if (class_name)
50641945
FN
1130 {
1131 int method_counter;
1132
8bd1f2c6 1133 CHECK_TYPEDEF (t);
50641945
FN
1134
1135 /* Loop over each method name. At this level, all overloads of a name
1136 are counted as a single name. There is an inner loop which loops over
1137 each overload. */
1138
1139 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1140 method_counter >= 0;
1141 --method_counter)
1142 {
0d5cff50 1143 const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
50641945
FN
1144 char dem_opname[64];
1145
61012eef
GB
1146 if (startswith (method_name, "__") ||
1147 startswith (method_name, "op") ||
1148 startswith (method_name, "type"))
50641945
FN
1149 {
1150 if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
1151 method_name = dem_opname;
1152 else if (cplus_demangle_opname (method_name, dem_opname, 0))
1153 method_name = dem_opname;
1154 }
1155
f8eba3c6
TT
1156 if (strcmp_iw (method_name, name) == 0)
1157 {
1158 int field_counter;
aee8d8ba 1159
f8eba3c6
TT
1160 for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
1161 - 1);
1162 field_counter >= 0;
1163 --field_counter)
1164 {
1165 struct fn_field *f;
1166 const char *phys_name;
1167
1168 f = TYPE_FN_FIELDLIST1 (t, method_counter);
1169 if (TYPE_FN_FIELD_STUB (f, field_counter))
1170 continue;
1171 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1172 VEC_safe_push (const_char_ptr, *result_names, phys_name);
1173 }
1174 }
aee8d8ba
DC
1175 }
1176 }
1177
f8eba3c6
TT
1178 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
1179 VEC_safe_push (typep, *superclasses, TYPE_BASECLASS (t, ibase));
50641945
FN
1180}
1181
50641945
FN
1182/* Find an instance of the character C in the string S that is outside
1183 of all parenthesis pairs, single-quoted strings, and double-quoted
8120c9d5
EZ
1184 strings. Also, ignore the char within a template name, like a ','
1185 within foo<int, int>. */
1186
40e084e1
KS
1187static const char *
1188find_toplevel_char (const char *s, char c)
50641945
FN
1189{
1190 int quoted = 0; /* zero if we're not in quotes;
1191 '"' if we're in a double-quoted string;
1192 '\'' if we're in a single-quoted string. */
a04257e6 1193 int depth = 0; /* Number of unclosed parens we've seen. */
40e084e1 1194 const char *scan;
50641945
FN
1195
1196 for (scan = s; *scan; scan++)
1197 {
1198 if (quoted)
1199 {
1200 if (*scan == quoted)
1201 quoted = 0;
1202 else if (*scan == '\\' && *(scan + 1))
1203 scan++;
1204 }
1205 else if (*scan == c && ! quoted && depth == 0)
1206 return scan;
1207 else if (*scan == '"' || *scan == '\'')
1208 quoted = *scan;
8120c9d5 1209 else if (*scan == '(' || *scan == '<')
50641945 1210 depth++;
8120c9d5 1211 else if ((*scan == ')' || *scan == '>') && depth > 0)
50641945
FN
1212 depth--;
1213 }
1214
1215 return 0;
1216}
1217
40e084e1
KS
1218/* The string equivalent of find_toplevel_char. Returns a pointer
1219 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1220 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
889f28e2 1221
40e084e1
KS
1222static const char *
1223find_toplevel_string (const char *haystack, const char *needle)
889f28e2 1224{
40e084e1
KS
1225 const char *s = haystack;
1226
1227 do
1228 {
1229 s = find_toplevel_char (s, *needle);
1230
1231 if (s != NULL)
1232 {
1233 /* Found first char in HAYSTACK; check rest of string. */
61012eef 1234 if (startswith (s, needle))
40e084e1
KS
1235 return s;
1236
1237 /* Didn't find it; loop over HAYSTACK, looking for the next
1238 instance of the first character of NEEDLE. */
1239 ++s;
1240 }
1241 }
1242 while (s != NULL && *s != '\0');
1243
1244 /* NEEDLE was not found in HAYSTACK. */
1245 return NULL;
889f28e2
AF
1246}
1247
33f448b1
JK
1248/* Convert CANONICAL to its string representation using
1249 symtab_to_fullname for SYMTAB. The caller must xfree the result. */
1250
1251static char *
1252canonical_to_fullform (const struct linespec_canonical_name *canonical)
1253{
1254 if (canonical->symtab == NULL)
1255 return xstrdup (canonical->suffix);
1256 else
1257 return xstrprintf ("%s:%s", symtab_to_fullname (canonical->symtab),
1258 canonical->suffix);
1259}
1260
f8eba3c6
TT
1261/* Given FILTERS, a list of canonical names, filter the sals in RESULT
1262 and store the result in SELF->CANONICAL. */
50641945 1263
f8eba3c6
TT
1264static void
1265filter_results (struct linespec_state *self,
1266 struct symtabs_and_lines *result,
1267 VEC (const_char_ptr) *filters)
1268{
1269 int i;
1270 const char *name;
1271
1272 for (i = 0; VEC_iterate (const_char_ptr, filters, i, name); ++i)
1273 {
1274 struct linespec_sals lsal;
1275 int j;
1276
1277 memset (&lsal, 0, sizeof (lsal));
1278
1279 for (j = 0; j < result->nelts; ++j)
1280 {
33f448b1
JK
1281 const struct linespec_canonical_name *canonical;
1282 char *fullform;
1283 struct cleanup *cleanup;
1284
1285 canonical = &self->canonical_names[j];
1286 fullform = canonical_to_fullform (canonical);
1287 cleanup = make_cleanup (xfree, fullform);
1288
1289 if (strcmp (name, fullform) == 0)
f8eba3c6 1290 add_sal_to_sals_basic (&lsal.sals, &result->sals[j]);
33f448b1
JK
1291
1292 do_cleanups (cleanup);
f8eba3c6
TT
1293 }
1294
1295 if (lsal.sals.nelts > 0)
1296 {
1297 lsal.canonical = xstrdup (name);
1298 VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
1299 }
1300 }
1301
1302 self->canonical->pre_expanded = 0;
1303}
1304
1305/* Store RESULT into SELF->CANONICAL. */
1306
1307static void
1308convert_results_to_lsals (struct linespec_state *self,
1309 struct symtabs_and_lines *result)
50641945 1310{
f8eba3c6
TT
1311 struct linespec_sals lsal;
1312
1313 lsal.canonical = NULL;
1314 lsal.sals = *result;
1315 VEC_safe_push (linespec_sals, self->canonical->sals, &lsal);
1316}
1317
33f448b1
JK
1318/* A structure that contains two string representations of a struct
1319 linespec_canonical_name:
1320 - one where the the symtab's fullname is used;
1321 - one where the filename followed the "set filename-display"
1322 setting. */
1323
1324struct decode_line_2_item
1325{
1326 /* The form using symtab_to_fullname.
1327 It must be xfree'ed after use. */
1328 char *fullform;
1329
1330 /* The form using symtab_to_filename_for_display.
1331 It must be xfree'ed after use. */
1332 char *displayform;
1333
1334 /* Field is initialized to zero and it is set to one if the user
1335 requested breakpoint for this entry. */
1336 unsigned int selected : 1;
1337};
1338
1339/* Helper for qsort to sort decode_line_2_item entries by DISPLAYFORM and
1340 secondarily by FULLFORM. */
1341
1342static int
1343decode_line_2_compare_items (const void *ap, const void *bp)
1344{
1345 const struct decode_line_2_item *a = ap;
1346 const struct decode_line_2_item *b = bp;
1347 int retval;
1348
1349 retval = strcmp (a->displayform, b->displayform);
1350 if (retval != 0)
1351 return retval;
1352
1353 return strcmp (a->fullform, b->fullform);
1354}
1355
f8eba3c6
TT
1356/* Handle multiple results in RESULT depending on SELECT_MODE. This
1357 will either return normally, throw an exception on multiple
1358 results, or present a menu to the user. On return, the SALS vector
1359 in SELF->CANONICAL is set up properly. */
1360
1361static void
1362decode_line_2 (struct linespec_state *self,
1363 struct symtabs_and_lines *result,
1364 const char *select_mode)
1365{
f8eba3c6 1366 char *args, *prompt;
50641945 1367 int i;
50641945 1368 struct cleanup *old_chain;
33f448b1 1369 VEC (const_char_ptr) *filters = NULL;
f8eba3c6 1370 struct get_number_or_range_state state;
33f448b1
JK
1371 struct decode_line_2_item *items;
1372 int items_count;
50641945 1373
f8eba3c6
TT
1374 gdb_assert (select_mode != multiple_symbols_all);
1375 gdb_assert (self->canonical != NULL);
33f448b1
JK
1376 gdb_assert (result->nelts >= 1);
1377
1378 old_chain = make_cleanup (VEC_cleanup (const_char_ptr), &filters);
50641945 1379
33f448b1
JK
1380 /* Prepare ITEMS array. */
1381 items_count = result->nelts;
1382 items = xmalloc (sizeof (*items) * items_count);
1383 make_cleanup (xfree, items);
1384 for (i = 0; i < items_count; ++i)
50641945 1385 {
33f448b1
JK
1386 const struct linespec_canonical_name *canonical;
1387 struct decode_line_2_item *item;
1388
1389 canonical = &self->canonical_names[i];
1390 gdb_assert (canonical->suffix != NULL);
1391 item = &items[i];
f8eba3c6 1392
33f448b1
JK
1393 item->fullform = canonical_to_fullform (canonical);
1394 make_cleanup (xfree, item->fullform);
1395
1396 if (canonical->symtab == NULL)
1397 item->displayform = canonical->suffix;
1398 else
f8eba3c6 1399 {
33f448b1
JK
1400 const char *fn_for_display;
1401
1402 fn_for_display = symtab_to_filename_for_display (canonical->symtab);
1403 item->displayform = xstrprintf ("%s:%s", fn_for_display,
1404 canonical->suffix);
1405 make_cleanup (xfree, item->displayform);
f8eba3c6
TT
1406 }
1407
33f448b1 1408 item->selected = 0;
50641945
FN
1409 }
1410
33f448b1
JK
1411 /* Sort the list of method names. */
1412 qsort (items, items_count, sizeof (*items), decode_line_2_compare_items);
1413
1414 /* Remove entries with the same FULLFORM. */
1415 if (items_count >= 2)
1416 {
1417 struct decode_line_2_item *dst, *src;
1418
1419 dst = items;
1420 for (src = &items[1]; src < &items[items_count]; src++)
1421 if (strcmp (src->fullform, dst->fullform) != 0)
1422 *++dst = *src;
1423 items_count = dst + 1 - items;
1424 }
1425
1426 if (select_mode == multiple_symbols_cancel && items_count > 1)
f8eba3c6
TT
1427 error (_("canceled because the command is ambiguous\n"
1428 "See set/show multiple-symbol."));
1429
33f448b1 1430 if (select_mode == multiple_symbols_all || items_count == 1)
50641945 1431 {
f8eba3c6
TT
1432 do_cleanups (old_chain);
1433 convert_results_to_lsals (self, result);
1434 return;
50641945
FN
1435 }
1436
f8eba3c6 1437 printf_unfiltered (_("[0] cancel\n[1] all\n"));
33f448b1
JK
1438 for (i = 0; i < items_count; i++)
1439 printf_unfiltered ("[%d] %s\n", i + 2, items[i].displayform);
f8eba3c6
TT
1440
1441 prompt = getenv ("PS2");
1442 if (prompt == NULL)
50641945 1443 {
f8eba3c6 1444 prompt = "> ";
50641945 1445 }
f8eba3c6 1446 args = command_line_input (prompt, 0, "overload-choice");
50641945
FN
1447
1448 if (args == 0 || *args == 0)
e2e0b3e5 1449 error_no_arg (_("one or more choice numbers"));
50641945 1450
f8eba3c6
TT
1451 init_number_or_range (&state, args);
1452 while (!state.finished)
50641945
FN
1453 {
1454 int num;
1455
f8eba3c6 1456 num = get_number_or_range (&state);
50641945
FN
1457
1458 if (num == 0)
8a3fe4f8 1459 error (_("canceled"));
50641945
FN
1460 else if (num == 1)
1461 {
f8eba3c6
TT
1462 /* We intentionally make this result in a single breakpoint,
1463 contrary to what older versions of gdb did. The
1464 rationale is that this lets a user get the
1465 multiple_symbols_all behavior even with the 'ask'
1466 setting; and he can get separate breakpoints by entering
1467 "2-57" at the query. */
1468 do_cleanups (old_chain);
1469 convert_results_to_lsals (self, result);
1470 return;
50641945
FN
1471 }
1472
f8eba3c6 1473 num -= 2;
33f448b1 1474 if (num >= items_count)
f8eba3c6 1475 printf_unfiltered (_("No choice number %d.\n"), num);
50641945
FN
1476 else
1477 {
33f448b1 1478 struct decode_line_2_item *item = &items[num];
f8eba3c6 1479
33f448b1 1480 if (!item->selected)
50641945 1481 {
33f448b1
JK
1482 VEC_safe_push (const_char_ptr, filters, item->fullform);
1483 item->selected = 1;
50641945
FN
1484 }
1485 else
1486 {
3e43a32a 1487 printf_unfiltered (_("duplicate request for %d ignored.\n"),
f6f99966 1488 num + 2);
50641945
FN
1489 }
1490 }
50641945 1491 }
f8eba3c6
TT
1492
1493 filter_results (self, result, filters);
1494 do_cleanups (old_chain);
50641945 1495}
94af9270 1496
40e084e1 1497\f
3d50dd94 1498
40e084e1
KS
1499/* The parser of linespec itself. */
1500
1501/* Throw an appropriate error when SYMBOL is not found (optionally in
1502 FILENAME). */
1503
1504static void ATTRIBUTE_NORETURN
5d94e27b 1505symbol_not_found_error (const char *symbol, const char *filename)
3d50dd94 1506{
40e084e1
KS
1507 if (symbol == NULL)
1508 symbol = "";
1509
1510 if (!have_full_symbols ()
1511 && !have_partial_symbols ()
1512 && !have_minimal_symbols ())
1513 throw_error (NOT_FOUND_ERROR,
1514 _("No symbol table is loaded. Use the \"file\" command."));
1515
1516 /* If SYMBOL starts with '$', the user attempted to either lookup
1517 a function/variable in his code starting with '$' or an internal
1518 variable of that name. Since we do not know which, be concise and
1519 explain both possibilities. */
1520 if (*symbol == '$')
1521 {
1522 if (filename)
1523 throw_error (NOT_FOUND_ERROR,
1524 _("Undefined convenience variable or function \"%s\" "
1525 "not defined in \"%s\"."), symbol, filename);
1526 else
1527 throw_error (NOT_FOUND_ERROR,
1528 _("Undefined convenience variable or function \"%s\" "
1529 "not defined."), symbol);
1530 }
1531 else
1532 {
1533 if (filename)
1534 throw_error (NOT_FOUND_ERROR,
1535 _("Function \"%s\" not defined in \"%s\"."),
1536 symbol, filename);
1537 else
1538 throw_error (NOT_FOUND_ERROR,
1539 _("Function \"%s\" not defined."), symbol);
1540 }
3d50dd94
JK
1541}
1542
40e084e1
KS
1543/* Throw an appropriate error when an unexpected token is encountered
1544 in the input. */
94af9270 1545
40e084e1
KS
1546static void ATTRIBUTE_NORETURN
1547unexpected_linespec_error (linespec_parser *parser)
94af9270 1548{
40e084e1
KS
1549 linespec_token token;
1550 static const char * token_type_strings[]
1551 = {"keyword", "colon", "string", "number", "comma", "end of input"};
94af9270 1552
40e084e1
KS
1553 /* Get the token that generated the error. */
1554 token = linespec_lexer_lex_one (parser);
94af9270 1555
40e084e1
KS
1556 /* Finally, throw the error. */
1557 if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
1558 || token.type == LSTOKEN_KEYWORD)
94af9270 1559 {
40e084e1
KS
1560 char *string;
1561 struct cleanup *cleanup;
1562
1563 string = copy_token_string (token);
1564 cleanup = make_cleanup (xfree, string);
1565 throw_error (GENERIC_ERROR,
1566 _("malformed linespec error: unexpected %s, \"%s\""),
1567 token_type_strings[token.type], string);
1568 }
1569 else
1570 throw_error (GENERIC_ERROR,
1571 _("malformed linespec error: unexpected %s"),
1572 token_type_strings[token.type]);
1573}
1574
1575/* Parse and return a line offset in STRING. */
1576
1577static struct line_offset
09cf2b22 1578linespec_parse_line_offset (const char *string)
40e084e1
KS
1579{
1580 struct line_offset line_offset = {0, LINE_OFFSET_NONE};
1581
1582 if (*string == '+')
1583 {
1584 line_offset.sign = LINE_OFFSET_PLUS;
1585 ++string;
1586 }
1587 else if (*string == '-')
1588 {
1589 line_offset.sign = LINE_OFFSET_MINUS;
1590 ++string;
1591 }
1592
1593 /* Right now, we only allow base 10 for offsets. */
1594 line_offset.offset = atoi (string);
1595 return line_offset;
1596}
1597
1598/* Parse the basic_spec in PARSER's input. */
1599
1600static void
1601linespec_parse_basic (linespec_parser *parser)
1602{
1603 char *name;
1604 linespec_token token;
1605 VEC (symbolp) *symbols, *labels;
f60e2d5c 1606 VEC (bound_minimal_symbol_d) *minimal_symbols;
40e084e1
KS
1607 struct cleanup *cleanup;
1608
1609 /* Get the next token. */
1610 token = linespec_lexer_lex_one (parser);
1611
1612 /* If it is EOI or KEYWORD, issue an error. */
1613 if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
1614 unexpected_linespec_error (parser);
1615 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1616 else if (token.type == LSTOKEN_NUMBER)
1617 {
1618 /* Record the line offset and get the next token. */
1619 name = copy_token_string (token);
1620 cleanup = make_cleanup (xfree, name);
1621 PARSER_RESULT (parser)->line_offset = linespec_parse_line_offset (name);
1622 do_cleanups (cleanup);
1623
1624 /* Get the next token. */
1625 token = linespec_lexer_consume_token (parser);
1626
1627 /* If the next token is a comma, stop parsing and return. */
1628 if (token.type == LSTOKEN_COMMA)
1629 return;
1630
1631 /* If the next token is anything but EOI or KEYWORD, issue
1632 an error. */
1633 if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
1634 unexpected_linespec_error (parser);
1635 }
1636
1637 if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
1638 return;
1639
1640 /* Next token must be LSTOKEN_STRING. */
1641 if (token.type != LSTOKEN_STRING)
1642 unexpected_linespec_error (parser);
1643
1644 /* The current token will contain the name of a function, method,
1645 or label. */
1646 name = copy_token_string (token);
1647 cleanup = make_cleanup (xfree, name);
1648
1649 /* Try looking it up as a function/method. */
1650 find_linespec_symbols (PARSER_STATE (parser),
1651 PARSER_RESULT (parser)->file_symtabs, name,
1652 &symbols, &minimal_symbols);
1653
1654 if (symbols != NULL || minimal_symbols != NULL)
1655 {
1656 PARSER_RESULT (parser)->function_symbols = symbols;
1657 PARSER_RESULT (parser)->minimal_symbols = minimal_symbols;
1658 PARSER_RESULT (parser)->function_name = name;
1659 symbols = NULL;
1660 discard_cleanups (cleanup);
1661 }
1662 else
1663 {
1664 /* NAME was not a function or a method. So it must be a label
b4013987 1665 name or user specified variable like "break foo.c:$zippo". */
40e084e1
KS
1666 labels = find_label_symbols (PARSER_STATE (parser), NULL,
1667 &symbols, name);
1668 if (labels != NULL)
94af9270 1669 {
40e084e1
KS
1670 PARSER_RESULT (parser)->labels.label_symbols = labels;
1671 PARSER_RESULT (parser)->labels.function_symbols = symbols;
1672 PARSER_RESULT (parser)->label_name = name;
1673 symbols = NULL;
1674 discard_cleanups (cleanup);
1675 }
b4013987
AA
1676 else if (token.type == LSTOKEN_STRING
1677 && *LS_TOKEN_STOKEN (token).ptr == '$')
1678 {
1679 /* User specified a convenience variable or history value. */
1680 PARSER_RESULT (parser)->line_offset
1681 = linespec_parse_variable (PARSER_STATE (parser), name);
1682
1683 if (PARSER_RESULT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
1684 {
1685 /* The user-specified variable was not valid. Do not
1686 throw an error here. parse_linespec will do it for us. */
1687 PARSER_RESULT (parser)->function_name = name;
1688 discard_cleanups (cleanup);
1689 return;
1690 }
c888a17d
KS
1691
1692 /* The convenience variable/history value parsed correctly.
1693 NAME is no longer needed. */
1694 do_cleanups (cleanup);
b4013987 1695 }
40e084e1
KS
1696 else
1697 {
1698 /* The name is also not a label. Abort parsing. Do not throw
1699 an error here. parse_linespec will do it for us. */
1700
1701 /* Save a copy of the name we were trying to lookup. */
1702 PARSER_RESULT (parser)->function_name = name;
1703 discard_cleanups (cleanup);
1704 return;
1705 }
1706 }
1707
1708 /* Get the next token. */
1709 token = linespec_lexer_consume_token (parser);
1710
1711 if (token.type == LSTOKEN_COLON)
1712 {
1713 /* User specified a label or a lineno. */
1714 token = linespec_lexer_consume_token (parser);
1715
1716 if (token.type == LSTOKEN_NUMBER)
1717 {
1718 /* User specified an offset. Record the line offset and
1719 get the next token. */
1720 name = copy_token_string (token);
1721 cleanup = make_cleanup (xfree, name);
1722 PARSER_RESULT (parser)->line_offset
1723 = linespec_parse_line_offset (name);
1724 do_cleanups (cleanup);
1725
1726 /* Ge the next token. */
1727 token = linespec_lexer_consume_token (parser);
1728 }
1729 else if (token.type == LSTOKEN_STRING)
1730 {
1731 /* Grab a copy of the label's name and look it up. */
1732 name = copy_token_string (token);
1733 cleanup = make_cleanup (xfree, name);
1734 labels = find_label_symbols (PARSER_STATE (parser),
1735 PARSER_RESULT (parser)->function_symbols,
1736 &symbols, name);
1737
1738 if (labels != NULL)
94af9270 1739 {
40e084e1
KS
1740 PARSER_RESULT (parser)->labels.label_symbols = labels;
1741 PARSER_RESULT (parser)->labels.function_symbols = symbols;
1742 PARSER_RESULT (parser)->label_name = name;
1743 symbols = NULL;
1744 discard_cleanups (cleanup);
1745 }
1746 else
1747 {
1748 /* We don't know what it was, but it isn't a label. */
1749 throw_error (NOT_FOUND_ERROR,
1750 _("No label \"%s\" defined in function \"%s\"."),
1751 name, PARSER_RESULT (parser)->function_name);
1752 }
1753
1754 /* Check for a line offset. */
1755 token = linespec_lexer_consume_token (parser);
1756 if (token.type == LSTOKEN_COLON)
1757 {
1758 /* Get the next token. */
1759 token = linespec_lexer_consume_token (parser);
1760
1761 /* It must be a line offset. */
1762 if (token.type != LSTOKEN_NUMBER)
1763 unexpected_linespec_error (parser);
1764
1765 /* Record the lione offset and get the next token. */
1766 name = copy_token_string (token);
1767 cleanup = make_cleanup (xfree, name);
1768
1769 PARSER_RESULT (parser)->line_offset
1770 = linespec_parse_line_offset (name);
1771 do_cleanups (cleanup);
1772
1773 /* Get the next token. */
1774 token = linespec_lexer_consume_token (parser);
94af9270
KS
1775 }
1776 }
40e084e1
KS
1777 else
1778 {
1779 /* Trailing ':' in the input. Issue an error. */
1780 unexpected_linespec_error (parser);
1781 }
94af9270 1782 }
40e084e1 1783}
94af9270 1784
40e084e1
KS
1785/* Canonicalize the linespec contained in LS. The result is saved into
1786 STATE->canonical. */
1787
1788static void
1789canonicalize_linespec (struct linespec_state *state, linespec_p ls)
1790{
1791 /* If canonicalization was not requested, no need to do anything. */
1792 if (!state->canonical)
1793 return;
1794
1795 /* Shortcut expressions, which can only appear by themselves. */
1796 if (ls->expression != NULL)
1797 state->canonical->addr_string = xstrdup (ls->expression);
1798 else
1799 {
1800 struct ui_file *buf;
1801 int need_colon = 0;
1802
1803 buf = mem_fileopen ();
1804 if (ls->source_filename)
1805 {
1806 fputs_unfiltered (ls->source_filename, buf);
1807 need_colon = 1;
1808 }
1809
1810 if (ls->function_name)
1811 {
1812 if (need_colon)
1813 fputc_unfiltered (':', buf);
1814 fputs_unfiltered (ls->function_name, buf);
1815 need_colon = 1;
1816 }
1817
1818 if (ls->label_name)
1819 {
1820 if (need_colon)
1821 fputc_unfiltered (':', buf);
1822
1823 if (ls->function_name == NULL)
1824 {
1825 struct symbol *s;
1826
1827 /* No function was specified, so add the symbol name. */
1828 gdb_assert (ls->labels.function_symbols != NULL
1829 && (VEC_length (symbolp, ls->labels.function_symbols)
1830 == 1));
1831 s = VEC_index (symbolp, ls->labels.function_symbols, 0);
1832 fputs_unfiltered (SYMBOL_NATURAL_NAME (s), buf);
1833 fputc_unfiltered (':', buf);
1834 }
1835
1836 fputs_unfiltered (ls->label_name, buf);
1837 need_colon = 1;
1838 state->canonical->special_display = 1;
1839 }
1840
1841 if (ls->line_offset.sign != LINE_OFFSET_UNKNOWN)
1842 {
1843 if (need_colon)
1844 fputc_unfiltered (':', buf);
1845 fprintf_filtered (buf, "%s%d",
1846 (ls->line_offset.sign == LINE_OFFSET_NONE ? ""
1847 : (ls->line_offset.sign
1848 == LINE_OFFSET_PLUS ? "+" : "-")),
1849 ls->line_offset.offset);
1850 }
1851
1852 state->canonical->addr_string = ui_file_xstrdup (buf, NULL);
1853 ui_file_delete (buf);
1854 }
94af9270 1855}
c00f8484 1856
40e084e1 1857/* Given a line offset in LS, construct the relevant SALs. */
c00f8484 1858
40e084e1
KS
1859static struct symtabs_and_lines
1860create_sals_line_offset (struct linespec_state *self,
1861 linespec_p ls)
c00f8484 1862{
40e084e1
KS
1863 struct symtabs_and_lines values;
1864 struct symtab_and_line val;
1865 int use_default = 0;
c00f8484 1866
40e084e1
KS
1867 init_sal (&val);
1868 values.sals = NULL;
1869 values.nelts = 0;
1870
1871 /* This is where we need to make sure we have good defaults.
1872 We must guarantee that this section of code is never executed
2e47c6ca 1873 when we are called with just a function name, since
40e084e1
KS
1874 set_default_source_symtab_and_line uses
1875 select_source_symtab that calls us with such an argument. */
1876
ec94af83
DE
1877 if (VEC_length (symtab_ptr, ls->file_symtabs) == 1
1878 && VEC_index (symtab_ptr, ls->file_symtabs, 0) == NULL)
3d50dd94 1879 {
05cba821
JK
1880 const char *fullname;
1881
40e084e1 1882 set_current_program_space (self->program_space);
c00f8484 1883
40e084e1
KS
1884 /* Make sure we have at least a default source line. */
1885 set_default_source_symtab_and_line ();
1886 initialize_defaults (&self->default_symtab, &self->default_line);
05cba821 1887 fullname = symtab_to_fullname (self->default_symtab);
ec94af83
DE
1888 VEC_pop (symtab_ptr, ls->file_symtabs);
1889 VEC_free (symtab_ptr, ls->file_symtabs);
05cba821 1890 ls->file_symtabs = collect_symtabs_from_filename (fullname);
40e084e1
KS
1891 use_default = 1;
1892 }
c00f8484 1893
40e084e1
KS
1894 val.line = ls->line_offset.offset;
1895 switch (ls->line_offset.sign)
1896 {
1897 case LINE_OFFSET_PLUS:
1898 if (ls->line_offset.offset == 0)
1899 val.line = 5;
1900 if (use_default)
1901 val.line = self->default_line + val.line;
1902 break;
1903
1904 case LINE_OFFSET_MINUS:
1905 if (ls->line_offset.offset == 0)
1906 val.line = 15;
1907 if (use_default)
1908 val.line = self->default_line - val.line;
1909 else
1910 val.line = -val.line;
1911 break;
1912
1913 case LINE_OFFSET_NONE:
1914 break; /* No need to adjust val.line. */
1915 }
1916
1917 if (self->list_mode)
1918 decode_digits_list_mode (self, ls, &values, val);
1919 else
1920 {
1921 struct linetable_entry *best_entry = NULL;
1922 int *filter;
3977b71f 1923 const struct block **blocks;
40e084e1
KS
1924 struct cleanup *cleanup;
1925 struct symtabs_and_lines intermediate_results;
1926 int i, j;
1927
1928 intermediate_results.sals = NULL;
1929 intermediate_results.nelts = 0;
1930
1931 decode_digits_ordinary (self, ls, val.line, &intermediate_results,
1932 &best_entry);
1933 if (intermediate_results.nelts == 0 && best_entry != NULL)
1934 decode_digits_ordinary (self, ls, best_entry->line,
1935 &intermediate_results, &best_entry);
1936
1937 cleanup = make_cleanup (xfree, intermediate_results.sals);
1938
1939 /* For optimized code, the compiler can scatter one source line
1940 across disjoint ranges of PC values, even when no duplicate
1941 functions or inline functions are involved. For example,
1942 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
1943 function can result in two PC ranges. In this case, we don't
1944 want to set a breakpoint on the first PC of each range. To filter
1945 such cases, we use containing blocks -- for each PC found
1946 above, we see if there are other PCs that are in the same
1947 block. If yes, the other PCs are filtered out. */
1948
1949 filter = XNEWVEC (int, intermediate_results.nelts);
1950 make_cleanup (xfree, filter);
3977b71f 1951 blocks = XNEWVEC (const struct block *, intermediate_results.nelts);
40e084e1
KS
1952 make_cleanup (xfree, blocks);
1953
1954 for (i = 0; i < intermediate_results.nelts; ++i)
3d50dd94 1955 {
40e084e1 1956 set_current_program_space (intermediate_results.sals[i].pspace);
c00f8484 1957
40e084e1
KS
1958 filter[i] = 1;
1959 blocks[i] = block_for_pc_sect (intermediate_results.sals[i].pc,
1960 intermediate_results.sals[i].section);
3d50dd94 1961 }
c00f8484 1962
40e084e1
KS
1963 for (i = 0; i < intermediate_results.nelts; ++i)
1964 {
1965 if (blocks[i] != NULL)
1966 for (j = i + 1; j < intermediate_results.nelts; ++j)
1967 {
1968 if (blocks[j] == blocks[i])
1969 {
1970 filter[j] = 0;
1971 break;
1972 }
1973 }
1974 }
c00f8484 1975
40e084e1
KS
1976 for (i = 0; i < intermediate_results.nelts; ++i)
1977 if (filter[i])
1978 {
1979 struct symbol *sym = (blocks[i]
1980 ? block_containing_function (blocks[i])
1981 : NULL);
3d50dd94 1982
40e084e1
KS
1983 if (self->funfirstline)
1984 skip_prologue_sal (&intermediate_results.sals[i]);
1985 /* Make sure the line matches the request, not what was
1986 found. */
1987 intermediate_results.sals[i].line = val.line;
1988 add_sal_to_sals (self, &values, &intermediate_results.sals[i],
66f1999b 1989 sym ? SYMBOL_NATURAL_NAME (sym) : NULL, 0);
40e084e1 1990 }
3d50dd94 1991
40e084e1 1992 do_cleanups (cleanup);
f17170e5 1993 }
c00f8484 1994
40e084e1
KS
1995 if (values.nelts == 0)
1996 {
1997 if (ls->source_filename)
1998 throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
1999 val.line, ls->source_filename);
2000 else
2001 throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
2002 val.line);
2003 }
3d50dd94 2004
40e084e1 2005 return values;
c00f8484
KS
2006}
2007
40e084e1
KS
2008/* Create and return SALs from the linespec LS. */
2009
2010static struct symtabs_and_lines
2011convert_linespec_to_sals (struct linespec_state *state, linespec_p ls)
2012{
2013 struct symtabs_and_lines sals = {NULL, 0};
2014
2015 if (ls->expression != NULL)
2016 {
66f1999b
KS
2017 struct symtab_and_line sal;
2018
40e084e1 2019 /* We have an expression. No other attribute is allowed. */
66f1999b
KS
2020 sal = find_pc_line (ls->expr_pc, 0);
2021 sal.pc = ls->expr_pc;
2022 sal.section = find_pc_overlay (ls->expr_pc);
2023 sal.explicit_pc = 1;
2024 add_sal_to_sals (state, &sals, &sal, ls->expression, 1);
40e084e1
KS
2025 }
2026 else if (ls->labels.label_symbols != NULL)
2027 {
2028 /* We have just a bunch of functions/methods or labels. */
2029 int i;
2030 struct symtab_and_line sal;
2031 struct symbol *sym;
2032
2033 for (i = 0; VEC_iterate (symbolp, ls->labels.label_symbols, i, sym); ++i)
2034 {
08be3fe3 2035 struct program_space *pspace = SYMTAB_PSPACE (symbol_symtab (sym));
fdbb204b
TT
2036
2037 if (symbol_to_sal (&sal, state->funfirstline, sym)
2038 && maybe_add_address (state->addr_set, pspace, sal.pc))
64b92e45
KS
2039 add_sal_to_sals (state, &sals, &sal,
2040 SYMBOL_NATURAL_NAME (sym), 0);
40e084e1
KS
2041 }
2042 }
2043 else if (ls->function_symbols != NULL || ls->minimal_symbols != NULL)
2044 {
2045 /* We have just a bunch of functions and/or methods. */
2046 int i;
2047 struct symtab_and_line sal;
2048 struct symbol *sym;
f60e2d5c 2049 bound_minimal_symbol_d *elem;
40e084e1
KS
2050 struct program_space *pspace;
2051
2052 if (ls->function_symbols != NULL)
2053 {
2054 /* Sort symbols so that symbols with the same program space are next
2055 to each other. */
2056 qsort (VEC_address (symbolp, ls->function_symbols),
2057 VEC_length (symbolp, ls->function_symbols),
2058 sizeof (symbolp), compare_symbols);
2059
2060 for (i = 0; VEC_iterate (symbolp, ls->function_symbols, i, sym); ++i)
2061 {
08be3fe3 2062 pspace = SYMTAB_PSPACE (symbol_symtab (sym));
40e084e1 2063 set_current_program_space (pspace);
64b92e45
KS
2064 if (symbol_to_sal (&sal, state->funfirstline, sym)
2065 && maybe_add_address (state->addr_set, pspace, sal.pc))
66f1999b
KS
2066 add_sal_to_sals (state, &sals, &sal,
2067 SYMBOL_NATURAL_NAME (sym), 0);
40e084e1
KS
2068 }
2069 }
2070
2071 if (ls->minimal_symbols != NULL)
2072 {
2073 /* Sort minimal symbols by program space, too. */
f60e2d5c
TT
2074 qsort (VEC_address (bound_minimal_symbol_d, ls->minimal_symbols),
2075 VEC_length (bound_minimal_symbol_d, ls->minimal_symbols),
2076 sizeof (bound_minimal_symbol_d), compare_msymbols);
40e084e1
KS
2077
2078 for (i = 0;
f60e2d5c
TT
2079 VEC_iterate (bound_minimal_symbol_d, ls->minimal_symbols,
2080 i, elem);
40e084e1
KS
2081 ++i)
2082 {
001822aa 2083 pspace = elem->objfile->pspace;
40e084e1
KS
2084 set_current_program_space (pspace);
2085 minsym_found (state, elem->objfile, elem->minsym, &sals);
2086 }
2087 }
2088 }
2089 else if (ls->line_offset.sign != LINE_OFFSET_UNKNOWN)
2090 {
2091 /* Only an offset was specified. */
2092 sals = create_sals_line_offset (state, ls);
2093
2094 /* Make sure we have a filename for canonicalization. */
2095 if (ls->source_filename == NULL)
05cba821
JK
2096 {
2097 const char *fullname = symtab_to_fullname (state->default_symtab);
2098
e93ba630
JK
2099 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2100 form so that displaying SOURCE_FILENAME can follow the current
2101 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2102 it has been kept for code simplicity only in absolute form. */
05cba821
JK
2103 ls->source_filename = xstrdup (fullname);
2104 }
40e084e1
KS
2105 }
2106 else
2107 {
2108 /* We haven't found any results... */
2109 return sals;
2110 }
2111
2112 canonicalize_linespec (state, ls);
2113
2114 if (sals.nelts > 0 && state->canonical != NULL)
2115 state->canonical->pre_expanded = 1;
2116
2117 return sals;
2118}
50641945 2119
40e084e1 2120/* Parse a string that specifies a linespec.
50641945
FN
2121 Pass the address of a char * variable; that variable will be
2122 advanced over the characters actually parsed.
2123
40e084e1 2124 The basic grammar of linespecs:
50641945 2125
40e084e1
KS
2126 linespec -> expr_spec | var_spec | basic_spec
2127 expr_spec -> '*' STRING
2128 var_spec -> '$' (STRING | NUMBER)
50641945 2129
40e084e1
KS
2130 basic_spec -> file_offset_spec | function_spec | label_spec
2131 file_offset_spec -> opt_file_spec offset_spec
2132 function_spec -> opt_file_spec function_name_spec opt_label_spec
2133 label_spec -> label_name_spec
50641945 2134
40e084e1
KS
2135 opt_file_spec -> "" | file_name_spec ':'
2136 opt_label_spec -> "" | ':' label_name_spec
2137
2138 file_name_spec -> STRING
2139 function_name_spec -> STRING
2140 label_name_spec -> STRING
2141 function_name_spec -> STRING
2142 offset_spec -> NUMBER
2143 -> '+' NUMBER
2144 -> '-' NUMBER
2145
2146 This may all be followed by several keywords such as "if EXPR",
2147 which we ignore.
2148
2149 A comma will terminate parsing.
2150
2151 The function may be an undebuggable function found in minimal symbol table.
50641945
FN
2152
2153 If the argument FUNFIRSTLINE is nonzero, we want the first line
2154 of real code inside a function when a function is specified, and it is
2155 not OK to specify a variable or type to get its line number.
2156
2157 DEFAULT_SYMTAB specifies the file to use if none is specified.
2158 It defaults to current_source_symtab.
2159 DEFAULT_LINE specifies the line number to use for relative
2160 line numbers (that start with signs). Defaults to current_source_line.
2161 If CANONICAL is non-NULL, store an array of strings containing the canonical
1777feb0 2162 line specs there if necessary. Currently overloaded member functions and
50641945 2163 line numbers or static functions without a filename yield a canonical
1777feb0 2164 line spec. The array and the line spec strings are allocated on the heap,
50641945
FN
2165 it is the callers responsibility to free them.
2166
2167 Note that it is possible to return zero for the symtab
2168 if no file is validly specified. Callers must check that.
58438ac1 2169 Also, the line number returned may be invalid. */
50641945 2170
40e084e1 2171/* Parse the linespec in ARGPTR. */
50641945 2172
ad32032e 2173static struct symtabs_and_lines
d7561cbb 2174parse_linespec (linespec_parser *parser, const char **argptr)
50641945 2175{
40e084e1
KS
2176 linespec_token token;
2177 struct symtabs_and_lines values;
7556d4a4 2178 struct gdb_exception file_exception = exception_none;
40e084e1
KS
2179 struct cleanup *cleanup;
2180
2181 /* A special case to start. It has become quite popular for
2182 IDEs to work around bugs in the previous parser by quoting
2183 the entire linespec, so we attempt to deal with this nicely. */
2184 parser->is_quote_enclosed = 0;
2185 if (!is_ada_operator (*argptr)
2186 && strchr (linespec_quote_characters, **argptr) != NULL)
2187 {
2188 const char *end;
9ef07c8c 2189
40e084e1
KS
2190 end = skip_quote_char (*argptr + 1, **argptr);
2191 if (end != NULL && is_closing_quote_enclosed (end))
136e1c30 2192 {
40e084e1
KS
2193 /* Here's the special case. Skip ARGPTR past the initial
2194 quote. */
2195 ++(*argptr);
2196 parser->is_quote_enclosed = 1;
136e1c30
DE
2197 }
2198 }
e8eb7bc5 2199
40e084e1
KS
2200 parser->lexer.saved_arg = *argptr;
2201 parser->lexer.stream = argptr;
d2630e69 2202
40e084e1
KS
2203 /* Initialize the default symtab and line offset. */
2204 initialize_defaults (&PARSER_STATE (parser)->default_symtab,
2205 &PARSER_STATE (parser)->default_line);
d2630e69 2206
40e084e1
KS
2207 /* Objective-C shortcut. */
2208 values = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), argptr);
2209 if (values.sals != NULL)
2210 return values;
e0881a8e 2211
40e084e1 2212 /* Start parsing. */
d2630e69 2213
40e084e1
KS
2214 /* Get the first token. */
2215 token = linespec_lexer_lex_one (parser);
50641945 2216
40e084e1
KS
2217 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
2218 if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '*')
50641945 2219 {
bbc13ae3
KS
2220 char *expr;
2221 const char *copy;
94af9270 2222
40e084e1
KS
2223 /* User specified an expression, *EXPR. */
2224 copy = expr = copy_token_string (token);
2225 cleanup = make_cleanup (xfree, expr);
2226 PARSER_RESULT (parser)->expr_pc = linespec_expression_to_pc (&copy);
2227 discard_cleanups (cleanup);
2228 PARSER_RESULT (parser)->expression = expr;
dcf9f4ab 2229
40e084e1
KS
2230 /* This is a little hacky/tricky. If linespec_expression_to_pc
2231 did not evaluate the entire token, then we must find the
2232 string COPY inside the original token buffer. */
2233 if (*copy != '\0')
2234 {
2235 PARSER_STREAM (parser) = strstr (parser->lexer.saved_arg, copy);
2236 gdb_assert (PARSER_STREAM (parser) != NULL);
2237 }
2f741504 2238
40e084e1
KS
2239 /* Consume the token. */
2240 linespec_lexer_consume_token (parser);
50641945 2241
40e084e1
KS
2242 goto convert_to_sals;
2243 }
2244 else if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
2245 {
2246 char *var;
50641945 2247
40e084e1 2248 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
ec94af83 2249 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
dcf9f4ab 2250
40e084e1
KS
2251 /* User specified a convenience variable or history value. */
2252 var = copy_token_string (token);
2253 cleanup = make_cleanup (xfree, var);
2254 PARSER_RESULT (parser)->line_offset
2255 = linespec_parse_variable (PARSER_STATE (parser), var);
cf4ded82 2256 do_cleanups (cleanup);
f8eba3c6 2257
40e084e1
KS
2258 /* If a line_offset wasn't found (VAR is the name of a user
2259 variable/function), then skip to normal symbol processing. */
2260 if (PARSER_RESULT (parser)->line_offset.sign != LINE_OFFSET_UNKNOWN)
2261 {
40e084e1
KS
2262 /* Consume this token. */
2263 linespec_lexer_consume_token (parser);
dcf9f4ab 2264
40e084e1 2265 goto convert_to_sals;
50641945 2266 }
40e084e1
KS
2267 }
2268 else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
2269 unexpected_linespec_error (parser);
50641945 2270
40e084e1
KS
2271 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2272 this token cannot represent a filename. */
2273 token = linespec_lexer_peek_token (parser);
0e0b460e 2274
40e084e1 2275 if (token.type == LSTOKEN_COLON)
0e0b460e 2276 {
40e084e1 2277 char *user_filename;
0e0b460e 2278
40e084e1
KS
2279 /* Get the current token again and extract the filename. */
2280 token = linespec_lexer_lex_one (parser);
2281 user_filename = copy_token_string (token);
50641945 2282
40e084e1 2283 /* Check if the input is a filename. */
492d29ea 2284 TRY
40e084e1
KS
2285 {
2286 PARSER_RESULT (parser)->file_symtabs
2287 = symtabs_from_filename (user_filename);
2288 }
492d29ea 2289 CATCH (ex, RETURN_MASK_ERROR)
7556d4a4
PA
2290 {
2291 file_exception = ex;
2292 }
492d29ea 2293 END_CATCH
50641945 2294
40e084e1
KS
2295 if (file_exception.reason >= 0)
2296 {
2297 /* Symtabs were found for the file. Record the filename. */
2298 PARSER_RESULT (parser)->source_filename = user_filename;
f8eba3c6 2299
40e084e1
KS
2300 /* Get the next token. */
2301 token = linespec_lexer_consume_token (parser);
50641945 2302
40e084e1
KS
2303 /* This is LSTOKEN_COLON; consume it. */
2304 linespec_lexer_consume_token (parser);
2305 }
2306 else
2307 {
2308 /* No symtabs found -- discard user_filename. */
2309 xfree (user_filename);
50641945 2310
40e084e1 2311 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
ec94af83 2312 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
40e084e1 2313 }
50641945 2314 }
40e084e1
KS
2315 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
2316 else if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD
2317 && token.type != LSTOKEN_COMMA)
d2630e69 2318 {
40e084e1
KS
2319 /* TOKEN is the _next_ token, not the one currently in the parser.
2320 Consuming the token will give the correct error message. */
2321 linespec_lexer_consume_token (parser);
2322 unexpected_linespec_error (parser);
d2630e69 2323 }
50641945
FN
2324 else
2325 {
40e084e1 2326 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
ec94af83 2327 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
50641945 2328 }
50641945 2329
40e084e1
KS
2330 /* Parse the rest of the linespec. */
2331 linespec_parse_basic (parser);
50641945 2332
40e084e1
KS
2333 if (PARSER_RESULT (parser)->function_symbols == NULL
2334 && PARSER_RESULT (parser)->labels.label_symbols == NULL
2335 && PARSER_RESULT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN
2336 && PARSER_RESULT (parser)->minimal_symbols == NULL)
f8eba3c6 2337 {
40e084e1
KS
2338 /* The linespec didn't parse. Re-throw the file exception if
2339 there was one. */
2340 if (file_exception.reason < 0)
2341 throw_exception (file_exception);
0f5238ed 2342
40e084e1
KS
2343 /* Otherwise, the symbol is not found. */
2344 symbol_not_found_error (PARSER_RESULT (parser)->function_name,
2345 PARSER_RESULT (parser)->source_filename);
0f5238ed
TT
2346 }
2347
40e084e1 2348 convert_to_sals:
9ef07c8c 2349
40e084e1
KS
2350 /* Get the last token and record how much of the input was parsed,
2351 if necessary. */
2352 token = linespec_lexer_lex_one (parser);
2353 if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD)
2354 PARSER_STREAM (parser) = LS_TOKEN_STOKEN (token).ptr;
50641945 2355
40e084e1
KS
2356 /* Convert the data in PARSER_RESULT to SALs. */
2357 values = convert_linespec_to_sals (PARSER_STATE (parser),
2358 PARSER_RESULT (parser));
f8eba3c6 2359
40e084e1 2360 return values;
413dad4d 2361}
50641945 2362
40e084e1 2363
f8eba3c6 2364/* A constructor for linespec_state. */
44fe14ab 2365
f8eba3c6
TT
2366static void
2367linespec_state_constructor (struct linespec_state *self,
40e084e1 2368 int flags, const struct language_defn *language,
f8eba3c6
TT
2369 struct symtab *default_symtab,
2370 int default_line,
2371 struct linespec_result *canonical)
2372{
2373 memset (self, 0, sizeof (*self));
40e084e1 2374 self->language = language;
f8eba3c6
TT
2375 self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
2376 self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
2377 self->default_symtab = default_symtab;
2378 self->default_line = default_line;
2379 self->canonical = canonical;
2380 self->program_space = current_program_space;
2381 self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
2382 xfree, xcalloc, xfree);
2383}
44fe14ab 2384
40e084e1 2385/* Initialize a new linespec parser. */
44fe14ab
DC
2386
2387static void
40e084e1
KS
2388linespec_parser_new (linespec_parser *parser,
2389 int flags, const struct language_defn *language,
2390 struct symtab *default_symtab,
2391 int default_line,
2392 struct linespec_result *canonical)
44fe14ab 2393{
40e084e1
KS
2394 parser->lexer.current.type = LSTOKEN_CONSUMED;
2395 memset (PARSER_RESULT (parser), 0, sizeof (struct linespec));
2396 PARSER_RESULT (parser)->line_offset.sign = LINE_OFFSET_UNKNOWN;
2397 linespec_state_constructor (PARSER_STATE (parser), flags, language,
2398 default_symtab, default_line, canonical);
2399}
2400
2401/* A destructor for linespec_state. */
44fe14ab 2402
40e084e1
KS
2403static void
2404linespec_state_destructor (struct linespec_state *self)
2405{
f8eba3c6
TT
2406 htab_delete (self->addr_set);
2407}
44fe14ab 2408
40e084e1
KS
2409/* Delete a linespec parser. */
2410
2411static void
2412linespec_parser_delete (void *arg)
2413{
2414 linespec_parser *parser = (linespec_parser *) arg;
2415
5d94e27b
KS
2416 xfree ((char *) PARSER_RESULT (parser)->expression);
2417 xfree ((char *) PARSER_RESULT (parser)->source_filename);
2418 xfree ((char *) PARSER_RESULT (parser)->label_name);
2419 xfree ((char *) PARSER_RESULT (parser)->function_name);
40e084e1
KS
2420
2421 if (PARSER_RESULT (parser)->file_symtabs != NULL)
ec94af83 2422 VEC_free (symtab_ptr, PARSER_RESULT (parser)->file_symtabs);
40e084e1
KS
2423
2424 if (PARSER_RESULT (parser)->function_symbols != NULL)
2425 VEC_free (symbolp, PARSER_RESULT (parser)->function_symbols);
2426
2427 if (PARSER_RESULT (parser)->minimal_symbols != NULL)
f60e2d5c 2428 VEC_free (bound_minimal_symbol_d, PARSER_RESULT (parser)->minimal_symbols);
40e084e1
KS
2429
2430 if (PARSER_RESULT (parser)->labels.label_symbols != NULL)
2431 VEC_free (symbolp, PARSER_RESULT (parser)->labels.label_symbols);
2432
2433 if (PARSER_RESULT (parser)->labels.function_symbols != NULL)
2434 VEC_free (symbolp, PARSER_RESULT (parser)->labels.function_symbols);
2435
2436 linespec_state_destructor (PARSER_STATE (parser));
2437}
2438
f8eba3c6 2439/* See linespec.h. */
44fe14ab 2440
f8eba3c6
TT
2441void
2442decode_line_full (char **argptr, int flags,
2443 struct symtab *default_symtab,
2444 int default_line, struct linespec_result *canonical,
2445 const char *select_mode,
2446 const char *filter)
44fe14ab 2447{
f8eba3c6 2448 struct symtabs_and_lines result;
f8eba3c6 2449 struct cleanup *cleanups;
f8eba3c6 2450 VEC (const_char_ptr) *filters = NULL;
40e084e1
KS
2451 linespec_parser parser;
2452 struct linespec_state *state;
d7561cbb 2453 const char *copy, *orig;
f8eba3c6
TT
2454
2455 gdb_assert (canonical != NULL);
2456 /* The filter only makes sense for 'all'. */
2457 gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
2458 gdb_assert (select_mode == NULL
2459 || select_mode == multiple_symbols_all
2460 || select_mode == multiple_symbols_ask
2461 || select_mode == multiple_symbols_cancel);
2462 gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);
2463
40e084e1
KS
2464 linespec_parser_new (&parser, flags, current_language, default_symtab,
2465 default_line, canonical);
2466 cleanups = make_cleanup (linespec_parser_delete, &parser);
f8eba3c6
TT
2467 save_current_program_space ();
2468
d7561cbb
KS
2469 orig = copy = *argptr;
2470 result = parse_linespec (&parser, &copy);
2471 *argptr += copy - orig;
40e084e1 2472 state = PARSER_STATE (&parser);
f8eba3c6
TT
2473
2474 gdb_assert (result.nelts == 1 || canonical->pre_expanded);
2475 gdb_assert (canonical->addr_string != NULL);
2476 canonical->pre_expanded = 1;
2477
66f1999b 2478 /* Arrange for allocated canonical names to be freed. */
f8eba3c6
TT
2479 if (result.nelts > 0)
2480 {
2481 int i;
2482
40e084e1 2483 make_cleanup (xfree, state->canonical_names);
f8eba3c6
TT
2484 for (i = 0; i < result.nelts; ++i)
2485 {
33f448b1
JK
2486 gdb_assert (state->canonical_names[i].suffix != NULL);
2487 make_cleanup (xfree, state->canonical_names[i].suffix);
f8eba3c6
TT
2488 }
2489 }
2490
2491 if (select_mode == NULL)
2492 {
2493 if (ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ())))
2494 select_mode = multiple_symbols_all;
2495 else
2496 select_mode = multiple_symbols_select_mode ();
2497 }
2498
2499 if (select_mode == multiple_symbols_all)
2500 {
2501 if (filter != NULL)
2502 {
2503 make_cleanup (VEC_cleanup (const_char_ptr), &filters);
2504 VEC_safe_push (const_char_ptr, filters, filter);
40e084e1 2505 filter_results (state, &result, filters);
f8eba3c6
TT
2506 }
2507 else
40e084e1 2508 convert_results_to_lsals (state, &result);
f8eba3c6
TT
2509 }
2510 else
40e084e1 2511 decode_line_2 (state, &result, select_mode);
f8eba3c6
TT
2512
2513 do_cleanups (cleanups);
2514}
2515
39cf75f7
DE
2516/* See linespec.h. */
2517
f8eba3c6
TT
2518struct symtabs_and_lines
2519decode_line_1 (char **argptr, int flags,
2520 struct symtab *default_symtab,
2521 int default_line)
2522{
2523 struct symtabs_and_lines result;
40e084e1 2524 linespec_parser parser;
f8eba3c6 2525 struct cleanup *cleanups;
d7561cbb 2526 const char *copy, *orig;
f8eba3c6 2527
40e084e1
KS
2528 linespec_parser_new (&parser, flags, current_language, default_symtab,
2529 default_line, NULL);
2530 cleanups = make_cleanup (linespec_parser_delete, &parser);
f8eba3c6
TT
2531 save_current_program_space ();
2532
d7561cbb
KS
2533 orig = copy = *argptr;
2534 result = parse_linespec (&parser, &copy);
2535 *argptr += copy - orig;
40e084e1 2536
f8eba3c6
TT
2537 do_cleanups (cleanups);
2538 return result;
2539}
2540
39cf75f7
DE
2541/* See linespec.h. */
2542
2543struct symtabs_and_lines
2544decode_line_with_current_source (char *string, int flags)
2545{
2546 struct symtabs_and_lines sals;
2547 struct symtab_and_line cursal;
2548
2549 if (string == 0)
2550 error (_("Empty line specification."));
2551
2552 /* We use whatever is set as the current source line. We do not try
2553 and get a default source symtab+line or it will recursively call us! */
2554 cursal = get_current_source_symtab_and_line ();
2555
2556 sals = decode_line_1 (&string, flags,
2557 cursal.symtab, cursal.line);
2558
2559 if (*string)
2560 error (_("Junk at end of line specification: %s"), string);
2561 return sals;
2562}
2563
2564/* See linespec.h. */
2565
2566struct symtabs_and_lines
2567decode_line_with_last_displayed (char *string, int flags)
2568{
2569 struct symtabs_and_lines sals;
2570
2571 if (string == 0)
2572 error (_("Empty line specification."));
2573
2574 if (last_displayed_sal_is_valid ())
2575 sals = decode_line_1 (&string, flags,
2576 get_last_displayed_symtab (),
2577 get_last_displayed_line ());
2578 else
2579 sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
2580
2581 if (*string)
2582 error (_("Junk at end of line specification: %s"), string);
2583 return sals;
2584}
2585
f8eba3c6
TT
2586\f
2587
2588/* First, some functions to initialize stuff at the beggining of the
2589 function. */
2590
2591static void
2592initialize_defaults (struct symtab **default_symtab, int *default_line)
2593{
2594 if (*default_symtab == 0)
2595 {
2596 /* Use whatever we have for the default source line. We don't use
2597 get_current_or_default_symtab_and_line as it can recurse and call
2598 us back! */
2599 struct symtab_and_line cursal =
2600 get_current_source_symtab_and_line ();
2601
2602 *default_symtab = cursal.symtab;
2603 *default_line = cursal.line;
2604 }
2605}
2606
2607\f
2608
40e084e1
KS
2609/* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
2610 advancing EXP_PTR past any parsed text. */
f8eba3c6 2611
40e084e1 2612static CORE_ADDR
bbc13ae3 2613linespec_expression_to_pc (const char **exp_ptr)
f8eba3c6 2614{
f8eba3c6
TT
2615 if (current_program_space->executing_startup)
2616 /* The error message doesn't really matter, because this case
2617 should only hit during breakpoint reset. */
2618 throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
2619 "program space is in startup"));
2620
40e084e1
KS
2621 (*exp_ptr)++;
2622 return value_as_address (parse_to_comma_and_eval (exp_ptr));
0960f083
DC
2623}
2624
2625\f
2626
d2630e69
AF
2627/* Here's where we recognise an Objective-C Selector. An Objective C
2628 selector may be implemented by more than one class, therefore it
2629 may represent more than one method/function. This gives us a
2630 situation somewhat analogous to C++ overloading. If there's more
2631 than one method that could represent the selector, then use some of
2632 the existing C++ code to let the user choose one. */
2633
f8eba3c6 2634static struct symtabs_and_lines
d7561cbb 2635decode_objc (struct linespec_state *self, linespec_p ls, const char **argptr)
d2630e69 2636{
f8eba3c6
TT
2637 struct collect_info info;
2638 VEC (const_char_ptr) *symbol_names = NULL;
40e084e1 2639 struct symtabs_and_lines values;
d7561cbb 2640 const char *new_argptr;
f8eba3c6
TT
2641 struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
2642 &symbol_names);
2643
2644 info.state = self;
40e084e1 2645 info.file_symtabs = NULL;
ec94af83
DE
2646 VEC_safe_push (symtab_ptr, info.file_symtabs, NULL);
2647 make_cleanup (VEC_cleanup (symtab_ptr), &info.file_symtabs);
40e084e1
KS
2648 info.result.symbols = NULL;
2649 info.result.minimal_symbols = NULL;
2650 values.nelts = 0;
2651 values.sals = NULL;
f8eba3c6
TT
2652
2653 new_argptr = find_imps (*argptr, &symbol_names);
2654 if (VEC_empty (const_char_ptr, symbol_names))
2655 {
2656 do_cleanups (cleanup);
40e084e1 2657 return values;
f8eba3c6 2658 }
d2630e69 2659
f8eba3c6 2660 add_all_symbol_names_from_pspace (&info, NULL, symbol_names);
d2630e69 2661
40e084e1 2662 if (!VEC_empty (symbolp, info.result.symbols)
f60e2d5c 2663 || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
d2630e69 2664 {
f8eba3c6 2665 char *saved_arg;
d2630e69 2666
f8eba3c6
TT
2667 saved_arg = alloca (new_argptr - *argptr + 1);
2668 memcpy (saved_arg, *argptr, new_argptr - *argptr);
2669 saved_arg[new_argptr - *argptr] = '\0';
d2630e69 2670
0f5f4ffe 2671 ls->function_name = xstrdup (saved_arg);
40e084e1
KS
2672 ls->function_symbols = info.result.symbols;
2673 ls->minimal_symbols = info.result.minimal_symbols;
2674 values = convert_linespec_to_sals (self, ls);
2675
f8eba3c6 2676 if (self->canonical)
d2630e69 2677 {
f8eba3c6 2678 self->canonical->pre_expanded = 1;
40e084e1 2679 if (ls->source_filename)
f8eba3c6 2680 self->canonical->addr_string
40e084e1 2681 = xstrprintf ("%s:%s", ls->source_filename, saved_arg);
f8eba3c6
TT
2682 else
2683 self->canonical->addr_string = xstrdup (saved_arg);
d2630e69 2684 }
d2630e69
AF
2685 }
2686
f8eba3c6 2687 *argptr = new_argptr;
d2630e69 2688
f8eba3c6 2689 do_cleanups (cleanup);
c00f8484 2690
40e084e1 2691 return values;
f8eba3c6 2692}
c00f8484 2693
f8eba3c6
TT
2694/* An instance of this type is used when collecting prefix symbols for
2695 decode_compound. */
17763fd9 2696
f8eba3c6
TT
2697struct decode_compound_collector
2698{
2699 /* The result vector. */
2700 VEC (symbolp) *symbols;
3a93a0c2 2701
f8eba3c6
TT
2702 /* A hash table of all symbols we found. We use this to avoid
2703 adding any symbol more than once. */
2704 htab_t unique_syms;
2705};
3a93a0c2 2706
f8eba3c6
TT
2707/* A callback for iterate_over_symbols that is used by
2708 lookup_prefix_sym to collect type symbols. */
c00f8484 2709
f8eba3c6
TT
2710static int
2711collect_one_symbol (struct symbol *sym, void *d)
2712{
2713 struct decode_compound_collector *collector = d;
2714 void **slot;
2715 struct type *t;
614b3b14 2716
f8eba3c6 2717 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
8e704927 2718 return 1; /* Continue iterating. */
f8eba3c6
TT
2719
2720 t = SYMBOL_TYPE (sym);
2721 CHECK_TYPEDEF (t);
2722 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2723 && TYPE_CODE (t) != TYPE_CODE_UNION
2724 && TYPE_CODE (t) != TYPE_CODE_NAMESPACE)
8e704927 2725 return 1; /* Continue iterating. */
614b3b14 2726
f8eba3c6
TT
2727 slot = htab_find_slot (collector->unique_syms, sym, INSERT);
2728 if (!*slot)
2729 {
2730 *slot = sym;
2731 VEC_safe_push (symbolp, collector->symbols, sym);
2732 }
2733
8e704927 2734 return 1; /* Continue iterating. */
f8eba3c6 2735}
93d91629 2736
40e084e1 2737/* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
93d91629 2738
f8eba3c6 2739static VEC (symbolp) *
ec94af83 2740lookup_prefix_sym (struct linespec_state *state, VEC (symtab_ptr) *file_symtabs,
40e084e1 2741 const char *class_name)
93d91629 2742{
f8eba3c6
TT
2743 int ix;
2744 struct symtab *elt;
2745 struct decode_compound_collector collector;
2746 struct cleanup *outer;
2747 struct cleanup *cleanup;
17763fd9 2748
f8eba3c6 2749 collector.symbols = NULL;
40e084e1 2750 outer = make_cleanup (VEC_cleanup (symbolp), &collector.symbols);
e0881a8e 2751
f8eba3c6
TT
2752 collector.unique_syms = htab_create_alloc (1, htab_hash_pointer,
2753 htab_eq_pointer, NULL,
2754 xcalloc, xfree);
2755 cleanup = make_cleanup_htab_delete (collector.unique_syms);
e0881a8e 2756
ec94af83 2757 for (ix = 0; VEC_iterate (symtab_ptr, file_symtabs, ix, elt); ++ix)
f8eba3c6
TT
2758 {
2759 if (elt == NULL)
2760 {
40e084e1 2761 iterate_over_all_matching_symtabs (state, class_name, STRUCT_DOMAIN,
f8eba3c6 2762 collect_one_symbol, &collector,
481860b3 2763 NULL, 0);
40e084e1 2764 iterate_over_all_matching_symtabs (state, class_name, VAR_DOMAIN,
f8eba3c6 2765 collect_one_symbol, &collector,
481860b3 2766 NULL, 0);
f8eba3c6
TT
2767 }
2768 else
2769 {
f8eba3c6
TT
2770 /* Program spaces that are executing startup should have
2771 been filtered out earlier. */
2772 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
2773 set_current_program_space (SYMTAB_PSPACE (elt));
4eeaa230
DE
2774 iterate_over_file_blocks (elt, class_name, STRUCT_DOMAIN,
2775 collect_one_symbol, &collector);
2776 iterate_over_file_blocks (elt, class_name, VAR_DOMAIN,
2777 collect_one_symbol, &collector);
1e5a1abc
KS
2778 }
2779 }
2780
f8eba3c6
TT
2781 do_cleanups (cleanup);
2782 discard_cleanups (outer);
2783 return collector.symbols;
93d91629
DC
2784}
2785
40e084e1
KS
2786/* A qsort comparison function for symbols. The resulting order does
2787 not actually matter; we just need to be able to sort them so that
2788 symbols with the same program space end up next to each other. */
2789
2790static int
2791compare_symbols (const void *a, const void *b)
2792{
2793 struct symbol * const *sa = a;
2794 struct symbol * const *sb = b;
2795 uintptr_t uia, uib;
2796
08be3fe3
DE
2797 uia = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sa));
2798 uib = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sb));
40e084e1
KS
2799
2800 if (uia < uib)
2801 return -1;
2802 if (uia > uib)
2803 return 1;
2804
2805 uia = (uintptr_t) *sa;
2806 uib = (uintptr_t) *sb;
2807
2808 if (uia < uib)
2809 return -1;
2810 if (uia > uib)
2811 return 1;
2812
2813 return 0;
2814}
2815
2816/* Like compare_symbols but for minimal symbols. */
4224873a 2817
f8eba3c6 2818static int
40e084e1 2819compare_msymbols (const void *a, const void *b)
4224873a 2820{
f60e2d5c
TT
2821 const struct bound_minimal_symbol *sa = a;
2822 const struct bound_minimal_symbol *sb = b;
f8eba3c6
TT
2823 uintptr_t uia, uib;
2824
001822aa
TT
2825 uia = (uintptr_t) sa->objfile->pspace;
2826 uib = (uintptr_t) sa->objfile->pspace;
f8eba3c6
TT
2827
2828 if (uia < uib)
2829 return -1;
2830 if (uia > uib)
2831 return 1;
2832
001822aa
TT
2833 uia = (uintptr_t) sa->minsym;
2834 uib = (uintptr_t) sb->minsym;
f8eba3c6
TT
2835
2836 if (uia < uib)
2837 return -1;
2838 if (uia > uib)
2839 return 1;
2840
2841 return 0;
2842}
2843
2844/* Look for all the matching instances of each symbol in NAMES. Only
2845 instances from PSPACE are considered; other program spaces are
2846 handled by our caller. If PSPACE is NULL, then all program spaces
2847 are considered. Results are stored into INFO. */
2848
2849static void
2850add_all_symbol_names_from_pspace (struct collect_info *info,
2851 struct program_space *pspace,
2852 VEC (const_char_ptr) *names)
2853{
2854 int ix;
2855 const char *iter;
2856
2857 for (ix = 0; VEC_iterate (const_char_ptr, names, ix, iter); ++ix)
2858 add_matching_symbols_to_info (iter, info, pspace);
2859}
2860
2861static void
2862find_superclass_methods (VEC (typep) *superclasses,
2863 const char *name,
2864 VEC (const_char_ptr) **result_names)
2865{
2866 int old_len = VEC_length (const_char_ptr, *result_names);
2867 VEC (typep) *iter_classes;
2868 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
2869
2870 iter_classes = superclasses;
2871 while (1)
2872 {
2873 VEC (typep) *new_supers = NULL;
2874 int ix;
2875 struct type *t;
2876
2877 make_cleanup (VEC_cleanup (typep), &new_supers);
2878 for (ix = 0; VEC_iterate (typep, iter_classes, ix, t); ++ix)
2879 find_methods (t, name, result_names, &new_supers);
2880
2881 if (VEC_length (const_char_ptr, *result_names) != old_len
2882 || VEC_empty (typep, new_supers))
2883 break;
4224873a 2884
f8eba3c6
TT
2885 iter_classes = new_supers;
2886 }
4224873a 2887
f8eba3c6
TT
2888 do_cleanups (cleanup);
2889}
2890
40e084e1
KS
2891/* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
2892 given by one of the symbols in SYM_CLASSES. Matches are returned
2893 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
f8eba3c6 2894
40e084e1 2895static void
ec94af83 2896find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
40e084e1
KS
2897 const char *class_name, const char *method_name,
2898 VEC (symbolp) *sym_classes, VEC (symbolp) **symbols,
f60e2d5c 2899 VEC (bound_minimal_symbol_d) **minsyms)
f8eba3c6 2900{
f8eba3c6
TT
2901 struct symbol *sym;
2902 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
2903 int ix;
2904 int last_result_len;
2905 VEC (typep) *superclass_vec;
2906 VEC (const_char_ptr) *result_names;
2907 struct collect_info info;
4224873a 2908
f8eba3c6
TT
2909 /* Sort symbols so that symbols with the same program space are next
2910 to each other. */
2911 qsort (VEC_address (symbolp, sym_classes),
2912 VEC_length (symbolp, sym_classes),
2913 sizeof (symbolp),
2914 compare_symbols);
2915
2916 info.state = self;
40e084e1
KS
2917 info.file_symtabs = file_symtabs;
2918 info.result.symbols = NULL;
2919 info.result.minimal_symbols = NULL;
f8eba3c6
TT
2920
2921 /* Iterate over all the types, looking for the names of existing
40e084e1 2922 methods matching METHOD_NAME. If we cannot find a direct method in a
f8eba3c6
TT
2923 given program space, then we consider inherited methods; this is
2924 not ideal (ideal would be to respect C++ hiding rules), but it
2925 seems good enough and is what GDB has historically done. We only
2926 need to collect the names because later we find all symbols with
2927 those names. This loop is written in a somewhat funny way
2928 because we collect data across the program space before deciding
2929 what to do. */
2930 superclass_vec = NULL;
2931 make_cleanup (VEC_cleanup (typep), &superclass_vec);
2932 result_names = NULL;
2933 make_cleanup (VEC_cleanup (const_char_ptr), &result_names);
2934 last_result_len = 0;
2935 for (ix = 0; VEC_iterate (symbolp, sym_classes, ix, sym); ++ix)
2936 {
2937 struct type *t;
2938 struct program_space *pspace;
2939
2940 /* Program spaces that are executing startup should have
2941 been filtered out earlier. */
08be3fe3
DE
2942 pspace = SYMTAB_PSPACE (symbol_symtab (sym));
2943 gdb_assert (!pspace->executing_startup);
f8eba3c6
TT
2944 set_current_program_space (pspace);
2945 t = check_typedef (SYMBOL_TYPE (sym));
40e084e1 2946 find_methods (t, method_name, &result_names, &superclass_vec);
f8eba3c6
TT
2947
2948 /* Handle all items from a single program space at once; and be
2949 sure not to miss the last batch. */
2950 if (ix == VEC_length (symbolp, sym_classes) - 1
2951 || (pspace
08be3fe3 2952 != SYMTAB_PSPACE (symbol_symtab (VEC_index (symbolp, sym_classes,
f8eba3c6 2953 ix + 1)))))
4224873a 2954 {
f8eba3c6
TT
2955 /* If we did not find a direct implementation anywhere in
2956 this program space, consider superclasses. */
2957 if (VEC_length (const_char_ptr, result_names) == last_result_len)
40e084e1
KS
2958 find_superclass_methods (superclass_vec, method_name,
2959 &result_names);
f8eba3c6
TT
2960
2961 /* We have a list of candidate symbol names, so now we
2962 iterate over the symbol tables looking for all
2963 matches in this pspace. */
2964 add_all_symbol_names_from_pspace (&info, pspace, result_names);
2965
2966 VEC_truncate (typep, superclass_vec, 0);
2967 last_result_len = VEC_length (const_char_ptr, result_names);
4224873a 2968 }
4224873a 2969 }
f8eba3c6 2970
40e084e1 2971 if (!VEC_empty (symbolp, info.result.symbols)
f60e2d5c 2972 || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
4224873a 2973 {
40e084e1
KS
2974 *symbols = info.result.symbols;
2975 *minsyms = info.result.minimal_symbols;
f8eba3c6 2976 do_cleanups (cleanup);
40e084e1 2977 return;
4224873a 2978 }
f8eba3c6 2979
40e084e1
KS
2980 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
2981 and other attempts to locate the symbol will be made. */
2982 throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
f8eba3c6
TT
2983}
2984
2985\f
2986
2987/* This object is used when collecting all matching symtabs. */
2988
2989struct symtab_collector
2990{
2991 /* The result vector of symtabs. */
ec94af83 2992 VEC (symtab_ptr) *symtabs;
f8eba3c6
TT
2993
2994 /* This is used to ensure the symtabs are unique. */
2995 htab_t symtab_table;
2996};
2997
2998/* Callback for iterate_over_symtabs. */
2999
3000static int
3001add_symtabs_to_list (struct symtab *symtab, void *d)
3002{
3003 struct symtab_collector *data = d;
3004 void **slot;
3005
3006 slot = htab_find_slot (data->symtab_table, symtab, INSERT);
3007 if (!*slot)
4224873a 3008 {
f8eba3c6 3009 *slot = symtab;
ec94af83 3010 VEC_safe_push (symtab_ptr, data->symtabs, symtab);
4224873a 3011 }
f8eba3c6
TT
3012
3013 return 0;
4224873a
DC
3014}
3015
f8eba3c6
TT
3016/* Given a file name, return a VEC of all matching symtabs. */
3017
ec94af83 3018static VEC (symtab_ptr) *
f8eba3c6
TT
3019collect_symtabs_from_filename (const char *file)
3020{
3021 struct symtab_collector collector;
3022 struct cleanup *cleanups;
3023 struct program_space *pspace;
3024
3025 collector.symtabs = NULL;
3026 collector.symtab_table = htab_create (1, htab_hash_pointer, htab_eq_pointer,
3027 NULL);
3028 cleanups = make_cleanup_htab_delete (collector.symtab_table);
3029
3030 /* Find that file's data. */
3031 ALL_PSPACES (pspace)
3032 {
3033 if (pspace->executing_startup)
3034 continue;
3035
3036 set_current_program_space (pspace);
3037 iterate_over_symtabs (file, add_symtabs_to_list, &collector);
3038 }
f3c39e76 3039
f8eba3c6
TT
3040 do_cleanups (cleanups);
3041 return collector.symtabs;
3042}
3043
40e084e1 3044/* Return all the symtabs associated to the FILENAME. */
f8eba3c6 3045
ec94af83 3046static VEC (symtab_ptr) *
40e084e1
KS
3047symtabs_from_filename (const char *filename)
3048{
ec94af83 3049 VEC (symtab_ptr) *result;
40e084e1
KS
3050
3051 result = collect_symtabs_from_filename (filename);
f8eba3c6 3052
ec94af83 3053 if (VEC_empty (symtab_ptr, result))
f8eba3c6 3054 {
40e084e1
KS
3055 if (!have_full_symbols () && !have_partial_symbols ())
3056 throw_error (NOT_FOUND_ERROR,
3057 _("No symbol table is loaded. "
3058 "Use the \"file\" command."));
3059 throw_error (NOT_FOUND_ERROR, _("No source file named %s."), filename);
f8eba3c6
TT
3060 }
3061
40e084e1 3062 return result;
84fba31b 3063}
f3c39e76 3064
40e084e1
KS
3065/* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3066 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3067 returned in MINSYMS. */
14e91ac5 3068
40e084e1
KS
3069static void
3070find_function_symbols (struct linespec_state *state,
ec94af83 3071 VEC (symtab_ptr) *file_symtabs, const char *name,
40e084e1 3072 VEC (symbolp) **symbols,
f60e2d5c 3073 VEC (bound_minimal_symbol_d) **minsyms)
14e91ac5 3074{
40e084e1
KS
3075 struct collect_info info;
3076 VEC (const_char_ptr) *symbol_names = NULL;
3077 struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
3078 &symbol_names);
14e91ac5 3079
40e084e1
KS
3080 info.state = state;
3081 info.result.symbols = NULL;
3082 info.result.minimal_symbols = NULL;
3083 info.file_symtabs = file_symtabs;
e0881a8e 3084
40e084e1 3085 /* Try NAME as an Objective-C selector. */
d7561cbb 3086 find_imps (name, &symbol_names);
40e084e1
KS
3087 if (!VEC_empty (const_char_ptr, symbol_names))
3088 add_all_symbol_names_from_pspace (&info, NULL, symbol_names);
3089 else
3090 add_matching_symbols_to_info (name, &info, NULL);
3091
3092 do_cleanups (cleanup);
3093
3094 if (VEC_empty (symbolp, info.result.symbols))
3095 {
3096 VEC_free (symbolp, info.result.symbols);
3097 *symbols = NULL;
14e91ac5
DC
3098 }
3099 else
40e084e1
KS
3100 *symbols = info.result.symbols;
3101
f60e2d5c 3102 if (VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
14e91ac5 3103 {
f60e2d5c 3104 VEC_free (bound_minimal_symbol_d, info.result.minimal_symbols);
40e084e1
KS
3105 *minsyms = NULL;
3106 }
3107 else
3108 *minsyms = info.result.minimal_symbols;
3109}
3110
3111/* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3112 in SYMBOLS and minimal symbols in MINSYMS. */
14e91ac5 3113
b1ae631a 3114static void
40e084e1 3115find_linespec_symbols (struct linespec_state *state,
ec94af83 3116 VEC (symtab_ptr) *file_symtabs,
40e084e1
KS
3117 const char *name,
3118 VEC (symbolp) **symbols,
f60e2d5c 3119 VEC (bound_minimal_symbol_d) **minsyms)
40e084e1 3120{
40e084e1 3121 struct cleanup *cleanup;
cc81e1c6
DE
3122 char *canon;
3123 const char *lookup_name;
f8eba3c6 3124
40e084e1
KS
3125 cleanup = demangle_for_lookup (name, state->language->la_language,
3126 &lookup_name);
3127 if (state->language->la_language == language_ada)
3128 {
3129 /* In Ada, the symbol lookups are performed using the encoded
3130 name rather than the demangled name. */
3131 lookup_name = ada_name_for_lookup (name);
3132 make_cleanup (xfree, (void *) lookup_name);
3133 }
31aba06f 3134
40e084e1
KS
3135 canon = cp_canonicalize_string_no_typedefs (lookup_name);
3136 if (canon != NULL)
3137 {
3138 lookup_name = canon;
e61727ab 3139 make_cleanup (xfree, canon);
40e084e1 3140 }
f8eba3c6 3141
cc81e1c6
DE
3142 /* It's important to not call expand_symtabs_matching unnecessarily
3143 as it can really slow things down (by unnecessarily expanding
3144 potentially 1000s of symtabs, which when debugging some apps can
3145 cost 100s of seconds). Avoid this to some extent by *first* calling
3146 find_function_symbols, and only if that doesn't find anything
3147 *then* call find_method. This handles two important cases:
3148 1) break (anonymous namespace)::foo
3149 2) break class::method where method is in class (and not a baseclass) */
14e91ac5 3150
cc81e1c6
DE
3151 find_function_symbols (state, file_symtabs, lookup_name,
3152 symbols, minsyms);
14e91ac5 3153
cc81e1c6
DE
3154 /* If we were unable to locate a symbol of the same name, try dividing
3155 the name into class and method names and searching the class and its
3156 baseclasses. */
3157 if (VEC_empty (symbolp, *symbols)
f60e2d5c 3158 && VEC_empty (bound_minimal_symbol_d, *minsyms))
40e084e1 3159 {
cc81e1c6
DE
3160 char *klass, *method;
3161 const char *last, *p, *scope_op;
3162 VEC (symbolp) *classes;
14e91ac5 3163
cc81e1c6
DE
3164 /* See if we can find a scope operator and break this symbol
3165 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3166 scope_op = "::";
3167 p = find_toplevel_string (lookup_name, scope_op);
3168 if (p == NULL)
3169 {
3170 /* No C++ scope operator. Try Java. */
3171 scope_op = ".";
3172 p = find_toplevel_string (lookup_name, scope_op);
3173 }
14e91ac5 3174
cc81e1c6
DE
3175 last = NULL;
3176 while (p != NULL)
f8eba3c6 3177 {
cc81e1c6
DE
3178 last = p;
3179 p = find_toplevel_string (p + strlen (scope_op), scope_op);
f8eba3c6 3180 }
14e91ac5 3181
cc81e1c6
DE
3182 /* If no scope operator was found, there is nothing more we can do;
3183 we already attempted to lookup the entire name as a symbol
3184 and failed. */
3185 if (last == NULL)
40e084e1
KS
3186 {
3187 do_cleanups (cleanup);
3188 return;
3189 }
cc81e1c6
DE
3190
3191 /* LOOKUP_NAME points to the class name.
3192 LAST points to the method name. */
3193 klass = xmalloc ((last - lookup_name + 1) * sizeof (char));
3194 make_cleanup (xfree, klass);
3195 strncpy (klass, lookup_name, last - lookup_name);
3196 klass[last - lookup_name] = '\0';
3197
3198 /* Skip past the scope operator. */
3199 last += strlen (scope_op);
3200 method = xmalloc ((strlen (last) + 1) * sizeof (char));
3201 make_cleanup (xfree, method);
3202 strcpy (method, last);
3203
3204 /* Find a list of classes named KLASS. */
3205 classes = lookup_prefix_sym (state, file_symtabs, klass);
3206 make_cleanup (VEC_cleanup (symbolp), &classes);
3207
3208 if (!VEC_empty (symbolp, classes))
3209 {
3210 /* Now locate a list of suitable methods named METHOD. */
492d29ea 3211 TRY
cc81e1c6
DE
3212 {
3213 find_method (state, file_symtabs, klass, method, classes,
3214 symbols, minsyms);
3215 }
3216
3217 /* If successful, we're done. If NOT_FOUND_ERROR
3218 was not thrown, rethrow the exception that we did get. */
492d29ea 3219 CATCH (except, RETURN_MASK_ERROR)
7556d4a4
PA
3220 {
3221 if (except.error != NOT_FOUND_ERROR)
3222 throw_exception (except);
3223 }
492d29ea 3224 END_CATCH
cc81e1c6 3225 }
f8eba3c6 3226 }
14e91ac5 3227
cc81e1c6 3228 do_cleanups (cleanup);
14e91ac5
DC
3229}
3230
40e084e1
KS
3231/* Return all labels named NAME in FUNCTION_SYMBOLS. Return the
3232 actual function symbol in which the label was found in LABEL_FUNC_RET. */
0f5238ed 3233
40e084e1
KS
3234static VEC (symbolp) *
3235find_label_symbols (struct linespec_state *self,
3236 VEC (symbolp) *function_symbols,
3237 VEC (symbolp) **label_funcs_ret, const char *name)
0f5238ed 3238{
f8eba3c6 3239 int ix;
3977b71f 3240 const struct block *block;
40e084e1
KS
3241 struct symbol *sym;
3242 struct symbol *fn_sym;
3243 VEC (symbolp) *result = NULL;
9ef07c8c 3244
f8eba3c6 3245 if (function_symbols == NULL)
9ef07c8c 3246 {
f8eba3c6 3247 set_current_program_space (self->program_space);
4eeaa230 3248 block = get_current_search_block ();
f8eba3c6 3249
9ef07c8c
TT
3250 for (;
3251 block && !BLOCK_FUNCTION (block);
3252 block = BLOCK_SUPERBLOCK (block))
3253 ;
3254 if (!block)
40e084e1 3255 return NULL;
f8eba3c6
TT
3256 fn_sym = BLOCK_FUNCTION (block);
3257
40e084e1 3258 sym = lookup_symbol (name, block, LABEL_DOMAIN, 0);
f8eba3c6 3259
40e084e1
KS
3260 if (sym != NULL)
3261 {
3262 VEC_safe_push (symbolp, result, sym);
3263 VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
3264 }
3265 }
3266 else
3267 {
3268 for (ix = 0;
3269 VEC_iterate (symbolp, function_symbols, ix, fn_sym); ++ix)
f8eba3c6 3270 {
08be3fe3 3271 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym)));
40e084e1
KS
3272 block = SYMBOL_BLOCK_VALUE (fn_sym);
3273 sym = lookup_symbol (name, block, LABEL_DOMAIN, 0);
3274
3275 if (sym != NULL)
3276 {
3277 VEC_safe_push (symbolp, result, sym);
3278 VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
3279 }
f8eba3c6 3280 }
40e084e1 3281 }
f8eba3c6 3282
40e084e1
KS
3283 return result;
3284}
f8eba3c6 3285
40e084e1
KS
3286\f
3287
3288/* A helper for create_sals_line_offset that handles the 'list_mode' case. */
3289
3290static void
3291decode_digits_list_mode (struct linespec_state *self,
3292 linespec_p ls,
3293 struct symtabs_and_lines *values,
3294 struct symtab_and_line val)
3295{
3296 int ix;
3297 struct symtab *elt;
3298
3299 gdb_assert (self->list_mode);
3300
ec94af83 3301 for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt);
40e084e1
KS
3302 ++ix)
3303 {
3304 /* The logic above should ensure this. */
3305 gdb_assert (elt != NULL);
3306
3307 set_current_program_space (SYMTAB_PSPACE (elt));
3308
3309 /* Simplistic search just for the list command. */
3310 val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
3311 if (val.symtab == NULL)
3312 val.symtab = elt;
3313 val.pspace = SYMTAB_PSPACE (elt);
3314 val.pc = 0;
3315 val.explicit_line = 1;
3316
66f1999b 3317 add_sal_to_sals (self, values, &val, NULL, 0);
f8eba3c6 3318 }
40e084e1 3319}
f8eba3c6 3320
40e084e1
KS
3321/* A helper for create_sals_line_offset that iterates over the symtabs,
3322 adding lines to the VEC. */
3323
3324static void
3325decode_digits_ordinary (struct linespec_state *self,
3326 linespec_p ls,
3327 int line,
3328 struct symtabs_and_lines *sals,
3329 struct linetable_entry **best_entry)
3330{
3331 int ix;
3332 struct symtab *elt;
f8eba3c6 3333
ec94af83 3334 for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt); ++ix)
f8eba3c6 3335 {
40e084e1
KS
3336 int i;
3337 VEC (CORE_ADDR) *pcs;
3338 CORE_ADDR pc;
3339
3340 /* The logic above should ensure this. */
3341 gdb_assert (elt != NULL);
f8eba3c6 3342
40e084e1 3343 set_current_program_space (SYMTAB_PSPACE (elt));
f8eba3c6 3344
40e084e1
KS
3345 pcs = find_pcs_for_symtab_line (elt, line, best_entry);
3346 for (i = 0; VEC_iterate (CORE_ADDR, pcs, i, pc); ++i)
f8eba3c6
TT
3347 {
3348 struct symtab_and_line sal;
40e084e1
KS
3349
3350 init_sal (&sal);
3351 sal.pspace = SYMTAB_PSPACE (elt);
3352 sal.symtab = elt;
3353 sal.line = line;
3354 sal.pc = pc;
3355 add_sal_to_sals_basic (sals, &sal);
f8eba3c6 3356 }
40e084e1
KS
3357
3358 VEC_free (CORE_ADDR, pcs);
f8eba3c6 3359 }
40e084e1
KS
3360}
3361
3362\f
3363
3364/* Return the line offset represented by VARIABLE. */
3365
3366static struct line_offset
3367linespec_parse_variable (struct linespec_state *self, const char *variable)
3368{
3369 int index = 0;
3370 const char *p;
3371 struct line_offset offset = {0, LINE_OFFSET_NONE};
f8eba3c6 3372
40e084e1
KS
3373 p = (variable[1] == '$') ? variable + 2 : variable + 1;
3374 if (*p == '$')
3375 ++p;
3376 while (*p >= '0' && *p <= '9')
3377 ++p;
3378 if (!*p) /* Reached end of token without hitting non-digit. */
f8eba3c6 3379 {
40e084e1
KS
3380 /* We have a value history reference. */
3381 struct value *val_history;
f8eba3c6 3382
40e084e1
KS
3383 sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index);
3384 val_history
3385 = access_value_history ((variable[1] == '$') ? -index : index);
3386 if (TYPE_CODE (value_type (val_history)) != TYPE_CODE_INT)
3387 error (_("History values used in line "
3388 "specs must have integer values."));
3389 offset.offset = value_as_long (val_history);
3390 }
3391 else
3392 {
3393 /* Not all digits -- may be user variable/function or a
3394 convenience variable. */
3395 LONGEST valx;
3396 struct internalvar *ivar;
3397
3398 /* Try it as a convenience variable. If it is not a convenience
3399 variable, return and allow normal symbol lookup to occur. */
3400 ivar = lookup_only_internalvar (variable + 1);
3401 if (ivar == NULL)
3402 /* No internal variable with that name. Mark the offset
3403 as unknown to allow the name to be looked up as a symbol. */
3404 offset.sign = LINE_OFFSET_UNKNOWN;
3405 else
3406 {
3407 /* We found a valid variable name. If it is not an integer,
3408 throw an error. */
3409 if (!get_internalvar_integer (ivar, &valx))
3410 error (_("Convenience variables used in line "
3411 "specs must have integer values."));
3412 else
3413 offset.offset = valx;
3414 }
f8eba3c6
TT
3415 }
3416
40e084e1 3417 return offset;
f8eba3c6 3418}
40e084e1 3419\f
f8eba3c6
TT
3420
3421/* A callback used to possibly add a symbol to the results. */
3422
3423static int
3424collect_symbols (struct symbol *sym, void *data)
3425{
3426 struct collect_info *info = data;
f8eba3c6 3427
40e084e1
KS
3428 /* In list mode, add all matching symbols, regardless of class.
3429 This allows the user to type "list a_global_variable". */
3430 if (SYMBOL_CLASS (sym) == LOC_BLOCK || info->state->list_mode)
3431 VEC_safe_push (symbolp, info->result.symbols, sym);
8e704927 3432 return 1; /* Continue iterating. */
f8eba3c6
TT
3433}
3434
40e084e1
KS
3435/* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
3436 linespec; return the SAL in RESULT. */
f8eba3c6
TT
3437
3438static void
3439minsym_found (struct linespec_state *self, struct objfile *objfile,
3440 struct minimal_symbol *msymbol,
3441 struct symtabs_and_lines *result)
3442{
3443 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3444 CORE_ADDR pc;
3445 struct symtab_and_line sal;
3446
77e371c0 3447 sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (objfile, msymbol),
f8eba3c6 3448 (struct obj_section *) 0, 0);
efd66ac6 3449 sal.section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
f8eba3c6
TT
3450
3451 /* The minimal symbol might point to a function descriptor;
3452 resolve it to the actual code address instead. */
3453 pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, &current_target);
3454 if (pc != sal.pc)
3455 sal = find_pc_sect_line (pc, NULL, 0);
3456
3457 if (self->funfirstline)
3458 skip_prologue_sal (&sal);
3459
07fea4b4 3460 if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
efd66ac6 3461 add_sal_to_sals (self, result, &sal, MSYMBOL_NATURAL_NAME (msymbol), 0);
f8eba3c6
TT
3462}
3463
39b856a4
TT
3464/* A helper struct to pass some data through
3465 iterate_over_minimal_symbols. */
3466
3467struct collect_minsyms
3468{
3469 /* The objfile we're examining. */
3470 struct objfile *objfile;
3471
87186c6a
MMN
3472 /* Only search the given symtab, or NULL to search for all symbols. */
3473 struct symtab *symtab;
3474
39b856a4
TT
3475 /* The funfirstline setting from the initial call. */
3476 int funfirstline;
3477
095bcf5e
JB
3478 /* The list_mode setting from the initial call. */
3479 int list_mode;
3480
39b856a4 3481 /* The resulting symbols. */
f60e2d5c 3482 VEC (bound_minimal_symbol_d) *msyms;
39b856a4
TT
3483};
3484
3485/* A helper function to classify a minimal_symbol_type according to
3486 priority. */
3487
3488static int
3489classify_mtype (enum minimal_symbol_type t)
3490{
3491 switch (t)
f8eba3c6 3492 {
39b856a4
TT
3493 case mst_file_text:
3494 case mst_file_data:
3495 case mst_file_bss:
3496 /* Intermediate priority. */
3497 return 1;
3498
3499 case mst_solib_trampoline:
3500 /* Lowest priority. */
3501 return 2;
3502
3503 default:
3504 /* Highest priority. */
3505 return 0;
f8eba3c6 3506 }
39b856a4
TT
3507}
3508
3509/* Callback for qsort that sorts symbols by priority. */
3510
3511static int
3512compare_msyms (const void *a, const void *b)
3513{
f60e2d5c
TT
3514 const bound_minimal_symbol_d *moa = a;
3515 const bound_minimal_symbol_d *mob = b;
39b856a4
TT
3516 enum minimal_symbol_type ta = MSYMBOL_TYPE (moa->minsym);
3517 enum minimal_symbol_type tb = MSYMBOL_TYPE (mob->minsym);
3518
3519 return classify_mtype (ta) - classify_mtype (tb);
3520}
3521
3522/* Callback for iterate_over_minimal_symbols that adds the symbol to
3523 the result. */
3524
3525static void
3526add_minsym (struct minimal_symbol *minsym, void *d)
3527{
3528 struct collect_minsyms *info = d;
f60e2d5c 3529 bound_minimal_symbol_d mo;
39b856a4 3530
77e371c0
TT
3531 mo.minsym = minsym;
3532 mo.objfile = info->objfile;
3533
87186c6a
MMN
3534 if (info->symtab != NULL)
3535 {
3536 CORE_ADDR pc;
3537 struct symtab_and_line sal;
3538 struct gdbarch *gdbarch = get_objfile_arch (info->objfile);
3539
3540 sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (info->objfile, minsym),
3541 NULL, 0);
3542 sal.section = MSYMBOL_OBJ_SECTION (info->objfile, minsym);
3543 pc
3544 = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, &current_target);
3545 if (pc != sal.pc)
3546 sal = find_pc_sect_line (pc, NULL, 0);
3547
3548 if (info->symtab != sal.symtab)
3549 return;
3550 }
3551
095bcf5e
JB
3552 /* Exclude data symbols when looking for breakpoint locations. */
3553 if (!info->list_mode)
3554 switch (minsym->type)
3555 {
3556 case mst_slot_got_plt:
3557 case mst_data:
3558 case mst_bss:
3559 case mst_abs:
3560 case mst_file_data:
3561 case mst_file_bss:
1a2da5ee
JB
3562 {
3563 /* Make sure this minsym is not a function descriptor
3564 before we decide to discard it. */
df6d5441 3565 struct gdbarch *gdbarch = get_objfile_arch (info->objfile);
1a2da5ee 3566 CORE_ADDR addr = gdbarch_convert_from_func_ptr_addr
77e371c0 3567 (gdbarch, BMSYMBOL_VALUE_ADDRESS (mo),
1a2da5ee
JB
3568 &current_target);
3569
77e371c0 3570 if (addr == BMSYMBOL_VALUE_ADDRESS (mo))
1a2da5ee
JB
3571 return;
3572 }
095bcf5e
JB
3573 }
3574
f60e2d5c 3575 VEC_safe_push (bound_minimal_symbol_d, info->msyms, &mo);
f8eba3c6
TT
3576}
3577
87186c6a 3578/* Search for minimal symbols called NAME. If SEARCH_PSPACE
f8eba3c6 3579 is not NULL, the search is restricted to just that program
87186c6a
MMN
3580 space.
3581
3582 If SYMTAB is NULL, search all objfiles, otherwise
3583 restrict results to the given SYMTAB. */
f8eba3c6
TT
3584
3585static void
3586search_minsyms_for_name (struct collect_info *info, const char *name,
87186c6a
MMN
3587 struct program_space *search_pspace,
3588 struct symtab *symtab)
f8eba3c6 3589{
87186c6a
MMN
3590 struct collect_minsyms local;
3591 struct cleanup *cleanup;
f8eba3c6 3592
87186c6a
MMN
3593 memset (&local, 0, sizeof (local));
3594 local.funfirstline = info->state->funfirstline;
3595 local.list_mode = info->state->list_mode;
3596 local.symtab = symtab;
39b856a4 3597
87186c6a 3598 cleanup = make_cleanup (VEC_cleanup (bound_minimal_symbol_d), &local.msyms);
f8eba3c6 3599
87186c6a
MMN
3600 if (symtab == NULL)
3601 {
3602 struct program_space *pspace;
f8eba3c6 3603
87186c6a
MMN
3604 ALL_PSPACES (pspace)
3605 {
3606 struct objfile *objfile;
39b856a4 3607
87186c6a
MMN
3608 if (search_pspace != NULL && search_pspace != pspace)
3609 continue;
3610 if (pspace->executing_startup)
3611 continue;
39b856a4 3612
87186c6a
MMN
3613 set_current_program_space (pspace);
3614
3615 ALL_OBJFILES (objfile)
3616 {
3617 local.objfile = objfile;
3618 iterate_over_minimal_symbols (objfile, name, add_minsym, &local);
3619 }
3620 }
3621 }
3622 else
f8eba3c6 3623 {
87186c6a
MMN
3624 if (search_pspace == NULL || SYMTAB_PSPACE (symtab) == search_pspace)
3625 {
3626 set_current_program_space (SYMTAB_PSPACE (symtab));
3627 local.objfile = SYMTAB_OBJFILE(symtab);
3628 iterate_over_minimal_symbols (local.objfile, name, add_minsym,
3629 &local);
3630 }
9ef07c8c 3631 }
39b856a4 3632
f60e2d5c 3633 if (!VEC_empty (bound_minimal_symbol_d, local.msyms))
39b856a4
TT
3634 {
3635 int classification;
3636 int ix;
f60e2d5c 3637 bound_minimal_symbol_d *item;
39b856a4 3638
f60e2d5c
TT
3639 qsort (VEC_address (bound_minimal_symbol_d, local.msyms),
3640 VEC_length (bound_minimal_symbol_d, local.msyms),
3641 sizeof (bound_minimal_symbol_d),
39b856a4
TT
3642 compare_msyms);
3643
3644 /* Now the minsyms are in classification order. So, we walk
3645 over them and process just the minsyms with the same
3646 classification as the very first minsym in the list. */
f60e2d5c 3647 item = VEC_index (bound_minimal_symbol_d, local.msyms, 0);
39b856a4
TT
3648 classification = classify_mtype (MSYMBOL_TYPE (item->minsym));
3649
3650 for (ix = 0;
f60e2d5c 3651 VEC_iterate (bound_minimal_symbol_d, local.msyms, ix, item);
39b856a4
TT
3652 ++ix)
3653 {
3654 if (classify_mtype (MSYMBOL_TYPE (item->minsym)) != classification)
3655 break;
3656
f60e2d5c 3657 VEC_safe_push (bound_minimal_symbol_d,
40e084e1 3658 info->result.minimal_symbols, item);
39b856a4
TT
3659 }
3660 }
3661
3662 do_cleanups (cleanup);
f8eba3c6
TT
3663}
3664
3665/* A helper function to add all symbols matching NAME to INFO. If
3666 PSPACE is not NULL, the search is restricted to just that program
3667 space. */
0f5238ed 3668
f8eba3c6
TT
3669static void
3670add_matching_symbols_to_info (const char *name,
3671 struct collect_info *info,
3672 struct program_space *pspace)
3673{
3674 int ix;
3675 struct symtab *elt;
0f5238ed 3676
ec94af83 3677 for (ix = 0; VEC_iterate (symtab_ptr, info->file_symtabs, ix, elt); ++ix)
f8eba3c6 3678 {
f8eba3c6
TT
3679 if (elt == NULL)
3680 {
40e084e1 3681 iterate_over_all_matching_symtabs (info->state, name, VAR_DOMAIN,
f8eba3c6 3682 collect_symbols, info,
481860b3 3683 pspace, 1);
87186c6a 3684 search_minsyms_for_name (info, name, pspace, NULL);
f8eba3c6
TT
3685 }
3686 else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt))
3687 {
87186c6a
MMN
3688 int prev_len = VEC_length (symbolp, info->result.symbols);
3689
f8eba3c6
TT
3690 /* Program spaces that are executing startup should have
3691 been filtered out earlier. */
3692 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
3693 set_current_program_space (SYMTAB_PSPACE (elt));
4eeaa230
DE
3694 iterate_over_file_blocks (elt, name, VAR_DOMAIN,
3695 collect_symbols, info);
87186c6a
MMN
3696
3697 /* If no new symbols were found in this iteration and this symtab
3698 is in assembler, we might actually be looking for a label for
3699 which we don't have debug info. Check for a minimal symbol in
3700 this case. */
3701 if (prev_len == VEC_length (symbolp, info->result.symbols)
3702 && elt->language == language_asm)
3703 search_minsyms_for_name (info, name, pspace, elt);
f8eba3c6
TT
3704 }
3705 }
0f5238ed
TT
3706}
3707
14e91ac5
DC
3708\f
3709
413dad4d
DC
3710/* Now come some functions that are called from multiple places within
3711 decode_line_1. */
3712
f8eba3c6
TT
3713static int
3714symbol_to_sal (struct symtab_and_line *result,
3715 int funfirstline, struct symbol *sym)
413dad4d 3716{
413dad4d 3717 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
50641945 3718 {
f8eba3c6
TT
3719 *result = find_function_start_sal (sym, funfirstline);
3720 return 1;
50641945 3721 }
413dad4d
DC
3722 else
3723 {
62853458 3724 if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
413dad4d 3725 {
f8eba3c6 3726 init_sal (result);
08be3fe3 3727 result->symtab = symbol_symtab (sym);
f8eba3c6
TT
3728 result->line = SYMBOL_LINE (sym);
3729 result->pc = SYMBOL_VALUE_ADDRESS (sym);
08be3fe3 3730 result->pspace = SYMTAB_PSPACE (result->symtab);
f8eba3c6
TT
3731 result->explicit_pc = 1;
3732 return 1;
413dad4d 3733 }
62853458 3734 else if (funfirstline)
dcf9f4ab 3735 {
f8eba3c6 3736 /* Nothing. */
dcf9f4ab 3737 }
62853458
TT
3738 else if (SYMBOL_LINE (sym) != 0)
3739 {
3740 /* We know its line number. */
f8eba3c6 3741 init_sal (result);
08be3fe3 3742 result->symtab = symbol_symtab (sym);
f8eba3c6 3743 result->line = SYMBOL_LINE (sym);
08be3fe3 3744 result->pspace = SYMTAB_PSPACE (result->symtab);
f8eba3c6 3745 return 1;
62853458 3746 }
413dad4d 3747 }
f8eba3c6
TT
3748
3749 return 0;
413dad4d 3750}
50641945 3751
f8eba3c6 3752/* See the comment in linespec.h. */
50641945 3753
f8eba3c6
TT
3754void
3755init_linespec_result (struct linespec_result *lr)
413dad4d 3756{
f8eba3c6
TT
3757 memset (lr, 0, sizeof (*lr));
3758}
413dad4d 3759
f8eba3c6 3760/* See the comment in linespec.h. */
bccdca4a 3761
f8eba3c6
TT
3762void
3763destroy_linespec_result (struct linespec_result *ls)
3764{
3765 int i;
3766 struct linespec_sals *lsal;
bccdca4a 3767
f8eba3c6
TT
3768 xfree (ls->addr_string);
3769 for (i = 0; VEC_iterate (linespec_sals, ls->sals, i, lsal); ++i)
3770 {
3771 xfree (lsal->canonical);
3772 xfree (lsal->sals.sals);
3773 }
3774 VEC_free (linespec_sals, ls->sals);
3775}
e48883f7 3776
f8eba3c6
TT
3777/* Cleanup function for a linespec_result. */
3778
3779static void
3780cleanup_linespec_result (void *a)
3781{
3782 destroy_linespec_result (a);
50641945 3783}
7efd8fc2 3784
f8eba3c6
TT
3785/* See the comment in linespec.h. */
3786
3787struct cleanup *
3788make_cleanup_destroy_linespec_result (struct linespec_result *ls)
7efd8fc2 3789{
f8eba3c6 3790 return make_cleanup (cleanup_linespec_result, ls);
7efd8fc2 3791}
This page took 1.460054 seconds and 4 git commands to generate.