gdb
[deliverable/binutils-gdb.git] / gdb / language.c
1 /* Multiple source language support for GDB.
2
3 Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 Contributed by the Department of Computer Science at the State University
7 of New York at Buffalo.
8
9 This file is part of GDB.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23
24 /* This file contains functions that return things that are specific
25 to languages. Each function should examine current_language if necessary,
26 and return the appropriate result. */
27
28 /* FIXME: Most of these would be better organized as macros which
29 return data out of a "language-specific" struct pointer that is set
30 whenever the working language changes. That would be a lot faster. */
31
32 #include "defs.h"
33 #include <ctype.h>
34 #include "gdb_string.h"
35
36 #include "symtab.h"
37 #include "gdbtypes.h"
38 #include "value.h"
39 #include "gdbcmd.h"
40 #include "expression.h"
41 #include "language.h"
42 #include "target.h"
43 #include "parser-defs.h"
44 #include "jv-lang.h"
45 #include "demangle.h"
46 #include "symfile.h"
47
48 extern void _initialize_language (void);
49
50 static void set_case_str (void);
51
52 static void set_range_str (void);
53
54 static void set_type_str (void);
55
56 static void set_lang_str (void);
57
58 static void unk_lang_error (char *);
59
60 static int unk_lang_parser (void);
61
62 static void show_check (char *, int);
63
64 static void set_check (char *, int);
65
66 static void set_type_range_case (void);
67
68 static void unk_lang_emit_char (int c, struct type *type,
69 struct ui_file *stream, int quoter);
70
71 static void unk_lang_printchar (int c, struct type *type,
72 struct ui_file *stream);
73
74 static void unk_lang_print_type (struct type *, char *, struct ui_file *,
75 int, int);
76
77 static int unk_lang_value_print (struct value *, struct ui_file *,
78 const struct value_print_options *);
79
80 static CORE_ADDR unk_lang_trampoline (struct frame_info *, CORE_ADDR pc);
81
82 /* Forward declaration */
83 extern const struct language_defn unknown_language_defn;
84
85 /* The current (default at startup) state of type and range checking.
86 (If the modes are set to "auto", though, these are changed based
87 on the default language at startup, and then again based on the
88 language of the first source file. */
89
90 enum range_mode range_mode = range_mode_auto;
91 enum range_check range_check = range_check_off;
92 enum type_mode type_mode = type_mode_auto;
93 enum type_check type_check = type_check_off;
94 enum case_mode case_mode = case_mode_auto;
95 enum case_sensitivity case_sensitivity = case_sensitive_on;
96
97 /* The current language and language_mode (see language.h) */
98
99 const struct language_defn *current_language = &unknown_language_defn;
100 enum language_mode language_mode = language_mode_auto;
101
102 /* The language that the user expects to be typing in (the language
103 of main(), or the last language we notified them about, or C). */
104
105 const struct language_defn *expected_language;
106
107 /* The list of supported languages. The list itself is malloc'd. */
108
109 static const struct language_defn **languages;
110 static unsigned languages_size;
111 static unsigned languages_allocsize;
112 #define DEFAULT_ALLOCSIZE 4
113
114 /* The "set language/type/range" commands all put stuff in these
115 buffers. This is to make them work as set/show commands. The
116 user's string is copied here, then the set_* commands look at
117 them and update them to something that looks nice when it is
118 printed out. */
119
120 static char *language;
121 static char *type;
122 static char *range;
123 static char *case_sensitive;
124
125 /* Warning issued when current_language and the language of the current
126 frame do not match. */
127 char lang_frame_mismatch_warn[] =
128 "Warning: the current language does not match this frame.";
129 \f
130 /* This page contains the functions corresponding to GDB commands
131 and their helpers. */
132
133 /* Show command. Display a warning if the language set
134 does not match the frame. */
135 static void
136 show_language_command (struct ui_file *file, int from_tty,
137 struct cmd_list_element *c, const char *value)
138 {
139 enum language flang; /* The language of the current frame */
140
141 deprecated_show_value_hack (file, from_tty, c, value);
142 flang = get_frame_language ();
143 if (flang != language_unknown &&
144 language_mode == language_mode_manual &&
145 current_language->la_language != flang)
146 printf_filtered ("%s\n", lang_frame_mismatch_warn);
147 }
148
149 /* Set command. Change the current working language. */
150 static void
151 set_language_command (char *ignore, int from_tty, struct cmd_list_element *c)
152 {
153 int i, len;
154 enum language flang;
155 char *err_lang, *tem;
156
157 /* Strip trailing whitespace. */
158 if (!language)
159 len = 0;
160 else
161 {
162 len = strlen (language);
163 while (len > 0 && language[len - 1] == ' ')
164 --len;
165 }
166
167 if (len == 0)
168 {
169 printf_unfiltered (_("\
170 The currently understood settings are:\n\n\
171 local or auto Automatic setting based on source file\n"));
172
173 for (i = 0; i < languages_size; ++i)
174 {
175 /* Already dealt with these above. */
176 if (languages[i]->la_language == language_unknown
177 || languages[i]->la_language == language_auto)
178 continue;
179
180 /* FIXME: i18n: for now assume that the human-readable name
181 is just a capitalization of the internal name. */
182 printf_unfiltered ("%-16s Use the %c%s language\n",
183 languages[i]->la_name,
184 /* Capitalize first letter of language
185 name. */
186 toupper (languages[i]->la_name[0]),
187 languages[i]->la_name + 1);
188 }
189 /* Restore the silly string. */
190 set_language (current_language->la_language);
191 return;
192 }
193
194 /* Reset LANGUAGE to avoid trailing spaces. */
195 tem = savestring (language, len);
196 xfree (language);
197 language = tem;
198
199 /* Search the list of languages for a match. */
200 for (i = 0; i < languages_size; i++)
201 {
202 if (strcmp (languages[i]->la_name, language) == 0)
203 {
204 /* Found it! Go into manual mode, and use this language. */
205 if (languages[i]->la_language == language_auto)
206 {
207 /* Enter auto mode. Set to the current frame's language, if
208 known, or fallback to the initial language. */
209 language_mode = language_mode_auto;
210 flang = get_frame_language ();
211 if (flang != language_unknown)
212 set_language (flang);
213 else
214 set_initial_language ();
215 expected_language = current_language;
216 return;
217 }
218 else
219 {
220 /* Enter manual mode. Set the specified language. */
221 language_mode = language_mode_manual;
222 current_language = languages[i];
223 set_type_range_case ();
224 set_lang_str ();
225 expected_language = current_language;
226 return;
227 }
228 }
229 }
230
231 /* Reset the language (esp. the global string "language") to the
232 correct values. */
233 err_lang = xstrdup (language);
234 make_cleanup (xfree, err_lang); /* Free it after error */
235 set_language (current_language->la_language);
236 error (_("Unknown language `%s'."), err_lang);
237 }
238
239 static char **
240 language_completer (struct cmd_list_element *self, char *text, char *word)
241 {
242 int i;
243 const char **langnames
244 = (const char **) alloca ((languages_size + 1) * sizeof (const char *));
245
246 for (i = 0; i < languages_size; ++i)
247 langnames[i] = languages[i]->la_name;
248 langnames[i] = NULL;
249
250 return complete_on_enum (langnames, text, word);
251 }
252
253 /* Show command. Display a warning if the type setting does
254 not match the current language. */
255 static void
256 show_type_command (struct ui_file *file, int from_tty,
257 struct cmd_list_element *c, const char *value)
258 {
259 deprecated_show_value_hack (file, from_tty, c, value);
260 if (type_check != current_language->la_type_check)
261 printf_unfiltered (
262 "Warning: the current type check setting does not match the language.\n");
263 }
264
265 /* Set command. Change the setting for type checking. */
266 static void
267 set_type_command (char *ignore, int from_tty, struct cmd_list_element *c)
268 {
269 int len;
270 char *tem;
271
272 /* Strip trailing whitespace. */
273 len = strlen (type);
274 while (len > 0 && type[len - 1] == ' ')
275 --len;
276 /* Reset TYPE. */
277 tem = savestring (type, len);
278 xfree (type);
279 type = tem;
280
281 if (strcmp (type, "on") == 0)
282 {
283 type_check = type_check_on;
284 type_mode = type_mode_manual;
285 }
286 else if (strcmp (type, "warn") == 0)
287 {
288 type_check = type_check_warn;
289 type_mode = type_mode_manual;
290 }
291 else if (strcmp (type, "off") == 0)
292 {
293 type_check = type_check_off;
294 type_mode = type_mode_manual;
295 }
296 else if (strcmp (type, "auto") == 0)
297 {
298 type_mode = type_mode_auto;
299 set_type_range_case ();
300 /* Avoid hitting the set_type_str call below. We
301 did it in set_type_range_case. */
302 return;
303 }
304 else
305 {
306 warning (_("Unrecognized type check setting: \"%s\""), type);
307 }
308 set_type_str ();
309 show_type_command (NULL, from_tty, NULL, NULL);
310 }
311
312 /* Show command. Display a warning if the range setting does
313 not match the current language. */
314 static void
315 show_range_command (struct ui_file *file, int from_tty,
316 struct cmd_list_element *c, const char *value)
317 {
318 deprecated_show_value_hack (file, from_tty, c, value);
319 if (range_check != current_language->la_range_check)
320 printf_unfiltered (
321 "Warning: the current range check setting does not match the language.\n");
322 }
323
324 /* Set command. Change the setting for range checking. */
325 static void
326 set_range_command (char *ignore, int from_tty, struct cmd_list_element *c)
327 {
328 int len;
329 char *tem;
330
331 /* Strip trailing whitespace. */
332 len = strlen (range);
333 while (len > 0 && range[len - 1] == ' ')
334 --len;
335 /* Reset RANGE. */
336 tem = savestring (range, len);
337 xfree (range);
338 range = tem;
339
340 if (strcmp (range, "on") == 0)
341 {
342 range_check = range_check_on;
343 range_mode = range_mode_manual;
344 }
345 else if (strcmp (range, "warn") == 0)
346 {
347 range_check = range_check_warn;
348 range_mode = range_mode_manual;
349 }
350 else if (strcmp (range, "off") == 0)
351 {
352 range_check = range_check_off;
353 range_mode = range_mode_manual;
354 }
355 else if (strcmp (range, "auto") == 0)
356 {
357 range_mode = range_mode_auto;
358 set_type_range_case ();
359 /* Avoid hitting the set_range_str call below. We
360 did it in set_type_range_case. */
361 return;
362 }
363 else
364 {
365 warning (_("Unrecognized range check setting: \"%s\""), range);
366 }
367 set_range_str ();
368 show_range_command (NULL, from_tty, NULL, NULL);
369 }
370
371 /* Completer for range and type parameters. */
372 static char **
373 range_or_type_completer (struct cmd_list_element *self, char *text, char *word)
374 {
375 static const char *values[] = { "on", "off", "warn", "auto", NULL };
376 return complete_on_enum (values, text, word);
377 }
378
379 /* Show command. Display a warning if the case sensitivity setting does
380 not match the current language. */
381 static void
382 show_case_command (struct ui_file *file, int from_tty,
383 struct cmd_list_element *c, const char *value)
384 {
385 deprecated_show_value_hack (file, from_tty, c, value);
386 if (case_sensitivity != current_language->la_case_sensitivity)
387 printf_unfiltered(
388 "Warning: the current case sensitivity setting does not match the language.\n");
389 }
390
391 /* Set command. Change the setting for case sensitivity. */
392
393 static void
394 set_case_command (char *ignore, int from_tty, struct cmd_list_element *c)
395 {
396 if (strcmp (case_sensitive, "on") == 0)
397 {
398 case_sensitivity = case_sensitive_on;
399 case_mode = case_mode_manual;
400 }
401 else if (strcmp (case_sensitive, "off") == 0)
402 {
403 case_sensitivity = case_sensitive_off;
404 case_mode = case_mode_manual;
405 }
406 else if (strcmp (case_sensitive, "auto") == 0)
407 {
408 case_mode = case_mode_auto;
409 set_type_range_case ();
410 /* Avoid hitting the set_case_str call below. We did it in
411 set_type_range_case. */
412 return;
413 }
414 else
415 {
416 warning (_("Unrecognized case-sensitive setting: \"%s\""),
417 case_sensitive);
418 }
419 set_case_str();
420 show_case_command (NULL, from_tty, NULL, NULL);
421 }
422
423 /* Completer for case-sensitive parameter. */
424 static char **
425 case_completer (struct cmd_list_element *self, char *text, char *word)
426 {
427 static const char *values[] = { "on", "off", "auto", NULL };
428 return complete_on_enum (values, text, word);
429 }
430
431 /* Set the status of range and type checking and case sensitivity based on
432 the current modes and the current language.
433 If SHOW is non-zero, then print out the current language,
434 type and range checking status. */
435 static void
436 set_type_range_case (void)
437 {
438
439 if (range_mode == range_mode_auto)
440 range_check = current_language->la_range_check;
441
442 if (type_mode == type_mode_auto)
443 type_check = current_language->la_type_check;
444
445 if (case_mode == case_mode_auto)
446 case_sensitivity = current_language->la_case_sensitivity;
447
448 set_type_str ();
449 set_range_str ();
450 set_case_str ();
451 }
452
453 /* Set current language to (enum language) LANG. Returns previous language. */
454
455 enum language
456 set_language (enum language lang)
457 {
458 int i;
459 enum language prev_language;
460
461 prev_language = current_language->la_language;
462
463 for (i = 0; i < languages_size; i++)
464 {
465 if (languages[i]->la_language == lang)
466 {
467 current_language = languages[i];
468 set_type_range_case ();
469 set_lang_str ();
470 break;
471 }
472 }
473
474 return prev_language;
475 }
476 \f
477 /* This page contains functions that update the global vars
478 language, type and range. */
479 static void
480 set_lang_str (void)
481 {
482 char *prefix = "";
483
484 if (language)
485 xfree (language);
486 if (language_mode == language_mode_auto)
487 prefix = "auto; currently ";
488
489 language = concat (prefix, current_language->la_name, (char *)NULL);
490 }
491
492 static void
493 set_type_str (void)
494 {
495 char *tmp = NULL, *prefix = "";
496
497 if (type)
498 xfree (type);
499 if (type_mode == type_mode_auto)
500 prefix = "auto; currently ";
501
502 switch (type_check)
503 {
504 case type_check_on:
505 tmp = "on";
506 break;
507 case type_check_off:
508 tmp = "off";
509 break;
510 case type_check_warn:
511 tmp = "warn";
512 break;
513 default:
514 error (_("Unrecognized type check setting."));
515 }
516
517 type = concat (prefix, tmp, (char *)NULL);
518 }
519
520 static void
521 set_range_str (void)
522 {
523 char *tmp, *pref = "";
524
525 if (range_mode == range_mode_auto)
526 pref = "auto; currently ";
527
528 switch (range_check)
529 {
530 case range_check_on:
531 tmp = "on";
532 break;
533 case range_check_off:
534 tmp = "off";
535 break;
536 case range_check_warn:
537 tmp = "warn";
538 break;
539 default:
540 error (_("Unrecognized range check setting."));
541 }
542
543 if (range)
544 xfree (range);
545 range = concat (pref, tmp, (char *)NULL);
546 }
547
548 static void
549 set_case_str (void)
550 {
551 char *tmp = NULL, *prefix = "";
552
553 if (case_mode==case_mode_auto)
554 prefix = "auto; currently ";
555
556 switch (case_sensitivity)
557 {
558 case case_sensitive_on:
559 tmp = "on";
560 break;
561 case case_sensitive_off:
562 tmp = "off";
563 break;
564 default:
565 error (_("Unrecognized case-sensitive setting."));
566 }
567
568 xfree (case_sensitive);
569 case_sensitive = concat (prefix, tmp, (char *)NULL);
570 }
571
572 /* Print out the current language settings: language, range and
573 type checking. If QUIETLY, print only what has changed. */
574
575 void
576 language_info (int quietly)
577 {
578 if (quietly && expected_language == current_language)
579 return;
580
581 expected_language = current_language;
582 printf_unfiltered (_("Current language: %s\n"), language);
583 show_language_command (NULL, 1, NULL, NULL);
584
585 if (!quietly)
586 {
587 printf_unfiltered (_("Type checking: %s\n"), type);
588 show_type_command (NULL, 1, NULL, NULL);
589 printf_unfiltered (_("Range checking: %s\n"), range);
590 show_range_command (NULL, 1, NULL, NULL);
591 printf_unfiltered (_("Case sensitivity: %s\n"), case_sensitive);
592 show_case_command (NULL, 1, NULL, NULL);
593 }
594 }
595 \f
596 /* Return the result of a binary operation. */
597
598 #if 0 /* Currently unused */
599
600 struct type *
601 binop_result_type (struct value *v1, struct value *v2)
602 {
603 int size, uns;
604 struct type *t1 = check_typedef (VALUE_TYPE (v1));
605 struct type *t2 = check_typedef (VALUE_TYPE (v2));
606
607 int l1 = TYPE_LENGTH (t1);
608 int l2 = TYPE_LENGTH (t2);
609
610 switch (current_language->la_language)
611 {
612 case language_c:
613 case language_cplus:
614 case language_objc:
615 if (TYPE_CODE (t1) == TYPE_CODE_FLT)
616 return TYPE_CODE (t2) == TYPE_CODE_FLT && l2 > l1 ?
617 VALUE_TYPE (v2) : VALUE_TYPE (v1);
618 else if (TYPE_CODE (t2) == TYPE_CODE_FLT)
619 return TYPE_CODE (t1) == TYPE_CODE_FLT && l1 > l2 ?
620 VALUE_TYPE (v1) : VALUE_TYPE (v2);
621 else if (TYPE_UNSIGNED (t1) && l1 > l2)
622 return VALUE_TYPE (v1);
623 else if (TYPE_UNSIGNED (t2) && l2 > l1)
624 return VALUE_TYPE (v2);
625 else /* Both are signed. Result is the longer type */
626 return l1 > l2 ? VALUE_TYPE (v1) : VALUE_TYPE (v2);
627 break;
628 case language_m2:
629 /* If we are doing type-checking, l1 should equal l2, so this is
630 not needed. */
631 return l1 > l2 ? VALUE_TYPE (v1) : VALUE_TYPE (v2);
632 break;
633 }
634 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
635 return (struct type *) 0; /* For lint */
636 }
637
638 #endif /* 0 */
639 #if 0
640 /* This page contains functions that are used in type/range checking.
641 They all return zero if the type/range check fails.
642
643 It is hoped that these will make extending GDB to parse different
644 languages a little easier. These are primarily used in eval.c when
645 evaluating expressions and making sure that their types are correct.
646 Instead of having a mess of conjucted/disjuncted expressions in an "if",
647 the ideas of type can be wrapped up in the following functions.
648
649 Note that some of them are not currently dependent upon which language
650 is currently being parsed. For example, floats are the same in
651 C and Modula-2 (ie. the only floating point type has TYPE_CODE of
652 TYPE_CODE_FLT), while booleans are different. */
653
654 /* Returns non-zero if its argument is a simple type. This is the same for
655 both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur,
656 and thus will never cause the failure of the test. */
657 int
658 simple_type (struct type *type)
659 {
660 CHECK_TYPEDEF (type);
661 switch (TYPE_CODE (type))
662 {
663 case TYPE_CODE_INT:
664 case TYPE_CODE_CHAR:
665 case TYPE_CODE_ENUM:
666 case TYPE_CODE_FLT:
667 case TYPE_CODE_RANGE:
668 case TYPE_CODE_BOOL:
669 return 1;
670
671 default:
672 return 0;
673 }
674 }
675
676 /* Returns non-zero if its argument is of an ordered type.
677 An ordered type is one in which the elements can be tested for the
678 properties of "greater than", "less than", etc, or for which the
679 operations "increment" or "decrement" make sense. */
680 int
681 ordered_type (struct type *type)
682 {
683 CHECK_TYPEDEF (type);
684 switch (TYPE_CODE (type))
685 {
686 case TYPE_CODE_INT:
687 case TYPE_CODE_CHAR:
688 case TYPE_CODE_ENUM:
689 case TYPE_CODE_FLT:
690 case TYPE_CODE_RANGE:
691 return 1;
692
693 default:
694 return 0;
695 }
696 }
697
698 /* Returns non-zero if the two types are the same */
699 int
700 same_type (struct type *arg1, struct type *arg2)
701 {
702 CHECK_TYPEDEF (type);
703 if (structured_type (arg1) ? !structured_type (arg2) : structured_type (arg2))
704 /* One is structured and one isn't */
705 return 0;
706 else if (structured_type (arg1) && structured_type (arg2))
707 return arg1 == arg2;
708 else if (numeric_type (arg1) && numeric_type (arg2))
709 return (TYPE_CODE (arg2) == TYPE_CODE (arg1)) &&
710 (TYPE_UNSIGNED (arg1) == TYPE_UNSIGNED (arg2))
711 ? 1 : 0;
712 else
713 return arg1 == arg2;
714 }
715
716 /* Returns non-zero if the type is integral */
717 int
718 integral_type (struct type *type)
719 {
720 CHECK_TYPEDEF (type);
721 switch (current_language->la_language)
722 {
723 case language_c:
724 case language_cplus:
725 case language_objc:
726 return (TYPE_CODE (type) != TYPE_CODE_INT) &&
727 (TYPE_CODE (type) != TYPE_CODE_ENUM) ? 0 : 1;
728 case language_m2:
729 case language_pascal:
730 return TYPE_CODE (type) != TYPE_CODE_INT ? 0 : 1;
731 default:
732 error (_("Language not supported."));
733 }
734 }
735
736 /* Returns non-zero if the value is numeric */
737 int
738 numeric_type (struct type *type)
739 {
740 CHECK_TYPEDEF (type);
741 switch (TYPE_CODE (type))
742 {
743 case TYPE_CODE_INT:
744 case TYPE_CODE_FLT:
745 return 1;
746
747 default:
748 return 0;
749 }
750 }
751
752 /* Returns non-zero if the value is a character type */
753 int
754 character_type (struct type *type)
755 {
756 CHECK_TYPEDEF (type);
757 switch (current_language->la_language)
758 {
759 case language_m2:
760 case language_pascal:
761 return TYPE_CODE (type) != TYPE_CODE_CHAR ? 0 : 1;
762
763 case language_c:
764 case language_cplus:
765 case language_objc:
766 return (TYPE_CODE (type) == TYPE_CODE_INT) &&
767 TYPE_LENGTH (type) == sizeof (char)
768 ? 1 : 0;
769 default:
770 return (0);
771 }
772 }
773
774 /* Returns non-zero if the value is a string type */
775 int
776 string_type (struct type *type)
777 {
778 CHECK_TYPEDEF (type);
779 switch (current_language->la_language)
780 {
781 case language_m2:
782 case language_pascal:
783 return TYPE_CODE (type) != TYPE_CODE_STRING ? 0 : 1;
784
785 case language_c:
786 case language_cplus:
787 case language_objc:
788 /* C does not have distinct string type. */
789 return (0);
790 default:
791 return (0);
792 }
793 }
794
795 /* Returns non-zero if the value is a boolean type */
796 int
797 boolean_type (struct type *type)
798 {
799 CHECK_TYPEDEF (type);
800 if (TYPE_CODE (type) == TYPE_CODE_BOOL)
801 return 1;
802 switch (current_language->la_language)
803 {
804 case language_c:
805 case language_cplus:
806 case language_objc:
807 /* Might be more cleanly handled by having a
808 TYPE_CODE_INT_NOT_BOOL for (the deleted) CHILL and such
809 languages, or a TYPE_CODE_INT_OR_BOOL for C. */
810 if (TYPE_CODE (type) == TYPE_CODE_INT)
811 return 1;
812 default:
813 break;
814 }
815 return 0;
816 }
817
818 /* Returns non-zero if the value is a floating-point type */
819 int
820 float_type (struct type *type)
821 {
822 CHECK_TYPEDEF (type);
823 return TYPE_CODE (type) == TYPE_CODE_FLT;
824 }
825
826 /* Returns non-zero if the value is a pointer type */
827 int
828 pointer_type (struct type *type)
829 {
830 return TYPE_CODE (type) == TYPE_CODE_PTR ||
831 TYPE_CODE (type) == TYPE_CODE_REF;
832 }
833
834 /* Returns non-zero if the value is a structured type */
835 int
836 structured_type (struct type *type)
837 {
838 CHECK_TYPEDEF (type);
839 switch (current_language->la_language)
840 {
841 case language_c:
842 case language_cplus:
843 case language_objc:
844 return (TYPE_CODE (type) == TYPE_CODE_STRUCT) ||
845 (TYPE_CODE (type) == TYPE_CODE_UNION) ||
846 (TYPE_CODE (type) == TYPE_CODE_ARRAY);
847 case language_pascal:
848 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
849 (TYPE_CODE(type) == TYPE_CODE_UNION) ||
850 (TYPE_CODE(type) == TYPE_CODE_SET) ||
851 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
852 case language_m2:
853 return (TYPE_CODE (type) == TYPE_CODE_STRUCT) ||
854 (TYPE_CODE (type) == TYPE_CODE_SET) ||
855 (TYPE_CODE (type) == TYPE_CODE_ARRAY);
856 default:
857 return (0);
858 }
859 }
860 #endif
861 \f
862 /* This page contains functions that return info about
863 (struct value) values used in GDB. */
864
865 /* Returns non-zero if the value VAL represents a true value. */
866 int
867 value_true (struct value *val)
868 {
869 /* It is possible that we should have some sort of error if a non-boolean
870 value is used in this context. Possibly dependent on some kind of
871 "boolean-checking" option like range checking. But it should probably
872 not depend on the language except insofar as is necessary to identify
873 a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
874 should be an error, probably). */
875 return !value_logical_not (val);
876 }
877 \f
878 /* This page contains functions for the printing out of
879 error messages that occur during type- and range-
880 checking. */
881
882 /* These are called when a language fails a type- or range-check. The
883 first argument should be a printf()-style format string, and the
884 rest of the arguments should be its arguments. If
885 [type|range]_check is [type|range]_check_on, an error is printed;
886 if [type|range]_check_warn, a warning; otherwise just the
887 message. */
888
889 void
890 type_error (const char *string,...)
891 {
892 va_list args;
893 va_start (args, string);
894
895 switch (type_check)
896 {
897 case type_check_warn:
898 vwarning (string, args);
899 break;
900 case type_check_on:
901 verror (string, args);
902 break;
903 case type_check_off:
904 /* FIXME: cagney/2002-01-30: Should this function print anything
905 when type error is off? */
906 vfprintf_filtered (gdb_stderr, string, args);
907 fprintf_filtered (gdb_stderr, "\n");
908 break;
909 default:
910 internal_error (__FILE__, __LINE__, _("bad switch"));
911 }
912 va_end (args);
913 }
914
915 void
916 range_error (const char *string,...)
917 {
918 va_list args;
919 va_start (args, string);
920
921 switch (range_check)
922 {
923 case range_check_warn:
924 vwarning (string, args);
925 break;
926 case range_check_on:
927 verror (string, args);
928 break;
929 case range_check_off:
930 /* FIXME: cagney/2002-01-30: Should this function print anything
931 when range error is off? */
932 vfprintf_filtered (gdb_stderr, string, args);
933 fprintf_filtered (gdb_stderr, "\n");
934 break;
935 default:
936 internal_error (__FILE__, __LINE__, _("bad switch"));
937 }
938 va_end (args);
939 }
940 \f
941
942 /* This page contains miscellaneous functions */
943
944 /* Return the language enum for a given language string. */
945
946 enum language
947 language_enum (char *str)
948 {
949 int i;
950
951 for (i = 0; i < languages_size; i++)
952 if (strcmp (languages[i]->la_name, str) == 0)
953 return languages[i]->la_language;
954
955 return language_unknown;
956 }
957
958 /* Return the language struct for a given language enum. */
959
960 const struct language_defn *
961 language_def (enum language lang)
962 {
963 int i;
964
965 for (i = 0; i < languages_size; i++)
966 {
967 if (languages[i]->la_language == lang)
968 {
969 return languages[i];
970 }
971 }
972 return NULL;
973 }
974
975 /* Return the language as a string */
976 char *
977 language_str (enum language lang)
978 {
979 int i;
980
981 for (i = 0; i < languages_size; i++)
982 {
983 if (languages[i]->la_language == lang)
984 {
985 return languages[i]->la_name;
986 }
987 }
988 return "Unknown";
989 }
990
991 static void
992 set_check (char *ignore, int from_tty)
993 {
994 printf_unfiltered (
995 "\"set check\" must be followed by the name of a check subcommand.\n");
996 help_list (setchecklist, "set check ", -1, gdb_stdout);
997 }
998
999 static void
1000 show_check (char *ignore, int from_tty)
1001 {
1002 cmd_show_list (showchecklist, from_tty, "");
1003 }
1004 \f
1005 /* Add a language to the set of known languages. */
1006
1007 void
1008 add_language (const struct language_defn *lang)
1009 {
1010 if (lang->la_magic != LANG_MAGIC)
1011 {
1012 fprintf_unfiltered (gdb_stderr, "Magic number of %s language struct wrong\n",
1013 lang->la_name);
1014 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
1015 }
1016
1017 if (!languages)
1018 {
1019 languages_allocsize = DEFAULT_ALLOCSIZE;
1020 languages = (const struct language_defn **) xmalloc
1021 (languages_allocsize * sizeof (*languages));
1022 }
1023 if (languages_size >= languages_allocsize)
1024 {
1025 languages_allocsize *= 2;
1026 languages = (const struct language_defn **) xrealloc ((char *) languages,
1027 languages_allocsize * sizeof (*languages));
1028 }
1029 languages[languages_size++] = lang;
1030 }
1031
1032 /* Iterate through all registered languages looking for and calling
1033 any non-NULL struct language_defn.skip_trampoline() functions.
1034 Return the result from the first that returns non-zero, or 0 if all
1035 `fail'. */
1036 CORE_ADDR
1037 skip_language_trampoline (struct frame_info *frame, CORE_ADDR pc)
1038 {
1039 int i;
1040
1041 for (i = 0; i < languages_size; i++)
1042 {
1043 if (languages[i]->skip_trampoline)
1044 {
1045 CORE_ADDR real_pc = (languages[i]->skip_trampoline) (frame, pc);
1046 if (real_pc)
1047 return real_pc;
1048 }
1049 }
1050
1051 return 0;
1052 }
1053
1054 /* Return demangled language symbol, or NULL.
1055 FIXME: Options are only useful for certain languages and ignored
1056 by others, so it would be better to remove them here and have a
1057 more flexible demangler for the languages that need it.
1058 FIXME: Sometimes the demangler is invoked when we don't know the
1059 language, so we can't use this everywhere. */
1060 char *
1061 language_demangle (const struct language_defn *current_language,
1062 const char *mangled, int options)
1063 {
1064 if (current_language != NULL && current_language->la_demangle)
1065 return current_language->la_demangle (mangled, options);
1066 return NULL;
1067 }
1068
1069 /* Return class name from physname or NULL. */
1070 char *
1071 language_class_name_from_physname (const struct language_defn *current_language,
1072 const char *physname)
1073 {
1074 if (current_language != NULL && current_language->la_class_name_from_physname)
1075 return current_language->la_class_name_from_physname (physname);
1076 return NULL;
1077 }
1078
1079 /* Return non-zero if TYPE should be passed (and returned) by
1080 reference at the language level. */
1081 int
1082 language_pass_by_reference (struct type *type)
1083 {
1084 return current_language->la_pass_by_reference (type);
1085 }
1086
1087 /* Return zero; by default, types are passed by value at the language
1088 level. The target ABI may pass or return some structs by reference
1089 independent of this. */
1090 int
1091 default_pass_by_reference (struct type *type)
1092 {
1093 return 0;
1094 }
1095
1096 /* Return the default string containing the list of characters
1097 delimiting words. This is a reasonable default value that
1098 most languages should be able to use. */
1099
1100 char *
1101 default_word_break_characters (void)
1102 {
1103 return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1104 }
1105
1106 /* Print the index of array elements using the C99 syntax. */
1107
1108 void
1109 default_print_array_index (struct value *index_value, struct ui_file *stream,
1110 const struct value_print_options *options)
1111 {
1112 fprintf_filtered (stream, "[");
1113 LA_VALUE_PRINT (index_value, stream, options);
1114 fprintf_filtered (stream, "] = ");
1115 }
1116
1117 void
1118 default_get_string (struct value *value, gdb_byte **buffer, int *length,
1119 const char **charset)
1120 {
1121 error (_("Getting a string is unsupported in this language."));
1122 }
1123
1124 /* Define the language that is no language. */
1125
1126 static int
1127 unk_lang_parser (void)
1128 {
1129 return 1;
1130 }
1131
1132 static void
1133 unk_lang_error (char *msg)
1134 {
1135 error (_("Attempted to parse an expression with unknown language"));
1136 }
1137
1138 static void
1139 unk_lang_emit_char (int c, struct type *type, struct ui_file *stream,
1140 int quoter)
1141 {
1142 error (_("internal error - unimplemented function unk_lang_emit_char called."));
1143 }
1144
1145 static void
1146 unk_lang_printchar (int c, struct type *type, struct ui_file *stream)
1147 {
1148 error (_("internal error - unimplemented function unk_lang_printchar called."));
1149 }
1150
1151 static void
1152 unk_lang_printstr (struct ui_file *stream, struct type *type,
1153 const gdb_byte *string, unsigned int length,
1154 int force_ellipses,
1155 const struct value_print_options *options)
1156 {
1157 error (_("internal error - unimplemented function unk_lang_printstr called."));
1158 }
1159
1160 static void
1161 unk_lang_print_type (struct type *type, char *varstring, struct ui_file *stream,
1162 int show, int level)
1163 {
1164 error (_("internal error - unimplemented function unk_lang_print_type called."));
1165 }
1166
1167 static int
1168 unk_lang_val_print (struct type *type, const gdb_byte *valaddr,
1169 int embedded_offset, CORE_ADDR address,
1170 struct ui_file *stream, int recurse,
1171 const struct value_print_options *options)
1172 {
1173 error (_("internal error - unimplemented function unk_lang_val_print called."));
1174 }
1175
1176 static int
1177 unk_lang_value_print (struct value *val, struct ui_file *stream,
1178 const struct value_print_options *options)
1179 {
1180 error (_("internal error - unimplemented function unk_lang_value_print called."));
1181 }
1182
1183 static CORE_ADDR unk_lang_trampoline (struct frame_info *frame, CORE_ADDR pc)
1184 {
1185 return 0;
1186 }
1187
1188 /* Unknown languages just use the cplus demangler. */
1189 static char *unk_lang_demangle (const char *mangled, int options)
1190 {
1191 return cplus_demangle (mangled, options);
1192 }
1193
1194 static char *unk_lang_class_name (const char *mangled)
1195 {
1196 return NULL;
1197 }
1198
1199 static const struct op_print unk_op_print_tab[] =
1200 {
1201 {NULL, OP_NULL, PREC_NULL, 0}
1202 };
1203
1204 static void
1205 unknown_language_arch_info (struct gdbarch *gdbarch,
1206 struct language_arch_info *lai)
1207 {
1208 lai->string_char_type = builtin_type (gdbarch)->builtin_char;
1209 lai->bool_type_default = builtin_type (gdbarch)->builtin_int;
1210 lai->primitive_type_vector = GDBARCH_OBSTACK_CALLOC (gdbarch, 1,
1211 struct type *);
1212 }
1213
1214 const struct language_defn unknown_language_defn =
1215 {
1216 "unknown",
1217 language_unknown,
1218 range_check_off,
1219 type_check_off,
1220 case_sensitive_on,
1221 array_row_major,
1222 macro_expansion_no,
1223 &exp_descriptor_standard,
1224 unk_lang_parser,
1225 unk_lang_error,
1226 null_post_parser,
1227 unk_lang_printchar, /* Print character constant */
1228 unk_lang_printstr,
1229 unk_lang_emit_char,
1230 unk_lang_print_type, /* Print a type using appropriate syntax */
1231 default_print_typedef, /* Print a typedef using appropriate syntax */
1232 unk_lang_val_print, /* Print a value using appropriate syntax */
1233 unk_lang_value_print, /* Print a top-level value */
1234 unk_lang_trampoline, /* Language specific skip_trampoline */
1235 "this", /* name_of_this */
1236 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1237 basic_lookup_transparent_type,/* lookup_transparent_type */
1238 unk_lang_demangle, /* Language specific symbol demangler */
1239 unk_lang_class_name, /* Language specific class_name_from_physname */
1240 unk_op_print_tab, /* expression operators for printing */
1241 1, /* c-style arrays */
1242 0, /* String lower bound */
1243 default_word_break_characters,
1244 default_make_symbol_completion_list,
1245 unknown_language_arch_info, /* la_language_arch_info. */
1246 default_print_array_index,
1247 default_pass_by_reference,
1248 default_get_string,
1249 LANG_MAGIC
1250 };
1251
1252 /* These two structs define fake entries for the "local" and "auto" options. */
1253 const struct language_defn auto_language_defn =
1254 {
1255 "auto",
1256 language_auto,
1257 range_check_off,
1258 type_check_off,
1259 case_sensitive_on,
1260 array_row_major,
1261 macro_expansion_no,
1262 &exp_descriptor_standard,
1263 unk_lang_parser,
1264 unk_lang_error,
1265 null_post_parser,
1266 unk_lang_printchar, /* Print character constant */
1267 unk_lang_printstr,
1268 unk_lang_emit_char,
1269 unk_lang_print_type, /* Print a type using appropriate syntax */
1270 default_print_typedef, /* Print a typedef using appropriate syntax */
1271 unk_lang_val_print, /* Print a value using appropriate syntax */
1272 unk_lang_value_print, /* Print a top-level value */
1273 unk_lang_trampoline, /* Language specific skip_trampoline */
1274 "this", /* name_of_this */
1275 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1276 basic_lookup_transparent_type,/* lookup_transparent_type */
1277 unk_lang_demangle, /* Language specific symbol demangler */
1278 unk_lang_class_name, /* Language specific class_name_from_physname */
1279 unk_op_print_tab, /* expression operators for printing */
1280 1, /* c-style arrays */
1281 0, /* String lower bound */
1282 default_word_break_characters,
1283 default_make_symbol_completion_list,
1284 unknown_language_arch_info, /* la_language_arch_info. */
1285 default_print_array_index,
1286 default_pass_by_reference,
1287 default_get_string,
1288 LANG_MAGIC
1289 };
1290
1291 const struct language_defn local_language_defn =
1292 {
1293 "local",
1294 language_auto,
1295 range_check_off,
1296 type_check_off,
1297 case_sensitive_on,
1298 array_row_major,
1299 macro_expansion_no,
1300 &exp_descriptor_standard,
1301 unk_lang_parser,
1302 unk_lang_error,
1303 null_post_parser,
1304 unk_lang_printchar, /* Print character constant */
1305 unk_lang_printstr,
1306 unk_lang_emit_char,
1307 unk_lang_print_type, /* Print a type using appropriate syntax */
1308 default_print_typedef, /* Print a typedef using appropriate syntax */
1309 unk_lang_val_print, /* Print a value using appropriate syntax */
1310 unk_lang_value_print, /* Print a top-level value */
1311 unk_lang_trampoline, /* Language specific skip_trampoline */
1312 "this", /* name_of_this */
1313 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1314 basic_lookup_transparent_type,/* lookup_transparent_type */
1315 unk_lang_demangle, /* Language specific symbol demangler */
1316 unk_lang_class_name, /* Language specific class_name_from_physname */
1317 unk_op_print_tab, /* expression operators for printing */
1318 1, /* c-style arrays */
1319 0, /* String lower bound */
1320 default_word_break_characters,
1321 default_make_symbol_completion_list,
1322 unknown_language_arch_info, /* la_language_arch_info. */
1323 default_print_array_index,
1324 default_pass_by_reference,
1325 default_get_string,
1326 LANG_MAGIC
1327 };
1328 \f
1329 /* Per-architecture language information. */
1330
1331 static struct gdbarch_data *language_gdbarch_data;
1332
1333 struct language_gdbarch
1334 {
1335 /* A vector of per-language per-architecture info. Indexed by "enum
1336 language". */
1337 struct language_arch_info arch_info[nr_languages];
1338 };
1339
1340 static void *
1341 language_gdbarch_post_init (struct gdbarch *gdbarch)
1342 {
1343 struct language_gdbarch *l;
1344 int i;
1345
1346 l = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct language_gdbarch);
1347 for (i = 0; i < languages_size; i++)
1348 {
1349 if (languages[i] != NULL
1350 && languages[i]->la_language_arch_info != NULL)
1351 languages[i]->la_language_arch_info
1352 (gdbarch, l->arch_info + languages[i]->la_language);
1353 }
1354 return l;
1355 }
1356
1357 struct type *
1358 language_string_char_type (const struct language_defn *la,
1359 struct gdbarch *gdbarch)
1360 {
1361 struct language_gdbarch *ld = gdbarch_data (gdbarch,
1362 language_gdbarch_data);
1363 return ld->arch_info[la->la_language].string_char_type;
1364 }
1365
1366 struct type *
1367 language_bool_type (const struct language_defn *la,
1368 struct gdbarch *gdbarch)
1369 {
1370 struct language_gdbarch *ld = gdbarch_data (gdbarch,
1371 language_gdbarch_data);
1372
1373 if (ld->arch_info[la->la_language].bool_type_symbol)
1374 {
1375 struct symbol *sym;
1376 sym = lookup_symbol (ld->arch_info[la->la_language].bool_type_symbol,
1377 NULL, VAR_DOMAIN, NULL);
1378 if (sym)
1379 {
1380 struct type *type = SYMBOL_TYPE (sym);
1381 if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
1382 return type;
1383 }
1384 }
1385
1386 return ld->arch_info[la->la_language].bool_type_default;
1387 }
1388
1389 struct type *
1390 language_lookup_primitive_type_by_name (const struct language_defn *la,
1391 struct gdbarch *gdbarch,
1392 const char *name)
1393 {
1394 struct language_gdbarch *ld = gdbarch_data (gdbarch,
1395 language_gdbarch_data);
1396 struct type *const *p;
1397 for (p = ld->arch_info[la->la_language].primitive_type_vector;
1398 (*p) != NULL;
1399 p++)
1400 {
1401 if (strcmp (TYPE_NAME (*p), name) == 0)
1402 return (*p);
1403 }
1404 return (NULL);
1405 }
1406
1407 /* Initialize the language routines */
1408
1409 void
1410 _initialize_language (void)
1411 {
1412 struct cmd_list_element *command;
1413
1414 language_gdbarch_data
1415 = gdbarch_data_register_post_init (language_gdbarch_post_init);
1416
1417 /* GDB commands for language specific stuff */
1418
1419 command = add_setshow_string_noescape_cmd ("language", class_support,
1420 &language, _("\
1421 Set the current source language."), _("\
1422 Show the current source language."), NULL,
1423 set_language_command,
1424 show_language_command,
1425 &setlist, &showlist);
1426 set_cmd_completer (command, language_completer);
1427
1428 add_prefix_cmd ("check", no_class, set_check,
1429 _("Set the status of the type/range checker."),
1430 &setchecklist, "set check ", 0, &setlist);
1431 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1432 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1433
1434 add_prefix_cmd ("check", no_class, show_check,
1435 _("Show the status of the type/range checker."),
1436 &showchecklist, "show check ", 0, &showlist);
1437 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1438 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1439
1440 command =
1441 add_setshow_string_noescape_cmd ("type", class_support,
1442 &type, _("\
1443 Set type checking. (on/warn/off/auto)"), _("\
1444 Show type checking. (on/warn/off/auto)"), NULL,
1445 set_type_command,
1446 show_type_command,
1447 &setchecklist, &showchecklist);
1448 set_cmd_completer (command, range_or_type_completer);
1449
1450 command =
1451 add_setshow_string_noescape_cmd ("range", class_support,
1452 &range, _("\
1453 Set range checking. (on/warn/off/auto)"), _("\
1454 Show range checking. (on/warn/off/auto)"), NULL,
1455 set_range_command,
1456 show_range_command,
1457 &setchecklist, &showchecklist);
1458 set_cmd_completer (command, range_or_type_completer);
1459
1460 command =
1461 add_setshow_string_noescape_cmd ("case-sensitive", class_support,
1462 &case_sensitive, _("\
1463 Set case sensitivity in name search. (on/off/auto)"), _("\
1464 Show case sensitivity in name search. (on/off/auto)"), _("\
1465 For Fortran the default is off; for other languages the default is on."),
1466 set_case_command,
1467 show_case_command,
1468 &setlist, &showlist);
1469 set_cmd_completer (command, case_completer);
1470
1471 add_language (&unknown_language_defn);
1472 add_language (&local_language_defn);
1473 add_language (&auto_language_defn);
1474
1475 language = xstrdup ("auto");
1476 type = xstrdup ("auto");
1477 range = xstrdup ("auto");
1478 case_sensitive = xstrdup ("auto");
1479
1480 /* Have the above take effect */
1481 set_language (language_auto);
1482 }
This page took 0.058314 seconds and 5 git commands to generate.