]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/language.c
gdb: delete SYMBOL_SECTION and MSYMBOL_SECTION macros
[thirdparty/binutils-gdb.git] / gdb / language.c
1 /* Multiple source language support for GDB.
2
3 Copyright (C) 1991-2021 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 (gdb_stdout,
118 _("The current source language is "
119 "\"auto; currently %s\".\n"),
120 current_language->name ());
121 else
122 fprintf_filtered (gdb_stdout,
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 printf_filtered ("%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 (gdb_stdout,
224 _("Range checking is \"auto; currently %s\".\n"),
225 tmp);
226 }
227 else
228 fprintf_filtered (gdb_stdout, _("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 (gdb_stdout,
300 _("Case sensitivity in "
301 "name search is \"auto; currently %s\".\n"),
302 tmp);
303 }
304 else
305 fprintf_filtered (gdb_stdout,
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 /* Print out the current language settings: language, range and
378 type checking. If QUIETLY, print only what has changed. */
379
380 void
381 language_info (int quietly)
382 {
383 if (quietly && expected_language == current_language)
384 return;
385
386 expected_language = current_language;
387 printf_unfiltered (_("Current language: %s\n"), language);
388 show_language_command (NULL, 1, NULL, NULL);
389
390 if (!quietly)
391 {
392 printf_unfiltered (_("Range checking: %s\n"), range);
393 show_range_command (NULL, 1, NULL, NULL);
394 printf_unfiltered (_("Case sensitivity: %s\n"), case_sensitive);
395 show_case_command (NULL, 1, NULL, NULL);
396 }
397 }
398 \f
399
400 /* Returns non-zero if the value is a pointer type. */
401 int
402 pointer_type (struct type *type)
403 {
404 return type->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type);
405 }
406
407 \f
408 /* This page contains functions that return info about
409 (struct value) values used in GDB. */
410
411 /* Returns non-zero if the value VAL represents a true value. */
412 int
413 value_true (struct value *val)
414 {
415 /* It is possible that we should have some sort of error if a non-boolean
416 value is used in this context. Possibly dependent on some kind of
417 "boolean-checking" option like range checking. But it should probably
418 not depend on the language except insofar as is necessary to identify
419 a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
420 should be an error, probably). */
421 return !value_logical_not (val);
422 }
423 \f
424 /* This page contains functions for the printing out of
425 error messages that occur during type- and range-
426 checking. */
427
428 /* This is called when a language fails a range-check. The
429 first argument should be a printf()-style format string, and the
430 rest of the arguments should be its arguments. If range_check is
431 range_check_on, an error is printed; if range_check_warn, a warning;
432 otherwise just the message. */
433
434 void
435 range_error (const char *string,...)
436 {
437 va_list args;
438
439 va_start (args, string);
440 switch (range_check)
441 {
442 case range_check_warn:
443 vwarning (string, args);
444 break;
445 case range_check_on:
446 verror (string, args);
447 break;
448 case range_check_off:
449 /* FIXME: cagney/2002-01-30: Should this function print anything
450 when range error is off? */
451 vfprintf_filtered (gdb_stderr, string, args);
452 fprintf_filtered (gdb_stderr, "\n");
453 break;
454 default:
455 internal_error (__FILE__, __LINE__, _("bad switch"));
456 }
457 va_end (args);
458 }
459 \f
460
461 /* This page contains miscellaneous functions. */
462
463 /* Return the language enum for a given language string. */
464
465 enum language
466 language_enum (const char *str)
467 {
468 for (const auto &lang : language_defn::languages)
469 if (strcmp (lang->name (), str) == 0)
470 return lang->la_language;
471
472 if (strcmp (str, "local") == 0)
473 return language_auto;
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. +1 for "local" and +1 for NULL
507 termination. */
508 language_names = new const char *[ARRAY_SIZE (language_defn::languages) + 2];
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++ = language_def (language_auto)->name ();
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_auto
521 || lang->la_language == language_unknown)
522 continue;
523 *language_names_p++ = lang->name ();
524 }
525 *language_names_p = NULL;
526 std::sort (sort_begin, language_names_p, compare_cstrings);
527
528 /* Add the filename extensions. */
529 for (const auto &lang : language_defn::languages)
530 for (const char * const &ext : lang->filename_extensions ())
531 add_filename_language (ext, lang->la_language);
532
533 /* Build the "help set language" docs. */
534 string_file doc;
535
536 doc.printf (_("Set the current source language.\n"
537 "The currently understood settings are:\n\nlocal or "
538 "auto Automatic setting based on source file"));
539
540 for (const auto &lang : language_defn::languages)
541 {
542 /* Already dealt with these above. */
543 if (lang->la_language == language_unknown
544 || lang->la_language == language_auto)
545 continue;
546
547 /* Note that we add the newline at the front, so we don't wind
548 up with a trailing newline. */
549 doc.printf ("\n%-16s Use the %s language",
550 lang->name (),
551 lang->natural_name ());
552 }
553
554 add_setshow_enum_cmd ("language", class_support,
555 language_names,
556 &language,
557 doc.c_str (),
558 _("Show the current source language."),
559 NULL, set_language_command,
560 show_language_command,
561 &setlist, &showlist);
562 }
563
564 /* Iterate through all registered languages looking for and calling
565 any non-NULL struct language_defn.skip_trampoline() functions.
566 Return the result from the first that returns non-zero, or 0 if all
567 `fail'. */
568 CORE_ADDR
569 skip_language_trampoline (struct frame_info *frame, CORE_ADDR pc)
570 {
571 for (const auto &lang : language_defn::languages)
572 {
573 CORE_ADDR real_pc = lang->skip_trampoline (frame, pc);
574
575 if (real_pc != 0)
576 return real_pc;
577 }
578
579 return 0;
580 }
581
582 /* Return demangled language symbol, or NULL.
583 FIXME: Options are only useful for certain languages and ignored
584 by others, so it would be better to remove them here and have a
585 more flexible demangler for the languages that need it.
586 FIXME: Sometimes the demangler is invoked when we don't know the
587 language, so we can't use this everywhere. */
588 char *
589 language_demangle (const struct language_defn *current_language,
590 const char *mangled, int options)
591 {
592 if (current_language != NULL)
593 return current_language->demangle_symbol (mangled, options);
594 return NULL;
595 }
596
597 /* Return information about whether TYPE should be passed
598 (and returned) by reference at the language level. */
599
600 struct language_pass_by_ref_info
601 language_pass_by_reference (struct type *type)
602 {
603 return current_language->pass_by_reference_info (type);
604 }
605
606 /* Return the default string containing the list of characters
607 delimiting words. This is a reasonable default value that
608 most languages should be able to use. */
609
610 const char *
611 default_word_break_characters (void)
612 {
613 return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
614 }
615
616 /* See language.h. */
617
618 void
619 language_defn::print_array_index (struct type *index_type, LONGEST index,
620 struct ui_file *stream,
621 const value_print_options *options) const
622 {
623 struct value *index_value = value_from_longest (index_type, index);
624
625 fprintf_filtered (stream, "[");
626 value_print (index_value, stream, options);
627 fprintf_filtered (stream, "] = ");
628 }
629
630 /* See language.h. */
631
632 gdb::unique_xmalloc_ptr<char>
633 language_defn::watch_location_expression (struct type *type,
634 CORE_ADDR addr) const
635 {
636 /* Generates an expression that assumes a C like syntax is valid. */
637 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
638 std::string name = type_to_string (type);
639 return gdb::unique_xmalloc_ptr<char>
640 (xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr)));
641 }
642
643 /* See language.h. */
644
645 void
646 language_defn::value_print (struct value *val, struct ui_file *stream,
647 const struct value_print_options *options) const
648 {
649 return c_value_print (val, stream, options);
650 }
651
652 /* See language.h. */
653
654 int
655 language_defn::parser (struct parser_state *ps) const
656 {
657 return c_parse (ps);
658 }
659
660 /* See language.h. */
661
662 void
663 language_defn::value_print_inner
664 (struct value *val, struct ui_file *stream, int recurse,
665 const struct value_print_options *options) const
666 {
667 return c_value_print_inner (val, stream, recurse, options);
668 }
669
670 /* See language.h. */
671
672 void
673 language_defn::emitchar (int ch, struct type *chtype,
674 struct ui_file * stream, int quoter) const
675 {
676 c_emit_char (ch, chtype, stream, quoter);
677 }
678
679 /* See language.h. */
680
681 void
682 language_defn::printstr (struct ui_file *stream, struct type *elttype,
683 const gdb_byte *string, unsigned int length,
684 const char *encoding, int force_ellipses,
685 const struct value_print_options *options) const
686 {
687 c_printstr (stream, elttype, string, length, encoding, force_ellipses,
688 options);
689 }
690
691 /* See language.h. */
692
693 void
694 language_defn::print_typedef (struct type *type, struct symbol *new_symbol,
695 struct ui_file *stream) const
696 {
697 c_print_typedef (type, new_symbol, stream);
698 }
699
700 /* See language.h. */
701
702 bool
703 language_defn::is_string_type_p (struct type *type) const
704 {
705 return c_is_string_type_p (type);
706 }
707
708 /* See language.h. */
709
710 std::unique_ptr<compile_instance>
711 language_defn::get_compile_instance () const
712 {
713 return {};
714 }
715
716 /* The default implementation of the get_symbol_name_matcher_inner method
717 from the language_defn class. Matches with strncmp_iw. */
718
719 static bool
720 default_symbol_name_matcher (const char *symbol_search_name,
721 const lookup_name_info &lookup_name,
722 completion_match_result *comp_match_res)
723 {
724 gdb::string_view name = lookup_name.name ();
725 completion_match_for_lcd *match_for_lcd
726 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
727 strncmp_iw_mode mode = (lookup_name.completion_mode ()
728 ? strncmp_iw_mode::NORMAL
729 : strncmp_iw_mode::MATCH_PARAMS);
730
731 if (strncmp_iw_with_mode (symbol_search_name, name.data (), name.size (),
732 mode, language_minimal, match_for_lcd) == 0)
733 {
734 if (comp_match_res != NULL)
735 comp_match_res->set_match (symbol_search_name);
736 return true;
737 }
738 else
739 return false;
740 }
741
742 /* See language.h. */
743
744 symbol_name_matcher_ftype *
745 language_defn::get_symbol_name_matcher
746 (const lookup_name_info &lookup_name) const
747 {
748 /* If currently in Ada mode, and the lookup name is wrapped in
749 '<...>', hijack all symbol name comparisons using the Ada
750 matcher, which handles the verbatim matching. */
751 if (current_language->la_language == language_ada
752 && lookup_name.ada ().verbatim_p ())
753 return current_language->get_symbol_name_matcher_inner (lookup_name);
754
755 return this->get_symbol_name_matcher_inner (lookup_name);
756 }
757
758 /* See language.h. */
759
760 symbol_name_matcher_ftype *
761 language_defn::get_symbol_name_matcher_inner
762 (const lookup_name_info &lookup_name) const
763 {
764 return default_symbol_name_matcher;
765 }
766
767 /* See language.h. */
768
769 const struct lang_varobj_ops *
770 language_defn::varobj_ops () const
771 {
772 /* The ops for the C language are suitable for the vast majority of the
773 supported languages. */
774 return &c_varobj_ops;
775 }
776
777 /* See language.h. */
778
779 const struct exp_descriptor *
780 language_defn::expression_ops () const
781 {
782 return &exp_descriptor_standard;
783 }
784
785 /* Parent class for both the "auto" and "unknown" languages. These two
786 pseudo-languages are very similar so merging their implementations like
787 this makes sense. */
788
789 class auto_or_unknown_language : public language_defn
790 {
791 public:
792 auto_or_unknown_language (enum language lang)
793 : language_defn (lang)
794 { /* Nothing. */ }
795
796 /* See language.h. */
797 void language_arch_info (struct gdbarch *gdbarch,
798 struct language_arch_info *lai) const override
799 {
800 lai->set_string_char_type (builtin_type (gdbarch)->builtin_char);
801 lai->set_bool_type (builtin_type (gdbarch)->builtin_int);
802 }
803
804 /* See language.h. */
805
806 void print_type (struct type *type, const char *varstring,
807 struct ui_file *stream, int show, int level,
808 const struct type_print_options *flags) const override
809 {
810 error (_("type printing not implemented for language \"%s\""),
811 natural_name ());
812 }
813
814 /* See language.h. */
815
816 char *demangle_symbol (const char *mangled, int options) const override
817 {
818 /* The auto language just uses the C++ demangler. */
819 return gdb_demangle (mangled, options);
820 }
821
822 /* See language.h. */
823
824 void value_print (struct value *val, struct ui_file *stream,
825 const struct value_print_options *options) const override
826 {
827 error (_("value printing not implemented for language \"%s\""),
828 natural_name ());
829 }
830
831 /* See language.h. */
832
833 void value_print_inner
834 (struct value *val, struct ui_file *stream, int recurse,
835 const struct value_print_options *options) const override
836 {
837 error (_("inner value printing not implemented for language \"%s\""),
838 natural_name ());
839 }
840
841 /* See language.h. */
842
843 int parser (struct parser_state *ps) const override
844 {
845 /* No parsing is done, just claim success. */
846 return 1;
847 }
848
849 /* See language.h. */
850
851 void emitchar (int ch, struct type *chtype,
852 struct ui_file *stream, int quoter) const override
853 {
854 error (_("emit character not implemented for language \"%s\""),
855 natural_name ());
856 }
857
858 /* See language.h. */
859
860 void printchar (int ch, struct type *chtype,
861 struct ui_file *stream) const override
862 {
863 error (_("print character not implemented for language \"%s\""),
864 natural_name ());
865 }
866
867 /* See language.h. */
868
869 void printstr (struct ui_file *stream, struct type *elttype,
870 const gdb_byte *string, unsigned int length,
871 const char *encoding, int force_ellipses,
872 const struct value_print_options *options) const override
873 {
874 error (_("print string not implemented for language \"%s\""),
875 natural_name ());
876 }
877
878 /* See language.h. */
879
880 void print_typedef (struct type *type, struct symbol *new_symbol,
881 struct ui_file *stream) const override
882 {
883 error (_("print typedef not implemented for language \"%s\""),
884 natural_name ());
885 }
886
887 /* See language.h. */
888
889 bool is_string_type_p (struct type *type) const override
890 {
891 type = check_typedef (type);
892 while (type->code () == TYPE_CODE_REF)
893 {
894 type = TYPE_TARGET_TYPE (type);
895 type = check_typedef (type);
896 }
897 return (type->code () == TYPE_CODE_STRING);
898 }
899
900 /* See language.h. */
901
902 const char *name_of_this () const override
903 { return "this"; }
904
905 /* See language.h. */
906
907 const struct op_print *opcode_print_table () const override
908 {
909 static const struct op_print unk_op_print_tab[] =
910 {
911 {NULL, OP_NULL, PREC_NULL, 0}
912 };
913
914 return unk_op_print_tab;
915 }
916 };
917
918 /* Class representing the fake "auto" language. */
919
920 class auto_language : public auto_or_unknown_language
921 {
922 public:
923 auto_language ()
924 : auto_or_unknown_language (language_auto)
925 { /* Nothing. */ }
926
927 /* See language.h. */
928
929 const char *name () const override
930 { return "auto"; }
931
932 /* See language.h. */
933
934 const char *natural_name () const override
935 { return "Auto"; }
936 };
937
938 /* Single instance of the fake "auto" language. */
939
940 static auto_language auto_language_defn;
941
942 /* Class representing the unknown language. */
943
944 class unknown_language : public auto_or_unknown_language
945 {
946 public:
947 unknown_language ()
948 : auto_or_unknown_language (language_unknown)
949 { /* Nothing. */ }
950
951 /* See language.h. */
952
953 const char *name () const override
954 { return "unknown"; }
955
956 /* See language.h. */
957
958 const char *natural_name () const override
959 { return "Unknown"; }
960
961 /* See language.h. */
962
963 bool store_sym_names_in_linkage_form_p () const override
964 { return true; }
965 };
966
967 /* Single instance of the unknown language class. */
968
969 static unknown_language unknown_language_defn;
970
971 \f
972 /* Per-architecture language information. */
973
974 static struct gdbarch_data *language_gdbarch_data;
975
976 struct language_gdbarch
977 {
978 /* A vector of per-language per-architecture info. Indexed by "enum
979 language". */
980 struct language_arch_info arch_info[nr_languages];
981 };
982
983 static void *
984 language_gdbarch_post_init (struct gdbarch *gdbarch)
985 {
986 struct language_gdbarch *l
987 = obstack_new<struct language_gdbarch> (gdbarch_obstack (gdbarch));
988 for (const auto &lang : language_defn::languages)
989 {
990 gdb_assert (lang != nullptr);
991 lang->language_arch_info (gdbarch, &l->arch_info[lang->la_language]);
992 }
993
994 return l;
995 }
996
997 /* See language.h. */
998
999 struct type *
1000 language_string_char_type (const struct language_defn *la,
1001 struct gdbarch *gdbarch)
1002 {
1003 struct language_gdbarch *ld
1004 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1005 return ld->arch_info[la->la_language].string_char_type ();
1006 }
1007
1008 /* See language.h. */
1009
1010 struct type *
1011 language_bool_type (const struct language_defn *la,
1012 struct gdbarch *gdbarch)
1013 {
1014 struct language_gdbarch *ld
1015 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1016 return ld->arch_info[la->la_language].bool_type ();
1017 }
1018
1019 /* See language.h. */
1020
1021 struct type *
1022 language_arch_info::bool_type () const
1023 {
1024 if (m_bool_type_name != nullptr)
1025 {
1026 struct symbol *sym;
1027
1028 sym = lookup_symbol (m_bool_type_name, NULL, VAR_DOMAIN, NULL).symbol;
1029 if (sym != nullptr)
1030 {
1031 struct type *type = SYMBOL_TYPE (sym);
1032 if (type != nullptr && type->code () == TYPE_CODE_BOOL)
1033 return type;
1034 }
1035 }
1036
1037 return m_bool_type_default;
1038 }
1039
1040 /* See language.h. */
1041
1042 struct symbol *
1043 language_arch_info::type_and_symbol::alloc_type_symbol
1044 (enum language lang, struct type *type)
1045 {
1046 struct symbol *symbol;
1047 struct gdbarch *gdbarch;
1048 gdb_assert (!type->is_objfile_owned ());
1049 gdbarch = type->arch_owner ();
1050 symbol = new (gdbarch_obstack (gdbarch)) struct symbol ();
1051 symbol->m_name = type->name ();
1052 symbol->set_language (lang, nullptr);
1053 symbol->owner.arch = gdbarch;
1054 SYMBOL_OBJFILE_OWNED (symbol) = 0;
1055 symbol->set_section_index (0);
1056 SYMBOL_TYPE (symbol) = type;
1057 SYMBOL_DOMAIN (symbol) = VAR_DOMAIN;
1058 SYMBOL_ACLASS_INDEX (symbol) = LOC_TYPEDEF;
1059 return symbol;
1060 }
1061
1062 /* See language.h. */
1063
1064 language_arch_info::type_and_symbol *
1065 language_arch_info::lookup_primitive_type_and_symbol (const char *name)
1066 {
1067 for (struct type_and_symbol &tas : primitive_types_and_symbols)
1068 {
1069 if (strcmp (tas.type ()->name (), name) == 0)
1070 return &tas;
1071 }
1072
1073 return nullptr;
1074 }
1075
1076 /* See language.h. */
1077
1078 struct type *
1079 language_arch_info::lookup_primitive_type (const char *name)
1080 {
1081 type_and_symbol *tas = lookup_primitive_type_and_symbol (name);
1082 if (tas != nullptr)
1083 return tas->type ();
1084 return nullptr;
1085 }
1086
1087 /* See language.h. */
1088
1089 struct type *
1090 language_arch_info::lookup_primitive_type
1091 (gdb::function_view<bool (struct type *)> filter)
1092 {
1093 for (struct type_and_symbol &tas : primitive_types_and_symbols)
1094 {
1095 if (filter (tas.type ()))
1096 return tas.type ();
1097 }
1098
1099 return nullptr;
1100 }
1101
1102 /* See language.h. */
1103
1104 struct symbol *
1105 language_arch_info::lookup_primitive_type_as_symbol (const char *name,
1106 enum language lang)
1107 {
1108 type_and_symbol *tas = lookup_primitive_type_and_symbol (name);
1109 if (tas != nullptr)
1110 return tas->symbol (lang);
1111 return nullptr;
1112 }
1113
1114 /* Helper for the language_lookup_primitive_type overloads to forward
1115 to the corresponding language's lookup_primitive_type overload. */
1116
1117 template<typename T>
1118 static struct type *
1119 language_lookup_primitive_type_1 (const struct language_defn *la,
1120 struct gdbarch *gdbarch,
1121 T arg)
1122 {
1123 struct language_gdbarch *ld =
1124 (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1125 return ld->arch_info[la->la_language].lookup_primitive_type (arg);
1126 }
1127
1128 /* See language.h. */
1129
1130 struct type *
1131 language_lookup_primitive_type (const struct language_defn *la,
1132 struct gdbarch *gdbarch,
1133 const char *name)
1134 {
1135 return language_lookup_primitive_type_1 (la, gdbarch, name);
1136 }
1137
1138 /* See language.h. */
1139
1140 struct type *
1141 language_lookup_primitive_type (const struct language_defn *la,
1142 struct gdbarch *gdbarch,
1143 gdb::function_view<bool (struct type *)> filter)
1144 {
1145 return language_lookup_primitive_type_1 (la, gdbarch, filter);
1146 }
1147
1148 /* See language.h. */
1149
1150 struct symbol *
1151 language_lookup_primitive_type_as_symbol (const struct language_defn *la,
1152 struct gdbarch *gdbarch,
1153 const char *name)
1154 {
1155 struct language_gdbarch *ld
1156 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1157 struct language_arch_info *lai = &ld->arch_info[la->la_language];
1158
1159 if (symbol_lookup_debug)
1160 fprintf_unfiltered (gdb_stdlog,
1161 "language_lookup_primitive_type_as_symbol"
1162 " (%s, %s, %s)",
1163 la->name (), host_address_to_string (gdbarch), name);
1164
1165 struct symbol *sym
1166 = lai->lookup_primitive_type_as_symbol (name, la->la_language);
1167
1168 if (symbol_lookup_debug)
1169 fprintf_unfiltered (gdb_stdlog, " = %s\n", host_address_to_string (sym));
1170
1171 /* Note: The result of symbol lookup is normally a symbol *and* the block
1172 it was found in. Builtin types don't live in blocks. We *could* give
1173 them one, but there is no current need so to keep things simple symbol
1174 lookup is extended to allow for BLOCK_FOUND to be NULL. */
1175
1176 return sym;
1177 }
1178
1179 /* Initialize the language routines. */
1180
1181 void _initialize_language ();
1182 void
1183 _initialize_language ()
1184 {
1185 static const char *const type_or_range_names[]
1186 = { "on", "off", "warn", "auto", NULL };
1187
1188 static const char *const case_sensitive_names[]
1189 = { "on", "off", "auto", NULL };
1190
1191 language_gdbarch_data
1192 = gdbarch_data_register_post_init (language_gdbarch_post_init);
1193
1194 /* GDB commands for language specific stuff. */
1195
1196 add_basic_prefix_cmd ("check", no_class,
1197 _("Set the status of the type/range checker."),
1198 &setchecklist, "set check ", 0, &setlist);
1199 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1200 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1201
1202 add_show_prefix_cmd ("check", no_class,
1203 _("Show the status of the type/range checker."),
1204 &showchecklist, "show check ", 0, &showlist);
1205 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1206 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1207
1208 add_setshow_enum_cmd ("range", class_support, type_or_range_names,
1209 &range,
1210 _("Set range checking (on/warn/off/auto)."),
1211 _("Show range checking (on/warn/off/auto)."),
1212 NULL, set_range_command,
1213 show_range_command,
1214 &setchecklist, &showchecklist);
1215
1216 add_setshow_enum_cmd ("case-sensitive", class_support, case_sensitive_names,
1217 &case_sensitive, _("\
1218 Set case sensitivity in name search (on/off/auto)."), _("\
1219 Show case sensitivity in name search (on/off/auto)."), _("\
1220 For Fortran the default is off; for other languages the default is on."),
1221 set_case_command,
1222 show_case_command,
1223 &setlist, &showlist);
1224
1225 /* In order to call SET_LANGUAGE (below) we need to make sure that
1226 CURRENT_LANGUAGE is not NULL. So first set the language to unknown,
1227 then we can change the language to 'auto'. */
1228 current_language = language_def (language_unknown);
1229
1230 add_set_language_command ();
1231
1232 language = "auto";
1233 range = "auto";
1234 case_sensitive = "auto";
1235
1236 /* Have the above take effect. */
1237 set_language (language_auto);
1238 }