]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support for printing Fortran types for GDB, the GNU debugger. |
1bac305b | 2 | |
4a94e368 | 3 | Copyright (C) 1986-2022 Free Software Foundation, Inc. |
1bac305b | 4 | |
c906108c SS |
5 | Contributed by Motorola. Adapted from the C version by Farooq Butt |
6 | ([email protected]). | |
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" | |
bf31fd38 | 24 | #include "gdbsupport/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" |
7f6aba03 | 34 | #include "cli/cli-style.h" |
c906108c | 35 | |
1a0ea399 | 36 | /* See f-lang.h. */ |
1f20c35e AB |
37 | |
38 | void | |
1a0ea399 AB |
39 | f_language::print_typedef (struct type *type, struct symbol *new_symbol, |
40 | struct ui_file *stream) const | |
1f20c35e AB |
41 | { |
42 | type = check_typedef (type); | |
1a0ea399 | 43 | print_type (type, "", stream, 0, 0, &type_print_raw_options); |
1f20c35e AB |
44 | } |
45 | ||
1a0ea399 | 46 | /* See f-lang.h. */ |
c906108c SS |
47 | |
48 | void | |
1a0ea399 AB |
49 | f_language::print_type (struct type *type, const char *varstring, |
50 | struct ui_file *stream, int show, int level, | |
51 | const struct type_print_options *flags) const | |
c906108c | 52 | { |
52f0bd74 | 53 | enum type_code code; |
c906108c SS |
54 | |
55 | f_type_print_base (type, stream, show, level); | |
78134374 | 56 | code = type->code (); |
c906108c | 57 | if ((varstring != NULL && *varstring != '\0') |
f1fdc960 AB |
58 | /* Need a space if going to print stars or brackets; but not if we |
59 | will print just a type name. */ | |
60 | || ((show > 0 | |
7d93a1e0 | 61 | || type->name () == 0) |
dda83cd7 | 62 | && (code == TYPE_CODE_FUNC |
905e0470 PM |
63 | || code == TYPE_CODE_METHOD |
64 | || code == TYPE_CODE_ARRAY | |
f1fdc960 AB |
65 | || ((code == TYPE_CODE_PTR |
66 | || code == TYPE_CODE_REF) | |
27710edb SM |
67 | && (type->target_type ()->code () == TYPE_CODE_FUNC |
68 | || (type->target_type ()->code () | |
f1fdc960 | 69 | == TYPE_CODE_METHOD) |
27710edb | 70 | || (type->target_type ()->code () |
f1fdc960 | 71 | == TYPE_CODE_ARRAY)))))) |
0426ad51 | 72 | gdb_puts (" ", stream); |
c906108c SS |
73 | f_type_print_varspec_prefix (type, stream, show, 0); |
74 | ||
a7dfd010 MD |
75 | if (varstring != NULL) |
76 | { | |
2123df0e YQ |
77 | int demangled_args; |
78 | ||
0426ad51 | 79 | gdb_puts (varstring, stream); |
c906108c | 80 | |
a7dfd010 | 81 | /* For demangled function names, we have the arglist as part of the name, |
dda83cd7 | 82 | so don't print an additional pair of ()'s. */ |
c906108c | 83 | |
2123df0e YQ |
84 | demangled_args = (*varstring != '\0' |
85 | && varstring[strlen (varstring) - 1] == ')'); | |
584a927c | 86 | f_type_print_varspec_suffix (type, stream, show, 0, demangled_args, 0, false); |
a7dfd010 | 87 | } |
c906108c SS |
88 | } |
89 | ||
1a0ea399 | 90 | /* See f-lang.h. */ |
c906108c SS |
91 | |
92 | void | |
1a0ea399 AB |
93 | f_language::f_type_print_varspec_prefix (struct type *type, |
94 | struct ui_file *stream, | |
95 | int show, int passed_a_ptr) const | |
c906108c SS |
96 | { |
97 | if (type == 0) | |
98 | return; | |
99 | ||
7d93a1e0 | 100 | if (type->name () && show <= 0) |
c906108c SS |
101 | return; |
102 | ||
103 | QUIT; | |
104 | ||
78134374 | 105 | switch (type->code ()) |
c906108c SS |
106 | { |
107 | case TYPE_CODE_PTR: | |
27710edb | 108 | f_type_print_varspec_prefix (type->target_type (), stream, 0, 1); |
c906108c SS |
109 | break; |
110 | ||
111 | case TYPE_CODE_FUNC: | |
27710edb | 112 | f_type_print_varspec_prefix (type->target_type (), stream, 0, 0); |
c906108c | 113 | if (passed_a_ptr) |
6cb06a8c | 114 | gdb_printf (stream, "("); |
c906108c SS |
115 | break; |
116 | ||
117 | case TYPE_CODE_ARRAY: | |
27710edb | 118 | f_type_print_varspec_prefix (type->target_type (), stream, 0, 0); |
c906108c SS |
119 | break; |
120 | ||
121 | case TYPE_CODE_UNDEF: | |
122 | case TYPE_CODE_STRUCT: | |
123 | case TYPE_CODE_UNION: | |
e9512253 | 124 | case TYPE_CODE_NAMELIST: |
c906108c SS |
125 | case TYPE_CODE_ENUM: |
126 | case TYPE_CODE_INT: | |
127 | case TYPE_CODE_FLT: | |
128 | case TYPE_CODE_VOID: | |
129 | case TYPE_CODE_ERROR: | |
130 | case TYPE_CODE_CHAR: | |
131 | case TYPE_CODE_BOOL: | |
132 | case TYPE_CODE_SET: | |
133 | case TYPE_CODE_RANGE: | |
134 | case TYPE_CODE_STRING: | |
c906108c | 135 | case TYPE_CODE_METHOD: |
c906108c SS |
136 | case TYPE_CODE_REF: |
137 | case TYPE_CODE_COMPLEX: | |
138 | case TYPE_CODE_TYPEDEF: | |
139 | /* These types need no prefix. They are listed here so that | |
dda83cd7 | 140 | gcc -Wall will reveal any types that haven't been handled. */ |
c906108c SS |
141 | break; |
142 | } | |
143 | } | |
144 | ||
1a0ea399 | 145 | /* See f-lang.h. */ |
584a927c | 146 | |
1a0ea399 AB |
147 | void |
148 | f_language::f_type_print_varspec_suffix (struct type *type, | |
149 | struct ui_file *stream, | |
150 | int show, int passed_a_ptr, | |
151 | int demangled_args, | |
152 | int arrayprint_recurse_level, | |
153 | bool print_rank_only) const | |
c906108c | 154 | { |
0311118f JK |
155 | /* No static variables are permitted as an error call may occur during |
156 | execution of this function. */ | |
c906108c SS |
157 | |
158 | if (type == 0) | |
159 | return; | |
160 | ||
7d93a1e0 | 161 | if (type->name () && show <= 0) |
c906108c SS |
162 | return; |
163 | ||
164 | QUIT; | |
165 | ||
78134374 | 166 | switch (type->code ()) |
c906108c SS |
167 | { |
168 | case TYPE_CODE_ARRAY: | |
169 | arrayprint_recurse_level++; | |
170 | ||
171 | if (arrayprint_recurse_level == 1) | |
6cb06a8c | 172 | gdb_printf (stream, "("); |
c906108c | 173 | |
3f2f83dd | 174 | if (type_not_associated (type)) |
584a927c | 175 | print_rank_only = true; |
3f2f83dd | 176 | else if (type_not_allocated (type)) |
584a927c AB |
177 | print_rank_only = true; |
178 | else if ((TYPE_ASSOCIATED_PROP (type) | |
8a6d5e35 | 179 | && PROP_CONST != TYPE_ASSOCIATED_PROP (type)->kind ()) |
584a927c | 180 | || (TYPE_ALLOCATED_PROP (type) |
8a6d5e35 | 181 | && PROP_CONST != TYPE_ALLOCATED_PROP (type)->kind ()) |
584a927c | 182 | || (TYPE_DATA_LOCATION (type) |
8a6d5e35 | 183 | && PROP_CONST != TYPE_DATA_LOCATION (type)->kind ())) |
584a927c AB |
184 | { |
185 | /* This case exist when we ptype a typename which has the dynamic | |
186 | properties but cannot be resolved as there is no object. */ | |
187 | print_rank_only = true; | |
188 | } | |
3f2f83dd | 189 | |
27710edb SM |
190 | if (type->target_type ()->code () == TYPE_CODE_ARRAY) |
191 | f_type_print_varspec_suffix (type->target_type (), stream, 0, | |
584a927c AB |
192 | 0, 0, arrayprint_recurse_level, |
193 | print_rank_only); | |
2880242d | 194 | |
584a927c | 195 | if (print_rank_only) |
6cb06a8c | 196 | gdb_printf (stream, ":"); |
584a927c AB |
197 | else |
198 | { | |
199 | LONGEST lower_bound = f77_get_lowerbound (type); | |
200 | if (lower_bound != 1) /* Not the default. */ | |
6cb06a8c | 201 | gdb_printf (stream, "%s:", plongest (lower_bound)); |
3f2f83dd | 202 | |
584a927c AB |
203 | /* Make sure that, if we have an assumed size array, we |
204 | print out a warning and print the upperbound as '*'. */ | |
3f2f83dd | 205 | |
cf88be68 | 206 | if (type->bounds ()->high.kind () == PROP_UNDEFINED) |
6cb06a8c | 207 | gdb_printf (stream, "*"); |
584a927c AB |
208 | else |
209 | { | |
210 | LONGEST upper_bound = f77_get_upperbound (type); | |
2880242d | 211 | |
0426ad51 | 212 | gdb_puts (plongest (upper_bound), stream); |
584a927c AB |
213 | } |
214 | } | |
215 | ||
27710edb SM |
216 | if (type->target_type ()->code () != TYPE_CODE_ARRAY) |
217 | f_type_print_varspec_suffix (type->target_type (), stream, 0, | |
584a927c AB |
218 | 0, 0, arrayprint_recurse_level, |
219 | print_rank_only); | |
3f2f83dd | 220 | |
c906108c | 221 | if (arrayprint_recurse_level == 1) |
6cb06a8c | 222 | gdb_printf (stream, ")"); |
c906108c | 223 | else |
6cb06a8c | 224 | gdb_printf (stream, ","); |
c906108c SS |
225 | arrayprint_recurse_level--; |
226 | break; | |
227 | ||
228 | case TYPE_CODE_PTR: | |
229 | case TYPE_CODE_REF: | |
27710edb | 230 | f_type_print_varspec_suffix (type->target_type (), stream, 0, 1, 0, |
584a927c | 231 | arrayprint_recurse_level, false); |
6cb06a8c | 232 | gdb_printf (stream, " )"); |
c906108c SS |
233 | break; |
234 | ||
235 | case TYPE_CODE_FUNC: | |
bf7a4de1 | 236 | { |
1f704f76 | 237 | int i, nfields = type->num_fields (); |
c906108c | 238 | |
27710edb | 239 | f_type_print_varspec_suffix (type->target_type (), stream, 0, |
584a927c AB |
240 | passed_a_ptr, 0, |
241 | arrayprint_recurse_level, false); | |
bf7a4de1 | 242 | if (passed_a_ptr) |
6cb06a8c TT |
243 | gdb_printf (stream, ") "); |
244 | gdb_printf (stream, "("); | |
7f9f399b | 245 | if (nfields == 0 && type->is_prototyped ()) |
8ee511af SM |
246 | print_type (builtin_f_type (type->arch ())->builtin_void, |
247 | "", stream, -1, 0, 0); | |
bf7a4de1 AB |
248 | else |
249 | for (i = 0; i < nfields; i++) | |
250 | { | |
251 | if (i > 0) | |
252 | { | |
0426ad51 | 253 | gdb_puts (", ", stream); |
1285ce86 | 254 | stream->wrap_here (4); |
bf7a4de1 | 255 | } |
1a0ea399 | 256 | print_type (type->field (i).type (), "", stream, -1, 0, 0); |
bf7a4de1 | 257 | } |
6cb06a8c | 258 | gdb_printf (stream, ")"); |
bf7a4de1 | 259 | } |
c906108c SS |
260 | break; |
261 | ||
262 | case TYPE_CODE_UNDEF: | |
263 | case TYPE_CODE_STRUCT: | |
264 | case TYPE_CODE_UNION: | |
e9512253 | 265 | case TYPE_CODE_NAMELIST: |
c906108c SS |
266 | case TYPE_CODE_ENUM: |
267 | case TYPE_CODE_INT: | |
268 | case TYPE_CODE_FLT: | |
269 | case TYPE_CODE_VOID: | |
270 | case TYPE_CODE_ERROR: | |
271 | case TYPE_CODE_CHAR: | |
272 | case TYPE_CODE_BOOL: | |
273 | case TYPE_CODE_SET: | |
274 | case TYPE_CODE_RANGE: | |
275 | case TYPE_CODE_STRING: | |
c906108c | 276 | case TYPE_CODE_METHOD: |
c906108c SS |
277 | case TYPE_CODE_COMPLEX: |
278 | case TYPE_CODE_TYPEDEF: | |
279 | /* These types do not need a suffix. They are listed so that | |
dda83cd7 | 280 | gcc -Wall will report types that may not have been considered. */ |
c906108c SS |
281 | break; |
282 | } | |
283 | } | |
284 | ||
1a0ea399 | 285 | /* See f-lang.h. */ |
c906108c | 286 | |
110aae55 BH |
287 | void |
288 | f_language::f_type_print_derivation_info (struct type *type, | |
289 | struct ui_file *stream) const | |
290 | { | |
291 | /* Fortran doesn't support multiple inheritance. */ | |
292 | const int i = 0; | |
293 | ||
294 | if (TYPE_N_BASECLASSES (type) > 0) | |
295 | gdb_printf (stream, ", extends(%s) ::", TYPE_BASECLASS (type, i)->name ()); | |
296 | } | |
297 | ||
298 | /* See f-lang.h. */ | |
299 | ||
c906108c | 300 | void |
1a0ea399 AB |
301 | f_language::f_type_print_base (struct type *type, struct ui_file *stream, |
302 | int show, int level) const | |
c906108c | 303 | { |
2a5e440c WZ |
304 | int index; |
305 | ||
c906108c SS |
306 | QUIT; |
307 | ||
1285ce86 | 308 | stream->wrap_here (4); |
c906108c SS |
309 | if (type == NULL) |
310 | { | |
7f6aba03 | 311 | fputs_styled ("<type unknown>", metadata_style.style (), stream); |
c906108c SS |
312 | return; |
313 | } | |
314 | ||
315 | /* When SHOW is zero or less, and there is a valid type name, then always | |
0963b4bd | 316 | just print the type name directly from the type. */ |
c906108c | 317 | |
7d93a1e0 | 318 | if ((show <= 0) && (type->name () != NULL)) |
c906108c | 319 | { |
e86ca25f | 320 | const char *prefix = ""; |
78134374 | 321 | if (type->code () == TYPE_CODE_UNION) |
e86ca25f | 322 | prefix = "Type, C_Union :: "; |
e9512253 BK |
323 | else if (type->code () == TYPE_CODE_STRUCT |
324 | || type->code () == TYPE_CODE_NAMELIST) | |
e86ca25f | 325 | prefix = "Type "; |
6cb06a8c | 326 | gdb_printf (stream, "%*s%s%s", level, "", prefix, type->name ()); |
c906108c SS |
327 | return; |
328 | } | |
329 | ||
78134374 | 330 | if (type->code () != TYPE_CODE_TYPEDEF) |
f168693b | 331 | type = check_typedef (type); |
c906108c | 332 | |
78134374 | 333 | switch (type->code ()) |
c906108c SS |
334 | { |
335 | case TYPE_CODE_TYPEDEF: | |
27710edb | 336 | f_type_print_base (type->target_type (), stream, 0, level); |
c906108c SS |
337 | break; |
338 | ||
339 | case TYPE_CODE_ARRAY: | |
27710edb | 340 | f_type_print_base (type->target_type (), stream, show, level); |
7022349d PA |
341 | break; |
342 | case TYPE_CODE_FUNC: | |
27710edb | 343 | if (type->target_type () == NULL) |
7022349d PA |
344 | type_print_unknown_return_type (stream); |
345 | else | |
27710edb | 346 | f_type_print_base (type->target_type (), stream, show, level); |
c906108c SS |
347 | break; |
348 | ||
c5aa993b | 349 | case TYPE_CODE_PTR: |
6cb06a8c | 350 | gdb_printf (stream, "%*sPTR TO -> ( ", level, ""); |
27710edb | 351 | f_type_print_base (type->target_type (), stream, show, 0); |
7e86466e RH |
352 | break; |
353 | ||
354 | case TYPE_CODE_REF: | |
6cb06a8c | 355 | gdb_printf (stream, "%*sREF TO -> ( ", level, ""); |
27710edb | 356 | f_type_print_base (type->target_type (), stream, show, 0); |
c906108c SS |
357 | break; |
358 | ||
359 | case TYPE_CODE_VOID: | |
bbe75b9d | 360 | { |
8ee511af | 361 | struct type *void_type = builtin_f_type (type->arch ())->builtin_void; |
6cb06a8c | 362 | gdb_printf (stream, "%*s%s", level, "", void_type->name ()); |
bbe75b9d | 363 | } |
c906108c SS |
364 | break; |
365 | ||
366 | case TYPE_CODE_UNDEF: | |
6cb06a8c | 367 | gdb_printf (stream, "%*sstruct <unknown>", level, ""); |
c906108c SS |
368 | break; |
369 | ||
370 | case TYPE_CODE_ERROR: | |
6cb06a8c | 371 | gdb_printf (stream, "%*s%s", level, "", TYPE_ERROR_NAME (type)); |
c906108c SS |
372 | break; |
373 | ||
374 | case TYPE_CODE_RANGE: | |
0963b4bd | 375 | /* This should not occur. */ |
6cb06a8c | 376 | gdb_printf (stream, "%*s<range type>", level, ""); |
c906108c SS |
377 | break; |
378 | ||
379 | case TYPE_CODE_CHAR: | |
c906108c SS |
380 | case TYPE_CODE_INT: |
381 | /* There may be some character types that attempt to come | |
dda83cd7 SM |
382 | through as TYPE_CODE_INT since dbxstclass.h is so |
383 | C-oriented, we must change these to "character" from "char". */ | |
c906108c | 384 | |
7d93a1e0 | 385 | if (strcmp (type->name (), "char") == 0) |
6cb06a8c | 386 | gdb_printf (stream, "%*scharacter", level, ""); |
c906108c SS |
387 | else |
388 | goto default_case; | |
389 | break; | |
390 | ||
c906108c | 391 | case TYPE_CODE_STRING: |
3dcc261c AB |
392 | /* Strings may have dynamic upperbounds (lengths) like arrays. We |
393 | check specifically for the PROP_CONST case to indicate that the | |
394 | dynamic type has been resolved. If we arrive here having been | |
395 | asked to print the type of a value with a dynamic type then the | |
396 | bounds will not have been resolved. */ | |
c906108c | 397 | |
3dcc261c | 398 | if (type->bounds ()->high.kind () == PROP_CONST) |
c906108c | 399 | { |
2880242d KS |
400 | LONGEST upper_bound = f77_get_upperbound (type); |
401 | ||
6cb06a8c | 402 | gdb_printf (stream, "character*%s", pulongest (upper_bound)); |
c906108c | 403 | } |
3dcc261c | 404 | else |
6cb06a8c | 405 | gdb_printf (stream, "%*scharacter*(*)", level, ""); |
c906108c SS |
406 | break; |
407 | ||
2a5e440c | 408 | case TYPE_CODE_STRUCT: |
9eec4d1e | 409 | case TYPE_CODE_UNION: |
e9512253 | 410 | case TYPE_CODE_NAMELIST: |
78134374 | 411 | if (type->code () == TYPE_CODE_UNION) |
110aae55 | 412 | gdb_printf (stream, "%*sType, C_Union ::", level, ""); |
9eec4d1e | 413 | else |
110aae55 BH |
414 | gdb_printf (stream, "%*sType", level, ""); |
415 | ||
416 | if (show > 0) | |
417 | f_type_print_derivation_info (type, stream); | |
418 | ||
419 | gdb_puts (" ", stream); | |
420 | ||
0426ad51 | 421 | gdb_puts (type->name (), stream); |
110aae55 | 422 | |
9b2db1fd | 423 | /* According to the definition, |
dda83cd7 | 424 | we only print structure elements in case show > 0. */ |
9b2db1fd | 425 | if (show > 0) |
2a5e440c | 426 | { |
0426ad51 | 427 | gdb_puts ("\n", stream); |
1f704f76 | 428 | for (index = 0; index < type->num_fields (); index++) |
9b2db1fd | 429 | { |
940da03e | 430 | f_type_print_base (type->field (index).type (), stream, |
e188eb36 | 431 | show - 1, level + 4); |
0426ad51 | 432 | gdb_puts (" :: ", stream); |
33d16dd9 | 433 | fputs_styled (type->field (index).name (), |
3f0cbb04 | 434 | variable_name_style.style (), stream); |
940da03e | 435 | f_type_print_varspec_suffix (type->field (index).type (), |
584a927c | 436 | stream, show - 1, 0, 0, 0, false); |
0426ad51 | 437 | gdb_puts ("\n", stream); |
9b2db1fd | 438 | } |
6cb06a8c | 439 | gdb_printf (stream, "%*sEnd Type ", level, ""); |
0426ad51 | 440 | gdb_puts (type->name (), stream); |
9b2db1fd | 441 | } |
2a5e440c WZ |
442 | break; |
443 | ||
f55ee35c | 444 | case TYPE_CODE_MODULE: |
6cb06a8c | 445 | gdb_printf (stream, "%*smodule %s", level, "", type->name ()); |
f55ee35c JK |
446 | break; |
447 | ||
c906108c SS |
448 | default_case: |
449 | default: | |
450 | /* Handle types not explicitly handled by the other cases, | |
dda83cd7 SM |
451 | such as fundamental types. For these, just print whatever |
452 | the type name is, as recorded in the type itself. If there | |
453 | is no type name, then complain. */ | |
7d93a1e0 | 454 | if (type->name () != NULL) |
6cb06a8c | 455 | gdb_printf (stream, "%*s%s", level, "", type->name ()); |
c906108c | 456 | else |
78134374 | 457 | error (_("Invalid type code (%d) in symbol table."), type->code ()); |
c906108c SS |
458 | break; |
459 | } | |
bc68014d AB |
460 | |
461 | if (TYPE_IS_ALLOCATABLE (type)) | |
6cb06a8c | 462 | gdb_printf (stream, ", allocatable"); |
c906108c | 463 | } |