OSDN Git Service

* coffread.c (decode_type): Use builtin_type_int32 instead
[pf3gnuchains/pf3gnuchains3x.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 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 {
91   unsigned int i;
92   unsigned int things_printed = 0;
93   int in_quotes = 0;
94   int need_comma = 0;
95
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
98      style.  */
99   if (!force_ellipses
100       && length > 0
101       && (extract_unsigned_integer (string + (length - 1) * width, width)
102           == '\0'))
103     length--;
104
105   if (length == 0)
106     {
107       fputs_filtered ("\"\"", stream);
108       return;
109     }
110
111   for (i = 0; i < length && things_printed < print_max; ++i)
112     {
113       /* Position of the character we are examining
114          to see whether it is repeated.  */
115       unsigned int rep1;
116       /* Number of repetitions we have detected so far.  */
117       unsigned int reps;
118       unsigned long current_char;
119
120       QUIT;
121
122       if (need_comma)
123         {
124           fputs_filtered (", ", stream);
125           need_comma = 0;
126         }
127
128       current_char = extract_unsigned_integer (string + i * width, width);
129
130       rep1 = i + 1;
131       reps = 1;
132       while (rep1 < length
133              && extract_unsigned_integer (string + rep1 * width, width)
134              == current_char)
135         {
136           ++rep1;
137           ++reps;
138         }
139
140       if (reps > repeat_count_threshold)
141         {
142           if (in_quotes)
143             {
144               if (inspect_it)
145                 fputs_filtered ("\\\", ", stream);
146               else
147                 fputs_filtered ("\", ", stream);
148               in_quotes = 0;
149             }
150           LA_PRINT_CHAR (current_char, stream);
151           fprintf_filtered (stream, _(" <repeats %u times>"), reps);
152           i = rep1 - 1;
153           things_printed += repeat_count_threshold;
154           need_comma = 1;
155         }
156       else
157         {
158           if (!in_quotes)
159             {
160               if (inspect_it)
161                 fputs_filtered ("\\\"", stream);
162               else
163                 fputs_filtered ("\"", stream);
164               in_quotes = 1;
165             }
166           LA_EMIT_CHAR (current_char, stream, '"');
167           ++things_printed;
168         }
169     }
170
171   /* Terminate the quotes if necessary.  */
172   if (in_quotes)
173     {
174       if (inspect_it)
175         fputs_filtered ("\\\"", stream);
176       else
177         fputs_filtered ("\"", stream);
178     }
179
180   if (force_ellipses || i < length)
181     fputs_filtered ("...", stream);
182 }
183 \f
184 /* Preprocessing and parsing C and C++ expressions.  */
185
186
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.
194
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
198
199      (gdb) break *ADDRESS if CONDITION
200
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;
206
207
208 void
209 scan_macro_expansion (char *expansion)
210 {
211   /* We'd better not be trying to push the stack twice.  */
212   gdb_assert (! macro_original_text);
213   gdb_assert (! macro_expanded_text);
214
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;
218   lexptr = expansion;
219
220   /* Save the expanded text, so we can free it when we're finished.  */
221   macro_expanded_text = expansion;
222 }
223
224
225 int
226 scanning_macro_expansion (void)
227 {
228   return macro_original_text != 0;
229 }
230
231
232 void 
233 finished_macro_expansion (void)
234 {
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);
239
240   /* Pop back to the original text.  */
241   lexptr = macro_original_text;
242   macro_original_text = 0;
243
244   /* Free the expanded text.  */
245   xfree (macro_expanded_text);
246   macro_expanded_text = 0;
247 }
248
249
250 static void
251 scan_macro_cleanup (void *dummy)
252 {
253   if (macro_original_text)
254     finished_macro_expansion ();
255 }
256
257
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;
262
263
264 static struct macro_definition *
265 null_macro_lookup (const char *name, void *baton)
266 {
267   return 0;
268 }
269
270
271 static int
272 c_preprocess_and_parse (void)
273 {
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);
277
278   if (expression_context_block)
279     scope = sal_macro_scope (find_pc_line (expression_context_pc, 0));
280   else
281     scope = default_macro_scope ();
282
283   if (scope)
284     {
285       expression_macro_lookup_func = standard_macro_lookup;
286       expression_macro_lookup_baton = (void *) scope;
287     }
288   else
289     {
290       expression_macro_lookup_func = null_macro_lookup;
291       expression_macro_lookup_baton = 0;      
292     }
293
294   gdb_assert (! macro_original_text);
295   make_cleanup (scan_macro_cleanup, 0);
296
297   {
298     int result = c_parse ();
299     do_cleanups (back_to);
300     return result;
301   }
302 }
303
304
305 \f
306 /* Table mapping opcodes into strings for printing operators
307    and precedences of the operators.  */
308
309 const struct op_print c_op_print_tab[] =
310 {
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},
340   {NULL, 0, 0, 0}
341 };
342 \f
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   nr_c_primitive_types
362 };
363
364 void
365 c_language_arch_info (struct gdbarch *gdbarch,
366                       struct language_arch_info *lai)
367 {
368   const struct builtin_type *builtin = builtin_type (gdbarch);
369   lai->string_char_type = builtin->builtin_char;
370   lai->primitive_type_vector
371     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_c_primitive_types + 1,
372                               struct type *);
373   lai->primitive_type_vector [c_primitive_type_int] = builtin->builtin_int;
374   lai->primitive_type_vector [c_primitive_type_long] = builtin->builtin_long;
375   lai->primitive_type_vector [c_primitive_type_short] = builtin->builtin_short;
376   lai->primitive_type_vector [c_primitive_type_char] = builtin->builtin_char;
377   lai->primitive_type_vector [c_primitive_type_float] = builtin->builtin_float;
378   lai->primitive_type_vector [c_primitive_type_double] = builtin->builtin_double;
379   lai->primitive_type_vector [c_primitive_type_void] = builtin->builtin_void;
380   lai->primitive_type_vector [c_primitive_type_long_long] = builtin->builtin_long_long;
381   lai->primitive_type_vector [c_primitive_type_signed_char] = builtin->builtin_signed_char;
382   lai->primitive_type_vector [c_primitive_type_unsigned_char] = builtin->builtin_unsigned_char;
383   lai->primitive_type_vector [c_primitive_type_unsigned_short] = builtin->builtin_unsigned_short;
384   lai->primitive_type_vector [c_primitive_type_unsigned_int] = builtin->builtin_unsigned_int;
385   lai->primitive_type_vector [c_primitive_type_unsigned_long] = builtin->builtin_unsigned_long;
386   lai->primitive_type_vector [c_primitive_type_unsigned_long_long] = builtin->builtin_unsigned_long_long;
387   lai->primitive_type_vector [c_primitive_type_long_double] = builtin->builtin_long_double;
388   lai->primitive_type_vector [c_primitive_type_complex] = builtin->builtin_complex;
389   lai->primitive_type_vector [c_primitive_type_double_complex] = builtin->builtin_double_complex;
390 }
391
392 const struct language_defn c_language_defn =
393 {
394   "c",                          /* Language name */
395   language_c,
396   range_check_off,
397   type_check_off,
398   case_sensitive_on,
399   array_row_major,
400   &exp_descriptor_standard,
401   c_preprocess_and_parse,
402   c_error,
403   null_post_parser,
404   c_printchar,                  /* Print a character constant */
405   c_printstr,                   /* Function to print string constant */
406   c_emit_char,                  /* Print a single char */
407   c_print_type,                 /* Print a type using appropriate syntax */
408   c_val_print,                  /* Print a value using appropriate syntax */
409   c_value_print,                /* Print a top-level value */
410   NULL,                         /* Language specific skip_trampoline */
411   NULL,                         /* value_of_this */
412   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
413   basic_lookup_transparent_type,/* lookup_transparent_type */
414   NULL,                         /* Language specific symbol demangler */
415   NULL,                         /* Language specific class_name_from_physname */
416   c_op_print_tab,               /* expression operators for printing */
417   1,                            /* c-style arrays */
418   0,                            /* String lower bound */
419   default_word_break_characters,
420   c_language_arch_info,
421   default_print_array_index,
422   default_pass_by_reference,
423   LANG_MAGIC
424 };
425
426 enum cplus_primitive_types {
427   cplus_primitive_type_int,
428   cplus_primitive_type_long,
429   cplus_primitive_type_short,
430   cplus_primitive_type_char,
431   cplus_primitive_type_float,
432   cplus_primitive_type_double,
433   cplus_primitive_type_void,
434   cplus_primitive_type_long_long,
435   cplus_primitive_type_signed_char,
436   cplus_primitive_type_unsigned_char,
437   cplus_primitive_type_unsigned_short,
438   cplus_primitive_type_unsigned_int,
439   cplus_primitive_type_unsigned_long,
440   cplus_primitive_type_unsigned_long_long,
441   cplus_primitive_type_long_double,
442   cplus_primitive_type_complex,
443   cplus_primitive_type_double_complex,
444   cplus_primitive_type_bool,
445   nr_cplus_primitive_types
446 };
447
448 static void
449 cplus_language_arch_info (struct gdbarch *gdbarch,
450                           struct language_arch_info *lai)
451 {
452   const struct builtin_type *builtin = builtin_type (gdbarch);
453   lai->string_char_type = builtin->builtin_char;
454   lai->primitive_type_vector
455     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_cplus_primitive_types + 1,
456                               struct type *);
457   lai->primitive_type_vector [cplus_primitive_type_int]
458     = builtin->builtin_int;
459   lai->primitive_type_vector [cplus_primitive_type_long]
460     = builtin->builtin_long;
461   lai->primitive_type_vector [cplus_primitive_type_short]
462     = builtin->builtin_short;
463   lai->primitive_type_vector [cplus_primitive_type_char]
464     = builtin->builtin_char;
465   lai->primitive_type_vector [cplus_primitive_type_float]
466     = builtin->builtin_float;
467   lai->primitive_type_vector [cplus_primitive_type_double]
468     = builtin->builtin_double;
469   lai->primitive_type_vector [cplus_primitive_type_void]
470     = builtin->builtin_void;
471   lai->primitive_type_vector [cplus_primitive_type_long_long]
472     = builtin->builtin_long_long;
473   lai->primitive_type_vector [cplus_primitive_type_signed_char]
474     = builtin->builtin_signed_char;
475   lai->primitive_type_vector [cplus_primitive_type_unsigned_char]
476     = builtin->builtin_unsigned_char;
477   lai->primitive_type_vector [cplus_primitive_type_unsigned_short]
478     = builtin->builtin_unsigned_short;
479   lai->primitive_type_vector [cplus_primitive_type_unsigned_int]
480     = builtin->builtin_unsigned_int;
481   lai->primitive_type_vector [cplus_primitive_type_unsigned_long]
482     = builtin->builtin_unsigned_long;
483   lai->primitive_type_vector [cplus_primitive_type_unsigned_long_long]
484     = builtin->builtin_unsigned_long_long;
485   lai->primitive_type_vector [cplus_primitive_type_long_double]
486     = builtin->builtin_long_double;
487   lai->primitive_type_vector [cplus_primitive_type_complex]
488     = builtin->builtin_complex;
489   lai->primitive_type_vector [cplus_primitive_type_double_complex]
490     = builtin->builtin_double_complex;
491   lai->primitive_type_vector [cplus_primitive_type_bool]
492     = builtin->builtin_bool;
493 }
494
495 const struct language_defn cplus_language_defn =
496 {
497   "c++",                        /* Language name */
498   language_cplus,
499   range_check_off,
500   type_check_off,
501   case_sensitive_on,
502   array_row_major,
503   &exp_descriptor_standard,
504   c_preprocess_and_parse,
505   c_error,
506   null_post_parser,
507   c_printchar,                  /* Print a character constant */
508   c_printstr,                   /* Function to print string constant */
509   c_emit_char,                  /* Print a single char */
510   c_print_type,                 /* Print a type using appropriate syntax */
511   c_val_print,                  /* Print a value using appropriate syntax */
512   c_value_print,                /* Print a top-level value */
513   cplus_skip_trampoline,        /* Language specific skip_trampoline */
514   value_of_this,                /* value_of_this */
515   cp_lookup_symbol_nonlocal,    /* lookup_symbol_nonlocal */
516   cp_lookup_transparent_type,   /* lookup_transparent_type */
517   cplus_demangle,               /* Language specific symbol demangler */
518   cp_class_name_from_physname,  /* Language specific class_name_from_physname */
519   c_op_print_tab,               /* expression operators for printing */
520   1,                            /* c-style arrays */
521   0,                            /* String lower bound */
522   default_word_break_characters,
523   cplus_language_arch_info,
524   default_print_array_index,
525   cp_pass_by_reference,
526   LANG_MAGIC
527 };
528
529 const struct language_defn asm_language_defn =
530 {
531   "asm",                        /* Language name */
532   language_asm,
533   range_check_off,
534   type_check_off,
535   case_sensitive_on,
536   array_row_major,
537   &exp_descriptor_standard,
538   c_preprocess_and_parse,
539   c_error,
540   null_post_parser,
541   c_printchar,                  /* Print a character constant */
542   c_printstr,                   /* Function to print string constant */
543   c_emit_char,                  /* Print a single char */
544   c_print_type,                 /* Print a type using appropriate syntax */
545   c_val_print,                  /* Print a value using appropriate syntax */
546   c_value_print,                /* Print a top-level value */
547   NULL,                         /* Language specific skip_trampoline */
548   NULL,                         /* value_of_this */
549   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
550   basic_lookup_transparent_type,/* lookup_transparent_type */
551   NULL,                         /* Language specific symbol demangler */
552   NULL,                         /* Language specific class_name_from_physname */
553   c_op_print_tab,               /* expression operators for printing */
554   1,                            /* c-style arrays */
555   0,                            /* String lower bound */
556   default_word_break_characters,
557   c_language_arch_info, /* FIXME: la_language_arch_info.  */
558   default_print_array_index,
559   default_pass_by_reference,
560   LANG_MAGIC
561 };
562
563 /* The following language_defn does not represent a real language.
564    It just provides a minimal support a-la-C that should allow users
565    to do some simple operations when debugging applications that use
566    a language currently not supported by GDB.  */
567
568 const struct language_defn minimal_language_defn =
569 {
570   "minimal",                    /* Language name */
571   language_minimal,
572   range_check_off,
573   type_check_off,
574   case_sensitive_on,
575   array_row_major,
576   &exp_descriptor_standard,
577   c_preprocess_and_parse,
578   c_error,
579   null_post_parser,
580   c_printchar,                  /* Print a character constant */
581   c_printstr,                   /* Function to print string constant */
582   c_emit_char,                  /* Print a single char */
583   c_print_type,                 /* Print a type using appropriate syntax */
584   c_val_print,                  /* Print a value using appropriate syntax */
585   c_value_print,                /* Print a top-level value */
586   NULL,                         /* Language specific skip_trampoline */
587   NULL,                         /* value_of_this */
588   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
589   basic_lookup_transparent_type,/* lookup_transparent_type */
590   NULL,                         /* Language specific symbol demangler */
591   NULL,                         /* Language specific class_name_from_physname */
592   c_op_print_tab,               /* expression operators for printing */
593   1,                            /* c-style arrays */
594   0,                            /* String lower bound */
595   default_word_break_characters,
596   c_language_arch_info,
597   default_print_array_index,
598   default_pass_by_reference,
599   LANG_MAGIC
600 };
601
602 void
603 _initialize_c_language (void)
604 {
605   add_language (&c_language_defn);
606   add_language (&cplus_language_defn);
607   add_language (&asm_language_defn);
608   add_language (&minimal_language_defn);
609 }