Add generated source files and fix thinko in aarch64-asm.c
[binutils-gdb.git] / gdb / language.c
blob038559a34bf6d833a504ca886ed95539d19a5642
1 /* Multiple source language support for GDB.
3 Copyright (C) 1991-2024 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"
50 static void set_range_case (void);
52 /* range_mode ==
53 range_mode_auto: range_check set automatically to default of language.
54 range_mode_manual: range_check set manually by user. */
56 enum range_mode
58 range_mode_auto, range_mode_manual
61 /* case_mode ==
62 case_mode_auto: case_sensitivity set upon selection of scope.
63 case_mode_manual: case_sensitivity set only by user. */
65 enum case_mode
67 case_mode_auto, case_mode_manual
70 /* The current (default at startup) state of type and range checking.
71 (If the modes are set to "auto", though, these are changed based
72 on the default language at startup, and then again based on the
73 language of the first source file. */
75 static enum range_mode range_mode = range_mode_auto;
76 enum range_check range_check = range_check_off;
77 static enum case_mode case_mode = case_mode_auto;
78 enum case_sensitivity case_sensitivity = case_sensitive_on;
80 /* The current language and language_mode (see language.h). */
82 static const struct language_defn *global_current_language;
83 static lazily_set_language_ftype *lazy_language_setter;
84 enum language_mode language_mode = language_mode_auto;
86 /* See language.h. */
88 const struct language_defn *
89 get_current_language ()
91 if (lazy_language_setter != nullptr)
93 /* Avoid recursive calls -- set_language refers to
94 current_language. */
95 lazily_set_language_ftype *call = lazy_language_setter;
96 lazy_language_setter = nullptr;
97 call ();
99 return global_current_language;
102 void
103 lazily_set_language (lazily_set_language_ftype *fun)
105 lazy_language_setter = fun;
108 scoped_restore_current_language::scoped_restore_current_language ()
109 : m_lang (global_current_language),
110 m_fun (lazy_language_setter)
114 scoped_restore_current_language::~scoped_restore_current_language ()
116 /* If both are NULL, then that means dont_restore was called. */
117 if (m_lang != nullptr || m_fun != nullptr)
119 global_current_language = m_lang;
120 lazy_language_setter = m_fun;
121 if (lazy_language_setter == nullptr)
122 set_range_case ();
126 /* The language that the user expects to be typing in (the language
127 of main(), or the last language we notified them about, or C). */
129 const struct language_defn *expected_language;
131 /* Define the array containing all languages. */
133 const struct language_defn *language_defn::languages[nr_languages];
135 /* The current values of the "set language/range/case-sensitive" enum
136 commands. */
137 static const char *range;
138 static const char *case_sensitive;
140 /* See language.h. */
141 const char lang_frame_mismatch_warn[] =
142 N_("Warning: the current language does not match this frame.");
144 /* This page contains the functions corresponding to GDB commands
145 and their helpers. */
147 /* Show command. Display a warning if the language set
148 does not match the frame. */
149 static void
150 show_language_command (struct ui_file *file, int from_tty,
151 struct cmd_list_element *c, const char *value)
153 enum language flang; /* The language of the frame. */
155 if (language_mode == language_mode_auto)
156 gdb_printf (file,
157 _("The current source language is "
158 "\"auto; currently %s\".\n"),
159 current_language->name ());
160 else
161 gdb_printf (file,
162 _("The current source language is \"%s\".\n"),
163 current_language->name ());
165 if (has_stack_frames ())
167 frame_info_ptr frame;
169 frame = get_selected_frame (NULL);
170 flang = get_frame_language (frame);
171 if (flang != language_unknown
172 && language_mode == language_mode_manual
173 && current_language->la_language != flang)
174 gdb_printf (file, "%s\n", _(lang_frame_mismatch_warn));
178 /* Set callback for the "set/show language" setting. */
180 static void
181 set_language (const char *language)
183 enum language flang = language_unknown;
185 /* "local" is a synonym of "auto". */
186 if (strcmp (language, "auto") == 0
187 || strcmp (language, "local") == 0)
189 /* Enter auto mode. Set to the current frame's language, if
190 known, or fallback to the initial language. */
191 language_mode = language_mode_auto;
194 frame_info_ptr frame;
196 frame = get_selected_frame (NULL);
197 flang = get_frame_language (frame);
199 catch (const gdb_exception_error &ex)
201 flang = language_unknown;
204 if (flang != language_unknown)
205 set_language (flang);
206 else
207 set_initial_language ();
209 expected_language = current_language;
210 return;
213 /* Search the list of languages for a match. */
214 for (const auto &lang : language_defn::languages)
216 if (strcmp (lang->name (), language) != 0)
217 continue;
219 /* Found it! Go into manual mode, and use this language. */
220 language_mode = language_mode_manual;
221 lazy_language_setter = nullptr;
222 global_current_language = lang;
223 set_range_case ();
224 expected_language = lang;
225 return;
228 internal_error ("Couldn't find language `%s' in known languages list.",
229 language);
232 /* Get callback for the "set/show language" setting. */
234 static const char *
235 get_language ()
237 if (language_mode == language_mode_auto)
238 return "auto";
240 return current_language->name ();
243 /* Show command. Display a warning if the range setting does
244 not match the current language. */
245 static void
246 show_range_command (struct ui_file *file, int from_tty,
247 struct cmd_list_element *c, const char *value)
249 if (range_mode == range_mode_auto)
251 const char *tmp;
253 switch (range_check)
255 case range_check_on:
256 tmp = "on";
257 break;
258 case range_check_off:
259 tmp = "off";
260 break;
261 case range_check_warn:
262 tmp = "warn";
263 break;
264 default:
265 internal_error ("Unrecognized range check setting.");
268 gdb_printf (file,
269 _("Range checking is \"auto; currently %s\".\n"),
270 tmp);
272 else
273 gdb_printf (file, _("Range checking is \"%s\".\n"),
274 value);
276 if (range_check == range_check_warn
277 || ((range_check == range_check_on)
278 != current_language->range_checking_on_by_default ()))
279 warning (_("the current range check setting "
280 "does not match the language."));
283 /* Set command. Change the setting for range checking. */
284 static void
285 set_range_command (const char *ignore,
286 int from_tty, struct cmd_list_element *c)
288 if (strcmp (range, "on") == 0)
290 range_check = range_check_on;
291 range_mode = range_mode_manual;
293 else if (strcmp (range, "warn") == 0)
295 range_check = range_check_warn;
296 range_mode = range_mode_manual;
298 else if (strcmp (range, "off") == 0)
300 range_check = range_check_off;
301 range_mode = range_mode_manual;
303 else if (strcmp (range, "auto") == 0)
305 range_mode = range_mode_auto;
306 set_range_case ();
307 return;
309 else
311 internal_error (_("Unrecognized range check setting: \"%s\""), range);
313 if (range_check == range_check_warn
314 || ((range_check == range_check_on)
315 != current_language->range_checking_on_by_default ()))
316 warning (_("the current range check setting "
317 "does not match the language."));
320 /* Show command. Display a warning if the case sensitivity setting does
321 not match the current language. */
322 static void
323 show_case_command (struct ui_file *file, int from_tty,
324 struct cmd_list_element *c, const char *value)
326 if (case_mode == case_mode_auto)
328 const char *tmp = NULL;
330 switch (case_sensitivity)
332 case case_sensitive_on:
333 tmp = "on";
334 break;
335 case case_sensitive_off:
336 tmp = "off";
337 break;
338 default:
339 internal_error ("Unrecognized case-sensitive setting.");
342 gdb_printf (file,
343 _("Case sensitivity in "
344 "name search is \"auto; currently %s\".\n"),
345 tmp);
347 else
348 gdb_printf (file,
349 _("Case sensitivity in name search is \"%s\".\n"),
350 value);
352 if (case_sensitivity != current_language->case_sensitivity ())
353 warning (_("the current case sensitivity setting does not match "
354 "the language."));
357 /* Set command. Change the setting for case sensitivity. */
359 static void
360 set_case_command (const char *ignore, int from_tty, struct cmd_list_element *c)
362 if (strcmp (case_sensitive, "on") == 0)
364 case_sensitivity = case_sensitive_on;
365 case_mode = case_mode_manual;
367 else if (strcmp (case_sensitive, "off") == 0)
369 case_sensitivity = case_sensitive_off;
370 case_mode = case_mode_manual;
372 else if (strcmp (case_sensitive, "auto") == 0)
374 case_mode = case_mode_auto;
375 set_range_case ();
376 return;
378 else
380 internal_error ("Unrecognized case-sensitive setting: \"%s\"",
381 case_sensitive);
384 if (case_sensitivity != current_language->case_sensitivity ())
385 warning (_("the current case sensitivity setting does not match "
386 "the language."));
389 /* Set the status of range and type checking and case sensitivity based on
390 the current modes and the current language.
391 If SHOW is non-zero, then print out the current language,
392 type and range checking status. */
393 static void
394 set_range_case (void)
396 if (range_mode == range_mode_auto)
397 range_check = (current_language->range_checking_on_by_default ()
398 ? range_check_on : range_check_off);
400 if (case_mode == case_mode_auto)
401 case_sensitivity = current_language->case_sensitivity ();
404 /* See language.h. */
406 void
407 set_language (enum language lang)
409 lazy_language_setter = nullptr;
410 global_current_language = language_def (lang);
411 set_range_case ();
415 /* See language.h. */
417 void
418 language_info ()
420 if (expected_language == current_language)
421 return;
423 expected_language = current_language;
424 gdb_printf (_("Current language: %s\n"), get_language ());
425 show_language_command (gdb_stdout, 1, NULL, NULL);
428 /* This page contains functions for the printing out of
429 error messages that occur during type- and range-
430 checking. */
432 /* This is called when a language fails a range-check. The
433 first argument should be a printf()-style format string, and the
434 rest of the arguments should be its arguments. If range_check is
435 range_check_on, an error is printed; if range_check_warn, a warning;
436 otherwise just the message. */
438 void
439 range_error (const char *string,...)
441 va_list args;
443 va_start (args, string);
444 switch (range_check)
446 case range_check_warn:
447 vwarning (string, args);
448 break;
449 case range_check_on:
450 verror (string, args);
451 break;
452 case range_check_off:
453 /* FIXME: cagney/2002-01-30: Should this function print anything
454 when range error is off? */
455 gdb_vprintf (gdb_stderr, string, args);
456 gdb_printf (gdb_stderr, "\n");
457 break;
458 default:
459 internal_error (_("bad switch"));
461 va_end (args);
465 /* This page contains miscellaneous functions. */
467 /* Return the language enum for a given language string. */
469 enum language
470 language_enum (const char *str)
472 for (const auto &lang : language_defn::languages)
473 if (strcmp (lang->name (), str) == 0)
474 return lang->la_language;
476 return language_unknown;
479 /* Return the language struct for a given language enum. */
481 const struct language_defn *
482 language_def (enum language lang)
484 const struct language_defn *l = language_defn::languages[lang];
485 gdb_assert (l != nullptr);
486 return l;
489 /* Return the language as a string. */
491 const char *
492 language_str (enum language lang)
494 return language_def (lang)->name ();
499 /* Build and install the "set language LANG" command. */
501 static void
502 add_set_language_command ()
504 static const char **language_names;
506 /* Build the language names array, to be used as enumeration in the
507 "set language" enum command. +3 for "auto", "local" and NULL
508 termination. */
509 language_names = new const char *[ARRAY_SIZE (language_defn::languages) + 3];
511 /* Display "auto", "local" and "unknown" first, and then the rest,
512 alpha sorted. */
513 const char **language_names_p = language_names;
514 *language_names_p++ = "auto";
515 *language_names_p++ = "local";
516 *language_names_p++ = language_def (language_unknown)->name ();
517 const char **sort_begin = language_names_p;
518 for (const auto &lang : language_defn::languages)
520 /* Already handled above. */
521 if (lang->la_language == language_unknown)
522 continue;
523 *language_names_p++ = lang->name ();
525 *language_names_p = NULL;
526 std::sort (sort_begin, language_names_p, compare_cstrings);
528 /* Add the filename extensions. */
529 for (const auto &lang : language_defn::languages)
530 for (const char * const &ext : lang->filename_extensions ())
531 add_filename_language (ext, lang->la_language);
533 /* Build the "help set language" docs. */
534 string_file doc;
536 doc.printf (_("Set the current source language.\n"
537 "The currently understood settings are:\n\nlocal or "
538 "auto Automatic setting based on source file"));
540 for (const auto &lang : language_defn::languages)
542 /* Already dealt with these above. */
543 if (lang->la_language == language_unknown)
544 continue;
546 /* Note that we add the newline at the front, so we don't wind
547 up with a trailing newline. */
548 doc.printf ("\n%-16s Use the %s language",
549 lang->name (),
550 lang->natural_name ());
553 add_setshow_enum_cmd ("language", class_support,
554 language_names,
555 doc.c_str (),
556 _("Show the current source language."),
557 NULL,
558 set_language,
559 get_language,
560 show_language_command,
561 &setlist, &showlist);
564 /* Iterate through all registered languages looking for and calling
565 any non-NULL struct language_defn.skip_trampoline() functions.
566 Return the result from the first that returns non-zero, or 0 if all
567 `fail'. */
568 CORE_ADDR
569 skip_language_trampoline (const frame_info_ptr &frame, CORE_ADDR pc)
571 for (const auto &lang : language_defn::languages)
573 CORE_ADDR real_pc = lang->skip_trampoline (frame, pc);
575 if (real_pc != 0)
576 return real_pc;
579 return 0;
582 /* Return information about whether TYPE should be passed
583 (and returned) by reference at the language level. */
585 struct language_pass_by_ref_info
586 language_pass_by_reference (struct type *type)
588 return current_language->pass_by_reference_info (type);
591 /* Return the default string containing the list of characters
592 delimiting words. This is a reasonable default value that
593 most languages should be able to use. */
595 const char *
596 default_word_break_characters (void)
598 return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
601 /* See language.h. */
603 void
604 language_defn::print_array_index (struct type *index_type, LONGEST index,
605 struct ui_file *stream,
606 const value_print_options *options) const
608 struct value *index_value = value_from_longest (index_type, index);
610 gdb_printf (stream, "[");
611 value_print (index_value, stream, options);
612 gdb_printf (stream, "] = ");
615 /* See language.h. */
617 gdb::unique_xmalloc_ptr<char>
618 language_defn::watch_location_expression (struct type *type,
619 CORE_ADDR addr) const
621 /* Generates an expression that assumes a C like syntax is valid. */
622 type = check_typedef (check_typedef (type)->target_type ());
623 std::string name = type_to_string (type);
624 return xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr));
627 /* See language.h. */
629 void
630 language_defn::value_print (struct value *val, struct ui_file *stream,
631 const struct value_print_options *options) const
633 return c_value_print (val, stream, options);
636 /* See language.h. */
639 language_defn::parser (struct parser_state *ps) const
641 return c_parse (ps);
644 /* See language.h. */
646 void
647 language_defn::value_print_inner
648 (struct value *val, struct ui_file *stream, int recurse,
649 const struct value_print_options *options) const
651 return c_value_print_inner (val, stream, recurse, options);
654 /* See language.h. */
656 void
657 language_defn::print_typedef (struct type *type, struct symbol *new_symbol,
658 struct ui_file *stream) const
660 c_print_typedef (type, new_symbol, stream);
663 /* See language.h. */
665 bool
666 language_defn::is_string_type_p (struct type *type) const
668 return c_is_string_type_p (type);
671 /* See language.h. */
673 std::unique_ptr<compile_instance>
674 language_defn::get_compile_instance () const
676 return {};
679 /* The default implementation of the get_symbol_name_matcher_inner method
680 from the language_defn class. Matches with strncmp_iw. */
682 static bool
683 default_symbol_name_matcher (const char *symbol_search_name,
684 const lookup_name_info &lookup_name,
685 completion_match_result *comp_match_res)
687 std::string_view name = lookup_name.name ();
688 completion_match_for_lcd *match_for_lcd
689 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
690 strncmp_iw_mode mode = (lookup_name.completion_mode ()
691 ? strncmp_iw_mode::NORMAL
692 : strncmp_iw_mode::MATCH_PARAMS);
694 if (strncmp_iw_with_mode (symbol_search_name, name.data (), name.size (),
695 mode, language_minimal, match_for_lcd) == 0)
697 if (comp_match_res != NULL)
698 comp_match_res->set_match (symbol_search_name);
699 return true;
701 else
702 return false;
705 /* See language.h. */
707 symbol_name_matcher_ftype *
708 language_defn::get_symbol_name_matcher
709 (const lookup_name_info &lookup_name) const
711 /* If currently in Ada mode, and the lookup name is wrapped in
712 '<...>', hijack all symbol name comparisons using the Ada
713 matcher, which handles the verbatim matching. */
714 if (current_language->la_language == language_ada
715 && lookup_name.ada ().verbatim_p ())
716 return current_language->get_symbol_name_matcher_inner (lookup_name);
718 return this->get_symbol_name_matcher_inner (lookup_name);
721 /* See language.h. */
723 symbol_name_matcher_ftype *
724 language_defn::get_symbol_name_matcher_inner
725 (const lookup_name_info &lookup_name) const
727 return default_symbol_name_matcher;
730 /* See language.h. */
732 const struct lang_varobj_ops *
733 language_defn::varobj_ops () const
735 /* The ops for the C language are suitable for the vast majority of the
736 supported languages. */
737 return &c_varobj_ops;
740 /* Class representing the "unknown" language. */
742 class unknown_language : public language_defn
744 public:
745 unknown_language () : language_defn (language_unknown)
746 { /* Nothing. */ }
748 /* See language.h. */
749 void language_arch_info (struct gdbarch *gdbarch,
750 struct language_arch_info *lai) const override
752 lai->set_string_char_type (builtin_type (gdbarch)->builtin_char);
753 lai->set_bool_type (builtin_type (gdbarch)->builtin_int);
756 /* See language.h. */
758 void print_type (struct type *type, const char *varstring,
759 struct ui_file *stream, int show, int level,
760 const struct type_print_options *flags) const override
762 error (_("type printing not implemented for language \"%s\""),
763 natural_name ());
766 /* See language.h. */
768 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
769 int options) const override
771 /* The auto language just uses the C++ demangler. */
772 return gdb_demangle (mangled, options);
775 /* See language.h. */
777 void value_print (struct value *val, struct ui_file *stream,
778 const struct value_print_options *options) const override
780 error (_("value printing not implemented for language \"%s\""),
781 natural_name ());
784 /* See language.h. */
786 void value_print_inner
787 (struct value *val, struct ui_file *stream, int recurse,
788 const struct value_print_options *options) const override
790 error (_("inner value printing not implemented for language \"%s\""),
791 natural_name ());
794 /* See language.h. */
796 int parser (struct parser_state *ps) const override
798 error (_("expression parsing not implemented for language \"%s\""),
799 natural_name ());
802 /* See language.h. */
804 void emitchar (int ch, struct type *chtype,
805 struct ui_file *stream, int quoter) const override
807 error (_("emit character not implemented for language \"%s\""),
808 natural_name ());
811 /* See language.h. */
813 void printchar (int ch, struct type *chtype,
814 struct ui_file *stream) const override
816 error (_("print character not implemented for language \"%s\""),
817 natural_name ());
820 /* See language.h. */
822 void printstr (struct ui_file *stream, struct type *elttype,
823 const gdb_byte *string, unsigned int length,
824 const char *encoding, int force_ellipses,
825 const struct value_print_options *options) const override
827 error (_("print string not implemented for language \"%s\""),
828 natural_name ());
831 /* See language.h. */
833 void print_typedef (struct type *type, struct symbol *new_symbol,
834 struct ui_file *stream) const override
836 error (_("print typedef not implemented for language \"%s\""),
837 natural_name ());
840 /* See language.h. */
842 bool is_string_type_p (struct type *type) const override
844 type = check_typedef (type);
845 while (type->code () == TYPE_CODE_REF)
847 type = type->target_type ();
848 type = check_typedef (type);
850 return (type->code () == TYPE_CODE_STRING);
853 /* See language.h. */
855 const char *name_of_this () const override
856 { return "this"; }
858 /* See language.h. */
860 const char *name () const override
861 { return "unknown"; }
863 /* See language.h. */
865 const char *natural_name () const override
866 { return "Unknown"; }
868 /* See language.h. */
870 bool store_sym_names_in_linkage_form_p () const override
871 { return true; }
874 /* Single instance of the unknown language class. */
876 static unknown_language unknown_language_defn;
879 /* Per-architecture language information. */
881 struct language_gdbarch
883 /* A vector of per-language per-architecture info. Indexed by "enum
884 language". */
885 struct language_arch_info arch_info[nr_languages];
888 static const registry<gdbarch>::key<language_gdbarch> language_gdbarch_data;
890 static language_gdbarch *
891 get_language_gdbarch (struct gdbarch *gdbarch)
893 struct language_gdbarch *l = language_gdbarch_data.get (gdbarch);
894 if (l == nullptr)
896 l = new struct language_gdbarch;
897 for (const auto &lang : language_defn::languages)
899 gdb_assert (lang != nullptr);
900 lang->language_arch_info (gdbarch, &l->arch_info[lang->la_language]);
902 language_gdbarch_data.set (gdbarch, l);
905 return l;
908 /* See language.h. */
910 struct type *
911 language_string_char_type (const struct language_defn *la,
912 struct gdbarch *gdbarch)
914 struct language_gdbarch *ld = get_language_gdbarch (gdbarch);
915 return ld->arch_info[la->la_language].string_char_type ();
918 /* See language.h. */
920 struct value *
921 language_defn::value_string (struct gdbarch *gdbarch,
922 const char *ptr, ssize_t len) const
924 struct type *type = language_string_char_type (this, gdbarch);
925 return value_cstring (ptr, len, type);
928 /* See language.h. */
930 struct type *
931 language_bool_type (const struct language_defn *la,
932 struct gdbarch *gdbarch)
934 struct language_gdbarch *ld = get_language_gdbarch (gdbarch);
935 return ld->arch_info[la->la_language].bool_type ();
938 /* See language.h. */
940 struct type *
941 language_arch_info::bool_type () const
943 if (m_bool_type_name != nullptr)
945 struct symbol *sym;
947 sym = lookup_symbol (m_bool_type_name, NULL, VAR_DOMAIN, NULL).symbol;
948 if (sym != nullptr)
950 struct type *type = sym->type ();
951 if (type != nullptr && type->code () == TYPE_CODE_BOOL)
952 return type;
956 return m_bool_type_default;
959 /* See language.h. */
961 struct symbol *
962 language_arch_info::type_and_symbol::alloc_type_symbol
963 (enum language lang, struct type *type)
965 struct symbol *symbol;
966 struct gdbarch *gdbarch;
967 gdb_assert (!type->is_objfile_owned ());
968 gdbarch = type->arch_owner ();
969 symbol = new (gdbarch_obstack (gdbarch)) struct symbol ();
970 symbol->m_name = type->name ();
971 symbol->set_language (lang, nullptr);
972 symbol->owner.arch = gdbarch;
973 symbol->set_is_objfile_owned (0);
974 symbol->set_section_index (0);
975 symbol->set_type (type);
976 symbol->set_domain (VAR_DOMAIN);
977 symbol->set_aclass_index (LOC_TYPEDEF);
978 return symbol;
981 /* See language.h. */
983 language_arch_info::type_and_symbol *
984 language_arch_info::lookup_primitive_type_and_symbol (const char *name)
986 for (struct type_and_symbol &tas : primitive_types_and_symbols)
988 if (strcmp (tas.type ()->name (), name) == 0)
989 return &tas;
992 return nullptr;
995 /* See language.h. */
997 struct type *
998 language_arch_info::lookup_primitive_type (const char *name)
1000 type_and_symbol *tas = lookup_primitive_type_and_symbol (name);
1001 if (tas != nullptr)
1002 return tas->type ();
1003 return nullptr;
1006 /* See language.h. */
1008 struct type *
1009 language_arch_info::lookup_primitive_type
1010 (gdb::function_view<bool (struct type *)> filter)
1012 for (struct type_and_symbol &tas : primitive_types_and_symbols)
1014 if (filter (tas.type ()))
1015 return tas.type ();
1018 return nullptr;
1021 /* See language.h. */
1023 struct symbol *
1024 language_arch_info::lookup_primitive_type_as_symbol (const char *name,
1025 enum language lang)
1027 type_and_symbol *tas = lookup_primitive_type_and_symbol (name);
1028 if (tas != nullptr)
1029 return tas->symbol (lang);
1030 return nullptr;
1033 /* Helper for the language_lookup_primitive_type overloads to forward
1034 to the corresponding language's lookup_primitive_type overload. */
1036 template<typename T>
1037 static struct type *
1038 language_lookup_primitive_type_1 (const struct language_defn *la,
1039 struct gdbarch *gdbarch,
1040 T arg)
1042 struct language_gdbarch *ld = get_language_gdbarch (gdbarch);
1043 return ld->arch_info[la->la_language].lookup_primitive_type (arg);
1046 /* See language.h. */
1048 struct type *
1049 language_lookup_primitive_type (const struct language_defn *la,
1050 struct gdbarch *gdbarch,
1051 const char *name)
1053 return language_lookup_primitive_type_1 (la, gdbarch, name);
1056 /* See language.h. */
1058 struct type *
1059 language_lookup_primitive_type (const struct language_defn *la,
1060 struct gdbarch *gdbarch,
1061 gdb::function_view<bool (struct type *)> filter)
1063 return language_lookup_primitive_type_1 (la, gdbarch, filter);
1066 /* See language.h. */
1068 struct symbol *
1069 language_lookup_primitive_type_as_symbol (const struct language_defn *la,
1070 struct gdbarch *gdbarch,
1071 const char *name)
1073 struct language_gdbarch *ld = get_language_gdbarch (gdbarch);
1074 struct language_arch_info *lai = &ld->arch_info[la->la_language];
1076 symbol_lookup_debug_printf
1077 ("language = \"%s\", gdbarch @ %s, type = \"%s\")",
1078 la->name (), host_address_to_string (gdbarch), name);
1080 struct symbol *sym
1081 = lai->lookup_primitive_type_as_symbol (name, la->la_language);
1083 symbol_lookup_debug_printf ("found symbol @ %s",
1084 host_address_to_string (sym));
1086 /* Note: The result of symbol lookup is normally a symbol *and* the block
1087 it was found in. Builtin types don't live in blocks. We *could* give
1088 them one, but there is no current need so to keep things simple symbol
1089 lookup is extended to allow for BLOCK_FOUND to be NULL. */
1091 return sym;
1094 /* Initialize the language routines. */
1096 void _initialize_language ();
1097 void
1098 _initialize_language ()
1100 static const char *const type_or_range_names[]
1101 = { "on", "off", "warn", "auto", NULL };
1103 static const char *const case_sensitive_names[]
1104 = { "on", "off", "auto", NULL };
1106 /* GDB commands for language specific stuff. */
1108 set_show_commands setshow_check_cmds
1109 = add_setshow_prefix_cmd ("check", no_class,
1110 _("Set the status of the type/range checker."),
1111 _("Show the status of the type/range checker."),
1112 &setchecklist, &showchecklist,
1113 &setlist, &showlist);
1114 add_alias_cmd ("c", setshow_check_cmds.set, no_class, 1, &setlist);
1115 add_alias_cmd ("ch", setshow_check_cmds.set, no_class, 1, &setlist);
1116 add_alias_cmd ("c", setshow_check_cmds.show, no_class, 1, &showlist);
1117 add_alias_cmd ("ch", setshow_check_cmds.show, no_class, 1, &showlist);
1119 range = type_or_range_names[3];
1120 gdb_assert (strcmp (range, "auto") == 0);
1121 add_setshow_enum_cmd ("range", class_support, type_or_range_names,
1122 &range,
1123 _("Set range checking (on/warn/off/auto)."),
1124 _("Show range checking (on/warn/off/auto)."),
1125 NULL, set_range_command,
1126 show_range_command,
1127 &setchecklist, &showchecklist);
1129 case_sensitive = case_sensitive_names[2];
1130 gdb_assert (strcmp (case_sensitive, "auto") == 0);
1131 add_setshow_enum_cmd ("case-sensitive", class_support, case_sensitive_names,
1132 &case_sensitive, _("\
1133 Set case sensitivity in name search (on/off/auto)."), _("\
1134 Show case sensitivity in name search (on/off/auto)."), _("\
1135 For Fortran the default is off; for other languages the default is on."),
1136 set_case_command,
1137 show_case_command,
1138 &setlist, &showlist);
1140 add_set_language_command ();