]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/f-typeprint.c
gdb/fortran: Update rules for printing whitespace in types
[thirdparty/binutils-gdb.git] / gdb / f-typeprint.c
CommitLineData
c906108c 1/* Support for printing Fortran types for GDB, the GNU debugger.
1bac305b 2
42a4f53d 3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
1bac305b 4
c906108c
SS
5 Contributed by Motorola. Adapted from the C version by Farooq Butt
6 (fmbutt@engage.sps.mot.com).
7
c5aa993b 8 This file is part of GDB.
c906108c 9
c5aa993b
JM
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
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
c5aa993b 13 (at your option) any later version.
c906108c 14
c5aa993b
JM
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.
c906108c 19
c5aa993b 20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
22
23#include "defs.h"
4de283e4 24#include "gdb_obstack.h"
c906108c 25#include "bfd.h"
4de283e4
TT
26#include "symtab.h"
27#include "gdbtypes.h"
c906108c 28#include "expression.h"
4de283e4 29#include "value.h"
c906108c
SS
30#include "gdbcore.h"
31#include "target.h"
4de283e4 32#include "f-lang.h"
3f2f83dd 33#include "typeprint.h"
c906108c 34
0963b4bd 35#if 0 /* Currently unused. */
d9fcf2fb 36static void f_type_print_args (struct type *, struct ui_file *);
c906108c
SS
37#endif
38
0311118f 39static void f_type_print_varspec_suffix (struct type *, struct ui_file *, int,
d9fcf2fb 40 int, int, int);
c906108c 41
d9fcf2fb
JM
42void f_type_print_varspec_prefix (struct type *, struct ui_file *,
43 int, int);
c906108c 44
d9fcf2fb 45void f_type_print_base (struct type *, struct ui_file *, int, int);
c906108c 46\f
c5aa993b 47
c906108c
SS
48/* LEVEL is the depth to indent lines by. */
49
50void
25b524e8 51f_print_type (struct type *type, const char *varstring, struct ui_file *stream,
79d43c61 52 int show, int level, const struct type_print_options *flags)
c906108c 53{
52f0bd74 54 enum type_code code;
c906108c 55
3f2f83dd
KB
56 if (type_not_associated (type))
57 {
58 val_print_not_associated (stream);
59 return;
60 }
61
62 if (type_not_allocated (type))
63 {
64 val_print_not_allocated (stream);
65 return;
66 }
67
c906108c
SS
68 f_type_print_base (type, stream, show, level);
69 code = TYPE_CODE (type);
70 if ((varstring != NULL && *varstring != '\0')
f1fdc960
AB
71 /* Need a space if going to print stars or brackets; but not if we
72 will print just a type name. */
73 || ((show > 0
74 || TYPE_NAME (type) == 0)
75 && (code == TYPE_CODE_FUNC
905e0470
PM
76 || code == TYPE_CODE_METHOD
77 || code == TYPE_CODE_ARRAY
f1fdc960
AB
78 || ((code == TYPE_CODE_PTR
79 || code == TYPE_CODE_REF)
80 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
81 || (TYPE_CODE (TYPE_TARGET_TYPE (type))
82 == TYPE_CODE_METHOD)
83 || (TYPE_CODE (TYPE_TARGET_TYPE (type))
84 == TYPE_CODE_ARRAY))))))
c906108c
SS
85 fputs_filtered (" ", stream);
86 f_type_print_varspec_prefix (type, stream, show, 0);
87
a7dfd010
MD
88 if (varstring != NULL)
89 {
2123df0e
YQ
90 int demangled_args;
91
a7dfd010 92 fputs_filtered (varstring, stream);
c906108c 93
a7dfd010 94 /* For demangled function names, we have the arglist as part of the name,
0963b4bd 95 so don't print an additional pair of ()'s. */
c906108c 96
2123df0e
YQ
97 demangled_args = (*varstring != '\0'
98 && varstring[strlen (varstring) - 1] == ')');
0311118f 99 f_type_print_varspec_suffix (type, stream, show, 0, demangled_args, 0);
a7dfd010 100 }
c906108c
SS
101}
102
103/* Print any asterisks or open-parentheses needed before the
104 variable name (to describe its type).
105
106 On outermost call, pass 0 for PASSED_A_PTR.
107 On outermost call, SHOW > 0 means should ignore
108 any typename for TYPE and show its details.
109 SHOW is always zero on recursive calls. */
110
111void
fba45db2
KB
112f_type_print_varspec_prefix (struct type *type, struct ui_file *stream,
113 int show, int passed_a_ptr)
c906108c
SS
114{
115 if (type == 0)
116 return;
117
118 if (TYPE_NAME (type) && show <= 0)
119 return;
120
121 QUIT;
122
123 switch (TYPE_CODE (type))
124 {
125 case TYPE_CODE_PTR:
126 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
127 break;
128
129 case TYPE_CODE_FUNC:
130 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
131 if (passed_a_ptr)
132 fprintf_filtered (stream, "(");
133 break;
134
135 case TYPE_CODE_ARRAY:
136 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
137 break;
138
139 case TYPE_CODE_UNDEF:
140 case TYPE_CODE_STRUCT:
141 case TYPE_CODE_UNION:
142 case TYPE_CODE_ENUM:
143 case TYPE_CODE_INT:
144 case TYPE_CODE_FLT:
145 case TYPE_CODE_VOID:
146 case TYPE_CODE_ERROR:
147 case TYPE_CODE_CHAR:
148 case TYPE_CODE_BOOL:
149 case TYPE_CODE_SET:
150 case TYPE_CODE_RANGE:
151 case TYPE_CODE_STRING:
c906108c 152 case TYPE_CODE_METHOD:
c906108c
SS
153 case TYPE_CODE_REF:
154 case TYPE_CODE_COMPLEX:
155 case TYPE_CODE_TYPEDEF:
156 /* These types need no prefix. They are listed here so that
c5aa993b 157 gcc -Wall will reveal any types that haven't been handled. */
c906108c
SS
158 break;
159 }
160}
161
c906108c
SS
162/* Print any array sizes, function arguments or close parentheses
163 needed after the variable name (to describe its type).
164 Args work like c_type_print_varspec_prefix. */
165
166static void
fba45db2 167f_type_print_varspec_suffix (struct type *type, struct ui_file *stream,
0311118f
JK
168 int show, int passed_a_ptr, int demangled_args,
169 int arrayprint_recurse_level)
c906108c 170{
0311118f
JK
171 /* No static variables are permitted as an error call may occur during
172 execution of this function. */
c906108c
SS
173
174 if (type == 0)
175 return;
176
177 if (TYPE_NAME (type) && show <= 0)
178 return;
179
180 QUIT;
181
182 switch (TYPE_CODE (type))
183 {
184 case TYPE_CODE_ARRAY:
185 arrayprint_recurse_level++;
186
187 if (arrayprint_recurse_level == 1)
c5aa993b 188 fprintf_filtered (stream, "(");
c906108c 189
3f2f83dd
KB
190 if (type_not_associated (type))
191 val_print_not_associated (stream);
192 else if (type_not_allocated (type))
193 val_print_not_allocated (stream);
c5aa993b 194 else
3f2f83dd
KB
195 {
196 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY)
197 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
198 0, 0, arrayprint_recurse_level);
199
2880242d
KS
200 LONGEST lower_bound = f77_get_lowerbound (type);
201
3f2f83dd 202 if (lower_bound != 1) /* Not the default. */
2880242d 203 fprintf_filtered (stream, "%s:", plongest (lower_bound));
3f2f83dd
KB
204
205 /* Make sure that, if we have an assumed size array, we
206 print out a warning and print the upperbound as '*'. */
207
208 if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
209 fprintf_filtered (stream, "*");
210 else
211 {
2880242d
KS
212 LONGEST upper_bound = f77_get_upperbound (type);
213
214 fputs_filtered (plongest (upper_bound), stream);
3f2f83dd
KB
215 }
216
217 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_ARRAY)
218 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
219 0, 0, arrayprint_recurse_level);
220 }
c906108c
SS
221 if (arrayprint_recurse_level == 1)
222 fprintf_filtered (stream, ")");
223 else
c5aa993b 224 fprintf_filtered (stream, ",");
c906108c
SS
225 arrayprint_recurse_level--;
226 break;
227
228 case TYPE_CODE_PTR:
229 case TYPE_CODE_REF:
0311118f
JK
230 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0,
231 arrayprint_recurse_level);
f1fdc960 232 fprintf_filtered (stream, " )");
c906108c
SS
233 break;
234
235 case TYPE_CODE_FUNC:
bf7a4de1
AB
236 {
237 int i, nfields = TYPE_NFIELDS (type);
c906108c 238
bf7a4de1
AB
239 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
240 passed_a_ptr, 0, arrayprint_recurse_level);
241 if (passed_a_ptr)
f1fdc960 242 fprintf_filtered (stream, ") ");
bf7a4de1
AB
243 fprintf_filtered (stream, "(");
244 if (nfields == 0 && TYPE_PROTOTYPED (type))
245 f_print_type (builtin_f_type (get_type_arch (type))->builtin_void,
246 "", stream, -1, 0, 0);
247 else
248 for (i = 0; i < nfields; i++)
249 {
250 if (i > 0)
251 {
252 fputs_filtered (", ", stream);
253 wrap_here (" ");
254 }
255 f_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0, 0);
256 }
257 fprintf_filtered (stream, ")");
258 }
c906108c
SS
259 break;
260
261 case TYPE_CODE_UNDEF:
262 case TYPE_CODE_STRUCT:
263 case TYPE_CODE_UNION:
264 case TYPE_CODE_ENUM:
265 case TYPE_CODE_INT:
266 case TYPE_CODE_FLT:
267 case TYPE_CODE_VOID:
268 case TYPE_CODE_ERROR:
269 case TYPE_CODE_CHAR:
270 case TYPE_CODE_BOOL:
271 case TYPE_CODE_SET:
272 case TYPE_CODE_RANGE:
273 case TYPE_CODE_STRING:
c906108c 274 case TYPE_CODE_METHOD:
c906108c
SS
275 case TYPE_CODE_COMPLEX:
276 case TYPE_CODE_TYPEDEF:
277 /* These types do not need a suffix. They are listed so that
c5aa993b 278 gcc -Wall will report types that may not have been considered. */
c906108c
SS
279 break;
280 }
281}
282
c906108c
SS
283/* Print the name of the type (or the ultimate pointer target,
284 function value or array element), or the description of a
285 structure or union.
286
287 SHOW nonzero means don't print this type as just its name;
288 show its real definition even if it has a name.
289 SHOW zero means print just typename or struct tag if there is one
290 SHOW negative means abbreviate structure elements.
291 SHOW is decremented for printing of structure elements.
292
293 LEVEL is the depth to indent by.
294 We increase it for some recursive calls. */
295
296void
fba45db2
KB
297f_type_print_base (struct type *type, struct ui_file *stream, int show,
298 int level)
c906108c 299{
2a5e440c
WZ
300 int index;
301
c906108c
SS
302 QUIT;
303
304 wrap_here (" ");
305 if (type == NULL)
306 {
307 fputs_filtered ("<type unknown>", stream);
308 return;
309 }
310
311 /* When SHOW is zero or less, and there is a valid type name, then always
0963b4bd 312 just print the type name directly from the type. */
c906108c
SS
313
314 if ((show <= 0) && (TYPE_NAME (type) != NULL))
315 {
e86ca25f
TT
316 const char *prefix = "";
317 if (TYPE_CODE (type) == TYPE_CODE_UNION)
318 prefix = "Type, C_Union :: ";
319 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
320 prefix = "Type ";
321 fprintfi_filtered (level, stream, "%s%s", prefix, TYPE_NAME (type));
c906108c
SS
322 return;
323 }
324
325 if (TYPE_CODE (type) != TYPE_CODE_TYPEDEF)
f168693b 326 type = check_typedef (type);
c906108c
SS
327
328 switch (TYPE_CODE (type))
329 {
330 case TYPE_CODE_TYPEDEF:
331 f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
332 break;
333
334 case TYPE_CODE_ARRAY:
c906108c 335 f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
7022349d
PA
336 break;
337 case TYPE_CODE_FUNC:
338 if (TYPE_TARGET_TYPE (type) == NULL)
339 type_print_unknown_return_type (stream);
340 else
341 f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
c906108c
SS
342 break;
343
c5aa993b 344 case TYPE_CODE_PTR:
a5ad232b
FP
345 fprintfi_filtered (level, stream, "PTR TO -> ( ");
346 f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, 0);
7e86466e
RH
347 break;
348
349 case TYPE_CODE_REF:
a5ad232b
FP
350 fprintfi_filtered (level, stream, "REF TO -> ( ");
351 f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, 0);
c906108c
SS
352 break;
353
354 case TYPE_CODE_VOID:
bbe75b9d
AB
355 {
356 gdbarch *gdbarch = get_type_arch (type);
357 struct type *void_type = builtin_f_type (gdbarch)->builtin_void;
358 fprintfi_filtered (level, stream, "%s", TYPE_NAME (void_type));
359 }
c906108c
SS
360 break;
361
362 case TYPE_CODE_UNDEF:
2a5e440c 363 fprintfi_filtered (level, stream, "struct <unknown>");
c906108c
SS
364 break;
365
366 case TYPE_CODE_ERROR:
b00fdb78 367 fprintfi_filtered (level, stream, "%s", TYPE_ERROR_NAME (type));
c906108c
SS
368 break;
369
370 case TYPE_CODE_RANGE:
0963b4bd 371 /* This should not occur. */
2a5e440c 372 fprintfi_filtered (level, stream, "<range type>");
c906108c
SS
373 break;
374
375 case TYPE_CODE_CHAR:
c906108c
SS
376 case TYPE_CODE_INT:
377 /* There may be some character types that attempt to come
378 through as TYPE_CODE_INT since dbxstclass.h is so
379 C-oriented, we must change these to "character" from "char". */
380
bde58177 381 if (strcmp (TYPE_NAME (type), "char") == 0)
2a5e440c 382 fprintfi_filtered (level, stream, "character");
c906108c
SS
383 else
384 goto default_case;
385 break;
386
c906108c 387 case TYPE_CODE_STRING:
0963b4bd 388 /* Strings may have dynamic upperbounds (lengths) like arrays. */
c906108c 389
d78df370 390 if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
2a5e440c 391 fprintfi_filtered (level, stream, "character*(*)");
c906108c
SS
392 else
393 {
2880242d
KS
394 LONGEST upper_bound = f77_get_upperbound (type);
395
396 fprintf_filtered (stream, "character*%s", pulongest (upper_bound));
c906108c
SS
397 }
398 break;
399
2a5e440c 400 case TYPE_CODE_STRUCT:
9eec4d1e
MD
401 case TYPE_CODE_UNION:
402 if (TYPE_CODE (type) == TYPE_CODE_UNION)
403 fprintfi_filtered (level, stream, "Type, C_Union :: ");
404 else
405 fprintfi_filtered (level, stream, "Type ");
e86ca25f 406 fputs_filtered (TYPE_NAME (type), stream);
9b2db1fd
BH
407 /* According to the definition,
408 we only print structure elements in case show > 0. */
409 if (show > 0)
2a5e440c 410 {
2a5e440c 411 fputs_filtered ("\n", stream);
9b2db1fd
BH
412 for (index = 0; index < TYPE_NFIELDS (type); index++)
413 {
e188eb36
BH
414 f_type_print_base (TYPE_FIELD_TYPE (type, index), stream,
415 show - 1, level + 4);
9b2db1fd
BH
416 fputs_filtered (" :: ", stream);
417 fputs_filtered (TYPE_FIELD_NAME (type, index), stream);
418 f_type_print_varspec_suffix (TYPE_FIELD_TYPE (type, index),
e188eb36 419 stream, show - 1, 0, 0, 0);
9b2db1fd
BH
420 fputs_filtered ("\n", stream);
421 }
422 fprintfi_filtered (level, stream, "End Type ");
e86ca25f 423 fputs_filtered (TYPE_NAME (type), stream);
9b2db1fd 424 }
2a5e440c
WZ
425 break;
426
f55ee35c 427 case TYPE_CODE_MODULE:
e86ca25f 428 fprintfi_filtered (level, stream, "module %s", TYPE_NAME (type));
f55ee35c
JK
429 break;
430
c906108c
SS
431 default_case:
432 default:
433 /* Handle types not explicitly handled by the other cases,
c5aa993b
JM
434 such as fundamental types. For these, just print whatever
435 the type name is, as recorded in the type itself. If there
0963b4bd 436 is no type name, then complain. */
c906108c 437 if (TYPE_NAME (type) != NULL)
848359ac 438 fprintfi_filtered (level, stream, "%s", TYPE_NAME (type));
c906108c 439 else
8a3fe4f8 440 error (_("Invalid type code (%d) in symbol table."), TYPE_CODE (type));
c906108c
SS
441 break;
442 }
443}