| 1 | /* Support for printing Fortran types for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 1986-2015 Free Software Foundation, Inc. |
| 4 | |
| 5 | Contributed by Motorola. Adapted from the C version by Farooq Butt |
| 6 | (fmbutt@engage.sps.mot.com). |
| 7 | |
| 8 | This file is part of GDB. |
| 9 | |
| 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. |
| 14 | |
| 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. |
| 19 | |
| 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/>. */ |
| 22 | |
| 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" |
| 33 | |
| 34 | #if 0 /* Currently unused. */ |
| 35 | static void f_type_print_args (struct type *, struct ui_file *); |
| 36 | #endif |
| 37 | |
| 38 | static void f_type_print_varspec_suffix (struct type *, struct ui_file *, int, |
| 39 | int, int, int); |
| 40 | |
| 41 | void f_type_print_varspec_prefix (struct type *, struct ui_file *, |
| 42 | int, int); |
| 43 | |
| 44 | void f_type_print_base (struct type *, struct ui_file *, int, int); |
| 45 | \f |
| 46 | |
| 47 | /* LEVEL is the depth to indent lines by. */ |
| 48 | |
| 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) |
| 52 | { |
| 53 | enum type_code code; |
| 54 | int demangled_args; |
| 55 | |
| 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); |
| 68 | |
| 69 | if (varstring != NULL) |
| 70 | { |
| 71 | fputs_filtered (varstring, stream); |
| 72 | |
| 73 | /* For demangled function names, we have the arglist as part of the name, |
| 74 | so don't print an additional pair of ()'s. */ |
| 75 | |
| 76 | demangled_args = varstring[strlen (varstring) - 1] == ')'; |
| 77 | f_type_print_varspec_suffix (type, stream, show, 0, demangled_args, 0); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* Print any asterisks or open-parentheses needed before the |
| 82 | variable name (to describe its type). |
| 83 | |
| 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. */ |
| 88 | |
| 89 | void |
| 90 | f_type_print_varspec_prefix (struct type *type, struct ui_file *stream, |
| 91 | int show, int passed_a_ptr) |
| 92 | { |
| 93 | if (type == 0) |
| 94 | return; |
| 95 | |
| 96 | if (TYPE_NAME (type) && show <= 0) |
| 97 | return; |
| 98 | |
| 99 | QUIT; |
| 100 | |
| 101 | switch (TYPE_CODE (type)) |
| 102 | { |
| 103 | case TYPE_CODE_PTR: |
| 104 | f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1); |
| 105 | break; |
| 106 | |
| 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; |
| 112 | |
| 113 | case TYPE_CODE_ARRAY: |
| 114 | f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0); |
| 115 | break; |
| 116 | |
| 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; |
| 137 | } |
| 138 | } |
| 139 | |
| 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. */ |
| 143 | |
| 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) |
| 148 | { |
| 149 | int upper_bound, lower_bound; |
| 150 | |
| 151 | /* No static variables are permitted as an error call may occur during |
| 152 | execution of this function. */ |
| 153 | |
| 154 | if (type == 0) |
| 155 | return; |
| 156 | |
| 157 | if (TYPE_NAME (type) && show <= 0) |
| 158 | return; |
| 159 | |
| 160 | QUIT; |
| 161 | |
| 162 | switch (TYPE_CODE (type)) |
| 163 | { |
| 164 | case TYPE_CODE_ARRAY: |
| 165 | arrayprint_recurse_level++; |
| 166 | |
| 167 | if (arrayprint_recurse_level == 1) |
| 168 | fprintf_filtered (stream, "("); |
| 169 | |
| 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); |
| 173 | |
| 174 | lower_bound = f77_get_lowerbound (type); |
| 175 | if (lower_bound != 1) /* Not the default. */ |
| 176 | fprintf_filtered (stream, "%d:", lower_bound); |
| 177 | |
| 178 | /* Make sure that, if we have an assumed size array, we |
| 179 | print out a warning and print the upperbound as '*'. */ |
| 180 | |
| 181 | if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type)) |
| 182 | fprintf_filtered (stream, "*"); |
| 183 | else |
| 184 | { |
| 185 | upper_bound = f77_get_upperbound (type); |
| 186 | fprintf_filtered (stream, "%d", upper_bound); |
| 187 | } |
| 188 | |
| 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; |
| 198 | |
| 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; |
| 205 | |
| 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, ")"); |
| 211 | |
| 212 | fprintf_filtered (stream, "()"); |
| 213 | break; |
| 214 | |
| 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; |
| 234 | } |
| 235 | } |
| 236 | |
| 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. |
| 240 | |
| 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. |
| 246 | |
| 247 | LEVEL is the depth to indent by. |
| 248 | We increase it for some recursive calls. */ |
| 249 | |
| 250 | void |
| 251 | f_type_print_base (struct type *type, struct ui_file *stream, int show, |
| 252 | int level) |
| 253 | { |
| 254 | int upper_bound; |
| 255 | int index; |
| 256 | |
| 257 | QUIT; |
| 258 | |
| 259 | wrap_here (" "); |
| 260 | if (type == NULL) |
| 261 | { |
| 262 | fputs_filtered ("<type unknown>", stream); |
| 263 | return; |
| 264 | } |
| 265 | |
| 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. */ |
| 268 | |
| 269 | if ((show <= 0) && (TYPE_NAME (type) != NULL)) |
| 270 | { |
| 271 | fputs_filtered (TYPE_NAME (type), stream); |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | if (TYPE_CODE (type) != TYPE_CODE_TYPEDEF) |
| 276 | type = check_typedef (type); |
| 277 | |
| 278 | switch (TYPE_CODE (type)) |
| 279 | { |
| 280 | case TYPE_CODE_TYPEDEF: |
| 281 | f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level); |
| 282 | break; |
| 283 | |
| 284 | case TYPE_CODE_ARRAY: |
| 285 | case TYPE_CODE_FUNC: |
| 286 | f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level); |
| 287 | break; |
| 288 | |
| 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; |
| 293 | |
| 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; |
| 298 | |
| 299 | case TYPE_CODE_VOID: |
| 300 | fprintfi_filtered (level, stream, "VOID"); |
| 301 | break; |
| 302 | |
| 303 | case TYPE_CODE_UNDEF: |
| 304 | fprintfi_filtered (level, stream, "struct <unknown>"); |
| 305 | break; |
| 306 | |
| 307 | case TYPE_CODE_ERROR: |
| 308 | fprintfi_filtered (level, stream, "%s", TYPE_ERROR_NAME (type)); |
| 309 | break; |
| 310 | |
| 311 | case TYPE_CODE_RANGE: |
| 312 | /* This should not occur. */ |
| 313 | fprintfi_filtered (level, stream, "<range type>"); |
| 314 | break; |
| 315 | |
| 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". */ |
| 321 | |
| 322 | if (strcmp (TYPE_NAME (type), "char") == 0) |
| 323 | fprintfi_filtered (level, stream, "character"); |
| 324 | else |
| 325 | goto default_case; |
| 326 | break; |
| 327 | |
| 328 | case TYPE_CODE_STRING: |
| 329 | /* Strings may have dynamic upperbounds (lengths) like arrays. */ |
| 330 | |
| 331 | if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type)) |
| 332 | fprintfi_filtered (level, stream, "character*(*)"); |
| 333 | else |
| 334 | { |
| 335 | upper_bound = f77_get_upperbound (type); |
| 336 | fprintf_filtered (stream, "character*%d", upper_bound); |
| 337 | } |
| 338 | break; |
| 339 | |
| 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++) |
| 349 | { |
| 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); |
| 357 | } |
| 358 | fprintfi_filtered (level, stream, "End Type "); |
| 359 | fputs_filtered (TYPE_TAG_NAME (type), stream); |
| 360 | break; |
| 361 | |
| 362 | case TYPE_CODE_MODULE: |
| 363 | fprintfi_filtered (level, stream, "module %s", TYPE_TAG_NAME (type)); |
| 364 | break; |
| 365 | |
| 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; |
| 377 | } |
| 378 | } |