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