Type-safe wrapper for enum flags
[deliverable/binutils-gdb.git] / gdb / c-lang.c
CommitLineData
c906108c 1/* C language support routines for GDB, the GNU debugger.
ce27fb25 2
32d0add0 3 Copyright (C) 1992-2015 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "symtab.h"
22#include "gdbtypes.h"
23#include "expression.h"
24#include "parser-defs.h"
25#include "language.h"
a53b64ea 26#include "varobj.h"
c906108c 27#include "c-lang.h"
745b8ca0 28#include "valprint.h"
84f0252a 29#include "macroscope.h"
234b45d4 30#include "charset.h"
9a3d7dfd 31#include "demangle.h"
b18be20d 32#include "cp-abi.h"
1fcb5155 33#include "cp-support.h"
6c7a06a3
TT
34#include "gdb_obstack.h"
35#include <ctype.h>
578d3588 36#include "gdbcore.h"
c906108c 37
a14ed312 38extern void _initialize_c_language (void);
6c7a06a3
TT
39
40/* Given a C string type, STR_TYPE, return the corresponding target
41 character set name. */
42
43static const char *
e17a4113 44charset_for_string_type (enum c_string_type str_type,
f870a310 45 struct gdbarch *gdbarch)
6c7a06a3
TT
46{
47 switch (str_type & ~C_CHAR)
48 {
49 case C_STRING:
f870a310 50 return target_charset (gdbarch);
6c7a06a3 51 case C_WIDE_STRING:
f870a310 52 return target_wide_charset (gdbarch);
6c7a06a3 53 case C_STRING_16:
b8899f2b 54 /* FIXME: UTF-16 is not always correct. */
f870a310 55 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
b8899f2b 56 return "UTF-16BE";
6c7a06a3 57 else
b8899f2b 58 return "UTF-16LE";
6c7a06a3 59 case C_STRING_32:
b8899f2b 60 /* FIXME: UTF-32 is not always correct. */
f870a310 61 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
b8899f2b 62 return "UTF-32BE";
6c7a06a3 63 else
b8899f2b 64 return "UTF-32LE";
6c7a06a3 65 }
9b20d036 66 internal_error (__FILE__, __LINE__, _("unhandled c_string_type"));
6c7a06a3
TT
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
aff410f1
MS
72 characters of this type in target BYTE_ORDER to the host character
73 set. */
6c7a06a3
TT
74
75static enum c_string_type
f870a310 76classify_type (struct type *elttype, struct gdbarch *gdbarch,
e17a4113 77 const char **encoding)
6c7a06a3 78{
6c7a06a3
TT
79 enum c_string_type result;
80
85e306ed
TT
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)
6c7a06a3 87 {
0d5cff50 88 const char *name = TYPE_NAME (elttype);
6c7a06a3
TT
89
90 if (TYPE_CODE (elttype) == TYPE_CODE_CHAR || !name)
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
85e306ed
TT
114 if (TYPE_CODE (elttype) != TYPE_CODE_TYPEDEF)
115 break;
116
117 /* Call for side effects. */
118 check_typedef (elttype);
119
120 if (TYPE_TARGET_TYPE (elttype))
121 elttype = TYPE_TARGET_TYPE (elttype);
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++. */
f168693b 127 elttype = check_typedef (elttype);
85e306ed 128 }
6c7a06a3 129 }
6c7a06a3
TT
130
131 /* Punt. */
132 result = C_CHAR;
133
134 done:
e17a4113 135 if (encoding)
f870a310 136 *encoding = charset_for_string_type (result, gdbarch);
e17a4113 137
6c7a06a3
TT
138 return result;
139}
140
aff410f1
MS
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. */
c906108c 144
6aecb9c2
JB
145void
146c_emit_char (int c, struct type *type,
147 struct ui_file *stream, int quoter)
c906108c 148{
6c7a06a3 149 const char *encoding;
234b45d4 150
f870a310 151 classify_type (type, get_type_arch (type), &encoding);
3b2b8fea 152 generic_emit_char (c, type, stream, quoter, encoding);
c906108c
SS
153}
154
155void
6c7a06a3 156c_printchar (int c, struct type *type, struct ui_file *stream)
c906108c 157{
6c7a06a3 158 enum c_string_type str_type;
6c7a06a3 159
f870a310 160 str_type = classify_type (type, get_type_arch (type), NULL);
6c7a06a3
TT
161 switch (str_type)
162 {
163 case C_CHAR:
164 break;
165 case C_WIDE_CHAR:
166 fputc_filtered ('L', stream);
167 break;
168 case C_CHAR_16:
169 fputc_filtered ('u', stream);
170 break;
171 case C_CHAR_32:
172 fputc_filtered ('U', stream);
173 break;
174 }
175
c906108c 176 fputc_filtered ('\'', stream);
6c7a06a3 177 LA_EMIT_CHAR (c, type, stream, '\'');
c906108c
SS
178 fputc_filtered ('\'', stream);
179}
180
aff410f1
MS
181/* Print the character string STRING, printing at most LENGTH
182 characters. LENGTH is -1 if the string is nul terminated. Each
183 character is WIDTH bytes long. Printing stops early if the number
184 hits print_max; repeat counts are printed as appropriate. Print
185 ellipses at the end if we had to stop before printing LENGTH
186 characters, or if FORCE_ELLIPSES. */
c906108c
SS
187
188void
aff410f1
MS
189c_printstr (struct ui_file *stream, struct type *type,
190 const gdb_byte *string, unsigned int length,
191 const char *user_encoding, int force_ellipses,
79a45b7d 192 const struct value_print_options *options)
c906108c 193{
3b2b8fea
TT
194 enum c_string_type str_type;
195 const char *type_encoding;
196 const char *encoding;
197
f870a310
TT
198 str_type = (classify_type (type, get_type_arch (type), &type_encoding)
199 & ~C_CHAR);
6c7a06a3
TT
200 switch (str_type)
201 {
202 case C_STRING:
203 break;
204 case C_WIDE_STRING:
205 fputs_filtered ("L", stream);
206 break;
207 case C_STRING_16:
208 fputs_filtered ("u", stream);
209 break;
210 case C_STRING_32:
211 fputs_filtered ("U", stream);
212 break;
213 }
214
3b2b8fea 215 encoding = (user_encoding && *user_encoding) ? user_encoding : type_encoding;
6c7a06a3 216
3b2b8fea
TT
217 generic_printstr (stream, type, string, length, encoding, force_ellipses,
218 '"', 1, options);
c906108c 219}
ae6a3a4c
TJB
220
221/* Obtain a C string from the inferior storing it in a newly allocated
aff410f1
MS
222 buffer in BUFFER, which should be freed by the caller. If the in-
223 and out-parameter *LENGTH is specified at -1, the string is read
fbb8f299 224 until a null character of the appropriate width is found, otherwise
aff410f1
MS
225 the string is read to the length of characters specified. The size
226 of a character is determined by the length of the target type of
0987cf35
DE
227 the pointer or array.
228
229 If VALUE is an array with a known length, and *LENGTH is -1,
230 the function will not read past the end of the array. However, any
231 declared size of the array is ignored if *LENGTH > 0.
232
233 On completion, *LENGTH will be set to the size of the string read in
fbb8f299
PM
234 characters. (If a length of -1 is specified, the length returned
235 will not include the null character). CHARSET is always set to the
236 target charset. */
ae6a3a4c
TJB
237
238void
aff410f1
MS
239c_get_string (struct value *value, gdb_byte **buffer,
240 int *length, struct type **char_type,
241 const char **charset)
ae6a3a4c
TJB
242{
243 int err, width;
244 unsigned int fetchlimit;
245 struct type *type = check_typedef (value_type (value));
246 struct type *element_type = TYPE_TARGET_TYPE (type);
fbb8f299 247 int req_length = *length;
aff410f1
MS
248 enum bfd_endian byte_order
249 = gdbarch_byte_order (get_type_arch (type));
ae6a3a4c
TJB
250
251 if (element_type == NULL)
252 goto error;
253
254 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
255 {
aff410f1
MS
256 /* If we know the size of the array, we can use it as a limit on
257 the number of characters to be fetched. */
ae6a3a4c
TJB
258 if (TYPE_NFIELDS (type) == 1
259 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_RANGE)
260 {
261 LONGEST low_bound, high_bound;
262
263 get_discrete_bounds (TYPE_FIELD_TYPE (type, 0),
264 &low_bound, &high_bound);
265 fetchlimit = high_bound - low_bound + 1;
266 }
267 else
268 fetchlimit = UINT_MAX;
269 }
270 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
271 fetchlimit = UINT_MAX;
272 else
273 /* We work only with arrays and pointers. */
274 goto error;
275
96c07c5b 276 if (! c_textual_element_type (element_type, 0))
ae6a3a4c 277 goto error;
df54f8eb 278 classify_type (element_type, get_type_arch (element_type), charset);
ae6a3a4c
TJB
279 width = TYPE_LENGTH (element_type);
280
aff410f1
MS
281 /* If the string lives in GDB's memory instead of the inferior's,
282 then we just need to copy it to BUFFER. Also, since such strings
283 are arrays with known size, FETCHLIMIT will hold the size of the
284 array. */
ae6a3a4c
TJB
285 if ((VALUE_LVAL (value) == not_lval
286 || VALUE_LVAL (value) == lval_internalvar)
287 && fetchlimit != UINT_MAX)
288 {
289 int i;
290 const gdb_byte *contents = value_contents (value);
291
fbb8f299
PM
292 /* If a length is specified, use that. */
293 if (*length >= 0)
294 i = *length;
295 else
296 /* Otherwise, look for a null character. */
297 for (i = 0; i < fetchlimit; i++)
aff410f1
MS
298 if (extract_unsigned_integer (contents + i * width,
299 width, byte_order) == 0)
fbb8f299
PM
300 break;
301
302 /* I is now either a user-defined length, the number of non-null
303 characters, or FETCHLIMIT. */
ae6a3a4c 304 *length = i * width;
224c3ddb 305 *buffer = (gdb_byte *) xmalloc (*length);
ae6a3a4c
TJB
306 memcpy (*buffer, contents, *length);
307 err = 0;
308 }
309 else
310 {
621c8364
TT
311 CORE_ADDR addr = value_as_address (value);
312
0987cf35
DE
313 /* Prior to the fix for PR 16196 read_string would ignore fetchlimit
314 if length > 0. The old "broken" behaviour is the behaviour we want:
315 The caller may want to fetch 100 bytes from a variable length array
316 implemented using the common idiom of having an array of length 1 at
317 the end of a struct. In this case we want to ignore the declared
318 size of the array. However, it's counterintuitive to implement that
319 behaviour in read_string: what does fetchlimit otherwise mean if
320 length > 0. Therefore we implement the behaviour we want here:
321 If *length > 0, don't specify a fetchlimit. This preserves the
322 previous behaviour. We could move this check above where we know
323 whether the array is declared with a fixed size, but we only want
324 to apply this behaviour when calling read_string. PR 16286. */
325 if (*length > 0)
326 fetchlimit = UINT_MAX;
327
621c8364
TT
328 err = read_string (addr, *length, width, fetchlimit,
329 byte_order, buffer, length);
d09f2c3f 330 if (err != 0)
ae6a3a4c 331 {
8ea5dfdf 332 xfree (*buffer);
d09f2c3f 333 memory_error (TARGET_XFER_E_IO, addr);
ae6a3a4c
TJB
334 }
335 }
336
fbb8f299
PM
337 /* If the LENGTH is specified at -1, we want to return the string
338 length up to the terminating null character. If an actual length
339 was specified, we want to return the length of exactly what was
340 read. */
341 if (req_length == -1)
342 /* If the last character is null, subtract it from LENGTH. */
343 if (*length > 0
aff410f1
MS
344 && extract_unsigned_integer (*buffer + *length - width,
345 width, byte_order) == 0)
fbb8f299
PM
346 *length -= width;
347
348 /* The read_string function will return the number of bytes read.
349 If length returned from read_string was > 0, return the number of
350 characters read by dividing the number of bytes by width. */
351 if (*length != 0)
352 *length = *length / width;
ae6a3a4c 353
96c07c5b 354 *char_type = element_type;
ae6a3a4c
TJB
355
356 return;
357
358 error:
359 {
360 char *type_str;
361
362 type_str = type_to_string (type);
363 if (type_str)
364 {
365 make_cleanup (xfree, type_str);
366 error (_("Trying to read string with inappropriate type `%s'."),
367 type_str);
368 }
369 else
370 error (_("Trying to read string with inappropriate type."));
371 }
372}
373
c906108c 374\f
6c7a06a3
TT
375/* Evaluating C and C++ expressions. */
376
377/* Convert a UCN. The digits of the UCN start at P and extend no
378 farther than LIMIT. DEST_CHARSET is the name of the character set
379 into which the UCN should be converted. The results are written to
380 OUTPUT. LENGTH is the maximum length of the UCN, either 4 or 8.
381 Returns a pointer to just after the final digit of the UCN. */
382
383static char *
384convert_ucn (char *p, char *limit, const char *dest_charset,
385 struct obstack *output, int length)
386{
387 unsigned long result = 0;
388 gdb_byte data[4];
389 int i;
390
391 for (i = 0; i < length && p < limit && isxdigit (*p); ++i, ++p)
392 result = (result << 4) + host_hex_value (*p);
393
394 for (i = 3; i >= 0; --i)
395 {
396 data[i] = result & 0xff;
397 result >>= 8;
398 }
399
aff410f1
MS
400 convert_between_encodings ("UTF-32BE", dest_charset, data,
401 4, 4, output, translit_none);
6c7a06a3
TT
402
403 return p;
404}
405
406/* Emit a character, VALUE, which was specified numerically, to
407 OUTPUT. TYPE is the target character type. */
408
409static void
410emit_numeric_character (struct type *type, unsigned long value,
411 struct obstack *output)
412{
413 gdb_byte *buffer;
414
224c3ddb 415 buffer = (gdb_byte *) alloca (TYPE_LENGTH (type));
6c7a06a3
TT
416 pack_long (buffer, type, value);
417 obstack_grow (output, buffer, TYPE_LENGTH (type));
418}
419
420/* Convert an octal escape sequence. TYPE is the target character
421 type. The digits of the escape sequence begin at P and extend no
422 farther than LIMIT. The result is written to OUTPUT. Returns a
423 pointer to just after the final digit of the escape sequence. */
424
425static char *
aff410f1
MS
426convert_octal (struct type *type, char *p,
427 char *limit, struct obstack *output)
6c7a06a3 428{
30b66ecc 429 int i;
6c7a06a3
TT
430 unsigned long value = 0;
431
30b66ecc
TT
432 for (i = 0;
433 i < 3 && p < limit && isdigit (*p) && *p != '8' && *p != '9';
434 ++i)
6c7a06a3
TT
435 {
436 value = 8 * value + host_hex_value (*p);
437 ++p;
438 }
439
440 emit_numeric_character (type, value, output);
441
442 return p;
443}
444
445/* Convert a hex escape sequence. TYPE is the target character type.
446 The digits of the escape sequence begin at P and extend no farther
447 than LIMIT. The result is written to OUTPUT. Returns a pointer to
448 just after the final digit of the escape sequence. */
449
450static char *
aff410f1
MS
451convert_hex (struct type *type, char *p,
452 char *limit, struct obstack *output)
6c7a06a3
TT
453{
454 unsigned long value = 0;
455
456 while (p < limit && isxdigit (*p))
457 {
458 value = 16 * value + host_hex_value (*p);
459 ++p;
460 }
461
462 emit_numeric_character (type, value, output);
463
464 return p;
465}
466
467#define ADVANCE \
468 do { \
469 ++p; \
470 if (p == limit) \
471 error (_("Malformed escape sequence")); \
472 } while (0)
473
474/* Convert an escape sequence to a target format. TYPE is the target
475 character type to use, and DEST_CHARSET is the name of the target
476 character set. The backslash of the escape sequence is at *P, and
477 the escape sequence will not extend past LIMIT. The results are
478 written to OUTPUT. Returns a pointer to just past the final
479 character of the escape sequence. */
480
481static char *
482convert_escape (struct type *type, const char *dest_charset,
483 char *p, char *limit, struct obstack *output)
484{
485 /* Skip the backslash. */
486 ADVANCE;
487
488 switch (*p)
489 {
490 case '\\':
491 obstack_1grow (output, '\\');
492 ++p;
493 break;
494
495 case 'x':
496 ADVANCE;
497 if (!isxdigit (*p))
498 error (_("\\x used with no following hex digits."));
499 p = convert_hex (type, p, limit, output);
500 break;
501
502 case '0':
503 case '1':
504 case '2':
505 case '3':
506 case '4':
507 case '5':
508 case '6':
509 case '7':
510 p = convert_octal (type, p, limit, output);
511 break;
512
513 case 'u':
514 case 'U':
515 {
516 int length = *p == 'u' ? 4 : 8;
c5504eaf 517
6c7a06a3
TT
518 ADVANCE;
519 if (!isxdigit (*p))
520 error (_("\\u used with no following hex digits"));
521 p = convert_ucn (p, limit, dest_charset, output, length);
522 }
523 }
524
525 return p;
526}
527
528/* Given a single string from a (C-specific) OP_STRING list, convert
529 it to a target string, handling escape sequences specially. The
530 output is written to OUTPUT. DATA is the input string, which has
531 length LEN. DEST_CHARSET is the name of the target character set,
532 and TYPE is the type of target character to use. */
533
534static void
535parse_one_string (struct obstack *output, char *data, int len,
536 const char *dest_charset, struct type *type)
537{
538 char *limit;
539
540 limit = data + len;
541
542 while (data < limit)
543 {
544 char *p = data;
c5504eaf 545
6c7a06a3
TT
546 /* Look for next escape, or the end of the input. */
547 while (p < limit && *p != '\\')
548 ++p;
549 /* If we saw a run of characters, convert them all. */
550 if (p > data)
551 convert_between_encodings (host_charset (), dest_charset,
ac91cd70 552 (gdb_byte *) data, p - data, 1,
aff410f1 553 output, translit_none);
6c7a06a3
TT
554 /* If we saw an escape, convert it. */
555 if (p < limit)
556 p = convert_escape (type, dest_charset, p, limit, output);
557 data = p;
558 }
559}
560
561/* Expression evaluator for the C language family. Most operations
562 are delegated to evaluate_subexp_standard; see that function for a
563 description of the arguments. */
564
f4b8a18d 565struct value *
6c7a06a3
TT
566evaluate_subexp_c (struct type *expect_type, struct expression *exp,
567 int *pos, enum noside noside)
568{
569 enum exp_opcode op = exp->elts[*pos].opcode;
570
571 switch (op)
572 {
573 case OP_STRING:
574 {
575 int oplen, limit;
576 struct type *type;
577 struct obstack output;
578 struct cleanup *cleanup;
579 struct value *result;
580 enum c_string_type dest_type;
581 const char *dest_charset;
c50491a7 582 int satisfy_expected = 0;
6c7a06a3
TT
583
584 obstack_init (&output);
585 cleanup = make_cleanup_obstack_free (&output);
586
587 ++*pos;
588 oplen = longest_to_int (exp->elts[*pos].longconst);
589
590 ++*pos;
591 limit = *pos + BYTES_TO_EXP_ELEM (oplen + 1);
592 dest_type
593 = (enum c_string_type) longest_to_int (exp->elts[*pos].longconst);
594 switch (dest_type & ~C_CHAR)
595 {
596 case C_STRING:
d80b854b
UW
597 type = language_string_char_type (exp->language_defn,
598 exp->gdbarch);
6c7a06a3
TT
599 break;
600 case C_WIDE_STRING:
e6c014f2
UW
601 type = lookup_typename (exp->language_defn, exp->gdbarch,
602 "wchar_t", NULL, 0);
6c7a06a3
TT
603 break;
604 case C_STRING_16:
e6c014f2
UW
605 type = lookup_typename (exp->language_defn, exp->gdbarch,
606 "char16_t", NULL, 0);
6c7a06a3
TT
607 break;
608 case C_STRING_32:
e6c014f2
UW
609 type = lookup_typename (exp->language_defn, exp->gdbarch,
610 "char32_t", NULL, 0);
6c7a06a3
TT
611 break;
612 default:
9b20d036 613 internal_error (__FILE__, __LINE__, _("unhandled c_string_type"));
6c7a06a3 614 }
546e879e
TT
615
616 /* Ensure TYPE_LENGTH is valid for TYPE. */
617 check_typedef (type);
618
c50491a7
TT
619 /* If the caller expects an array of some integral type,
620 satisfy them. If something odder is expected, rely on the
621 caller to cast. */
622 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_ARRAY)
623 {
624 struct type *element_type
625 = check_typedef (TYPE_TARGET_TYPE (expect_type));
626
627 if (TYPE_CODE (element_type) == TYPE_CODE_INT
628 || TYPE_CODE (element_type) == TYPE_CODE_CHAR)
629 {
630 type = element_type;
631 satisfy_expected = 1;
632 }
633 }
634
f870a310 635 dest_charset = charset_for_string_type (dest_type, exp->gdbarch);
6c7a06a3
TT
636
637 ++*pos;
638 while (*pos < limit)
639 {
640 int len;
641
642 len = longest_to_int (exp->elts[*pos].longconst);
643
644 ++*pos;
645 if (noside != EVAL_SKIP)
646 parse_one_string (&output, &exp->elts[*pos].string, len,
647 dest_charset, type);
648 *pos += BYTES_TO_EXP_ELEM (len);
649 }
650
651 /* Skip the trailing length and opcode. */
652 *pos += 2;
653
654 if (noside == EVAL_SKIP)
334cc82d
TT
655 {
656 /* Return a dummy value of the appropriate type. */
c50491a7
TT
657 if (expect_type != NULL)
658 result = allocate_value (expect_type);
659 else if ((dest_type & C_CHAR) != 0)
334cc82d
TT
660 result = allocate_value (type);
661 else
3b7538c0 662 result = value_cstring ("", 0, type);
334cc82d
TT
663 do_cleanups (cleanup);
664 return result;
665 }
6c7a06a3
TT
666
667 if ((dest_type & C_CHAR) != 0)
668 {
669 LONGEST value;
670
671 if (obstack_object_size (&output) != TYPE_LENGTH (type))
3e43a32a
MS
672 error (_("Could not convert character "
673 "constant to target character set"));
51a5cd90 674 value = unpack_long (type, (gdb_byte *) obstack_base (&output));
6c7a06a3
TT
675 result = value_from_longest (type, value);
676 }
677 else
678 {
679 int i;
c5504eaf 680
6c7a06a3
TT
681 /* Write the terminating character. */
682 for (i = 0; i < TYPE_LENGTH (type); ++i)
683 obstack_1grow (&output, 0);
c50491a7
TT
684
685 if (satisfy_expected)
686 {
687 LONGEST low_bound, high_bound;
688 int element_size = TYPE_LENGTH (type);
689
690 if (get_discrete_bounds (TYPE_INDEX_TYPE (expect_type),
691 &low_bound, &high_bound) < 0)
692 {
693 low_bound = 0;
694 high_bound = (TYPE_LENGTH (expect_type) / element_size) - 1;
695 }
696 if (obstack_object_size (&output) / element_size
697 > (high_bound - low_bound + 1))
698 error (_("Too many array elements"));
699
700 result = allocate_value (expect_type);
701 memcpy (value_contents_raw (result), obstack_base (&output),
702 obstack_object_size (&output));
703 }
704 else
705 result = value_cstring (obstack_base (&output),
706 obstack_object_size (&output),
707 type);
6c7a06a3
TT
708 }
709 do_cleanups (cleanup);
710 return result;
711 }
712 break;
713
714 default:
715 break;
716 }
717 return evaluate_subexp_standard (expect_type, exp, pos, noside);
718}
c5aa993b 719
84f0252a 720
84f0252a 721\f
c906108c
SS
722/* Table mapping opcodes into strings for printing operators
723 and precedences of the operators. */
724
725const struct op_print c_op_print_tab[] =
c5aa993b
JM
726{
727 {",", BINOP_COMMA, PREC_COMMA, 0},
728 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
729 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
730 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
731 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
732 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
733 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
734 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
735 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
736 {"<=", BINOP_LEQ, PREC_ORDER, 0},
737 {">=", BINOP_GEQ, PREC_ORDER, 0},
738 {">", BINOP_GTR, PREC_ORDER, 0},
739 {"<", BINOP_LESS, PREC_ORDER, 0},
740 {">>", BINOP_RSH, PREC_SHIFT, 0},
741 {"<<", BINOP_LSH, PREC_SHIFT, 0},
742 {"+", BINOP_ADD, PREC_ADD, 0},
743 {"-", BINOP_SUB, PREC_ADD, 0},
744 {"*", BINOP_MUL, PREC_MUL, 0},
745 {"/", BINOP_DIV, PREC_MUL, 0},
746 {"%", BINOP_REM, PREC_MUL, 0},
747 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
a016fc87 748 {"+", UNOP_PLUS, PREC_PREFIX, 0},
c5aa993b
JM
749 {"-", UNOP_NEG, PREC_PREFIX, 0},
750 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
751 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
752 {"*", UNOP_IND, PREC_PREFIX, 0},
753 {"&", UNOP_ADDR, PREC_PREFIX, 0},
754 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
755 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
756 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
f486487f 757 {NULL, OP_NULL, PREC_PREFIX, 0}
c906108c
SS
758};
759\f
685419e2
AC
760enum c_primitive_types {
761 c_primitive_type_int,
762 c_primitive_type_long,
763 c_primitive_type_short,
764 c_primitive_type_char,
765 c_primitive_type_float,
766 c_primitive_type_double,
767 c_primitive_type_void,
768 c_primitive_type_long_long,
769 c_primitive_type_signed_char,
770 c_primitive_type_unsigned_char,
771 c_primitive_type_unsigned_short,
772 c_primitive_type_unsigned_int,
773 c_primitive_type_unsigned_long,
774 c_primitive_type_unsigned_long_long,
775 c_primitive_type_long_double,
776 c_primitive_type_complex,
777 c_primitive_type_double_complex,
213e4dc2
TJB
778 c_primitive_type_decfloat,
779 c_primitive_type_decdouble,
780 c_primitive_type_declong,
685419e2
AC
781 nr_c_primitive_types
782};
783
e9667a65 784void
685419e2
AC
785c_language_arch_info (struct gdbarch *gdbarch,
786 struct language_arch_info *lai)
787{
788 const struct builtin_type *builtin = builtin_type (gdbarch);
c5504eaf 789
e9667a65 790 lai->string_char_type = builtin->builtin_char;
685419e2
AC
791 lai->primitive_type_vector
792 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_c_primitive_types + 1,
793 struct type *);
794 lai->primitive_type_vector [c_primitive_type_int] = builtin->builtin_int;
795 lai->primitive_type_vector [c_primitive_type_long] = builtin->builtin_long;
796 lai->primitive_type_vector [c_primitive_type_short] = builtin->builtin_short;
797 lai->primitive_type_vector [c_primitive_type_char] = builtin->builtin_char;
798 lai->primitive_type_vector [c_primitive_type_float] = builtin->builtin_float;
799 lai->primitive_type_vector [c_primitive_type_double] = builtin->builtin_double;
800 lai->primitive_type_vector [c_primitive_type_void] = builtin->builtin_void;
801 lai->primitive_type_vector [c_primitive_type_long_long] = builtin->builtin_long_long;
802 lai->primitive_type_vector [c_primitive_type_signed_char] = builtin->builtin_signed_char;
803 lai->primitive_type_vector [c_primitive_type_unsigned_char] = builtin->builtin_unsigned_char;
804 lai->primitive_type_vector [c_primitive_type_unsigned_short] = builtin->builtin_unsigned_short;
805 lai->primitive_type_vector [c_primitive_type_unsigned_int] = builtin->builtin_unsigned_int;
806 lai->primitive_type_vector [c_primitive_type_unsigned_long] = builtin->builtin_unsigned_long;
807 lai->primitive_type_vector [c_primitive_type_unsigned_long_long] = builtin->builtin_unsigned_long_long;
808 lai->primitive_type_vector [c_primitive_type_long_double] = builtin->builtin_long_double;
809 lai->primitive_type_vector [c_primitive_type_complex] = builtin->builtin_complex;
810 lai->primitive_type_vector [c_primitive_type_double_complex] = builtin->builtin_double_complex;
213e4dc2
TJB
811 lai->primitive_type_vector [c_primitive_type_decfloat] = builtin->builtin_decfloat;
812 lai->primitive_type_vector [c_primitive_type_decdouble] = builtin->builtin_decdouble;
813 lai->primitive_type_vector [c_primitive_type_declong] = builtin->builtin_declong;
fbb06eb1
UW
814
815 lai->bool_type_default = builtin->builtin_int;
cad351d1 816}
685419e2 817
6aecb9c2 818const struct exp_descriptor exp_descriptor_c =
6c7a06a3
TT
819{
820 print_subexp_standard,
821 operator_length_standard,
c0201579 822 operator_check_standard,
6c7a06a3
TT
823 op_name_standard,
824 dump_subexp_body_standard,
825 evaluate_subexp_c
826};
827
c5aa993b
JM
828const struct language_defn c_language_defn =
829{
c906108c 830 "c", /* Language name */
6abde28f 831 "C",
c906108c 832 language_c,
c906108c 833 range_check_off,
63872f9d 834 case_sensitive_on,
7ca2d3a3 835 array_row_major,
9a044a89 836 macro_expansion_c,
6c7a06a3 837 &exp_descriptor_c,
7c8adf68 838 c_parse,
c906108c 839 c_error,
e85c3284 840 null_post_parser,
c906108c
SS
841 c_printchar, /* Print a character constant */
842 c_printstr, /* Function to print string constant */
843 c_emit_char, /* Print a single char */
c906108c 844 c_print_type, /* Print a type using appropriate syntax */
5c6ce71d 845 c_print_typedef, /* Print a typedef using appropriate syntax */
c906108c
SS
846 c_val_print, /* Print a value using appropriate syntax */
847 c_value_print, /* Print a top-level value */
a5ee536b 848 default_read_var_value, /* la_read_var_value */
f636b87d 849 NULL, /* Language specific skip_trampoline */
2b2d9e11 850 NULL, /* name_of_this */
5f9a71c3 851 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 852 basic_lookup_transparent_type,/* lookup_transparent_type */
9a3d7dfd 853 NULL, /* Language specific symbol demangler */
aff410f1
MS
854 NULL, /* Language specific
855 class_name_from_physname */
c906108c
SS
856 c_op_print_tab, /* expression operators for printing */
857 1, /* c-style arrays */
858 0, /* String lower bound */
6084f43a 859 default_word_break_characters,
41d27058 860 default_make_symbol_completion_list,
685419e2 861 c_language_arch_info,
e79af960 862 default_print_array_index,
41f1b697 863 default_pass_by_reference,
ae6a3a4c 864 c_get_string,
1a119f36 865 NULL, /* la_get_symbol_name_cmp */
f8eba3c6 866 iterate_over_symbols,
a53b64ea 867 &c_varobj_ops,
bb2ec1b3
TT
868 c_get_compile_context,
869 c_compute_program,
c906108c
SS
870 LANG_MAGIC
871};
872
cad351d1
UW
873enum cplus_primitive_types {
874 cplus_primitive_type_int,
875 cplus_primitive_type_long,
876 cplus_primitive_type_short,
877 cplus_primitive_type_char,
878 cplus_primitive_type_float,
879 cplus_primitive_type_double,
880 cplus_primitive_type_void,
881 cplus_primitive_type_long_long,
882 cplus_primitive_type_signed_char,
883 cplus_primitive_type_unsigned_char,
884 cplus_primitive_type_unsigned_short,
885 cplus_primitive_type_unsigned_int,
886 cplus_primitive_type_unsigned_long,
887 cplus_primitive_type_unsigned_long_long,
888 cplus_primitive_type_long_double,
889 cplus_primitive_type_complex,
890 cplus_primitive_type_double_complex,
891 cplus_primitive_type_bool,
213e4dc2
TJB
892 cplus_primitive_type_decfloat,
893 cplus_primitive_type_decdouble,
894 cplus_primitive_type_declong,
cad351d1 895 nr_cplus_primitive_types
c906108c
SS
896};
897
cad351d1
UW
898static void
899cplus_language_arch_info (struct gdbarch *gdbarch,
900 struct language_arch_info *lai)
901{
902 const struct builtin_type *builtin = builtin_type (gdbarch);
c5504eaf 903
cad351d1
UW
904 lai->string_char_type = builtin->builtin_char;
905 lai->primitive_type_vector
906 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_cplus_primitive_types + 1,
907 struct type *);
908 lai->primitive_type_vector [cplus_primitive_type_int]
909 = builtin->builtin_int;
910 lai->primitive_type_vector [cplus_primitive_type_long]
911 = builtin->builtin_long;
912 lai->primitive_type_vector [cplus_primitive_type_short]
913 = builtin->builtin_short;
914 lai->primitive_type_vector [cplus_primitive_type_char]
915 = builtin->builtin_char;
916 lai->primitive_type_vector [cplus_primitive_type_float]
917 = builtin->builtin_float;
918 lai->primitive_type_vector [cplus_primitive_type_double]
919 = builtin->builtin_double;
920 lai->primitive_type_vector [cplus_primitive_type_void]
921 = builtin->builtin_void;
922 lai->primitive_type_vector [cplus_primitive_type_long_long]
923 = builtin->builtin_long_long;
924 lai->primitive_type_vector [cplus_primitive_type_signed_char]
925 = builtin->builtin_signed_char;
926 lai->primitive_type_vector [cplus_primitive_type_unsigned_char]
927 = builtin->builtin_unsigned_char;
928 lai->primitive_type_vector [cplus_primitive_type_unsigned_short]
929 = builtin->builtin_unsigned_short;
930 lai->primitive_type_vector [cplus_primitive_type_unsigned_int]
931 = builtin->builtin_unsigned_int;
932 lai->primitive_type_vector [cplus_primitive_type_unsigned_long]
933 = builtin->builtin_unsigned_long;
934 lai->primitive_type_vector [cplus_primitive_type_unsigned_long_long]
935 = builtin->builtin_unsigned_long_long;
936 lai->primitive_type_vector [cplus_primitive_type_long_double]
937 = builtin->builtin_long_double;
938 lai->primitive_type_vector [cplus_primitive_type_complex]
939 = builtin->builtin_complex;
940 lai->primitive_type_vector [cplus_primitive_type_double_complex]
941 = builtin->builtin_double_complex;
942 lai->primitive_type_vector [cplus_primitive_type_bool]
943 = builtin->builtin_bool;
213e4dc2
TJB
944 lai->primitive_type_vector [cplus_primitive_type_decfloat]
945 = builtin->builtin_decfloat;
946 lai->primitive_type_vector [cplus_primitive_type_decdouble]
947 = builtin->builtin_decdouble;
948 lai->primitive_type_vector [cplus_primitive_type_declong]
949 = builtin->builtin_declong;
fbb06eb1
UW
950
951 lai->bool_type_symbol = "bool";
952 lai->bool_type_default = builtin->builtin_bool;
cad351d1
UW
953}
954
c5aa993b
JM
955const struct language_defn cplus_language_defn =
956{
957 "c++", /* Language name */
6abde28f 958 "C++",
c906108c 959 language_cplus,
c906108c 960 range_check_off,
63872f9d 961 case_sensitive_on,
7ca2d3a3 962 array_row_major,
9a044a89 963 macro_expansion_c,
6c7a06a3 964 &exp_descriptor_c,
7c8adf68 965 c_parse,
c906108c 966 c_error,
e85c3284 967 null_post_parser,
c906108c
SS
968 c_printchar, /* Print a character constant */
969 c_printstr, /* Function to print string constant */
970 c_emit_char, /* Print a single char */
c906108c 971 c_print_type, /* Print a type using appropriate syntax */
5c6ce71d 972 c_print_typedef, /* Print a typedef using appropriate syntax */
c906108c
SS
973 c_val_print, /* Print a value using appropriate syntax */
974 c_value_print, /* Print a top-level value */
a5ee536b 975 default_read_var_value, /* la_read_var_value */
b18be20d 976 cplus_skip_trampoline, /* Language specific skip_trampoline */
2b2d9e11 977 "this", /* name_of_this */
1fcb5155 978 cp_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 979 cp_lookup_transparent_type, /* lookup_transparent_type */
8de20a37 980 gdb_demangle, /* Language specific symbol demangler */
aff410f1
MS
981 cp_class_name_from_physname, /* Language specific
982 class_name_from_physname */
c906108c
SS
983 c_op_print_tab, /* expression operators for printing */
984 1, /* c-style arrays */
985 0, /* String lower bound */
6084f43a 986 default_word_break_characters,
41d27058 987 default_make_symbol_completion_list,
cad351d1 988 cplus_language_arch_info,
e79af960 989 default_print_array_index,
41f1b697 990 cp_pass_by_reference,
ae6a3a4c 991 c_get_string,
1a119f36 992 NULL, /* la_get_symbol_name_cmp */
f8eba3c6 993 iterate_over_symbols,
a53b64ea 994 &cplus_varobj_ops,
bb2ec1b3
TT
995 NULL,
996 NULL,
c906108c
SS
997 LANG_MAGIC
998};
999
c5aa993b
JM
1000const struct language_defn asm_language_defn =
1001{
c906108c 1002 "asm", /* Language name */
6abde28f 1003 "assembly",
c906108c 1004 language_asm,
c906108c 1005 range_check_off,
63872f9d 1006 case_sensitive_on,
7ca2d3a3 1007 array_row_major,
9a044a89 1008 macro_expansion_c,
6c7a06a3 1009 &exp_descriptor_c,
7c8adf68 1010 c_parse,
c906108c 1011 c_error,
e85c3284 1012 null_post_parser,
c906108c
SS
1013 c_printchar, /* Print a character constant */
1014 c_printstr, /* Function to print string constant */
1015 c_emit_char, /* Print a single char */
c906108c 1016 c_print_type, /* Print a type using appropriate syntax */
5c6ce71d 1017 c_print_typedef, /* Print a typedef using appropriate syntax */
c906108c
SS
1018 c_val_print, /* Print a value using appropriate syntax */
1019 c_value_print, /* Print a top-level value */
a5ee536b 1020 default_read_var_value, /* la_read_var_value */
f636b87d 1021 NULL, /* Language specific skip_trampoline */
2b2d9e11 1022 NULL, /* name_of_this */
5f9a71c3 1023 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 1024 basic_lookup_transparent_type,/* lookup_transparent_type */
9a3d7dfd 1025 NULL, /* Language specific symbol demangler */
aff410f1
MS
1026 NULL, /* Language specific
1027 class_name_from_physname */
c906108c
SS
1028 c_op_print_tab, /* expression operators for printing */
1029 1, /* c-style arrays */
1030 0, /* String lower bound */
6084f43a 1031 default_word_break_characters,
41d27058 1032 default_make_symbol_completion_list,
aff410f1 1033 c_language_arch_info, /* FIXME: la_language_arch_info. */
e79af960 1034 default_print_array_index,
41f1b697 1035 default_pass_by_reference,
ae6a3a4c 1036 c_get_string,
1a119f36 1037 NULL, /* la_get_symbol_name_cmp */
f8eba3c6 1038 iterate_over_symbols,
a53b64ea 1039 &default_varobj_ops,
bb2ec1b3
TT
1040 NULL,
1041 NULL,
c906108c
SS
1042 LANG_MAGIC
1043};
1044
20a0e81d
JB
1045/* The following language_defn does not represent a real language.
1046 It just provides a minimal support a-la-C that should allow users
1047 to do some simple operations when debugging applications that use
1048 a language currently not supported by GDB. */
1049
1050const struct language_defn minimal_language_defn =
1051{
1052 "minimal", /* Language name */
6abde28f 1053 "Minimal",
20a0e81d 1054 language_minimal,
20a0e81d 1055 range_check_off,
20a0e81d 1056 case_sensitive_on,
7ca2d3a3 1057 array_row_major,
9a044a89 1058 macro_expansion_c,
6c7a06a3 1059 &exp_descriptor_c,
7c8adf68 1060 c_parse,
20a0e81d 1061 c_error,
e85c3284 1062 null_post_parser,
20a0e81d
JB
1063 c_printchar, /* Print a character constant */
1064 c_printstr, /* Function to print string constant */
1065 c_emit_char, /* Print a single char */
20a0e81d 1066 c_print_type, /* Print a type using appropriate syntax */
5c6ce71d 1067 c_print_typedef, /* Print a typedef using appropriate syntax */
20a0e81d
JB
1068 c_val_print, /* Print a value using appropriate syntax */
1069 c_value_print, /* Print a top-level value */
a5ee536b 1070 default_read_var_value, /* la_read_var_value */
20a0e81d 1071 NULL, /* Language specific skip_trampoline */
2b2d9e11 1072 NULL, /* name_of_this */
5f9a71c3 1073 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 1074 basic_lookup_transparent_type,/* lookup_transparent_type */
20a0e81d 1075 NULL, /* Language specific symbol demangler */
aff410f1
MS
1076 NULL, /* Language specific
1077 class_name_from_physname */
20a0e81d
JB
1078 c_op_print_tab, /* expression operators for printing */
1079 1, /* c-style arrays */
1080 0, /* String lower bound */
6084f43a 1081 default_word_break_characters,
41d27058 1082 default_make_symbol_completion_list,
e9667a65 1083 c_language_arch_info,
e79af960 1084 default_print_array_index,
41f1b697 1085 default_pass_by_reference,
ae6a3a4c 1086 c_get_string,
1a119f36 1087 NULL, /* la_get_symbol_name_cmp */
f8eba3c6 1088 iterate_over_symbols,
a53b64ea 1089 &default_varobj_ops,
bb2ec1b3
TT
1090 NULL,
1091 NULL,
20a0e81d
JB
1092 LANG_MAGIC
1093};
1094
c906108c 1095void
fba45db2 1096_initialize_c_language (void)
c906108c
SS
1097{
1098 add_language (&c_language_defn);
1099 add_language (&cplus_language_defn);
1100 add_language (&asm_language_defn);
20a0e81d 1101 add_language (&minimal_language_defn);
c906108c 1102}
This page took 1.091974 seconds and 4 git commands to generate.