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