2002-01-20 Daniel Jacobowitz <drow@mvista.com>
[platform/upstream/binutils.git] / gdb / f-typeprint.c
1 /* Support for printing Fortran types for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991, 1993, 1994, 1995, 1996, 1998, 2000
3    Free Software Foundation, Inc.
4    Contributed by Motorola.  Adapted from the C version by Farooq Butt
5    (fmbutt@engage.sps.mot.com).
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 #include "defs.h"
25 #include "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 (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   register 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_MEMBER
75         || code == TYPE_CODE_REF)))
76     fputs_filtered (" ", stream);
77   f_type_print_varspec_prefix (type, stream, show, 0);
78
79   fputs_filtered (varstring, stream);
80
81   /* For demangled function names, we have the arglist as part of the name,
82      so don't print an additional pair of ()'s */
83
84   demangled_args = varstring[strlen (varstring) - 1] == ')';
85   f_type_print_varspec_suffix (type, stream, show, 0, demangled_args);
86 }
87
88 /* Print any asterisks or open-parentheses needed before the
89    variable name (to describe its type).
90
91    On outermost call, pass 0 for PASSED_A_PTR.
92    On outermost call, SHOW > 0 means should ignore
93    any typename for TYPE and show its details.
94    SHOW is always zero on recursive calls.  */
95
96 void
97 f_type_print_varspec_prefix (struct type *type, struct ui_file *stream,
98                              int show, int passed_a_ptr)
99 {
100   if (type == 0)
101     return;
102
103   if (TYPE_NAME (type) && show <= 0)
104     return;
105
106   QUIT;
107
108   switch (TYPE_CODE (type))
109     {
110     case TYPE_CODE_PTR:
111       f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
112       break;
113
114     case TYPE_CODE_FUNC:
115       f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
116       if (passed_a_ptr)
117         fprintf_filtered (stream, "(");
118       break;
119
120     case TYPE_CODE_ARRAY:
121       f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
122       break;
123
124     case TYPE_CODE_UNDEF:
125     case TYPE_CODE_STRUCT:
126     case TYPE_CODE_UNION:
127     case TYPE_CODE_ENUM:
128     case TYPE_CODE_INT:
129     case TYPE_CODE_FLT:
130     case TYPE_CODE_VOID:
131     case TYPE_CODE_ERROR:
132     case TYPE_CODE_CHAR:
133     case TYPE_CODE_BOOL:
134     case TYPE_CODE_SET:
135     case TYPE_CODE_RANGE:
136     case TYPE_CODE_STRING:
137     case TYPE_CODE_BITSTRING:
138     case TYPE_CODE_METHOD:
139     case TYPE_CODE_MEMBER:
140     case TYPE_CODE_REF:
141     case TYPE_CODE_COMPLEX:
142     case TYPE_CODE_TYPEDEF:
143       /* These types need no prefix.  They are listed here so that
144          gcc -Wall will reveal any types that haven't been handled.  */
145       break;
146     }
147 }
148
149 /* Print any array sizes, function arguments or close parentheses
150    needed after the variable name (to describe its type).
151    Args work like c_type_print_varspec_prefix.  */
152
153 static void
154 f_type_print_varspec_suffix (struct type *type, struct ui_file *stream,
155                              int show, int passed_a_ptr, int demangled_args)
156 {
157   int upper_bound, lower_bound;
158   int lower_bound_was_default = 0;
159   static int arrayprint_recurse_level = 0;
160   int retcode;
161
162   if (type == 0)
163     return;
164
165   if (TYPE_NAME (type) && show <= 0)
166     return;
167
168   QUIT;
169
170   switch (TYPE_CODE (type))
171     {
172     case TYPE_CODE_ARRAY:
173       arrayprint_recurse_level++;
174
175       if (arrayprint_recurse_level == 1)
176         fprintf_filtered (stream, "(");
177
178       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY)
179         f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
180
181       retcode = f77_get_dynamic_lowerbound (type, &lower_bound);
182
183       lower_bound_was_default = 0;
184
185       if (retcode == BOUND_FETCH_ERROR)
186         fprintf_filtered (stream, "???");
187       else if (lower_bound == 1)        /* The default */
188         lower_bound_was_default = 1;
189       else
190         fprintf_filtered (stream, "%d", lower_bound);
191
192       if (lower_bound_was_default)
193         lower_bound_was_default = 0;
194       else
195         fprintf_filtered (stream, ":");
196
197       /* Make sure that, if we have an assumed size array, we
198          print out a warning and print the upperbound as '*' */
199
200       if (TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
201         fprintf_filtered (stream, "*");
202       else
203         {
204           retcode = f77_get_dynamic_upperbound (type, &upper_bound);
205
206           if (retcode == BOUND_FETCH_ERROR)
207             fprintf_filtered (stream, "???");
208           else
209             fprintf_filtered (stream, "%d", upper_bound);
210         }
211
212       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_ARRAY)
213         f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
214       if (arrayprint_recurse_level == 1)
215         fprintf_filtered (stream, ")");
216       else
217         fprintf_filtered (stream, ",");
218       arrayprint_recurse_level--;
219       break;
220
221     case TYPE_CODE_PTR:
222     case TYPE_CODE_REF:
223       f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0);
224       fprintf_filtered (stream, ")");
225       break;
226
227     case TYPE_CODE_FUNC:
228       f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
229                                    passed_a_ptr, 0);
230       if (passed_a_ptr)
231         fprintf_filtered (stream, ")");
232
233       fprintf_filtered (stream, "()");
234       break;
235
236     case TYPE_CODE_UNDEF:
237     case TYPE_CODE_STRUCT:
238     case TYPE_CODE_UNION:
239     case TYPE_CODE_ENUM:
240     case TYPE_CODE_INT:
241     case TYPE_CODE_FLT:
242     case TYPE_CODE_VOID:
243     case TYPE_CODE_ERROR:
244     case TYPE_CODE_CHAR:
245     case TYPE_CODE_BOOL:
246     case TYPE_CODE_SET:
247     case TYPE_CODE_RANGE:
248     case TYPE_CODE_STRING:
249     case TYPE_CODE_BITSTRING:
250     case TYPE_CODE_METHOD:
251     case TYPE_CODE_MEMBER:
252     case TYPE_CODE_COMPLEX:
253     case TYPE_CODE_TYPEDEF:
254       /* These types do not need a suffix.  They are listed so that
255          gcc -Wall will report types that may not have been considered.  */
256       break;
257     }
258 }
259
260 static void
261 print_equivalent_f77_float_type (struct type *type, struct ui_file *stream)
262 {
263   /* Override type name "float" and make it the
264      appropriate real. XLC stupidly outputs -12 as a type
265      for real when it really should be outputting -18 */
266
267   fprintf_filtered (stream, "real*%d", TYPE_LENGTH (type));
268 }
269
270 /* Print the name of the type (or the ultimate pointer target,
271    function value or array element), or the description of a
272    structure or union.
273
274    SHOW nonzero means don't print this type as just its name;
275    show its real definition even if it has a name.
276    SHOW zero means print just typename or struct tag if there is one
277    SHOW negative means abbreviate structure elements.
278    SHOW is decremented for printing of structure elements.
279
280    LEVEL is the depth to indent by.
281    We increase it for some recursive calls.  */
282
283 void
284 f_type_print_base (struct type *type, struct ui_file *stream, int show,
285                    int level)
286 {
287   int retcode;
288   int upper_bound;
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 (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_VOID:
331       fprintf_filtered (stream, "VOID");
332       break;
333
334     case TYPE_CODE_UNDEF:
335       fprintf_filtered (stream, "struct <unknown>");
336       break;
337
338     case TYPE_CODE_ERROR:
339       fprintf_filtered (stream, "<unknown type>");
340       break;
341
342     case TYPE_CODE_RANGE:
343       /* This should not occur */
344       fprintf_filtered (stream, "<range type>");
345       break;
346
347     case TYPE_CODE_CHAR:
348       /* Override name "char" and make it "character" */
349       fprintf_filtered (stream, "character");
350       break;
351
352     case TYPE_CODE_INT:
353       /* There may be some character types that attempt to come
354          through as TYPE_CODE_INT since dbxstclass.h is so
355          C-oriented, we must change these to "character" from "char".  */
356
357       if (STREQ (TYPE_NAME (type), "char"))
358         fprintf_filtered (stream, "character");
359       else
360         goto default_case;
361       break;
362
363     case TYPE_CODE_COMPLEX:
364       fprintf_filtered (stream, "complex*%d", TYPE_LENGTH (type));
365       break;
366
367     case TYPE_CODE_FLT:
368       print_equivalent_f77_float_type (type, stream);
369       break;
370
371     case TYPE_CODE_STRING:
372       /* Strings may have dynamic upperbounds (lengths) like arrays. */
373
374       if (TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
375         fprintf_filtered (stream, "character*(*)");
376       else
377         {
378           retcode = f77_get_dynamic_upperbound (type, &upper_bound);
379
380           if (retcode == BOUND_FETCH_ERROR)
381             fprintf_filtered (stream, "character*???");
382           else
383             fprintf_filtered (stream, "character*%d", upper_bound);
384         }
385       break;
386
387     default_case:
388     default:
389       /* Handle types not explicitly handled by the other cases,
390          such as fundamental types.  For these, just print whatever
391          the type name is, as recorded in the type itself.  If there
392          is no type name, then complain. */
393       if (TYPE_NAME (type) != NULL)
394         fputs_filtered (TYPE_NAME (type), stream);
395       else
396         error ("Invalid type code (%d) in symbol table.", TYPE_CODE (type));
397       break;
398     }
399 }