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