1 /* D language support routines for GDB, the GNU debugger.
3 Copyright (C) 2005-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
26 #include "cp-support.h"
28 #include "parser-defs.h"
30 /* The name of the symbol to use to get the name of the main subprogram. */
31 static const char D_MAIN
[] = "D main";
33 /* Function returning the special symbol name used by D for the main
34 procedure in the main program if it is found in minimal symbol list.
35 This function tries to find minimal symbols so that it finds them even
36 if the program was compiled without debugging information. */
41 struct bound_minimal_symbol msym
;
43 msym
= lookup_minimal_symbol (D_MAIN
, NULL
, NULL
);
44 if (msym
.minsym
!= NULL
)
47 /* No known entry procedure found, the main program is probably not D. */
51 /* Implements the la_demangle language_defn routine for language D. */
53 gdb::unique_xmalloc_ptr
<char>
54 d_demangle (const char *symbol
, int options
)
56 return gdb_demangle (symbol
, options
| DMGL_DLANG
);
59 /* Class representing the D language. */
61 class d_language
: public language_defn
65 : language_defn (language_d
)
70 const char *name () const override
75 const char *natural_name () const override
80 const std::vector
<const char *> &filename_extensions () const override
82 static const std::vector
<const char *> extensions
= { ".d" };
87 void language_arch_info (struct gdbarch
*gdbarch
,
88 struct language_arch_info
*lai
) const override
90 const struct builtin_d_type
*builtin
= builtin_d_type (gdbarch
);
92 /* Helper function to allow shorter lines below. */
93 auto add
= [&] (struct type
* t
)
95 lai
->add_primitive_type (t
);
98 add (builtin
->builtin_void
);
99 add (builtin
->builtin_bool
);
100 add (builtin
->builtin_byte
);
101 add (builtin
->builtin_ubyte
);
102 add (builtin
->builtin_short
);
103 add (builtin
->builtin_ushort
);
104 add (builtin
->builtin_int
);
105 add (builtin
->builtin_uint
);
106 add (builtin
->builtin_long
);
107 add (builtin
->builtin_ulong
);
108 add (builtin
->builtin_cent
);
109 add (builtin
->builtin_ucent
);
110 add (builtin
->builtin_float
);
111 add (builtin
->builtin_double
);
112 add (builtin
->builtin_real
);
113 add (builtin
->builtin_ifloat
);
114 add (builtin
->builtin_idouble
);
115 add (builtin
->builtin_ireal
);
116 add (builtin
->builtin_cfloat
);
117 add (builtin
->builtin_cdouble
);
118 add (builtin
->builtin_creal
);
119 add (builtin
->builtin_char
);
120 add (builtin
->builtin_wchar
);
121 add (builtin
->builtin_dchar
);
123 lai
->set_string_char_type (builtin
->builtin_char
);
124 lai
->set_bool_type (builtin
->builtin_bool
, "bool");
127 /* See language.h. */
128 bool sniff_from_mangled_name
129 (const char *mangled
,
130 gdb::unique_xmalloc_ptr
<char> *demangled
) const override
132 *demangled
= d_demangle (mangled
, 0);
133 return *demangled
!= NULL
;
136 /* See language.h. */
138 gdb::unique_xmalloc_ptr
<char> demangle_symbol (const char *mangled
,
139 int options
) const override
141 return d_demangle (mangled
, options
);
144 /* See language.h. */
146 bool can_print_type_offsets () const override
151 /* See language.h. */
153 void print_type (struct type
*type
, const char *varstring
,
154 struct ui_file
*stream
, int show
, int level
,
155 const struct type_print_options
*flags
) const override
157 c_print_type (type
, varstring
, stream
, show
, level
, la_language
, flags
);
160 /* See language.h. */
162 void value_print_inner
163 (struct value
*val
, struct ui_file
*stream
, int recurse
,
164 const struct value_print_options
*options
) const override
166 return d_value_print_inner (val
, stream
, recurse
, options
);
169 /* See language.h. */
171 struct block_symbol lookup_symbol_nonlocal
172 (const char *name
, const struct block
*block
,
173 const domain_search_flags domain
) const override
175 return d_lookup_symbol_nonlocal (this, name
, block
, domain
);
178 /* See language.h. */
180 int parser (struct parser_state
*ps
) const override
185 /* See language.h. */
187 const char *name_of_this () const override
191 /* Single instance of the D language class. */
193 static d_language d_language_defn
;
195 /* Build all D language types for the specified architecture. */
197 static struct builtin_d_type
*
198 build_d_types (struct gdbarch
*gdbarch
)
200 struct builtin_d_type
*builtin_d_type
= new struct builtin_d_type
;
203 type_allocator
alloc (gdbarch
);
204 builtin_d_type
->builtin_void
= builtin_type (gdbarch
)->builtin_void
;
205 builtin_d_type
->builtin_bool
206 = init_boolean_type (alloc
, 8, 1, "bool");
207 builtin_d_type
->builtin_byte
208 = init_integer_type (alloc
, 8, 0, "byte");
209 builtin_d_type
->builtin_ubyte
210 = init_integer_type (alloc
, 8, 1, "ubyte");
211 builtin_d_type
->builtin_short
212 = init_integer_type (alloc
, 16, 0, "short");
213 builtin_d_type
->builtin_ushort
214 = init_integer_type (alloc
, 16, 1, "ushort");
215 builtin_d_type
->builtin_int
216 = init_integer_type (alloc
, 32, 0, "int");
217 builtin_d_type
->builtin_uint
218 = init_integer_type (alloc
, 32, 1, "uint");
219 builtin_d_type
->builtin_long
220 = init_integer_type (alloc
, 64, 0, "long");
221 builtin_d_type
->builtin_ulong
222 = init_integer_type (alloc
, 64, 1, "ulong");
223 builtin_d_type
->builtin_cent
224 = init_integer_type (alloc
, 128, 0, "cent");
225 builtin_d_type
->builtin_ucent
226 = init_integer_type (alloc
, 128, 1, "ucent");
227 builtin_d_type
->builtin_float
228 = init_float_type (alloc
, gdbarch_float_bit (gdbarch
),
229 "float", gdbarch_float_format (gdbarch
));
230 builtin_d_type
->builtin_double
231 = init_float_type (alloc
, gdbarch_double_bit (gdbarch
),
232 "double", gdbarch_double_format (gdbarch
));
233 builtin_d_type
->builtin_real
234 = init_float_type (alloc
, gdbarch_long_double_bit (gdbarch
),
235 "real", gdbarch_long_double_format (gdbarch
));
237 builtin_d_type
->builtin_byte
->set_instance_flags
238 (builtin_d_type
->builtin_byte
->instance_flags ()
239 | TYPE_INSTANCE_FLAG_NOTTEXT
);
241 builtin_d_type
->builtin_ubyte
->set_instance_flags
242 (builtin_d_type
->builtin_ubyte
->instance_flags ()
243 | TYPE_INSTANCE_FLAG_NOTTEXT
);
245 /* Imaginary and complex types. */
246 builtin_d_type
->builtin_ifloat
247 = init_float_type (alloc
, gdbarch_float_bit (gdbarch
),
248 "ifloat", gdbarch_float_format (gdbarch
));
249 builtin_d_type
->builtin_idouble
250 = init_float_type (alloc
, gdbarch_double_bit (gdbarch
),
251 "idouble", gdbarch_double_format (gdbarch
));
252 builtin_d_type
->builtin_ireal
253 = init_float_type (alloc
, gdbarch_long_double_bit (gdbarch
),
254 "ireal", gdbarch_long_double_format (gdbarch
));
255 builtin_d_type
->builtin_cfloat
256 = init_complex_type ("cfloat", builtin_d_type
->builtin_float
);
257 builtin_d_type
->builtin_cdouble
258 = init_complex_type ("cdouble", builtin_d_type
->builtin_double
);
259 builtin_d_type
->builtin_creal
260 = init_complex_type ("creal", builtin_d_type
->builtin_real
);
262 /* Character types. */
263 builtin_d_type
->builtin_char
264 = init_character_type (alloc
, 8, 1, "char");
265 builtin_d_type
->builtin_wchar
266 = init_character_type (alloc
, 16, 1, "wchar");
267 builtin_d_type
->builtin_dchar
268 = init_character_type (alloc
, 32, 1, "dchar");
270 return builtin_d_type
;
273 static const registry
<gdbarch
>::key
<struct builtin_d_type
> d_type_data
;
275 /* Return the D type table for the specified architecture. */
277 const struct builtin_d_type
*
278 builtin_d_type (struct gdbarch
*gdbarch
)
280 struct builtin_d_type
*result
= d_type_data
.get (gdbarch
);
281 if (result
== nullptr)
283 result
= build_d_types (gdbarch
);
284 d_type_data
.set (gdbarch
, result
);