1 /* C language support routines for GDB, the GNU debugger.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2002, 2003,
4 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "expression.h"
25 #include "parser-defs.h"
29 #include "macroscope.h"
30 #include "gdb_assert.h"
32 #include "gdb_string.h"
35 #include "cp-support.h"
37 extern void _initialize_c_language (void);
38 static void c_emit_char (int c
, struct ui_file
* stream
, int quoter
);
40 /* Print the character C on STREAM as part of the contents of a literal
41 string whose delimiter is QUOTER. Note that that format for printing
42 characters and strings is language specific. */
45 c_emit_char (int c
, struct ui_file
*stream
, int quoter
)
50 c
&= 0xFF; /* Avoid sign bit follies */
52 escape
= c_target_char_has_backslash_escape (c
);
55 if (quoter
== '"' && strcmp (escape
, "0") == 0)
56 /* Print nulls embedded in double quoted strings as \000 to
58 fprintf_filtered (stream
, "\\000");
60 fprintf_filtered (stream
, "\\%s", escape
);
62 else if (target_char_to_host (c
, &host_char
)
63 && host_char_print_literally (host_char
))
65 if (host_char
== '\\' || host_char
== quoter
)
66 fputs_filtered ("\\", stream
);
67 fprintf_filtered (stream
, "%c", host_char
);
70 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
74 c_printchar (int c
, struct ui_file
*stream
)
76 fputc_filtered ('\'', stream
);
77 LA_EMIT_CHAR (c
, stream
, '\'');
78 fputc_filtered ('\'', stream
);
81 /* Print the character string STRING, printing at most LENGTH characters.
82 LENGTH is -1 if the string is nul terminated. Each character is WIDTH bytes
83 long. Printing stops early if the number hits print_max; repeat counts are
84 printed as appropriate. Print ellipses at the end if we had to stop before
85 printing LENGTH characters, or if FORCE_ELLIPSES. */
88 c_printstr (struct ui_file
*stream
, const gdb_byte
*string
,
89 unsigned int length
, int width
, int force_ellipses
)
92 unsigned int things_printed
= 0;
96 /* If the string was not truncated due to `set print elements', and
97 the last byte of it is a null, we don't print that, in traditional C
101 && (extract_unsigned_integer (string
+ (length
- 1) * width
, width
)
107 fputs_filtered ("\"\"", stream
);
111 for (i
= 0; i
< length
&& things_printed
< print_max
; ++i
)
113 /* Position of the character we are examining
114 to see whether it is repeated. */
116 /* Number of repetitions we have detected so far. */
118 unsigned long current_char
;
124 fputs_filtered (", ", stream
);
128 current_char
= extract_unsigned_integer (string
+ i
* width
, width
);
133 && extract_unsigned_integer (string
+ rep1
* width
, width
)
140 if (reps
> repeat_count_threshold
)
145 fputs_filtered ("\\\", ", stream
);
147 fputs_filtered ("\", ", stream
);
150 LA_PRINT_CHAR (current_char
, stream
);
151 fprintf_filtered (stream
, _(" <repeats %u times>"), reps
);
153 things_printed
+= repeat_count_threshold
;
161 fputs_filtered ("\\\"", stream
);
163 fputs_filtered ("\"", stream
);
166 LA_EMIT_CHAR (current_char
, stream
, '"');
171 /* Terminate the quotes if necessary. */
175 fputs_filtered ("\\\"", stream
);
177 fputs_filtered ("\"", stream
);
180 if (force_ellipses
|| i
< length
)
181 fputs_filtered ("...", stream
);
184 /* Preprocessing and parsing C and C++ expressions. */
187 /* When we find that lexptr (the global var defined in parse.c) is
188 pointing at a macro invocation, we expand the invocation, and call
189 scan_macro_expansion to save the old lexptr here and point lexptr
190 into the expanded text. When we reach the end of that, we call
191 end_macro_expansion to pop back to the value we saved here. The
192 macro expansion code promises to return only fully-expanded text,
193 so we don't need to "push" more than one level.
195 This is disgusting, of course. It would be cleaner to do all macro
196 expansion beforehand, and then hand that to lexptr. But we don't
197 really know where the expression ends. Remember, in a command like
199 (gdb) break *ADDRESS if CONDITION
201 we evaluate ADDRESS in the scope of the current frame, but we
202 evaluate CONDITION in the scope of the breakpoint's location. So
203 it's simply wrong to try to macro-expand the whole thing at once. */
204 static char *macro_original_text
;
205 static char *macro_expanded_text
;
209 scan_macro_expansion (char *expansion
)
211 /* We'd better not be trying to push the stack twice. */
212 gdb_assert (! macro_original_text
);
213 gdb_assert (! macro_expanded_text
);
215 /* Save the old lexptr value, so we can return to it when we're done
216 parsing the expanded text. */
217 macro_original_text
= lexptr
;
220 /* Save the expanded text, so we can free it when we're finished. */
221 macro_expanded_text
= expansion
;
226 scanning_macro_expansion (void)
228 return macro_original_text
!= 0;
233 finished_macro_expansion (void)
235 /* There'd better be something to pop back to, and we better have
236 saved a pointer to the start of the expanded text. */
237 gdb_assert (macro_original_text
);
238 gdb_assert (macro_expanded_text
);
240 /* Pop back to the original text. */
241 lexptr
= macro_original_text
;
242 macro_original_text
= 0;
244 /* Free the expanded text. */
245 xfree (macro_expanded_text
);
246 macro_expanded_text
= 0;
251 scan_macro_cleanup (void *dummy
)
253 if (macro_original_text
)
254 finished_macro_expansion ();
258 /* We set these global variables before calling c_parse, to tell it
259 how it to find macro definitions for the expression at hand. */
260 macro_lookup_ftype
*expression_macro_lookup_func
;
261 void *expression_macro_lookup_baton
;
264 static struct macro_definition
*
265 null_macro_lookup (const char *name
, void *baton
)
272 c_preprocess_and_parse (void)
274 /* Set up a lookup function for the macro expander. */
275 struct macro_scope
*scope
= 0;
276 struct cleanup
*back_to
= make_cleanup (free_current_contents
, &scope
);
278 if (expression_context_block
)
279 scope
= sal_macro_scope (find_pc_line (expression_context_pc
, 0));
281 scope
= default_macro_scope ();
285 expression_macro_lookup_func
= standard_macro_lookup
;
286 expression_macro_lookup_baton
= (void *) scope
;
290 expression_macro_lookup_func
= null_macro_lookup
;
291 expression_macro_lookup_baton
= 0;
294 gdb_assert (! macro_original_text
);
295 make_cleanup (scan_macro_cleanup
, 0);
298 int result
= c_parse ();
299 do_cleanups (back_to
);
306 /* Table mapping opcodes into strings for printing operators
307 and precedences of the operators. */
309 const struct op_print c_op_print_tab
[] =
311 {",", BINOP_COMMA
, PREC_COMMA
, 0},
312 {"=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
313 {"||", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
314 {"&&", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
315 {"|", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
316 {"^", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
317 {"&", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
318 {"==", BINOP_EQUAL
, PREC_EQUAL
, 0},
319 {"!=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
320 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
321 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
322 {">", BINOP_GTR
, PREC_ORDER
, 0},
323 {"<", BINOP_LESS
, PREC_ORDER
, 0},
324 {">>", BINOP_RSH
, PREC_SHIFT
, 0},
325 {"<<", BINOP_LSH
, PREC_SHIFT
, 0},
326 {"+", BINOP_ADD
, PREC_ADD
, 0},
327 {"-", BINOP_SUB
, PREC_ADD
, 0},
328 {"*", BINOP_MUL
, PREC_MUL
, 0},
329 {"/", BINOP_DIV
, PREC_MUL
, 0},
330 {"%", BINOP_REM
, PREC_MUL
, 0},
331 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
332 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
333 {"!", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
334 {"~", UNOP_COMPLEMENT
, PREC_PREFIX
, 0},
335 {"*", UNOP_IND
, PREC_PREFIX
, 0},
336 {"&", UNOP_ADDR
, PREC_PREFIX
, 0},
337 {"sizeof ", UNOP_SIZEOF
, PREC_PREFIX
, 0},
338 {"++", UNOP_PREINCREMENT
, PREC_PREFIX
, 0},
339 {"--", UNOP_PREDECREMENT
, PREC_PREFIX
, 0},
343 enum c_primitive_types
{
344 c_primitive_type_int
,
345 c_primitive_type_long
,
346 c_primitive_type_short
,
347 c_primitive_type_char
,
348 c_primitive_type_float
,
349 c_primitive_type_double
,
350 c_primitive_type_void
,
351 c_primitive_type_long_long
,
352 c_primitive_type_signed_char
,
353 c_primitive_type_unsigned_char
,
354 c_primitive_type_unsigned_short
,
355 c_primitive_type_unsigned_int
,
356 c_primitive_type_unsigned_long
,
357 c_primitive_type_unsigned_long_long
,
358 c_primitive_type_long_double
,
359 c_primitive_type_complex
,
360 c_primitive_type_double_complex
,
361 c_primitive_type_decfloat
,
362 c_primitive_type_decdouble
,
363 c_primitive_type_declong
,
368 c_language_arch_info (struct gdbarch
*gdbarch
,
369 struct language_arch_info
*lai
)
371 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
372 lai
->string_char_type
= builtin
->builtin_char
;
373 lai
->primitive_type_vector
374 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_c_primitive_types
+ 1,
376 lai
->primitive_type_vector
[c_primitive_type_int
] = builtin
->builtin_int
;
377 lai
->primitive_type_vector
[c_primitive_type_long
] = builtin
->builtin_long
;
378 lai
->primitive_type_vector
[c_primitive_type_short
] = builtin
->builtin_short
;
379 lai
->primitive_type_vector
[c_primitive_type_char
] = builtin
->builtin_char
;
380 lai
->primitive_type_vector
[c_primitive_type_float
] = builtin
->builtin_float
;
381 lai
->primitive_type_vector
[c_primitive_type_double
] = builtin
->builtin_double
;
382 lai
->primitive_type_vector
[c_primitive_type_void
] = builtin
->builtin_void
;
383 lai
->primitive_type_vector
[c_primitive_type_long_long
] = builtin
->builtin_long_long
;
384 lai
->primitive_type_vector
[c_primitive_type_signed_char
] = builtin
->builtin_signed_char
;
385 lai
->primitive_type_vector
[c_primitive_type_unsigned_char
] = builtin
->builtin_unsigned_char
;
386 lai
->primitive_type_vector
[c_primitive_type_unsigned_short
] = builtin
->builtin_unsigned_short
;
387 lai
->primitive_type_vector
[c_primitive_type_unsigned_int
] = builtin
->builtin_unsigned_int
;
388 lai
->primitive_type_vector
[c_primitive_type_unsigned_long
] = builtin
->builtin_unsigned_long
;
389 lai
->primitive_type_vector
[c_primitive_type_unsigned_long_long
] = builtin
->builtin_unsigned_long_long
;
390 lai
->primitive_type_vector
[c_primitive_type_long_double
] = builtin
->builtin_long_double
;
391 lai
->primitive_type_vector
[c_primitive_type_complex
] = builtin
->builtin_complex
;
392 lai
->primitive_type_vector
[c_primitive_type_double_complex
] = builtin
->builtin_double_complex
;
393 lai
->primitive_type_vector
[c_primitive_type_decfloat
] = builtin
->builtin_decfloat
;
394 lai
->primitive_type_vector
[c_primitive_type_decdouble
] = builtin
->builtin_decdouble
;
395 lai
->primitive_type_vector
[c_primitive_type_declong
] = builtin
->builtin_declong
;
398 const struct language_defn c_language_defn
=
400 "c", /* Language name */
406 &exp_descriptor_standard
,
407 c_preprocess_and_parse
,
410 c_printchar
, /* Print a character constant */
411 c_printstr
, /* Function to print string constant */
412 c_emit_char
, /* Print a single char */
413 c_print_type
, /* Print a type using appropriate syntax */
414 c_val_print
, /* Print a value using appropriate syntax */
415 c_value_print
, /* Print a top-level value */
416 NULL
, /* Language specific skip_trampoline */
417 NULL
, /* value_of_this */
418 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
419 basic_lookup_transparent_type
,/* lookup_transparent_type */
420 NULL
, /* Language specific symbol demangler */
421 NULL
, /* Language specific class_name_from_physname */
422 c_op_print_tab
, /* expression operators for printing */
423 1, /* c-style arrays */
424 0, /* String lower bound */
425 default_word_break_characters
,
426 c_language_arch_info
,
427 default_print_array_index
,
428 default_pass_by_reference
,
432 enum cplus_primitive_types
{
433 cplus_primitive_type_int
,
434 cplus_primitive_type_long
,
435 cplus_primitive_type_short
,
436 cplus_primitive_type_char
,
437 cplus_primitive_type_float
,
438 cplus_primitive_type_double
,
439 cplus_primitive_type_void
,
440 cplus_primitive_type_long_long
,
441 cplus_primitive_type_signed_char
,
442 cplus_primitive_type_unsigned_char
,
443 cplus_primitive_type_unsigned_short
,
444 cplus_primitive_type_unsigned_int
,
445 cplus_primitive_type_unsigned_long
,
446 cplus_primitive_type_unsigned_long_long
,
447 cplus_primitive_type_long_double
,
448 cplus_primitive_type_complex
,
449 cplus_primitive_type_double_complex
,
450 cplus_primitive_type_bool
,
451 cplus_primitive_type_decfloat
,
452 cplus_primitive_type_decdouble
,
453 cplus_primitive_type_declong
,
454 nr_cplus_primitive_types
458 cplus_language_arch_info (struct gdbarch
*gdbarch
,
459 struct language_arch_info
*lai
)
461 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
462 lai
->string_char_type
= builtin
->builtin_char
;
463 lai
->primitive_type_vector
464 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_cplus_primitive_types
+ 1,
466 lai
->primitive_type_vector
[cplus_primitive_type_int
]
467 = builtin
->builtin_int
;
468 lai
->primitive_type_vector
[cplus_primitive_type_long
]
469 = builtin
->builtin_long
;
470 lai
->primitive_type_vector
[cplus_primitive_type_short
]
471 = builtin
->builtin_short
;
472 lai
->primitive_type_vector
[cplus_primitive_type_char
]
473 = builtin
->builtin_char
;
474 lai
->primitive_type_vector
[cplus_primitive_type_float
]
475 = builtin
->builtin_float
;
476 lai
->primitive_type_vector
[cplus_primitive_type_double
]
477 = builtin
->builtin_double
;
478 lai
->primitive_type_vector
[cplus_primitive_type_void
]
479 = builtin
->builtin_void
;
480 lai
->primitive_type_vector
[cplus_primitive_type_long_long
]
481 = builtin
->builtin_long_long
;
482 lai
->primitive_type_vector
[cplus_primitive_type_signed_char
]
483 = builtin
->builtin_signed_char
;
484 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_char
]
485 = builtin
->builtin_unsigned_char
;
486 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_short
]
487 = builtin
->builtin_unsigned_short
;
488 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_int
]
489 = builtin
->builtin_unsigned_int
;
490 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_long
]
491 = builtin
->builtin_unsigned_long
;
492 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_long_long
]
493 = builtin
->builtin_unsigned_long_long
;
494 lai
->primitive_type_vector
[cplus_primitive_type_long_double
]
495 = builtin
->builtin_long_double
;
496 lai
->primitive_type_vector
[cplus_primitive_type_complex
]
497 = builtin
->builtin_complex
;
498 lai
->primitive_type_vector
[cplus_primitive_type_double_complex
]
499 = builtin
->builtin_double_complex
;
500 lai
->primitive_type_vector
[cplus_primitive_type_bool
]
501 = builtin
->builtin_bool
;
502 lai
->primitive_type_vector
[cplus_primitive_type_decfloat
]
503 = builtin
->builtin_decfloat
;
504 lai
->primitive_type_vector
[cplus_primitive_type_decdouble
]
505 = builtin
->builtin_decdouble
;
506 lai
->primitive_type_vector
[cplus_primitive_type_declong
]
507 = builtin
->builtin_declong
;
510 const struct language_defn cplus_language_defn
=
512 "c++", /* Language name */
518 &exp_descriptor_standard
,
519 c_preprocess_and_parse
,
522 c_printchar
, /* Print a character constant */
523 c_printstr
, /* Function to print string constant */
524 c_emit_char
, /* Print a single char */
525 c_print_type
, /* Print a type using appropriate syntax */
526 c_val_print
, /* Print a value using appropriate syntax */
527 c_value_print
, /* Print a top-level value */
528 cplus_skip_trampoline
, /* Language specific skip_trampoline */
529 value_of_this
, /* value_of_this */
530 cp_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
531 cp_lookup_transparent_type
, /* lookup_transparent_type */
532 cplus_demangle
, /* Language specific symbol demangler */
533 cp_class_name_from_physname
, /* Language specific class_name_from_physname */
534 c_op_print_tab
, /* expression operators for printing */
535 1, /* c-style arrays */
536 0, /* String lower bound */
537 default_word_break_characters
,
538 cplus_language_arch_info
,
539 default_print_array_index
,
540 cp_pass_by_reference
,
544 const struct language_defn asm_language_defn
=
546 "asm", /* Language name */
552 &exp_descriptor_standard
,
553 c_preprocess_and_parse
,
556 c_printchar
, /* Print a character constant */
557 c_printstr
, /* Function to print string constant */
558 c_emit_char
, /* Print a single char */
559 c_print_type
, /* Print a type using appropriate syntax */
560 c_val_print
, /* Print a value using appropriate syntax */
561 c_value_print
, /* Print a top-level value */
562 NULL
, /* Language specific skip_trampoline */
563 NULL
, /* value_of_this */
564 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
565 basic_lookup_transparent_type
,/* lookup_transparent_type */
566 NULL
, /* Language specific symbol demangler */
567 NULL
, /* Language specific class_name_from_physname */
568 c_op_print_tab
, /* expression operators for printing */
569 1, /* c-style arrays */
570 0, /* String lower bound */
571 default_word_break_characters
,
572 c_language_arch_info
, /* FIXME: la_language_arch_info. */
573 default_print_array_index
,
574 default_pass_by_reference
,
578 /* The following language_defn does not represent a real language.
579 It just provides a minimal support a-la-C that should allow users
580 to do some simple operations when debugging applications that use
581 a language currently not supported by GDB. */
583 const struct language_defn minimal_language_defn
=
585 "minimal", /* Language name */
591 &exp_descriptor_standard
,
592 c_preprocess_and_parse
,
595 c_printchar
, /* Print a character constant */
596 c_printstr
, /* Function to print string constant */
597 c_emit_char
, /* Print a single char */
598 c_print_type
, /* Print a type using appropriate syntax */
599 c_val_print
, /* Print a value using appropriate syntax */
600 c_value_print
, /* Print a top-level value */
601 NULL
, /* Language specific skip_trampoline */
602 NULL
, /* value_of_this */
603 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
604 basic_lookup_transparent_type
,/* lookup_transparent_type */
605 NULL
, /* Language specific symbol demangler */
606 NULL
, /* Language specific class_name_from_physname */
607 c_op_print_tab
, /* expression operators for printing */
608 1, /* c-style arrays */
609 0, /* String lower bound */
610 default_word_break_characters
,
611 c_language_arch_info
,
612 default_print_array_index
,
613 default_pass_by_reference
,
618 _initialize_c_language (void)
620 add_language (&c_language_defn
);
621 add_language (&cplus_language_defn
);
622 add_language (&asm_language_defn
);
623 add_language (&minimal_language_defn
);