expression.h: New ops OP_NSSTRING, OP_SELECTOR, OP_MSGCALL, and OP_SELF.
[deliverable/binutils-gdb.git] / gdb / c-lang.c
1 /* C language support routines for GDB, the GNU debugger.
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "parser-defs.h"
27 #include "language.h"
28 #include "c-lang.h"
29 #include "valprint.h"
30 #include "macroscope.h"
31 #include "gdb_assert.h"
32 #include "charset.h"
33 #include "gdb_string.h"
34
35 extern void _initialize_c_language (void);
36 static void c_emit_char (int c, struct ui_file * stream, int quoter);
37
38 /* Print the character C on STREAM as part of the contents of a literal
39 string whose delimiter is QUOTER. Note that that format for printing
40 characters and strings is language specific. */
41
42 static void
43 c_emit_char (register int c, struct ui_file *stream, int quoter)
44 {
45 const char *escape;
46 int host_char;
47
48 c &= 0xFF; /* Avoid sign bit follies */
49
50 escape = c_target_char_has_backslash_escape (c);
51 if (escape)
52 {
53 if (quoter == '"' && strcmp (escape, "0") == 0)
54 /* Print nulls embedded in double quoted strings as \000 to
55 prevent ambiguity. */
56 fprintf_filtered (stream, "\\000");
57 else
58 fprintf_filtered (stream, "\\%s", escape);
59 }
60 else if (target_char_to_host (c, &host_char)
61 && host_char_print_literally (host_char))
62 {
63 if (host_char == '\\' || host_char == quoter)
64 fputs_filtered ("\\", stream);
65 fprintf_filtered (stream, "%c", host_char);
66 }
67 else
68 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
69 }
70
71 void
72 c_printchar (int c, struct ui_file *stream)
73 {
74 fputc_filtered ('\'', stream);
75 LA_EMIT_CHAR (c, stream, '\'');
76 fputc_filtered ('\'', stream);
77 }
78
79 /* Print the character string STRING, printing at most LENGTH characters.
80 LENGTH is -1 if the string is nul terminated. Each character is WIDTH bytes
81 long. Printing stops early if the number hits print_max; repeat counts are
82 printed as appropriate. Print ellipses at the end if we had to stop before
83 printing LENGTH characters, or if FORCE_ELLIPSES. */
84
85 void
86 c_printstr (struct ui_file *stream, char *string, unsigned int length,
87 int width, int force_ellipses)
88 {
89 register unsigned int i;
90 unsigned int things_printed = 0;
91 int in_quotes = 0;
92 int need_comma = 0;
93 extern int inspect_it;
94
95 /* If the string was not truncated due to `set print elements', and
96 the last byte of it is a null, we don't print that, in traditional C
97 style. */
98 if (!force_ellipses
99 && length > 0
100 && (extract_unsigned_integer (string + (length - 1) * width, width)
101 == '\0'))
102 length--;
103
104 if (length == 0)
105 {
106 fputs_filtered ("\"\"", stream);
107 return;
108 }
109
110 for (i = 0; i < length && things_printed < print_max; ++i)
111 {
112 /* Position of the character we are examining
113 to see whether it is repeated. */
114 unsigned int rep1;
115 /* Number of repetitions we have detected so far. */
116 unsigned int reps;
117 unsigned long current_char;
118
119 QUIT;
120
121 if (need_comma)
122 {
123 fputs_filtered (", ", stream);
124 need_comma = 0;
125 }
126
127 current_char = extract_unsigned_integer (string + i * width, width);
128
129 rep1 = i + 1;
130 reps = 1;
131 while (rep1 < length
132 && extract_unsigned_integer (string + rep1 * width, width)
133 == current_char)
134 {
135 ++rep1;
136 ++reps;
137 }
138
139 if (reps > repeat_count_threshold)
140 {
141 if (in_quotes)
142 {
143 if (inspect_it)
144 fputs_filtered ("\\\", ", stream);
145 else
146 fputs_filtered ("\", ", stream);
147 in_quotes = 0;
148 }
149 LA_PRINT_CHAR (current_char, stream);
150 fprintf_filtered (stream, " <repeats %u times>", reps);
151 i = rep1 - 1;
152 things_printed += repeat_count_threshold;
153 need_comma = 1;
154 }
155 else
156 {
157 if (!in_quotes)
158 {
159 if (inspect_it)
160 fputs_filtered ("\\\"", stream);
161 else
162 fputs_filtered ("\"", stream);
163 in_quotes = 1;
164 }
165 LA_EMIT_CHAR (current_char, stream, '"');
166 ++things_printed;
167 }
168 }
169
170 /* Terminate the quotes if necessary. */
171 if (in_quotes)
172 {
173 if (inspect_it)
174 fputs_filtered ("\\\"", stream);
175 else
176 fputs_filtered ("\"", stream);
177 }
178
179 if (force_ellipses || i < length)
180 fputs_filtered ("...", stream);
181 }
182
183 /* Create a fundamental C type using default reasonable for the current
184 target machine.
185
186 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
187 define fundamental types such as "int" or "double". Others (stabs or
188 DWARF version 2, etc) do define fundamental types. For the formats which
189 don't provide fundamental types, gdb can create such types using this
190 function.
191
192 FIXME: Some compilers distinguish explicitly signed integral types
193 (signed short, signed int, signed long) from "regular" integral types
194 (short, int, long) in the debugging information. There is some dis-
195 agreement as to how useful this feature is. In particular, gcc does
196 not support this. Also, only some debugging formats allow the
197 distinction to be passed on to a debugger. For now, we always just
198 use "short", "int", or "long" as the type name, for both the implicit
199 and explicitly signed types. This also makes life easier for the
200 gdb test suite since we don't have to account for the differences
201 in output depending upon what the compiler and debugging format
202 support. We will probably have to re-examine the issue when gdb
203 starts taking it's fundamental type information directly from the
204 debugging information supplied by the compiler. fnf@cygnus.com */
205
206 struct type *
207 c_create_fundamental_type (struct objfile *objfile, int typeid)
208 {
209 register struct type *type = NULL;
210
211 switch (typeid)
212 {
213 default:
214 /* FIXME: For now, if we are asked to produce a type not in this
215 language, create the equivalent of a C integer type with the
216 name "<?type?>". When all the dust settles from the type
217 reconstruction work, this should probably become an error. */
218 type = init_type (TYPE_CODE_INT,
219 TARGET_INT_BIT / TARGET_CHAR_BIT,
220 0, "<?type?>", objfile);
221 warning ("internal error: no C/C++ fundamental type %d", typeid);
222 break;
223 case FT_VOID:
224 type = init_type (TYPE_CODE_VOID,
225 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
226 0, "void", objfile);
227 break;
228 case FT_BOOLEAN:
229 type = init_type (TYPE_CODE_BOOL,
230 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
231 0, "bool", objfile);
232 break;
233 case FT_CHAR:
234 type = init_type (TYPE_CODE_INT,
235 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
236 TYPE_FLAG_NOSIGN, "char", objfile);
237 break;
238 case FT_SIGNED_CHAR:
239 type = init_type (TYPE_CODE_INT,
240 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
241 0, "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 0, "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 0, "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 0, "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 0, "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 case FT_COMPLEX:
324 type = init_type (TYPE_CODE_FLT,
325 2 * TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
326 0, "complex float", objfile);
327 TYPE_TARGET_TYPE (type)
328 = init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
329 0, "float", objfile);
330 break;
331 case FT_DBL_PREC_COMPLEX:
332 type = init_type (TYPE_CODE_FLT,
333 2 * TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
334 0, "complex double", objfile);
335 TYPE_TARGET_TYPE (type)
336 = init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
337 0, "double", objfile);
338 break;
339 case FT_EXT_PREC_COMPLEX:
340 type = init_type (TYPE_CODE_FLT,
341 2 * TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
342 0, "complex long double", objfile);
343 TYPE_TARGET_TYPE (type)
344 = init_type (TYPE_CODE_FLT, 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 break;
352 }
353 return (type);
354 }
355 \f
356 /* Preprocessing and parsing C and C++ expressions. */
357
358
359 /* When we find that lexptr (the global var defined in parse.c) is
360 pointing at a macro invocation, we expand the invocation, and call
361 scan_macro_expansion to save the old lexptr here and point lexptr
362 into the expanded text. When we reach the end of that, we call
363 end_macro_expansion to pop back to the value we saved here. The
364 macro expansion code promises to return only fully-expanded text,
365 so we don't need to "push" more than one level.
366
367 This is disgusting, of course. It would be cleaner to do all macro
368 expansion beforehand, and then hand that to lexptr. But we don't
369 really know where the expression ends. Remember, in a command like
370
371 (gdb) break *ADDRESS if CONDITION
372
373 we evaluate ADDRESS in the scope of the current frame, but we
374 evaluate CONDITION in the scope of the breakpoint's location. So
375 it's simply wrong to try to macro-expand the whole thing at once. */
376 static char *macro_original_text;
377 static char *macro_expanded_text;
378
379
380 void
381 scan_macro_expansion (char *expansion)
382 {
383 /* We'd better not be trying to push the stack twice. */
384 gdb_assert (! macro_original_text);
385 gdb_assert (! macro_expanded_text);
386
387 /* Save the old lexptr value, so we can return to it when we're done
388 parsing the expanded text. */
389 macro_original_text = lexptr;
390 lexptr = expansion;
391
392 /* Save the expanded text, so we can free it when we're finished. */
393 macro_expanded_text = expansion;
394 }
395
396
397 int
398 scanning_macro_expansion (void)
399 {
400 return macro_original_text != 0;
401 }
402
403
404 void
405 finished_macro_expansion (void)
406 {
407 /* There'd better be something to pop back to, and we better have
408 saved a pointer to the start of the expanded text. */
409 gdb_assert (macro_original_text);
410 gdb_assert (macro_expanded_text);
411
412 /* Pop back to the original text. */
413 lexptr = macro_original_text;
414 macro_original_text = 0;
415
416 /* Free the expanded text. */
417 xfree (macro_expanded_text);
418 macro_expanded_text = 0;
419 }
420
421
422 static void
423 scan_macro_cleanup (void *dummy)
424 {
425 if (macro_original_text)
426 finished_macro_expansion ();
427 }
428
429
430 /* We set these global variables before calling c_parse, to tell it
431 how it to find macro definitions for the expression at hand. */
432 macro_lookup_ftype *expression_macro_lookup_func;
433 void *expression_macro_lookup_baton;
434
435
436 static struct macro_definition *
437 null_macro_lookup (const char *name, void *baton)
438 {
439 return 0;
440 }
441
442
443 static int
444 c_preprocess_and_parse (void)
445 {
446 /* Set up a lookup function for the macro expander. */
447 struct macro_scope *scope = 0;
448 struct cleanup *back_to = make_cleanup (free_current_contents, &scope);
449
450 if (expression_context_block)
451 scope = sal_macro_scope (find_pc_line (expression_context_pc, 0));
452 else
453 scope = default_macro_scope ();
454
455 if (scope)
456 {
457 expression_macro_lookup_func = standard_macro_lookup;
458 expression_macro_lookup_baton = (void *) scope;
459 }
460 else
461 {
462 expression_macro_lookup_func = null_macro_lookup;
463 expression_macro_lookup_baton = 0;
464 }
465
466 gdb_assert (! macro_original_text);
467 make_cleanup (scan_macro_cleanup, 0);
468
469 {
470 int result = c_parse ();
471 do_cleanups (back_to);
472 return result;
473 }
474 }
475
476
477 \f
478 /* Table mapping opcodes into strings for printing operators
479 and precedences of the operators. */
480
481 const struct op_print c_op_print_tab[] =
482 {
483 {",", BINOP_COMMA, PREC_COMMA, 0},
484 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
485 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
486 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
487 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
488 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
489 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
490 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
491 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
492 {"<=", BINOP_LEQ, PREC_ORDER, 0},
493 {">=", BINOP_GEQ, PREC_ORDER, 0},
494 {">", BINOP_GTR, PREC_ORDER, 0},
495 {"<", BINOP_LESS, PREC_ORDER, 0},
496 {">>", BINOP_RSH, PREC_SHIFT, 0},
497 {"<<", BINOP_LSH, PREC_SHIFT, 0},
498 {"+", BINOP_ADD, PREC_ADD, 0},
499 {"-", BINOP_SUB, PREC_ADD, 0},
500 {"*", BINOP_MUL, PREC_MUL, 0},
501 {"/", BINOP_DIV, PREC_MUL, 0},
502 {"%", BINOP_REM, PREC_MUL, 0},
503 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
504 {"-", UNOP_NEG, PREC_PREFIX, 0},
505 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
506 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
507 {"*", UNOP_IND, PREC_PREFIX, 0},
508 {"&", UNOP_ADDR, PREC_PREFIX, 0},
509 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
510 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
511 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
512 {NULL, 0, 0, 0}
513 };
514 \f
515 struct type **const (c_builtin_types[]) =
516 {
517 &builtin_type_int,
518 &builtin_type_long,
519 &builtin_type_short,
520 &builtin_type_char,
521 &builtin_type_float,
522 &builtin_type_double,
523 &builtin_type_void,
524 &builtin_type_long_long,
525 &builtin_type_signed_char,
526 &builtin_type_unsigned_char,
527 &builtin_type_unsigned_short,
528 &builtin_type_unsigned_int,
529 &builtin_type_unsigned_long,
530 &builtin_type_unsigned_long_long,
531 &builtin_type_long_double,
532 &builtin_type_complex,
533 &builtin_type_double_complex,
534 0
535 };
536
537 const struct language_defn c_language_defn =
538 {
539 "c", /* Language name */
540 language_c,
541 c_builtin_types,
542 range_check_off,
543 type_check_off,
544 case_sensitive_on,
545 c_preprocess_and_parse,
546 c_error,
547 evaluate_subexp_standard,
548 c_printchar, /* Print a character constant */
549 c_printstr, /* Function to print string constant */
550 c_emit_char, /* Print a single char */
551 c_create_fundamental_type, /* Create fundamental type in this language */
552 c_print_type, /* Print a type using appropriate syntax */
553 c_val_print, /* Print a value using appropriate syntax */
554 c_value_print, /* Print a top-level value */
555 {"", "", "", ""}, /* Binary format info */
556 {"0%lo", "0", "o", ""}, /* Octal format info */
557 {"%ld", "", "d", ""}, /* Decimal format info */
558 {"0x%lx", "0x", "x", ""}, /* Hex format info */
559 c_op_print_tab, /* expression operators for printing */
560 1, /* c-style arrays */
561 0, /* String lower bound */
562 &builtin_type_char, /* Type of string elements */
563 LANG_MAGIC
564 };
565
566 struct type **const (cplus_builtin_types[]) =
567 {
568 &builtin_type_int,
569 &builtin_type_long,
570 &builtin_type_short,
571 &builtin_type_char,
572 &builtin_type_float,
573 &builtin_type_double,
574 &builtin_type_void,
575 &builtin_type_long_long,
576 &builtin_type_signed_char,
577 &builtin_type_unsigned_char,
578 &builtin_type_unsigned_short,
579 &builtin_type_unsigned_int,
580 &builtin_type_unsigned_long,
581 &builtin_type_unsigned_long_long,
582 &builtin_type_long_double,
583 &builtin_type_complex,
584 &builtin_type_double_complex,
585 &builtin_type_bool,
586 0
587 };
588
589 const struct language_defn cplus_language_defn =
590 {
591 "c++", /* Language name */
592 language_cplus,
593 cplus_builtin_types,
594 range_check_off,
595 type_check_off,
596 case_sensitive_on,
597 c_preprocess_and_parse,
598 c_error,
599 evaluate_subexp_standard,
600 c_printchar, /* Print a character constant */
601 c_printstr, /* Function to print string constant */
602 c_emit_char, /* Print a single char */
603 c_create_fundamental_type, /* Create fundamental type in this language */
604 c_print_type, /* Print a type using appropriate syntax */
605 c_val_print, /* Print a value using appropriate syntax */
606 c_value_print, /* Print a top-level value */
607 {"", "", "", ""}, /* Binary format info */
608 {"0%lo", "0", "o", ""}, /* Octal format info */
609 {"%ld", "", "d", ""}, /* Decimal format info */
610 {"0x%lx", "0x", "x", ""}, /* Hex format info */
611 c_op_print_tab, /* expression operators for printing */
612 1, /* c-style arrays */
613 0, /* String lower bound */
614 &builtin_type_char, /* Type of string elements */
615 LANG_MAGIC
616 };
617
618 const struct language_defn asm_language_defn =
619 {
620 "asm", /* Language name */
621 language_asm,
622 c_builtin_types,
623 range_check_off,
624 type_check_off,
625 case_sensitive_on,
626 c_preprocess_and_parse,
627 c_error,
628 evaluate_subexp_standard,
629 c_printchar, /* Print a character constant */
630 c_printstr, /* Function to print string constant */
631 c_emit_char, /* Print a single char */
632 c_create_fundamental_type, /* Create fundamental type in this language */
633 c_print_type, /* Print a type using appropriate syntax */
634 c_val_print, /* Print a value using appropriate syntax */
635 c_value_print, /* Print a top-level value */
636 {"", "", "", ""}, /* Binary format info */
637 {"0%lo", "0", "o", ""}, /* Octal format info */
638 {"%ld", "", "d", ""}, /* Decimal format info */
639 {"0x%lx", "0x", "x", ""}, /* Hex format info */
640 c_op_print_tab, /* expression operators for printing */
641 1, /* c-style arrays */
642 0, /* String lower bound */
643 &builtin_type_char, /* Type of string elements */
644 LANG_MAGIC
645 };
646
647 void
648 _initialize_c_language (void)
649 {
650 add_language (&c_language_defn);
651 add_language (&cplus_language_defn);
652 add_language (&asm_language_defn);
653 }
This page took 0.055712 seconds and 4 git commands to generate.