s390-vregs.exp: Avoid compile errors with older GCCs and on 31-bit targets
[binutils-gdb.git] / gdb / f-typeprint.c
blob4957e1f9b9dedfa514ee6c1ce44a5d3995fe3948
1 /* Support for printing Fortran types for GDB, the GNU debugger.
3 Copyright (C) 1986-2015 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 "defs.h"
24 #include "gdb_obstack.h"
25 #include "bfd.h"
26 #include "symtab.h"
27 #include "gdbtypes.h"
28 #include "expression.h"
29 #include "value.h"
30 #include "gdbcore.h"
31 #include "target.h"
32 #include "f-lang.h"
34 #if 0 /* Currently unused. */
35 static void f_type_print_args (struct type *, struct ui_file *);
36 #endif
38 static void f_type_print_varspec_suffix (struct type *, struct ui_file *, int,
39 int, int, int);
41 void f_type_print_varspec_prefix (struct type *, struct ui_file *,
42 int, int);
44 void f_type_print_base (struct type *, struct ui_file *, int, int);
47 /* LEVEL is the depth to indent lines by. */
49 void
50 f_print_type (struct type *type, const char *varstring, struct ui_file *stream,
51 int show, int level, const struct type_print_options *flags)
53 enum type_code code;
54 int demangled_args;
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;
60 but not if we will print just a type name. */
61 || ((show > 0 || TYPE_NAME (type) == 0)
62 && (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
63 || code == TYPE_CODE_METHOD
64 || code == TYPE_CODE_ARRAY
65 || code == TYPE_CODE_REF)))
66 fputs_filtered (" ", stream);
67 f_type_print_varspec_prefix (type, stream, show, 0);
69 if (varstring != NULL)
71 fputs_filtered (varstring, stream);
73 /* For demangled function names, we have the arglist as part of the name,
74 so don't print an additional pair of ()'s. */
76 demangled_args = varstring[strlen (varstring) - 1] == ')';
77 f_type_print_varspec_suffix (type, stream, show, 0, demangled_args, 0);
81 /* Print any asterisks or open-parentheses needed before the
82 variable name (to describe its type).
84 On outermost call, pass 0 for PASSED_A_PTR.
85 On outermost call, SHOW > 0 means should ignore
86 any typename for TYPE and show its details.
87 SHOW is always zero on recursive calls. */
89 void
90 f_type_print_varspec_prefix (struct type *type, struct ui_file *stream,
91 int show, int passed_a_ptr)
93 if (type == 0)
94 return;
96 if (TYPE_NAME (type) && show <= 0)
97 return;
99 QUIT;
101 switch (TYPE_CODE (type))
103 case TYPE_CODE_PTR:
104 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
105 break;
107 case TYPE_CODE_FUNC:
108 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
109 if (passed_a_ptr)
110 fprintf_filtered (stream, "(");
111 break;
113 case TYPE_CODE_ARRAY:
114 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
115 break;
117 case TYPE_CODE_UNDEF:
118 case TYPE_CODE_STRUCT:
119 case TYPE_CODE_UNION:
120 case TYPE_CODE_ENUM:
121 case TYPE_CODE_INT:
122 case TYPE_CODE_FLT:
123 case TYPE_CODE_VOID:
124 case TYPE_CODE_ERROR:
125 case TYPE_CODE_CHAR:
126 case TYPE_CODE_BOOL:
127 case TYPE_CODE_SET:
128 case TYPE_CODE_RANGE:
129 case TYPE_CODE_STRING:
130 case TYPE_CODE_METHOD:
131 case TYPE_CODE_REF:
132 case TYPE_CODE_COMPLEX:
133 case TYPE_CODE_TYPEDEF:
134 /* These types need no prefix. They are listed here so that
135 gcc -Wall will reveal any types that haven't been handled. */
136 break;
140 /* Print any array sizes, function arguments or close parentheses
141 needed after the variable name (to describe its type).
142 Args work like c_type_print_varspec_prefix. */
144 static void
145 f_type_print_varspec_suffix (struct type *type, struct ui_file *stream,
146 int show, int passed_a_ptr, int demangled_args,
147 int arrayprint_recurse_level)
149 int upper_bound, lower_bound;
151 /* No static variables are permitted as an error call may occur during
152 execution of this function. */
154 if (type == 0)
155 return;
157 if (TYPE_NAME (type) && show <= 0)
158 return;
160 QUIT;
162 switch (TYPE_CODE (type))
164 case TYPE_CODE_ARRAY:
165 arrayprint_recurse_level++;
167 if (arrayprint_recurse_level == 1)
168 fprintf_filtered (stream, "(");
170 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY)
171 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0,
172 arrayprint_recurse_level);
174 lower_bound = f77_get_lowerbound (type);
175 if (lower_bound != 1) /* Not the default. */
176 fprintf_filtered (stream, "%d:", lower_bound);
178 /* Make sure that, if we have an assumed size array, we
179 print out a warning and print the upperbound as '*'. */
181 if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
182 fprintf_filtered (stream, "*");
183 else
185 upper_bound = f77_get_upperbound (type);
186 fprintf_filtered (stream, "%d", upper_bound);
189 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_ARRAY)
190 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0,
191 arrayprint_recurse_level);
192 if (arrayprint_recurse_level == 1)
193 fprintf_filtered (stream, ")");
194 else
195 fprintf_filtered (stream, ",");
196 arrayprint_recurse_level--;
197 break;
199 case TYPE_CODE_PTR:
200 case TYPE_CODE_REF:
201 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0,
202 arrayprint_recurse_level);
203 fprintf_filtered (stream, ")");
204 break;
206 case TYPE_CODE_FUNC:
207 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
208 passed_a_ptr, 0, arrayprint_recurse_level);
209 if (passed_a_ptr)
210 fprintf_filtered (stream, ")");
212 fprintf_filtered (stream, "()");
213 break;
215 case TYPE_CODE_UNDEF:
216 case TYPE_CODE_STRUCT:
217 case TYPE_CODE_UNION:
218 case TYPE_CODE_ENUM:
219 case TYPE_CODE_INT:
220 case TYPE_CODE_FLT:
221 case TYPE_CODE_VOID:
222 case TYPE_CODE_ERROR:
223 case TYPE_CODE_CHAR:
224 case TYPE_CODE_BOOL:
225 case TYPE_CODE_SET:
226 case TYPE_CODE_RANGE:
227 case TYPE_CODE_STRING:
228 case TYPE_CODE_METHOD:
229 case TYPE_CODE_COMPLEX:
230 case TYPE_CODE_TYPEDEF:
231 /* These types do not need a suffix. They are listed so that
232 gcc -Wall will report types that may not have been considered. */
233 break;
237 /* Print the name of the type (or the ultimate pointer target,
238 function value or array element), or the description of a
239 structure or union.
241 SHOW nonzero means don't print this type as just its name;
242 show its real definition even if it has a name.
243 SHOW zero means print just typename or struct tag if there is one
244 SHOW negative means abbreviate structure elements.
245 SHOW is decremented for printing of structure elements.
247 LEVEL is the depth to indent by.
248 We increase it for some recursive calls. */
250 void
251 f_type_print_base (struct type *type, struct ui_file *stream, int show,
252 int level)
254 int upper_bound;
255 int index;
257 QUIT;
259 wrap_here (" ");
260 if (type == NULL)
262 fputs_filtered ("<type unknown>", stream);
263 return;
266 /* When SHOW is zero or less, and there is a valid type name, then always
267 just print the type name directly from the type. */
269 if ((show <= 0) && (TYPE_NAME (type) != NULL))
271 fputs_filtered (TYPE_NAME (type), stream);
272 return;
275 if (TYPE_CODE (type) != TYPE_CODE_TYPEDEF)
276 CHECK_TYPEDEF (type);
278 switch (TYPE_CODE (type))
280 case TYPE_CODE_TYPEDEF:
281 f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
282 break;
284 case TYPE_CODE_ARRAY:
285 case TYPE_CODE_FUNC:
286 f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
287 break;
289 case TYPE_CODE_PTR:
290 fprintf_filtered (stream, "PTR TO -> ( ");
291 f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
292 break;
294 case TYPE_CODE_REF:
295 fprintf_filtered (stream, "REF TO -> ( ");
296 f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
297 break;
299 case TYPE_CODE_VOID:
300 fprintfi_filtered (level, stream, "VOID");
301 break;
303 case TYPE_CODE_UNDEF:
304 fprintfi_filtered (level, stream, "struct <unknown>");
305 break;
307 case TYPE_CODE_ERROR:
308 fprintfi_filtered (level, stream, "%s", TYPE_ERROR_NAME (type));
309 break;
311 case TYPE_CODE_RANGE:
312 /* This should not occur. */
313 fprintfi_filtered (level, stream, "<range type>");
314 break;
316 case TYPE_CODE_CHAR:
317 case TYPE_CODE_INT:
318 /* There may be some character types that attempt to come
319 through as TYPE_CODE_INT since dbxstclass.h is so
320 C-oriented, we must change these to "character" from "char". */
322 if (strcmp (TYPE_NAME (type), "char") == 0)
323 fprintfi_filtered (level, stream, "character");
324 else
325 goto default_case;
326 break;
328 case TYPE_CODE_STRING:
329 /* Strings may have dynamic upperbounds (lengths) like arrays. */
331 if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
332 fprintfi_filtered (level, stream, "character*(*)");
333 else
335 upper_bound = f77_get_upperbound (type);
336 fprintf_filtered (stream, "character*%d", upper_bound);
338 break;
340 case TYPE_CODE_STRUCT:
341 case TYPE_CODE_UNION:
342 if (TYPE_CODE (type) == TYPE_CODE_UNION)
343 fprintfi_filtered (level, stream, "Type, C_Union :: ");
344 else
345 fprintfi_filtered (level, stream, "Type ");
346 fputs_filtered (TYPE_TAG_NAME (type), stream);
347 fputs_filtered ("\n", stream);
348 for (index = 0; index < TYPE_NFIELDS (type); index++)
350 f_type_print_base (TYPE_FIELD_TYPE (type, index), stream, show,
351 level + 4);
352 fputs_filtered (" :: ", stream);
353 fputs_filtered (TYPE_FIELD_NAME (type, index), stream);
354 f_type_print_varspec_suffix (TYPE_FIELD_TYPE (type, index),
355 stream, 0, 0, 0, 0);
356 fputs_filtered ("\n", stream);
358 fprintfi_filtered (level, stream, "End Type ");
359 fputs_filtered (TYPE_TAG_NAME (type), stream);
360 break;
362 case TYPE_CODE_MODULE:
363 fprintfi_filtered (level, stream, "module %s", TYPE_TAG_NAME (type));
364 break;
366 default_case:
367 default:
368 /* Handle types not explicitly handled by the other cases,
369 such as fundamental types. For these, just print whatever
370 the type name is, as recorded in the type itself. If there
371 is no type name, then complain. */
372 if (TYPE_NAME (type) != NULL)
373 fprintfi_filtered (level, stream, "%s", TYPE_NAME (type));
374 else
375 error (_("Invalid type code (%d) in symbol table."), TYPE_CODE (type));
376 break;