]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/linespec.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / linespec.c
CommitLineData
50641945 1/* Parser for linespec for the GNU debugger, GDB.
05ff989b 2
213516ef 3 Copyright (C) 1986-2023 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 }
f8631e5e
SM
1504
1505 std::string buffer;
1506 args = command_line_input (buffer, prompt, "overload-choice");
50641945
FN
1507
1508 if (args == 0 || *args == 0)
e2e0b3e5 1509 error_no_arg (_("one or more choice numbers"));
50641945 1510
bfd28288
PA
1511 number_or_range_parser parser (args);
1512 while (!parser.finished ())
50641945 1513 {
bfd28288 1514 int num = parser.get_number ();
50641945
FN
1515
1516 if (num == 0)
8a3fe4f8 1517 error (_("canceled"));
50641945
FN
1518 else if (num == 1)
1519 {
f8eba3c6
TT
1520 /* We intentionally make this result in a single breakpoint,
1521 contrary to what older versions of gdb did. The
1522 rationale is that this lets a user get the
1523 multiple_symbols_all behavior even with the 'ask'
1524 setting; and he can get separate breakpoints by entering
1525 "2-57" at the query. */
f8eba3c6
TT
1526 convert_results_to_lsals (self, result);
1527 return;
50641945
FN
1528 }
1529
f8eba3c6 1530 num -= 2;
53a0f8a2 1531 if (num >= items.size ())
f8eba3c6 1532 printf_unfiltered (_("No choice number %d.\n"), num);
50641945
FN
1533 else
1534 {
33f448b1 1535 struct decode_line_2_item *item = &items[num];
f8eba3c6 1536
33f448b1 1537 if (!item->selected)
50641945 1538 {
f73c6ece 1539 filters.push_back (item->fullform.c_str ());
33f448b1 1540 item->selected = 1;
50641945
FN
1541 }
1542 else
1543 {
3e43a32a 1544 printf_unfiltered (_("duplicate request for %d ignored.\n"),
f6f99966 1545 num + 2);
50641945
FN
1546 }
1547 }
50641945 1548 }
f8eba3c6
TT
1549
1550 filter_results (self, result, filters);
50641945 1551}
94af9270 1552
40e084e1 1553\f
3d50dd94 1554
40e084e1
KS
1555/* The parser of linespec itself. */
1556
1557/* Throw an appropriate error when SYMBOL is not found (optionally in
1558 FILENAME). */
1559
1560static void ATTRIBUTE_NORETURN
5d94e27b 1561symbol_not_found_error (const char *symbol, const char *filename)
3d50dd94 1562{
40e084e1
KS
1563 if (symbol == NULL)
1564 symbol = "";
1565
1566 if (!have_full_symbols ()
1567 && !have_partial_symbols ()
1568 && !have_minimal_symbols ())
1569 throw_error (NOT_FOUND_ERROR,
1570 _("No symbol table is loaded. Use the \"file\" command."));
1571
1572 /* If SYMBOL starts with '$', the user attempted to either lookup
1573 a function/variable in his code starting with '$' or an internal
1574 variable of that name. Since we do not know which, be concise and
1575 explain both possibilities. */
1576 if (*symbol == '$')
1577 {
1578 if (filename)
1579 throw_error (NOT_FOUND_ERROR,
1580 _("Undefined convenience variable or function \"%s\" "
1581 "not defined in \"%s\"."), symbol, filename);
1582 else
1583 throw_error (NOT_FOUND_ERROR,
1584 _("Undefined convenience variable or function \"%s\" "
1585 "not defined."), symbol);
1586 }
1587 else
1588 {
1589 if (filename)
1590 throw_error (NOT_FOUND_ERROR,
1591 _("Function \"%s\" not defined in \"%s\"."),
1592 symbol, filename);
1593 else
1594 throw_error (NOT_FOUND_ERROR,
1595 _("Function \"%s\" not defined."), symbol);
1596 }
3d50dd94
JK
1597}
1598
40e084e1
KS
1599/* Throw an appropriate error when an unexpected token is encountered
1600 in the input. */
94af9270 1601
40e084e1
KS
1602static void ATTRIBUTE_NORETURN
1603unexpected_linespec_error (linespec_parser *parser)
94af9270 1604{
40e084e1
KS
1605 linespec_token token;
1606 static const char * token_type_strings[]
1607 = {"keyword", "colon", "string", "number", "comma", "end of input"};
94af9270 1608
40e084e1
KS
1609 /* Get the token that generated the error. */
1610 token = linespec_lexer_lex_one (parser);
94af9270 1611
40e084e1
KS
1612 /* Finally, throw the error. */
1613 if (token.type == LSTOKEN_STRING || token.type == LSTOKEN_NUMBER
1614 || token.type == LSTOKEN_KEYWORD)
94af9270 1615 {
a5b5adf5 1616 gdb::unique_xmalloc_ptr<char> string = copy_token_string (token);
40e084e1
KS
1617 throw_error (GENERIC_ERROR,
1618 _("malformed linespec error: unexpected %s, \"%s\""),
a5b5adf5 1619 token_type_strings[token.type], string.get ());
40e084e1
KS
1620 }
1621 else
1622 throw_error (GENERIC_ERROR,
1623 _("malformed linespec error: unexpected %s"),
1624 token_type_strings[token.type]);
1625}
1626
00e52e53
KS
1627/* Throw an undefined label error. */
1628
1629static void ATTRIBUTE_NORETURN
1630undefined_label_error (const char *function, const char *label)
1631{
1632 if (function != NULL)
1633 throw_error (NOT_FOUND_ERROR,
dda83cd7
SM
1634 _("No label \"%s\" defined in function \"%s\"."),
1635 label, function);
00e52e53
KS
1636 else
1637 throw_error (NOT_FOUND_ERROR,
dda83cd7
SM
1638 _("No label \"%s\" defined in current function."),
1639 label);
00e52e53
KS
1640}
1641
1642/* Throw a source file not found error. */
1643
1644static void ATTRIBUTE_NORETURN
1645source_file_not_found_error (const char *name)
1646{
1647 throw_error (NOT_FOUND_ERROR, _("No source file named %s."), name);
1648}
1649
c45ec17c
PA
1650/* Unless at EIO, save the current stream position as completion word
1651 point, and consume the next token. */
1652
1653static linespec_token
1654save_stream_and_consume_token (linespec_parser *parser)
1655{
1656 if (linespec_lexer_peek_token (parser).type != LSTOKEN_EOI)
1657 parser->completion_word = PARSER_STREAM (parser);
1658 return linespec_lexer_consume_token (parser);
1659}
1660
87f0e720 1661/* See description in linespec.h. */
40e084e1 1662
87f0e720 1663struct line_offset
09cf2b22 1664linespec_parse_line_offset (const char *string)
40e084e1 1665{
87f0e720 1666 const char *start = string;
dfea48fc 1667 struct line_offset line_offset;
40e084e1
KS
1668
1669 if (*string == '+')
1670 {
1671 line_offset.sign = LINE_OFFSET_PLUS;
1672 ++string;
1673 }
1674 else if (*string == '-')
1675 {
1676 line_offset.sign = LINE_OFFSET_MINUS;
1677 ++string;
1678 }
dfea48fc
PA
1679 else
1680 line_offset.sign = LINE_OFFSET_NONE;
40e084e1 1681
87f0e720
KS
1682 if (*string != '\0' && !isdigit (*string))
1683 error (_("malformed line offset: \"%s\""), start);
1684
40e084e1
KS
1685 /* Right now, we only allow base 10 for offsets. */
1686 line_offset.offset = atoi (string);
1687 return line_offset;
1688}
1689
c45ec17c
PA
1690/* In completion mode, if the user is still typing the number, there's
1691 no possible completion to offer. But if there's already input past
1692 the number, setup to expect NEXT. */
1693
1694static void
1695set_completion_after_number (linespec_parser *parser,
1696 linespec_complete_what next)
1697{
1698 if (*PARSER_STREAM (parser) == ' ')
1699 {
f1735a53 1700 parser->completion_word = skip_spaces (PARSER_STREAM (parser) + 1);
c45ec17c
PA
1701 parser->complete_what = next;
1702 }
1703 else
1704 {
1705 parser->completion_word = PARSER_STREAM (parser);
1706 parser->complete_what = linespec_complete_what::NOTHING;
1707 }
1708}
1709
40e084e1
KS
1710/* Parse the basic_spec in PARSER's input. */
1711
1712static void
1713linespec_parse_basic (linespec_parser *parser)
1714{
a5b5adf5 1715 gdb::unique_xmalloc_ptr<char> name;
40e084e1 1716 linespec_token token;
40e084e1
KS
1717
1718 /* Get the next token. */
1719 token = linespec_lexer_lex_one (parser);
1720
1721 /* If it is EOI or KEYWORD, issue an error. */
c45ec17c
PA
1722 if (token.type == LSTOKEN_KEYWORD)
1723 {
1724 parser->complete_what = linespec_complete_what::NOTHING;
1725 unexpected_linespec_error (parser);
1726 }
1727 else if (token.type == LSTOKEN_EOI)
1728 {
1729 unexpected_linespec_error (parser);
1730 }
40e084e1
KS
1731 /* If it is a LSTOKEN_NUMBER, we have an offset. */
1732 else if (token.type == LSTOKEN_NUMBER)
1733 {
c45ec17c
PA
1734 set_completion_after_number (parser, linespec_complete_what::KEYWORD);
1735
40e084e1
KS
1736 /* Record the line offset and get the next token. */
1737 name = copy_token_string (token);
a5b5adf5
TT
1738 PARSER_EXPLICIT (parser)->line_offset
1739 = linespec_parse_line_offset (name.get ());
40e084e1
KS
1740
1741 /* Get the next token. */
1742 token = linespec_lexer_consume_token (parser);
1743
1744 /* If the next token is a comma, stop parsing and return. */
1745 if (token.type == LSTOKEN_COMMA)
c45ec17c
PA
1746 {
1747 parser->complete_what = linespec_complete_what::NOTHING;
1748 return;
1749 }
40e084e1
KS
1750
1751 /* If the next token is anything but EOI or KEYWORD, issue
1752 an error. */
1753 if (token.type != LSTOKEN_KEYWORD && token.type != LSTOKEN_EOI)
1754 unexpected_linespec_error (parser);
1755 }
1756
1757 if (token.type == LSTOKEN_KEYWORD || token.type == LSTOKEN_EOI)
1758 return;
1759
1760 /* Next token must be LSTOKEN_STRING. */
1761 if (token.type != LSTOKEN_STRING)
c45ec17c
PA
1762 {
1763 parser->complete_what = linespec_complete_what::NOTHING;
1764 unexpected_linespec_error (parser);
1765 }
40e084e1
KS
1766
1767 /* The current token will contain the name of a function, method,
1768 or label. */
c45ec17c 1769 name = copy_token_string (token);
c45ec17c
PA
1770
1771 if (parser->completion_tracker != NULL)
1772 {
1773 /* If the function name ends with a ":", then this may be an
1774 incomplete "::" scope operator instead of a label separator.
1775 E.g.,
1776 "b klass:<tab>"
1777 which should expand to:
1778 "b klass::method()"
1779
1780 Do a tentative completion assuming the later. If we find
1781 completions, advance the stream past the colon token and make
1782 it part of the function name/token. */
1783
1784 if (!parser->completion_quote_char
1785 && strcmp (PARSER_STREAM (parser), ":") == 0)
1786 {
1787 completion_tracker tmp_tracker;
1788 const char *source_filename
1789 = PARSER_EXPLICIT (parser)->source_filename;
a20714ff
PA
1790 symbol_name_match_type match_type
1791 = PARSER_EXPLICIT (parser)->func_name_match_type;
c45ec17c
PA
1792
1793 linespec_complete_function (tmp_tracker,
1794 parser->completion_word,
a20714ff 1795 match_type,
c45ec17c
PA
1796 source_filename);
1797
1798 if (tmp_tracker.have_completions ())
1799 {
1800 PARSER_STREAM (parser)++;
1801 LS_TOKEN_STOKEN (token).length++;
1802
a5b5adf5
TT
1803 name.reset (savestring (parser->completion_word,
1804 (PARSER_STREAM (parser)
1805 - parser->completion_word)));
c45ec17c
PA
1806 }
1807 }
1808
a5b5adf5 1809 PARSER_EXPLICIT (parser)->function_name = name.release ();
c45ec17c
PA
1810 }
1811 else
1812 {
28a0d291
SM
1813 std::vector<block_symbol> symbols;
1814 std::vector<bound_minimal_symbol> minimal_symbols;
1815
6a307fc5
TT
1816 /* Try looking it up as a function/method. */
1817 find_linespec_symbols (PARSER_STATE (parser),
a5b5adf5 1818 PARSER_RESULT (parser)->file_symtabs, name.get (),
6a307fc5
TT
1819 PARSER_EXPLICIT (parser)->func_name_match_type,
1820 &symbols, &minimal_symbols);
40e084e1 1821
c2a031c5 1822 if (!symbols.empty () || !minimal_symbols.empty ())
94af9270 1823 {
28a0d291
SM
1824 PARSER_RESULT (parser)->function_symbols = std::move (symbols);
1825 PARSER_RESULT (parser)->minimal_symbols = std::move (minimal_symbols);
a5b5adf5 1826 PARSER_EXPLICIT (parser)->function_name = name.release ();
40e084e1 1827 }
6a307fc5 1828 else
b4013987 1829 {
6a307fc5
TT
1830 /* NAME was not a function or a method. So it must be a label
1831 name or user specified variable like "break foo.c:$zippo". */
28a0d291
SM
1832 std::vector<block_symbol> labels
1833 = find_label_symbols (PARSER_STATE (parser), {}, &symbols,
1834 name.get ());
1835
1836 if (!labels.empty ())
6a307fc5 1837 {
28a0d291 1838 PARSER_RESULT (parser)->labels.label_symbols = std::move (labels);
7243d011 1839 PARSER_RESULT (parser)->labels.function_symbols
28a0d291 1840 = std::move (symbols);
a5b5adf5 1841 PARSER_EXPLICIT (parser)->label_name = name.release ();
6a307fc5
TT
1842 }
1843 else if (token.type == LSTOKEN_STRING
1844 && *LS_TOKEN_STOKEN (token).ptr == '$')
1845 {
1846 /* User specified a convenience variable or history value. */
1847 PARSER_EXPLICIT (parser)->line_offset
a5b5adf5 1848 = linespec_parse_variable (PARSER_STATE (parser), name.get ());
b4013987 1849
6a307fc5
TT
1850 if (PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN)
1851 {
1852 /* The user-specified variable was not valid. Do not
1853 throw an error here. parse_linespec will do it for us. */
a5b5adf5 1854 PARSER_EXPLICIT (parser)->function_name = name.release ();
6a307fc5
TT
1855 return;
1856 }
6a307fc5
TT
1857 }
1858 else
b4013987 1859 {
6a307fc5
TT
1860 /* The name is also not a label. Abort parsing. Do not throw
1861 an error here. parse_linespec will do it for us. */
1862
1863 /* Save a copy of the name we were trying to lookup. */
a5b5adf5 1864 PARSER_EXPLICIT (parser)->function_name = name.release ();
b4013987
AA
1865 return;
1866 }
1867 }
c45ec17c
PA
1868 }
1869
1870 int previous_qc = parser->completion_quote_char;
40e084e1
KS
1871
1872 /* Get the next token. */
1873 token = linespec_lexer_consume_token (parser);
1874
c45ec17c
PA
1875 if (token.type == LSTOKEN_EOI)
1876 {
1877 if (previous_qc && !parser->completion_quote_char)
1878 parser->complete_what = linespec_complete_what::KEYWORD;
1879 }
1880 else if (token.type == LSTOKEN_COLON)
40e084e1
KS
1881 {
1882 /* User specified a label or a lineno. */
1883 token = linespec_lexer_consume_token (parser);
1884
1885 if (token.type == LSTOKEN_NUMBER)
1886 {
1887 /* User specified an offset. Record the line offset and
1888 get the next token. */
c45ec17c
PA
1889 set_completion_after_number (parser, linespec_complete_what::KEYWORD);
1890
40e084e1 1891 name = copy_token_string (token);
00e52e53 1892 PARSER_EXPLICIT (parser)->line_offset
a5b5adf5 1893 = linespec_parse_line_offset (name.get ());
40e084e1 1894
c45ec17c 1895 /* Get the next token. */
40e084e1
KS
1896 token = linespec_lexer_consume_token (parser);
1897 }
c45ec17c
PA
1898 else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
1899 {
1900 parser->complete_what = linespec_complete_what::LABEL;
1901 }
40e084e1
KS
1902 else if (token.type == LSTOKEN_STRING)
1903 {
c45ec17c
PA
1904 parser->complete_what = linespec_complete_what::LABEL;
1905
1906 /* If we have text after the label separated by whitespace
1907 (e.g., "b func():lab i<tab>"), don't consider it part of
1908 the label. In completion mode that should complete to
1909 "if", in normal mode, the 'i' should be treated as
1910 garbage. */
1911 if (parser->completion_quote_char == '\0')
1912 {
1913 const char *ptr = LS_TOKEN_STOKEN (token).ptr;
1914 for (size_t i = 0; i < LS_TOKEN_STOKEN (token).length; i++)
1915 {
1916 if (ptr[i] == ' ')
1917 {
1918 LS_TOKEN_STOKEN (token).length = i;
f1735a53 1919 PARSER_STREAM (parser) = skip_spaces (ptr + i + 1);
c45ec17c
PA
1920 break;
1921 }
1922 }
1923 }
1924
1925 if (parser->completion_tracker != NULL)
1926 {
1927 if (PARSER_STREAM (parser)[-1] == ' ')
1928 {
1929 parser->completion_word = PARSER_STREAM (parser);
1930 parser->complete_what = linespec_complete_what::KEYWORD;
1931 }
1932 }
1933 else
1934 {
28a0d291
SM
1935 std::vector<block_symbol> symbols;
1936
6a307fc5
TT
1937 /* Grab a copy of the label's name and look it up. */
1938 name = copy_token_string (token);
28a0d291 1939 std::vector<block_symbol> labels
6a307fc5
TT
1940 = find_label_symbols (PARSER_STATE (parser),
1941 PARSER_RESULT (parser)->function_symbols,
a5b5adf5 1942 &symbols, name.get ());
40e084e1 1943
28a0d291 1944 if (!labels.empty ())
6a307fc5 1945 {
28a0d291
SM
1946 PARSER_RESULT (parser)->labels.label_symbols
1947 = std::move (labels);
7243d011 1948 PARSER_RESULT (parser)->labels.function_symbols
28a0d291 1949 = std::move (symbols);
a5b5adf5 1950 PARSER_EXPLICIT (parser)->label_name = name.release ();
6a307fc5
TT
1951 }
1952 else
1953 {
1954 /* We don't know what it was, but it isn't a label. */
1955 undefined_label_error
a5b5adf5 1956 (PARSER_EXPLICIT (parser)->function_name, name.get ());
6a307fc5 1957 }
40e084e1 1958
c45ec17c
PA
1959 }
1960
40e084e1 1961 /* Check for a line offset. */
c45ec17c 1962 token = save_stream_and_consume_token (parser);
40e084e1
KS
1963 if (token.type == LSTOKEN_COLON)
1964 {
1965 /* Get the next token. */
1966 token = linespec_lexer_consume_token (parser);
1967
1968 /* It must be a line offset. */
1969 if (token.type != LSTOKEN_NUMBER)
1970 unexpected_linespec_error (parser);
1971
c6756f62 1972 /* Record the line offset and get the next token. */
40e084e1 1973 name = copy_token_string (token);
40e084e1 1974
00e52e53 1975 PARSER_EXPLICIT (parser)->line_offset
a5b5adf5 1976 = linespec_parse_line_offset (name.get ());
40e084e1
KS
1977
1978 /* Get the next token. */
1979 token = linespec_lexer_consume_token (parser);
94af9270
KS
1980 }
1981 }
40e084e1
KS
1982 else
1983 {
1984 /* Trailing ':' in the input. Issue an error. */
1985 unexpected_linespec_error (parser);
1986 }
94af9270 1987 }
40e084e1 1988}
94af9270 1989
40e084e1 1990/* Canonicalize the linespec contained in LS. The result is saved into
00e52e53
KS
1991 STATE->canonical. This function handles both linespec and explicit
1992 locations. */
40e084e1
KS
1993
1994static void
1487a14e 1995canonicalize_linespec (struct linespec_state *state, const linespec *ls)
40e084e1
KS
1996{
1997 /* If canonicalization was not requested, no need to do anything. */
1998 if (!state->canonical)
1999 return;
2000
00e52e53 2001 /* Save everything as an explicit location. */
40d97ee2
PA
2002 state->canonical->locspec = ls->explicit_loc.clone ();
2003 explicit_location_spec *explicit_loc
2004 = as_explicit_location_spec (state->canonical->locspec.get ());
40e084e1 2005
67994074 2006 if (explicit_loc->label_name != NULL)
a06efdd6 2007 {
00e52e53 2008 state->canonical->special_display = 1;
40e084e1 2009
67994074 2010 if (explicit_loc->function_name == NULL)
40e084e1 2011 {
a06efdd6 2012 /* No function was specified, so add the symbol name. */
28a0d291
SM
2013 gdb_assert (ls->labels.function_symbols.size () == 1);
2014 block_symbol s = ls->labels.function_symbols.front ();
987012b8 2015 explicit_loc->function_name = xstrdup (s.symbol->natural_name ());
40e084e1 2016 }
a06efdd6 2017 }
40e084e1 2018
00e52e53
KS
2019 /* If this location originally came from a linespec, save a string
2020 representation of it for display and saving to file. */
2021 if (state->is_linespec)
dac9773e 2022 explicit_loc->set_string (explicit_loc->to_linespec ());
94af9270 2023}
c00f8484 2024
40e084e1 2025/* Given a line offset in LS, construct the relevant SALs. */
c00f8484 2026
6c5b2ebe 2027static std::vector<symtab_and_line>
40e084e1 2028create_sals_line_offset (struct linespec_state *self,
1487a14e 2029 linespec *ls)
c00f8484 2030{
40e084e1 2031 int use_default = 0;
c00f8484 2032
40e084e1
KS
2033 /* This is where we need to make sure we have good defaults.
2034 We must guarantee that this section of code is never executed
2e47c6ca 2035 when we are called with just a function name, since
40e084e1
KS
2036 set_default_source_symtab_and_line uses
2037 select_source_symtab that calls us with such an argument. */
2038
28a0d291
SM
2039 if (ls->file_symtabs.size () == 1
2040 && ls->file_symtabs.front () == nullptr)
3d50dd94 2041 {
40e084e1 2042 set_current_program_space (self->program_space);
c00f8484 2043
40e084e1
KS
2044 /* Make sure we have at least a default source line. */
2045 set_default_source_symtab_and_line ();
2046 initialize_defaults (&self->default_symtab, &self->default_line);
28a0d291 2047 ls->file_symtabs
bb995d00
KS
2048 = collect_symtabs_from_filename (self->default_symtab->filename,
2049 self->search_pspace);
40e084e1
KS
2050 use_default = 1;
2051 }
c00f8484 2052
51abb421 2053 symtab_and_line val;
67994074
KS
2054 val.line = ls->explicit_loc.line_offset.offset;
2055 switch (ls->explicit_loc.line_offset.sign)
40e084e1
KS
2056 {
2057 case LINE_OFFSET_PLUS:
67994074 2058 if (ls->explicit_loc.line_offset.offset == 0)
40e084e1
KS
2059 val.line = 5;
2060 if (use_default)
2061 val.line = self->default_line + val.line;
2062 break;
2063
2064 case LINE_OFFSET_MINUS:
67994074 2065 if (ls->explicit_loc.line_offset.offset == 0)
40e084e1
KS
2066 val.line = 15;
2067 if (use_default)
2068 val.line = self->default_line - val.line;
2069 else
2070 val.line = -val.line;
2071 break;
2072
2073 case LINE_OFFSET_NONE:
2074 break; /* No need to adjust val.line. */
2075 }
2076
6c5b2ebe 2077 std::vector<symtab_and_line> values;
40e084e1 2078 if (self->list_mode)
6c5b2ebe 2079 values = decode_digits_list_mode (self, ls, val);
40e084e1
KS
2080 else
2081 {
2082 struct linetable_entry *best_entry = NULL;
40e084e1
KS
2083 int i, j;
2084
6c5b2ebe
PA
2085 std::vector<symtab_and_line> intermediate_results
2086 = decode_digits_ordinary (self, ls, val.line, &best_entry);
2087 if (intermediate_results.empty () && best_entry != NULL)
2088 intermediate_results = decode_digits_ordinary (self, ls,
2089 best_entry->line,
2090 &best_entry);
40e084e1
KS
2091
2092 /* For optimized code, the compiler can scatter one source line
2093 across disjoint ranges of PC values, even when no duplicate
2094 functions or inline functions are involved. For example,
2095 'for (;;)' inside a non-template, non-inline, and non-ctor-or-dtor
2096 function can result in two PC ranges. In this case, we don't
2097 want to set a breakpoint on the first PC of each range. To filter
2098 such cases, we use containing blocks -- for each PC found
2099 above, we see if there are other PCs that are in the same
2100 block. If yes, the other PCs are filtered out. */
2101
0fc21fd8
TT
2102 gdb::def_vector<int> filter (intermediate_results.size ());
2103 gdb::def_vector<const block *> blocks (intermediate_results.size ());
40e084e1 2104
6c5b2ebe 2105 for (i = 0; i < intermediate_results.size (); ++i)
3d50dd94 2106 {
6c5b2ebe 2107 set_current_program_space (intermediate_results[i].pspace);
c00f8484 2108
40e084e1 2109 filter[i] = 1;
6c5b2ebe
PA
2110 blocks[i] = block_for_pc_sect (intermediate_results[i].pc,
2111 intermediate_results[i].section);
3d50dd94 2112 }
c00f8484 2113
6c5b2ebe 2114 for (i = 0; i < intermediate_results.size (); ++i)
40e084e1
KS
2115 {
2116 if (blocks[i] != NULL)
6c5b2ebe 2117 for (j = i + 1; j < intermediate_results.size (); ++j)
40e084e1
KS
2118 {
2119 if (blocks[j] == blocks[i])
2120 {
2121 filter[j] = 0;
2122 break;
2123 }
2124 }
2125 }
c00f8484 2126
6c5b2ebe 2127 for (i = 0; i < intermediate_results.size (); ++i)
40e084e1
KS
2128 if (filter[i])
2129 {
2130 struct symbol *sym = (blocks[i]
2131 ? block_containing_function (blocks[i])
2132 : NULL);
3d50dd94 2133
40e084e1 2134 if (self->funfirstline)
6c5b2ebe 2135 skip_prologue_sal (&intermediate_results[i]);
cd2bb709 2136 intermediate_results[i].symbol = sym;
6c5b2ebe 2137 add_sal_to_sals (self, &values, &intermediate_results[i],
987012b8 2138 sym ? sym->natural_name () : NULL, 0);
40e084e1 2139 }
f17170e5 2140 }
c00f8484 2141
6c5b2ebe 2142 if (values.empty ())
40e084e1 2143 {
67994074 2144 if (ls->explicit_loc.source_filename)
40e084e1 2145 throw_error (NOT_FOUND_ERROR, _("No line %d in file \"%s\"."),
67994074 2146 val.line, ls->explicit_loc.source_filename);
40e084e1
KS
2147 else
2148 throw_error (NOT_FOUND_ERROR, _("No line %d in the current file."),
2149 val.line);
2150 }
3d50dd94 2151
40e084e1 2152 return values;
c00f8484
KS
2153}
2154
a06efdd6
KS
2155/* Convert the given ADDRESS into SaLs. */
2156
6c5b2ebe 2157static std::vector<symtab_and_line>
a06efdd6
KS
2158convert_address_location_to_sals (struct linespec_state *self,
2159 CORE_ADDR address)
2160{
6c5b2ebe 2161 symtab_and_line sal = find_pc_line (address, 0);
a06efdd6
KS
2162 sal.pc = address;
2163 sal.section = find_pc_overlay (address);
2164 sal.explicit_pc = 1;
cd2bb709 2165 sal.symbol = find_pc_sect_containing_function (sal.pc, sal.section);
6c5b2ebe
PA
2166
2167 std::vector<symtab_and_line> sals;
a06efdd6
KS
2168 add_sal_to_sals (self, &sals, &sal, core_addr_to_string (address), 1);
2169
2170 return sals;
2171}
2172
40e084e1
KS
2173/* Create and return SALs from the linespec LS. */
2174
6c5b2ebe 2175static std::vector<symtab_and_line>
1487a14e 2176convert_linespec_to_sals (struct linespec_state *state, linespec *ls)
40e084e1 2177{
6c5b2ebe 2178 std::vector<symtab_and_line> sals;
40e084e1 2179
28a0d291 2180 if (!ls->labels.label_symbols.empty ())
40e084e1
KS
2181 {
2182 /* We have just a bunch of functions/methods or labels. */
7e41c8db
KS
2183 struct symtab_and_line sal;
2184
28a0d291 2185 for (const auto &sym : ls->labels.label_symbols)
40e084e1 2186 {
7e41c8db 2187 struct program_space *pspace
4206d69e 2188 = sym.symbol->symtab ()->compunit ()->objfile ()->pspace;
fdbb204b 2189
7e41c8db 2190 if (symbol_to_sal (&sal, state->funfirstline, sym.symbol)
fdbb204b 2191 && maybe_add_address (state->addr_set, pspace, sal.pc))
64b92e45 2192 add_sal_to_sals (state, &sals, &sal,
987012b8 2193 sym.symbol->natural_name (), 0);
40e084e1
KS
2194 }
2195 }
28a0d291 2196 else if (!ls->function_symbols.empty () || !ls->minimal_symbols.empty ())
40e084e1
KS
2197 {
2198 /* We have just a bunch of functions and/or methods. */
28a0d291 2199 if (!ls->function_symbols.empty ())
40e084e1
KS
2200 {
2201 /* Sort symbols so that symbols with the same program space are next
2202 to each other. */
28a0d291
SM
2203 std::sort (ls->function_symbols.begin (),
2204 ls->function_symbols.end (),
4dedf84d 2205 compare_symbols);
40e084e1 2206
28a0d291 2207 for (const auto &sym : ls->function_symbols)
40e084e1 2208 {
7e41c8db 2209 program_space *pspace
4206d69e 2210 = sym.symbol->symtab ()->compunit ()->objfile ()->pspace;
40e084e1 2211 set_current_program_space (pspace);
76af0f26
PA
2212
2213 /* Don't skip to the first line of the function if we
2214 had found an ifunc minimal symbol for this function,
2215 because that means that this function is an ifunc
2216 resolver with the same name as the ifunc itself. */
2217 bool found_ifunc = false;
2218
2219 if (state->funfirstline
28a0d291 2220 && !ls->minimal_symbols.empty ()
66d7f48f 2221 && sym.symbol->aclass () == LOC_BLOCK)
76af0f26
PA
2222 {
2223 const CORE_ADDR addr
6395b628 2224 = sym.symbol->value_block ()->entry_pc ();
76af0f26 2225
28a0d291 2226 for (const auto &elem : ls->minimal_symbols)
76af0f26 2227 {
60f62e2b
SM
2228 if (elem.minsym->type () == mst_text_gnu_ifunc
2229 || elem.minsym->type () == mst_data_gnu_ifunc)
76af0f26 2230 {
4aeddc50 2231 CORE_ADDR msym_addr = elem.value_address ();
60f62e2b 2232 if (elem.minsym->type () == mst_data_gnu_ifunc)
f50776aa
PA
2233 {
2234 struct gdbarch *gdbarch
08feed99 2235 = elem.objfile->arch ();
f50776aa
PA
2236 msym_addr
2237 = (gdbarch_convert_from_func_ptr_addr
2238 (gdbarch,
2239 msym_addr,
328d42d8 2240 current_inferior ()->top_target ()));
f50776aa
PA
2241 }
2242
2243 if (msym_addr == addr)
2244 {
2245 found_ifunc = true;
2246 break;
2247 }
76af0f26
PA
2248 }
2249 }
2250 }
2251
2252 if (!found_ifunc)
2253 {
2254 symtab_and_line sal;
7e41c8db 2255 if (symbol_to_sal (&sal, state->funfirstline, sym.symbol)
76af0f26
PA
2256 && maybe_add_address (state->addr_set, pspace, sal.pc))
2257 add_sal_to_sals (state, &sals, &sal,
987012b8 2258 sym.symbol->natural_name (), 0);
76af0f26 2259 }
40e084e1
KS
2260 }
2261 }
2262
28a0d291 2263 if (!ls->minimal_symbols.empty ())
40e084e1 2264 {
76af0f26 2265 /* Sort minimal symbols by program space, too */
28a0d291
SM
2266 std::sort (ls->minimal_symbols.begin (),
2267 ls->minimal_symbols.end (),
c2a031c5 2268 compare_msymbols);
40e084e1 2269
28a0d291 2270 for (const auto &elem : ls->minimal_symbols)
40e084e1 2271 {
c2a031c5 2272 program_space *pspace = elem.objfile->pspace;
40e084e1 2273 set_current_program_space (pspace);
c2a031c5 2274 minsym_found (state, elem.objfile, elem.minsym, &sals);
40e084e1
KS
2275 }
2276 }
2277 }
67994074 2278 else if (ls->explicit_loc.line_offset.sign != LINE_OFFSET_UNKNOWN)
40e084e1
KS
2279 {
2280 /* Only an offset was specified. */
2281 sals = create_sals_line_offset (state, ls);
2282
2283 /* Make sure we have a filename for canonicalization. */
67994074 2284 if (ls->explicit_loc.source_filename == NULL)
05cba821
JK
2285 {
2286 const char *fullname = symtab_to_fullname (state->default_symtab);
2287
e93ba630
JK
2288 /* It may be more appropriate to keep DEFAULT_SYMTAB in its symtab
2289 form so that displaying SOURCE_FILENAME can follow the current
2290 FILENAME_DISPLAY_STRING setting. But as it is used only rarely
2291 it has been kept for code simplicity only in absolute form. */
67994074 2292 ls->explicit_loc.source_filename = xstrdup (fullname);
05cba821 2293 }
40e084e1
KS
2294 }
2295 else
2296 {
2297 /* We haven't found any results... */
2298 return sals;
2299 }
2300
2301 canonicalize_linespec (state, ls);
2302
6c5b2ebe 2303 if (!sals.empty () && state->canonical != NULL)
40e084e1
KS
2304 state->canonical->pre_expanded = 1;
2305
2306 return sals;
2307}
50641945 2308
40d97ee2
PA
2309/* Build RESULT from the explicit location spec components
2310 SOURCE_FILENAME, FUNCTION_NAME, LABEL_NAME and LINE_OFFSET. */
00e52e53 2311
a2459270 2312static void
40d97ee2
PA
2313convert_explicit_location_spec_to_linespec
2314 (struct linespec_state *self,
2315 linespec *result,
2316 const char *source_filename,
2317 const char *function_name,
2318 symbol_name_match_type fname_match_type,
2319 const char *label_name,
2320 struct line_offset line_offset)
00e52e53 2321{
c2a031c5 2322 std::vector<bound_minimal_symbol> minimal_symbols;
00e52e53 2323
a20714ff
PA
2324 result->explicit_loc.func_name_match_type = fname_match_type;
2325
a2459270 2326 if (source_filename != NULL)
00e52e53 2327 {
a70b8144 2328 try
00e52e53 2329 {
28a0d291 2330 result->file_symtabs
4717cec4 2331 = symtabs_from_filename (source_filename, self->search_pspace);
00e52e53 2332 }
230d2906 2333 catch (const gdb_exception_error &except)
00e52e53 2334 {
a2459270 2335 source_file_not_found_error (source_filename);
00e52e53 2336 }
a2459270 2337 result->explicit_loc.source_filename = xstrdup (source_filename);
00e52e53
KS
2338 }
2339 else
2340 {
2341 /* A NULL entry means to use the default symtab. */
28a0d291 2342 result->file_symtabs.push_back (nullptr);
00e52e53
KS
2343 }
2344
a2459270 2345 if (function_name != NULL)
00e52e53 2346 {
28a0d291
SM
2347 std::vector<block_symbol> symbols;
2348
00e52e53 2349 find_linespec_symbols (self, result->file_symtabs,
a20714ff
PA
2350 function_name, fname_match_type,
2351 &symbols, &minimal_symbols);
00e52e53 2352
c2a031c5 2353 if (symbols.empty () && minimal_symbols.empty ())
a2459270 2354 symbol_not_found_error (function_name,
67994074 2355 result->explicit_loc.source_filename);
00e52e53 2356
a2459270 2357 result->explicit_loc.function_name = xstrdup (function_name);
28a0d291
SM
2358 result->function_symbols = std::move (symbols);
2359 result->minimal_symbols = std::move (minimal_symbols);
00e52e53
KS
2360 }
2361
a2459270 2362 if (label_name != NULL)
00e52e53 2363 {
28a0d291
SM
2364 std::vector<block_symbol> symbols;
2365 std::vector<block_symbol> labels
2366 = find_label_symbols (self, result->function_symbols,
2367 &symbols, label_name);
00e52e53 2368
28a0d291 2369 if (labels.empty ())
67994074 2370 undefined_label_error (result->explicit_loc.function_name,
a2459270 2371 label_name);
00e52e53 2372
a2459270 2373 result->explicit_loc.label_name = xstrdup (label_name);
00e52e53 2374 result->labels.label_symbols = labels;
28a0d291 2375 result->labels.function_symbols = std::move (symbols);
00e52e53
KS
2376 }
2377
a2459270
PA
2378 if (line_offset.sign != LINE_OFFSET_UNKNOWN)
2379 result->explicit_loc.line_offset = line_offset;
2380}
00e52e53 2381
a2459270
PA
2382/* Convert the explicit location EXPLICIT_LOC into SaLs. */
2383
6c5b2ebe 2384static std::vector<symtab_and_line>
40d97ee2
PA
2385convert_explicit_location_spec_to_sals
2386 (struct linespec_state *self,
2387 linespec *result,
2388 const explicit_location_spec *explicit_spec)
2389{
2390 convert_explicit_location_spec_to_linespec (self, result,
2391 explicit_spec->source_filename,
2392 explicit_spec->function_name,
2393 explicit_spec->func_name_match_type,
2394 explicit_spec->label_name,
2395 explicit_spec->line_offset);
a2459270 2396 return convert_linespec_to_sals (self, result);
00e52e53
KS
2397}
2398
40e084e1 2399/* Parse a string that specifies a linespec.
50641945 2400
40e084e1 2401 The basic grammar of linespecs:
50641945 2402
a06efdd6 2403 linespec -> var_spec | basic_spec
40e084e1 2404 var_spec -> '$' (STRING | NUMBER)
50641945 2405
40e084e1
KS
2406 basic_spec -> file_offset_spec | function_spec | label_spec
2407 file_offset_spec -> opt_file_spec offset_spec
2408 function_spec -> opt_file_spec function_name_spec opt_label_spec
2409 label_spec -> label_name_spec
50641945 2410
40e084e1
KS
2411 opt_file_spec -> "" | file_name_spec ':'
2412 opt_label_spec -> "" | ':' label_name_spec
2413
2414 file_name_spec -> STRING
2415 function_name_spec -> STRING
2416 label_name_spec -> STRING
2417 function_name_spec -> STRING
2418 offset_spec -> NUMBER
dda83cd7 2419 -> '+' NUMBER
40e084e1
KS
2420 -> '-' NUMBER
2421
2422 This may all be followed by several keywords such as "if EXPR",
2423 which we ignore.
2424
2425 A comma will terminate parsing.
2426
2427 The function may be an undebuggable function found in minimal symbol table.
50641945
FN
2428
2429 If the argument FUNFIRSTLINE is nonzero, we want the first line
2430 of real code inside a function when a function is specified, and it is
2431 not OK to specify a variable or type to get its line number.
2432
2433 DEFAULT_SYMTAB specifies the file to use if none is specified.
2434 It defaults to current_source_symtab.
2435 DEFAULT_LINE specifies the line number to use for relative
2436 line numbers (that start with signs). Defaults to current_source_line.
2437 If CANONICAL is non-NULL, store an array of strings containing the canonical
1777feb0 2438 line specs there if necessary. Currently overloaded member functions and
50641945 2439 line numbers or static functions without a filename yield a canonical
1777feb0 2440 line spec. The array and the line spec strings are allocated on the heap,
50641945
FN
2441 it is the callers responsibility to free them.
2442
2443 Note that it is possible to return zero for the symtab
2444 if no file is validly specified. Callers must check that.
58438ac1 2445 Also, the line number returned may be invalid. */
50641945 2446
8e454b9c
AB
2447/* Parse the linespec in ARG, which must not be nullptr. MATCH_TYPE
2448 indicates how function names should be matched. */
50641945 2449
6c5b2ebe 2450static std::vector<symtab_and_line>
a20714ff
PA
2451parse_linespec (linespec_parser *parser, const char *arg,
2452 symbol_name_match_type match_type)
50641945 2453{
8e454b9c
AB
2454 gdb_assert (arg != nullptr);
2455
cc06b668 2456 struct gdb_exception file_exception;
40e084e1
KS
2457
2458 /* A special case to start. It has become quite popular for
2459 IDEs to work around bugs in the previous parser by quoting
2460 the entire linespec, so we attempt to deal with this nicely. */
2461 parser->is_quote_enclosed = 0;
c45ec17c
PA
2462 if (parser->completion_tracker == NULL
2463 && !is_ada_operator (arg)
8e454b9c 2464 && *arg != '\0'
f00aae0f 2465 && strchr (linespec_quote_characters, *arg) != NULL)
40e084e1 2466 {
8e454b9c 2467 const char *end = skip_quote_char (arg + 1, *arg);
40e084e1 2468 if (end != NULL && is_closing_quote_enclosed (end))
136e1c30 2469 {
f00aae0f 2470 /* Here's the special case. Skip ARG past the initial
40e084e1 2471 quote. */
f00aae0f 2472 ++arg;
40e084e1 2473 parser->is_quote_enclosed = 1;
136e1c30
DE
2474 }
2475 }
e8eb7bc5 2476
f00aae0f
KS
2477 parser->lexer.saved_arg = arg;
2478 parser->lexer.stream = arg;
c45ec17c
PA
2479 parser->completion_word = arg;
2480 parser->complete_what = linespec_complete_what::FUNCTION;
a20714ff 2481 PARSER_EXPLICIT (parser)->func_name_match_type = match_type;
d2630e69 2482
40e084e1
KS
2483 /* Initialize the default symtab and line offset. */
2484 initialize_defaults (&PARSER_STATE (parser)->default_symtab,
2485 &PARSER_STATE (parser)->default_line);
d2630e69 2486
40e084e1 2487 /* Objective-C shortcut. */
c45ec17c
PA
2488 if (parser->completion_tracker == NULL)
2489 {
6c5b2ebe
PA
2490 std::vector<symtab_and_line> values
2491 = decode_objc (PARSER_STATE (parser), PARSER_RESULT (parser), arg);
2492 if (!values.empty ())
c45ec17c
PA
2493 return values;
2494 }
2495 else
2496 {
2497 /* "-"/"+" is either an objc selector, or a number. There's
2498 nothing to complete the latter to, so just let the caller
2499 complete on functions, which finds objc selectors, if there's
2500 any. */
2501 if ((arg[0] == '-' || arg[0] == '+') && arg[1] == '\0')
2502 return {};
2503 }
e0881a8e 2504
40e084e1 2505 /* Start parsing. */
d2630e69 2506
40e084e1 2507 /* Get the first token. */
8e454b9c 2508 linespec_token token = linespec_lexer_consume_token (parser);
50641945 2509
40e084e1 2510 /* It must be either LSTOKEN_STRING or LSTOKEN_NUMBER. */
a06efdd6 2511 if (token.type == LSTOKEN_STRING && *LS_TOKEN_STOKEN (token).ptr == '$')
40e084e1 2512 {
40e084e1 2513 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
c45ec17c 2514 if (parser->completion_tracker == NULL)
28a0d291 2515 PARSER_RESULT (parser)->file_symtabs.push_back (nullptr);
dcf9f4ab 2516
40e084e1 2517 /* User specified a convenience variable or history value. */
a5b5adf5 2518 gdb::unique_xmalloc_ptr<char> var = copy_token_string (token);
00e52e53 2519 PARSER_EXPLICIT (parser)->line_offset
a5b5adf5 2520 = linespec_parse_variable (PARSER_STATE (parser), var.get ());
f8eba3c6 2521
40e084e1
KS
2522 /* If a line_offset wasn't found (VAR is the name of a user
2523 variable/function), then skip to normal symbol processing. */
00e52e53 2524 if (PARSER_EXPLICIT (parser)->line_offset.sign != LINE_OFFSET_UNKNOWN)
40e084e1 2525 {
40e084e1
KS
2526 /* Consume this token. */
2527 linespec_lexer_consume_token (parser);
dcf9f4ab 2528
40e084e1 2529 goto convert_to_sals;
50641945 2530 }
40e084e1 2531 }
c45ec17c
PA
2532 else if (token.type == LSTOKEN_EOI && parser->completion_tracker != NULL)
2533 {
2534 /* Let the default linespec_complete_what::FUNCTION kick in. */
2535 unexpected_linespec_error (parser);
2536 }
40e084e1 2537 else if (token.type != LSTOKEN_STRING && token.type != LSTOKEN_NUMBER)
c45ec17c
PA
2538 {
2539 parser->complete_what = linespec_complete_what::NOTHING;
2540 unexpected_linespec_error (parser);
2541 }
50641945 2542
40e084e1
KS
2543 /* Shortcut: If the next token is not LSTOKEN_COLON, we know that
2544 this token cannot represent a filename. */
2545 token = linespec_lexer_peek_token (parser);
0e0b460e 2546
40e084e1 2547 if (token.type == LSTOKEN_COLON)
0e0b460e 2548 {
40e084e1
KS
2549 /* Get the current token again and extract the filename. */
2550 token = linespec_lexer_lex_one (parser);
a5b5adf5 2551 gdb::unique_xmalloc_ptr<char> user_filename = copy_token_string (token);
50641945 2552
40e084e1 2553 /* Check if the input is a filename. */
a70b8144 2554 try
40e084e1 2555 {
28a0d291 2556 PARSER_RESULT (parser)->file_symtabs
a5b5adf5 2557 = symtabs_from_filename (user_filename.get (),
c2f4122d 2558 PARSER_STATE (parser)->search_pspace);
40e084e1 2559 }
94aeb44b 2560 catch (gdb_exception_error &ex)
7556d4a4 2561 {
94aeb44b 2562 file_exception = std::move (ex);
7556d4a4 2563 }
50641945 2564
40e084e1
KS
2565 if (file_exception.reason >= 0)
2566 {
2567 /* Symtabs were found for the file. Record the filename. */
a5b5adf5 2568 PARSER_EXPLICIT (parser)->source_filename = user_filename.release ();
f8eba3c6 2569
40e084e1
KS
2570 /* Get the next token. */
2571 token = linespec_lexer_consume_token (parser);
50641945 2572
40e084e1
KS
2573 /* This is LSTOKEN_COLON; consume it. */
2574 linespec_lexer_consume_token (parser);
2575 }
2576 else
2577 {
40e084e1 2578 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
28a0d291 2579 PARSER_RESULT (parser)->file_symtabs.push_back (nullptr);
40e084e1 2580 }
50641945 2581 }
40e084e1 2582 /* If the next token is not EOI, KEYWORD, or COMMA, issue an error. */
c45ec17c
PA
2583 else if (parser->completion_tracker == NULL
2584 && (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD
2585 && token.type != LSTOKEN_COMMA))
d2630e69 2586 {
40e084e1
KS
2587 /* TOKEN is the _next_ token, not the one currently in the parser.
2588 Consuming the token will give the correct error message. */
2589 linespec_lexer_consume_token (parser);
2590 unexpected_linespec_error (parser);
d2630e69 2591 }
50641945
FN
2592 else
2593 {
40e084e1 2594 /* A NULL entry means to use GLOBAL_DEFAULT_SYMTAB. */
28a0d291 2595 PARSER_RESULT (parser)->file_symtabs.push_back (nullptr);
50641945 2596 }
50641945 2597
40e084e1
KS
2598 /* Parse the rest of the linespec. */
2599 linespec_parse_basic (parser);
50641945 2600
c45ec17c 2601 if (parser->completion_tracker == NULL
28a0d291
SM
2602 && PARSER_RESULT (parser)->function_symbols.empty ()
2603 && PARSER_RESULT (parser)->labels.label_symbols.empty ()
00e52e53 2604 && PARSER_EXPLICIT (parser)->line_offset.sign == LINE_OFFSET_UNKNOWN
28a0d291 2605 && PARSER_RESULT (parser)->minimal_symbols.empty ())
f8eba3c6 2606 {
40e084e1
KS
2607 /* The linespec didn't parse. Re-throw the file exception if
2608 there was one. */
2609 if (file_exception.reason < 0)
94aeb44b 2610 throw_exception (std::move (file_exception));
0f5238ed 2611
40e084e1 2612 /* Otherwise, the symbol is not found. */
00e52e53
KS
2613 symbol_not_found_error (PARSER_EXPLICIT (parser)->function_name,
2614 PARSER_EXPLICIT (parser)->source_filename);
0f5238ed
TT
2615 }
2616
40e084e1 2617 convert_to_sals:
9ef07c8c 2618
40e084e1
KS
2619 /* Get the last token and record how much of the input was parsed,
2620 if necessary. */
2621 token = linespec_lexer_lex_one (parser);
2622 if (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD)
c45ec17c
PA
2623 unexpected_linespec_error (parser);
2624 else if (token.type == LSTOKEN_KEYWORD)
2625 {
2626 /* Setup the completion word past the keyword. Lexing never
2627 advances past a keyword automatically, so skip it
2628 manually. */
2629 parser->completion_word
f1735a53 2630 = skip_spaces (skip_to_space (PARSER_STREAM (parser)));
c45ec17c
PA
2631 parser->complete_what = linespec_complete_what::EXPRESSION;
2632 }
50641945 2633
40e084e1 2634 /* Convert the data in PARSER_RESULT to SALs. */
c45ec17c 2635 if (parser->completion_tracker == NULL)
6c5b2ebe
PA
2636 return convert_linespec_to_sals (PARSER_STATE (parser),
2637 PARSER_RESULT (parser));
f8eba3c6 2638
6c5b2ebe 2639 return {};
413dad4d 2640}
50641945 2641
40e084e1 2642
f8eba3c6 2643/* A constructor for linespec_state. */
44fe14ab 2644
f8eba3c6
TT
2645static void
2646linespec_state_constructor (struct linespec_state *self,
40e084e1 2647 int flags, const struct language_defn *language,
c2f4122d 2648 struct program_space *search_pspace,
f8eba3c6
TT
2649 struct symtab *default_symtab,
2650 int default_line,
2651 struct linespec_result *canonical)
2652{
2653 memset (self, 0, sizeof (*self));
40e084e1 2654 self->language = language;
f8eba3c6
TT
2655 self->funfirstline = (flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0;
2656 self->list_mode = (flags & DECODE_LINE_LIST_MODE) ? 1 : 0;
c2f4122d 2657 self->search_pspace = search_pspace;
f8eba3c6
TT
2658 self->default_symtab = default_symtab;
2659 self->default_line = default_line;
2660 self->canonical = canonical;
2661 self->program_space = current_program_space;
2662 self->addr_set = htab_create_alloc (10, hash_address_entry, eq_address_entry,
2663 xfree, xcalloc, xfree);
00e52e53 2664 self->is_linespec = 0;
f8eba3c6 2665}
44fe14ab 2666
40e084e1 2667/* Initialize a new linespec parser. */
44fe14ab 2668
3a6ae42d
TT
2669linespec_parser::linespec_parser (int flags,
2670 const struct language_defn *language,
2671 struct program_space *search_pspace,
2672 struct symtab *default_symtab,
2673 int default_line,
2674 struct linespec_result *canonical)
2675{
2676 lexer.current.type = LSTOKEN_CONSUMED;
3a6ae42d 2677 PARSER_EXPLICIT (this)->func_name_match_type
a20714ff 2678 = symbol_name_match_type::WILD;
3a6ae42d
TT
2679 PARSER_EXPLICIT (this)->line_offset.sign = LINE_OFFSET_UNKNOWN;
2680 linespec_state_constructor (PARSER_STATE (this), flags, language,
c2f4122d 2681 search_pspace,
40e084e1
KS
2682 default_symtab, default_line, canonical);
2683}
2684
2685/* A destructor for linespec_state. */
44fe14ab 2686
40e084e1
KS
2687static void
2688linespec_state_destructor (struct linespec_state *self)
2689{
f8eba3c6 2690 htab_delete (self->addr_set);
1055a3b4 2691 xfree (self->canonical_names);
f8eba3c6 2692}
44fe14ab 2693
40e084e1
KS
2694/* Delete a linespec parser. */
2695
3a6ae42d 2696linespec_parser::~linespec_parser ()
40e084e1 2697{
3a6ae42d 2698 linespec_state_destructor (PARSER_STATE (this));
40e084e1
KS
2699}
2700
c7c1b3e9
KS
2701/* See description in linespec.h. */
2702
2703void
f2fc3015 2704linespec_lex_to_end (const char **stringp)
c7c1b3e9 2705{
c7c1b3e9 2706 linespec_token token;
c7c1b3e9
KS
2707 const char *orig;
2708
2709 if (stringp == NULL || *stringp == NULL)
2710 return;
2711
3a6ae42d 2712 linespec_parser parser (0, current_language, NULL, NULL, 0, NULL);
c7c1b3e9
KS
2713 parser.lexer.saved_arg = *stringp;
2714 PARSER_STREAM (&parser) = orig = *stringp;
2715
2716 do
2717 {
2718 /* Stop before any comma tokens; we need it to keep it
2719 as the next token in the string. */
2720 token = linespec_lexer_peek_token (&parser);
2721 if (token.type == LSTOKEN_COMMA)
2722 break;
c7c1b3e9
KS
2723 token = linespec_lexer_consume_token (&parser);
2724 }
2725 while (token.type != LSTOKEN_EOI && token.type != LSTOKEN_KEYWORD);
2726
2727 *stringp += PARSER_STREAM (&parser) - orig;
c7c1b3e9
KS
2728}
2729
c6756f62
PA
2730/* See linespec.h. */
2731
2732void
2733linespec_complete_function (completion_tracker &tracker,
2734 const char *function,
a20714ff 2735 symbol_name_match_type func_match_type,
c6756f62
PA
2736 const char *source_filename)
2737{
2738 complete_symbol_mode mode = complete_symbol_mode::LINESPEC;
2739
2740 if (source_filename != NULL)
2741 {
b5ec771e
PA
2742 collect_file_symbol_completion_matches (tracker, mode, func_match_type,
2743 function, function, source_filename);
c6756f62
PA
2744 }
2745 else
b5ec771e
PA
2746 {
2747 collect_symbol_completion_matches (tracker, mode, func_match_type,
2748 function, function);
2749
2750 }
c6756f62
PA
2751}
2752
c45ec17c
PA
2753/* Helper for complete_linespec to simplify it. SOURCE_FILENAME is
2754 only meaningful if COMPONENT is FUNCTION. */
2755
2756static void
2757complete_linespec_component (linespec_parser *parser,
2758 completion_tracker &tracker,
2759 const char *text,
2760 linespec_complete_what component,
2761 const char *source_filename)
2762{
2763 if (component == linespec_complete_what::KEYWORD)
2764 {
2765 complete_on_enum (tracker, linespec_keywords, text, text);
2766 }
2767 else if (component == linespec_complete_what::EXPRESSION)
2768 {
2769 const char *word
2770 = advance_to_expression_complete_word_point (tracker, text);
2771 complete_expression (tracker, text, word);
2772 }
2773 else if (component == linespec_complete_what::FUNCTION)
2774 {
2775 completion_list fn_list;
2776
a20714ff
PA
2777 symbol_name_match_type match_type
2778 = PARSER_EXPLICIT (parser)->func_name_match_type;
2779 linespec_complete_function (tracker, text, match_type, source_filename);
c45ec17c
PA
2780 if (source_filename == NULL)
2781 {
2782 /* Haven't seen a source component, like in "b
2783 file.c:function[TAB]". Maybe this wasn't a function, but
2784 a filename instead, like "b file.[TAB]". */
2785 fn_list = complete_source_filenames (text);
2786 }
2787
2788 /* If we only have a single filename completion, append a ':' for
2789 the user, since that's the only thing that can usefully follow
2790 the filename. */
2791 if (fn_list.size () == 1 && !tracker.have_completions ())
2792 {
2793 char *fn = fn_list[0].release ();
2794
2795 /* If we also need to append a quote char, it needs to be
2796 appended before the ':'. Append it now, and make ':' the
2797 new "quote" char. */
2798 if (tracker.quote_char ())
2799 {
896a7aa6 2800 char quote_char_str[2] = { (char) tracker.quote_char () };
c45ec17c
PA
2801
2802 fn = reconcat (fn, fn, quote_char_str, (char *) NULL);
2803 tracker.set_quote_char (':');
2804 }
2805 else
2806 fn = reconcat (fn, fn, ":", (char *) NULL);
2807 fn_list[0].reset (fn);
2808
2809 /* Tell readline to skip appending a space. */
2810 tracker.set_suppress_append_ws (true);
2811 }
2812 tracker.add_completions (std::move (fn_list));
2813 }
2814}
2815
a2459270
PA
2816/* Helper for linespec_complete_label. Find labels that match
2817 LABEL_NAME in the function symbols listed in the PARSER, and add
2818 them to the tracker. */
2819
2820static void
2821complete_label (completion_tracker &tracker,
2822 linespec_parser *parser,
2823 const char *label_name)
2824{
7e41c8db 2825 std::vector<block_symbol> label_function_symbols;
28a0d291 2826 std::vector<block_symbol> labels
a2459270
PA
2827 = find_label_symbols (PARSER_STATE (parser),
2828 PARSER_RESULT (parser)->function_symbols,
2829 &label_function_symbols,
2830 label_name, true);
2831
28a0d291 2832 for (const auto &label : labels)
a2459270 2833 {
28a0d291
SM
2834 char *match = xstrdup (label.symbol->search_name ());
2835 tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
a2459270 2836 }
a2459270
PA
2837}
2838
2839/* See linespec.h. */
2840
2841void
2842linespec_complete_label (completion_tracker &tracker,
2843 const struct language_defn *language,
2844 const char *source_filename,
2845 const char *function_name,
a20714ff 2846 symbol_name_match_type func_name_match_type,
a2459270
PA
2847 const char *label_name)
2848{
3a6ae42d 2849 linespec_parser parser (0, language, NULL, NULL, 0, NULL);
a2459270 2850
dfea48fc 2851 line_offset unknown_offset;
a2459270 2852
a70b8144 2853 try
a2459270 2854 {
40d97ee2
PA
2855 convert_explicit_location_spec_to_linespec (PARSER_STATE (&parser),
2856 PARSER_RESULT (&parser),
2857 source_filename,
2858 function_name,
2859 func_name_match_type,
2860 NULL, unknown_offset);
a2459270 2861 }
230d2906 2862 catch (const gdb_exception_error &ex)
a2459270 2863 {
a2459270
PA
2864 return;
2865 }
a2459270
PA
2866
2867 complete_label (tracker, &parser, label_name);
a2459270
PA
2868}
2869
c45ec17c
PA
2870/* See description in linespec.h. */
2871
2872void
a20714ff
PA
2873linespec_complete (completion_tracker &tracker, const char *text,
2874 symbol_name_match_type match_type)
c45ec17c 2875{
c45ec17c
PA
2876 const char *orig = text;
2877
3a6ae42d 2878 linespec_parser parser (0, current_language, NULL, NULL, 0, NULL);
c45ec17c 2879 parser.lexer.saved_arg = text;
a20714ff 2880 PARSER_EXPLICIT (&parser)->func_name_match_type = match_type;
c45ec17c
PA
2881 PARSER_STREAM (&parser) = text;
2882
2883 parser.completion_tracker = &tracker;
2884 PARSER_STATE (&parser)->is_linespec = 1;
2885
2886 /* Parse as much as possible. parser.completion_word will hold
2887 furthest completion point we managed to parse to. */
a70b8144 2888 try
c45ec17c 2889 {
a20714ff 2890 parse_linespec (&parser, text, match_type);
c45ec17c 2891 }
230d2906 2892 catch (const gdb_exception_error &except)
c45ec17c
PA
2893 {
2894 }
c45ec17c
PA
2895
2896 if (parser.completion_quote_char != '\0'
2897 && parser.completion_quote_end != NULL
2898 && parser.completion_quote_end[1] == '\0')
2899 {
2900 /* If completing a quoted string with the cursor right at
2901 terminating quote char, complete the completion word without
2902 interpretation, so that readline advances the cursor one
2903 whitespace past the quote, even if there's no match. This
2904 makes these cases behave the same:
2905
2906 before: "b function()"
2907 after: "b function() "
2908
2909 before: "b 'function()'"
2910 after: "b 'function()' "
2911
2912 and trusts the user in this case:
2913
2914 before: "b 'not_loaded_function_yet()'"
2915 after: "b 'not_loaded_function_yet()' "
2916 */
2917 parser.complete_what = linespec_complete_what::NOTHING;
2918 parser.completion_quote_char = '\0';
2919
2920 gdb::unique_xmalloc_ptr<char> text_copy
2921 (xstrdup (parser.completion_word));
2922 tracker.add_completion (std::move (text_copy));
2923 }
2924
2925 tracker.set_quote_char (parser.completion_quote_char);
2926
2927 if (parser.complete_what == linespec_complete_what::LABEL)
2928 {
2929 parser.complete_what = linespec_complete_what::NOTHING;
2930
2931 const char *func_name = PARSER_EXPLICIT (&parser)->function_name;
2932
7e41c8db 2933 std::vector<block_symbol> function_symbols;
c2a031c5 2934 std::vector<bound_minimal_symbol> minimal_symbols;
c45ec17c
PA
2935 find_linespec_symbols (PARSER_STATE (&parser),
2936 PARSER_RESULT (&parser)->file_symtabs,
a20714ff 2937 func_name, match_type,
c45ec17c
PA
2938 &function_symbols, &minimal_symbols);
2939
28a0d291
SM
2940 PARSER_RESULT (&parser)->function_symbols = std::move (function_symbols);
2941 PARSER_RESULT (&parser)->minimal_symbols = std::move (minimal_symbols);
c45ec17c
PA
2942
2943 complete_label (tracker, &parser, parser.completion_word);
2944 }
2945 else if (parser.complete_what == linespec_complete_what::FUNCTION)
2946 {
2947 /* While parsing/lexing, we didn't know whether the completion
2948 word completes to a unique function/source name already or
2949 not.
2950
2951 E.g.:
2952 "b function() <tab>"
2953 may need to complete either to:
2954 "b function() const"
2955 or to:
2956 "b function() if/thread/task"
2957
2958 Or, this:
2959 "b foo t"
2960 may need to complete either to:
2961 "b foo template_fun<T>()"
2962 with "foo" being the template function's return type, or to:
2963 "b foo thread/task"
2964
2965 Or, this:
2966 "b file<TAB>"
2967 may need to complete either to a source file name:
2968 "b file.c"
2969 or this, also a filename, but a unique completion:
2970 "b file.c:"
2971 or to a function name:
2972 "b file_function"
2973
2974 Address that by completing assuming source or function, and
2975 seeing if we find a completion that matches exactly the
2976 completion word. If so, then it must be a function (see note
2977 below) and we advance the completion word to the end of input
2978 and switch to KEYWORD completion mode.
2979
2980 Note: if we find a unique completion for a source filename,
2981 then it won't match the completion word, because the LCD will
2982 contain a trailing ':'. And if we're completing at or after
2983 the ':', then complete_linespec_component won't try to
2984 complete on source filenames. */
2985
c45ec17c
PA
2986 const char *word = parser.completion_word;
2987
2988 complete_linespec_component (&parser, tracker,
2989 parser.completion_word,
2990 linespec_complete_what::FUNCTION,
2991 PARSER_EXPLICIT (&parser)->source_filename);
2992
2993 parser.complete_what = linespec_complete_what::NOTHING;
2994
2995 if (tracker.quote_char ())
2996 {
2997 /* The function/file name was not close-quoted, so this
2998 can't be a keyword. Note: complete_linespec_component
2999 may have swapped the original quote char for ':' when we
3000 get here, but that still indicates the same. */
3001 }
3002 else if (!tracker.have_completions ())
3003 {
3004 size_t key_start;
3005 size_t wordlen = strlen (parser.completion_word);
3006
3007 key_start
3008 = string_find_incomplete_keyword_at_end (linespec_keywords,
3009 parser.completion_word,
3010 wordlen);
3011
3012 if (key_start != -1
3013 || (wordlen > 0
3014 && parser.completion_word[wordlen - 1] == ' '))
3015 {
3016 parser.completion_word += key_start;
3017 parser.complete_what = linespec_complete_what::KEYWORD;
3018 }
3019 }
3020 else if (tracker.completes_to_completion_word (word))
3021 {
3022 /* Skip the function and complete on keywords. */
3023 parser.completion_word += strlen (word);
3024 parser.complete_what = linespec_complete_what::KEYWORD;
3025 tracker.discard_completions ();
3026 }
3027 }
3028
3029 tracker.advance_custom_word_point_by (parser.completion_word - orig);
3030
3031 complete_linespec_component (&parser, tracker,
3032 parser.completion_word,
3033 parser.complete_what,
3034 PARSER_EXPLICIT (&parser)->source_filename);
3035
3036 /* If we're past the "filename:function:label:offset" linespec, and
3037 didn't find any match, then assume the user might want to create
3038 a pending breakpoint anyway and offer the keyword
3039 completions. */
3040 if (!parser.completion_quote_char
3041 && (parser.complete_what == linespec_complete_what::FUNCTION
3042 || parser.complete_what == linespec_complete_what::LABEL
3043 || parser.complete_what == linespec_complete_what::NOTHING)
3044 && !tracker.have_completions ())
3045 {
3046 const char *end
3047 = parser.completion_word + strlen (parser.completion_word);
3048
3049 if (end > orig && end[-1] == ' ')
3050 {
3051 tracker.advance_custom_word_point_by (end - parser.completion_word);
3052
3053 complete_linespec_component (&parser, tracker, end,
3054 linespec_complete_what::KEYWORD,
3055 NULL);
3056 }
3057 }
c45ec17c
PA
3058}
3059
f00aae0f 3060/* A helper function for decode_line_full and decode_line_1 to
264f9890 3061 turn LOCSPEC into std::vector<symtab_and_line>. */
f00aae0f 3062
6c5b2ebe 3063static std::vector<symtab_and_line>
264f9890
PA
3064location_spec_to_sals (linespec_parser *parser,
3065 const location_spec *locspec)
f00aae0f 3066{
6c5b2ebe 3067 std::vector<symtab_and_line> result;
f00aae0f 3068
7464aeaa 3069 switch (locspec->type ())
f00aae0f 3070 {
264f9890 3071 case LINESPEC_LOCATION_SPEC:
f00aae0f 3072 {
40d97ee2 3073 const linespec_location_spec *ls = as_linespec_location_spec (locspec);
00e52e53 3074 PARSER_STATE (parser)->is_linespec = 1;
40d97ee2 3075 result = parse_linespec (parser, ls->spec_string, ls->match_type);
f00aae0f
KS
3076 }
3077 break;
3078
264f9890 3079 case ADDRESS_LOCATION_SPEC:
305e13e6 3080 {
40d97ee2
PA
3081 const address_location_spec *addr_spec
3082 = as_address_location_spec (locspec);
3083 const char *addr_string = addr_spec->to_string ();
3084 CORE_ADDR addr;
305e13e6
JB
3085
3086 if (addr_string != NULL)
3087 {
9be2c17a 3088 addr = linespec_expression_to_pc (&addr_string);
305e13e6 3089 if (PARSER_STATE (parser)->canonical != NULL)
5c1ddcb6 3090 PARSER_STATE (parser)->canonical->locspec = locspec->clone ();
305e13e6 3091 }
40d97ee2
PA
3092 else
3093 addr = addr_spec->address;
305e13e6
JB
3094
3095 result = convert_address_location_to_sals (PARSER_STATE (parser),
3096 addr);
3097 }
a06efdd6
KS
3098 break;
3099
264f9890 3100 case EXPLICIT_LOCATION_SPEC:
00e52e53 3101 {
40d97ee2
PA
3102 const explicit_location_spec *explicit_locspec
3103 = as_explicit_location_spec (locspec);
3104 result = convert_explicit_location_spec_to_sals (PARSER_STATE (parser),
3105 PARSER_RESULT (parser),
3106 explicit_locspec);
00e52e53
KS
3107 }
3108 break;
3109
264f9890 3110 case PROBE_LOCATION_SPEC:
5b56227b
KS
3111 /* Probes are handled by their own decoders. */
3112 gdb_assert_not_reached ("attempt to decode probe location");
3113 break;
3114
f00aae0f 3115 default:
264f9890 3116 gdb_assert_not_reached ("unhandled location spec type");
f00aae0f
KS
3117 }
3118
3119 return result;
3120}
3121
f8eba3c6 3122/* See linespec.h. */
44fe14ab 3123
f8eba3c6 3124void
264f9890 3125decode_line_full (struct location_spec *locspec, int flags,
c2f4122d 3126 struct program_space *search_pspace,
f8eba3c6
TT
3127 struct symtab *default_symtab,
3128 int default_line, struct linespec_result *canonical,
3129 const char *select_mode,
3130 const char *filter)
44fe14ab 3131{
f73c6ece 3132 std::vector<const char *> filters;
40e084e1 3133 struct linespec_state *state;
f8eba3c6
TT
3134
3135 gdb_assert (canonical != NULL);
3136 /* The filter only makes sense for 'all'. */
3137 gdb_assert (filter == NULL || select_mode == multiple_symbols_all);
3138 gdb_assert (select_mode == NULL
3139 || select_mode == multiple_symbols_all
3140 || select_mode == multiple_symbols_ask
3141 || select_mode == multiple_symbols_cancel);
3142 gdb_assert ((flags & DECODE_LINE_LIST_MODE) == 0);
3143
3a6ae42d
TT
3144 linespec_parser parser (flags, current_language,
3145 search_pspace, default_symtab,
3146 default_line, canonical);
5ed8105e
PA
3147
3148 scoped_restore_current_program_space restore_pspace;
f8eba3c6 3149
264f9890
PA
3150 std::vector<symtab_and_line> result = location_spec_to_sals (&parser,
3151 locspec);
40e084e1 3152 state = PARSER_STATE (&parser);
f8eba3c6 3153
626d2320
TV
3154 if (result.size () == 0)
3155 throw_error (NOT_SUPPORTED_ERROR, _("Location %s not available"),
709438c7 3156 locspec->to_string ());
626d2320 3157
6c5b2ebe 3158 gdb_assert (result.size () == 1 || canonical->pre_expanded);
f8eba3c6
TT
3159 canonical->pre_expanded = 1;
3160
66f1999b 3161 /* Arrange for allocated canonical names to be freed. */
3a6ae42d
TT
3162 std::vector<gdb::unique_xmalloc_ptr<char>> hold_names;
3163 for (int i = 0; i < result.size (); ++i)
f8eba3c6 3164 {
3a6ae42d
TT
3165 gdb_assert (state->canonical_names[i].suffix != NULL);
3166 hold_names.emplace_back (state->canonical_names[i].suffix);
f8eba3c6
TT
3167 }
3168
3169 if (select_mode == NULL)
3170 {
29f94340 3171 if (top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
f8eba3c6
TT
3172 select_mode = multiple_symbols_all;
3173 else
3174 select_mode = multiple_symbols_select_mode ();
3175 }
3176
3177 if (select_mode == multiple_symbols_all)
3178 {
3179 if (filter != NULL)
3180 {
f73c6ece 3181 filters.push_back (filter);
40e084e1 3182 filter_results (state, &result, filters);
f8eba3c6
TT
3183 }
3184 else
40e084e1 3185 convert_results_to_lsals (state, &result);
f8eba3c6
TT
3186 }
3187 else
40e084e1 3188 decode_line_2 (state, &result, select_mode);
f8eba3c6
TT
3189}
3190
39cf75f7
DE
3191/* See linespec.h. */
3192
6c5b2ebe 3193std::vector<symtab_and_line>
264f9890 3194decode_line_1 (const location_spec *locspec, int flags,
c2f4122d 3195 struct program_space *search_pspace,
f8eba3c6
TT
3196 struct symtab *default_symtab,
3197 int default_line)
3198{
3a6ae42d
TT
3199 linespec_parser parser (flags, current_language,
3200 search_pspace, default_symtab,
3201 default_line, NULL);
5ed8105e
PA
3202
3203 scoped_restore_current_program_space restore_pspace;
f8eba3c6 3204
264f9890 3205 return location_spec_to_sals (&parser, locspec);
f8eba3c6
TT
3206}
3207
39cf75f7
DE
3208/* See linespec.h. */
3209
6c5b2ebe 3210std::vector<symtab_and_line>
f2fc3015 3211decode_line_with_current_source (const char *string, int flags)
39cf75f7 3212{
39cf75f7
DE
3213 if (string == 0)
3214 error (_("Empty line specification."));
3215
3216 /* We use whatever is set as the current source line. We do not try
3217 and get a default source symtab+line or it will recursively call us! */
6c5b2ebe 3218 symtab_and_line cursal = get_current_source_symtab_and_line ();
39cf75f7 3219
264f9890
PA
3220 location_spec_up locspec = string_to_location_spec (&string,
3221 current_language);
6c5b2ebe 3222 std::vector<symtab_and_line> sals
264f9890 3223 = decode_line_1 (locspec.get (), flags, NULL, cursal.symtab, cursal.line);
39cf75f7
DE
3224
3225 if (*string)
3226 error (_("Junk at end of line specification: %s"), string);
f00aae0f 3227
39cf75f7
DE
3228 return sals;
3229}
3230
3231/* See linespec.h. */
3232
6c5b2ebe 3233std::vector<symtab_and_line>
f2fc3015 3234decode_line_with_last_displayed (const char *string, int flags)
39cf75f7 3235{
39cf75f7
DE
3236 if (string == 0)
3237 error (_("Empty line specification."));
3238
264f9890
PA
3239 location_spec_up locspec = string_to_location_spec (&string,
3240 current_language);
6c5b2ebe
PA
3241 std::vector<symtab_and_line> sals
3242 = (last_displayed_sal_is_valid ()
264f9890 3243 ? decode_line_1 (locspec.get (), flags, NULL,
6c5b2ebe
PA
3244 get_last_displayed_symtab (),
3245 get_last_displayed_line ())
264f9890 3246 : decode_line_1 (locspec.get (), flags, NULL, NULL, 0));
39cf75f7
DE
3247
3248 if (*string)
3249 error (_("Junk at end of line specification: %s"), string);
f00aae0f 3250
39cf75f7
DE
3251 return sals;
3252}
3253
f8eba3c6
TT
3254\f
3255
85102364 3256/* First, some functions to initialize stuff at the beginning of the
f8eba3c6
TT
3257 function. */
3258
3259static void
3260initialize_defaults (struct symtab **default_symtab, int *default_line)
3261{
3262 if (*default_symtab == 0)
3263 {
3264 /* Use whatever we have for the default source line. We don't use
dda83cd7 3265 get_current_or_default_symtab_and_line as it can recurse and call
f8eba3c6
TT
3266 us back! */
3267 struct symtab_and_line cursal =
3268 get_current_source_symtab_and_line ();
3269
3270 *default_symtab = cursal.symtab;
3271 *default_line = cursal.line;
3272 }
3273}
3274
3275\f
3276
40e084e1
KS
3277/* Evaluate the expression pointed to by EXP_PTR into a CORE_ADDR,
3278 advancing EXP_PTR past any parsed text. */
f8eba3c6 3279
a06efdd6 3280CORE_ADDR
bbc13ae3 3281linespec_expression_to_pc (const char **exp_ptr)
f8eba3c6 3282{
f8eba3c6
TT
3283 if (current_program_space->executing_startup)
3284 /* The error message doesn't really matter, because this case
3285 should only hit during breakpoint reset. */
3286 throw_error (NOT_FOUND_ERROR, _("cannot evaluate expressions while "
3287 "program space is in startup"));
3288
40e084e1
KS
3289 (*exp_ptr)++;
3290 return value_as_address (parse_to_comma_and_eval (exp_ptr));
0960f083
DC
3291}
3292
3293\f
3294
d2630e69
AF
3295/* Here's where we recognise an Objective-C Selector. An Objective C
3296 selector may be implemented by more than one class, therefore it
3297 may represent more than one method/function. This gives us a
3298 situation somewhat analogous to C++ overloading. If there's more
3299 than one method that could represent the selector, then use some of
3300 the existing C++ code to let the user choose one. */
3301
6c5b2ebe 3302static std::vector<symtab_and_line>
1487a14e 3303decode_objc (struct linespec_state *self, linespec *ls, const char *arg)
d2630e69 3304{
f8eba3c6 3305 struct collect_info info;
9b2f8581 3306 std::vector<const char *> symbol_names;
d7561cbb 3307 const char *new_argptr;
f8eba3c6
TT
3308
3309 info.state = self;
2a908241
KS
3310 std::vector<symtab *> symtabs;
3311 symtabs.push_back (nullptr);
3312
3313 info.file_symtabs = &symtabs;
7243d011 3314
7e41c8db 3315 std::vector<block_symbol> symbols;
7243d011 3316 info.result.symbols = &symbols;
c2a031c5
KS
3317 std::vector<bound_minimal_symbol> minimal_symbols;
3318 info.result.minimal_symbols = &minimal_symbols;
f8eba3c6 3319
f00aae0f 3320 new_argptr = find_imps (arg, &symbol_names);
9b2f8581 3321 if (symbol_names.empty ())
2a908241 3322 return {};
d2630e69 3323
56d87ef7
PA
3324 add_all_symbol_names_from_pspace (&info, NULL, symbol_names,
3325 FUNCTIONS_DOMAIN);
d2630e69 3326
6c5b2ebe 3327 std::vector<symtab_and_line> values;
c2a031c5 3328 if (!symbols.empty () || !minimal_symbols.empty ())
d2630e69 3329 {
f8eba3c6 3330 char *saved_arg;
d2630e69 3331
224c3ddb 3332 saved_arg = (char *) alloca (new_argptr - arg + 1);
f00aae0f
KS
3333 memcpy (saved_arg, arg, new_argptr - arg);
3334 saved_arg[new_argptr - arg] = '\0';
d2630e69 3335
67994074 3336 ls->explicit_loc.function_name = xstrdup (saved_arg);
28a0d291
SM
3337 ls->function_symbols = std::move (symbols);
3338 ls->minimal_symbols = std::move (minimal_symbols);
40e084e1
KS
3339 values = convert_linespec_to_sals (self, ls);
3340
f8eba3c6 3341 if (self->canonical)
d2630e69 3342 {
f2fc3015
TT
3343 std::string holder;
3344 const char *str;
f00aae0f 3345
f8eba3c6 3346 self->canonical->pre_expanded = 1;
f00aae0f 3347
67994074 3348 if (ls->explicit_loc.source_filename)
f00aae0f 3349 {
f2fc3015
TT
3350 holder = string_printf ("%s:%s",
3351 ls->explicit_loc.source_filename,
3352 saved_arg);
3353 str = holder.c_str ();
f00aae0f 3354 }
f8eba3c6 3355 else
f2fc3015 3356 str = saved_arg;
f00aae0f 3357
264f9890
PA
3358 self->canonical->locspec
3359 = new_linespec_location_spec (&str, symbol_name_match_type::FULL);
d2630e69 3360 }
d2630e69
AF
3361 }
3362
40e084e1 3363 return values;
f8eba3c6 3364}
c00f8484 3365
ffdbe864
YQ
3366namespace {
3367
14bc53a8
PA
3368/* A function object that serves as symbol_found_callback_ftype
3369 callback for iterate_over_symbols. This is used by
3370 lookup_prefix_sym to collect type symbols. */
3371class decode_compound_collector
f8eba3c6 3372{
14bc53a8 3373public:
fc4007c9 3374 decode_compound_collector ()
7a8a5d47
TT
3375 : m_unique_syms (htab_create_alloc (1, htab_hash_pointer,
3376 htab_eq_pointer, NULL,
3377 xcalloc, xfree))
fc4007c9 3378 {
fc4007c9 3379 }
3a93a0c2 3380
4dedf84d 3381 /* Return all symbols collected. */
7e41c8db 3382 std::vector<block_symbol> release_symbols ()
14bc53a8 3383 {
4dedf84d 3384 return std::move (m_symbols);
14bc53a8 3385 }
c00f8484 3386
14bc53a8 3387 /* Callable as a symbol_found_callback_ftype callback. */
7e41c8db 3388 bool operator () (block_symbol *bsym);
14bc53a8
PA
3389
3390private:
3391 /* A hash table of all symbols we found. We use this to avoid
3392 adding any symbol more than once. */
7a8a5d47 3393 htab_up m_unique_syms;
14bc53a8
PA
3394
3395 /* The result vector. */
7e41c8db 3396 std::vector<block_symbol> m_symbols;
14bc53a8
PA
3397};
3398
3399bool
7e41c8db 3400decode_compound_collector::operator () (block_symbol *bsym)
f8eba3c6 3401{
f8eba3c6
TT
3402 void **slot;
3403 struct type *t;
7e41c8db 3404 struct symbol *sym = bsym->symbol;
614b3b14 3405
66d7f48f 3406 if (sym->aclass () != LOC_TYPEDEF)
14bc53a8 3407 return true; /* Continue iterating. */
f8eba3c6 3408
5f9c5a63 3409 t = sym->type ();
f168693b 3410 t = check_typedef (t);
78134374
SM
3411 if (t->code () != TYPE_CODE_STRUCT
3412 && t->code () != TYPE_CODE_UNION
3413 && t->code () != TYPE_CODE_NAMESPACE)
14bc53a8 3414 return true; /* Continue iterating. */
614b3b14 3415
7a8a5d47 3416 slot = htab_find_slot (m_unique_syms.get (), sym, INSERT);
f8eba3c6
TT
3417 if (!*slot)
3418 {
3419 *slot = sym;
7e41c8db 3420 m_symbols.push_back (*bsym);
f8eba3c6
TT
3421 }
3422
14bc53a8 3423 return true; /* Continue iterating. */
f8eba3c6 3424}
93d91629 3425
ffdbe864
YQ
3426} // namespace
3427
40e084e1 3428/* Return any symbols corresponding to CLASS_NAME in FILE_SYMTABS. */
93d91629 3429
7e41c8db 3430static std::vector<block_symbol>
2a908241 3431lookup_prefix_sym (struct linespec_state *state,
28a0d291 3432 const std::vector<symtab *> &file_symtabs,
40e084e1 3433 const char *class_name)
93d91629 3434{
14bc53a8 3435 decode_compound_collector collector;
e0881a8e 3436
b5ec771e
PA
3437 lookup_name_info lookup_name (class_name, symbol_name_match_type::FULL);
3438
28a0d291 3439 for (const auto &elt : file_symtabs)
f8eba3c6 3440 {
2a908241 3441 if (elt == nullptr)
f8eba3c6 3442 {
b5ec771e 3443 iterate_over_all_matching_symtabs (state, lookup_name,
56d87ef7
PA
3444 STRUCT_DOMAIN, ALL_DOMAIN,
3445 NULL, false, collector);
b5ec771e 3446 iterate_over_all_matching_symtabs (state, lookup_name,
56d87ef7
PA
3447 VAR_DOMAIN, ALL_DOMAIN,
3448 NULL, false, collector);
f8eba3c6
TT
3449 }
3450 else
3451 {
f8eba3c6
TT
3452 /* Program spaces that are executing startup should have
3453 been filtered out earlier. */
7678d130
SM
3454 program_space *pspace = elt->compunit ()->objfile ()->pspace;
3455
3456 gdb_assert (!pspace->executing_startup);
3457 set_current_program_space (pspace);
b5ec771e
PA
3458 iterate_over_file_blocks (elt, lookup_name, STRUCT_DOMAIN, collector);
3459 iterate_over_file_blocks (elt, lookup_name, VAR_DOMAIN, collector);
1e5a1abc
KS
3460 }
3461 }
3462
14bc53a8 3463 return collector.release_symbols ();
93d91629
DC
3464}
3465
7243d011
KS
3466/* A std::sort comparison function for symbols. The resulting order does
3467 not actually matter; we just need to be able to sort them so that
3468 symbols with the same program space end up next to each other. */
3469
3470static bool
7e41c8db 3471compare_symbols (const block_symbol &a, const block_symbol &b)
7243d011
KS
3472{
3473 uintptr_t uia, uib;
3474
4206d69e
TT
3475 uia = (uintptr_t) a.symbol->symtab ()->compunit ()->objfile ()->pspace;
3476 uib = (uintptr_t) b.symbol->symtab ()->compunit ()->objfile ()->pspace;
7243d011
KS
3477
3478 if (uia < uib)
3479 return true;
3480 if (uia > uib)
3481 return false;
3482
7e41c8db
KS
3483 uia = (uintptr_t) a.symbol;
3484 uib = (uintptr_t) b.symbol;
7243d011
KS
3485
3486 if (uia < uib)
3487 return true;
3488
3489 return false;
3490}
3491
40e084e1 3492/* Like compare_symbols but for minimal symbols. */
4224873a 3493
c2a031c5
KS
3494static bool
3495compare_msymbols (const bound_minimal_symbol &a, const bound_minimal_symbol &b)
4224873a 3496{
f8eba3c6
TT
3497 uintptr_t uia, uib;
3498
c2a031c5
KS
3499 uia = (uintptr_t) a.objfile->pspace;
3500 uib = (uintptr_t) a.objfile->pspace;
f8eba3c6
TT
3501
3502 if (uia < uib)
c2a031c5 3503 return true;
f8eba3c6 3504 if (uia > uib)
c2a031c5 3505 return false;
f8eba3c6 3506
c2a031c5
KS
3507 uia = (uintptr_t) a.minsym;
3508 uib = (uintptr_t) b.minsym;
f8eba3c6
TT
3509
3510 if (uia < uib)
c2a031c5 3511 return true;
f8eba3c6 3512
c2a031c5 3513 return false;
f8eba3c6
TT
3514}
3515
3516/* Look for all the matching instances of each symbol in NAMES. Only
3517 instances from PSPACE are considered; other program spaces are
3518 handled by our caller. If PSPACE is NULL, then all program spaces
3519 are considered. Results are stored into INFO. */
3520
3521static void
3522add_all_symbol_names_from_pspace (struct collect_info *info,
3523 struct program_space *pspace,
9b2f8581 3524 const std::vector<const char *> &names,
56d87ef7 3525 enum search_domain search_domain)
f8eba3c6 3526{
9b2f8581 3527 for (const char *iter : names)
b5ec771e
PA
3528 add_matching_symbols_to_info (iter,
3529 symbol_name_match_type::FULL,
56d87ef7 3530 search_domain, info, pspace);
f8eba3c6
TT
3531}
3532
3533static void
8e8d776e 3534find_superclass_methods (std::vector<struct type *> &&superclasses,
b5ec771e 3535 const char *name, enum language name_lang,
9b2f8581 3536 std::vector<const char *> *result_names)
f8eba3c6 3537{
9b2f8581 3538 size_t old_len = result_names->size ();
f8eba3c6 3539
f8eba3c6
TT
3540 while (1)
3541 {
8e8d776e 3542 std::vector<struct type *> new_supers;
f8eba3c6 3543
52941706 3544 for (type *t : superclasses)
b5ec771e 3545 find_methods (t, name_lang, name, result_names, &new_supers);
f8eba3c6 3546
8e8d776e 3547 if (result_names->size () != old_len || new_supers.empty ())
f8eba3c6 3548 break;
4224873a 3549
8e8d776e 3550 superclasses = std::move (new_supers);
f8eba3c6 3551 }
f8eba3c6
TT
3552}
3553
40e084e1
KS
3554/* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
3555 given by one of the symbols in SYM_CLASSES. Matches are returned
3556 in SYMBOLS (for debug symbols) and MINSYMS (for minimal symbols). */
f8eba3c6 3557
40e084e1 3558static void
28a0d291
SM
3559find_method (struct linespec_state *self,
3560 const std::vector<symtab *> &file_symtabs,
40e084e1 3561 const char *class_name, const char *method_name,
7e41c8db
KS
3562 std::vector<block_symbol> *sym_classes,
3563 std::vector<block_symbol> *symbols,
c2a031c5 3564 std::vector<bound_minimal_symbol> *minsyms)
f8eba3c6 3565{
9b2f8581 3566 size_t last_result_len;
8e8d776e 3567 std::vector<struct type *> superclass_vec;
9b2f8581 3568 std::vector<const char *> result_names;
f8eba3c6 3569 struct collect_info info;
4224873a 3570
f8eba3c6
TT
3571 /* Sort symbols so that symbols with the same program space are next
3572 to each other. */
4dedf84d
KS
3573 std::sort (sym_classes->begin (), sym_classes->end (),
3574 compare_symbols);
f8eba3c6
TT
3575
3576 info.state = self;
28a0d291 3577 info.file_symtabs = &file_symtabs;
7243d011 3578 info.result.symbols = symbols;
c2a031c5 3579 info.result.minimal_symbols = minsyms;
f8eba3c6
TT
3580
3581 /* Iterate over all the types, looking for the names of existing
40e084e1 3582 methods matching METHOD_NAME. If we cannot find a direct method in a
f8eba3c6
TT
3583 given program space, then we consider inherited methods; this is
3584 not ideal (ideal would be to respect C++ hiding rules), but it
3585 seems good enough and is what GDB has historically done. We only
3586 need to collect the names because later we find all symbols with
3587 those names. This loop is written in a somewhat funny way
3588 because we collect data across the program space before deciding
3589 what to do. */
f8eba3c6 3590 last_result_len = 0;
7e41c8db 3591 for (const auto &elt : *sym_classes)
f8eba3c6
TT
3592 {
3593 struct type *t;
3594 struct program_space *pspace;
7e41c8db 3595 struct symbol *sym = elt.symbol;
ea90f227 3596 unsigned int ix = &elt - &*sym_classes->begin ();
f8eba3c6
TT
3597
3598 /* Program spaces that are executing startup should have
3599 been filtered out earlier. */
4206d69e 3600 pspace = sym->symtab ()->compunit ()->objfile ()->pspace;
08be3fe3 3601 gdb_assert (!pspace->executing_startup);
f8eba3c6 3602 set_current_program_space (pspace);
5f9c5a63 3603 t = check_typedef (sym->type ());
c1b5c1eb 3604 find_methods (t, sym->language (),
b5ec771e 3605 method_name, &result_names, &superclass_vec);
f8eba3c6
TT
3606
3607 /* Handle all items from a single program space at once; and be
3608 sure not to miss the last batch. */
4dedf84d 3609 if (ix == sym_classes->size () - 1
f8eba3c6 3610 || (pspace
4206d69e 3611 != (sym_classes->at (ix + 1).symbol->symtab ()
7678d130 3612 ->compunit ()->objfile ()->pspace)))
4224873a 3613 {
f8eba3c6
TT
3614 /* If we did not find a direct implementation anywhere in
3615 this program space, consider superclasses. */
9b2f8581 3616 if (result_names.size () == last_result_len)
8e8d776e 3617 find_superclass_methods (std::move (superclass_vec), method_name,
c1b5c1eb 3618 sym->language (), &result_names);
f8eba3c6
TT
3619
3620 /* We have a list of candidate symbol names, so now we
3621 iterate over the symbol tables looking for all
3622 matches in this pspace. */
56d87ef7
PA
3623 add_all_symbol_names_from_pspace (&info, pspace, result_names,
3624 FUNCTIONS_DOMAIN);
f8eba3c6 3625
8e8d776e 3626 superclass_vec.clear ();
9b2f8581 3627 last_result_len = result_names.size ();
4224873a 3628 }
4224873a 3629 }
f8eba3c6 3630
c2a031c5
KS
3631 if (!symbols->empty () || !minsyms->empty ())
3632 return;
f8eba3c6 3633
40e084e1
KS
3634 /* Throw an NOT_FOUND_ERROR. This will be caught by the caller
3635 and other attempts to locate the symbol will be made. */
3636 throw_error (NOT_FOUND_ERROR, _("see caller, this text doesn't matter"));
f8eba3c6
TT
3637}
3638
3639\f
3640
ffdbe864
YQ
3641namespace {
3642
14bc53a8
PA
3643/* This function object is a callback for iterate_over_symtabs, used
3644 when collecting all matching symtabs. */
f8eba3c6 3645
14bc53a8 3646class symtab_collector
f8eba3c6 3647{
14bc53a8 3648public:
fc4007c9 3649 symtab_collector ()
7a8a5d47
TT
3650 : m_symtab_table (htab_create (1, htab_hash_pointer, htab_eq_pointer,
3651 NULL))
fc4007c9 3652 {
fc4007c9 3653 }
f8eba3c6 3654
14bc53a8
PA
3655 /* Callable as a symbol_found_callback_ftype callback. */
3656 bool operator () (symtab *sym);
f8eba3c6 3657
4717cec4
SM
3658 /* Return an rvalue reference to the collected symtabs. */
3659 std::vector<symtab *> &&release_symtabs ()
14bc53a8 3660 {
2a908241 3661 return std::move (m_symtabs);
14bc53a8
PA
3662 }
3663
3664private:
3665 /* The result vector of symtabs. */
4717cec4 3666 std::vector<symtab *> m_symtabs;
14bc53a8
PA
3667
3668 /* This is used to ensure the symtabs are unique. */
7a8a5d47 3669 htab_up m_symtab_table;
14bc53a8
PA
3670};
3671
3672bool
3673symtab_collector::operator () (struct symtab *symtab)
f8eba3c6 3674{
f8eba3c6
TT
3675 void **slot;
3676
7a8a5d47 3677 slot = htab_find_slot (m_symtab_table.get (), symtab, INSERT);
f8eba3c6 3678 if (!*slot)
4224873a 3679 {
f8eba3c6 3680 *slot = symtab;
4717cec4 3681 m_symtabs.push_back (symtab);
4224873a 3682 }
f8eba3c6 3683
14bc53a8 3684 return false;
4224873a
DC
3685}
3686
ffdbe864
YQ
3687} // namespace
3688
2a908241 3689/* Given a file name, return a list of all matching symtabs. If
c2f4122d
PA
3690 SEARCH_PSPACE is not NULL, the search is restricted to just that
3691 program space. */
f8eba3c6 3692
4717cec4 3693static std::vector<symtab *>
c2f4122d
PA
3694collect_symtabs_from_filename (const char *file,
3695 struct program_space *search_pspace)
f8eba3c6 3696{
14bc53a8 3697 symtab_collector collector;
f8eba3c6
TT
3698
3699 /* Find that file's data. */
c2f4122d
PA
3700 if (search_pspace == NULL)
3701 {
94c93c35 3702 for (struct program_space *pspace : program_spaces)
dda83cd7 3703 {
c2f4122d
PA
3704 if (pspace->executing_startup)
3705 continue;
f8eba3c6 3706
c2f4122d 3707 set_current_program_space (pspace);
14bc53a8 3708 iterate_over_symtabs (file, collector);
c2f4122d
PA
3709 }
3710 }
3711 else
3712 {
3713 set_current_program_space (search_pspace);
14bc53a8 3714 iterate_over_symtabs (file, collector);
c2f4122d 3715 }
f3c39e76 3716
14bc53a8 3717 return collector.release_symtabs ();
f8eba3c6
TT
3718}
3719
c2f4122d
PA
3720/* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is
3721 not NULL, the search is restricted to just that program space. */
f8eba3c6 3722
4717cec4 3723static std::vector<symtab *>
c2f4122d
PA
3724symtabs_from_filename (const char *filename,
3725 struct program_space *search_pspace)
40e084e1 3726{
4717cec4 3727 std::vector<symtab *> result
2a908241 3728 = collect_symtabs_from_filename (filename, search_pspace);
f8eba3c6 3729
4717cec4 3730 if (result.empty ())
f8eba3c6 3731 {
40e084e1
KS
3732 if (!have_full_symbols () && !have_partial_symbols ())
3733 throw_error (NOT_FOUND_ERROR,
3734 _("No symbol table is loaded. "
3735 "Use the \"file\" command."));
00e52e53 3736 source_file_not_found_error (filename);
f8eba3c6
TT
3737 }
3738
40e084e1 3739 return result;
84fba31b 3740}
f3c39e76 3741
fcaad03c
KS
3742/* See symtab.h. */
3743
3744void
3745symbol_searcher::find_all_symbols (const std::string &name,
3746 const struct language_defn *language,
3747 enum search_domain search_domain,
3748 std::vector<symtab *> *search_symtabs,
3749 struct program_space *search_pspace)
3750{
3751 symbol_searcher_collect_info info;
3752 struct linespec_state state;
3753
3754 memset (&state, 0, sizeof (state));
3755 state.language = language;
3756 info.state = &state;
3757
3758 info.result.symbols = &m_symbols;
3759 info.result.minimal_symbols = &m_minimal_symbols;
3760 std::vector<symtab *> all_symtabs;
3761 if (search_symtabs == nullptr)
3762 {
3763 all_symtabs.push_back (nullptr);
3764 search_symtabs = &all_symtabs;
3765 }
3766 info.file_symtabs = search_symtabs;
3767
3768 add_matching_symbols_to_info (name.c_str (), symbol_name_match_type::WILD,
3769 search_domain, &info, search_pspace);
3770}
3771
40e084e1
KS
3772/* Look up a function symbol named NAME in symtabs FILE_SYMTABS. Matching
3773 debug symbols are returned in SYMBOLS. Matching minimal symbols are
3774 returned in MINSYMS. */
14e91ac5 3775
40e084e1
KS
3776static void
3777find_function_symbols (struct linespec_state *state,
28a0d291 3778 const std::vector<symtab *> &file_symtabs, const char *name,
a20714ff 3779 symbol_name_match_type name_match_type,
7e41c8db 3780 std::vector<block_symbol> *symbols,
c2a031c5 3781 std::vector<bound_minimal_symbol> *minsyms)
14e91ac5 3782{
40e084e1 3783 struct collect_info info;
9b2f8581 3784 std::vector<const char *> symbol_names;
14e91ac5 3785
40e084e1 3786 info.state = state;
7243d011 3787 info.result.symbols = symbols;
c2a031c5 3788 info.result.minimal_symbols = minsyms;
28a0d291 3789 info.file_symtabs = &file_symtabs;
e0881a8e 3790
40e084e1 3791 /* Try NAME as an Objective-C selector. */
d7561cbb 3792 find_imps (name, &symbol_names);
9b2f8581 3793 if (!symbol_names.empty ())
c2f4122d 3794 add_all_symbol_names_from_pspace (&info, state->search_pspace,
56d87ef7 3795 symbol_names, FUNCTIONS_DOMAIN);
40e084e1 3796 else
a20714ff 3797 add_matching_symbols_to_info (name, name_match_type, FUNCTIONS_DOMAIN,
b5ec771e 3798 &info, state->search_pspace);
40e084e1
KS
3799}
3800
3801/* Find all symbols named NAME in FILE_SYMTABS, returning debug symbols
3802 in SYMBOLS and minimal symbols in MINSYMS. */
14e91ac5 3803
b1ae631a 3804static void
40e084e1 3805find_linespec_symbols (struct linespec_state *state,
28a0d291 3806 const std::vector<symtab *> &file_symtabs,
b5ec771e 3807 const char *lookup_name,
a20714ff 3808 symbol_name_match_type name_match_type,
7e41c8db 3809 std::vector <block_symbol> *symbols,
c2a031c5 3810 std::vector<bound_minimal_symbol> *minsyms)
40e084e1 3811{
596dc4ad
TT
3812 gdb::unique_xmalloc_ptr<char> canon
3813 = cp_canonicalize_string_no_typedefs (lookup_name);
3814 if (canon != nullptr)
3815 lookup_name = canon.get ();
2f408ecb 3816
cc81e1c6
DE
3817 /* It's important to not call expand_symtabs_matching unnecessarily
3818 as it can really slow things down (by unnecessarily expanding
3819 potentially 1000s of symtabs, which when debugging some apps can
3820 cost 100s of seconds). Avoid this to some extent by *first* calling
3821 find_function_symbols, and only if that doesn't find anything
3822 *then* call find_method. This handles two important cases:
3823 1) break (anonymous namespace)::foo
3824 2) break class::method where method is in class (and not a baseclass) */
14e91ac5 3825
cc81e1c6 3826 find_function_symbols (state, file_symtabs, lookup_name,
7243d011 3827 name_match_type, symbols, minsyms);
14e91ac5 3828
cc81e1c6
DE
3829 /* If we were unable to locate a symbol of the same name, try dividing
3830 the name into class and method names and searching the class and its
3831 baseclasses. */
c2a031c5 3832 if (symbols->empty () && minsyms->empty ())
40e084e1 3833 {
2f408ecb 3834 std::string klass, method;
cc81e1c6 3835 const char *last, *p, *scope_op;
14e91ac5 3836
cc81e1c6
DE
3837 /* See if we can find a scope operator and break this symbol
3838 name into namespaces${SCOPE_OPERATOR}class_name and method_name. */
3839 scope_op = "::";
3840 p = find_toplevel_string (lookup_name, scope_op);
14e91ac5 3841
cc81e1c6
DE
3842 last = NULL;
3843 while (p != NULL)
f8eba3c6 3844 {
cc81e1c6
DE
3845 last = p;
3846 p = find_toplevel_string (p + strlen (scope_op), scope_op);
f8eba3c6 3847 }
14e91ac5 3848
cc81e1c6
DE
3849 /* If no scope operator was found, there is nothing more we can do;
3850 we already attempted to lookup the entire name as a symbol
3851 and failed. */
3852 if (last == NULL)
2f408ecb 3853 return;
cc81e1c6
DE
3854
3855 /* LOOKUP_NAME points to the class name.
3856 LAST points to the method name. */
2f408ecb 3857 klass = std::string (lookup_name, last - lookup_name);
cc81e1c6
DE
3858
3859 /* Skip past the scope operator. */
3860 last += strlen (scope_op);
2f408ecb 3861 method = last;
cc81e1c6
DE
3862
3863 /* Find a list of classes named KLASS. */
7e41c8db 3864 std::vector<block_symbol> classes
4dedf84d
KS
3865 = lookup_prefix_sym (state, file_symtabs, klass.c_str ());
3866 if (!classes.empty ())
cc81e1c6
DE
3867 {
3868 /* Now locate a list of suitable methods named METHOD. */
a70b8144 3869 try
cc81e1c6 3870 {
2f408ecb
PA
3871 find_method (state, file_symtabs,
3872 klass.c_str (), method.c_str (),
4dedf84d 3873 &classes, symbols, minsyms);
cc81e1c6
DE
3874 }
3875
3876 /* If successful, we're done. If NOT_FOUND_ERROR
3877 was not thrown, rethrow the exception that we did get. */
230d2906 3878 catch (const gdb_exception_error &except)
7556d4a4
PA
3879 {
3880 if (except.error != NOT_FOUND_ERROR)
eedc3f4f 3881 throw;
7556d4a4 3882 }
cc81e1c6 3883 }
2f408ecb 3884 }
14e91ac5
DC
3885}
3886
a2459270
PA
3887/* Helper for find_label_symbols. Find all labels that match name
3888 NAME in BLOCK. Return all labels that match in FUNCTION_SYMBOLS.
3889 Return the actual function symbol in which the label was found in
3890 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
3891 interpreted as a label name prefix. Otherwise, only a label named
3892 exactly NAME match. */
3893
3894static void
3895find_label_symbols_in_block (const struct block *block,
3896 const char *name, struct symbol *fn_sym,
3897 bool completion_mode,
7e41c8db
KS
3898 std::vector<block_symbol> *result,
3899 std::vector<block_symbol> *label_funcs_ret)
a2459270
PA
3900{
3901 if (completion_mode)
3902 {
3903 struct block_iterator iter;
3904 struct symbol *sym;
3905 size_t name_len = strlen (name);
3906
3907 int (*cmp) (const char *, const char *, size_t);
3908 cmp = case_sensitivity == case_sensitive_on ? strncmp : strncasecmp;
3909
3910 ALL_BLOCK_SYMBOLS (block, iter, sym)
3911 {
c1b5c1eb 3912 if (symbol_matches_domain (sym->language (),
6c9c307c 3913 sym->domain (), LABEL_DOMAIN)
987012b8 3914 && cmp (sym->search_name (), name, name_len) == 0)
a2459270 3915 {
7e41c8db
KS
3916 result->push_back ({sym, block});
3917 label_funcs_ret->push_back ({fn_sym, block});
a2459270
PA
3918 }
3919 }
3920 }
3921 else
3922 {
7e41c8db
KS
3923 struct block_symbol label_sym
3924 = lookup_symbol (name, block, LABEL_DOMAIN, 0);
a2459270 3925
7e41c8db 3926 if (label_sym.symbol != NULL)
a2459270 3927 {
7e41c8db
KS
3928 result->push_back (label_sym);
3929 label_funcs_ret->push_back ({fn_sym, block});
a2459270
PA
3930 }
3931 }
3932}
3933
28a0d291 3934/* Return all labels that match name NAME in FUNCTION_SYMBOLS.
3553eadc
KS
3935
3936 Return the actual function symbol in which the label was found in
a2459270
PA
3937 LABEL_FUNC_RET. If COMPLETION_MODE is true, then NAME is
3938 interpreted as a label name prefix. Otherwise, only labels named
3939 exactly NAME match. */
0f5238ed 3940
7e41c8db 3941
28a0d291 3942static std::vector<block_symbol>
40e084e1 3943find_label_symbols (struct linespec_state *self,
28a0d291 3944 const std::vector<block_symbol> &function_symbols,
7e41c8db
KS
3945 std::vector<block_symbol> *label_funcs_ret,
3946 const char *name,
a2459270 3947 bool completion_mode)
0f5238ed 3948{
3977b71f 3949 const struct block *block;
40e084e1 3950 struct symbol *fn_sym;
7e41c8db 3951 std::vector<block_symbol> result;
9ef07c8c 3952
28a0d291 3953 if (function_symbols.empty ())
9ef07c8c 3954 {
f8eba3c6 3955 set_current_program_space (self->program_space);
4eeaa230 3956 block = get_current_search_block ();
f8eba3c6 3957
9ef07c8c 3958 for (;
6c00f721 3959 block && !block->function ();
f135fe72 3960 block = block->superblock ())
9ef07c8c 3961 ;
28a0d291 3962
9ef07c8c 3963 if (!block)
28a0d291
SM
3964 return {};
3965
6c00f721 3966 fn_sym = block->function ();
f8eba3c6 3967
a2459270
PA
3968 find_label_symbols_in_block (block, name, fn_sym, completion_mode,
3969 &result, label_funcs_ret);
40e084e1
KS
3970 }
3971 else
3972 {
28a0d291 3973 for (const auto &elt : function_symbols)
f8eba3c6 3974 {
7e41c8db 3975 fn_sym = elt.symbol;
7678d130 3976 set_current_program_space
4206d69e 3977 (fn_sym->symtab ()->compunit ()->objfile ()->pspace);
4aeddc50 3978 block = fn_sym->value_block ();
40e084e1 3979
7e41c8db 3980 find_label_symbols_in_block (block, name, fn_sym, completion_mode,
a2459270 3981 &result, label_funcs_ret);
f8eba3c6 3982 }
40e084e1 3983 }
f8eba3c6 3984
28a0d291 3985 return result;
40e084e1 3986}
f8eba3c6 3987
40e084e1
KS
3988\f
3989
3990/* A helper for create_sals_line_offset that handles the 'list_mode' case. */
3991
6c5b2ebe 3992static std::vector<symtab_and_line>
40e084e1 3993decode_digits_list_mode (struct linespec_state *self,
1487a14e 3994 linespec *ls,
40e084e1
KS
3995 struct symtab_and_line val)
3996{
40e084e1
KS
3997 gdb_assert (self->list_mode);
3998
6c5b2ebe
PA
3999 std::vector<symtab_and_line> values;
4000
28a0d291 4001 for (const auto &elt : ls->file_symtabs)
40e084e1
KS
4002 {
4003 /* The logic above should ensure this. */
4004 gdb_assert (elt != NULL);
4005
7678d130
SM
4006 program_space *pspace = elt->compunit ()->objfile ()->pspace;
4007 set_current_program_space (pspace);
40e084e1
KS
4008
4009 /* Simplistic search just for the list command. */
4010 val.symtab = find_line_symtab (elt, val.line, NULL, NULL);
4011 if (val.symtab == NULL)
4012 val.symtab = elt;
7678d130 4013 val.pspace = pspace;
40e084e1 4014 val.pc = 0;
5b0e2db4 4015 val.explicit_line = true;
40e084e1 4016
6c5b2ebe 4017 add_sal_to_sals (self, &values, &val, NULL, 0);
f8eba3c6 4018 }
6c5b2ebe
PA
4019
4020 return values;
40e084e1 4021}
f8eba3c6 4022
791b7405
AB
4023/* A helper for create_sals_line_offset that iterates over the symtabs
4024 associated with LS and returns a vector of corresponding symtab_and_line
4025 structures. */
40e084e1 4026
6c5b2ebe 4027static std::vector<symtab_and_line>
40e084e1 4028decode_digits_ordinary (struct linespec_state *self,
1487a14e 4029 linespec *ls,
40e084e1 4030 int line,
40e084e1
KS
4031 struct linetable_entry **best_entry)
4032{
6c5b2ebe 4033 std::vector<symtab_and_line> sals;
28a0d291 4034 for (const auto &elt : ls->file_symtabs)
f8eba3c6 4035 {
67d89901 4036 std::vector<CORE_ADDR> pcs;
40e084e1
KS
4037
4038 /* The logic above should ensure this. */
4039 gdb_assert (elt != NULL);
f8eba3c6 4040
7678d130
SM
4041 program_space *pspace = elt->compunit ()->objfile ()->pspace;
4042 set_current_program_space (pspace);
f8eba3c6 4043
40e084e1 4044 pcs = find_pcs_for_symtab_line (elt, line, best_entry);
67d89901 4045 for (CORE_ADDR pc : pcs)
f8eba3c6 4046 {
51abb421 4047 symtab_and_line sal;
7678d130 4048 sal.pspace = pspace;
40e084e1
KS
4049 sal.symtab = elt;
4050 sal.line = line;
5b0e2db4 4051 sal.explicit_line = true;
40e084e1 4052 sal.pc = pc;
6c5b2ebe 4053 sals.push_back (std::move (sal));
f8eba3c6
TT
4054 }
4055 }
6c5b2ebe
PA
4056
4057 return sals;
40e084e1
KS
4058}
4059
4060\f
4061
4062/* Return the line offset represented by VARIABLE. */
4063
4064static struct line_offset
4065linespec_parse_variable (struct linespec_state *self, const char *variable)
4066{
4067 int index = 0;
4068 const char *p;
dfea48fc 4069 line_offset offset;
f8eba3c6 4070
40e084e1
KS
4071 p = (variable[1] == '$') ? variable + 2 : variable + 1;
4072 if (*p == '$')
4073 ++p;
4074 while (*p >= '0' && *p <= '9')
4075 ++p;
4076 if (!*p) /* Reached end of token without hitting non-digit. */
f8eba3c6 4077 {
40e084e1
KS
4078 /* We have a value history reference. */
4079 struct value *val_history;
f8eba3c6 4080
40e084e1
KS
4081 sscanf ((variable[1] == '$') ? variable + 2 : variable + 1, "%d", &index);
4082 val_history
4083 = access_value_history ((variable[1] == '$') ? -index : index);
78134374 4084 if (value_type (val_history)->code () != TYPE_CODE_INT)
40e084e1
KS
4085 error (_("History values used in line "
4086 "specs must have integer values."));
4087 offset.offset = value_as_long (val_history);
dfea48fc 4088 offset.sign = LINE_OFFSET_NONE;
40e084e1
KS
4089 }
4090 else
4091 {
4092 /* Not all digits -- may be user variable/function or a
4093 convenience variable. */
4094 LONGEST valx;
4095 struct internalvar *ivar;
4096
4097 /* Try it as a convenience variable. If it is not a convenience
4098 variable, return and allow normal symbol lookup to occur. */
4099 ivar = lookup_only_internalvar (variable + 1);
dfea48fc
PA
4100 /* If there's no internal variable with that name, let the
4101 offset remain as unknown to allow the name to be looked up
4102 as a symbol. */
4103 if (ivar != nullptr)
40e084e1
KS
4104 {
4105 /* We found a valid variable name. If it is not an integer,
4106 throw an error. */
4107 if (!get_internalvar_integer (ivar, &valx))
4108 error (_("Convenience variables used in line "
4109 "specs must have integer values."));
4110 else
dfea48fc
PA
4111 {
4112 offset.offset = valx;
4113 offset.sign = LINE_OFFSET_NONE;
4114 }
40e084e1 4115 }
f8eba3c6
TT
4116 }
4117
40e084e1 4118 return offset;
f8eba3c6 4119}
40e084e1 4120\f
f8eba3c6 4121
40e084e1 4122/* We've found a minimal symbol MSYMBOL in OBJFILE to associate with our
6e22494e
JK
4123 linespec; return the SAL in RESULT. This function should return SALs
4124 matching those from find_function_start_sal, otherwise false
4125 multiple-locations breakpoints could be placed. */
f8eba3c6
TT
4126
4127static void
4128minsym_found (struct linespec_state *self, struct objfile *objfile,
4129 struct minimal_symbol *msymbol,
6c5b2ebe 4130 std::vector<symtab_and_line> *result)
f8eba3c6 4131{
3467ec66 4132 bool want_start_sal;
f8eba3c6 4133
4024cf2b 4134 CORE_ADDR func_addr;
3467ec66
PA
4135 bool is_function = msymbol_is_function (objfile, msymbol, &func_addr);
4136
4137 if (is_function)
4138 {
c9d95fa3 4139 const char *msym_name = msymbol->linkage_name ();
3467ec66 4140
60f62e2b
SM
4141 if (msymbol->type () == mst_text_gnu_ifunc
4142 || msymbol->type () == mst_data_gnu_ifunc)
3467ec66
PA
4143 want_start_sal = gnu_ifunc_resolve_name (msym_name, &func_addr);
4144 else
4145 want_start_sal = true;
4146 }
4147
4148 symtab_and_line sal;
4149
4150 if (is_function && want_start_sal)
42ddae10 4151 sal = find_function_start_sal (func_addr, NULL, self->funfirstline);
e5f25bc5
PA
4152 else
4153 {
4154 sal.objfile = objfile;
3467ec66
PA
4155 sal.msymbol = msymbol;
4156 /* Store func_addr, not the minsym's address in case this was an
4157 ifunc that hasn't been resolved yet. */
4158 if (is_function)
4159 sal.pc = func_addr;
4160 else
4aeddc50 4161 sal.pc = msymbol->value_address (objfile);
e5f25bc5 4162 sal.pspace = current_program_space;
6e22494e 4163 }
f8eba3c6 4164
ebbc3a7d 4165 sal.section = msymbol->obj_section (objfile);
6b05c8bd 4166
07fea4b4 4167 if (maybe_add_address (self->addr_set, objfile->pspace, sal.pc))
c9d95fa3 4168 add_sal_to_sals (self, result, &sal, msymbol->natural_name (), 0);
f8eba3c6
TT
4169}
4170
41c1efc6
TT
4171/* Helper for search_minsyms_for_name that adds the symbol to the
4172 result. */
39b856a4
TT
4173
4174static void
41c1efc6
TT
4175add_minsym (struct minimal_symbol *minsym, struct objfile *objfile,
4176 struct symtab *symtab, int list_mode,
4177 std::vector<struct bound_minimal_symbol> *msyms)
39b856a4 4178{
41c1efc6 4179 if (symtab != NULL)
87186c6a 4180 {
4024cf2b
PA
4181 /* We're looking for a label for which we don't have debug
4182 info. */
4183 CORE_ADDR func_addr;
41c1efc6 4184 if (msymbol_is_function (objfile, minsym, &func_addr))
4024cf2b
PA
4185 {
4186 symtab_and_line sal = find_pc_sect_line (func_addr, NULL, 0);
87186c6a 4187
41c1efc6 4188 if (symtab != sal.symtab)
4024cf2b
PA
4189 return;
4190 }
87186c6a
MMN
4191 }
4192
4024cf2b 4193 /* Exclude data symbols when looking for breakpoint locations. */
41c1efc6 4194 if (!list_mode && !msymbol_is_function (objfile, minsym))
4024cf2b 4195 return;
095bcf5e 4196
f6b3ad54 4197 msyms->emplace_back (minsym, objfile);
ca31ab1d 4198 return;
f8eba3c6
TT
4199}
4200
87186c6a 4201/* Search for minimal symbols called NAME. If SEARCH_PSPACE
f8eba3c6 4202 is not NULL, the search is restricted to just that program
87186c6a
MMN
4203 space.
4204
4205 If SYMTAB is NULL, search all objfiles, otherwise
4206 restrict results to the given SYMTAB. */
f8eba3c6
TT
4207
4208static void
b5ec771e
PA
4209search_minsyms_for_name (struct collect_info *info,
4210 const lookup_name_info &name,
87186c6a
MMN
4211 struct program_space *search_pspace,
4212 struct symtab *symtab)
f8eba3c6 4213{
41c1efc6 4214 std::vector<struct bound_minimal_symbol> minsyms;
f8eba3c6 4215
87186c6a
MMN
4216 if (symtab == NULL)
4217 {
94c93c35
TT
4218 for (struct program_space *pspace : program_spaces)
4219 {
4220 if (search_pspace != NULL && search_pspace != pspace)
4221 continue;
4222 if (pspace->executing_startup)
4223 continue;
39b856a4 4224
94c93c35 4225 set_current_program_space (pspace);
87186c6a 4226
94c93c35
TT
4227 for (objfile *objfile : current_program_space->objfiles ())
4228 {
4229 iterate_over_minimal_symbols (objfile, name,
4230 [&] (struct minimal_symbol *msym)
4231 {
4232 add_minsym (msym, objfile, nullptr,
4233 info->state->list_mode,
4234 &minsyms);
4235 return false;
4236 });
4237 }
4238 }
87186c6a
MMN
4239 }
4240 else
f8eba3c6 4241 {
7678d130
SM
4242 program_space *pspace = symtab->compunit ()->objfile ()->pspace;
4243
4244 if (search_pspace == NULL || pspace == search_pspace)
87186c6a 4245 {
7678d130 4246 set_current_program_space (pspace);
41c1efc6 4247 iterate_over_minimal_symbols
3c86fae3 4248 (symtab->compunit ()->objfile (), name,
41c1efc6
TT
4249 [&] (struct minimal_symbol *msym)
4250 {
3c86fae3 4251 add_minsym (msym, symtab->compunit ()->objfile (), symtab,
41c1efc6 4252 info->state->list_mode, &minsyms);
ca31ab1d 4253 return false;
41c1efc6 4254 });
87186c6a 4255 }
9ef07c8c 4256 }
39b856a4 4257
77f2120b
PA
4258 /* Return true if TYPE is a static symbol. */
4259 auto msymbol_type_is_static = [] (enum minimal_symbol_type type)
41c1efc6 4260 {
77f2120b
PA
4261 switch (type)
4262 {
4263 case mst_file_text:
4264 case mst_file_data:
4265 case mst_file_bss:
4266 return true;
4267 default:
4268 return false;
4269 }
4270 };
39b856a4 4271
77f2120b
PA
4272 /* Add minsyms to the result set, but filter out trampoline symbols
4273 if we also found extern symbols with the same name. I.e., don't
4274 set a breakpoint on both '<foo@plt>' and 'foo', assuming that
4275 'foo' is the symbol that the plt resolves to. */
4276 for (const bound_minimal_symbol &item : minsyms)
4277 {
4278 bool skip = false;
60f62e2b 4279 if (item.minsym->type () == mst_solib_trampoline)
77f2120b
PA
4280 {
4281 for (const bound_minimal_symbol &item2 : minsyms)
4282 {
4283 if (&item2 == &item)
4284 continue;
39b856a4 4285
77f2120b
PA
4286 /* Trampoline symbols can only jump to exported
4287 symbols. */
60f62e2b 4288 if (msymbol_type_is_static (item2.minsym->type ()))
77f2120b 4289 continue;
41c1efc6 4290
77f2120b
PA
4291 if (strcmp (item.minsym->linkage_name (),
4292 item2.minsym->linkage_name ()) != 0)
4293 continue;
41c1efc6 4294
77f2120b
PA
4295 /* Found a global minsym with the same name as the
4296 trampoline. Don't create a location for this
4297 trampoline. */
4298 skip = true;
4299 break;
4300 }
41c1efc6 4301 }
77f2120b
PA
4302
4303 if (!skip)
4304 info->result.minimal_symbols->push_back (item);
41c1efc6 4305 }
f8eba3c6
TT
4306}
4307
4308/* A helper function to add all symbols matching NAME to INFO. If
4309 PSPACE is not NULL, the search is restricted to just that program
4310 space. */
0f5238ed 4311
f8eba3c6
TT
4312static void
4313add_matching_symbols_to_info (const char *name,
b5ec771e 4314 symbol_name_match_type name_match_type,
56d87ef7 4315 enum search_domain search_domain,
f8eba3c6
TT
4316 struct collect_info *info,
4317 struct program_space *pspace)
4318{
b5ec771e
PA
4319 lookup_name_info lookup_name (name, name_match_type);
4320
2a908241 4321 for (const auto &elt : *info->file_symtabs)
f8eba3c6 4322 {
2a908241 4323 if (elt == nullptr)
f8eba3c6 4324 {
b5ec771e 4325 iterate_over_all_matching_symtabs (info->state, lookup_name,
56d87ef7 4326 VAR_DOMAIN, search_domain,
7e41c8db
KS
4327 pspace, true,
4328 [&] (block_symbol *bsym)
4329 { return info->add_symbol (bsym); });
b5ec771e 4330 search_minsyms_for_name (info, lookup_name, pspace, NULL);
f8eba3c6 4331 }
7678d130 4332 else if (pspace == NULL || pspace == elt->compunit ()->objfile ()->pspace)
f8eba3c6 4333 {
7243d011 4334 int prev_len = info->result.symbols->size ();
87186c6a 4335
f8eba3c6
TT
4336 /* Program spaces that are executing startup should have
4337 been filtered out earlier. */
7678d130
SM
4338 program_space *elt_pspace = elt->compunit ()->objfile ()->pspace;
4339 gdb_assert (!elt_pspace->executing_startup);
4340 set_current_program_space (elt_pspace);
b5ec771e 4341 iterate_over_file_blocks (elt, lookup_name, VAR_DOMAIN,
7e41c8db
KS
4342 [&] (block_symbol *bsym)
4343 { return info->add_symbol (bsym); });
87186c6a
MMN
4344
4345 /* If no new symbols were found in this iteration and this symtab
4346 is in assembler, we might actually be looking for a label for
4347 which we don't have debug info. Check for a minimal symbol in
4348 this case. */
7243d011 4349 if (prev_len == info->result.symbols->size ()
1ee2e9f9 4350 && elt->language () == language_asm)
b5ec771e 4351 search_minsyms_for_name (info, lookup_name, pspace, elt);
f8eba3c6
TT
4352 }
4353 }
0f5238ed
TT
4354}
4355
14e91ac5
DC
4356\f
4357
413dad4d
DC
4358/* Now come some functions that are called from multiple places within
4359 decode_line_1. */
4360
f8eba3c6
TT
4361static int
4362symbol_to_sal (struct symtab_and_line *result,
4363 int funfirstline, struct symbol *sym)
413dad4d 4364{
66d7f48f 4365 if (sym->aclass () == LOC_BLOCK)
50641945 4366 {
f8eba3c6
TT
4367 *result = find_function_start_sal (sym, funfirstline);
4368 return 1;
50641945 4369 }
413dad4d
DC
4370 else
4371 {
4aeddc50 4372 if (sym->aclass () == LOC_LABEL && sym->value_address () != 0)
413dad4d 4373 {
51abb421 4374 *result = {};
4206d69e 4375 result->symtab = sym->symtab ();
06871ae8 4376 result->symbol = sym;
5d0027b9 4377 result->line = sym->line ();
4aeddc50 4378 result->pc = sym->value_address ();
7678d130 4379 result->pspace = result->symtab->compunit ()->objfile ()->pspace;
f8eba3c6
TT
4380 result->explicit_pc = 1;
4381 return 1;
413dad4d 4382 }
62853458 4383 else if (funfirstline)
dcf9f4ab 4384 {
f8eba3c6 4385 /* Nothing. */
dcf9f4ab 4386 }
5d0027b9 4387 else if (sym->line () != 0)
62853458
TT
4388 {
4389 /* We know its line number. */
51abb421 4390 *result = {};
4206d69e 4391 result->symtab = sym->symtab ();
06871ae8 4392 result->symbol = sym;
5d0027b9 4393 result->line = sym->line ();
4aeddc50 4394 result->pc = sym->value_address ();
7678d130 4395 result->pspace = result->symtab->compunit ()->objfile ()->pspace;
f8eba3c6 4396 return 1;
62853458 4397 }
413dad4d 4398 }
f8eba3c6
TT
4399
4400 return 0;
413dad4d 4401}
50641945 4402
16e802b9 4403linespec_result::~linespec_result ()
f8eba3c6 4404{
6c5b2ebe
PA
4405 for (linespec_sals &lsal : lsals)
4406 xfree (lsal.canonical);
7efd8fc2 4407}
87f0e720
KS
4408
4409/* Return the quote characters permitted by the linespec parser. */
4410
4411const char *
4412get_gdb_linespec_parser_quote_characters (void)
4413{
4414 return linespec_quote_characters;
4415}