1 /* Rust language support definitions for GDB, the GNU debugger.
2 
3    Copyright (C) 2016-2021 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 /* 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);
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 
44 extern 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.  */
50 extern 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 
55 class rust_language : public language_defn
56 {
57 public:
rust_language()58   rust_language ()
59     : language_defn (language_rust)
60   { /* Nothing.  */ }
61 
62   /* See language.h.  */
63 
name()64   const char *name () const override
65   { return "rust"; }
66 
67   /* See language.h.  */
68 
natural_name()69   const char *natural_name () const override
70   { return "Rust"; }
71 
72   /* See language.h.  */
73 
filename_extensions()74   const std::vector<const char *> &filename_extensions () const override
75   {
76     static const std::vector<const char *> extensions = { ".rs" };
77     return extensions;
78   }
79 
80   /* See language.h.  */
81 
82   void language_arch_info (struct gdbarch *gdbarch,
83 			   struct language_arch_info *lai) const override;
84 
85   /* See language.h.  */
86 
sniff_from_mangled_name(const char * mangled,char ** demangled)87   bool sniff_from_mangled_name (const char *mangled,
88 				char **demangled) const override
89   {
90     *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
91     return *demangled != NULL;
92   }
93 
94   /* See language.h.  */
95 
demangle_symbol(const char * mangled,int options)96   char *demangle_symbol (const char *mangled, int options) const override
97   {
98     return gdb_demangle (mangled, options);
99   }
100 
101   /* See language.h.  */
102 
103   void print_type (struct type *type, const char *varstring,
104 		   struct ui_file *stream, int show, int level,
105 		   const struct type_print_options *flags) const override;
106 
107   /* See language.h.  */
108 
watch_location_expression(struct type * type,CORE_ADDR addr)109   gdb::unique_xmalloc_ptr<char> watch_location_expression
110 	(struct type *type, CORE_ADDR addr) const override
111   {
112     type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
113     std::string name = type_to_string (type);
114     return gdb::unique_xmalloc_ptr<char>
115       (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
116 		   name.c_str ()));
117   }
118 
119   /* See language.h.  */
120 
121   void value_print_inner
122 	(struct value *val, struct ui_file *stream, int recurse,
123 	 const struct value_print_options *options) const override;
124 
125   /* See language.h.  */
126 
lookup_symbol_nonlocal(const char * name,const struct block * block,const domain_enum domain)127   struct block_symbol lookup_symbol_nonlocal
128 	(const char *name, const struct block *block,
129 	 const domain_enum domain) const override
130   {
131     struct block_symbol result = {};
132 
133     if (symbol_lookup_debug)
134       {
135 	fprintf_unfiltered (gdb_stdlog,
136 			    "rust_lookup_symbol_non_local"
137 			    " (%s, %s (scope %s), %s)\n",
138 			    name, host_address_to_string (block),
139 			    block_scope (block), domain_name (domain));
140       }
141 
142     /* Look up bare names in the block's scope.  */
143     std::string scopedname;
144     if (name[cp_find_first_component (name)] == '\0')
145       {
146 	const char *scope = block_scope (block);
147 
148 	if (scope[0] != '\0')
149 	  {
150 	    scopedname = std::string (scope) + "::" + name;
151 	    name = scopedname.c_str ();
152 	  }
153 	else
154 	  name = NULL;
155       }
156 
157     if (name != NULL)
158       {
159 	result = lookup_symbol_in_static_block (name, block, domain);
160 	if (result.symbol == NULL)
161 	  result = lookup_global_symbol (name, block, domain);
162       }
163     return result;
164   }
165 
166   /* See language.h.  */
167 
168   int parser (struct parser_state *ps) const override;
169 
170   /* See language.h.  */
171 
172   void emitchar (int ch, struct type *chtype,
173 		 struct ui_file *stream, int quoter) const override;
174 
175   /* See language.h.  */
176 
printchar(int ch,struct type * chtype,struct ui_file * stream)177   void printchar (int ch, struct type *chtype,
178 		  struct ui_file *stream) const override
179   {
180     fputs_filtered ("'", stream);
181     emitchar (ch, chtype, stream, '\'');
182     fputs_filtered ("'", stream);
183   }
184 
185   /* See language.h.  */
186 
187   void printstr (struct ui_file *stream, struct type *elttype,
188 		 const gdb_byte *string, unsigned int length,
189 		 const char *encoding, int force_ellipses,
190 		 const struct value_print_options *options) const override;
191 
192   /* See language.h.  */
193 
print_typedef(struct type * type,struct symbol * new_symbol,struct ui_file * stream)194   void print_typedef (struct type *type, struct symbol *new_symbol,
195 		      struct ui_file *stream) const override
196   {
197     type = check_typedef (type);
198     fprintf_filtered (stream, "type %s = ", new_symbol->print_name ());
199     type_print (type, "", stream, 0);
200     fprintf_filtered (stream, ";");
201   }
202 
203   /* See language.h.  */
204 
205   bool is_string_type_p (struct type *type) const override;
206 
207   /* See language.h.  */
208 
range_checking_on_by_default()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 */
229