Add support for distinct host and target character sets.
[deliverable/binutils-gdb.git] / gdb / c-lang.c
1 /* C language support routines for GDB, the GNU debugger.
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2002
3 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 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "parser-defs.h"
27 #include "language.h"
28 #include "c-lang.h"
29 #include "valprint.h"
30 #include "macroscope.h"
31 #include "gdb_assert.h"
32 #include "charset.h"
33
34 extern void _initialize_c_language (void);
35 static void c_emit_char (int c, struct ui_file * stream, int quoter);
36
37 /* Print the character C on STREAM as part of the contents of a literal
38 string whose delimiter is QUOTER. Note that that format for printing
39 characters and strings is language specific. */
40
41 static void
42 c_emit_char (register int c, struct ui_file *stream, int quoter)
43 {
44 const char *escape;
45 int host_char;
46
47 c &= 0xFF; /* Avoid sign bit follies */
48
49 escape = c_target_char_has_backslash_escape (c);
50 if (escape)
51 {
52 if (quoter == '"' && strcmp (escape, "0") == 0)
53 /* Print nulls embedded in double quoted strings as \000 to
54 prevent ambiguity. */
55 fprintf_filtered (stream, "\\000");
56 else
57 fprintf_filtered (stream, "\\%s", escape);
58 }
59 else if (target_char_to_host (c, &host_char)
60 && host_char_print_literally (host_char))
61 {
62 if (host_char == '\\' || host_char == quoter)
63 fputs_filtered ("\\", stream);
64 fprintf_filtered (stream, "%c", host_char);
65 }
66 else
67 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
68 }
69
70 void
71 c_printchar (int c, struct ui_file *stream)
72 {
73 fputc_filtered ('\'', stream);
74 LA_EMIT_CHAR (c, stream, '\'');
75 fputc_filtered ('\'', stream);
76 }
77
78 /* Print the character string STRING, printing at most LENGTH characters.
79 LENGTH is -1 if the string is nul terminated. Each character is WIDTH bytes
80 long. Printing stops early if the number hits print_max; repeat counts are
81 printed as appropriate. Print ellipses at the end if we had to stop before
82 printing LENGTH characters, or if FORCE_ELLIPSES. */
83
84 void
85 c_printstr (struct ui_file *stream, char *string, unsigned int length,
86 int width, int force_ellipses)
87 {
88 register unsigned int i;
89 unsigned int things_printed = 0;
90 int in_quotes = 0;
91 int need_comma = 0;
92 extern int inspect_it;
93
94 /* If the string was not truncated due to `set print elements', and
95 the last byte of it is a null, we don't print that, in traditional C
96 style. */
97 if (!force_ellipses
98 && length > 0
99 && (extract_unsigned_integer (string + (length - 1) * width, width)
100 == '\0'))
101 length--;
102
103 if (length == 0)
104 {
105 fputs_filtered ("\"\"", stream);
106 return;
107 }
108
109 for (i = 0; i < length && things_printed < print_max; ++i)
110 {
111 /* Position of the character we are examining
112 to see whether it is repeated. */
113 unsigned int rep1;
114 /* Number of repetitions we have detected so far. */
115 unsigned int reps;
116 unsigned long current_char;
117
118 QUIT;
119
120 if (need_comma)
121 {
122 fputs_filtered (", ", stream);
123 need_comma = 0;
124 }
125
126 current_char = extract_unsigned_integer (string + i * width, width);
127
128 rep1 = i + 1;
129 reps = 1;
130 while (rep1 < length
131 && extract_unsigned_integer (string + rep1 * width, width)
132 == current_char)
133 {
134 ++rep1;
135 ++reps;
136 }
137
138 if (reps > repeat_count_threshold)
139 {
140 if (in_quotes)
141 {
142 if (inspect_it)
143 fputs_filtered ("\\\", ", stream);
144 else
145 fputs_filtered ("\", ", stream);
146 in_quotes = 0;
147 }
148 LA_PRINT_CHAR (current_char, stream);
149 fprintf_filtered (stream, " <repeats %u times>", reps);
150 i = rep1 - 1;
151 things_printed += repeat_count_threshold;
152 need_comma = 1;
153 }
154 else
155 {
156 if (!in_quotes)
157 {
158 if (inspect_it)
159 fputs_filtered ("\\\"", stream);
160 else
161 fputs_filtered ("\"", stream);
162 in_quotes = 1;
163 }
164 LA_EMIT_CHAR (current_char, stream, '"');
165 ++things_printed;
166 }
167 }
168
169 /* Terminate the quotes if necessary. */
170 if (in_quotes)
171 {
172 if (inspect_it)
173 fputs_filtered ("\\\"", stream);
174 else
175 fputs_filtered ("\"", stream);
176 }
177
178 if (force_ellipses || i < length)
179 fputs_filtered ("...", stream);
180 }
181
182 /* Create a fundamental C type using default reasonable for the current
183 target machine.
184
185 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
186 define fundamental types such as "int" or "double". Others (stabs or
187 DWARF version 2, etc) do define fundamental types. For the formats which
188 don't provide fundamental types, gdb can create such types using this
189 function.
190
191 FIXME: Some compilers distinguish explicitly signed integral types
192 (signed short, signed int, signed long) from "regular" integral types
193 (short, int, long) in the debugging information. There is some dis-
194 agreement as to how useful this feature is. In particular, gcc does
195 not support this. Also, only some debugging formats allow the
196 distinction to be passed on to a debugger. For now, we always just
197 use "short", "int", or "long" as the type name, for both the implicit
198 and explicitly signed types. This also makes life easier for the
199 gdb test suite since we don't have to account for the differences
200 in output depending upon what the compiler and debugging format
201 support. We will probably have to re-examine the issue when gdb
202 starts taking it's fundamental type information directly from the
203 debugging information supplied by the compiler. fnf@cygnus.com */
204
205 struct type *
206 c_create_fundamental_type (struct objfile *objfile, int typeid)
207 {
208 register struct type *type = NULL;
209
210 switch (typeid)
211 {
212 default:
213 /* FIXME: For now, if we are asked to produce a type not in this
214 language, create the equivalent of a C integer type with the
215 name "<?type?>". When all the dust settles from the type
216 reconstruction work, this should probably become an error. */
217 type = init_type (TYPE_CODE_INT,
218 TARGET_INT_BIT / TARGET_CHAR_BIT,
219 0, "<?type?>", objfile);
220 warning ("internal error: no C/C++ fundamental type %d", typeid);
221 break;
222 case FT_VOID:
223 type = init_type (TYPE_CODE_VOID,
224 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
225 0, "void", objfile);
226 break;
227 case FT_BOOLEAN:
228 type = init_type (TYPE_CODE_BOOL,
229 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
230 0, "bool", objfile);
231 break;
232 case FT_CHAR:
233 type = init_type (TYPE_CODE_INT,
234 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
235 TYPE_FLAG_NOSIGN, "char", objfile);
236 break;
237 case FT_SIGNED_CHAR:
238 type = init_type (TYPE_CODE_INT,
239 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
240 0, "signed char", objfile);
241 break;
242 case FT_UNSIGNED_CHAR:
243 type = init_type (TYPE_CODE_INT,
244 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
245 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
246 break;
247 case FT_SHORT:
248 type = init_type (TYPE_CODE_INT,
249 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
250 0, "short", objfile);
251 break;
252 case FT_SIGNED_SHORT:
253 type = init_type (TYPE_CODE_INT,
254 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
255 0, "short", objfile); /* FIXME-fnf */
256 break;
257 case FT_UNSIGNED_SHORT:
258 type = init_type (TYPE_CODE_INT,
259 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
260 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
261 break;
262 case FT_INTEGER:
263 type = init_type (TYPE_CODE_INT,
264 TARGET_INT_BIT / TARGET_CHAR_BIT,
265 0, "int", objfile);
266 break;
267 case FT_SIGNED_INTEGER:
268 type = init_type (TYPE_CODE_INT,
269 TARGET_INT_BIT / TARGET_CHAR_BIT,
270 0, "int", objfile); /* FIXME -fnf */
271 break;
272 case FT_UNSIGNED_INTEGER:
273 type = init_type (TYPE_CODE_INT,
274 TARGET_INT_BIT / TARGET_CHAR_BIT,
275 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
276 break;
277 case FT_LONG:
278 type = init_type (TYPE_CODE_INT,
279 TARGET_LONG_BIT / TARGET_CHAR_BIT,
280 0, "long", objfile);
281 break;
282 case FT_SIGNED_LONG:
283 type = init_type (TYPE_CODE_INT,
284 TARGET_LONG_BIT / TARGET_CHAR_BIT,
285 0, "long", objfile); /* FIXME -fnf */
286 break;
287 case FT_UNSIGNED_LONG:
288 type = init_type (TYPE_CODE_INT,
289 TARGET_LONG_BIT / TARGET_CHAR_BIT,
290 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
291 break;
292 case FT_LONG_LONG:
293 type = init_type (TYPE_CODE_INT,
294 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
295 0, "long long", objfile);
296 break;
297 case FT_SIGNED_LONG_LONG:
298 type = init_type (TYPE_CODE_INT,
299 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
300 0, "signed long long", objfile);
301 break;
302 case FT_UNSIGNED_LONG_LONG:
303 type = init_type (TYPE_CODE_INT,
304 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
305 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
306 break;
307 case FT_FLOAT:
308 type = init_type (TYPE_CODE_FLT,
309 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
310 0, "float", objfile);
311 break;
312 case FT_DBL_PREC_FLOAT:
313 type = init_type (TYPE_CODE_FLT,
314 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
315 0, "double", objfile);
316 break;
317 case FT_EXT_PREC_FLOAT:
318 type = init_type (TYPE_CODE_FLT,
319 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
320 0, "long double", objfile);
321 break;
322 case FT_COMPLEX:
323 type = init_type (TYPE_CODE_FLT,
324 2 * TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
325 0, "complex float", objfile);
326 TYPE_TARGET_TYPE (type)
327 = init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
328 0, "float", objfile);
329 break;
330 case FT_DBL_PREC_COMPLEX:
331 type = init_type (TYPE_CODE_FLT,
332 2 * TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
333 0, "complex double", objfile);
334 TYPE_TARGET_TYPE (type)
335 = init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
336 0, "double", objfile);
337 break;
338 case FT_EXT_PREC_COMPLEX:
339 type = init_type (TYPE_CODE_FLT,
340 2 * TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
341 0, "complex long double", objfile);
342 TYPE_TARGET_TYPE (type)
343 = init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
344 0, "long double", objfile);
345 break;
346 case FT_TEMPLATE_ARG:
347 type = init_type (TYPE_CODE_TEMPLATE_ARG,
348 0,
349 0, "<template arg>", objfile);
350 break;
351 }
352 return (type);
353 }
354 \f
355 /* Preprocessing and parsing C and C++ expressions. */
356
357
358 /* When we find that lexptr (the global var defined in parse.c) is
359 pointing at a macro invocation, we expand the invocation, and call
360 scan_macro_expansion to save the old lexptr here and point lexptr
361 into the expanded text. When we reach the end of that, we call
362 end_macro_expansion to pop back to the value we saved here. The
363 macro expansion code promises to return only fully-expanded text,
364 so we don't need to "push" more than one level.
365
366 This is disgusting, of course. It would be cleaner to do all macro
367 expansion beforehand, and then hand that to lexptr. But we don't
368 really know where the expression ends. Remember, in a command like
369
370 (gdb) break *ADDRESS if CONDITION
371
372 we evaluate ADDRESS in the scope of the current frame, but we
373 evaluate CONDITION in the scope of the breakpoint's location. So
374 it's simply wrong to try to macro-expand the whole thing at once. */
375 static char *macro_original_text;
376 static char *macro_expanded_text;
377
378
379 void
380 scan_macro_expansion (char *expansion)
381 {
382 /* We'd better not be trying to push the stack twice. */
383 gdb_assert (! macro_original_text);
384 gdb_assert (! macro_expanded_text);
385
386 /* Save the old lexptr value, so we can return to it when we're done
387 parsing the expanded text. */
388 macro_original_text = lexptr;
389 lexptr = expansion;
390
391 /* Save the expanded text, so we can free it when we're finished. */
392 macro_expanded_text = expansion;
393 }
394
395
396 int
397 scanning_macro_expansion (void)
398 {
399 return macro_original_text != 0;
400 }
401
402
403 void
404 finished_macro_expansion (void)
405 {
406 /* There'd better be something to pop back to, and we better have
407 saved a pointer to the start of the expanded text. */
408 gdb_assert (macro_original_text);
409 gdb_assert (macro_expanded_text);
410
411 /* Pop back to the original text. */
412 lexptr = macro_original_text;
413 macro_original_text = 0;
414
415 /* Free the expanded text. */
416 xfree (macro_expanded_text);
417 macro_expanded_text = 0;
418 }
419
420
421 static void
422 scan_macro_cleanup (void *dummy)
423 {
424 if (macro_original_text)
425 finished_macro_expansion ();
426 }
427
428
429 /* We set these global variables before calling c_parse, to tell it
430 how it to find macro definitions for the expression at hand. */
431 macro_lookup_ftype *expression_macro_lookup_func;
432 void *expression_macro_lookup_baton;
433
434
435 static struct macro_definition *
436 null_macro_lookup (const char *name, void *baton)
437 {
438 return 0;
439 }
440
441
442 static int
443 c_preprocess_and_parse (void)
444 {
445 /* Set up a lookup function for the macro expander. */
446 struct macro_scope *scope = 0;
447 struct cleanup *back_to = make_cleanup (free_current_contents, &scope);
448
449 if (expression_context_block)
450 scope = sal_macro_scope (find_pc_line (expression_context_pc, 0));
451 else
452 scope = default_macro_scope ();
453
454 if (scope)
455 {
456 expression_macro_lookup_func = standard_macro_lookup;
457 expression_macro_lookup_baton = (void *) scope;
458 }
459 else
460 {
461 expression_macro_lookup_func = null_macro_lookup;
462 expression_macro_lookup_baton = 0;
463 }
464
465 gdb_assert (! macro_original_text);
466 make_cleanup (scan_macro_cleanup, 0);
467
468 {
469 int result = c_parse ();
470 do_cleanups (back_to);
471 return result;
472 }
473 }
474
475
476 \f
477 /* Table mapping opcodes into strings for printing operators
478 and precedences of the operators. */
479
480 const struct op_print c_op_print_tab[] =
481 {
482 {",", BINOP_COMMA, PREC_COMMA, 0},
483 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
484 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
485 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
486 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
487 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
488 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
489 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
490 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
491 {"<=", BINOP_LEQ, PREC_ORDER, 0},
492 {">=", BINOP_GEQ, PREC_ORDER, 0},
493 {">", BINOP_GTR, PREC_ORDER, 0},
494 {"<", BINOP_LESS, PREC_ORDER, 0},
495 {">>", BINOP_RSH, PREC_SHIFT, 0},
496 {"<<", BINOP_LSH, PREC_SHIFT, 0},
497 {"+", BINOP_ADD, PREC_ADD, 0},
498 {"-", BINOP_SUB, PREC_ADD, 0},
499 {"*", BINOP_MUL, PREC_MUL, 0},
500 {"/", BINOP_DIV, PREC_MUL, 0},
501 {"%", BINOP_REM, PREC_MUL, 0},
502 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
503 {"-", UNOP_NEG, PREC_PREFIX, 0},
504 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
505 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
506 {"*", UNOP_IND, PREC_PREFIX, 0},
507 {"&", UNOP_ADDR, PREC_PREFIX, 0},
508 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
509 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
510 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
511 {NULL, 0, 0, 0}
512 };
513 \f
514 struct type **const (c_builtin_types[]) =
515 {
516 &builtin_type_int,
517 &builtin_type_long,
518 &builtin_type_short,
519 &builtin_type_char,
520 &builtin_type_float,
521 &builtin_type_double,
522 &builtin_type_void,
523 &builtin_type_long_long,
524 &builtin_type_signed_char,
525 &builtin_type_unsigned_char,
526 &builtin_type_unsigned_short,
527 &builtin_type_unsigned_int,
528 &builtin_type_unsigned_long,
529 &builtin_type_unsigned_long_long,
530 &builtin_type_long_double,
531 &builtin_type_complex,
532 &builtin_type_double_complex,
533 0
534 };
535
536 const struct language_defn c_language_defn =
537 {
538 "c", /* Language name */
539 language_c,
540 c_builtin_types,
541 range_check_off,
542 type_check_off,
543 case_sensitive_on,
544 c_preprocess_and_parse,
545 c_error,
546 evaluate_subexp_standard,
547 c_printchar, /* Print a character constant */
548 c_printstr, /* Function to print string constant */
549 c_emit_char, /* Print a single char */
550 c_create_fundamental_type, /* Create fundamental type in this language */
551 c_print_type, /* Print a type using appropriate syntax */
552 c_val_print, /* Print a value using appropriate syntax */
553 c_value_print, /* Print a top-level value */
554 {"", "", "", ""}, /* Binary format info */
555 {"0%lo", "0", "o", ""}, /* Octal format info */
556 {"%ld", "", "d", ""}, /* Decimal format info */
557 {"0x%lx", "0x", "x", ""}, /* Hex format info */
558 c_op_print_tab, /* expression operators for printing */
559 1, /* c-style arrays */
560 0, /* String lower bound */
561 &builtin_type_char, /* Type of string elements */
562 LANG_MAGIC
563 };
564
565 struct type **const (cplus_builtin_types[]) =
566 {
567 &builtin_type_int,
568 &builtin_type_long,
569 &builtin_type_short,
570 &builtin_type_char,
571 &builtin_type_float,
572 &builtin_type_double,
573 &builtin_type_void,
574 &builtin_type_long_long,
575 &builtin_type_signed_char,
576 &builtin_type_unsigned_char,
577 &builtin_type_unsigned_short,
578 &builtin_type_unsigned_int,
579 &builtin_type_unsigned_long,
580 &builtin_type_unsigned_long_long,
581 &builtin_type_long_double,
582 &builtin_type_complex,
583 &builtin_type_double_complex,
584 &builtin_type_bool,
585 0
586 };
587
588 const struct language_defn cplus_language_defn =
589 {
590 "c++", /* Language name */
591 language_cplus,
592 cplus_builtin_types,
593 range_check_off,
594 type_check_off,
595 case_sensitive_on,
596 c_preprocess_and_parse,
597 c_error,
598 evaluate_subexp_standard,
599 c_printchar, /* Print a character constant */
600 c_printstr, /* Function to print string constant */
601 c_emit_char, /* Print a single char */
602 c_create_fundamental_type, /* Create fundamental type in this language */
603 c_print_type, /* Print a type using appropriate syntax */
604 c_val_print, /* Print a value using appropriate syntax */
605 c_value_print, /* Print a top-level value */
606 {"", "", "", ""}, /* Binary format info */
607 {"0%lo", "0", "o", ""}, /* Octal format info */
608 {"%ld", "", "d", ""}, /* Decimal format info */
609 {"0x%lx", "0x", "x", ""}, /* Hex format info */
610 c_op_print_tab, /* expression operators for printing */
611 1, /* c-style arrays */
612 0, /* String lower bound */
613 &builtin_type_char, /* Type of string elements */
614 LANG_MAGIC
615 };
616
617 const struct language_defn asm_language_defn =
618 {
619 "asm", /* Language name */
620 language_asm,
621 c_builtin_types,
622 range_check_off,
623 type_check_off,
624 case_sensitive_on,
625 c_preprocess_and_parse,
626 c_error,
627 evaluate_subexp_standard,
628 c_printchar, /* Print a character constant */
629 c_printstr, /* Function to print string constant */
630 c_emit_char, /* Print a single char */
631 c_create_fundamental_type, /* Create fundamental type in this language */
632 c_print_type, /* Print a type using appropriate syntax */
633 c_val_print, /* Print a value using appropriate syntax */
634 c_value_print, /* Print a top-level value */
635 {"", "", "", ""}, /* Binary format info */
636 {"0%lo", "0", "o", ""}, /* Octal format info */
637 {"%ld", "", "d", ""}, /* Decimal format info */
638 {"0x%lx", "0x", "x", ""}, /* Hex format info */
639 c_op_print_tab, /* expression operators for printing */
640 1, /* c-style arrays */
641 0, /* String lower bound */
642 &builtin_type_char, /* Type of string elements */
643 LANG_MAGIC
644 };
645
646 void
647 _initialize_c_language (void)
648 {
649 add_language (&c_language_defn);
650 add_language (&cplus_language_defn);
651 add_language (&asm_language_defn);
652 }
This page took 0.05695 seconds and 4 git commands to generate.