* stabsread.c (read_array_type): Allow negative array bounds,
[binutils-gdb.git] / gdb / ch-typeprint.c
1 /* Support for printing Chill types for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "obstack.h"
22 #include "bfd.h" /* Binary File Description */
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "value.h"
27 #include "gdbcore.h"
28 #include "target.h"
29 #include "command.h"
30 #include "gdbcmd.h"
31 #include "language.h"
32 #include "demangle.h"
33 #include "ch-lang.h"
34 #include "typeprint.h"
35
36 #include <string.h>
37 #include <errno.h>
38
39 static void
40 chill_type_print_base PARAMS ((struct type *, GDB_FILE *, int, int));
41
42 void
43 chill_print_type (type, varstring, stream, show, level)
44 struct type *type;
45 char *varstring;
46 GDB_FILE *stream;
47 int show;
48 int level;
49 {
50 if (varstring != NULL && *varstring != '\0')
51 {
52 fputs_filtered (varstring, stream);
53 fputs_filtered (" ", stream);
54 }
55 chill_type_print_base (type, stream, show, level);
56 }
57
58 /* Print the name of the type (or the ultimate pointer target,
59 function value or array element).
60
61 SHOW nonzero means don't print this type as just its name;
62 show its real definition even if it has a name.
63 SHOW zero means print just typename or tag if there is one
64 SHOW negative means abbreviate structure elements.
65 SHOW is decremented for printing of structure elements.
66
67 LEVEL is the depth to indent by.
68 We increase it for some recursive calls. */
69
70 static void
71 chill_type_print_base (type, stream, show, level)
72 struct type *type;
73 GDB_FILE *stream;
74 int show;
75 int level;
76 {
77 char *name;
78 register int len;
79 register int i;
80 struct type *index_type;
81 struct type *range_type;
82 LONGEST low_bound;
83 LONGEST high_bound;
84
85 QUIT;
86
87 wrap_here (" ");
88 if (type == NULL)
89 {
90 fputs_filtered ("<type unknown>", stream);
91 return;
92 }
93
94 /* When SHOW is zero or less, and there is a valid type name, then always
95 just print the type name directly from the type. */
96
97 if ((show <= 0) && (TYPE_NAME (type) != NULL))
98 {
99 fputs_filtered (TYPE_NAME (type), stream);
100 return;
101 }
102
103 check_stub_type (type);
104
105 switch (TYPE_CODE (type))
106 {
107 case TYPE_CODE_PTR:
108 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
109 {
110 fprintf_filtered (stream, "PTR");
111 break;
112 }
113 fprintf_filtered (stream, "REF ");
114 chill_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
115 break;
116
117 case TYPE_CODE_BOOL:
118 fprintf_filtered (stream, "BOOL");
119 break;
120
121 case TYPE_CODE_ARRAY:
122 range_type = TYPE_FIELD_TYPE (type, 0);
123 index_type = TYPE_TARGET_TYPE (range_type);
124 low_bound = TYPE_FIELD_BITPOS (range_type, 0);
125 high_bound = TYPE_FIELD_BITPOS (range_type, 1);
126 fputs_filtered ("ARRAY (", stream);
127 print_type_scalar (index_type, low_bound, stream);
128 fputs_filtered (":", stream);
129 print_type_scalar (index_type, high_bound, stream);
130 fputs_filtered (") ", stream);
131 chill_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
132 break;
133
134 case TYPE_CODE_STRING:
135 range_type = TYPE_FIELD_TYPE (type, 0);
136 index_type = TYPE_TARGET_TYPE (range_type);
137 high_bound = TYPE_FIELD_BITPOS (range_type, 1);
138 fputs_filtered ("CHAR (", stream);
139 print_type_scalar (index_type, high_bound + 1, stream);
140 fputs_filtered (")", stream);
141 break;
142
143 case TYPE_CODE_MEMBER:
144 fprintf_filtered (stream, "MEMBER ");
145 chill_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
146 break;
147 case TYPE_CODE_REF:
148 fprintf_filtered (stream, "/*LOC*/ ");
149 chill_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
150 break;
151 case TYPE_CODE_FUNC:
152 fprintf_filtered (stream, "PROC (?)");
153 chill_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
154 break;
155
156 case TYPE_CODE_STRUCT:
157 fprintf_filtered (stream, "STRUCT ");
158 if ((name = type_name_no_tag (type)) != NULL)
159 {
160 fputs_filtered (name, stream);
161 fputs_filtered (" ", stream);
162 wrap_here (" ");
163 }
164 if (show < 0)
165 {
166 fprintf_filtered (stream, "(...)");
167 }
168 else
169 {
170 fprintf_filtered (stream, "(\n");
171 if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0))
172 {
173 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
174 {
175 fprintfi_filtered (level + 4, stream, "<incomplete type>\n");
176 }
177 else
178 {
179 fprintfi_filtered (level + 4, stream, "<no data fields>\n");
180 }
181 }
182 else
183 {
184 len = TYPE_NFIELDS (type);
185 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
186 {
187 struct type *field_type = TYPE_FIELD_TYPE (type, i);
188 QUIT;
189 print_spaces_filtered (level + 4, stream);
190 if (TYPE_CODE (field_type) == TYPE_CODE_UNION)
191 { int j; /* variant number */
192 fputs_filtered ("CASE OF\n", stream);
193 for (j = 0; j < TYPE_NFIELDS (field_type); j++)
194 { int k; /* variant field index */
195 struct type *variant_type
196 = TYPE_FIELD_TYPE (field_type, j);
197 int var_len = TYPE_NFIELDS (variant_type);
198 print_spaces_filtered (level + 4, stream);
199 if (strcmp (TYPE_FIELD_NAME (field_type, j),
200 "else") == 0)
201 fputs_filtered ("ELSE\n", stream);
202 else
203 fputs_filtered (":\n", stream);
204 if (TYPE_CODE (variant_type) != TYPE_CODE_STRUCT)
205 error ("variant record confusion");
206 for (k = 0; k < var_len; k++)
207 {
208 print_spaces_filtered (level + 8, stream);
209 chill_print_type (TYPE_FIELD_TYPE (variant_type, k),
210 TYPE_FIELD_NAME (variant_type, k),
211 stream, show - 1, level + 8);
212 if (k < (var_len - 1))
213 fputs_filtered (",", stream);
214 fputs_filtered ("\n", stream);
215 }
216 }
217 fputs_filtered ("ESAC\n", stream);
218 }
219 else
220 chill_print_type (field_type,
221 TYPE_FIELD_NAME (type, i),
222 stream, show - 1, level + 4);
223 if (i < (len - 1))
224 {
225 fputs_filtered (",", stream);
226 }
227 fputs_filtered ("\n", stream);
228 }
229 }
230 fprintfi_filtered (level, stream, ")");
231 }
232 break;
233
234 case TYPE_CODE_RANGE:
235 if (TYPE_TARGET_TYPE (type))
236 {
237 chill_type_print_base (TYPE_TARGET_TYPE (type),
238 stream, show, level);
239 fputs_filtered (" (", stream);
240 print_type_scalar (TYPE_TARGET_TYPE (type),
241 TYPE_FIELD_BITPOS (type, 0), stream);
242 fputs_filtered (":", stream);
243 print_type_scalar (TYPE_TARGET_TYPE (type),
244 TYPE_FIELD_BITPOS (type, 1), stream);
245 fputs_filtered (")", stream);
246 }
247 else
248 fprintf_filtered (stream, "RANGE? (%s : %d)",
249 TYPE_FIELD_BITPOS (type, 0),
250 TYPE_FIELD_BITPOS (type, 1));
251 break;
252
253 case TYPE_CODE_VOID:
254 case TYPE_CODE_UNDEF:
255 case TYPE_CODE_ERROR:
256 case TYPE_CODE_ENUM:
257 case TYPE_CODE_UNION:
258 case TYPE_CODE_METHOD:
259 error ("missing language support in chill_type_print_base");
260 break;
261
262 default:
263
264 /* Handle types not explicitly handled by the other cases,
265 such as fundamental types. For these, just print whatever
266 the type name is, as recorded in the type itself. If there
267 is no type name, then complain. */
268
269 if (TYPE_NAME (type) != NULL)
270 {
271 fputs_filtered (TYPE_NAME (type), stream);
272 }
273 else
274 {
275 error ("Unrecognized type code (%d) in symbol table.",
276 TYPE_CODE (type));
277 }
278 break;
279 }
280 }