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