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