testsuite, threads: fix LD_LIBRARY_PATH in 'tls-sepdebug.exp'
[binutils-gdb.git] / gdb / d-lang.c
blob32d9cc86eef0563056261abc0db480f0019dd62f
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/>. */
20 #include "symtab.h"
21 #include "language.h"
22 #include "varobj.h"
23 #include "d-lang.h"
24 #include "c-lang.h"
25 #include "demangle.h"
26 #include "cp-support.h"
27 #include "gdbarch.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. */
38 const char *
39 d_main_name (void)
41 bound_minimal_symbol msym
42 = lookup_minimal_symbol (current_program_space, D_MAIN);
43 if (msym.minsym != NULL)
44 return D_MAIN;
46 /* No known entry procedure found, the main program is probably not D. */
47 return NULL;
50 /* Implements the la_demangle language_defn routine for language D. */
52 gdb::unique_xmalloc_ptr<char>
53 d_demangle (const char *symbol, int options)
55 return gdb_demangle (symbol, options | DMGL_DLANG);
58 /* Class representing the D language. */
60 class d_language : public language_defn
62 public:
63 d_language ()
64 : language_defn (language_d)
65 { /* Nothing. */ }
67 /* See language.h. */
69 const char *name () const override
70 { return "d"; }
72 /* See language.h. */
74 const char *natural_name () const override
75 { return "D"; }
77 /* See language.h. */
79 const std::vector<const char *> &filename_extensions () const override
81 static const std::vector<const char *> extensions = { ".d" };
82 return extensions;
85 /* See language.h. */
86 void language_arch_info (struct gdbarch *gdbarch,
87 struct language_arch_info *lai) const override
89 const struct builtin_d_type *builtin = builtin_d_type (gdbarch);
91 /* Helper function to allow shorter lines below. */
92 auto add = [&] (struct type * t)
94 lai->add_primitive_type (t);
97 add (builtin->builtin_void);
98 add (builtin->builtin_bool);
99 add (builtin->builtin_byte);
100 add (builtin->builtin_ubyte);
101 add (builtin->builtin_short);
102 add (builtin->builtin_ushort);
103 add (builtin->builtin_int);
104 add (builtin->builtin_uint);
105 add (builtin->builtin_long);
106 add (builtin->builtin_ulong);
107 add (builtin->builtin_cent);
108 add (builtin->builtin_ucent);
109 add (builtin->builtin_float);
110 add (builtin->builtin_double);
111 add (builtin->builtin_real);
112 add (builtin->builtin_ifloat);
113 add (builtin->builtin_idouble);
114 add (builtin->builtin_ireal);
115 add (builtin->builtin_cfloat);
116 add (builtin->builtin_cdouble);
117 add (builtin->builtin_creal);
118 add (builtin->builtin_char);
119 add (builtin->builtin_wchar);
120 add (builtin->builtin_dchar);
122 lai->set_string_char_type (builtin->builtin_char);
123 lai->set_bool_type (builtin->builtin_bool, "bool");
126 /* See language.h. */
127 bool sniff_from_mangled_name
128 (const char *mangled,
129 gdb::unique_xmalloc_ptr<char> *demangled) const override
131 *demangled = d_demangle (mangled, 0);
132 return *demangled != NULL;
135 /* See language.h. */
137 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
138 int options) const override
140 return d_demangle (mangled, options);
143 /* See language.h. */
145 bool can_print_type_offsets () const override
147 return true;
150 /* See language.h. */
152 void print_type (struct type *type, const char *varstring,
153 struct ui_file *stream, int show, int level,
154 const struct type_print_options *flags) const override
156 c_print_type (type, varstring, stream, show, level, la_language, flags);
159 /* See language.h. */
161 void value_print_inner
162 (struct value *val, struct ui_file *stream, int recurse,
163 const struct value_print_options *options) const override
165 return d_value_print_inner (val, stream, recurse, options);
168 /* See language.h. */
170 struct block_symbol lookup_symbol_nonlocal
171 (const char *name, const struct block *block,
172 const domain_search_flags domain) const override
174 return d_lookup_symbol_nonlocal (this, name, block, domain);
177 /* See language.h. */
179 int parser (struct parser_state *ps) const override
181 return d_parse (ps);
184 /* See language.h. */
186 const char *name_of_this () const override
187 { return "this"; }
190 /* Single instance of the D language class. */
192 static d_language d_language_defn;
194 /* Build all D language types for the specified architecture. */
196 static struct builtin_d_type *
197 build_d_types (struct gdbarch *gdbarch)
199 struct builtin_d_type *builtin_d_type = new struct builtin_d_type;
201 /* Basic types. */
202 type_allocator alloc (gdbarch);
203 builtin_d_type->builtin_void = builtin_type (gdbarch)->builtin_void;
204 builtin_d_type->builtin_bool
205 = init_boolean_type (alloc, 8, 1, "bool");
206 builtin_d_type->builtin_byte
207 = init_integer_type (alloc, 8, 0, "byte");
208 builtin_d_type->builtin_ubyte
209 = init_integer_type (alloc, 8, 1, "ubyte");
210 builtin_d_type->builtin_short
211 = init_integer_type (alloc, 16, 0, "short");
212 builtin_d_type->builtin_ushort
213 = init_integer_type (alloc, 16, 1, "ushort");
214 builtin_d_type->builtin_int
215 = init_integer_type (alloc, 32, 0, "int");
216 builtin_d_type->builtin_uint
217 = init_integer_type (alloc, 32, 1, "uint");
218 builtin_d_type->builtin_long
219 = init_integer_type (alloc, 64, 0, "long");
220 builtin_d_type->builtin_ulong
221 = init_integer_type (alloc, 64, 1, "ulong");
222 builtin_d_type->builtin_cent
223 = init_integer_type (alloc, 128, 0, "cent");
224 builtin_d_type->builtin_ucent
225 = init_integer_type (alloc, 128, 1, "ucent");
226 builtin_d_type->builtin_float
227 = init_float_type (alloc, gdbarch_float_bit (gdbarch),
228 "float", gdbarch_float_format (gdbarch));
229 builtin_d_type->builtin_double
230 = init_float_type (alloc, gdbarch_double_bit (gdbarch),
231 "double", gdbarch_double_format (gdbarch));
232 builtin_d_type->builtin_real
233 = init_float_type (alloc, gdbarch_long_double_bit (gdbarch),
234 "real", gdbarch_long_double_format (gdbarch));
236 builtin_d_type->builtin_byte->set_instance_flags
237 (builtin_d_type->builtin_byte->instance_flags ()
238 | TYPE_INSTANCE_FLAG_NOTTEXT);
240 builtin_d_type->builtin_ubyte->set_instance_flags
241 (builtin_d_type->builtin_ubyte->instance_flags ()
242 | TYPE_INSTANCE_FLAG_NOTTEXT);
244 /* Imaginary and complex types. */
245 builtin_d_type->builtin_ifloat
246 = init_float_type (alloc, gdbarch_float_bit (gdbarch),
247 "ifloat", gdbarch_float_format (gdbarch));
248 builtin_d_type->builtin_idouble
249 = init_float_type (alloc, gdbarch_double_bit (gdbarch),
250 "idouble", gdbarch_double_format (gdbarch));
251 builtin_d_type->builtin_ireal
252 = init_float_type (alloc, gdbarch_long_double_bit (gdbarch),
253 "ireal", gdbarch_long_double_format (gdbarch));
254 builtin_d_type->builtin_cfloat
255 = init_complex_type ("cfloat", builtin_d_type->builtin_float);
256 builtin_d_type->builtin_cdouble
257 = init_complex_type ("cdouble", builtin_d_type->builtin_double);
258 builtin_d_type->builtin_creal
259 = init_complex_type ("creal", builtin_d_type->builtin_real);
261 /* Character types. */
262 builtin_d_type->builtin_char
263 = init_character_type (alloc, 8, 1, "char");
264 builtin_d_type->builtin_wchar
265 = init_character_type (alloc, 16, 1, "wchar");
266 builtin_d_type->builtin_dchar
267 = init_character_type (alloc, 32, 1, "dchar");
269 return builtin_d_type;
272 static const registry<gdbarch>::key<struct builtin_d_type> d_type_data;
274 /* Return the D type table for the specified architecture. */
276 const struct builtin_d_type *
277 builtin_d_type (struct gdbarch *gdbarch)
279 struct builtin_d_type *result = d_type_data.get (gdbarch);
280 if (result == nullptr)
282 result = build_d_types (gdbarch);
283 d_type_data.set (gdbarch, result);
286 return result;