1 /* Rust language support definitions for GDB, the GNU debugger.
3 Copyright (C) 2016-2022 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 /* Given a block, find the name of the block's crate. Returns an empty
38 stringif no crate name can be found. */
39 extern std::string
rust_crate_for_block (const struct block
*block
);
41 /* Returns the last segment of a Rust path like foo::bar::baz. Will
42 not handle cases where the last segment contains generics. */
44 extern const char *rust_last_path_segment (const char *path
);
46 /* Create a new slice type. NAME is the name of the type. ELT_TYPE
47 is the type of the elements of the slice. USIZE_TYPE is the Rust
48 "usize" type to use. The new type is allocated whereever ELT_TYPE
50 extern struct type
*rust_slice_type (const char *name
, struct type
*elt_type
,
51 struct type
*usize_type
);
53 /* Class representing the Rust language. */
55 class rust_language
: public language_defn
59 : language_defn (language_rust
)
64 const char *name () const override
69 const char *natural_name () const override
74 const char *get_digit_separator () const override
79 const std::vector
<const char *> &filename_extensions () const override
81 static const std::vector
<const char *> extensions
= { ".rs" };
87 void language_arch_info (struct gdbarch
*gdbarch
,
88 struct language_arch_info
*lai
) const override
;
92 bool sniff_from_mangled_name
93 (const char *mangled
, gdb::unique_xmalloc_ptr
<char> *demangled
)
96 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
97 return *demangled
!= NULL
;
100 /* See language.h. */
102 gdb::unique_xmalloc_ptr
<char> demangle_symbol (const char *mangled
,
103 int options
) const override
105 return gdb_demangle (mangled
, options
);
108 /* See language.h. */
110 void print_type (struct type
*type
, const char *varstring
,
111 struct ui_file
*stream
, int show
, int level
,
112 const struct type_print_options
*flags
) const override
;
114 /* See language.h. */
116 gdb::unique_xmalloc_ptr
<char> watch_location_expression
117 (struct type
*type
, CORE_ADDR addr
) const override
119 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
120 std::string name
= type_to_string (type
);
121 return xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
125 /* See language.h. */
127 void value_print_inner
128 (struct value
*val
, struct ui_file
*stream
, int recurse
,
129 const struct value_print_options
*options
) const override
;
131 /* See language.h. */
133 void value_print (struct value
*val
, struct ui_file
*stream
,
134 const struct value_print_options
*options
) const override
;
136 /* See language.h. */
138 struct block_symbol lookup_symbol_nonlocal
139 (const char *name
, const struct block
*block
,
140 const domain_enum domain
) const override
142 struct block_symbol result
= {};
144 if (symbol_lookup_debug
)
146 gdb_printf (gdb_stdlog
,
147 "rust_lookup_symbol_non_local"
148 " (%s, %s (scope %s), %s)\n",
149 name
, host_address_to_string (block
),
150 block_scope (block
), domain_name (domain
));
153 /* Look up bare names in the block's scope. */
154 std::string scopedname
;
155 if (name
[cp_find_first_component (name
)] == '\0')
157 const char *scope
= block_scope (block
);
159 if (scope
[0] != '\0')
161 scopedname
= std::string (scope
) + "::" + name
;
162 name
= scopedname
.c_str ();
170 result
= lookup_symbol_in_static_block (name
, block
, domain
);
171 if (result
.symbol
== NULL
)
172 result
= lookup_global_symbol (name
, block
, domain
);
177 /* See language.h. */
179 int parser (struct parser_state
*ps
) const override
;
181 /* See language.h. */
183 void emitchar (int ch
, struct type
*chtype
,
184 struct ui_file
*stream
, int quoter
) const override
;
186 /* See language.h. */
188 void printchar (int ch
, struct type
*chtype
,
189 struct ui_file
*stream
) const override
191 gdb_puts ("'", stream
);
192 emitchar (ch
, chtype
, stream
, '\'');
193 gdb_puts ("'", stream
);
196 /* See language.h. */
198 void printstr (struct ui_file
*stream
, struct type
*elttype
,
199 const gdb_byte
*string
, unsigned int length
,
200 const char *encoding
, int force_ellipses
,
201 const struct value_print_options
*options
) const override
;
203 /* See language.h. */
205 void print_typedef (struct type
*type
, struct symbol
*new_symbol
,
206 struct ui_file
*stream
) const override
208 type
= check_typedef (type
);
209 gdb_printf (stream
, "type %s = ", new_symbol
->print_name ());
210 type_print (type
, "", stream
, 0);
211 gdb_printf (stream
, ";");
214 /* See language.h. */
216 bool is_string_type_p (struct type
*type
) const override
;
218 /* See language.h. */
220 bool range_checking_on_by_default () const override
225 /* Helper for value_print_inner, arguments are as for that function.
226 Prints structs and untagged unions. */
228 void val_print_struct (struct value
*val
, struct ui_file
*stream
,
230 const struct value_print_options
*options
) const;
232 /* Helper for value_print_inner, arguments are as for that function.
233 Prints discriminated unions (Rust enums). */
235 void print_enum (struct value
*val
, struct ui_file
*stream
, int recurse
,
236 const struct value_print_options
*options
) const;
239 #endif /* RUST_LANG_H */