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