]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/c-lang.c
gdb
[thirdparty/binutils-gdb.git] / gdb / c-lang.c
1 /* C language support routines for GDB, the GNU debugger.
2
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2002, 2003,
4 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "parser-defs.h"
26 #include "language.h"
27 #include "c-lang.h"
28 #include "valprint.h"
29 #include "macroscope.h"
30 #include "gdb_assert.h"
31 #include "charset.h"
32 #include "gdb_string.h"
33 #include "demangle.h"
34 #include "cp-abi.h"
35 #include "cp-support.h"
36
37 extern void _initialize_c_language (void);
38 static void c_emit_char (int c, struct ui_file * stream, int quoter);
39
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. */
43
44 static void
45 c_emit_char (int c, struct ui_file *stream, int quoter)
46 {
47 const char *escape;
48 int host_char;
49
50 c &= 0xFF; /* Avoid sign bit follies */
51
52 escape = c_target_char_has_backslash_escape (c);
53 if (escape)
54 {
55 if (quoter == '"' && strcmp (escape, "0") == 0)
56 /* Print nulls embedded in double quoted strings as \000 to
57 prevent ambiguity. */
58 fprintf_filtered (stream, "\\000");
59 else
60 fprintf_filtered (stream, "\\%s", escape);
61 }
62 else if (target_char_to_host (c, &host_char)
63 && host_char_print_literally (host_char))
64 {
65 if (host_char == '\\' || host_char == quoter)
66 fputs_filtered ("\\", stream);
67 fprintf_filtered (stream, "%c", host_char);
68 }
69 else
70 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
71 }
72
73 void
74 c_printchar (int c, struct ui_file *stream)
75 {
76 fputc_filtered ('\'', stream);
77 LA_EMIT_CHAR (c, stream, '\'');
78 fputc_filtered ('\'', stream);
79 }
80
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. */
86
87 void
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)
91 {
92 unsigned int i;
93 unsigned int things_printed = 0;
94 int in_quotes = 0;
95 int need_comma = 0;
96
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
99 style. */
100 if (!force_ellipses
101 && length > 0
102 && (extract_unsigned_integer (string + (length - 1) * width, width)
103 == '\0'))
104 length--;
105
106 if (length == 0)
107 {
108 fputs_filtered ("\"\"", stream);
109 return;
110 }
111
112 for (i = 0; i < length && things_printed < options->print_max; ++i)
113 {
114 /* Position of the character we are examining
115 to see whether it is repeated. */
116 unsigned int rep1;
117 /* Number of repetitions we have detected so far. */
118 unsigned int reps;
119 unsigned long current_char;
120
121 QUIT;
122
123 if (need_comma)
124 {
125 fputs_filtered (", ", stream);
126 need_comma = 0;
127 }
128
129 current_char = extract_unsigned_integer (string + i * width, width);
130
131 rep1 = i + 1;
132 reps = 1;
133 while (rep1 < length
134 && extract_unsigned_integer (string + rep1 * width, width)
135 == current_char)
136 {
137 ++rep1;
138 ++reps;
139 }
140
141 if (reps > options->repeat_count_threshold)
142 {
143 if (in_quotes)
144 {
145 if (options->inspect_it)
146 fputs_filtered ("\\\", ", stream);
147 else
148 fputs_filtered ("\", ", stream);
149 in_quotes = 0;
150 }
151 LA_PRINT_CHAR (current_char, stream);
152 fprintf_filtered (stream, _(" <repeats %u times>"), reps);
153 i = rep1 - 1;
154 things_printed += options->repeat_count_threshold;
155 need_comma = 1;
156 }
157 else
158 {
159 if (!in_quotes)
160 {
161 if (options->inspect_it)
162 fputs_filtered ("\\\"", stream);
163 else
164 fputs_filtered ("\"", stream);
165 in_quotes = 1;
166 }
167 LA_EMIT_CHAR (current_char, stream, '"');
168 ++things_printed;
169 }
170 }
171
172 /* Terminate the quotes if necessary. */
173 if (in_quotes)
174 {
175 if (options->inspect_it)
176 fputs_filtered ("\\\"", stream);
177 else
178 fputs_filtered ("\"", stream);
179 }
180
181 if (force_ellipses || i < length)
182 fputs_filtered ("...", stream);
183 }
184 \f
185 /* Preprocessing and parsing C and C++ expressions. */
186
187
188 /* When we find that lexptr (the global var defined in parse.c) is
189 pointing at a macro invocation, we expand the invocation, and call
190 scan_macro_expansion to save the old lexptr here and point lexptr
191 into the expanded text. When we reach the end of that, we call
192 end_macro_expansion to pop back to the value we saved here. The
193 macro expansion code promises to return only fully-expanded text,
194 so we don't need to "push" more than one level.
195
196 This is disgusting, of course. It would be cleaner to do all macro
197 expansion beforehand, and then hand that to lexptr. But we don't
198 really know where the expression ends. Remember, in a command like
199
200 (gdb) break *ADDRESS if CONDITION
201
202 we evaluate ADDRESS in the scope of the current frame, but we
203 evaluate CONDITION in the scope of the breakpoint's location. So
204 it's simply wrong to try to macro-expand the whole thing at once. */
205 static char *macro_original_text;
206 static char *macro_expanded_text;
207
208
209 void
210 scan_macro_expansion (char *expansion)
211 {
212 /* We'd better not be trying to push the stack twice. */
213 gdb_assert (! macro_original_text);
214 gdb_assert (! macro_expanded_text);
215
216 /* Save the old lexptr value, so we can return to it when we're done
217 parsing the expanded text. */
218 macro_original_text = lexptr;
219 lexptr = expansion;
220
221 /* Save the expanded text, so we can free it when we're finished. */
222 macro_expanded_text = expansion;
223 }
224
225
226 int
227 scanning_macro_expansion (void)
228 {
229 return macro_original_text != 0;
230 }
231
232
233 void
234 finished_macro_expansion (void)
235 {
236 /* There'd better be something to pop back to, and we better have
237 saved a pointer to the start of the expanded text. */
238 gdb_assert (macro_original_text);
239 gdb_assert (macro_expanded_text);
240
241 /* Pop back to the original text. */
242 lexptr = macro_original_text;
243 macro_original_text = 0;
244
245 /* Free the expanded text. */
246 xfree (macro_expanded_text);
247 macro_expanded_text = 0;
248 }
249
250
251 static void
252 scan_macro_cleanup (void *dummy)
253 {
254 if (macro_original_text)
255 finished_macro_expansion ();
256 }
257
258
259 /* We set these global variables before calling c_parse, to tell it
260 how it to find macro definitions for the expression at hand. */
261 macro_lookup_ftype *expression_macro_lookup_func;
262 void *expression_macro_lookup_baton;
263
264
265 static int
266 c_preprocess_and_parse (void)
267 {
268 /* Set up a lookup function for the macro expander. */
269 struct macro_scope *scope = 0;
270 struct cleanup *back_to = make_cleanup (free_current_contents, &scope);
271
272 if (expression_context_block)
273 scope = sal_macro_scope (find_pc_line (expression_context_pc, 0));
274 else
275 scope = default_macro_scope ();
276 if (! scope)
277 scope = user_macro_scope ();
278
279 expression_macro_lookup_func = standard_macro_lookup;
280 expression_macro_lookup_baton = (void *) scope;
281
282 gdb_assert (! macro_original_text);
283 make_cleanup (scan_macro_cleanup, 0);
284
285 {
286 int result = c_parse ();
287 do_cleanups (back_to);
288 return result;
289 }
290 }
291
292
293 \f
294 /* Table mapping opcodes into strings for printing operators
295 and precedences of the operators. */
296
297 const struct op_print c_op_print_tab[] =
298 {
299 {",", BINOP_COMMA, PREC_COMMA, 0},
300 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
301 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
302 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
303 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
304 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
305 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
306 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
307 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
308 {"<=", BINOP_LEQ, PREC_ORDER, 0},
309 {">=", BINOP_GEQ, PREC_ORDER, 0},
310 {">", BINOP_GTR, PREC_ORDER, 0},
311 {"<", BINOP_LESS, PREC_ORDER, 0},
312 {">>", BINOP_RSH, PREC_SHIFT, 0},
313 {"<<", BINOP_LSH, PREC_SHIFT, 0},
314 {"+", BINOP_ADD, PREC_ADD, 0},
315 {"-", BINOP_SUB, PREC_ADD, 0},
316 {"*", BINOP_MUL, PREC_MUL, 0},
317 {"/", BINOP_DIV, PREC_MUL, 0},
318 {"%", BINOP_REM, PREC_MUL, 0},
319 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
320 {"-", UNOP_NEG, PREC_PREFIX, 0},
321 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
322 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
323 {"*", UNOP_IND, PREC_PREFIX, 0},
324 {"&", UNOP_ADDR, PREC_PREFIX, 0},
325 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
326 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
327 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
328 {NULL, 0, 0, 0}
329 };
330 \f
331 enum c_primitive_types {
332 c_primitive_type_int,
333 c_primitive_type_long,
334 c_primitive_type_short,
335 c_primitive_type_char,
336 c_primitive_type_float,
337 c_primitive_type_double,
338 c_primitive_type_void,
339 c_primitive_type_long_long,
340 c_primitive_type_signed_char,
341 c_primitive_type_unsigned_char,
342 c_primitive_type_unsigned_short,
343 c_primitive_type_unsigned_int,
344 c_primitive_type_unsigned_long,
345 c_primitive_type_unsigned_long_long,
346 c_primitive_type_long_double,
347 c_primitive_type_complex,
348 c_primitive_type_double_complex,
349 c_primitive_type_decfloat,
350 c_primitive_type_decdouble,
351 c_primitive_type_declong,
352 nr_c_primitive_types
353 };
354
355 void
356 c_language_arch_info (struct gdbarch *gdbarch,
357 struct language_arch_info *lai)
358 {
359 const struct builtin_type *builtin = builtin_type (gdbarch);
360 lai->string_char_type = builtin->builtin_char;
361 lai->primitive_type_vector
362 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_c_primitive_types + 1,
363 struct type *);
364 lai->primitive_type_vector [c_primitive_type_int] = builtin->builtin_int;
365 lai->primitive_type_vector [c_primitive_type_long] = builtin->builtin_long;
366 lai->primitive_type_vector [c_primitive_type_short] = builtin->builtin_short;
367 lai->primitive_type_vector [c_primitive_type_char] = builtin->builtin_char;
368 lai->primitive_type_vector [c_primitive_type_float] = builtin->builtin_float;
369 lai->primitive_type_vector [c_primitive_type_double] = builtin->builtin_double;
370 lai->primitive_type_vector [c_primitive_type_void] = builtin->builtin_void;
371 lai->primitive_type_vector [c_primitive_type_long_long] = builtin->builtin_long_long;
372 lai->primitive_type_vector [c_primitive_type_signed_char] = builtin->builtin_signed_char;
373 lai->primitive_type_vector [c_primitive_type_unsigned_char] = builtin->builtin_unsigned_char;
374 lai->primitive_type_vector [c_primitive_type_unsigned_short] = builtin->builtin_unsigned_short;
375 lai->primitive_type_vector [c_primitive_type_unsigned_int] = builtin->builtin_unsigned_int;
376 lai->primitive_type_vector [c_primitive_type_unsigned_long] = builtin->builtin_unsigned_long;
377 lai->primitive_type_vector [c_primitive_type_unsigned_long_long] = builtin->builtin_unsigned_long_long;
378 lai->primitive_type_vector [c_primitive_type_long_double] = builtin->builtin_long_double;
379 lai->primitive_type_vector [c_primitive_type_complex] = builtin->builtin_complex;
380 lai->primitive_type_vector [c_primitive_type_double_complex] = builtin->builtin_double_complex;
381 lai->primitive_type_vector [c_primitive_type_decfloat] = builtin->builtin_decfloat;
382 lai->primitive_type_vector [c_primitive_type_decdouble] = builtin->builtin_decdouble;
383 lai->primitive_type_vector [c_primitive_type_declong] = builtin->builtin_declong;
384
385 lai->bool_type_default = builtin->builtin_int;
386 }
387
388 const struct language_defn c_language_defn =
389 {
390 "c", /* Language name */
391 language_c,
392 range_check_off,
393 type_check_off,
394 case_sensitive_on,
395 array_row_major,
396 macro_expansion_c,
397 &exp_descriptor_standard,
398 c_preprocess_and_parse,
399 c_error,
400 null_post_parser,
401 c_printchar, /* Print a character constant */
402 c_printstr, /* Function to print string constant */
403 c_emit_char, /* Print a single char */
404 c_print_type, /* Print a type using appropriate syntax */
405 c_print_typedef, /* Print a typedef using appropriate syntax */
406 c_val_print, /* Print a value using appropriate syntax */
407 c_value_print, /* Print a top-level value */
408 NULL, /* Language specific skip_trampoline */
409 NULL, /* name_of_this */
410 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
411 basic_lookup_transparent_type,/* lookup_transparent_type */
412 NULL, /* Language specific symbol demangler */
413 NULL, /* Language specific class_name_from_physname */
414 c_op_print_tab, /* expression operators for printing */
415 1, /* c-style arrays */
416 0, /* String lower bound */
417 default_word_break_characters,
418 default_make_symbol_completion_list,
419 c_language_arch_info,
420 default_print_array_index,
421 default_pass_by_reference,
422 LANG_MAGIC
423 };
424
425 enum cplus_primitive_types {
426 cplus_primitive_type_int,
427 cplus_primitive_type_long,
428 cplus_primitive_type_short,
429 cplus_primitive_type_char,
430 cplus_primitive_type_float,
431 cplus_primitive_type_double,
432 cplus_primitive_type_void,
433 cplus_primitive_type_long_long,
434 cplus_primitive_type_signed_char,
435 cplus_primitive_type_unsigned_char,
436 cplus_primitive_type_unsigned_short,
437 cplus_primitive_type_unsigned_int,
438 cplus_primitive_type_unsigned_long,
439 cplus_primitive_type_unsigned_long_long,
440 cplus_primitive_type_long_double,
441 cplus_primitive_type_complex,
442 cplus_primitive_type_double_complex,
443 cplus_primitive_type_bool,
444 cplus_primitive_type_decfloat,
445 cplus_primitive_type_decdouble,
446 cplus_primitive_type_declong,
447 nr_cplus_primitive_types
448 };
449
450 static void
451 cplus_language_arch_info (struct gdbarch *gdbarch,
452 struct language_arch_info *lai)
453 {
454 const struct builtin_type *builtin = builtin_type (gdbarch);
455 lai->string_char_type = builtin->builtin_char;
456 lai->primitive_type_vector
457 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_cplus_primitive_types + 1,
458 struct type *);
459 lai->primitive_type_vector [cplus_primitive_type_int]
460 = builtin->builtin_int;
461 lai->primitive_type_vector [cplus_primitive_type_long]
462 = builtin->builtin_long;
463 lai->primitive_type_vector [cplus_primitive_type_short]
464 = builtin->builtin_short;
465 lai->primitive_type_vector [cplus_primitive_type_char]
466 = builtin->builtin_char;
467 lai->primitive_type_vector [cplus_primitive_type_float]
468 = builtin->builtin_float;
469 lai->primitive_type_vector [cplus_primitive_type_double]
470 = builtin->builtin_double;
471 lai->primitive_type_vector [cplus_primitive_type_void]
472 = builtin->builtin_void;
473 lai->primitive_type_vector [cplus_primitive_type_long_long]
474 = builtin->builtin_long_long;
475 lai->primitive_type_vector [cplus_primitive_type_signed_char]
476 = builtin->builtin_signed_char;
477 lai->primitive_type_vector [cplus_primitive_type_unsigned_char]
478 = builtin->builtin_unsigned_char;
479 lai->primitive_type_vector [cplus_primitive_type_unsigned_short]
480 = builtin->builtin_unsigned_short;
481 lai->primitive_type_vector [cplus_primitive_type_unsigned_int]
482 = builtin->builtin_unsigned_int;
483 lai->primitive_type_vector [cplus_primitive_type_unsigned_long]
484 = builtin->builtin_unsigned_long;
485 lai->primitive_type_vector [cplus_primitive_type_unsigned_long_long]
486 = builtin->builtin_unsigned_long_long;
487 lai->primitive_type_vector [cplus_primitive_type_long_double]
488 = builtin->builtin_long_double;
489 lai->primitive_type_vector [cplus_primitive_type_complex]
490 = builtin->builtin_complex;
491 lai->primitive_type_vector [cplus_primitive_type_double_complex]
492 = builtin->builtin_double_complex;
493 lai->primitive_type_vector [cplus_primitive_type_bool]
494 = builtin->builtin_bool;
495 lai->primitive_type_vector [cplus_primitive_type_decfloat]
496 = builtin->builtin_decfloat;
497 lai->primitive_type_vector [cplus_primitive_type_decdouble]
498 = builtin->builtin_decdouble;
499 lai->primitive_type_vector [cplus_primitive_type_declong]
500 = builtin->builtin_declong;
501
502 lai->bool_type_symbol = "bool";
503 lai->bool_type_default = builtin->builtin_bool;
504 }
505
506 const struct language_defn cplus_language_defn =
507 {
508 "c++", /* Language name */
509 language_cplus,
510 range_check_off,
511 type_check_off,
512 case_sensitive_on,
513 array_row_major,
514 macro_expansion_c,
515 &exp_descriptor_standard,
516 c_preprocess_and_parse,
517 c_error,
518 null_post_parser,
519 c_printchar, /* Print a character constant */
520 c_printstr, /* Function to print string constant */
521 c_emit_char, /* Print a single char */
522 c_print_type, /* Print a type using appropriate syntax */
523 c_print_typedef, /* Print a typedef using appropriate syntax */
524 c_val_print, /* Print a value using appropriate syntax */
525 c_value_print, /* Print a top-level value */
526 cplus_skip_trampoline, /* Language specific skip_trampoline */
527 "this", /* name_of_this */
528 cp_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
529 cp_lookup_transparent_type, /* lookup_transparent_type */
530 cplus_demangle, /* Language specific symbol demangler */
531 cp_class_name_from_physname, /* Language specific class_name_from_physname */
532 c_op_print_tab, /* expression operators for printing */
533 1, /* c-style arrays */
534 0, /* String lower bound */
535 default_word_break_characters,
536 default_make_symbol_completion_list,
537 cplus_language_arch_info,
538 default_print_array_index,
539 cp_pass_by_reference,
540 LANG_MAGIC
541 };
542
543 const struct language_defn asm_language_defn =
544 {
545 "asm", /* Language name */
546 language_asm,
547 range_check_off,
548 type_check_off,
549 case_sensitive_on,
550 array_row_major,
551 macro_expansion_c,
552 &exp_descriptor_standard,
553 c_preprocess_and_parse,
554 c_error,
555 null_post_parser,
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_print_typedef, /* Print a typedef using appropriate syntax */
561 c_val_print, /* Print a value using appropriate syntax */
562 c_value_print, /* Print a top-level value */
563 NULL, /* Language specific skip_trampoline */
564 NULL, /* name_of_this */
565 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
566 basic_lookup_transparent_type,/* lookup_transparent_type */
567 NULL, /* Language specific symbol demangler */
568 NULL, /* Language specific class_name_from_physname */
569 c_op_print_tab, /* expression operators for printing */
570 1, /* c-style arrays */
571 0, /* String lower bound */
572 default_word_break_characters,
573 default_make_symbol_completion_list,
574 c_language_arch_info, /* FIXME: la_language_arch_info. */
575 default_print_array_index,
576 default_pass_by_reference,
577 LANG_MAGIC
578 };
579
580 /* The following language_defn does not represent a real language.
581 It just provides a minimal support a-la-C that should allow users
582 to do some simple operations when debugging applications that use
583 a language currently not supported by GDB. */
584
585 const struct language_defn minimal_language_defn =
586 {
587 "minimal", /* Language name */
588 language_minimal,
589 range_check_off,
590 type_check_off,
591 case_sensitive_on,
592 array_row_major,
593 macro_expansion_c,
594 &exp_descriptor_standard,
595 c_preprocess_and_parse,
596 c_error,
597 null_post_parser,
598 c_printchar, /* Print a character constant */
599 c_printstr, /* Function to print string constant */
600 c_emit_char, /* Print a single char */
601 c_print_type, /* Print a type using appropriate syntax */
602 c_print_typedef, /* Print a typedef using appropriate syntax */
603 c_val_print, /* Print a value using appropriate syntax */
604 c_value_print, /* Print a top-level value */
605 NULL, /* Language specific skip_trampoline */
606 NULL, /* name_of_this */
607 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
608 basic_lookup_transparent_type,/* lookup_transparent_type */
609 NULL, /* Language specific symbol demangler */
610 NULL, /* Language specific class_name_from_physname */
611 c_op_print_tab, /* expression operators for printing */
612 1, /* c-style arrays */
613 0, /* String lower bound */
614 default_word_break_characters,
615 default_make_symbol_completion_list,
616 c_language_arch_info,
617 default_print_array_index,
618 default_pass_by_reference,
619 LANG_MAGIC
620 };
621
622 void
623 _initialize_c_language (void)
624 {
625 add_language (&c_language_defn);
626 add_language (&cplus_language_defn);
627 add_language (&asm_language_defn);
628 add_language (&minimal_language_defn);
629 }