Automatic date update in version.in
[binutils-gdb.git] / gdb / language.c
blobaf58a374dd6d52a281ff7f3ce09335d23bfb370d
1 /* Multiple source language support for GDB.
3 Copyright (C) 1991-2022 Free Software Foundation, Inc.
5 Contributed by the Department of Computer Science at the State University
6 of New York at Buffalo.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* This file contains functions that return things that are specific
24 to languages. Each function should examine current_language if necessary,
25 and return the appropriate result. */
27 /* FIXME: Most of these would be better organized as macros which
28 return data out of a "language-specific" struct pointer that is set
29 whenever the working language changes. That would be a lot faster. */
31 #include "defs.h"
32 #include <ctype.h>
33 #include "symtab.h"
34 #include "gdbtypes.h"
35 #include "value.h"
36 #include "gdbcmd.h"
37 #include "expression.h"
38 #include "language.h"
39 #include "varobj.h"
40 #include "target.h"
41 #include "parser-defs.h"
42 #include "demangle.h"
43 #include "symfile.h"
44 #include "cp-support.h"
45 #include "frame.h"
46 #include "c-lang.h"
47 #include <algorithm>
48 #include "gdbarch.h"
49 #include "compile/compile-internal.h"
51 static void set_range_case (void);
53 /* range_mode ==
54 range_mode_auto: range_check set automatically to default of language.
55 range_mode_manual: range_check set manually by user. */
57 enum range_mode
59 range_mode_auto, range_mode_manual
62 /* case_mode ==
63 case_mode_auto: case_sensitivity set upon selection of scope.
64 case_mode_manual: case_sensitivity set only by user. */
66 enum case_mode
68 case_mode_auto, case_mode_manual
71 /* The current (default at startup) state of type and range checking.
72 (If the modes are set to "auto", though, these are changed based
73 on the default language at startup, and then again based on the
74 language of the first source file. */
76 static enum range_mode range_mode = range_mode_auto;
77 enum range_check range_check = range_check_off;
78 static enum case_mode case_mode = case_mode_auto;
79 enum case_sensitivity case_sensitivity = case_sensitive_on;
81 /* The current language and language_mode (see language.h). */
83 const struct language_defn *current_language = nullptr;
84 enum language_mode language_mode = language_mode_auto;
86 /* The language that the user expects to be typing in (the language
87 of main(), or the last language we notified them about, or C). */
89 const struct language_defn *expected_language;
91 /* Define the array containing all languages. */
93 const struct language_defn *language_defn::languages[nr_languages];
95 /* The current values of the "set language/range/case-sensitive" enum
96 commands. */
97 static const char *language;
98 static const char *range;
99 static const char *case_sensitive;
101 /* See language.h. */
102 const char lang_frame_mismatch_warn[] =
103 N_("Warning: the current language does not match this frame.");
105 /* This page contains the functions corresponding to GDB commands
106 and their helpers. */
108 /* Show command. Display a warning if the language set
109 does not match the frame. */
110 static void
111 show_language_command (struct ui_file *file, int from_tty,
112 struct cmd_list_element *c, const char *value)
114 enum language flang; /* The language of the frame. */
116 if (language_mode == language_mode_auto)
117 gdb_printf (file,
118 _("The current source language is "
119 "\"auto; currently %s\".\n"),
120 current_language->name ());
121 else
122 gdb_printf (file,
123 _("The current source language is \"%s\".\n"),
124 current_language->name ());
126 if (has_stack_frames ())
128 struct frame_info *frame;
130 frame = get_selected_frame (NULL);
131 flang = get_frame_language (frame);
132 if (flang != language_unknown
133 && language_mode == language_mode_manual
134 && current_language->la_language != flang)
135 gdb_printf (file, "%s\n", _(lang_frame_mismatch_warn));
139 /* Set command. Change the current working language. */
140 static void
141 set_language_command (const char *ignore,
142 int from_tty, struct cmd_list_element *c)
144 enum language flang = language_unknown;
146 /* "local" is a synonym of "auto". */
147 if (strcmp (language, "local") == 0)
148 language = "auto";
150 /* Search the list of languages for a match. */
151 for (const auto &lang : language_defn::languages)
153 if (strcmp (lang->name (), language) == 0)
155 /* Found it! Go into manual mode, and use this language. */
156 if (lang->la_language == language_auto)
158 /* Enter auto mode. Set to the current frame's language, if
159 known, or fallback to the initial language. */
160 language_mode = language_mode_auto;
163 struct frame_info *frame;
165 frame = get_selected_frame (NULL);
166 flang = get_frame_language (frame);
168 catch (const gdb_exception_error &ex)
170 flang = language_unknown;
173 if (flang != language_unknown)
174 set_language (flang);
175 else
176 set_initial_language ();
177 expected_language = current_language;
178 return;
180 else
182 /* Enter manual mode. Set the specified language. */
183 language_mode = language_mode_manual;
184 current_language = lang;
185 set_range_case ();
186 expected_language = current_language;
187 return;
192 internal_error (__FILE__, __LINE__,
193 "Couldn't find language `%s' in known languages list.",
194 language);
197 /* Show command. Display a warning if the range setting does
198 not match the current language. */
199 static void
200 show_range_command (struct ui_file *file, int from_tty,
201 struct cmd_list_element *c, const char *value)
203 if (range_mode == range_mode_auto)
205 const char *tmp;
207 switch (range_check)
209 case range_check_on:
210 tmp = "on";
211 break;
212 case range_check_off:
213 tmp = "off";
214 break;
215 case range_check_warn:
216 tmp = "warn";
217 break;
218 default:
219 internal_error (__FILE__, __LINE__,
220 "Unrecognized range check setting.");
223 gdb_printf (file,
224 _("Range checking is \"auto; currently %s\".\n"),
225 tmp);
227 else
228 gdb_printf (file, _("Range checking is \"%s\".\n"),
229 value);
231 if (range_check == range_check_warn
232 || ((range_check == range_check_on)
233 != current_language->range_checking_on_by_default ()))
234 warning (_("the current range check setting "
235 "does not match the language.\n"));
238 /* Set command. Change the setting for range checking. */
239 static void
240 set_range_command (const char *ignore,
241 int from_tty, struct cmd_list_element *c)
243 if (strcmp (range, "on") == 0)
245 range_check = range_check_on;
246 range_mode = range_mode_manual;
248 else if (strcmp (range, "warn") == 0)
250 range_check = range_check_warn;
251 range_mode = range_mode_manual;
253 else if (strcmp (range, "off") == 0)
255 range_check = range_check_off;
256 range_mode = range_mode_manual;
258 else if (strcmp (range, "auto") == 0)
260 range_mode = range_mode_auto;
261 set_range_case ();
262 return;
264 else
266 internal_error (__FILE__, __LINE__,
267 _("Unrecognized range check setting: \"%s\""), range);
269 if (range_check == range_check_warn
270 || ((range_check == range_check_on)
271 != current_language->range_checking_on_by_default ()))
272 warning (_("the current range check setting "
273 "does not match the language.\n"));
276 /* Show command. Display a warning if the case sensitivity setting does
277 not match the current language. */
278 static void
279 show_case_command (struct ui_file *file, int from_tty,
280 struct cmd_list_element *c, const char *value)
282 if (case_mode == case_mode_auto)
284 const char *tmp = NULL;
286 switch (case_sensitivity)
288 case case_sensitive_on:
289 tmp = "on";
290 break;
291 case case_sensitive_off:
292 tmp = "off";
293 break;
294 default:
295 internal_error (__FILE__, __LINE__,
296 "Unrecognized case-sensitive setting.");
299 gdb_printf (file,
300 _("Case sensitivity in "
301 "name search is \"auto; currently %s\".\n"),
302 tmp);
304 else
305 gdb_printf (file,
306 _("Case sensitivity in name search is \"%s\".\n"),
307 value);
309 if (case_sensitivity != current_language->case_sensitivity ())
310 warning (_("the current case sensitivity setting does not match "
311 "the language.\n"));
314 /* Set command. Change the setting for case sensitivity. */
316 static void
317 set_case_command (const char *ignore, int from_tty, struct cmd_list_element *c)
319 if (strcmp (case_sensitive, "on") == 0)
321 case_sensitivity = case_sensitive_on;
322 case_mode = case_mode_manual;
324 else if (strcmp (case_sensitive, "off") == 0)
326 case_sensitivity = case_sensitive_off;
327 case_mode = case_mode_manual;
329 else if (strcmp (case_sensitive, "auto") == 0)
331 case_mode = case_mode_auto;
332 set_range_case ();
333 return;
335 else
337 internal_error (__FILE__, __LINE__,
338 "Unrecognized case-sensitive setting: \"%s\"",
339 case_sensitive);
342 if (case_sensitivity != current_language->case_sensitivity ())
343 warning (_("the current case sensitivity setting does not match "
344 "the language.\n"));
347 /* Set the status of range and type checking and case sensitivity based on
348 the current modes and the current language.
349 If SHOW is non-zero, then print out the current language,
350 type and range checking status. */
351 static void
352 set_range_case (void)
354 if (range_mode == range_mode_auto)
355 range_check = (current_language->range_checking_on_by_default ()
356 ? range_check_on : range_check_off);
358 if (case_mode == case_mode_auto)
359 case_sensitivity = current_language->case_sensitivity ();
362 /* Set current language to (enum language) LANG. Returns previous
363 language. */
365 enum language
366 set_language (enum language lang)
368 enum language prev_language;
370 prev_language = current_language->la_language;
371 current_language = language_def (lang);
372 set_range_case ();
373 return prev_language;
377 /* See language.h. */
379 void
380 language_info ()
382 if (expected_language == current_language)
383 return;
385 expected_language = current_language;
386 gdb_printf (_("Current language: %s\n"), language);
387 show_language_command (gdb_stdout, 1, NULL, NULL);
390 /* This page contains functions for the printing out of
391 error messages that occur during type- and range-
392 checking. */
394 /* This is called when a language fails a range-check. The
395 first argument should be a printf()-style format string, and the
396 rest of the arguments should be its arguments. If range_check is
397 range_check_on, an error is printed; if range_check_warn, a warning;
398 otherwise just the message. */
400 void
401 range_error (const char *string,...)
403 va_list args;
405 va_start (args, string);
406 switch (range_check)
408 case range_check_warn:
409 vwarning (string, args);
410 break;
411 case range_check_on:
412 verror (string, args);
413 break;
414 case range_check_off:
415 /* FIXME: cagney/2002-01-30: Should this function print anything
416 when range error is off? */
417 gdb_vprintf (gdb_stderr, string, args);
418 gdb_printf (gdb_stderr, "\n");
419 break;
420 default:
421 internal_error (__FILE__, __LINE__, _("bad switch"));
423 va_end (args);
427 /* This page contains miscellaneous functions. */
429 /* Return the language enum for a given language string. */
431 enum language
432 language_enum (const char *str)
434 for (const auto &lang : language_defn::languages)
435 if (strcmp (lang->name (), str) == 0)
436 return lang->la_language;
438 if (strcmp (str, "local") == 0)
439 return language_auto;
441 return language_unknown;
444 /* Return the language struct for a given language enum. */
446 const struct language_defn *
447 language_def (enum language lang)
449 const struct language_defn *l = language_defn::languages[lang];
450 gdb_assert (l != nullptr);
451 return l;
454 /* Return the language as a string. */
456 const char *
457 language_str (enum language lang)
459 return language_def (lang)->name ();
464 /* Build and install the "set language LANG" command. */
466 static void
467 add_set_language_command ()
469 static const char **language_names;
471 /* Build the language names array, to be used as enumeration in the
472 "set language" enum command. +1 for "local" and +1 for NULL
473 termination. */
474 language_names = new const char *[ARRAY_SIZE (language_defn::languages) + 2];
476 /* Display "auto", "local" and "unknown" first, and then the rest,
477 alpha sorted. */
478 const char **language_names_p = language_names;
479 language = language_def (language_auto)->name ();
480 *language_names_p++ = language;
481 *language_names_p++ = "local";
482 *language_names_p++ = language_def (language_unknown)->name ();
483 const char **sort_begin = language_names_p;
484 for (const auto &lang : language_defn::languages)
486 /* Already handled above. */
487 if (lang->la_language == language_auto
488 || lang->la_language == language_unknown)
489 continue;
490 *language_names_p++ = lang->name ();
492 *language_names_p = NULL;
493 std::sort (sort_begin, language_names_p, compare_cstrings);
495 /* Add the filename extensions. */
496 for (const auto &lang : language_defn::languages)
497 for (const char * const &ext : lang->filename_extensions ())
498 add_filename_language (ext, lang->la_language);
500 /* Build the "help set language" docs. */
501 string_file doc;
503 doc.printf (_("Set the current source language.\n"
504 "The currently understood settings are:\n\nlocal or "
505 "auto Automatic setting based on source file"));
507 for (const auto &lang : language_defn::languages)
509 /* Already dealt with these above. */
510 if (lang->la_language == language_unknown
511 || lang->la_language == language_auto)
512 continue;
514 /* Note that we add the newline at the front, so we don't wind
515 up with a trailing newline. */
516 doc.printf ("\n%-16s Use the %s language",
517 lang->name (),
518 lang->natural_name ());
521 add_setshow_enum_cmd ("language", class_support,
522 language_names,
523 &language,
524 doc.c_str (),
525 _("Show the current source language."),
526 NULL, set_language_command,
527 show_language_command,
528 &setlist, &showlist);
531 /* Iterate through all registered languages looking for and calling
532 any non-NULL struct language_defn.skip_trampoline() functions.
533 Return the result from the first that returns non-zero, or 0 if all
534 `fail'. */
535 CORE_ADDR
536 skip_language_trampoline (struct frame_info *frame, CORE_ADDR pc)
538 for (const auto &lang : language_defn::languages)
540 CORE_ADDR real_pc = lang->skip_trampoline (frame, pc);
542 if (real_pc != 0)
543 return real_pc;
546 return 0;
549 /* Return demangled language symbol, or NULL.
550 FIXME: Options are only useful for certain languages and ignored
551 by others, so it would be better to remove them here and have a
552 more flexible demangler for the languages that need it.
553 FIXME: Sometimes the demangler is invoked when we don't know the
554 language, so we can't use this everywhere. */
555 gdb::unique_xmalloc_ptr<char>
556 language_demangle (const struct language_defn *current_language,
557 const char *mangled, int options)
559 if (current_language != NULL)
560 return current_language->demangle_symbol (mangled, options);
561 return NULL;
564 /* Return information about whether TYPE should be passed
565 (and returned) by reference at the language level. */
567 struct language_pass_by_ref_info
568 language_pass_by_reference (struct type *type)
570 return current_language->pass_by_reference_info (type);
573 /* Return the default string containing the list of characters
574 delimiting words. This is a reasonable default value that
575 most languages should be able to use. */
577 const char *
578 default_word_break_characters (void)
580 return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
583 /* See language.h. */
585 void
586 language_defn::print_array_index (struct type *index_type, LONGEST index,
587 struct ui_file *stream,
588 const value_print_options *options) const
590 struct value *index_value = value_from_longest (index_type, index);
592 gdb_printf (stream, "[");
593 value_print (index_value, stream, options);
594 gdb_printf (stream, "] = ");
597 /* See language.h. */
599 gdb::unique_xmalloc_ptr<char>
600 language_defn::watch_location_expression (struct type *type,
601 CORE_ADDR addr) const
603 /* Generates an expression that assumes a C like syntax is valid. */
604 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
605 std::string name = type_to_string (type);
606 return xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr));
609 /* See language.h. */
611 void
612 language_defn::value_print (struct value *val, struct ui_file *stream,
613 const struct value_print_options *options) const
615 return c_value_print (val, stream, options);
618 /* See language.h. */
621 language_defn::parser (struct parser_state *ps) const
623 return c_parse (ps);
626 /* See language.h. */
628 void
629 language_defn::value_print_inner
630 (struct value *val, struct ui_file *stream, int recurse,
631 const struct value_print_options *options) const
633 return c_value_print_inner (val, stream, recurse, options);
636 /* See language.h. */
638 void
639 language_defn::emitchar (int ch, struct type *chtype,
640 struct ui_file * stream, int quoter) const
642 c_emit_char (ch, chtype, stream, quoter);
645 /* See language.h. */
647 void
648 language_defn::printstr (struct ui_file *stream, struct type *elttype,
649 const gdb_byte *string, unsigned int length,
650 const char *encoding, int force_ellipses,
651 const struct value_print_options *options) const
653 c_printstr (stream, elttype, string, length, encoding, force_ellipses,
654 options);
657 /* See language.h. */
659 void
660 language_defn::print_typedef (struct type *type, struct symbol *new_symbol,
661 struct ui_file *stream) const
663 c_print_typedef (type, new_symbol, stream);
666 /* See language.h. */
668 bool
669 language_defn::is_string_type_p (struct type *type) const
671 return c_is_string_type_p (type);
674 /* See language.h. */
676 std::unique_ptr<compile_instance>
677 language_defn::get_compile_instance () const
679 return {};
682 /* The default implementation of the get_symbol_name_matcher_inner method
683 from the language_defn class. Matches with strncmp_iw. */
685 static bool
686 default_symbol_name_matcher (const char *symbol_search_name,
687 const lookup_name_info &lookup_name,
688 completion_match_result *comp_match_res)
690 gdb::string_view name = lookup_name.name ();
691 completion_match_for_lcd *match_for_lcd
692 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
693 strncmp_iw_mode mode = (lookup_name.completion_mode ()
694 ? strncmp_iw_mode::NORMAL
695 : strncmp_iw_mode::MATCH_PARAMS);
697 if (strncmp_iw_with_mode (symbol_search_name, name.data (), name.size (),
698 mode, language_minimal, match_for_lcd) == 0)
700 if (comp_match_res != NULL)
701 comp_match_res->set_match (symbol_search_name);
702 return true;
704 else
705 return false;
708 /* See language.h. */
710 symbol_name_matcher_ftype *
711 language_defn::get_symbol_name_matcher
712 (const lookup_name_info &lookup_name) const
714 /* If currently in Ada mode, and the lookup name is wrapped in
715 '<...>', hijack all symbol name comparisons using the Ada
716 matcher, which handles the verbatim matching. */
717 if (current_language->la_language == language_ada
718 && lookup_name.ada ().verbatim_p ())
719 return current_language->get_symbol_name_matcher_inner (lookup_name);
721 return this->get_symbol_name_matcher_inner (lookup_name);
724 /* See language.h. */
726 symbol_name_matcher_ftype *
727 language_defn::get_symbol_name_matcher_inner
728 (const lookup_name_info &lookup_name) const
730 return default_symbol_name_matcher;
733 /* See language.h. */
735 const struct lang_varobj_ops *
736 language_defn::varobj_ops () const
738 /* The ops for the C language are suitable for the vast majority of the
739 supported languages. */
740 return &c_varobj_ops;
743 /* Parent class for both the "auto" and "unknown" languages. These two
744 pseudo-languages are very similar so merging their implementations like
745 this makes sense. */
747 class auto_or_unknown_language : public language_defn
749 public:
750 auto_or_unknown_language (enum language lang)
751 : language_defn (lang)
752 { /* Nothing. */ }
754 /* See language.h. */
755 void language_arch_info (struct gdbarch *gdbarch,
756 struct language_arch_info *lai) const override
758 lai->set_string_char_type (builtin_type (gdbarch)->builtin_char);
759 lai->set_bool_type (builtin_type (gdbarch)->builtin_int);
762 /* See language.h. */
764 void print_type (struct type *type, const char *varstring,
765 struct ui_file *stream, int show, int level,
766 const struct type_print_options *flags) const override
768 error (_("type printing not implemented for language \"%s\""),
769 natural_name ());
772 /* See language.h. */
774 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
775 int options) const override
777 /* The auto language just uses the C++ demangler. */
778 return gdb_demangle (mangled, options);
781 /* See language.h. */
783 void value_print (struct value *val, struct ui_file *stream,
784 const struct value_print_options *options) const override
786 error (_("value printing not implemented for language \"%s\""),
787 natural_name ());
790 /* See language.h. */
792 void value_print_inner
793 (struct value *val, struct ui_file *stream, int recurse,
794 const struct value_print_options *options) const override
796 error (_("inner value printing not implemented for language \"%s\""),
797 natural_name ());
800 /* See language.h. */
802 int parser (struct parser_state *ps) const override
804 error (_("expression parsing not implemented for language \"%s\""),
805 natural_name ());
808 /* See language.h. */
810 void emitchar (int ch, struct type *chtype,
811 struct ui_file *stream, int quoter) const override
813 error (_("emit character not implemented for language \"%s\""),
814 natural_name ());
817 /* See language.h. */
819 void printchar (int ch, struct type *chtype,
820 struct ui_file *stream) const override
822 error (_("print character not implemented for language \"%s\""),
823 natural_name ());
826 /* See language.h. */
828 void printstr (struct ui_file *stream, struct type *elttype,
829 const gdb_byte *string, unsigned int length,
830 const char *encoding, int force_ellipses,
831 const struct value_print_options *options) const override
833 error (_("print string not implemented for language \"%s\""),
834 natural_name ());
837 /* See language.h. */
839 void print_typedef (struct type *type, struct symbol *new_symbol,
840 struct ui_file *stream) const override
842 error (_("print typedef not implemented for language \"%s\""),
843 natural_name ());
846 /* See language.h. */
848 bool is_string_type_p (struct type *type) const override
850 type = check_typedef (type);
851 while (type->code () == TYPE_CODE_REF)
853 type = TYPE_TARGET_TYPE (type);
854 type = check_typedef (type);
856 return (type->code () == TYPE_CODE_STRING);
859 /* See language.h. */
861 const char *name_of_this () const override
862 { return "this"; }
865 /* Class representing the fake "auto" language. */
867 class auto_language : public auto_or_unknown_language
869 public:
870 auto_language ()
871 : auto_or_unknown_language (language_auto)
872 { /* Nothing. */ }
874 /* See language.h. */
876 const char *name () const override
877 { return "auto"; }
879 /* See language.h. */
881 const char *natural_name () const override
882 { return "Auto"; }
885 /* Single instance of the fake "auto" language. */
887 static auto_language auto_language_defn;
889 /* Class representing the unknown language. */
891 class unknown_language : public auto_or_unknown_language
893 public:
894 unknown_language ()
895 : auto_or_unknown_language (language_unknown)
896 { /* Nothing. */ }
898 /* See language.h. */
900 const char *name () const override
901 { return "unknown"; }
903 /* See language.h. */
905 const char *natural_name () const override
906 { return "Unknown"; }
908 /* See language.h. */
910 bool store_sym_names_in_linkage_form_p () const override
911 { return true; }
914 /* Single instance of the unknown language class. */
916 static unknown_language unknown_language_defn;
919 /* Per-architecture language information. */
921 static struct gdbarch_data *language_gdbarch_data;
923 struct language_gdbarch
925 /* A vector of per-language per-architecture info. Indexed by "enum
926 language". */
927 struct language_arch_info arch_info[nr_languages];
930 static void *
931 language_gdbarch_post_init (struct gdbarch *gdbarch)
933 struct language_gdbarch *l
934 = obstack_new<struct language_gdbarch> (gdbarch_obstack (gdbarch));
935 for (const auto &lang : language_defn::languages)
937 gdb_assert (lang != nullptr);
938 lang->language_arch_info (gdbarch, &l->arch_info[lang->la_language]);
941 return l;
944 /* See language.h. */
946 struct type *
947 language_string_char_type (const struct language_defn *la,
948 struct gdbarch *gdbarch)
950 struct language_gdbarch *ld
951 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
952 return ld->arch_info[la->la_language].string_char_type ();
955 /* See language.h. */
957 struct type *
958 language_bool_type (const struct language_defn *la,
959 struct gdbarch *gdbarch)
961 struct language_gdbarch *ld
962 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
963 return ld->arch_info[la->la_language].bool_type ();
966 /* See language.h. */
968 struct type *
969 language_arch_info::bool_type () const
971 if (m_bool_type_name != nullptr)
973 struct symbol *sym;
975 sym = lookup_symbol (m_bool_type_name, NULL, VAR_DOMAIN, NULL).symbol;
976 if (sym != nullptr)
978 struct type *type = sym->type ();
979 if (type != nullptr && type->code () == TYPE_CODE_BOOL)
980 return type;
984 return m_bool_type_default;
987 /* See language.h. */
989 struct symbol *
990 language_arch_info::type_and_symbol::alloc_type_symbol
991 (enum language lang, struct type *type)
993 struct symbol *symbol;
994 struct gdbarch *gdbarch;
995 gdb_assert (!type->is_objfile_owned ());
996 gdbarch = type->arch_owner ();
997 symbol = new (gdbarch_obstack (gdbarch)) struct symbol ();
998 symbol->m_name = type->name ();
999 symbol->set_language (lang, nullptr);
1000 symbol->owner.arch = gdbarch;
1001 symbol->set_is_objfile_owned (0);
1002 symbol->set_section_index (0);
1003 symbol->set_type (type);
1004 symbol->set_domain (VAR_DOMAIN);
1005 symbol->set_aclass_index (LOC_TYPEDEF);
1006 return symbol;
1009 /* See language.h. */
1011 language_arch_info::type_and_symbol *
1012 language_arch_info::lookup_primitive_type_and_symbol (const char *name)
1014 for (struct type_and_symbol &tas : primitive_types_and_symbols)
1016 if (strcmp (tas.type ()->name (), name) == 0)
1017 return &tas;
1020 return nullptr;
1023 /* See language.h. */
1025 struct type *
1026 language_arch_info::lookup_primitive_type (const char *name)
1028 type_and_symbol *tas = lookup_primitive_type_and_symbol (name);
1029 if (tas != nullptr)
1030 return tas->type ();
1031 return nullptr;
1034 /* See language.h. */
1036 struct type *
1037 language_arch_info::lookup_primitive_type
1038 (gdb::function_view<bool (struct type *)> filter)
1040 for (struct type_and_symbol &tas : primitive_types_and_symbols)
1042 if (filter (tas.type ()))
1043 return tas.type ();
1046 return nullptr;
1049 /* See language.h. */
1051 struct symbol *
1052 language_arch_info::lookup_primitive_type_as_symbol (const char *name,
1053 enum language lang)
1055 type_and_symbol *tas = lookup_primitive_type_and_symbol (name);
1056 if (tas != nullptr)
1057 return tas->symbol (lang);
1058 return nullptr;
1061 /* Helper for the language_lookup_primitive_type overloads to forward
1062 to the corresponding language's lookup_primitive_type overload. */
1064 template<typename T>
1065 static struct type *
1066 language_lookup_primitive_type_1 (const struct language_defn *la,
1067 struct gdbarch *gdbarch,
1068 T arg)
1070 struct language_gdbarch *ld =
1071 (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1072 return ld->arch_info[la->la_language].lookup_primitive_type (arg);
1075 /* See language.h. */
1077 struct type *
1078 language_lookup_primitive_type (const struct language_defn *la,
1079 struct gdbarch *gdbarch,
1080 const char *name)
1082 return language_lookup_primitive_type_1 (la, gdbarch, name);
1085 /* See language.h. */
1087 struct type *
1088 language_lookup_primitive_type (const struct language_defn *la,
1089 struct gdbarch *gdbarch,
1090 gdb::function_view<bool (struct type *)> filter)
1092 return language_lookup_primitive_type_1 (la, gdbarch, filter);
1095 /* See language.h. */
1097 struct symbol *
1098 language_lookup_primitive_type_as_symbol (const struct language_defn *la,
1099 struct gdbarch *gdbarch,
1100 const char *name)
1102 struct language_gdbarch *ld
1103 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1104 struct language_arch_info *lai = &ld->arch_info[la->la_language];
1106 if (symbol_lookup_debug)
1107 gdb_printf (gdb_stdlog,
1108 "language_lookup_primitive_type_as_symbol"
1109 " (%s, %s, %s)",
1110 la->name (), host_address_to_string (gdbarch), name);
1112 struct symbol *sym
1113 = lai->lookup_primitive_type_as_symbol (name, la->la_language);
1115 if (symbol_lookup_debug)
1116 gdb_printf (gdb_stdlog, " = %s\n", host_address_to_string (sym));
1118 /* Note: The result of symbol lookup is normally a symbol *and* the block
1119 it was found in. Builtin types don't live in blocks. We *could* give
1120 them one, but there is no current need so to keep things simple symbol
1121 lookup is extended to allow for BLOCK_FOUND to be NULL. */
1123 return sym;
1126 /* Initialize the language routines. */
1128 void _initialize_language ();
1129 void
1130 _initialize_language ()
1132 static const char *const type_or_range_names[]
1133 = { "on", "off", "warn", "auto", NULL };
1135 static const char *const case_sensitive_names[]
1136 = { "on", "off", "auto", NULL };
1138 language_gdbarch_data
1139 = gdbarch_data_register_post_init (language_gdbarch_post_init);
1141 /* GDB commands for language specific stuff. */
1143 set_show_commands setshow_check_cmds
1144 = add_setshow_prefix_cmd ("check", no_class,
1145 _("Set the status of the type/range checker."),
1146 _("Show the status of the type/range checker."),
1147 &setchecklist, &showchecklist,
1148 &setlist, &showlist);
1149 add_alias_cmd ("c", setshow_check_cmds.set, no_class, 1, &setlist);
1150 add_alias_cmd ("ch", setshow_check_cmds.set, no_class, 1, &setlist);
1151 add_alias_cmd ("c", setshow_check_cmds.show, no_class, 1, &showlist);
1152 add_alias_cmd ("ch", setshow_check_cmds.show, no_class, 1, &showlist);
1154 range = type_or_range_names[3];
1155 gdb_assert (strcmp (range, "auto") == 0);
1156 add_setshow_enum_cmd ("range", class_support, type_or_range_names,
1157 &range,
1158 _("Set range checking (on/warn/off/auto)."),
1159 _("Show range checking (on/warn/off/auto)."),
1160 NULL, set_range_command,
1161 show_range_command,
1162 &setchecklist, &showchecklist);
1164 case_sensitive = case_sensitive_names[2];
1165 gdb_assert (strcmp (case_sensitive, "auto") == 0);
1166 add_setshow_enum_cmd ("case-sensitive", class_support, case_sensitive_names,
1167 &case_sensitive, _("\
1168 Set case sensitivity in name search (on/off/auto)."), _("\
1169 Show case sensitivity in name search (on/off/auto)."), _("\
1170 For Fortran the default is off; for other languages the default is on."),
1171 set_case_command,
1172 show_case_command,
1173 &setlist, &showlist);
1175 /* In order to call SET_LANGUAGE (below) we need to make sure that
1176 CURRENT_LANGUAGE is not NULL. So first set the language to unknown,
1177 then we can change the language to 'auto'. */
1178 current_language = language_def (language_unknown);
1180 add_set_language_command ();
1182 /* Have the above take effect. */
1183 set_language (language_auto);