1 /* Support for printing Fortran types for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 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"
35 #if 0 /* Currently unused. */
36 static void f_type_print_args (struct type *, struct ui_file *);
39 static void f_type_print_varspec_suffix (struct type *, struct ui_file *, int,
42 void f_type_print_varspec_prefix (struct type *, struct ui_file *,
45 void f_type_print_base (struct type *, struct ui_file *, int, int);
48 /* LEVEL is the depth to indent lines by. */
51 f_print_type (struct type *type, const char *varstring, struct ui_file *stream,
52 int show, int level, const struct type_print_options *flags)
56 f_type_print_base (type, stream, show, level);
57 code = TYPE_CODE (type);
58 if ((varstring != NULL && *varstring != '\0')
59 /* Need a space if going to print stars or brackets; but not if we
60 will print just a type name. */
62 || TYPE_NAME (type) == 0)
63 && (code == TYPE_CODE_FUNC
64 || code == TYPE_CODE_METHOD
65 || code == TYPE_CODE_ARRAY
66 || ((code == TYPE_CODE_PTR
67 || code == TYPE_CODE_REF)
68 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
69 || (TYPE_CODE (TYPE_TARGET_TYPE (type))
71 || (TYPE_CODE (TYPE_TARGET_TYPE (type))
72 == TYPE_CODE_ARRAY))))))
73 fputs_filtered (" ", stream);
74 f_type_print_varspec_prefix (type, stream, show, 0);
76 if (varstring != NULL)
80 fputs_filtered (varstring, stream);
82 /* For demangled function names, we have the arglist as part of the name,
83 so don't print an additional pair of ()'s. */
85 demangled_args = (*varstring != '\0'
86 && varstring[strlen (varstring) - 1] == ')');
87 f_type_print_varspec_suffix (type, stream, show, 0, demangled_args, 0, false);
91 /* Print any asterisks or open-parentheses needed before the
92 variable name (to describe its type).
94 On outermost call, pass 0 for PASSED_A_PTR.
95 On outermost call, SHOW > 0 means should ignore
96 any typename for TYPE and show its details.
97 SHOW is always zero on recursive calls. */
100 f_type_print_varspec_prefix (struct type *type, struct ui_file *stream,
101 int show, int passed_a_ptr)
106 if (TYPE_NAME (type) && show <= 0)
111 switch (TYPE_CODE (type))
114 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
118 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
120 fprintf_filtered (stream, "(");
123 case TYPE_CODE_ARRAY:
124 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
127 case TYPE_CODE_UNDEF:
128 case TYPE_CODE_STRUCT:
129 case TYPE_CODE_UNION:
134 case TYPE_CODE_ERROR:
138 case TYPE_CODE_RANGE:
139 case TYPE_CODE_STRING:
140 case TYPE_CODE_METHOD:
142 case TYPE_CODE_COMPLEX:
143 case TYPE_CODE_TYPEDEF:
144 /* These types need no prefix. They are listed here so that
145 gcc -Wall will reveal any types that haven't been handled. */
150 /* Print any array sizes, function arguments or close parentheses
151 needed after the variable name (to describe its type).
152 Args work like c_type_print_varspec_prefix.
154 PRINT_RANK_ONLY is true when TYPE is an array which should be printed
155 without the upper and lower bounds being specified, this will occur
156 when the array is not allocated or not associated and so there are no
157 known upper or lower bounds. */
160 f_type_print_varspec_suffix (struct type *type, struct ui_file *stream,
161 int show, int passed_a_ptr, int demangled_args,
162 int arrayprint_recurse_level, bool print_rank_only)
164 /* No static variables are permitted as an error call may occur during
165 execution of this function. */
170 if (TYPE_NAME (type) && show <= 0)
175 switch (TYPE_CODE (type))
177 case TYPE_CODE_ARRAY:
178 arrayprint_recurse_level++;
180 if (arrayprint_recurse_level == 1)
181 fprintf_filtered (stream, "(");
183 if (type_not_associated (type))
184 print_rank_only = true;
185 else if (type_not_allocated (type))
186 print_rank_only = true;
187 else if ((TYPE_ASSOCIATED_PROP (type)
188 && PROP_CONST != TYPE_DYN_PROP_KIND (TYPE_ASSOCIATED_PROP (type)))
189 || (TYPE_ALLOCATED_PROP (type)
190 && PROP_CONST != TYPE_DYN_PROP_KIND (TYPE_ALLOCATED_PROP (type)))
191 || (TYPE_DATA_LOCATION (type)
192 && PROP_CONST != TYPE_DYN_PROP_KIND (TYPE_DATA_LOCATION (type))))
194 /* This case exist when we ptype a typename which has the dynamic
195 properties but cannot be resolved as there is no object. */
196 print_rank_only = true;
199 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY)
200 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
201 0, 0, arrayprint_recurse_level,
205 fprintf_filtered (stream, ":");
208 LONGEST lower_bound = f77_get_lowerbound (type);
209 if (lower_bound != 1) /* Not the default. */
210 fprintf_filtered (stream, "%s:", plongest (lower_bound));
212 /* Make sure that, if we have an assumed size array, we
213 print out a warning and print the upperbound as '*'. */
215 if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
216 fprintf_filtered (stream, "*");
219 LONGEST upper_bound = f77_get_upperbound (type);
221 fputs_filtered (plongest (upper_bound), stream);
225 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_ARRAY)
226 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
227 0, 0, arrayprint_recurse_level,
230 if (arrayprint_recurse_level == 1)
231 fprintf_filtered (stream, ")");
233 fprintf_filtered (stream, ",");
234 arrayprint_recurse_level--;
239 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0,
240 arrayprint_recurse_level, false);
241 fprintf_filtered (stream, " )");
246 int i, nfields = TYPE_NFIELDS (type);
248 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
250 arrayprint_recurse_level, false);
252 fprintf_filtered (stream, ") ");
253 fprintf_filtered (stream, "(");
254 if (nfields == 0 && TYPE_PROTOTYPED (type))
255 f_print_type (builtin_f_type (get_type_arch (type))->builtin_void,
256 "", stream, -1, 0, 0);
258 for (i = 0; i < nfields; i++)
262 fputs_filtered (", ", stream);
265 f_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0, 0);
267 fprintf_filtered (stream, ")");
271 case TYPE_CODE_UNDEF:
272 case TYPE_CODE_STRUCT:
273 case TYPE_CODE_UNION:
278 case TYPE_CODE_ERROR:
282 case TYPE_CODE_RANGE:
283 case TYPE_CODE_STRING:
284 case TYPE_CODE_METHOD:
285 case TYPE_CODE_COMPLEX:
286 case TYPE_CODE_TYPEDEF:
287 /* These types do not need a suffix. They are listed so that
288 gcc -Wall will report types that may not have been considered. */
293 /* Print the name of the type (or the ultimate pointer target,
294 function value or array element), or the description of a
297 SHOW nonzero means don't print this type as just its name;
298 show its real definition even if it has a name.
299 SHOW zero means print just typename or struct tag if there is one
300 SHOW negative means abbreviate structure elements.
301 SHOW is decremented for printing of structure elements.
303 LEVEL is the depth to indent by.
304 We increase it for some recursive calls. */
307 f_type_print_base (struct type *type, struct ui_file *stream, int show,
317 fputs_filtered ("<type unknown>", stream);
321 /* When SHOW is zero or less, and there is a valid type name, then always
322 just print the type name directly from the type. */
324 if ((show <= 0) && (TYPE_NAME (type) != NULL))
326 const char *prefix = "";
327 if (TYPE_CODE (type) == TYPE_CODE_UNION)
328 prefix = "Type, C_Union :: ";
329 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
331 fprintfi_filtered (level, stream, "%s%s", prefix, TYPE_NAME (type));
335 if (TYPE_CODE (type) != TYPE_CODE_TYPEDEF)
336 type = check_typedef (type);
338 switch (TYPE_CODE (type))
340 case TYPE_CODE_TYPEDEF:
341 f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
344 case TYPE_CODE_ARRAY:
345 f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
348 if (TYPE_TARGET_TYPE (type) == NULL)
349 type_print_unknown_return_type (stream);
351 f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
355 fprintfi_filtered (level, stream, "PTR TO -> ( ");
356 f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, 0);
360 fprintfi_filtered (level, stream, "REF TO -> ( ");
361 f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, 0);
366 gdbarch *gdbarch = get_type_arch (type);
367 struct type *void_type = builtin_f_type (gdbarch)->builtin_void;
368 fprintfi_filtered (level, stream, "%s", TYPE_NAME (void_type));
372 case TYPE_CODE_UNDEF:
373 fprintfi_filtered (level, stream, "struct <unknown>");
376 case TYPE_CODE_ERROR:
377 fprintfi_filtered (level, stream, "%s", TYPE_ERROR_NAME (type));
380 case TYPE_CODE_RANGE:
381 /* This should not occur. */
382 fprintfi_filtered (level, stream, "<range type>");
387 /* There may be some character types that attempt to come
388 through as TYPE_CODE_INT since dbxstclass.h is so
389 C-oriented, we must change these to "character" from "char". */
391 if (strcmp (TYPE_NAME (type), "char") == 0)
392 fprintfi_filtered (level, stream, "character");
397 case TYPE_CODE_STRING:
398 /* Strings may have dynamic upperbounds (lengths) like arrays. */
400 if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
401 fprintfi_filtered (level, stream, "character*(*)");
404 LONGEST upper_bound = f77_get_upperbound (type);
406 fprintf_filtered (stream, "character*%s", pulongest (upper_bound));
410 case TYPE_CODE_STRUCT:
411 case TYPE_CODE_UNION:
412 if (TYPE_CODE (type) == TYPE_CODE_UNION)
413 fprintfi_filtered (level, stream, "Type, C_Union :: ");
415 fprintfi_filtered (level, stream, "Type ");
416 fputs_filtered (TYPE_NAME (type), stream);
417 /* According to the definition,
418 we only print structure elements in case show > 0. */
421 fputs_filtered ("\n", stream);
422 for (index = 0; index < TYPE_NFIELDS (type); index++)
424 f_type_print_base (TYPE_FIELD_TYPE (type, index), stream,
425 show - 1, level + 4);
426 fputs_filtered (" :: ", stream);
427 fputs_filtered (TYPE_FIELD_NAME (type, index), stream);
428 f_type_print_varspec_suffix (TYPE_FIELD_TYPE (type, index),
429 stream, show - 1, 0, 0, 0, false);
430 fputs_filtered ("\n", stream);
432 fprintfi_filtered (level, stream, "End Type ");
433 fputs_filtered (TYPE_NAME (type), stream);
437 case TYPE_CODE_MODULE:
438 fprintfi_filtered (level, stream, "module %s", TYPE_NAME (type));
443 /* Handle types not explicitly handled by the other cases,
444 such as fundamental types. For these, just print whatever
445 the type name is, as recorded in the type itself. If there
446 is no type name, then complain. */
447 if (TYPE_NAME (type) != NULL)
448 fprintfi_filtered (level, stream, "%s", TYPE_NAME (type));
450 error (_("Invalid type code (%d) in symbol table."), TYPE_CODE (type));
454 if (TYPE_IS_ALLOCATABLE (type))
455 fprintf_filtered (stream, ", allocatable");