* ax-gdb.c (gen_exp_binop_rest) [BINOP_SUBSCRIPT]: Error out if
[binutils-gdb.git] / gdb / f-typeprint.c
blob0332932d9975bf6e7be58d830dc4bfe3270e3c98
1 /* Support for printing Fortran types for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1991, 1993, 1994, 1995, 1996, 1998, 2000,
4 2001, 2002, 2003, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 Contributed by Motorola. Adapted from the C version by Farooq Butt
8 (fmbutt@engage.sps.mot.com).
10 This file is part of GDB.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "defs.h"
26 #include "gdb_obstack.h"
27 #include "bfd.h"
28 #include "symtab.h"
29 #include "gdbtypes.h"
30 #include "expression.h"
31 #include "value.h"
32 #include "gdbcore.h"
33 #include "target.h"
34 #include "f-lang.h"
36 #include "gdb_string.h"
37 #include <errno.h>
39 #if 0 /* Currently unused */
40 static void f_type_print_args (struct type *, struct ui_file *);
41 #endif
43 static void f_type_print_varspec_suffix (struct type *, struct ui_file *, int,
44 int, int, int);
46 void f_type_print_varspec_prefix (struct type *, struct ui_file *,
47 int, int);
49 void f_type_print_base (struct type *, struct ui_file *, int, int);
52 /* LEVEL is the depth to indent lines by. */
54 void
55 f_print_type (struct type *type, char *varstring, struct ui_file *stream,
56 int show, int level)
58 enum type_code code;
59 int demangled_args;
61 f_type_print_base (type, stream, show, level);
62 code = TYPE_CODE (type);
63 if ((varstring != NULL && *varstring != '\0')
64 /* Need a space if going to print stars or brackets;
65 but not if we will print just a type name. */
66 || ((show > 0 || TYPE_NAME (type) == 0)
67 && (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
68 || code == TYPE_CODE_METHOD
69 || code == TYPE_CODE_ARRAY
70 || code == TYPE_CODE_REF)))
71 fputs_filtered (" ", stream);
72 f_type_print_varspec_prefix (type, stream, show, 0);
74 if (varstring != NULL)
76 fputs_filtered (varstring, stream);
78 /* For demangled function names, we have the arglist as part of the name,
79 so don't print an additional pair of ()'s */
81 demangled_args = varstring[strlen (varstring) - 1] == ')';
82 f_type_print_varspec_suffix (type, stream, show, 0, demangled_args, 0);
86 /* Print any asterisks or open-parentheses needed before the
87 variable name (to describe its type).
89 On outermost call, pass 0 for PASSED_A_PTR.
90 On outermost call, SHOW > 0 means should ignore
91 any typename for TYPE and show its details.
92 SHOW is always zero on recursive calls. */
94 void
95 f_type_print_varspec_prefix (struct type *type, struct ui_file *stream,
96 int show, int passed_a_ptr)
98 if (type == 0)
99 return;
101 if (TYPE_NAME (type) && show <= 0)
102 return;
104 QUIT;
106 switch (TYPE_CODE (type))
108 case TYPE_CODE_PTR:
109 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
110 break;
112 case TYPE_CODE_FUNC:
113 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
114 if (passed_a_ptr)
115 fprintf_filtered (stream, "(");
116 break;
118 case TYPE_CODE_ARRAY:
119 f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
120 break;
122 case TYPE_CODE_UNDEF:
123 case TYPE_CODE_STRUCT:
124 case TYPE_CODE_UNION:
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:
135 case TYPE_CODE_BITSTRING:
136 case TYPE_CODE_METHOD:
137 case TYPE_CODE_REF:
138 case TYPE_CODE_COMPLEX:
139 case TYPE_CODE_TYPEDEF:
140 /* These types need no prefix. They are listed here so that
141 gcc -Wall will reveal any types that haven't been handled. */
142 break;
146 /* Print any array sizes, function arguments or close parentheses
147 needed after the variable name (to describe its type).
148 Args work like c_type_print_varspec_prefix. */
150 static void
151 f_type_print_varspec_suffix (struct type *type, struct ui_file *stream,
152 int show, int passed_a_ptr, int demangled_args,
153 int arrayprint_recurse_level)
155 int upper_bound, lower_bound;
156 int retcode;
157 /* No static variables are permitted as an error call may occur during
158 execution of this function. */
160 if (type == 0)
161 return;
163 if (TYPE_NAME (type) && show <= 0)
164 return;
166 QUIT;
168 switch (TYPE_CODE (type))
170 case TYPE_CODE_ARRAY:
171 arrayprint_recurse_level++;
173 if (arrayprint_recurse_level == 1)
174 fprintf_filtered (stream, "(");
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 arrayprint_recurse_level);
180 lower_bound = f77_get_lowerbound (type);
181 if (lower_bound != 1) /* Not the default. */
182 fprintf_filtered (stream, "%d:", lower_bound);
184 /* Make sure that, if we have an assumed size array, we
185 print out a warning and print the upperbound as '*' */
187 if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
188 fprintf_filtered (stream, "*");
189 else
191 upper_bound = f77_get_upperbound (type);
192 fprintf_filtered (stream, "%d", upper_bound);
195 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_ARRAY)
196 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0,
197 arrayprint_recurse_level);
198 if (arrayprint_recurse_level == 1)
199 fprintf_filtered (stream, ")");
200 else
201 fprintf_filtered (stream, ",");
202 arrayprint_recurse_level--;
203 break;
205 case TYPE_CODE_PTR:
206 case TYPE_CODE_REF:
207 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0,
208 arrayprint_recurse_level);
209 fprintf_filtered (stream, ")");
210 break;
212 case TYPE_CODE_FUNC:
213 f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
214 passed_a_ptr, 0, arrayprint_recurse_level);
215 if (passed_a_ptr)
216 fprintf_filtered (stream, ")");
218 fprintf_filtered (stream, "()");
219 break;
221 case TYPE_CODE_UNDEF:
222 case TYPE_CODE_STRUCT:
223 case TYPE_CODE_UNION:
224 case TYPE_CODE_ENUM:
225 case TYPE_CODE_INT:
226 case TYPE_CODE_FLT:
227 case TYPE_CODE_VOID:
228 case TYPE_CODE_ERROR:
229 case TYPE_CODE_CHAR:
230 case TYPE_CODE_BOOL:
231 case TYPE_CODE_SET:
232 case TYPE_CODE_RANGE:
233 case TYPE_CODE_STRING:
234 case TYPE_CODE_BITSTRING:
235 case TYPE_CODE_METHOD:
236 case TYPE_CODE_COMPLEX:
237 case TYPE_CODE_TYPEDEF:
238 /* These types do not need a suffix. They are listed so that
239 gcc -Wall will report types that may not have been considered. */
240 break;
244 /* Print the name of the type (or the ultimate pointer target,
245 function value or array element), or the description of a
246 structure or union.
248 SHOW nonzero means don't print this type as just its name;
249 show its real definition even if it has a name.
250 SHOW zero means print just typename or struct tag if there is one
251 SHOW negative means abbreviate structure elements.
252 SHOW is decremented for printing of structure elements.
254 LEVEL is the depth to indent by.
255 We increase it for some recursive calls. */
257 void
258 f_type_print_base (struct type *type, struct ui_file *stream, int show,
259 int level)
261 int retcode;
262 int upper_bound;
264 int index;
266 QUIT;
268 wrap_here (" ");
269 if (type == NULL)
271 fputs_filtered ("<type unknown>", stream);
272 return;
275 /* When SHOW is zero or less, and there is a valid type name, then always
276 just print the type name directly from the type. */
278 if ((show <= 0) && (TYPE_NAME (type) != NULL))
280 fputs_filtered (TYPE_NAME (type), stream);
281 return;
284 if (TYPE_CODE (type) != TYPE_CODE_TYPEDEF)
285 CHECK_TYPEDEF (type);
287 switch (TYPE_CODE (type))
289 case TYPE_CODE_TYPEDEF:
290 f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
291 break;
293 case TYPE_CODE_ARRAY:
294 case TYPE_CODE_FUNC:
295 f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
296 break;
298 case TYPE_CODE_PTR:
299 fprintf_filtered (stream, "PTR TO -> ( ");
300 f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
301 break;
303 case TYPE_CODE_REF:
304 fprintf_filtered (stream, "REF TO -> ( ");
305 f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
306 break;
308 case TYPE_CODE_VOID:
309 fprintfi_filtered (level, stream, "VOID");
310 break;
312 case TYPE_CODE_UNDEF:
313 fprintfi_filtered (level, stream, "struct <unknown>");
314 break;
316 case TYPE_CODE_ERROR:
317 fprintfi_filtered (level, stream, "<unknown type>");
318 break;
320 case TYPE_CODE_RANGE:
321 /* This should not occur */
322 fprintfi_filtered (level, stream, "<range type>");
323 break;
325 case TYPE_CODE_CHAR:
326 /* Override name "char" and make it "character" */
327 fprintfi_filtered (level, stream, "character");
328 break;
330 case TYPE_CODE_INT:
331 /* There may be some character types that attempt to come
332 through as TYPE_CODE_INT since dbxstclass.h is so
333 C-oriented, we must change these to "character" from "char". */
335 if (strcmp (TYPE_NAME (type), "char") == 0)
336 fprintfi_filtered (level, stream, "character");
337 else
338 goto default_case;
339 break;
341 case TYPE_CODE_STRING:
342 /* Strings may have dynamic upperbounds (lengths) like arrays. */
344 if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
345 fprintfi_filtered (level, stream, "character*(*)");
346 else
348 upper_bound = f77_get_upperbound (type);
349 fprintf_filtered (stream, "character*%d", upper_bound);
351 break;
353 case TYPE_CODE_STRUCT:
354 case TYPE_CODE_UNION:
355 if (TYPE_CODE (type) == TYPE_CODE_UNION)
356 fprintfi_filtered (level, stream, "Type, C_Union :: ");
357 else
358 fprintfi_filtered (level, stream, "Type ");
359 fputs_filtered (TYPE_TAG_NAME (type), stream);
360 fputs_filtered ("\n", stream);
361 for (index = 0; index < TYPE_NFIELDS (type); index++)
363 f_type_print_base (TYPE_FIELD_TYPE (type, index), stream, show,
364 level + 4);
365 fputs_filtered (" :: ", stream);
366 fputs_filtered (TYPE_FIELD_NAME (type, index), stream);
367 f_type_print_varspec_suffix (TYPE_FIELD_TYPE (type, index),
368 stream, 0, 0, 0, 0);
369 fputs_filtered ("\n", stream);
371 fprintfi_filtered (level, stream, "End Type ");
372 fputs_filtered (TYPE_TAG_NAME (type), stream);
373 break;
375 default_case:
376 default:
377 /* Handle types not explicitly handled by the other cases,
378 such as fundamental types. For these, just print whatever
379 the type name is, as recorded in the type itself. If there
380 is no type name, then complain. */
381 if (TYPE_NAME (type) != NULL)
382 fprintfi_filtered (level, stream, "%s", TYPE_NAME (type));
383 else
384 error (_("Invalid type code (%d) in symbol table."), TYPE_CODE (type));
385 break;