GDB (xrefs)
Loading...
Searching...
No Matches
rust-lang.h
Go to the documentation of this file.
1/* Rust language support definitions for GDB, the GNU debugger.
2
3 Copyright (C) 2016-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#ifndef RUST_LANG_H
21#define RUST_LANG_H
22
23#include "demangle.h"
24#include "language.h"
25#include "value.h"
26#include "c-lang.h"
27
28struct parser_state;
29struct type;
30
31/* Return true if TYPE is a tuple type; otherwise false. */
32extern bool rust_tuple_type_p (struct type *type);
33
34/* Return true if TYPE is a tuple struct type; otherwise false. */
35extern bool rust_tuple_struct_type_p (struct type *type);
36
37/* Return true if TYPE is a slice type, otherwise false. */
38extern bool rust_slice_type_p (const struct type *type);
39
40/* Given a block, find the name of the block's crate. Returns an empty
41 stringif no crate name can be found. */
42extern std::string rust_crate_for_block (const struct block *block);
43
44/* Returns the last segment of a Rust path like foo::bar::baz. Will
45 not handle cases where the last segment contains generics. */
46
47extern const char *rust_last_path_segment (const char *path);
48
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
52 is allocated. */
53extern struct type *rust_slice_type (const char *name, struct type *elt_type,
54 struct type *usize_type);
55
56/* Return a new array that holds the contents of the given slice,
57 VAL. */
58extern struct value *rust_slice_to_array (struct value *val);
59
60/* Class representing the Rust language. */
61
63{
64public:
67 { /* Nothing. */ }
68
69 /* See language.h. */
70
71 const char *name () const override
72 { return "rust"; }
73
74 /* See language.h. */
75
76 const char *natural_name () const override
77 { return "Rust"; }
78
79 /* See language.h. */
80
81 const char *get_digit_separator () const override
82 { return "_"; }
83
84 /* See language.h. */
85
86 const std::vector<const char *> &filename_extensions () const override
87 {
88 static const std::vector<const char *> extensions = { ".rs" };
89 return extensions;
90 }
91
92 /* See language.h. */
93
95 struct language_arch_info *lai) const override;
96
97 /* See language.h. */
98
100 (const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled)
101 const override
102 {
103 demangled->reset (rust_demangle (mangled, 0));
104 return *demangled != NULL;
105 }
106
107 /* See language.h. */
108
109 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
110 int options) const override
111 {
112 return gdb::unique_xmalloc_ptr<char> (rust_demangle (mangled, options));
113 }
114
115 /* See language.h. */
116
117 bool can_print_type_offsets () const override
118 {
119 return true;
120 }
121
122 /* See language.h. */
123
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;
127
128 /* See language.h. */
129
130 gdb::unique_xmalloc_ptr<char> watch_location_expression
131 (struct type *type, CORE_ADDR addr) const override
132 {
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),
136 name.c_str ());
137 }
138
139 /* See language.h. */
140
142 (struct value *val, struct ui_file *stream, int recurse,
143 const struct value_print_options *options) const override;
144
145 /* See language.h. */
146
147 void value_print (struct value *val, struct ui_file *stream,
148 const struct value_print_options *options) const override;
149
150 /* See language.h. */
151
153 (const char *name, const struct block *block,
154 const domain_enum domain) const override;
155
156 /* See language.h. */
157
158 int parser (struct parser_state *ps) const override;
159
160 /* See language.h. */
161
162 void emitchar (int ch, struct type *chtype,
163 struct ui_file *stream, int quoter) const override;
164
165 /* See language.h. */
166
167 void printchar (int ch, struct type *chtype,
168 struct ui_file *stream) const override
169 {
170 gdb_puts ("'", stream);
171 emitchar (ch, chtype, stream, '\'');
172 gdb_puts ("'", stream);
173 }
174
175 /* See language.h. */
176
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;
181
182 /* See language.h. */
183
184 void print_typedef (struct type *type, struct symbol *new_symbol,
185 struct ui_file *stream) const override
186 {
188 gdb_printf (stream, "type %s = ", new_symbol->print_name ());
189 type_print (type, "", stream, 0);
190 gdb_printf (stream, ";");
191 }
192
193 /* See language.h. */
194
195 bool is_string_type_p (struct type *type) const override;
196
197 /* See language.h. */
198
199 bool is_array_like (struct type *type) const override
200 { return rust_slice_type_p (type); }
201
202 /* See language.h. */
203
204 struct value *to_array (struct value *val) const override
205 { return rust_slice_to_array (val); }
206
207 /* See language.h. */
208
209 bool range_checking_on_by_default () const override
210 { return true; }
211
212private:
213
214 /* Helper for value_print_inner, arguments are as for that function.
215 Prints structs and untagged unions. */
216
217 void val_print_struct (struct value *val, struct ui_file *stream,
218 int recurse,
219 const struct value_print_options *options) const;
220
221 /* Helper for value_print_inner, arguments are as for that function.
222 Prints discriminated unions (Rust enums). */
223
224 void print_enum (struct value *val, struct ui_file *stream, int recurse,
225 const struct value_print_options *options) const;
226};
227
228#endif /* RUST_LANG_H */
const char *const name
void printchar(int ch, struct type *chtype, struct ui_file *stream) const override
Definition rust-lang.h:167
struct block_symbol lookup_symbol_nonlocal(const char *name, const struct block *block, const domain_enum domain) const override
Definition rust-lang.c:1701
const char * get_digit_separator() const override
Definition rust-lang.h:81
struct value * to_array(struct value *val) const override
Definition rust-lang.h:204
const char * natural_name() const override
Definition rust-lang.h:76
const std::vector< const char * > & filename_extensions() const override
Definition rust-lang.h:86
bool sniff_from_mangled_name(const char *mangled, gdb::unique_xmalloc_ptr< char > *demangled) const override
Definition rust-lang.h:100
const char * name() const override
Definition rust-lang.h:71
gdb::unique_xmalloc_ptr< char > watch_location_expression(struct type *type, CORE_ADDR addr) const override
Definition rust-lang.h:131
int parser(struct parser_state *ps) const override
bool range_checking_on_by_default() const override
Definition rust-lang.h:209
void language_arch_info(struct gdbarch *gdbarch, struct language_arch_info *lai) const override
Definition rust-lang.c:1594
void print_enum(struct value *val, struct ui_file *stream, int recurse, const struct value_print_options *options) const
Definition rust-lang.c:466
void value_print(struct value *val, struct ui_file *stream, const struct value_print_options *options) const override
Definition rust-lang.c:650
bool is_array_like(struct type *type) const override
Definition rust-lang.h:199
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
Definition rust-lang.c:277
void value_print_inner(struct value *val, struct ui_file *stream, int recurse, const struct value_print_options *options) const override
Definition rust-lang.c:541
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
Definition rust-lang.c:1641
bool is_string_type_p(struct type *type) const override
Definition rust-lang.c:1680
void print_typedef(struct type *type, struct symbol *new_symbol, struct ui_file *stream) const override
Definition rust-lang.h:184
void emitchar(int ch, struct type *chtype, struct ui_file *stream, int quoter) const override
Definition rust-lang.c:1653
bool can_print_type_offsets() const override
Definition rust-lang.h:117
gdb::unique_xmalloc_ptr< char > demangle_symbol(const char *mangled, int options) const override
Definition rust-lang.h:109
void val_print_struct(struct value *val, struct ui_file *stream, int recurse, const struct value_print_options *options) const
Definition rust-lang.c:384
@ language_rust
Definition defs.h:215
struct type * check_typedef(struct type *type)
Definition gdbtypes.c:2966
mach_port_t kern_return_t mach_port_t mach_msg_type_name_t msgportsPoly mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
Definition gnu-nat.c:1861
static struct symbol * new_symbol(struct die_info *, struct type *, struct dwarf2_cu *, struct symbol *=NULL)
Definition read.c:18978
bool rust_slice_type_p(const struct type *type)
Definition rust-lang.c:159
std::string rust_crate_for_block(const struct block *block)
Definition rust-lang.c:58
const char * rust_last_path_segment(const char *path)
Definition rust-lang.c:46
struct type * rust_slice_type(const char *name, struct type *elt_type, struct type *usize_type)
Definition rust-lang.c:1017
bool rust_tuple_struct_type_p(struct type *type)
Definition rust-lang.c:148
struct value * rust_slice_to_array(struct value *val)
Definition rust-lang.c:325
bool rust_tuple_type_p(struct type *type)
Definition rust-lang.c:108
Definition block.h:109
const char * print_name() const
Definition symtab.h:475
Definition value.h:130
domain_enum
Definition symtab.h:900
std::string type_to_string(struct type *type)
Definition typeprint.c:399
void type_print(struct type *type, const char *varstring, struct ui_file *stream, int show)
Definition typeprint.c:388
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
void gdb_puts(const char *linebuffer, struct ui_file *stream)
Definition utils.c:1809