+static VEC (symtab_ptr) *
+ collect_symtabs_from_filename (const char *file,
+ struct program_space *pspace);
+
+static void decode_digits_ordinary (struct linespec_state *self,
+ linespec_p ls,
+ int line,
+ struct symtabs_and_lines *sals,
+ struct linetable_entry **best_entry);
+
+static void decode_digits_list_mode (struct linespec_state *self,
+ linespec_p ls,
+ struct symtabs_and_lines *values,
+ struct symtab_and_line val);
+
+static void minsym_found (struct linespec_state *self, struct objfile *objfile,
+ struct minimal_symbol *msymbol,
+ struct symtabs_and_lines *result);
+
+static int compare_symbols (const void *a, const void *b);
+
+static int compare_msymbols (const void *a, const void *b);
+
+/* Permitted quote characters for the parser. This is different from the
+ completer's quote characters to allow backward compatibility with the
+ previous parser. */
+static const char *const linespec_quote_characters = "\"\'";
+
+/* Lexer functions. */
+
+/* Lex a number from the input in PARSER. This only supports
+ decimal numbers.
+
+ Return true if input is decimal numbers. Return false if not. */
+
+static int
+linespec_lexer_lex_number (linespec_parser *parser, linespec_token *tokenp)
+{
+ tokenp->type = LSTOKEN_NUMBER;
+ LS_TOKEN_STOKEN (*tokenp).length = 0;
+ LS_TOKEN_STOKEN (*tokenp).ptr = PARSER_STREAM (parser);
+
+ /* Keep any sign at the start of the stream. */
+ if (*PARSER_STREAM (parser) == '+' || *PARSER_STREAM (parser) == '-')
+ {
+ ++LS_TOKEN_STOKEN (*tokenp).length;
+ ++(PARSER_STREAM (parser));
+ }
+
+ while (isdigit (*PARSER_STREAM (parser)))
+ {
+ ++LS_TOKEN_STOKEN (*tokenp).length;
+ ++(PARSER_STREAM (parser));
+ }
+
+ /* If the next character in the input buffer is not a space, comma,
+ quote, or colon, this input does not represent a number. */
+ if (*PARSER_STREAM (parser) != '\0'
+ && !isspace (*PARSER_STREAM (parser)) && *PARSER_STREAM (parser) != ','
+ && *PARSER_STREAM (parser) != ':'
+ && !strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
+ {
+ PARSER_STREAM (parser) = LS_TOKEN_STOKEN (*tokenp).ptr;
+ return 0;
+ }
+
+ return 1;
+}
+
+/* See linespec.h. */
+
+const char *
+linespec_lexer_lex_keyword (const char *p)
+{
+ int i;
+
+ if (p != NULL)
+ {
+ for (i = 0; i < ARRAY_SIZE (linespec_keywords); ++i)
+ {
+ int len = strlen (linespec_keywords[i]);
+
+ /* If P begins with one of the keywords and the next
+ character is whitespace, we may have found a keyword.
+ It is only a keyword if it is not followed by another
+ keyword. */
+ if (strncmp (p, linespec_keywords[i], len) == 0
+ && isspace (p[len]))
+ {
+ int j;
+
+ /* Special case: "if" ALWAYS stops the lexer, since it
+ is not possible to predict what is going to appear in
+ the condition, which can only be parsed after SaLs have
+ been found. */
+ if (i != IF_KEYWORD_INDEX)
+ {
+ p += len;
+ p = skip_spaces_const (p);
+ for (j = 0; j < ARRAY_SIZE (linespec_keywords); ++j)
+ {
+ int nextlen = strlen (linespec_keywords[j]);
+
+ if (strncmp (p, linespec_keywords[j], nextlen) == 0
+ && isspace (p[nextlen]))
+ return NULL;
+ }
+ }
+
+ return linespec_keywords[i];
+ }
+ }
+ }
+
+ return NULL;
+}
+
+/* See description in linespec.h. */
+
+int
+is_ada_operator (const char *string)
+{
+ const struct ada_opname_map *mapping;
+
+ for (mapping = ada_opname_table;
+ mapping->encoded != NULL
+ && !startswith (string, mapping->decoded); ++mapping)
+ ;
+
+ return mapping->decoded == NULL ? 0 : strlen (mapping->decoded);
+}
+
+/* Find QUOTE_CHAR in STRING, accounting for the ':' terminal. Return
+ the location of QUOTE_CHAR, or NULL if not found. */
+
+static const char *
+skip_quote_char (const char *string, char quote_char)
+{
+ const char *p, *last;
+
+ p = last = find_toplevel_char (string, quote_char);
+ while (p && *p != '\0' && *p != ':')
+ {
+ p = find_toplevel_char (p, quote_char);
+ if (p != NULL)
+ last = p++;
+ }
+
+ return last;
+}
+
+/* Make a writable copy of the string given in TOKEN, trimming
+ any trailing whitespace. */
+
+static char *
+copy_token_string (linespec_token token)
+{
+ char *str, *s;
+
+ if (token.type == LSTOKEN_KEYWORD)
+ return xstrdup (LS_TOKEN_KEYWORD (token));
+
+ str = savestring (LS_TOKEN_STOKEN (token).ptr,
+ LS_TOKEN_STOKEN (token).length);
+ s = remove_trailing_whitespace (str, str + LS_TOKEN_STOKEN (token).length);
+ *s = '\0';
+
+ return str;
+}
+
+/* Does P represent the end of a quote-enclosed linespec? */
+
+static int
+is_closing_quote_enclosed (const char *p)
+{
+ if (strchr (linespec_quote_characters, *p))
+ ++p;
+ p = skip_spaces ((char *) p);
+ return (*p == '\0' || linespec_lexer_lex_keyword (p));
+}
+
+/* Find the end of the parameter list that starts with *INPUT.
+ This helper function assists with lexing string segments
+ which might contain valid (non-terminating) commas. */
+
+static const char *
+find_parameter_list_end (const char *input)
+{
+ char end_char, start_char;
+ int depth;
+ const char *p;
+
+ start_char = *input;
+ if (start_char == '(')
+ end_char = ')';
+ else if (start_char == '<')
+ end_char = '>';
+ else
+ return NULL;
+
+ p = input;
+ depth = 0;
+ while (*p)
+ {
+ if (*p == start_char)
+ ++depth;
+ else if (*p == end_char)
+ {
+ if (--depth == 0)
+ {
+ ++p;
+ break;
+ }
+ }
+ ++p;
+ }
+
+ return p;
+}
+
+
+/* Lex a string from the input in PARSER. */
+
+static linespec_token
+linespec_lexer_lex_string (linespec_parser *parser)
+{
+ linespec_token token;
+ const char *start = PARSER_STREAM (parser);
+
+ token.type = LSTOKEN_STRING;
+
+ /* If the input stream starts with a quote character, skip to the next
+ quote character, regardless of the content. */
+ if (strchr (linespec_quote_characters, *PARSER_STREAM (parser)))
+ {
+ const char *end;
+ char quote_char = *PARSER_STREAM (parser);
+
+ /* Special case: Ada operators. */
+ if (PARSER_STATE (parser)->language->la_language == language_ada
+ && quote_char == '\"')
+ {
+ int len = is_ada_operator (PARSER_STREAM (parser));
+
+ if (len != 0)
+ {
+ /* The input is an Ada operator. Return the quoted string
+ as-is. */
+ LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
+ LS_TOKEN_STOKEN (token).length = len;
+ PARSER_STREAM (parser) += len;
+ return token;
+ }
+
+ /* The input does not represent an Ada operator -- fall through
+ to normal quoted string handling. */
+ }
+
+ /* Skip past the beginning quote. */
+ ++(PARSER_STREAM (parser));
+
+ /* Mark the start of the string. */
+ LS_TOKEN_STOKEN (token).ptr = PARSER_STREAM (parser);
+
+ /* Skip to the ending quote. */
+ end = skip_quote_char (PARSER_STREAM (parser), quote_char);
+
+ /* Error if the input did not terminate properly. */
+ if (end == NULL)
+ error (_("unmatched quote"));
+
+ /* Skip over the ending quote and mark the length of the string. */
+ PARSER_STREAM (parser) = (char *) ++end;
+ LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - 2 - start;
+ }
+ else
+ {
+ const char *p;
+
+ /* Otherwise, only identifier characters are permitted.
+ Spaces are the exception. In general, we keep spaces,
+ but only if the next characters in the input do not resolve
+ to one of the keywords.
+
+ This allows users to forgo quoting CV-qualifiers, template arguments,
+ and similar common language constructs. */
+
+ while (1)
+ {
+ if (isspace (*PARSER_STREAM (parser)))
+ {
+ p = skip_spaces_const (PARSER_STREAM (parser));
+ /* When we get here we know we've found something followed by
+ a space (we skip over parens and templates below).
+ So if we find a keyword now, we know it is a keyword and not,
+ say, a function name. */
+ if (linespec_lexer_lex_keyword (p) != NULL)
+ {
+ LS_TOKEN_STOKEN (token).ptr = start;
+ LS_TOKEN_STOKEN (token).length
+ = PARSER_STREAM (parser) - start;
+ return token;
+ }
+
+ /* Advance past the whitespace. */
+ PARSER_STREAM (parser) = p;
+ }
+
+ /* If the next character is EOI or (single) ':', the
+ string is complete; return the token. */
+ if (*PARSER_STREAM (parser) == 0)
+ {
+ LS_TOKEN_STOKEN (token).ptr = start;
+ LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
+ return token;
+ }
+ else if (PARSER_STREAM (parser)[0] == ':')
+ {
+ /* Do not tokenize the C++ scope operator. */
+ if (PARSER_STREAM (parser)[1] == ':')
+ ++(PARSER_STREAM (parser));
+
+ /* Do not tokenify if the input length so far is one
+ (i.e, a single-letter drive name) and the next character
+ is a directory separator. This allows Windows-style
+ paths to be recognized as filenames without quoting it. */
+ else if ((PARSER_STREAM (parser) - start) != 1
+ || !IS_DIR_SEPARATOR (PARSER_STREAM (parser)[1]))
+ {
+ LS_TOKEN_STOKEN (token).ptr = start;
+ LS_TOKEN_STOKEN (token).length
+ = PARSER_STREAM (parser) - start;
+ return token;
+ }
+ }
+ /* Special case: permit quote-enclosed linespecs. */
+ else if (parser->is_quote_enclosed
+ && strchr (linespec_quote_characters,
+ *PARSER_STREAM (parser))
+ && is_closing_quote_enclosed (PARSER_STREAM (parser)))
+ {
+ LS_TOKEN_STOKEN (token).ptr = start;
+ LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
+ return token;
+ }
+ /* Because commas may terminate a linespec and appear in
+ the middle of valid string input, special cases for
+ '<' and '(' are necessary. */
+ else if (*PARSER_STREAM (parser) == '<'
+ || *PARSER_STREAM (parser) == '(')
+ {
+ const char *p;
+
+ p = find_parameter_list_end (PARSER_STREAM (parser));
+ if (p != NULL)
+ {
+ PARSER_STREAM (parser) = p;
+ continue;
+ }
+ }
+ /* Commas are terminators, but not if they are part of an
+ operator name. */
+ else if (*PARSER_STREAM (parser) == ',')
+ {
+ if ((PARSER_STATE (parser)->language->la_language
+ == language_cplus)
+ && (PARSER_STREAM (parser) - start) > 8
+ /* strlen ("operator") */)
+ {
+ const char *p = strstr (start, "operator");
+
+ if (p != NULL && is_operator_name (p))
+ {
+ /* This is an operator name. Keep going. */
+ ++(PARSER_STREAM (parser));
+ continue;
+ }
+ }
+
+ /* Comma terminates the string. */
+ LS_TOKEN_STOKEN (token).ptr = start;
+ LS_TOKEN_STOKEN (token).length = PARSER_STREAM (parser) - start;
+ return token;
+ }
+
+ /* Advance the stream. */
+ ++(PARSER_STREAM (parser));
+ }
+ }
+
+ return token;
+}
+
+/* Lex a single linespec token from PARSER. */
+
+static linespec_token
+linespec_lexer_lex_one (linespec_parser *parser)
+{
+ const char *keyword;
+
+ if (parser->lexer.current.type == LSTOKEN_CONSUMED)
+ {
+ /* Skip any whitespace. */
+ PARSER_STREAM (parser) = skip_spaces_const (PARSER_STREAM (parser));
+
+ /* Check for a keyword, they end the linespec. */
+ keyword = linespec_lexer_lex_keyword (PARSER_STREAM (parser));
+ if (keyword != NULL)
+ {
+ parser->lexer.current.type = LSTOKEN_KEYWORD;
+ LS_TOKEN_KEYWORD (parser->lexer.current) = keyword;
+ /* We do not advance the stream here intentionally:
+ we would like lexing to stop when a keyword is seen.
+
+ PARSER_STREAM (parser) += strlen (keyword); */
+
+ return parser->lexer.current;
+ }
+
+ /* Handle other tokens. */
+ switch (*PARSER_STREAM (parser))
+ {
+ case 0:
+ parser->lexer.current.type = LSTOKEN_EOI;
+ break;
+
+ case '+': case '-':
+ case '0': case '1': case '2': case '3': case '4':
+ case '5': case '6': case '7': case '8': case '9':
+ if (!linespec_lexer_lex_number (parser, &(parser->lexer.current)))
+ parser->lexer.current = linespec_lexer_lex_string (parser);
+ break;
+
+ case ':':
+ /* If we have a scope operator, lex the input as a string.
+ Otherwise, return LSTOKEN_COLON. */
+ if (PARSER_STREAM (parser)[1] == ':')
+ parser->lexer.current = linespec_lexer_lex_string (parser);
+ else
+ {
+ parser->lexer.current.type = LSTOKEN_COLON;
+ ++(PARSER_STREAM (parser));
+ }
+ break;
+
+ case '\'': case '\"':
+ /* Special case: permit quote-enclosed linespecs. */
+ if (parser->is_quote_enclosed
+ && is_closing_quote_enclosed (PARSER_STREAM (parser)))
+ {
+ ++(PARSER_STREAM (parser));
+ parser->lexer.current.type = LSTOKEN_EOI;
+ }
+ else
+ parser->lexer.current = linespec_lexer_lex_string (parser);
+ break;
+
+ case ',':
+ parser->lexer.current.type = LSTOKEN_COMMA;
+ LS_TOKEN_STOKEN (parser->lexer.current).ptr
+ = PARSER_STREAM (parser);
+ LS_TOKEN_STOKEN (parser->lexer.current).length = 1;
+ ++(PARSER_STREAM (parser));
+ break;
+
+ default:
+ /* If the input is not a number, it must be a string.
+ [Keywords were already considered above.] */
+ parser->lexer.current = linespec_lexer_lex_string (parser);
+ break;
+ }
+ }
+
+ return parser->lexer.current;
+}
+
+/* Consume the current token and return the next token in PARSER's
+ input stream. */
+
+static linespec_token
+linespec_lexer_consume_token (linespec_parser *parser)
+{
+ parser->lexer.current.type = LSTOKEN_CONSUMED;
+ return linespec_lexer_lex_one (parser);
+}
+
+/* Return the next token without consuming the current token. */
+
+static linespec_token
+linespec_lexer_peek_token (linespec_parser *parser)
+{
+ linespec_token next;
+ const char *saved_stream = PARSER_STREAM (parser);
+ linespec_token saved_token = parser->lexer.current;
+
+ next = linespec_lexer_consume_token (parser);
+ PARSER_STREAM (parser) = saved_stream;
+ parser->lexer.current = saved_token;
+ return next;
+}
+