f-lang.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. /* Fortran language support routines for GDB, the GNU debugger.
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. Contributed by Motorola. Adapted from the C parser by Farooq Butt
  4. (fmbutt@engage.sps.mot.com).
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "defs.h"
  17. #include "symtab.h"
  18. #include "gdbtypes.h"
  19. #include "expression.h"
  20. #include "parser-defs.h"
  21. #include "language.h"
  22. #include "varobj.h"
  23. #include "gdbcore.h"
  24. #include "f-lang.h"
  25. #include "valprint.h"
  26. #include "value.h"
  27. #include "cp-support.h"
  28. #include "charset.h"
  29. #include "c-lang.h"
  30. #include "target-float.h"
  31. #include "gdbarch.h"
  32. #include "gdbcmd.h"
  33. #include "f-array-walker.h"
  34. #include "f-exp.h"
  35. #include <math.h>
  36. /* Whether GDB should repack array slices created by the user. */
  37. static bool repack_array_slices = false;
  38. /* Implement 'show fortran repack-array-slices'. */
  39. static void
  40. show_repack_array_slices (struct ui_file *file, int from_tty,
  41. struct cmd_list_element *c, const char *value)
  42. {
  43. gdb_printf (file, _("Repacking of Fortran array slices is %s.\n"),
  44. value);
  45. }
  46. /* Debugging of Fortran's array slicing. */
  47. static bool fortran_array_slicing_debug = false;
  48. /* Implement 'show debug fortran-array-slicing'. */
  49. static void
  50. show_fortran_array_slicing_debug (struct ui_file *file, int from_tty,
  51. struct cmd_list_element *c,
  52. const char *value)
  53. {
  54. gdb_printf (file, _("Debugging of Fortran array slicing is %s.\n"),
  55. value);
  56. }
  57. /* Local functions */
  58. static value *fortran_prepare_argument (struct expression *exp,
  59. expr::operation *subexp,
  60. int arg_num, bool is_internal_call_p,
  61. struct type *func_type, enum noside noside);
  62. /* Return the encoding that should be used for the character type
  63. TYPE. */
  64. const char *
  65. f_language::get_encoding (struct type *type)
  66. {
  67. const char *encoding;
  68. switch (TYPE_LENGTH (type))
  69. {
  70. case 1:
  71. encoding = target_charset (type->arch ());
  72. break;
  73. case 4:
  74. if (type_byte_order (type) == BFD_ENDIAN_BIG)
  75. encoding = "UTF-32BE";
  76. else
  77. encoding = "UTF-32LE";
  78. break;
  79. default:
  80. error (_("unrecognized character type"));
  81. }
  82. return encoding;
  83. }
  84. /* A helper function for the "bound" intrinsics that checks that TYPE
  85. is an array. LBOUND_P is true for lower bound; this is used for
  86. the error message, if any. */
  87. static void
  88. fortran_require_array (struct type *type, bool lbound_p)
  89. {
  90. type = check_typedef (type);
  91. if (type->code () != TYPE_CODE_ARRAY)
  92. {
  93. if (lbound_p)
  94. error (_("LBOUND can only be applied to arrays"));
  95. else
  96. error (_("UBOUND can only be applied to arrays"));
  97. }
  98. }
  99. /* Create an array containing the lower bounds (when LBOUND_P is true) or
  100. the upper bounds (when LBOUND_P is false) of ARRAY (which must be of
  101. array type). GDBARCH is the current architecture. */
  102. static struct value *
  103. fortran_bounds_all_dims (bool lbound_p,
  104. struct gdbarch *gdbarch,
  105. struct value *array)
  106. {
  107. type *array_type = check_typedef (value_type (array));
  108. int ndimensions = calc_f77_array_dims (array_type);
  109. /* Allocate a result value of the correct type. */
  110. struct type *range
  111. = create_static_range_type (nullptr,
  112. builtin_type (gdbarch)->builtin_int,
  113. 1, ndimensions);
  114. struct type *elm_type = builtin_type (gdbarch)->builtin_long_long;
  115. struct type *result_type = create_array_type (nullptr, elm_type, range);
  116. struct value *result = allocate_value (result_type);
  117. /* Walk the array dimensions backwards due to the way the array will be
  118. laid out in memory, the first dimension will be the most inner. */
  119. LONGEST elm_len = TYPE_LENGTH (elm_type);
  120. for (LONGEST dst_offset = elm_len * (ndimensions - 1);
  121. dst_offset >= 0;
  122. dst_offset -= elm_len)
  123. {
  124. LONGEST b;
  125. /* Grab the required bound. */
  126. if (lbound_p)
  127. b = f77_get_lowerbound (array_type);
  128. else
  129. b = f77_get_upperbound (array_type);
  130. /* And copy the value into the result value. */
  131. struct value *v = value_from_longest (elm_type, b);
  132. gdb_assert (dst_offset + TYPE_LENGTH (value_type (v))
  133. <= TYPE_LENGTH (value_type (result)));
  134. gdb_assert (TYPE_LENGTH (value_type (v)) == elm_len);
  135. value_contents_copy (result, dst_offset, v, 0, elm_len);
  136. /* Peel another dimension of the array. */
  137. array_type = TYPE_TARGET_TYPE (array_type);
  138. }
  139. return result;
  140. }
  141. /* Return the lower bound (when LBOUND_P is true) or the upper bound (when
  142. LBOUND_P is false) for dimension DIM_VAL (which must be an integer) of
  143. ARRAY (which must be an array). GDBARCH is the current architecture. */
  144. static struct value *
  145. fortran_bounds_for_dimension (bool lbound_p,
  146. struct gdbarch *gdbarch,
  147. struct value *array,
  148. struct value *dim_val)
  149. {
  150. /* Check the requested dimension is valid for this array. */
  151. type *array_type = check_typedef (value_type (array));
  152. int ndimensions = calc_f77_array_dims (array_type);
  153. long dim = value_as_long (dim_val);
  154. if (dim < 1 || dim > ndimensions)
  155. {
  156. if (lbound_p)
  157. error (_("LBOUND dimension must be from 1 to %d"), ndimensions);
  158. else
  159. error (_("UBOUND dimension must be from 1 to %d"), ndimensions);
  160. }
  161. /* The type for the result. */
  162. struct type *bound_type = builtin_type (gdbarch)->builtin_long_long;
  163. /* Walk the dimensions backwards, due to the ordering in which arrays are
  164. laid out the first dimension is the most inner. */
  165. for (int i = ndimensions - 1; i >= 0; --i)
  166. {
  167. /* If this is the requested dimension then we're done. Grab the
  168. bounds and return. */
  169. if (i == dim - 1)
  170. {
  171. LONGEST b;
  172. if (lbound_p)
  173. b = f77_get_lowerbound (array_type);
  174. else
  175. b = f77_get_upperbound (array_type);
  176. return value_from_longest (bound_type, b);
  177. }
  178. /* Peel off another dimension of the array. */
  179. array_type = TYPE_TARGET_TYPE (array_type);
  180. }
  181. gdb_assert_not_reached ("failed to find matching dimension");
  182. }
  183. /* Return the number of dimensions for a Fortran array or string. */
  184. int
  185. calc_f77_array_dims (struct type *array_type)
  186. {
  187. int ndimen = 1;
  188. struct type *tmp_type;
  189. if ((array_type->code () == TYPE_CODE_STRING))
  190. return 1;
  191. if ((array_type->code () != TYPE_CODE_ARRAY))
  192. error (_("Can't get dimensions for a non-array type"));
  193. tmp_type = array_type;
  194. while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
  195. {
  196. if (tmp_type->code () == TYPE_CODE_ARRAY)
  197. ++ndimen;
  198. }
  199. return ndimen;
  200. }
  201. /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
  202. slices. This is a base class for two alternative repacking mechanisms,
  203. one for when repacking from a lazy value, and one for repacking from a
  204. non-lazy (already loaded) value. */
  205. class fortran_array_repacker_base_impl
  206. : public fortran_array_walker_base_impl
  207. {
  208. public:
  209. /* Constructor, DEST is the value we are repacking into. */
  210. fortran_array_repacker_base_impl (struct value *dest)
  211. : m_dest (dest),
  212. m_dest_offset (0)
  213. { /* Nothing. */ }
  214. /* When we start processing the inner most dimension, this is where we
  215. will be creating values for each element as we load them and then copy
  216. them into the M_DEST value. Set a value mark so we can free these
  217. temporary values. */
  218. void start_dimension (struct type *index_type, LONGEST nelts, bool inner_p)
  219. {
  220. if (inner_p)
  221. {
  222. gdb_assert (m_mark == nullptr);
  223. m_mark = value_mark ();
  224. }
  225. }
  226. /* When we finish processing the inner most dimension free all temporary
  227. value that were created. */
  228. void finish_dimension (bool inner_p, bool last_p)
  229. {
  230. if (inner_p)
  231. {
  232. gdb_assert (m_mark != nullptr);
  233. value_free_to_mark (m_mark);
  234. m_mark = nullptr;
  235. }
  236. }
  237. protected:
  238. /* Copy the contents of array element ELT into M_DEST at the next
  239. available offset. */
  240. void copy_element_to_dest (struct value *elt)
  241. {
  242. value_contents_copy (m_dest, m_dest_offset, elt, 0,
  243. TYPE_LENGTH (value_type (elt)));
  244. m_dest_offset += TYPE_LENGTH (value_type (elt));
  245. }
  246. /* The value being written to. */
  247. struct value *m_dest;
  248. /* The byte offset in M_DEST at which the next element should be
  249. written. */
  250. LONGEST m_dest_offset;
  251. /* Set with a call to VALUE_MARK, and then reset after calling
  252. VALUE_FREE_TO_MARK. */
  253. struct value *m_mark = nullptr;
  254. };
  255. /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
  256. slices. This class is specialised for repacking an array slice from a
  257. lazy array value, as such it does not require the parent array value to
  258. be loaded into GDB's memory; the parent value could be huge, while the
  259. slice could be tiny. */
  260. class fortran_lazy_array_repacker_impl
  261. : public fortran_array_repacker_base_impl
  262. {
  263. public:
  264. /* Constructor. TYPE is the type of the slice being loaded from the
  265. parent value, so this type will correctly reflect the strides required
  266. to find all of the elements from the parent value. ADDRESS is the
  267. address in target memory of value matching TYPE, and DEST is the value
  268. we are repacking into. */
  269. explicit fortran_lazy_array_repacker_impl (struct type *type,
  270. CORE_ADDR address,
  271. struct value *dest)
  272. : fortran_array_repacker_base_impl (dest),
  273. m_addr (address)
  274. { /* Nothing. */ }
  275. /* Create a lazy value in target memory representing a single element,
  276. then load the element into GDB's memory and copy the contents into the
  277. destination value. */
  278. void process_element (struct type *elt_type, LONGEST elt_off,
  279. LONGEST index, bool last_p)
  280. {
  281. copy_element_to_dest (value_at_lazy (elt_type, m_addr + elt_off));
  282. }
  283. private:
  284. /* The address in target memory where the parent value starts. */
  285. CORE_ADDR m_addr;
  286. };
  287. /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
  288. slices. This class is specialised for repacking an array slice from a
  289. previously loaded (non-lazy) array value, as such it fetches the
  290. element values from the contents of the parent value. */
  291. class fortran_array_repacker_impl
  292. : public fortran_array_repacker_base_impl
  293. {
  294. public:
  295. /* Constructor. TYPE is the type for the array slice within the parent
  296. value, as such it has stride values as required to find the elements
  297. within the original parent value. ADDRESS is the address in target
  298. memory of the value matching TYPE. BASE_OFFSET is the offset from
  299. the start of VAL's content buffer to the start of the object of TYPE,
  300. VAL is the parent object from which we are loading the value, and
  301. DEST is the value into which we are repacking. */
  302. explicit fortran_array_repacker_impl (struct type *type, CORE_ADDR address,
  303. LONGEST base_offset,
  304. struct value *val, struct value *dest)
  305. : fortran_array_repacker_base_impl (dest),
  306. m_base_offset (base_offset),
  307. m_val (val)
  308. {
  309. gdb_assert (!value_lazy (val));
  310. }
  311. /* Extract an element of ELT_TYPE at offset (M_BASE_OFFSET + ELT_OFF)
  312. from the content buffer of M_VAL then copy this extracted value into
  313. the repacked destination value. */
  314. void process_element (struct type *elt_type, LONGEST elt_off,
  315. LONGEST index, bool last_p)
  316. {
  317. struct value *elt
  318. = value_from_component (m_val, elt_type, (elt_off + m_base_offset));
  319. copy_element_to_dest (elt);
  320. }
  321. private:
  322. /* The offset into the content buffer of M_VAL to the start of the slice
  323. being extracted. */
  324. LONGEST m_base_offset;
  325. /* The parent value from which we are extracting a slice. */
  326. struct value *m_val;
  327. };
  328. /* Evaluate FORTRAN_ASSOCIATED expressions. Both GDBARCH and LANG are
  329. extracted from the expression being evaluated. POINTER is the required
  330. first argument to the 'associated' keyword, and TARGET is the optional
  331. second argument, this will be nullptr if the user only passed one
  332. argument to their use of 'associated'. */
  333. static struct value *
  334. fortran_associated (struct gdbarch *gdbarch, const language_defn *lang,
  335. struct value *pointer, struct value *target = nullptr)
  336. {
  337. struct type *result_type = language_bool_type (lang, gdbarch);
  338. /* All Fortran pointers should have the associated property, this is
  339. how we know the pointer is pointing at something or not. */
  340. struct type *pointer_type = check_typedef (value_type (pointer));
  341. if (TYPE_ASSOCIATED_PROP (pointer_type) == nullptr
  342. && pointer_type->code () != TYPE_CODE_PTR)
  343. error (_("ASSOCIATED can only be applied to pointers"));
  344. /* Get an address from POINTER. Fortran (or at least gfortran) models
  345. array pointers as arrays with a dynamic data address, so we need to
  346. use two approaches here, for real pointers we take the contents of the
  347. pointer as an address. For non-pointers we take the address of the
  348. content. */
  349. CORE_ADDR pointer_addr;
  350. if (pointer_type->code () == TYPE_CODE_PTR)
  351. pointer_addr = value_as_address (pointer);
  352. else
  353. pointer_addr = value_address (pointer);
  354. /* The single argument case, is POINTER associated with anything? */
  355. if (target == nullptr)
  356. {
  357. bool is_associated = false;
  358. /* If POINTER is an actual pointer and doesn't have an associated
  359. property then we need to figure out whether this pointer is
  360. associated by looking at the value of the pointer itself. We make
  361. the assumption that a non-associated pointer will be set to 0.
  362. This is probably true for most targets, but might not be true for
  363. everyone. */
  364. if (pointer_type->code () == TYPE_CODE_PTR
  365. && TYPE_ASSOCIATED_PROP (pointer_type) == nullptr)
  366. is_associated = (pointer_addr != 0);
  367. else
  368. is_associated = !type_not_associated (pointer_type);
  369. return value_from_longest (result_type, is_associated ? 1 : 0);
  370. }
  371. /* The two argument case, is POINTER associated with TARGET? */
  372. struct type *target_type = check_typedef (value_type (target));
  373. struct type *pointer_target_type;
  374. if (pointer_type->code () == TYPE_CODE_PTR)
  375. pointer_target_type = TYPE_TARGET_TYPE (pointer_type);
  376. else
  377. pointer_target_type = pointer_type;
  378. struct type *target_target_type;
  379. if (target_type->code () == TYPE_CODE_PTR)
  380. target_target_type = TYPE_TARGET_TYPE (target_type);
  381. else
  382. target_target_type = target_type;
  383. if (pointer_target_type->code () != target_target_type->code ()
  384. || (pointer_target_type->code () != TYPE_CODE_ARRAY
  385. && (TYPE_LENGTH (pointer_target_type)
  386. != TYPE_LENGTH (target_target_type))))
  387. error (_("arguments to associated must be of same type and kind"));
  388. /* If TARGET is not in memory, or the original pointer is specifically
  389. known to be not associated with anything, then the answer is obviously
  390. false. Alternatively, if POINTER is an actual pointer and has no
  391. associated property, then we have to check if its associated by
  392. looking the value of the pointer itself. We make the assumption that
  393. a non-associated pointer will be set to 0. This is probably true for
  394. most targets, but might not be true for everyone. */
  395. if (value_lval_const (target) != lval_memory
  396. || type_not_associated (pointer_type)
  397. || (TYPE_ASSOCIATED_PROP (pointer_type) == nullptr
  398. && pointer_type->code () == TYPE_CODE_PTR
  399. && pointer_addr == 0))
  400. return value_from_longest (result_type, 0);
  401. /* See the comment for POINTER_ADDR above. */
  402. CORE_ADDR target_addr;
  403. if (target_type->code () == TYPE_CODE_PTR)
  404. target_addr = value_as_address (target);
  405. else
  406. target_addr = value_address (target);
  407. /* Wrap the following checks inside a do { ... } while (false) loop so
  408. that we can use `break' to jump out of the loop. */
  409. bool is_associated = false;
  410. do
  411. {
  412. /* If the addresses are different then POINTER is definitely not
  413. pointing at TARGET. */
  414. if (pointer_addr != target_addr)
  415. break;
  416. /* If POINTER is a real pointer (i.e. not an array pointer, which are
  417. implemented as arrays with a dynamic content address), then this
  418. is all the checking that is needed. */
  419. if (pointer_type->code () == TYPE_CODE_PTR)
  420. {
  421. is_associated = true;
  422. break;
  423. }
  424. /* We have an array pointer. Check the number of dimensions. */
  425. int pointer_dims = calc_f77_array_dims (pointer_type);
  426. int target_dims = calc_f77_array_dims (target_type);
  427. if (pointer_dims != target_dims)
  428. break;
  429. /* Now check that every dimension has the same upper bound, lower
  430. bound, and stride value. */
  431. int dim = 0;
  432. while (dim < pointer_dims)
  433. {
  434. LONGEST pointer_lowerbound, pointer_upperbound, pointer_stride;
  435. LONGEST target_lowerbound, target_upperbound, target_stride;
  436. pointer_type = check_typedef (pointer_type);
  437. target_type = check_typedef (target_type);
  438. struct type *pointer_range = pointer_type->index_type ();
  439. struct type *target_range = target_type->index_type ();
  440. if (!get_discrete_bounds (pointer_range, &pointer_lowerbound,
  441. &pointer_upperbound))
  442. break;
  443. if (!get_discrete_bounds (target_range, &target_lowerbound,
  444. &target_upperbound))
  445. break;
  446. if (pointer_lowerbound != target_lowerbound
  447. || pointer_upperbound != target_upperbound)
  448. break;
  449. /* Figure out the stride (in bits) for both pointer and target.
  450. If either doesn't have a stride then we take the element size,
  451. but we need to convert to bits (hence the * 8). */
  452. pointer_stride = pointer_range->bounds ()->bit_stride ();
  453. if (pointer_stride == 0)
  454. pointer_stride
  455. = type_length_units (check_typedef
  456. (TYPE_TARGET_TYPE (pointer_type))) * 8;
  457. target_stride = target_range->bounds ()->bit_stride ();
  458. if (target_stride == 0)
  459. target_stride
  460. = type_length_units (check_typedef
  461. (TYPE_TARGET_TYPE (target_type))) * 8;
  462. if (pointer_stride != target_stride)
  463. break;
  464. ++dim;
  465. }
  466. if (dim < pointer_dims)
  467. break;
  468. is_associated = true;
  469. }
  470. while (false);
  471. return value_from_longest (result_type, is_associated ? 1 : 0);
  472. }
  473. struct value *
  474. eval_op_f_associated (struct type *expect_type,
  475. struct expression *exp,
  476. enum noside noside,
  477. enum exp_opcode opcode,
  478. struct value *arg1)
  479. {
  480. return fortran_associated (exp->gdbarch, exp->language_defn, arg1);
  481. }
  482. struct value *
  483. eval_op_f_associated (struct type *expect_type,
  484. struct expression *exp,
  485. enum noside noside,
  486. enum exp_opcode opcode,
  487. struct value *arg1,
  488. struct value *arg2)
  489. {
  490. return fortran_associated (exp->gdbarch, exp->language_defn, arg1, arg2);
  491. }
  492. /* Implement FORTRAN_ARRAY_SIZE expression, this corresponds to the 'SIZE'
  493. keyword. Both GDBARCH and LANG are extracted from the expression being
  494. evaluated. ARRAY is the value that should be an array, though this will
  495. not have been checked before calling this function. DIM is optional, if
  496. present then it should be an integer identifying a dimension of the
  497. array to ask about. As with ARRAY the validity of DIM is not checked
  498. before calling this function.
  499. Return either the total number of elements in ARRAY (when DIM is
  500. nullptr), or the number of elements in dimension DIM. */
  501. static struct value *
  502. fortran_array_size (struct gdbarch *gdbarch, const language_defn *lang,
  503. struct value *array, struct value *dim_val = nullptr)
  504. {
  505. /* Check that ARRAY is the correct type. */
  506. struct type *array_type = check_typedef (value_type (array));
  507. if (array_type->code () != TYPE_CODE_ARRAY)
  508. error (_("SIZE can only be applied to arrays"));
  509. if (type_not_allocated (array_type) || type_not_associated (array_type))
  510. error (_("SIZE can only be used on allocated/associated arrays"));
  511. int ndimensions = calc_f77_array_dims (array_type);
  512. int dim = -1;
  513. LONGEST result = 0;
  514. if (dim_val != nullptr)
  515. {
  516. if (check_typedef (value_type (dim_val))->code () != TYPE_CODE_INT)
  517. error (_("DIM argument to SIZE must be an integer"));
  518. dim = (int) value_as_long (dim_val);
  519. if (dim < 1 || dim > ndimensions)
  520. error (_("DIM argument to SIZE must be between 1 and %d"),
  521. ndimensions);
  522. }
  523. /* Now walk over all the dimensions of the array totalling up the
  524. elements in each dimension. */
  525. for (int i = ndimensions - 1; i >= 0; --i)
  526. {
  527. /* If this is the requested dimension then we're done. Grab the
  528. bounds and return. */
  529. if (i == dim - 1 || dim == -1)
  530. {
  531. LONGEST lbound, ubound;
  532. struct type *range = array_type->index_type ();
  533. if (!get_discrete_bounds (range, &lbound, &ubound))
  534. error (_("failed to find array bounds"));
  535. LONGEST dim_size = (ubound - lbound + 1);
  536. if (result == 0)
  537. result = dim_size;
  538. else
  539. result *= dim_size;
  540. if (dim != -1)
  541. break;
  542. }
  543. /* Peel off another dimension of the array. */
  544. array_type = TYPE_TARGET_TYPE (array_type);
  545. }
  546. struct type *result_type
  547. = builtin_f_type (gdbarch)->builtin_integer;
  548. return value_from_longest (result_type, result);
  549. }
  550. /* See f-exp.h. */
  551. struct value *
  552. eval_op_f_array_size (struct type *expect_type,
  553. struct expression *exp,
  554. enum noside noside,
  555. enum exp_opcode opcode,
  556. struct value *arg1)
  557. {
  558. gdb_assert (opcode == FORTRAN_ARRAY_SIZE);
  559. return fortran_array_size (exp->gdbarch, exp->language_defn, arg1);
  560. }
  561. /* See f-exp.h. */
  562. struct value *
  563. eval_op_f_array_size (struct type *expect_type,
  564. struct expression *exp,
  565. enum noside noside,
  566. enum exp_opcode opcode,
  567. struct value *arg1,
  568. struct value *arg2)
  569. {
  570. gdb_assert (opcode == FORTRAN_ARRAY_SIZE);
  571. return fortran_array_size (exp->gdbarch, exp->language_defn, arg1, arg2);
  572. }
  573. /* Implement UNOP_FORTRAN_SHAPE expression. Both GDBARCH and LANG are
  574. extracted from the expression being evaluated. VAL is the value on
  575. which 'shape' was used, this can be any type.
  576. Return an array of integers. If VAL is not an array then the returned
  577. array should have zero elements. If VAL is an array then the returned
  578. array should have one element per dimension, with the element
  579. containing the extent of that dimension from VAL. */
  580. static struct value *
  581. fortran_array_shape (struct gdbarch *gdbarch, const language_defn *lang,
  582. struct value *val)
  583. {
  584. struct type *val_type = check_typedef (value_type (val));
  585. /* If we are passed an array that is either not allocated, or not
  586. associated, then this is explicitly not allowed according to the
  587. Fortran specification. */
  588. if (val_type->code () == TYPE_CODE_ARRAY
  589. && (type_not_associated (val_type) || type_not_allocated (val_type)))
  590. error (_("The array passed to SHAPE must be allocated or associated"));
  591. /* The Fortran specification allows non-array types to be passed to this
  592. function, in which case we get back an empty array.
  593. Calculate the number of dimensions for the resulting array. */
  594. int ndimensions = 0;
  595. if (val_type->code () == TYPE_CODE_ARRAY)
  596. ndimensions = calc_f77_array_dims (val_type);
  597. /* Allocate a result value of the correct type. */
  598. struct type *range
  599. = create_static_range_type (nullptr,
  600. builtin_type (gdbarch)->builtin_int,
  601. 1, ndimensions);
  602. struct type *elm_type = builtin_f_type (gdbarch)->builtin_integer;
  603. struct type *result_type = create_array_type (nullptr, elm_type, range);
  604. struct value *result = allocate_value (result_type);
  605. LONGEST elm_len = TYPE_LENGTH (elm_type);
  606. /* Walk the array dimensions backwards due to the way the array will be
  607. laid out in memory, the first dimension will be the most inner.
  608. If VAL was not an array then ndimensions will be 0, in which case we
  609. will never go around this loop. */
  610. for (LONGEST dst_offset = elm_len * (ndimensions - 1);
  611. dst_offset >= 0;
  612. dst_offset -= elm_len)
  613. {
  614. LONGEST lbound, ubound;
  615. if (!get_discrete_bounds (val_type->index_type (), &lbound, &ubound))
  616. error (_("failed to find array bounds"));
  617. LONGEST dim_size = (ubound - lbound + 1);
  618. /* And copy the value into the result value. */
  619. struct value *v = value_from_longest (elm_type, dim_size);
  620. gdb_assert (dst_offset + TYPE_LENGTH (value_type (v))
  621. <= TYPE_LENGTH (value_type (result)));
  622. gdb_assert (TYPE_LENGTH (value_type (v)) == elm_len);
  623. value_contents_copy (result, dst_offset, v, 0, elm_len);
  624. /* Peel another dimension of the array. */
  625. val_type = TYPE_TARGET_TYPE (val_type);
  626. }
  627. return result;
  628. }
  629. /* See f-exp.h. */
  630. struct value *
  631. eval_op_f_array_shape (struct type *expect_type, struct expression *exp,
  632. enum noside noside, enum exp_opcode opcode,
  633. struct value *arg1)
  634. {
  635. gdb_assert (opcode == UNOP_FORTRAN_SHAPE);
  636. return fortran_array_shape (exp->gdbarch, exp->language_defn, arg1);
  637. }
  638. /* A helper function for UNOP_ABS. */
  639. struct value *
  640. eval_op_f_abs (struct type *expect_type, struct expression *exp,
  641. enum noside noside,
  642. enum exp_opcode opcode,
  643. struct value *arg1)
  644. {
  645. struct type *type = value_type (arg1);
  646. switch (type->code ())
  647. {
  648. case TYPE_CODE_FLT:
  649. {
  650. double d
  651. = fabs (target_float_to_host_double (value_contents (arg1).data (),
  652. value_type (arg1)));
  653. return value_from_host_double (type, d);
  654. }
  655. case TYPE_CODE_INT:
  656. {
  657. LONGEST l = value_as_long (arg1);
  658. l = llabs (l);
  659. return value_from_longest (type, l);
  660. }
  661. }
  662. error (_("ABS of type %s not supported"), TYPE_SAFE_NAME (type));
  663. }
  664. /* A helper function for BINOP_MOD. */
  665. struct value *
  666. eval_op_f_mod (struct type *expect_type, struct expression *exp,
  667. enum noside noside,
  668. enum exp_opcode opcode,
  669. struct value *arg1, struct value *arg2)
  670. {
  671. struct type *type = value_type (arg1);
  672. if (type->code () != value_type (arg2)->code ())
  673. error (_("non-matching types for parameters to MOD ()"));
  674. switch (type->code ())
  675. {
  676. case TYPE_CODE_FLT:
  677. {
  678. double d1
  679. = target_float_to_host_double (value_contents (arg1).data (),
  680. value_type (arg1));
  681. double d2
  682. = target_float_to_host_double (value_contents (arg2).data (),
  683. value_type (arg2));
  684. double d3 = fmod (d1, d2);
  685. return value_from_host_double (type, d3);
  686. }
  687. case TYPE_CODE_INT:
  688. {
  689. LONGEST v1 = value_as_long (arg1);
  690. LONGEST v2 = value_as_long (arg2);
  691. if (v2 == 0)
  692. error (_("calling MOD (N, 0) is undefined"));
  693. LONGEST v3 = v1 - (v1 / v2) * v2;
  694. return value_from_longest (value_type (arg1), v3);
  695. }
  696. }
  697. error (_("MOD of type %s not supported"), TYPE_SAFE_NAME (type));
  698. }
  699. /* A helper function for UNOP_FORTRAN_CEILING. */
  700. struct value *
  701. eval_op_f_ceil (struct type *expect_type, struct expression *exp,
  702. enum noside noside,
  703. enum exp_opcode opcode,
  704. struct value *arg1)
  705. {
  706. struct type *type = value_type (arg1);
  707. if (type->code () != TYPE_CODE_FLT)
  708. error (_("argument to CEILING must be of type float"));
  709. double val
  710. = target_float_to_host_double (value_contents (arg1).data (),
  711. value_type (arg1));
  712. val = ceil (val);
  713. return value_from_host_double (type, val);
  714. }
  715. /* A helper function for UNOP_FORTRAN_FLOOR. */
  716. struct value *
  717. eval_op_f_floor (struct type *expect_type, struct expression *exp,
  718. enum noside noside,
  719. enum exp_opcode opcode,
  720. struct value *arg1)
  721. {
  722. struct type *type = value_type (arg1);
  723. if (type->code () != TYPE_CODE_FLT)
  724. error (_("argument to FLOOR must be of type float"));
  725. double val
  726. = target_float_to_host_double (value_contents (arg1).data (),
  727. value_type (arg1));
  728. val = floor (val);
  729. return value_from_host_double (type, val);
  730. }
  731. /* A helper function for BINOP_FORTRAN_MODULO. */
  732. struct value *
  733. eval_op_f_modulo (struct type *expect_type, struct expression *exp,
  734. enum noside noside,
  735. enum exp_opcode opcode,
  736. struct value *arg1, struct value *arg2)
  737. {
  738. struct type *type = value_type (arg1);
  739. if (type->code () != value_type (arg2)->code ())
  740. error (_("non-matching types for parameters to MODULO ()"));
  741. /* MODULO(A, P) = A - FLOOR (A / P) * P */
  742. switch (type->code ())
  743. {
  744. case TYPE_CODE_INT:
  745. {
  746. LONGEST a = value_as_long (arg1);
  747. LONGEST p = value_as_long (arg2);
  748. LONGEST result = a - (a / p) * p;
  749. if (result != 0 && (a < 0) != (p < 0))
  750. result += p;
  751. return value_from_longest (value_type (arg1), result);
  752. }
  753. case TYPE_CODE_FLT:
  754. {
  755. double a
  756. = target_float_to_host_double (value_contents (arg1).data (),
  757. value_type (arg1));
  758. double p
  759. = target_float_to_host_double (value_contents (arg2).data (),
  760. value_type (arg2));
  761. double result = fmod (a, p);
  762. if (result != 0 && (a < 0.0) != (p < 0.0))
  763. result += p;
  764. return value_from_host_double (type, result);
  765. }
  766. }
  767. error (_("MODULO of type %s not supported"), TYPE_SAFE_NAME (type));
  768. }
  769. /* A helper function for BINOP_FORTRAN_CMPLX. */
  770. struct value *
  771. eval_op_f_cmplx (struct type *expect_type, struct expression *exp,
  772. enum noside noside,
  773. enum exp_opcode opcode,
  774. struct value *arg1, struct value *arg2)
  775. {
  776. struct type *type = builtin_f_type(exp->gdbarch)->builtin_complex_s16;
  777. return value_literal_complex (arg1, arg2, type);
  778. }
  779. /* A helper function for UNOP_FORTRAN_KIND. */
  780. struct value *
  781. eval_op_f_kind (struct type *expect_type, struct expression *exp,
  782. enum noside noside,
  783. enum exp_opcode opcode,
  784. struct value *arg1)
  785. {
  786. struct type *type = value_type (arg1);
  787. switch (type->code ())
  788. {
  789. case TYPE_CODE_STRUCT:
  790. case TYPE_CODE_UNION:
  791. case TYPE_CODE_MODULE:
  792. case TYPE_CODE_FUNC:
  793. error (_("argument to kind must be an intrinsic type"));
  794. }
  795. if (!TYPE_TARGET_TYPE (type))
  796. return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
  797. TYPE_LENGTH (type));
  798. return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
  799. TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
  800. }
  801. /* A helper function for UNOP_FORTRAN_ALLOCATED. */
  802. struct value *
  803. eval_op_f_allocated (struct type *expect_type, struct expression *exp,
  804. enum noside noside, enum exp_opcode op,
  805. struct value *arg1)
  806. {
  807. struct type *type = check_typedef (value_type (arg1));
  808. if (type->code () != TYPE_CODE_ARRAY)
  809. error (_("ALLOCATED can only be applied to arrays"));
  810. struct type *result_type
  811. = builtin_f_type (exp->gdbarch)->builtin_logical;
  812. LONGEST result_value = type_not_allocated (type) ? 0 : 1;
  813. return value_from_longest (result_type, result_value);
  814. }
  815. /* See f-exp.h. */
  816. struct value *
  817. eval_op_f_rank (struct type *expect_type,
  818. struct expression *exp,
  819. enum noside noside,
  820. enum exp_opcode op,
  821. struct value *arg1)
  822. {
  823. gdb_assert (op == UNOP_FORTRAN_RANK);
  824. struct type *result_type
  825. = builtin_f_type (exp->gdbarch)->builtin_integer;
  826. struct type *type = check_typedef (value_type (arg1));
  827. if (type->code () != TYPE_CODE_ARRAY)
  828. return value_from_longest (result_type, 0);
  829. LONGEST ndim = calc_f77_array_dims (type);
  830. return value_from_longest (result_type, ndim);
  831. }
  832. /* A helper function for UNOP_FORTRAN_LOC. */
  833. struct value *
  834. eval_op_f_loc (struct type *expect_type, struct expression *exp,
  835. enum noside noside, enum exp_opcode op,
  836. struct value *arg1)
  837. {
  838. struct type *result_type;
  839. if (gdbarch_ptr_bit (exp->gdbarch) == 16)
  840. result_type = builtin_f_type (exp->gdbarch)->builtin_integer_s2;
  841. else if (gdbarch_ptr_bit (exp->gdbarch) == 32)
  842. result_type = builtin_f_type (exp->gdbarch)->builtin_integer;
  843. else
  844. result_type = builtin_f_type (exp->gdbarch)->builtin_integer_s8;
  845. LONGEST result_value = value_address (arg1);
  846. return value_from_longest (result_type, result_value);
  847. }
  848. namespace expr
  849. {
  850. /* Called from evaluate to perform array indexing, and sub-range
  851. extraction, for Fortran. As well as arrays this function also
  852. handles strings as they can be treated like arrays of characters.
  853. ARRAY is the array or string being accessed. EXP and NOSIDE are as
  854. for evaluate. */
  855. value *
  856. fortran_undetermined::value_subarray (value *array,
  857. struct expression *exp,
  858. enum noside noside)
  859. {
  860. type *original_array_type = check_typedef (value_type (array));
  861. bool is_string_p = original_array_type->code () == TYPE_CODE_STRING;
  862. const std::vector<operation_up> &ops = std::get<1> (m_storage);
  863. int nargs = ops.size ();
  864. /* Perform checks for ARRAY not being available. The somewhat overly
  865. complex logic here is just to keep backward compatibility with the
  866. errors that we used to get before FORTRAN_VALUE_SUBARRAY was
  867. rewritten. Maybe a future task would streamline the error messages we
  868. get here, and update all the expected test results. */
  869. if (ops[0]->opcode () != OP_RANGE)
  870. {
  871. if (type_not_associated (original_array_type))
  872. error (_("no such vector element (vector not associated)"));
  873. else if (type_not_allocated (original_array_type))
  874. error (_("no such vector element (vector not allocated)"));
  875. }
  876. else
  877. {
  878. if (type_not_associated (original_array_type))
  879. error (_("array not associated"));
  880. else if (type_not_allocated (original_array_type))
  881. error (_("array not allocated"));
  882. }
  883. /* First check that the number of dimensions in the type we are slicing
  884. matches the number of arguments we were passed. */
  885. int ndimensions = calc_f77_array_dims (original_array_type);
  886. if (nargs != ndimensions)
  887. error (_("Wrong number of subscripts"));
  888. /* This will be initialised below with the type of the elements held in
  889. ARRAY. */
  890. struct type *inner_element_type;
  891. /* Extract the types of each array dimension from the original array
  892. type. We need these available so we can fill in the default upper and
  893. lower bounds if the user requested slice doesn't provide that
  894. information. Additionally unpacking the dimensions like this gives us
  895. the inner element type. */
  896. std::vector<struct type *> dim_types;
  897. {
  898. dim_types.reserve (ndimensions);
  899. struct type *type = original_array_type;
  900. for (int i = 0; i < ndimensions; ++i)
  901. {
  902. dim_types.push_back (type);
  903. type = TYPE_TARGET_TYPE (type);
  904. }
  905. /* TYPE is now the inner element type of the array, we start the new
  906. array slice off as this type, then as we process the requested slice
  907. (from the user) we wrap new types around this to build up the final
  908. slice type. */
  909. inner_element_type = type;
  910. }
  911. /* As we analyse the new slice type we need to understand if the data
  912. being referenced is contiguous. Do decide this we must track the size
  913. of an element at each dimension of the new slice array. Initially the
  914. elements of the inner most dimension of the array are the same inner
  915. most elements as the original ARRAY. */
  916. LONGEST slice_element_size = TYPE_LENGTH (inner_element_type);
  917. /* Start off assuming all data is contiguous, this will be set to false
  918. if access to any dimension results in non-contiguous data. */
  919. bool is_all_contiguous = true;
  920. /* The TOTAL_OFFSET is the distance in bytes from the start of the
  921. original ARRAY to the start of the new slice. This is calculated as
  922. we process the information from the user. */
  923. LONGEST total_offset = 0;
  924. /* A structure representing information about each dimension of the
  925. resulting slice. */
  926. struct slice_dim
  927. {
  928. /* Constructor. */
  929. slice_dim (LONGEST l, LONGEST h, LONGEST s, struct type *idx)
  930. : low (l),
  931. high (h),
  932. stride (s),
  933. index (idx)
  934. { /* Nothing. */ }
  935. /* The low bound for this dimension of the slice. */
  936. LONGEST low;
  937. /* The high bound for this dimension of the slice. */
  938. LONGEST high;
  939. /* The byte stride for this dimension of the slice. */
  940. LONGEST stride;
  941. struct type *index;
  942. };
  943. /* The dimensions of the resulting slice. */
  944. std::vector<slice_dim> slice_dims;
  945. /* Process the incoming arguments. These arguments are in the reverse
  946. order to the array dimensions, that is the first argument refers to
  947. the last array dimension. */
  948. if (fortran_array_slicing_debug)
  949. debug_printf ("Processing array access:\n");
  950. for (int i = 0; i < nargs; ++i)
  951. {
  952. /* For each dimension of the array the user will have either provided
  953. a ranged access with optional lower bound, upper bound, and
  954. stride, or the user will have supplied a single index. */
  955. struct type *dim_type = dim_types[ndimensions - (i + 1)];
  956. fortran_range_operation *range_op
  957. = dynamic_cast<fortran_range_operation *> (ops[i].get ());
  958. if (range_op != nullptr)
  959. {
  960. enum range_flag range_flag = range_op->get_flags ();
  961. LONGEST low, high, stride;
  962. low = high = stride = 0;
  963. if ((range_flag & RANGE_LOW_BOUND_DEFAULT) == 0)
  964. low = value_as_long (range_op->evaluate0 (exp, noside));
  965. else
  966. low = f77_get_lowerbound (dim_type);
  967. if ((range_flag & RANGE_HIGH_BOUND_DEFAULT) == 0)
  968. high = value_as_long (range_op->evaluate1 (exp, noside));
  969. else
  970. high = f77_get_upperbound (dim_type);
  971. if ((range_flag & RANGE_HAS_STRIDE) == RANGE_HAS_STRIDE)
  972. stride = value_as_long (range_op->evaluate2 (exp, noside));
  973. else
  974. stride = 1;
  975. if (stride == 0)
  976. error (_("stride must not be 0"));
  977. /* Get information about this dimension in the original ARRAY. */
  978. struct type *target_type = TYPE_TARGET_TYPE (dim_type);
  979. struct type *index_type = dim_type->index_type ();
  980. LONGEST lb = f77_get_lowerbound (dim_type);
  981. LONGEST ub = f77_get_upperbound (dim_type);
  982. LONGEST sd = index_type->bit_stride ();
  983. if (sd == 0)
  984. sd = TYPE_LENGTH (target_type) * 8;
  985. if (fortran_array_slicing_debug)
  986. {
  987. debug_printf ("|-> Range access\n");
  988. std::string str = type_to_string (dim_type);
  989. debug_printf ("| |-> Type: %s\n", str.c_str ());
  990. debug_printf ("| |-> Array:\n");
  991. debug_printf ("| | |-> Low bound: %s\n", plongest (lb));
  992. debug_printf ("| | |-> High bound: %s\n", plongest (ub));
  993. debug_printf ("| | |-> Bit stride: %s\n", plongest (sd));
  994. debug_printf ("| | |-> Byte stride: %s\n", plongest (sd / 8));
  995. debug_printf ("| | |-> Type size: %s\n",
  996. pulongest (TYPE_LENGTH (dim_type)));
  997. debug_printf ("| | '-> Target type size: %s\n",
  998. pulongest (TYPE_LENGTH (target_type)));
  999. debug_printf ("| |-> Accessing:\n");
  1000. debug_printf ("| | |-> Low bound: %s\n",
  1001. plongest (low));
  1002. debug_printf ("| | |-> High bound: %s\n",
  1003. plongest (high));
  1004. debug_printf ("| | '-> Element stride: %s\n",
  1005. plongest (stride));
  1006. }
  1007. /* Check the user hasn't asked for something invalid. */
  1008. if (high > ub || low < lb)
  1009. error (_("array subscript out of bounds"));
  1010. /* Calculate what this dimension of the new slice array will look
  1011. like. OFFSET is the byte offset from the start of the
  1012. previous (more outer) dimension to the start of this
  1013. dimension. E_COUNT is the number of elements in this
  1014. dimension. REMAINDER is the number of elements remaining
  1015. between the last included element and the upper bound. For
  1016. example an access '1:6:2' will include elements 1, 3, 5 and
  1017. have a remainder of 1 (element #6). */
  1018. LONGEST lowest = std::min (low, high);
  1019. LONGEST offset = (sd / 8) * (lowest - lb);
  1020. LONGEST e_count = std::abs (high - low) + 1;
  1021. e_count = (e_count + (std::abs (stride) - 1)) / std::abs (stride);
  1022. LONGEST new_low = 1;
  1023. LONGEST new_high = new_low + e_count - 1;
  1024. LONGEST new_stride = (sd * stride) / 8;
  1025. LONGEST last_elem = low + ((e_count - 1) * stride);
  1026. LONGEST remainder = high - last_elem;
  1027. if (low > high)
  1028. {
  1029. offset += std::abs (remainder) * TYPE_LENGTH (target_type);
  1030. if (stride > 0)
  1031. error (_("incorrect stride and boundary combination"));
  1032. }
  1033. else if (stride < 0)
  1034. error (_("incorrect stride and boundary combination"));
  1035. /* Is the data within this dimension contiguous? It is if the
  1036. newly computed stride is the same size as a single element of
  1037. this dimension. */
  1038. bool is_dim_contiguous = (new_stride == slice_element_size);
  1039. is_all_contiguous &= is_dim_contiguous;
  1040. if (fortran_array_slicing_debug)
  1041. {
  1042. debug_printf ("| '-> Results:\n");
  1043. debug_printf ("| |-> Offset = %s\n", plongest (offset));
  1044. debug_printf ("| |-> Elements = %s\n", plongest (e_count));
  1045. debug_printf ("| |-> Low bound = %s\n", plongest (new_low));
  1046. debug_printf ("| |-> High bound = %s\n",
  1047. plongest (new_high));
  1048. debug_printf ("| |-> Byte stride = %s\n",
  1049. plongest (new_stride));
  1050. debug_printf ("| |-> Last element = %s\n",
  1051. plongest (last_elem));
  1052. debug_printf ("| |-> Remainder = %s\n",
  1053. plongest (remainder));
  1054. debug_printf ("| '-> Contiguous = %s\n",
  1055. (is_dim_contiguous ? "Yes" : "No"));
  1056. }
  1057. /* Figure out how big (in bytes) an element of this dimension of
  1058. the new array slice will be. */
  1059. slice_element_size = std::abs (new_stride * e_count);
  1060. slice_dims.emplace_back (new_low, new_high, new_stride,
  1061. index_type);
  1062. /* Update the total offset. */
  1063. total_offset += offset;
  1064. }
  1065. else
  1066. {
  1067. /* There is a single index for this dimension. */
  1068. LONGEST index
  1069. = value_as_long (ops[i]->evaluate_with_coercion (exp, noside));
  1070. /* Get information about this dimension in the original ARRAY. */
  1071. struct type *target_type = TYPE_TARGET_TYPE (dim_type);
  1072. struct type *index_type = dim_type->index_type ();
  1073. LONGEST lb = f77_get_lowerbound (dim_type);
  1074. LONGEST ub = f77_get_upperbound (dim_type);
  1075. LONGEST sd = index_type->bit_stride () / 8;
  1076. if (sd == 0)
  1077. sd = TYPE_LENGTH (target_type);
  1078. if (fortran_array_slicing_debug)
  1079. {
  1080. debug_printf ("|-> Index access\n");
  1081. std::string str = type_to_string (dim_type);
  1082. debug_printf ("| |-> Type: %s\n", str.c_str ());
  1083. debug_printf ("| |-> Array:\n");
  1084. debug_printf ("| | |-> Low bound: %s\n", plongest (lb));
  1085. debug_printf ("| | |-> High bound: %s\n", plongest (ub));
  1086. debug_printf ("| | |-> Byte stride: %s\n", plongest (sd));
  1087. debug_printf ("| | |-> Type size: %s\n",
  1088. pulongest (TYPE_LENGTH (dim_type)));
  1089. debug_printf ("| | '-> Target type size: %s\n",
  1090. pulongest (TYPE_LENGTH (target_type)));
  1091. debug_printf ("| '-> Accessing:\n");
  1092. debug_printf ("| '-> Index: %s\n",
  1093. plongest (index));
  1094. }
  1095. /* If the array has actual content then check the index is in
  1096. bounds. An array without content (an unbound array) doesn't
  1097. have a known upper bound, so don't error check in that
  1098. situation. */
  1099. if (index < lb
  1100. || (dim_type->index_type ()->bounds ()->high.kind () != PROP_UNDEFINED
  1101. && index > ub)
  1102. || (VALUE_LVAL (array) != lval_memory
  1103. && dim_type->index_type ()->bounds ()->high.kind () == PROP_UNDEFINED))
  1104. {
  1105. if (type_not_associated (dim_type))
  1106. error (_("no such vector element (vector not associated)"));
  1107. else if (type_not_allocated (dim_type))
  1108. error (_("no such vector element (vector not allocated)"));
  1109. else
  1110. error (_("no such vector element"));
  1111. }
  1112. /* Calculate using the type stride, not the target type size. */
  1113. LONGEST offset = sd * (index - lb);
  1114. total_offset += offset;
  1115. }
  1116. }
  1117. /* Build a type that represents the new array slice in the target memory
  1118. of the original ARRAY, this type makes use of strides to correctly
  1119. find only those elements that are part of the new slice. */
  1120. struct type *array_slice_type = inner_element_type;
  1121. for (const auto &d : slice_dims)
  1122. {
  1123. /* Create the range. */
  1124. dynamic_prop p_low, p_high, p_stride;
  1125. p_low.set_const_val (d.low);
  1126. p_high.set_const_val (d.high);
  1127. p_stride.set_const_val (d.stride);
  1128. struct type *new_range
  1129. = create_range_type_with_stride ((struct type *) NULL,
  1130. TYPE_TARGET_TYPE (d.index),
  1131. &p_low, &p_high, 0, &p_stride,
  1132. true);
  1133. array_slice_type
  1134. = create_array_type (nullptr, array_slice_type, new_range);
  1135. }
  1136. if (fortran_array_slicing_debug)
  1137. {
  1138. debug_printf ("'-> Final result:\n");
  1139. debug_printf (" |-> Type: %s\n",
  1140. type_to_string (array_slice_type).c_str ());
  1141. debug_printf (" |-> Total offset: %s\n",
  1142. plongest (total_offset));
  1143. debug_printf (" |-> Base address: %s\n",
  1144. core_addr_to_string (value_address (array)));
  1145. debug_printf (" '-> Contiguous = %s\n",
  1146. (is_all_contiguous ? "Yes" : "No"));
  1147. }
  1148. /* Should we repack this array slice? */
  1149. if (!is_all_contiguous && (repack_array_slices || is_string_p))
  1150. {
  1151. /* Build a type for the repacked slice. */
  1152. struct type *repacked_array_type = inner_element_type;
  1153. for (const auto &d : slice_dims)
  1154. {
  1155. /* Create the range. */
  1156. dynamic_prop p_low, p_high, p_stride;
  1157. p_low.set_const_val (d.low);
  1158. p_high.set_const_val (d.high);
  1159. p_stride.set_const_val (TYPE_LENGTH (repacked_array_type));
  1160. struct type *new_range
  1161. = create_range_type_with_stride ((struct type *) NULL,
  1162. TYPE_TARGET_TYPE (d.index),
  1163. &p_low, &p_high, 0, &p_stride,
  1164. true);
  1165. repacked_array_type
  1166. = create_array_type (nullptr, repacked_array_type, new_range);
  1167. }
  1168. /* Now copy the elements from the original ARRAY into the packed
  1169. array value DEST. */
  1170. struct value *dest = allocate_value (repacked_array_type);
  1171. if (value_lazy (array)
  1172. || (total_offset + TYPE_LENGTH (array_slice_type)
  1173. > TYPE_LENGTH (check_typedef (value_type (array)))))
  1174. {
  1175. fortran_array_walker<fortran_lazy_array_repacker_impl> p
  1176. (array_slice_type, value_address (array) + total_offset, dest);
  1177. p.walk ();
  1178. }
  1179. else
  1180. {
  1181. fortran_array_walker<fortran_array_repacker_impl> p
  1182. (array_slice_type, value_address (array) + total_offset,
  1183. total_offset, array, dest);
  1184. p.walk ();
  1185. }
  1186. array = dest;
  1187. }
  1188. else
  1189. {
  1190. if (VALUE_LVAL (array) == lval_memory)
  1191. {
  1192. /* If the value we're taking a slice from is not yet loaded, or
  1193. the requested slice is outside the values content range then
  1194. just create a new lazy value pointing at the memory where the
  1195. contents we're looking for exist. */
  1196. if (value_lazy (array)
  1197. || (total_offset + TYPE_LENGTH (array_slice_type)
  1198. > TYPE_LENGTH (check_typedef (value_type (array)))))
  1199. array = value_at_lazy (array_slice_type,
  1200. value_address (array) + total_offset);
  1201. else
  1202. array = value_from_contents_and_address
  1203. (array_slice_type, value_contents (array).data () + total_offset,
  1204. value_address (array) + total_offset);
  1205. }
  1206. else if (!value_lazy (array))
  1207. array = value_from_component (array, array_slice_type, total_offset);
  1208. else
  1209. error (_("cannot subscript arrays that are not in memory"));
  1210. }
  1211. return array;
  1212. }
  1213. value *
  1214. fortran_undetermined::evaluate (struct type *expect_type,
  1215. struct expression *exp,
  1216. enum noside noside)
  1217. {
  1218. value *callee = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
  1219. if (noside == EVAL_AVOID_SIDE_EFFECTS
  1220. && is_dynamic_type (value_type (callee)))
  1221. callee = std::get<0> (m_storage)->evaluate (nullptr, exp, EVAL_NORMAL);
  1222. struct type *type = check_typedef (value_type (callee));
  1223. enum type_code code = type->code ();
  1224. if (code == TYPE_CODE_PTR)
  1225. {
  1226. /* Fortran always passes variable to subroutines as pointer.
  1227. So we need to look into its target type to see if it is
  1228. array, string or function. If it is, we need to switch
  1229. to the target value the original one points to. */
  1230. struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
  1231. if (target_type->code () == TYPE_CODE_ARRAY
  1232. || target_type->code () == TYPE_CODE_STRING
  1233. || target_type->code () == TYPE_CODE_FUNC)
  1234. {
  1235. callee = value_ind (callee);
  1236. type = check_typedef (value_type (callee));
  1237. code = type->code ();
  1238. }
  1239. }
  1240. switch (code)
  1241. {
  1242. case TYPE_CODE_ARRAY:
  1243. case TYPE_CODE_STRING:
  1244. return value_subarray (callee, exp, noside);
  1245. case TYPE_CODE_PTR:
  1246. case TYPE_CODE_FUNC:
  1247. case TYPE_CODE_INTERNAL_FUNCTION:
  1248. {
  1249. /* It's a function call. Allocate arg vector, including
  1250. space for the function to be called in argvec[0] and a
  1251. termination NULL. */
  1252. const std::vector<operation_up> &actual (std::get<1> (m_storage));
  1253. std::vector<value *> argvec (actual.size ());
  1254. bool is_internal_func = (code == TYPE_CODE_INTERNAL_FUNCTION);
  1255. for (int tem = 0; tem < argvec.size (); tem++)
  1256. argvec[tem] = fortran_prepare_argument (exp, actual[tem].get (),
  1257. tem, is_internal_func,
  1258. value_type (callee),
  1259. noside);
  1260. return evaluate_subexp_do_call (exp, noside, callee, argvec,
  1261. nullptr, expect_type);
  1262. }
  1263. default:
  1264. error (_("Cannot perform substring on this type"));
  1265. }
  1266. }
  1267. value *
  1268. fortran_bound_1arg::evaluate (struct type *expect_type,
  1269. struct expression *exp,
  1270. enum noside noside)
  1271. {
  1272. bool lbound_p = std::get<0> (m_storage) == FORTRAN_LBOUND;
  1273. value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
  1274. fortran_require_array (value_type (arg1), lbound_p);
  1275. return fortran_bounds_all_dims (lbound_p, exp->gdbarch, arg1);
  1276. }
  1277. value *
  1278. fortran_bound_2arg::evaluate (struct type *expect_type,
  1279. struct expression *exp,
  1280. enum noside noside)
  1281. {
  1282. bool lbound_p = std::get<0> (m_storage) == FORTRAN_LBOUND;
  1283. value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
  1284. fortran_require_array (value_type (arg1), lbound_p);
  1285. /* User asked for the bounds of a specific dimension of the array. */
  1286. value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
  1287. struct type *type = check_typedef (value_type (arg2));
  1288. if (type->code () != TYPE_CODE_INT)
  1289. {
  1290. if (lbound_p)
  1291. error (_("LBOUND second argument should be an integer"));
  1292. else
  1293. error (_("UBOUND second argument should be an integer"));
  1294. }
  1295. return fortran_bounds_for_dimension (lbound_p, exp->gdbarch, arg1, arg2);
  1296. }
  1297. /* Implement STRUCTOP_STRUCT for Fortran. See operation::evaluate in
  1298. expression.h for argument descriptions. */
  1299. value *
  1300. fortran_structop_operation::evaluate (struct type *expect_type,
  1301. struct expression *exp,
  1302. enum noside noside)
  1303. {
  1304. value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
  1305. const char *str = std::get<1> (m_storage).c_str ();
  1306. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  1307. {
  1308. struct type *type = lookup_struct_elt_type (value_type (arg1), str, 1);
  1309. if (type != nullptr && is_dynamic_type (type))
  1310. arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, EVAL_NORMAL);
  1311. }
  1312. value *elt = value_struct_elt (&arg1, {}, str, NULL, "structure");
  1313. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  1314. {
  1315. struct type *elt_type = value_type (elt);
  1316. if (is_dynamic_type (elt_type))
  1317. {
  1318. const gdb_byte *valaddr = value_contents_for_printing (elt).data ();
  1319. CORE_ADDR address = value_address (elt);
  1320. gdb::array_view<const gdb_byte> view
  1321. = gdb::make_array_view (valaddr, TYPE_LENGTH (elt_type));
  1322. elt_type = resolve_dynamic_type (elt_type, view, address);
  1323. }
  1324. elt = value_zero (elt_type, VALUE_LVAL (elt));
  1325. }
  1326. return elt;
  1327. }
  1328. } /* namespace expr */
  1329. /* See language.h. */
  1330. void
  1331. f_language::print_array_index (struct type *index_type, LONGEST index,
  1332. struct ui_file *stream,
  1333. const value_print_options *options) const
  1334. {
  1335. struct value *index_value = value_from_longest (index_type, index);
  1336. gdb_printf (stream, "(");
  1337. value_print (index_value, stream, options);
  1338. gdb_printf (stream, ") = ");
  1339. }
  1340. /* See language.h. */
  1341. void
  1342. f_language::language_arch_info (struct gdbarch *gdbarch,
  1343. struct language_arch_info *lai) const
  1344. {
  1345. const struct builtin_f_type *builtin = builtin_f_type (gdbarch);
  1346. /* Helper function to allow shorter lines below. */
  1347. auto add = [&] (struct type * t)
  1348. {
  1349. lai->add_primitive_type (t);
  1350. };
  1351. add (builtin->builtin_character);
  1352. add (builtin->builtin_logical);
  1353. add (builtin->builtin_logical_s1);
  1354. add (builtin->builtin_logical_s2);
  1355. add (builtin->builtin_logical_s8);
  1356. add (builtin->builtin_real);
  1357. add (builtin->builtin_real_s8);
  1358. add (builtin->builtin_real_s16);
  1359. add (builtin->builtin_complex_s8);
  1360. add (builtin->builtin_complex_s16);
  1361. add (builtin->builtin_void);
  1362. lai->set_string_char_type (builtin->builtin_character);
  1363. lai->set_bool_type (builtin->builtin_logical_s2, "logical");
  1364. }
  1365. /* See language.h. */
  1366. unsigned int
  1367. f_language::search_name_hash (const char *name) const
  1368. {
  1369. return cp_search_name_hash (name);
  1370. }
  1371. /* See language.h. */
  1372. struct block_symbol
  1373. f_language::lookup_symbol_nonlocal (const char *name,
  1374. const struct block *block,
  1375. const domain_enum domain) const
  1376. {
  1377. return cp_lookup_symbol_nonlocal (this, name, block, domain);
  1378. }
  1379. /* See language.h. */
  1380. symbol_name_matcher_ftype *
  1381. f_language::get_symbol_name_matcher_inner
  1382. (const lookup_name_info &lookup_name) const
  1383. {
  1384. return cp_get_symbol_name_matcher (lookup_name);
  1385. }
  1386. /* Single instance of the Fortran language class. */
  1387. static f_language f_language_defn;
  1388. static void *
  1389. build_fortran_types (struct gdbarch *gdbarch)
  1390. {
  1391. struct builtin_f_type *builtin_f_type
  1392. = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_f_type);
  1393. builtin_f_type->builtin_void
  1394. = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
  1395. builtin_f_type->builtin_character
  1396. = arch_type (gdbarch, TYPE_CODE_CHAR, TARGET_CHAR_BIT, "character");
  1397. builtin_f_type->builtin_logical_s1
  1398. = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "logical*1");
  1399. builtin_f_type->builtin_integer_s2
  1400. = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), 0,
  1401. "integer*2");
  1402. builtin_f_type->builtin_integer_s8
  1403. = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch), 0,
  1404. "integer*8");
  1405. builtin_f_type->builtin_logical_s2
  1406. = arch_boolean_type (gdbarch, gdbarch_short_bit (gdbarch), 1,
  1407. "logical*2");
  1408. builtin_f_type->builtin_logical_s8
  1409. = arch_boolean_type (gdbarch, gdbarch_long_long_bit (gdbarch), 1,
  1410. "logical*8");
  1411. builtin_f_type->builtin_integer
  1412. = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0,
  1413. "integer");
  1414. builtin_f_type->builtin_logical
  1415. = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1,
  1416. "logical*4");
  1417. builtin_f_type->builtin_real
  1418. = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
  1419. "real", gdbarch_float_format (gdbarch));
  1420. builtin_f_type->builtin_real_s8
  1421. = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
  1422. "real*8", gdbarch_double_format (gdbarch));
  1423. auto fmt = gdbarch_floatformat_for_type (gdbarch, "real(kind=16)", 128);
  1424. if (fmt != nullptr)
  1425. builtin_f_type->builtin_real_s16
  1426. = arch_float_type (gdbarch, 128, "real*16", fmt);
  1427. else if (gdbarch_long_double_bit (gdbarch) == 128)
  1428. builtin_f_type->builtin_real_s16
  1429. = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
  1430. "real*16", gdbarch_long_double_format (gdbarch));
  1431. else
  1432. builtin_f_type->builtin_real_s16
  1433. = arch_type (gdbarch, TYPE_CODE_ERROR, 128, "real*16");
  1434. builtin_f_type->builtin_complex_s8
  1435. = init_complex_type ("complex*8", builtin_f_type->builtin_real);
  1436. builtin_f_type->builtin_complex_s16
  1437. = init_complex_type ("complex*16", builtin_f_type->builtin_real_s8);
  1438. if (builtin_f_type->builtin_real_s16->code () == TYPE_CODE_ERROR)
  1439. builtin_f_type->builtin_complex_s32
  1440. = arch_type (gdbarch, TYPE_CODE_ERROR, 256, "complex*32");
  1441. else
  1442. builtin_f_type->builtin_complex_s32
  1443. = init_complex_type ("complex*32", builtin_f_type->builtin_real_s16);
  1444. return builtin_f_type;
  1445. }
  1446. static struct gdbarch_data *f_type_data;
  1447. const struct builtin_f_type *
  1448. builtin_f_type (struct gdbarch *gdbarch)
  1449. {
  1450. return (const struct builtin_f_type *) gdbarch_data (gdbarch, f_type_data);
  1451. }
  1452. /* Command-list for the "set/show fortran" prefix command. */
  1453. static struct cmd_list_element *set_fortran_list;
  1454. static struct cmd_list_element *show_fortran_list;
  1455. void _initialize_f_language ();
  1456. void
  1457. _initialize_f_language ()
  1458. {
  1459. f_type_data = gdbarch_data_register_post_init (build_fortran_types);
  1460. add_setshow_prefix_cmd
  1461. ("fortran", no_class,
  1462. _("Prefix command for changing Fortran-specific settings."),
  1463. _("Generic command for showing Fortran-specific settings."),
  1464. &set_fortran_list, &show_fortran_list,
  1465. &setlist, &showlist);
  1466. add_setshow_boolean_cmd ("repack-array-slices", class_vars,
  1467. &repack_array_slices, _("\
  1468. Enable or disable repacking of non-contiguous array slices."), _("\
  1469. Show whether non-contiguous array slices are repacked."), _("\
  1470. When the user requests a slice of a Fortran array then we can either return\n\
  1471. a descriptor that describes the array in place (using the original array data\n\
  1472. in its existing location) or the original data can be repacked (copied) to a\n\
  1473. new location.\n\
  1474. \n\
  1475. When the content of the array slice is contiguous within the original array\n\
  1476. then the result will never be repacked, but when the data for the new array\n\
  1477. is non-contiguous within the original array repacking will only be performed\n\
  1478. when this setting is on."),
  1479. NULL,
  1480. show_repack_array_slices,
  1481. &set_fortran_list, &show_fortran_list);
  1482. /* Debug Fortran's array slicing logic. */
  1483. add_setshow_boolean_cmd ("fortran-array-slicing", class_maintenance,
  1484. &fortran_array_slicing_debug, _("\
  1485. Set debugging of Fortran array slicing."), _("\
  1486. Show debugging of Fortran array slicing."), _("\
  1487. When on, debugging of Fortran array slicing is enabled."),
  1488. NULL,
  1489. show_fortran_array_slicing_debug,
  1490. &setdebuglist, &showdebuglist);
  1491. }
  1492. /* Ensures that function argument VALUE is in the appropriate form to
  1493. pass to a Fortran function. Returns a possibly new value that should
  1494. be used instead of VALUE.
  1495. When IS_ARTIFICIAL is true this indicates an artificial argument,
  1496. e.g. hidden string lengths which the GNU Fortran argument passing
  1497. convention specifies as being passed by value.
  1498. When IS_ARTIFICIAL is false, the argument is passed by pointer. If the
  1499. value is already in target memory then return a value that is a pointer
  1500. to VALUE. If VALUE is not in memory (e.g. an integer literal), allocate
  1501. space in the target, copy VALUE in, and return a pointer to the in
  1502. memory copy. */
  1503. static struct value *
  1504. fortran_argument_convert (struct value *value, bool is_artificial)
  1505. {
  1506. if (!is_artificial)
  1507. {
  1508. /* If the value is not in the inferior e.g. registers values,
  1509. convenience variables and user input. */
  1510. if (VALUE_LVAL (value) != lval_memory)
  1511. {
  1512. struct type *type = value_type (value);
  1513. const int length = TYPE_LENGTH (type);
  1514. const CORE_ADDR addr
  1515. = value_as_long (value_allocate_space_in_inferior (length));
  1516. write_memory (addr, value_contents (value).data (), length);
  1517. struct value *val = value_from_contents_and_address
  1518. (type, value_contents (value).data (), addr);
  1519. return value_addr (val);
  1520. }
  1521. else
  1522. return value_addr (value); /* Program variables, e.g. arrays. */
  1523. }
  1524. return value;
  1525. }
  1526. /* Prepare (and return) an argument value ready for an inferior function
  1527. call to a Fortran function. EXP and POS are the expressions describing
  1528. the argument to prepare. ARG_NUM is the argument number being
  1529. prepared, with 0 being the first argument and so on. FUNC_TYPE is the
  1530. type of the function being called.
  1531. IS_INTERNAL_CALL_P is true if this is a call to a function of type
  1532. TYPE_CODE_INTERNAL_FUNCTION, otherwise this parameter is false.
  1533. NOSIDE has its usual meaning for expression parsing (see eval.c).
  1534. Arguments in Fortran are normally passed by address, we coerce the
  1535. arguments here rather than in value_arg_coerce as otherwise the call to
  1536. malloc (to place the non-lvalue parameters in target memory) is hit by
  1537. this Fortran specific logic. This results in malloc being called with a
  1538. pointer to an integer followed by an attempt to malloc the arguments to
  1539. malloc in target memory. Infinite recursion ensues. */
  1540. static value *
  1541. fortran_prepare_argument (struct expression *exp,
  1542. expr::operation *subexp,
  1543. int arg_num, bool is_internal_call_p,
  1544. struct type *func_type, enum noside noside)
  1545. {
  1546. if (is_internal_call_p)
  1547. return subexp->evaluate_with_coercion (exp, noside);
  1548. bool is_artificial = ((arg_num >= func_type->num_fields ())
  1549. ? true
  1550. : TYPE_FIELD_ARTIFICIAL (func_type, arg_num));
  1551. /* If this is an artificial argument, then either, this is an argument
  1552. beyond the end of the known arguments, or possibly, there are no known
  1553. arguments (maybe missing debug info).
  1554. For these artificial arguments, if the user has prefixed it with '&'
  1555. (for address-of), then lets always allow this to succeed, even if the
  1556. argument is not actually in inferior memory. This will allow the user
  1557. to pass arguments to a Fortran function even when there's no debug
  1558. information.
  1559. As we already pass the address of non-artificial arguments, all we
  1560. need to do if skip the UNOP_ADDR operator in the expression and mark
  1561. the argument as non-artificial. */
  1562. if (is_artificial)
  1563. {
  1564. expr::unop_addr_operation *addrop
  1565. = dynamic_cast<expr::unop_addr_operation *> (subexp);
  1566. if (addrop != nullptr)
  1567. {
  1568. subexp = addrop->get_expression ().get ();
  1569. is_artificial = false;
  1570. }
  1571. }
  1572. struct value *arg_val = subexp->evaluate_with_coercion (exp, noside);
  1573. return fortran_argument_convert (arg_val, is_artificial);
  1574. }
  1575. /* See f-lang.h. */
  1576. struct type *
  1577. fortran_preserve_arg_pointer (struct value *arg, struct type *type)
  1578. {
  1579. if (value_type (arg)->code () == TYPE_CODE_PTR)
  1580. return value_type (arg);
  1581. return type;
  1582. }
  1583. /* See f-lang.h. */
  1584. CORE_ADDR
  1585. fortran_adjust_dynamic_array_base_address_hack (struct type *type,
  1586. CORE_ADDR address)
  1587. {
  1588. gdb_assert (type->code () == TYPE_CODE_ARRAY);
  1589. /* We can't adjust the base address for arrays that have no content. */
  1590. if (type_not_allocated (type) || type_not_associated (type))
  1591. return address;
  1592. int ndimensions = calc_f77_array_dims (type);
  1593. LONGEST total_offset = 0;
  1594. /* Walk through each of the dimensions of this array type and figure out
  1595. if any of the dimensions are "backwards", that is the base address
  1596. for this dimension points to the element at the highest memory
  1597. address and the stride is negative. */
  1598. struct type *tmp_type = type;
  1599. for (int i = 0 ; i < ndimensions; ++i)
  1600. {
  1601. /* Grab the range for this dimension and extract the lower and upper
  1602. bounds. */
  1603. tmp_type = check_typedef (tmp_type);
  1604. struct type *range_type = tmp_type->index_type ();
  1605. LONGEST lowerbound, upperbound, stride;
  1606. if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
  1607. error ("failed to get range bounds");
  1608. /* Figure out the stride for this dimension. */
  1609. struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (tmp_type));
  1610. stride = tmp_type->index_type ()->bounds ()->bit_stride ();
  1611. if (stride == 0)
  1612. stride = type_length_units (elt_type);
  1613. else
  1614. {
  1615. int unit_size
  1616. = gdbarch_addressable_memory_unit_size (elt_type->arch ());
  1617. stride /= (unit_size * 8);
  1618. }
  1619. /* If this dimension is "backward" then figure out the offset
  1620. adjustment required to point to the element at the lowest memory
  1621. address, and add this to the total offset. */
  1622. LONGEST offset = 0;
  1623. if (stride < 0 && lowerbound < upperbound)
  1624. offset = (upperbound - lowerbound) * stride;
  1625. total_offset += offset;
  1626. tmp_type = TYPE_TARGET_TYPE (tmp_type);
  1627. }
  1628. /* Adjust the address of this object and return it. */
  1629. address += total_offset;
  1630. return address;
  1631. }