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