f-array-walker.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* Copyright (C) 2020-2022 Free Software Foundation, Inc.
  2. This file is part of GDB.
  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. /* Support classes to wrap up the process of iterating over a
  14. multi-dimensional Fortran array. */
  15. #ifndef F_ARRAY_WALKER_H
  16. #define F_ARRAY_WALKER_H
  17. #include "defs.h"
  18. #include "gdbtypes.h"
  19. #include "f-lang.h"
  20. /* Class for calculating the byte offset for elements within a single
  21. dimension of a Fortran array. */
  22. class fortran_array_offset_calculator
  23. {
  24. public:
  25. /* Create a new offset calculator for TYPE, which is either an array or a
  26. string. */
  27. explicit fortran_array_offset_calculator (struct type *type)
  28. {
  29. /* Validate the type. */
  30. type = check_typedef (type);
  31. if (type->code () != TYPE_CODE_ARRAY
  32. && (type->code () != TYPE_CODE_STRING))
  33. error (_("can only compute offsets for arrays and strings"));
  34. /* Get the range, and extract the bounds. */
  35. struct type *range_type = type->index_type ();
  36. if (!get_discrete_bounds (range_type, &m_lowerbound, &m_upperbound))
  37. error ("unable to read array bounds");
  38. /* Figure out the stride for this array. */
  39. struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (type));
  40. m_stride = type->index_type ()->bounds ()->bit_stride ();
  41. if (m_stride == 0)
  42. m_stride = type_length_units (elt_type);
  43. else
  44. {
  45. int unit_size
  46. = gdbarch_addressable_memory_unit_size (elt_type->arch ());
  47. m_stride /= (unit_size * 8);
  48. }
  49. };
  50. /* Get the byte offset for element INDEX within the type we are working
  51. on. There is no bounds checking done on INDEX. If the stride is
  52. negative then we still assume that the base address (for the array
  53. object) points to the element with the lowest memory address, we then
  54. calculate an offset assuming that index 0 will be the element at the
  55. highest address, index 1 the next highest, and so on. This is not
  56. quite how Fortran works in reality; in reality the base address of
  57. the object would point at the element with the highest address, and
  58. we would index backwards from there in the "normal" way, however,
  59. GDB's current value contents model doesn't support having the base
  60. address be near to the end of the value contents, so we currently
  61. adjust the base address of Fortran arrays with negative strides so
  62. their base address points at the lowest memory address. This code
  63. here is part of working around this weirdness. */
  64. LONGEST index_offset (LONGEST index)
  65. {
  66. LONGEST offset;
  67. if (m_stride < 0)
  68. offset = std::abs (m_stride) * (m_upperbound - index);
  69. else
  70. offset = std::abs (m_stride) * (index - m_lowerbound);
  71. return offset;
  72. }
  73. private:
  74. /* The stride for the type we are working with. */
  75. LONGEST m_stride;
  76. /* The upper bound for the type we are working with. */
  77. LONGEST m_upperbound;
  78. /* The lower bound for the type we are working with. */
  79. LONGEST m_lowerbound;
  80. };
  81. /* A base class used by fortran_array_walker. There's no virtual methods
  82. here, sub-classes should just override the functions they want in order
  83. to specialise the behaviour to their needs. The functionality
  84. provided in these default implementations will visit every array
  85. element, but do nothing for each element. */
  86. struct fortran_array_walker_base_impl
  87. {
  88. /* Called when iterating between the lower and upper bounds of each
  89. dimension of the array. Return true if GDB should continue iterating,
  90. otherwise, return false.
  91. SHOULD_CONTINUE indicates if GDB is going to stop anyway, and should
  92. be taken into consideration when deciding what to return. If
  93. SHOULD_CONTINUE is false then this function must also return false,
  94. the function is still called though in case extra work needs to be
  95. done as part of the stopping process. */
  96. bool continue_walking (bool should_continue)
  97. { return should_continue; }
  98. /* Called when GDB starts iterating over a dimension of the array. The
  99. argument INDEX_TYPE is the type of the index used to address elements
  100. in the dimension, NELTS holds the number of the elements there, and
  101. INNER_P is true for the inner most dimension (the dimension containing
  102. the actual elements of the array), and false for more outer dimensions.
  103. For a concrete example of how this function is called see the comment
  104. on process_element below. */
  105. void start_dimension (struct type *index_type, LONGEST nelts, bool inner_p)
  106. { /* Nothing. */ }
  107. /* Called when GDB finishes iterating over a dimension of the array. The
  108. argument INNER_P is true for the inner most dimension (the dimension
  109. containing the actual elements of the array), and false for more outer
  110. dimensions. LAST_P is true for the last call at a particular
  111. dimension. For a concrete example of how this function is called
  112. see the comment on process_element below. */
  113. void finish_dimension (bool inner_p, bool last_p)
  114. { /* Nothing. */ }
  115. /* Called when processing dimensions of the array other than the
  116. innermost one. WALK_1 is the walker to normally call, ELT_TYPE is
  117. the type of the element being extracted, and ELT_OFF is the offset
  118. of the element from the start of array being walked. INDEX is the
  119. value of the index the current element is at in the upper dimension.
  120. Finally LAST_P is true only when this is the last element that will
  121. be processed in this dimension. */
  122. void process_dimension (gdb::function_view<void (struct type *,
  123. int, bool)> walk_1,
  124. struct type *elt_type, LONGEST elt_off,
  125. LONGEST index, bool last_p)
  126. {
  127. walk_1 (elt_type, elt_off, last_p);
  128. }
  129. /* Called when processing the inner most dimension of the array, for
  130. every element in the array. ELT_TYPE is the type of the element being
  131. extracted, and ELT_OFF is the offset of the element from the start of
  132. array being walked. INDEX is the value of the index the current
  133. element is at in the upper dimension. Finally LAST_P is true only
  134. when this is the last element that will be processed in this dimension.
  135. Given this two dimensional array ((1, 2) (3, 4) (5, 6)), the calls to
  136. start_dimension, process_element, and finish_dimension look like this:
  137. start_dimension (INDEX_TYPE, 3, false);
  138. start_dimension (INDEX_TYPE, 2, true);
  139. process_element (TYPE, OFFSET, false);
  140. process_element (TYPE, OFFSET, true);
  141. finish_dimension (true, false);
  142. start_dimension (INDEX_TYPE, 2, true);
  143. process_element (TYPE, OFFSET, false);
  144. process_element (TYPE, OFFSET, true);
  145. finish_dimension (true, true);
  146. start_dimension (INDEX_TYPE, 2, true);
  147. process_element (TYPE, OFFSET, false);
  148. process_element (TYPE, OFFSET, true);
  149. finish_dimension (true, true);
  150. finish_dimension (false, true); */
  151. void process_element (struct type *elt_type, LONGEST elt_off,
  152. LONGEST index, bool last_p)
  153. { /* Nothing. */ }
  154. };
  155. /* A class to wrap up the process of iterating over a multi-dimensional
  156. Fortran array. IMPL is used to specialise what happens as we walk over
  157. the array. See class FORTRAN_ARRAY_WALKER_BASE_IMPL (above) for the
  158. methods than can be used to customise the array walk. */
  159. template<typename Impl>
  160. class fortran_array_walker
  161. {
  162. /* Ensure that Impl is derived from the required base class. This just
  163. ensures that all of the required API methods are available and have a
  164. sensible default implementation. */
  165. gdb_static_assert ((std::is_base_of<fortran_array_walker_base_impl,Impl>::value));
  166. public:
  167. /* Create a new array walker. TYPE is the type of the array being walked
  168. over, and ADDRESS is the base address for the object of TYPE in
  169. memory. All other arguments are forwarded to the constructor of the
  170. template parameter class IMPL. */
  171. template <typename ...Args>
  172. fortran_array_walker (struct type *type, CORE_ADDR address,
  173. Args... args)
  174. : m_type (type),
  175. m_address (address),
  176. m_impl (type, address, args...),
  177. m_ndimensions (calc_f77_array_dims (m_type)),
  178. m_nss (0)
  179. { /* Nothing. */ }
  180. /* Walk the array. */
  181. void
  182. walk ()
  183. {
  184. walk_1 (m_type, 0, false);
  185. }
  186. private:
  187. /* The core of the array walking algorithm. TYPE is the type of
  188. the current dimension being processed and OFFSET is the offset
  189. (in bytes) for the start of this dimension. */
  190. void
  191. walk_1 (struct type *type, int offset, bool last_p)
  192. {
  193. /* Extract the range, and get lower and upper bounds. */
  194. struct type *range_type = check_typedef (type)->index_type ();
  195. LONGEST lowerbound, upperbound;
  196. if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
  197. error ("failed to get range bounds");
  198. /* CALC is used to calculate the offsets for each element in this
  199. dimension. */
  200. fortran_array_offset_calculator calc (type);
  201. m_nss++;
  202. gdb_assert (range_type->code () == TYPE_CODE_RANGE);
  203. m_impl.start_dimension (TYPE_TARGET_TYPE (range_type),
  204. upperbound - lowerbound + 1,
  205. m_nss == m_ndimensions);
  206. if (m_nss != m_ndimensions)
  207. {
  208. struct type *subarray_type = TYPE_TARGET_TYPE (check_typedef (type));
  209. /* For dimensions other than the inner most, walk each element and
  210. recurse while peeling off one more dimension of the array. */
  211. for (LONGEST i = lowerbound;
  212. m_impl.continue_walking (i < upperbound + 1);
  213. i++)
  214. {
  215. /* Use the index and the stride to work out a new offset. */
  216. LONGEST new_offset = offset + calc.index_offset (i);
  217. /* Now print the lower dimension. */
  218. m_impl.process_dimension
  219. ([this] (struct type *w_type, int w_offset, bool w_last_p) -> void
  220. {
  221. this->walk_1 (w_type, w_offset, w_last_p);
  222. },
  223. subarray_type, new_offset, i, i == upperbound);
  224. }
  225. }
  226. else
  227. {
  228. struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (type));
  229. /* For the inner most dimension of the array, process each element
  230. within this dimension. */
  231. for (LONGEST i = lowerbound;
  232. m_impl.continue_walking (i < upperbound + 1);
  233. i++)
  234. {
  235. LONGEST elt_off = offset + calc.index_offset (i);
  236. if (is_dynamic_type (elt_type))
  237. {
  238. CORE_ADDR e_address = m_address + elt_off;
  239. elt_type = resolve_dynamic_type (elt_type, {}, e_address);
  240. }
  241. m_impl.process_element (elt_type, elt_off, i, i == upperbound);
  242. }
  243. }
  244. m_impl.finish_dimension (m_nss == m_ndimensions, last_p || m_nss == 1);
  245. m_nss--;
  246. }
  247. /* The array type being processed. */
  248. struct type *m_type;
  249. /* The address in target memory for the object of M_TYPE being
  250. processed. This is required in order to resolve dynamic types. */
  251. CORE_ADDR m_address;
  252. /* An instance of the template specialisation class. */
  253. Impl m_impl;
  254. /* The total number of dimensions in M_TYPE. */
  255. int m_ndimensions;
  256. /* The current dimension number being processed. */
  257. int m_nss;
  258. };
  259. #endif /* F_ARRAY_WALKER_H */