1 /* Support for printing Fortran types for GDB, the GNU debugger.
3 Copyright (C) 1986-2020 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/>. */
24 #include "gdb_obstack.h"
28 #include "expression.h"
33 #include "typeprint.h"
34 #include "cli/cli-style.h"
36 #if 0 /* Currently unused. */
37 static void f_type_print_args (struct type
*, struct ui_file
*);
40 static void f_type_print_varspec_suffix (struct type
*, struct ui_file
*, int,
43 void f_type_print_varspec_prefix (struct type
*, struct ui_file
*,
46 void f_type_print_base (struct type
*, struct ui_file
*, int, int);
49 /* See documentation in f-lang.h. */
52 f_print_typedef (struct type
*type
, struct symbol
*new_symbol
,
53 struct ui_file
*stream
)
55 type
= check_typedef (type
);
56 f_print_type (type
, "", stream
, 0, 0, &type_print_raw_options
);
59 /* LEVEL is the depth to indent lines by. */
62 f_print_type (struct type
*type
, const char *varstring
, struct ui_file
*stream
,
63 int show
, int level
, const struct type_print_options
*flags
)
67 f_type_print_base (type
, stream
, show
, level
);
68 code
= TYPE_CODE (type
);
69 if ((varstring
!= NULL
&& *varstring
!= '\0')
70 /* Need a space if going to print stars or brackets; but not if we
71 will print just a type name. */
73 || TYPE_NAME (type
) == 0)
74 && (code
== TYPE_CODE_FUNC
75 || code
== TYPE_CODE_METHOD
76 || code
== TYPE_CODE_ARRAY
77 || ((code
== TYPE_CODE_PTR
78 || code
== TYPE_CODE_REF
)
79 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_FUNC
80 || (TYPE_CODE (TYPE_TARGET_TYPE (type
))
82 || (TYPE_CODE (TYPE_TARGET_TYPE (type
))
83 == TYPE_CODE_ARRAY
))))))
84 fputs_filtered (" ", stream
);
85 f_type_print_varspec_prefix (type
, stream
, show
, 0);
87 if (varstring
!= NULL
)
91 fputs_filtered (varstring
, stream
);
93 /* For demangled function names, we have the arglist as part of the name,
94 so don't print an additional pair of ()'s. */
96 demangled_args
= (*varstring
!= '\0'
97 && varstring
[strlen (varstring
) - 1] == ')');
98 f_type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
, 0, false);
102 /* Print any asterisks or open-parentheses needed before the
103 variable name (to describe its type).
105 On outermost call, pass 0 for PASSED_A_PTR.
106 On outermost call, SHOW > 0 means should ignore
107 any typename for TYPE and show its details.
108 SHOW is always zero on recursive calls. */
111 f_type_print_varspec_prefix (struct type
*type
, struct ui_file
*stream
,
112 int show
, int passed_a_ptr
)
117 if (TYPE_NAME (type
) && show
<= 0)
122 switch (TYPE_CODE (type
))
125 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
129 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
131 fprintf_filtered (stream
, "(");
134 case TYPE_CODE_ARRAY
:
135 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
138 case TYPE_CODE_UNDEF
:
139 case TYPE_CODE_STRUCT
:
140 case TYPE_CODE_UNION
:
145 case TYPE_CODE_ERROR
:
149 case TYPE_CODE_RANGE
:
150 case TYPE_CODE_STRING
:
151 case TYPE_CODE_METHOD
:
153 case TYPE_CODE_COMPLEX
:
154 case TYPE_CODE_TYPEDEF
:
155 /* These types need no prefix. They are listed here so that
156 gcc -Wall will reveal any types that haven't been handled. */
161 /* Print any array sizes, function arguments or close parentheses
162 needed after the variable name (to describe its type).
163 Args work like c_type_print_varspec_prefix.
165 PRINT_RANK_ONLY is true when TYPE is an array which should be printed
166 without the upper and lower bounds being specified, this will occur
167 when the array is not allocated or not associated and so there are no
168 known upper or lower bounds. */
171 f_type_print_varspec_suffix (struct type
*type
, struct ui_file
*stream
,
172 int show
, int passed_a_ptr
, int demangled_args
,
173 int arrayprint_recurse_level
, bool print_rank_only
)
175 /* No static variables are permitted as an error call may occur during
176 execution of this function. */
181 if (TYPE_NAME (type
) && show
<= 0)
186 switch (TYPE_CODE (type
))
188 case TYPE_CODE_ARRAY
:
189 arrayprint_recurse_level
++;
191 if (arrayprint_recurse_level
== 1)
192 fprintf_filtered (stream
, "(");
194 if (type_not_associated (type
))
195 print_rank_only
= true;
196 else if (type_not_allocated (type
))
197 print_rank_only
= true;
198 else if ((TYPE_ASSOCIATED_PROP (type
)
199 && PROP_CONST
!= TYPE_DYN_PROP_KIND (TYPE_ASSOCIATED_PROP (type
)))
200 || (TYPE_ALLOCATED_PROP (type
)
201 && PROP_CONST
!= TYPE_DYN_PROP_KIND (TYPE_ALLOCATED_PROP (type
)))
202 || (TYPE_DATA_LOCATION (type
)
203 && PROP_CONST
!= TYPE_DYN_PROP_KIND (TYPE_DATA_LOCATION (type
))))
205 /* This case exist when we ptype a typename which has the dynamic
206 properties but cannot be resolved as there is no object. */
207 print_rank_only
= true;
210 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
)
211 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
212 0, 0, arrayprint_recurse_level
,
216 fprintf_filtered (stream
, ":");
219 LONGEST lower_bound
= f77_get_lowerbound (type
);
220 if (lower_bound
!= 1) /* Not the default. */
221 fprintf_filtered (stream
, "%s:", plongest (lower_bound
));
223 /* Make sure that, if we have an assumed size array, we
224 print out a warning and print the upperbound as '*'. */
226 if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
227 fprintf_filtered (stream
, "*");
230 LONGEST upper_bound
= f77_get_upperbound (type
);
232 fputs_filtered (plongest (upper_bound
), stream
);
236 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_ARRAY
)
237 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
238 0, 0, arrayprint_recurse_level
,
241 if (arrayprint_recurse_level
== 1)
242 fprintf_filtered (stream
, ")");
244 fprintf_filtered (stream
, ",");
245 arrayprint_recurse_level
--;
250 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 1, 0,
251 arrayprint_recurse_level
, false);
252 fprintf_filtered (stream
, " )");
257 int i
, nfields
= TYPE_NFIELDS (type
);
259 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
261 arrayprint_recurse_level
, false);
263 fprintf_filtered (stream
, ") ");
264 fprintf_filtered (stream
, "(");
265 if (nfields
== 0 && TYPE_PROTOTYPED (type
))
266 f_print_type (builtin_f_type (get_type_arch (type
))->builtin_void
,
267 "", stream
, -1, 0, 0);
269 for (i
= 0; i
< nfields
; i
++)
273 fputs_filtered (", ", stream
);
276 f_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0, 0);
278 fprintf_filtered (stream
, ")");
282 case TYPE_CODE_UNDEF
:
283 case TYPE_CODE_STRUCT
:
284 case TYPE_CODE_UNION
:
289 case TYPE_CODE_ERROR
:
293 case TYPE_CODE_RANGE
:
294 case TYPE_CODE_STRING
:
295 case TYPE_CODE_METHOD
:
296 case TYPE_CODE_COMPLEX
:
297 case TYPE_CODE_TYPEDEF
:
298 /* These types do not need a suffix. They are listed so that
299 gcc -Wall will report types that may not have been considered. */
304 /* Print the name of the type (or the ultimate pointer target,
305 function value or array element), or the description of a
308 SHOW nonzero means don't print this type as just its name;
309 show its real definition even if it has a name.
310 SHOW zero means print just typename or struct tag if there is one
311 SHOW negative means abbreviate structure elements.
312 SHOW is decremented for printing of structure elements.
314 LEVEL is the depth to indent by.
315 We increase it for some recursive calls. */
318 f_type_print_base (struct type
*type
, struct ui_file
*stream
, int show
,
328 fputs_styled ("<type unknown>", metadata_style
.style (), stream
);
332 /* When SHOW is zero or less, and there is a valid type name, then always
333 just print the type name directly from the type. */
335 if ((show
<= 0) && (TYPE_NAME (type
) != NULL
))
337 const char *prefix
= "";
338 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
339 prefix
= "Type, C_Union :: ";
340 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
342 fprintfi_filtered (level
, stream
, "%s%s", prefix
, TYPE_NAME (type
));
346 if (TYPE_CODE (type
) != TYPE_CODE_TYPEDEF
)
347 type
= check_typedef (type
);
349 switch (TYPE_CODE (type
))
351 case TYPE_CODE_TYPEDEF
:
352 f_type_print_base (TYPE_TARGET_TYPE (type
), stream
, 0, level
);
355 case TYPE_CODE_ARRAY
:
356 f_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
359 if (TYPE_TARGET_TYPE (type
) == NULL
)
360 type_print_unknown_return_type (stream
);
362 f_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
366 fprintfi_filtered (level
, stream
, "PTR TO -> ( ");
367 f_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0);
371 fprintfi_filtered (level
, stream
, "REF TO -> ( ");
372 f_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, 0);
377 gdbarch
*gdbarch
= get_type_arch (type
);
378 struct type
*void_type
= builtin_f_type (gdbarch
)->builtin_void
;
379 fprintfi_filtered (level
, stream
, "%s", TYPE_NAME (void_type
));
383 case TYPE_CODE_UNDEF
:
384 fprintfi_filtered (level
, stream
, "struct <unknown>");
387 case TYPE_CODE_ERROR
:
388 fprintfi_filtered (level
, stream
, "%s", TYPE_ERROR_NAME (type
));
391 case TYPE_CODE_RANGE
:
392 /* This should not occur. */
393 fprintfi_filtered (level
, stream
, "<range type>");
398 /* There may be some character types that attempt to come
399 through as TYPE_CODE_INT since dbxstclass.h is so
400 C-oriented, we must change these to "character" from "char". */
402 if (strcmp (TYPE_NAME (type
), "char") == 0)
403 fprintfi_filtered (level
, stream
, "character");
408 case TYPE_CODE_STRING
:
409 /* Strings may have dynamic upperbounds (lengths) like arrays. */
411 if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
412 fprintfi_filtered (level
, stream
, "character*(*)");
415 LONGEST upper_bound
= f77_get_upperbound (type
);
417 fprintf_filtered (stream
, "character*%s", pulongest (upper_bound
));
421 case TYPE_CODE_STRUCT
:
422 case TYPE_CODE_UNION
:
423 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
424 fprintfi_filtered (level
, stream
, "Type, C_Union :: ");
426 fprintfi_filtered (level
, stream
, "Type ");
427 fputs_filtered (TYPE_NAME (type
), stream
);
428 /* According to the definition,
429 we only print structure elements in case show > 0. */
432 fputs_filtered ("\n", stream
);
433 for (index
= 0; index
< TYPE_NFIELDS (type
); index
++)
435 f_type_print_base (TYPE_FIELD_TYPE (type
, index
), stream
,
436 show
- 1, level
+ 4);
437 fputs_filtered (" :: ", stream
);
438 fputs_filtered (TYPE_FIELD_NAME (type
, index
), stream
);
439 f_type_print_varspec_suffix (TYPE_FIELD_TYPE (type
, index
),
440 stream
, show
- 1, 0, 0, 0, false);
441 fputs_filtered ("\n", stream
);
443 fprintfi_filtered (level
, stream
, "End Type ");
444 fputs_filtered (TYPE_NAME (type
), stream
);
448 case TYPE_CODE_MODULE
:
449 fprintfi_filtered (level
, stream
, "module %s", TYPE_NAME (type
));
454 /* Handle types not explicitly handled by the other cases,
455 such as fundamental types. For these, just print whatever
456 the type name is, as recorded in the type itself. If there
457 is no type name, then complain. */
458 if (TYPE_NAME (type
) != NULL
)
459 fprintfi_filtered (level
, stream
, "%s", TYPE_NAME (type
));
461 error (_("Invalid type code (%d) in symbol table."), TYPE_CODE (type
));
465 if (TYPE_IS_ALLOCATABLE (type
))
466 fprintf_filtered (stream
, ", allocatable");