Remove path name from test case
[binutils-gdb.git] / gdb / rust-lang.h
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
28 struct parser_state;
29 struct type;
30
31 /* Return true if TYPE is a tuple type; otherwise false. */
32 extern bool rust_tuple_type_p (struct type *type);
33
34 /* Return true if TYPE is a tuple struct type; otherwise false. */
35 extern bool rust_tuple_struct_type_p (struct type *type);
36
37 /* Return true if TYPE is a slice type, otherwise false. */
38 extern 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. */
42 extern 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
47 extern 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. */
53 extern 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. */
58 extern struct value *rust_slice_to_array (struct value *val);
59
60 /* Class representing the Rust language. */
61
62 class rust_language : public language_defn
63 {
64 public:
65 rust_language ()
66 : language_defn (language_rust)
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
94 void language_arch_info (struct gdbarch *gdbarch,
95 struct language_arch_info *lai) const override;
96
97 /* See language.h. */
98
99 bool sniff_from_mangled_name
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
141 void value_print_inner
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
152 struct block_symbol lookup_symbol_nonlocal
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 {
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, ";");
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
212 private:
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 */