Commit | Line | Data |
---|---|---|
a8a69e63 | 1 | /* Support for printing Chill values for GDB, the GNU debugger. |
67e9b3b3 PS |
2 | Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994 |
3 | Free Software Foundation, Inc. | |
a8a69e63 FF |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | #include "defs.h" | |
22 | #include "obstack.h" | |
23 | #include "symtab.h" | |
24 | #include "gdbtypes.h" | |
25 | #include "valprint.h" | |
26 | #include "expression.h" | |
8fbdca53 | 27 | #include "value.h" |
a8a69e63 | 28 | #include "language.h" |
5e81259d | 29 | #include "demangle.h" |
100f92e2 | 30 | #include "c-lang.h" /* For c_val_print */ |
1c95d7ab JK |
31 | #include "typeprint.h" |
32 | #include "ch-lang.h" | |
a8a69e63 | 33 | |
8fbdca53 | 34 | static void |
199b2450 | 35 | chill_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int, |
8fbdca53 FF |
36 | enum val_prettyprint, struct type **)); |
37 | ||
a8a69e63 FF |
38 | \f |
39 | /* Print data of type TYPE located at VALADDR (within GDB), which came from | |
40 | the inferior at address ADDRESS, onto stdio stream STREAM according to | |
41 | FORMAT (a letter or 0 for natural format). The data at VALADDR is in | |
42 | target byte order. | |
43 | ||
44 | If the data are a string pointer, returns the number of string characters | |
45 | printed. | |
46 | ||
47 | If DEREF_REF is nonzero, then dereference references, otherwise just print | |
48 | them like pointers. | |
49 | ||
50 | The PRETTY parameter controls prettyprinting. */ | |
51 | ||
52 | int | |
53 | chill_val_print (type, valaddr, address, stream, format, deref_ref, recurse, | |
54 | pretty) | |
55 | struct type *type; | |
56 | char *valaddr; | |
57 | CORE_ADDR address; | |
199b2450 | 58 | GDB_FILE *stream; |
a8a69e63 FF |
59 | int format; |
60 | int deref_ref; | |
61 | int recurse; | |
62 | enum val_prettyprint pretty; | |
63 | { | |
a8a69e63 | 64 | LONGEST val; |
ec16f701 | 65 | unsigned int i = 0; /* Number of characters printed. */ |
c7da3ed3 | 66 | struct type *elttype; |
c7da3ed3 | 67 | CORE_ADDR addr; |
a8a69e63 FF |
68 | |
69 | switch (TYPE_CODE (type)) | |
70 | { | |
71 | case TYPE_CODE_ARRAY: | |
72 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0) | |
73 | { | |
a8a69e63 FF |
74 | if (prettyprint_arrays) |
75 | { | |
76 | print_spaces_filtered (2 + 2 * recurse, stream); | |
77 | } | |
78 | fprintf_filtered (stream, "["); | |
a9b37611 FF |
79 | val_print_array_elements (type, valaddr, address, stream, format, |
80 | deref_ref, recurse, pretty, 0); | |
a8a69e63 FF |
81 | fprintf_filtered (stream, "]"); |
82 | } | |
83 | else | |
84 | { | |
85 | error ("unimplemented in chill_val_print; unspecified array length"); | |
86 | } | |
87 | break; | |
88 | ||
89 | case TYPE_CODE_INT: | |
90 | format = format ? format : output_format; | |
91 | if (format) | |
92 | { | |
93 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
94 | } | |
95 | else | |
96 | { | |
97 | val_print_type_code_int (type, valaddr, stream); | |
98 | } | |
99 | break; | |
100 | ||
101 | case TYPE_CODE_CHAR: | |
102 | format = format ? format : output_format; | |
103 | if (format) | |
104 | { | |
105 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
106 | } | |
107 | else | |
108 | { | |
109 | LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr), | |
110 | stream); | |
111 | } | |
112 | break; | |
113 | ||
114 | case TYPE_CODE_FLT: | |
115 | if (format) | |
116 | { | |
117 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
118 | } | |
119 | else | |
120 | { | |
121 | print_floating (valaddr, type, stream); | |
122 | } | |
123 | break; | |
124 | ||
125 | case TYPE_CODE_BOOL: | |
126 | format = format ? format : output_format; | |
127 | if (format) | |
128 | { | |
129 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
130 | } | |
131 | else | |
132 | { | |
61932a8e | 133 | /* FIXME: Why is this using builtin_type_chill_bool not type? */ |
a8a69e63 FF |
134 | val = unpack_long (builtin_type_chill_bool, valaddr); |
135 | fprintf_filtered (stream, val ? "TRUE" : "FALSE"); | |
136 | } | |
137 | break; | |
138 | ||
139 | case TYPE_CODE_UNDEF: | |
140 | /* This happens (without TYPE_FLAG_STUB set) on systems which don't use | |
141 | dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar" | |
142 | and no complete type for struct foo in that file. */ | |
143 | fprintf_filtered (stream, "<incomplete type>"); | |
144 | break; | |
145 | ||
146 | case TYPE_CODE_PTR: | |
c7da3ed3 FF |
147 | if (format && format != 's') |
148 | { | |
149 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
150 | break; | |
151 | } | |
152 | addr = unpack_pointer (type, valaddr); | |
153 | elttype = TYPE_TARGET_TYPE (type); | |
154 | ||
155 | if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) | |
156 | { | |
157 | /* Try to print what function it points to. */ | |
158 | print_address_demangle (addr, stream, demangle); | |
159 | /* Return value is irrelevant except for string pointers. */ | |
160 | return (0); | |
161 | } | |
162 | if (addressprint && format != 's') | |
163 | { | |
d24c0599 | 164 | print_address_numeric (addr, 1, stream); |
c7da3ed3 FF |
165 | } |
166 | ||
167 | /* For a pointer to char or unsigned char, also print the string | |
168 | pointed to, unless pointer is null. */ | |
c7da3ed3 FF |
169 | if (TYPE_LENGTH (elttype) == 1 |
170 | && TYPE_CODE (elttype) == TYPE_CODE_CHAR | |
171 | && (format == 0 || format == 's') | |
172 | && addr != 0 | |
173 | && /* If print_max is UINT_MAX, the alloca below will fail. | |
174 | In that case don't try to print the string. */ | |
175 | print_max < UINT_MAX) | |
176 | { | |
8fbdca53 | 177 | i = val_print_string (addr, 0, stream); |
c7da3ed3 FF |
178 | } |
179 | /* Return number of characters printed, plus one for the | |
180 | terminating null if we have "reached the end". */ | |
181 | return (i + (print_max && i != print_max)); | |
182 | break; | |
183 | ||
c4413e2c FF |
184 | case TYPE_CODE_STRING: |
185 | if (format && format != 's') | |
186 | { | |
187 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
188 | break; | |
189 | } | |
ec16f701 FF |
190 | i = TYPE_LENGTH (type); |
191 | LA_PRINT_STRING (stream, valaddr, i, 0); | |
c4413e2c FF |
192 | /* Return number of characters printed, plus one for the terminating |
193 | null if we have "reached the end". */ | |
194 | return (i + (print_max && i != print_max)); | |
195 | break; | |
196 | ||
cba00921 | 197 | case TYPE_CODE_BITSTRING: |
e909f287 PB |
198 | case TYPE_CODE_SET: |
199 | { | |
200 | struct type *range = TYPE_FIELD_TYPE (type, 0); | |
cba00921 PB |
201 | int low_bound = TYPE_LOW_BOUND (range); |
202 | int high_bound = TYPE_HIGH_BOUND (range); | |
e909f287 | 203 | int i; |
cba00921 | 204 | int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING; |
e909f287 | 205 | int need_comma = 0; |
e909f287 PB |
206 | |
207 | if (is_bitstring) | |
208 | fputs_filtered ("B'", stream); | |
209 | else | |
210 | fputs_filtered ("[", stream); | |
211 | for (i = low_bound; i <= high_bound; i++) | |
212 | { | |
213 | int element = value_bit_index (type, valaddr, i); | |
214 | if (is_bitstring) | |
215 | fprintf_filtered (stream, "%d", element); | |
216 | else if (element) | |
217 | { | |
218 | if (need_comma) | |
219 | fputs_filtered (", ", stream); | |
220 | print_type_scalar (TYPE_TARGET_TYPE (range), i, stream); | |
221 | need_comma = 1; | |
222 | ||
223 | /* Look for a continuous range of true elements. */ | |
224 | if (i+1 <= high_bound && value_bit_index (type, valaddr, ++i)) | |
225 | { | |
226 | int j = i; /* j is the upper bound so far of the range */ | |
227 | fputs_filtered (":", stream); | |
228 | while (i+1 <= high_bound | |
229 | && value_bit_index (type, valaddr, ++i)) | |
230 | j = i; | |
231 | print_type_scalar (TYPE_TARGET_TYPE (range), j, stream); | |
232 | } | |
233 | } | |
234 | } | |
235 | if (is_bitstring) | |
236 | fputs_filtered ("'", stream); | |
237 | else | |
238 | fputs_filtered ("]", stream); | |
239 | } | |
240 | break; | |
241 | ||
8fbdca53 | 242 | case TYPE_CODE_STRUCT: |
cba00921 PB |
243 | if (chill_is_varying_struct (type)) |
244 | { | |
245 | struct type *inner = TYPE_FIELD_TYPE (type, 1); | |
246 | long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr); | |
247 | char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8; | |
248 | ||
249 | switch (TYPE_CODE (inner)) | |
250 | { | |
251 | case TYPE_CODE_STRING: | |
252 | if (length > TYPE_LENGTH (type)) | |
253 | { | |
254 | fprintf_filtered (stream, | |
255 | "<dynamic length %d > static length %d>", | |
256 | length, TYPE_LENGTH (type)); | |
cba00921 PB |
257 | } |
258 | LA_PRINT_STRING (stream, data_addr, length, 0); | |
259 | return length; | |
1c95d7ab JK |
260 | default: |
261 | break; | |
cba00921 PB |
262 | } |
263 | } | |
8fbdca53 FF |
264 | chill_print_value_fields (type, valaddr, stream, format, recurse, pretty, |
265 | 0); | |
266 | break; | |
267 | ||
a8a69e63 | 268 | case TYPE_CODE_REF: |
8a177da6 PB |
269 | if (addressprint) |
270 | { | |
833e0d94 JK |
271 | fprintf_filtered (stream, "LOC("); |
272 | print_address_numeric | |
273 | (extract_address (valaddr, TARGET_PTR_BIT / HOST_CHAR_BIT), | |
d24c0599 | 274 | 1, |
833e0d94 JK |
275 | stream); |
276 | fprintf_filtered (stream, ")"); | |
8a177da6 PB |
277 | if (deref_ref) |
278 | fputs_filtered (": ", stream); | |
279 | } | |
280 | /* De-reference the reference. */ | |
281 | if (deref_ref) | |
282 | { | |
283 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF) | |
284 | { | |
82a2edfb | 285 | value_ptr deref_val = |
8a177da6 PB |
286 | value_at |
287 | (TYPE_TARGET_TYPE (type), | |
288 | unpack_pointer (lookup_pointer_type (builtin_type_void), | |
289 | valaddr)); | |
290 | val_print (VALUE_TYPE (deref_val), | |
291 | VALUE_CONTENTS (deref_val), | |
292 | VALUE_ADDRESS (deref_val), stream, format, | |
293 | deref_ref, recurse + 1, pretty); | |
294 | } | |
295 | else | |
296 | fputs_filtered ("???", stream); | |
297 | } | |
298 | break; | |
299 | ||
a8a69e63 | 300 | case TYPE_CODE_ENUM: |
8a177da6 PB |
301 | c_val_print (type, valaddr, address, stream, format, |
302 | deref_ref, recurse, pretty); | |
303 | break; | |
304 | ||
11b959da FF |
305 | case TYPE_CODE_RANGE: |
306 | if (TYPE_TARGET_TYPE (type)) | |
307 | chill_val_print (TYPE_TARGET_TYPE (type), valaddr, address, stream, | |
308 | format, deref_ref, recurse, pretty); | |
309 | break; | |
310 | ||
8a177da6 PB |
311 | case TYPE_CODE_MEMBER: |
312 | case TYPE_CODE_UNION: | |
a8a69e63 FF |
313 | case TYPE_CODE_FUNC: |
314 | case TYPE_CODE_VOID: | |
315 | case TYPE_CODE_ERROR: | |
a8a69e63 | 316 | default: |
11b959da | 317 | /* Let's defer printing to the C printer, rather than |
8a177da6 PB |
318 | print an error message. FIXME! */ |
319 | c_val_print (type, valaddr, address, stream, format, | |
320 | deref_ref, recurse, pretty); | |
a8a69e63 | 321 | } |
199b2450 | 322 | gdb_flush (stream); |
a8a69e63 FF |
323 | return (0); |
324 | } | |
8fbdca53 FF |
325 | |
326 | /* Mutually recursive subroutines of cplus_print_value and c_val_print to | |
327 | print out a structure's fields: cp_print_value_fields and cplus_print_value. | |
328 | ||
329 | TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the | |
330 | same meanings as in cplus_print_value and c_val_print. | |
331 | ||
332 | DONT_PRINT is an array of baseclass types that we | |
333 | should not print, or zero if called from top level. */ | |
334 | ||
335 | static void | |
336 | chill_print_value_fields (type, valaddr, stream, format, recurse, pretty, | |
337 | dont_print) | |
338 | struct type *type; | |
339 | char *valaddr; | |
199b2450 | 340 | GDB_FILE *stream; |
8fbdca53 FF |
341 | int format; |
342 | int recurse; | |
343 | enum val_prettyprint pretty; | |
344 | struct type **dont_print; | |
345 | { | |
346 | int i, len; | |
347 | int fields_seen = 0; | |
348 | ||
349 | check_stub_type (type); | |
350 | ||
8a177da6 | 351 | fprintf_filtered (stream, "["); |
8fbdca53 FF |
352 | len = TYPE_NFIELDS (type); |
353 | if (len == 0) | |
354 | { | |
355 | fprintf_filtered (stream, "<No data fields>"); | |
356 | } | |
357 | else | |
358 | { | |
359 | for (i = 0; i < len; i++) | |
360 | { | |
361 | if (fields_seen) | |
362 | { | |
363 | fprintf_filtered (stream, ", "); | |
364 | } | |
365 | fields_seen = 1; | |
366 | if (pretty) | |
367 | { | |
368 | fprintf_filtered (stream, "\n"); | |
369 | print_spaces_filtered (2 + 2 * recurse, stream); | |
370 | } | |
371 | else | |
372 | { | |
373 | wrap_here (n_spaces (2 + 2 * recurse)); | |
374 | } | |
8a177da6 | 375 | fputs_filtered (".", stream); |
5e81259d FF |
376 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), |
377 | language_chill, DMGL_NO_OPTS); | |
8a177da6 | 378 | fputs_filtered (": ", stream); |
8fbdca53 FF |
379 | if (TYPE_FIELD_PACKED (type, i)) |
380 | { | |
82a2edfb | 381 | value_ptr v; |
8fbdca53 FF |
382 | |
383 | /* Bitfields require special handling, especially due to byte | |
384 | order problems. */ | |
385 | v = value_from_longest (TYPE_FIELD_TYPE (type, i), | |
386 | unpack_field_as_long (type, valaddr, i)); | |
387 | ||
388 | chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, | |
389 | stream, format, 0, recurse + 1, pretty); | |
390 | } | |
391 | else | |
392 | { | |
393 | chill_val_print (TYPE_FIELD_TYPE (type, i), | |
394 | valaddr + TYPE_FIELD_BITPOS (type, i) / 8, | |
395 | 0, stream, format, 0, recurse + 1, pretty); | |
396 | } | |
397 | } | |
398 | if (pretty) | |
399 | { | |
400 | fprintf_filtered (stream, "\n"); | |
401 | print_spaces_filtered (2 * recurse, stream); | |
402 | } | |
403 | } | |
8a177da6 | 404 | fprintf_filtered (stream, "]"); |
8fbdca53 | 405 | } |