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