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