gdb: Convert language_data::la_op_print_tab to a method
[deliverable/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 void set_range_case (void);
51
52 /* range_mode ==
53 range_mode_auto: range_check set automatically to default of language.
54 range_mode_manual: range_check set manually by user. */
55
56 enum range_mode
57 {
58 range_mode_auto, range_mode_manual
59 };
60
61 /* case_mode ==
62 case_mode_auto: case_sensitivity set upon selection of scope.
63 case_mode_manual: case_sensitivity set only by user. */
64
65 enum case_mode
66 {
67 case_mode_auto, case_mode_manual
68 };
69
70 /* The current (default at startup) state of type and range checking.
71 (If the modes are set to "auto", though, these are changed based
72 on the default language at startup, and then again based on the
73 language of the first source file. */
74
75 static enum range_mode range_mode = range_mode_auto;
76 enum range_check range_check = range_check_off;
77 static enum case_mode case_mode = case_mode_auto;
78 enum case_sensitivity case_sensitivity = case_sensitive_on;
79
80 /* The current language and language_mode (see language.h). */
81
82 const struct language_defn *current_language = nullptr;
83 enum language_mode language_mode = language_mode_auto;
84
85 /* The language that the user expects to be typing in (the language
86 of main(), or the last language we notified them about, or C). */
87
88 const struct language_defn *expected_language;
89
90 /* Define the array containing all languages. */
91
92 const struct language_defn *language_defn::languages[nr_languages];
93
94 /* The current values of the "set language/range/case-sensitive" enum
95 commands. */
96 static const char *language;
97 static const char *range;
98 static const char *case_sensitive;
99
100 /* See language.h. */
101 const char lang_frame_mismatch_warn[] =
102 N_("Warning: the current language does not match this frame.");
103 \f
104 /* This page contains the functions corresponding to GDB commands
105 and their helpers. */
106
107 /* Show command. Display a warning if the language set
108 does not match the frame. */
109 static void
110 show_language_command (struct ui_file *file, int from_tty,
111 struct cmd_list_element *c, const char *value)
112 {
113 enum language flang; /* The language of the frame. */
114
115 if (language_mode == language_mode_auto)
116 fprintf_filtered (gdb_stdout,
117 _("The current source language is "
118 "\"auto; currently %s\".\n"),
119 current_language->name ());
120 else
121 fprintf_filtered (gdb_stdout,
122 _("The current source language is \"%s\".\n"),
123 current_language->name ());
124
125 if (has_stack_frames ())
126 {
127 struct frame_info *frame;
128
129 frame = get_selected_frame (NULL);
130 flang = get_frame_language (frame);
131 if (flang != language_unknown
132 && language_mode == language_mode_manual
133 && current_language->la_language != flang)
134 printf_filtered ("%s\n", _(lang_frame_mismatch_warn));
135 }
136 }
137
138 /* Set command. Change the current working language. */
139 static void
140 set_language_command (const char *ignore,
141 int from_tty, struct cmd_list_element *c)
142 {
143 enum language flang = language_unknown;
144
145 /* "local" is a synonym of "auto". */
146 if (strcmp (language, "local") == 0)
147 language = "auto";
148
149 /* Search the list of languages for a match. */
150 for (const auto &lang : language_defn::languages)
151 {
152 if (strcmp (lang->name (), language) == 0)
153 {
154 /* Found it! Go into manual mode, and use this language. */
155 if (lang->la_language == language_auto)
156 {
157 /* Enter auto mode. Set to the current frame's language, if
158 known, or fallback to the initial language. */
159 language_mode = language_mode_auto;
160 try
161 {
162 struct frame_info *frame;
163
164 frame = get_selected_frame (NULL);
165 flang = get_frame_language (frame);
166 }
167 catch (const gdb_exception_error &ex)
168 {
169 flang = language_unknown;
170 }
171
172 if (flang != language_unknown)
173 set_language (flang);
174 else
175 set_initial_language ();
176 expected_language = current_language;
177 return;
178 }
179 else
180 {
181 /* Enter manual mode. Set the specified language. */
182 language_mode = language_mode_manual;
183 current_language = lang;
184 set_range_case ();
185 expected_language = current_language;
186 return;
187 }
188 }
189 }
190
191 internal_error (__FILE__, __LINE__,
192 "Couldn't find language `%s' in known languages list.",
193 language);
194 }
195
196 /* Show command. Display a warning if the range setting does
197 not match the current language. */
198 static void
199 show_range_command (struct ui_file *file, int from_tty,
200 struct cmd_list_element *c, const char *value)
201 {
202 if (range_mode == range_mode_auto)
203 {
204 const char *tmp;
205
206 switch (range_check)
207 {
208 case range_check_on:
209 tmp = "on";
210 break;
211 case range_check_off:
212 tmp = "off";
213 break;
214 case range_check_warn:
215 tmp = "warn";
216 break;
217 default:
218 internal_error (__FILE__, __LINE__,
219 "Unrecognized range check setting.");
220 }
221
222 fprintf_filtered (gdb_stdout,
223 _("Range checking is \"auto; currently %s\".\n"),
224 tmp);
225 }
226 else
227 fprintf_filtered (gdb_stdout, _("Range checking is \"%s\".\n"),
228 value);
229
230 if (range_check == range_check_warn
231 || ((range_check == range_check_on)
232 != current_language->range_checking_on_by_default ()))
233 warning (_("the current range check setting "
234 "does not match the language.\n"));
235 }
236
237 /* Set command. Change the setting for range checking. */
238 static void
239 set_range_command (const char *ignore,
240 int from_tty, struct cmd_list_element *c)
241 {
242 if (strcmp (range, "on") == 0)
243 {
244 range_check = range_check_on;
245 range_mode = range_mode_manual;
246 }
247 else if (strcmp (range, "warn") == 0)
248 {
249 range_check = range_check_warn;
250 range_mode = range_mode_manual;
251 }
252 else if (strcmp (range, "off") == 0)
253 {
254 range_check = range_check_off;
255 range_mode = range_mode_manual;
256 }
257 else if (strcmp (range, "auto") == 0)
258 {
259 range_mode = range_mode_auto;
260 set_range_case ();
261 return;
262 }
263 else
264 {
265 internal_error (__FILE__, __LINE__,
266 _("Unrecognized range check setting: \"%s\""), range);
267 }
268 if (range_check == range_check_warn
269 || ((range_check == range_check_on)
270 != current_language->range_checking_on_by_default ()))
271 warning (_("the current range check setting "
272 "does not match the language.\n"));
273 }
274
275 /* Show command. Display a warning if the case sensitivity setting does
276 not match the current language. */
277 static void
278 show_case_command (struct ui_file *file, int from_tty,
279 struct cmd_list_element *c, const char *value)
280 {
281 if (case_mode == case_mode_auto)
282 {
283 const char *tmp = NULL;
284
285 switch (case_sensitivity)
286 {
287 case case_sensitive_on:
288 tmp = "on";
289 break;
290 case case_sensitive_off:
291 tmp = "off";
292 break;
293 default:
294 internal_error (__FILE__, __LINE__,
295 "Unrecognized case-sensitive setting.");
296 }
297
298 fprintf_filtered (gdb_stdout,
299 _("Case sensitivity in "
300 "name search is \"auto; currently %s\".\n"),
301 tmp);
302 }
303 else
304 fprintf_filtered (gdb_stdout,
305 _("Case sensitivity in name search is \"%s\".\n"),
306 value);
307
308 if (case_sensitivity != current_language->case_sensitivity ())
309 warning (_("the current case sensitivity setting does not match "
310 "the language.\n"));
311 }
312
313 /* Set command. Change the setting for case sensitivity. */
314
315 static void
316 set_case_command (const char *ignore, int from_tty, struct cmd_list_element *c)
317 {
318 if (strcmp (case_sensitive, "on") == 0)
319 {
320 case_sensitivity = case_sensitive_on;
321 case_mode = case_mode_manual;
322 }
323 else if (strcmp (case_sensitive, "off") == 0)
324 {
325 case_sensitivity = case_sensitive_off;
326 case_mode = case_mode_manual;
327 }
328 else if (strcmp (case_sensitive, "auto") == 0)
329 {
330 case_mode = case_mode_auto;
331 set_range_case ();
332 return;
333 }
334 else
335 {
336 internal_error (__FILE__, __LINE__,
337 "Unrecognized case-sensitive setting: \"%s\"",
338 case_sensitive);
339 }
340
341 if (case_sensitivity != current_language->case_sensitivity ())
342 warning (_("the current case sensitivity setting does not match "
343 "the language.\n"));
344 }
345
346 /* Set the status of range and type checking and case sensitivity based on
347 the current modes and the current language.
348 If SHOW is non-zero, then print out the current language,
349 type and range checking status. */
350 static void
351 set_range_case (void)
352 {
353 if (range_mode == range_mode_auto)
354 range_check = (current_language->range_checking_on_by_default ()
355 ? range_check_on : range_check_off);
356
357 if (case_mode == case_mode_auto)
358 case_sensitivity = current_language->case_sensitivity ();
359 }
360
361 /* Set current language to (enum language) LANG. Returns previous
362 language. */
363
364 enum language
365 set_language (enum language lang)
366 {
367 enum language prev_language;
368
369 prev_language = current_language->la_language;
370 current_language = language_def (lang);
371 set_range_case ();
372 return prev_language;
373 }
374 \f
375
376 /* Print out the current language settings: language, range and
377 type checking. If QUIETLY, print only what has changed. */
378
379 void
380 language_info (int quietly)
381 {
382 if (quietly && expected_language == current_language)
383 return;
384
385 expected_language = current_language;
386 printf_unfiltered (_("Current language: %s\n"), language);
387 show_language_command (NULL, 1, NULL, NULL);
388
389 if (!quietly)
390 {
391 printf_unfiltered (_("Range checking: %s\n"), range);
392 show_range_command (NULL, 1, NULL, NULL);
393 printf_unfiltered (_("Case sensitivity: %s\n"), case_sensitive);
394 show_case_command (NULL, 1, NULL, NULL);
395 }
396 }
397 \f
398
399 /* Returns non-zero if the value is a pointer type. */
400 int
401 pointer_type (struct type *type)
402 {
403 return type->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type);
404 }
405
406 \f
407 /* This page contains functions that return info about
408 (struct value) values used in GDB. */
409
410 /* Returns non-zero if the value VAL represents a true value. */
411 int
412 value_true (struct value *val)
413 {
414 /* It is possible that we should have some sort of error if a non-boolean
415 value is used in this context. Possibly dependent on some kind of
416 "boolean-checking" option like range checking. But it should probably
417 not depend on the language except insofar as is necessary to identify
418 a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
419 should be an error, probably). */
420 return !value_logical_not (val);
421 }
422 \f
423 /* This page contains functions for the printing out of
424 error messages that occur during type- and range-
425 checking. */
426
427 /* This is called when a language fails a range-check. The
428 first argument should be a printf()-style format string, and the
429 rest of the arguments should be its arguments. If range_check is
430 range_check_on, an error is printed; if range_check_warn, a warning;
431 otherwise just the message. */
432
433 void
434 range_error (const char *string,...)
435 {
436 va_list args;
437
438 va_start (args, string);
439 switch (range_check)
440 {
441 case range_check_warn:
442 vwarning (string, args);
443 break;
444 case range_check_on:
445 verror (string, args);
446 break;
447 case range_check_off:
448 /* FIXME: cagney/2002-01-30: Should this function print anything
449 when range error is off? */
450 vfprintf_filtered (gdb_stderr, string, args);
451 fprintf_filtered (gdb_stderr, "\n");
452 break;
453 default:
454 internal_error (__FILE__, __LINE__, _("bad switch"));
455 }
456 va_end (args);
457 }
458 \f
459
460 /* This page contains miscellaneous functions. */
461
462 /* Return the language enum for a given language string. */
463
464 enum language
465 language_enum (const char *str)
466 {
467 for (const auto &lang : language_defn::languages)
468 if (strcmp (lang->name (), str) == 0)
469 return lang->la_language;
470
471 if (strcmp (str, "local") == 0)
472 return language_auto;
473
474 return language_unknown;
475 }
476
477 /* Return the language struct for a given language enum. */
478
479 const struct language_defn *
480 language_def (enum language lang)
481 {
482 const struct language_defn *l = language_defn::languages[lang];
483 gdb_assert (l != nullptr);
484 return l;
485 }
486
487 /* Return the language as a string. */
488
489 const char *
490 language_str (enum language lang)
491 {
492 return language_def (lang)->name ();
493 }
494
495 \f
496
497 /* Build and install the "set language LANG" command. */
498
499 static void
500 add_set_language_command ()
501 {
502 static const char **language_names;
503
504 /* Build the language names array, to be used as enumeration in the
505 "set language" enum command. +1 for "local" and +1 for NULL
506 termination. */
507 language_names = new const char *[ARRAY_SIZE (language_defn::languages) + 2];
508
509 /* Display "auto", "local" and "unknown" first, and then the rest,
510 alpha sorted. */
511 const char **language_names_p = language_names;
512 *language_names_p++ = language_def (language_auto)->name ();
513 *language_names_p++ = "local";
514 *language_names_p++ = language_def (language_unknown)->name ();
515 const char **sort_begin = language_names_p;
516 for (const auto &lang : language_defn::languages)
517 {
518 /* Already handled above. */
519 if (lang->la_language == language_auto
520 || lang->la_language == language_unknown)
521 continue;
522 *language_names_p++ = lang->name ();
523 }
524 *language_names_p = NULL;
525 std::sort (sort_begin, language_names_p, compare_cstrings);
526
527 /* Add the filename extensions. */
528 for (const auto &lang : language_defn::languages)
529 for (const char * const &ext : lang->filename_extensions ())
530 add_filename_language (ext, lang->la_language);
531
532 /* Build the "help set language" docs. */
533 string_file doc;
534
535 doc.printf (_("Set the current source language.\n"
536 "The currently understood settings are:\n\nlocal or "
537 "auto Automatic setting based on source file"));
538
539 for (const auto &lang : language_defn::languages)
540 {
541 /* Already dealt with these above. */
542 if (lang->la_language == language_unknown
543 || lang->la_language == language_auto)
544 continue;
545
546 /* Note that we add the newline at the front, so we don't wind
547 up with a trailing newline. */
548 doc.printf ("\n%-16s Use the %s language",
549 lang->name (),
550 lang->natural_name ());
551 }
552
553 add_setshow_enum_cmd ("language", class_support,
554 language_names,
555 &language,
556 doc.c_str (),
557 _("Show the current source language."),
558 NULL, set_language_command,
559 show_language_command,
560 &setlist, &showlist);
561 }
562
563 /* Iterate through all registered languages looking for and calling
564 any non-NULL struct language_defn.skip_trampoline() functions.
565 Return the result from the first that returns non-zero, or 0 if all
566 `fail'. */
567 CORE_ADDR
568 skip_language_trampoline (struct frame_info *frame, CORE_ADDR pc)
569 {
570 for (const auto &lang : language_defn::languages)
571 {
572 CORE_ADDR real_pc = lang->skip_trampoline (frame, pc);
573
574 if (real_pc != 0)
575 return real_pc;
576 }
577
578 return 0;
579 }
580
581 /* Return demangled language symbol, or NULL.
582 FIXME: Options are only useful for certain languages and ignored
583 by others, so it would be better to remove them here and have a
584 more flexible demangler for the languages that need it.
585 FIXME: Sometimes the demangler is invoked when we don't know the
586 language, so we can't use this everywhere. */
587 char *
588 language_demangle (const struct language_defn *current_language,
589 const char *mangled, int options)
590 {
591 if (current_language != NULL)
592 return current_language->demangle (mangled, options);
593 return NULL;
594 }
595
596 /* Return information about whether TYPE should be passed
597 (and returned) by reference at the language level. */
598
599 struct language_pass_by_ref_info
600 language_pass_by_reference (struct type *type)
601 {
602 return current_language->pass_by_reference_info (type);
603 }
604
605 /* Return the default string containing the list of characters
606 delimiting words. This is a reasonable default value that
607 most languages should be able to use. */
608
609 const char *
610 default_word_break_characters (void)
611 {
612 return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
613 }
614
615 /* See language.h. */
616
617 void
618 language_defn::print_array_index (struct type *index_type, LONGEST index,
619 struct ui_file *stream,
620 const value_print_options *options) const
621 {
622 struct value *index_value = value_from_longest (index_type, index);
623
624 fprintf_filtered (stream, "[");
625 LA_VALUE_PRINT (index_value, stream, options);
626 fprintf_filtered (stream, "] = ");
627 }
628
629 /* See language.h. */
630
631 gdb::unique_xmalloc_ptr<char>
632 language_defn::watch_location_expression (struct type *type,
633 CORE_ADDR addr) const
634 {
635 /* Generates an expression that assumes a C like syntax is valid. */
636 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
637 std::string name = type_to_string (type);
638 return gdb::unique_xmalloc_ptr<char>
639 (xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr)));
640 }
641
642 /* See language.h. */
643
644 void
645 language_defn::value_print (struct value *val, struct ui_file *stream,
646 const struct value_print_options *options) const
647 {
648 return c_value_print (val, stream, options);
649 }
650
651 /* See language.h. */
652
653 int
654 language_defn::parser (struct parser_state *ps) const
655 {
656 return c_parse (ps);
657 }
658
659 /* See language.h. */
660
661 void
662 language_defn::value_print_inner
663 (struct value *val, struct ui_file *stream, int recurse,
664 const struct value_print_options *options) const
665 {
666 return c_value_print_inner (val, stream, recurse, options);
667 }
668
669 /* See language.h. */
670
671 void
672 language_defn::emitchar (int ch, struct type *chtype,
673 struct ui_file * stream, int quoter) const
674 {
675 c_emit_char (ch, chtype, stream, quoter);
676 }
677
678 /* See language.h. */
679
680 void
681 language_defn::printchar (int ch, struct type *chtype,
682 struct ui_file * stream) const
683 {
684 c_printchar (ch, chtype, stream);
685 }
686
687 /* See language.h. */
688
689 void
690 language_defn::printstr (struct ui_file *stream, struct type *elttype,
691 const gdb_byte *string, unsigned int length,
692 const char *encoding, int force_ellipses,
693 const struct value_print_options *options) const
694 {
695 c_printstr (stream, elttype, string, length, encoding, force_ellipses,
696 options);
697 }
698
699 /* See language.h. */
700
701 void
702 language_defn::print_typedef (struct type *type, struct symbol *new_symbol,
703 struct ui_file *stream) const
704 {
705 c_print_typedef (type, new_symbol, stream);
706 }
707
708 /* See language.h. */
709
710 bool
711 language_defn::is_string_type_p (struct type *type) const
712 {
713 return c_is_string_type_p (type);
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 /* Return true if TYPE is a string type, otherwise return false. This
786 default implementation only detects TYPE_CODE_STRING. */
787
788 static bool
789 default_is_string_type_p (struct type *type)
790 {
791 type = check_typedef (type);
792 while (type->code () == TYPE_CODE_REF)
793 {
794 type = TYPE_TARGET_TYPE (type);
795 type = check_typedef (type);
796 }
797 return (type->code () == TYPE_CODE_STRING);
798 }
799
800 static const struct op_print unk_op_print_tab[] =
801 {
802 {NULL, OP_NULL, PREC_NULL, 0}
803 };
804
805 static void
806 unknown_language_arch_info (struct gdbarch *gdbarch,
807 struct language_arch_info *lai)
808 {
809 lai->string_char_type = builtin_type (gdbarch)->builtin_char;
810 lai->bool_type_default = builtin_type (gdbarch)->builtin_int;
811 lai->primitive_type_vector = GDBARCH_OBSTACK_CALLOC (gdbarch, 1,
812 struct type *);
813 }
814
815 /* Constant data that describes the unknown language. */
816
817 extern const struct language_data unknown_language_data =
818 {
819 };
820
821 /* Class representing the unknown language. */
822
823 class unknown_language : public language_defn
824 {
825 public:
826 unknown_language ()
827 : language_defn (language_unknown, unknown_language_data)
828 { /* Nothing. */ }
829
830 /* See language.h. */
831
832 const char *name () const override
833 { return "unknown"; }
834
835 /* See language.h. */
836
837 const char *natural_name () const override
838 { return "Unknown"; }
839
840 /* See language.h. */
841 void language_arch_info (struct gdbarch *gdbarch,
842 struct language_arch_info *lai) const override
843 {
844 unknown_language_arch_info (gdbarch, lai);
845 }
846
847 /* See language.h. */
848
849 void print_type (struct type *type, const char *varstring,
850 struct ui_file *stream, int show, int level,
851 const struct type_print_options *flags) const override
852 {
853 error (_("unimplemented unknown_language::print_type called"));
854 }
855
856 /* See language.h. */
857
858 char *demangle (const char *mangled, int options) const override
859 {
860 /* The unknown language just uses the C++ demangler. */
861 return gdb_demangle (mangled, options);
862 }
863
864 /* See language.h. */
865
866 void value_print (struct value *val, struct ui_file *stream,
867 const struct value_print_options *options) const override
868 {
869 error (_("unimplemented unknown_language::value_print called"));
870 }
871
872 /* See language.h. */
873
874 void value_print_inner
875 (struct value *val, struct ui_file *stream, int recurse,
876 const struct value_print_options *options) const override
877 {
878 error (_("unimplemented unknown_language::value_print_inner called"));
879 }
880
881 /* See language.h. */
882
883 int parser (struct parser_state *ps) const override
884 {
885 /* No parsing is done, just claim success. */
886 return 1;
887 }
888
889 /* See language.h. */
890
891 void emitchar (int ch, struct type *chtype,
892 struct ui_file *stream, int quoter) const override
893 {
894 error (_("unimplemented unknown_language::emitchar called"));
895 }
896
897 /* See language.h. */
898
899 void printchar (int ch, struct type *chtype,
900 struct ui_file *stream) const override
901 {
902 error (_("unimplemented unknown_language::printchar called"));
903 }
904
905 /* See language.h. */
906
907 void printstr (struct ui_file *stream, struct type *elttype,
908 const gdb_byte *string, unsigned int length,
909 const char *encoding, int force_ellipses,
910 const struct value_print_options *options) const override
911 {
912 error (_("unimplemented unknown_language::printstr called"));
913 }
914
915 /* See language.h. */
916
917 void print_typedef (struct type *type, struct symbol *new_symbol,
918 struct ui_file *stream) const override
919 {
920 error (_("unimplemented unknown_language::print_typedef called"));
921 }
922
923 /* See language.h. */
924
925 bool is_string_type_p (struct type *type) const override
926 {
927 return default_is_string_type_p (type);
928 }
929
930 /* See language.h. */
931
932 const char *name_of_this () const override
933 { return "this"; }
934
935 /* See language.h. */
936
937 bool store_sym_names_in_linkage_form_p () const override
938 { return true; }
939
940 /* See language.h. */
941
942 const struct op_print *opcode_print_table () const override
943 { return unk_op_print_tab; }
944 };
945
946 /* Single instance of the unknown language class. */
947
948 static unknown_language unknown_language_defn;
949
950 /* Constant data for the fake "auto" language. */
951
952 extern const struct language_data auto_language_data =
953 {
954 };
955
956 /* Class representing the fake "auto" language. */
957
958 class auto_language : public language_defn
959 {
960 public:
961 auto_language ()
962 : language_defn (language_auto, auto_language_data)
963 { /* Nothing. */ }
964
965 /* See language.h. */
966
967 const char *name () const override
968 { return "auto"; }
969
970 /* See language.h. */
971
972 const char *natural_name () const override
973 { return "Auto"; }
974
975 /* See language.h. */
976 void language_arch_info (struct gdbarch *gdbarch,
977 struct language_arch_info *lai) const override
978 {
979 unknown_language_arch_info (gdbarch, lai);
980 }
981
982 /* See language.h. */
983
984 void print_type (struct type *type, const char *varstring,
985 struct ui_file *stream, int show, int level,
986 const struct type_print_options *flags) const override
987 {
988 error (_("unimplemented auto_language::print_type called"));
989 }
990
991 /* See language.h. */
992
993 char *demangle (const char *mangled, int options) const override
994 {
995 /* The auto language just uses the C++ demangler. */
996 return gdb_demangle (mangled, options);
997 }
998
999 /* See language.h. */
1000
1001 void value_print (struct value *val, struct ui_file *stream,
1002 const struct value_print_options *options) const override
1003 {
1004 error (_("unimplemented auto_language::value_print called"));
1005 }
1006
1007 /* See language.h. */
1008
1009 void value_print_inner
1010 (struct value *val, struct ui_file *stream, int recurse,
1011 const struct value_print_options *options) const override
1012 {
1013 error (_("unimplemented auto_language::value_print_inner called"));
1014 }
1015
1016 /* See language.h. */
1017
1018 int parser (struct parser_state *ps) const override
1019 {
1020 /* No parsing is done, just claim success. */
1021 return 1;
1022 }
1023
1024 /* See language.h. */
1025
1026 void emitchar (int ch, struct type *chtype,
1027 struct ui_file *stream, int quoter) const override
1028 {
1029 error (_("unimplemented auto_language::emitchar called"));
1030 }
1031
1032 /* See language.h. */
1033
1034 void printchar (int ch, struct type *chtype,
1035 struct ui_file *stream) const override
1036 {
1037 error (_("unimplemented auto_language::printchar called"));
1038 }
1039
1040 /* See language.h. */
1041
1042 void printstr (struct ui_file *stream, struct type *elttype,
1043 const gdb_byte *string, unsigned int length,
1044 const char *encoding, int force_ellipses,
1045 const struct value_print_options *options) const override
1046 {
1047 error (_("unimplemented auto_language::printstr called"));
1048 }
1049
1050 /* See language.h. */
1051
1052 void print_typedef (struct type *type, struct symbol *new_symbol,
1053 struct ui_file *stream) const override
1054 {
1055 error (_("unimplemented auto_language::print_typedef called"));
1056 }
1057
1058 /* See language.h. */
1059
1060 bool is_string_type_p (struct type *type) const override
1061 {
1062 return default_is_string_type_p (type);
1063 }
1064
1065 /* See language.h. */
1066
1067 const char *name_of_this () const override
1068 { return "this"; }
1069
1070 /* See language.h. */
1071
1072 const struct op_print *opcode_print_table () const override
1073 { return unk_op_print_tab; }
1074 };
1075
1076 /* Single instance of the fake "auto" language. */
1077
1078 static auto_language auto_language_defn;
1079
1080 \f
1081 /* Per-architecture language information. */
1082
1083 static struct gdbarch_data *language_gdbarch_data;
1084
1085 struct language_gdbarch
1086 {
1087 /* A vector of per-language per-architecture info. Indexed by "enum
1088 language". */
1089 struct language_arch_info arch_info[nr_languages];
1090 };
1091
1092 static void *
1093 language_gdbarch_post_init (struct gdbarch *gdbarch)
1094 {
1095 struct language_gdbarch *l;
1096
1097 l = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct language_gdbarch);
1098 for (const auto &lang : language_defn::languages)
1099 {
1100 gdb_assert (lang != nullptr);
1101 lang->language_arch_info (gdbarch,
1102 l->arch_info + lang->la_language);
1103 }
1104
1105 return l;
1106 }
1107
1108 struct type *
1109 language_string_char_type (const struct language_defn *la,
1110 struct gdbarch *gdbarch)
1111 {
1112 struct language_gdbarch *ld
1113 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1114
1115 return ld->arch_info[la->la_language].string_char_type;
1116 }
1117
1118 struct type *
1119 language_bool_type (const struct language_defn *la,
1120 struct gdbarch *gdbarch)
1121 {
1122 struct language_gdbarch *ld
1123 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1124
1125 if (ld->arch_info[la->la_language].bool_type_symbol)
1126 {
1127 struct symbol *sym;
1128
1129 sym = lookup_symbol (ld->arch_info[la->la_language].bool_type_symbol,
1130 NULL, VAR_DOMAIN, NULL).symbol;
1131 if (sym)
1132 {
1133 struct type *type = SYMBOL_TYPE (sym);
1134
1135 if (type && type->code () == TYPE_CODE_BOOL)
1136 return type;
1137 }
1138 }
1139
1140 return ld->arch_info[la->la_language].bool_type_default;
1141 }
1142
1143 /* Helper function for primitive type lookup. */
1144
1145 static struct type **
1146 language_lookup_primitive_type_1 (const struct language_arch_info *lai,
1147 const char *name)
1148 {
1149 struct type **p;
1150
1151 for (p = lai->primitive_type_vector; (*p) != NULL; p++)
1152 {
1153 if (strcmp ((*p)->name (), name) == 0)
1154 return p;
1155 }
1156 return NULL;
1157 }
1158
1159 /* See language.h. */
1160
1161 struct type *
1162 language_lookup_primitive_type (const struct language_defn *la,
1163 struct gdbarch *gdbarch,
1164 const char *name)
1165 {
1166 struct language_gdbarch *ld =
1167 (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1168 struct type **typep;
1169
1170 typep = language_lookup_primitive_type_1 (&ld->arch_info[la->la_language],
1171 name);
1172 if (typep == NULL)
1173 return NULL;
1174 return *typep;
1175 }
1176
1177 /* Helper function for type lookup as a symbol.
1178 Create the symbol corresponding to type TYPE in language LANG. */
1179
1180 static struct symbol *
1181 language_alloc_type_symbol (enum language lang, struct type *type)
1182 {
1183 struct symbol *symbol;
1184 struct gdbarch *gdbarch;
1185
1186 gdb_assert (!TYPE_OBJFILE_OWNED (type));
1187
1188 gdbarch = TYPE_OWNER (type).gdbarch;
1189 symbol = new (gdbarch_obstack (gdbarch)) struct symbol ();
1190
1191 symbol->m_name = type->name ();
1192 symbol->set_language (lang, nullptr);
1193 symbol->owner.arch = gdbarch;
1194 SYMBOL_OBJFILE_OWNED (symbol) = 0;
1195 SYMBOL_SECTION (symbol) = 0;
1196 SYMBOL_TYPE (symbol) = type;
1197 SYMBOL_DOMAIN (symbol) = VAR_DOMAIN;
1198 SYMBOL_ACLASS_INDEX (symbol) = LOC_TYPEDEF;
1199
1200 return symbol;
1201 }
1202
1203 /* Initialize the primitive type symbols of language LD.
1204 The primitive type vector must have already been initialized. */
1205
1206 static void
1207 language_init_primitive_type_symbols (struct language_arch_info *lai,
1208 const struct language_defn *la,
1209 struct gdbarch *gdbarch)
1210 {
1211 int n;
1212
1213 gdb_assert (lai->primitive_type_vector != NULL);
1214
1215 for (n = 0; lai->primitive_type_vector[n] != NULL; ++n)
1216 continue;
1217
1218 lai->primitive_type_symbols
1219 = GDBARCH_OBSTACK_CALLOC (gdbarch, n + 1, struct symbol *);
1220
1221 for (n = 0; lai->primitive_type_vector[n] != NULL; ++n)
1222 {
1223 lai->primitive_type_symbols[n]
1224 = language_alloc_type_symbol (la->la_language,
1225 lai->primitive_type_vector[n]);
1226 }
1227
1228 /* Note: The result of symbol lookup is normally a symbol *and* the block
1229 it was found in. Builtin types don't live in blocks. We *could* give
1230 them one, but there is no current need so to keep things simple symbol
1231 lookup is extended to allow for BLOCK_FOUND to be NULL. */
1232 }
1233
1234 /* See language.h. */
1235
1236 struct symbol *
1237 language_lookup_primitive_type_as_symbol (const struct language_defn *la,
1238 struct gdbarch *gdbarch,
1239 const char *name)
1240 {
1241 struct language_gdbarch *ld
1242 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1243 struct language_arch_info *lai = &ld->arch_info[la->la_language];
1244 struct type **typep;
1245 struct symbol *sym;
1246
1247 if (symbol_lookup_debug)
1248 {
1249 fprintf_unfiltered (gdb_stdlog,
1250 "language_lookup_primitive_type_as_symbol"
1251 " (%s, %s, %s)",
1252 la->name (), host_address_to_string (gdbarch), name);
1253 }
1254
1255 typep = language_lookup_primitive_type_1 (lai, name);
1256 if (typep == NULL)
1257 {
1258 if (symbol_lookup_debug)
1259 fprintf_unfiltered (gdb_stdlog, " = NULL\n");
1260 return NULL;
1261 }
1262
1263 /* The set of symbols is lazily initialized. */
1264 if (lai->primitive_type_symbols == NULL)
1265 language_init_primitive_type_symbols (lai, la, gdbarch);
1266
1267 sym = lai->primitive_type_symbols[typep - lai->primitive_type_vector];
1268
1269 if (symbol_lookup_debug)
1270 fprintf_unfiltered (gdb_stdlog, " = %s\n", host_address_to_string (sym));
1271 return sym;
1272 }
1273
1274 /* Initialize the language routines. */
1275
1276 void _initialize_language ();
1277 void
1278 _initialize_language ()
1279 {
1280 static const char *const type_or_range_names[]
1281 = { "on", "off", "warn", "auto", NULL };
1282
1283 static const char *const case_sensitive_names[]
1284 = { "on", "off", "auto", NULL };
1285
1286 language_gdbarch_data
1287 = gdbarch_data_register_post_init (language_gdbarch_post_init);
1288
1289 /* GDB commands for language specific stuff. */
1290
1291 add_basic_prefix_cmd ("check", no_class,
1292 _("Set the status of the type/range checker."),
1293 &setchecklist, "set check ", 0, &setlist);
1294 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1295 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1296
1297 add_show_prefix_cmd ("check", no_class,
1298 _("Show the status of the type/range checker."),
1299 &showchecklist, "show check ", 0, &showlist);
1300 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1301 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1302
1303 add_setshow_enum_cmd ("range", class_support, type_or_range_names,
1304 &range,
1305 _("Set range checking (on/warn/off/auto)."),
1306 _("Show range checking (on/warn/off/auto)."),
1307 NULL, set_range_command,
1308 show_range_command,
1309 &setchecklist, &showchecklist);
1310
1311 add_setshow_enum_cmd ("case-sensitive", class_support, case_sensitive_names,
1312 &case_sensitive, _("\
1313 Set case sensitivity in name search (on/off/auto)."), _("\
1314 Show case sensitivity in name search (on/off/auto)."), _("\
1315 For Fortran the default is off; for other languages the default is on."),
1316 set_case_command,
1317 show_case_command,
1318 &setlist, &showlist);
1319
1320 /* In order to call SET_LANGUAGE (below) we need to make sure that
1321 CURRENT_LANGUAGE is not NULL. So first set the language to unknown,
1322 then we can change the language to 'auto'. */
1323 current_language = language_def (language_unknown);
1324
1325 add_set_language_command ();
1326
1327 language = "auto";
1328 range = "auto";
1329 case_sensitive = "auto";
1330
1331 /* Have the above take effect. */
1332 set_language (language_auto);
1333 }
This page took 0.055141 seconds and 5 git commands to generate.