Update/correct copyright notices.
[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 "command.h"
34 #include "gdbcmd.h"
35 #include "language.h"
36 #include "demangle.h"
37 #include "f-lang.h"
38 #include "typeprint.h"
39 #include "frame.h"              /* ??? */
40
41 #include "gdb_string.h"
42 #include <errno.h>
43
44 #if 0                           /* Currently unused */
45 static void f_type_print_args (struct type *, struct ui_file *);
46 #endif
47
48 static void print_equivalent_f77_float_type (struct type *,
49                                              struct ui_file *);
50
51 static void f_type_print_varspec_suffix (struct type *, struct ui_file *,
52                                          int, int, int);
53
54 void f_type_print_varspec_prefix (struct type *, struct ui_file *,
55                                   int, int);
56
57 void f_type_print_base (struct type *, struct ui_file *, int, int);
58 \f
59
60 /* LEVEL is the depth to indent lines by.  */
61
62 void
63 f_print_type (struct type *type, char *varstring, struct ui_file *stream,
64               int show, int level)
65 {
66   register enum type_code code;
67   int demangled_args;
68
69   f_type_print_base (type, stream, show, level);
70   code = TYPE_CODE (type);
71   if ((varstring != NULL && *varstring != '\0')
72       ||
73   /* Need a space if going to print stars or brackets;
74      but not if we will print just a type name.  */
75       ((show > 0 || TYPE_NAME (type) == 0)
76        &&
77        (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
78         || code == TYPE_CODE_METHOD
79         || code == TYPE_CODE_ARRAY
80         || code == TYPE_CODE_MEMBER
81         || code == TYPE_CODE_REF)))
82     fputs_filtered (" ", stream);
83   f_type_print_varspec_prefix (type, stream, show, 0);
84
85   fputs_filtered (varstring, stream);
86
87   /* For demangled function names, we have the arglist as part of the name,
88      so don't print an additional pair of ()'s */
89
90   demangled_args = varstring[strlen (varstring) - 1] == ')';
91   f_type_print_varspec_suffix (type, stream, show, 0, demangled_args);
92 }
93
94 /* Print any asterisks or open-parentheses needed before the
95    variable name (to describe its type).
96
97    On outermost call, pass 0 for PASSED_A_PTR.
98    On outermost call, SHOW > 0 means should ignore
99    any typename for TYPE and show its details.
100    SHOW is always zero on recursive calls.  */
101
102 void
103 f_type_print_varspec_prefix (struct type *type, struct ui_file *stream,
104                              int show, int passed_a_ptr)
105 {
106   if (type == 0)
107     return;
108
109   if (TYPE_NAME (type) && show <= 0)
110     return;
111
112   QUIT;
113
114   switch (TYPE_CODE (type))
115     {
116     case TYPE_CODE_PTR:
117       f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
118       break;
119
120     case TYPE_CODE_FUNC:
121       f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
122       if (passed_a_ptr)
123         fprintf_filtered (stream, "(");
124       break;
125
126     case TYPE_CODE_ARRAY:
127       f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
128       break;
129
130     case TYPE_CODE_UNDEF:
131     case TYPE_CODE_STRUCT:
132     case TYPE_CODE_UNION:
133     case TYPE_CODE_ENUM:
134     case TYPE_CODE_INT:
135     case TYPE_CODE_FLT:
136     case TYPE_CODE_VOID:
137     case TYPE_CODE_ERROR:
138     case TYPE_CODE_CHAR:
139     case TYPE_CODE_BOOL:
140     case TYPE_CODE_SET:
141     case TYPE_CODE_RANGE:
142     case TYPE_CODE_STRING:
143     case TYPE_CODE_BITSTRING:
144     case TYPE_CODE_METHOD:
145     case TYPE_CODE_MEMBER:
146     case TYPE_CODE_REF:
147     case TYPE_CODE_COMPLEX:
148     case TYPE_CODE_TYPEDEF:
149       /* These types need no prefix.  They are listed here so that
150          gcc -Wall will reveal any types that haven't been handled.  */
151       break;
152     }
153 }
154
155 #if 0                           /* Currently unused */
156
157 static void
158 f_type_print_args (struct type *type, struct ui_file *stream)
159 {
160   int i;
161   struct type **args;
162
163   fprintf_filtered (stream, "(");
164   args = TYPE_ARG_TYPES (type);
165   if (args != NULL)
166     {
167       if (args[1] == NULL)
168         {
169           fprintf_filtered (stream, "...");
170         }
171       else
172         {
173           for (i = 1; args[i] != NULL && args[i]->code != TYPE_CODE_VOID; i++)
174             {
175               f_print_type (args[i], "", stream, -1, 0);
176               if (args[i + 1] == NULL)
177                 fprintf_filtered (stream, "...");
178               else if (args[i + 1]->code != TYPE_CODE_VOID)
179                 {
180                   fprintf_filtered (stream, ",");
181                   wrap_here ("    ");
182                 }
183             }
184         }
185     }
186   fprintf_filtered (stream, ")");
187 }
188
189 #endif /* 0 */
190
191 /* Print any array sizes, function arguments or close parentheses
192    needed after the variable name (to describe its type).
193    Args work like c_type_print_varspec_prefix.  */
194
195 static void
196 f_type_print_varspec_suffix (struct type *type, struct ui_file *stream,
197                              int show, int passed_a_ptr, int demangled_args)
198 {
199   int upper_bound, lower_bound;
200   int lower_bound_was_default = 0;
201   static int arrayprint_recurse_level = 0;
202   int retcode;
203
204   if (type == 0)
205     return;
206
207   if (TYPE_NAME (type) && show <= 0)
208     return;
209
210   QUIT;
211
212   switch (TYPE_CODE (type))
213     {
214     case TYPE_CODE_ARRAY:
215       arrayprint_recurse_level++;
216
217       if (arrayprint_recurse_level == 1)
218         fprintf_filtered (stream, "(");
219
220       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY)
221         f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
222
223       retcode = f77_get_dynamic_lowerbound (type, &lower_bound);
224
225       lower_bound_was_default = 0;
226
227       if (retcode == BOUND_FETCH_ERROR)
228         fprintf_filtered (stream, "???");
229       else if (lower_bound == 1)        /* The default */
230         lower_bound_was_default = 1;
231       else
232         fprintf_filtered (stream, "%d", lower_bound);
233
234       if (lower_bound_was_default)
235         lower_bound_was_default = 0;
236       else
237         fprintf_filtered (stream, ":");
238
239       /* Make sure that, if we have an assumed size array, we
240          print out a warning and print the upperbound as '*' */
241
242       if (TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
243         fprintf_filtered (stream, "*");
244       else
245         {
246           retcode = f77_get_dynamic_upperbound (type, &upper_bound);
247
248           if (retcode == BOUND_FETCH_ERROR)
249             fprintf_filtered (stream, "???");
250           else
251             fprintf_filtered (stream, "%d", upper_bound);
252         }
253
254       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_ARRAY)
255         f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
256       if (arrayprint_recurse_level == 1)
257         fprintf_filtered (stream, ")");
258       else
259         fprintf_filtered (stream, ",");
260       arrayprint_recurse_level--;
261       break;
262
263     case TYPE_CODE_PTR:
264     case TYPE_CODE_REF:
265       f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0);
266       fprintf_filtered (stream, ")");
267       break;
268
269     case TYPE_CODE_FUNC:
270       f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
271                                    passed_a_ptr, 0);
272       if (passed_a_ptr)
273         fprintf_filtered (stream, ")");
274
275       fprintf_filtered (stream, "()");
276       break;
277
278     case TYPE_CODE_UNDEF:
279     case TYPE_CODE_STRUCT:
280     case TYPE_CODE_UNION:
281     case TYPE_CODE_ENUM:
282     case TYPE_CODE_INT:
283     case TYPE_CODE_FLT:
284     case TYPE_CODE_VOID:
285     case TYPE_CODE_ERROR:
286     case TYPE_CODE_CHAR:
287     case TYPE_CODE_BOOL:
288     case TYPE_CODE_SET:
289     case TYPE_CODE_RANGE:
290     case TYPE_CODE_STRING:
291     case TYPE_CODE_BITSTRING:
292     case TYPE_CODE_METHOD:
293     case TYPE_CODE_MEMBER:
294     case TYPE_CODE_COMPLEX:
295     case TYPE_CODE_TYPEDEF:
296       /* These types do not need a suffix.  They are listed so that
297          gcc -Wall will report types that may not have been considered.  */
298       break;
299     }
300 }
301
302 static void
303 print_equivalent_f77_float_type (struct type *type, struct ui_file *stream)
304 {
305   /* Override type name "float" and make it the
306      appropriate real. XLC stupidly outputs -12 as a type
307      for real when it really should be outputting -18 */
308
309   fprintf_filtered (stream, "real*%d", TYPE_LENGTH (type));
310 }
311
312 /* Print the name of the type (or the ultimate pointer target,
313    function value or array element), or the description of a
314    structure or union.
315
316    SHOW nonzero means don't print this type as just its name;
317    show its real definition even if it has a name.
318    SHOW zero means print just typename or struct tag if there is one
319    SHOW negative means abbreviate structure elements.
320    SHOW is decremented for printing of structure elements.
321
322    LEVEL is the depth to indent by.
323    We increase it for some recursive calls.  */
324
325 void
326 f_type_print_base (struct type *type, struct ui_file *stream, int show,
327                    int level)
328 {
329   int retcode;
330   int upper_bound;
331
332   QUIT;
333
334   wrap_here ("    ");
335   if (type == NULL)
336     {
337       fputs_filtered ("<type unknown>", stream);
338       return;
339     }
340
341   /* When SHOW is zero or less, and there is a valid type name, then always
342      just print the type name directly from the type. */
343
344   if ((show <= 0) && (TYPE_NAME (type) != NULL))
345     {
346       if (TYPE_CODE (type) == TYPE_CODE_FLT)
347         print_equivalent_f77_float_type (type, stream);
348       else
349         fputs_filtered (TYPE_NAME (type), stream);
350       return;
351     }
352
353   if (TYPE_CODE (type) != TYPE_CODE_TYPEDEF)
354     CHECK_TYPEDEF (type);
355
356   switch (TYPE_CODE (type))
357     {
358     case TYPE_CODE_TYPEDEF:
359       f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
360       break;
361
362     case TYPE_CODE_ARRAY:
363     case TYPE_CODE_FUNC:
364       f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
365       break;
366
367     case TYPE_CODE_PTR:
368       fprintf_filtered (stream, "PTR TO -> ( ");
369       f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
370       break;
371
372     case TYPE_CODE_VOID:
373       fprintf_filtered (stream, "VOID");
374       break;
375
376     case TYPE_CODE_UNDEF:
377       fprintf_filtered (stream, "struct <unknown>");
378       break;
379
380     case TYPE_CODE_ERROR:
381       fprintf_filtered (stream, "<unknown type>");
382       break;
383
384     case TYPE_CODE_RANGE:
385       /* This should not occur */
386       fprintf_filtered (stream, "<range type>");
387       break;
388
389     case TYPE_CODE_CHAR:
390       /* Override name "char" and make it "character" */
391       fprintf_filtered (stream, "character");
392       break;
393
394     case TYPE_CODE_INT:
395       /* There may be some character types that attempt to come
396          through as TYPE_CODE_INT since dbxstclass.h is so
397          C-oriented, we must change these to "character" from "char".  */
398
399       if (STREQ (TYPE_NAME (type), "char"))
400         fprintf_filtered (stream, "character");
401       else
402         goto default_case;
403       break;
404
405     case TYPE_CODE_COMPLEX:
406       fprintf_filtered (stream, "complex*%d", TYPE_LENGTH (type));
407       break;
408
409     case TYPE_CODE_FLT:
410       print_equivalent_f77_float_type (type, stream);
411       break;
412
413     case TYPE_CODE_STRING:
414       /* Strings may have dynamic upperbounds (lengths) like arrays. */
415
416       if (TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
417         fprintf_filtered (stream, "character*(*)");
418       else
419         {
420           retcode = f77_get_dynamic_upperbound (type, &upper_bound);
421
422           if (retcode == BOUND_FETCH_ERROR)
423             fprintf_filtered (stream, "character*???");
424           else
425             fprintf_filtered (stream, "character*%d", upper_bound);
426         }
427       break;
428
429     default_case:
430     default:
431       /* Handle types not explicitly handled by the other cases,
432          such as fundamental types.  For these, just print whatever
433          the type name is, as recorded in the type itself.  If there
434          is no type name, then complain. */
435       if (TYPE_NAME (type) != NULL)
436         fputs_filtered (TYPE_NAME (type), stream);
437       else
438         error ("Invalid type code (%d) in symbol table.", TYPE_CODE (type));
439       break;
440     }
441 }