]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/language.c
gdb: Convert language la_lookup_symbol_nonlocal field to a method
[thirdparty/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 unk_op_print_tab, /* expression operators for printing */
789 1, /* c-style arrays */
790 0, /* String lower bound */
791 &default_varobj_ops,
792 default_is_string_type_p,
793 "{...}" /* la_struct_too_deep_ellipsis */
794 };
795
796 /* Class representing the unknown language. */
797
798 class unknown_language : public language_defn
799 {
800 public:
801 unknown_language ()
802 : language_defn (language_unknown, unknown_language_data)
803 { /* Nothing. */ }
804
805 /* See language.h. */
806 void language_arch_info (struct gdbarch *gdbarch,
807 struct language_arch_info *lai) const override
808 {
809 unknown_language_arch_info (gdbarch, lai);
810 }
811
812 /* See language.h. */
813
814 void print_type (struct type *type, const char *varstring,
815 struct ui_file *stream, int show, int level,
816 const struct type_print_options *flags) const override
817 {
818 error (_("unimplemented unknown_language::print_type called"));
819 }
820
821 /* See language.h. */
822
823 char *demangle (const char *mangled, int options) const override
824 {
825 /* The unknown language just uses the C++ demangler. */
826 return gdb_demangle (mangled, options);
827 }
828
829 /* See language.h. */
830
831 void value_print (struct value *val, struct ui_file *stream,
832 const struct value_print_options *options) const override
833 {
834 error (_("unimplemented unknown_language::value_print called"));
835 }
836
837 /* See language.h. */
838
839 void value_print_inner
840 (struct value *val, struct ui_file *stream, int recurse,
841 const struct value_print_options *options) const override
842 {
843 error (_("unimplemented unknown_language::value_print_inner called"));
844 }
845 };
846
847 /* Single instance of the unknown language class. */
848
849 static unknown_language unknown_language_defn;
850
851 /* Constant data for the fake "auto" language. */
852
853 extern const struct language_data auto_language_data =
854 {
855 "auto",
856 "Auto",
857 language_auto,
858 range_check_off,
859 case_sensitive_on,
860 array_row_major,
861 macro_expansion_no,
862 NULL,
863 &exp_descriptor_standard,
864 unk_lang_parser,
865 null_post_parser,
866 unk_lang_printchar, /* Print character constant */
867 unk_lang_printstr,
868 unk_lang_emit_char,
869 default_print_typedef, /* Print a typedef using appropriate syntax */
870 "this", /* name_of_this */
871 false, /* store_sym_names_in_linkage_form_p */
872 unk_op_print_tab, /* expression operators for printing */
873 1, /* c-style arrays */
874 0, /* String lower bound */
875 &default_varobj_ops,
876 default_is_string_type_p,
877 "{...}" /* la_struct_too_deep_ellipsis */
878 };
879
880 /* Class representing the fake "auto" language. */
881
882 class auto_language : public language_defn
883 {
884 public:
885 auto_language ()
886 : language_defn (language_auto, auto_language_data)
887 { /* Nothing. */ }
888
889 /* See language.h. */
890 void language_arch_info (struct gdbarch *gdbarch,
891 struct language_arch_info *lai) const override
892 {
893 unknown_language_arch_info (gdbarch, lai);
894 }
895
896 /* See language.h. */
897
898 void print_type (struct type *type, const char *varstring,
899 struct ui_file *stream, int show, int level,
900 const struct type_print_options *flags) const override
901 {
902 error (_("unimplemented auto_language::print_type called"));
903 }
904
905 /* See language.h. */
906
907 char *demangle (const char *mangled, int options) const override
908 {
909 /* The auto language just uses the C++ demangler. */
910 return gdb_demangle (mangled, options);
911 }
912
913 /* See language.h. */
914
915 void value_print (struct value *val, struct ui_file *stream,
916 const struct value_print_options *options) const override
917 {
918 error (_("unimplemented auto_language::value_print called"));
919 }
920
921 /* See language.h. */
922
923 void value_print_inner
924 (struct value *val, struct ui_file *stream, int recurse,
925 const struct value_print_options *options) const override
926 {
927 error (_("unimplemented auto_language::value_print_inner called"));
928 }
929 };
930
931 /* Single instance of the fake "auto" language. */
932
933 static auto_language auto_language_defn;
934
935 \f
936 /* Per-architecture language information. */
937
938 static struct gdbarch_data *language_gdbarch_data;
939
940 struct language_gdbarch
941 {
942 /* A vector of per-language per-architecture info. Indexed by "enum
943 language". */
944 struct language_arch_info arch_info[nr_languages];
945 };
946
947 static void *
948 language_gdbarch_post_init (struct gdbarch *gdbarch)
949 {
950 struct language_gdbarch *l;
951
952 l = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct language_gdbarch);
953 for (const auto &lang : language_defn::languages)
954 {
955 gdb_assert (lang != nullptr);
956 lang->language_arch_info (gdbarch,
957 l->arch_info + lang->la_language);
958 }
959
960 return l;
961 }
962
963 struct type *
964 language_string_char_type (const struct language_defn *la,
965 struct gdbarch *gdbarch)
966 {
967 struct language_gdbarch *ld
968 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
969
970 return ld->arch_info[la->la_language].string_char_type;
971 }
972
973 struct type *
974 language_bool_type (const struct language_defn *la,
975 struct gdbarch *gdbarch)
976 {
977 struct language_gdbarch *ld
978 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
979
980 if (ld->arch_info[la->la_language].bool_type_symbol)
981 {
982 struct symbol *sym;
983
984 sym = lookup_symbol (ld->arch_info[la->la_language].bool_type_symbol,
985 NULL, VAR_DOMAIN, NULL).symbol;
986 if (sym)
987 {
988 struct type *type = SYMBOL_TYPE (sym);
989
990 if (type && type->code () == TYPE_CODE_BOOL)
991 return type;
992 }
993 }
994
995 return ld->arch_info[la->la_language].bool_type_default;
996 }
997
998 /* Helper function for primitive type lookup. */
999
1000 static struct type **
1001 language_lookup_primitive_type_1 (const struct language_arch_info *lai,
1002 const char *name)
1003 {
1004 struct type **p;
1005
1006 for (p = lai->primitive_type_vector; (*p) != NULL; p++)
1007 {
1008 if (strcmp ((*p)->name (), name) == 0)
1009 return p;
1010 }
1011 return NULL;
1012 }
1013
1014 /* See language.h. */
1015
1016 struct type *
1017 language_lookup_primitive_type (const struct language_defn *la,
1018 struct gdbarch *gdbarch,
1019 const char *name)
1020 {
1021 struct language_gdbarch *ld =
1022 (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1023 struct type **typep;
1024
1025 typep = language_lookup_primitive_type_1 (&ld->arch_info[la->la_language],
1026 name);
1027 if (typep == NULL)
1028 return NULL;
1029 return *typep;
1030 }
1031
1032 /* Helper function for type lookup as a symbol.
1033 Create the symbol corresponding to type TYPE in language LANG. */
1034
1035 static struct symbol *
1036 language_alloc_type_symbol (enum language lang, struct type *type)
1037 {
1038 struct symbol *symbol;
1039 struct gdbarch *gdbarch;
1040
1041 gdb_assert (!TYPE_OBJFILE_OWNED (type));
1042
1043 gdbarch = TYPE_OWNER (type).gdbarch;
1044 symbol = new (gdbarch_obstack (gdbarch)) struct symbol ();
1045
1046 symbol->m_name = type->name ();
1047 symbol->set_language (lang, nullptr);
1048 symbol->owner.arch = gdbarch;
1049 SYMBOL_OBJFILE_OWNED (symbol) = 0;
1050 SYMBOL_SECTION (symbol) = 0;
1051 SYMBOL_TYPE (symbol) = type;
1052 SYMBOL_DOMAIN (symbol) = VAR_DOMAIN;
1053 SYMBOL_ACLASS_INDEX (symbol) = LOC_TYPEDEF;
1054
1055 return symbol;
1056 }
1057
1058 /* Initialize the primitive type symbols of language LD.
1059 The primitive type vector must have already been initialized. */
1060
1061 static void
1062 language_init_primitive_type_symbols (struct language_arch_info *lai,
1063 const struct language_defn *la,
1064 struct gdbarch *gdbarch)
1065 {
1066 int n;
1067
1068 gdb_assert (lai->primitive_type_vector != NULL);
1069
1070 for (n = 0; lai->primitive_type_vector[n] != NULL; ++n)
1071 continue;
1072
1073 lai->primitive_type_symbols
1074 = GDBARCH_OBSTACK_CALLOC (gdbarch, n + 1, struct symbol *);
1075
1076 for (n = 0; lai->primitive_type_vector[n] != NULL; ++n)
1077 {
1078 lai->primitive_type_symbols[n]
1079 = language_alloc_type_symbol (la->la_language,
1080 lai->primitive_type_vector[n]);
1081 }
1082
1083 /* Note: The result of symbol lookup is normally a symbol *and* the block
1084 it was found in. Builtin types don't live in blocks. We *could* give
1085 them one, but there is no current need so to keep things simple symbol
1086 lookup is extended to allow for BLOCK_FOUND to be NULL. */
1087 }
1088
1089 /* See language.h. */
1090
1091 struct symbol *
1092 language_lookup_primitive_type_as_symbol (const struct language_defn *la,
1093 struct gdbarch *gdbarch,
1094 const char *name)
1095 {
1096 struct language_gdbarch *ld
1097 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1098 struct language_arch_info *lai = &ld->arch_info[la->la_language];
1099 struct type **typep;
1100 struct symbol *sym;
1101
1102 if (symbol_lookup_debug)
1103 {
1104 fprintf_unfiltered (gdb_stdlog,
1105 "language_lookup_primitive_type_as_symbol"
1106 " (%s, %s, %s)",
1107 la->la_name, host_address_to_string (gdbarch), name);
1108 }
1109
1110 typep = language_lookup_primitive_type_1 (lai, name);
1111 if (typep == NULL)
1112 {
1113 if (symbol_lookup_debug)
1114 fprintf_unfiltered (gdb_stdlog, " = NULL\n");
1115 return NULL;
1116 }
1117
1118 /* The set of symbols is lazily initialized. */
1119 if (lai->primitive_type_symbols == NULL)
1120 language_init_primitive_type_symbols (lai, la, gdbarch);
1121
1122 sym = lai->primitive_type_symbols[typep - lai->primitive_type_vector];
1123
1124 if (symbol_lookup_debug)
1125 fprintf_unfiltered (gdb_stdlog, " = %s\n", host_address_to_string (sym));
1126 return sym;
1127 }
1128
1129 /* Initialize the language routines. */
1130
1131 void _initialize_language ();
1132 void
1133 _initialize_language ()
1134 {
1135 static const char *const type_or_range_names[]
1136 = { "on", "off", "warn", "auto", NULL };
1137
1138 static const char *const case_sensitive_names[]
1139 = { "on", "off", "auto", NULL };
1140
1141 language_gdbarch_data
1142 = gdbarch_data_register_post_init (language_gdbarch_post_init);
1143
1144 /* GDB commands for language specific stuff. */
1145
1146 add_basic_prefix_cmd ("check", no_class,
1147 _("Set the status of the type/range checker."),
1148 &setchecklist, "set check ", 0, &setlist);
1149 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1150 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1151
1152 add_show_prefix_cmd ("check", no_class,
1153 _("Show the status of the type/range checker."),
1154 &showchecklist, "show check ", 0, &showlist);
1155 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1156 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1157
1158 add_setshow_enum_cmd ("range", class_support, type_or_range_names,
1159 &range,
1160 _("Set range checking (on/warn/off/auto)."),
1161 _("Show range checking (on/warn/off/auto)."),
1162 NULL, set_range_command,
1163 show_range_command,
1164 &setchecklist, &showchecklist);
1165
1166 add_setshow_enum_cmd ("case-sensitive", class_support, case_sensitive_names,
1167 &case_sensitive, _("\
1168 Set case sensitivity in name search (on/off/auto)."), _("\
1169 Show case sensitivity in name search (on/off/auto)."), _("\
1170 For Fortran the default is off; for other languages the default is on."),
1171 set_case_command,
1172 show_case_command,
1173 &setlist, &showlist);
1174
1175 /* In order to call SET_LANGUAGE (below) we need to make sure that
1176 CURRENT_LANGUAGE is not NULL. So first set the language to unknown,
1177 then we can change the language to 'auto'. */
1178 current_language = language_def (language_unknown);
1179
1180 add_set_language_command ();
1181
1182 language = "auto";
1183 range = "auto";
1184 case_sensitive = "auto";
1185
1186 /* Have the above take effect. */
1187 set_language (language_auto);
1188 }