Automatic date update in version.in
[binutils-gdb.git] / gdb / language.c
blobe43e0fe7a172a60a8c190a28adca3235ff8aaf06
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 <ctype.h>
32 #include "symtab.h"
33 #include "gdbtypes.h"
34 #include "value.h"
35 #include "gdbcmd.h"
36 #include "expression.h"
37 #include "language.h"
38 #include "varobj.h"
39 #include "target.h"
40 #include "parser-defs.h"
41 #include "demangle.h"
42 #include "symfile.h"
43 #include "cp-support.h"
44 #include "frame.h"
45 #include "c-lang.h"
46 #include <algorithm>
47 #include "gdbarch.h"
49 static void set_range_case (void);
51 /* range_mode ==
52 range_mode_auto: range_check set automatically to default of language.
53 range_mode_manual: range_check set manually by user. */
55 enum range_mode
57 range_mode_auto, range_mode_manual
60 /* case_mode ==
61 case_mode_auto: case_sensitivity set upon selection of scope.
62 case_mode_manual: case_sensitivity set only by user. */
64 enum case_mode
66 case_mode_auto, case_mode_manual
69 /* The current (default at startup) state of type and range checking.
70 (If the modes are set to "auto", though, these are changed based
71 on the default language at startup, and then again based on the
72 language of the first source file. */
74 static enum range_mode range_mode = range_mode_auto;
75 enum range_check range_check = range_check_off;
76 static enum case_mode case_mode = case_mode_auto;
77 enum case_sensitivity case_sensitivity = case_sensitive_on;
79 /* The current language and language_mode (see language.h). */
81 static const struct language_defn *global_current_language;
82 static lazily_set_language_ftype *lazy_language_setter;
83 enum language_mode language_mode = language_mode_auto;
85 /* See language.h. */
87 const struct language_defn *
88 get_current_language ()
90 if (lazy_language_setter != nullptr)
92 /* Avoid recursive calls -- set_language refers to
93 current_language. */
94 lazily_set_language_ftype *call = lazy_language_setter;
95 lazy_language_setter = nullptr;
96 call ();
98 return global_current_language;
101 void
102 lazily_set_language (lazily_set_language_ftype *fun)
104 lazy_language_setter = fun;
107 scoped_restore_current_language::scoped_restore_current_language ()
108 : m_lang (global_current_language),
109 m_fun (lazy_language_setter)
113 scoped_restore_current_language::~scoped_restore_current_language ()
115 /* If both are NULL, then that means dont_restore was called. */
116 if (m_lang != nullptr || m_fun != nullptr)
118 global_current_language = m_lang;
119 lazy_language_setter = m_fun;
120 if (lazy_language_setter == nullptr)
121 set_range_case ();
125 /* The language that the user expects to be typing in (the language
126 of main(), or the last language we notified them about, or C). */
128 const struct language_defn *expected_language;
130 /* Define the array containing all languages. */
132 const struct language_defn *language_defn::languages[nr_languages];
134 /* The current values of the "set language/range/case-sensitive" enum
135 commands. */
136 static const char *range;
137 static const char *case_sensitive;
139 /* See language.h. */
140 const char lang_frame_mismatch_warn[] =
141 N_("Warning: the current language does not match this frame.");
143 /* This page contains the functions corresponding to GDB commands
144 and their helpers. */
146 /* Show command. Display a warning if the language set
147 does not match the frame. */
148 static void
149 show_language_command (struct ui_file *file, int from_tty,
150 struct cmd_list_element *c, const char *value)
152 enum language flang; /* The language of the frame. */
154 if (language_mode == language_mode_auto)
155 gdb_printf (file,
156 _("The current source language is "
157 "\"auto; currently %s\".\n"),
158 current_language->name ());
159 else
160 gdb_printf (file,
161 _("The current source language is \"%s\".\n"),
162 current_language->name ());
164 if (has_stack_frames ())
166 frame_info_ptr frame;
168 frame = get_selected_frame (NULL);
169 flang = get_frame_language (frame);
170 if (flang != language_unknown
171 && language_mode == language_mode_manual
172 && current_language->la_language != flang)
173 gdb_printf (file, "%s\n", _(lang_frame_mismatch_warn));
177 /* Set callback for the "set/show language" setting. */
179 static void
180 set_language (const char *language)
182 enum language flang = language_unknown;
184 /* "local" is a synonym of "auto". */
185 if (strcmp (language, "auto") == 0
186 || strcmp (language, "local") == 0)
188 /* Enter auto mode. Set to the current frame's language, if
189 known, or fallback to the initial language. */
190 language_mode = language_mode_auto;
193 frame_info_ptr frame;
195 frame = get_selected_frame (NULL);
196 flang = get_frame_language (frame);
198 catch (const gdb_exception_error &ex)
200 flang = language_unknown;
203 if (flang != language_unknown)
204 set_language (flang);
205 else
206 set_initial_language ();
208 expected_language = current_language;
209 return;
212 /* Search the list of languages for a match. */
213 for (const auto &lang : language_defn::languages)
215 if (strcmp (lang->name (), language) != 0)
216 continue;
218 /* Found it! Go into manual mode, and use this language. */
219 language_mode = language_mode_manual;
220 lazy_language_setter = nullptr;
221 global_current_language = lang;
222 set_range_case ();
223 expected_language = lang;
224 return;
227 internal_error ("Couldn't find language `%s' in known languages list.",
228 language);
231 /* Get callback for the "set/show language" setting. */
233 static const char *
234 get_language ()
236 if (language_mode == language_mode_auto)
237 return "auto";
239 return current_language->name ();
242 /* Show command. Display a warning if the range setting does
243 not match the current language. */
244 static void
245 show_range_command (struct ui_file *file, int from_tty,
246 struct cmd_list_element *c, const char *value)
248 if (range_mode == range_mode_auto)
250 const char *tmp;
252 switch (range_check)
254 case range_check_on:
255 tmp = "on";
256 break;
257 case range_check_off:
258 tmp = "off";
259 break;
260 case range_check_warn:
261 tmp = "warn";
262 break;
263 default:
264 internal_error ("Unrecognized range check setting.");
267 gdb_printf (file,
268 _("Range checking is \"auto; currently %s\".\n"),
269 tmp);
271 else
272 gdb_printf (file, _("Range checking is \"%s\".\n"),
273 value);
275 if (range_check == range_check_warn
276 || ((range_check == range_check_on)
277 != current_language->range_checking_on_by_default ()))
278 warning (_("the current range check setting "
279 "does not match the language."));
282 /* Set command. Change the setting for range checking. */
283 static void
284 set_range_command (const char *ignore,
285 int from_tty, struct cmd_list_element *c)
287 if (strcmp (range, "on") == 0)
289 range_check = range_check_on;
290 range_mode = range_mode_manual;
292 else if (strcmp (range, "warn") == 0)
294 range_check = range_check_warn;
295 range_mode = range_mode_manual;
297 else if (strcmp (range, "off") == 0)
299 range_check = range_check_off;
300 range_mode = range_mode_manual;
302 else if (strcmp (range, "auto") == 0)
304 range_mode = range_mode_auto;
305 set_range_case ();
306 return;
308 else
310 internal_error (_("Unrecognized range check setting: \"%s\""), range);
312 if (range_check == range_check_warn
313 || ((range_check == range_check_on)
314 != current_language->range_checking_on_by_default ()))
315 warning (_("the current range check setting "
316 "does not match the language."));
319 /* Show command. Display a warning if the case sensitivity setting does
320 not match the current language. */
321 static void
322 show_case_command (struct ui_file *file, int from_tty,
323 struct cmd_list_element *c, const char *value)
325 if (case_mode == case_mode_auto)
327 const char *tmp = NULL;
329 switch (case_sensitivity)
331 case case_sensitive_on:
332 tmp = "on";
333 break;
334 case case_sensitive_off:
335 tmp = "off";
336 break;
337 default:
338 internal_error ("Unrecognized case-sensitive setting.");
341 gdb_printf (file,
342 _("Case sensitivity in "
343 "name search is \"auto; currently %s\".\n"),
344 tmp);
346 else
347 gdb_printf (file,
348 _("Case sensitivity in name search is \"%s\".\n"),
349 value);
351 if (case_sensitivity != current_language->case_sensitivity ())
352 warning (_("the current case sensitivity setting does not match "
353 "the language."));
356 /* Set command. Change the setting for case sensitivity. */
358 static void
359 set_case_command (const char *ignore, int from_tty, struct cmd_list_element *c)
361 if (strcmp (case_sensitive, "on") == 0)
363 case_sensitivity = case_sensitive_on;
364 case_mode = case_mode_manual;
366 else if (strcmp (case_sensitive, "off") == 0)
368 case_sensitivity = case_sensitive_off;
369 case_mode = case_mode_manual;
371 else if (strcmp (case_sensitive, "auto") == 0)
373 case_mode = case_mode_auto;
374 set_range_case ();
375 return;
377 else
379 internal_error ("Unrecognized case-sensitive setting: \"%s\"",
380 case_sensitive);
383 if (case_sensitivity != current_language->case_sensitivity ())
384 warning (_("the current case sensitivity setting does not match "
385 "the language."));
388 /* Set the status of range and type checking and case sensitivity based on
389 the current modes and the current language.
390 If SHOW is non-zero, then print out the current language,
391 type and range checking status. */
392 static void
393 set_range_case (void)
395 if (range_mode == range_mode_auto)
396 range_check = (current_language->range_checking_on_by_default ()
397 ? range_check_on : range_check_off);
399 if (case_mode == case_mode_auto)
400 case_sensitivity = current_language->case_sensitivity ();
403 /* See language.h. */
405 void
406 set_language (enum language lang)
408 lazy_language_setter = nullptr;
409 global_current_language = language_def (lang);
410 set_range_case ();
414 /* See language.h. */
416 void
417 language_info ()
419 if (expected_language == current_language)
420 return;
422 expected_language = current_language;
423 gdb_printf (_("Current language: %s\n"), get_language ());
424 show_language_command (gdb_stdout, 1, NULL, NULL);
427 /* This page contains functions for the printing out of
428 error messages that occur during type- and range-
429 checking. */
431 /* This is called when a language fails a range-check. The
432 first argument should be a printf()-style format string, and the
433 rest of the arguments should be its arguments. If range_check is
434 range_check_on, an error is printed; if range_check_warn, a warning;
435 otherwise just the message. */
437 void
438 range_error (const char *string,...)
440 va_list args;
442 va_start (args, string);
443 switch (range_check)
445 case range_check_warn:
446 vwarning (string, args);
447 break;
448 case range_check_on:
449 verror (string, args);
450 break;
451 case range_check_off:
452 /* FIXME: cagney/2002-01-30: Should this function print anything
453 when range error is off? */
454 gdb_vprintf (gdb_stderr, string, args);
455 gdb_printf (gdb_stderr, "\n");
456 break;
457 default:
458 internal_error (_("bad switch"));
460 va_end (args);
464 /* This page contains miscellaneous functions. */
466 /* Return the language enum for a given language string. */
468 enum language
469 language_enum (const char *str)
471 for (const auto &lang : language_defn::languages)
472 if (strcmp (lang->name (), str) == 0)
473 return lang->la_language;
475 return language_unknown;
478 /* Return the language struct for a given language enum. */
480 const struct language_defn *
481 language_def (enum language lang)
483 const struct language_defn *l = language_defn::languages[lang];
484 gdb_assert (l != nullptr);
485 return l;
488 /* Return the language as a string. */
490 const char *
491 language_str (enum language lang)
493 return language_def (lang)->name ();
498 /* Build and install the "set language LANG" command. */
500 static void
501 add_set_language_command ()
503 static const char **language_names;
505 /* Build the language names array, to be used as enumeration in the
506 "set language" enum command. +3 for "auto", "local" and NULL
507 termination. */
508 language_names = new const char *[ARRAY_SIZE (language_defn::languages) + 3];
510 /* Display "auto", "local" and "unknown" first, and then the rest,
511 alpha sorted. */
512 const char **language_names_p = language_names;
513 *language_names_p++ = "auto";
514 *language_names_p++ = "local";
515 *language_names_p++ = language_def (language_unknown)->name ();
516 const char **sort_begin = language_names_p;
517 for (const auto &lang : language_defn::languages)
519 /* Already handled above. */
520 if (lang->la_language == language_unknown)
521 continue;
522 *language_names_p++ = lang->name ();
524 *language_names_p = NULL;
525 std::sort (sort_begin, language_names_p, compare_cstrings);
527 /* Add the filename extensions. */
528 for (const auto &lang : language_defn::languages)
529 for (const char * const &ext : lang->filename_extensions ())
530 add_filename_language (ext, lang->la_language);
532 /* Build the "help set language" docs. */
533 string_file doc;
535 doc.printf (_("Set the current source language.\n"
536 "The currently understood settings are:\n\nlocal or "
537 "auto Automatic setting based on source file"));
539 for (const auto &lang : language_defn::languages)
541 /* Already dealt with these above. */
542 if (lang->la_language == language_unknown)
543 continue;
545 /* Note that we add the newline at the front, so we don't wind
546 up with a trailing newline. */
547 doc.printf ("\n%-16s Use the %s language",
548 lang->name (),
549 lang->natural_name ());
552 add_setshow_enum_cmd ("language", class_support,
553 language_names,
554 doc.c_str (),
555 _("Show the current source language."),
556 NULL,
557 set_language,
558 get_language,
559 show_language_command,
560 &setlist, &showlist);
563 /* Iterate through all registered languages looking for and calling
564 any non-NULL struct language_defn.skip_trampoline() functions.
565 Return the result from the first that returns non-zero, or 0 if all
566 `fail'. */
567 CORE_ADDR
568 skip_language_trampoline (const frame_info_ptr &frame, CORE_ADDR pc)
570 for (const auto &lang : language_defn::languages)
572 CORE_ADDR real_pc = lang->skip_trampoline (frame, pc);
574 if (real_pc != 0)
575 return real_pc;
578 return 0;
581 /* Return information about whether TYPE should be passed
582 (and returned) by reference at the language level. */
584 struct language_pass_by_ref_info
585 language_pass_by_reference (struct type *type)
587 return current_language->pass_by_reference_info (type);
590 /* Return the default string containing the list of characters
591 delimiting words. This is a reasonable default value that
592 most languages should be able to use. */
594 const char *
595 default_word_break_characters (void)
597 return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
600 /* See language.h. */
602 void
603 language_defn::print_array_index (struct type *index_type, LONGEST index,
604 struct ui_file *stream,
605 const value_print_options *options) const
607 struct value *index_value = value_from_longest (index_type, index);
609 gdb_printf (stream, "[");
610 value_print (index_value, stream, options);
611 gdb_printf (stream, "] = ");
614 /* See language.h. */
616 gdb::unique_xmalloc_ptr<char>
617 language_defn::watch_location_expression (struct type *type,
618 CORE_ADDR addr) const
620 /* Generates an expression that assumes a C like syntax is valid. */
621 type = check_typedef (check_typedef (type)->target_type ());
622 std::string name = type_to_string (type);
623 return xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr));
626 /* See language.h. */
628 void
629 language_defn::value_print (struct value *val, struct ui_file *stream,
630 const struct value_print_options *options) const
632 return c_value_print (val, stream, options);
635 /* See language.h. */
638 language_defn::parser (struct parser_state *ps) const
640 return c_parse (ps);
643 /* See language.h. */
645 void
646 language_defn::value_print_inner
647 (struct value *val, struct ui_file *stream, int recurse,
648 const struct value_print_options *options) const
650 return c_value_print_inner (val, stream, recurse, options);
653 /* See language.h. */
655 void
656 language_defn::print_typedef (struct type *type, struct symbol *new_symbol,
657 struct ui_file *stream) const
659 c_print_typedef (type, new_symbol, stream);
662 /* See language.h. */
664 bool
665 language_defn::is_string_type_p (struct type *type) const
667 return c_is_string_type_p (type);
670 /* See language.h. */
672 std::unique_ptr<compile_instance>
673 language_defn::get_compile_instance () const
675 return {};
678 /* The default implementation of the get_symbol_name_matcher_inner method
679 from the language_defn class. Matches with strncmp_iw. */
681 static bool
682 default_symbol_name_matcher (const char *symbol_search_name,
683 const lookup_name_info &lookup_name,
684 completion_match_result *comp_match_res)
686 std::string_view name = lookup_name.name ();
687 completion_match_for_lcd *match_for_lcd
688 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
689 strncmp_iw_mode mode = (lookup_name.completion_mode ()
690 ? strncmp_iw_mode::NORMAL
691 : strncmp_iw_mode::MATCH_PARAMS);
693 if (strncmp_iw_with_mode (symbol_search_name, name.data (), name.size (),
694 mode, language_minimal, match_for_lcd) == 0)
696 if (comp_match_res != NULL)
697 comp_match_res->set_match (symbol_search_name);
698 return true;
700 else
701 return false;
704 /* See language.h. */
706 symbol_name_matcher_ftype *
707 language_defn::get_symbol_name_matcher
708 (const lookup_name_info &lookup_name) const
710 /* If currently in Ada mode, and the lookup name is wrapped in
711 '<...>', hijack all symbol name comparisons using the Ada
712 matcher, which handles the verbatim matching. */
713 if (current_language->la_language == language_ada
714 && lookup_name.ada ().verbatim_p ())
715 return current_language->get_symbol_name_matcher_inner (lookup_name);
717 return this->get_symbol_name_matcher_inner (lookup_name);
720 /* See language.h. */
722 symbol_name_matcher_ftype *
723 language_defn::get_symbol_name_matcher_inner
724 (const lookup_name_info &lookup_name) const
726 return default_symbol_name_matcher;
729 /* See language.h. */
731 const struct lang_varobj_ops *
732 language_defn::varobj_ops () const
734 /* The ops for the C language are suitable for the vast majority of the
735 supported languages. */
736 return &c_varobj_ops;
739 /* Class representing the "unknown" language. */
741 class unknown_language : public language_defn
743 public:
744 unknown_language () : language_defn (language_unknown)
745 { /* Nothing. */ }
747 /* See language.h. */
748 void language_arch_info (struct gdbarch *gdbarch,
749 struct language_arch_info *lai) const override
751 lai->set_string_char_type (builtin_type (gdbarch)->builtin_char);
752 lai->set_bool_type (builtin_type (gdbarch)->builtin_int);
755 /* See language.h. */
757 void print_type (struct type *type, const char *varstring,
758 struct ui_file *stream, int show, int level,
759 const struct type_print_options *flags) const override
761 error (_("type printing not implemented for language \"%s\""),
762 natural_name ());
765 /* See language.h. */
767 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
768 int options) const override
770 /* The auto language just uses the C++ demangler. */
771 return gdb_demangle (mangled, options);
774 /* See language.h. */
776 void value_print (struct value *val, struct ui_file *stream,
777 const struct value_print_options *options) const override
779 error (_("value printing not implemented for language \"%s\""),
780 natural_name ());
783 /* See language.h. */
785 void value_print_inner
786 (struct value *val, struct ui_file *stream, int recurse,
787 const struct value_print_options *options) const override
789 error (_("inner value printing not implemented for language \"%s\""),
790 natural_name ());
793 /* See language.h. */
795 int parser (struct parser_state *ps) const override
797 error (_("expression parsing not implemented for language \"%s\""),
798 natural_name ());
801 /* See language.h. */
803 void emitchar (int ch, struct type *chtype,
804 struct ui_file *stream, int quoter) const override
806 error (_("emit character not implemented for language \"%s\""),
807 natural_name ());
810 /* See language.h. */
812 void printchar (int ch, struct type *chtype,
813 struct ui_file *stream) const override
815 error (_("print character not implemented for language \"%s\""),
816 natural_name ());
819 /* See language.h. */
821 void printstr (struct ui_file *stream, struct type *elttype,
822 const gdb_byte *string, unsigned int length,
823 const char *encoding, int force_ellipses,
824 const struct value_print_options *options) const override
826 error (_("print string not implemented for language \"%s\""),
827 natural_name ());
830 /* See language.h. */
832 void print_typedef (struct type *type, struct symbol *new_symbol,
833 struct ui_file *stream) const override
835 error (_("print typedef not implemented for language \"%s\""),
836 natural_name ());
839 /* See language.h. */
841 bool is_string_type_p (struct type *type) const override
843 type = check_typedef (type);
844 while (type->code () == TYPE_CODE_REF)
846 type = type->target_type ();
847 type = check_typedef (type);
849 return (type->code () == TYPE_CODE_STRING);
852 /* See language.h. */
854 const char *name_of_this () const override
855 { return "this"; }
857 /* See language.h. */
859 const char *name () const override
860 { return "unknown"; }
862 /* See language.h. */
864 const char *natural_name () const override
865 { return "Unknown"; }
867 /* See language.h. */
869 bool store_sym_names_in_linkage_form_p () const override
870 { return true; }
873 /* Single instance of the unknown language class. */
875 static unknown_language unknown_language_defn;
878 /* Per-architecture language information. */
880 struct language_gdbarch
882 /* A vector of per-language per-architecture info. Indexed by "enum
883 language". */
884 struct language_arch_info arch_info[nr_languages];
887 static const registry<gdbarch>::key<language_gdbarch> language_gdbarch_data;
889 static language_gdbarch *
890 get_language_gdbarch (struct gdbarch *gdbarch)
892 struct language_gdbarch *l = language_gdbarch_data.get (gdbarch);
893 if (l == nullptr)
895 l = new struct language_gdbarch;
896 for (const auto &lang : language_defn::languages)
898 gdb_assert (lang != nullptr);
899 lang->language_arch_info (gdbarch, &l->arch_info[lang->la_language]);
901 language_gdbarch_data.set (gdbarch, l);
904 return l;
907 /* See language.h. */
909 struct type *
910 language_string_char_type (const struct language_defn *la,
911 struct gdbarch *gdbarch)
913 struct language_gdbarch *ld = get_language_gdbarch (gdbarch);
914 return ld->arch_info[la->la_language].string_char_type ();
917 /* See language.h. */
919 struct value *
920 language_defn::value_string (struct gdbarch *gdbarch,
921 const char *ptr, ssize_t len) const
923 struct type *type = language_string_char_type (this, gdbarch);
924 return value_cstring (ptr, len, type);
927 /* See language.h. */
929 struct type *
930 language_bool_type (const struct language_defn *la,
931 struct gdbarch *gdbarch)
933 struct language_gdbarch *ld = get_language_gdbarch (gdbarch);
934 return ld->arch_info[la->la_language].bool_type ();
937 /* See language.h. */
939 struct type *
940 language_arch_info::bool_type () const
942 if (m_bool_type_name != nullptr)
944 struct symbol *sym;
946 sym = lookup_symbol (m_bool_type_name, nullptr, SEARCH_TYPE_DOMAIN,
947 nullptr).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 (TYPE_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 ();