* Makefile.in (VERSION): Bump to 4.7.4.
[platform/upstream/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 /* These variables point to the objects
367    representing the predefined C data types.  */
368
369 struct type *builtin_type_void;
370 struct type *builtin_type_char;
371 struct type *builtin_type_short;
372 struct type *builtin_type_int;
373 struct type *builtin_type_long;
374 struct type *builtin_type_long_long;
375 struct type *builtin_type_signed_char;
376 struct type *builtin_type_unsigned_char;
377 struct type *builtin_type_unsigned_short;
378 struct type *builtin_type_unsigned_int;
379 struct type *builtin_type_unsigned_long;
380 struct type *builtin_type_unsigned_long_long;
381 struct type *builtin_type_float;
382 struct type *builtin_type_double;
383 struct type *builtin_type_long_double;
384 struct type *builtin_type_complex;
385 struct type *builtin_type_double_complex;
386
387 struct type ** const (c_builtin_types[]) = 
388 {
389   &builtin_type_int,
390   &builtin_type_long,
391   &builtin_type_short,
392   &builtin_type_char,
393   &builtin_type_float,
394   &builtin_type_double,
395   &builtin_type_void,
396   &builtin_type_long_long,
397   &builtin_type_signed_char,
398   &builtin_type_unsigned_char,
399   &builtin_type_unsigned_short,
400   &builtin_type_unsigned_int,
401   &builtin_type_unsigned_long,
402   &builtin_type_unsigned_long_long,
403   &builtin_type_long_double,
404   &builtin_type_complex,
405   &builtin_type_double_complex,
406   0
407 };
408
409 const struct language_defn c_language_defn = {
410   "c",                          /* Language name */
411   language_c,
412   c_builtin_types,
413   range_check_off,
414   type_check_off,
415   c_parse,
416   c_error,
417   c_printchar,                  /* Print a character constant */
418   c_printstr,                   /* Function to print string constant */
419   c_create_fundamental_type,    /* Create fundamental type in this language */
420   c_print_type,                 /* Print a type using appropriate syntax */
421   c_val_print,                  /* Print a value using appropriate syntax */
422   &BUILTIN_TYPE_LONGEST,        /* longest signed   integral type */
423   &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
424   &builtin_type_double,         /* longest floating point type */ /*FIXME*/
425   {"",     "",    "",  ""},     /* Binary format info */
426   {"0%o",  "0",   "o", ""},     /* Octal format info */
427   {"%d",   "",    "d", ""},     /* Decimal format info */
428   {"0x%x", "0x",  "x", ""},     /* Hex format info */
429   c_op_print_tab,               /* expression operators for printing */
430   LANG_MAGIC
431 };
432
433 const struct language_defn cplus_language_defn = {
434   "c++",                                /* Language name */
435   language_cplus,
436   c_builtin_types,
437   range_check_off,
438   type_check_off,
439   c_parse,
440   c_error,
441   c_printchar,                  /* Print a character constant */
442   c_printstr,                   /* Function to print string constant */
443   c_create_fundamental_type,    /* Create fundamental type in this language */
444   c_print_type,                 /* Print a type using appropriate syntax */
445   c_val_print,                  /* Print a value using appropriate syntax */
446   &BUILTIN_TYPE_LONGEST,         /* longest signed   integral type */
447   &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
448   &builtin_type_double,         /* longest floating point type */ /*FIXME*/
449   {"",      "",    "",   ""},   /* Binary format info */
450   {"0%o",   "0",   "o",  ""},   /* Octal format info */
451   {"%d",    "",    "d",  ""},   /* Decimal format info */
452   {"0x%x",  "0x",  "x",  ""},   /* Hex format info */
453   c_op_print_tab,               /* expression operators for printing */
454   LANG_MAGIC
455 };
456
457 void
458 _initialize_c_language ()
459 {
460   builtin_type_void =
461     init_type (TYPE_CODE_VOID, 1,
462                0,
463                "void", (struct objfile *) NULL);
464   builtin_type_char =
465     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
466                0,
467                "char", (struct objfile *) NULL);
468   builtin_type_signed_char =
469     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
470                TYPE_FLAG_SIGNED,
471                "signed char", (struct objfile *) NULL);
472   builtin_type_unsigned_char =
473     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
474                TYPE_FLAG_UNSIGNED,
475                "unsigned char", (struct objfile *) NULL);
476   builtin_type_short =
477     init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
478                0,
479                "short", (struct objfile *) NULL);
480   builtin_type_unsigned_short =
481     init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
482                TYPE_FLAG_UNSIGNED,
483                "unsigned short", (struct objfile *) NULL);
484   builtin_type_int =
485     init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
486                0,
487                "int", (struct objfile *) NULL);
488   builtin_type_unsigned_int =
489     init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
490                TYPE_FLAG_UNSIGNED,
491                "unsigned int", (struct objfile *) NULL);
492   builtin_type_long =
493     init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
494                0,
495                "long", (struct objfile *) NULL);
496   builtin_type_unsigned_long =
497     init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
498                TYPE_FLAG_UNSIGNED,
499                "unsigned long", (struct objfile *) NULL);
500   builtin_type_long_long =
501     init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
502                0,
503                "long long", (struct objfile *) NULL);
504   builtin_type_unsigned_long_long = 
505     init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
506                TYPE_FLAG_UNSIGNED,
507                "unsigned long long", (struct objfile *) NULL);
508   builtin_type_float =
509     init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
510                0,
511                "float", (struct objfile *) NULL);
512   builtin_type_double =
513     init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
514                0,
515                "double", (struct objfile *) NULL);
516   builtin_type_long_double =
517     init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
518                0,
519                "long double", (struct objfile *) NULL);
520   builtin_type_complex =
521     init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
522                0,
523                "complex", (struct objfile *) NULL);
524   builtin_type_double_complex =
525     init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
526                0,
527                "double complex", (struct objfile *) NULL);
528
529   add_language (&c_language_defn);
530   add_language (&cplus_language_defn);
531 }