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"
24 #include "gdbsupport/gdb_obstack.h"
28 #include "expression.h"
33 #include "typeprint.h"
34 #include "cli/cli-style.h"
39 f_language::print_typedef (struct type
*type
, struct symbol
*new_symbol
,
40 struct ui_file
*stream
) const
42 type
= check_typedef (type
);
43 print_type (type
, "", stream
, 0, 0, &type_print_raw_options
);
49 f_language::print_type (struct type
*type
, const char *varstring
,
50 struct ui_file
*stream
, int show
, int level
,
51 const struct type_print_options
*flags
) const
55 f_type_print_base (type
, stream
, show
, level
);
57 if ((varstring
!= NULL
&& *varstring
!= '\0')
58 /* Need a space if going to print stars or brackets; but not if we
59 will print just a type name. */
61 || type
->name () == 0)
62 && (code
== TYPE_CODE_FUNC
63 || code
== TYPE_CODE_METHOD
64 || code
== TYPE_CODE_ARRAY
65 || ((code
== TYPE_CODE_PTR
66 || code
== TYPE_CODE_REF
)
67 && (type
->target_type ()->code () == TYPE_CODE_FUNC
68 || (type
->target_type ()->code ()
70 || (type
->target_type ()->code ()
71 == TYPE_CODE_ARRAY
))))))
72 gdb_puts (" ", stream
);
73 f_type_print_varspec_prefix (type
, stream
, show
, 0);
75 if (varstring
!= NULL
)
79 gdb_puts (varstring
, stream
);
81 /* For demangled function names, we have the arglist as part of the name,
82 so don't print an additional pair of ()'s. */
84 demangled_args
= (*varstring
!= '\0'
85 && varstring
[strlen (varstring
) - 1] == ')');
86 f_type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
, 0, false);
93 f_language::f_type_print_varspec_prefix (struct type
*type
,
94 struct ui_file
*stream
,
95 int show
, int passed_a_ptr
) const
100 if (type
->name () && show
<= 0)
105 switch (type
->code ())
108 f_type_print_varspec_prefix (type
->target_type (), stream
, 0, 1);
112 f_type_print_varspec_prefix (type
->target_type (), stream
, 0, 0);
114 gdb_printf (stream
, "(");
117 case TYPE_CODE_ARRAY
:
118 f_type_print_varspec_prefix (type
->target_type (), stream
, 0, 0);
121 case TYPE_CODE_UNDEF
:
122 case TYPE_CODE_STRUCT
:
123 case TYPE_CODE_UNION
:
124 case TYPE_CODE_NAMELIST
:
129 case TYPE_CODE_ERROR
:
133 case TYPE_CODE_RANGE
:
134 case TYPE_CODE_STRING
:
135 case TYPE_CODE_METHOD
:
137 case TYPE_CODE_COMPLEX
:
138 case TYPE_CODE_TYPEDEF
:
139 /* These types need no prefix. They are listed here so that
140 gcc -Wall will reveal any types that haven't been handled. */
148 f_language::f_type_print_varspec_suffix (struct type
*type
,
149 struct ui_file
*stream
,
150 int show
, int passed_a_ptr
,
152 int arrayprint_recurse_level
,
153 bool print_rank_only
) const
155 /* No static variables are permitted as an error call may occur during
156 execution of this function. */
161 if (type
->name () && show
<= 0)
166 switch (type
->code ())
168 case TYPE_CODE_ARRAY
:
169 arrayprint_recurse_level
++;
171 if (arrayprint_recurse_level
== 1)
172 gdb_printf (stream
, "(");
174 if (type_not_associated (type
))
175 print_rank_only
= true;
176 else if (type_not_allocated (type
))
177 print_rank_only
= true;
178 else if ((TYPE_ASSOCIATED_PROP (type
)
179 && !TYPE_ASSOCIATED_PROP (type
)->is_constant ())
180 || (TYPE_ALLOCATED_PROP (type
)
181 && !TYPE_ALLOCATED_PROP (type
)->is_constant ())
182 || (TYPE_DATA_LOCATION (type
)
183 && !TYPE_DATA_LOCATION (type
)->is_constant ()))
185 /* This case exist when we ptype a typename which has the dynamic
186 properties but cannot be resolved as there is no object. */
187 print_rank_only
= true;
190 if (type
->target_type ()->code () == TYPE_CODE_ARRAY
)
191 f_type_print_varspec_suffix (type
->target_type (), stream
, 0,
192 0, 0, arrayprint_recurse_level
,
196 gdb_printf (stream
, ":");
199 LONGEST lower_bound
= f77_get_lowerbound (type
);
200 if (lower_bound
!= 1) /* Not the default. */
201 gdb_printf (stream
, "%s:", plongest (lower_bound
));
203 /* Make sure that, if we have an assumed size array, we
204 print out a warning and print the upperbound as '*'. */
206 if (type
->bounds ()->high
.kind () == PROP_UNDEFINED
)
207 gdb_printf (stream
, "*");
210 LONGEST upper_bound
= f77_get_upperbound (type
);
212 gdb_puts (plongest (upper_bound
), stream
);
216 if (type
->target_type ()->code () != TYPE_CODE_ARRAY
)
217 f_type_print_varspec_suffix (type
->target_type (), stream
, 0,
218 0, 0, arrayprint_recurse_level
,
221 if (arrayprint_recurse_level
== 1)
222 gdb_printf (stream
, ")");
224 gdb_printf (stream
, ",");
225 arrayprint_recurse_level
--;
230 f_type_print_varspec_suffix (type
->target_type (), stream
, 0, 1, 0,
231 arrayprint_recurse_level
, false);
232 gdb_printf (stream
, " )");
237 int i
, nfields
= type
->num_fields ();
239 f_type_print_varspec_suffix (type
->target_type (), stream
, 0,
241 arrayprint_recurse_level
, false);
243 gdb_printf (stream
, ") ");
244 gdb_printf (stream
, "(");
245 if (nfields
== 0 && type
->is_prototyped ())
246 print_type (builtin_f_type (type
->arch ())->builtin_void
,
247 "", stream
, -1, 0, 0);
249 for (i
= 0; i
< nfields
; i
++)
253 gdb_puts (", ", stream
);
254 stream
->wrap_here (4);
256 print_type (type
->field (i
).type (), "", stream
, -1, 0, 0);
258 gdb_printf (stream
, ")");
262 case TYPE_CODE_UNDEF
:
263 case TYPE_CODE_STRUCT
:
264 case TYPE_CODE_UNION
:
265 case TYPE_CODE_NAMELIST
:
270 case TYPE_CODE_ERROR
:
274 case TYPE_CODE_RANGE
:
275 case TYPE_CODE_STRING
:
276 case TYPE_CODE_METHOD
:
277 case TYPE_CODE_COMPLEX
:
278 case TYPE_CODE_TYPEDEF
:
279 /* These types do not need a suffix. They are listed so that
280 gcc -Wall will report types that may not have been considered. */
288 f_language::f_type_print_derivation_info (struct type
*type
,
289 struct ui_file
*stream
) const
291 /* Fortran doesn't support multiple inheritance. */
294 if (TYPE_N_BASECLASSES (type
) > 0)
295 gdb_printf (stream
, ", extends(%s) ::", TYPE_BASECLASS (type
, i
)->name ());
301 f_language::f_type_print_base (struct type
*type
, struct ui_file
*stream
,
302 int show
, int level
) const
308 stream
->wrap_here (4);
311 fputs_styled ("<type unknown>", metadata_style
.style (), stream
);
315 /* When SHOW is zero or less, and there is a valid type name, then always
316 just print the type name directly from the type. */
318 if ((show
<= 0) && (type
->name () != NULL
))
320 const char *prefix
= "";
321 if (type
->code () == TYPE_CODE_UNION
)
322 prefix
= "Type, C_Union :: ";
323 else if (type
->code () == TYPE_CODE_STRUCT
324 || type
->code () == TYPE_CODE_NAMELIST
)
326 gdb_printf (stream
, "%*s%s%s", level
, "", prefix
, type
->name ());
330 if (type
->code () != TYPE_CODE_TYPEDEF
)
331 type
= check_typedef (type
);
333 switch (type
->code ())
335 case TYPE_CODE_TYPEDEF
:
336 f_type_print_base (type
->target_type (), stream
, 0, level
);
339 case TYPE_CODE_ARRAY
:
340 f_type_print_base (type
->target_type (), stream
, show
, level
);
343 if (type
->target_type () == NULL
)
344 type_print_unknown_return_type (stream
);
346 f_type_print_base (type
->target_type (), stream
, show
, level
);
350 gdb_printf (stream
, "%*sPTR TO -> ( ", level
, "");
351 f_type_print_base (type
->target_type (), stream
, show
, 0);
355 gdb_printf (stream
, "%*sREF TO -> ( ", level
, "");
356 f_type_print_base (type
->target_type (), stream
, show
, 0);
361 struct type
*void_type
= builtin_f_type (type
->arch ())->builtin_void
;
362 gdb_printf (stream
, "%*s%s", level
, "", void_type
->name ());
366 case TYPE_CODE_UNDEF
:
367 gdb_printf (stream
, "%*sstruct <unknown>", level
, "");
370 case TYPE_CODE_ERROR
:
371 gdb_printf (stream
, "%*s%s", level
, "", TYPE_ERROR_NAME (type
));
374 case TYPE_CODE_RANGE
:
375 /* This should not occur. */
376 gdb_printf (stream
, "%*s<range type>", level
, "");
381 /* There may be some character types that attempt to come
382 through as TYPE_CODE_INT since dbxstclass.h is so
383 C-oriented, we must change these to "character" from "char". */
385 if (strcmp (type
->name (), "char") == 0)
386 gdb_printf (stream
, "%*scharacter", level
, "");
391 case TYPE_CODE_STRING
:
392 /* Strings may have dynamic upperbounds (lengths) like arrays. We
393 check specifically for the PROP_CONST case to indicate that the
394 dynamic type has been resolved. If we arrive here having been
395 asked to print the type of a value with a dynamic type then the
396 bounds will not have been resolved. */
398 if (type
->bounds ()->high
.is_constant ())
400 LONGEST upper_bound
= f77_get_upperbound (type
);
402 gdb_printf (stream
, "character*%s", pulongest (upper_bound
));
405 gdb_printf (stream
, "%*scharacter*(*)", level
, "");
408 case TYPE_CODE_STRUCT
:
409 case TYPE_CODE_UNION
:
410 case TYPE_CODE_NAMELIST
:
411 if (type
->code () == TYPE_CODE_UNION
)
412 gdb_printf (stream
, "%*sType, C_Union ::", level
, "");
414 gdb_printf (stream
, "%*sType", level
, "");
417 f_type_print_derivation_info (type
, stream
);
419 gdb_puts (" ", stream
);
421 gdb_puts (type
->name (), stream
);
423 /* According to the definition,
424 we only print structure elements in case show > 0. */
427 gdb_puts ("\n", stream
);
428 for (index
= 0; index
< type
->num_fields (); index
++)
430 f_type_print_base (type
->field (index
).type (), stream
,
431 show
- 1, level
+ 4);
432 gdb_puts (" :: ", stream
);
433 fputs_styled (type
->field (index
).name (),
434 variable_name_style
.style (), stream
);
435 f_type_print_varspec_suffix (type
->field (index
).type (),
436 stream
, show
- 1, 0, 0, 0, false);
437 gdb_puts ("\n", stream
);
439 gdb_printf (stream
, "%*sEnd Type ", level
, "");
440 gdb_puts (type
->name (), stream
);
444 case TYPE_CODE_MODULE
:
445 gdb_printf (stream
, "%*smodule %s", level
, "", type
->name ());
450 /* Handle types not explicitly handled by the other cases,
451 such as fundamental types. For these, just print whatever
452 the type name is, as recorded in the type itself. If there
453 is no type name, then complain. */
454 if (type
->name () != NULL
)
455 gdb_printf (stream
, "%*s%s", level
, "", type
->name ());
457 error (_("Invalid type code (%d) in symbol table."), type
->code ());
461 if (TYPE_IS_ALLOCATABLE (type
))
462 gdb_printf (stream
, ", allocatable");