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