2016-04-27 09:38:08 +08:00
|
|
|
/* Rust language support definitions for GDB, the GNU debugger.
|
|
|
|
|
2024-01-12 23:30:44 +08:00
|
|
|
Copyright (C) 2016-2024 Free Software Foundation, Inc.
|
2016-04-27 09:38:08 +08:00
|
|
|
|
|
|
|
This file is part of GDB.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#ifndef RUST_LANG_H
|
|
|
|
#define RUST_LANG_H
|
|
|
|
|
2020-10-30 23:09:35 +08:00
|
|
|
#include "demangle.h"
|
|
|
|
#include "language.h"
|
|
|
|
#include "value.h"
|
|
|
|
#include "c-lang.h"
|
|
|
|
|
2016-04-27 09:38:08 +08:00
|
|
|
struct parser_state;
|
|
|
|
struct type;
|
|
|
|
|
|
|
|
/* Return true if TYPE is a tuple type; otherwise false. */
|
2017-02-03 12:21:19 +08:00
|
|
|
extern bool rust_tuple_type_p (struct type *type);
|
2016-04-27 09:38:08 +08:00
|
|
|
|
|
|
|
/* Return true if TYPE is a tuple struct type; otherwise false. */
|
2017-02-03 12:21:19 +08:00
|
|
|
extern bool rust_tuple_struct_type_p (struct type *type);
|
2016-04-27 09:38:08 +08:00
|
|
|
|
2017-02-03 11:58:12 +08:00
|
|
|
/* Given a block, find the name of the block's crate. Returns an empty
|
|
|
|
stringif no crate name can be found. */
|
|
|
|
extern std::string rust_crate_for_block (const struct block *block);
|
2016-04-27 09:38:08 +08:00
|
|
|
|
Convert Rust to use discriminated unions
A Rust enum is, essentially, a discriminated union. Currently the
Rust language support handles Rust enums locally, in rust-lang.c.
However, because I am changing the Rust compiler to use
DW_TAG_variant* to represent enums, it seemed better to have a single
internal representation for Rust enums in gdb.
This patch implements this idea by moving the current Rust enum
handling code to dwarf2read. This allows the simplification of some
parts of rust-lang.c as well.
2018-02-26 Tom Tromey <tom@tromey.com>
* rust-lang.h (rust_last_path_segment): Declare.
* rust-lang.c (rust_last_path_segment): Now public. Change
contract.
(struct disr_info): Remove.
(RUST_ENUM_PREFIX, RUST_ENCODED_ENUM_REAL)
(RUST_ENCODED_ENUM_HIDDEN, rust_union_is_untagged)
(rust_get_disr_info, rust_tuple_variant_type_p): Remove.
(rust_enum_p, rust_enum_variant): New function.
(rust_underscore_fields): Remove "offset" parameter.
(rust_print_enum): New function.
(rust_val_print) <TYPE_CODE_UNION>: Remove enum code.
<TYPE_CODE_STRUCT>: Call rust_print_enum when appropriate.
(rust_print_struct_def): Add "for_rust_enum" parameter. Handle
enums.
(rust_internal_print_type): New function, from rust_print_type.
Remove enum code.
(rust_print_type): Call rust_internal_print_type.
(rust_evaluate_subexp) <STRUCTOP_ANONYMOUS, STRUCTOP_STRUCT>:
Update enum handling.
* dwarf2read.c (struct dwarf2_cu) <rust_unions>: New field.
(rust_fully_qualify, alloc_discriminant_info, quirk_rust_enum)
(rust_union_quirks): New functions.
(process_full_comp_unit, process_full_type_unit): Call
rust_union_quirks.
(process_structure_scope): Update rust_unions if necessary.
2018-02-26 Tom Tromey <tom@tromey.com>
* gdb.rust/simple.exp: Accept more possible results in enum test.
2018-02-10 04:31:51 +08:00
|
|
|
/* Returns the last segment of a Rust path like foo::bar::baz. Will
|
|
|
|
not handle cases where the last segment contains generics. */
|
|
|
|
|
|
|
|
extern const char *rust_last_path_segment (const char *path);
|
|
|
|
|
2016-04-27 09:38:08 +08:00
|
|
|
/* Create a new slice type. NAME is the name of the type. ELT_TYPE
|
|
|
|
is the type of the elements of the slice. USIZE_TYPE is the Rust
|
2024-10-06 13:59:48 +08:00
|
|
|
"usize" type to use. The new type is allocated wherever ELT_TYPE
|
2016-04-27 09:38:08 +08:00
|
|
|
is allocated. */
|
2017-06-07 04:27:42 +08:00
|
|
|
extern struct type *rust_slice_type (const char *name, struct type *elt_type,
|
|
|
|
struct type *usize_type);
|
2016-04-27 09:38:08 +08:00
|
|
|
|
2023-08-05 03:46:44 +08:00
|
|
|
/* Return a new array that holds the contents of the given slice,
|
|
|
|
VAL. */
|
|
|
|
extern struct value *rust_slice_to_array (struct value *val);
|
|
|
|
|
2020-10-30 23:09:35 +08:00
|
|
|
/* Class representing the Rust language. */
|
|
|
|
|
|
|
|
class rust_language : public language_defn
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
rust_language ()
|
|
|
|
: language_defn (language_rust)
|
|
|
|
{ /* Nothing. */ }
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
|
|
|
const char *name () const override
|
|
|
|
{ return "rust"; }
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
|
|
|
const char *natural_name () const override
|
|
|
|
{ return "Rust"; }
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
2022-06-12 13:22:25 +08:00
|
|
|
const char *get_digit_separator () const override
|
|
|
|
{ return "_"; }
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
2020-10-30 23:09:35 +08:00
|
|
|
const std::vector<const char *> &filename_extensions () const override
|
|
|
|
{
|
|
|
|
static const std::vector<const char *> extensions = { ".rs" };
|
|
|
|
return extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
|
|
|
void language_arch_info (struct gdbarch *gdbarch,
|
|
|
|
struct language_arch_info *lai) const override;
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
2021-10-04 22:44:22 +08:00
|
|
|
bool sniff_from_mangled_name
|
|
|
|
(const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled)
|
|
|
|
const override
|
2020-10-30 23:09:35 +08:00
|
|
|
{
|
2023-03-19 23:13:10 +08:00
|
|
|
demangled->reset (rust_demangle (mangled, 0));
|
2020-10-30 23:09:35 +08:00
|
|
|
return *demangled != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
2021-10-04 22:44:22 +08:00
|
|
|
gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
|
|
|
|
int options) const override
|
2020-10-30 23:09:35 +08:00
|
|
|
{
|
2023-03-19 23:13:10 +08:00
|
|
|
return gdb::unique_xmalloc_ptr<char> (rust_demangle (mangled, options));
|
2020-10-30 23:09:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
2022-11-08 02:12:35 +08:00
|
|
|
bool can_print_type_offsets () const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
2020-10-30 23:09:35 +08:00
|
|
|
void print_type (struct type *type, const char *varstring,
|
|
|
|
struct ui_file *stream, int show, int level,
|
|
|
|
const struct type_print_options *flags) const override;
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
|
|
|
gdb::unique_xmalloc_ptr<char> watch_location_expression
|
|
|
|
(struct type *type, CORE_ADDR addr) const override
|
|
|
|
{
|
2022-07-31 10:43:54 +08:00
|
|
|
type = check_typedef (check_typedef (type)->target_type ());
|
2020-10-30 23:09:35 +08:00
|
|
|
std::string name = type_to_string (type);
|
2021-11-08 22:58:46 +08:00
|
|
|
return xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
|
|
|
|
name.c_str ());
|
2020-10-30 23:09:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
|
|
|
void value_print_inner
|
|
|
|
(struct value *val, struct ui_file *stream, int recurse,
|
|
|
|
const struct value_print_options *options) const override;
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
2022-03-28 05:29:28 +08:00
|
|
|
void value_print (struct value *val, struct ui_file *stream,
|
|
|
|
const struct value_print_options *options) const override;
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
2020-10-30 23:09:35 +08:00
|
|
|
struct block_symbol lookup_symbol_nonlocal
|
|
|
|
(const char *name, const struct block *block,
|
2023-03-31 13:00:26 +08:00
|
|
|
const domain_search_flags domain) const override;
|
2020-10-30 23:09:35 +08:00
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
|
|
|
int parser (struct parser_state *ps) const override;
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
|
|
|
void emitchar (int ch, struct type *chtype,
|
|
|
|
struct ui_file *stream, int quoter) const override;
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
|
|
|
void printchar (int ch, struct type *chtype,
|
|
|
|
struct ui_file *stream) const override
|
|
|
|
{
|
2022-01-03 02:36:44 +08:00
|
|
|
gdb_puts ("'", stream);
|
2020-10-30 23:09:35 +08:00
|
|
|
emitchar (ch, chtype, stream, '\'');
|
2022-01-03 02:36:44 +08:00
|
|
|
gdb_puts ("'", stream);
|
2020-10-30 23:09:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
|
|
|
void printstr (struct ui_file *stream, struct type *elttype,
|
|
|
|
const gdb_byte *string, unsigned int length,
|
|
|
|
const char *encoding, int force_ellipses,
|
|
|
|
const struct value_print_options *options) const override;
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
|
|
|
void print_typedef (struct type *type, struct symbol *new_symbol,
|
|
|
|
struct ui_file *stream) const override
|
|
|
|
{
|
|
|
|
type = check_typedef (type);
|
2022-01-03 02:46:15 +08:00
|
|
|
gdb_printf (stream, "type %s = ", new_symbol->print_name ());
|
2020-10-30 23:09:35 +08:00
|
|
|
type_print (type, "", stream, 0);
|
2022-01-03 02:46:15 +08:00
|
|
|
gdb_printf (stream, ";");
|
2020-10-30 23:09:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
|
|
|
bool is_string_type_p (struct type *type) const override;
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
2024-01-31 01:06:46 +08:00
|
|
|
bool is_array_like (struct type *type) const override;
|
2023-09-06 03:08:29 +08:00
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
|
|
|
struct value *to_array (struct value *val) const override
|
|
|
|
{ return rust_slice_to_array (val); }
|
|
|
|
|
|
|
|
/* See language.h. */
|
|
|
|
|
2020-10-30 23:09:35 +08:00
|
|
|
bool range_checking_on_by_default () const override
|
|
|
|
{ return true; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2024-01-31 01:06:46 +08:00
|
|
|
/* Helper for value_print_inner, arguments are as for that function.
|
|
|
|
Prints a slice. */
|
|
|
|
|
|
|
|
void val_print_slice (struct value *val, struct ui_file *stream,
|
|
|
|
int recurse,
|
|
|
|
const struct value_print_options *options) const;
|
|
|
|
|
2020-10-30 23:09:35 +08:00
|
|
|
/* Helper for value_print_inner, arguments are as for that function.
|
|
|
|
Prints structs and untagged unions. */
|
|
|
|
|
|
|
|
void val_print_struct (struct value *val, struct ui_file *stream,
|
|
|
|
int recurse,
|
|
|
|
const struct value_print_options *options) const;
|
|
|
|
|
|
|
|
/* Helper for value_print_inner, arguments are as for that function.
|
|
|
|
Prints discriminated unions (Rust enums). */
|
|
|
|
|
|
|
|
void print_enum (struct value *val, struct ui_file *stream, int recurse,
|
|
|
|
const struct value_print_options *options) const;
|
|
|
|
};
|
|
|
|
|
2016-04-27 09:38:08 +08:00
|
|
|
#endif /* RUST_LANG_H */
|