1 /* Rust language support definitions for GDB, the GNU debugger.
3 Copyright (C) 2016-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 /* Return true if TYPE is a tuple type; otherwise false. */
32 extern bool rust_tuple_type_p (struct type
*type
);
34 /* Return true if TYPE is a tuple struct type; otherwise false. */
35 extern bool rust_tuple_struct_type_p (struct type
*type
);
37 /* Return true if TYPE is a slice type, otherwise false. */
38 extern bool rust_slice_type_p (const struct type
*type
);
40 /* Given a block, find the name of the block's crate. Returns an empty
41 stringif no crate name can be found. */
42 extern std::string
rust_crate_for_block (const struct block
*block
);
44 /* Returns the last segment of a Rust path like foo::bar::baz. Will
45 not handle cases where the last segment contains generics. */
47 extern const char *rust_last_path_segment (const char *path
);
49 /* Create a new slice type. NAME is the name of the type. ELT_TYPE
50 is the type of the elements of the slice. USIZE_TYPE is the Rust
51 "usize" type to use. The new type is allocated whereever ELT_TYPE
53 extern struct type
*rust_slice_type (const char *name
, struct type
*elt_type
,
54 struct type
*usize_type
);
56 /* Return a new array that holds the contents of the given slice,
58 extern struct value
*rust_slice_to_array (struct value
*val
);
60 /* Class representing the Rust language. */
62 class rust_language
: public language_defn
66 : language_defn (language_rust
)
71 const char *name () const override
76 const char *natural_name () const override
81 const char *get_digit_separator () const override
86 const std::vector
<const char *> &filename_extensions () const override
88 static const std::vector
<const char *> extensions
= { ".rs" };
94 void language_arch_info (struct gdbarch
*gdbarch
,
95 struct language_arch_info
*lai
) const override
;
99 bool sniff_from_mangled_name
100 (const char *mangled
, gdb::unique_xmalloc_ptr
<char> *demangled
)
103 demangled
->reset (rust_demangle (mangled
, 0));
104 return *demangled
!= NULL
;
107 /* See language.h. */
109 gdb::unique_xmalloc_ptr
<char> demangle_symbol (const char *mangled
,
110 int options
) const override
112 return gdb::unique_xmalloc_ptr
<char> (rust_demangle (mangled
, options
));
115 /* See language.h. */
117 bool can_print_type_offsets () const override
122 /* See language.h. */
124 void print_type (struct type
*type
, const char *varstring
,
125 struct ui_file
*stream
, int show
, int level
,
126 const struct type_print_options
*flags
) const override
;
128 /* See language.h. */
130 gdb::unique_xmalloc_ptr
<char> watch_location_expression
131 (struct type
*type
, CORE_ADDR addr
) const override
133 type
= check_typedef (check_typedef (type
)->target_type ());
134 std::string name
= type_to_string (type
);
135 return xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
139 /* See language.h. */
141 void value_print_inner
142 (struct value
*val
, struct ui_file
*stream
, int recurse
,
143 const struct value_print_options
*options
) const override
;
145 /* See language.h. */
147 void value_print (struct value
*val
, struct ui_file
*stream
,
148 const struct value_print_options
*options
) const override
;
150 /* See language.h. */
152 struct block_symbol lookup_symbol_nonlocal
153 (const char *name
, const struct block
*block
,
154 const domain_search_flags domain
) const override
;
156 /* See language.h. */
158 int parser (struct parser_state
*ps
) const override
;
160 /* See language.h. */
162 void emitchar (int ch
, struct type
*chtype
,
163 struct ui_file
*stream
, int quoter
) const override
;
165 /* See language.h. */
167 void printchar (int ch
, struct type
*chtype
,
168 struct ui_file
*stream
) const override
170 gdb_puts ("'", stream
);
171 emitchar (ch
, chtype
, stream
, '\'');
172 gdb_puts ("'", stream
);
175 /* See language.h. */
177 void printstr (struct ui_file
*stream
, struct type
*elttype
,
178 const gdb_byte
*string
, unsigned int length
,
179 const char *encoding
, int force_ellipses
,
180 const struct value_print_options
*options
) const override
;
182 /* See language.h. */
184 void print_typedef (struct type
*type
, struct symbol
*new_symbol
,
185 struct ui_file
*stream
) const override
187 type
= check_typedef (type
);
188 gdb_printf (stream
, "type %s = ", new_symbol
->print_name ());
189 type_print (type
, "", stream
, 0);
190 gdb_printf (stream
, ";");
193 /* See language.h. */
195 bool is_string_type_p (struct type
*type
) const override
;
197 /* See language.h. */
199 bool is_array_like (struct type
*type
) const override
200 { return rust_slice_type_p (type
); }
202 /* See language.h. */
204 struct value
*to_array (struct value
*val
) const override
205 { return rust_slice_to_array (val
); }
207 /* See language.h. */
209 bool range_checking_on_by_default () const override
214 /* Helper for value_print_inner, arguments are as for that function.
215 Prints structs and untagged unions. */
217 void val_print_struct (struct value
*val
, struct ui_file
*stream
,
219 const struct value_print_options
*options
) const;
221 /* Helper for value_print_inner, arguments are as for that function.
222 Prints discriminated unions (Rust enums). */
224 void print_enum (struct value
*val
, struct ui_file
*stream
, int recurse
,
225 const struct value_print_options
*options
) const;
228 #endif /* RUST_LANG_H */