gdb: rewrite how per language primitive types are managed
[binutils-gdb.git] / gdb / d-lang.c
1 /* D language support routines for GDB, the GNU debugger.
2
3 Copyright (C) 2005-2020 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
30 /* The name of the symbol to use to get the name of the main subprogram. */
31 static const char D_MAIN[] = "D main";
32
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. */
37
38 const char *
39 d_main_name (void)
40 {
41 struct bound_minimal_symbol msym;
42
43 msym = lookup_minimal_symbol (D_MAIN, NULL, NULL);
44 if (msym.minsym != NULL)
45 return D_MAIN;
46
47 /* No known entry procedure found, the main program is probably not D. */
48 return NULL;
49 }
50
51 /* Implements the la_demangle language_defn routine for language D. */
52
53 char *
54 d_demangle (const char *symbol, int options)
55 {
56 return gdb_demangle (symbol, options | DMGL_DLANG);
57 }
58
59 /* Table mapping opcodes into strings for printing operators
60 and precedences of the operators. */
61 static const struct op_print d_op_print_tab[] =
62 {
63 {",", BINOP_COMMA, PREC_COMMA, 0},
64 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
65 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
66 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
67 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
68 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
69 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
70 {"==", BINOP_EQUAL, PREC_ORDER, 0},
71 {"!=", BINOP_NOTEQUAL, PREC_ORDER, 0},
72 {"<=", BINOP_LEQ, PREC_ORDER, 0},
73 {">=", BINOP_GEQ, PREC_ORDER, 0},
74 {">", BINOP_GTR, PREC_ORDER, 0},
75 {"<", BINOP_LESS, PREC_ORDER, 0},
76 {">>", BINOP_RSH, PREC_SHIFT, 0},
77 {"<<", BINOP_LSH, PREC_SHIFT, 0},
78 {"+", BINOP_ADD, PREC_ADD, 0},
79 {"-", BINOP_SUB, PREC_ADD, 0},
80 {"~", BINOP_CONCAT, PREC_ADD, 0},
81 {"*", BINOP_MUL, PREC_MUL, 0},
82 {"/", BINOP_DIV, PREC_MUL, 0},
83 {"%", BINOP_REM, PREC_MUL, 0},
84 {"^^", BINOP_EXP, PREC_REPEAT, 0},
85 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
86 {"-", UNOP_NEG, PREC_PREFIX, 0},
87 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
88 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
89 {"*", UNOP_IND, PREC_PREFIX, 0},
90 {"&", UNOP_ADDR, PREC_PREFIX, 0},
91 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
92 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
93 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
94 {NULL, OP_NULL, PREC_PREFIX, 0}
95 };
96
97 /* Class representing the D language. */
98
99 class d_language : public language_defn
100 {
101 public:
102 d_language ()
103 : language_defn (language_d)
104 { /* Nothing. */ }
105
106 /* See language.h. */
107
108 const char *name () const override
109 { return "d"; }
110
111 /* See language.h. */
112
113 const char *natural_name () const override
114 { return "D"; }
115
116 /* See language.h. */
117
118 const std::vector<const char *> &filename_extensions () const override
119 {
120 static const std::vector<const char *> extensions = { ".d" };
121 return extensions;
122 }
123
124 /* See language.h. */
125 void language_arch_info (struct gdbarch *gdbarch,
126 struct language_arch_info *lai) const override
127 {
128 const struct builtin_d_type *builtin = builtin_d_type (gdbarch);
129
130 /* Helper function to allow shorter lines below. */
131 auto add = [&] (struct type * t)
132 {
133 lai->add_primitive_type (t);
134 };
135
136 add (builtin->builtin_void);
137 add (builtin->builtin_bool);
138 add (builtin->builtin_byte);
139 add (builtin->builtin_ubyte);
140 add (builtin->builtin_short);
141 add (builtin->builtin_ushort);
142 add (builtin->builtin_int);
143 add (builtin->builtin_uint);
144 add (builtin->builtin_long);
145 add (builtin->builtin_ulong);
146 add (builtin->builtin_cent);
147 add (builtin->builtin_ucent);
148 add (builtin->builtin_float);
149 add (builtin->builtin_double);
150 add (builtin->builtin_real);
151 add (builtin->builtin_ifloat);
152 add (builtin->builtin_idouble);
153 add (builtin->builtin_ireal);
154 add (builtin->builtin_cfloat);
155 add (builtin->builtin_cdouble);
156 add (builtin->builtin_creal);
157 add (builtin->builtin_char);
158 add (builtin->builtin_wchar);
159 add (builtin->builtin_dchar);
160
161 lai->set_string_char_type (builtin->builtin_char);
162 lai->set_bool_type (builtin->builtin_bool, "bool");
163 }
164
165 /* See language.h. */
166 bool sniff_from_mangled_name (const char *mangled,
167 char **demangled) const override
168 {
169 *demangled = d_demangle (mangled, 0);
170 return *demangled != NULL;
171 }
172
173 /* See language.h. */
174
175 char *demangle_symbol (const char *mangled, int options) const override
176 {
177 return d_demangle (mangled, options);
178 }
179
180 /* See language.h. */
181
182 void print_type (struct type *type, const char *varstring,
183 struct ui_file *stream, int show, int level,
184 const struct type_print_options *flags) const override
185 {
186 c_print_type (type, varstring, stream, show, level, flags);
187 }
188
189 /* See language.h. */
190
191 void value_print_inner
192 (struct value *val, struct ui_file *stream, int recurse,
193 const struct value_print_options *options) const override
194 {
195 return d_value_print_inner (val, stream, recurse, options);
196 }
197
198 /* See language.h. */
199
200 struct block_symbol lookup_symbol_nonlocal
201 (const char *name, const struct block *block,
202 const domain_enum domain) const override
203 {
204 return d_lookup_symbol_nonlocal (this, name, block, domain);
205 }
206
207 /* See language.h. */
208
209 int parser (struct parser_state *ps) const override
210 {
211 return d_parse (ps);
212 }
213
214 /* See language.h. */
215
216 const char *name_of_this () const override
217 { return "this"; }
218
219 /* See language.h. */
220
221 const struct exp_descriptor *expression_ops () const override
222 { return &exp_descriptor_c; }
223
224 /* See language.h. */
225
226 const struct op_print *opcode_print_table () const override
227 { return d_op_print_tab; }
228 };
229
230 /* Single instance of the D language class. */
231
232 static d_language d_language_defn;
233
234 /* Build all D language types for the specified architecture. */
235
236 static void *
237 build_d_types (struct gdbarch *gdbarch)
238 {
239 struct builtin_d_type *builtin_d_type
240 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_d_type);
241
242 /* Basic types. */
243 builtin_d_type->builtin_void
244 = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
245 builtin_d_type->builtin_bool
246 = arch_boolean_type (gdbarch, 8, 1, "bool");
247 builtin_d_type->builtin_byte
248 = arch_integer_type (gdbarch, 8, 0, "byte");
249 builtin_d_type->builtin_ubyte
250 = arch_integer_type (gdbarch, 8, 1, "ubyte");
251 builtin_d_type->builtin_short
252 = arch_integer_type (gdbarch, 16, 0, "short");
253 builtin_d_type->builtin_ushort
254 = arch_integer_type (gdbarch, 16, 1, "ushort");
255 builtin_d_type->builtin_int
256 = arch_integer_type (gdbarch, 32, 0, "int");
257 builtin_d_type->builtin_uint
258 = arch_integer_type (gdbarch, 32, 1, "uint");
259 builtin_d_type->builtin_long
260 = arch_integer_type (gdbarch, 64, 0, "long");
261 builtin_d_type->builtin_ulong
262 = arch_integer_type (gdbarch, 64, 1, "ulong");
263 builtin_d_type->builtin_cent
264 = arch_integer_type (gdbarch, 128, 0, "cent");
265 builtin_d_type->builtin_ucent
266 = arch_integer_type (gdbarch, 128, 1, "ucent");
267 builtin_d_type->builtin_float
268 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
269 "float", gdbarch_float_format (gdbarch));
270 builtin_d_type->builtin_double
271 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
272 "double", gdbarch_double_format (gdbarch));
273 builtin_d_type->builtin_real
274 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
275 "real", gdbarch_long_double_format (gdbarch));
276
277 builtin_d_type->builtin_byte->set_instance_flags
278 (builtin_d_type->builtin_byte->instance_flags ()
279 | TYPE_INSTANCE_FLAG_NOTTEXT);
280
281 builtin_d_type->builtin_ubyte->set_instance_flags
282 (builtin_d_type->builtin_ubyte->instance_flags ()
283 | TYPE_INSTANCE_FLAG_NOTTEXT);
284
285 /* Imaginary and complex types. */
286 builtin_d_type->builtin_ifloat
287 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
288 "ifloat", gdbarch_float_format (gdbarch));
289 builtin_d_type->builtin_idouble
290 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
291 "idouble", gdbarch_double_format (gdbarch));
292 builtin_d_type->builtin_ireal
293 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
294 "ireal", gdbarch_long_double_format (gdbarch));
295 builtin_d_type->builtin_cfloat
296 = init_complex_type ("cfloat", builtin_d_type->builtin_float);
297 builtin_d_type->builtin_cdouble
298 = init_complex_type ("cdouble", builtin_d_type->builtin_double);
299 builtin_d_type->builtin_creal
300 = init_complex_type ("creal", builtin_d_type->builtin_real);
301
302 /* Character types. */
303 builtin_d_type->builtin_char
304 = arch_character_type (gdbarch, 8, 1, "char");
305 builtin_d_type->builtin_wchar
306 = arch_character_type (gdbarch, 16, 1, "wchar");
307 builtin_d_type->builtin_dchar
308 = arch_character_type (gdbarch, 32, 1, "dchar");
309
310 return builtin_d_type;
311 }
312
313 static struct gdbarch_data *d_type_data;
314
315 /* Return the D type table for the specified architecture. */
316
317 const struct builtin_d_type *
318 builtin_d_type (struct gdbarch *gdbarch)
319 {
320 return (const struct builtin_d_type *) gdbarch_data (gdbarch, d_type_data);
321 }
322
323 void _initialize_d_language ();
324 void
325 _initialize_d_language ()
326 {
327 d_type_data = gdbarch_data_register_post_init (build_d_types);
328 }