]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/linespec.c
Introduce lookup_name_info and generalize Ada's FULL/WILD name matching
[thirdparty/binutils-gdb.git] / gdb / linespec.c
CommitLineData
50641945 1/* Parser for linespec for the GNU debugger, GDB.
05ff989b 2
61baf725 3 Copyright (C) 1986-2017 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"
f00aae0f 46#include "location.h"
14bc53a8 47#include "common/function-view.h"
0fc21fd8 48#include "common/def-vector.h"
f8eba3c6 49
c45ec17c
PA
50/* An enumeration of the various things a user might attempt to
51 complete for a linespec location. */
52
53enum class linespec_complete_what
54{
55 /* Nothing, no possible completion. */
56 NOTHING,
57
58 /* A function/method name. Due to ambiguity between
59
60 (gdb) b source[TAB]
61 source_file.c
62 source_function
63
64 this can also indicate a source filename, iff we haven't seen a
65 separate source filename component, as in "b source.c:function". */
66 FUNCTION,
67
68 /* A label symbol. E.g., break file.c:function:LABEL. */
69 LABEL,
70
71 /* An expression. E.g., "break foo if EXPR", or "break *EXPR". */
72 EXPRESSION,
73
74 /* A linespec keyword ("if"/"thread"/"task").
75 E.g., "break func threa<tab>". */
76 KEYWORD,
77};
78
f8eba3c6
TT
79typedef struct symbol *symbolp;
80DEF_VEC_P (symbolp);
81
82typedef struct type *typep;
83DEF_VEC_P (typep);
84
85/* An address entry is used to ensure that any given location is only
86 added to the result a single time. It holds an address and the
87 program space from which the address came. */
88
89struct address_entry
90{
91 struct program_space *pspace;
92 CORE_ADDR addr;
93};
94
f60e2d5c 95typedef struct bound_minimal_symbol bound_minimal_symbol_d;
40e084e1 96
f60e2d5c 97DEF_VEC_O (bound_minimal_symbol_d);
40e084e1 98
40e084e1
KS
99/* A linespec. Elements of this structure are filled in by a parser
100 (either parse_linespec or some other function). The structure is
101 then converted into SALs by convert_linespec_to_sals. */
102
103struct linespec
104{
00e52e53 105 /* An explicit location describing the SaLs. */
67994074 106 struct explicit_location explicit_loc;
40e084e1
KS
107
108 /* The list of symtabs to search to which to limit the search. May not
00e52e53
KS
109 be NULL. If explicit.SOURCE_FILENAME is NULL (no user-specified
110 filename), FILE_SYMTABS should contain one single NULL member. This
111 will cause the code to use the default symtab. */
ec94af83 112 VEC (symtab_ptr) *file_symtabs;
40e084e1 113
40e084e1
KS
114 /* A list of matching function symbols and minimal symbols. Both lists
115 may be NULL if no matching symbols were found. */
116 VEC (symbolp) *function_symbols;
f60e2d5c 117 VEC (bound_minimal_symbol_d) *minimal_symbols;
40e084e1 118
40e084e1
KS
119 /* A structure of matching label symbols and the corresponding
120 function symbol in which the label was found. Both may be NULL
121 or both must be non-NULL. */
122 struct
123 {
124 VEC (symbolp) *label_symbols;
125 VEC (symbolp) *function_symbols;
126 } labels;
40e084e1
KS
127};
128typedef struct linespec *linespec_p;
129
33f448b1
JK
130/* A canonical linespec represented as a symtab-related string.
131
132 Each entry represents the "SYMTAB:SUFFIX" linespec string.
133 SYMTAB can be converted for example by symtab_to_fullname or
134 symtab_to_filename_for_display as needed. */
135
136struct linespec_canonical_name
137{
138 /* Remaining text part of the linespec string. */
139 char *suffix;
140
141 /* If NULL then SUFFIX is the whole linespec string. */
142 struct symtab *symtab;
143};
144
f8eba3c6
TT
145/* An instance of this is used to keep all state while linespec
146 operates. This instance is passed around as a 'this' pointer to
147 the various implementation methods. */
148
149struct linespec_state
150{
40e084e1
KS
151 /* The language in use during linespec processing. */
152 const struct language_defn *language;
153
f8eba3c6
TT
154 /* The program space as seen when the module was entered. */
155 struct program_space *program_space;
156
c2f4122d
PA
157 /* If not NULL, the search is restricted to just this program
158 space. */
159 struct program_space *search_pspace;
160
f8eba3c6
TT
161 /* The default symtab to use, if no other symtab is specified. */
162 struct symtab *default_symtab;
163
164 /* The default line to use. */
165 int default_line;
166
f8eba3c6
TT
167 /* The 'funfirstline' value that was passed in to decode_line_1 or
168 decode_line_full. */
169 int funfirstline;
170
171 /* Nonzero if we are running in 'list' mode; see decode_line_list. */
172 int list_mode;
173
174 /* The 'canonical' value passed to decode_line_full, or NULL. */
175 struct linespec_result *canonical;
176
6c5b2ebe 177 /* Canonical strings that mirror the std::vector<symtab_and_line> result. */
33f448b1 178 struct linespec_canonical_name *canonical_names;
f8eba3c6
TT
179
180 /* This is a set of address_entry objects which is used to prevent
181 duplicate symbols from being entered into the result. */
182 htab_t addr_set;
00e52e53
KS
183
184 /* Are we building a linespec? */
185 int is_linespec;
f8eba3c6
TT
186};
187
188/* This is a helper object that is used when collecting symbols into a
189 result. */
190
191struct collect_info
192{
193 /* The linespec object in use. */
194 struct linespec_state *state;
195
40e084e1 196 /* A list of symtabs to which to restrict matches. */
ec94af83 197 VEC (symtab_ptr) *file_symtabs;
40e084e1 198
f8eba3c6 199 /* The result being accumulated. */
40e084e1
KS
200 struct
201 {
202 VEC (symbolp) *symbols;
f60e2d5c 203 VEC (bound_minimal_symbol_d) *minimal_symbols;
40e084e1 204 } result;
14bc53a8
PA
205
206 /* Possibly add a symbol to the results. */
207 bool add_symbol (symbol *sym);
f8eba3c6 208};
50641945 209
14bc53a8
PA
210bool
211collect_info::add_symbol (symbol *sym)
212{
213 /* In list mode, add all matching symbols, regardless of class.
214 This allows the user to type "list a_global_variable". */
215 if (SYMBOL_CLASS (sym) == LOC_BLOCK || this->state->list_mode)
216 VEC_safe_push (symbolp, this->result.symbols, sym);
217
218 /* Continue iterating. */
219 return true;
220}
221
40e084e1 222/* Token types */
50641945 223
40e084e1
KS
224enum ls_token_type
225{
226 /* A keyword */
227 LSTOKEN_KEYWORD = 0,
44fe14ab 228
40e084e1
KS
229 /* A colon "separator" */
230 LSTOKEN_COLON,
44fe14ab 231
40e084e1
KS
232 /* A string */
233 LSTOKEN_STRING,
0960f083 234
40e084e1
KS
235 /* A number */
236 LSTOKEN_NUMBER,
237
238 /* A comma */
239 LSTOKEN_COMMA,
240
241 /* EOI (end of input) */
242 LSTOKEN_EOI,
243
244 /* Consumed token */
245 LSTOKEN_CONSUMED
246};
247typedef enum ls_token_type linespec_token_type;
248
c6756f62
PA
249/* List of keywords. This is NULL-terminated so that it can be used
250 as enum completer. */
251const char * const linespec_keywords[] = { "if", "thread", "task", NULL };
0578b14e 252#define IF_KEYWORD_INDEX 0
40e084e1
KS
253
254/* A token of the linespec lexer */
255
256struct ls_token
257{
258 /* The type of the token */
259 linespec_token_type type;
260
261 /* Data for the token */
262 union
263 {
264 /* A string, given as a stoken */
265 struct stoken string;
266
267 /* A keyword */
268 const char *keyword;
269 } data;
270};
271typedef struct ls_token linespec_token;
272
273#define LS_TOKEN_STOKEN(TOK) (TOK).data.string
274#define LS_TOKEN_KEYWORD(TOK) (TOK).data.keyword
275
276/* An instance of the linespec parser. */
277
278struct ls_parser
279{
280 /* Lexer internal data */
281 struct
282 {
283 /* Save head of input stream. */
d7561cbb 284 const char *saved_arg;
d2630e69 285
40e084e1 286 /* Head of the input stream. */
f00aae0f
KS
287 const char *stream;
288#define PARSER_STREAM(P) ((P)->lexer.stream)
614b3b14 289
40e084e1
KS
290 /* The current token. */
291 linespec_token current;
292 } lexer;
93d91629 293
40e084e1
KS
294 /* Is the entire linespec quote-enclosed? */
295 int is_quote_enclosed;
296
297 /* The state of the parse. */
298 struct linespec_state state;
299#define PARSER_STATE(PPTR) (&(PPTR)->state)
4224873a 300
40e084e1
KS
301 /* The result of the parse. */
302 struct linespec result;
303#define PARSER_RESULT(PPTR) (&(PPTR)->result)
c45ec17c
PA
304
305 /* What the parser believes the current word point should complete
306 to. */
307 linespec_complete_what complete_what;
308
309 /* The completion word point. The parser advances this as it skips
310 tokens. At some point the input string will end or parsing will
311 fail, and then we attempt completion at the captured completion
312 word point, interpreting the string at completion_word as
313 COMPLETE_WHAT. */
314 const char *completion_word;
315
316 /* If the current token was a quoted string, then this is the
317 quoting character (either " or '). */
318 int completion_quote_char;
319
320 /* If the current token was a quoted string, then this points at the
321 end of the quoted string. */
322 const char *completion_quote_end;
323
324 /* If parsing for completion, then this points at the completion
325 tracker. Otherwise, this is NULL. */
326 struct completion_tracker *completion_tracker;
40e084e1
KS
327};
328typedef struct ls_parser linespec_parser;
50641945 329
00e52e53
KS
330/* A convenience macro for accessing the explicit location result of
331 the parser. */
67994074 332#define PARSER_EXPLICIT(PPTR) (&PARSER_RESULT ((PPTR))->explicit_loc)
00e52e53 333
40e084e1 334/* Prototypes for local functions. */
50641945 335
14bc53a8 336static void iterate_over_file_blocks
b5ec771e
PA
337 (struct symtab *symtab, const lookup_name_info &name,
338 domain_enum domain,
14bc53a8 339 gdb::function_view<symbol_found_callback_ftype> callback);
4eeaa230 340
40e084e1
KS
341static void initialize_defaults (struct symtab **default_symtab,
342 int *default_line);
50641945 343
a06efdd6 344CORE_ADDR linespec_expression_to_pc (const char **exp_ptr);
aee8d8ba 345
6c5b2ebe
PA
346static std::vector<symtab_and_line> decode_objc (struct linespec_state *self,
347 linespec_p ls,
348 const char *arg);
aee8d8ba 349
c2f4122d
PA
350static VEC (symtab_ptr) *symtabs_from_filename (const char *,
351 struct program_space *pspace);
50641945 352
40e084e1
KS
353static VEC (symbolp) *find_label_symbols (struct linespec_state *self,
354 VEC (symbolp) *function_symbols,
355 VEC (symbolp) **label_funcs_ret,
a2459270
PA
356 const char *name,
357 bool completion_mode = false);
50641945 358
b1ae631a 359static void find_linespec_symbols (struct linespec_state *self,
ec94af83 360 VEC (symtab_ptr) *file_symtabs,
b1ae631a
DE
361 const char *name,
362 VEC (symbolp) **symbols,
f60e2d5c 363 VEC (bound_minimal_symbol_d) **minsyms);
f8eba3c6 364
40e084e1
KS
365static struct line_offset
366 linespec_parse_variable (struct linespec_state *self,
367 const char *variable);
889f28e2 368
f8eba3c6
TT
369static int symbol_to_sal (struct symtab_and_line *result,
370 int funfirstline, struct symbol *sym);
50641945 371
f8eba3c6 372static void add_matching_symbols_to_info (const char *name,
b5ec771e 373 symbol_name_match_type name_match_type,
f8eba3c6
TT
374 struct collect_info *info,
375 struct program_space *pspace);
f3c39e76 376
f8eba3c6
TT
377static void add_all_symbol_names_from_pspace (struct collect_info *info,
378 struct program_space *pspace,
379 VEC (const_char_ptr) *names);
9ef07c8c 380
c2f4122d
PA
381static VEC (symtab_ptr) *
382 collect_symtabs_from_filename (const char *file,
383 struct program_space *pspace);
84fba31b 384
6c5b2ebe
PA
385static std::vector<symtab_and_line> decode_digits_ordinary
386 (struct linespec_state *self,
387 linespec_p ls,
388 int line,
389 linetable_entry **best_entry);
14e91ac5 390
6c5b2ebe
PA
391static std::vector<symtab_and_line> decode_digits_list_mode
392 (struct linespec_state *self,
393 linespec_p ls,
394 struct symtab_and_line val);
0f5238ed 395
40e084e1
KS
396static void minsym_found (struct linespec_state *self, struct objfile *objfile,
397 struct minimal_symbol *msymbol,
6c5b2ebe 398 std::vector<symtab_and_line> *result);
bca02a8a 399
40e084e1 400static int compare_symbols (const void *a, const void *b);
413dad4d 401
40e084e1 402static int compare_msymbols (const void *a, const void *b);
413dad4d 403
40e084e1
KS
404/* Permitted quote characters for the parser. This is different from the
405 completer's quote characters to allow backward compatibility with the
406 previous parser. */
407static const char *const linespec_quote_characters = "\"\'";
f8eba3c6 408
40e084e1
KS
409/* Lexer functions. */
410
411/* Lex a number from the input in PARSER. This only supports
dd3818c8
KS
412 decimal numbers.
413
d7cbec71 414 Return true if input is decimal numbers. Return false if not. */
40e084e1 415
d7cbec71
HZ
416static int
417linespec_lexer_lex_number (linespec_parser *parser, linespec_token *tokenp)
40e084e1 418{
d7cbec71
HZ
419 tokenp->type = LSTOKEN_NUMBER;
420 LS_TOKEN_STOKEN (*tokenp).length = 0;
421 LS_TOKEN_STOKEN (*tokenp).ptr = PARSER_STREAM (parser);
40e084e1
KS
422
423 /* Keep any sign at the start of the stream. */
424 if (*PARSER_STREAM (parser) == '+' || *PARSER_STREAM (parser) == '-')
425 {
d7cbec71 426 ++LS_TOKEN_STOKEN (*tokenp).length;
40e084e1
KS
427 ++(PARSER_STREAM (parser));
428 }
429
430 while (isdigit (*PARSER_STREAM (parser)))
431 {
d7cbec71 432 ++LS_TOKEN_STOKEN (*tokenp).length;
40e084e1 433 ++(PARSER_STREAM (parser));
f8eba3c6 434 }
40e084e1 435
dd3818c8 436 /* If the next character in the input buffer is not a space, comma,
eff9c3e6 437 quote, or colon, this input does not represent a number. */
dd3818c8
KS
438 if (*PARSER_STREAM (parser) != '\0'
439 && !isspace (*PARSER_STREAM (parser)) && *PARSER_STREAM (parser) != ','
eff9c3e6
KS
440 && *PARSER_STREAM (parser) != ':'
441 && !strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
d7cbec71
HZ
442 {
443 PARSER_STREAM (parser) = LS_TOKEN_STOKEN (*tokenp).ptr;
444 return 0;
445 }
446
447 return 1;
f8eba3c6
TT
448}
449
32b40af9 450/* See linespec.h. */
f8eba3c6 451
0578b14e 452const char *
40e084e1 453linespec_lexer_lex_keyword (const char *p)
f8eba3c6 454{
40e084e1 455 int i;
f8eba3c6 456
40e084e1
KS
457 if (p != NULL)
458 {
c6756f62 459 for (i = 0; linespec_keywords[i] != NULL; ++i)
40e084e1
KS
460 {
461 int len = strlen (linespec_keywords[i]);
462
463 /* If P begins with one of the keywords and the next
0578b14e
KS
464 character is whitespace, we may have found a keyword.
465 It is only a keyword if it is not followed by another
466 keyword. */
40e084e1 467 if (strncmp (p, linespec_keywords[i], len) == 0
0578b14e
KS
468 && isspace (p[len]))
469 {
470 int j;
471
472 /* Special case: "if" ALWAYS stops the lexer, since it
473 is not possible to predict what is going to appear in
474 the condition, which can only be parsed after SaLs have
475 been found. */
476 if (i != IF_KEYWORD_INDEX)
477 {
478 p += len;
f1735a53 479 p = skip_spaces (p);
c6756f62 480 for (j = 0; linespec_keywords[j] != NULL; ++j)
0578b14e
KS
481 {
482 int nextlen = strlen (linespec_keywords[j]);
483
484 if (strncmp (p, linespec_keywords[j], nextlen) == 0
485 && isspace (p[nextlen]))
486 return NULL;
487 }
488 }
489
490 return linespec_keywords[i];
491 }
40e084e1
KS
492 }
493 }
494
495 return NULL;
f8eba3c6
TT
496}
497
87f0e720 498/* See description in linespec.h. */
f8eba3c6 499
87f0e720 500int
40e084e1 501is_ada_operator (const char *string)
f8eba3c6 502{
40e084e1 503 const struct ada_opname_map *mapping;
f8eba3c6 504
40e084e1
KS
505 for (mapping = ada_opname_table;
506 mapping->encoded != NULL
61012eef 507 && !startswith (string, mapping->decoded); ++mapping)
40e084e1
KS
508 ;
509
510 return mapping->decoded == NULL ? 0 : strlen (mapping->decoded);
f8eba3c6
TT
511}
512
40e084e1
KS
513/* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
514 the location of QUOTE_CHAR, or NULL if not found. */
f8eba3c6 515
40e084e1
KS
516static const char *
517skip_quote_char (const char *string, char quote_char)
f8eba3c6 518{
40e084e1 519 const char *p, *last;
f8eba3c6 520
40e084e1
KS
521 p = last = find_toplevel_char (string, quote_char);
522 while (p && *p != '\0' && *p != ':')
523 {
524 p = find_toplevel_char (p, quote_char);
525 if (p != NULL)
526 last = p++;
527 }
f8eba3c6 528
40e084e1 529 return last;
f8eba3c6 530}
50641945 531
40e084e1
KS
532/* Make a writable copy of the string given in TOKEN, trimming
533 any trailing whitespace. */
50641945 534
40e084e1
KS
535static char *
536copy_token_string (linespec_token token)
50641945 537{
40e084e1 538 char *str, *s;
e0881a8e 539
40e084e1
KS
540 if (token.type == LSTOKEN_KEYWORD)
541 return xstrdup (LS_TOKEN_KEYWORD (token));
255e7dbf 542
40e084e1
KS
543 str = savestring (LS_TOKEN_STOKEN (token).ptr,
544 LS_TOKEN_STOKEN (token).length);
545 s = remove_trailing_whitespace (str, str + LS_TOKEN_STOKEN (token).length);
546 *s = '\0';
e0881a8e 547
40e084e1
KS
548 return str;
549}
255e7dbf 550
40e084e1 551/* Does P represent the end of a quote-enclosed linespec? */
f3a5f1de 552
40e084e1
KS
553static int
554is_closing_quote_enclosed (const char *p)
555{
556 if (strchr (linespec_quote_characters, *p))
557 ++p;
558 p = skip_spaces ((char *) p);
559 return (*p == '\0' || linespec_lexer_lex_keyword (p));
50641945
FN
560}
561
40e084e1
KS
562/* Find the end of the parameter list that starts with *INPUT.
563 This helper function assists with lexing string segments
564 which might contain valid (non-terminating) commas. */
481860b3 565
d7561cbb
KS
566static const char *
567find_parameter_list_end (const char *input)
481860b3 568{
40e084e1
KS
569 char end_char, start_char;
570 int depth;
d7561cbb 571 const char *p;
481860b3 572
40e084e1
KS
573 start_char = *input;
574 if (start_char == '(')
575 end_char = ')';
576 else if (start_char == '<')
577 end_char = '>';
578 else
579 return NULL;
481860b3 580
40e084e1
KS
581 p = input;
582 depth = 0;
583 while (*p)
481860b3 584 {
40e084e1
KS
585 if (*p == start_char)
586 ++depth;
587 else if (*p == end_char)
588 {
589 if (--depth == 0)
590 {
591 ++p;
592 break;
593 }
594 }
595 ++p;
481860b3 596 }
40e084e1
KS
597
598 return p;
481860b3
GB
599}
600
c45ec17c
PA
601/* If the [STRING, STRING_LEN) string ends with what looks like a
602 keyword, return the keyword start offset in STRING. Return -1
603 otherwise. */
604
605static size_t
606string_find_incomplete_keyword_at_end (const char * const *keywords,
607 const char *string, size_t string_len)
608{
609 const char *end = string + string_len;
610 const char *p = end;
611
612 while (p > string && *p != ' ')
613 --p;
614 if (p > string)
615 {
616 p++;
617 size_t len = end - p;
618 for (size_t i = 0; keywords[i] != NULL; ++i)
619 if (strncmp (keywords[i], p, len) == 0)
620 return p - string;
621 }
622
623 return -1;
624}
74ccd7f5 625
40e084e1
KS
626/* Lex a string from the input in PARSER. */
627
628static linespec_token
629linespec_lexer_lex_string (linespec_parser *parser)
74ccd7f5 630{
40e084e1 631 linespec_token token;
d7561cbb 632 const char *start = PARSER_STREAM (parser);
74ccd7f5 633
40e084e1 634 token.type = LSTOKEN_STRING;
74ccd7f5 635
40e084e1
KS
636 /* If the input stream starts with a quote character, skip to the next
637 quote character, regardless of the content. */
638 if (strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
639 {
640 const char *end;
641 char quote_char = *PARSER_STREAM (parser);
50641945 642
40e084e1
KS
643 /* Special case: Ada operators. */
644 if (PARSER_STATE (parser)->language->la_language == language_ada
645 && quote_char == '\"')
646 {
647 int len = is_ada_operator (PARSER_STREAM (parser));
50641945 648
40e084e1
KS
649 if (len != 0)
650 {
651 /* The input is an Ada operator. Return the quoted string
652 as-is. */
653 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
654 LS_TOKEN_STOKEN (token).length = len;
655 PARSER_STREAM (parser) += len;
656 return token;
657 }
f8eba3c6 658
40e084e1
KS
659 /* The input does not represent an Ada operator -- fall through
660 to normal quoted string handling. */
661 }
f8eba3c6 662
40e084e1
KS
663 /* Skip past the beginning quote. */
664 ++(PARSER_STREAM (parser));
74ccd7f5 665
40e084e1
KS
666 /* Mark the start of the string. */
667 LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
f8eba3c6 668
40e084e1
KS
669 /* Skip to the ending quote. */
670 end = skip_quote_char (PARSER_STREAM (parser), quote_char);
671
c45ec17c
PA
672 /* This helps the completer mode decide whether we have a
673 complete string. */
674 parser->completion_quote_char = quote_char;
675 parser->completion_quote_end = end;
40e084e1 676
c45ec17c
PA
677 /* Error if the input did not terminate properly, unless in
678 completion mode. */
679 if (end == NULL)
680 {
681 if (parser->completion_tracker == NULL)
682 error (_("unmatched quote"));
683
684 /* In completion mode, we'll try to complete the incomplete
685 token. */
686 token.type = LSTOKEN_STRING;
687 while (*PARSER_STREAM (parser) != '\0')
688 PARSER_STREAM (parser)++;
689 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 1 - start;
690 }
691 else
692 {
693 /* Skip over the ending quote and mark the length of the string. */
694 PARSER_STREAM (parser) = (char *) ++end;
695 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 2 - start;
696 }
40e084e1
KS
697 }
698 else
699 {
d7561cbb 700 const char *p;
40e084e1
KS
701
702 /* Otherwise, only identifier characters are permitted.
703 Spaces are the exception. In general, we keep spaces,
704 but only if the next characters in the input do not resolve
705 to one of the keywords.
706
707 This allows users to forgo quoting CV-qualifiers, template arguments,
708 and similar common language constructs. */
709
710 while (1)
711 {
712 if (isspace (*PARSER_STREAM (parser)))
713 {
f1735a53 714 p = skip_spaces (PARSER_STREAM (parser));
7c09e5a0
DE
715 /* When we get here we know we've found something followed by
716 a space (we skip over parens and templates below).
717 So if we find a keyword now, we know it is a keyword and not,
718 say, a function name. */
40e084e1
KS
719 if (linespec_lexer_lex_keyword (p) != NULL)
720 {
721 LS_TOKEN_STOKEN (token).ptr = start;
722 LS_TOKEN_STOKEN (token).length
723 = PARSER_STREAM (parser) - start;
724 return token;
725 }
726
727 /* Advance past the whitespace. */
728 PARSER_STREAM (parser) = p;
729 }
730
731 /* If the next character is EOI or (single) ':', the
732 string is complete; return the token. */
733 if (*PARSER_STREAM (parser) == 0)
734 {
735 LS_TOKEN_STOKEN (token).ptr = start;
736 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
737 return token;
738 }
739 else if (PARSER_STREAM (parser)[0] == ':')
740 {
741 /* Do not tokenize the C++ scope operator. */
742 if (PARSER_STREAM (parser)[1] == ':')
743 ++(PARSER_STREAM (parser));
744
745 /* Do not tokenify if the input length so far is one
746 (i.e, a single-letter drive name) and the next character
747 is a directory separator. This allows Windows-style
748 paths to be recognized as filenames without quoting it. */
749 else if ((PARSER_STREAM (parser) - start) != 1
750 || !IS_DIR_SEPARATOR (PARSER_STREAM (parser)[1]))
751 {
752 LS_TOKEN_STOKEN (token).ptr = start;
753 LS_TOKEN_STOKEN (token).length
754 = PARSER_STREAM (parser) - start;
755 return token;
756 }
757 }
758 /* Special case: permit quote-enclosed linespecs. */
759 else if (parser->is_quote_enclosed
760 && strchr (linespec_quote_characters,
761 *PARSER_STREAM (parser))
762 && is_closing_quote_enclosed (PARSER_STREAM (parser)))
763 {
764 LS_TOKEN_STOKEN (token).ptr = start;
765 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
766 return token;
767 }
768 /* Because commas may terminate a linespec and appear in
769 the middle of valid string input, special cases for
770 '<' and '(' are necessary. */
771 else if (*PARSER_STREAM (parser) == '<'
772 || *PARSER_STREAM (parser) == '(')
773 {
be966d42
PA
774 /* Don't interpret 'operator<' / 'operator<<' as a
775 template parameter list though. */
776 if (*PARSER_STREAM (parser) == '<'
777 && (PARSER_STATE (parser)->language->la_language
778 == language_cplus)
779 && (PARSER_STREAM (parser) - start) >= CP_OPERATOR_LEN)
780 {
781 const char *p = PARSER_STREAM (parser);
782
783 while (p > start && isspace (p[-1]))
784 p--;
785 if (p - start >= CP_OPERATOR_LEN)
786 {
787 p -= CP_OPERATOR_LEN;
788 if (strncmp (p, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0
789 && (p == start
790 || !(isalnum (p[-1]) || p[-1] == '_')))
791 {
792 /* This is an operator name. Keep going. */
793 ++(PARSER_STREAM (parser));
794 if (*PARSER_STREAM (parser) == '<')
795 ++(PARSER_STREAM (parser));
796 continue;
797 }
798 }
799 }
800
801 const char *p = find_parameter_list_end (PARSER_STREAM (parser));
802 PARSER_STREAM (parser) = p;
40e084e1 803
be966d42
PA
804 /* Don't loop around to the normal \0 case above because
805 we don't want to misinterpret a potential keyword at
806 the end of the token when the string isn't
807 "()<>"-balanced. This handles "b
808 function(thread<tab>" in completion mode. */
809 if (*p == '\0')
40e084e1 810 {
be966d42
PA
811 LS_TOKEN_STOKEN (token).ptr = start;
812 LS_TOKEN_STOKEN (token).length
813 = PARSER_STREAM (parser) - start;
814 return token;
40e084e1 815 }
be966d42
PA
816 else
817 continue;
40e084e1
KS
818 }
819 /* Commas are terminators, but not if they are part of an
820 operator name. */
821 else if (*PARSER_STREAM (parser) == ',')
822 {
823 if ((PARSER_STATE (parser)->language->la_language
824 == language_cplus)
8090b426 825 && (PARSER_STREAM (parser) - start) > CP_OPERATOR_LEN)
40e084e1 826 {
8090b426 827 const char *p = strstr (start, CP_OPERATOR_STR);
40e084e1
KS
828
829 if (p != NULL && is_operator_name (p))
830 {
831 /* This is an operator name. Keep going. */
832 ++(PARSER_STREAM (parser));
833 continue;
834 }
835 }
836
837 /* Comma terminates the string. */
838 LS_TOKEN_STOKEN (token).ptr = start;
839 LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
840 return token;
841 }
842
843 /* Advance the stream. */
844 ++(PARSER_STREAM (parser));
845 }
846 }
847
848 return token;
849}
850
851/* Lex a single linespec token from PARSER. */
852
853static linespec_token
854linespec_lexer_lex_one (linespec_parser *parser)
855{
856 const char *keyword;
857
858 if (parser->lexer.current.type == LSTOKEN_CONSUMED)
859 {
860 /* Skip any whitespace. */
f1735a53 861 PARSER_STREAM (parser) = skip_spaces (PARSER_STREAM (parser));
40e084e1 862
7c09e5a0 863 /* Check for a keyword, they end the linespec. */
0578b14e 864 keyword = linespec_lexer_lex_keyword (PARSER_STREAM (parser));
40e084e1
KS
865 if (keyword != NULL)
866 {
867 parser->lexer.current.type = LSTOKEN_KEYWORD;
868 LS_TOKEN_KEYWORD (parser->lexer.current) = keyword;
0578b14e
KS
869 /* We do not advance the stream here intentionally:
870 we would like lexing to stop when a keyword is seen.
871
872 PARSER_STREAM (parser) += strlen (keyword); */
873
40e084e1
KS
874 return parser->lexer.current;
875 }
876
877 /* Handle other tokens. */
878 switch (*PARSER_STREAM (parser))
879 {
880 case 0:
881 parser->lexer.current.type = LSTOKEN_EOI;
882 break;
883
884 case '+': case '-':
885 case '0': case '1': case '2': case '3': case '4':
886 case '5': case '6': case '7': case '8': case '9':
d7cbec71
HZ
887 if (!linespec_lexer_lex_number (parser, &(parser->lexer.current)))
888 parser->lexer.current = linespec_lexer_lex_string (parser);
40e084e1
KS
889 break;
890
891 case ':':
892 /* If we have a scope operator, lex the input as a string.
893 Otherwise, return LSTOKEN_COLON. */
894 if (PARSER_STREAM (parser)[1] == ':')
895 parser->lexer.current = linespec_lexer_lex_string (parser);
896 else
897 {
898 parser->lexer.current.type = LSTOKEN_COLON;
899 ++(PARSER_STREAM (parser));
900 }
901 break;
902
903 case '\'': case '\"':
904 /* Special case: permit quote-enclosed linespecs. */
905 if (parser->is_quote_enclosed
906 && is_closing_quote_enclosed (PARSER_STREAM (parser)))
907 {
908 ++(PARSER_STREAM (parser));
909 parser->lexer.current.type = LSTOKEN_EOI;
910 }
911 else
912 parser->lexer.current = linespec_lexer_lex_string (parser);
913 break;
914
915 case ',':
916 parser->lexer.current.type = LSTOKEN_COMMA;
917 LS_TOKEN_STOKEN (parser->lexer.current).ptr
918 = PARSER_STREAM (parser);
919 LS_TOKEN_STOKEN (parser->lexer.current).length = 1;
920 ++(PARSER_STREAM (parser));
921 break;
922
923 default:
924 /* If the input is not a number, it must be a string.
925 [Keywords were already considered above.] */
926 parser->lexer.current = linespec_lexer_lex_string (parser);
927 break;
928 }
929 }
930
931 return parser->lexer.current;
932}
933
934/* Consume the current token and return the next token in PARSER's
c45ec17c
PA
935 input stream. Also advance the completion word for completion
936 mode. */
40e084e1
KS
937
938static linespec_token
939linespec_lexer_consume_token (linespec_parser *parser)
940{
c45ec17c
PA
941 gdb_assert (parser->lexer.current.type != LSTOKEN_EOI);
942
943 bool advance_word = (parser->lexer.current.type != LSTOKEN_STRING
944 || *PARSER_STREAM (parser) != '\0');
945
946 /* If we're moving past a string to some other token, it must be the
947 quote was terminated. */
948 if (parser->completion_quote_char)
949 {
950 gdb_assert (parser->lexer.current.type == LSTOKEN_STRING);
951
952 /* If the string was the last (non-EOI) token, we're past the
953 quote, but remember that for later. */
954 if (*PARSER_STREAM (parser) != '\0')
955 {
956 parser->completion_quote_char = '\0';
957 parser->completion_quote_end = NULL;;
958 }
959 }
960
40e084e1 961 parser->lexer.current.type = LSTOKEN_CONSUMED;
c45ec17c
PA
962 linespec_lexer_lex_one (parser);
963
964 if (parser->lexer.current.type == LSTOKEN_STRING)
965 {
966 /* Advance the completion word past a potential initial
967 quote-char. */
968 parser->completion_word = LS_TOKEN_STOKEN (parser->lexer.current).ptr;
969 }
970 else if (advance_word)
971 {
972 /* Advance the completion word past any whitespace. */
973 parser->completion_word = PARSER_STREAM (parser);
974 }
975
976 return parser->lexer.current;
40e084e1
KS
977}
978
979/* Return the next token without consuming the current token. */
980
981static linespec_token
982linespec_lexer_peek_token (linespec_parser *parser)
983{
984 linespec_token next;
d7561cbb 985 const char *saved_stream = PARSER_STREAM (parser);
40e084e1 986 linespec_token saved_token = parser->lexer.current;
c45ec17c
PA
987 int saved_completion_quote_char = parser->completion_quote_char;
988 const char *saved_completion_quote_end = parser->completion_quote_end;
989 const char *saved_completion_word = parser->completion_word;
40e084e1
KS
990
991 next = linespec_lexer_consume_token (parser);
992 PARSER_STREAM (parser) = saved_stream;
993 parser->lexer.current = saved_token;
c45ec17c
PA
994 parser->completion_quote_char = saved_completion_quote_char;
995 parser->completion_quote_end = saved_completion_quote_end;
996 parser->completion_word = saved_completion_word;
40e084e1
KS
997 return next;
998}
999
1000/* Helper functions. */
1001
40e084e1
KS
1002/* Add SAL to SALS, and also update SELF->CANONICAL_NAMES to reflect
1003 the new sal, if needed. If not NULL, SYMNAME is the name of the
66f1999b
KS
1004 symbol to use when constructing the new canonical name.
1005
1006 If LITERAL_CANONICAL is non-zero, SYMNAME will be used as the
1007 canonical name for the SAL. */
40e084e1
KS
1008
1009static void
1010add_sal_to_sals (struct linespec_state *self,
6c5b2ebe 1011 std::vector<symtab_and_line> *sals,
40e084e1 1012 struct symtab_and_line *sal,
66f1999b 1013 const char *symname, int literal_canonical)
40e084e1 1014{
6c5b2ebe 1015 sals->push_back (*sal);
40e084e1
KS
1016
1017 if (self->canonical)
1018 {
33f448b1 1019 struct linespec_canonical_name *canonical;
40e084e1 1020
224c3ddb 1021 self->canonical_names = XRESIZEVEC (struct linespec_canonical_name,
6c5b2ebe
PA
1022 self->canonical_names,
1023 sals->size ());
1024 canonical = &self->canonical_names[sals->size () - 1];
4e04028d 1025 if (!literal_canonical && sal->symtab)
40e084e1 1026 {
df140a0b
TS
1027 symtab_to_fullname (sal->symtab);
1028
40e084e1
KS
1029 /* Note that the filter doesn't have to be a valid linespec
1030 input. We only apply the ":LINE" treatment to Ada for
1031 the time being. */
1032 if (symname != NULL && sal->line != 0
1033 && self->language->la_language == language_ada)
33f448b1 1034 canonical->suffix = xstrprintf ("%s:%d", symname, sal->line);
40e084e1 1035 else if (symname != NULL)
33f448b1 1036 canonical->suffix = xstrdup (symname);
40e084e1 1037 else
33f448b1
JK
1038 canonical->suffix = xstrprintf ("%d", sal->line);
1039 canonical->symtab = sal->symtab;
1040 }
1041 else
1042 {
1043 if (symname != NULL)
1044 canonical->suffix = xstrdup (symname);
1045 else
e617b069 1046 canonical->suffix = xstrdup ("<unknown>");
33f448b1 1047 canonical->symtab = NULL;
40e084e1 1048 }
40e084e1
KS
1049 }
1050}
1051
1052/* A hash function for address_entry. */
1053
1054static hashval_t
1055hash_address_entry (const void *p)
1056{
9a3c8263 1057 const struct address_entry *aep = (const struct address_entry *) p;
40e084e1
KS
1058 hashval_t hash;
1059
1060 hash = iterative_hash_object (aep->pspace, 0);
1061 return iterative_hash_object (aep->addr, hash);
1062}
1063
1064/* An equality function for address_entry. */
1065
1066static int
1067eq_address_entry (const void *a, const void *b)
1068{
9a3c8263
SM
1069 const struct address_entry *aea = (const struct address_entry *) a;
1070 const struct address_entry *aeb = (const struct address_entry *) b;
40e084e1
KS
1071
1072 return aea->pspace == aeb->pspace && aea->addr == aeb->addr;
1073}
1074
1075/* Check whether the address, represented by PSPACE and ADDR, is
1076 already in the set. If so, return 0. Otherwise, add it and return
1077 1. */
1078
1079static int
1080maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr)
1081{
1082 struct address_entry e, *p;
1083 void **slot;
1084
1085 e.pspace = pspace;
1086 e.addr = addr;
1087 slot = htab_find_slot (set, &e, INSERT);
1088 if (*slot)
1089 return 0;
1090
1091 p = XNEW (struct address_entry);
1092 memcpy (p, &e, sizeof (struct address_entry));
1093 *slot = p;
1094
1095 return 1;
1096}
1097
40e084e1
KS
1098/* A helper that walks over all matching symtabs in all objfiles and
1099 calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is
1100 not NULL, then the search is restricted to just that program
14bc53a8 1101 space. If INCLUDE_INLINE is true then symbols representing
40e084e1
KS
1102 inlined instances of functions will be included in the result. */
1103
1104static void
14bc53a8 1105iterate_over_all_matching_symtabs
b5ec771e
PA
1106 (struct linespec_state *state,
1107 const lookup_name_info &lookup_name,
1108 const domain_enum name_domain,
14bc53a8
PA
1109 struct program_space *search_pspace, bool include_inline,
1110 gdb::function_view<symbol_found_callback_ftype> callback)
40e084e1
KS
1111{
1112 struct objfile *objfile;
1113 struct program_space *pspace;
40e084e1 1114
40e084e1
KS
1115 ALL_PSPACES (pspace)
1116 {
1117 if (search_pspace != NULL && search_pspace != pspace)
1118 continue;
1119 if (pspace->executing_startup)
f8eba3c6
TT
1120 continue;
1121
1122 set_current_program_space (pspace);
50641945 1123
f8eba3c6
TT
1124 ALL_OBJFILES (objfile)
1125 {
43f3e411 1126 struct compunit_symtab *cu;
f8eba3c6
TT
1127
1128 if (objfile->sf)
b5ec771e
PA
1129 objfile->sf->qf->expand_symtabs_matching (objfile,
1130 NULL,
1131 lookup_name,
1132 NULL, NULL,
1133 ALL_DOMAIN);
f8eba3c6 1134
43f3e411 1135 ALL_OBJFILE_COMPUNITS (objfile, cu)
f8eba3c6 1136 {
43f3e411
DE
1137 struct symtab *symtab = COMPUNIT_FILETABS (cu);
1138
b5ec771e 1139 iterate_over_file_blocks (symtab, lookup_name, name_domain, callback);
481860b3 1140
d790cf0a
DE
1141 if (include_inline)
1142 {
4eeaa230 1143 struct block *block;
d790cf0a 1144 int i;
481860b3 1145
d790cf0a 1146 for (i = FIRST_LOCAL_BLOCK;
439247b6
DE
1147 i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab));
1148 i++)
d790cf0a 1149 {
439247b6 1150 block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i);
4ae24af0 1151 state->language->la_iterate_over_symbols
b5ec771e 1152 (block, lookup_name, name_domain, [&] (symbol *sym)
14bc53a8
PA
1153 {
1154 /* Restrict calls to CALLBACK to symbols
1155 representing inline symbols only. */
1156 if (SYMBOL_INLINED (sym))
1157 return callback (sym);
1158 return true;
1159 });
481860b3 1160 }
f8eba3c6
TT
1161 }
1162 }
1163 }
1164 }
50641945
FN
1165}
1166
4eeaa230
DE
1167/* Returns the block to be used for symbol searches from
1168 the current location. */
e8eb7bc5 1169
3977b71f 1170static const struct block *
e482a1a7 1171get_current_search_block (void)
e8eb7bc5 1172{
3977b71f 1173 const struct block *block;
4eeaa230 1174 enum language save_language;
e8eb7bc5 1175
4eeaa230
DE
1176 /* get_selected_block can change the current language when there is
1177 no selected frame yet. */
1178 save_language = current_language->la_language;
1179 block = get_selected_block (0);
1180 set_language (save_language);
e8eb7bc5
KS
1181
1182 return block;
1183}
1184
4eeaa230
DE
1185/* Iterate over static and global blocks. */
1186
1187static void
14bc53a8 1188iterate_over_file_blocks
b5ec771e
PA
1189 (struct symtab *symtab, const lookup_name_info &name,
1190 domain_enum domain, gdb::function_view<symbol_found_callback_ftype> callback)
4eeaa230
DE
1191{
1192 struct block *block;
1193
439247b6 1194 for (block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK);
4eeaa230
DE
1195 block != NULL;
1196 block = BLOCK_SUPERBLOCK (block))
14bc53a8 1197 LA_ITERATE_OVER_SYMBOLS (block, name, domain, callback);
4eeaa230
DE
1198}
1199
b5ec771e
PA
1200/* A helper for find_method. This finds all methods in type T of
1201 language T_LANG which match NAME. It adds matching symbol names to
1202 RESULT_NAMES, and adds T's direct superclasses to SUPERCLASSES. */
50641945 1203
f8eba3c6 1204static void
b5ec771e 1205find_methods (struct type *t, enum language t_lang, const char *name,
f8eba3c6
TT
1206 VEC (const_char_ptr) **result_names,
1207 VEC (typep) **superclasses)
50641945 1208{
50641945 1209 int ibase;
0d5cff50 1210 const char *class_name = type_name_no_tag (t);
c00f8484 1211
50641945
FN
1212 /* Ignore this class if it doesn't have a name. This is ugly, but
1213 unless we figure out how to get the physname without the name of
1214 the class, then the loop can't do any good. */
f8eba3c6 1215 if (class_name)
50641945
FN
1216 {
1217 int method_counter;
b5ec771e
PA
1218 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
1219 symbol_name_matcher_ftype *symbol_name_compare
1220 = language_get_symbol_name_matcher (language_def (t_lang), lookup_name);
50641945 1221
f168693b 1222 t = check_typedef (t);
50641945
FN
1223
1224 /* Loop over each method name. At this level, all overloads of a name
1225 are counted as a single name. There is an inner loop which loops over
1226 each overload. */
1227
1228 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1229 method_counter >= 0;
1230 --method_counter)
1231 {
0d5cff50 1232 const char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
50641945
FN
1233 char dem_opname[64];
1234
61012eef
GB
1235 if (startswith (method_name, "__") ||
1236 startswith (method_name, "op") ||
1237 startswith (method_name, "type"))
50641945
FN
1238 {
1239 if (cplus_demangle_opname (method_name, dem_opname, DMGL_ANSI))
1240 method_name = dem_opname;
1241 else if (cplus_demangle_opname (method_name, dem_opname, 0))
1242 method_name = dem_opname;
1243 }
1244
b5ec771e 1245 if (symbol_name_compare (method_name, lookup_name, NULL))
f8eba3c6
TT
1246 {
1247 int field_counter;
aee8d8ba 1248
f8eba3c6
TT
1249 for (field_counter = (TYPE_FN_FIELDLIST_LENGTH (t, method_counter)
1250 - 1);
1251 field_counter >= 0;
1252 --field_counter)
1253 {
1254 struct fn_field *f;
1255 const char *phys_name;
1256
1257 f = TYPE_FN_FIELDLIST1 (t, method_counter);
1258 if (TYPE_FN_FIELD_STUB (f, field_counter))
1259 continue;
1260 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1261 VEC_safe_push (const_char_ptr, *result_names, phys_name);
1262 }
1263 }
aee8d8ba
DC
1264 }
1265 }
1266
f8eba3c6
TT
1267 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
1268 VEC_safe_push (typep, *superclasses, TYPE_BASECLASS (t, ibase));
50641945
FN
1269}
1270
50641945
FN
1271/* Find an instance of the character C in the string S that is outside
1272 of all parenthesis pairs, single-quoted strings, and double-quoted
8120c9d5 1273 strings. Also, ignore the char within a template name, like a ','
be966d42 1274 within foo<int, int>, while considering C++ operator</operator<<. */
8120c9d5 1275
87f0e720 1276const char *
40e084e1 1277find_toplevel_char (const char *s, char c)
50641945
FN
1278{
1279 int quoted = 0; /* zero if we're not in quotes;
1280 '"' if we're in a double-quoted string;
1281 '\'' if we're in a single-quoted string. */
a04257e6 1282 int depth = 0; /* Number of unclosed parens we've seen. */
40e084e1 1283 const char *scan;
50641945
FN
1284
1285 for (scan = s; *scan; scan++)
1286 {
1287 if (quoted)
1288 {
1289 if (*scan == quoted)
1290 quoted = 0;
1291 else if (*scan == '\\' && *(scan + 1))
1292 scan++;
1293 }
1294 else if (*scan == c && ! quoted && depth == 0)
1295 return scan;
1296 else if (*scan == '"' || *scan == '\'')
1297 quoted = *scan;
8120c9d5 1298 else if (*scan == '(' || *scan == '<')
50641945 1299 depth++;
8120c9d5 1300 else if ((*scan == ')' || *scan == '>') && depth > 0)
50641945 1301 depth--;
be966d42
PA
1302 else if (*scan == 'o' && !quoted && depth == 0)
1303 {
1304 /* Handle C++ operator names. */
1305 if (strncmp (scan, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0)
1306 {
1307 scan += CP_OPERATOR_LEN;
1308 if (*scan == c)
1309 return scan;
1310 while (isspace (*scan))
1311 {
1312 ++scan;
1313 if (*scan == c)
1314 return scan;
1315 }
1316 if (*scan == '\0')
1317 break;
1318
1319 switch (*scan)
1320 {
1321 /* Skip over one less than the appropriate number of
1322 characters: the for loop will skip over the last
1323 one. */
1324 case '<':
1325 if (scan[1] == '<')
1326 {
1327 scan++;
1328 if (*scan == c)
1329 return scan;
1330 }
1331 break;
1332 case '>':
1333 if (scan[1] == '>')
1334 {
1335 scan++;
1336 if (*scan == c)
1337 return scan;
1338 }
1339 break;
1340 }
1341 }
1342 }
50641945
FN
1343 }
1344
1345 return 0;
1346}
1347
40e084e1
KS
1348/* The string equivalent of find_toplevel_char. Returns a pointer
1349 to the location of NEEDLE in HAYSTACK, ignoring any occurrences
1350 inside "()" and "<>". Returns NULL if NEEDLE was not found. */
889f28e2 1351
40e084e1
KS
1352static const char *
1353find_toplevel_string (const char *haystack, const char *needle)
889f28e2 1354{
40e084e1
KS
1355 const char *s = haystack;
1356
1357 do
1358 {
1359 s = find_toplevel_char (s, *needle);
1360
1361 if (s != NULL)
1362 {
1363 /* Found first char in HAYSTACK; check rest of string. */
61012eef 1364 if (startswith (s, needle))
40e084e1
KS
1365 return s;
1366
1367 /* Didn't find it; loop over HAYSTACK, looking for the next
1368 instance of the first character of NEEDLE. */
1369 ++s;
1370 }
1371 }
1372 while (s != NULL && *s != '\0');
1373
1374 /* NEEDLE was not found in HAYSTACK. */
1375 return NULL;
889f28e2
AF
1376}
1377
33f448b1
JK
1378/* Convert CANONICAL to its string representation using
1379 symtab_to_fullname for SYMTAB. The caller must xfree the result. */
1380
1381static char *
1382canonical_to_fullform (const struct linespec_canonical_name *canonical)
1383{
1384 if (canonical->symtab == NULL)
1385 return xstrdup (canonical->suffix);
1386 else
1387 return xstrprintf ("%s:%s", symtab_to_fullname (canonical->symtab),
1388 canonical->suffix);
1389}
1390
f8eba3c6
TT
1391/* Given FILTERS, a list of canonical names, filter the sals in RESULT
1392 and store the result in SELF->CANONICAL. */
50641945 1393
f8eba3c6
TT
1394static void
1395filter_results (struct linespec_state *self,
6c5b2ebe 1396 std::vector<symtab_and_line> *result,
f8eba3c6
TT
1397 VEC (const_char_ptr) *filters)
1398{
1399 int i;
1400 const char *name;
1401
1402 for (i = 0; VEC_iterate (const_char_ptr, filters, i, name); ++i)
1403 {
6c5b2ebe 1404 linespec_sals lsal;
f8eba3c6 1405
6c5b2ebe 1406 for (size_t j = 0; j < result->size (); ++j)
f8eba3c6 1407 {
33f448b1
JK
1408 const struct linespec_canonical_name *canonical;
1409 char *fullform;
1410 struct cleanup *cleanup;
1411
1412 canonical = &self->canonical_names[j];
1413 fullform = canonical_to_fullform (canonical);
1414 cleanup = make_cleanup (xfree, fullform);
1415
1416 if (strcmp (name, fullform) == 0)
6c5b2ebe 1417 lsal.sals.push_back ((*result)[j]);
33f448b1
JK
1418
1419 do_cleanups (cleanup);
f8eba3c6
TT
1420 }
1421
6c5b2ebe 1422 if (!lsal.sals.empty ())
f8eba3c6
TT
1423 {
1424 lsal.canonical = xstrdup (name);
6c5b2ebe 1425 self->canonical->lsals.push_back (std::move (lsal));
f8eba3c6
TT
1426 }
1427 }
1428
1429 self->canonical->pre_expanded = 0;
1430}
1431
1432/* Store RESULT into SELF->CANONICAL. */
1433
1434static void
1435convert_results_to_lsals (struct linespec_state *self,
6c5b2ebe 1436 std::vector<symtab_and_line> *result)
50641945 1437{
f8eba3c6
TT
1438 struct linespec_sals lsal;
1439
1440 lsal.canonical = NULL;
6c5b2ebe
PA
1441 lsal.sals = std::move (*result);
1442 self->canonical->lsals.push_back (std::move (lsal));
f8eba3c6
TT
1443}
1444
33f448b1
JK
1445/* A structure that contains two string representations of a struct
1446 linespec_canonical_name:
1447 - one where the the symtab's fullname is used;
1448 - one where the filename followed the "set filename-display"
1449 setting. */
1450
1451struct decode_line_2_item
1452{
1453 /* The form using symtab_to_fullname.
1454 It must be xfree'ed after use. */
1455 char *fullform;
1456
1457 /* The form using symtab_to_filename_for_display.
1458 It must be xfree'ed after use. */
1459 char *displayform;
1460
1461 /* Field is initialized to zero and it is set to one if the user
1462 requested breakpoint for this entry. */
1463 unsigned int selected : 1;
1464};
1465
1466/* Helper for qsort to sort decode_line_2_item entries by DISPLAYFORM and
1467 secondarily by FULLFORM. */
1468
1469static int
1470decode_line_2_compare_items (const void *ap, const void *bp)
1471{
9a3c8263
SM
1472 const struct decode_line_2_item *a = (const struct decode_line_2_item *) ap;
1473 const struct decode_line_2_item *b = (const struct decode_line_2_item *) bp;
33f448b1
JK
1474 int retval;
1475
1476 retval = strcmp (a->displayform, b->displayform);
1477 if (retval != 0)
1478 return retval;
1479
1480 return strcmp (a->fullform, b->fullform);
1481}
1482
f8eba3c6
TT
1483/* Handle multiple results in RESULT depending on SELECT_MODE. This
1484 will either return normally, throw an exception on multiple
1485 results, or present a menu to the user. On return, the SALS vector
1486 in SELF->CANONICAL is set up properly. */
1487
1488static void
1489decode_line_2 (struct linespec_state *self,
6c5b2ebe 1490 std::vector<symtab_and_line> *result,
f8eba3c6
TT
1491 const char *select_mode)
1492{
a121b7c1
PA
1493 char *args;
1494 const char *prompt;
50641945 1495 int i;
50641945 1496 struct cleanup *old_chain;
33f448b1 1497 VEC (const_char_ptr) *filters = NULL;
33f448b1
JK
1498 struct decode_line_2_item *items;
1499 int items_count;
50641945 1500
f8eba3c6
TT
1501 gdb_assert (select_mode != multiple_symbols_all);
1502 gdb_assert (self->canonical != NULL);
6c5b2ebe 1503 gdb_assert (!result->empty ());
33f448b1
JK
1504
1505 old_chain = make_cleanup (VEC_cleanup (const_char_ptr), &filters);
50641945 1506
33f448b1 1507 /* Prepare ITEMS array. */
6c5b2ebe 1508 items_count = result->size ();
8d749320 1509 items = XNEWVEC (struct decode_line_2_item, items_count);
33f448b1
JK
1510 make_cleanup (xfree, items);
1511 for (i = 0; i < items_count; ++i)
50641945 1512 {
33f448b1
JK
1513 const struct linespec_canonical_name *canonical;
1514 struct decode_line_2_item *item;
1515
1516 canonical = &self->canonical_names[i];
1517 gdb_assert (canonical->suffix != NULL);
1518 item = &items[i];
f8eba3c6 1519
33f448b1
JK
1520 item->fullform = canonical_to_fullform (canonical);
1521 make_cleanup (xfree, item->fullform);
1522
1523 if (canonical->symtab == NULL)
1524 item->displayform = canonical->suffix;
1525 else
f8eba3c6 1526 {
33f448b1
JK
1527 const char *fn_for_display;
1528
1529 fn_for_display = symtab_to_filename_for_display (canonical->symtab);
1530 item->displayform = xstrprintf ("%s:%s", fn_for_display,
1531 canonical->suffix);
1532 make_cleanup (xfree, item->displayform);
f8eba3c6
TT
1533 }
1534
33f448b1 1535 item->selected = 0;
50641945
FN
1536 }
1537
33f448b1
JK
1538 /* Sort the list of method names. */
1539 qsort (items, items_count, sizeof (*items), decode_line_2_compare_items);
1540
1541 /* Remove entries with the same FULLFORM. */
1542 if (items_count >= 2)
1543 {
1544 struct decode_line_2_item *dst, *src;
1545
1546 dst = items;
1547 for (src = &items[1]; src < &items[items_count]; src++)
1548 if (strcmp (src->fullform, dst->fullform) != 0)
1549 *++dst = *src;
1550 items_count = dst + 1 - items;
1551 }
1552
1553 if (select_mode == multiple_symbols_cancel && items_count > 1)
f8eba3c6
TT
1554 error (_("canceled because the command is ambiguous\n"
1555 "See set/show multiple-symbol."));
1556
33f448b1 1557 if (select_mode == multiple_symbols_all || items_count == 1)
50641945 1558 {
f8eba3c6
TT
1559 do_cleanups (old_chain);
1560 convert_results_to_lsals (self, result);
1561 return;
50641945
FN
1562 }
1563
f8eba3c6 1564 printf_unfiltered (_("[0] cancel\n[1] all\n"));
33f448b1
JK
1565 for (i = 0; i < items_count; i++)
1566 printf_unfiltered ("[%d] %s\n", i + 2, items[i].displayform);
f8eba3c6
TT
1567
1568 prompt = getenv ("PS2");
1569 if (prompt == NULL)
50641945 1570 {
f8eba3c6 1571 prompt = "> ";
50641945 1572 }
f8eba3c6 1573 args = command_line_input (prompt, 0, "overload-choice");
50641945
FN
1574
1575 if (args == 0 || *args == 0)
e2e0b3e5 1576 error_no_arg (_("one or more choice numbers"));
50641945 1577
bfd28288
PA
1578 number_or_range_parser parser (args);
1579 while (!parser.finished ())
50641945 1580 {
bfd28288 1581 int num = parser.get_number ();
50641945
FN
1582
1583 if (num == 0)
8a3fe4f8 1584 error (_("canceled"));
50641945
FN
1585 else if (num == 1)
1586 {
f8eba3c6
TT
1587 /* We intentionally make this result in a single breakpoint,
1588 contrary to what older versions of gdb did. The
1589 rationale is that this lets a user get the
1590 multiple_symbols_all behavior even with the 'ask'
1591 setting; and he can get separate breakpoints by entering
1592 "2-57" at the query. */
1593 do_cleanups (old_chain);
1594 convert_results_to_lsals (self, result);
1595 return;
50641945
FN
1596 }
1597
f8eba3c6 1598 num -= 2;
33f448b1 1599 if (num >= items_count)
f8eba3c6 1600 printf_unfiltered (_("No choice number %d.\n"), num);
50641945
FN
1601 else
1602 {
33f448b1 1603 struct decode_line_2_item *item = &items[num];
f8eba3c6 1604
33f448b1 1605 if (!item->selected)
50641945 1606 {
33f448b1
JK
1607 VEC_safe_push (const_char_ptr, filters, item->fullform);
1608 item->selected = 1;
50641945
FN
1609 }
1610 else
1611 {
3e43a32a 1612 printf_unfiltered (_("duplicate request for %d ignored.\n"),
f6f99966 1613 num + 2);
50641945
FN
1614 }
1615 }
50641945 1616 }
f8eba3c6
TT
1617
1618 filter_results (self, result, filters);
1619 do_cleanups (old_chain);
50641945 1620}
94af9270 1621
40e084e1 1622\f
3d50dd94 1623
40e084e1
KS
1624/* The parser of linespec itself. */
1625
1626/* Throw an appropriate error when SYMBOL is not found (optionally in
1627 FILENAME). */
1628
1629static void ATTRIBUTE_NORETURN
5d94e27b 1630symbol_not_found_error (const char *symbol, const char *filename)
3d50dd94 1631{
40e084e1
KS
1632 if (symbol == NULL)
1633 symbol = "";
1634
1635 if (!have_full_symbols ()
1636 && !have_partial_symbols ()
1637 && !have_minimal_symbols ())
1638 throw_error (NOT_FOUND_ERROR,
1639 _("No symbol table is loaded. Use the \"file\" command."));
1640
1641 /* If SYMBOL starts with '$', the user attempted to either lookup
1642 a function/variable in his code starting with '$' or an internal
1643 variable of that name. Since we do not know which, be concise and
1644 explain both possibilities. */
1645 if (*symbol == '$')
1646 {
1647 if (filename)
1648 throw_error (NOT_FOUND_ERROR,
1649 _("Undefined convenience variable or function \"%s\" "
1650 "not defined in \"%s\"."), symbol, filename);
1651 else
1652 throw_error (NOT_FOUND_ERROR,
1653 _("Undefined convenience variable or function \"%s\" "
1654 "not defined."), symbol);
1655 }
1656 else
1657 {
1658 if (filename)
1659 throw_error (NOT_FOUND_ERROR,
1660 _("Function \"%s\" not defined in \"%s\"."),
1661 symbol, filename);
1662 else
1663 throw_error (NOT_FOUND_ERROR,
1664 _("Function \"%s\" not defined."), symbol);
1665 }
3d50dd94
JK
1666}
1667
40e084e1
KS
1668/* Throw an appropriate error when an unexpected token is encountered
1669 in the input. */
94af9270 1670
40e084e1
KS
1671static void ATTRIBUTE_NORETURN
1672unexpected_linespec_error (linespec_parser *parser)
94af9270 1673{
40e084e1
KS
1674 linespec_token token;
1675 static const char * token_type_strings[]
1676 = {"keyword", "colon", "string", "number", "comma", "end of input"};
94af9270 1677
40e084e1
KS
1678 /* Get the token that generated the error. */
1679 token = linespec_lexer_lex_one (parser);
94af9270 1680
40e084e1
KS
1681 /* Finally, throw the error. */
1682 if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
1683 || token.type == LSTOKEN_KEYWORD)
94af9270 1684 {
40e084e1 1685 char *string;
40e084e1
KS
1686
1687 string = copy_token_string (token);
78cc6c2d 1688 make_cleanup (xfree, string);
40e084e1
KS
1689 throw_error (GENERIC_ERROR,
1690 _("malformed linespec error: unexpected %s, \"%s\""),
1691 token_type_strings[token.type], string);
1692 }
1693 else
1694 throw_error (GENERIC_ERROR,
1695 _("malformed linespec error: unexpected %s"),
1696 token_type_strings[token.type]);
1697}
1698
00e52e53
KS
1699/* Throw an undefined label error. */
1700
1701static void ATTRIBUTE_NORETURN
1702undefined_label_error (const char *function, const char *label)
1703{
1704 if (function != NULL)
1705 throw_error (NOT_FOUND_ERROR,
1706 _("No label \"%s\" defined in function \"%s\"."),
1707 label, function);
1708 else
1709 throw_error (NOT_FOUND_ERROR,
1710 _("No label \"%s\" defined in current function."),
1711 label);
1712}
1713
1714/* Throw a source file not found error. */
1715
1716static void ATTRIBUTE_NORETURN
1717source_file_not_found_error (const char *name)
1718{
1719 throw_error (NOT_FOUND_ERROR, _("No source file named %s."), name);
1720}
1721
c45ec17c
PA
1722/* Unless at EIO, save the current stream position as completion word
1723 point, and consume the next token. */
1724
1725static linespec_token
1726save_stream_and_consume_token (linespec_parser *parser)
1727{
1728 if (linespec_lexer_peek_token (parser).type != LSTOKEN_EOI)
1729 parser->completion_word = PARSER_STREAM (parser);
1730 return linespec_lexer_consume_token (parser);
1731}
1732
87f0e720 1733/* See description in linespec.h. */
40e084e1 1734
87f0e720 1735struct line_offset
09cf2b22 1736linespec_parse_line_offset (const char *string)
40e084e1 1737{
87f0e720 1738 const char *start = string;
40e084e1
KS
1739 struct line_offset line_offset = {0, LINE_OFFSET_NONE};
1740
1741 if (*string == '+')
1742 {
1743 line_offset.sign = LINE_OFFSET_PLUS;
1744 ++string;
1745 }
1746 else if (*string == '-')
1747 {
1748 line_offset.sign = LINE_OFFSET_MINUS;
1749 ++string;
1750 }
1751
87f0e720
KS
1752 if (*string != '\0' && !isdigit (*string))
1753 error (_("malformed line offset: \"%s\""), start);
1754
40e084e1
KS
1755 /* Right now, we only allow base 10 for offsets. */
1756 line_offset.offset = atoi (string);
1757 return line_offset;
1758}
1759
c45ec17c
PA
1760/* In completion mode, if the user is still typing the number, there's
1761 no possible completion to offer. But if there's already input past
1762 the number, setup to expect NEXT. */
1763
1764static void
1765set_completion_after_number (linespec_parser *parser,
1766 linespec_complete_what next)
1767{
1768 if (*PARSER_STREAM (parser) == ' ')
1769 {
f1735a53 1770 parser->completion_word = skip_spaces (PARSER_STREAM (parser) + 1);
c45ec17c
PA
1771 parser->complete_what = next;
1772 }
1773 else
1774 {
1775 parser->completion_word = PARSER_STREAM (parser);
1776 parser->complete_what = linespec_complete_what::NOTHING;
1777 }
1778}
1779
40e084e1
KS
1780/* Parse the basic_spec in PARSER's input. */
1781
1782static void
1783linespec_parse_basic (linespec_parser *parser)
1784{
1785 char *name;
1786 linespec_token token;
1787 VEC (symbolp) *symbols, *labels;
f60e2d5c 1788 VEC (bound_minimal_symbol_d) *minimal_symbols;
40e084e1
KS
1789 struct cleanup *cleanup;
1790
1791 /* Get the next token. */
1792 token = linespec_lexer_lex_one (parser);
1793
1794 /* If it is EOI or KEYWORD, issue an error. */
c45ec17c
PA
1795 if (token.type == LSTOKEN_KEYWORD)
1796 {
1797 parser->complete_what = linespec_complete_what::NOTHING;
1798 unexpected_linespec_error (parser);
1799 }
1800 else if (token.type == LSTOKEN_EOI)
1801 {
1802 unexpected_linespec_error (parser);
1803 }
40e084e1
KS
1804 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1805 else if (token.type == LSTOKEN_NUMBER)
1806 {
c45ec17c
PA
1807 set_completion_after_number (parser, linespec_complete_what::KEYWORD);
1808
40e084e1
KS
1809 /* Record the line offset and get the next token. */
1810 name = copy_token_string (token);
1811 cleanup = make_cleanup (xfree, name);
00e52e53 1812 PARSER_EXPLICIT (parser)->line_offset = linespec_parse_line_offset (name);
40e084e1
KS
1813 do_cleanups (cleanup);
1814
1815 /* Get the next token. */
1816 token = linespec_lexer_consume_token (parser);
1817
1818 /* If the next token is a comma, stop parsing and return. */
1819 if (token.type == LSTOKEN_COMMA)
c45ec17c
PA
1820 {
1821 parser->complete_what = linespec_complete_what::NOTHING;
1822 return;
1823 }
40e084e1
KS
1824
1825 /* If the next token is anything but EOI or KEYWORD, issue
1826 an error. */
1827 if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
1828 unexpected_linespec_error (parser);
1829 }
1830
1831 if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
1832 return;
1833
1834 /* Next token must be LSTOKEN_STRING. */
1835 if (token.type != LSTOKEN_STRING)
c45ec17c
PA
1836 {
1837 parser->complete_what = linespec_complete_what::NOTHING;
1838 unexpected_linespec_error (parser);
1839 }
40e084e1
KS
1840
1841 /* The current token will contain the name of a function, method,
1842 or label. */
c45ec17c
PA
1843 name = copy_token_string (token);
1844 cleanup = make_cleanup (free_current_contents, &name);
1845
1846 if (parser->completion_tracker != NULL)
1847 {
1848 /* If the function name ends with a ":", then this may be an
1849 incomplete "::" scope operator instead of a label separator.
1850 E.g.,
1851 "b klass:<tab>"
1852 which should expand to:
1853 "b klass::method()"
1854
1855 Do a tentative completion assuming the later. If we find
1856 completions, advance the stream past the colon token and make
1857 it part of the function name/token. */
1858
1859 if (!parser->completion_quote_char
1860 && strcmp (PARSER_STREAM (parser), ":") == 0)
1861 {
1862 completion_tracker tmp_tracker;
1863 const char *source_filename
1864 = PARSER_EXPLICIT (parser)->source_filename;
1865
1866 linespec_complete_function (tmp_tracker,
1867 parser->completion_word,
1868 source_filename);
1869
1870 if (tmp_tracker.have_completions ())
1871 {
1872 PARSER_STREAM (parser)++;
1873 LS_TOKEN_STOKEN (token).length++;
1874
1875 xfree (name);
1876 name = savestring (parser->completion_word,
1877 (PARSER_STREAM (parser)
1878 - parser->completion_word));
1879 }
1880 }
1881
1882 PARSER_EXPLICIT (parser)->function_name = name;
1883 discard_cleanups (cleanup);
1884 }
1885 else
1886 {
1887 /* XXX Reindent before pushing. */
40e084e1
KS
1888
1889 /* Try looking it up as a function/method. */
1890 find_linespec_symbols (PARSER_STATE (parser),
1891 PARSER_RESULT (parser)->file_symtabs, name,
1892 &symbols, &minimal_symbols);
1893
1894 if (symbols != NULL || minimal_symbols != NULL)
1895 {
1896 PARSER_RESULT (parser)->function_symbols = symbols;
1897 PARSER_RESULT (parser)->minimal_symbols = minimal_symbols;
00e52e53 1898 PARSER_EXPLICIT (parser)->function_name = name;
40e084e1
KS
1899 symbols = NULL;
1900 discard_cleanups (cleanup);
1901 }
1902 else
1903 {
1904 /* NAME was not a function or a method. So it must be a label
b4013987 1905 name or user specified variable like "break foo.c:$zippo". */
40e084e1
KS
1906 labels = find_label_symbols (PARSER_STATE (parser), NULL,
1907 &symbols, name);
1908 if (labels != NULL)
94af9270 1909 {
40e084e1
KS
1910 PARSER_RESULT (parser)->labels.label_symbols = labels;
1911 PARSER_RESULT (parser)->labels.function_symbols = symbols;
00e52e53 1912 PARSER_EXPLICIT (parser)->label_name = name;
40e084e1
KS
1913 symbols = NULL;
1914 discard_cleanups (cleanup);
1915 }
b4013987
AA
1916 else if (token.type == LSTOKEN_STRING
1917 && *LS_TOKEN_STOKEN (token).ptr == '$')
1918 {
1919 /* User specified a convenience variable or history value. */
00e52e53 1920 PARSER_EXPLICIT (parser)->line_offset
b4013987
AA
1921 = linespec_parse_variable (PARSER_STATE (parser), name);
1922
00e52e53 1923 if (PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
b4013987
AA
1924 {
1925 /* The user-specified variable was not valid. Do not
1926 throw an error here. parse_linespec will do it for us. */
00e52e53 1927 PARSER_EXPLICIT (parser)->function_name = name;
b4013987
AA
1928 discard_cleanups (cleanup);
1929 return;
1930 }
c888a17d
KS
1931
1932 /* The convenience variable/history value parsed correctly.
1933 NAME is no longer needed. */
1934 do_cleanups (cleanup);
b4013987 1935 }
40e084e1
KS
1936 else
1937 {
1938 /* The name is also not a label. Abort parsing. Do not throw
1939 an error here. parse_linespec will do it for us. */
1940
1941 /* Save a copy of the name we were trying to lookup. */
00e52e53 1942 PARSER_EXPLICIT (parser)->function_name = name;
40e084e1
KS
1943 discard_cleanups (cleanup);
1944 return;
1945 }
1946 }
c45ec17c
PA
1947 }
1948
1949 int previous_qc = parser->completion_quote_char;
40e084e1
KS
1950
1951 /* Get the next token. */
1952 token = linespec_lexer_consume_token (parser);
1953
c45ec17c
PA
1954 if (token.type == LSTOKEN_EOI)
1955 {
1956 if (previous_qc && !parser->completion_quote_char)
1957 parser->complete_what = linespec_complete_what::KEYWORD;
1958 }
1959 else if (token.type == LSTOKEN_COLON)
40e084e1
KS
1960 {
1961 /* User specified a label or a lineno. */
1962 token = linespec_lexer_consume_token (parser);
1963
1964 if (token.type == LSTOKEN_NUMBER)
1965 {
1966 /* User specified an offset. Record the line offset and
1967 get the next token. */
c45ec17c
PA
1968 set_completion_after_number (parser, linespec_complete_what::KEYWORD);
1969
40e084e1
KS
1970 name = copy_token_string (token);
1971 cleanup = make_cleanup (xfree, name);
00e52e53 1972 PARSER_EXPLICIT (parser)->line_offset
40e084e1
KS
1973 = linespec_parse_line_offset (name);
1974 do_cleanups (cleanup);
1975
c45ec17c 1976 /* Get the next token. */
40e084e1
KS
1977 token = linespec_lexer_consume_token (parser);
1978 }
c45ec17c
PA
1979 else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
1980 {
1981 parser->complete_what = linespec_complete_what::LABEL;
1982 }
40e084e1
KS
1983 else if (token.type == LSTOKEN_STRING)
1984 {
c45ec17c
PA
1985 parser->complete_what = linespec_complete_what::LABEL;
1986
1987 /* If we have text after the label separated by whitespace
1988 (e.g., "b func():lab i<tab>"), don't consider it part of
1989 the label. In completion mode that should complete to
1990 "if", in normal mode, the 'i' should be treated as
1991 garbage. */
1992 if (parser->completion_quote_char == '\0')
1993 {
1994 const char *ptr = LS_TOKEN_STOKEN (token).ptr;
1995 for (size_t i = 0; i < LS_TOKEN_STOKEN (token).length; i++)
1996 {
1997 if (ptr[i] == ' ')
1998 {
1999 LS_TOKEN_STOKEN (token).length = i;
f1735a53 2000 PARSER_STREAM (parser) = skip_spaces (ptr + i + 1);
c45ec17c
PA
2001 break;
2002 }
2003 }
2004 }
2005
2006 if (parser->completion_tracker != NULL)
2007 {
2008 if (PARSER_STREAM (parser)[-1] == ' ')
2009 {
2010 parser->completion_word = PARSER_STREAM (parser);
2011 parser->complete_what = linespec_complete_what::KEYWORD;
2012 }
2013 }
2014 else
2015 {
2016 /* XXX Reindent before pushing. */
2017
40e084e1
KS
2018 /* Grab a copy of the label's name and look it up. */
2019 name = copy_token_string (token);
2020 cleanup = make_cleanup (xfree, name);
2021 labels = find_label_symbols (PARSER_STATE (parser),
2022 PARSER_RESULT (parser)->function_symbols,
2023 &symbols, name);
2024
2025 if (labels != NULL)
94af9270 2026 {
40e084e1
KS
2027 PARSER_RESULT (parser)->labels.label_symbols = labels;
2028 PARSER_RESULT (parser)->labels.function_symbols = symbols;
00e52e53 2029 PARSER_EXPLICIT (parser)->label_name = name;
40e084e1
KS
2030 symbols = NULL;
2031 discard_cleanups (cleanup);
2032 }
2033 else
2034 {
2035 /* We don't know what it was, but it isn't a label. */
00e52e53
KS
2036 undefined_label_error (PARSER_EXPLICIT (parser)->function_name,
2037 name);
40e084e1
KS
2038 }
2039
c45ec17c
PA
2040 }
2041
40e084e1 2042 /* Check for a line offset. */
c45ec17c 2043 token = save_stream_and_consume_token (parser);
40e084e1
KS
2044 if (token.type == LSTOKEN_COLON)
2045 {
2046 /* Get the next token. */
2047 token = linespec_lexer_consume_token (parser);
2048
2049 /* It must be a line offset. */
2050 if (token.type != LSTOKEN_NUMBER)
2051 unexpected_linespec_error (parser);
2052
c6756f62 2053 /* Record the line offset and get the next token. */
40e084e1
KS
2054 name = copy_token_string (token);
2055 cleanup = make_cleanup (xfree, name);
2056
00e52e53 2057 PARSER_EXPLICIT (parser)->line_offset
40e084e1
KS
2058 = linespec_parse_line_offset (name);
2059 do_cleanups (cleanup);
2060
2061 /* Get the next token. */
2062 token = linespec_lexer_consume_token (parser);
94af9270
KS
2063 }
2064 }
40e084e1
KS
2065 else
2066 {
2067 /* Trailing ':' in the input. Issue an error. */
2068 unexpected_linespec_error (parser);
2069 }
94af9270 2070 }
40e084e1 2071}
94af9270 2072
40e084e1 2073/* Canonicalize the linespec contained in LS. The result is saved into
00e52e53
KS
2074 STATE->canonical. This function handles both linespec and explicit
2075 locations. */
40e084e1
KS
2076
2077static void
f00aae0f 2078canonicalize_linespec (struct linespec_state *state, const linespec_p ls)
40e084e1 2079{
00e52e53 2080 struct event_location *canon;
67994074 2081 struct explicit_location *explicit_loc;
f00aae0f 2082
40e084e1
KS
2083 /* If canonicalization was not requested, no need to do anything. */
2084 if (!state->canonical)
2085 return;
2086
00e52e53 2087 /* Save everything as an explicit location. */
8e9e35b1
TT
2088 state->canonical->location
2089 = new_explicit_location (&ls->explicit_loc);
2090 canon = state->canonical->location.get ();
67994074 2091 explicit_loc = get_explicit_location (canon);
40e084e1 2092
67994074 2093 if (explicit_loc->label_name != NULL)
a06efdd6 2094 {
00e52e53 2095 state->canonical->special_display = 1;
40e084e1 2096
67994074 2097 if (explicit_loc->function_name == NULL)
40e084e1 2098 {
a06efdd6
KS
2099 struct symbol *s;
2100
2101 /* No function was specified, so add the symbol name. */
2102 gdb_assert (ls->labels.function_symbols != NULL
2103 && (VEC_length (symbolp, ls->labels.function_symbols)
2104 == 1));
2105 s = VEC_index (symbolp, ls->labels.function_symbols, 0);
67994074 2106 explicit_loc->function_name = xstrdup (SYMBOL_NATURAL_NAME (s));
40e084e1 2107 }
a06efdd6 2108 }
40e084e1 2109
00e52e53
KS
2110 /* If this location originally came from a linespec, save a string
2111 representation of it for display and saving to file. */
2112 if (state->is_linespec)
a06efdd6 2113 {
67994074 2114 char *linespec = explicit_location_to_linespec (explicit_loc);
a06efdd6 2115
00e52e53
KS
2116 set_event_location_string (canon, linespec);
2117 xfree (linespec);
2118 }
94af9270 2119}
c00f8484 2120
40e084e1 2121/* Given a line offset in LS, construct the relevant SALs. */
c00f8484 2122
6c5b2ebe 2123static std::vector<symtab_and_line>
40e084e1
KS
2124create_sals_line_offset (struct linespec_state *self,
2125 linespec_p ls)
c00f8484 2126{
40e084e1 2127 int use_default = 0;
c00f8484 2128
40e084e1
KS
2129 /* This is where we need to make sure we have good defaults.
2130 We must guarantee that this section of code is never executed
2e47c6ca 2131 when we are called with just a function name, since
40e084e1
KS
2132 set_default_source_symtab_and_line uses
2133 select_source_symtab that calls us with such an argument. */
2134
ec94af83
DE
2135 if (VEC_length (symtab_ptr, ls->file_symtabs) == 1
2136 && VEC_index (symtab_ptr, ls->file_symtabs, 0) == NULL)
3d50dd94 2137 {
05cba821
JK
2138 const char *fullname;
2139
40e084e1 2140 set_current_program_space (self->program_space);
c00f8484 2141
40e084e1
KS
2142 /* Make sure we have at least a default source line. */
2143 set_default_source_symtab_and_line ();
2144 initialize_defaults (&self->default_symtab, &self->default_line);
05cba821 2145 fullname = symtab_to_fullname (self->default_symtab);
ec94af83
DE
2146 VEC_pop (symtab_ptr, ls->file_symtabs);
2147 VEC_free (symtab_ptr, ls->file_symtabs);
c2f4122d
PA
2148 ls->file_symtabs = collect_symtabs_from_filename (fullname,
2149 self->search_pspace);
40e084e1
KS
2150 use_default = 1;
2151 }
c00f8484 2152
51abb421 2153 symtab_and_line val;
67994074
KS
2154 val.line = ls->explicit_loc.line_offset.offset;
2155 switch (ls->explicit_loc.line_offset.sign)
40e084e1
KS
2156 {
2157 case LINE_OFFSET_PLUS:
67994074 2158 if (ls->explicit_loc.line_offset.offset == 0)
40e084e1
KS
2159 val.line = 5;
2160 if (use_default)
2161 val.line = self->default_line + val.line;
2162 break;
2163
2164 case LINE_OFFSET_MINUS:
67994074 2165 if (ls->explicit_loc.line_offset.offset == 0)
40e084e1
KS
2166 val.line = 15;
2167 if (use_default)
2168 val.line = self->default_line - val.line;
2169 else
2170 val.line = -val.line;
2171 break;
2172
2173 case LINE_OFFSET_NONE:
2174 break; /* No need to adjust val.line. */
2175 }
2176
6c5b2ebe 2177 std::vector<symtab_and_line> values;
40e084e1 2178 if (self->list_mode)
6c5b2ebe 2179 values = decode_digits_list_mode (self, ls, val);
40e084e1
KS
2180 else
2181 {
2182 struct linetable_entry *best_entry = NULL;
40e084e1
KS
2183 int i, j;
2184
6c5b2ebe
PA
2185 std::vector<symtab_and_line> intermediate_results
2186 = decode_digits_ordinary (self, ls, val.line, &best_entry);
2187 if (intermediate_results.empty () && best_entry != NULL)
2188 intermediate_results = decode_digits_ordinary (self, ls,
2189 best_entry->line,
2190 &best_entry);
40e084e1
KS
2191
2192 /* For optimized code, the compiler can scatter one source line
2193 across disjoint ranges of PC values, even when no duplicate
2194 functions or inline functions are involved. For example,
2195 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
2196 function can result in two PC ranges. In this case, we don't
2197 want to set a breakpoint on the first PC of each range. To filter
2198 such cases, we use containing blocks -- for each PC found
2199 above, we see if there are other PCs that are in the same
2200 block. If yes, the other PCs are filtered out. */
2201
0fc21fd8
TT
2202 gdb::def_vector<int> filter (intermediate_results.size ());
2203 gdb::def_vector<const block *> blocks (intermediate_results.size ());
40e084e1 2204
6c5b2ebe 2205 for (i = 0; i < intermediate_results.size (); ++i)
3d50dd94 2206 {
6c5b2ebe 2207 set_current_program_space (intermediate_results[i].pspace);
c00f8484 2208
40e084e1 2209 filter[i] = 1;
6c5b2ebe
PA
2210 blocks[i] = block_for_pc_sect (intermediate_results[i].pc,
2211 intermediate_results[i].section);
3d50dd94 2212 }
c00f8484 2213
6c5b2ebe 2214 for (i = 0; i < intermediate_results.size (); ++i)
40e084e1
KS
2215 {
2216 if (blocks[i] != NULL)
6c5b2ebe 2217 for (j = i + 1; j < intermediate_results.size (); ++j)
40e084e1
KS
2218 {
2219 if (blocks[j] == blocks[i])
2220 {
2221 filter[j] = 0;
2222 break;
2223 }
2224 }
2225 }
c00f8484 2226
6c5b2ebe 2227 for (i = 0; i < intermediate_results.size (); ++i)
40e084e1
KS
2228 if (filter[i])
2229 {
2230 struct symbol *sym = (blocks[i]
2231 ? block_containing_function (blocks[i])
2232 : NULL);
3d50dd94 2233
40e084e1 2234 if (self->funfirstline)
6c5b2ebe 2235 skip_prologue_sal (&intermediate_results[i]);
40e084e1
KS
2236 /* Make sure the line matches the request, not what was
2237 found. */
6c5b2ebe
PA
2238 intermediate_results[i].line = val.line;
2239 add_sal_to_sals (self, &values, &intermediate_results[i],
66f1999b 2240 sym ? SYMBOL_NATURAL_NAME (sym) : NULL, 0);
40e084e1 2241 }
f17170e5 2242 }
c00f8484 2243
6c5b2ebe 2244 if (values.empty ())
40e084e1 2245 {
67994074 2246 if (ls->explicit_loc.source_filename)
40e084e1 2247 throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
67994074 2248 val.line, ls->explicit_loc.source_filename);
40e084e1
KS
2249 else
2250 throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
2251 val.line);
2252 }
3d50dd94 2253
40e084e1 2254 return values;
c00f8484
KS
2255}
2256
a06efdd6
KS
2257/* Convert the given ADDRESS into SaLs. */
2258
6c5b2ebe 2259static std::vector<symtab_and_line>
a06efdd6
KS
2260convert_address_location_to_sals (struct linespec_state *self,
2261 CORE_ADDR address)
2262{
6c5b2ebe 2263 symtab_and_line sal = find_pc_line (address, 0);
a06efdd6
KS
2264 sal.pc = address;
2265 sal.section = find_pc_overlay (address);
2266 sal.explicit_pc = 1;
6c5b2ebe
PA
2267
2268 std::vector<symtab_and_line> sals;
a06efdd6
KS
2269 add_sal_to_sals (self, &sals, &sal, core_addr_to_string (address), 1);
2270
2271 return sals;
2272}
2273
40e084e1
KS
2274/* Create and return SALs from the linespec LS. */
2275
6c5b2ebe 2276static std::vector<symtab_and_line>
40e084e1
KS
2277convert_linespec_to_sals (struct linespec_state *state, linespec_p ls)
2278{
6c5b2ebe 2279 std::vector<symtab_and_line> sals;
40e084e1 2280
a06efdd6 2281 if (ls->labels.label_symbols != NULL)
40e084e1
KS
2282 {
2283 /* We have just a bunch of functions/methods or labels. */
2284 int i;
2285 struct symtab_and_line sal;
2286 struct symbol *sym;
2287
2288 for (i = 0; VEC_iterate (symbolp, ls->labels.label_symbols, i, sym); ++i)
2289 {
08be3fe3 2290 struct program_space *pspace = SYMTAB_PSPACE (symbol_symtab (sym));
fdbb204b
TT
2291
2292 if (symbol_to_sal (&sal, state->funfirstline, sym)
2293 && maybe_add_address (state->addr_set, pspace, sal.pc))
64b92e45
KS
2294 add_sal_to_sals (state, &sals, &sal,
2295 SYMBOL_NATURAL_NAME (sym), 0);
40e084e1
KS
2296 }
2297 }
2298 else if (ls->function_symbols != NULL || ls->minimal_symbols != NULL)
2299 {
2300 /* We have just a bunch of functions and/or methods. */
2301 int i;
2302 struct symtab_and_line sal;
2303 struct symbol *sym;
f60e2d5c 2304 bound_minimal_symbol_d *elem;
40e084e1
KS
2305 struct program_space *pspace;
2306
2307 if (ls->function_symbols != NULL)
2308 {
2309 /* Sort symbols so that symbols with the same program space are next
2310 to each other. */
2311 qsort (VEC_address (symbolp, ls->function_symbols),
2312 VEC_length (symbolp, ls->function_symbols),
2313 sizeof (symbolp), compare_symbols);
2314
2315 for (i = 0; VEC_iterate (symbolp, ls->function_symbols, i, sym); ++i)
2316 {
08be3fe3 2317 pspace = SYMTAB_PSPACE (symbol_symtab (sym));
40e084e1 2318 set_current_program_space (pspace);
64b92e45
KS
2319 if (symbol_to_sal (&sal, state->funfirstline, sym)
2320 && maybe_add_address (state->addr_set, pspace, sal.pc))
66f1999b
KS
2321 add_sal_to_sals (state, &sals, &sal,
2322 SYMBOL_NATURAL_NAME (sym), 0);
40e084e1
KS
2323 }
2324 }
2325
2326 if (ls->minimal_symbols != NULL)
2327 {
2328 /* Sort minimal symbols by program space, too. */
f60e2d5c
TT
2329 qsort (VEC_address (bound_minimal_symbol_d, ls->minimal_symbols),
2330 VEC_length (bound_minimal_symbol_d, ls->minimal_symbols),
2331 sizeof (bound_minimal_symbol_d), compare_msymbols);
40e084e1
KS
2332
2333 for (i = 0;
f60e2d5c
TT
2334 VEC_iterate (bound_minimal_symbol_d, ls->minimal_symbols,
2335 i, elem);
40e084e1
KS
2336 ++i)
2337 {
001822aa 2338 pspace = elem->objfile->pspace;
40e084e1
KS
2339 set_current_program_space (pspace);
2340 minsym_found (state, elem->objfile, elem->minsym, &sals);
2341 }
2342 }
2343 }
67994074 2344 else if (ls->explicit_loc.line_offset.sign != LINE_OFFSET_UNKNOWN)
40e084e1
KS
2345 {
2346 /* Only an offset was specified. */
2347 sals = create_sals_line_offset (state, ls);
2348
2349 /* Make sure we have a filename for canonicalization. */
67994074 2350 if (ls->explicit_loc.source_filename == NULL)
05cba821
JK
2351 {
2352 const char *fullname = symtab_to_fullname (state->default_symtab);
2353
e93ba630
JK
2354 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2355 form so that displaying SOURCE_FILENAME can follow the current
2356 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2357 it has been kept for code simplicity only in absolute form. */
67994074 2358 ls->explicit_loc.source_filename = xstrdup (fullname);
05cba821 2359 }
40e084e1
KS
2360 }
2361 else
2362 {
2363 /* We haven't found any results... */
2364 return sals;
2365 }
2366
2367 canonicalize_linespec (state, ls);
2368
6c5b2ebe 2369 if (!sals.empty () && state->canonical != NULL)
40e084e1
KS
2370 state->canonical->pre_expanded = 1;
2371
2372 return sals;
2373}
50641945 2374
a2459270
PA
2375/* Build RESULT from the explicit location components SOURCE_FILENAME,
2376 FUNCTION_NAME, LABEL_NAME and LINE_OFFSET. */
00e52e53 2377
a2459270
PA
2378static void
2379convert_explicit_location_to_linespec (struct linespec_state *self,
2380 linespec_p result,
2381 const char *source_filename,
2382 const char *function_name,
2383 const char *label_name,
2384 struct line_offset line_offset)
00e52e53
KS
2385{
2386 VEC (symbolp) *symbols, *labels;
2387 VEC (bound_minimal_symbol_d) *minimal_symbols;
2388
a2459270 2389 if (source_filename != NULL)
00e52e53
KS
2390 {
2391 TRY
2392 {
2393 result->file_symtabs
a2459270 2394 = symtabs_from_filename (source_filename, self->search_pspace);
00e52e53
KS
2395 }
2396 CATCH (except, RETURN_MASK_ERROR)
2397 {
a2459270 2398 source_file_not_found_error (source_filename);
00e52e53
KS
2399 }
2400 END_CATCH
a2459270 2401 result->explicit_loc.source_filename = xstrdup (source_filename);
00e52e53
KS
2402 }
2403 else
2404 {
2405 /* A NULL entry means to use the default symtab. */
2406 VEC_safe_push (symtab_ptr, result->file_symtabs, NULL);
2407 }
2408
a2459270 2409 if (function_name != NULL)
00e52e53
KS
2410 {
2411 find_linespec_symbols (self, result->file_symtabs,
a2459270 2412 function_name, &symbols,
00e52e53
KS
2413 &minimal_symbols);
2414
2415 if (symbols == NULL && minimal_symbols == NULL)
a2459270 2416 symbol_not_found_error (function_name,
67994074 2417 result->explicit_loc.source_filename);
00e52e53 2418
a2459270 2419 result->explicit_loc.function_name = xstrdup (function_name);
00e52e53
KS
2420 result->function_symbols = symbols;
2421 result->minimal_symbols = minimal_symbols;
2422 }
2423
a2459270 2424 if (label_name != NULL)
00e52e53
KS
2425 {
2426 symbols = NULL;
2427 labels = find_label_symbols (self, result->function_symbols,
a2459270 2428 &symbols, label_name);
00e52e53
KS
2429
2430 if (labels == NULL)
67994074 2431 undefined_label_error (result->explicit_loc.function_name,
a2459270 2432 label_name);
00e52e53 2433
a2459270 2434 result->explicit_loc.label_name = xstrdup (label_name);
00e52e53
KS
2435 result->labels.label_symbols = labels;
2436 result->labels.function_symbols = symbols;
2437 }
2438
a2459270
PA
2439 if (line_offset.sign != LINE_OFFSET_UNKNOWN)
2440 result->explicit_loc.line_offset = line_offset;
2441}
00e52e53 2442
a2459270
PA
2443/* Convert the explicit location EXPLICIT_LOC into SaLs. */
2444
6c5b2ebe 2445static std::vector<symtab_and_line>
a2459270
PA
2446convert_explicit_location_to_sals (struct linespec_state *self,
2447 linespec_p result,
2448 const struct explicit_location *explicit_loc)
2449{
2450 convert_explicit_location_to_linespec (self, result,
2451 explicit_loc->source_filename,
2452 explicit_loc->function_name,
2453 explicit_loc->label_name,
2454 explicit_loc->line_offset);
2455 return convert_linespec_to_sals (self, result);
00e52e53
KS
2456}
2457
40e084e1 2458/* Parse a string that specifies a linespec.
50641945 2459
40e084e1 2460 The basic grammar of linespecs:
50641945 2461
a06efdd6 2462 linespec -> var_spec | basic_spec
40e084e1 2463 var_spec -> '$' (STRING | NUMBER)
50641945 2464
40e084e1
KS
2465 basic_spec -> file_offset_spec | function_spec | label_spec
2466 file_offset_spec -> opt_file_spec offset_spec
2467 function_spec -> opt_file_spec function_name_spec opt_label_spec
2468 label_spec -> label_name_spec
50641945 2469
40e084e1
KS
2470 opt_file_spec -> "" | file_name_spec ':'
2471 opt_label_spec -> "" | ':' label_name_spec
2472
2473 file_name_spec -> STRING
2474 function_name_spec -> STRING
2475 label_name_spec -> STRING
2476 function_name_spec -> STRING
2477 offset_spec -> NUMBER
2478 -> '+' NUMBER
2479 -> '-' NUMBER
2480
2481 This may all be followed by several keywords such as "if EXPR",
2482 which we ignore.
2483
2484 A comma will terminate parsing.
2485
2486 The function may be an undebuggable function found in minimal symbol table.
50641945
FN
2487
2488 If the argument FUNFIRSTLINE is nonzero, we want the first line
2489 of real code inside a function when a function is specified, and it is
2490 not OK to specify a variable or type to get its line number.
2491
2492 DEFAULT_SYMTAB specifies the file to use if none is specified.
2493 It defaults to current_source_symtab.
2494 DEFAULT_LINE specifies the line number to use for relative
2495 line numbers (that start with signs). Defaults to current_source_line.
2496 If CANONICAL is non-NULL, store an array of strings containing the canonical
1777feb0 2497 line specs there if necessary. Currently overloaded member functions and
50641945 2498 line numbers or static functions without a filename yield a canonical
1777feb0 2499 line spec. The array and the line spec strings are allocated on the heap,
50641945
FN
2500 it is the callers responsibility to free them.
2501
2502 Note that it is possible to return zero for the symtab
2503 if no file is validly specified. Callers must check that.
58438ac1 2504 Also, the line number returned may be invalid. */
50641945 2505
f00aae0f 2506/* Parse the linespec in ARG. */
50641945 2507
6c5b2ebe 2508static std::vector<symtab_and_line>
f00aae0f 2509parse_linespec (linespec_parser *parser, const char *arg)
50641945 2510{
40e084e1 2511 linespec_token token;
7556d4a4 2512 struct gdb_exception file_exception = exception_none;
40e084e1
KS
2513 struct cleanup *cleanup;
2514
2515 /* A special case to start. It has become quite popular for
2516 IDEs to work around bugs in the previous parser by quoting
2517 the entire linespec, so we attempt to deal with this nicely. */
2518 parser->is_quote_enclosed = 0;
c45ec17c
PA
2519 if (parser->completion_tracker == NULL
2520 && !is_ada_operator (arg)
f00aae0f 2521 && strchr (linespec_quote_characters, *arg) != NULL)
40e084e1
KS
2522 {
2523 const char *end;
9ef07c8c 2524
f00aae0f 2525 end = skip_quote_char (arg + 1, *arg);
40e084e1 2526 if (end != NULL && is_closing_quote_enclosed (end))
136e1c30 2527 {
f00aae0f 2528 /* Here's the special case. Skip ARG past the initial
40e084e1 2529 quote. */
f00aae0f 2530 ++arg;
40e084e1 2531 parser->is_quote_enclosed = 1;
136e1c30
DE
2532 }
2533 }
e8eb7bc5 2534
f00aae0f
KS
2535 parser->lexer.saved_arg = arg;
2536 parser->lexer.stream = arg;
c45ec17c
PA
2537 parser->completion_word = arg;
2538 parser->complete_what = linespec_complete_what::FUNCTION;
d2630e69 2539
40e084e1
KS
2540 /* Initialize the default symtab and line offset. */
2541 initialize_defaults (&PARSER_STATE (parser)->default_symtab,
2542 &PARSER_STATE (parser)->default_line);
d2630e69 2543
40e084e1 2544 /* Objective-C shortcut. */
c45ec17c
PA
2545 if (parser->completion_tracker == NULL)
2546 {
6c5b2ebe
PA
2547 std::vector<symtab_and_line> values
2548 = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), arg);
2549 if (!values.empty ())
c45ec17c
PA
2550 return values;
2551 }
2552 else
2553 {
2554 /* "-"/"+" is either an objc selector, or a number. There's
2555 nothing to complete the latter to, so just let the caller
2556 complete on functions, which finds objc selectors, if there's
2557 any. */
2558 if ((arg[0] == '-' || arg[0] == '+') && arg[1] == '\0')
2559 return {};
2560 }
e0881a8e 2561
40e084e1 2562 /* Start parsing. */
d2630e69 2563
40e084e1 2564 /* Get the first token. */
c45ec17c 2565 token = linespec_lexer_consume_token (parser);
50641945 2566
40e084e1 2567 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
a06efdd6 2568 if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
40e084e1
KS
2569 {
2570 char *var;
50641945 2571
40e084e1 2572 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
c45ec17c
PA
2573 if (parser->completion_tracker == NULL)
2574 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
dcf9f4ab 2575
40e084e1
KS
2576 /* User specified a convenience variable or history value. */
2577 var = copy_token_string (token);
2578 cleanup = make_cleanup (xfree, var);
00e52e53 2579 PARSER_EXPLICIT (parser)->line_offset
40e084e1 2580 = linespec_parse_variable (PARSER_STATE (parser), var);
cf4ded82 2581 do_cleanups (cleanup);
f8eba3c6 2582
40e084e1
KS
2583 /* If a line_offset wasn't found (VAR is the name of a user
2584 variable/function), then skip to normal symbol processing. */
00e52e53 2585 if (PARSER_EXPLICIT (parser)->line_offset.sign != LINE_OFFSET_UNKNOWN)
40e084e1 2586 {
40e084e1
KS
2587 /* Consume this token. */
2588 linespec_lexer_consume_token (parser);
dcf9f4ab 2589
40e084e1 2590 goto convert_to_sals;
50641945 2591 }
40e084e1 2592 }
c45ec17c
PA
2593 else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
2594 {
2595 /* Let the default linespec_complete_what::FUNCTION kick in. */
2596 unexpected_linespec_error (parser);
2597 }
40e084e1 2598 else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
c45ec17c
PA
2599 {
2600 parser->complete_what = linespec_complete_what::NOTHING;
2601 unexpected_linespec_error (parser);
2602 }
50641945 2603
40e084e1
KS
2604 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2605 this token cannot represent a filename. */
2606 token = linespec_lexer_peek_token (parser);
0e0b460e 2607
40e084e1 2608 if (token.type == LSTOKEN_COLON)
0e0b460e 2609 {
40e084e1 2610 char *user_filename;
0e0b460e 2611
40e084e1
KS
2612 /* Get the current token again and extract the filename. */
2613 token = linespec_lexer_lex_one (parser);
2614 user_filename = copy_token_string (token);
50641945 2615
40e084e1 2616 /* Check if the input is a filename. */
492d29ea 2617 TRY
40e084e1
KS
2618 {
2619 PARSER_RESULT (parser)->file_symtabs
c2f4122d
PA
2620 = symtabs_from_filename (user_filename,
2621 PARSER_STATE (parser)->search_pspace);
40e084e1 2622 }
492d29ea 2623 CATCH (ex, RETURN_MASK_ERROR)
7556d4a4
PA
2624 {
2625 file_exception = ex;
2626 }
492d29ea 2627 END_CATCH
50641945 2628
40e084e1
KS
2629 if (file_exception.reason >= 0)
2630 {
2631 /* Symtabs were found for the file. Record the filename. */
00e52e53 2632 PARSER_EXPLICIT (parser)->source_filename = user_filename;
f8eba3c6 2633
40e084e1
KS
2634 /* Get the next token. */
2635 token = linespec_lexer_consume_token (parser);
50641945 2636
40e084e1
KS
2637 /* This is LSTOKEN_COLON; consume it. */
2638 linespec_lexer_consume_token (parser);
2639 }
2640 else
2641 {
2642 /* No symtabs found -- discard user_filename. */
2643 xfree (user_filename);
50641945 2644
40e084e1 2645 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
ec94af83 2646 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
40e084e1 2647 }
50641945 2648 }
40e084e1 2649 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
c45ec17c
PA
2650 else if (parser->completion_tracker == NULL
2651 && (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD
2652 && token.type != LSTOKEN_COMMA))
d2630e69 2653 {
40e084e1
KS
2654 /* TOKEN is the _next_ token, not the one currently in the parser.
2655 Consuming the token will give the correct error message. */
2656 linespec_lexer_consume_token (parser);
2657 unexpected_linespec_error (parser);
d2630e69 2658 }
50641945
FN
2659 else
2660 {
40e084e1 2661 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
ec94af83 2662 VEC_safe_push (symtab_ptr, PARSER_RESULT (parser)->file_symtabs, NULL);
50641945 2663 }
50641945 2664
40e084e1
KS
2665 /* Parse the rest of the linespec. */
2666 linespec_parse_basic (parser);
50641945 2667
c45ec17c
PA
2668 if (parser->completion_tracker == NULL
2669 && PARSER_RESULT (parser)->function_symbols == NULL
40e084e1 2670 && PARSER_RESULT (parser)->labels.label_symbols == NULL
00e52e53 2671 && PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN
40e084e1 2672 && PARSER_RESULT (parser)->minimal_symbols == NULL)
f8eba3c6 2673 {
40e084e1
KS
2674 /* The linespec didn't parse. Re-throw the file exception if
2675 there was one. */
2676 if (file_exception.reason < 0)
2677 throw_exception (file_exception);
0f5238ed 2678
40e084e1 2679 /* Otherwise, the symbol is not found. */
00e52e53
KS
2680 symbol_not_found_error (PARSER_EXPLICIT (parser)->function_name,
2681 PARSER_EXPLICIT (parser)->source_filename);
0f5238ed
TT
2682 }
2683
40e084e1 2684 convert_to_sals:
9ef07c8c 2685
40e084e1
KS
2686 /* Get the last token and record how much of the input was parsed,
2687 if necessary. */
2688 token = linespec_lexer_lex_one (parser);
2689 if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD)
c45ec17c
PA
2690 unexpected_linespec_error (parser);
2691 else if (token.type == LSTOKEN_KEYWORD)
2692 {
2693 /* Setup the completion word past the keyword. Lexing never
2694 advances past a keyword automatically, so skip it
2695 manually. */
2696 parser->completion_word
f1735a53 2697 = skip_spaces (skip_to_space (PARSER_STREAM (parser)));
c45ec17c
PA
2698 parser->complete_what = linespec_complete_what::EXPRESSION;
2699 }
50641945 2700
40e084e1 2701 /* Convert the data in PARSER_RESULT to SALs. */
c45ec17c 2702 if (parser->completion_tracker == NULL)
6c5b2ebe
PA
2703 return convert_linespec_to_sals (PARSER_STATE (parser),
2704 PARSER_RESULT (parser));
f8eba3c6 2705
6c5b2ebe 2706 return {};
413dad4d 2707}
50641945 2708
40e084e1 2709
f8eba3c6 2710/* A constructor for linespec_state. */
44fe14ab 2711
f8eba3c6
TT
2712static void
2713linespec_state_constructor (struct linespec_state *self,
40e084e1 2714 int flags, const struct language_defn *language,
c2f4122d 2715 struct program_space *search_pspace,
f8eba3c6
TT
2716 struct symtab *default_symtab,
2717 int default_line,
2718 struct linespec_result *canonical)
2719{
2720 memset (self, 0, sizeof (*self));
40e084e1 2721 self->language = language;
f8eba3c6
TT
2722 self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
2723 self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
c2f4122d 2724 self->search_pspace = search_pspace;
f8eba3c6
TT
2725 self->default_symtab = default_symtab;
2726 self->default_line = default_line;
2727 self->canonical = canonical;
2728 self->program_space = current_program_space;
2729 self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
2730 xfree, xcalloc, xfree);
00e52e53 2731 self->is_linespec = 0;
f8eba3c6 2732}
44fe14ab 2733
40e084e1 2734/* Initialize a new linespec parser. */
44fe14ab
DC
2735
2736static void
40e084e1
KS
2737linespec_parser_new (linespec_parser *parser,
2738 int flags, const struct language_defn *language,
c2f4122d 2739 struct program_space *search_pspace,
40e084e1
KS
2740 struct symtab *default_symtab,
2741 int default_line,
2742 struct linespec_result *canonical)
44fe14ab 2743{
f00aae0f 2744 memset (parser, 0, sizeof (linespec_parser));
40e084e1
KS
2745 parser->lexer.current.type = LSTOKEN_CONSUMED;
2746 memset (PARSER_RESULT (parser), 0, sizeof (struct linespec));
00e52e53 2747 PARSER_EXPLICIT (parser)->line_offset.sign = LINE_OFFSET_UNKNOWN;
40e084e1 2748 linespec_state_constructor (PARSER_STATE (parser), flags, language,
c2f4122d 2749 search_pspace,
40e084e1
KS
2750 default_symtab, default_line, canonical);
2751}
2752
2753/* A destructor for linespec_state. */
44fe14ab 2754
40e084e1
KS
2755static void
2756linespec_state_destructor (struct linespec_state *self)
2757{
f8eba3c6
TT
2758 htab_delete (self->addr_set);
2759}
44fe14ab 2760
40e084e1
KS
2761/* Delete a linespec parser. */
2762
2763static void
2764linespec_parser_delete (void *arg)
2765{
2766 linespec_parser *parser = (linespec_parser *) arg;
2767
00e52e53
KS
2768 xfree (PARSER_EXPLICIT (parser)->source_filename);
2769 xfree (PARSER_EXPLICIT (parser)->label_name);
2770 xfree (PARSER_EXPLICIT (parser)->function_name);
40e084e1
KS
2771
2772 if (PARSER_RESULT (parser)->file_symtabs != NULL)
ec94af83 2773 VEC_free (symtab_ptr, PARSER_RESULT (parser)->file_symtabs);
40e084e1
KS
2774
2775 if (PARSER_RESULT (parser)->function_symbols != NULL)
2776 VEC_free (symbolp, PARSER_RESULT (parser)->function_symbols);
2777
2778 if (PARSER_RESULT (parser)->minimal_symbols != NULL)
f60e2d5c 2779 VEC_free (bound_minimal_symbol_d, PARSER_RESULT (parser)->minimal_symbols);
40e084e1
KS
2780
2781 if (PARSER_RESULT (parser)->labels.label_symbols != NULL)
2782 VEC_free (symbolp, PARSER_RESULT (parser)->labels.label_symbols);
2783
2784 if (PARSER_RESULT (parser)->labels.function_symbols != NULL)
2785 VEC_free (symbolp, PARSER_RESULT (parser)->labels.function_symbols);
2786
2787 linespec_state_destructor (PARSER_STATE (parser));
2788}
2789
c7c1b3e9
KS
2790/* See description in linespec.h. */
2791
2792void
f2fc3015 2793linespec_lex_to_end (const char **stringp)
c7c1b3e9
KS
2794{
2795 linespec_parser parser;
2796 struct cleanup *cleanup;
2797 linespec_token token;
c7c1b3e9
KS
2798 const char *orig;
2799
2800 if (stringp == NULL || *stringp == NULL)
2801 return;
2802
c2f4122d 2803 linespec_parser_new (&parser, 0, current_language, NULL, NULL, 0, NULL);
c7c1b3e9
KS
2804 cleanup = make_cleanup (linespec_parser_delete, &parser);
2805 parser.lexer.saved_arg = *stringp;
2806 PARSER_STREAM (&parser) = orig = *stringp;
2807
2808 do
2809 {
2810 /* Stop before any comma tokens; we need it to keep it
2811 as the next token in the string. */
2812 token = linespec_lexer_peek_token (&parser);
2813 if (token.type == LSTOKEN_COMMA)
2814 break;
c7c1b3e9
KS
2815 token = linespec_lexer_consume_token (&parser);
2816 }
2817 while (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD);
2818
2819 *stringp += PARSER_STREAM (&parser) - orig;
2820 do_cleanups (cleanup);
2821}
2822
c6756f62
PA
2823/* See linespec.h. */
2824
2825void
2826linespec_complete_function (completion_tracker &tracker,
2827 const char *function,
2828 const char *source_filename)
2829{
2830 complete_symbol_mode mode = complete_symbol_mode::LINESPEC;
b5ec771e 2831 symbol_name_match_type func_match_type = symbol_name_match_type::WILD;
c6756f62
PA
2832
2833 if (source_filename != NULL)
2834 {
b5ec771e
PA
2835 collect_file_symbol_completion_matches (tracker, mode, func_match_type,
2836 function, function, source_filename);
c6756f62
PA
2837 }
2838 else
b5ec771e
PA
2839 {
2840 collect_symbol_completion_matches (tracker, mode, func_match_type,
2841 function, function);
2842
2843 }
c6756f62
PA
2844}
2845
c45ec17c
PA
2846/* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
2847 only meaningful if COMPONENT is FUNCTION. */
2848
2849static void
2850complete_linespec_component (linespec_parser *parser,
2851 completion_tracker &tracker,
2852 const char *text,
2853 linespec_complete_what component,
2854 const char *source_filename)
2855{
2856 if (component == linespec_complete_what::KEYWORD)
2857 {
2858 complete_on_enum (tracker, linespec_keywords, text, text);
2859 }
2860 else if (component == linespec_complete_what::EXPRESSION)
2861 {
2862 const char *word
2863 = advance_to_expression_complete_word_point (tracker, text);
2864 complete_expression (tracker, text, word);
2865 }
2866 else if (component == linespec_complete_what::FUNCTION)
2867 {
2868 completion_list fn_list;
2869
2870 linespec_complete_function (tracker, text, source_filename);
2871 if (source_filename == NULL)
2872 {
2873 /* Haven't seen a source component, like in "b
2874 file.c:function[TAB]". Maybe this wasn't a function, but
2875 a filename instead, like "b file.[TAB]". */
2876 fn_list = complete_source_filenames (text);
2877 }
2878
2879 /* If we only have a single filename completion, append a ':' for
2880 the user, since that's the only thing that can usefully follow
2881 the filename. */
2882 if (fn_list.size () == 1 && !tracker.have_completions ())
2883 {
2884 char *fn = fn_list[0].release ();
2885
2886 /* If we also need to append a quote char, it needs to be
2887 appended before the ':'. Append it now, and make ':' the
2888 new "quote" char. */
2889 if (tracker.quote_char ())
2890 {
2891 char quote_char_str[2] = { tracker.quote_char () };
2892
2893 fn = reconcat (fn, fn, quote_char_str, (char *) NULL);
2894 tracker.set_quote_char (':');
2895 }
2896 else
2897 fn = reconcat (fn, fn, ":", (char *) NULL);
2898 fn_list[0].reset (fn);
2899
2900 /* Tell readline to skip appending a space. */
2901 tracker.set_suppress_append_ws (true);
2902 }
2903 tracker.add_completions (std::move (fn_list));
2904 }
2905}
2906
a2459270
PA
2907/* Helper for linespec_complete_label. Find labels that match
2908 LABEL_NAME in the function symbols listed in the PARSER, and add
2909 them to the tracker. */
2910
2911static void
2912complete_label (completion_tracker &tracker,
2913 linespec_parser *parser,
2914 const char *label_name)
2915{
2916 VEC (symbolp) *label_function_symbols = NULL;
2917 VEC (symbolp) *labels
2918 = find_label_symbols (PARSER_STATE (parser),
2919 PARSER_RESULT (parser)->function_symbols,
2920 &label_function_symbols,
2921 label_name, true);
2922
2923 symbol *label;
2924 for (int ix = 0;
2925 VEC_iterate (symbolp, labels, ix, label); ++ix)
2926 {
2927 char *match = xstrdup (SYMBOL_SEARCH_NAME (label));
2928 tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
2929 }
2930 VEC_free (symbolp, labels);
2931}
2932
2933/* See linespec.h. */
2934
2935void
2936linespec_complete_label (completion_tracker &tracker,
2937 const struct language_defn *language,
2938 const char *source_filename,
2939 const char *function_name,
2940 const char *label_name)
2941{
2942 linespec_parser parser;
2943 struct cleanup *cleanup;
2944
2945 linespec_parser_new (&parser, 0, language, NULL, NULL, 0, NULL);
2946 cleanup = make_cleanup (linespec_parser_delete, &parser);
2947
2948 line_offset unknown_offset = { 0, LINE_OFFSET_UNKNOWN };
2949
2950 TRY
2951 {
2952 convert_explicit_location_to_linespec (PARSER_STATE (&parser),
2953 PARSER_RESULT (&parser),
2954 source_filename,
2955 function_name,
2956 NULL, unknown_offset);
2957 }
2958 CATCH (ex, RETURN_MASK_ERROR)
2959 {
2960 do_cleanups (cleanup);
2961 return;
2962 }
2963 END_CATCH
2964
2965 complete_label (tracker, &parser, label_name);
2966
2967 do_cleanups (cleanup);
2968}
2969
c45ec17c
PA
2970/* See description in linespec.h. */
2971
2972void
2973linespec_complete (completion_tracker &tracker, const char *text)
2974{
2975 linespec_parser parser;
2976 struct cleanup *cleanup;
2977 const char *orig = text;
2978
2979 linespec_parser_new (&parser, 0, current_language, NULL, NULL, 0, NULL);
2980 cleanup = make_cleanup (linespec_parser_delete, &parser);
2981 parser.lexer.saved_arg = text;
2982 PARSER_STREAM (&parser) = text;
2983
2984 parser.completion_tracker = &tracker;
2985 PARSER_STATE (&parser)->is_linespec = 1;
2986
2987 /* Parse as much as possible. parser.completion_word will hold
2988 furthest completion point we managed to parse to. */
2989 TRY
2990 {
2991 parse_linespec (&parser, text);
2992 }
2993 CATCH (except, RETURN_MASK_ERROR)
2994 {
2995 }
2996 END_CATCH
2997
2998 if (parser.completion_quote_char != '\0'
2999 && parser.completion_quote_end != NULL
3000 && parser.completion_quote_end[1] == '\0')
3001 {
3002 /* If completing a quoted string with the cursor right at
3003 terminating quote char, complete the completion word without
3004 interpretation, so that readline advances the cursor one
3005 whitespace past the quote, even if there's no match. This
3006 makes these cases behave the same:
3007
3008 before: "b function()"
3009 after: "b function() "
3010
3011 before: "b 'function()'"
3012 after: "b 'function()' "
3013
3014 and trusts the user in this case:
3015
3016 before: "b 'not_loaded_function_yet()'"
3017 after: "b 'not_loaded_function_yet()' "
3018 */
3019 parser.complete_what = linespec_complete_what::NOTHING;
3020 parser.completion_quote_char = '\0';
3021
3022 gdb::unique_xmalloc_ptr<char> text_copy
3023 (xstrdup (parser.completion_word));
3024 tracker.add_completion (std::move (text_copy));
3025 }
3026
3027 tracker.set_quote_char (parser.completion_quote_char);
3028
3029 if (parser.complete_what == linespec_complete_what::LABEL)
3030 {
3031 parser.complete_what = linespec_complete_what::NOTHING;
3032
3033 const char *func_name = PARSER_EXPLICIT (&parser)->function_name;
3034
3035 VEC (symbolp) *function_symbols;
3036 VEC (bound_minimal_symbol_d) *minimal_symbols;
3037 find_linespec_symbols (PARSER_STATE (&parser),
3038 PARSER_RESULT (&parser)->file_symtabs,
3039 func_name,
3040 &function_symbols, &minimal_symbols);
3041
3042 PARSER_RESULT (&parser)->function_symbols = function_symbols;
3043 PARSER_RESULT (&parser)->minimal_symbols = minimal_symbols;
3044
3045 complete_label (tracker, &parser, parser.completion_word);
3046 }
3047 else if (parser.complete_what == linespec_complete_what::FUNCTION)
3048 {
3049 /* While parsing/lexing, we didn't know whether the completion
3050 word completes to a unique function/source name already or
3051 not.
3052
3053 E.g.:
3054 "b function() <tab>"
3055 may need to complete either to:
3056 "b function() const"
3057 or to:
3058 "b function() if/thread/task"
3059
3060 Or, this:
3061 "b foo t"
3062 may need to complete either to:
3063 "b foo template_fun<T>()"
3064 with "foo" being the template function's return type, or to:
3065 "b foo thread/task"
3066
3067 Or, this:
3068 "b file<TAB>"
3069 may need to complete either to a source file name:
3070 "b file.c"
3071 or this, also a filename, but a unique completion:
3072 "b file.c:"
3073 or to a function name:
3074 "b file_function"
3075
3076 Address that by completing assuming source or function, and
3077 seeing if we find a completion that matches exactly the
3078 completion word. If so, then it must be a function (see note
3079 below) and we advance the completion word to the end of input
3080 and switch to KEYWORD completion mode.
3081
3082 Note: if we find a unique completion for a source filename,
3083 then it won't match the completion word, because the LCD will
3084 contain a trailing ':'. And if we're completing at or after
3085 the ':', then complete_linespec_component won't try to
3086 complete on source filenames. */
3087
3088 const char *text = parser.completion_word;
3089 const char *word = parser.completion_word;
3090
3091 complete_linespec_component (&parser, tracker,
3092 parser.completion_word,
3093 linespec_complete_what::FUNCTION,
3094 PARSER_EXPLICIT (&parser)->source_filename);
3095
3096 parser.complete_what = linespec_complete_what::NOTHING;
3097
3098 if (tracker.quote_char ())
3099 {
3100 /* The function/file name was not close-quoted, so this
3101 can't be a keyword. Note: complete_linespec_component
3102 may have swapped the original quote char for ':' when we
3103 get here, but that still indicates the same. */
3104 }
3105 else if (!tracker.have_completions ())
3106 {
3107 size_t key_start;
3108 size_t wordlen = strlen (parser.completion_word);
3109
3110 key_start
3111 = string_find_incomplete_keyword_at_end (linespec_keywords,
3112 parser.completion_word,
3113 wordlen);
3114
3115 if (key_start != -1
3116 || (wordlen > 0
3117 && parser.completion_word[wordlen - 1] == ' '))
3118 {
3119 parser.completion_word += key_start;
3120 parser.complete_what = linespec_complete_what::KEYWORD;
3121 }
3122 }
3123 else if (tracker.completes_to_completion_word (word))
3124 {
3125 /* Skip the function and complete on keywords. */
3126 parser.completion_word += strlen (word);
3127 parser.complete_what = linespec_complete_what::KEYWORD;
3128 tracker.discard_completions ();
3129 }
3130 }
3131
3132 tracker.advance_custom_word_point_by (parser.completion_word - orig);
3133
3134 complete_linespec_component (&parser, tracker,
3135 parser.completion_word,
3136 parser.complete_what,
3137 PARSER_EXPLICIT (&parser)->source_filename);
3138
3139 /* If we're past the "filename:function:label:offset" linespec, and
3140 didn't find any match, then assume the user might want to create
3141 a pending breakpoint anyway and offer the keyword
3142 completions. */
3143 if (!parser.completion_quote_char
3144 && (parser.complete_what == linespec_complete_what::FUNCTION
3145 || parser.complete_what == linespec_complete_what::LABEL
3146 || parser.complete_what == linespec_complete_what::NOTHING)
3147 && !tracker.have_completions ())
3148 {
3149 const char *end
3150 = parser.completion_word + strlen (parser.completion_word);
3151
3152 if (end > orig && end[-1] == ' ')
3153 {
3154 tracker.advance_custom_word_point_by (end - parser.completion_word);
3155
3156 complete_linespec_component (&parser, tracker, end,
3157 linespec_complete_what::KEYWORD,
3158 NULL);
3159 }
3160 }
3161
3162 do_cleanups (cleanup);
3163}
3164
f00aae0f 3165/* A helper function for decode_line_full and decode_line_1 to
6c5b2ebe 3166 turn LOCATION into std::vector<symtab_and_line>. */
f00aae0f 3167
6c5b2ebe 3168static std::vector<symtab_and_line>
f00aae0f
KS
3169event_location_to_sals (linespec_parser *parser,
3170 const struct event_location *location)
3171{
6c5b2ebe 3172 std::vector<symtab_and_line> result;
f00aae0f
KS
3173
3174 switch (event_location_type (location))
3175 {
3176 case LINESPEC_LOCATION:
3177 {
00e52e53 3178 PARSER_STATE (parser)->is_linespec = 1;
f00aae0f
KS
3179 TRY
3180 {
3181 result = parse_linespec (parser, get_linespec_location (location));
3182 }
3183 CATCH (except, RETURN_MASK_ERROR)
3184 {
3185 throw_exception (except);
3186 }
3187 END_CATCH
3188 }
3189 break;
3190
a06efdd6 3191 case ADDRESS_LOCATION:
305e13e6
JB
3192 {
3193 const char *addr_string = get_address_string_location (location);
3194 CORE_ADDR addr = get_address_location (location);
3195
3196 if (addr_string != NULL)
3197 {
3198 char *expr = xstrdup (addr_string);
3199 const char *const_expr = expr;
3200 struct cleanup *cleanup = make_cleanup (xfree, expr);
3201
3202 addr = linespec_expression_to_pc (&const_expr);
3203 if (PARSER_STATE (parser)->canonical != NULL)
3204 PARSER_STATE (parser)->canonical->location
8e9e35b1 3205 = copy_event_location (location);
305e13e6
JB
3206
3207 do_cleanups (cleanup);
3208 }
3209
3210 result = convert_address_location_to_sals (PARSER_STATE (parser),
3211 addr);
3212 }
a06efdd6
KS
3213 break;
3214
00e52e53
KS
3215 case EXPLICIT_LOCATION:
3216 {
67994074 3217 const struct explicit_location *explicit_loc;
00e52e53 3218
67994074 3219 explicit_loc = get_explicit_location_const (location);
00e52e53
KS
3220 result = convert_explicit_location_to_sals (PARSER_STATE (parser),
3221 PARSER_RESULT (parser),
67994074 3222 explicit_loc);
00e52e53
KS
3223 }
3224 break;
3225
5b56227b
KS
3226 case PROBE_LOCATION:
3227 /* Probes are handled by their own decoders. */
3228 gdb_assert_not_reached ("attempt to decode probe location");
3229 break;
3230
f00aae0f
KS
3231 default:
3232 gdb_assert_not_reached ("unhandled event location type");
3233 }
3234
3235 return result;
3236}
3237
f8eba3c6 3238/* See linespec.h. */
44fe14ab 3239
f8eba3c6 3240void
f00aae0f 3241decode_line_full (const struct event_location *location, int flags,
c2f4122d 3242 struct program_space *search_pspace,
f8eba3c6
TT
3243 struct symtab *default_symtab,
3244 int default_line, struct linespec_result *canonical,
3245 const char *select_mode,
3246 const char *filter)
44fe14ab 3247{
f8eba3c6 3248 struct cleanup *cleanups;
f8eba3c6 3249 VEC (const_char_ptr) *filters = NULL;
40e084e1
KS
3250 linespec_parser parser;
3251 struct linespec_state *state;
f8eba3c6
TT
3252
3253 gdb_assert (canonical != NULL);
3254 /* The filter only makes sense for 'all'. */
3255 gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
3256 gdb_assert (select_mode == NULL
3257 || select_mode == multiple_symbols_all
3258 || select_mode == multiple_symbols_ask
3259 || select_mode == multiple_symbols_cancel);
3260 gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);
3261
c2f4122d
PA
3262 linespec_parser_new (&parser, flags, current_language,
3263 search_pspace, default_symtab,
40e084e1
KS
3264 default_line, canonical);
3265 cleanups = make_cleanup (linespec_parser_delete, &parser);
5ed8105e
PA
3266
3267 scoped_restore_current_program_space restore_pspace;
f8eba3c6 3268
6c5b2ebe
PA
3269 std::vector<symtab_and_line> result = event_location_to_sals (&parser,
3270 location);
40e084e1 3271 state = PARSER_STATE (&parser);
f8eba3c6 3272
6c5b2ebe 3273 gdb_assert (result.size () == 1 || canonical->pre_expanded);
f8eba3c6
TT
3274 canonical->pre_expanded = 1;
3275
66f1999b 3276 /* Arrange for allocated canonical names to be freed. */
6c5b2ebe 3277 if (!result.empty ())
f8eba3c6
TT
3278 {
3279 int i;
3280
40e084e1 3281 make_cleanup (xfree, state->canonical_names);
6c5b2ebe 3282 for (i = 0; i < result.size (); ++i)
f8eba3c6 3283 {
33f448b1
JK
3284 gdb_assert (state->canonical_names[i].suffix != NULL);
3285 make_cleanup (xfree, state->canonical_names[i].suffix);
f8eba3c6
TT
3286 }
3287 }
3288
3289 if (select_mode == NULL)
3290 {
112e8700 3291 if (interp_ui_out (top_level_interpreter ())->is_mi_like_p ())
f8eba3c6
TT
3292 select_mode = multiple_symbols_all;
3293 else
3294 select_mode = multiple_symbols_select_mode ();
3295 }
3296
3297 if (select_mode == multiple_symbols_all)
3298 {
3299 if (filter != NULL)
3300 {
3301 make_cleanup (VEC_cleanup (const_char_ptr), &filters);
3302 VEC_safe_push (const_char_ptr, filters, filter);
40e084e1 3303 filter_results (state, &result, filters);
f8eba3c6
TT
3304 }
3305 else
40e084e1 3306 convert_results_to_lsals (state, &result);
f8eba3c6
TT
3307 }
3308 else
40e084e1 3309 decode_line_2 (state, &result, select_mode);
f8eba3c6
TT
3310
3311 do_cleanups (cleanups);
3312}
3313
39cf75f7
DE
3314/* See linespec.h. */
3315
6c5b2ebe 3316std::vector<symtab_and_line>
f00aae0f 3317decode_line_1 (const struct event_location *location, int flags,
c2f4122d 3318 struct program_space *search_pspace,
f8eba3c6
TT
3319 struct symtab *default_symtab,
3320 int default_line)
3321{
40e084e1 3322 linespec_parser parser;
f8eba3c6
TT
3323 struct cleanup *cleanups;
3324
c2f4122d
PA
3325 linespec_parser_new (&parser, flags, current_language,
3326 search_pspace, default_symtab,
40e084e1
KS
3327 default_line, NULL);
3328 cleanups = make_cleanup (linespec_parser_delete, &parser);
5ed8105e
PA
3329
3330 scoped_restore_current_program_space restore_pspace;
f8eba3c6 3331
6c5b2ebe
PA
3332 std::vector<symtab_and_line> result = event_location_to_sals (&parser,
3333 location);
40e084e1 3334
f8eba3c6
TT
3335 do_cleanups (cleanups);
3336 return result;
3337}
3338
39cf75f7
DE
3339/* See linespec.h. */
3340
6c5b2ebe 3341std::vector<symtab_and_line>
f2fc3015 3342decode_line_with_current_source (const char *string, int flags)
39cf75f7 3343{
39cf75f7
DE
3344 if (string == 0)
3345 error (_("Empty line specification."));
3346
3347 /* We use whatever is set as the current source line. We do not try
3348 and get a default source symtab+line or it will recursively call us! */
6c5b2ebe 3349 symtab_and_line cursal = get_current_source_symtab_and_line ();
39cf75f7 3350
ffc2605c
TT
3351 event_location_up location = string_to_event_location (&string,
3352 current_language);
6c5b2ebe
PA
3353 std::vector<symtab_and_line> sals
3354 = decode_line_1 (location.get (), flags, NULL, cursal.symtab, cursal.line);
39cf75f7
DE
3355
3356 if (*string)
3357 error (_("Junk at end of line specification: %s"), string);
f00aae0f 3358
39cf75f7
DE
3359 return sals;
3360}
3361
3362/* See linespec.h. */
3363
6c5b2ebe 3364std::vector<symtab_and_line>
f2fc3015 3365decode_line_with_last_displayed (const char *string, int flags)
39cf75f7 3366{
39cf75f7
DE
3367 if (string == 0)
3368 error (_("Empty line specification."));
3369
ffc2605c
TT
3370 event_location_up location = string_to_event_location (&string,
3371 current_language);
6c5b2ebe
PA
3372 std::vector<symtab_and_line> sals
3373 = (last_displayed_sal_is_valid ()
3374 ? decode_line_1 (location.get (), flags, NULL,
3375 get_last_displayed_symtab (),
3376 get_last_displayed_line ())
3377 : decode_line_1 (location.get (), flags, NULL,
3378 (struct symtab *) NULL, 0));
39cf75f7
DE
3379
3380 if (*string)
3381 error (_("Junk at end of line specification: %s"), string);
f00aae0f 3382
39cf75f7
DE
3383 return sals;
3384}
3385
f8eba3c6
TT
3386\f
3387
3388/* First, some functions to initialize stuff at the beggining of the
3389 function. */
3390
3391static void
3392initialize_defaults (struct symtab **default_symtab, int *default_line)
3393{
3394 if (*default_symtab == 0)
3395 {
3396 /* Use whatever we have for the default source line. We don't use
3397 get_current_or_default_symtab_and_line as it can recurse and call
3398 us back! */
3399 struct symtab_and_line cursal =
3400 get_current_source_symtab_and_line ();
3401
3402 *default_symtab = cursal.symtab;
3403 *default_line = cursal.line;
3404 }
3405}
3406
3407\f
3408
40e084e1
KS
3409/* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
3410 advancing EXP_PTR past any parsed text. */
f8eba3c6 3411
a06efdd6 3412CORE_ADDR
bbc13ae3 3413linespec_expression_to_pc (const char **exp_ptr)
f8eba3c6 3414{
f8eba3c6
TT
3415 if (current_program_space->executing_startup)
3416 /* The error message doesn't really matter, because this case
3417 should only hit during breakpoint reset. */
3418 throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
3419 "program space is in startup"));
3420
40e084e1
KS
3421 (*exp_ptr)++;
3422 return value_as_address (parse_to_comma_and_eval (exp_ptr));
0960f083
DC
3423}
3424
3425\f
3426
d2630e69
AF
3427/* Here's where we recognise an Objective-C Selector. An Objective C
3428 selector may be implemented by more than one class, therefore it
3429 may represent more than one method/function. This gives us a
3430 situation somewhat analogous to C++ overloading. If there's more
3431 than one method that could represent the selector, then use some of
3432 the existing C++ code to let the user choose one. */
3433
6c5b2ebe 3434static std::vector<symtab_and_line>
f00aae0f 3435decode_objc (struct linespec_state *self, linespec_p ls, const char *arg)
d2630e69 3436{
f8eba3c6
TT
3437 struct collect_info info;
3438 VEC (const_char_ptr) *symbol_names = NULL;
d7561cbb 3439 const char *new_argptr;
f8eba3c6
TT
3440 struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
3441 &symbol_names);
3442
3443 info.state = self;
40e084e1 3444 info.file_symtabs = NULL;
ec94af83
DE
3445 VEC_safe_push (symtab_ptr, info.file_symtabs, NULL);
3446 make_cleanup (VEC_cleanup (symtab_ptr), &info.file_symtabs);
40e084e1
KS
3447 info.result.symbols = NULL;
3448 info.result.minimal_symbols = NULL;
f8eba3c6 3449
f00aae0f 3450 new_argptr = find_imps (arg, &symbol_names);
f8eba3c6
TT
3451 if (VEC_empty (const_char_ptr, symbol_names))
3452 {
3453 do_cleanups (cleanup);
6c5b2ebe 3454 return {};
f8eba3c6 3455 }
d2630e69 3456
f8eba3c6 3457 add_all_symbol_names_from_pspace (&info, NULL, symbol_names);
d2630e69 3458
6c5b2ebe 3459 std::vector<symtab_and_line> values;
40e084e1 3460 if (!VEC_empty (symbolp, info.result.symbols)
f60e2d5c 3461 || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
d2630e69 3462 {
f8eba3c6 3463 char *saved_arg;
d2630e69 3464
224c3ddb 3465 saved_arg = (char *) alloca (new_argptr - arg + 1);
f00aae0f
KS
3466 memcpy (saved_arg, arg, new_argptr - arg);
3467 saved_arg[new_argptr - arg] = '\0';
d2630e69 3468
67994074 3469 ls->explicit_loc.function_name = xstrdup (saved_arg);
40e084e1
KS
3470 ls->function_symbols = info.result.symbols;
3471 ls->minimal_symbols = info.result.minimal_symbols;
3472 values = convert_linespec_to_sals (self, ls);
3473
f8eba3c6 3474 if (self->canonical)
d2630e69 3475 {
f2fc3015
TT
3476 std::string holder;
3477 const char *str;
f00aae0f 3478
f8eba3c6 3479 self->canonical->pre_expanded = 1;
f00aae0f 3480
67994074 3481 if (ls->explicit_loc.source_filename)
f00aae0f 3482 {
f2fc3015
TT
3483 holder = string_printf ("%s:%s",
3484 ls->explicit_loc.source_filename,
3485 saved_arg);
3486 str = holder.c_str ();
f00aae0f 3487 }
f8eba3c6 3488 else
f2fc3015 3489 str = saved_arg;
f00aae0f 3490
8e9e35b1 3491 self->canonical->location = new_linespec_location (&str);
d2630e69 3492 }
d2630e69
AF
3493 }
3494
f8eba3c6 3495 do_cleanups (cleanup);
c00f8484 3496
40e084e1 3497 return values;
f8eba3c6 3498}
c00f8484 3499
ffdbe864
YQ
3500namespace {
3501
14bc53a8
PA
3502/* A function object that serves as symbol_found_callback_ftype
3503 callback for iterate_over_symbols. This is used by
3504 lookup_prefix_sym to collect type symbols. */
3505class decode_compound_collector
f8eba3c6 3506{
14bc53a8 3507public:
fc4007c9 3508 decode_compound_collector ()
14bc53a8 3509 : m_symbols (NULL)
fc4007c9 3510 {
14bc53a8
PA
3511 m_unique_syms = htab_create_alloc (1, htab_hash_pointer,
3512 htab_eq_pointer, NULL,
3513 xcalloc, xfree);
fc4007c9
TT
3514 }
3515
3516 ~decode_compound_collector ()
3517 {
14bc53a8
PA
3518 if (m_unique_syms != NULL)
3519 htab_delete (m_unique_syms);
fc4007c9 3520 }
3a93a0c2 3521
14bc53a8
PA
3522 /* Releases ownership of the collected symbols and returns them. */
3523 VEC (symbolp) *release_symbols ()
3524 {
3525 VEC (symbolp) *res = m_symbols;
3526 m_symbols = NULL;
3527 return res;
3528 }
c00f8484 3529
14bc53a8
PA
3530 /* Callable as a symbol_found_callback_ftype callback. */
3531 bool operator () (symbol *sym);
3532
3533private:
3534 /* A hash table of all symbols we found. We use this to avoid
3535 adding any symbol more than once. */
3536 htab_t m_unique_syms;
3537
3538 /* The result vector. */
3539 VEC (symbolp) *m_symbols;
3540};
3541
3542bool
3543decode_compound_collector::operator () (symbol *sym)
f8eba3c6 3544{
f8eba3c6
TT
3545 void **slot;
3546 struct type *t;
614b3b14 3547
f8eba3c6 3548 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
14bc53a8 3549 return true; /* Continue iterating. */
f8eba3c6
TT
3550
3551 t = SYMBOL_TYPE (sym);
f168693b 3552 t = check_typedef (t);
f8eba3c6
TT
3553 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
3554 && TYPE_CODE (t) != TYPE_CODE_UNION
3555 && TYPE_CODE (t) != TYPE_CODE_NAMESPACE)
14bc53a8 3556 return true; /* Continue iterating. */
614b3b14 3557
14bc53a8 3558 slot = htab_find_slot (m_unique_syms, sym, INSERT);
f8eba3c6
TT
3559 if (!*slot)
3560 {
3561 *slot = sym;
14bc53a8 3562 VEC_safe_push (symbolp, m_symbols, sym);
f8eba3c6
TT
3563 }
3564
14bc53a8 3565 return true; /* Continue iterating. */
f8eba3c6 3566}
93d91629 3567
ffdbe864
YQ
3568} // namespace
3569
40e084e1 3570/* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
93d91629 3571
f8eba3c6 3572static VEC (symbolp) *
ec94af83 3573lookup_prefix_sym (struct linespec_state *state, VEC (symtab_ptr) *file_symtabs,
40e084e1 3574 const char *class_name)
93d91629 3575{
f8eba3c6
TT
3576 int ix;
3577 struct symtab *elt;
14bc53a8 3578 decode_compound_collector collector;
e0881a8e 3579
b5ec771e
PA
3580 lookup_name_info lookup_name (class_name, symbol_name_match_type::FULL);
3581
ec94af83 3582 for (ix = 0; VEC_iterate (symtab_ptr, file_symtabs, ix, elt); ++ix)
f8eba3c6
TT
3583 {
3584 if (elt == NULL)
3585 {
b5ec771e
PA
3586 iterate_over_all_matching_symtabs (state, lookup_name,
3587 STRUCT_DOMAIN, NULL, false,
3588 collector);
3589 iterate_over_all_matching_symtabs (state, lookup_name,
3590 VAR_DOMAIN, NULL, false,
3591 collector);
f8eba3c6
TT
3592 }
3593 else
3594 {
f8eba3c6
TT
3595 /* Program spaces that are executing startup should have
3596 been filtered out earlier. */
3597 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
3598 set_current_program_space (SYMTAB_PSPACE (elt));
b5ec771e
PA
3599 iterate_over_file_blocks (elt, lookup_name, STRUCT_DOMAIN, collector);
3600 iterate_over_file_blocks (elt, lookup_name, VAR_DOMAIN, collector);
1e5a1abc
KS
3601 }
3602 }
3603
14bc53a8 3604 return collector.release_symbols ();
93d91629
DC
3605}
3606
40e084e1
KS
3607/* A qsort comparison function for symbols. The resulting order does
3608 not actually matter; we just need to be able to sort them so that
3609 symbols with the same program space end up next to each other. */
3610
3611static int
3612compare_symbols (const void *a, const void *b)
3613{
9a3c8263
SM
3614 struct symbol * const *sa = (struct symbol * const*) a;
3615 struct symbol * const *sb = (struct symbol * const*) b;
40e084e1
KS
3616 uintptr_t uia, uib;
3617
08be3fe3
DE
3618 uia = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sa));
3619 uib = (uintptr_t) SYMTAB_PSPACE (symbol_symtab (*sb));
40e084e1
KS
3620
3621 if (uia < uib)
3622 return -1;
3623 if (uia > uib)
3624 return 1;
3625
3626 uia = (uintptr_t) *sa;
3627 uib = (uintptr_t) *sb;
3628
3629 if (uia < uib)
3630 return -1;
3631 if (uia > uib)
3632 return 1;
3633
3634 return 0;
3635}
3636
3637/* Like compare_symbols but for minimal symbols. */
4224873a 3638
f8eba3c6 3639static int
40e084e1 3640compare_msymbols (const void *a, const void *b)
4224873a 3641{
9a3c8263
SM
3642 const struct bound_minimal_symbol *sa
3643 = (const struct bound_minimal_symbol *) a;
3644 const struct bound_minimal_symbol *sb
3645 = (const struct bound_minimal_symbol *) b;
f8eba3c6
TT
3646 uintptr_t uia, uib;
3647
001822aa
TT
3648 uia = (uintptr_t) sa->objfile->pspace;
3649 uib = (uintptr_t) sa->objfile->pspace;
f8eba3c6
TT
3650
3651 if (uia < uib)
3652 return -1;
3653 if (uia > uib)
3654 return 1;
3655
001822aa
TT
3656 uia = (uintptr_t) sa->minsym;
3657 uib = (uintptr_t) sb->minsym;
f8eba3c6
TT
3658
3659 if (uia < uib)
3660 return -1;
3661 if (uia > uib)
3662 return 1;
3663
3664 return 0;
3665}
3666
3667/* Look for all the matching instances of each symbol in NAMES. Only
3668 instances from PSPACE are considered; other program spaces are
3669 handled by our caller. If PSPACE is NULL, then all program spaces
3670 are considered. Results are stored into INFO. */
3671
3672static void
3673add_all_symbol_names_from_pspace (struct collect_info *info,
3674 struct program_space *pspace,
3675 VEC (const_char_ptr) *names)
3676{
3677 int ix;
3678 const char *iter;
3679
3680 for (ix = 0; VEC_iterate (const_char_ptr, names, ix, iter); ++ix)
b5ec771e
PA
3681 add_matching_symbols_to_info (iter,
3682 symbol_name_match_type::FULL,
3683 info, pspace);
f8eba3c6
TT
3684}
3685
3686static void
3687find_superclass_methods (VEC (typep) *superclasses,
b5ec771e 3688 const char *name, enum language name_lang,
f8eba3c6
TT
3689 VEC (const_char_ptr) **result_names)
3690{
3691 int old_len = VEC_length (const_char_ptr, *result_names);
3692 VEC (typep) *iter_classes;
3693 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
3694
3695 iter_classes = superclasses;
3696 while (1)
3697 {
3698 VEC (typep) *new_supers = NULL;
3699 int ix;
3700 struct type *t;
3701
3702 make_cleanup (VEC_cleanup (typep), &new_supers);
3703 for (ix = 0; VEC_iterate (typep, iter_classes, ix, t); ++ix)
b5ec771e 3704 find_methods (t, name_lang, name, result_names, &new_supers);
f8eba3c6
TT
3705
3706 if (VEC_length (const_char_ptr, *result_names) != old_len
3707 || VEC_empty (typep, new_supers))
3708 break;
4224873a 3709
f8eba3c6
TT
3710 iter_classes = new_supers;
3711 }
4224873a 3712
f8eba3c6
TT
3713 do_cleanups (cleanup);
3714}
3715
40e084e1
KS
3716/* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3717 given by one of the symbols in SYM_CLASSES. Matches are returned
3718 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
f8eba3c6 3719
40e084e1 3720static void
ec94af83 3721find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
40e084e1
KS
3722 const char *class_name, const char *method_name,
3723 VEC (symbolp) *sym_classes, VEC (symbolp) **symbols,
f60e2d5c 3724 VEC (bound_minimal_symbol_d) **minsyms)
f8eba3c6 3725{
f8eba3c6
TT
3726 struct symbol *sym;
3727 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
3728 int ix;
3729 int last_result_len;
3730 VEC (typep) *superclass_vec;
3731 VEC (const_char_ptr) *result_names;
3732 struct collect_info info;
4224873a 3733
f8eba3c6
TT
3734 /* Sort symbols so that symbols with the same program space are next
3735 to each other. */
3736 qsort (VEC_address (symbolp, sym_classes),
3737 VEC_length (symbolp, sym_classes),
3738 sizeof (symbolp),
3739 compare_symbols);
3740
3741 info.state = self;
40e084e1
KS
3742 info.file_symtabs = file_symtabs;
3743 info.result.symbols = NULL;
3744 info.result.minimal_symbols = NULL;
f8eba3c6
TT
3745
3746 /* Iterate over all the types, looking for the names of existing
40e084e1 3747 methods matching METHOD_NAME. If we cannot find a direct method in a
f8eba3c6
TT
3748 given program space, then we consider inherited methods; this is
3749 not ideal (ideal would be to respect C++ hiding rules), but it
3750 seems good enough and is what GDB has historically done. We only
3751 need to collect the names because later we find all symbols with
3752 those names. This loop is written in a somewhat funny way
3753 because we collect data across the program space before deciding
3754 what to do. */
3755 superclass_vec = NULL;
3756 make_cleanup (VEC_cleanup (typep), &superclass_vec);
3757 result_names = NULL;
3758 make_cleanup (VEC_cleanup (const_char_ptr), &result_names);
3759 last_result_len = 0;
3760 for (ix = 0; VEC_iterate (symbolp, sym_classes, ix, sym); ++ix)
3761 {
3762 struct type *t;
3763 struct program_space *pspace;
3764
3765 /* Program spaces that are executing startup should have
3766 been filtered out earlier. */
08be3fe3
DE
3767 pspace = SYMTAB_PSPACE (symbol_symtab (sym));
3768 gdb_assert (!pspace->executing_startup);
f8eba3c6
TT
3769 set_current_program_space (pspace);
3770 t = check_typedef (SYMBOL_TYPE (sym));
b5ec771e
PA
3771 find_methods (t, SYMBOL_LANGUAGE (sym),
3772 method_name, &result_names, &superclass_vec);
f8eba3c6
TT
3773
3774 /* Handle all items from a single program space at once; and be
3775 sure not to miss the last batch. */
3776 if (ix == VEC_length (symbolp, sym_classes) - 1
3777 || (pspace
08be3fe3 3778 != SYMTAB_PSPACE (symbol_symtab (VEC_index (symbolp, sym_classes,
f8eba3c6 3779 ix + 1)))))
4224873a 3780 {
f8eba3c6
TT
3781 /* If we did not find a direct implementation anywhere in
3782 this program space, consider superclasses. */
3783 if (VEC_length (const_char_ptr, result_names) == last_result_len)
40e084e1 3784 find_superclass_methods (superclass_vec, method_name,
b5ec771e 3785 SYMBOL_LANGUAGE (sym), &result_names);
f8eba3c6
TT
3786
3787 /* We have a list of candidate symbol names, so now we
3788 iterate over the symbol tables looking for all
3789 matches in this pspace. */
3790 add_all_symbol_names_from_pspace (&info, pspace, result_names);
3791
3792 VEC_truncate (typep, superclass_vec, 0);
3793 last_result_len = VEC_length (const_char_ptr, result_names);
4224873a 3794 }
4224873a 3795 }
f8eba3c6 3796
40e084e1 3797 if (!VEC_empty (symbolp, info.result.symbols)
f60e2d5c 3798 || !VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
4224873a 3799 {
40e084e1
KS
3800 *symbols = info.result.symbols;
3801 *minsyms = info.result.minimal_symbols;
f8eba3c6 3802 do_cleanups (cleanup);
40e084e1 3803 return;
4224873a 3804 }
f8eba3c6 3805
40e084e1
KS
3806 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3807 and other attempts to locate the symbol will be made. */
3808 throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
f8eba3c6
TT
3809}
3810
3811\f
3812
ffdbe864
YQ
3813namespace {
3814
14bc53a8
PA
3815/* This function object is a callback for iterate_over_symtabs, used
3816 when collecting all matching symtabs. */
f8eba3c6 3817
14bc53a8 3818class symtab_collector
f8eba3c6 3819{
14bc53a8 3820public:
fc4007c9 3821 symtab_collector ()
fc4007c9 3822 {
14bc53a8
PA
3823 m_symtabs = NULL;
3824 m_symtab_table = htab_create (1, htab_hash_pointer, htab_eq_pointer,
3825 NULL);
fc4007c9
TT
3826 }
3827
3828 ~symtab_collector ()
3829 {
14bc53a8
PA
3830 if (m_symtab_table != NULL)
3831 htab_delete (m_symtab_table);
fc4007c9 3832 }
f8eba3c6 3833
14bc53a8
PA
3834 /* Callable as a symbol_found_callback_ftype callback. */
3835 bool operator () (symtab *sym);
f8eba3c6 3836
14bc53a8
PA
3837 /* Releases ownership of the collected symtabs and returns them. */
3838 VEC (symtab_ptr) *release_symtabs ()
3839 {
3840 VEC (symtab_ptr) *res = m_symtabs;
3841 m_symtabs = NULL;
3842 return res;
3843 }
3844
3845private:
3846 /* The result vector of symtabs. */
3847 VEC (symtab_ptr) *m_symtabs;
3848
3849 /* This is used to ensure the symtabs are unique. */
3850 htab_t m_symtab_table;
3851};
3852
3853bool
3854symtab_collector::operator () (struct symtab *symtab)
f8eba3c6 3855{
f8eba3c6
TT
3856 void **slot;
3857
14bc53a8 3858 slot = htab_find_slot (m_symtab_table, symtab, INSERT);
f8eba3c6 3859 if (!*slot)
4224873a 3860 {
f8eba3c6 3861 *slot = symtab;
14bc53a8 3862 VEC_safe_push (symtab_ptr, m_symtabs, symtab);
4224873a 3863 }
f8eba3c6 3864
14bc53a8 3865 return false;
4224873a
DC
3866}
3867
ffdbe864
YQ
3868} // namespace
3869
c2f4122d
PA
3870/* Given a file name, return a VEC of all matching symtabs. If
3871 SEARCH_PSPACE is not NULL, the search is restricted to just that
3872 program space. */
f8eba3c6 3873
ec94af83 3874static VEC (symtab_ptr) *
c2f4122d
PA
3875collect_symtabs_from_filename (const char *file,
3876 struct program_space *search_pspace)
f8eba3c6 3877{
14bc53a8 3878 symtab_collector collector;
f8eba3c6
TT
3879
3880 /* Find that file's data. */
c2f4122d
PA
3881 if (search_pspace == NULL)
3882 {
14bc53a8
PA
3883 struct program_space *pspace;
3884
c2f4122d
PA
3885 ALL_PSPACES (pspace)
3886 {
3887 if (pspace->executing_startup)
3888 continue;
f8eba3c6 3889
c2f4122d 3890 set_current_program_space (pspace);
14bc53a8 3891 iterate_over_symtabs (file, collector);
c2f4122d
PA
3892 }
3893 }
3894 else
3895 {
3896 set_current_program_space (search_pspace);
14bc53a8 3897 iterate_over_symtabs (file, collector);
c2f4122d 3898 }
f3c39e76 3899
14bc53a8 3900 return collector.release_symtabs ();
f8eba3c6
TT
3901}
3902
c2f4122d
PA
3903/* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3904 not NULL, the search is restricted to just that program space. */
f8eba3c6 3905
ec94af83 3906static VEC (symtab_ptr) *
c2f4122d
PA
3907symtabs_from_filename (const char *filename,
3908 struct program_space *search_pspace)
40e084e1 3909{
ec94af83 3910 VEC (symtab_ptr) *result;
40e084e1 3911
c2f4122d 3912 result = collect_symtabs_from_filename (filename, search_pspace);
f8eba3c6 3913
ec94af83 3914 if (VEC_empty (symtab_ptr, result))
f8eba3c6 3915 {
40e084e1
KS
3916 if (!have_full_symbols () && !have_partial_symbols ())
3917 throw_error (NOT_FOUND_ERROR,
3918 _("No symbol table is loaded. "
3919 "Use the \"file\" command."));
00e52e53 3920 source_file_not_found_error (filename);
f8eba3c6
TT
3921 }
3922
40e084e1 3923 return result;
84fba31b 3924}
f3c39e76 3925
40e084e1
KS
3926/* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3927 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3928 returned in MINSYMS. */
14e91ac5 3929
40e084e1
KS
3930static void
3931find_function_symbols (struct linespec_state *state,
ec94af83 3932 VEC (symtab_ptr) *file_symtabs, const char *name,
40e084e1 3933 VEC (symbolp) **symbols,
f60e2d5c 3934 VEC (bound_minimal_symbol_d) **minsyms)
14e91ac5 3935{
40e084e1
KS
3936 struct collect_info info;
3937 VEC (const_char_ptr) *symbol_names = NULL;
3938 struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
3939 &symbol_names);
14e91ac5 3940
40e084e1
KS
3941 info.state = state;
3942 info.result.symbols = NULL;
3943 info.result.minimal_symbols = NULL;
3944 info.file_symtabs = file_symtabs;
e0881a8e 3945
40e084e1 3946 /* Try NAME as an Objective-C selector. */
d7561cbb 3947 find_imps (name, &symbol_names);
40e084e1 3948 if (!VEC_empty (const_char_ptr, symbol_names))
c2f4122d
PA
3949 add_all_symbol_names_from_pspace (&info, state->search_pspace,
3950 symbol_names);
40e084e1 3951 else
b5ec771e
PA
3952 add_matching_symbols_to_info (name, symbol_name_match_type::WILD,
3953 &info, state->search_pspace);
40e084e1
KS
3954
3955 do_cleanups (cleanup);
3956
3957 if (VEC_empty (symbolp, info.result.symbols))
3958 {
3959 VEC_free (symbolp, info.result.symbols);
3960 *symbols = NULL;
14e91ac5
DC
3961 }
3962 else
40e084e1
KS
3963 *symbols = info.result.symbols;
3964
f60e2d5c 3965 if (VEC_empty (bound_minimal_symbol_d, info.result.minimal_symbols))
14e91ac5 3966 {
f60e2d5c 3967 VEC_free (bound_minimal_symbol_d, info.result.minimal_symbols);
40e084e1
KS
3968 *minsyms = NULL;
3969 }
3970 else
3971 *minsyms = info.result.minimal_symbols;
3972}
3973
3974/* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3975 in SYMBOLS and minimal symbols in MINSYMS. */
14e91ac5 3976
b1ae631a 3977static void
40e084e1 3978find_linespec_symbols (struct linespec_state *state,
ec94af83 3979 VEC (symtab_ptr) *file_symtabs,
b5ec771e 3980 const char *lookup_name,
40e084e1 3981 VEC (symbolp) **symbols,
f60e2d5c 3982 VEC (bound_minimal_symbol_d) **minsyms)
40e084e1 3983{
2f408ecb
PA
3984 std::string canon = cp_canonicalize_string_no_typedefs (lookup_name);
3985 if (!canon.empty ())
3986 lookup_name = canon.c_str ();
3987
cc81e1c6
DE
3988 /* It's important to not call expand_symtabs_matching unnecessarily
3989 as it can really slow things down (by unnecessarily expanding
3990 potentially 1000s of symtabs, which when debugging some apps can
3991 cost 100s of seconds). Avoid this to some extent by *first* calling
3992 find_function_symbols, and only if that doesn't find anything
3993 *then* call find_method. This handles two important cases:
3994 1) break (anonymous namespace)::foo
3995 2) break class::method where method is in class (and not a baseclass) */
14e91ac5 3996
cc81e1c6
DE
3997 find_function_symbols (state, file_symtabs, lookup_name,
3998 symbols, minsyms);
14e91ac5 3999
cc81e1c6
DE
4000 /* If we were unable to locate a symbol of the same name, try dividing
4001 the name into class and method names and searching the class and its
4002 baseclasses. */
4003 if (VEC_empty (symbolp, *symbols)
f60e2d5c 4004 && VEC_empty (bound_minimal_symbol_d, *minsyms))
40e084e1 4005 {
2f408ecb 4006 std::string klass, method;
cc81e1c6
DE
4007 const char *last, *p, *scope_op;
4008 VEC (symbolp) *classes;
14e91ac5 4009
cc81e1c6
DE
4010 /* See if we can find a scope operator and break this symbol
4011 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
4012 scope_op = "::";
4013 p = find_toplevel_string (lookup_name, scope_op);
14e91ac5 4014
cc81e1c6
DE
4015 last = NULL;
4016 while (p != NULL)
f8eba3c6 4017 {
cc81e1c6
DE
4018 last = p;
4019 p = find_toplevel_string (p + strlen (scope_op), scope_op);
f8eba3c6 4020 }
14e91ac5 4021
cc81e1c6
DE
4022 /* If no scope operator was found, there is nothing more we can do;
4023 we already attempted to lookup the entire name as a symbol
4024 and failed. */
4025 if (last == NULL)
2f408ecb 4026 return;
cc81e1c6
DE
4027
4028 /* LOOKUP_NAME points to the class name.
4029 LAST points to the method name. */
2f408ecb 4030 klass = std::string (lookup_name, last - lookup_name);
cc81e1c6
DE
4031
4032 /* Skip past the scope operator. */
4033 last += strlen (scope_op);
2f408ecb 4034 method = last;
cc81e1c6
DE
4035
4036 /* Find a list of classes named KLASS. */
2f408ecb
PA
4037 classes = lookup_prefix_sym (state, file_symtabs, klass.c_str ());
4038 struct cleanup *old_chain
4039 = make_cleanup (VEC_cleanup (symbolp), &classes);
cc81e1c6
DE
4040
4041 if (!VEC_empty (symbolp, classes))
4042 {
4043 /* Now locate a list of suitable methods named METHOD. */
492d29ea 4044 TRY
cc81e1c6 4045 {
2f408ecb
PA
4046 find_method (state, file_symtabs,
4047 klass.c_str (), method.c_str (),
4048 classes, symbols, minsyms);
cc81e1c6
DE
4049 }
4050
4051 /* If successful, we're done. If NOT_FOUND_ERROR
4052 was not thrown, rethrow the exception that we did get. */
492d29ea 4053 CATCH (except, RETURN_MASK_ERROR)
7556d4a4
PA
4054 {
4055 if (except.error != NOT_FOUND_ERROR)
4056 throw_exception (except);
4057 }
492d29ea 4058 END_CATCH
cc81e1c6 4059 }
14e91ac5 4060
2f408ecb
PA
4061 do_cleanups (old_chain);
4062 }
14e91ac5
DC
4063}
4064
a2459270
PA
4065/* Helper for find_label_symbols. Find all labels that match name
4066 NAME in BLOCK. Return all labels that match in FUNCTION_SYMBOLS.
4067 Return the actual function symbol in which the label was found in
4068 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
4069 interpreted as a label name prefix. Otherwise, only a label named
4070 exactly NAME match. */
4071
4072static void
4073find_label_symbols_in_block (const struct block *block,
4074 const char *name, struct symbol *fn_sym,
4075 bool completion_mode,
4076 VEC (symbolp) **result,
4077 VEC (symbolp) **label_funcs_ret)
4078{
4079 if (completion_mode)
4080 {
4081 struct block_iterator iter;
4082 struct symbol *sym;
4083 size_t name_len = strlen (name);
4084
4085 int (*cmp) (const char *, const char *, size_t);
4086 cmp = case_sensitivity == case_sensitive_on ? strncmp : strncasecmp;
4087
4088 ALL_BLOCK_SYMBOLS (block, iter, sym)
4089 {
4090 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
4091 SYMBOL_DOMAIN (sym), LABEL_DOMAIN)
4092 && cmp (SYMBOL_SEARCH_NAME (sym), name, name_len) == 0)
4093 {
4094 VEC_safe_push (symbolp, *result, sym);
4095 VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
4096 }
4097 }
4098 }
4099 else
4100 {
4101 struct symbol *sym = lookup_symbol (name, block, LABEL_DOMAIN, 0).symbol;
4102
4103 if (sym != NULL)
4104 {
4105 VEC_safe_push (symbolp, *result, sym);
4106 VEC_safe_push (symbolp, *label_funcs_ret, fn_sym);
4107 }
4108 }
4109}
4110
4111/* Return all labels that match name NAME in FUNCTION_SYMBOLS. Return
4112 the actual function symbol in which the label was found in
4113 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
4114 interpreted as a label name prefix. Otherwise, only labels named
4115 exactly NAME match. */
0f5238ed 4116
40e084e1
KS
4117static VEC (symbolp) *
4118find_label_symbols (struct linespec_state *self,
4119 VEC (symbolp) *function_symbols,
a2459270
PA
4120 VEC (symbolp) **label_funcs_ret, const char *name,
4121 bool completion_mode)
0f5238ed 4122{
f8eba3c6 4123 int ix;
3977b71f 4124 const struct block *block;
40e084e1
KS
4125 struct symbol *sym;
4126 struct symbol *fn_sym;
4127 VEC (symbolp) *result = NULL;
9ef07c8c 4128
f8eba3c6 4129 if (function_symbols == NULL)
9ef07c8c 4130 {
f8eba3c6 4131 set_current_program_space (self->program_space);
4eeaa230 4132 block = get_current_search_block ();
f8eba3c6 4133
9ef07c8c
TT
4134 for (;
4135 block && !BLOCK_FUNCTION (block);
4136 block = BLOCK_SUPERBLOCK (block))
4137 ;
4138 if (!block)
40e084e1 4139 return NULL;
f8eba3c6
TT
4140 fn_sym = BLOCK_FUNCTION (block);
4141
a2459270
PA
4142 find_label_symbols_in_block (block, name, fn_sym, completion_mode,
4143 &result, label_funcs_ret);
40e084e1
KS
4144 }
4145 else
4146 {
4147 for (ix = 0;
4148 VEC_iterate (symbolp, function_symbols, ix, fn_sym); ++ix)
f8eba3c6 4149 {
08be3fe3 4150 set_current_program_space (SYMTAB_PSPACE (symbol_symtab (fn_sym)));
40e084e1 4151 block = SYMBOL_BLOCK_VALUE (fn_sym);
40e084e1 4152
a2459270
PA
4153 find_label_symbols_in_block (block, name, fn_sym, completion_mode,
4154 &result, label_funcs_ret);
f8eba3c6 4155 }
40e084e1 4156 }
f8eba3c6 4157
40e084e1
KS
4158 return result;
4159}
f8eba3c6 4160
40e084e1
KS
4161\f
4162
4163/* A helper for create_sals_line_offset that handles the 'list_mode' case. */
4164
6c5b2ebe 4165static std::vector<symtab_and_line>
40e084e1
KS
4166decode_digits_list_mode (struct linespec_state *self,
4167 linespec_p ls,
40e084e1
KS
4168 struct symtab_and_line val)
4169{
4170 int ix;
4171 struct symtab *elt;
4172
4173 gdb_assert (self->list_mode);
4174
6c5b2ebe
PA
4175 std::vector<symtab_and_line> values;
4176
ec94af83 4177 for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt);
40e084e1
KS
4178 ++ix)
4179 {
4180 /* The logic above should ensure this. */
4181 gdb_assert (elt != NULL);
4182
4183 set_current_program_space (SYMTAB_PSPACE (elt));
4184
4185 /* Simplistic search just for the list command. */
4186 val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
4187 if (val.symtab == NULL)
4188 val.symtab = elt;
4189 val.pspace = SYMTAB_PSPACE (elt);
4190 val.pc = 0;
4191 val.explicit_line = 1;
4192
6c5b2ebe 4193 add_sal_to_sals (self, &values, &val, NULL, 0);
f8eba3c6 4194 }
6c5b2ebe
PA
4195
4196 return values;
40e084e1 4197}
f8eba3c6 4198
40e084e1
KS
4199/* A helper for create_sals_line_offset that iterates over the symtabs,
4200 adding lines to the VEC. */
4201
6c5b2ebe 4202static std::vector<symtab_and_line>
40e084e1
KS
4203decode_digits_ordinary (struct linespec_state *self,
4204 linespec_p ls,
4205 int line,
40e084e1
KS
4206 struct linetable_entry **best_entry)
4207{
4208 int ix;
4209 struct symtab *elt;
f8eba3c6 4210
6c5b2ebe 4211 std::vector<symtab_and_line> sals;
ec94af83 4212 for (ix = 0; VEC_iterate (symtab_ptr, ls->file_symtabs, ix, elt); ++ix)
f8eba3c6 4213 {
67d89901 4214 std::vector<CORE_ADDR> pcs;
40e084e1
KS
4215
4216 /* The logic above should ensure this. */
4217 gdb_assert (elt != NULL);
f8eba3c6 4218
40e084e1 4219 set_current_program_space (SYMTAB_PSPACE (elt));
f8eba3c6 4220
40e084e1 4221 pcs = find_pcs_for_symtab_line (elt, line, best_entry);
67d89901 4222 for (CORE_ADDR pc : pcs)
f8eba3c6 4223 {
51abb421 4224 symtab_and_line sal;
40e084e1
KS
4225 sal.pspace = SYMTAB_PSPACE (elt);
4226 sal.symtab = elt;
4227 sal.line = line;
4228 sal.pc = pc;
6c5b2ebe 4229 sals.push_back (std::move (sal));
f8eba3c6
TT
4230 }
4231 }
6c5b2ebe
PA
4232
4233 return sals;
40e084e1
KS
4234}
4235
4236\f
4237
4238/* Return the line offset represented by VARIABLE. */
4239
4240static struct line_offset
4241linespec_parse_variable (struct linespec_state *self, const char *variable)
4242{
4243 int index = 0;
4244 const char *p;
4245 struct line_offset offset = {0, LINE_OFFSET_NONE};
f8eba3c6 4246
40e084e1
KS
4247 p = (variable[1] == '$') ? variable + 2 : variable + 1;
4248 if (*p == '$')
4249 ++p;
4250 while (*p >= '0' && *p <= '9')
4251 ++p;
4252 if (!*p) /* Reached end of token without hitting non-digit. */
f8eba3c6 4253 {
40e084e1
KS
4254 /* We have a value history reference. */
4255 struct value *val_history;
f8eba3c6 4256
40e084e1
KS
4257 sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index);
4258 val_history
4259 = access_value_history ((variable[1] == '$') ? -index : index);
4260 if (TYPE_CODE (value_type (val_history)) != TYPE_CODE_INT)
4261 error (_("History values used in line "
4262 "specs must have integer values."));
4263 offset.offset = value_as_long (val_history);
4264 }
4265 else
4266 {
4267 /* Not all digits -- may be user variable/function or a
4268 convenience variable. */
4269 LONGEST valx;
4270 struct internalvar *ivar;
4271
4272 /* Try it as a convenience variable. If it is not a convenience
4273 variable, return and allow normal symbol lookup to occur. */
4274 ivar = lookup_only_internalvar (variable + 1);
4275 if (ivar == NULL)
4276 /* No internal variable with that name. Mark the offset
4277 as unknown to allow the name to be looked up as a symbol. */
4278 offset.sign = LINE_OFFSET_UNKNOWN;
4279 else
4280 {
4281 /* We found a valid variable name. If it is not an integer,
4282 throw an error. */
4283 if (!get_internalvar_integer (ivar, &valx))
4284 error (_("Convenience variables used in line "
4285 "specs must have integer values."));
4286 else
4287 offset.offset = valx;
4288 }
f8eba3c6
TT
4289 }
4290
40e084e1 4291 return offset;
f8eba3c6 4292}
40e084e1 4293\f
f8eba3c6 4294
40e084e1 4295/* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
6e22494e
JK
4296 linespec; return the SAL in RESULT. This function should return SALs
4297 matching those from find_function_start_sal, otherwise false
4298 multiple-locations breakpoints could be placed. */
f8eba3c6
TT
4299
4300static void
4301minsym_found (struct linespec_state *self, struct objfile *objfile,
4302 struct minimal_symbol *msymbol,
6c5b2ebe 4303 std::vector<symtab_and_line> *result)
f8eba3c6
TT
4304{
4305 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4306 CORE_ADDR pc;
4307 struct symtab_and_line sal;
4308
e5f25bc5 4309 if (msymbol_is_text (msymbol))
6e22494e 4310 {
e5f25bc5
PA
4311 sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (objfile, msymbol),
4312 (struct obj_section *) 0, 0);
4313 sal.section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
4314
4315 /* The minimal symbol might point to a function descriptor;
4316 resolve it to the actual code address instead. */
4317 pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc,
4318 &current_target);
4319 if (pc != sal.pc)
4320 sal = find_pc_sect_line (pc, NULL, 0);
4321
4322 if (self->funfirstline)
6e22494e 4323 {
e5f25bc5
PA
4324 if (sal.symtab != NULL
4325 && (COMPUNIT_LOCATIONS_VALID (SYMTAB_COMPUNIT (sal.symtab))
4326 || SYMTAB_LANGUAGE (sal.symtab) == language_asm))
4327 {
4328 /* If gdbarch_convert_from_func_ptr_addr does not apply then
4329 sal.SECTION, sal.LINE&co. will stay correct from above.
4330 If gdbarch_convert_from_func_ptr_addr applies then
4331 sal.SECTION is cleared from above and sal.LINE&co. will
4332 stay correct from the last find_pc_sect_line above. */
4333 sal.pc = MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
4334 sal.pc = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc,
4335 &current_target);
4336 if (gdbarch_skip_entrypoint_p (gdbarch))
4337 sal.pc = gdbarch_skip_entrypoint (gdbarch, sal.pc);
4338 }
4339 else
4340 skip_prologue_sal (&sal);
6e22494e 4341 }
e5f25bc5
PA
4342 }
4343 else
4344 {
4345 sal.objfile = objfile;
4346 sal.pc = MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
4347 sal.pspace = current_program_space;
4348 sal.section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
6e22494e 4349 }
f8eba3c6 4350
07fea4b4 4351 if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
efd66ac6 4352 add_sal_to_sals (self, result, &sal, MSYMBOL_NATURAL_NAME (msymbol), 0);
f8eba3c6
TT
4353}
4354
39b856a4
TT
4355/* A helper struct to pass some data through
4356 iterate_over_minimal_symbols. */
4357
4358struct collect_minsyms
4359{
4360 /* The objfile we're examining. */
4361 struct objfile *objfile;
4362
87186c6a
MMN
4363 /* Only search the given symtab, or NULL to search for all symbols. */
4364 struct symtab *symtab;
4365
39b856a4
TT
4366 /* The funfirstline setting from the initial call. */
4367 int funfirstline;
4368
095bcf5e
JB
4369 /* The list_mode setting from the initial call. */
4370 int list_mode;
4371
39b856a4 4372 /* The resulting symbols. */
f60e2d5c 4373 VEC (bound_minimal_symbol_d) *msyms;
39b856a4
TT
4374};
4375
4376/* A helper function to classify a minimal_symbol_type according to
4377 priority. */
4378
4379static int
4380classify_mtype (enum minimal_symbol_type t)
4381{
4382 switch (t)
f8eba3c6 4383 {
39b856a4
TT
4384 case mst_file_text:
4385 case mst_file_data:
4386 case mst_file_bss:
4387 /* Intermediate priority. */
4388 return 1;
4389
4390 case mst_solib_trampoline:
4391 /* Lowest priority. */
4392 return 2;
4393
4394 default:
4395 /* Highest priority. */
4396 return 0;
f8eba3c6 4397 }
39b856a4
TT
4398}
4399
4400/* Callback for qsort that sorts symbols by priority. */
4401
4402static int
4403compare_msyms (const void *a, const void *b)
4404{
9a3c8263
SM
4405 const bound_minimal_symbol_d *moa = (const bound_minimal_symbol_d *) a;
4406 const bound_minimal_symbol_d *mob = (const bound_minimal_symbol_d *) b;
39b856a4
TT
4407 enum minimal_symbol_type ta = MSYMBOL_TYPE (moa->minsym);
4408 enum minimal_symbol_type tb = MSYMBOL_TYPE (mob->minsym);
4409
4410 return classify_mtype (ta) - classify_mtype (tb);
4411}
4412
4413/* Callback for iterate_over_minimal_symbols that adds the symbol to
4414 the result. */
4415
4416static void
4417add_minsym (struct minimal_symbol *minsym, void *d)
4418{
9a3c8263 4419 struct collect_minsyms *info = (struct collect_minsyms *) d;
f60e2d5c 4420 bound_minimal_symbol_d mo;
39b856a4 4421
77e371c0
TT
4422 mo.minsym = minsym;
4423 mo.objfile = info->objfile;
4424
87186c6a
MMN
4425 if (info->symtab != NULL)
4426 {
4427 CORE_ADDR pc;
4428 struct symtab_and_line sal;
4429 struct gdbarch *gdbarch = get_objfile_arch (info->objfile);
4430
4431 sal = find_pc_sect_line (MSYMBOL_VALUE_ADDRESS (info->objfile, minsym),
4432 NULL, 0);
4433 sal.section = MSYMBOL_OBJ_SECTION (info->objfile, minsym);
4434 pc
4435 = gdbarch_convert_from_func_ptr_addr (gdbarch, sal.pc, &current_target);
4436 if (pc != sal.pc)
4437 sal = find_pc_sect_line (pc, NULL, 0);
4438
4439 if (info->symtab != sal.symtab)
4440 return;
4441 }
4442
095bcf5e
JB
4443 /* Exclude data symbols when looking for breakpoint locations. */
4444 if (!info->list_mode)
4445 switch (minsym->type)
4446 {
4447 case mst_slot_got_plt:
4448 case mst_data:
4449 case mst_bss:
4450 case mst_abs:
4451 case mst_file_data:
4452 case mst_file_bss:
1a2da5ee
JB
4453 {
4454 /* Make sure this minsym is not a function descriptor
4455 before we decide to discard it. */
df6d5441 4456 struct gdbarch *gdbarch = get_objfile_arch (info->objfile);
1a2da5ee 4457 CORE_ADDR addr = gdbarch_convert_from_func_ptr_addr
77e371c0 4458 (gdbarch, BMSYMBOL_VALUE_ADDRESS (mo),
1a2da5ee
JB
4459 &current_target);
4460
77e371c0 4461 if (addr == BMSYMBOL_VALUE_ADDRESS (mo))
1a2da5ee
JB
4462 return;
4463 }
095bcf5e
JB
4464 }
4465
f60e2d5c 4466 VEC_safe_push (bound_minimal_symbol_d, info->msyms, &mo);
f8eba3c6
TT
4467}
4468
87186c6a 4469/* Search for minimal symbols called NAME. If SEARCH_PSPACE
f8eba3c6 4470 is not NULL, the search is restricted to just that program
87186c6a
MMN
4471 space.
4472
4473 If SYMTAB is NULL, search all objfiles, otherwise
4474 restrict results to the given SYMTAB. */
f8eba3c6
TT
4475
4476static void
b5ec771e
PA
4477search_minsyms_for_name (struct collect_info *info,
4478 const lookup_name_info &name,
87186c6a
MMN
4479 struct program_space *search_pspace,
4480 struct symtab *symtab)
f8eba3c6 4481{
87186c6a
MMN
4482 struct collect_minsyms local;
4483 struct cleanup *cleanup;
f8eba3c6 4484
87186c6a
MMN
4485 memset (&local, 0, sizeof (local));
4486 local.funfirstline = info->state->funfirstline;
4487 local.list_mode = info->state->list_mode;
4488 local.symtab = symtab;
39b856a4 4489
87186c6a 4490 cleanup = make_cleanup (VEC_cleanup (bound_minimal_symbol_d), &local.msyms);
f8eba3c6 4491
87186c6a
MMN
4492 if (symtab == NULL)
4493 {
4494 struct program_space *pspace;
f8eba3c6 4495
87186c6a
MMN
4496 ALL_PSPACES (pspace)
4497 {
4498 struct objfile *objfile;
39b856a4 4499
87186c6a
MMN
4500 if (search_pspace != NULL && search_pspace != pspace)
4501 continue;
4502 if (pspace->executing_startup)
4503 continue;
39b856a4 4504
87186c6a
MMN
4505 set_current_program_space (pspace);
4506
4507 ALL_OBJFILES (objfile)
4508 {
4509 local.objfile = objfile;
4510 iterate_over_minimal_symbols (objfile, name, add_minsym, &local);
4511 }
4512 }
4513 }
4514 else
f8eba3c6 4515 {
87186c6a
MMN
4516 if (search_pspace == NULL || SYMTAB_PSPACE (symtab) == search_pspace)
4517 {
4518 set_current_program_space (SYMTAB_PSPACE (symtab));
4519 local.objfile = SYMTAB_OBJFILE(symtab);
b5ec771e 4520 iterate_over_minimal_symbols (local.objfile, name, add_minsym, &local);
87186c6a 4521 }
9ef07c8c 4522 }
39b856a4 4523
f60e2d5c 4524 if (!VEC_empty (bound_minimal_symbol_d, local.msyms))
39b856a4
TT
4525 {
4526 int classification;
4527 int ix;
f60e2d5c 4528 bound_minimal_symbol_d *item;
39b856a4 4529
f60e2d5c
TT
4530 qsort (VEC_address (bound_minimal_symbol_d, local.msyms),
4531 VEC_length (bound_minimal_symbol_d, local.msyms),
4532 sizeof (bound_minimal_symbol_d),
39b856a4
TT
4533 compare_msyms);
4534
4535 /* Now the minsyms are in classification order. So, we walk
4536 over them and process just the minsyms with the same
4537 classification as the very first minsym in the list. */
f60e2d5c 4538 item = VEC_index (bound_minimal_symbol_d, local.msyms, 0);
39b856a4
TT
4539 classification = classify_mtype (MSYMBOL_TYPE (item->minsym));
4540
4541 for (ix = 0;
f60e2d5c 4542 VEC_iterate (bound_minimal_symbol_d, local.msyms, ix, item);
39b856a4
TT
4543 ++ix)
4544 {
4545 if (classify_mtype (MSYMBOL_TYPE (item->minsym)) != classification)
4546 break;
4547
f60e2d5c 4548 VEC_safe_push (bound_minimal_symbol_d,
40e084e1 4549 info->result.minimal_symbols, item);
39b856a4
TT
4550 }
4551 }
4552
4553 do_cleanups (cleanup);
f8eba3c6
TT
4554}
4555
4556/* A helper function to add all symbols matching NAME to INFO. If
4557 PSPACE is not NULL, the search is restricted to just that program
4558 space. */
0f5238ed 4559
f8eba3c6
TT
4560static void
4561add_matching_symbols_to_info (const char *name,
b5ec771e 4562 symbol_name_match_type name_match_type,
f8eba3c6
TT
4563 struct collect_info *info,
4564 struct program_space *pspace)
4565{
4566 int ix;
4567 struct symtab *elt;
0f5238ed 4568
b5ec771e
PA
4569 lookup_name_info lookup_name (name, name_match_type);
4570
ec94af83 4571 for (ix = 0; VEC_iterate (symtab_ptr, info->file_symtabs, ix, elt); ++ix)
f8eba3c6 4572 {
f8eba3c6
TT
4573 if (elt == NULL)
4574 {
b5ec771e
PA
4575 iterate_over_all_matching_symtabs (info->state, lookup_name,
4576 VAR_DOMAIN,
14bc53a8
PA
4577 pspace, true, [&] (symbol *sym)
4578 { return info->add_symbol (sym); });
b5ec771e 4579 search_minsyms_for_name (info, lookup_name, pspace, NULL);
f8eba3c6
TT
4580 }
4581 else if (pspace == NULL || pspace == SYMTAB_PSPACE (elt))
4582 {
87186c6a
MMN
4583 int prev_len = VEC_length (symbolp, info->result.symbols);
4584
f8eba3c6
TT
4585 /* Program spaces that are executing startup should have
4586 been filtered out earlier. */
4587 gdb_assert (!SYMTAB_PSPACE (elt)->executing_startup);
4588 set_current_program_space (SYMTAB_PSPACE (elt));
b5ec771e
PA
4589 iterate_over_file_blocks (elt, lookup_name, VAR_DOMAIN,
4590 [&] (symbol *sym)
14bc53a8 4591 { return info->add_symbol (sym); });
87186c6a
MMN
4592
4593 /* If no new symbols were found in this iteration and this symtab
4594 is in assembler, we might actually be looking for a label for
4595 which we don't have debug info. Check for a minimal symbol in
4596 this case. */
4597 if (prev_len == VEC_length (symbolp, info->result.symbols)
4598 && elt->language == language_asm)
b5ec771e 4599 search_minsyms_for_name (info, lookup_name, pspace, elt);
f8eba3c6
TT
4600 }
4601 }
0f5238ed
TT
4602}
4603
14e91ac5
DC
4604\f
4605
413dad4d
DC
4606/* Now come some functions that are called from multiple places within
4607 decode_line_1. */
4608
f8eba3c6
TT
4609static int
4610symbol_to_sal (struct symtab_and_line *result,
4611 int funfirstline, struct symbol *sym)
413dad4d 4612{
413dad4d 4613 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
50641945 4614 {
f8eba3c6
TT
4615 *result = find_function_start_sal (sym, funfirstline);
4616 return 1;
50641945 4617 }
413dad4d
DC
4618 else
4619 {
62853458 4620 if (SYMBOL_CLASS (sym) == LOC_LABEL && SYMBOL_VALUE_ADDRESS (sym) != 0)
413dad4d 4621 {
51abb421 4622 *result = {};
08be3fe3 4623 result->symtab = symbol_symtab (sym);
06871ae8 4624 result->symbol = sym;
f8eba3c6
TT
4625 result->line = SYMBOL_LINE (sym);
4626 result->pc = SYMBOL_VALUE_ADDRESS (sym);
08be3fe3 4627 result->pspace = SYMTAB_PSPACE (result->symtab);
f8eba3c6
TT
4628 result->explicit_pc = 1;
4629 return 1;
413dad4d 4630 }
62853458 4631 else if (funfirstline)
dcf9f4ab 4632 {
f8eba3c6 4633 /* Nothing. */
dcf9f4ab 4634 }
62853458
TT
4635 else if (SYMBOL_LINE (sym) != 0)
4636 {
4637 /* We know its line number. */
51abb421 4638 *result = {};
08be3fe3 4639 result->symtab = symbol_symtab (sym);
06871ae8 4640 result->symbol = sym;
f8eba3c6 4641 result->line = SYMBOL_LINE (sym);
e5f25bc5 4642 result->pc = SYMBOL_VALUE_ADDRESS (sym);
08be3fe3 4643 result->pspace = SYMTAB_PSPACE (result->symtab);
f8eba3c6 4644 return 1;
62853458 4645 }
413dad4d 4646 }
f8eba3c6
TT
4647
4648 return 0;
413dad4d 4649}
50641945 4650
16e802b9 4651linespec_result::~linespec_result ()
f8eba3c6 4652{
6c5b2ebe
PA
4653 for (linespec_sals &lsal : lsals)
4654 xfree (lsal.canonical);
7efd8fc2 4655}
87f0e720
KS
4656
4657/* Return the quote characters permitted by the linespec parser. */
4658
4659const char *
4660get_gdb_linespec_parser_quote_characters (void)
4661{
4662 return linespec_quote_characters;
4663}