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.
5 This file is part of GDB.
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.
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.
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. */
25 #include "expression.h"
26 #include "parser-defs.h"
30 #include "macroscope.h"
31 #include "gdb_assert.h"
33 #include "gdb_string.h"
35 extern void _initialize_c_language (void);
36 static void c_emit_char (int c, struct ui_file * stream, int quoter);
38 /* Print the character C on STREAM as part of the contents of a literal
39 string whose delimiter is QUOTER. Note that that format for printing
40 characters and strings is language specific. */
43 c_emit_char (register int c, struct ui_file *stream, int quoter)
48 c &= 0xFF; /* Avoid sign bit follies */
50 escape = c_target_char_has_backslash_escape (c);
53 if (quoter == '"' && strcmp (escape, "0") == 0)
54 /* Print nulls embedded in double quoted strings as \000 to
56 fprintf_filtered (stream, "\\000");
58 fprintf_filtered (stream, "\\%s", escape);
60 else if (target_char_to_host (c, &host_char)
61 && host_char_print_literally (host_char))
63 if (host_char == '\\' || host_char == quoter)
64 fputs_filtered ("\\", stream);
65 fprintf_filtered (stream, "%c", host_char);
68 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
72 c_printchar (int c, struct ui_file *stream)
74 fputc_filtered ('\'', stream);
75 LA_EMIT_CHAR (c, stream, '\'');
76 fputc_filtered ('\'', stream);
79 /* Print the character string STRING, printing at most LENGTH characters.
80 LENGTH is -1 if the string is nul terminated. Each character is WIDTH bytes
81 long. Printing stops early if the number hits print_max; repeat counts are
82 printed as appropriate. Print ellipses at the end if we had to stop before
83 printing LENGTH characters, or if FORCE_ELLIPSES. */
86 c_printstr (struct ui_file *stream, char *string, unsigned int length,
87 int width, int force_ellipses)
89 register unsigned int i;
90 unsigned int things_printed = 0;
93 extern int inspect_it;
95 /* If the string was not truncated due to `set print elements', and
96 the last byte of it is a null, we don't print that, in traditional C
100 && (extract_unsigned_integer (string + (length - 1) * width, width)
106 fputs_filtered ("\"\"", stream);
110 for (i = 0; i < length && things_printed < print_max; ++i)
112 /* Position of the character we are examining
113 to see whether it is repeated. */
115 /* Number of repetitions we have detected so far. */
117 unsigned long current_char;
123 fputs_filtered (", ", stream);
127 current_char = extract_unsigned_integer (string + i * width, width);
132 && extract_unsigned_integer (string + rep1 * width, width)
139 if (reps > repeat_count_threshold)
144 fputs_filtered ("\\\", ", stream);
146 fputs_filtered ("\", ", stream);
149 LA_PRINT_CHAR (current_char, stream);
150 fprintf_filtered (stream, " <repeats %u times>", reps);
152 things_printed += repeat_count_threshold;
160 fputs_filtered ("\\\"", stream);
162 fputs_filtered ("\"", stream);
165 LA_EMIT_CHAR (current_char, stream, '"');
170 /* Terminate the quotes if necessary. */
174 fputs_filtered ("\\\"", stream);
176 fputs_filtered ("\"", stream);
179 if (force_ellipses || i < length)
180 fputs_filtered ("...", stream);
183 /* Create a fundamental C type using default reasonable for the current
186 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
187 define fundamental types such as "int" or "double". Others (stabs or
188 DWARF version 2, etc) do define fundamental types. For the formats which
189 don't provide fundamental types, gdb can create such types using this
192 FIXME: Some compilers distinguish explicitly signed integral types
193 (signed short, signed int, signed long) from "regular" integral types
194 (short, int, long) in the debugging information. There is some dis-
195 agreement as to how useful this feature is. In particular, gcc does
196 not support this. Also, only some debugging formats allow the
197 distinction to be passed on to a debugger. For now, we always just
198 use "short", "int", or "long" as the type name, for both the implicit
199 and explicitly signed types. This also makes life easier for the
200 gdb test suite since we don't have to account for the differences
201 in output depending upon what the compiler and debugging format
202 support. We will probably have to re-examine the issue when gdb
203 starts taking it's fundamental type information directly from the
207 c_create_fundamental_type (struct objfile *objfile, int typeid)
209 register struct type *type = NULL;
214 /* FIXME: For now, if we are asked to produce a type not in this
215 language, create the equivalent of a C integer type with the
216 name "<?type?>". When all the dust settles from the type
217 reconstruction work, this should probably become an error. */
218 type = init_type (TYPE_CODE_INT,
219 TARGET_INT_BIT / TARGET_CHAR_BIT,
220 0, "<?type?>", objfile);
221 warning ("internal error: no C/C++ fundamental type %d", typeid);
224 type = init_type (TYPE_CODE_VOID,
225 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
229 type = init_type (TYPE_CODE_BOOL,
230 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
234 type = init_type (TYPE_CODE_INT,
235 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
236 TYPE_FLAG_NOSIGN, "char", objfile);
239 type = init_type (TYPE_CODE_INT,
240 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
241 0, "signed char", objfile);
243 case FT_UNSIGNED_CHAR:
244 type = init_type (TYPE_CODE_INT,
245 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
246 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
249 type = init_type (TYPE_CODE_INT,
250 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
251 0, "short", objfile);
253 case FT_SIGNED_SHORT:
254 type = init_type (TYPE_CODE_INT,
255 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
256 0, "short", objfile); /* FIXME-fnf */
258 case FT_UNSIGNED_SHORT:
259 type = init_type (TYPE_CODE_INT,
260 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
261 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
264 type = init_type (TYPE_CODE_INT,
265 TARGET_INT_BIT / TARGET_CHAR_BIT,
268 case FT_SIGNED_INTEGER:
269 type = init_type (TYPE_CODE_INT,
270 TARGET_INT_BIT / TARGET_CHAR_BIT,
271 0, "int", objfile); /* FIXME -fnf */
273 case FT_UNSIGNED_INTEGER:
274 type = init_type (TYPE_CODE_INT,
275 TARGET_INT_BIT / TARGET_CHAR_BIT,
276 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
279 type = init_type (TYPE_CODE_INT,
280 TARGET_LONG_BIT / TARGET_CHAR_BIT,
284 type = init_type (TYPE_CODE_INT,
285 TARGET_LONG_BIT / TARGET_CHAR_BIT,
286 0, "long", objfile); /* FIXME -fnf */
288 case FT_UNSIGNED_LONG:
289 type = init_type (TYPE_CODE_INT,
290 TARGET_LONG_BIT / TARGET_CHAR_BIT,
291 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
294 type = init_type (TYPE_CODE_INT,
295 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
296 0, "long long", objfile);
298 case FT_SIGNED_LONG_LONG:
299 type = init_type (TYPE_CODE_INT,
300 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
301 0, "signed long long", objfile);
303 case FT_UNSIGNED_LONG_LONG:
304 type = init_type (TYPE_CODE_INT,
305 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
306 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
309 type = init_type (TYPE_CODE_FLT,
310 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
311 0, "float", objfile);
313 case FT_DBL_PREC_FLOAT:
314 type = init_type (TYPE_CODE_FLT,
315 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
316 0, "double", objfile);
318 case FT_EXT_PREC_FLOAT:
319 type = init_type (TYPE_CODE_FLT,
320 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
321 0, "long double", objfile);
324 type = init_type (TYPE_CODE_FLT,
325 2 * TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
326 0, "complex float", objfile);
327 TYPE_TARGET_TYPE (type)
328 = init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
329 0, "float", objfile);
331 case FT_DBL_PREC_COMPLEX:
332 type = init_type (TYPE_CODE_FLT,
333 2 * TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
334 0, "complex double", objfile);
335 TYPE_TARGET_TYPE (type)
336 = init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
337 0, "double", objfile);
339 case FT_EXT_PREC_COMPLEX:
340 type = init_type (TYPE_CODE_FLT,
341 2 * TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
342 0, "complex long double", objfile);
343 TYPE_TARGET_TYPE (type)
344 = init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
345 0, "long double", objfile);
347 case FT_TEMPLATE_ARG:
348 type = init_type (TYPE_CODE_TEMPLATE_ARG,
350 0, "<template arg>", objfile);
356 /* Preprocessing and parsing C and C++ expressions. */
359 /* When we find that lexptr (the global var defined in parse.c) is
360 pointing at a macro invocation, we expand the invocation, and call
361 scan_macro_expansion to save the old lexptr here and point lexptr
362 into the expanded text. When we reach the end of that, we call
363 end_macro_expansion to pop back to the value we saved here. The
364 macro expansion code promises to return only fully-expanded text,
365 so we don't need to "push" more than one level.
367 This is disgusting, of course. It would be cleaner to do all macro
368 expansion beforehand, and then hand that to lexptr. But we don't
369 really know where the expression ends. Remember, in a command like
371 (gdb) break *ADDRESS if CONDITION
373 we evaluate ADDRESS in the scope of the current frame, but we
374 evaluate CONDITION in the scope of the breakpoint's location. So
375 it's simply wrong to try to macro-expand the whole thing at once. */
376 static char *macro_original_text;
377 static char *macro_expanded_text;
381 scan_macro_expansion (char *expansion)
383 /* We'd better not be trying to push the stack twice. */
384 gdb_assert (! macro_original_text);
385 gdb_assert (! macro_expanded_text);
387 /* Save the old lexptr value, so we can return to it when we're done
388 parsing the expanded text. */
389 macro_original_text = lexptr;
392 /* Save the expanded text, so we can free it when we're finished. */
393 macro_expanded_text = expansion;
398 scanning_macro_expansion (void)
400 return macro_original_text != 0;
405 finished_macro_expansion (void)
407 /* There'd better be something to pop back to, and we better have
408 saved a pointer to the start of the expanded text. */
409 gdb_assert (macro_original_text);
410 gdb_assert (macro_expanded_text);
412 /* Pop back to the original text. */
413 lexptr = macro_original_text;
414 macro_original_text = 0;
416 /* Free the expanded text. */
417 xfree (macro_expanded_text);
418 macro_expanded_text = 0;
423 scan_macro_cleanup (void *dummy)
425 if (macro_original_text)
426 finished_macro_expansion ();
430 /* We set these global variables before calling c_parse, to tell it
431 how it to find macro definitions for the expression at hand. */
432 macro_lookup_ftype *expression_macro_lookup_func;
433 void *expression_macro_lookup_baton;
436 static struct macro_definition *
437 null_macro_lookup (const char *name, void *baton)
444 c_preprocess_and_parse (void)
446 /* Set up a lookup function for the macro expander. */
447 struct macro_scope *scope = 0;
448 struct cleanup *back_to = make_cleanup (free_current_contents, &scope);
450 if (expression_context_block)
451 scope = sal_macro_scope (find_pc_line (expression_context_pc, 0));
453 scope = default_macro_scope ();
457 expression_macro_lookup_func = standard_macro_lookup;
458 expression_macro_lookup_baton = (void *) scope;
462 expression_macro_lookup_func = null_macro_lookup;
463 expression_macro_lookup_baton = 0;
466 gdb_assert (! macro_original_text);
467 make_cleanup (scan_macro_cleanup, 0);
470 int result = c_parse ();
471 do_cleanups (back_to);
478 /* Table mapping opcodes into strings for printing operators
479 and precedences of the operators. */
481 const struct op_print c_op_print_tab[] =
483 {",", BINOP_COMMA, PREC_COMMA, 0},
484 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
485 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
486 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
487 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
488 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
489 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
490 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
491 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
492 {"<=", BINOP_LEQ, PREC_ORDER, 0},
493 {">=", BINOP_GEQ, PREC_ORDER, 0},
494 {">", BINOP_GTR, PREC_ORDER, 0},
495 {"<", BINOP_LESS, PREC_ORDER, 0},
496 {">>", BINOP_RSH, PREC_SHIFT, 0},
497 {"<<", BINOP_LSH, PREC_SHIFT, 0},
498 {"+", BINOP_ADD, PREC_ADD, 0},
499 {"-", BINOP_SUB, PREC_ADD, 0},
500 {"*", BINOP_MUL, PREC_MUL, 0},
501 {"/", BINOP_DIV, PREC_MUL, 0},
502 {"%", BINOP_REM, PREC_MUL, 0},
503 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
504 {"-", UNOP_NEG, PREC_PREFIX, 0},
505 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
506 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
507 {"*", UNOP_IND, PREC_PREFIX, 0},
508 {"&", UNOP_ADDR, PREC_PREFIX, 0},
509 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
510 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
511 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
515 struct type **const (c_builtin_types[]) =
522 &builtin_type_double,
524 &builtin_type_long_long,
525 &builtin_type_signed_char,
526 &builtin_type_unsigned_char,
527 &builtin_type_unsigned_short,
528 &builtin_type_unsigned_int,
529 &builtin_type_unsigned_long,
530 &builtin_type_unsigned_long_long,
531 &builtin_type_long_double,
532 &builtin_type_complex,
533 &builtin_type_double_complex,
537 const struct language_defn c_language_defn =
539 "c", /* Language name */
545 c_preprocess_and_parse,
547 evaluate_subexp_standard,
548 c_printchar, /* Print a character constant */
549 c_printstr, /* Function to print string constant */
550 c_emit_char, /* Print a single char */
551 c_create_fundamental_type, /* Create fundamental type in this language */
552 c_print_type, /* Print a type using appropriate syntax */
553 c_val_print, /* Print a value using appropriate syntax */
554 c_value_print, /* Print a top-level value */
555 {"", "", "", ""}, /* Binary format info */
556 {"0%lo", "0", "o", ""}, /* Octal format info */
557 {"%ld", "", "d", ""}, /* Decimal format info */
558 {"0x%lx", "0x", "x", ""}, /* Hex format info */
559 c_op_print_tab, /* expression operators for printing */
560 1, /* c-style arrays */
561 0, /* String lower bound */
562 &builtin_type_char, /* Type of string elements */
566 struct type **const (cplus_builtin_types[]) =
573 &builtin_type_double,
575 &builtin_type_long_long,
576 &builtin_type_signed_char,
577 &builtin_type_unsigned_char,
578 &builtin_type_unsigned_short,
579 &builtin_type_unsigned_int,
580 &builtin_type_unsigned_long,
581 &builtin_type_unsigned_long_long,
582 &builtin_type_long_double,
583 &builtin_type_complex,
584 &builtin_type_double_complex,
589 const struct language_defn cplus_language_defn =
591 "c++", /* Language name */
597 c_preprocess_and_parse,
599 evaluate_subexp_standard,
600 c_printchar, /* Print a character constant */
601 c_printstr, /* Function to print string constant */
602 c_emit_char, /* Print a single char */
603 c_create_fundamental_type, /* Create fundamental type in this language */
604 c_print_type, /* Print a type using appropriate syntax */
605 c_val_print, /* Print a value using appropriate syntax */
606 c_value_print, /* Print a top-level value */
607 {"", "", "", ""}, /* Binary format info */
608 {"0%lo", "0", "o", ""}, /* Octal format info */
609 {"%ld", "", "d", ""}, /* Decimal format info */
610 {"0x%lx", "0x", "x", ""}, /* Hex format info */
611 c_op_print_tab, /* expression operators for printing */
612 1, /* c-style arrays */
613 0, /* String lower bound */
614 &builtin_type_char, /* Type of string elements */
618 const struct language_defn asm_language_defn =
620 "asm", /* Language name */
626 c_preprocess_and_parse,
628 evaluate_subexp_standard,
629 c_printchar, /* Print a character constant */
630 c_printstr, /* Function to print string constant */
631 c_emit_char, /* Print a single char */
632 c_create_fundamental_type, /* Create fundamental type in this language */
633 c_print_type, /* Print a type using appropriate syntax */
634 c_val_print, /* Print a value using appropriate syntax */
635 c_value_print, /* Print a top-level value */
636 {"", "", "", ""}, /* Binary format info */
637 {"0%lo", "0", "o", ""}, /* Octal format info */
638 {"%ld", "", "d", ""}, /* Decimal format info */
639 {"0x%lx", "0x", "x", ""}, /* Hex format info */
640 c_op_print_tab, /* expression operators for printing */
641 1, /* c-style arrays */
642 0, /* String lower bound */
643 &builtin_type_char, /* Type of string elements */
648 _initialize_c_language (void)
650 add_language (&c_language_defn);
651 add_language (&cplus_language_defn);
652 add_language (&asm_language_defn);