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