* scripttempl/mipsbsd.sc: Let sections align to their natural
[deliverable/binutils-gdb.git] / gdb / c-lang.c
CommitLineData
22e39759 1/* C language support routines for GDB, the GNU debugger.
c5c00171 2 Copyright 1992, 1993 Free Software Foundation, Inc.
22e39759
FF
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, 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
32static void
33emit_char (c, stream, quoter)
34 register int c;
199b2450 35 GDB_FILE *stream;
22e39759
FF
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
81static void
82c_printchar (c, stream)
83 int c;
199b2450 84 GDB_FILE *stream;
22e39759
FF
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
96static void
97c_printstr (stream, string, length, force_ellipses)
199b2450 98 GDB_FILE *stream;
22e39759
FF
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
c5c00171
JG
111 /* If the string was not truncated due to `set print elements', and
112 the last byte of it is a null, we don't print that, in traditional C
113 style. */
961ccde6 114 if ((!force_ellipses) && length > 0 && string[length-1] == '\0')
c5c00171
JG
115 length--;
116
22e39759
FF
117 if (length == 0)
118 {
55768580 119 fputs_filtered ("\"\"", stream);
22e39759
FF
120 return;
121 }
122
123 for (i = 0; i < length && things_printed < print_max; ++i)
124 {
125 /* Position of the character we are examining
126 to see whether it is repeated. */
127 unsigned int rep1;
128 /* Number of repetitions we have detected so far. */
129 unsigned int reps;
130
131 QUIT;
132
133 if (need_comma)
134 {
135 fputs_filtered (", ", stream);
136 need_comma = 0;
137 }
138
139 rep1 = i + 1;
140 reps = 1;
141 while (rep1 < length && string[rep1] == string[i])
142 {
143 ++rep1;
144 ++reps;
145 }
146
147 if (reps > repeat_count_threshold)
148 {
149 if (in_quotes)
150 {
151 if (inspect_it)
152 fputs_filtered ("\\\", ", stream);
153 else
154 fputs_filtered ("\", ", stream);
155 in_quotes = 0;
156 }
157 c_printchar (string[i], stream);
158 fprintf_filtered (stream, " <repeats %u times>", reps);
159 i = rep1 - 1;
160 things_printed += repeat_count_threshold;
161 need_comma = 1;
162 }
163 else
164 {
165 if (!in_quotes)
166 {
167 if (inspect_it)
168 fputs_filtered ("\\\"", stream);
169 else
170 fputs_filtered ("\"", stream);
171 in_quotes = 1;
172 }
173 emit_char (string[i], stream, '"');
174 ++things_printed;
175 }
176 }
177
178 /* Terminate the quotes if necessary. */
179 if (in_quotes)
180 {
181 if (inspect_it)
182 fputs_filtered ("\\\"", stream);
183 else
184 fputs_filtered ("\"", stream);
185 }
186
187 if (force_ellipses || i < length)
188 fputs_filtered ("...", stream);
189}
190
191/* Create a fundamental C type using default reasonable for the current
192 target machine.
193
194 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
195 define fundamental types such as "int" or "double". Others (stabs or
196 DWARF version 2, etc) do define fundamental types. For the formats which
197 don't provide fundamental types, gdb can create such types using this
198 function.
199
200 FIXME: Some compilers distinguish explicitly signed integral types
201 (signed short, signed int, signed long) from "regular" integral types
202 (short, int, long) in the debugging information. There is some dis-
203 agreement as to how useful this feature is. In particular, gcc does
204 not support this. Also, only some debugging formats allow the
205 distinction to be passed on to a debugger. For now, we always just
206 use "short", "int", or "long" as the type name, for both the implicit
207 and explicitly signed types. This also makes life easier for the
208 gdb test suite since we don't have to account for the differences
209 in output depending upon what the compiler and debugging format
210 support. We will probably have to re-examine the issue when gdb
211 starts taking it's fundamental type information directly from the
212 debugging information supplied by the compiler. fnf@cygnus.com */
213
214static struct type *
215c_create_fundamental_type (objfile, typeid)
216 struct objfile *objfile;
217 int typeid;
218{
219 register struct type *type = NULL;
22e39759
FF
220
221 switch (typeid)
222 {
223 default:
224 /* FIXME: For now, if we are asked to produce a type not in this
225 language, create the equivalent of a C integer type with the
226 name "<?type?>". When all the dust settles from the type
227 reconstruction work, this should probably become an error. */
228 type = init_type (TYPE_CODE_INT,
229 TARGET_INT_BIT / TARGET_CHAR_BIT,
230 0, "<?type?>", objfile);
231 warning ("internal error: no C/C++ fundamental type %d", typeid);
232 break;
233 case FT_VOID:
234 type = init_type (TYPE_CODE_VOID,
235 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
236 0, "void", objfile);
237 break;
238 case FT_CHAR:
239 type = init_type (TYPE_CODE_INT,
240 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
241 0, "char", objfile);
242 break;
243 case FT_SIGNED_CHAR:
244 type = init_type (TYPE_CODE_INT,
245 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
dda398c3 246 0, "signed char", objfile);
22e39759
FF
247 break;
248 case FT_UNSIGNED_CHAR:
249 type = init_type (TYPE_CODE_INT,
250 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
251 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
252 break;
253 case FT_SHORT:
254 type = init_type (TYPE_CODE_INT,
255 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
256 0, "short", objfile);
257 break;
258 case FT_SIGNED_SHORT:
259 type = init_type (TYPE_CODE_INT,
260 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
dda398c3 261 0, "short", objfile); /* FIXME-fnf */
22e39759
FF
262 break;
263 case FT_UNSIGNED_SHORT:
264 type = init_type (TYPE_CODE_INT,
265 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
266 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
267 break;
268 case FT_INTEGER:
269 type = init_type (TYPE_CODE_INT,
270 TARGET_INT_BIT / TARGET_CHAR_BIT,
271 0, "int", objfile);
272 break;
273 case FT_SIGNED_INTEGER:
274 type = init_type (TYPE_CODE_INT,
275 TARGET_INT_BIT / TARGET_CHAR_BIT,
dda398c3 276 0, "int", objfile); /* FIXME -fnf */
22e39759
FF
277 break;
278 case FT_UNSIGNED_INTEGER:
279 type = init_type (TYPE_CODE_INT,
280 TARGET_INT_BIT / TARGET_CHAR_BIT,
281 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
282 break;
283 case FT_LONG:
284 type = init_type (TYPE_CODE_INT,
285 TARGET_LONG_BIT / TARGET_CHAR_BIT,
286 0, "long", objfile);
287 break;
288 case FT_SIGNED_LONG:
289 type = init_type (TYPE_CODE_INT,
290 TARGET_LONG_BIT / TARGET_CHAR_BIT,
dda398c3 291 0, "long", objfile); /* FIXME -fnf */
22e39759
FF
292 break;
293 case FT_UNSIGNED_LONG:
294 type = init_type (TYPE_CODE_INT,
295 TARGET_LONG_BIT / TARGET_CHAR_BIT,
296 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
297 break;
298 case FT_LONG_LONG:
299 type = init_type (TYPE_CODE_INT,
300 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
301 0, "long long", objfile);
302 break;
303 case FT_SIGNED_LONG_LONG:
304 type = init_type (TYPE_CODE_INT,
305 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
dda398c3 306 0, "signed long long", objfile);
22e39759
FF
307 break;
308 case FT_UNSIGNED_LONG_LONG:
309 type = init_type (TYPE_CODE_INT,
310 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
311 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
312 break;
313 case FT_FLOAT:
314 type = init_type (TYPE_CODE_FLT,
315 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
316 0, "float", objfile);
317 break;
318 case FT_DBL_PREC_FLOAT:
319 type = init_type (TYPE_CODE_FLT,
320 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
321 0, "double", objfile);
322 break;
323 case FT_EXT_PREC_FLOAT:
324 type = init_type (TYPE_CODE_FLT,
325 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
326 0, "long double", objfile);
327 break;
328 }
329 return (type);
330}
331
332\f
333/* Table mapping opcodes into strings for printing operators
334 and precedences of the operators. */
335
a8a69e63 336static const struct op_print c_op_print_tab[] =
22e39759
FF
337 {
338 {",", BINOP_COMMA, PREC_COMMA, 0},
339 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
340 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
341 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
342 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
343 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
344 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
345 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
346 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
347 {"<=", BINOP_LEQ, PREC_ORDER, 0},
348 {">=", BINOP_GEQ, PREC_ORDER, 0},
349 {">", BINOP_GTR, PREC_ORDER, 0},
350 {"<", BINOP_LESS, PREC_ORDER, 0},
351 {">>", BINOP_RSH, PREC_SHIFT, 0},
352 {"<<", BINOP_LSH, PREC_SHIFT, 0},
353 {"+", BINOP_ADD, PREC_ADD, 0},
354 {"-", BINOP_SUB, PREC_ADD, 0},
355 {"*", BINOP_MUL, PREC_MUL, 0},
356 {"/", BINOP_DIV, PREC_MUL, 0},
357 {"%", BINOP_REM, PREC_MUL, 0},
358 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
359 {"-", UNOP_NEG, PREC_PREFIX, 0},
360 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
361 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
362 {"*", UNOP_IND, PREC_PREFIX, 0},
363 {"&", UNOP_ADDR, PREC_PREFIX, 0},
364 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
365 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
366 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
367 /* C++ */
368 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
369 {NULL, 0, 0, 0}
370};
371\f
22e39759
FF
372struct type ** const (c_builtin_types[]) =
373{
374 &builtin_type_int,
375 &builtin_type_long,
376 &builtin_type_short,
377 &builtin_type_char,
378 &builtin_type_float,
379 &builtin_type_double,
380 &builtin_type_void,
381 &builtin_type_long_long,
382 &builtin_type_signed_char,
383 &builtin_type_unsigned_char,
384 &builtin_type_unsigned_short,
385 &builtin_type_unsigned_int,
386 &builtin_type_unsigned_long,
387 &builtin_type_unsigned_long_long,
388 &builtin_type_long_double,
389 &builtin_type_complex,
390 &builtin_type_double_complex,
391 0
392};
393
394const struct language_defn c_language_defn = {
395 "c", /* Language name */
396 language_c,
397 c_builtin_types,
398 range_check_off,
399 type_check_off,
400 c_parse,
401 c_error,
402 c_printchar, /* Print a character constant */
403 c_printstr, /* Function to print string constant */
404 c_create_fundamental_type, /* Create fundamental type in this language */
a8a69e63
FF
405 c_print_type, /* Print a type using appropriate syntax */
406 c_val_print, /* Print a value using appropriate syntax */
e10cfcaa 407 c_value_print, /* Print a top-level value */
22e39759
FF
408 &builtin_type_double, /* longest floating point type */ /*FIXME*/
409 {"", "", "", ""}, /* Binary format info */
5573d7d4
JK
410 {"0%lo", "0", "o", ""}, /* Octal format info */
411 {"%ld", "", "d", ""}, /* Decimal format info */
412 {"0x%lx", "0x", "x", ""}, /* Hex format info */
22e39759
FF
413 c_op_print_tab, /* expression operators for printing */
414 LANG_MAGIC
415};
416
417const struct language_defn cplus_language_defn = {
418 "c++", /* Language name */
419 language_cplus,
420 c_builtin_types,
421 range_check_off,
422 type_check_off,
423 c_parse,
424 c_error,
425 c_printchar, /* Print a character constant */
426 c_printstr, /* Function to print string constant */
427 c_create_fundamental_type, /* Create fundamental type in this language */
a8a69e63
FF
428 c_print_type, /* Print a type using appropriate syntax */
429 c_val_print, /* Print a value using appropriate syntax */
e10cfcaa 430 c_value_print, /* Print a top-level value */
22e39759
FF
431 &builtin_type_double, /* longest floating point type */ /*FIXME*/
432 {"", "", "", ""}, /* Binary format info */
5573d7d4
JK
433 {"0%lo", "0", "o", ""}, /* Octal format info */
434 {"%ld", "", "d", ""}, /* Decimal format info */
435 {"0x%lx", "0x", "x", ""}, /* Hex format info */
22e39759
FF
436 c_op_print_tab, /* expression operators for printing */
437 LANG_MAGIC
438};
439
2c068010
PS
440const struct language_defn asm_language_defn = {
441 "asm", /* Language name */
442 language_asm,
443 c_builtin_types,
444 range_check_off,
445 type_check_off,
446 c_parse,
447 c_error,
448 c_printchar, /* Print a character constant */
449 c_printstr, /* Function to print string constant */
450 c_create_fundamental_type, /* Create fundamental type in this language */
451 c_print_type, /* Print a type using appropriate syntax */
452 c_val_print, /* Print a value using appropriate syntax */
453 c_value_print, /* Print a top-level value */
454 &builtin_type_double, /* longest floating point type */ /*FIXME*/
455 {"", "", "", ""}, /* Binary format info */
456 {"0%lo", "0", "o", ""}, /* Octal format info */
457 {"%ld", "", "d", ""}, /* Decimal format info */
458 {"0x%lx", "0x", "x", ""}, /* Hex format info */
459 c_op_print_tab, /* expression operators for printing */
460 LANG_MAGIC
461};
462
22e39759 463void
d62e7a20 464_initialize_c_language ()
22e39759 465{
22e39759
FF
466 add_language (&c_language_defn);
467 add_language (&cplus_language_defn);
2c068010 468 add_language (&asm_language_defn);
22e39759 469}
This page took 0.114054 seconds and 4 git commands to generate.