xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/d-lang.c (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1 /* D language support routines for GDB, the GNU debugger.
2 
3    Copyright (C) 2005-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 #include "defs.h"
21 #include "symtab.h"
22 #include "language.h"
23 #include "varobj.h"
24 #include "d-lang.h"
25 #include "c-lang.h"
26 #include "demangle.h"
27 #include "cp-support.h"
28 #include "gdbarch.h"
29 #include "parser-defs.h"
30 
31 /* The name of the symbol to use to get the name of the main subprogram.  */
32 static const char D_MAIN[] = "D main";
33 
34 /* Function returning the special symbol name used by D for the main
35    procedure in the main program if it is found in minimal symbol list.
36    This function tries to find minimal symbols so that it finds them even
37    if the program was compiled without debugging information.  */
38 
39 const char *
40 d_main_name (void)
41 {
42   struct bound_minimal_symbol msym;
43 
44   msym = lookup_minimal_symbol (D_MAIN, NULL, NULL);
45   if (msym.minsym != NULL)
46     return D_MAIN;
47 
48   /* No known entry procedure found, the main program is probably not D.  */
49   return NULL;
50 }
51 
52 /* Implements the la_demangle language_defn routine for language D.  */
53 
54 gdb::unique_xmalloc_ptr<char>
55 d_demangle (const char *symbol, int options)
56 {
57   return gdb_demangle (symbol, options | DMGL_DLANG);
58 }
59 
60 /* Class representing the D language.  */
61 
62 class d_language : public language_defn
63 {
64 public:
65   d_language ()
66     : language_defn (language_d)
67   { /* Nothing.  */ }
68 
69   /* See language.h.  */
70 
71   const char *name () const override
72   { return "d"; }
73 
74   /* See language.h.  */
75 
76   const char *natural_name () const override
77   { return "D"; }
78 
79   /* See language.h.  */
80 
81   const std::vector<const char *> &filename_extensions () const override
82   {
83     static const std::vector<const char *> extensions = { ".d" };
84     return extensions;
85   }
86 
87   /* See language.h.  */
88   void language_arch_info (struct gdbarch *gdbarch,
89 			   struct language_arch_info *lai) const override
90   {
91     const struct builtin_d_type *builtin = builtin_d_type (gdbarch);
92 
93     /* Helper function to allow shorter lines below.  */
94     auto add  = [&] (struct type * t)
95     {
96       lai->add_primitive_type (t);
97     };
98 
99     add (builtin->builtin_void);
100     add (builtin->builtin_bool);
101     add (builtin->builtin_byte);
102     add (builtin->builtin_ubyte);
103     add (builtin->builtin_short);
104     add (builtin->builtin_ushort);
105     add (builtin->builtin_int);
106     add (builtin->builtin_uint);
107     add (builtin->builtin_long);
108     add (builtin->builtin_ulong);
109     add (builtin->builtin_cent);
110     add (builtin->builtin_ucent);
111     add (builtin->builtin_float);
112     add (builtin->builtin_double);
113     add (builtin->builtin_real);
114     add (builtin->builtin_ifloat);
115     add (builtin->builtin_idouble);
116     add (builtin->builtin_ireal);
117     add (builtin->builtin_cfloat);
118     add (builtin->builtin_cdouble);
119     add (builtin->builtin_creal);
120     add (builtin->builtin_char);
121     add (builtin->builtin_wchar);
122     add (builtin->builtin_dchar);
123 
124     lai->set_string_char_type (builtin->builtin_char);
125     lai->set_bool_type (builtin->builtin_bool, "bool");
126   }
127 
128   /* See language.h.  */
129   bool sniff_from_mangled_name
130        (const char *mangled,
131 	gdb::unique_xmalloc_ptr<char> *demangled) const override
132   {
133     *demangled = d_demangle (mangled, 0);
134     return *demangled != NULL;
135   }
136 
137   /* See language.h.  */
138 
139   gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
140 						 int options) const override
141   {
142     return d_demangle (mangled, options);
143   }
144 
145   /* See language.h.  */
146 
147   bool can_print_type_offsets () const override
148   {
149     return true;
150   }
151 
152   /* See language.h.  */
153 
154   void print_type (struct type *type, const char *varstring,
155 		   struct ui_file *stream, int show, int level,
156 		   const struct type_print_options *flags) const override
157   {
158     c_print_type (type, varstring, stream, show, level, la_language, flags);
159   }
160 
161   /* See language.h.  */
162 
163   void value_print_inner
164 	(struct value *val, struct ui_file *stream, int recurse,
165 	 const struct value_print_options *options) const override
166   {
167     return d_value_print_inner (val, stream, recurse, options);
168   }
169 
170   /* See language.h.  */
171 
172   struct block_symbol lookup_symbol_nonlocal
173 	(const char *name, const struct block *block,
174 	 const domain_enum domain) const override
175   {
176     return d_lookup_symbol_nonlocal (this, name, block, domain);
177   }
178 
179   /* See language.h.  */
180 
181   int parser (struct parser_state *ps) const override
182   {
183     return d_parse (ps);
184   }
185 
186   /* See language.h.  */
187 
188   const char *name_of_this () const override
189   { return "this"; }
190 };
191 
192 /* Single instance of the D language class.  */
193 
194 static d_language d_language_defn;
195 
196 /* Build all D language types for the specified architecture.  */
197 
198 static struct builtin_d_type *
199 build_d_types (struct gdbarch *gdbarch)
200 {
201   struct builtin_d_type *builtin_d_type = new struct builtin_d_type;
202 
203   /* Basic types.  */
204   builtin_d_type->builtin_void
205     = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
206   builtin_d_type->builtin_bool
207     = arch_boolean_type (gdbarch, 8, 1, "bool");
208   builtin_d_type->builtin_byte
209     = arch_integer_type (gdbarch, 8, 0, "byte");
210   builtin_d_type->builtin_ubyte
211     = arch_integer_type (gdbarch, 8, 1, "ubyte");
212   builtin_d_type->builtin_short
213     = arch_integer_type (gdbarch, 16, 0, "short");
214   builtin_d_type->builtin_ushort
215     = arch_integer_type (gdbarch, 16, 1, "ushort");
216   builtin_d_type->builtin_int
217     = arch_integer_type (gdbarch, 32, 0, "int");
218   builtin_d_type->builtin_uint
219     = arch_integer_type (gdbarch, 32, 1, "uint");
220   builtin_d_type->builtin_long
221     = arch_integer_type (gdbarch, 64, 0, "long");
222   builtin_d_type->builtin_ulong
223     = arch_integer_type (gdbarch, 64, 1, "ulong");
224   builtin_d_type->builtin_cent
225     = arch_integer_type (gdbarch, 128, 0, "cent");
226   builtin_d_type->builtin_ucent
227     = arch_integer_type (gdbarch, 128, 1, "ucent");
228   builtin_d_type->builtin_float
229     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
230 		       "float", gdbarch_float_format (gdbarch));
231   builtin_d_type->builtin_double
232     = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
233 		       "double", gdbarch_double_format (gdbarch));
234   builtin_d_type->builtin_real
235     = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
236 		       "real", gdbarch_long_double_format (gdbarch));
237 
238   builtin_d_type->builtin_byte->set_instance_flags
239     (builtin_d_type->builtin_byte->instance_flags ()
240      | TYPE_INSTANCE_FLAG_NOTTEXT);
241 
242   builtin_d_type->builtin_ubyte->set_instance_flags
243     (builtin_d_type->builtin_ubyte->instance_flags ()
244      | TYPE_INSTANCE_FLAG_NOTTEXT);
245 
246   /* Imaginary and complex types.  */
247   builtin_d_type->builtin_ifloat
248     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
249 		       "ifloat", gdbarch_float_format (gdbarch));
250   builtin_d_type->builtin_idouble
251     = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
252 		       "idouble", gdbarch_double_format (gdbarch));
253   builtin_d_type->builtin_ireal
254     = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
255 		       "ireal", gdbarch_long_double_format (gdbarch));
256   builtin_d_type->builtin_cfloat
257     = init_complex_type ("cfloat", builtin_d_type->builtin_float);
258   builtin_d_type->builtin_cdouble
259     = init_complex_type ("cdouble", builtin_d_type->builtin_double);
260   builtin_d_type->builtin_creal
261     = init_complex_type ("creal", builtin_d_type->builtin_real);
262 
263   /* Character types.  */
264   builtin_d_type->builtin_char
265     = arch_character_type (gdbarch, 8, 1, "char");
266   builtin_d_type->builtin_wchar
267     = arch_character_type (gdbarch, 16, 1, "wchar");
268   builtin_d_type->builtin_dchar
269     = arch_character_type (gdbarch, 32, 1, "dchar");
270 
271   return builtin_d_type;
272 }
273 
274 static const registry<gdbarch>::key<struct builtin_d_type> d_type_data;
275 
276 /* Return the D type table for the specified architecture.  */
277 
278 const struct builtin_d_type *
279 builtin_d_type (struct gdbarch *gdbarch)
280 {
281   struct builtin_d_type *result = d_type_data.get (gdbarch);
282   if (result == nullptr)
283     {
284       result = build_d_types (gdbarch);
285       d_type_data.set (gdbarch, result);
286     }
287 
288   return result;
289 }
290