]>
Commit | Line | Data |
---|---|---|
1 | /* C language support routines for GDB, the GNU debugger. | |
2 | ||
3 | Copyright (C) 1992-2025 Free Software Foundation, Inc. | |
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 3 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, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "extract-store-integer.h" | |
21 | #include "symtab.h" | |
22 | #include "gdbtypes.h" | |
23 | #include "expression.h" | |
24 | #include "parser-defs.h" | |
25 | #include "language.h" | |
26 | #include "varobj.h" | |
27 | #include "c-lang.h" | |
28 | #include "c-support.h" | |
29 | #include "valprint.h" | |
30 | #include "macroscope.h" | |
31 | #include "charset.h" | |
32 | #include "demangle.h" | |
33 | #include "cp-abi.h" | |
34 | #include "cp-support.h" | |
35 | #include "gdbsupport/gdb_obstack.h" | |
36 | #include <ctype.h> | |
37 | #include "gdbcore.h" | |
38 | #include "gdbarch.h" | |
39 | #include "c-exp.h" | |
40 | ||
41 | /* Given a C string type, STR_TYPE, return the corresponding target | |
42 | character set name. */ | |
43 | ||
44 | static const char * | |
45 | charset_for_string_type (c_string_type str_type, struct gdbarch *gdbarch) | |
46 | { | |
47 | switch (str_type & ~C_CHAR) | |
48 | { | |
49 | case C_STRING: | |
50 | return target_charset (gdbarch); | |
51 | case C_WIDE_STRING: | |
52 | return target_wide_charset (gdbarch); | |
53 | case C_STRING_16: | |
54 | /* FIXME: UTF-16 is not always correct. */ | |
55 | if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) | |
56 | return "UTF-16BE"; | |
57 | else | |
58 | return "UTF-16LE"; | |
59 | case C_STRING_32: | |
60 | /* FIXME: UTF-32 is not always correct. */ | |
61 | if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) | |
62 | return "UTF-32BE"; | |
63 | else | |
64 | return "UTF-32LE"; | |
65 | } | |
66 | internal_error (_("unhandled c_string_type")); | |
67 | } | |
68 | ||
69 | /* Classify ELTTYPE according to what kind of character it is. Return | |
70 | the enum constant representing the character type. Also set | |
71 | *ENCODING to the name of the character set to use when converting | |
72 | characters of this type in target BYTE_ORDER to the host character | |
73 | set. */ | |
74 | ||
75 | static c_string_type | |
76 | classify_type (struct type *elttype, struct gdbarch *gdbarch, | |
77 | const char **encoding) | |
78 | { | |
79 | c_string_type result; | |
80 | ||
81 | /* We loop because ELTTYPE may be a typedef, and we want to | |
82 | successively peel each typedef until we reach a type we | |
83 | understand. We don't use CHECK_TYPEDEF because that will strip | |
84 | all typedefs at once -- but in C, wchar_t is itself a typedef, so | |
85 | that would do the wrong thing. */ | |
86 | while (elttype) | |
87 | { | |
88 | const char *name = elttype->name (); | |
89 | ||
90 | if (name == nullptr) | |
91 | { | |
92 | result = C_CHAR; | |
93 | goto done; | |
94 | } | |
95 | ||
96 | if (!strcmp (name, "wchar_t")) | |
97 | { | |
98 | result = C_WIDE_CHAR; | |
99 | goto done; | |
100 | } | |
101 | ||
102 | if (!strcmp (name, "char16_t")) | |
103 | { | |
104 | result = C_CHAR_16; | |
105 | goto done; | |
106 | } | |
107 | ||
108 | if (!strcmp (name, "char32_t")) | |
109 | { | |
110 | result = C_CHAR_32; | |
111 | goto done; | |
112 | } | |
113 | ||
114 | if (elttype->code () != TYPE_CODE_TYPEDEF) | |
115 | break; | |
116 | ||
117 | /* Call for side effects. */ | |
118 | check_typedef (elttype); | |
119 | ||
120 | if (elttype->target_type ()) | |
121 | elttype = elttype->target_type (); | |
122 | else | |
123 | { | |
124 | /* Perhaps check_typedef did not update the target type. In | |
125 | this case, force the lookup again and hope it works out. | |
126 | It never will for C, but it might for C++. */ | |
127 | elttype = check_typedef (elttype); | |
128 | } | |
129 | } | |
130 | ||
131 | /* Punt. */ | |
132 | result = C_CHAR; | |
133 | ||
134 | done: | |
135 | if (encoding) | |
136 | *encoding = charset_for_string_type (result, gdbarch); | |
137 | ||
138 | return result; | |
139 | } | |
140 | ||
141 | /* Print the character C on STREAM as part of the contents of a | |
142 | literal string whose delimiter is QUOTER. Note that that format | |
143 | for printing characters and strings is language specific. */ | |
144 | ||
145 | void | |
146 | language_defn::emitchar (int c, struct type *type, | |
147 | struct ui_file *stream, int quoter) const | |
148 | { | |
149 | const char *encoding; | |
150 | ||
151 | classify_type (type, type->arch (), &encoding); | |
152 | generic_emit_char (c, type, stream, quoter, encoding); | |
153 | } | |
154 | ||
155 | /* See language.h. */ | |
156 | ||
157 | void | |
158 | language_defn::printchar (int c, struct type *type, | |
159 | struct ui_file * stream) const | |
160 | { | |
161 | c_string_type str_type; | |
162 | ||
163 | str_type = classify_type (type, type->arch (), NULL); | |
164 | switch (str_type) | |
165 | { | |
166 | case C_CHAR: | |
167 | break; | |
168 | case C_WIDE_CHAR: | |
169 | gdb_putc ('L', stream); | |
170 | break; | |
171 | case C_CHAR_16: | |
172 | gdb_putc ('u', stream); | |
173 | break; | |
174 | case C_CHAR_32: | |
175 | gdb_putc ('U', stream); | |
176 | break; | |
177 | } | |
178 | ||
179 | gdb_putc ('\'', stream); | |
180 | emitchar (c, type, stream, '\''); | |
181 | gdb_putc ('\'', stream); | |
182 | } | |
183 | ||
184 | /* Print the character string STRING, printing at most LENGTH | |
185 | characters. LENGTH is -1 if the string is nul terminated. Each | |
186 | character is WIDTH bytes long. Printing stops early if the number | |
187 | hits print_max_chars; repeat counts are printed as appropriate. | |
188 | Print ellipses at the end if we had to stop before printing LENGTH | |
189 | characters, or if FORCE_ELLIPSES. */ | |
190 | ||
191 | void | |
192 | language_defn::printstr (struct ui_file *stream, struct type *type, | |
193 | const gdb_byte *string, unsigned int length, | |
194 | const char *user_encoding, int force_ellipses, | |
195 | const struct value_print_options *options) const | |
196 | { | |
197 | c_string_type str_type; | |
198 | const char *type_encoding; | |
199 | const char *encoding; | |
200 | ||
201 | str_type = (classify_type (type, type->arch (), &type_encoding) | |
202 | & ~C_CHAR); | |
203 | switch (str_type) | |
204 | { | |
205 | case C_STRING: | |
206 | break; | |
207 | case C_WIDE_STRING: | |
208 | gdb_puts ("L", stream); | |
209 | break; | |
210 | case C_STRING_16: | |
211 | gdb_puts ("u", stream); | |
212 | break; | |
213 | case C_STRING_32: | |
214 | gdb_puts ("U", stream); | |
215 | break; | |
216 | } | |
217 | ||
218 | encoding = (user_encoding && *user_encoding) ? user_encoding : type_encoding; | |
219 | ||
220 | generic_printstr (stream, type, string, length, encoding, force_ellipses, | |
221 | '"', 1, options); | |
222 | } | |
223 | ||
224 | /* Obtain a C string from the inferior storing it in a newly allocated | |
225 | buffer in BUFFER, which should be freed by the caller. If the in- | |
226 | and out-parameter *LENGTH is specified at -1, the string is read | |
227 | until a null character of the appropriate width is found, otherwise | |
228 | the string is read to the length of characters specified. The size | |
229 | of a character is determined by the length of the target type of | |
230 | the pointer or array. | |
231 | ||
232 | If VALUE is an array with a known length, and *LENGTH is -1, | |
233 | the function will not read past the end of the array. However, any | |
234 | declared size of the array is ignored if *LENGTH > 0. | |
235 | ||
236 | On completion, *LENGTH will be set to the size of the string read in | |
237 | characters. (If a length of -1 is specified, the length returned | |
238 | will not include the null character). CHARSET is always set to the | |
239 | target charset. */ | |
240 | ||
241 | void | |
242 | c_get_string (struct value *value, gdb::unique_xmalloc_ptr<gdb_byte> *buffer, | |
243 | int *length, struct type **char_type, | |
244 | const char **charset) | |
245 | { | |
246 | int err, width; | |
247 | unsigned int fetchlimit; | |
248 | struct type *type = check_typedef (value->type ()); | |
249 | struct type *element_type = type->target_type (); | |
250 | int req_length = *length; | |
251 | enum bfd_endian byte_order | |
252 | = type_byte_order (type); | |
253 | ||
254 | if (element_type == NULL) | |
255 | goto error; | |
256 | ||
257 | if (type->code () == TYPE_CODE_ARRAY) | |
258 | { | |
259 | /* If we know the size of the array, we can use it as a limit on | |
260 | the number of characters to be fetched. */ | |
261 | if (type->num_fields () == 1 | |
262 | && type->field (0).type ()->code () == TYPE_CODE_RANGE) | |
263 | { | |
264 | LONGEST low_bound, high_bound; | |
265 | ||
266 | get_discrete_bounds (type->field (0).type (), | |
267 | &low_bound, &high_bound); | |
268 | fetchlimit = high_bound - low_bound + 1; | |
269 | } | |
270 | else | |
271 | fetchlimit = UINT_MAX; | |
272 | } | |
273 | else if (type->code () == TYPE_CODE_PTR) | |
274 | fetchlimit = UINT_MAX; | |
275 | else | |
276 | /* We work only with arrays and pointers. */ | |
277 | goto error; | |
278 | ||
279 | if (! c_textual_element_type (element_type, 0)) | |
280 | goto error; | |
281 | classify_type (element_type, element_type->arch (), charset); | |
282 | width = element_type->length (); | |
283 | ||
284 | /* If the string lives in GDB's memory instead of the inferior's, | |
285 | then we just need to copy it to BUFFER. Also, since such strings | |
286 | are arrays with known size, FETCHLIMIT will hold the size of the | |
287 | array. | |
288 | ||
289 | An array is assumed to live in GDB's memory, so we take this path | |
290 | here. | |
291 | ||
292 | However, it's possible for the caller to request more array | |
293 | elements than apparently exist -- this can happen when using the | |
294 | C struct hack. So, only do this if either no length was | |
295 | specified, or the length is within the existing bounds. This | |
296 | avoids running off the end of the value's contents. */ | |
297 | if ((value->lval () == not_lval | |
298 | || value->lval () == lval_internalvar | |
299 | || type->code () == TYPE_CODE_ARRAY) | |
300 | && fetchlimit != UINT_MAX | |
301 | && (*length < 0 || *length <= fetchlimit)) | |
302 | { | |
303 | int i; | |
304 | const gdb_byte *contents = value->contents ().data (); | |
305 | ||
306 | /* If a length is specified, use that. */ | |
307 | if (*length >= 0) | |
308 | i = *length; | |
309 | else | |
310 | /* Otherwise, look for a null character. */ | |
311 | for (i = 0; i < fetchlimit; i++) | |
312 | if (extract_unsigned_integer (contents + i * width, | |
313 | width, byte_order) == 0) | |
314 | break; | |
315 | ||
316 | /* I is now either a user-defined length, the number of non-null | |
317 | characters, or FETCHLIMIT. */ | |
318 | *length = i * width; | |
319 | buffer->reset ((gdb_byte *) xmalloc (*length)); | |
320 | memcpy (buffer->get (), contents, *length); | |
321 | err = 0; | |
322 | } | |
323 | else | |
324 | { | |
325 | /* value_as_address does not return an address for an array when | |
326 | c_style_arrays is false, so we handle that specially | |
327 | here. */ | |
328 | CORE_ADDR addr; | |
329 | if (type->code () == TYPE_CODE_ARRAY) | |
330 | { | |
331 | if (value->lval () != lval_memory) | |
332 | error (_("Attempt to take address of value " | |
333 | "not located in memory.")); | |
334 | addr = value->address (); | |
335 | } | |
336 | else | |
337 | addr = value_as_address (value); | |
338 | ||
339 | /* Prior to the fix for PR 16196 read_string would ignore fetchlimit | |
340 | if length > 0. The old "broken" behavior is the behavior we want: | |
341 | The caller may want to fetch 100 bytes from a variable length array | |
342 | implemented using the common idiom of having an array of length 1 at | |
343 | the end of a struct. In this case we want to ignore the declared | |
344 | size of the array. However, it's counterintuitive to implement that | |
345 | behavior in read_string: what does fetchlimit otherwise mean if | |
346 | length > 0. Therefore we implement the behavior we want here: | |
347 | If *length > 0, don't specify a fetchlimit. This preserves the | |
348 | previous behavior. We could move this check above where we know | |
349 | whether the array is declared with a fixed size, but we only want | |
350 | to apply this behavior when calling read_string. PR 16286. */ | |
351 | if (*length > 0) | |
352 | fetchlimit = UINT_MAX; | |
353 | ||
354 | err = target_read_string (addr, *length, width, fetchlimit, | |
355 | buffer, length); | |
356 | if (err != 0) | |
357 | memory_error (TARGET_XFER_E_IO, addr); | |
358 | } | |
359 | ||
360 | /* If the LENGTH is specified at -1, we want to return the string | |
361 | length up to the terminating null character. If an actual length | |
362 | was specified, we want to return the length of exactly what was | |
363 | read. */ | |
364 | if (req_length == -1) | |
365 | /* If the last character is null, subtract it from LENGTH. */ | |
366 | if (*length > 0 | |
367 | && extract_unsigned_integer (buffer->get () + *length - width, | |
368 | width, byte_order) == 0) | |
369 | *length -= width; | |
370 | ||
371 | /* The read_string function will return the number of bytes read. | |
372 | If length returned from read_string was > 0, return the number of | |
373 | characters read by dividing the number of bytes by width. */ | |
374 | if (*length != 0) | |
375 | *length = *length / width; | |
376 | ||
377 | *char_type = element_type; | |
378 | ||
379 | return; | |
380 | ||
381 | error: | |
382 | { | |
383 | std::string type_str = type_to_string (type); | |
384 | if (!type_str.empty ()) | |
385 | { | |
386 | error (_("Trying to read string with inappropriate type `%s'."), | |
387 | type_str.c_str ()); | |
388 | } | |
389 | else | |
390 | error (_("Trying to read string with inappropriate type.")); | |
391 | } | |
392 | } | |
393 | ||
394 | \f | |
395 | /* Evaluating C and C++ expressions. */ | |
396 | ||
397 | /* Convert a UCN. The digits of the UCN start at P and extend no | |
398 | farther than LIMIT. DEST_CHARSET is the name of the character set | |
399 | into which the UCN should be converted. The results are written to | |
400 | OUTPUT. LENGTH is the maximum length of the UCN, either 4 or 8. | |
401 | Returns a pointer to just after the final digit of the UCN. */ | |
402 | ||
403 | static const char * | |
404 | convert_ucn (const char *p, const char *limit, const char *dest_charset, | |
405 | struct obstack *output, int length) | |
406 | { | |
407 | unsigned long result = 0; | |
408 | gdb_byte data[4]; | |
409 | int i; | |
410 | ||
411 | for (i = 0; i < length && p < limit && ISXDIGIT (*p); ++i, ++p) | |
412 | result = (result << 4) + fromhex (*p); | |
413 | ||
414 | for (i = 3; i >= 0; --i) | |
415 | { | |
416 | data[i] = result & 0xff; | |
417 | result >>= 8; | |
418 | } | |
419 | ||
420 | convert_between_encodings ("UTF-32BE", dest_charset, data, | |
421 | 4, 4, output, translit_none); | |
422 | ||
423 | return p; | |
424 | } | |
425 | ||
426 | /* Emit a character, VALUE, which was specified numerically, to | |
427 | OUTPUT. TYPE is the target character type. */ | |
428 | ||
429 | static void | |
430 | emit_numeric_character (struct type *type, unsigned long value, | |
431 | struct obstack *output) | |
432 | { | |
433 | gdb_byte *buffer; | |
434 | ||
435 | buffer = (gdb_byte *) alloca (type->length ()); | |
436 | pack_long (buffer, type, value); | |
437 | obstack_grow (output, buffer, type->length ()); | |
438 | } | |
439 | ||
440 | /* Convert an octal escape sequence. TYPE is the target character | |
441 | type. The digits of the escape sequence begin at P and extend no | |
442 | farther than LIMIT. The result is written to OUTPUT. Returns a | |
443 | pointer to just after the final digit of the escape sequence. */ | |
444 | ||
445 | static const char * | |
446 | convert_octal (struct type *type, const char *p, | |
447 | const char *limit, struct obstack *output) | |
448 | { | |
449 | int i; | |
450 | unsigned long value = 0; | |
451 | ||
452 | for (i = 0; | |
453 | i < 3 && p < limit && ISDIGIT (*p) && *p != '8' && *p != '9'; | |
454 | ++i) | |
455 | { | |
456 | value = 8 * value + fromhex (*p); | |
457 | ++p; | |
458 | } | |
459 | ||
460 | emit_numeric_character (type, value, output); | |
461 | ||
462 | return p; | |
463 | } | |
464 | ||
465 | /* Convert a hex escape sequence. TYPE is the target character type. | |
466 | The digits of the escape sequence begin at P and extend no farther | |
467 | than LIMIT. The result is written to OUTPUT. Returns a pointer to | |
468 | just after the final digit of the escape sequence. */ | |
469 | ||
470 | static const char * | |
471 | convert_hex (struct type *type, const char *p, | |
472 | const char *limit, struct obstack *output) | |
473 | { | |
474 | unsigned long value = 0; | |
475 | ||
476 | while (p < limit && ISXDIGIT (*p)) | |
477 | { | |
478 | value = 16 * value + fromhex (*p); | |
479 | ++p; | |
480 | } | |
481 | ||
482 | emit_numeric_character (type, value, output); | |
483 | ||
484 | return p; | |
485 | } | |
486 | ||
487 | #define ADVANCE \ | |
488 | do { \ | |
489 | ++p; \ | |
490 | if (p == limit) \ | |
491 | error (_("Malformed escape sequence")); \ | |
492 | } while (0) | |
493 | ||
494 | /* Convert an escape sequence to a target format. TYPE is the target | |
495 | character type to use, and DEST_CHARSET is the name of the target | |
496 | character set. The backslash of the escape sequence is at *P, and | |
497 | the escape sequence will not extend past LIMIT. The results are | |
498 | written to OUTPUT. Returns a pointer to just past the final | |
499 | character of the escape sequence. */ | |
500 | ||
501 | static const char * | |
502 | convert_escape (struct type *type, const char *dest_charset, | |
503 | const char *p, const char *limit, struct obstack *output) | |
504 | { | |
505 | /* Skip the backslash. */ | |
506 | ADVANCE; | |
507 | ||
508 | switch (*p) | |
509 | { | |
510 | case '\\': | |
511 | obstack_1grow (output, '\\'); | |
512 | ++p; | |
513 | break; | |
514 | ||
515 | case 'x': | |
516 | ADVANCE; | |
517 | if (!ISXDIGIT (*p)) | |
518 | error (_("\\x used with no following hex digits.")); | |
519 | p = convert_hex (type, p, limit, output); | |
520 | break; | |
521 | ||
522 | case '0': | |
523 | case '1': | |
524 | case '2': | |
525 | case '3': | |
526 | case '4': | |
527 | case '5': | |
528 | case '6': | |
529 | case '7': | |
530 | p = convert_octal (type, p, limit, output); | |
531 | break; | |
532 | ||
533 | case 'u': | |
534 | case 'U': | |
535 | { | |
536 | int length = *p == 'u' ? 4 : 8; | |
537 | ||
538 | ADVANCE; | |
539 | if (!ISXDIGIT (*p)) | |
540 | error (_("\\u used with no following hex digits")); | |
541 | p = convert_ucn (p, limit, dest_charset, output, length); | |
542 | } | |
543 | } | |
544 | ||
545 | return p; | |
546 | } | |
547 | ||
548 | /* Given a single string from a (C-specific) OP_STRING list, convert | |
549 | it to a target string, handling escape sequences specially. The | |
550 | output is written to OUTPUT. DATA is the input string, which has | |
551 | length LEN. DEST_CHARSET is the name of the target character set, | |
552 | and TYPE is the type of target character to use. */ | |
553 | ||
554 | static void | |
555 | parse_one_string (struct obstack *output, const char *data, int len, | |
556 | const char *dest_charset, struct type *type) | |
557 | { | |
558 | const char *limit; | |
559 | ||
560 | limit = data + len; | |
561 | ||
562 | while (data < limit) | |
563 | { | |
564 | const char *p = data; | |
565 | ||
566 | /* Look for next escape, or the end of the input. */ | |
567 | while (p < limit && *p != '\\') | |
568 | ++p; | |
569 | /* If we saw a run of characters, convert them all. */ | |
570 | if (p > data) | |
571 | convert_between_encodings (host_charset (), dest_charset, | |
572 | (const gdb_byte *) data, p - data, 1, | |
573 | output, translit_none); | |
574 | /* If we saw an escape, convert it. */ | |
575 | if (p < limit) | |
576 | p = convert_escape (type, dest_charset, p, limit, output); | |
577 | data = p; | |
578 | } | |
579 | } | |
580 | ||
581 | namespace expr | |
582 | { | |
583 | ||
584 | value * | |
585 | c_string_operation::evaluate (struct type *expect_type, | |
586 | struct expression *exp, | |
587 | enum noside noside) | |
588 | { | |
589 | struct type *type; | |
590 | struct value *result; | |
591 | c_string_type dest_type; | |
592 | const char *dest_charset; | |
593 | int satisfy_expected = 0; | |
594 | ||
595 | auto_obstack output; | |
596 | ||
597 | dest_type = std::get<0> (m_storage); | |
598 | ||
599 | switch (dest_type & ~C_CHAR) | |
600 | { | |
601 | case C_STRING: | |
602 | type = language_string_char_type (exp->language_defn, | |
603 | exp->gdbarch); | |
604 | break; | |
605 | case C_WIDE_STRING: | |
606 | type = lookup_typename (exp->language_defn, "wchar_t", NULL, 0); | |
607 | break; | |
608 | case C_STRING_16: | |
609 | type = lookup_typename (exp->language_defn, "char16_t", NULL, 0); | |
610 | break; | |
611 | case C_STRING_32: | |
612 | type = lookup_typename (exp->language_defn, "char32_t", NULL, 0); | |
613 | break; | |
614 | default: | |
615 | internal_error (_("unhandled c_string_type")); | |
616 | } | |
617 | ||
618 | /* If the caller expects an array of some integral type, | |
619 | satisfy them. If something odder is expected, rely on the | |
620 | caller to cast. */ | |
621 | if (expect_type && expect_type->code () == TYPE_CODE_ARRAY) | |
622 | { | |
623 | struct type *element_type | |
624 | = check_typedef (expect_type->target_type ()); | |
625 | ||
626 | if (element_type->code () == TYPE_CODE_INT | |
627 | || element_type->code () == TYPE_CODE_CHAR) | |
628 | { | |
629 | type = element_type; | |
630 | satisfy_expected = 1; | |
631 | } | |
632 | } | |
633 | ||
634 | dest_charset = charset_for_string_type (dest_type, exp->gdbarch); | |
635 | ||
636 | for (const std::string &item : std::get<1> (m_storage)) | |
637 | parse_one_string (&output, item.c_str (), item.size (), | |
638 | dest_charset, type); | |
639 | ||
640 | if ((dest_type & C_CHAR) != 0) | |
641 | { | |
642 | LONGEST value; | |
643 | ||
644 | if (obstack_object_size (&output) != type->length ()) | |
645 | error (_("Could not convert character " | |
646 | "constant to target character set")); | |
647 | value = unpack_long (type, (gdb_byte *) obstack_base (&output)); | |
648 | result = value_from_longest (type, value); | |
649 | } | |
650 | else | |
651 | { | |
652 | int element_size = type->length (); | |
653 | ||
654 | if (satisfy_expected) | |
655 | { | |
656 | LONGEST low_bound, high_bound; | |
657 | ||
658 | if (!get_discrete_bounds (expect_type->index_type (), | |
659 | &low_bound, &high_bound)) | |
660 | { | |
661 | low_bound = 0; | |
662 | high_bound = (expect_type->length () / element_size) - 1; | |
663 | } | |
664 | if (obstack_object_size (&output) / element_size | |
665 | > (high_bound - low_bound + 1)) | |
666 | error (_("Too many array elements")); | |
667 | ||
668 | result = value::allocate (expect_type); | |
669 | memcpy (result->contents_raw ().data (), obstack_base (&output), | |
670 | obstack_object_size (&output)); | |
671 | /* Write the terminating character. */ | |
672 | memset (result->contents_raw ().data () + obstack_object_size (&output), | |
673 | 0, element_size); | |
674 | } | |
675 | else | |
676 | result = value_cstring ((const gdb_byte *) obstack_base (&output), | |
677 | obstack_object_size (&output) / element_size, | |
678 | type); | |
679 | } | |
680 | return result; | |
681 | } | |
682 | ||
683 | } /* namespace expr */ | |
684 | ||
685 | \f | |
686 | /* See c-lang.h. */ | |
687 | ||
688 | bool | |
689 | c_is_string_type_p (struct type *type) | |
690 | { | |
691 | type = check_typedef (type); | |
692 | while (type->code () == TYPE_CODE_REF) | |
693 | { | |
694 | type = type->target_type (); | |
695 | type = check_typedef (type); | |
696 | } | |
697 | ||
698 | switch (type->code ()) | |
699 | { | |
700 | case TYPE_CODE_ARRAY: | |
701 | { | |
702 | /* See if target type looks like a string. */ | |
703 | struct type *array_target_type = type->target_type (); | |
704 | return (type->length () > 0 | |
705 | && array_target_type->length () > 0 | |
706 | && c_textual_element_type (array_target_type, 0)); | |
707 | } | |
708 | case TYPE_CODE_STRING: | |
709 | return true; | |
710 | case TYPE_CODE_PTR: | |
711 | { | |
712 | struct type *element_type = type->target_type (); | |
713 | return c_textual_element_type (element_type, 0); | |
714 | } | |
715 | default: | |
716 | break; | |
717 | } | |
718 | ||
719 | return false; | |
720 | } | |
721 | ||
722 | \f | |
723 | ||
724 | /* See c-lang.h. */ | |
725 | ||
726 | gdb::unique_xmalloc_ptr<char> | |
727 | c_canonicalize_name (const char *name) | |
728 | { | |
729 | if (strchr (name, ' ') != nullptr | |
730 | || streq (name, "signed") | |
731 | || streq (name, "unsigned")) | |
732 | return cp_canonicalize_string (name); | |
733 | return nullptr; | |
734 | } | |
735 | ||
736 | \f | |
737 | ||
738 | void | |
739 | c_language_arch_info (struct gdbarch *gdbarch, | |
740 | struct language_arch_info *lai) | |
741 | { | |
742 | const struct builtin_type *builtin = builtin_type (gdbarch); | |
743 | ||
744 | /* Helper function to allow shorter lines below. */ | |
745 | auto add = [&] (struct type * t) | |
746 | { | |
747 | lai->add_primitive_type (t); | |
748 | }; | |
749 | ||
750 | add (builtin->builtin_int); | |
751 | add (builtin->builtin_long); | |
752 | add (builtin->builtin_short); | |
753 | add (builtin->builtin_char); | |
754 | add (builtin->builtin_float); | |
755 | add (builtin->builtin_double); | |
756 | add (builtin->builtin_void); | |
757 | add (builtin->builtin_long_long); | |
758 | add (builtin->builtin_signed_char); | |
759 | add (builtin->builtin_unsigned_char); | |
760 | add (builtin->builtin_unsigned_short); | |
761 | add (builtin->builtin_unsigned_int); | |
762 | add (builtin->builtin_unsigned_long); | |
763 | add (builtin->builtin_unsigned_long_long); | |
764 | add (builtin->builtin_long_double); | |
765 | add (builtin->builtin_complex); | |
766 | add (builtin->builtin_double_complex); | |
767 | add (builtin->builtin_decfloat); | |
768 | add (builtin->builtin_decdouble); | |
769 | add (builtin->builtin_declong); | |
770 | ||
771 | lai->set_string_char_type (builtin->builtin_char); | |
772 | lai->set_bool_type (builtin->builtin_int); | |
773 | } | |
774 | ||
775 | /* Class representing the C language. */ | |
776 | ||
777 | class c_language : public language_defn | |
778 | { | |
779 | public: | |
780 | c_language () | |
781 | : language_defn (language_c) | |
782 | { /* Nothing. */ } | |
783 | ||
784 | /* See language.h. */ | |
785 | ||
786 | const char *name () const override | |
787 | { return "c"; } | |
788 | ||
789 | /* See language.h. */ | |
790 | ||
791 | const char *natural_name () const override | |
792 | { return "C"; } | |
793 | ||
794 | /* See language.h. */ | |
795 | ||
796 | const std::vector<const char *> &filename_extensions () const override | |
797 | { | |
798 | static const std::vector<const char *> extensions = { ".c" }; | |
799 | return extensions; | |
800 | } | |
801 | ||
802 | /* See language.h. */ | |
803 | void language_arch_info (struct gdbarch *gdbarch, | |
804 | struct language_arch_info *lai) const override | |
805 | { | |
806 | c_language_arch_info (gdbarch, lai); | |
807 | } | |
808 | ||
809 | /* See language.h. */ | |
810 | ||
811 | bool can_print_type_offsets () const override | |
812 | { | |
813 | return true; | |
814 | } | |
815 | ||
816 | /* See language.h. */ | |
817 | ||
818 | void print_type (struct type *type, const char *varstring, | |
819 | struct ui_file *stream, int show, int level, | |
820 | const struct type_print_options *flags) const override | |
821 | { | |
822 | c_print_type (type, varstring, stream, show, level, la_language, flags); | |
823 | } | |
824 | ||
825 | /* See language.h. */ | |
826 | ||
827 | bool store_sym_names_in_linkage_form_p () const override | |
828 | { return true; } | |
829 | ||
830 | /* See language.h. */ | |
831 | ||
832 | enum macro_expansion macro_expansion () const override | |
833 | { return macro_expansion_c; } | |
834 | }; | |
835 | ||
836 | /* Single instance of the C language class. */ | |
837 | ||
838 | static c_language c_language_defn; | |
839 | ||
840 | /* A class for the C++ language. */ | |
841 | ||
842 | class cplus_language : public language_defn | |
843 | { | |
844 | public: | |
845 | cplus_language () | |
846 | : language_defn (language_cplus) | |
847 | { /* Nothing. */ } | |
848 | ||
849 | /* See language.h. */ | |
850 | ||
851 | const char *name () const override | |
852 | { return "c++"; } | |
853 | ||
854 | /* See language.h. */ | |
855 | ||
856 | const char *natural_name () const override | |
857 | { return "C++"; } | |
858 | ||
859 | /* See language.h */ | |
860 | const char *get_digit_separator () const override | |
861 | { return "\'"; } | |
862 | ||
863 | /* See language.h. */ | |
864 | ||
865 | const std::vector<const char *> &filename_extensions () const override | |
866 | { | |
867 | static const std::vector<const char *> extensions | |
868 | = { ".C", ".cc", ".cp", ".cpp", ".cxx", ".c++" }; | |
869 | return extensions; | |
870 | } | |
871 | ||
872 | /* See language.h. */ | |
873 | ||
874 | struct language_pass_by_ref_info pass_by_reference_info | |
875 | (struct type *type) const override | |
876 | { | |
877 | return cp_pass_by_reference (type); | |
878 | } | |
879 | ||
880 | /* See language.h. */ | |
881 | void language_arch_info (struct gdbarch *gdbarch, | |
882 | struct language_arch_info *lai) const override | |
883 | { | |
884 | const struct builtin_type *builtin = builtin_type (gdbarch); | |
885 | ||
886 | /* Helper function to allow shorter lines below. */ | |
887 | auto add = [&] (struct type * t) | |
888 | { | |
889 | lai->add_primitive_type (t); | |
890 | }; | |
891 | ||
892 | add (builtin->builtin_int); | |
893 | add (builtin->builtin_long); | |
894 | add (builtin->builtin_short); | |
895 | add (builtin->builtin_char); | |
896 | add (builtin->builtin_float); | |
897 | add (builtin->builtin_double); | |
898 | add (builtin->builtin_void); | |
899 | add (builtin->builtin_long_long); | |
900 | add (builtin->builtin_signed_char); | |
901 | add (builtin->builtin_unsigned_char); | |
902 | add (builtin->builtin_unsigned_short); | |
903 | add (builtin->builtin_unsigned_int); | |
904 | add (builtin->builtin_unsigned_long); | |
905 | add (builtin->builtin_unsigned_long_long); | |
906 | add (builtin->builtin_long_double); | |
907 | add (builtin->builtin_complex); | |
908 | add (builtin->builtin_double_complex); | |
909 | add (builtin->builtin_bool); | |
910 | add (builtin->builtin_decfloat); | |
911 | add (builtin->builtin_decdouble); | |
912 | add (builtin->builtin_declong); | |
913 | add (builtin->builtin_char16); | |
914 | add (builtin->builtin_char32); | |
915 | add (builtin->builtin_wchar); | |
916 | ||
917 | lai->set_string_char_type (builtin->builtin_char); | |
918 | lai->set_bool_type (builtin->builtin_bool, "bool"); | |
919 | } | |
920 | ||
921 | /* See language.h. */ | |
922 | struct type *lookup_transparent_type (const char *name, | |
923 | domain_search_flags flags) | |
924 | const override | |
925 | { | |
926 | return cp_lookup_transparent_type (name, flags); | |
927 | } | |
928 | ||
929 | /* See language.h. */ | |
930 | unsigned int search_name_hash (const char *name) const override | |
931 | { | |
932 | return cp_search_name_hash (name); | |
933 | } | |
934 | ||
935 | /* See language.h. */ | |
936 | bool sniff_from_mangled_name | |
937 | (const char *mangled, | |
938 | gdb::unique_xmalloc_ptr<char> *demangled) const override | |
939 | { | |
940 | *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI); | |
941 | return *demangled != NULL; | |
942 | } | |
943 | ||
944 | /* See language.h. */ | |
945 | ||
946 | gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled, | |
947 | int options) const override | |
948 | { | |
949 | return gdb_demangle (mangled, options); | |
950 | } | |
951 | ||
952 | /* See language.h. */ | |
953 | ||
954 | bool can_print_type_offsets () const override | |
955 | { | |
956 | return true; | |
957 | } | |
958 | ||
959 | /* See language.h. */ | |
960 | ||
961 | void print_type (struct type *type, const char *varstring, | |
962 | struct ui_file *stream, int show, int level, | |
963 | const struct type_print_options *flags) const override | |
964 | { | |
965 | c_print_type (type, varstring, stream, show, level, la_language, flags); | |
966 | } | |
967 | ||
968 | /* See language.h. */ | |
969 | ||
970 | CORE_ADDR skip_trampoline (const frame_info_ptr &fi, | |
971 | CORE_ADDR pc) const override | |
972 | { | |
973 | return cplus_skip_trampoline (fi, pc); | |
974 | } | |
975 | ||
976 | /* See language.h. */ | |
977 | ||
978 | char *class_name_from_physname (const char *physname) const override | |
979 | { | |
980 | return cp_class_name_from_physname (physname); | |
981 | } | |
982 | ||
983 | /* See language.h. */ | |
984 | ||
985 | struct block_symbol lookup_symbol_local | |
986 | (const char *scope, | |
987 | const char *name, | |
988 | const struct block *block, | |
989 | const domain_search_flags domain) const override | |
990 | { | |
991 | return cp_lookup_symbol_imports (scope, name, block, domain); | |
992 | } | |
993 | ||
994 | /* See language.h. */ | |
995 | ||
996 | struct block_symbol lookup_symbol_nonlocal | |
997 | (const char *name, const struct block *block, | |
998 | const domain_search_flags domain) const override | |
999 | { | |
1000 | return cp_lookup_symbol_nonlocal (this, name, block, domain); | |
1001 | } | |
1002 | ||
1003 | /* See language.h. */ | |
1004 | ||
1005 | const char *name_of_this () const override | |
1006 | { return "this"; } | |
1007 | ||
1008 | /* See language.h. */ | |
1009 | ||
1010 | enum macro_expansion macro_expansion () const override | |
1011 | { return macro_expansion_c; } | |
1012 | ||
1013 | /* See language.h. */ | |
1014 | ||
1015 | const struct lang_varobj_ops *varobj_ops () const override | |
1016 | { return &cplus_varobj_ops; } | |
1017 | ||
1018 | protected: | |
1019 | ||
1020 | /* See language.h. */ | |
1021 | ||
1022 | symbol_name_matcher_ftype *get_symbol_name_matcher_inner | |
1023 | (const lookup_name_info &lookup_name) const override | |
1024 | { | |
1025 | return cp_get_symbol_name_matcher (lookup_name); | |
1026 | } | |
1027 | }; | |
1028 | ||
1029 | /* The single instance of the C++ language class. */ | |
1030 | ||
1031 | static cplus_language cplus_language_defn; | |
1032 | ||
1033 | /* A class for the ASM language. */ | |
1034 | ||
1035 | class asm_language : public language_defn | |
1036 | { | |
1037 | public: | |
1038 | asm_language () | |
1039 | : language_defn (language_asm) | |
1040 | { /* Nothing. */ } | |
1041 | ||
1042 | /* See language.h. */ | |
1043 | ||
1044 | const char *name () const override | |
1045 | { return "asm"; } | |
1046 | ||
1047 | /* See language.h. */ | |
1048 | ||
1049 | const char *natural_name () const override | |
1050 | { return "Assembly"; } | |
1051 | ||
1052 | /* See language.h. */ | |
1053 | ||
1054 | const std::vector<const char *> &filename_extensions () const override | |
1055 | { | |
1056 | static const std::vector<const char *> extensions | |
1057 | = { ".s", ".sx", ".S" }; | |
1058 | return extensions; | |
1059 | } | |
1060 | ||
1061 | /* See language.h. | |
1062 | ||
1063 | FIXME: Should this have its own arch info method? */ | |
1064 | void language_arch_info (struct gdbarch *gdbarch, | |
1065 | struct language_arch_info *lai) const override | |
1066 | { | |
1067 | c_language_arch_info (gdbarch, lai); | |
1068 | } | |
1069 | ||
1070 | /* See language.h. */ | |
1071 | ||
1072 | bool can_print_type_offsets () const override | |
1073 | { | |
1074 | return true; | |
1075 | } | |
1076 | ||
1077 | /* See language.h. */ | |
1078 | ||
1079 | void print_type (struct type *type, const char *varstring, | |
1080 | struct ui_file *stream, int show, int level, | |
1081 | const struct type_print_options *flags) const override | |
1082 | { | |
1083 | c_print_type (type, varstring, stream, show, level, la_language, flags); | |
1084 | } | |
1085 | ||
1086 | /* See language.h. */ | |
1087 | ||
1088 | bool store_sym_names_in_linkage_form_p () const override | |
1089 | { return true; } | |
1090 | ||
1091 | /* See language.h. */ | |
1092 | ||
1093 | enum macro_expansion macro_expansion () const override | |
1094 | { return macro_expansion_c; } | |
1095 | }; | |
1096 | ||
1097 | /* The single instance of the ASM language class. */ | |
1098 | static asm_language asm_language_defn; | |
1099 | ||
1100 | /* A class for the minimal language. This does not represent a real | |
1101 | language. It just provides a minimal support a-la-C that should allow | |
1102 | users to do some simple operations when debugging applications that use | |
1103 | a language currently not supported by GDB. */ | |
1104 | ||
1105 | class minimal_language : public language_defn | |
1106 | { | |
1107 | public: | |
1108 | minimal_language () | |
1109 | : language_defn (language_minimal) | |
1110 | { /* Nothing. */ } | |
1111 | ||
1112 | /* See language.h. */ | |
1113 | ||
1114 | const char *name () const override | |
1115 | { return "minimal"; } | |
1116 | ||
1117 | /* See language.h. */ | |
1118 | ||
1119 | const char *natural_name () const override | |
1120 | { return "Minimal"; } | |
1121 | ||
1122 | /* See language.h. */ | |
1123 | void language_arch_info (struct gdbarch *gdbarch, | |
1124 | struct language_arch_info *lai) const override | |
1125 | { | |
1126 | c_language_arch_info (gdbarch, lai); | |
1127 | } | |
1128 | ||
1129 | /* See language.h. */ | |
1130 | ||
1131 | bool can_print_type_offsets () const override | |
1132 | { | |
1133 | return true; | |
1134 | } | |
1135 | ||
1136 | /* See language.h. */ | |
1137 | ||
1138 | void print_type (struct type *type, const char *varstring, | |
1139 | struct ui_file *stream, int show, int level, | |
1140 | const struct type_print_options *flags) const override | |
1141 | { | |
1142 | c_print_type (type, varstring, stream, show, level, la_language, flags); | |
1143 | } | |
1144 | ||
1145 | /* See language.h. */ | |
1146 | ||
1147 | bool store_sym_names_in_linkage_form_p () const override | |
1148 | { return true; } | |
1149 | ||
1150 | /* See language.h. */ | |
1151 | ||
1152 | enum macro_expansion macro_expansion () const override | |
1153 | { return macro_expansion_c; } | |
1154 | }; | |
1155 | ||
1156 | /* The single instance of the minimal language class. */ | |
1157 | static minimal_language minimal_language_defn; |