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