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