c-varobj.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. /* varobj support for C and C++.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include "defs.h"
  14. #include "value.h"
  15. #include "varobj.h"
  16. #include "gdbthread.h"
  17. #include "valprint.h"
  18. static void cplus_class_num_children (struct type *type, int children[3]);
  19. /* The names of varobjs representing anonymous structs or unions. */
  20. #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
  21. #define ANONYMOUS_UNION_NAME _("<anonymous union>")
  22. /* Does CHILD represent a child with no name? This happens when
  23. the child is an anonymous struct or union and it has no field name
  24. in its parent variable.
  25. This has already been determined by *_describe_child. The easiest
  26. thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
  27. bool
  28. varobj_is_anonymous_child (const struct varobj *child)
  29. {
  30. return (child->name == ANONYMOUS_STRUCT_NAME
  31. || child->name == ANONYMOUS_UNION_NAME);
  32. }
  33. /* Given the value and the type of a variable object,
  34. adjust the value and type to those necessary
  35. for getting children of the variable object.
  36. This includes dereferencing top-level references
  37. to all types and dereferencing pointers to
  38. structures.
  39. If LOOKUP_ACTUAL_TYPE is set the enclosing type of the
  40. value will be fetched and if it differs from static type
  41. the value will be casted to it.
  42. Both TYPE and *TYPE should be non-null. VALUE
  43. can be null if we want to only translate type.
  44. *VALUE can be null as well -- if the parent
  45. value is not known.
  46. If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
  47. depending on whether pointer was dereferenced
  48. in this function. */
  49. static void
  50. adjust_value_for_child_access (struct value **value,
  51. struct type **type,
  52. int *was_ptr,
  53. int lookup_actual_type)
  54. {
  55. gdb_assert (type && *type);
  56. if (was_ptr)
  57. *was_ptr = 0;
  58. *type = check_typedef (*type);
  59. /* The type of value stored in varobj, that is passed
  60. to us, is already supposed to be
  61. reference-stripped. */
  62. gdb_assert (!TYPE_IS_REFERENCE (*type));
  63. /* Pointers to structures are treated just like
  64. structures when accessing children. Don't
  65. dereference pointers to other types. */
  66. if ((*type)->code () == TYPE_CODE_PTR)
  67. {
  68. struct type *target_type = get_target_type (*type);
  69. if (target_type->code () == TYPE_CODE_STRUCT
  70. || target_type->code () == TYPE_CODE_UNION)
  71. {
  72. if (value && *value)
  73. {
  74. try
  75. {
  76. *value = value_ind (*value);
  77. }
  78. catch (const gdb_exception_error &except)
  79. {
  80. *value = NULL;
  81. }
  82. }
  83. *type = target_type;
  84. if (was_ptr)
  85. *was_ptr = 1;
  86. }
  87. }
  88. /* The 'get_target_type' function calls check_typedef on
  89. result, so we can immediately check type code. No
  90. need to call check_typedef here. */
  91. /* Access a real type of the value (if necessary and possible). */
  92. if (value && *value && lookup_actual_type)
  93. {
  94. struct type *enclosing_type;
  95. int real_type_found = 0;
  96. enclosing_type = value_actual_type (*value, 1, &real_type_found);
  97. if (real_type_found)
  98. {
  99. *type = enclosing_type;
  100. *value = value_cast (enclosing_type, *value);
  101. }
  102. }
  103. }
  104. /* Is VAR a path expression parent, i.e., can it be used to construct
  105. a valid path expression? */
  106. static bool
  107. c_is_path_expr_parent (const struct varobj *var)
  108. {
  109. struct type *type;
  110. /* "Fake" children are not path_expr parents. */
  111. if (CPLUS_FAKE_CHILD (var))
  112. return false;
  113. type = varobj_get_gdb_type (var);
  114. /* Anonymous unions and structs are also not path_expr parents. */
  115. if ((type->code () == TYPE_CODE_STRUCT
  116. || type->code () == TYPE_CODE_UNION)
  117. && type->name () == NULL)
  118. {
  119. const struct varobj *parent = var->parent;
  120. while (parent != NULL && CPLUS_FAKE_CHILD (parent))
  121. parent = parent->parent;
  122. if (parent != NULL)
  123. {
  124. struct type *parent_type;
  125. int was_ptr;
  126. parent_type = varobj_get_value_type (parent);
  127. adjust_value_for_child_access (NULL, &parent_type, &was_ptr, 0);
  128. if (parent_type->code () == TYPE_CODE_STRUCT
  129. || parent_type->code () == TYPE_CODE_UNION)
  130. {
  131. const char *field_name;
  132. gdb_assert (var->index < parent_type->num_fields ());
  133. field_name = parent_type->field (var->index).name ();
  134. return !(field_name == NULL || *field_name == '\0');
  135. }
  136. }
  137. return false;
  138. }
  139. return true;
  140. }
  141. /* C */
  142. static int
  143. c_number_of_children (const struct varobj *var)
  144. {
  145. struct type *type = varobj_get_value_type (var);
  146. int children = 0;
  147. struct type *target;
  148. adjust_value_for_child_access (NULL, &type, NULL, 0);
  149. target = get_target_type (type);
  150. switch (type->code ())
  151. {
  152. case TYPE_CODE_ARRAY:
  153. if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
  154. && (type->bounds ()->high.kind () != PROP_UNDEFINED))
  155. children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
  156. else
  157. /* If we don't know how many elements there are, don't display
  158. any. */
  159. children = 0;
  160. break;
  161. case TYPE_CODE_STRUCT:
  162. case TYPE_CODE_UNION:
  163. children = type->num_fields ();
  164. break;
  165. case TYPE_CODE_PTR:
  166. /* The type here is a pointer to non-struct. Typically, pointers
  167. have one child, except for function ptrs, which have no children,
  168. and except for void*, as we don't know what to show.
  169. We can show char* so we allow it to be dereferenced. If you decide
  170. to test for it, please mind that a little magic is necessary to
  171. properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
  172. TYPE_NAME == "char". */
  173. if (target->code () == TYPE_CODE_FUNC
  174. || target->code () == TYPE_CODE_VOID)
  175. children = 0;
  176. else
  177. children = 1;
  178. break;
  179. default:
  180. /* Other types have no children. */
  181. break;
  182. }
  183. return children;
  184. }
  185. static std::string
  186. c_name_of_variable (const struct varobj *parent)
  187. {
  188. return parent->name;
  189. }
  190. /* Return the value of element TYPE_INDEX of a structure
  191. value VALUE. VALUE's type should be a structure,
  192. or union, or a typedef to struct/union.
  193. Returns NULL if getting the value fails. Never throws. */
  194. static struct value *
  195. value_struct_element_index (struct value *value, int type_index)
  196. {
  197. struct value *result = NULL;
  198. struct type *type = value_type (value);
  199. type = check_typedef (type);
  200. gdb_assert (type->code () == TYPE_CODE_STRUCT
  201. || type->code () == TYPE_CODE_UNION);
  202. try
  203. {
  204. if (field_is_static (&type->field (type_index)))
  205. result = value_static_field (type, type_index);
  206. else
  207. result = value_primitive_field (value, 0, type_index, type);
  208. }
  209. catch (const gdb_exception_error &e)
  210. {
  211. return NULL;
  212. }
  213. return result;
  214. }
  215. /* Obtain the information about child INDEX of the variable
  216. object PARENT.
  217. If CNAME is not null, sets *CNAME to the name of the child relative
  218. to the parent.
  219. If CVALUE is not null, sets *CVALUE to the value of the child.
  220. If CTYPE is not null, sets *CTYPE to the type of the child.
  221. If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
  222. information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
  223. to empty. */
  224. static void
  225. c_describe_child (const struct varobj *parent, int index,
  226. std::string *cname, struct value **cvalue,
  227. struct type **ctype, std::string *cfull_expression)
  228. {
  229. struct value *value = parent->value.get ();
  230. struct type *type = varobj_get_value_type (parent);
  231. std::string parent_expression;
  232. int was_ptr;
  233. if (cname)
  234. *cname = std::string ();
  235. if (cvalue)
  236. *cvalue = NULL;
  237. if (ctype)
  238. *ctype = NULL;
  239. if (cfull_expression)
  240. {
  241. *cfull_expression = std::string ();
  242. parent_expression
  243. = varobj_get_path_expr (varobj_get_path_expr_parent (parent));
  244. }
  245. adjust_value_for_child_access (&value, &type, &was_ptr, 0);
  246. switch (type->code ())
  247. {
  248. case TYPE_CODE_ARRAY:
  249. if (cname)
  250. *cname = int_string (index + type->bounds ()->low.const_val (),
  251. 10, 1, 0, 0);
  252. if (cvalue && value)
  253. {
  254. int real_index
  255. = index + type->bounds ()->low.const_val ();
  256. try
  257. {
  258. *cvalue = value_subscript (value, real_index);
  259. }
  260. catch (const gdb_exception_error &except)
  261. {
  262. }
  263. }
  264. if (ctype)
  265. *ctype = get_target_type (type);
  266. if (cfull_expression)
  267. *cfull_expression = string_printf
  268. ("(%s)[%s]", parent_expression.c_str (),
  269. int_string (index + type->bounds ()->low.const_val (),
  270. 10, 1, 0, 0));
  271. break;
  272. case TYPE_CODE_STRUCT:
  273. case TYPE_CODE_UNION:
  274. {
  275. const char *field_name;
  276. /* If the type is anonymous and the field has no name,
  277. set an appropriate name. */
  278. field_name = type->field (index).name ();
  279. if (field_name == NULL || *field_name == '\0')
  280. {
  281. if (cname)
  282. {
  283. if (type->field (index).type ()->code ()
  284. == TYPE_CODE_STRUCT)
  285. *cname = ANONYMOUS_STRUCT_NAME;
  286. else
  287. *cname = ANONYMOUS_UNION_NAME;
  288. }
  289. if (cfull_expression)
  290. *cfull_expression = "";
  291. }
  292. else
  293. {
  294. if (cname)
  295. *cname = field_name;
  296. if (cfull_expression)
  297. {
  298. const char *join = was_ptr ? "->" : ".";
  299. *cfull_expression = string_printf ("(%s)%s%s",
  300. parent_expression.c_str (),
  301. join, field_name);
  302. }
  303. }
  304. if (cvalue && value)
  305. {
  306. /* For C, varobj index is the same as type index. */
  307. *cvalue = value_struct_element_index (value, index);
  308. }
  309. if (ctype)
  310. *ctype = type->field (index).type ();
  311. }
  312. break;
  313. case TYPE_CODE_PTR:
  314. if (cname)
  315. *cname = string_printf ("*%s", parent->name.c_str ());
  316. if (cvalue && value)
  317. {
  318. try
  319. {
  320. *cvalue = value_ind (value);
  321. }
  322. catch (const gdb_exception_error &except)
  323. {
  324. *cvalue = NULL;
  325. }
  326. }
  327. /* Don't use get_target_type because it calls
  328. check_typedef and here, we want to show the true
  329. declared type of the variable. */
  330. if (ctype)
  331. *ctype = TYPE_TARGET_TYPE (type);
  332. if (cfull_expression)
  333. *cfull_expression = string_printf ("*(%s)", parent_expression.c_str ());
  334. break;
  335. default:
  336. /* This should not happen. */
  337. if (cname)
  338. *cname = "???";
  339. if (cfull_expression)
  340. *cfull_expression = "???";
  341. /* Don't set value and type, we don't know then. */
  342. }
  343. }
  344. static std::string
  345. c_name_of_child (const struct varobj *parent, int index)
  346. {
  347. std::string name;
  348. c_describe_child (parent, index, &name, NULL, NULL, NULL);
  349. return name;
  350. }
  351. static std::string
  352. c_path_expr_of_child (const struct varobj *child)
  353. {
  354. std::string path_expr;
  355. c_describe_child (child->parent, child->index, NULL, NULL, NULL,
  356. &path_expr);
  357. return path_expr;
  358. }
  359. static struct value *
  360. c_value_of_child (const struct varobj *parent, int index)
  361. {
  362. struct value *value = NULL;
  363. c_describe_child (parent, index, NULL, &value, NULL, NULL);
  364. return value;
  365. }
  366. static struct type *
  367. c_type_of_child (const struct varobj *parent, int index)
  368. {
  369. struct type *type = NULL;
  370. c_describe_child (parent, index, NULL, NULL, &type, NULL);
  371. return type;
  372. }
  373. /* This returns the type of the variable. It also skips past typedefs
  374. to return the real type of the variable. */
  375. static struct type *
  376. get_type (const struct varobj *var)
  377. {
  378. struct type *type;
  379. type = var->type;
  380. if (type != NULL)
  381. type = check_typedef (type);
  382. return type;
  383. }
  384. static std::string
  385. c_value_of_variable (const struct varobj *var,
  386. enum varobj_display_formats format)
  387. {
  388. /* BOGUS: if val_print sees a struct/class, or a reference to one,
  389. it will print out its children instead of "{...}". So we need to
  390. catch that case explicitly. */
  391. struct type *type = get_type (var);
  392. /* Strip top-level references. */
  393. while (TYPE_IS_REFERENCE (type))
  394. type = check_typedef (TYPE_TARGET_TYPE (type));
  395. switch (type->code ())
  396. {
  397. case TYPE_CODE_STRUCT:
  398. case TYPE_CODE_UNION:
  399. return "{...}";
  400. /* break; */
  401. case TYPE_CODE_ARRAY:
  402. return string_printf ("[%d]", var->num_children);
  403. /* break; */
  404. default:
  405. {
  406. if (var->value == NULL)
  407. {
  408. /* This can happen if we attempt to get the value of a struct
  409. member when the parent is an invalid pointer. This is an
  410. error condition, so we should tell the caller. */
  411. return std::string ();
  412. }
  413. else
  414. {
  415. if (var->not_fetched && value_lazy (var->value.get ()))
  416. /* Frozen variable and no value yet. We don't
  417. implicitly fetch the value. MI response will
  418. use empty string for the value, which is OK. */
  419. return std::string ();
  420. gdb_assert (varobj_value_is_changeable_p (var));
  421. gdb_assert (!value_lazy (var->value.get ()));
  422. /* If the specified format is the current one,
  423. we can reuse print_value. */
  424. if (format == var->format)
  425. return var->print_value;
  426. else
  427. return varobj_value_get_print_value (var->value.get (), format,
  428. var);
  429. }
  430. }
  431. }
  432. }
  433. /* varobj operations for c. */
  434. const struct lang_varobj_ops c_varobj_ops =
  435. {
  436. c_number_of_children,
  437. c_name_of_variable,
  438. c_name_of_child,
  439. c_path_expr_of_child,
  440. c_value_of_child,
  441. c_type_of_child,
  442. c_value_of_variable,
  443. varobj_default_value_is_changeable_p,
  444. NULL, /* value_has_mutated */
  445. c_is_path_expr_parent /* is_path_expr_parent */
  446. };
  447. /* A little convenience enum for dealing with C++. */
  448. enum vsections
  449. {
  450. v_public = 0, v_private, v_protected
  451. };
  452. /* C++ */
  453. static int
  454. cplus_number_of_children (const struct varobj *var)
  455. {
  456. struct value *value = NULL;
  457. struct type *type;
  458. int children, dont_know;
  459. int lookup_actual_type = 0;
  460. struct value_print_options opts;
  461. dont_know = 1;
  462. children = 0;
  463. get_user_print_options (&opts);
  464. if (!CPLUS_FAKE_CHILD (var))
  465. {
  466. type = varobj_get_value_type (var);
  467. /* It is necessary to access a real type (via RTTI). */
  468. if (opts.objectprint)
  469. {
  470. value = var->value.get ();
  471. lookup_actual_type = var->type->is_pointer_or_reference ();
  472. }
  473. adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
  474. if (((type->code ()) == TYPE_CODE_STRUCT)
  475. || ((type->code ()) == TYPE_CODE_UNION))
  476. {
  477. int kids[3];
  478. cplus_class_num_children (type, kids);
  479. if (kids[v_public] != 0)
  480. children++;
  481. if (kids[v_private] != 0)
  482. children++;
  483. if (kids[v_protected] != 0)
  484. children++;
  485. /* Add any baseclasses. */
  486. children += TYPE_N_BASECLASSES (type);
  487. dont_know = 0;
  488. /* FIXME: save children in var. */
  489. }
  490. }
  491. else
  492. {
  493. int kids[3];
  494. type = varobj_get_value_type (var->parent);
  495. /* It is necessary to access a real type (via RTTI). */
  496. if (opts.objectprint)
  497. {
  498. const struct varobj *parent = var->parent;
  499. value = parent->value.get ();
  500. lookup_actual_type = parent->type->is_pointer_or_reference ();
  501. }
  502. adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
  503. cplus_class_num_children (type, kids);
  504. if (var->name == "public")
  505. children = kids[v_public];
  506. else if (var->name == "private")
  507. children = kids[v_private];
  508. else
  509. children = kids[v_protected];
  510. dont_know = 0;
  511. }
  512. if (dont_know)
  513. children = c_number_of_children (var);
  514. return children;
  515. }
  516. /* Compute # of public, private, and protected variables in this class.
  517. That means we need to descend into all baseclasses and find out
  518. how many are there, too. */
  519. static void
  520. cplus_class_num_children (struct type *type, int children[3])
  521. {
  522. int i, vptr_fieldno;
  523. struct type *basetype = NULL;
  524. children[v_public] = 0;
  525. children[v_private] = 0;
  526. children[v_protected] = 0;
  527. vptr_fieldno = get_vptr_fieldno (type, &basetype);
  528. for (i = TYPE_N_BASECLASSES (type); i < type->num_fields (); i++)
  529. {
  530. /* If we have a virtual table pointer, omit it. Even if virtual
  531. table pointers are not specifically marked in the debug info,
  532. they should be artificial. */
  533. if ((type == basetype && i == vptr_fieldno)
  534. || TYPE_FIELD_ARTIFICIAL (type, i))
  535. continue;
  536. if (TYPE_FIELD_PROTECTED (type, i))
  537. children[v_protected]++;
  538. else if (TYPE_FIELD_PRIVATE (type, i))
  539. children[v_private]++;
  540. else
  541. children[v_public]++;
  542. }
  543. }
  544. static std::string
  545. cplus_name_of_variable (const struct varobj *parent)
  546. {
  547. return c_name_of_variable (parent);
  548. }
  549. enum accessibility { private_field, protected_field, public_field };
  550. /* Check if field INDEX of TYPE has the specified accessibility.
  551. Return 0 if so and 1 otherwise. */
  552. static int
  553. match_accessibility (struct type *type, int index, enum accessibility acc)
  554. {
  555. if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
  556. return 1;
  557. else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
  558. return 1;
  559. else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
  560. && !TYPE_FIELD_PROTECTED (type, index))
  561. return 1;
  562. else
  563. return 0;
  564. }
  565. static void
  566. cplus_describe_child (const struct varobj *parent, int index,
  567. std::string *cname, struct value **cvalue, struct type **ctype,
  568. std::string *cfull_expression)
  569. {
  570. struct value *value;
  571. struct type *type;
  572. int was_ptr;
  573. int lookup_actual_type = 0;
  574. const char *parent_expression = NULL;
  575. const struct varobj *var;
  576. struct value_print_options opts;
  577. if (cname)
  578. *cname = std::string ();
  579. if (cvalue)
  580. *cvalue = NULL;
  581. if (ctype)
  582. *ctype = NULL;
  583. if (cfull_expression)
  584. *cfull_expression = std::string ();
  585. get_user_print_options (&opts);
  586. var = (CPLUS_FAKE_CHILD (parent)) ? parent->parent : parent;
  587. if (opts.objectprint)
  588. lookup_actual_type = var->type->is_pointer_or_reference ();
  589. value = var->value.get ();
  590. type = varobj_get_value_type (var);
  591. if (cfull_expression)
  592. parent_expression
  593. = varobj_get_path_expr (varobj_get_path_expr_parent (var));
  594. adjust_value_for_child_access (&value, &type, &was_ptr, lookup_actual_type);
  595. if (type->code () == TYPE_CODE_STRUCT
  596. || type->code () == TYPE_CODE_UNION)
  597. {
  598. const char *join = was_ptr ? "->" : ".";
  599. if (CPLUS_FAKE_CHILD (parent))
  600. {
  601. /* The fields of the class type are ordered as they
  602. appear in the class. We are given an index for a
  603. particular access control type ("public","protected",
  604. or "private"). We must skip over fields that don't
  605. have the access control we are looking for to properly
  606. find the indexed field. */
  607. int type_index = TYPE_N_BASECLASSES (type);
  608. enum accessibility acc = public_field;
  609. int vptr_fieldno;
  610. struct type *basetype = NULL;
  611. const char *field_name;
  612. vptr_fieldno = get_vptr_fieldno (type, &basetype);
  613. if (parent->name == "private")
  614. acc = private_field;
  615. else if (parent->name == "protected")
  616. acc = protected_field;
  617. while (index >= 0)
  618. {
  619. if ((type == basetype && type_index == vptr_fieldno)
  620. || TYPE_FIELD_ARTIFICIAL (type, type_index))
  621. ; /* ignore vptr */
  622. else if (match_accessibility (type, type_index, acc))
  623. --index;
  624. ++type_index;
  625. }
  626. --type_index;
  627. /* If the type is anonymous and the field has no name,
  628. set an appropriate name. */
  629. field_name = type->field (type_index).name ();
  630. if (field_name == NULL || *field_name == '\0')
  631. {
  632. if (cname)
  633. {
  634. if (type->field (type_index).type ()->code ()
  635. == TYPE_CODE_STRUCT)
  636. *cname = ANONYMOUS_STRUCT_NAME;
  637. else if (type->field (type_index).type ()->code ()
  638. == TYPE_CODE_UNION)
  639. *cname = ANONYMOUS_UNION_NAME;
  640. }
  641. if (cfull_expression)
  642. *cfull_expression = std::string ();
  643. }
  644. else
  645. {
  646. if (cname)
  647. *cname = type->field (type_index).name ();
  648. if (cfull_expression)
  649. *cfull_expression
  650. = string_printf ("((%s)%s%s)", parent_expression, join,
  651. field_name);
  652. }
  653. if (cvalue && value)
  654. *cvalue = value_struct_element_index (value, type_index);
  655. if (ctype)
  656. *ctype = type->field (type_index).type ();
  657. }
  658. else if (index < TYPE_N_BASECLASSES (type))
  659. {
  660. /* This is a baseclass. */
  661. if (cname)
  662. *cname = type->field (index).name ();
  663. if (cvalue && value)
  664. *cvalue = value_cast (type->field (index).type (), value);
  665. if (ctype)
  666. {
  667. *ctype = type->field (index).type ();
  668. }
  669. if (cfull_expression)
  670. {
  671. const char *ptr = was_ptr ? "*" : "";
  672. /* Cast the parent to the base' type. Note that in gdb,
  673. expression like
  674. (Base1)d
  675. will create an lvalue, for all appearences, so we don't
  676. need to use more fancy:
  677. *(Base1*)(&d)
  678. construct.
  679. When we are in the scope of the base class or of one
  680. of its children, the type field name will be interpreted
  681. as a constructor, if it exists. Therefore, we must
  682. indicate that the name is a class name by using the
  683. 'class' keyword. See PR mi/11912 */
  684. *cfull_expression = string_printf ("(%s(class %s%s) %s)",
  685. ptr,
  686. type->field (index).name (),
  687. ptr,
  688. parent_expression);
  689. }
  690. }
  691. else
  692. {
  693. const char *access = NULL;
  694. int children[3];
  695. cplus_class_num_children (type, children);
  696. /* Everything beyond the baseclasses can
  697. only be "public", "private", or "protected"
  698. The special "fake" children are always output by varobj in
  699. this order. So if INDEX == 2, it MUST be "protected". */
  700. index -= TYPE_N_BASECLASSES (type);
  701. switch (index)
  702. {
  703. case 0:
  704. if (children[v_public] > 0)
  705. access = "public";
  706. else if (children[v_private] > 0)
  707. access = "private";
  708. else
  709. access = "protected";
  710. break;
  711. case 1:
  712. if (children[v_public] > 0)
  713. {
  714. if (children[v_private] > 0)
  715. access = "private";
  716. else
  717. access = "protected";
  718. }
  719. else if (children[v_private] > 0)
  720. access = "protected";
  721. break;
  722. case 2:
  723. /* Must be protected. */
  724. access = "protected";
  725. break;
  726. default:
  727. /* error! */
  728. break;
  729. }
  730. gdb_assert (access);
  731. if (cname)
  732. *cname = access;
  733. /* Value and type and full expression are null here. */
  734. }
  735. }
  736. else
  737. {
  738. c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
  739. }
  740. }
  741. static std::string
  742. cplus_name_of_child (const struct varobj *parent, int index)
  743. {
  744. std::string name;
  745. cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
  746. return name;
  747. }
  748. static std::string
  749. cplus_path_expr_of_child (const struct varobj *child)
  750. {
  751. std::string path_expr;
  752. cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
  753. &path_expr);
  754. return path_expr;
  755. }
  756. static struct value *
  757. cplus_value_of_child (const struct varobj *parent, int index)
  758. {
  759. struct value *value = NULL;
  760. cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
  761. return value;
  762. }
  763. static struct type *
  764. cplus_type_of_child (const struct varobj *parent, int index)
  765. {
  766. struct type *type = NULL;
  767. cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
  768. return type;
  769. }
  770. static std::string
  771. cplus_value_of_variable (const struct varobj *var,
  772. enum varobj_display_formats format)
  773. {
  774. /* If we have one of our special types, don't print out
  775. any value. */
  776. if (CPLUS_FAKE_CHILD (var))
  777. return std::string ();
  778. return c_value_of_variable (var, format);
  779. }
  780. /* varobj operations for c++. */
  781. const struct lang_varobj_ops cplus_varobj_ops =
  782. {
  783. cplus_number_of_children,
  784. cplus_name_of_variable,
  785. cplus_name_of_child,
  786. cplus_path_expr_of_child,
  787. cplus_value_of_child,
  788. cplus_type_of_child,
  789. cplus_value_of_variable,
  790. varobj_default_value_is_changeable_p,
  791. NULL, /* value_has_mutated */
  792. c_is_path_expr_parent /* is_path_expr_parent */
  793. };