gdb
[external/binutils.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 \f
189 /* Table mapping opcodes into strings for printing operators
190    and precedences of the operators.  */
191
192 const struct op_print c_op_print_tab[] =
193 {
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},
223   {NULL, 0, 0, 0}
224 };
225 \f
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,
247   nr_c_primitive_types
248 };
249
250 void
251 c_language_arch_info (struct gdbarch *gdbarch,
252                       struct language_arch_info *lai)
253 {
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,
258                               struct type *);
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;
279
280   lai->bool_type_default = builtin->builtin_int;
281 }
282
283 const struct language_defn c_language_defn =
284 {
285   "c",                          /* Language name */
286   language_c,
287   range_check_off,
288   type_check_off,
289   case_sensitive_on,
290   array_row_major,
291   macro_expansion_c,
292   &exp_descriptor_standard,
293   c_parse,
294   c_error,
295   null_post_parser,
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,
317   LANG_MAGIC
318 };
319
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
343 };
344
345 static void
346 cplus_language_arch_info (struct gdbarch *gdbarch,
347                           struct language_arch_info *lai)
348 {
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,
353                               struct type *);
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;
396
397   lai->bool_type_symbol = "bool";
398   lai->bool_type_default = builtin->builtin_bool;
399 }
400
401 const struct language_defn cplus_language_defn =
402 {
403   "c++",                        /* Language name */
404   language_cplus,
405   range_check_off,
406   type_check_off,
407   case_sensitive_on,
408   array_row_major,
409   macro_expansion_c,
410   &exp_descriptor_standard,
411   c_parse,
412   c_error,
413   null_post_parser,
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,
435   LANG_MAGIC
436 };
437
438 const struct language_defn asm_language_defn =
439 {
440   "asm",                        /* Language name */
441   language_asm,
442   range_check_off,
443   type_check_off,
444   case_sensitive_on,
445   array_row_major,
446   macro_expansion_c,
447   &exp_descriptor_standard,
448   c_parse,
449   c_error,
450   null_post_parser,
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,
472   LANG_MAGIC
473 };
474
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.  */
479
480 const struct language_defn minimal_language_defn =
481 {
482   "minimal",                    /* Language name */
483   language_minimal,
484   range_check_off,
485   type_check_off,
486   case_sensitive_on,
487   array_row_major,
488   macro_expansion_c,
489   &exp_descriptor_standard,
490   c_parse,
491   c_error,
492   null_post_parser,
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,
514   LANG_MAGIC
515 };
516
517 void
518 _initialize_c_language (void)
519 {
520   add_language (&c_language_defn);
521   add_language (&cplus_language_defn);
522   add_language (&asm_language_defn);
523   add_language (&minimal_language_defn);
524 }