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