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