This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gdb / c-lang.c
1 /* C language support routines for GDB, the GNU debugger.
2    Copyright 1992, 1993, 1994, 2000 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
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
30 extern void _initialize_c_language (void);
31 static void c_emit_char (int c, struct ui_file * stream, int quoter);
32
33 /* Print the character C on STREAM as part of the contents of a literal
34    string whose delimiter is QUOTER.  Note that that format for printing
35    characters and strings is language specific. */
36
37 static void
38 c_emit_char (c, stream, quoter)
39      register int c;
40      struct ui_file *stream;
41      int quoter;
42 {
43   c &= 0xFF;                    /* Avoid sign bit follies */
44
45   if (PRINT_LITERAL_FORM (c))
46     {
47       if (c == '\\' || c == quoter)
48         {
49           fputs_filtered ("\\", stream);
50         }
51       fprintf_filtered (stream, "%c", c);
52     }
53   else
54     {
55       switch (c)
56         {
57         case '\n':
58           fputs_filtered ("\\n", stream);
59           break;
60         case '\b':
61           fputs_filtered ("\\b", stream);
62           break;
63         case '\t':
64           fputs_filtered ("\\t", stream);
65           break;
66         case '\f':
67           fputs_filtered ("\\f", stream);
68           break;
69         case '\r':
70           fputs_filtered ("\\r", stream);
71           break;
72         case '\033':
73           fputs_filtered ("\\e", stream);
74           break;
75         case '\007':
76           fputs_filtered ("\\a", stream);
77           break;
78         default:
79           fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
80           break;
81         }
82     }
83 }
84
85 void
86 c_printchar (c, stream)
87      int c;
88      struct ui_file *stream;
89 {
90   fputc_filtered ('\'', stream);
91   LA_EMIT_CHAR (c, stream, '\'');
92   fputc_filtered ('\'', stream);
93 }
94
95 /* Print the character string STRING, printing at most LENGTH characters.
96    LENGTH is -1 if the string is nul terminated.  Each character is WIDTH bytes
97    long.  Printing stops early if the number hits print_max; repeat counts are
98    printed as appropriate.  Print ellipses at the end if we had to stop before
99    printing LENGTH characters, or if FORCE_ELLIPSES.  */
100
101 void
102 c_printstr (stream, string, length, width, force_ellipses)
103      struct ui_file *stream;
104      char *string;
105      unsigned int length;
106      int width;
107      int force_ellipses;
108 {
109   register unsigned int i;
110   unsigned int things_printed = 0;
111   int in_quotes = 0;
112   int need_comma = 0;
113   extern int inspect_it;
114
115   /* If the string was not truncated due to `set print elements', and
116      the last byte of it is a null, we don't print that, in traditional C
117      style.  */
118   if (!force_ellipses
119       && length > 0
120   && extract_unsigned_integer (string + (length - 1) * width, width) == '\0')
121     length--;
122
123   if (length == 0)
124     {
125       fputs_filtered ("\"\"", stream);
126       return;
127     }
128
129   for (i = 0; i < length && things_printed < print_max; ++i)
130     {
131       /* Position of the character we are examining
132          to see whether it is repeated.  */
133       unsigned int rep1;
134       /* Number of repetitions we have detected so far.  */
135       unsigned int reps;
136       unsigned long current_char;
137
138       QUIT;
139
140       if (need_comma)
141         {
142           fputs_filtered (", ", stream);
143           need_comma = 0;
144         }
145
146       current_char = extract_unsigned_integer (string + i * width, width);
147
148       rep1 = i + 1;
149       reps = 1;
150       while (rep1 < length
151              && extract_unsigned_integer (string + rep1 * width, width)
152              == current_char)
153         {
154           ++rep1;
155           ++reps;
156         }
157
158       if (reps > repeat_count_threshold)
159         {
160           if (in_quotes)
161             {
162               if (inspect_it)
163                 fputs_filtered ("\\\", ", stream);
164               else
165                 fputs_filtered ("\", ", stream);
166               in_quotes = 0;
167             }
168           LA_PRINT_CHAR (current_char, stream);
169           fprintf_filtered (stream, " <repeats %u times>", reps);
170           i = rep1 - 1;
171           things_printed += repeat_count_threshold;
172           need_comma = 1;
173         }
174       else
175         {
176           if (!in_quotes)
177             {
178               if (inspect_it)
179                 fputs_filtered ("\\\"", stream);
180               else
181                 fputs_filtered ("\"", stream);
182               in_quotes = 1;
183             }
184           LA_EMIT_CHAR (current_char, stream, '"');
185           ++things_printed;
186         }
187     }
188
189   /* Terminate the quotes if necessary.  */
190   if (in_quotes)
191     {
192       if (inspect_it)
193         fputs_filtered ("\\\"", stream);
194       else
195         fputs_filtered ("\"", stream);
196     }
197
198   if (force_ellipses || i < length)
199     fputs_filtered ("...", stream);
200 }
201
202 /* Create a fundamental C type using default reasonable for the current
203    target machine.
204
205    Some object/debugging file formats (DWARF version 1, COFF, etc) do not
206    define fundamental types such as "int" or "double".  Others (stabs or
207    DWARF version 2, etc) do define fundamental types.  For the formats which
208    don't provide fundamental types, gdb can create such types using this
209    function.
210
211    FIXME:  Some compilers distinguish explicitly signed integral types
212    (signed short, signed int, signed long) from "regular" integral types
213    (short, int, long) in the debugging information.  There is some dis-
214    agreement as to how useful this feature is.  In particular, gcc does
215    not support this.  Also, only some debugging formats allow the
216    distinction to be passed on to a debugger.  For now, we always just
217    use "short", "int", or "long" as the type name, for both the implicit
218    and explicitly signed types.  This also makes life easier for the
219    gdb test suite since we don't have to account for the differences
220    in output depending upon what the compiler and debugging format
221    support.  We will probably have to re-examine the issue when gdb
222    starts taking it's fundamental type information directly from the
223    debugging information supplied by the compiler.  fnf@cygnus.com */
224
225 struct type *
226 c_create_fundamental_type (objfile, typeid)
227      struct objfile *objfile;
228      int typeid;
229 {
230   register struct type *type = NULL;
231
232   switch (typeid)
233     {
234     default:
235       /* FIXME:  For now, if we are asked to produce a type not in this
236          language, create the equivalent of a C integer type with the
237          name "<?type?>".  When all the dust settles from the type
238          reconstruction work, this should probably become an error. */
239       type = init_type (TYPE_CODE_INT,
240                         TARGET_INT_BIT / TARGET_CHAR_BIT,
241                         0, "<?type?>", objfile);
242       warning ("internal error: no C/C++ fundamental type %d", typeid);
243       break;
244     case FT_VOID:
245       type = init_type (TYPE_CODE_VOID,
246                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
247                         0, "void", objfile);
248       break;
249     case FT_BOOLEAN:
250       type = init_type (TYPE_CODE_BOOL,
251                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
252                         0, "bool", objfile);
253
254       break;
255     case FT_CHAR:
256       type = init_type (TYPE_CODE_INT,
257                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
258                         0, "char", objfile);
259       TYPE_FLAGS (type) |= TYPE_FLAG_NOSIGN;
260       break;
261     case FT_SIGNED_CHAR:
262       type = init_type (TYPE_CODE_INT,
263                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
264                         0, "signed char", objfile);
265       break;
266     case FT_UNSIGNED_CHAR:
267       type = init_type (TYPE_CODE_INT,
268                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
269                         TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
270       break;
271     case FT_SHORT:
272       type = init_type (TYPE_CODE_INT,
273                         TARGET_SHORT_BIT / TARGET_CHAR_BIT,
274                         0, "short", objfile);
275       break;
276     case FT_SIGNED_SHORT:
277       type = init_type (TYPE_CODE_INT,
278                         TARGET_SHORT_BIT / TARGET_CHAR_BIT,
279                         0, "short", objfile);   /* FIXME-fnf */
280       break;
281     case FT_UNSIGNED_SHORT:
282       type = init_type (TYPE_CODE_INT,
283                         TARGET_SHORT_BIT / TARGET_CHAR_BIT,
284                         TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
285       break;
286     case FT_INTEGER:
287       type = init_type (TYPE_CODE_INT,
288                         TARGET_INT_BIT / TARGET_CHAR_BIT,
289                         0, "int", objfile);
290       break;
291     case FT_SIGNED_INTEGER:
292       type = init_type (TYPE_CODE_INT,
293                         TARGET_INT_BIT / TARGET_CHAR_BIT,
294                         0, "int", objfile);     /* FIXME -fnf */
295       break;
296     case FT_UNSIGNED_INTEGER:
297       type = init_type (TYPE_CODE_INT,
298                         TARGET_INT_BIT / TARGET_CHAR_BIT,
299                         TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
300       break;
301     case FT_LONG:
302       type = init_type (TYPE_CODE_INT,
303                         TARGET_LONG_BIT / TARGET_CHAR_BIT,
304                         0, "long", objfile);
305       break;
306     case FT_SIGNED_LONG:
307       type = init_type (TYPE_CODE_INT,
308                         TARGET_LONG_BIT / TARGET_CHAR_BIT,
309                         0, "long", objfile);    /* FIXME -fnf */
310       break;
311     case FT_UNSIGNED_LONG:
312       type = init_type (TYPE_CODE_INT,
313                         TARGET_LONG_BIT / TARGET_CHAR_BIT,
314                         TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
315       break;
316     case FT_LONG_LONG:
317       type = init_type (TYPE_CODE_INT,
318                         TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
319                         0, "long long", objfile);
320       break;
321     case FT_SIGNED_LONG_LONG:
322       type = init_type (TYPE_CODE_INT,
323                         TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
324                         0, "signed long long", objfile);
325       break;
326     case FT_UNSIGNED_LONG_LONG:
327       type = init_type (TYPE_CODE_INT,
328                         TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
329                         TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
330       break;
331     case FT_FLOAT:
332       type = init_type (TYPE_CODE_FLT,
333                         TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
334                         0, "float", objfile);
335       break;
336     case FT_DBL_PREC_FLOAT:
337       type = init_type (TYPE_CODE_FLT,
338                         TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
339                         0, "double", objfile);
340       break;
341     case FT_EXT_PREC_FLOAT:
342       type = init_type (TYPE_CODE_FLT,
343                         TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
344                         0, "long double", objfile);
345       break;
346     case FT_TEMPLATE_ARG:
347       type = init_type (TYPE_CODE_TEMPLATE_ARG,
348                         0,
349                         0, "<template arg>", objfile);
350
351       break;
352     }
353   return (type);
354 }
355 \f
356
357 /* Table mapping opcodes into strings for printing operators
358    and precedences of the operators.  */
359
360 const struct op_print c_op_print_tab[] =
361 {
362   {",", BINOP_COMMA, PREC_COMMA, 0},
363   {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
364   {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
365   {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
366   {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
367   {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
368   {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
369   {"==", BINOP_EQUAL, PREC_EQUAL, 0},
370   {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
371   {"<=", BINOP_LEQ, PREC_ORDER, 0},
372   {">=", BINOP_GEQ, PREC_ORDER, 0},
373   {">", BINOP_GTR, PREC_ORDER, 0},
374   {"<", BINOP_LESS, PREC_ORDER, 0},
375   {">>", BINOP_RSH, PREC_SHIFT, 0},
376   {"<<", BINOP_LSH, PREC_SHIFT, 0},
377   {"+", BINOP_ADD, PREC_ADD, 0},
378   {"-", BINOP_SUB, PREC_ADD, 0},
379   {"*", BINOP_MUL, PREC_MUL, 0},
380   {"/", BINOP_DIV, PREC_MUL, 0},
381   {"%", BINOP_REM, PREC_MUL, 0},
382   {"@", BINOP_REPEAT, PREC_REPEAT, 0},
383   {"-", UNOP_NEG, PREC_PREFIX, 0},
384   {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
385   {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
386   {"*", UNOP_IND, PREC_PREFIX, 0},
387   {"&", UNOP_ADDR, PREC_PREFIX, 0},
388   {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
389   {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
390   {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
391   {NULL, 0, 0, 0}
392 };
393 \f
394 struct type **CONST_PTR (c_builtin_types[]) =
395 {
396   &builtin_type_int,
397     &builtin_type_long,
398     &builtin_type_short,
399     &builtin_type_char,
400     &builtin_type_float,
401     &builtin_type_double,
402     &builtin_type_void,
403     &builtin_type_long_long,
404     &builtin_type_signed_char,
405     &builtin_type_unsigned_char,
406     &builtin_type_unsigned_short,
407     &builtin_type_unsigned_int,
408     &builtin_type_unsigned_long,
409     &builtin_type_unsigned_long_long,
410     &builtin_type_long_double,
411     &builtin_type_complex,
412     &builtin_type_double_complex,
413     0
414 };
415
416 const struct language_defn c_language_defn =
417 {
418   "c",                          /* Language name */
419   language_c,
420   c_builtin_types,
421   range_check_off,
422   type_check_off,
423   c_parse,
424   c_error,
425   evaluate_subexp_standard,
426   c_printchar,                  /* Print a character constant */
427   c_printstr,                   /* Function to print string constant */
428   c_emit_char,                  /* Print a single char */
429   c_create_fundamental_type,    /* Create fundamental type in this language */
430   c_print_type,                 /* Print a type using appropriate syntax */
431   c_val_print,                  /* Print a value using appropriate syntax */
432   c_value_print,                /* Print a top-level value */
433   {"", "", "", ""},             /* Binary format info */
434   {"0%lo", "0", "o", ""},       /* Octal format info */
435   {"%ld", "", "d", ""},         /* Decimal format info */
436   {"0x%lx", "0x", "x", ""},     /* Hex format info */
437   c_op_print_tab,               /* expression operators for printing */
438   1,                            /* c-style arrays */
439   0,                            /* String lower bound */
440   &builtin_type_char,           /* Type of string elements */
441   LANG_MAGIC
442 };
443
444 struct type **const (cplus_builtin_types[]) =
445 {
446   &builtin_type_int,
447     &builtin_type_long,
448     &builtin_type_short,
449     &builtin_type_char,
450     &builtin_type_float,
451     &builtin_type_double,
452     &builtin_type_void,
453     &builtin_type_long_long,
454     &builtin_type_signed_char,
455     &builtin_type_unsigned_char,
456     &builtin_type_unsigned_short,
457     &builtin_type_unsigned_int,
458     &builtin_type_unsigned_long,
459     &builtin_type_unsigned_long_long,
460     &builtin_type_long_double,
461     &builtin_type_complex,
462     &builtin_type_double_complex,
463     &builtin_type_bool,
464     0
465 };
466
467 const struct language_defn cplus_language_defn =
468 {
469   "c++",                        /* Language name */
470   language_cplus,
471   cplus_builtin_types,
472   range_check_off,
473   type_check_off,
474   c_parse,
475   c_error,
476   evaluate_subexp_standard,
477   c_printchar,                  /* Print a character constant */
478   c_printstr,                   /* Function to print string constant */
479   c_emit_char,                  /* Print a single char */
480   c_create_fundamental_type,    /* Create fundamental type in this language */
481   c_print_type,                 /* Print a type using appropriate syntax */
482   c_val_print,                  /* Print a value using appropriate syntax */
483   c_value_print,                /* Print a top-level value */
484   {"", "", "", ""},             /* Binary format info */
485   {"0%lo", "0", "o", ""},       /* Octal format info */
486   {"%ld", "", "d", ""},         /* Decimal format info */
487   {"0x%lx", "0x", "x", ""},     /* Hex format info */
488   c_op_print_tab,               /* expression operators for printing */
489   1,                            /* c-style arrays */
490   0,                            /* String lower bound */
491   &builtin_type_char,           /* Type of string elements */
492   LANG_MAGIC
493 };
494
495 const struct language_defn asm_language_defn =
496 {
497   "asm",                        /* Language name */
498   language_asm,
499   c_builtin_types,
500   range_check_off,
501   type_check_off,
502   c_parse,
503   c_error,
504   evaluate_subexp_standard,
505   c_printchar,                  /* Print a character constant */
506   c_printstr,                   /* Function to print string constant */
507   c_emit_char,                  /* Print a single char */
508   c_create_fundamental_type,    /* Create fundamental type in this language */
509   c_print_type,                 /* Print a type using appropriate syntax */
510   c_val_print,                  /* Print a value using appropriate syntax */
511   c_value_print,                /* Print a top-level value */
512   {"", "", "", ""},             /* Binary format info */
513   {"0%lo", "0", "o", ""},       /* Octal format info */
514   {"%ld", "", "d", ""},         /* Decimal format info */
515   {"0x%lx", "0x", "x", ""},     /* Hex format info */
516   c_op_print_tab,               /* expression operators for printing */
517   1,                            /* c-style arrays */
518   0,                            /* String lower bound */
519   &builtin_type_char,           /* Type of string elements */
520   LANG_MAGIC
521 };
522
523 void
524 _initialize_c_language ()
525 {
526   add_language (&c_language_defn);
527   add_language (&cplus_language_defn);
528   add_language (&asm_language_defn);
529 }