]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/language.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / language.c
1 /* Multiple source language support for GDB.
2
3 Copyright (C) 1991-2025 Free Software Foundation, Inc.
4
5 Contributed by the Department of Computer Science at the State University
6 of New York at Buffalo.
7
8 This file is part of GDB.
9
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.
14
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.
19
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/>. */
22
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. */
26
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. */
30
31 #include <ctype.h>
32 #include "symtab.h"
33 #include "gdbtypes.h"
34 #include "value.h"
35 #include "cli/cli-cmds.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"
48
49 static void set_range_case (void);
50
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. */
54
55 enum range_mode
56 {
57 range_mode_auto, range_mode_manual
58 };
59
60 /* case_mode ==
61 case_mode_auto: case_sensitivity set upon selection of scope.
62 case_mode_manual: case_sensitivity set only by user. */
63
64 enum case_mode
65 {
66 case_mode_auto, case_mode_manual
67 };
68
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. */
73
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;
78
79 /* The current language and language_mode (see language.h). */
80
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;
84
85 /* Whether to warn on language changes. */
86 bool warn_frame_lang_mismatch = true;
87
88 /* See language.h. */
89
90 const struct language_defn *
91 get_current_language ()
92 {
93 if (lazy_language_setter != nullptr)
94 {
95 /* Avoid recursive calls -- set_language refers to
96 current_language. */
97 lazily_set_language_ftype *call = lazy_language_setter;
98 lazy_language_setter = nullptr;
99 call ();
100 }
101 return global_current_language;
102 }
103
104 void
105 lazily_set_language (lazily_set_language_ftype *fun)
106 {
107 lazy_language_setter = fun;
108 }
109
110 scoped_restore_current_language::scoped_restore_current_language ()
111 : m_lang (global_current_language),
112 m_fun (lazy_language_setter)
113 {
114 }
115
116 scoped_restore_current_language::scoped_restore_current_language
117 (enum language lang)
118 : scoped_restore_current_language ()
119 {
120 set_language (lang);
121 }
122
123 scoped_restore_current_language::~scoped_restore_current_language ()
124 {
125 /* If both are NULL, then that means dont_restore was called. */
126 if (m_lang != nullptr || m_fun != nullptr)
127 {
128 global_current_language = m_lang;
129 lazy_language_setter = m_fun;
130 if (lazy_language_setter == nullptr)
131 set_range_case ();
132 }
133 }
134
135 /* The language that the user expects to be typing in (the language
136 of main(), or the last language we notified them about, or C). */
137
138 const struct language_defn *expected_language;
139
140 /* Define the array containing all languages. */
141
142 const struct language_defn *language_defn::languages[nr_languages];
143
144 /* The current values of the "set language/range/case-sensitive" enum
145 commands. */
146 static const char *range;
147 static const char *case_sensitive;
148
149 /* See language.h. */
150 const char lang_frame_mismatch_warn[] =
151 N_("Warning: the current language does not match this frame.");
152 \f
153 /* This page contains the functions corresponding to GDB commands
154 and their helpers. */
155
156 /* Show command. Display a warning if the language set
157 does not match the frame. */
158 static void
159 show_language_command (struct ui_file *file, int from_tty,
160 struct cmd_list_element *c, const char *value)
161 {
162 enum language flang; /* The language of the frame. */
163
164 if (language_mode == language_mode_auto)
165 gdb_printf (file,
166 _("The current source language is "
167 "\"auto; currently %s\".\n"),
168 current_language->name ());
169 else
170 gdb_printf (file,
171 _("The current source language is \"%s\".\n"),
172 current_language->name ());
173
174 if (warn_frame_lang_mismatch && has_stack_frames ())
175 {
176 frame_info_ptr frame;
177
178 frame = get_selected_frame (NULL);
179 flang = get_frame_language (frame);
180 if (flang != language_unknown
181 && language_mode == language_mode_manual
182 && current_language->la_language != flang)
183 gdb_printf (file, "%s\n", _(lang_frame_mismatch_warn));
184 }
185 }
186
187 /* Set callback for the "set/show language" setting. */
188
189 static void
190 set_language (const char *language)
191 {
192 enum language flang = language_unknown;
193
194 /* "local" is a synonym of "auto". */
195 if (strcmp (language, "auto") == 0
196 || strcmp (language, "local") == 0)
197 {
198 /* Enter auto mode. Set to the current frame's language, if
199 known, or fallback to the initial language. */
200 language_mode = language_mode_auto;
201 try
202 {
203 frame_info_ptr frame;
204
205 frame = get_selected_frame (NULL);
206 flang = get_frame_language (frame);
207 }
208 catch (const gdb_exception_error &ex)
209 {
210 flang = language_unknown;
211 }
212
213 if (flang != language_unknown)
214 set_language (flang);
215 else
216 set_initial_language ();
217
218 expected_language = current_language;
219 return;
220 }
221
222 /* Search the list of languages for a match. */
223 for (const auto &lang : language_defn::languages)
224 {
225 if (strcmp (lang->name (), language) != 0)
226 continue;
227
228 /* Found it! Go into manual mode, and use this language. */
229 language_mode = language_mode_manual;
230 lazy_language_setter = nullptr;
231 global_current_language = lang;
232 set_range_case ();
233 expected_language = lang;
234 return;
235 }
236
237 internal_error ("Couldn't find language `%s' in known languages list.",
238 language);
239 }
240
241 /* Get callback for the "set/show language" setting. */
242
243 static const char *
244 get_language ()
245 {
246 if (language_mode == language_mode_auto)
247 return "auto";
248
249 return current_language->name ();
250 }
251
252 /* Show command. Display a warning if the range setting does
253 not match the current language. */
254 static void
255 show_range_command (struct ui_file *file, int from_tty,
256 struct cmd_list_element *c, const char *value)
257 {
258 if (range_mode == range_mode_auto)
259 {
260 const char *tmp;
261
262 switch (range_check)
263 {
264 case range_check_on:
265 tmp = "on";
266 break;
267 case range_check_off:
268 tmp = "off";
269 break;
270 case range_check_warn:
271 tmp = "warn";
272 break;
273 default:
274 internal_error ("Unrecognized range check setting.");
275 }
276
277 gdb_printf (file,
278 _("Range checking is \"auto; currently %s\".\n"),
279 tmp);
280 }
281 else
282 gdb_printf (file, _("Range checking is \"%s\".\n"),
283 value);
284
285 if (range_check == range_check_warn
286 || ((range_check == range_check_on)
287 != current_language->range_checking_on_by_default ()))
288 warning (_("the current range check setting "
289 "does not match the language."));
290 }
291
292 /* Set command. Change the setting for range checking. */
293 static void
294 set_range_command (const char *ignore,
295 int from_tty, struct cmd_list_element *c)
296 {
297 if (strcmp (range, "on") == 0)
298 {
299 range_check = range_check_on;
300 range_mode = range_mode_manual;
301 }
302 else if (strcmp (range, "warn") == 0)
303 {
304 range_check = range_check_warn;
305 range_mode = range_mode_manual;
306 }
307 else if (strcmp (range, "off") == 0)
308 {
309 range_check = range_check_off;
310 range_mode = range_mode_manual;
311 }
312 else if (strcmp (range, "auto") == 0)
313 {
314 range_mode = range_mode_auto;
315 set_range_case ();
316 return;
317 }
318 else
319 {
320 internal_error (_("Unrecognized range check setting: \"%s\""), range);
321 }
322 if (range_check == range_check_warn
323 || ((range_check == range_check_on)
324 != current_language->range_checking_on_by_default ()))
325 warning (_("the current range check setting "
326 "does not match the language."));
327 }
328
329 /* Show command. Display a warning if the case sensitivity setting does
330 not match the current language. */
331 static void
332 show_case_command (struct ui_file *file, int from_tty,
333 struct cmd_list_element *c, const char *value)
334 {
335 if (case_mode == case_mode_auto)
336 {
337 const char *tmp = NULL;
338
339 switch (case_sensitivity)
340 {
341 case case_sensitive_on:
342 tmp = "on";
343 break;
344 case case_sensitive_off:
345 tmp = "off";
346 break;
347 default:
348 internal_error ("Unrecognized case-sensitive setting.");
349 }
350
351 gdb_printf (file,
352 _("Case sensitivity in "
353 "name search is \"auto; currently %s\".\n"),
354 tmp);
355 }
356 else
357 gdb_printf (file,
358 _("Case sensitivity in name search is \"%s\".\n"),
359 value);
360
361 if (case_sensitivity != current_language->case_sensitivity ())
362 warning (_("the current case sensitivity setting does not match "
363 "the language."));
364 }
365
366 /* Set command. Change the setting for case sensitivity. */
367
368 static void
369 set_case_command (const char *ignore, int from_tty, struct cmd_list_element *c)
370 {
371 if (strcmp (case_sensitive, "on") == 0)
372 {
373 case_sensitivity = case_sensitive_on;
374 case_mode = case_mode_manual;
375 }
376 else if (strcmp (case_sensitive, "off") == 0)
377 {
378 case_sensitivity = case_sensitive_off;
379 case_mode = case_mode_manual;
380 }
381 else if (strcmp (case_sensitive, "auto") == 0)
382 {
383 case_mode = case_mode_auto;
384 set_range_case ();
385 return;
386 }
387 else
388 {
389 internal_error ("Unrecognized case-sensitive setting: \"%s\"",
390 case_sensitive);
391 }
392
393 if (case_sensitivity != current_language->case_sensitivity ())
394 warning (_("the current case sensitivity setting does not match "
395 "the language."));
396 }
397
398 /* Set the status of range and type checking and case sensitivity based on
399 the current modes and the current language.
400 If SHOW is non-zero, then print out the current language,
401 type and range checking status. */
402 static void
403 set_range_case (void)
404 {
405 if (range_mode == range_mode_auto)
406 range_check = (current_language->range_checking_on_by_default ()
407 ? range_check_on : range_check_off);
408
409 if (case_mode == case_mode_auto)
410 case_sensitivity = current_language->case_sensitivity ();
411 }
412
413 /* See language.h. */
414
415 void
416 set_language (enum language lang)
417 {
418 lazy_language_setter = nullptr;
419 global_current_language = language_def (lang);
420 set_range_case ();
421 }
422 \f
423
424 /* See language.h. */
425
426 void
427 language_info ()
428 {
429 if (expected_language == current_language)
430 return;
431
432 expected_language = current_language;
433 gdb_printf (_("Current language: %s\n"), get_language ());
434 show_language_command (gdb_stdout, 1, NULL, NULL);
435 }
436 \f
437 /* This page contains functions for the printing out of
438 error messages that occur during type- and range-
439 checking. */
440
441 /* This is called when a language fails a range-check. The
442 first argument should be a printf()-style format string, and the
443 rest of the arguments should be its arguments. If range_check is
444 range_check_on, an error is printed; if range_check_warn, a warning;
445 otherwise just the message. */
446
447 void
448 range_error (const char *string,...)
449 {
450 va_list args;
451
452 va_start (args, string);
453 switch (range_check)
454 {
455 case range_check_warn:
456 vwarning (string, args);
457 break;
458 case range_check_on:
459 verror (string, args);
460 break;
461 case range_check_off:
462 /* FIXME: cagney/2002-01-30: Should this function print anything
463 when range error is off? */
464 gdb_vprintf (gdb_stderr, string, args);
465 gdb_printf (gdb_stderr, "\n");
466 break;
467 default:
468 internal_error (_("bad switch"));
469 }
470 va_end (args);
471 }
472 \f
473
474 /* This page contains miscellaneous functions. */
475
476 /* Return the language enum for a given language string. */
477
478 enum language
479 language_enum (const char *str)
480 {
481 for (const auto &lang : language_defn::languages)
482 if (strcmp (lang->name (), str) == 0)
483 return lang->la_language;
484
485 return language_unknown;
486 }
487
488 /* Return the language struct for a given language enum. */
489
490 const struct language_defn *
491 language_def (enum language lang)
492 {
493 const struct language_defn *l = language_defn::languages[lang];
494 gdb_assert (l != nullptr);
495 return l;
496 }
497
498 /* Return the language as a string. */
499
500 const char *
501 language_str (enum language lang)
502 {
503 return language_def (lang)->name ();
504 }
505
506 \f
507
508 /* Build and install the "set language LANG" command. */
509
510 static void
511 add_set_language_command ()
512 {
513 static const char **language_names;
514
515 /* Build the language names array, to be used as enumeration in the
516 "set language" enum command. +3 for "auto", "local" and NULL
517 termination. */
518 language_names = new const char *[ARRAY_SIZE (language_defn::languages) + 3];
519
520 /* Display "auto", "local" and "unknown" first, and then the rest,
521 alpha sorted. */
522 const char **language_names_p = language_names;
523 *language_names_p++ = "auto";
524 *language_names_p++ = "local";
525 *language_names_p++ = language_def (language_unknown)->name ();
526 const char **sort_begin = language_names_p;
527 for (const auto &lang : language_defn::languages)
528 {
529 /* Already handled above. */
530 if (lang->la_language == language_unknown)
531 continue;
532 *language_names_p++ = lang->name ();
533 }
534 *language_names_p = NULL;
535 std::sort (sort_begin, language_names_p, compare_cstrings);
536
537 /* Add the filename extensions. */
538 for (const auto &lang : language_defn::languages)
539 for (const char * const &ext : lang->filename_extensions ())
540 add_filename_language (ext, lang->la_language);
541
542 /* Build the "help set language" docs. */
543 string_file doc;
544
545 doc.printf (_("Set the current source language.\n"
546 "The currently understood settings are:\n\nlocal or "
547 "auto Automatic setting based on source file"));
548
549 for (const auto &lang : language_defn::languages)
550 {
551 /* Already dealt with these above. */
552 if (lang->la_language == language_unknown)
553 continue;
554
555 /* Note that we add the newline at the front, so we don't wind
556 up with a trailing newline. */
557 doc.printf ("\n%-16s Use the %s language",
558 lang->name (),
559 lang->natural_name ());
560 }
561
562 add_setshow_enum_cmd ("language", class_support,
563 language_names,
564 doc.c_str (),
565 _("Show the current source language."),
566 NULL,
567 set_language,
568 get_language,
569 show_language_command,
570 &setlist, &showlist);
571 }
572
573 /* Iterate through all registered languages looking for and calling
574 any non-NULL struct language_defn.skip_trampoline() functions.
575 Return the result from the first that returns non-zero, or 0 if all
576 `fail'. */
577 CORE_ADDR
578 skip_language_trampoline (const frame_info_ptr &frame, CORE_ADDR pc)
579 {
580 for (const auto &lang : language_defn::languages)
581 {
582 CORE_ADDR real_pc = lang->skip_trampoline (frame, pc);
583
584 if (real_pc != 0)
585 return real_pc;
586 }
587
588 return 0;
589 }
590
591 /* Return information about whether TYPE should be passed
592 (and returned) by reference at the language level. */
593
594 struct language_pass_by_ref_info
595 language_pass_by_reference (struct type *type)
596 {
597 return current_language->pass_by_reference_info (type);
598 }
599
600 /* Return the default string containing the list of characters
601 delimiting words. This is a reasonable default value that
602 most languages should be able to use. */
603
604 const char *
605 default_word_break_characters (void)
606 {
607 return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
608 }
609
610 /* See language.h. */
611
612 void
613 language_defn::print_array_index (struct type *index_type, LONGEST index,
614 struct ui_file *stream,
615 const value_print_options *options) const
616 {
617 struct value *index_value = value_from_longest (index_type, index);
618
619 gdb_printf (stream, "[");
620 value_print (index_value, stream, options);
621 gdb_printf (stream, "] = ");
622 }
623
624 /* See language.h. */
625
626 gdb::unique_xmalloc_ptr<char>
627 language_defn::watch_location_expression (struct type *type,
628 CORE_ADDR addr) const
629 {
630 /* Generates an expression that assumes a C like syntax is valid. */
631 type = check_typedef (check_typedef (type)->target_type ());
632 std::string name = type_to_string (type);
633 return xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr));
634 }
635
636 /* See language.h. */
637
638 void
639 language_defn::value_print (struct value *val, struct ui_file *stream,
640 const struct value_print_options *options) const
641 {
642 return c_value_print (val, stream, options);
643 }
644
645 /* See language.h. */
646
647 int
648 language_defn::parser (struct parser_state *ps) const
649 {
650 return c_parse (ps);
651 }
652
653 /* See language.h. */
654
655 void
656 language_defn::value_print_inner
657 (struct value *val, struct ui_file *stream, int recurse,
658 const struct value_print_options *options) const
659 {
660 return c_value_print_inner (val, stream, recurse, options);
661 }
662
663 /* See language.h. */
664
665 void
666 language_defn::print_typedef (struct type *type, struct symbol *new_symbol,
667 struct ui_file *stream) const
668 {
669 c_print_typedef (type, new_symbol, stream);
670 }
671
672 /* See language.h. */
673
674 bool
675 language_defn::is_string_type_p (struct type *type) const
676 {
677 return c_is_string_type_p (type);
678 }
679
680 /* The default implementation of the get_symbol_name_matcher_inner method
681 from the language_defn class. Matches with strncmp_iw. */
682
683 static bool
684 default_symbol_name_matcher (const char *symbol_search_name,
685 const lookup_name_info &lookup_name,
686 completion_match_result *comp_match_res)
687 {
688 std::string_view name = lookup_name.name ();
689 completion_match_for_lcd *match_for_lcd
690 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
691 strncmp_iw_mode mode = (lookup_name.completion_mode ()
692 ? strncmp_iw_mode::NORMAL
693 : strncmp_iw_mode::MATCH_PARAMS);
694
695 if (strncmp_iw_with_mode (symbol_search_name, name.data (), name.size (),
696 mode, language_minimal, match_for_lcd) == 0)
697 {
698 if (comp_match_res != NULL)
699 comp_match_res->set_match (symbol_search_name);
700 return true;
701 }
702 else
703 return false;
704 }
705
706 /* See language.h. */
707
708 symbol_name_matcher_ftype *
709 language_defn::get_symbol_name_matcher
710 (const lookup_name_info &lookup_name) const
711 {
712 /* If currently in Ada mode, and the lookup name is wrapped in
713 '<...>', hijack all symbol name comparisons using the Ada
714 matcher, which handles the verbatim matching. */
715 if (current_language->la_language == language_ada
716 && lookup_name.ada ().verbatim_p ())
717 return current_language->get_symbol_name_matcher_inner (lookup_name);
718
719 return this->get_symbol_name_matcher_inner (lookup_name);
720 }
721
722 /* See language.h. */
723
724 symbol_name_matcher_ftype *
725 language_defn::get_symbol_name_matcher_inner
726 (const lookup_name_info &lookup_name) const
727 {
728 return default_symbol_name_matcher;
729 }
730
731 /* See language.h. */
732
733 const struct lang_varobj_ops *
734 language_defn::varobj_ops () const
735 {
736 /* The ops for the C language are suitable for the vast majority of the
737 supported languages. */
738 return &c_varobj_ops;
739 }
740
741 /* Class representing the "unknown" language. */
742
743 class unknown_language : public language_defn
744 {
745 public:
746 unknown_language () : language_defn (language_unknown)
747 { /* Nothing. */ }
748
749 /* See language.h. */
750 void language_arch_info (struct gdbarch *gdbarch,
751 struct language_arch_info *lai) const override
752 {
753 lai->set_string_char_type (builtin_type (gdbarch)->builtin_char);
754 lai->set_bool_type (builtin_type (gdbarch)->builtin_int);
755 }
756
757 /* See language.h. */
758
759 void print_type (struct type *type, const char *varstring,
760 struct ui_file *stream, int show, int level,
761 const struct type_print_options *flags) const override
762 {
763 error (_("type printing not implemented for language \"%s\""),
764 natural_name ());
765 }
766
767 /* See language.h. */
768
769 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
770 int options) const override
771 {
772 /* The auto language just uses the C++ demangler. */
773 return gdb_demangle (mangled, options);
774 }
775
776 /* See language.h. */
777
778 void value_print (struct value *val, struct ui_file *stream,
779 const struct value_print_options *options) const override
780 {
781 error (_("value printing not implemented for language \"%s\""),
782 natural_name ());
783 }
784
785 /* See language.h. */
786
787 void value_print_inner
788 (struct value *val, struct ui_file *stream, int recurse,
789 const struct value_print_options *options) const override
790 {
791 error (_("inner value printing not implemented for language \"%s\""),
792 natural_name ());
793 }
794
795 /* See language.h. */
796
797 int parser (struct parser_state *ps) const override
798 {
799 error (_("expression parsing not implemented for language \"%s\""),
800 natural_name ());
801 }
802
803 /* See language.h. */
804
805 void emitchar (int ch, struct type *chtype,
806 struct ui_file *stream, int quoter) const override
807 {
808 error (_("emit character not implemented for language \"%s\""),
809 natural_name ());
810 }
811
812 /* See language.h. */
813
814 void printchar (int ch, struct type *chtype,
815 struct ui_file *stream) const override
816 {
817 error (_("print character not implemented for language \"%s\""),
818 natural_name ());
819 }
820
821 /* See language.h. */
822
823 void printstr (struct ui_file *stream, struct type *elttype,
824 const gdb_byte *string, unsigned int length,
825 const char *encoding, int force_ellipses,
826 const struct value_print_options *options) const override
827 {
828 error (_("print string not implemented for language \"%s\""),
829 natural_name ());
830 }
831
832 /* See language.h. */
833
834 void print_typedef (struct type *type, struct symbol *new_symbol,
835 struct ui_file *stream) const override
836 {
837 error (_("print typedef not implemented for language \"%s\""),
838 natural_name ());
839 }
840
841 /* See language.h. */
842
843 bool is_string_type_p (struct type *type) const override
844 {
845 type = check_typedef (type);
846 while (type->code () == TYPE_CODE_REF)
847 {
848 type = type->target_type ();
849 type = check_typedef (type);
850 }
851 return (type->code () == TYPE_CODE_STRING);
852 }
853
854 /* See language.h. */
855
856 const char *name_of_this () const override
857 { return "this"; }
858
859 /* See language.h. */
860
861 const char *name () const override
862 { return "unknown"; }
863
864 /* See language.h. */
865
866 const char *natural_name () const override
867 { return "Unknown"; }
868
869 /* See language.h. */
870
871 bool store_sym_names_in_linkage_form_p () const override
872 { return true; }
873 };
874
875 /* Single instance of the unknown language class. */
876
877 static unknown_language unknown_language_defn;
878
879 \f
880 /* Per-architecture language information. */
881
882 struct language_gdbarch
883 {
884 /* A vector of per-language per-architecture info. Indexed by "enum
885 language". */
886 struct language_arch_info arch_info[nr_languages];
887 };
888
889 static const registry<gdbarch>::key<language_gdbarch> language_gdbarch_data;
890
891 static language_gdbarch *
892 get_language_gdbarch (struct gdbarch *gdbarch)
893 {
894 struct language_gdbarch *l = language_gdbarch_data.get (gdbarch);
895 if (l == nullptr)
896 {
897 l = new struct language_gdbarch;
898 for (const auto &lang : language_defn::languages)
899 {
900 gdb_assert (lang != nullptr);
901 lang->language_arch_info (gdbarch, &l->arch_info[lang->la_language]);
902 }
903 language_gdbarch_data.set (gdbarch, l);
904 }
905
906 return l;
907 }
908
909 /* See language.h. */
910
911 struct type *
912 language_string_char_type (const struct language_defn *la,
913 struct gdbarch *gdbarch)
914 {
915 struct language_gdbarch *ld = get_language_gdbarch (gdbarch);
916 return ld->arch_info[la->la_language].string_char_type ();
917 }
918
919 /* See language.h. */
920
921 struct value *
922 language_defn::value_string (struct gdbarch *gdbarch,
923 const char *ptr, ssize_t len) const
924 {
925 struct type *type = language_string_char_type (this, gdbarch);
926 return value_cstring (ptr, len, type);
927 }
928
929 /* See language.h. */
930
931 struct type *
932 language_bool_type (const struct language_defn *la,
933 struct gdbarch *gdbarch)
934 {
935 struct language_gdbarch *ld = get_language_gdbarch (gdbarch);
936 return ld->arch_info[la->la_language].bool_type ();
937 }
938
939 /* See language.h. */
940
941 struct type *
942 language_arch_info::bool_type () const
943 {
944 if (m_bool_type_name != nullptr)
945 {
946 struct symbol *sym;
947
948 sym = lookup_symbol (m_bool_type_name, nullptr, SEARCH_TYPE_DOMAIN,
949 nullptr).symbol;
950 if (sym != nullptr)
951 {
952 struct type *type = sym->type ();
953 if (type != nullptr && type->code () == TYPE_CODE_BOOL)
954 return type;
955 }
956 }
957
958 return m_bool_type_default;
959 }
960
961 /* See language.h. */
962
963 struct symbol *
964 language_arch_info::type_and_symbol::alloc_type_symbol
965 (enum language lang, struct type *type)
966 {
967 struct symbol *symbol;
968 struct gdbarch *gdbarch;
969 gdb_assert (!type->is_objfile_owned ());
970 gdbarch = type->arch_owner ();
971 symbol = new (gdbarch_obstack (gdbarch)) struct symbol ();
972 symbol->m_name = type->name ();
973 symbol->set_language (lang, nullptr);
974 symbol->owner.arch = gdbarch;
975 symbol->set_is_objfile_owned (0);
976 symbol->set_section_index (0);
977 symbol->set_type (type);
978 symbol->set_domain (TYPE_DOMAIN);
979 symbol->set_aclass_index (LOC_TYPEDEF);
980 return symbol;
981 }
982
983 /* See language.h. */
984
985 language_arch_info::type_and_symbol *
986 language_arch_info::lookup_primitive_type_and_symbol (const char *name)
987 {
988 for (struct type_and_symbol &tas : primitive_types_and_symbols)
989 {
990 if (strcmp (tas.type ()->name (), name) == 0)
991 return &tas;
992 }
993
994 return nullptr;
995 }
996
997 /* See language.h. */
998
999 struct type *
1000 language_arch_info::lookup_primitive_type (const char *name)
1001 {
1002 type_and_symbol *tas = lookup_primitive_type_and_symbol (name);
1003 if (tas != nullptr)
1004 return tas->type ();
1005 return nullptr;
1006 }
1007
1008 /* See language.h. */
1009
1010 struct type *
1011 language_arch_info::lookup_primitive_type
1012 (gdb::function_view<bool (struct type *)> filter)
1013 {
1014 for (struct type_and_symbol &tas : primitive_types_and_symbols)
1015 {
1016 if (filter (tas.type ()))
1017 return tas.type ();
1018 }
1019
1020 return nullptr;
1021 }
1022
1023 /* See language.h. */
1024
1025 struct symbol *
1026 language_arch_info::lookup_primitive_type_as_symbol (const char *name,
1027 enum language lang)
1028 {
1029 type_and_symbol *tas = lookup_primitive_type_and_symbol (name);
1030 if (tas != nullptr)
1031 return tas->symbol (lang);
1032 return nullptr;
1033 }
1034
1035 /* Helper for the language_lookup_primitive_type overloads to forward
1036 to the corresponding language's lookup_primitive_type overload. */
1037
1038 template<typename T>
1039 static struct type *
1040 language_lookup_primitive_type_1 (const struct language_defn *la,
1041 struct gdbarch *gdbarch,
1042 T arg)
1043 {
1044 struct language_gdbarch *ld = get_language_gdbarch (gdbarch);
1045 return ld->arch_info[la->la_language].lookup_primitive_type (arg);
1046 }
1047
1048 /* See language.h. */
1049
1050 struct type *
1051 language_lookup_primitive_type (const struct language_defn *la,
1052 struct gdbarch *gdbarch,
1053 const char *name)
1054 {
1055 return language_lookup_primitive_type_1 (la, gdbarch, name);
1056 }
1057
1058 /* See language.h. */
1059
1060 struct type *
1061 language_lookup_primitive_type (const struct language_defn *la,
1062 struct gdbarch *gdbarch,
1063 gdb::function_view<bool (struct type *)> filter)
1064 {
1065 return language_lookup_primitive_type_1 (la, gdbarch, filter);
1066 }
1067
1068 /* See language.h. */
1069
1070 struct symbol *
1071 language_lookup_primitive_type_as_symbol (const struct language_defn *la,
1072 struct gdbarch *gdbarch,
1073 const char *name)
1074 {
1075 struct language_gdbarch *ld = get_language_gdbarch (gdbarch);
1076 struct language_arch_info *lai = &ld->arch_info[la->la_language];
1077
1078 symbol_lookup_debug_printf
1079 ("language = \"%s\", gdbarch @ %s, type = \"%s\")",
1080 la->name (), host_address_to_string (gdbarch), name);
1081
1082 struct symbol *sym
1083 = lai->lookup_primitive_type_as_symbol (name, la->la_language);
1084
1085 symbol_lookup_debug_printf ("found symbol @ %s",
1086 host_address_to_string (sym));
1087
1088 /* Note: The result of symbol lookup is normally a symbol *and* the block
1089 it was found in. Builtin types don't live in blocks. We *could* give
1090 them one, but there is no current need so to keep things simple symbol
1091 lookup is extended to allow for BLOCK_FOUND to be NULL. */
1092
1093 return sym;
1094 }
1095
1096 /* Initialize the language routines. */
1097
1098 INIT_GDB_FILE (language)
1099 {
1100 static const char *const type_or_range_names[]
1101 = { "on", "off", "warn", "auto", NULL };
1102
1103 static const char *const case_sensitive_names[]
1104 = { "on", "off", "auto", NULL };
1105
1106 /* GDB commands for language specific stuff. */
1107
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);
1118
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);
1128
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);
1139
1140 add_setshow_boolean_cmd ("warn-language-frame-mismatch", class_obscure,
1141 &warn_frame_lang_mismatch, _("\
1142 Enable or disable the frame language-mismatch warning."),
1143 _("\
1144 Show the current setting of the frame language-mismatch warning."),
1145 _("\
1146 The frame-language-mismatch warning is issued when the current language\n\
1147 does not match the selected frame's language."), nullptr, nullptr,
1148 &setlist, &showlist);
1149
1150 add_set_language_command ();
1151 }