d-lang.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* D language support routines for GDB, the GNU debugger.
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "language.h"
  17. #include "varobj.h"
  18. #include "d-lang.h"
  19. #include "c-lang.h"
  20. #include "demangle.h"
  21. #include "cp-support.h"
  22. #include "gdbarch.h"
  23. #include "parser-defs.h"
  24. /* The name of the symbol to use to get the name of the main subprogram. */
  25. static const char D_MAIN[] = "D main";
  26. /* Function returning the special symbol name used by D for the main
  27. procedure in the main program if it is found in minimal symbol list.
  28. This function tries to find minimal symbols so that it finds them even
  29. if the program was compiled without debugging information. */
  30. const char *
  31. d_main_name (void)
  32. {
  33. struct bound_minimal_symbol msym;
  34. msym = lookup_minimal_symbol (D_MAIN, NULL, NULL);
  35. if (msym.minsym != NULL)
  36. return D_MAIN;
  37. /* No known entry procedure found, the main program is probably not D. */
  38. return NULL;
  39. }
  40. /* Implements the la_demangle language_defn routine for language D. */
  41. gdb::unique_xmalloc_ptr<char>
  42. d_demangle (const char *symbol, int options)
  43. {
  44. return gdb_demangle (symbol, options | DMGL_DLANG);
  45. }
  46. /* Class representing the D language. */
  47. class d_language : public language_defn
  48. {
  49. public:
  50. d_language ()
  51. : language_defn (language_d)
  52. { /* Nothing. */ }
  53. /* See language.h. */
  54. const char *name () const override
  55. { return "d"; }
  56. /* See language.h. */
  57. const char *natural_name () const override
  58. { return "D"; }
  59. /* See language.h. */
  60. const std::vector<const char *> &filename_extensions () const override
  61. {
  62. static const std::vector<const char *> extensions = { ".d" };
  63. return extensions;
  64. }
  65. /* See language.h. */
  66. void language_arch_info (struct gdbarch *gdbarch,
  67. struct language_arch_info *lai) const override
  68. {
  69. const struct builtin_d_type *builtin = builtin_d_type (gdbarch);
  70. /* Helper function to allow shorter lines below. */
  71. auto add = [&] (struct type * t)
  72. {
  73. lai->add_primitive_type (t);
  74. };
  75. add (builtin->builtin_void);
  76. add (builtin->builtin_bool);
  77. add (builtin->builtin_byte);
  78. add (builtin->builtin_ubyte);
  79. add (builtin->builtin_short);
  80. add (builtin->builtin_ushort);
  81. add (builtin->builtin_int);
  82. add (builtin->builtin_uint);
  83. add (builtin->builtin_long);
  84. add (builtin->builtin_ulong);
  85. add (builtin->builtin_cent);
  86. add (builtin->builtin_ucent);
  87. add (builtin->builtin_float);
  88. add (builtin->builtin_double);
  89. add (builtin->builtin_real);
  90. add (builtin->builtin_ifloat);
  91. add (builtin->builtin_idouble);
  92. add (builtin->builtin_ireal);
  93. add (builtin->builtin_cfloat);
  94. add (builtin->builtin_cdouble);
  95. add (builtin->builtin_creal);
  96. add (builtin->builtin_char);
  97. add (builtin->builtin_wchar);
  98. add (builtin->builtin_dchar);
  99. lai->set_string_char_type (builtin->builtin_char);
  100. lai->set_bool_type (builtin->builtin_bool, "bool");
  101. }
  102. /* See language.h. */
  103. bool sniff_from_mangled_name
  104. (const char *mangled,
  105. gdb::unique_xmalloc_ptr<char> *demangled) const override
  106. {
  107. *demangled = d_demangle (mangled, 0);
  108. return *demangled != NULL;
  109. }
  110. /* See language.h. */
  111. gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
  112. int options) const override
  113. {
  114. return d_demangle (mangled, options);
  115. }
  116. /* See language.h. */
  117. void print_type (struct type *type, const char *varstring,
  118. struct ui_file *stream, int show, int level,
  119. const struct type_print_options *flags) const override
  120. {
  121. c_print_type (type, varstring, stream, show, level, flags);
  122. }
  123. /* See language.h. */
  124. void value_print_inner
  125. (struct value *val, struct ui_file *stream, int recurse,
  126. const struct value_print_options *options) const override
  127. {
  128. return d_value_print_inner (val, stream, recurse, options);
  129. }
  130. /* See language.h. */
  131. struct block_symbol lookup_symbol_nonlocal
  132. (const char *name, const struct block *block,
  133. const domain_enum domain) const override
  134. {
  135. return d_lookup_symbol_nonlocal (this, name, block, domain);
  136. }
  137. /* See language.h. */
  138. int parser (struct parser_state *ps) const override
  139. {
  140. return d_parse (ps);
  141. }
  142. /* See language.h. */
  143. const char *name_of_this () const override
  144. { return "this"; }
  145. };
  146. /* Single instance of the D language class. */
  147. static d_language d_language_defn;
  148. /* Build all D language types for the specified architecture. */
  149. static void *
  150. build_d_types (struct gdbarch *gdbarch)
  151. {
  152. struct builtin_d_type *builtin_d_type
  153. = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_d_type);
  154. /* Basic types. */
  155. builtin_d_type->builtin_void
  156. = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
  157. builtin_d_type->builtin_bool
  158. = arch_boolean_type (gdbarch, 8, 1, "bool");
  159. builtin_d_type->builtin_byte
  160. = arch_integer_type (gdbarch, 8, 0, "byte");
  161. builtin_d_type->builtin_ubyte
  162. = arch_integer_type (gdbarch, 8, 1, "ubyte");
  163. builtin_d_type->builtin_short
  164. = arch_integer_type (gdbarch, 16, 0, "short");
  165. builtin_d_type->builtin_ushort
  166. = arch_integer_type (gdbarch, 16, 1, "ushort");
  167. builtin_d_type->builtin_int
  168. = arch_integer_type (gdbarch, 32, 0, "int");
  169. builtin_d_type->builtin_uint
  170. = arch_integer_type (gdbarch, 32, 1, "uint");
  171. builtin_d_type->builtin_long
  172. = arch_integer_type (gdbarch, 64, 0, "long");
  173. builtin_d_type->builtin_ulong
  174. = arch_integer_type (gdbarch, 64, 1, "ulong");
  175. builtin_d_type->builtin_cent
  176. = arch_integer_type (gdbarch, 128, 0, "cent");
  177. builtin_d_type->builtin_ucent
  178. = arch_integer_type (gdbarch, 128, 1, "ucent");
  179. builtin_d_type->builtin_float
  180. = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
  181. "float", gdbarch_float_format (gdbarch));
  182. builtin_d_type->builtin_double
  183. = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
  184. "double", gdbarch_double_format (gdbarch));
  185. builtin_d_type->builtin_real
  186. = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
  187. "real", gdbarch_long_double_format (gdbarch));
  188. builtin_d_type->builtin_byte->set_instance_flags
  189. (builtin_d_type->builtin_byte->instance_flags ()
  190. | TYPE_INSTANCE_FLAG_NOTTEXT);
  191. builtin_d_type->builtin_ubyte->set_instance_flags
  192. (builtin_d_type->builtin_ubyte->instance_flags ()
  193. | TYPE_INSTANCE_FLAG_NOTTEXT);
  194. /* Imaginary and complex types. */
  195. builtin_d_type->builtin_ifloat
  196. = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
  197. "ifloat", gdbarch_float_format (gdbarch));
  198. builtin_d_type->builtin_idouble
  199. = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
  200. "idouble", gdbarch_double_format (gdbarch));
  201. builtin_d_type->builtin_ireal
  202. = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
  203. "ireal", gdbarch_long_double_format (gdbarch));
  204. builtin_d_type->builtin_cfloat
  205. = init_complex_type ("cfloat", builtin_d_type->builtin_float);
  206. builtin_d_type->builtin_cdouble
  207. = init_complex_type ("cdouble", builtin_d_type->builtin_double);
  208. builtin_d_type->builtin_creal
  209. = init_complex_type ("creal", builtin_d_type->builtin_real);
  210. /* Character types. */
  211. builtin_d_type->builtin_char
  212. = arch_character_type (gdbarch, 8, 1, "char");
  213. builtin_d_type->builtin_wchar
  214. = arch_character_type (gdbarch, 16, 1, "wchar");
  215. builtin_d_type->builtin_dchar
  216. = arch_character_type (gdbarch, 32, 1, "dchar");
  217. return builtin_d_type;
  218. }
  219. static struct gdbarch_data *d_type_data;
  220. /* Return the D type table for the specified architecture. */
  221. const struct builtin_d_type *
  222. builtin_d_type (struct gdbarch *gdbarch)
  223. {
  224. return (const struct builtin_d_type *) gdbarch_data (gdbarch, d_type_data);
  225. }
  226. void _initialize_d_language ();
  227. void
  228. _initialize_d_language ()
  229. {
  230. d_type_data = gdbarch_data_register_post_init (build_d_types);
  231. }