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