]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/f-typeprint.c
Switch the license of all .c files to GPLv3.
[thirdparty/binutils-gdb.git] / gdb / f-typeprint.c
1 /* Support for printing Fortran types for GDB, the GNU debugger.
2
3 Copyright (C) 1986, 1988, 1989, 1991, 1993, 1994, 1995, 1996, 1998, 2000,
4 2001, 2002, 2003, 2006, 2007 Free Software Foundation, Inc.
5
6 Contributed by Motorola. Adapted from the C version by Farooq Butt
7 (fmbutt@engage.sps.mot.com).
8
9 This file is part of GDB.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23
24 #include "defs.h"
25 #include "gdb_obstack.h"
26 #include "bfd.h"
27 #include "symtab.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "value.h"
31 #include "gdbcore.h"
32 #include "target.h"
33 #include "f-lang.h"
34
35 #include "gdb_string.h"
36 #include <errno.h>
37
38 #if 0 /* Currently unused */
39 static void f_type_print_args (struct type *, struct ui_file *);
40 #endif
41
42 static void print_equivalent_f77_float_type (int level, struct type *,
43 struct ui_file *);
44
45 static void f_type_print_varspec_suffix (struct type *, struct ui_file *,
46 int, int, int);
47
48 void f_type_print_varspec_prefix (struct type *, struct ui_file *,
49 int, int);
50
51 void f_type_print_base (struct type *, struct ui_file *, int, int);
52 \f
53
54 /* LEVEL is the depth to indent lines by. */
55
56 void
57 f_print_type (struct type *type, char *varstring, struct ui_file *stream,
58 int show, int level)
59 {
60 enum type_code code;
61 int demangled_args;
62
63 f_type_print_base (type, stream, show, level);
64 code = TYPE_CODE (type);
65 if ((varstring != NULL && *varstring != '\0')
66 ||
67 /* Need a space if going to print stars or brackets;
68 but not if we will print just a type name. */
69 ((show > 0 || TYPE_NAME (type) == 0)
70 &&
71 (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
72 || code == TYPE_CODE_METHOD
73 || code == TYPE_CODE_ARRAY
74 || code == TYPE_CODE_REF)))
75 fputs_filtered (" ", stream);
76 f_type_print_varspec_prefix (type, stream, show, 0);
77
78 fputs_filtered (varstring, stream);
79
80 /* For demangled function names, we have the arglist as part of the name,
81 so don't print an additional pair of ()'s */
82
83 demangled_args = varstring[strlen (varstring) - 1] == ')';
84 f_type_print_varspec_suffix (type, stream, show, 0, demangled_args);
85 }
86
87 /* Print any asterisks or open-parentheses needed before the
88 variable name (to describe its type).
89
90 On outermost call, pass 0 for PASSED_A_PTR.
91 On outermost call, SHOW > 0 means should ignore
92 any typename for TYPE and show its details.
93 SHOW is always zero on recursive calls. */
94
95 void
96 f_type_print_varspec_prefix (struct type *type, struct ui_file *stream,
97 int show, int passed_a_ptr)
98 {
99 if (type == 0)
100 return;
101
102 if (TYPE_NAME (type) && show <= 0)
103 return;
104
105 QUIT;
106
107 switch (TYPE_CODE (type))
108 {
109 case TYPE_CODE_PTR:
110 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
111 break;
112
113 case TYPE_CODE_FUNC:
114 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
115 if (passed_a_ptr)
116 fprintf_filtered (stream, "(");
117 break;
118
119 case TYPE_CODE_ARRAY:
120 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
121 break;
122
123 case TYPE_CODE_UNDEF:
124 case TYPE_CODE_STRUCT:
125 case TYPE_CODE_UNION:
126 case TYPE_CODE_ENUM:
127 case TYPE_CODE_INT:
128 case TYPE_CODE_FLT:
129 case TYPE_CODE_VOID:
130 case TYPE_CODE_ERROR:
131 case TYPE_CODE_CHAR:
132 case TYPE_CODE_BOOL:
133 case TYPE_CODE_SET:
134 case TYPE_CODE_RANGE:
135 case TYPE_CODE_STRING:
136 case TYPE_CODE_BITSTRING:
137 case TYPE_CODE_METHOD:
138 case TYPE_CODE_REF:
139 case TYPE_CODE_COMPLEX:
140 case TYPE_CODE_TYPEDEF:
141 /* These types need no prefix. They are listed here so that
142 gcc -Wall will reveal any types that haven't been handled. */
143 break;
144 }
145 }
146
147 /* Print any array sizes, function arguments or close parentheses
148 needed after the variable name (to describe its type).
149 Args work like c_type_print_varspec_prefix. */
150
151 static void
152 f_type_print_varspec_suffix (struct type *type, struct ui_file *stream,
153 int show, int passed_a_ptr, int demangled_args)
154 {
155 int upper_bound, lower_bound;
156 int lower_bound_was_default = 0;
157 static int arrayprint_recurse_level = 0;
158 int retcode;
159
160 if (type == 0)
161 return;
162
163 if (TYPE_NAME (type) && show <= 0)
164 return;
165
166 QUIT;
167
168 switch (TYPE_CODE (type))
169 {
170 case TYPE_CODE_ARRAY:
171 arrayprint_recurse_level++;
172
173 if (arrayprint_recurse_level == 1)
174 fprintf_filtered (stream, "(");
175
176 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY)
177 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
178
179 retcode = f77_get_dynamic_lowerbound (type, &lower_bound);
180
181 lower_bound_was_default = 0;
182
183 if (retcode == BOUND_FETCH_ERROR)
184 fprintf_filtered (stream, "???");
185 else if (lower_bound == 1) /* The default */
186 lower_bound_was_default = 1;
187 else
188 fprintf_filtered (stream, "%d", lower_bound);
189
190 if (lower_bound_was_default)
191 lower_bound_was_default = 0;
192 else
193 fprintf_filtered (stream, ":");
194
195 /* Make sure that, if we have an assumed size array, we
196 print out a warning and print the upperbound as '*' */
197
198 if (TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
199 fprintf_filtered (stream, "*");
200 else
201 {
202 retcode = f77_get_dynamic_upperbound (type, &upper_bound);
203
204 if (retcode == BOUND_FETCH_ERROR)
205 fprintf_filtered (stream, "???");
206 else
207 fprintf_filtered (stream, "%d", upper_bound);
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, 0, 0);
212 if (arrayprint_recurse_level == 1)
213 fprintf_filtered (stream, ")");
214 else
215 fprintf_filtered (stream, ",");
216 arrayprint_recurse_level--;
217 break;
218
219 case TYPE_CODE_PTR:
220 case TYPE_CODE_REF:
221 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0);
222 fprintf_filtered (stream, ")");
223 break;
224
225 case TYPE_CODE_FUNC:
226 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
227 passed_a_ptr, 0);
228 if (passed_a_ptr)
229 fprintf_filtered (stream, ")");
230
231 fprintf_filtered (stream, "()");
232 break;
233
234 case TYPE_CODE_UNDEF:
235 case TYPE_CODE_STRUCT:
236 case TYPE_CODE_UNION:
237 case TYPE_CODE_ENUM:
238 case TYPE_CODE_INT:
239 case TYPE_CODE_FLT:
240 case TYPE_CODE_VOID:
241 case TYPE_CODE_ERROR:
242 case TYPE_CODE_CHAR:
243 case TYPE_CODE_BOOL:
244 case TYPE_CODE_SET:
245 case TYPE_CODE_RANGE:
246 case TYPE_CODE_STRING:
247 case TYPE_CODE_BITSTRING:
248 case TYPE_CODE_METHOD:
249 case TYPE_CODE_COMPLEX:
250 case TYPE_CODE_TYPEDEF:
251 /* These types do not need a suffix. They are listed so that
252 gcc -Wall will report types that may not have been considered. */
253 break;
254 }
255 }
256
257 static void
258 print_equivalent_f77_float_type (int level, struct type *type,
259 struct ui_file *stream)
260 {
261 /* Override type name "float" and make it the
262 appropriate real. XLC stupidly outputs -12 as a type
263 for real when it really should be outputting -18 */
264
265 fprintfi_filtered (level, stream, "real*%d", TYPE_LENGTH (type));
266 }
267
268 /* Print the name of the type (or the ultimate pointer target,
269 function value or array element), or the description of a
270 structure or union.
271
272 SHOW nonzero means don't print this type as just its name;
273 show its real definition even if it has a name.
274 SHOW zero means print just typename or struct tag if there is one
275 SHOW negative means abbreviate structure elements.
276 SHOW is decremented for printing of structure elements.
277
278 LEVEL is the depth to indent by.
279 We increase it for some recursive calls. */
280
281 void
282 f_type_print_base (struct type *type, struct ui_file *stream, int show,
283 int level)
284 {
285 int retcode;
286 int upper_bound;
287
288 int index;
289
290 QUIT;
291
292 wrap_here (" ");
293 if (type == NULL)
294 {
295 fputs_filtered ("<type unknown>", stream);
296 return;
297 }
298
299 /* When SHOW is zero or less, and there is a valid type name, then always
300 just print the type name directly from the type. */
301
302 if ((show <= 0) && (TYPE_NAME (type) != NULL))
303 {
304 if (TYPE_CODE (type) == TYPE_CODE_FLT)
305 print_equivalent_f77_float_type (level, type, stream);
306 else
307 fputs_filtered (TYPE_NAME (type), stream);
308 return;
309 }
310
311 if (TYPE_CODE (type) != TYPE_CODE_TYPEDEF)
312 CHECK_TYPEDEF (type);
313
314 switch (TYPE_CODE (type))
315 {
316 case TYPE_CODE_TYPEDEF:
317 f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
318 break;
319
320 case TYPE_CODE_ARRAY:
321 case TYPE_CODE_FUNC:
322 f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
323 break;
324
325 case TYPE_CODE_PTR:
326 fprintf_filtered (stream, "PTR TO -> ( ");
327 f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
328 break;
329
330 case TYPE_CODE_REF:
331 fprintf_filtered (stream, "REF TO -> ( ");
332 f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
333 break;
334
335 case TYPE_CODE_VOID:
336 fprintfi_filtered (level, stream, "VOID");
337 break;
338
339 case TYPE_CODE_UNDEF:
340 fprintfi_filtered (level, stream, "struct <unknown>");
341 break;
342
343 case TYPE_CODE_ERROR:
344 fprintfi_filtered (level, stream, "<unknown type>");
345 break;
346
347 case TYPE_CODE_RANGE:
348 /* This should not occur */
349 fprintfi_filtered (level, stream, "<range type>");
350 break;
351
352 case TYPE_CODE_CHAR:
353 /* Override name "char" and make it "character" */
354 fprintfi_filtered (level, stream, "character");
355 break;
356
357 case TYPE_CODE_INT:
358 /* There may be some character types that attempt to come
359 through as TYPE_CODE_INT since dbxstclass.h is so
360 C-oriented, we must change these to "character" from "char". */
361
362 if (strcmp (TYPE_NAME (type), "char") == 0)
363 fprintfi_filtered (level, stream, "character");
364 else
365 goto default_case;
366 break;
367
368 case TYPE_CODE_COMPLEX:
369 fprintfi_filtered (level, stream, "complex*%d", TYPE_LENGTH (type));
370 break;
371
372 case TYPE_CODE_FLT:
373 print_equivalent_f77_float_type (level, type, stream);
374 break;
375
376 case TYPE_CODE_STRING:
377 /* Strings may have dynamic upperbounds (lengths) like arrays. */
378
379 if (TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
380 fprintfi_filtered (level, stream, "character*(*)");
381 else
382 {
383 retcode = f77_get_dynamic_upperbound (type, &upper_bound);
384
385 if (retcode == BOUND_FETCH_ERROR)
386 fprintf_filtered (stream, "character*???");
387 else
388 fprintf_filtered (stream, "character*%d", upper_bound);
389 }
390 break;
391
392 case TYPE_CODE_STRUCT:
393 fprintfi_filtered (level, stream, "Type ");
394 fputs_filtered (TYPE_TAG_NAME (type), stream);
395 fputs_filtered ("\n", stream);
396 for (index = 0; index < TYPE_NFIELDS (type); index++)
397 {
398 f_print_type (TYPE_FIELD_TYPE (type, index), "", stream, show, level + 4);
399 fputs_filtered (" :: ", stream);
400 fputs_filtered (TYPE_FIELD_NAME (type, index), stream);
401 fputs_filtered ("\n", stream);
402 }
403 fprintfi_filtered (level, stream, "End Type ");
404 fputs_filtered (TYPE_TAG_NAME (type), stream);
405 break;
406
407 default_case:
408 default:
409 /* Handle types not explicitly handled by the other cases,
410 such as fundamental types. For these, just print whatever
411 the type name is, as recorded in the type itself. If there
412 is no type name, then complain. */
413 if (TYPE_NAME (type) != NULL)
414 fprintfi_filtered (level, stream, "%s", TYPE_NAME (type));
415 else
416 error (_("Invalid type code (%d) in symbol table."), TYPE_CODE (type));
417 break;
418 }
419 }