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