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