* c-exp.y, m2-exp.y: Migrate code that has nothing to do with
[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   register int nbytes;
215
216   switch (typeid)
217     {
218       default:
219         /* FIXME:  For now, if we are asked to produce a type not in this
220            language, create the equivalent of a C integer type with the
221            name "<?type?>".  When all the dust settles from the type
222            reconstruction work, this should probably become an error. */
223         type = init_type (TYPE_CODE_INT,
224                           TARGET_INT_BIT / TARGET_CHAR_BIT,
225                           0, "<?type?>", objfile);
226         warning ("internal error: no C/C++ fundamental type %d", typeid);
227         break;
228       case FT_VOID:
229         type = init_type (TYPE_CODE_VOID,
230                           TARGET_CHAR_BIT / TARGET_CHAR_BIT,
231                           0, "void", objfile);
232         break;
233       case FT_CHAR:
234         type = init_type (TYPE_CODE_INT,
235                           TARGET_CHAR_BIT / TARGET_CHAR_BIT,
236                           0, "char", objfile);
237         break;
238       case FT_SIGNED_CHAR:
239         type = init_type (TYPE_CODE_INT,
240                           TARGET_CHAR_BIT / TARGET_CHAR_BIT,
241                           TYPE_FLAG_SIGNED, "signed char", objfile);
242         break;
243       case FT_UNSIGNED_CHAR:
244         type = init_type (TYPE_CODE_INT,
245                           TARGET_CHAR_BIT / TARGET_CHAR_BIT,
246                           TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
247         break;
248       case FT_SHORT:
249         type = init_type (TYPE_CODE_INT,
250                           TARGET_SHORT_BIT / TARGET_CHAR_BIT,
251                           0, "short", objfile);
252         break;
253       case FT_SIGNED_SHORT:
254         type = init_type (TYPE_CODE_INT,
255                           TARGET_SHORT_BIT / TARGET_CHAR_BIT,
256                           TYPE_FLAG_SIGNED, "short", objfile);  /* FIXME-fnf */
257         break;
258       case FT_UNSIGNED_SHORT:
259         type = init_type (TYPE_CODE_INT,
260                           TARGET_SHORT_BIT / TARGET_CHAR_BIT,
261                           TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
262         break;
263       case FT_INTEGER:
264         type = init_type (TYPE_CODE_INT,
265                           TARGET_INT_BIT / TARGET_CHAR_BIT,
266                           0, "int", objfile);
267         break;
268       case FT_SIGNED_INTEGER:
269         type = init_type (TYPE_CODE_INT,
270                           TARGET_INT_BIT / TARGET_CHAR_BIT,
271                           TYPE_FLAG_SIGNED, "int", objfile); /* FIXME -fnf */
272         break;
273       case FT_UNSIGNED_INTEGER:
274         type = init_type (TYPE_CODE_INT,
275                           TARGET_INT_BIT / TARGET_CHAR_BIT,
276                           TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
277         break;
278       case FT_LONG:
279         type = init_type (TYPE_CODE_INT,
280                           TARGET_LONG_BIT / TARGET_CHAR_BIT,
281                           0, "long", objfile);
282         break;
283       case FT_SIGNED_LONG:
284         type = init_type (TYPE_CODE_INT,
285                           TARGET_LONG_BIT / TARGET_CHAR_BIT,
286                           TYPE_FLAG_SIGNED, "long", objfile); /* FIXME -fnf */
287         break;
288       case FT_UNSIGNED_LONG:
289         type = init_type (TYPE_CODE_INT,
290                           TARGET_LONG_BIT / TARGET_CHAR_BIT,
291                           TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
292         break;
293       case FT_LONG_LONG:
294         type = init_type (TYPE_CODE_INT,
295                           TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
296                           0, "long long", objfile);
297         break;
298       case FT_SIGNED_LONG_LONG:
299         type = init_type (TYPE_CODE_INT,
300                           TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
301                           TYPE_FLAG_SIGNED, "signed long long", objfile);
302         break;
303       case FT_UNSIGNED_LONG_LONG:
304         type = init_type (TYPE_CODE_INT,
305                           TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
306                           TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
307         break;
308       case FT_FLOAT:
309         type = init_type (TYPE_CODE_FLT,
310                           TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
311                           0, "float", objfile);
312         break;
313       case FT_DBL_PREC_FLOAT:
314         type = init_type (TYPE_CODE_FLT,
315                           TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
316                           0, "double", objfile);
317         break;
318       case FT_EXT_PREC_FLOAT:
319         type = init_type (TYPE_CODE_FLT,
320                           TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
321                           0, "long double", objfile);
322         break;
323       }
324   return (type);
325 }
326
327 \f
328 /* Table mapping opcodes into strings for printing operators
329    and precedences of the operators.  */
330
331 const static struct op_print c_op_print_tab[] =
332   {
333     {",",  BINOP_COMMA, PREC_COMMA, 0},
334     {"=",  BINOP_ASSIGN, PREC_ASSIGN, 1},
335     {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
336     {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
337     {"|",  BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
338     {"^",  BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
339     {"&",  BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
340     {"==", BINOP_EQUAL, PREC_EQUAL, 0},
341     {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
342     {"<=", BINOP_LEQ, PREC_ORDER, 0},
343     {">=", BINOP_GEQ, PREC_ORDER, 0},
344     {">",  BINOP_GTR, PREC_ORDER, 0},
345     {"<",  BINOP_LESS, PREC_ORDER, 0},
346     {">>", BINOP_RSH, PREC_SHIFT, 0},
347     {"<<", BINOP_LSH, PREC_SHIFT, 0},
348     {"+",  BINOP_ADD, PREC_ADD, 0},
349     {"-",  BINOP_SUB, PREC_ADD, 0},
350     {"*",  BINOP_MUL, PREC_MUL, 0},
351     {"/",  BINOP_DIV, PREC_MUL, 0},
352     {"%",  BINOP_REM, PREC_MUL, 0},
353     {"@",  BINOP_REPEAT, PREC_REPEAT, 0},
354     {"-",  UNOP_NEG, PREC_PREFIX, 0},
355     {"!",  UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
356     {"~",  UNOP_COMPLEMENT, PREC_PREFIX, 0},
357     {"*",  UNOP_IND, PREC_PREFIX, 0},
358     {"&",  UNOP_ADDR, PREC_PREFIX, 0},
359     {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
360     {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
361     {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
362     /* C++  */
363     {"::", BINOP_SCOPE, PREC_PREFIX, 0},
364     {NULL, 0, 0, 0}
365 };
366 \f
367 /* These variables point to the objects
368    representing the predefined C data types.  */
369
370 struct type *builtin_type_void;
371 struct type *builtin_type_char;
372 struct type *builtin_type_short;
373 struct type *builtin_type_int;
374 struct type *builtin_type_long;
375 struct type *builtin_type_long_long;
376 struct type *builtin_type_signed_char;
377 struct type *builtin_type_unsigned_char;
378 struct type *builtin_type_unsigned_short;
379 struct type *builtin_type_unsigned_int;
380 struct type *builtin_type_unsigned_long;
381 struct type *builtin_type_unsigned_long_long;
382 struct type *builtin_type_float;
383 struct type *builtin_type_double;
384 struct type *builtin_type_long_double;
385 struct type *builtin_type_complex;
386 struct type *builtin_type_double_complex;
387
388 struct type ** const (c_builtin_types[]) = 
389 {
390   &builtin_type_int,
391   &builtin_type_long,
392   &builtin_type_short,
393   &builtin_type_char,
394   &builtin_type_float,
395   &builtin_type_double,
396   &builtin_type_void,
397   &builtin_type_long_long,
398   &builtin_type_signed_char,
399   &builtin_type_unsigned_char,
400   &builtin_type_unsigned_short,
401   &builtin_type_unsigned_int,
402   &builtin_type_unsigned_long,
403   &builtin_type_unsigned_long_long,
404   &builtin_type_long_double,
405   &builtin_type_complex,
406   &builtin_type_double_complex,
407   0
408 };
409
410 const struct language_defn c_language_defn = {
411   "c",                          /* Language name */
412   language_c,
413   c_builtin_types,
414   range_check_off,
415   type_check_off,
416   c_parse,
417   c_error,
418   c_printchar,                  /* Print a character constant */
419   c_printstr,                   /* Function to print string constant */
420   c_create_fundamental_type,    /* Create fundamental type in this language */
421   &BUILTIN_TYPE_LONGEST,        /* longest signed   integral type */
422   &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
423   &builtin_type_double,         /* longest floating point type */ /*FIXME*/
424   {"",     "",    "",  ""},     /* Binary format info */
425   {"0%o",  "0",   "o", ""},     /* Octal format info */
426   {"%d",   "",    "d", ""},     /* Decimal format info */
427   {"0x%x", "0x",  "x", ""},     /* Hex format info */
428   c_op_print_tab,               /* expression operators for printing */
429   LANG_MAGIC
430 };
431
432 const struct language_defn cplus_language_defn = {
433   "c++",                                /* Language name */
434   language_cplus,
435   c_builtin_types,
436   range_check_off,
437   type_check_off,
438   c_parse,
439   c_error,
440   c_printchar,                  /* Print a character constant */
441   c_printstr,                   /* Function to print string constant */
442   c_create_fundamental_type,    /* Create fundamental type in this language */
443   &BUILTIN_TYPE_LONGEST,         /* longest signed   integral type */
444   &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
445   &builtin_type_double,         /* longest floating point type */ /*FIXME*/
446   {"",      "",    "",   ""},   /* Binary format info */
447   {"0%o",   "0",   "o",  ""},   /* Octal format info */
448   {"%d",    "",    "d",  ""},   /* Decimal format info */
449   {"0x%x",  "0x",  "x",  ""},   /* Hex format info */
450   c_op_print_tab,               /* expression operators for printing */
451   LANG_MAGIC
452 };
453
454 void
455 _initialize_c_exp ()
456 {
457   builtin_type_void =
458     init_type (TYPE_CODE_VOID, 1,
459                0,
460                "void", (struct objfile *) NULL);
461   builtin_type_char =
462     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
463                0,
464                "char", (struct objfile *) NULL);
465   builtin_type_signed_char =
466     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
467                TYPE_FLAG_SIGNED,
468                "signed char", (struct objfile *) NULL);
469   builtin_type_unsigned_char =
470     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
471                TYPE_FLAG_UNSIGNED,
472                "unsigned char", (struct objfile *) NULL);
473   builtin_type_short =
474     init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
475                0,
476                "short", (struct objfile *) NULL);
477   builtin_type_unsigned_short =
478     init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
479                TYPE_FLAG_UNSIGNED,
480                "unsigned short", (struct objfile *) NULL);
481   builtin_type_int =
482     init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
483                0,
484                "int", (struct objfile *) NULL);
485   builtin_type_unsigned_int =
486     init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
487                TYPE_FLAG_UNSIGNED,
488                "unsigned int", (struct objfile *) NULL);
489   builtin_type_long =
490     init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
491                0,
492                "long", (struct objfile *) NULL);
493   builtin_type_unsigned_long =
494     init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
495                TYPE_FLAG_UNSIGNED,
496                "unsigned long", (struct objfile *) NULL);
497   builtin_type_long_long =
498     init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
499                0,
500                "long long", (struct objfile *) NULL);
501   builtin_type_unsigned_long_long = 
502     init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
503                TYPE_FLAG_UNSIGNED,
504                "unsigned long long", (struct objfile *) NULL);
505   builtin_type_float =
506     init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
507                0,
508                "float", (struct objfile *) NULL);
509   builtin_type_double =
510     init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
511                0,
512                "double", (struct objfile *) NULL);
513   builtin_type_long_double =
514     init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
515                0,
516                "long double", (struct objfile *) NULL);
517   builtin_type_complex =
518     init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
519                0,
520                "complex", (struct objfile *) NULL);
521   builtin_type_double_complex =
522     init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
523                0,
524                "double complex", (struct objfile *) NULL);
525
526   add_language (&c_language_defn);
527   add_language (&cplus_language_defn);
528 }