1 /* Support for printing Fortran types for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 Contributed by Motorola. Adapted from the C version by Farooq Butt
6 (fmbutt@engage.sps.mot.com).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "event-top.h"
26 #include "expression.h"
31 #include "typeprint.h"
32 #include "cli/cli-style.h"
37 f_language::print_typedef (struct type
*type
, struct symbol
*new_symbol
,
38 struct ui_file
*stream
) const
40 type
= check_typedef (type
);
41 print_type (type
, "", stream
, 0, 0, &type_print_raw_options
);
47 f_language::print_type (struct type
*type
, const char *varstring
,
48 struct ui_file
*stream
, int show
, int level
,
49 const struct type_print_options
*flags
) const
53 f_type_print_base (type
, stream
, show
, level
);
55 if ((varstring
!= NULL
&& *varstring
!= '\0')
56 /* Need a space if going to print stars or brackets; but not if we
57 will print just a type name. */
59 || type
->name () == 0)
60 && (code
== TYPE_CODE_FUNC
61 || code
== TYPE_CODE_METHOD
62 || code
== TYPE_CODE_ARRAY
63 || ((code
== TYPE_CODE_PTR
64 || code
== TYPE_CODE_REF
)
65 && (type
->target_type ()->code () == TYPE_CODE_FUNC
66 || (type
->target_type ()->code ()
68 || (type
->target_type ()->code ()
69 == TYPE_CODE_ARRAY
))))))
70 gdb_puts (" ", stream
);
71 f_type_print_varspec_prefix (type
, stream
, show
, 0);
73 if (varstring
!= NULL
)
77 gdb_puts (varstring
, stream
);
79 /* For demangled function names, we have the arglist as part of the name,
80 so don't print an additional pair of ()'s. */
82 demangled_args
= (*varstring
!= '\0'
83 && varstring
[strlen (varstring
) - 1] == ')');
84 f_type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
, 0, false);
91 f_language::f_type_print_varspec_prefix (struct type
*type
,
92 struct ui_file
*stream
,
93 int show
, int passed_a_ptr
) const
98 if (type
->name () && show
<= 0)
103 switch (type
->code ())
106 f_type_print_varspec_prefix (type
->target_type (), stream
, 0, 1);
110 f_type_print_varspec_prefix (type
->target_type (), stream
, 0, 0);
112 gdb_printf (stream
, "(");
115 case TYPE_CODE_ARRAY
:
116 f_type_print_varspec_prefix (type
->target_type (), stream
, 0, 0);
119 case TYPE_CODE_UNDEF
:
120 case TYPE_CODE_STRUCT
:
121 case TYPE_CODE_UNION
:
122 case TYPE_CODE_NAMELIST
:
127 case TYPE_CODE_ERROR
:
131 case TYPE_CODE_RANGE
:
132 case TYPE_CODE_STRING
:
133 case TYPE_CODE_METHOD
:
135 case TYPE_CODE_COMPLEX
:
136 case TYPE_CODE_TYPEDEF
:
137 /* These types need no prefix. They are listed here so that
138 gcc -Wall will reveal any types that haven't been handled. */
146 f_language::f_type_print_varspec_suffix (struct type
*type
,
147 struct ui_file
*stream
,
148 int show
, int passed_a_ptr
,
150 int arrayprint_recurse_level
,
151 bool print_rank_only
) const
153 /* No static variables are permitted as an error call may occur during
154 execution of this function. */
159 if (type
->name () && show
<= 0)
164 switch (type
->code ())
166 case TYPE_CODE_ARRAY
:
167 arrayprint_recurse_level
++;
169 if (arrayprint_recurse_level
== 1)
170 gdb_printf (stream
, "(");
172 if (type_not_associated (type
))
173 print_rank_only
= true;
174 else if (type_not_allocated (type
))
175 print_rank_only
= true;
176 else if ((TYPE_ASSOCIATED_PROP (type
)
177 && !TYPE_ASSOCIATED_PROP (type
)->is_constant ())
178 || (TYPE_ALLOCATED_PROP (type
)
179 && !TYPE_ALLOCATED_PROP (type
)->is_constant ())
180 || (TYPE_DATA_LOCATION (type
)
181 && !TYPE_DATA_LOCATION (type
)->is_constant ()))
183 /* This case exist when we ptype a typename which has the dynamic
184 properties but cannot be resolved as there is no object. */
185 print_rank_only
= true;
188 if (type
->target_type ()->code () == TYPE_CODE_ARRAY
)
189 f_type_print_varspec_suffix (type
->target_type (), stream
, 0,
190 0, 0, arrayprint_recurse_level
,
194 gdb_printf (stream
, ":");
197 LONGEST lower_bound
= f77_get_lowerbound (type
);
198 if (lower_bound
!= 1) /* Not the default. */
199 gdb_printf (stream
, "%s:", plongest (lower_bound
));
201 /* Make sure that, if we have an assumed size array, we
202 print out a warning and print the upperbound as '*'. */
204 if (type
->bounds ()->high
.kind () == PROP_UNDEFINED
)
205 gdb_printf (stream
, "*");
208 LONGEST upper_bound
= f77_get_upperbound (type
);
210 gdb_puts (plongest (upper_bound
), stream
);
214 if (type
->target_type ()->code () != TYPE_CODE_ARRAY
)
215 f_type_print_varspec_suffix (type
->target_type (), stream
, 0,
216 0, 0, arrayprint_recurse_level
,
219 if (arrayprint_recurse_level
== 1)
220 gdb_printf (stream
, ")");
222 gdb_printf (stream
, ",");
223 arrayprint_recurse_level
--;
228 f_type_print_varspec_suffix (type
->target_type (), stream
, 0, 1, 0,
229 arrayprint_recurse_level
, false);
230 gdb_printf (stream
, " )");
235 int i
, nfields
= type
->num_fields ();
237 f_type_print_varspec_suffix (type
->target_type (), stream
, 0,
239 arrayprint_recurse_level
, false);
241 gdb_printf (stream
, ") ");
242 gdb_printf (stream
, "(");
243 if (nfields
== 0 && type
->is_prototyped ())
244 print_type (builtin_f_type (type
->arch ())->builtin_void
,
245 "", stream
, -1, 0, 0);
247 for (i
= 0; i
< nfields
; i
++)
251 gdb_puts (", ", stream
);
252 stream
->wrap_here (4);
254 print_type (type
->field (i
).type (), "", stream
, -1, 0, 0);
256 gdb_printf (stream
, ")");
260 case TYPE_CODE_UNDEF
:
261 case TYPE_CODE_STRUCT
:
262 case TYPE_CODE_UNION
:
263 case TYPE_CODE_NAMELIST
:
268 case TYPE_CODE_ERROR
:
272 case TYPE_CODE_RANGE
:
273 case TYPE_CODE_STRING
:
274 case TYPE_CODE_METHOD
:
275 case TYPE_CODE_COMPLEX
:
276 case TYPE_CODE_TYPEDEF
:
277 /* These types do not need a suffix. They are listed so that
278 gcc -Wall will report types that may not have been considered. */
286 f_language::f_type_print_derivation_info (struct type
*type
,
287 struct ui_file
*stream
) const
289 /* Fortran doesn't support multiple inheritance. */
292 if (TYPE_N_BASECLASSES (type
) > 0)
293 gdb_printf (stream
, ", extends(%s) ::", TYPE_BASECLASS (type
, i
)->name ());
299 f_language::f_type_print_base (struct type
*type
, struct ui_file
*stream
,
300 int show
, int level
) const
306 stream
->wrap_here (4);
309 fputs_styled ("<type unknown>", metadata_style
.style (), stream
);
313 /* When SHOW is zero or less, and there is a valid type name, then always
314 just print the type name directly from the type. */
316 if ((show
<= 0) && (type
->name () != NULL
))
318 const char *prefix
= "";
319 if (type
->code () == TYPE_CODE_UNION
)
320 prefix
= "Type, C_Union :: ";
321 else if (type
->code () == TYPE_CODE_STRUCT
322 || type
->code () == TYPE_CODE_NAMELIST
)
324 gdb_printf (stream
, "%*s%s%s", level
, "", prefix
, type
->name ());
328 if (type
->code () != TYPE_CODE_TYPEDEF
)
329 type
= check_typedef (type
);
331 switch (type
->code ())
333 case TYPE_CODE_TYPEDEF
:
334 f_type_print_base (type
->target_type (), stream
, 0, level
);
337 case TYPE_CODE_ARRAY
:
338 f_type_print_base (type
->target_type (), stream
, show
, level
);
341 if (type
->target_type () == NULL
)
342 type_print_unknown_return_type (stream
);
344 f_type_print_base (type
->target_type (), stream
, show
, level
);
348 gdb_printf (stream
, "%*sPTR TO -> ( ", level
, "");
349 f_type_print_base (type
->target_type (), stream
, show
, 0);
353 gdb_printf (stream
, "%*sREF TO -> ( ", level
, "");
354 f_type_print_base (type
->target_type (), stream
, show
, 0);
359 struct type
*void_type
= builtin_f_type (type
->arch ())->builtin_void
;
360 gdb_printf (stream
, "%*s%s", level
, "", void_type
->name ());
364 case TYPE_CODE_UNDEF
:
365 gdb_printf (stream
, "%*sstruct <unknown>", level
, "");
368 case TYPE_CODE_ERROR
:
369 gdb_printf (stream
, "%*s%s", level
, "", TYPE_ERROR_NAME (type
));
372 case TYPE_CODE_RANGE
:
373 /* This should not occur. */
374 gdb_printf (stream
, "%*s<range type>", level
, "");
379 /* There may be some character types that attempt to come
380 through as TYPE_CODE_INT since dbxstclass.h is so
381 C-oriented, we must change these to "character" from "char". */
383 if (strcmp (type
->name (), "char") == 0)
384 gdb_printf (stream
, "%*scharacter", level
, "");
389 case TYPE_CODE_STRING
:
390 /* Strings may have dynamic upperbounds (lengths) like arrays. We
391 check specifically for the PROP_CONST case to indicate that the
392 dynamic type has been resolved. If we arrive here having been
393 asked to print the type of a value with a dynamic type then the
394 bounds will not have been resolved. */
396 if (type
->bounds ()->high
.is_constant ())
398 LONGEST upper_bound
= f77_get_upperbound (type
);
400 gdb_printf (stream
, "character*%s", pulongest (upper_bound
));
403 gdb_printf (stream
, "%*scharacter*(*)", level
, "");
406 case TYPE_CODE_STRUCT
:
407 case TYPE_CODE_UNION
:
408 case TYPE_CODE_NAMELIST
:
409 if (type
->code () == TYPE_CODE_UNION
)
410 gdb_printf (stream
, "%*sType, C_Union ::", level
, "");
412 gdb_printf (stream
, "%*sType", level
, "");
415 f_type_print_derivation_info (type
, stream
);
417 gdb_puts (" ", stream
);
419 gdb_puts (type
->name (), stream
);
421 /* According to the definition,
422 we only print structure elements in case show > 0. */
425 gdb_puts ("\n", stream
);
426 for (index
= 0; index
< type
->num_fields (); index
++)
428 f_type_print_base (type
->field (index
).type (), stream
,
429 show
- 1, level
+ 4);
430 gdb_puts (" :: ", stream
);
431 fputs_styled (type
->field (index
).name (),
432 variable_name_style
.style (), stream
);
433 f_type_print_varspec_suffix (type
->field (index
).type (),
434 stream
, show
- 1, 0, 0, 0, false);
435 gdb_puts ("\n", stream
);
437 gdb_printf (stream
, "%*sEnd Type ", level
, "");
438 gdb_puts (type
->name (), stream
);
442 case TYPE_CODE_MODULE
:
443 gdb_printf (stream
, "%*smodule %s", level
, "", type
->name ());
448 /* Handle types not explicitly handled by the other cases,
449 such as fundamental types. For these, just print whatever
450 the type name is, as recorded in the type itself. If there
451 is no type name, then complain. */
452 if (type
->name () != NULL
)
453 gdb_printf (stream
, "%*s%s", level
, "", type
->name ());
455 error (_("Invalid type code (%d) in symbol table."), type
->code ());
459 if (TYPE_IS_ALLOCATABLE (type
))
460 gdb_printf (stream
, ", allocatable");