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