Approved by Jim Blandy:
[platform/upstream/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) == '\0')
113     length--;
114
115   if (length == 0)
116     {
117       fputs_filtered ("\"\"", stream);
118       return;
119     }
120
121   for (i = 0; i < length && things_printed < print_max; ++i)
122     {
123       /* Position of the character we are examining
124          to see whether it is repeated.  */
125       unsigned int rep1;
126       /* Number of repetitions we have detected so far.  */
127       unsigned int reps;
128       unsigned long current_char;
129
130       QUIT;
131
132       if (need_comma)
133         {
134           fputs_filtered (", ", stream);
135           need_comma = 0;
136         }
137
138       current_char = extract_unsigned_integer (string + i * width, width);
139
140       rep1 = i + 1;
141       reps = 1;
142       while (rep1 < length
143              && extract_unsigned_integer (string + rep1 * width, width)
144              == current_char)
145         {
146           ++rep1;
147           ++reps;
148         }
149
150       if (reps > repeat_count_threshold)
151         {
152           if (in_quotes)
153             {
154               if (inspect_it)
155                 fputs_filtered ("\\\", ", stream);
156               else
157                 fputs_filtered ("\", ", stream);
158               in_quotes = 0;
159             }
160           LA_PRINT_CHAR (current_char, stream);
161           fprintf_filtered (stream, " <repeats %u times>", reps);
162           i = rep1 - 1;
163           things_printed += repeat_count_threshold;
164           need_comma = 1;
165         }
166       else
167         {
168           if (!in_quotes)
169             {
170               if (inspect_it)
171                 fputs_filtered ("\\\"", stream);
172               else
173                 fputs_filtered ("\"", stream);
174               in_quotes = 1;
175             }
176           LA_EMIT_CHAR (current_char, stream, '"');
177           ++things_printed;
178         }
179     }
180
181   /* Terminate the quotes if necessary.  */
182   if (in_quotes)
183     {
184       if (inspect_it)
185         fputs_filtered ("\\\"", stream);
186       else
187         fputs_filtered ("\"", stream);
188     }
189
190   if (force_ellipses || i < length)
191     fputs_filtered ("...", stream);
192 }
193
194 /* Create a fundamental C type using default reasonable for the current
195    target machine.
196
197    Some object/debugging file formats (DWARF version 1, COFF, etc) do not
198    define fundamental types such as "int" or "double".  Others (stabs or
199    DWARF version 2, etc) do define fundamental types.  For the formats which
200    don't provide fundamental types, gdb can create such types using this
201    function.
202
203    FIXME:  Some compilers distinguish explicitly signed integral types
204    (signed short, signed int, signed long) from "regular" integral types
205    (short, int, long) in the debugging information.  There is some dis-
206    agreement as to how useful this feature is.  In particular, gcc does
207    not support this.  Also, only some debugging formats allow the
208    distinction to be passed on to a debugger.  For now, we always just
209    use "short", "int", or "long" as the type name, for both the implicit
210    and explicitly signed types.  This also makes life easier for the
211    gdb test suite since we don't have to account for the differences
212    in output depending upon what the compiler and debugging format
213    support.  We will probably have to re-examine the issue when gdb
214    starts taking it's fundamental type information directly from the
215    debugging information supplied by the compiler.  fnf@cygnus.com */
216
217 struct type *
218 c_create_fundamental_type (struct objfile *objfile, int typeid)
219 {
220   register struct type *type = NULL;
221
222   switch (typeid)
223     {
224     default:
225       /* FIXME:  For now, if we are asked to produce a type not in this
226          language, create the equivalent of a C integer type with the
227          name "<?type?>".  When all the dust settles from the type
228          reconstruction work, this should probably become an error. */
229       type = init_type (TYPE_CODE_INT,
230                         TARGET_INT_BIT / TARGET_CHAR_BIT,
231                         0, "<?type?>", objfile);
232       warning ("internal error: no C/C++ fundamental type %d", typeid);
233       break;
234     case FT_VOID:
235       type = init_type (TYPE_CODE_VOID,
236                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
237                         0, "void", objfile);
238       break;
239     case FT_BOOLEAN:
240       type = init_type (TYPE_CODE_BOOL,
241                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
242                         0, "bool", objfile);
243       break;
244     case FT_CHAR:
245       type = init_type (TYPE_CODE_INT,
246                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
247                         TYPE_FLAG_NOSIGN, "char", objfile);
248       break;
249     case FT_SIGNED_CHAR:
250       type = init_type (TYPE_CODE_INT,
251                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
252                         0, "signed char", objfile);
253       break;
254     case FT_UNSIGNED_CHAR:
255       type = init_type (TYPE_CODE_INT,
256                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
257                         TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
258       break;
259     case FT_SHORT:
260       type = init_type (TYPE_CODE_INT,
261                         TARGET_SHORT_BIT / TARGET_CHAR_BIT,
262                         0, "short", objfile);
263       break;
264     case FT_SIGNED_SHORT:
265       type = init_type (TYPE_CODE_INT,
266                         TARGET_SHORT_BIT / TARGET_CHAR_BIT,
267                         0, "short", objfile);   /* FIXME-fnf */
268       break;
269     case FT_UNSIGNED_SHORT:
270       type = init_type (TYPE_CODE_INT,
271                         TARGET_SHORT_BIT / TARGET_CHAR_BIT,
272                         TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
273       break;
274     case FT_INTEGER:
275       type = init_type (TYPE_CODE_INT,
276                         TARGET_INT_BIT / TARGET_CHAR_BIT,
277                         0, "int", objfile);
278       break;
279     case FT_SIGNED_INTEGER:
280       type = init_type (TYPE_CODE_INT,
281                         TARGET_INT_BIT / TARGET_CHAR_BIT,
282                         0, "int", objfile);     /* FIXME -fnf */
283       break;
284     case FT_UNSIGNED_INTEGER:
285       type = init_type (TYPE_CODE_INT,
286                         TARGET_INT_BIT / TARGET_CHAR_BIT,
287                         TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
288       break;
289     case FT_LONG:
290       type = init_type (TYPE_CODE_INT,
291                         TARGET_LONG_BIT / TARGET_CHAR_BIT,
292                         0, "long", objfile);
293       break;
294     case FT_SIGNED_LONG:
295       type = init_type (TYPE_CODE_INT,
296                         TARGET_LONG_BIT / TARGET_CHAR_BIT,
297                         0, "long", objfile);    /* FIXME -fnf */
298       break;
299     case FT_UNSIGNED_LONG:
300       type = init_type (TYPE_CODE_INT,
301                         TARGET_LONG_BIT / TARGET_CHAR_BIT,
302                         TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
303       break;
304     case FT_LONG_LONG:
305       type = init_type (TYPE_CODE_INT,
306                         TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
307                         0, "long long", objfile);
308       break;
309     case FT_SIGNED_LONG_LONG:
310       type = init_type (TYPE_CODE_INT,
311                         TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
312                         0, "signed long long", objfile);
313       break;
314     case FT_UNSIGNED_LONG_LONG:
315       type = init_type (TYPE_CODE_INT,
316                         TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
317                         TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
318       break;
319     case FT_FLOAT:
320       type = init_type (TYPE_CODE_FLT,
321                         TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
322                         0, "float", objfile);
323       break;
324     case FT_DBL_PREC_FLOAT:
325       type = init_type (TYPE_CODE_FLT,
326                         TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
327                         0, "double", objfile);
328       break;
329     case FT_EXT_PREC_FLOAT:
330       type = init_type (TYPE_CODE_FLT,
331                         TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
332                         0, "long double", objfile);
333       break;
334     case FT_TEMPLATE_ARG:
335       type = init_type (TYPE_CODE_TEMPLATE_ARG,
336                         0,
337                         0, "<template arg>", objfile);
338       break;
339     }
340   return (type);
341 }
342 \f
343
344 /* Table mapping opcodes into strings for printing operators
345    and precedences of the operators.  */
346
347 const struct op_print c_op_print_tab[] =
348 {
349   {",", BINOP_COMMA, PREC_COMMA, 0},
350   {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
351   {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
352   {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
353   {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
354   {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
355   {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
356   {"==", BINOP_EQUAL, PREC_EQUAL, 0},
357   {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
358   {"<=", BINOP_LEQ, PREC_ORDER, 0},
359   {">=", BINOP_GEQ, PREC_ORDER, 0},
360   {">", BINOP_GTR, PREC_ORDER, 0},
361   {"<", BINOP_LESS, PREC_ORDER, 0},
362   {">>", BINOP_RSH, PREC_SHIFT, 0},
363   {"<<", BINOP_LSH, PREC_SHIFT, 0},
364   {"+", BINOP_ADD, PREC_ADD, 0},
365   {"-", BINOP_SUB, PREC_ADD, 0},
366   {"*", BINOP_MUL, PREC_MUL, 0},
367   {"/", BINOP_DIV, PREC_MUL, 0},
368   {"%", BINOP_REM, PREC_MUL, 0},
369   {"@", BINOP_REPEAT, PREC_REPEAT, 0},
370   {"-", UNOP_NEG, PREC_PREFIX, 0},
371   {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
372   {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
373   {"*", UNOP_IND, PREC_PREFIX, 0},
374   {"&", UNOP_ADDR, PREC_PREFIX, 0},
375   {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
376   {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
377   {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
378   {NULL, 0, 0, 0}
379 };
380 \f
381 struct type **CONST_PTR (c_builtin_types[]) =
382 {
383   &builtin_type_int,
384     &builtin_type_long,
385     &builtin_type_short,
386     &builtin_type_char,
387     &builtin_type_float,
388     &builtin_type_double,
389     &builtin_type_void,
390     &builtin_type_long_long,
391     &builtin_type_signed_char,
392     &builtin_type_unsigned_char,
393     &builtin_type_unsigned_short,
394     &builtin_type_unsigned_int,
395     &builtin_type_unsigned_long,
396     &builtin_type_unsigned_long_long,
397     &builtin_type_long_double,
398     &builtin_type_complex,
399     &builtin_type_double_complex,
400     0
401 };
402
403 const struct language_defn c_language_defn =
404 {
405   "c",                          /* Language name */
406   language_c,
407   c_builtin_types,
408   range_check_off,
409   type_check_off,
410   case_sensitive_on,
411   c_parse,
412   c_error,
413   evaluate_subexp_standard,
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_create_fundamental_type,    /* Create fundamental type in this language */
418   c_print_type,                 /* Print a type using appropriate syntax */
419   c_val_print,                  /* Print a value using appropriate syntax */
420   c_value_print,                /* Print a top-level value */
421   {"", "", "", ""},             /* Binary format info */
422   {"0%lo", "0", "o", ""},       /* Octal format info */
423   {"%ld", "", "d", ""},         /* Decimal format info */
424   {"0x%lx", "0x", "x", ""},     /* Hex format info */
425   c_op_print_tab,               /* expression operators for printing */
426   1,                            /* c-style arrays */
427   0,                            /* String lower bound */
428   &builtin_type_char,           /* Type of string elements */
429   LANG_MAGIC
430 };
431
432 struct type **const (cplus_builtin_types[]) =
433 {
434   &builtin_type_int,
435     &builtin_type_long,
436     &builtin_type_short,
437     &builtin_type_char,
438     &builtin_type_float,
439     &builtin_type_double,
440     &builtin_type_void,
441     &builtin_type_long_long,
442     &builtin_type_signed_char,
443     &builtin_type_unsigned_char,
444     &builtin_type_unsigned_short,
445     &builtin_type_unsigned_int,
446     &builtin_type_unsigned_long,
447     &builtin_type_unsigned_long_long,
448     &builtin_type_long_double,
449     &builtin_type_complex,
450     &builtin_type_double_complex,
451     &builtin_type_bool,
452     0
453 };
454
455 const struct language_defn cplus_language_defn =
456 {
457   "c++",                        /* Language name */
458   language_cplus,
459   cplus_builtin_types,
460   range_check_off,
461   type_check_off,
462   case_sensitive_on,
463   c_parse,
464   c_error,
465   evaluate_subexp_standard,
466   c_printchar,                  /* Print a character constant */
467   c_printstr,                   /* Function to print string constant */
468   c_emit_char,                  /* Print a single char */
469   c_create_fundamental_type,    /* Create fundamental type in this language */
470   c_print_type,                 /* Print a type using appropriate syntax */
471   c_val_print,                  /* Print a value using appropriate syntax */
472   c_value_print,                /* Print a top-level value */
473   {"", "", "", ""},             /* Binary format info */
474   {"0%lo", "0", "o", ""},       /* Octal format info */
475   {"%ld", "", "d", ""},         /* Decimal format info */
476   {"0x%lx", "0x", "x", ""},     /* Hex format info */
477   c_op_print_tab,               /* expression operators for printing */
478   1,                            /* c-style arrays */
479   0,                            /* String lower bound */
480   &builtin_type_char,           /* Type of string elements */
481   LANG_MAGIC
482 };
483
484 const struct language_defn asm_language_defn =
485 {
486   "asm",                        /* Language name */
487   language_asm,
488   c_builtin_types,
489   range_check_off,
490   type_check_off,
491   case_sensitive_on,
492   c_parse,
493   c_error,
494   evaluate_subexp_standard,
495   c_printchar,                  /* Print a character constant */
496   c_printstr,                   /* Function to print string constant */
497   c_emit_char,                  /* Print a single char */
498   c_create_fundamental_type,    /* Create fundamental type in this language */
499   c_print_type,                 /* Print a type using appropriate syntax */
500   c_val_print,                  /* Print a value using appropriate syntax */
501   c_value_print,                /* Print a top-level value */
502   {"", "", "", ""},             /* Binary format info */
503   {"0%lo", "0", "o", ""},       /* Octal format info */
504   {"%ld", "", "d", ""},         /* Decimal format info */
505   {"0x%lx", "0x", "x", ""},     /* Hex format info */
506   c_op_print_tab,               /* expression operators for printing */
507   1,                            /* c-style arrays */
508   0,                            /* String lower bound */
509   &builtin_type_char,           /* Type of string elements */
510   LANG_MAGIC
511 };
512
513 void
514 _initialize_c_language (void)
515 {
516   add_language (&c_language_defn);
517   add_language (&cplus_language_defn);
518   add_language (&asm_language_defn);
519 }