rust-lang.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. /* Rust language support routines 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. #include "defs.h"
  15. #include <ctype.h>
  16. #include "block.h"
  17. #include "c-lang.h"
  18. #include "charset.h"
  19. #include "cp-support.h"
  20. #include "demangle.h"
  21. #include "gdbarch.h"
  22. #include "infcall.h"
  23. #include "objfiles.h"
  24. #include "psymtab.h"
  25. #include "rust-lang.h"
  26. #include "typeprint.h"
  27. #include "valprint.h"
  28. #include "varobj.h"
  29. #include <algorithm>
  30. #include <string>
  31. #include <vector>
  32. #include "cli/cli-style.h"
  33. #include "parser-defs.h"
  34. #include "rust-exp.h"
  35. /* See rust-lang.h. */
  36. const char *
  37. rust_last_path_segment (const char *path)
  38. {
  39. const char *result = strrchr (path, ':');
  40. if (result == NULL)
  41. return path;
  42. return result + 1;
  43. }
  44. /* See rust-lang.h. */
  45. std::string
  46. rust_crate_for_block (const struct block *block)
  47. {
  48. const char *scope = block_scope (block);
  49. if (scope[0] == '\0')
  50. return std::string ();
  51. return std::string (scope, cp_find_first_component (scope));
  52. }
  53. /* Return true if TYPE, which must be a struct type, represents a Rust
  54. enum. */
  55. static bool
  56. rust_enum_p (struct type *type)
  57. {
  58. /* is_dynamic_type will return true if any field has a dynamic
  59. attribute -- but we only want to check the top level. */
  60. return TYPE_HAS_VARIANT_PARTS (type);
  61. }
  62. /* Return true if TYPE, which must be an already-resolved enum type,
  63. has no variants. */
  64. static bool
  65. rust_empty_enum_p (const struct type *type)
  66. {
  67. return type->num_fields () == 0;
  68. }
  69. /* Given an already-resolved enum type and contents, find which
  70. variant is active. */
  71. static int
  72. rust_enum_variant (struct type *type)
  73. {
  74. /* The active variant is simply the first non-artificial field. */
  75. for (int i = 0; i < type->num_fields (); ++i)
  76. if (!TYPE_FIELD_ARTIFICIAL (type, i))
  77. return i;
  78. /* Perhaps we could get here by trying to print an Ada variant
  79. record in Rust mode. Unlikely, but an error is safer than an
  80. assert. */
  81. error (_("Could not find active enum variant"));
  82. }
  83. /* See rust-lang.h. */
  84. bool
  85. rust_tuple_type_p (struct type *type)
  86. {
  87. /* The current implementation is a bit of a hack, but there's
  88. nothing else in the debuginfo to distinguish a tuple from a
  89. struct. */
  90. return (type->code () == TYPE_CODE_STRUCT
  91. && type->name () != NULL
  92. && type->name ()[0] == '(');
  93. }
  94. /* Return true if all non-static fields of a structlike type are in a
  95. sequence like __0, __1, __2. */
  96. static bool
  97. rust_underscore_fields (struct type *type)
  98. {
  99. int i, field_number;
  100. field_number = 0;
  101. if (type->code () != TYPE_CODE_STRUCT)
  102. return false;
  103. for (i = 0; i < type->num_fields (); ++i)
  104. {
  105. if (!field_is_static (&type->field (i)))
  106. {
  107. char buf[20];
  108. xsnprintf (buf, sizeof (buf), "__%d", field_number);
  109. if (strcmp (buf, type->field (i).name ()) != 0)
  110. return false;
  111. field_number++;
  112. }
  113. }
  114. return true;
  115. }
  116. /* See rust-lang.h. */
  117. bool
  118. rust_tuple_struct_type_p (struct type *type)
  119. {
  120. /* This is just an approximation until DWARF can represent Rust more
  121. precisely. We exclude zero-length structs because they may not
  122. be tuple structs, and there's no way to tell. */
  123. return type->num_fields () > 0 && rust_underscore_fields (type);
  124. }
  125. /* Return true if TYPE is a slice type, otherwise false. */
  126. static bool
  127. rust_slice_type_p (struct type *type)
  128. {
  129. return (type->code () == TYPE_CODE_STRUCT
  130. && type->name () != NULL
  131. && (strncmp (type->name (), "&[", 2) == 0
  132. || strcmp (type->name (), "&str") == 0));
  133. }
  134. /* Return true if TYPE is a range type, otherwise false. */
  135. static bool
  136. rust_range_type_p (struct type *type)
  137. {
  138. int i;
  139. if (type->code () != TYPE_CODE_STRUCT
  140. || type->num_fields () > 2
  141. || type->name () == NULL
  142. || strstr (type->name (), "::Range") == NULL)
  143. return false;
  144. if (type->num_fields () == 0)
  145. return true;
  146. i = 0;
  147. if (strcmp (type->field (0).name (), "start") == 0)
  148. {
  149. if (type->num_fields () == 1)
  150. return true;
  151. i = 1;
  152. }
  153. else if (type->num_fields () == 2)
  154. {
  155. /* First field had to be "start". */
  156. return false;
  157. }
  158. return strcmp (type->field (i).name (), "end") == 0;
  159. }
  160. /* Return true if TYPE is an inclusive range type, otherwise false.
  161. This is only valid for types which are already known to be range
  162. types. */
  163. static bool
  164. rust_inclusive_range_type_p (struct type *type)
  165. {
  166. return (strstr (type->name (), "::RangeInclusive") != NULL
  167. || strstr (type->name (), "::RangeToInclusive") != NULL);
  168. }
  169. /* Return true if TYPE seems to be the type "u8", otherwise false. */
  170. static bool
  171. rust_u8_type_p (struct type *type)
  172. {
  173. return (type->code () == TYPE_CODE_INT
  174. && type->is_unsigned ()
  175. && TYPE_LENGTH (type) == 1);
  176. }
  177. /* Return true if TYPE is a Rust character type. */
  178. static bool
  179. rust_chartype_p (struct type *type)
  180. {
  181. return (type->code () == TYPE_CODE_CHAR
  182. && TYPE_LENGTH (type) == 4
  183. && type->is_unsigned ());
  184. }
  185. /* If VALUE represents a trait object pointer, return the underlying
  186. pointer with the correct (i.e., runtime) type. Otherwise, return
  187. NULL. */
  188. static struct value *
  189. rust_get_trait_object_pointer (struct value *value)
  190. {
  191. struct type *type = check_typedef (value_type (value));
  192. if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
  193. return NULL;
  194. /* Try to be a bit resilient if the ABI changes. */
  195. int vtable_field = 0;
  196. for (int i = 0; i < 2; ++i)
  197. {
  198. if (strcmp (type->field (i).name (), "vtable") == 0)
  199. vtable_field = i;
  200. else if (strcmp (type->field (i).name (), "pointer") != 0)
  201. return NULL;
  202. }
  203. CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
  204. struct symbol *symbol = find_symbol_at_address (vtable);
  205. if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
  206. return NULL;
  207. struct rust_vtable_symbol *vtable_sym
  208. = static_cast<struct rust_vtable_symbol *> (symbol);
  209. struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
  210. return value_cast (pointer_type, value_field (value, 1 - vtable_field));
  211. }
  212. /* See language.h. */
  213. void
  214. rust_language::printstr (struct ui_file *stream, struct type *type,
  215. const gdb_byte *string, unsigned int length,
  216. const char *user_encoding, int force_ellipses,
  217. const struct value_print_options *options) const
  218. {
  219. /* Rust always uses UTF-8, but let the caller override this if need
  220. be. */
  221. const char *encoding = user_encoding;
  222. if (user_encoding == NULL || !*user_encoding)
  223. {
  224. /* In Rust strings, characters are "u8". */
  225. if (rust_u8_type_p (type))
  226. encoding = "UTF-8";
  227. else
  228. {
  229. /* This is probably some C string, so let's let C deal with
  230. it. */
  231. c_printstr (stream, type, string, length, user_encoding,
  232. force_ellipses, options);
  233. return;
  234. }
  235. }
  236. /* This is not ideal as it doesn't use our character printer. */
  237. generic_printstr (stream, type, string, length, encoding, force_ellipses,
  238. '"', 0, options);
  239. }
  240. /* Helper function to print a string slice. */
  241. static void
  242. rust_val_print_str (struct ui_file *stream, struct value *val,
  243. const struct value_print_options *options)
  244. {
  245. struct value *base = value_struct_elt (&val, {}, "data_ptr", NULL,
  246. "slice");
  247. struct value *len = value_struct_elt (&val, {}, "length", NULL, "slice");
  248. val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8",
  249. value_as_address (base), value_as_long (len), stream,
  250. options);
  251. }
  252. /* See rust-lang.h. */
  253. void
  254. rust_language::val_print_struct
  255. (struct value *val, struct ui_file *stream, int recurse,
  256. const struct value_print_options *options) const
  257. {
  258. int i;
  259. int first_field;
  260. struct type *type = check_typedef (value_type (val));
  261. if (rust_slice_type_p (type) && strcmp (type->name (), "&str") == 0)
  262. {
  263. /* If what we are printing here is actually a string within a
  264. structure then VAL will be the original parent value, while TYPE
  265. will be the type of the structure representing the string we want
  266. to print.
  267. However, RUST_VAL_PRINT_STR looks up the fields of the string
  268. inside VAL, assuming that VAL is the string.
  269. So, recreate VAL as a value representing just the string. */
  270. val = value_at_lazy (type, value_address (val));
  271. rust_val_print_str (stream, val, options);
  272. return;
  273. }
  274. bool is_tuple = rust_tuple_type_p (type);
  275. bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
  276. struct value_print_options opts;
  277. if (!is_tuple)
  278. {
  279. if (type->name () != NULL)
  280. gdb_printf (stream, "%s", type->name ());
  281. if (type->num_fields () == 0)
  282. return;
  283. if (type->name () != NULL)
  284. gdb_puts (" ", stream);
  285. }
  286. if (is_tuple || is_tuple_struct)
  287. gdb_puts ("(", stream);
  288. else
  289. gdb_puts ("{", stream);
  290. opts = *options;
  291. opts.deref_ref = 0;
  292. first_field = 1;
  293. for (i = 0; i < type->num_fields (); ++i)
  294. {
  295. if (field_is_static (&type->field (i)))
  296. continue;
  297. if (!first_field)
  298. gdb_puts (",", stream);
  299. if (options->prettyformat)
  300. {
  301. gdb_puts ("\n", stream);
  302. print_spaces (2 + 2 * recurse, stream);
  303. }
  304. else if (!first_field)
  305. gdb_puts (" ", stream);
  306. first_field = 0;
  307. if (!is_tuple && !is_tuple_struct)
  308. {
  309. fputs_styled (type->field (i).name (),
  310. variable_name_style.style (), stream);
  311. gdb_puts (": ", stream);
  312. }
  313. common_val_print (value_field (val, i), stream, recurse + 1, &opts,
  314. this);
  315. }
  316. if (options->prettyformat)
  317. {
  318. gdb_puts ("\n", stream);
  319. print_spaces (2 * recurse, stream);
  320. }
  321. if (is_tuple || is_tuple_struct)
  322. gdb_puts (")", stream);
  323. else
  324. gdb_puts ("}", stream);
  325. }
  326. /* See rust-lang.h. */
  327. void
  328. rust_language::print_enum (struct value *val, struct ui_file *stream,
  329. int recurse,
  330. const struct value_print_options *options) const
  331. {
  332. struct value_print_options opts = *options;
  333. struct type *type = check_typedef (value_type (val));
  334. opts.deref_ref = 0;
  335. gdb_assert (rust_enum_p (type));
  336. gdb::array_view<const gdb_byte> view
  337. (value_contents_for_printing (val).data (),
  338. TYPE_LENGTH (value_type (val)));
  339. type = resolve_dynamic_type (type, view, value_address (val));
  340. if (rust_empty_enum_p (type))
  341. {
  342. /* Print the enum type name here to be more clear. */
  343. gdb_printf (stream, _("%s {%p[<No data fields>%p]}"),
  344. type->name (),
  345. metadata_style.style ().ptr (), nullptr);
  346. return;
  347. }
  348. int variant_fieldno = rust_enum_variant (type);
  349. val = value_field (val, variant_fieldno);
  350. struct type *variant_type = type->field (variant_fieldno).type ();
  351. int nfields = variant_type->num_fields ();
  352. bool is_tuple = rust_tuple_struct_type_p (variant_type);
  353. gdb_printf (stream, "%s", variant_type->name ());
  354. if (nfields == 0)
  355. {
  356. /* In case of a nullary variant like 'None', just output
  357. the name. */
  358. return;
  359. }
  360. /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
  361. if (is_tuple)
  362. gdb_printf (stream, "(");
  363. else
  364. {
  365. /* struct variant. */
  366. gdb_printf (stream, "{");
  367. }
  368. bool first_field = true;
  369. for (int j = 0; j < variant_type->num_fields (); j++)
  370. {
  371. if (!first_field)
  372. gdb_puts (", ", stream);
  373. first_field = false;
  374. if (!is_tuple)
  375. gdb_printf (stream, "%ps: ",
  376. styled_string (variable_name_style.style (),
  377. variant_type->field (j).name ()));
  378. common_val_print (value_field (val, j), stream, recurse + 1, &opts,
  379. this);
  380. }
  381. if (is_tuple)
  382. gdb_puts (")", stream);
  383. else
  384. gdb_puts ("}", stream);
  385. }
  386. static const struct generic_val_print_decorations rust_decorations =
  387. {
  388. /* Complex isn't used in Rust, but we provide C-ish values just in
  389. case. */
  390. "",
  391. " + ",
  392. " * I",
  393. "true",
  394. "false",
  395. "()",
  396. "[",
  397. "]"
  398. };
  399. /* See language.h. */
  400. void
  401. rust_language::value_print_inner
  402. (struct value *val, struct ui_file *stream, int recurse,
  403. const struct value_print_options *options) const
  404. {
  405. struct value_print_options opts = *options;
  406. opts.deref_ref = 1;
  407. if (opts.prettyformat == Val_prettyformat_default)
  408. opts.prettyformat = (opts.prettyformat_structs
  409. ? Val_prettyformat : Val_no_prettyformat);
  410. struct type *type = check_typedef (value_type (val));
  411. switch (type->code ())
  412. {
  413. case TYPE_CODE_PTR:
  414. {
  415. LONGEST low_bound, high_bound;
  416. if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
  417. && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
  418. && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
  419. &high_bound))
  420. {
  421. /* We have a pointer to a byte string, so just print
  422. that. */
  423. struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
  424. CORE_ADDR addr = value_as_address (val);
  425. struct gdbarch *arch = type->arch ();
  426. if (opts.addressprint)
  427. {
  428. gdb_puts (paddress (arch, addr), stream);
  429. gdb_puts (" ", stream);
  430. }
  431. gdb_puts ("b", stream);
  432. val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
  433. high_bound - low_bound + 1, stream,
  434. &opts);
  435. break;
  436. }
  437. }
  438. goto generic_print;
  439. case TYPE_CODE_INT:
  440. /* Recognize the unit type. */
  441. if (type->is_unsigned () && TYPE_LENGTH (type) == 0
  442. && type->name () != NULL && strcmp (type->name (), "()") == 0)
  443. {
  444. gdb_puts ("()", stream);
  445. break;
  446. }
  447. goto generic_print;
  448. case TYPE_CODE_STRING:
  449. {
  450. LONGEST low_bound, high_bound;
  451. if (!get_array_bounds (type, &low_bound, &high_bound))
  452. error (_("Could not determine the array bounds"));
  453. /* If we see a plain TYPE_CODE_STRING, then we're printing a
  454. byte string, hence the choice of "ASCII" as the
  455. encoding. */
  456. gdb_puts ("b", stream);
  457. printstr (stream, TYPE_TARGET_TYPE (type),
  458. value_contents_for_printing (val).data (),
  459. high_bound - low_bound + 1, "ASCII", 0, &opts);
  460. }
  461. break;
  462. case TYPE_CODE_ARRAY:
  463. {
  464. LONGEST low_bound, high_bound;
  465. if (get_array_bounds (type, &low_bound, &high_bound)
  466. && high_bound - low_bound + 1 == 0)
  467. gdb_puts ("[]", stream);
  468. else
  469. goto generic_print;
  470. }
  471. break;
  472. case TYPE_CODE_UNION:
  473. /* Untagged unions are printed as if they are structs. Since
  474. the field bit positions overlap in the debuginfo, the code
  475. for printing a union is same as that for a struct, the only
  476. difference is that the input type will have overlapping
  477. fields. */
  478. val_print_struct (val, stream, recurse, &opts);
  479. break;
  480. case TYPE_CODE_STRUCT:
  481. if (rust_enum_p (type))
  482. print_enum (val, stream, recurse, &opts);
  483. else
  484. val_print_struct (val, stream, recurse, &opts);
  485. break;
  486. default:
  487. generic_print:
  488. /* Nothing special yet. */
  489. generic_value_print (val, stream, recurse, &opts, &rust_decorations);
  490. }
  491. }
  492. static void
  493. rust_internal_print_type (struct type *type, const char *varstring,
  494. struct ui_file *stream, int show, int level,
  495. const struct type_print_options *flags,
  496. bool for_rust_enum, print_offset_data *podata);
  497. /* Print a struct or union typedef. */
  498. static void
  499. rust_print_struct_def (struct type *type, const char *varstring,
  500. struct ui_file *stream, int show, int level,
  501. const struct type_print_options *flags,
  502. bool for_rust_enum, print_offset_data *podata)
  503. {
  504. /* Print a tuple type simply. */
  505. if (rust_tuple_type_p (type))
  506. {
  507. gdb_puts (type->name (), stream);
  508. return;
  509. }
  510. /* If we see a base class, delegate to C. */
  511. if (TYPE_N_BASECLASSES (type) > 0)
  512. c_print_type (type, varstring, stream, show, level, flags);
  513. if (flags->print_offsets)
  514. {
  515. /* Temporarily bump the level so that the output lines up
  516. correctly. */
  517. level += 2;
  518. }
  519. /* Compute properties of TYPE here because, in the enum case, the
  520. rest of the code ends up looking only at the variant part. */
  521. const char *tagname = type->name ();
  522. bool is_tuple_struct = rust_tuple_struct_type_p (type);
  523. bool is_tuple = rust_tuple_type_p (type);
  524. bool is_enum = rust_enum_p (type);
  525. if (for_rust_enum)
  526. {
  527. /* Already printing an outer enum, so nothing to print here. */
  528. }
  529. else
  530. {
  531. /* This code path is also used by unions and enums. */
  532. if (is_enum)
  533. {
  534. gdb_puts ("enum ", stream);
  535. dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
  536. if (prop != nullptr && prop->kind () == PROP_TYPE)
  537. type = prop->original_type ();
  538. }
  539. else if (type->code () == TYPE_CODE_STRUCT)
  540. gdb_puts ("struct ", stream);
  541. else
  542. gdb_puts ("union ", stream);
  543. if (tagname != NULL)
  544. gdb_puts (tagname, stream);
  545. }
  546. if (type->num_fields () == 0 && !is_tuple)
  547. return;
  548. if (for_rust_enum && !flags->print_offsets)
  549. gdb_puts (is_tuple_struct ? "(" : "{", stream);
  550. else
  551. gdb_puts (is_tuple_struct ? " (\n" : " {\n", stream);
  552. /* When printing offsets, we rearrange the fields into storage
  553. order. This lets us show holes more clearly. We work using
  554. field indices here because it simplifies calls to
  555. print_offset_data::update below. */
  556. std::vector<int> fields;
  557. for (int i = 0; i < type->num_fields (); ++i)
  558. {
  559. if (field_is_static (&type->field (i)))
  560. continue;
  561. if (is_enum && TYPE_FIELD_ARTIFICIAL (type, i))
  562. continue;
  563. fields.push_back (i);
  564. }
  565. if (flags->print_offsets)
  566. std::sort (fields.begin (), fields.end (),
  567. [&] (int a, int b)
  568. {
  569. return (type->field (a).loc_bitpos ()
  570. < type->field (b).loc_bitpos ());
  571. });
  572. for (int i : fields)
  573. {
  574. QUIT;
  575. gdb_assert (!field_is_static (&type->field (i)));
  576. gdb_assert (! (is_enum && TYPE_FIELD_ARTIFICIAL (type, i)));
  577. if (flags->print_offsets)
  578. podata->update (type, i, stream);
  579. /* We'd like to print "pub" here as needed, but rustc
  580. doesn't emit the debuginfo, and our types don't have
  581. cplus_struct_type attached. */
  582. /* For a tuple struct we print the type but nothing
  583. else. */
  584. if (!for_rust_enum || flags->print_offsets)
  585. print_spaces (level + 2, stream);
  586. if (is_enum)
  587. fputs_styled (type->field (i).name (), variable_name_style.style (),
  588. stream);
  589. else if (!is_tuple_struct)
  590. gdb_printf (stream, "%ps: ",
  591. styled_string (variable_name_style.style (),
  592. type->field (i).name ()));
  593. rust_internal_print_type (type->field (i).type (), NULL,
  594. stream, (is_enum ? show : show - 1),
  595. level + 2, flags, is_enum, podata);
  596. if (!for_rust_enum || flags->print_offsets)
  597. gdb_puts (",\n", stream);
  598. /* Note that this check of "I" is ok because we only sorted the
  599. fields by offset when print_offsets was set, so we won't take
  600. this branch in that case. */
  601. else if (i + 1 < type->num_fields ())
  602. gdb_puts (", ", stream);
  603. }
  604. if (flags->print_offsets)
  605. {
  606. /* Undo the temporary level increase we did above. */
  607. level -= 2;
  608. podata->finish (type, level, stream);
  609. print_spaces (print_offset_data::indentation, stream);
  610. if (level == 0)
  611. print_spaces (2, stream);
  612. }
  613. if (!for_rust_enum || flags->print_offsets)
  614. print_spaces (level, stream);
  615. gdb_puts (is_tuple_struct ? ")" : "}", stream);
  616. }
  617. /* la_print_type implementation for Rust. */
  618. static void
  619. rust_internal_print_type (struct type *type, const char *varstring,
  620. struct ui_file *stream, int show, int level,
  621. const struct type_print_options *flags,
  622. bool for_rust_enum, print_offset_data *podata)
  623. {
  624. QUIT;
  625. if (show <= 0
  626. && type->name () != NULL)
  627. {
  628. /* Rust calls the unit type "void" in its debuginfo,
  629. but we don't want to print it as that. */
  630. if (type->code () == TYPE_CODE_VOID)
  631. gdb_puts ("()", stream);
  632. else
  633. gdb_puts (type->name (), stream);
  634. return;
  635. }
  636. type = check_typedef (type);
  637. switch (type->code ())
  638. {
  639. case TYPE_CODE_VOID:
  640. /* If we have an enum, we've already printed the type's
  641. unqualified name, and there is nothing else to print
  642. here. */
  643. if (!for_rust_enum)
  644. gdb_puts ("()", stream);
  645. break;
  646. case TYPE_CODE_FUNC:
  647. /* Delegate varargs to the C printer. */
  648. if (type->has_varargs ())
  649. goto c_printer;
  650. gdb_puts ("fn ", stream);
  651. if (varstring != NULL)
  652. gdb_puts (varstring, stream);
  653. gdb_puts ("(", stream);
  654. for (int i = 0; i < type->num_fields (); ++i)
  655. {
  656. QUIT;
  657. if (i > 0)
  658. gdb_puts (", ", stream);
  659. rust_internal_print_type (type->field (i).type (), "", stream,
  660. -1, 0, flags, false, podata);
  661. }
  662. gdb_puts (")", stream);
  663. /* If it returns unit, we can omit the return type. */
  664. if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
  665. {
  666. gdb_puts (" -> ", stream);
  667. rust_internal_print_type (TYPE_TARGET_TYPE (type), "", stream,
  668. -1, 0, flags, false, podata);
  669. }
  670. break;
  671. case TYPE_CODE_ARRAY:
  672. {
  673. LONGEST low_bound, high_bound;
  674. gdb_puts ("[", stream);
  675. rust_internal_print_type (TYPE_TARGET_TYPE (type), NULL,
  676. stream, show - 1, level, flags, false,
  677. podata);
  678. if (type->bounds ()->high.kind () == PROP_LOCEXPR
  679. || type->bounds ()->high.kind () == PROP_LOCLIST)
  680. gdb_printf (stream, "; variable length");
  681. else if (get_array_bounds (type, &low_bound, &high_bound))
  682. gdb_printf (stream, "; %s",
  683. plongest (high_bound - low_bound + 1));
  684. gdb_puts ("]", stream);
  685. }
  686. break;
  687. case TYPE_CODE_UNION:
  688. case TYPE_CODE_STRUCT:
  689. rust_print_struct_def (type, varstring, stream, show, level, flags,
  690. for_rust_enum, podata);
  691. break;
  692. case TYPE_CODE_ENUM:
  693. {
  694. int len = 0;
  695. gdb_puts ("enum ", stream);
  696. if (type->name () != NULL)
  697. {
  698. gdb_puts (type->name (), stream);
  699. gdb_puts (" ", stream);
  700. len = strlen (type->name ());
  701. }
  702. gdb_puts ("{\n", stream);
  703. for (int i = 0; i < type->num_fields (); ++i)
  704. {
  705. const char *name = type->field (i).name ();
  706. QUIT;
  707. if (len > 0
  708. && strncmp (name, type->name (), len) == 0
  709. && name[len] == ':'
  710. && name[len + 1] == ':')
  711. name += len + 2;
  712. gdb_printf (stream, "%*s%ps,\n",
  713. level + 2, "",
  714. styled_string (variable_name_style.style (),
  715. name));
  716. }
  717. gdb_puts ("}", stream);
  718. }
  719. break;
  720. case TYPE_CODE_PTR:
  721. {
  722. if (type->name () != nullptr)
  723. gdb_puts (type->name (), stream);
  724. else
  725. {
  726. /* We currently can't distinguish between pointers and
  727. references. */
  728. gdb_puts ("*mut ", stream);
  729. type_print (TYPE_TARGET_TYPE (type), "", stream, 0);
  730. }
  731. }
  732. break;
  733. default:
  734. c_printer:
  735. c_print_type (type, varstring, stream, show, level, flags);
  736. }
  737. }
  738. /* Like arch_composite_type, but uses TYPE to decide how to allocate
  739. -- either on an obstack or on a gdbarch. */
  740. static struct type *
  741. rust_composite_type (struct type *original,
  742. const char *name,
  743. const char *field1, struct type *type1,
  744. const char *field2, struct type *type2)
  745. {
  746. struct type *result = alloc_type_copy (original);
  747. int i, nfields, bitpos;
  748. nfields = 0;
  749. if (field1 != NULL)
  750. ++nfields;
  751. if (field2 != NULL)
  752. ++nfields;
  753. result->set_code (TYPE_CODE_STRUCT);
  754. result->set_name (name);
  755. result->set_num_fields (nfields);
  756. result->set_fields
  757. ((struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field)));
  758. i = 0;
  759. bitpos = 0;
  760. if (field1 != NULL)
  761. {
  762. struct field *field = &result->field (i);
  763. field->set_loc_bitpos (bitpos);
  764. bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
  765. field->set_name (field1);
  766. field->set_type (type1);
  767. ++i;
  768. }
  769. if (field2 != NULL)
  770. {
  771. struct field *field = &result->field (i);
  772. unsigned align = type_align (type2);
  773. if (align != 0)
  774. {
  775. int delta;
  776. align *= TARGET_CHAR_BIT;
  777. delta = bitpos % align;
  778. if (delta != 0)
  779. bitpos += align - delta;
  780. }
  781. field->set_loc_bitpos (bitpos);
  782. field->set_name (field2);
  783. field->set_type (type2);
  784. ++i;
  785. }
  786. if (i > 0)
  787. TYPE_LENGTH (result)
  788. = (result->field (i - 1).loc_bitpos () / TARGET_CHAR_BIT +
  789. TYPE_LENGTH (result->field (i - 1).type ()));
  790. return result;
  791. }
  792. /* See rust-lang.h. */
  793. struct type *
  794. rust_slice_type (const char *name, struct type *elt_type,
  795. struct type *usize_type)
  796. {
  797. struct type *type;
  798. elt_type = lookup_pointer_type (elt_type);
  799. type = rust_composite_type (elt_type, name,
  800. "data_ptr", elt_type,
  801. "length", usize_type);
  802. return type;
  803. }
  804. /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
  805. struct value *
  806. rust_range (struct type *expect_type, struct expression *exp,
  807. enum noside noside, enum range_flag kind,
  808. struct value *low, struct value *high)
  809. {
  810. struct value *addrval, *result;
  811. CORE_ADDR addr;
  812. struct type *range_type;
  813. struct type *index_type;
  814. struct type *temp_type;
  815. const char *name;
  816. bool inclusive = !(kind & RANGE_HIGH_BOUND_EXCLUSIVE);
  817. if (low == NULL)
  818. {
  819. if (high == NULL)
  820. {
  821. index_type = NULL;
  822. name = "std::ops::RangeFull";
  823. }
  824. else
  825. {
  826. index_type = value_type (high);
  827. name = (inclusive
  828. ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
  829. }
  830. }
  831. else
  832. {
  833. if (high == NULL)
  834. {
  835. index_type = value_type (low);
  836. name = "std::ops::RangeFrom";
  837. }
  838. else
  839. {
  840. if (!types_equal (value_type (low), value_type (high)))
  841. error (_("Range expression with different types"));
  842. index_type = value_type (low);
  843. name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
  844. }
  845. }
  846. /* If we don't have an index type, just allocate this on the
  847. arch. Here any type will do. */
  848. temp_type = (index_type == NULL
  849. ? language_bool_type (exp->language_defn, exp->gdbarch)
  850. : index_type);
  851. /* It would be nicer to cache the range type. */
  852. range_type = rust_composite_type (temp_type, name,
  853. low == NULL ? NULL : "start", index_type,
  854. high == NULL ? NULL : "end", index_type);
  855. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  856. return value_zero (range_type, lval_memory);
  857. addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
  858. addr = value_as_long (addrval);
  859. result = value_at_lazy (range_type, addr);
  860. if (low != NULL)
  861. {
  862. struct value *start = value_struct_elt (&result, {}, "start", NULL,
  863. "range");
  864. value_assign (start, low);
  865. }
  866. if (high != NULL)
  867. {
  868. struct value *end = value_struct_elt (&result, {}, "end", NULL,
  869. "range");
  870. value_assign (end, high);
  871. }
  872. result = value_at_lazy (range_type, addr);
  873. return result;
  874. }
  875. /* A helper function to compute the range and kind given a range
  876. value. TYPE is the type of the range value. RANGE is the range
  877. value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
  878. parameters might be filled in, or might not be, depending on the
  879. kind of range this is. KIND will always be set to the appropriate
  880. value describing the kind of range, and this can be used to
  881. determine whether LOW or HIGH are valid. */
  882. static void
  883. rust_compute_range (struct type *type, struct value *range,
  884. LONGEST *low, LONGEST *high,
  885. range_flags *kind)
  886. {
  887. int i;
  888. *low = 0;
  889. *high = 0;
  890. *kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
  891. if (type->num_fields () == 0)
  892. return;
  893. i = 0;
  894. if (strcmp (type->field (0).name (), "start") == 0)
  895. {
  896. *kind = RANGE_HIGH_BOUND_DEFAULT;
  897. *low = value_as_long (value_field (range, 0));
  898. ++i;
  899. }
  900. if (type->num_fields () > i
  901. && strcmp (type->field (i).name (), "end") == 0)
  902. {
  903. *kind = (*kind == (RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT)
  904. ? RANGE_LOW_BOUND_DEFAULT : RANGE_STANDARD);
  905. *high = value_as_long (value_field (range, i));
  906. if (rust_inclusive_range_type_p (type))
  907. ++*high;
  908. }
  909. }
  910. /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
  911. struct value *
  912. rust_subscript (struct type *expect_type, struct expression *exp,
  913. enum noside noside, bool for_addr,
  914. struct value *lhs, struct value *rhs)
  915. {
  916. struct value *result;
  917. struct type *rhstype;
  918. LONGEST low, high_bound;
  919. /* Initialized to appease the compiler. */
  920. range_flags kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
  921. LONGEST high = 0;
  922. int want_slice = 0;
  923. rhstype = check_typedef (value_type (rhs));
  924. if (rust_range_type_p (rhstype))
  925. {
  926. if (!for_addr)
  927. error (_("Can't take slice of array without '&'"));
  928. rust_compute_range (rhstype, rhs, &low, &high, &kind);
  929. want_slice = 1;
  930. }
  931. else
  932. low = value_as_long (rhs);
  933. struct type *type = check_typedef (value_type (lhs));
  934. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  935. {
  936. struct type *base_type = nullptr;
  937. if (type->code () == TYPE_CODE_ARRAY)
  938. base_type = TYPE_TARGET_TYPE (type);
  939. else if (rust_slice_type_p (type))
  940. {
  941. for (int i = 0; i < type->num_fields (); ++i)
  942. {
  943. if (strcmp (type->field (i).name (), "data_ptr") == 0)
  944. {
  945. base_type = TYPE_TARGET_TYPE (type->field (i).type ());
  946. break;
  947. }
  948. }
  949. if (base_type == nullptr)
  950. error (_("Could not find 'data_ptr' in slice type"));
  951. }
  952. else if (type->code () == TYPE_CODE_PTR)
  953. base_type = TYPE_TARGET_TYPE (type);
  954. else
  955. error (_("Cannot subscript non-array type"));
  956. struct type *new_type;
  957. if (want_slice)
  958. {
  959. if (rust_slice_type_p (type))
  960. new_type = type;
  961. else
  962. {
  963. struct type *usize
  964. = language_lookup_primitive_type (exp->language_defn,
  965. exp->gdbarch,
  966. "usize");
  967. new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
  968. }
  969. }
  970. else
  971. new_type = base_type;
  972. return value_zero (new_type, VALUE_LVAL (lhs));
  973. }
  974. else
  975. {
  976. LONGEST low_bound;
  977. struct value *base;
  978. if (type->code () == TYPE_CODE_ARRAY)
  979. {
  980. base = lhs;
  981. if (!get_array_bounds (type, &low_bound, &high_bound))
  982. error (_("Can't compute array bounds"));
  983. if (low_bound != 0)
  984. error (_("Found array with non-zero lower bound"));
  985. ++high_bound;
  986. }
  987. else if (rust_slice_type_p (type))
  988. {
  989. struct value *len;
  990. base = value_struct_elt (&lhs, {}, "data_ptr", NULL, "slice");
  991. len = value_struct_elt (&lhs, {}, "length", NULL, "slice");
  992. low_bound = 0;
  993. high_bound = value_as_long (len);
  994. }
  995. else if (type->code () == TYPE_CODE_PTR)
  996. {
  997. base = lhs;
  998. low_bound = 0;
  999. high_bound = LONGEST_MAX;
  1000. }
  1001. else
  1002. error (_("Cannot subscript non-array type"));
  1003. if (want_slice && (kind & RANGE_LOW_BOUND_DEFAULT))
  1004. low = low_bound;
  1005. if (low < 0)
  1006. error (_("Index less than zero"));
  1007. if (low > high_bound)
  1008. error (_("Index greater than length"));
  1009. result = value_subscript (base, low);
  1010. }
  1011. if (for_addr)
  1012. {
  1013. if (want_slice)
  1014. {
  1015. struct type *usize, *slice;
  1016. CORE_ADDR addr;
  1017. struct value *addrval, *tem;
  1018. if (kind & RANGE_HIGH_BOUND_DEFAULT)
  1019. high = high_bound;
  1020. if (high < 0)
  1021. error (_("High index less than zero"));
  1022. if (low > high)
  1023. error (_("Low index greater than high index"));
  1024. if (high > high_bound)
  1025. error (_("High index greater than length"));
  1026. usize = language_lookup_primitive_type (exp->language_defn,
  1027. exp->gdbarch,
  1028. "usize");
  1029. const char *new_name = ((type != nullptr
  1030. && rust_slice_type_p (type))
  1031. ? type->name () : "&[*gdb*]");
  1032. slice = rust_slice_type (new_name, value_type (result), usize);
  1033. addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
  1034. addr = value_as_long (addrval);
  1035. tem = value_at_lazy (slice, addr);
  1036. value_assign (value_field (tem, 0), value_addr (result));
  1037. value_assign (value_field (tem, 1),
  1038. value_from_longest (usize, high - low));
  1039. result = value_at_lazy (slice, addr);
  1040. }
  1041. else
  1042. result = value_addr (result);
  1043. }
  1044. return result;
  1045. }
  1046. namespace expr
  1047. {
  1048. struct value *
  1049. rust_unop_ind_operation::evaluate (struct type *expect_type,
  1050. struct expression *exp,
  1051. enum noside noside)
  1052. {
  1053. if (noside != EVAL_NORMAL)
  1054. return unop_ind_operation::evaluate (expect_type, exp, noside);
  1055. struct value *value = std::get<0> (m_storage)->evaluate (nullptr, exp,
  1056. noside);
  1057. struct value *trait_ptr = rust_get_trait_object_pointer (value);
  1058. if (trait_ptr != NULL)
  1059. value = trait_ptr;
  1060. return value_ind (value);
  1061. }
  1062. } /* namespace expr */
  1063. /* A helper function for UNOP_COMPLEMENT. */
  1064. struct value *
  1065. eval_op_rust_complement (struct type *expect_type, struct expression *exp,
  1066. enum noside noside,
  1067. enum exp_opcode opcode,
  1068. struct value *value)
  1069. {
  1070. if (value_type (value)->code () == TYPE_CODE_BOOL)
  1071. return value_from_longest (value_type (value), value_logical_not (value));
  1072. return value_complement (value);
  1073. }
  1074. /* A helper function for OP_ARRAY. */
  1075. struct value *
  1076. eval_op_rust_array (struct type *expect_type, struct expression *exp,
  1077. enum noside noside,
  1078. enum exp_opcode opcode,
  1079. struct value *elt, struct value *ncopies)
  1080. {
  1081. int copies = value_as_long (ncopies);
  1082. if (copies < 0)
  1083. error (_("Array with negative number of elements"));
  1084. if (noside == EVAL_NORMAL)
  1085. {
  1086. int i;
  1087. std::vector<struct value *> eltvec (copies);
  1088. for (i = 0; i < copies; ++i)
  1089. eltvec[i] = elt;
  1090. return value_array (0, copies - 1, eltvec.data ());
  1091. }
  1092. else
  1093. {
  1094. struct type *arraytype
  1095. = lookup_array_range_type (value_type (elt), 0, copies - 1);
  1096. return allocate_value (arraytype);
  1097. }
  1098. }
  1099. namespace expr
  1100. {
  1101. struct value *
  1102. rust_struct_anon::evaluate (struct type *expect_type,
  1103. struct expression *exp,
  1104. enum noside noside)
  1105. {
  1106. value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
  1107. int field_number = std::get<0> (m_storage);
  1108. struct type *type = value_type (lhs);
  1109. if (type->code () == TYPE_CODE_STRUCT)
  1110. {
  1111. struct type *outer_type = NULL;
  1112. if (rust_enum_p (type))
  1113. {
  1114. type = resolve_dynamic_type (type, value_contents (lhs),
  1115. value_address (lhs));
  1116. if (rust_empty_enum_p (type))
  1117. error (_("Cannot access field %d of empty enum %s"),
  1118. field_number, type->name ());
  1119. int fieldno = rust_enum_variant (type);
  1120. lhs = value_primitive_field (lhs, 0, fieldno, type);
  1121. outer_type = type;
  1122. type = value_type (lhs);
  1123. }
  1124. /* Tuples and tuple structs */
  1125. int nfields = type->num_fields ();
  1126. if (field_number >= nfields || field_number < 0)
  1127. {
  1128. if (outer_type != NULL)
  1129. error(_("Cannot access field %d of variant %s::%s, "
  1130. "there are only %d fields"),
  1131. field_number, outer_type->name (),
  1132. rust_last_path_segment (type->name ()),
  1133. nfields);
  1134. else
  1135. error(_("Cannot access field %d of %s, "
  1136. "there are only %d fields"),
  1137. field_number, type->name (), nfields);
  1138. }
  1139. /* Tuples are tuple structs too. */
  1140. if (!rust_tuple_struct_type_p (type))
  1141. {
  1142. if (outer_type != NULL)
  1143. error(_("Variant %s::%s is not a tuple variant"),
  1144. outer_type->name (),
  1145. rust_last_path_segment (type->name ()));
  1146. else
  1147. error(_("Attempting to access anonymous field %d "
  1148. "of %s, which is not a tuple, tuple struct, or "
  1149. "tuple-like variant"),
  1150. field_number, type->name ());
  1151. }
  1152. return value_primitive_field (lhs, 0, field_number, type);
  1153. }
  1154. else
  1155. error(_("Anonymous field access is only allowed on tuples, \
  1156. tuple structs, and tuple-like enum variants"));
  1157. }
  1158. struct value *
  1159. rust_structop::evaluate (struct type *expect_type,
  1160. struct expression *exp,
  1161. enum noside noside)
  1162. {
  1163. value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
  1164. const char *field_name = std::get<1> (m_storage).c_str ();
  1165. struct value *result;
  1166. struct type *type = value_type (lhs);
  1167. if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
  1168. {
  1169. type = resolve_dynamic_type (type, value_contents (lhs),
  1170. value_address (lhs));
  1171. if (rust_empty_enum_p (type))
  1172. error (_("Cannot access field %s of empty enum %s"),
  1173. field_name, type->name ());
  1174. int fieldno = rust_enum_variant (type);
  1175. lhs = value_primitive_field (lhs, 0, fieldno, type);
  1176. struct type *outer_type = type;
  1177. type = value_type (lhs);
  1178. if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
  1179. error (_("Attempting to access named field %s of tuple "
  1180. "variant %s::%s, which has only anonymous fields"),
  1181. field_name, outer_type->name (),
  1182. rust_last_path_segment (type->name ()));
  1183. try
  1184. {
  1185. result = value_struct_elt (&lhs, {}, field_name,
  1186. NULL, "structure");
  1187. }
  1188. catch (const gdb_exception_error &except)
  1189. {
  1190. error (_("Could not find field %s of struct variant %s::%s"),
  1191. field_name, outer_type->name (),
  1192. rust_last_path_segment (type->name ()));
  1193. }
  1194. }
  1195. else
  1196. result = value_struct_elt (&lhs, {}, field_name, NULL, "structure");
  1197. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  1198. result = value_zero (value_type (result), VALUE_LVAL (result));
  1199. return result;
  1200. }
  1201. value *
  1202. rust_aggregate_operation::evaluate (struct type *expect_type,
  1203. struct expression *exp,
  1204. enum noside noside)
  1205. {
  1206. struct type *type = std::get<0> (m_storage);
  1207. CORE_ADDR addr = 0;
  1208. struct value *addrval = NULL;
  1209. value *result;
  1210. if (noside == EVAL_NORMAL)
  1211. {
  1212. addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
  1213. addr = value_as_long (addrval);
  1214. result = value_at_lazy (type, addr);
  1215. }
  1216. if (std::get<1> (m_storage) != nullptr)
  1217. {
  1218. struct value *init = std::get<1> (m_storage)->evaluate (nullptr, exp,
  1219. noside);
  1220. if (noside == EVAL_NORMAL)
  1221. {
  1222. /* This isn't quite right but will do for the time
  1223. being, seeing that we can't implement the Copy
  1224. trait anyway. */
  1225. value_assign (result, init);
  1226. }
  1227. }
  1228. for (const auto &item : std::get<2> (m_storage))
  1229. {
  1230. value *val = item.second->evaluate (nullptr, exp, noside);
  1231. if (noside == EVAL_NORMAL)
  1232. {
  1233. const char *fieldname = item.first.c_str ();
  1234. value *field = value_struct_elt (&result, {}, fieldname,
  1235. nullptr, "structure");
  1236. value_assign (field, val);
  1237. }
  1238. }
  1239. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  1240. result = allocate_value (type);
  1241. else
  1242. result = value_at_lazy (type, addr);
  1243. return result;
  1244. }
  1245. value *
  1246. rust_structop::evaluate_funcall (struct type *expect_type,
  1247. struct expression *exp,
  1248. enum noside noside,
  1249. const std::vector<operation_up> &ops)
  1250. {
  1251. std::vector<struct value *> args (ops.size () + 1);
  1252. /* Evaluate the argument to STRUCTOP_STRUCT, then find its
  1253. type in order to look up the method. */
  1254. args[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
  1255. /* We don't yet implement real Deref semantics. */
  1256. while (value_type (args[0])->code () == TYPE_CODE_PTR)
  1257. args[0] = value_ind (args[0]);
  1258. struct type *type = value_type (args[0]);
  1259. if ((type->code () != TYPE_CODE_STRUCT
  1260. && type->code () != TYPE_CODE_UNION
  1261. && type->code () != TYPE_CODE_ENUM)
  1262. || rust_tuple_type_p (type))
  1263. error (_("Method calls only supported on struct or enum types"));
  1264. if (type->name () == NULL)
  1265. error (_("Method call on nameless type"));
  1266. std::string name = (std::string (type->name ()) + "::"
  1267. + std::get<1> (m_storage));
  1268. const struct block *block = get_selected_block (0);
  1269. struct block_symbol sym = lookup_symbol (name.c_str (), block,
  1270. VAR_DOMAIN, NULL);
  1271. if (sym.symbol == NULL)
  1272. error (_("Could not find function named '%s'"), name.c_str ());
  1273. struct type *fn_type = sym.symbol->type ();
  1274. if (fn_type->num_fields () == 0)
  1275. error (_("Function '%s' takes no arguments"), name.c_str ());
  1276. if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR)
  1277. args[0] = value_addr (args[0]);
  1278. value *function = address_of_variable (sym.symbol, block);
  1279. for (int i = 0; i < ops.size (); ++i)
  1280. args[i + 1] = ops[i]->evaluate (nullptr, exp, noside);
  1281. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  1282. return value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
  1283. return call_function_by_hand (function, NULL, args);
  1284. }
  1285. }
  1286. /* See language.h. */
  1287. void
  1288. rust_language::language_arch_info (struct gdbarch *gdbarch,
  1289. struct language_arch_info *lai) const
  1290. {
  1291. const struct builtin_type *builtin = builtin_type (gdbarch);
  1292. /* Helper function to allow shorter lines below. */
  1293. auto add = [&] (struct type * t) -> struct type *
  1294. {
  1295. lai->add_primitive_type (t);
  1296. return t;
  1297. };
  1298. struct type *bool_type
  1299. = add (arch_boolean_type (gdbarch, 8, 1, "bool"));
  1300. add (arch_character_type (gdbarch, 32, 1, "char"));
  1301. add (arch_integer_type (gdbarch, 8, 0, "i8"));
  1302. struct type *u8_type
  1303. = add (arch_integer_type (gdbarch, 8, 1, "u8"));
  1304. add (arch_integer_type (gdbarch, 16, 0, "i16"));
  1305. add (arch_integer_type (gdbarch, 16, 1, "u16"));
  1306. add (arch_integer_type (gdbarch, 32, 0, "i32"));
  1307. add (arch_integer_type (gdbarch, 32, 1, "u32"));
  1308. add (arch_integer_type (gdbarch, 64, 0, "i64"));
  1309. add (arch_integer_type (gdbarch, 64, 1, "u64"));
  1310. unsigned int length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
  1311. add (arch_integer_type (gdbarch, length, 0, "isize"));
  1312. struct type *usize_type
  1313. = add (arch_integer_type (gdbarch, length, 1, "usize"));
  1314. add (arch_float_type (gdbarch, 32, "f32", floatformats_ieee_single));
  1315. add (arch_float_type (gdbarch, 64, "f64", floatformats_ieee_double));
  1316. add (arch_integer_type (gdbarch, 0, 1, "()"));
  1317. struct type *tem = make_cv_type (1, 0, u8_type, NULL);
  1318. add (rust_slice_type ("&str", tem, usize_type));
  1319. lai->set_bool_type (bool_type);
  1320. lai->set_string_char_type (u8_type);
  1321. }
  1322. /* See language.h. */
  1323. void
  1324. rust_language::print_type (struct type *type, const char *varstring,
  1325. struct ui_file *stream, int show, int level,
  1326. const struct type_print_options *flags) const
  1327. {
  1328. print_offset_data podata (flags);
  1329. rust_internal_print_type (type, varstring, stream, show, level,
  1330. flags, false, &podata);
  1331. }
  1332. /* See language.h. */
  1333. void
  1334. rust_language::emitchar (int ch, struct type *chtype,
  1335. struct ui_file *stream, int quoter) const
  1336. {
  1337. if (!rust_chartype_p (chtype))
  1338. generic_emit_char (ch, chtype, stream, quoter,
  1339. target_charset (chtype->arch ()));
  1340. else if (ch == '\\' || ch == quoter)
  1341. gdb_printf (stream, "\\%c", ch);
  1342. else if (ch == '\n')
  1343. gdb_puts ("\\n", stream);
  1344. else if (ch == '\r')
  1345. gdb_puts ("\\r", stream);
  1346. else if (ch == '\t')
  1347. gdb_puts ("\\t", stream);
  1348. else if (ch == '\0')
  1349. gdb_puts ("\\0", stream);
  1350. else if (ch >= 32 && ch <= 127 && isprint (ch))
  1351. gdb_putc (ch, stream);
  1352. else if (ch <= 255)
  1353. gdb_printf (stream, "\\x%02x", ch);
  1354. else
  1355. gdb_printf (stream, "\\u{%06x}", ch);
  1356. }
  1357. /* See language.h. */
  1358. bool
  1359. rust_language::is_string_type_p (struct type *type) const
  1360. {
  1361. LONGEST low_bound, high_bound;
  1362. type = check_typedef (type);
  1363. return ((type->code () == TYPE_CODE_STRING)
  1364. || (type->code () == TYPE_CODE_PTR
  1365. && (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
  1366. && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
  1367. && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
  1368. &high_bound)))
  1369. || (type->code () == TYPE_CODE_STRUCT
  1370. && !rust_enum_p (type)
  1371. && rust_slice_type_p (type)
  1372. && strcmp (type->name (), "&str") == 0));
  1373. }
  1374. /* Single instance of the Rust language class. */
  1375. static rust_language rust_language_defn;