GDB (xrefs)
Loading...
Searching...
No Matches
/tmp/gdb-13.1/gdb/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/* Given a block, find the name of the block's crate. Returns an empty
38 stringif no crate name can be found. */
39extern std::string rust_crate_for_block (const struct block *block);
40
41/* Returns the last segment of a Rust path like foo::bar::baz. Will
42 not handle cases where the last segment contains generics. */
43
44extern const char *rust_last_path_segment (const char *path);
45
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
49 is allocated. */
50extern struct type *rust_slice_type (const char *name, struct type *elt_type,
51 struct type *usize_type);
52
53/* Class representing the Rust language. */
54
56{
57public:
60 { /* Nothing. */ }
61
62 /* See language.h. */
63
64 const char *name () const override
65 { return "rust"; }
66
67 /* See language.h. */
68
69 const char *natural_name () const override
70 { return "Rust"; }
71
72 /* See language.h. */
73
74 const char *get_digit_separator () const override
75 { return "_"; }
76
77 /* See language.h. */
78
79 const std::vector<const char *> &filename_extensions () const override
80 {
81 static const std::vector<const char *> extensions = { ".rs" };
82 return extensions;
83 }
84
85 /* See language.h. */
86
88 struct language_arch_info *lai) const override;
89
90 /* See language.h. */
91
93 (const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled)
94 const override
95 {
96 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
97 return *demangled != NULL;
98 }
99
100 /* See language.h. */
101
102 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
103 int options) const override
104 {
105 return gdb_demangle (mangled, options);
106 }
107
108 /* See language.h. */
109
110 bool can_print_type_offsets () const override
111 {
112 return true;
113 }
114
115 /* See language.h. */
116
117 void print_type (struct type *type, const char *varstring,
118 struct ui_file *stream, int show, int level,
119 const struct type_print_options *flags) const override;
120
121 /* See language.h. */
122
123 gdb::unique_xmalloc_ptr<char> watch_location_expression
124 (struct type *type, CORE_ADDR addr) const override
125 {
126 type = check_typedef (check_typedef (type)->target_type ());
127 std::string name = type_to_string (type);
128 return xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
129 name.c_str ());
130 }
131
132 /* See language.h. */
133
135 (struct value *val, struct ui_file *stream, int recurse,
136 const struct value_print_options *options) const override;
137
138 /* See language.h. */
139
140 void value_print (struct value *val, struct ui_file *stream,
141 const struct value_print_options *options) const override;
142
143 /* See language.h. */
144
146 (const char *name, const struct block *block,
147 const domain_enum domain) const override
148 {
149 struct block_symbol result = {};
150
152 ("rust_lookup_symbol_non_local (%s, %s (scope %s), %s)",
153 name, host_address_to_string (block), block_scope (block),
154 domain_name (domain));
155
156 /* Look up bare names in the block's scope. */
157 std::string scopedname;
158 if (name[cp_find_first_component (name)] == '\0')
159 {
160 const char *scope = block_scope (block);
161
162 if (scope[0] != '\0')
163 {
164 scopedname = std::string (scope) + "::" + name;
165 name = scopedname.c_str ();
166 }
167 else
168 name = NULL;
169 }
170
171 if (name != NULL)
172 {
173 result = lookup_symbol_in_static_block (name, block, domain);
174 if (result.symbol == NULL)
175 result = lookup_global_symbol (name, block, domain);
176 }
177 return result;
178 }
179
180 /* See language.h. */
181
182 int parser (struct parser_state *ps) const override;
183
184 /* See language.h. */
185
186 void emitchar (int ch, struct type *chtype,
187 struct ui_file *stream, int quoter) const override;
188
189 /* See language.h. */
190
191 void printchar (int ch, struct type *chtype,
192 struct ui_file *stream) const override
193 {
194 gdb_puts ("'", stream);
195 emitchar (ch, chtype, stream, '\'');
196 gdb_puts ("'", stream);
197 }
198
199 /* See language.h. */
200
201 void printstr (struct ui_file *stream, struct type *elttype,
202 const gdb_byte *string, unsigned int length,
203 const char *encoding, int force_ellipses,
204 const struct value_print_options *options) const override;
205
206 /* See language.h. */
207
208 void print_typedef (struct type *type, struct symbol *new_symbol,
209 struct ui_file *stream) const override
210 {
212 gdb_printf (stream, "type %s = ", new_symbol->print_name ());
213 type_print (type, "", stream, 0);
214 gdb_printf (stream, ";");
215 }
216
217 /* See language.h. */
218
219 bool is_string_type_p (struct type *type) const override;
220
221 /* See language.h. */
222
223 bool range_checking_on_by_default () const override
224 { return true; }
225
226private:
227
228 /* Helper for value_print_inner, arguments are as for that function.
229 Prints structs and untagged unions. */
230
231 void val_print_struct (struct value *val, struct ui_file *stream,
232 int recurse,
233 const struct value_print_options *options) const;
234
235 /* Helper for value_print_inner, arguments are as for that function.
236 Prints discriminated unions (Rust enums). */
237
238 void print_enum (struct value *val, struct ui_file *stream, int recurse,
239 const struct value_print_options *options) const;
240};
241
242#endif /* RUST_LANG_H */
const char *const name
Definition: aarch64-tdep.c:67
const char * block_scope(const struct block *block)
Definition: block.c:296
void printchar(int ch, struct type *chtype, struct ui_file *stream) const override
Definition: rust-lang.h:191
struct block_symbol lookup_symbol_nonlocal(const char *name, const struct block *block, const domain_enum domain) const override
Definition: rust-lang.h:146
const char * get_digit_separator() const override
Definition: rust-lang.h:74
const char * natural_name() const override
Definition: rust-lang.h:69
const std::vector< const char * > & filename_extensions() const override
Definition: rust-lang.h:79
bool sniff_from_mangled_name(const char *mangled, gdb::unique_xmalloc_ptr< char > *demangled) const override
Definition: rust-lang.h:93
const char * name() const override
Definition: rust-lang.h:64
gdb::unique_xmalloc_ptr< char > watch_location_expression(struct type *type, CORE_ADDR addr) const override
Definition: rust-lang.h:124
int parser(struct parser_state *ps) const override
Definition: rust-parse.c:2123
bool range_checking_on_by_default() const override
Definition: rust-lang.h:223
void print_enum(struct value *val, struct ui_file *stream, int recurse, const struct value_print_options *options) const
Definition: rust-lang.c:448
void value_print(struct value *val, struct ui_file *stream, const struct value_print_options *options) const override
Definition: rust-lang.c:632
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:278
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:523
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:1629
bool is_string_type_p(struct type *type) const override
Definition: rust-lang.c:1668
void print_typedef(struct type *type, struct symbol *new_symbol, struct ui_file *stream) const override
Definition: rust-lang.h:208
void emitchar(int ch, struct type *chtype, struct ui_file *stream, int quoter) const override
Definition: rust-lang.c:1641
bool can_print_type_offsets() const override
Definition: rust-lang.h:110
gdb::unique_xmalloc_ptr< char > demangle_symbol(const char *mangled, int options) const override
Definition: rust-lang.h:102
void val_print_struct(struct value *val, struct ui_file *stream, int recurse, const struct value_print_options *options) const
Definition: rust-lang.c:366
gdb::unique_xmalloc_ptr< char > gdb_demangle(const char *name, int options)
Definition: cp-support.c:1606
unsigned int cp_find_first_component(const char *name)
Definition: cp-support.c:1036
static struct symbol * new_symbol(struct ctf_context *cp, struct type *type, ctf_id_t tid)
Definition: ctfread.c:468
@ language_rust
Definition: defs.h:224
struct type * check_typedef(struct type *type)
Definition: gdbtypes.c:3010
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:1862
std::string rust_crate_for_block(const struct block *block)
Definition: rust-lang.c:59
const char * rust_last_path_segment(const char *path)
Definition: rust-lang.c:47
struct type * rust_slice_type(const char *name, struct type *elt_type, struct type *usize_type)
Definition: rust-lang.c:1001
bool rust_tuple_struct_type_p(struct type *type)
Definition: rust-lang.c:149
bool rust_tuple_type_p(struct type *type)
Definition: rust-lang.c:109
struct symbol * symbol
Definition: symtab.h:1494
Definition: block.h:109
const char * print_name() const
Definition: symtab.h:474
Definition: gdbtypes.h:922
Definition: value.c:181
const char * domain_name(domain_enum e)
Definition: symtab.c:306
struct block_symbol lookup_global_symbol(const char *name, const struct block *block, const domain_enum domain)
Definition: symtab.c:2614
struct block_symbol lookup_symbol_in_static_block(const char *name, const struct block *block, const domain_enum domain)
Definition: symtab.c:2483
#define symbol_lookup_debug_printf(fmt,...)
Definition: symtab.h:2646
domain_enum
Definition: symtab.h:871
std::string type_to_string(struct type *type)
Definition: typeprint.c:402
void type_print(struct type *type, const char *varstring, struct ui_file *stream, int show)
Definition: typeprint.c:391
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition: utils.c:1865
void gdb_puts(const char *linebuffer, struct ui_file *stream)
Definition: utils.c:1788