rust-lang.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* Rust language support definitions for GDB, the GNU debugger.
  2. Copyright (C) 2016-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. #ifndef RUST_LANG_H
  15. #define RUST_LANG_H
  16. #include "demangle.h"
  17. #include "language.h"
  18. #include "value.h"
  19. #include "c-lang.h"
  20. struct parser_state;
  21. struct type;
  22. /* Return true if TYPE is a tuple type; otherwise false. */
  23. extern bool rust_tuple_type_p (struct type *type);
  24. /* Return true if TYPE is a tuple struct type; otherwise false. */
  25. extern bool rust_tuple_struct_type_p (struct type *type);
  26. /* Given a block, find the name of the block's crate. Returns an empty
  27. stringif no crate name can be found. */
  28. extern std::string rust_crate_for_block (const struct block *block);
  29. /* Returns the last segment of a Rust path like foo::bar::baz. Will
  30. not handle cases where the last segment contains generics. */
  31. extern const char *rust_last_path_segment (const char *path);
  32. /* Create a new slice type. NAME is the name of the type. ELT_TYPE
  33. is the type of the elements of the slice. USIZE_TYPE is the Rust
  34. "usize" type to use. The new type is allocated whereever ELT_TYPE
  35. is allocated. */
  36. extern struct type *rust_slice_type (const char *name, struct type *elt_type,
  37. struct type *usize_type);
  38. /* Class representing the Rust language. */
  39. class rust_language : public language_defn
  40. {
  41. public:
  42. rust_language ()
  43. : language_defn (language_rust)
  44. { /* Nothing. */ }
  45. /* See language.h. */
  46. const char *name () const override
  47. { return "rust"; }
  48. /* See language.h. */
  49. const char *natural_name () const override
  50. { return "Rust"; }
  51. /* See language.h. */
  52. const std::vector<const char *> &filename_extensions () const override
  53. {
  54. static const std::vector<const char *> extensions = { ".rs" };
  55. return extensions;
  56. }
  57. /* See language.h. */
  58. void language_arch_info (struct gdbarch *gdbarch,
  59. struct language_arch_info *lai) const override;
  60. /* See language.h. */
  61. bool sniff_from_mangled_name
  62. (const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled)
  63. const override
  64. {
  65. *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
  66. return *demangled != NULL;
  67. }
  68. /* See language.h. */
  69. gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
  70. int options) const override
  71. {
  72. return gdb_demangle (mangled, options);
  73. }
  74. /* See language.h. */
  75. void print_type (struct type *type, const char *varstring,
  76. struct ui_file *stream, int show, int level,
  77. const struct type_print_options *flags) const override;
  78. /* See language.h. */
  79. gdb::unique_xmalloc_ptr<char> watch_location_expression
  80. (struct type *type, CORE_ADDR addr) const override
  81. {
  82. type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
  83. std::string name = type_to_string (type);
  84. return xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
  85. name.c_str ());
  86. }
  87. /* See language.h. */
  88. void value_print_inner
  89. (struct value *val, struct ui_file *stream, int recurse,
  90. const struct value_print_options *options) const override;
  91. /* See language.h. */
  92. struct block_symbol lookup_symbol_nonlocal
  93. (const char *name, const struct block *block,
  94. const domain_enum domain) const override
  95. {
  96. struct block_symbol result = {};
  97. if (symbol_lookup_debug)
  98. {
  99. gdb_printf (gdb_stdlog,
  100. "rust_lookup_symbol_non_local"
  101. " (%s, %s (scope %s), %s)\n",
  102. name, host_address_to_string (block),
  103. block_scope (block), domain_name (domain));
  104. }
  105. /* Look up bare names in the block's scope. */
  106. std::string scopedname;
  107. if (name[cp_find_first_component (name)] == '\0')
  108. {
  109. const char *scope = block_scope (block);
  110. if (scope[0] != '\0')
  111. {
  112. scopedname = std::string (scope) + "::" + name;
  113. name = scopedname.c_str ();
  114. }
  115. else
  116. name = NULL;
  117. }
  118. if (name != NULL)
  119. {
  120. result = lookup_symbol_in_static_block (name, block, domain);
  121. if (result.symbol == NULL)
  122. result = lookup_global_symbol (name, block, domain);
  123. }
  124. return result;
  125. }
  126. /* See language.h. */
  127. int parser (struct parser_state *ps) const override;
  128. /* See language.h. */
  129. void emitchar (int ch, struct type *chtype,
  130. struct ui_file *stream, int quoter) const override;
  131. /* See language.h. */
  132. void printchar (int ch, struct type *chtype,
  133. struct ui_file *stream) const override
  134. {
  135. gdb_puts ("'", stream);
  136. emitchar (ch, chtype, stream, '\'');
  137. gdb_puts ("'", stream);
  138. }
  139. /* See language.h. */
  140. void printstr (struct ui_file *stream, struct type *elttype,
  141. const gdb_byte *string, unsigned int length,
  142. const char *encoding, int force_ellipses,
  143. const struct value_print_options *options) const override;
  144. /* See language.h. */
  145. void print_typedef (struct type *type, struct symbol *new_symbol,
  146. struct ui_file *stream) const override
  147. {
  148. type = check_typedef (type);
  149. gdb_printf (stream, "type %s = ", new_symbol->print_name ());
  150. type_print (type, "", stream, 0);
  151. gdb_printf (stream, ";");
  152. }
  153. /* See language.h. */
  154. bool is_string_type_p (struct type *type) const override;
  155. /* See language.h. */
  156. bool range_checking_on_by_default () const override
  157. { return true; }
  158. private:
  159. /* Helper for value_print_inner, arguments are as for that function.
  160. Prints structs and untagged unions. */
  161. void val_print_struct (struct value *val, struct ui_file *stream,
  162. int recurse,
  163. const struct value_print_options *options) const;
  164. /* Helper for value_print_inner, arguments are as for that function.
  165. Prints discriminated unions (Rust enums). */
  166. void print_enum (struct value *val, struct ui_file *stream, int recurse,
  167. const struct value_print_options *options) const;
  168. };
  169. #endif /* RUST_LANG_H */