+ complete_label (tracker, &parser, label_name);
+}
+
+/* See description in linespec.h. */
+
+void
+linespec_complete (completion_tracker &tracker, const char *text,
+ symbol_name_match_type match_type)
+{
+ const char *orig = text;
+
+ linespec_parser parser (0, current_language, NULL, NULL, 0, NULL);
+ parser.lexer.saved_arg = text;
+ PARSER_EXPLICIT (&parser)->func_name_match_type = match_type;
+ PARSER_STREAM (&parser) = text;
+
+ parser.completion_tracker = &tracker;
+ PARSER_STATE (&parser)->is_linespec = 1;
+
+ /* Parse as much as possible. parser.completion_word will hold
+ furthest completion point we managed to parse to. */
+ TRY
+ {
+ parse_linespec (&parser, text, match_type);
+ }
+ CATCH (except, RETURN_MASK_ERROR)
+ {
+ }
+ END_CATCH
+
+ if (parser.completion_quote_char != '\0'
+ && parser.completion_quote_end != NULL
+ && parser.completion_quote_end[1] == '\0')
+ {
+ /* If completing a quoted string with the cursor right at
+ terminating quote char, complete the completion word without
+ interpretation, so that readline advances the cursor one
+ whitespace past the quote, even if there's no match. This
+ makes these cases behave the same:
+
+ before: "b function()"
+ after: "b function() "
+
+ before: "b 'function()'"
+ after: "b 'function()' "
+
+ and trusts the user in this case:
+
+ before: "b 'not_loaded_function_yet()'"
+ after: "b 'not_loaded_function_yet()' "
+ */
+ parser.complete_what = linespec_complete_what::NOTHING;
+ parser.completion_quote_char = '\0';
+
+ gdb::unique_xmalloc_ptr<char> text_copy
+ (xstrdup (parser.completion_word));
+ tracker.add_completion (std::move (text_copy));
+ }
+
+ tracker.set_quote_char (parser.completion_quote_char);
+
+ if (parser.complete_what == linespec_complete_what::LABEL)
+ {
+ parser.complete_what = linespec_complete_what::NOTHING;
+
+ const char *func_name = PARSER_EXPLICIT (&parser)->function_name;
+
+ std::vector<block_symbol> function_symbols;
+ std::vector<bound_minimal_symbol> minimal_symbols;
+ find_linespec_symbols (PARSER_STATE (&parser),
+ PARSER_RESULT (&parser)->file_symtabs,
+ func_name, match_type,
+ &function_symbols, &minimal_symbols);
+
+ PARSER_RESULT (&parser)->function_symbols
+ = new std::vector<block_symbol> (std::move (function_symbols));
+ PARSER_RESULT (&parser)->minimal_symbols
+ = new std::vector<bound_minimal_symbol> (std::move (minimal_symbols));
+
+ complete_label (tracker, &parser, parser.completion_word);
+ }
+ else if (parser.complete_what == linespec_complete_what::FUNCTION)
+ {
+ /* While parsing/lexing, we didn't know whether the completion
+ word completes to a unique function/source name already or
+ not.
+
+ E.g.:
+ "b function() <tab>"
+ may need to complete either to:
+ "b function() const"
+ or to:
+ "b function() if/thread/task"
+
+ Or, this:
+ "b foo t"
+ may need to complete either to:
+ "b foo template_fun<T>()"
+ with "foo" being the template function's return type, or to:
+ "b foo thread/task"
+
+ Or, this:
+ "b file<TAB>"
+ may need to complete either to a source file name:
+ "b file.c"
+ or this, also a filename, but a unique completion:
+ "b file.c:"
+ or to a function name:
+ "b file_function"
+
+ Address that by completing assuming source or function, and
+ seeing if we find a completion that matches exactly the
+ completion word. If so, then it must be a function (see note
+ below) and we advance the completion word to the end of input
+ and switch to KEYWORD completion mode.
+
+ Note: if we find a unique completion for a source filename,
+ then it won't match the completion word, because the LCD will
+ contain a trailing ':'. And if we're completing at or after
+ the ':', then complete_linespec_component won't try to
+ complete on source filenames. */
+
+ const char *word = parser.completion_word;
+
+ complete_linespec_component (&parser, tracker,
+ parser.completion_word,
+ linespec_complete_what::FUNCTION,
+ PARSER_EXPLICIT (&parser)->source_filename);
+
+ parser.complete_what = linespec_complete_what::NOTHING;
+
+ if (tracker.quote_char ())
+ {
+ /* The function/file name was not close-quoted, so this
+ can't be a keyword. Note: complete_linespec_component
+ may have swapped the original quote char for ':' when we
+ get here, but that still indicates the same. */
+ }
+ else if (!tracker.have_completions ())
+ {
+ size_t key_start;
+ size_t wordlen = strlen (parser.completion_word);
+
+ key_start
+ = string_find_incomplete_keyword_at_end (linespec_keywords,
+ parser.completion_word,
+ wordlen);
+
+ if (key_start != -1
+ || (wordlen > 0
+ && parser.completion_word[wordlen - 1] == ' '))
+ {
+ parser.completion_word += key_start;
+ parser.complete_what = linespec_complete_what::KEYWORD;
+ }
+ }
+ else if (tracker.completes_to_completion_word (word))
+ {
+ /* Skip the function and complete on keywords. */
+ parser.completion_word += strlen (word);
+ parser.complete_what = linespec_complete_what::KEYWORD;
+ tracker.discard_completions ();
+ }
+ }
+
+ tracker.advance_custom_word_point_by (parser.completion_word - orig);
+
+ complete_linespec_component (&parser, tracker,
+ parser.completion_word,
+ parser.complete_what,
+ PARSER_EXPLICIT (&parser)->source_filename);
+
+ /* If we're past the "filename:function:label:offset" linespec, and
+ didn't find any match, then assume the user might want to create
+ a pending breakpoint anyway and offer the keyword
+ completions. */
+ if (!parser.completion_quote_char
+ && (parser.complete_what == linespec_complete_what::FUNCTION
+ || parser.complete_what == linespec_complete_what::LABEL
+ || parser.complete_what == linespec_complete_what::NOTHING)
+ && !tracker.have_completions ())
+ {
+ const char *end
+ = parser.completion_word + strlen (parser.completion_word);
+
+ if (end > orig && end[-1] == ' ')
+ {
+ tracker.advance_custom_word_point_by (end - parser.completion_word);
+
+ complete_linespec_component (&parser, tracker, end,
+ linespec_complete_what::KEYWORD,
+ NULL);
+ }
+ }
+}
+
+/* A helper function for decode_line_full and decode_line_1 to
+ turn LOCATION into std::vector<symtab_and_line>. */
+
+static std::vector<symtab_and_line>
+event_location_to_sals (linespec_parser *parser,
+ const struct event_location *location)
+{
+ std::vector<symtab_and_line> result;
+
+ switch (event_location_type (location))
+ {
+ case LINESPEC_LOCATION:
+ {
+ PARSER_STATE (parser)->is_linespec = 1;
+ TRY
+ {
+ const linespec_location *ls = get_linespec_location (location);
+ result = parse_linespec (parser,
+ ls->spec_string, ls->match_type);
+ }
+ CATCH (except, RETURN_MASK_ERROR)
+ {
+ throw_exception (except);
+ }
+ END_CATCH
+ }
+ break;
+
+ case ADDRESS_LOCATION:
+ {
+ const char *addr_string = get_address_string_location (location);
+ CORE_ADDR addr = get_address_location (location);
+
+ if (addr_string != NULL)
+ {
+ addr = linespec_expression_to_pc (&addr_string);
+ if (PARSER_STATE (parser)->canonical != NULL)
+ PARSER_STATE (parser)->canonical->location
+ = copy_event_location (location);
+ }
+
+ result = convert_address_location_to_sals (PARSER_STATE (parser),
+ addr);
+ }
+ break;
+
+ case EXPLICIT_LOCATION:
+ {
+ const struct explicit_location *explicit_loc;
+
+ explicit_loc = get_explicit_location_const (location);
+ result = convert_explicit_location_to_sals (PARSER_STATE (parser),
+ PARSER_RESULT (parser),
+ explicit_loc);
+ }
+ break;