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
,
90 const struct value_print_options
*options
)
93 unsigned int things_printed
= 0;
97 /* If the string was not truncated due to `set print elements', and
98 the last byte of it is a null, we don't print that, in traditional C
102 && (extract_unsigned_integer (string
+ (length
- 1) * width
, width
)
108 fputs_filtered ("\"\"", stream
);
112 for (i
= 0; i
< length
&& things_printed
< options
->print_max
; ++i
)
114 /* Position of the character we are examining
115 to see whether it is repeated. */
117 /* Number of repetitions we have detected so far. */
119 unsigned long current_char
;
125 fputs_filtered (", ", stream
);
129 current_char
= extract_unsigned_integer (string
+ i
* width
, width
);
134 && extract_unsigned_integer (string
+ rep1
* width
, width
)
141 if (reps
> options
->repeat_count_threshold
)
145 if (options
->inspect_it
)
146 fputs_filtered ("\\\", ", stream
);
148 fputs_filtered ("\", ", stream
);
151 LA_PRINT_CHAR (current_char
, stream
);
152 fprintf_filtered (stream
, _(" <repeats %u times>"), reps
);
154 things_printed
+= options
->repeat_count_threshold
;
161 if (options
->inspect_it
)
162 fputs_filtered ("\\\"", stream
);
164 fputs_filtered ("\"", stream
);
167 LA_EMIT_CHAR (current_char
, stream
, '"');
172 /* Terminate the quotes if necessary. */
175 if (options
->inspect_it
)
176 fputs_filtered ("\\\"", stream
);
178 fputs_filtered ("\"", stream
);
181 if (force_ellipses
|| i
< length
)
182 fputs_filtered ("...", stream
);
185 /* Preprocessing and parsing C and C++ expressions. */
189 /* Table mapping opcodes into strings for printing operators
190 and precedences of the operators. */
192 const struct op_print c_op_print_tab
[] =
194 {",", BINOP_COMMA
, PREC_COMMA
, 0},
195 {"=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
196 {"||", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
197 {"&&", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
198 {"|", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
199 {"^", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
200 {"&", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
201 {"==", BINOP_EQUAL
, PREC_EQUAL
, 0},
202 {"!=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
203 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
204 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
205 {">", BINOP_GTR
, PREC_ORDER
, 0},
206 {"<", BINOP_LESS
, PREC_ORDER
, 0},
207 {">>", BINOP_RSH
, PREC_SHIFT
, 0},
208 {"<<", BINOP_LSH
, PREC_SHIFT
, 0},
209 {"+", BINOP_ADD
, PREC_ADD
, 0},
210 {"-", BINOP_SUB
, PREC_ADD
, 0},
211 {"*", BINOP_MUL
, PREC_MUL
, 0},
212 {"/", BINOP_DIV
, PREC_MUL
, 0},
213 {"%", BINOP_REM
, PREC_MUL
, 0},
214 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
215 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
216 {"!", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
217 {"~", UNOP_COMPLEMENT
, PREC_PREFIX
, 0},
218 {"*", UNOP_IND
, PREC_PREFIX
, 0},
219 {"&", UNOP_ADDR
, PREC_PREFIX
, 0},
220 {"sizeof ", UNOP_SIZEOF
, PREC_PREFIX
, 0},
221 {"++", UNOP_PREINCREMENT
, PREC_PREFIX
, 0},
222 {"--", UNOP_PREDECREMENT
, PREC_PREFIX
, 0},
226 enum c_primitive_types
{
227 c_primitive_type_int
,
228 c_primitive_type_long
,
229 c_primitive_type_short
,
230 c_primitive_type_char
,
231 c_primitive_type_float
,
232 c_primitive_type_double
,
233 c_primitive_type_void
,
234 c_primitive_type_long_long
,
235 c_primitive_type_signed_char
,
236 c_primitive_type_unsigned_char
,
237 c_primitive_type_unsigned_short
,
238 c_primitive_type_unsigned_int
,
239 c_primitive_type_unsigned_long
,
240 c_primitive_type_unsigned_long_long
,
241 c_primitive_type_long_double
,
242 c_primitive_type_complex
,
243 c_primitive_type_double_complex
,
244 c_primitive_type_decfloat
,
245 c_primitive_type_decdouble
,
246 c_primitive_type_declong
,
251 c_language_arch_info (struct gdbarch
*gdbarch
,
252 struct language_arch_info
*lai
)
254 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
255 lai
->string_char_type
= builtin
->builtin_char
;
256 lai
->primitive_type_vector
257 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_c_primitive_types
+ 1,
259 lai
->primitive_type_vector
[c_primitive_type_int
] = builtin
->builtin_int
;
260 lai
->primitive_type_vector
[c_primitive_type_long
] = builtin
->builtin_long
;
261 lai
->primitive_type_vector
[c_primitive_type_short
] = builtin
->builtin_short
;
262 lai
->primitive_type_vector
[c_primitive_type_char
] = builtin
->builtin_char
;
263 lai
->primitive_type_vector
[c_primitive_type_float
] = builtin
->builtin_float
;
264 lai
->primitive_type_vector
[c_primitive_type_double
] = builtin
->builtin_double
;
265 lai
->primitive_type_vector
[c_primitive_type_void
] = builtin
->builtin_void
;
266 lai
->primitive_type_vector
[c_primitive_type_long_long
] = builtin
->builtin_long_long
;
267 lai
->primitive_type_vector
[c_primitive_type_signed_char
] = builtin
->builtin_signed_char
;
268 lai
->primitive_type_vector
[c_primitive_type_unsigned_char
] = builtin
->builtin_unsigned_char
;
269 lai
->primitive_type_vector
[c_primitive_type_unsigned_short
] = builtin
->builtin_unsigned_short
;
270 lai
->primitive_type_vector
[c_primitive_type_unsigned_int
] = builtin
->builtin_unsigned_int
;
271 lai
->primitive_type_vector
[c_primitive_type_unsigned_long
] = builtin
->builtin_unsigned_long
;
272 lai
->primitive_type_vector
[c_primitive_type_unsigned_long_long
] = builtin
->builtin_unsigned_long_long
;
273 lai
->primitive_type_vector
[c_primitive_type_long_double
] = builtin
->builtin_long_double
;
274 lai
->primitive_type_vector
[c_primitive_type_complex
] = builtin
->builtin_complex
;
275 lai
->primitive_type_vector
[c_primitive_type_double_complex
] = builtin
->builtin_double_complex
;
276 lai
->primitive_type_vector
[c_primitive_type_decfloat
] = builtin
->builtin_decfloat
;
277 lai
->primitive_type_vector
[c_primitive_type_decdouble
] = builtin
->builtin_decdouble
;
278 lai
->primitive_type_vector
[c_primitive_type_declong
] = builtin
->builtin_declong
;
280 lai
->bool_type_default
= builtin
->builtin_int
;
283 const struct language_defn c_language_defn
=
285 "c", /* Language name */
292 &exp_descriptor_standard
,
296 c_printchar
, /* Print a character constant */
297 c_printstr
, /* Function to print string constant */
298 c_emit_char
, /* Print a single char */
299 c_print_type
, /* Print a type using appropriate syntax */
300 c_print_typedef
, /* Print a typedef using appropriate syntax */
301 c_val_print
, /* Print a value using appropriate syntax */
302 c_value_print
, /* Print a top-level value */
303 NULL
, /* Language specific skip_trampoline */
304 NULL
, /* name_of_this */
305 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
306 basic_lookup_transparent_type
,/* lookup_transparent_type */
307 NULL
, /* Language specific symbol demangler */
308 NULL
, /* Language specific class_name_from_physname */
309 c_op_print_tab
, /* expression operators for printing */
310 1, /* c-style arrays */
311 0, /* String lower bound */
312 default_word_break_characters
,
313 default_make_symbol_completion_list
,
314 c_language_arch_info
,
315 default_print_array_index
,
316 default_pass_by_reference
,
320 enum cplus_primitive_types
{
321 cplus_primitive_type_int
,
322 cplus_primitive_type_long
,
323 cplus_primitive_type_short
,
324 cplus_primitive_type_char
,
325 cplus_primitive_type_float
,
326 cplus_primitive_type_double
,
327 cplus_primitive_type_void
,
328 cplus_primitive_type_long_long
,
329 cplus_primitive_type_signed_char
,
330 cplus_primitive_type_unsigned_char
,
331 cplus_primitive_type_unsigned_short
,
332 cplus_primitive_type_unsigned_int
,
333 cplus_primitive_type_unsigned_long
,
334 cplus_primitive_type_unsigned_long_long
,
335 cplus_primitive_type_long_double
,
336 cplus_primitive_type_complex
,
337 cplus_primitive_type_double_complex
,
338 cplus_primitive_type_bool
,
339 cplus_primitive_type_decfloat
,
340 cplus_primitive_type_decdouble
,
341 cplus_primitive_type_declong
,
342 nr_cplus_primitive_types
346 cplus_language_arch_info (struct gdbarch
*gdbarch
,
347 struct language_arch_info
*lai
)
349 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
350 lai
->string_char_type
= builtin
->builtin_char
;
351 lai
->primitive_type_vector
352 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_cplus_primitive_types
+ 1,
354 lai
->primitive_type_vector
[cplus_primitive_type_int
]
355 = builtin
->builtin_int
;
356 lai
->primitive_type_vector
[cplus_primitive_type_long
]
357 = builtin
->builtin_long
;
358 lai
->primitive_type_vector
[cplus_primitive_type_short
]
359 = builtin
->builtin_short
;
360 lai
->primitive_type_vector
[cplus_primitive_type_char
]
361 = builtin
->builtin_char
;
362 lai
->primitive_type_vector
[cplus_primitive_type_float
]
363 = builtin
->builtin_float
;
364 lai
->primitive_type_vector
[cplus_primitive_type_double
]
365 = builtin
->builtin_double
;
366 lai
->primitive_type_vector
[cplus_primitive_type_void
]
367 = builtin
->builtin_void
;
368 lai
->primitive_type_vector
[cplus_primitive_type_long_long
]
369 = builtin
->builtin_long_long
;
370 lai
->primitive_type_vector
[cplus_primitive_type_signed_char
]
371 = builtin
->builtin_signed_char
;
372 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_char
]
373 = builtin
->builtin_unsigned_char
;
374 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_short
]
375 = builtin
->builtin_unsigned_short
;
376 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_int
]
377 = builtin
->builtin_unsigned_int
;
378 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_long
]
379 = builtin
->builtin_unsigned_long
;
380 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_long_long
]
381 = builtin
->builtin_unsigned_long_long
;
382 lai
->primitive_type_vector
[cplus_primitive_type_long_double
]
383 = builtin
->builtin_long_double
;
384 lai
->primitive_type_vector
[cplus_primitive_type_complex
]
385 = builtin
->builtin_complex
;
386 lai
->primitive_type_vector
[cplus_primitive_type_double_complex
]
387 = builtin
->builtin_double_complex
;
388 lai
->primitive_type_vector
[cplus_primitive_type_bool
]
389 = builtin
->builtin_bool
;
390 lai
->primitive_type_vector
[cplus_primitive_type_decfloat
]
391 = builtin
->builtin_decfloat
;
392 lai
->primitive_type_vector
[cplus_primitive_type_decdouble
]
393 = builtin
->builtin_decdouble
;
394 lai
->primitive_type_vector
[cplus_primitive_type_declong
]
395 = builtin
->builtin_declong
;
397 lai
->bool_type_symbol
= "bool";
398 lai
->bool_type_default
= builtin
->builtin_bool
;
401 const struct language_defn cplus_language_defn
=
403 "c++", /* Language name */
410 &exp_descriptor_standard
,
414 c_printchar
, /* Print a character constant */
415 c_printstr
, /* Function to print string constant */
416 c_emit_char
, /* Print a single char */
417 c_print_type
, /* Print a type using appropriate syntax */
418 c_print_typedef
, /* Print a typedef using appropriate syntax */
419 c_val_print
, /* Print a value using appropriate syntax */
420 c_value_print
, /* Print a top-level value */
421 cplus_skip_trampoline
, /* Language specific skip_trampoline */
422 "this", /* name_of_this */
423 cp_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
424 cp_lookup_transparent_type
, /* lookup_transparent_type */
425 cplus_demangle
, /* Language specific symbol demangler */
426 cp_class_name_from_physname
, /* Language specific class_name_from_physname */
427 c_op_print_tab
, /* expression operators for printing */
428 1, /* c-style arrays */
429 0, /* String lower bound */
430 default_word_break_characters
,
431 default_make_symbol_completion_list
,
432 cplus_language_arch_info
,
433 default_print_array_index
,
434 cp_pass_by_reference
,
438 const struct language_defn asm_language_defn
=
440 "asm", /* Language name */
447 &exp_descriptor_standard
,
451 c_printchar
, /* Print a character constant */
452 c_printstr
, /* Function to print string constant */
453 c_emit_char
, /* Print a single char */
454 c_print_type
, /* Print a type using appropriate syntax */
455 c_print_typedef
, /* Print a typedef using appropriate syntax */
456 c_val_print
, /* Print a value using appropriate syntax */
457 c_value_print
, /* Print a top-level value */
458 NULL
, /* Language specific skip_trampoline */
459 NULL
, /* name_of_this */
460 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
461 basic_lookup_transparent_type
,/* lookup_transparent_type */
462 NULL
, /* Language specific symbol demangler */
463 NULL
, /* Language specific class_name_from_physname */
464 c_op_print_tab
, /* expression operators for printing */
465 1, /* c-style arrays */
466 0, /* String lower bound */
467 default_word_break_characters
,
468 default_make_symbol_completion_list
,
469 c_language_arch_info
, /* FIXME: la_language_arch_info. */
470 default_print_array_index
,
471 default_pass_by_reference
,
475 /* The following language_defn does not represent a real language.
476 It just provides a minimal support a-la-C that should allow users
477 to do some simple operations when debugging applications that use
478 a language currently not supported by GDB. */
480 const struct language_defn minimal_language_defn
=
482 "minimal", /* Language name */
489 &exp_descriptor_standard
,
493 c_printchar
, /* Print a character constant */
494 c_printstr
, /* Function to print string constant */
495 c_emit_char
, /* Print a single char */
496 c_print_type
, /* Print a type using appropriate syntax */
497 c_print_typedef
, /* Print a typedef using appropriate syntax */
498 c_val_print
, /* Print a value using appropriate syntax */
499 c_value_print
, /* Print a top-level value */
500 NULL
, /* Language specific skip_trampoline */
501 NULL
, /* name_of_this */
502 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
503 basic_lookup_transparent_type
,/* lookup_transparent_type */
504 NULL
, /* Language specific symbol demangler */
505 NULL
, /* Language specific class_name_from_physname */
506 c_op_print_tab
, /* expression operators for printing */
507 1, /* c-style arrays */
508 0, /* String lower bound */
509 default_word_break_characters
,
510 default_make_symbol_completion_list
,
511 c_language_arch_info
,
512 default_print_array_index
,
513 default_pass_by_reference
,
518 _initialize_c_language (void)
520 add_language (&c_language_defn
);
521 add_language (&cplus_language_defn
);
522 add_language (&asm_language_defn
);
523 add_language (&minimal_language_defn
);