Merge branch 'vendor/OPENSSH'
[dragonfly.git] / contrib / gdb-7 / 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, 2008, 2009 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 f_type_print_varspec_suffix (struct type *, struct ui_file *, int,
43                                          int, int, int);
44
45 void f_type_print_varspec_prefix (struct type *, struct ui_file *,
46                                   int, int);
47
48 void f_type_print_base (struct type *, struct ui_file *, int, int);
49 \f
50
51 /* LEVEL is the depth to indent lines by.  */
52
53 void
54 f_print_type (struct type *type, char *varstring, struct ui_file *stream,
55               int show, int level)
56 {
57   enum type_code code;
58   int demangled_args;
59
60   f_type_print_base (type, stream, show, level);
61   code = TYPE_CODE (type);
62   if ((varstring != NULL && *varstring != '\0')
63       ||
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        &&
68        (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
69         || code == TYPE_CODE_METHOD
70         || code == TYPE_CODE_ARRAY
71         || code == TYPE_CODE_REF)))
72     fputs_filtered (" ", stream);
73   f_type_print_varspec_prefix (type, stream, show, 0);
74
75   if (varstring != NULL)
76     {
77       fputs_filtered (varstring, stream);
78
79       /* For demangled function names, we have the arglist as part of the name,
80          so don't print an additional pair of ()'s */
81
82       demangled_args = varstring[strlen (varstring) - 1] == ')'; 
83       f_type_print_varspec_suffix (type, stream, show, 0, demangled_args, 0);
84    }
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                              int arrayprint_recurse_level)
155 {
156   int upper_bound, lower_bound;
157   int retcode;
158   /* No static variables are permitted as an error call may occur during
159      execution of this function.  */
160
161   if (type == 0)
162     return;
163
164   if (TYPE_NAME (type) && show <= 0)
165     return;
166
167   QUIT;
168
169   switch (TYPE_CODE (type))
170     {
171     case TYPE_CODE_ARRAY:
172       arrayprint_recurse_level++;
173
174       if (arrayprint_recurse_level == 1)
175         fprintf_filtered (stream, "(");
176
177       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY)
178         f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0,
179                                      arrayprint_recurse_level);
180
181       lower_bound = f77_get_lowerbound (type);
182       if (lower_bound != 1)     /* Not the default.  */
183         fprintf_filtered (stream, "%d:", lower_bound);
184
185       /* Make sure that, if we have an assumed size array, we
186          print out a warning and print the upperbound as '*' */
187
188       if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
189         fprintf_filtered (stream, "*");
190       else
191         {
192           upper_bound = f77_get_upperbound (type);
193           fprintf_filtered (stream, "%d", upper_bound);
194         }
195
196       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_ARRAY)
197         f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0,
198                                      arrayprint_recurse_level);
199       if (arrayprint_recurse_level == 1)
200         fprintf_filtered (stream, ")");
201       else
202         fprintf_filtered (stream, ",");
203       arrayprint_recurse_level--;
204       break;
205
206     case TYPE_CODE_PTR:
207     case TYPE_CODE_REF:
208       f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0,
209                                    arrayprint_recurse_level);
210       fprintf_filtered (stream, ")");
211       break;
212
213     case TYPE_CODE_FUNC:
214       f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
215                                    passed_a_ptr, 0, arrayprint_recurse_level);
216       if (passed_a_ptr)
217         fprintf_filtered (stream, ")");
218
219       fprintf_filtered (stream, "()");
220       break;
221
222     case TYPE_CODE_UNDEF:
223     case TYPE_CODE_STRUCT:
224     case TYPE_CODE_UNION:
225     case TYPE_CODE_ENUM:
226     case TYPE_CODE_INT:
227     case TYPE_CODE_FLT:
228     case TYPE_CODE_VOID:
229     case TYPE_CODE_ERROR:
230     case TYPE_CODE_CHAR:
231     case TYPE_CODE_BOOL:
232     case TYPE_CODE_SET:
233     case TYPE_CODE_RANGE:
234     case TYPE_CODE_STRING:
235     case TYPE_CODE_BITSTRING:
236     case TYPE_CODE_METHOD:
237     case TYPE_CODE_COMPLEX:
238     case TYPE_CODE_TYPEDEF:
239       /* These types do not need a suffix.  They are listed so that
240          gcc -Wall will report types that may not have been considered.  */
241       break;
242     }
243 }
244
245 /* Print the name of the type (or the ultimate pointer target,
246    function value or array element), or the description of a
247    structure or union.
248
249    SHOW nonzero means don't print this type as just its name;
250    show its real definition even if it has a name.
251    SHOW zero means print just typename or struct tag if there is one
252    SHOW negative means abbreviate structure elements.
253    SHOW is decremented for printing of structure elements.
254
255    LEVEL is the depth to indent by.
256    We increase it for some recursive calls.  */
257
258 void
259 f_type_print_base (struct type *type, struct ui_file *stream, int show,
260                    int level)
261 {
262   int retcode;
263   int upper_bound;
264
265   int index;
266
267   QUIT;
268
269   wrap_here ("    ");
270   if (type == NULL)
271     {
272       fputs_filtered ("<type unknown>", stream);
273       return;
274     }
275
276   /* When SHOW is zero or less, and there is a valid type name, then always
277      just print the type name directly from the type. */
278
279   if ((show <= 0) && (TYPE_NAME (type) != NULL))
280     {
281       fputs_filtered (TYPE_NAME (type), stream);
282       return;
283     }
284
285   if (TYPE_CODE (type) != TYPE_CODE_TYPEDEF)
286     CHECK_TYPEDEF (type);
287
288   switch (TYPE_CODE (type))
289     {
290     case TYPE_CODE_TYPEDEF:
291       f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
292       break;
293
294     case TYPE_CODE_ARRAY:
295     case TYPE_CODE_FUNC:
296       f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
297       break;
298
299     case TYPE_CODE_PTR:
300       fprintf_filtered (stream, "PTR TO -> ( ");
301       f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
302       break;
303
304     case TYPE_CODE_REF:
305       fprintf_filtered (stream, "REF TO -> ( ");
306       f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
307       break;
308
309     case TYPE_CODE_VOID:
310       fprintfi_filtered (level, stream, "VOID");
311       break;
312
313     case TYPE_CODE_UNDEF:
314       fprintfi_filtered (level, stream, "struct <unknown>");
315       break;
316
317     case TYPE_CODE_ERROR:
318       fprintfi_filtered (level, stream, "<unknown type>");
319       break;
320
321     case TYPE_CODE_RANGE:
322       /* This should not occur */
323       fprintfi_filtered (level, stream, "<range type>");
324       break;
325
326     case TYPE_CODE_CHAR:
327       /* Override name "char" and make it "character" */
328       fprintfi_filtered (level, stream, "character");
329       break;
330
331     case TYPE_CODE_INT:
332       /* There may be some character types that attempt to come
333          through as TYPE_CODE_INT since dbxstclass.h is so
334          C-oriented, we must change these to "character" from "char".  */
335
336       if (strcmp (TYPE_NAME (type), "char") == 0)
337         fprintfi_filtered (level, stream, "character");
338       else
339         goto default_case;
340       break;
341
342     case TYPE_CODE_STRING:
343       /* Strings may have dynamic upperbounds (lengths) like arrays. */
344
345       if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
346         fprintfi_filtered (level, stream, "character*(*)");
347       else
348         {
349           upper_bound = f77_get_upperbound (type);
350           fprintf_filtered (stream, "character*%d", upper_bound);
351         }
352       break;
353
354     case TYPE_CODE_STRUCT:
355     case TYPE_CODE_UNION:
356       if (TYPE_CODE (type) == TYPE_CODE_UNION)
357         fprintfi_filtered (level, stream, "Type, C_Union :: ");
358       else
359         fprintfi_filtered (level, stream, "Type ");
360       fputs_filtered (TYPE_TAG_NAME (type), stream);
361       fputs_filtered ("\n", stream);
362       for (index = 0; index < TYPE_NFIELDS (type); index++)
363         {
364           f_type_print_base (TYPE_FIELD_TYPE (type, index), stream, show,
365                              level + 4);
366           fputs_filtered (" :: ", stream);
367           fputs_filtered (TYPE_FIELD_NAME (type, index), stream);
368           f_type_print_varspec_suffix (TYPE_FIELD_TYPE (type, index),
369                                        stream, 0, 0, 0, 0);
370           fputs_filtered ("\n", stream);
371         } 
372       fprintfi_filtered (level, stream, "End Type ");
373       fputs_filtered (TYPE_TAG_NAME (type), stream);
374       break;
375
376     default_case:
377     default:
378       /* Handle types not explicitly handled by the other cases,
379          such as fundamental types.  For these, just print whatever
380          the type name is, as recorded in the type itself.  If there
381          is no type name, then complain. */
382       if (TYPE_NAME (type) != NULL)
383         fprintfi_filtered (level, stream, "%s", TYPE_NAME (type));
384       else
385         error (_("Invalid type code (%d) in symbol table."), TYPE_CODE (type));
386       break;
387     }
388 }