PR gdb/8869:
[external/binutils.git] / gdb / language.c
1 /* Multiple source language support for GDB.
2
3    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4    2002, 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6    Contributed by the Department of Computer Science at the State University
7    of New York at Buffalo.
8
9    This file is part of GDB.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
24 /* This file contains functions that return things that are specific
25    to languages.  Each function should examine current_language if necessary,
26    and return the appropriate result. */
27
28 /* FIXME:  Most of these would be better organized as macros which
29    return data out of a "language-specific" struct pointer that is set
30    whenever the working language changes.  That would be a lot faster.  */
31
32 #include "defs.h"
33 #include <ctype.h>
34 #include "gdb_string.h"
35
36 #include "symtab.h"
37 #include "gdbtypes.h"
38 #include "value.h"
39 #include "gdbcmd.h"
40 #include "expression.h"
41 #include "language.h"
42 #include "target.h"
43 #include "parser-defs.h"
44 #include "jv-lang.h"
45 #include "demangle.h"
46 #include "symfile.h"
47
48 extern void _initialize_language (void);
49
50 static void set_case_str (void);
51
52 static void set_range_str (void);
53
54 static void set_type_str (void);
55
56 static void set_lang_str (void);
57
58 static void unk_lang_error (char *);
59
60 static int unk_lang_parser (void);
61
62 static void show_check (char *, int);
63
64 static void set_check (char *, int);
65
66 static void set_type_range_case (void);
67
68 static void unk_lang_emit_char (int c, struct type *type,
69                                 struct ui_file *stream, int quoter);
70
71 static void unk_lang_printchar (int c, struct type *type,
72                                 struct ui_file *stream);
73
74 static void unk_lang_print_type (struct type *, char *, struct ui_file *,
75                                  int, int);
76
77 static int unk_lang_value_print (struct value *, struct ui_file *,
78                                  const struct value_print_options *);
79
80 static CORE_ADDR unk_lang_trampoline (struct frame_info *, CORE_ADDR pc);
81
82 /* Forward declaration */
83 extern const struct language_defn unknown_language_defn;
84
85 /* The current (default at startup) state of type and range checking.
86    (If the modes are set to "auto", though, these are changed based
87    on the default language at startup, and then again based on the
88    language of the first source file.  */
89
90 enum range_mode range_mode = range_mode_auto;
91 enum range_check range_check = range_check_off;
92 enum type_mode type_mode = type_mode_auto;
93 enum type_check type_check = type_check_off;
94 enum case_mode case_mode = case_mode_auto;
95 enum case_sensitivity case_sensitivity = case_sensitive_on;
96
97 /* The current language and language_mode (see language.h) */
98
99 const struct language_defn *current_language = &unknown_language_defn;
100 enum language_mode language_mode = language_mode_auto;
101
102 /* The language that the user expects to be typing in (the language
103    of main(), or the last language we notified them about, or C).  */
104
105 const struct language_defn *expected_language;
106
107 /* The list of supported languages.  The list itself is malloc'd.  */
108
109 static const struct language_defn **languages;
110 static unsigned languages_size;
111 static unsigned languages_allocsize;
112 #define DEFAULT_ALLOCSIZE 4
113
114 /* The "set language/type/range" commands all put stuff in these
115    buffers.  This is to make them work as set/show commands.  The
116    user's string is copied here, then the set_* commands look at
117    them and update them to something that looks nice when it is
118    printed out. */
119
120 static char *language;
121 static char *type;
122 static char *range;
123 static char *case_sensitive;
124
125 /* Warning issued when current_language and the language of the current
126    frame do not match. */
127 char lang_frame_mismatch_warn[] =
128 "Warning: the current language does not match this frame.";
129 \f
130 /* This page contains the functions corresponding to GDB commands
131    and their helpers. */
132
133 /* Show command.  Display a warning if the language set
134    does not match the frame. */
135 static void
136 show_language_command (struct ui_file *file, int from_tty,
137                        struct cmd_list_element *c, const char *value)
138 {
139   enum language flang;          /* The language of the current frame */
140
141   deprecated_show_value_hack (file, from_tty, c, value);
142   flang = get_frame_language ();
143   if (flang != language_unknown &&
144       language_mode == language_mode_manual &&
145       current_language->la_language != flang)
146     printf_filtered ("%s\n", lang_frame_mismatch_warn);
147 }
148
149 /* Set command.  Change the current working language. */
150 static void
151 set_language_command (char *ignore, int from_tty, struct cmd_list_element *c)
152 {
153   int i;
154   enum language flang;
155   char *err_lang;
156
157   if (!language || !language[0])
158     {
159       printf_unfiltered (_("\
160 The currently understood settings are:\n\n\
161 local or auto    Automatic setting based on source file\n"));
162
163       for (i = 0; i < languages_size; ++i)
164         {
165           /* Already dealt with these above.  */
166           if (languages[i]->la_language == language_unknown
167               || languages[i]->la_language == language_auto)
168             continue;
169
170           /* FIXME: i18n: for now assume that the human-readable name
171              is just a capitalization of the internal name.  */
172           printf_unfiltered ("%-16s Use the %c%s language\n",
173                              languages[i]->la_name,
174           /* Capitalize first letter of language
175              name.  */
176                              toupper (languages[i]->la_name[0]),
177                              languages[i]->la_name + 1);
178         }
179       /* Restore the silly string. */
180       set_language (current_language->la_language);
181       return;
182     }
183
184   /* Search the list of languages for a match.  */
185   for (i = 0; i < languages_size; i++)
186     {
187       if (strcmp (languages[i]->la_name, language) == 0)
188         {
189           /* Found it!  Go into manual mode, and use this language.  */
190           if (languages[i]->la_language == language_auto)
191             {
192               /* Enter auto mode.  Set to the current frame's language, if
193                  known, or fallback to the initial language.  */
194               language_mode = language_mode_auto;
195               flang = get_frame_language ();
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 = languages[i];
208               set_type_range_case ();
209               set_lang_str ();
210               expected_language = current_language;
211               return;
212             }
213         }
214     }
215
216   /* Reset the language (esp. the global string "language") to the 
217      correct values. */
218   err_lang = xstrdup (language);
219   make_cleanup (xfree, err_lang);       /* Free it after error */
220   set_language (current_language->la_language);
221   error (_("Unknown language `%s'."), err_lang);
222 }
223
224 static char **
225 language_completer (struct cmd_list_element *self, char *text, char *word)
226 {
227   int i;
228   const char **langnames
229     = (const char **) alloca ((languages_size + 1) * sizeof (const char *));
230
231   for (i = 0; i < languages_size; ++i)
232     langnames[i] = languages[i]->la_name;
233   langnames[i] = NULL;
234
235   return complete_on_enum (langnames, text, word);
236 }
237
238 /* Show command.  Display a warning if the type setting does
239    not match the current language. */
240 static void
241 show_type_command (struct ui_file *file, int from_tty,
242                    struct cmd_list_element *c, const char *value)
243 {
244   deprecated_show_value_hack (file, from_tty, c, value);
245   if (type_check != current_language->la_type_check)
246     printf_unfiltered (
247                         "Warning: the current type check setting does not match the language.\n");
248 }
249
250 /* Set command.  Change the setting for type checking. */
251 static void
252 set_type_command (char *ignore, int from_tty, struct cmd_list_element *c)
253 {
254   if (strcmp (type, "on") == 0)
255     {
256       type_check = type_check_on;
257       type_mode = type_mode_manual;
258     }
259   else if (strcmp (type, "warn") == 0)
260     {
261       type_check = type_check_warn;
262       type_mode = type_mode_manual;
263     }
264   else if (strcmp (type, "off") == 0)
265     {
266       type_check = type_check_off;
267       type_mode = type_mode_manual;
268     }
269   else if (strcmp (type, "auto") == 0)
270     {
271       type_mode = type_mode_auto;
272       set_type_range_case ();
273       /* Avoid hitting the set_type_str call below.  We
274          did it in set_type_range_case. */
275       return;
276     }
277   else
278     {
279       warning (_("Unrecognized type check setting: \"%s\""), type);
280     }
281   set_type_str ();
282   show_type_command (NULL, from_tty, NULL, NULL);
283 }
284
285 /* Show command.  Display a warning if the range setting does
286    not match the current language. */
287 static void
288 show_range_command (struct ui_file *file, int from_tty,
289                     struct cmd_list_element *c, const char *value)
290 {
291   deprecated_show_value_hack (file, from_tty, c, value);
292   if (range_check != current_language->la_range_check)
293     printf_unfiltered (
294                         "Warning: the current range check setting does not match the language.\n");
295 }
296
297 /* Set command.  Change the setting for range checking. */
298 static void
299 set_range_command (char *ignore, int from_tty, struct cmd_list_element *c)
300 {
301   if (strcmp (range, "on") == 0)
302     {
303       range_check = range_check_on;
304       range_mode = range_mode_manual;
305     }
306   else if (strcmp (range, "warn") == 0)
307     {
308       range_check = range_check_warn;
309       range_mode = range_mode_manual;
310     }
311   else if (strcmp (range, "off") == 0)
312     {
313       range_check = range_check_off;
314       range_mode = range_mode_manual;
315     }
316   else if (strcmp (range, "auto") == 0)
317     {
318       range_mode = range_mode_auto;
319       set_type_range_case ();
320       /* Avoid hitting the set_range_str call below.  We
321          did it in set_type_range_case. */
322       return;
323     }
324   else
325     {
326       warning (_("Unrecognized range check setting: \"%s\""), range);
327     }
328   set_range_str ();
329   show_range_command (NULL, from_tty, NULL, NULL);
330 }
331
332 /* Completer for range and type parameters.  */
333 static char **
334 range_or_type_completer (struct cmd_list_element *self, char *text, char *word)
335 {
336   static const char *values[] = { "on", "off", "warn", "auto", NULL };
337   return complete_on_enum (values, text, word);
338 }
339
340 /* Show command.  Display a warning if the case sensitivity setting does
341    not match the current language. */
342 static void
343 show_case_command (struct ui_file *file, int from_tty,
344                    struct cmd_list_element *c, const char *value)
345 {
346   deprecated_show_value_hack (file, from_tty, c, value);
347   if (case_sensitivity != current_language->la_case_sensitivity)
348     printf_unfiltered(
349 "Warning: the current case sensitivity setting does not match the language.\n");
350 }
351
352 /* Set command.  Change the setting for case sensitivity.  */
353
354 static void
355 set_case_command (char *ignore, int from_tty, struct cmd_list_element *c)
356 {
357    if (strcmp (case_sensitive, "on") == 0)
358      {
359        case_sensitivity = case_sensitive_on;
360        case_mode = case_mode_manual;
361      }
362    else if (strcmp (case_sensitive, "off") == 0)
363      {
364        case_sensitivity = case_sensitive_off;
365        case_mode = case_mode_manual;
366      }
367    else if (strcmp (case_sensitive, "auto") == 0)
368      {
369        case_mode = case_mode_auto;
370        set_type_range_case ();
371        /* Avoid hitting the set_case_str call below.  We did it in
372           set_type_range_case.  */
373        return;
374      }
375    else
376      {
377        warning (_("Unrecognized case-sensitive setting: \"%s\""),
378                 case_sensitive);
379      }
380    set_case_str();
381    show_case_command (NULL, from_tty, NULL, NULL);
382 }
383
384 /* Completer for case-sensitive parameter.  */
385 static char **
386 case_completer (struct cmd_list_element *self, char *text, char *word)
387 {
388   static const char *values[] = { "on", "off", "auto", NULL };
389   return complete_on_enum (values, text, word);
390 }
391
392 /* Set the status of range and type checking and case sensitivity based on
393    the current modes and the current language.
394    If SHOW is non-zero, then print out the current language,
395    type and range checking status. */
396 static void
397 set_type_range_case (void)
398 {
399
400   if (range_mode == range_mode_auto)
401     range_check = current_language->la_range_check;
402
403   if (type_mode == type_mode_auto)
404     type_check = current_language->la_type_check;
405
406   if (case_mode == case_mode_auto)
407     case_sensitivity = current_language->la_case_sensitivity;
408
409   set_type_str ();
410   set_range_str ();
411   set_case_str ();
412 }
413
414 /* Set current language to (enum language) LANG.  Returns previous language. */
415
416 enum language
417 set_language (enum language lang)
418 {
419   int i;
420   enum language prev_language;
421
422   prev_language = current_language->la_language;
423
424   for (i = 0; i < languages_size; i++)
425     {
426       if (languages[i]->la_language == lang)
427         {
428           current_language = languages[i];
429           set_type_range_case ();
430           set_lang_str ();
431           break;
432         }
433     }
434
435   return prev_language;
436 }
437 \f
438 /* This page contains functions that update the global vars
439    language, type and range. */
440 static void
441 set_lang_str (void)
442 {
443   char *prefix = "";
444
445   if (language)
446     xfree (language);
447   if (language_mode == language_mode_auto)
448     prefix = "auto; currently ";
449
450   language = concat (prefix, current_language->la_name, (char *)NULL);
451 }
452
453 static void
454 set_type_str (void)
455 {
456   char *tmp = NULL, *prefix = "";
457
458   if (type)
459     xfree (type);
460   if (type_mode == type_mode_auto)
461     prefix = "auto; currently ";
462
463   switch (type_check)
464     {
465     case type_check_on:
466       tmp = "on";
467       break;
468     case type_check_off:
469       tmp = "off";
470       break;
471     case type_check_warn:
472       tmp = "warn";
473       break;
474     default:
475       error (_("Unrecognized type check setting."));
476     }
477
478   type = concat (prefix, tmp, (char *)NULL);
479 }
480
481 static void
482 set_range_str (void)
483 {
484   char *tmp, *pref = "";
485
486   if (range_mode == range_mode_auto)
487     pref = "auto; currently ";
488
489   switch (range_check)
490     {
491     case range_check_on:
492       tmp = "on";
493       break;
494     case range_check_off:
495       tmp = "off";
496       break;
497     case range_check_warn:
498       tmp = "warn";
499       break;
500     default:
501       error (_("Unrecognized range check setting."));
502     }
503
504   if (range)
505     xfree (range);
506   range = concat (pref, tmp, (char *)NULL);
507 }
508
509 static void
510 set_case_str (void)
511 {
512    char *tmp = NULL, *prefix = "";
513
514    if (case_mode==case_mode_auto)
515       prefix = "auto; currently ";
516
517    switch (case_sensitivity)
518    {
519    case case_sensitive_on:
520      tmp = "on";
521      break;
522    case case_sensitive_off:
523      tmp = "off";
524      break;
525    default:
526      error (_("Unrecognized case-sensitive setting."));
527    }
528
529    xfree (case_sensitive);
530    case_sensitive = concat (prefix, tmp, (char *)NULL);
531 }
532
533 /* Print out the current language settings: language, range and
534    type checking.  If QUIETLY, print only what has changed.  */
535
536 void
537 language_info (int quietly)
538 {
539   if (quietly && expected_language == current_language)
540     return;
541
542   expected_language = current_language;
543   printf_unfiltered (_("Current language:  %s\n"), language);
544   show_language_command (NULL, 1, NULL, NULL);
545
546   if (!quietly)
547     {
548       printf_unfiltered (_("Type checking:     %s\n"), type);
549       show_type_command (NULL, 1, NULL, NULL);
550       printf_unfiltered (_("Range checking:    %s\n"), range);
551       show_range_command (NULL, 1, NULL, NULL);
552       printf_unfiltered (_("Case sensitivity:  %s\n"), case_sensitive);
553       show_case_command (NULL, 1, NULL, NULL);
554     }
555 }
556 \f
557 /* Return the result of a binary operation. */
558
559 #if 0                           /* Currently unused */
560
561 struct type *
562 binop_result_type (struct value *v1, struct value *v2)
563 {
564   int size, uns;
565   struct type *t1 = check_typedef (VALUE_TYPE (v1));
566   struct type *t2 = check_typedef (VALUE_TYPE (v2));
567
568   int l1 = TYPE_LENGTH (t1);
569   int l2 = TYPE_LENGTH (t2);
570
571   switch (current_language->la_language)
572     {
573     case language_c:
574     case language_cplus:
575     case language_objc:
576       if (TYPE_CODE (t1) == TYPE_CODE_FLT)
577         return TYPE_CODE (t2) == TYPE_CODE_FLT && l2 > l1 ?
578           VALUE_TYPE (v2) : VALUE_TYPE (v1);
579       else if (TYPE_CODE (t2) == TYPE_CODE_FLT)
580         return TYPE_CODE (t1) == TYPE_CODE_FLT && l1 > l2 ?
581           VALUE_TYPE (v1) : VALUE_TYPE (v2);
582       else if (TYPE_UNSIGNED (t1) && l1 > l2)
583         return VALUE_TYPE (v1);
584       else if (TYPE_UNSIGNED (t2) && l2 > l1)
585         return VALUE_TYPE (v2);
586       else                      /* Both are signed.  Result is the longer type */
587         return l1 > l2 ? VALUE_TYPE (v1) : VALUE_TYPE (v2);
588       break;
589     case language_m2:
590       /* If we are doing type-checking, l1 should equal l2, so this is
591          not needed. */
592       return l1 > l2 ? VALUE_TYPE (v1) : VALUE_TYPE (v2);
593       break;
594     }
595   internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
596   return (struct type *) 0;     /* For lint */
597 }
598
599 #endif /* 0 */
600 #if 0
601 /* This page contains functions that are used in type/range checking.
602    They all return zero if the type/range check fails.
603
604    It is hoped that these will make extending GDB to parse different
605    languages a little easier.  These are primarily used in eval.c when
606    evaluating expressions and making sure that their types are correct.
607    Instead of having a mess of conjucted/disjuncted expressions in an "if",
608    the ideas of type can be wrapped up in the following functions.
609
610    Note that some of them are not currently dependent upon which language
611    is currently being parsed.  For example, floats are the same in
612    C and Modula-2 (ie. the only floating point type has TYPE_CODE of
613    TYPE_CODE_FLT), while booleans are different. */
614
615 /* Returns non-zero if its argument is a simple type.  This is the same for
616    both Modula-2 and for C.  In the C case, TYPE_CODE_CHAR will never occur,
617    and thus will never cause the failure of the test. */
618 int
619 simple_type (struct type *type)
620 {
621   CHECK_TYPEDEF (type);
622   switch (TYPE_CODE (type))
623     {
624     case TYPE_CODE_INT:
625     case TYPE_CODE_CHAR:
626     case TYPE_CODE_ENUM:
627     case TYPE_CODE_FLT:
628     case TYPE_CODE_RANGE:
629     case TYPE_CODE_BOOL:
630       return 1;
631
632     default:
633       return 0;
634     }
635 }
636
637 /* Returns non-zero if its argument is of an ordered type.
638    An ordered type is one in which the elements can be tested for the
639    properties of "greater than", "less than", etc, or for which the
640    operations "increment" or "decrement" make sense. */
641 int
642 ordered_type (struct type *type)
643 {
644   CHECK_TYPEDEF (type);
645   switch (TYPE_CODE (type))
646     {
647     case TYPE_CODE_INT:
648     case TYPE_CODE_CHAR:
649     case TYPE_CODE_ENUM:
650     case TYPE_CODE_FLT:
651     case TYPE_CODE_RANGE:
652       return 1;
653
654     default:
655       return 0;
656     }
657 }
658
659 /* Returns non-zero if the two types are the same */
660 int
661 same_type (struct type *arg1, struct type *arg2)
662 {
663   CHECK_TYPEDEF (type);
664   if (structured_type (arg1) ? !structured_type (arg2) : structured_type (arg2))
665     /* One is structured and one isn't */
666     return 0;
667   else if (structured_type (arg1) && structured_type (arg2))
668     return arg1 == arg2;
669   else if (numeric_type (arg1) && numeric_type (arg2))
670     return (TYPE_CODE (arg2) == TYPE_CODE (arg1)) &&
671       (TYPE_UNSIGNED (arg1) == TYPE_UNSIGNED (arg2))
672       ? 1 : 0;
673   else
674     return arg1 == arg2;
675 }
676
677 /* Returns non-zero if the type is integral */
678 int
679 integral_type (struct type *type)
680 {
681   CHECK_TYPEDEF (type);
682   switch (current_language->la_language)
683     {
684     case language_c:
685     case language_cplus:
686     case language_objc:
687       return (TYPE_CODE (type) != TYPE_CODE_INT) &&
688         (TYPE_CODE (type) != TYPE_CODE_ENUM) ? 0 : 1;
689     case language_m2:
690     case language_pascal:
691       return TYPE_CODE (type) != TYPE_CODE_INT ? 0 : 1;
692     default:
693       error (_("Language not supported."));
694     }
695 }
696
697 /* Returns non-zero if the value is numeric */
698 int
699 numeric_type (struct type *type)
700 {
701   CHECK_TYPEDEF (type);
702   switch (TYPE_CODE (type))
703     {
704     case TYPE_CODE_INT:
705     case TYPE_CODE_FLT:
706       return 1;
707
708     default:
709       return 0;
710     }
711 }
712
713 /* Returns non-zero if the value is a character type */
714 int
715 character_type (struct type *type)
716 {
717   CHECK_TYPEDEF (type);
718   switch (current_language->la_language)
719     {
720     case language_m2:
721     case language_pascal:
722       return TYPE_CODE (type) != TYPE_CODE_CHAR ? 0 : 1;
723
724     case language_c:
725     case language_cplus:
726     case language_objc:
727       return (TYPE_CODE (type) == TYPE_CODE_INT) &&
728         TYPE_LENGTH (type) == sizeof (char)
729       ? 1 : 0;
730     default:
731       return (0);
732     }
733 }
734
735 /* Returns non-zero if the value is a string type */
736 int
737 string_type (struct type *type)
738 {
739   CHECK_TYPEDEF (type);
740   switch (current_language->la_language)
741     {
742     case language_m2:
743     case language_pascal:
744       return TYPE_CODE (type) != TYPE_CODE_STRING ? 0 : 1;
745
746     case language_c:
747     case language_cplus:
748     case language_objc:
749       /* C does not have distinct string type. */
750       return (0);
751     default:
752       return (0);
753     }
754 }
755
756 /* Returns non-zero if the value is a boolean type */
757 int
758 boolean_type (struct type *type)
759 {
760   CHECK_TYPEDEF (type);
761   if (TYPE_CODE (type) == TYPE_CODE_BOOL)
762     return 1;
763   switch (current_language->la_language)
764     {
765     case language_c:
766     case language_cplus:
767     case language_objc:
768       /* Might be more cleanly handled by having a
769          TYPE_CODE_INT_NOT_BOOL for (the deleted) CHILL and such
770          languages, or a TYPE_CODE_INT_OR_BOOL for C.  */
771       if (TYPE_CODE (type) == TYPE_CODE_INT)
772         return 1;
773     default:
774       break;
775     }
776   return 0;
777 }
778
779 /* Returns non-zero if the value is a floating-point type */
780 int
781 float_type (struct type *type)
782 {
783   CHECK_TYPEDEF (type);
784   return TYPE_CODE (type) == TYPE_CODE_FLT;
785 }
786
787 /* Returns non-zero if the value is a pointer type */
788 int
789 pointer_type (struct type *type)
790 {
791   return TYPE_CODE (type) == TYPE_CODE_PTR ||
792     TYPE_CODE (type) == TYPE_CODE_REF;
793 }
794
795 /* Returns non-zero if the value is a structured type */
796 int
797 structured_type (struct type *type)
798 {
799   CHECK_TYPEDEF (type);
800   switch (current_language->la_language)
801     {
802     case language_c:
803     case language_cplus:
804     case language_objc:
805       return (TYPE_CODE (type) == TYPE_CODE_STRUCT) ||
806         (TYPE_CODE (type) == TYPE_CODE_UNION) ||
807         (TYPE_CODE (type) == TYPE_CODE_ARRAY);
808    case language_pascal:
809       return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
810          (TYPE_CODE(type) == TYPE_CODE_UNION) ||
811          (TYPE_CODE(type) == TYPE_CODE_SET) ||
812             (TYPE_CODE(type) == TYPE_CODE_ARRAY);
813     case language_m2:
814       return (TYPE_CODE (type) == TYPE_CODE_STRUCT) ||
815         (TYPE_CODE (type) == TYPE_CODE_SET) ||
816         (TYPE_CODE (type) == TYPE_CODE_ARRAY);
817     default:
818       return (0);
819     }
820 }
821 #endif
822 \f
823 /* This page contains functions that return info about
824    (struct value) values used in GDB. */
825
826 /* Returns non-zero if the value VAL represents a true value. */
827 int
828 value_true (struct value *val)
829 {
830   /* It is possible that we should have some sort of error if a non-boolean
831      value is used in this context.  Possibly dependent on some kind of
832      "boolean-checking" option like range checking.  But it should probably
833      not depend on the language except insofar as is necessary to identify
834      a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
835      should be an error, probably).  */
836   return !value_logical_not (val);
837 }
838 \f
839 /* This page contains functions for the printing out of
840    error messages that occur during type- and range-
841    checking. */
842
843 /* These are called when a language fails a type- or range-check.  The
844    first argument should be a printf()-style format string, and the
845    rest of the arguments should be its arguments.  If
846    [type|range]_check is [type|range]_check_on, an error is printed;
847    if [type|range]_check_warn, a warning; otherwise just the
848    message. */
849
850 void
851 type_error (const char *string,...)
852 {
853   va_list args;
854   va_start (args, string);
855
856   switch (type_check)
857     {
858     case type_check_warn:
859       vwarning (string, args);
860       break;
861     case type_check_on:
862       verror (string, args);
863       break;
864     case type_check_off:
865       /* FIXME: cagney/2002-01-30: Should this function print anything
866          when type error is off?  */
867       vfprintf_filtered (gdb_stderr, string, args);
868       fprintf_filtered (gdb_stderr, "\n");
869       break;
870     default:
871       internal_error (__FILE__, __LINE__, _("bad switch"));
872     }
873   va_end (args);
874 }
875
876 void
877 range_error (const char *string,...)
878 {
879   va_list args;
880   va_start (args, string);
881
882   switch (range_check)
883     {
884     case range_check_warn:
885       vwarning (string, args);
886       break;
887     case range_check_on:
888       verror (string, args);
889       break;
890     case range_check_off:
891       /* FIXME: cagney/2002-01-30: Should this function print anything
892          when range error is off?  */
893       vfprintf_filtered (gdb_stderr, string, args);
894       fprintf_filtered (gdb_stderr, "\n");
895       break;
896     default:
897       internal_error (__FILE__, __LINE__, _("bad switch"));
898     }
899   va_end (args);
900 }
901 \f
902
903 /* This page contains miscellaneous functions */
904
905 /* Return the language enum for a given language string. */
906
907 enum language
908 language_enum (char *str)
909 {
910   int i;
911
912   for (i = 0; i < languages_size; i++)
913     if (strcmp (languages[i]->la_name, str) == 0)
914       return languages[i]->la_language;
915
916   return language_unknown;
917 }
918
919 /* Return the language struct for a given language enum. */
920
921 const struct language_defn *
922 language_def (enum language lang)
923 {
924   int i;
925
926   for (i = 0; i < languages_size; i++)
927     {
928       if (languages[i]->la_language == lang)
929         {
930           return languages[i];
931         }
932     }
933   return NULL;
934 }
935
936 /* Return the language as a string */
937 char *
938 language_str (enum language lang)
939 {
940   int i;
941
942   for (i = 0; i < languages_size; i++)
943     {
944       if (languages[i]->la_language == lang)
945         {
946           return languages[i]->la_name;
947         }
948     }
949   return "Unknown";
950 }
951
952 static void
953 set_check (char *ignore, int from_tty)
954 {
955   printf_unfiltered (
956      "\"set check\" must be followed by the name of a check subcommand.\n");
957   help_list (setchecklist, "set check ", -1, gdb_stdout);
958 }
959
960 static void
961 show_check (char *ignore, int from_tty)
962 {
963   cmd_show_list (showchecklist, from_tty, "");
964 }
965 \f
966 /* Add a language to the set of known languages.  */
967
968 void
969 add_language (const struct language_defn *lang)
970 {
971   if (lang->la_magic != LANG_MAGIC)
972     {
973       fprintf_unfiltered (gdb_stderr, "Magic number of %s language struct wrong\n",
974                           lang->la_name);
975       internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
976     }
977
978   if (!languages)
979     {
980       languages_allocsize = DEFAULT_ALLOCSIZE;
981       languages = (const struct language_defn **) xmalloc
982         (languages_allocsize * sizeof (*languages));
983     }
984   if (languages_size >= languages_allocsize)
985     {
986       languages_allocsize *= 2;
987       languages = (const struct language_defn **) xrealloc ((char *) languages,
988                                  languages_allocsize * sizeof (*languages));
989     }
990   languages[languages_size++] = lang;
991 }
992
993 /* Iterate through all registered languages looking for and calling
994    any non-NULL struct language_defn.skip_trampoline() functions.
995    Return the result from the first that returns non-zero, or 0 if all
996    `fail'.  */
997 CORE_ADDR 
998 skip_language_trampoline (struct frame_info *frame, CORE_ADDR pc)
999 {
1000   int i;
1001
1002   for (i = 0; i < languages_size; i++)
1003     {
1004       if (languages[i]->skip_trampoline)
1005         {
1006           CORE_ADDR real_pc = (languages[i]->skip_trampoline) (frame, pc);
1007           if (real_pc)
1008             return real_pc;
1009         }
1010     }
1011
1012   return 0;
1013 }
1014
1015 /* Return demangled language symbol, or NULL.  
1016    FIXME: Options are only useful for certain languages and ignored
1017    by others, so it would be better to remove them here and have a
1018    more flexible demangler for the languages that need it.  
1019    FIXME: Sometimes the demangler is invoked when we don't know the
1020    language, so we can't use this everywhere.  */
1021 char *
1022 language_demangle (const struct language_defn *current_language, 
1023                                 const char *mangled, int options)
1024 {
1025   if (current_language != NULL && current_language->la_demangle)
1026     return current_language->la_demangle (mangled, options);
1027   return NULL;
1028 }
1029
1030 /* Return class name from physname or NULL.  */
1031 char *
1032 language_class_name_from_physname (const struct language_defn *current_language,
1033                                    const char *physname)
1034 {
1035   if (current_language != NULL && current_language->la_class_name_from_physname)
1036     return current_language->la_class_name_from_physname (physname);
1037   return NULL;
1038 }
1039
1040 /* Return non-zero if TYPE should be passed (and returned) by
1041    reference at the language level.  */
1042 int
1043 language_pass_by_reference (struct type *type)
1044 {
1045   return current_language->la_pass_by_reference (type);
1046 }
1047
1048 /* Return zero; by default, types are passed by value at the language
1049    level.  The target ABI may pass or return some structs by reference
1050    independent of this.  */
1051 int
1052 default_pass_by_reference (struct type *type)
1053 {
1054   return 0;
1055 }
1056
1057 /* Return the default string containing the list of characters
1058    delimiting words.  This is a reasonable default value that
1059    most languages should be able to use.  */
1060
1061 char *
1062 default_word_break_characters (void)
1063 {
1064   return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1065 }
1066
1067 /* Print the index of array elements using the C99 syntax.  */
1068
1069 void
1070 default_print_array_index (struct value *index_value, struct ui_file *stream,
1071                            const struct value_print_options *options)
1072 {
1073   fprintf_filtered (stream, "[");
1074   LA_VALUE_PRINT (index_value, stream, options);
1075   fprintf_filtered (stream, "] = ");
1076 }
1077
1078 void
1079 default_get_string (struct value *value, gdb_byte **buffer, int *length,
1080                     const char **charset)
1081 {
1082   error (_("Getting a string is unsupported in this language."));
1083 }
1084
1085 /* Define the language that is no language.  */
1086
1087 static int
1088 unk_lang_parser (void)
1089 {
1090   return 1;
1091 }
1092
1093 static void
1094 unk_lang_error (char *msg)
1095 {
1096   error (_("Attempted to parse an expression with unknown language"));
1097 }
1098
1099 static void
1100 unk_lang_emit_char (int c, struct type *type, struct ui_file *stream,
1101                     int quoter)
1102 {
1103   error (_("internal error - unimplemented function unk_lang_emit_char called."));
1104 }
1105
1106 static void
1107 unk_lang_printchar (int c, struct type *type, struct ui_file *stream)
1108 {
1109   error (_("internal error - unimplemented function unk_lang_printchar called."));
1110 }
1111
1112 static void
1113 unk_lang_printstr (struct ui_file *stream, struct type *type,
1114                    const gdb_byte *string, unsigned int length,
1115                    int force_ellipses,
1116                    const struct value_print_options *options)
1117 {
1118   error (_("internal error - unimplemented function unk_lang_printstr called."));
1119 }
1120
1121 static void
1122 unk_lang_print_type (struct type *type, char *varstring, struct ui_file *stream,
1123                      int show, int level)
1124 {
1125   error (_("internal error - unimplemented function unk_lang_print_type called."));
1126 }
1127
1128 static int
1129 unk_lang_val_print (struct type *type, const gdb_byte *valaddr,
1130                     int embedded_offset, CORE_ADDR address,
1131                     struct ui_file *stream, int recurse,
1132                     const struct value_print_options *options)
1133 {
1134   error (_("internal error - unimplemented function unk_lang_val_print called."));
1135 }
1136
1137 static int
1138 unk_lang_value_print (struct value *val, struct ui_file *stream,
1139                       const struct value_print_options *options)
1140 {
1141   error (_("internal error - unimplemented function unk_lang_value_print called."));
1142 }
1143
1144 static CORE_ADDR unk_lang_trampoline (struct frame_info *frame, CORE_ADDR pc)
1145 {
1146   return 0;
1147 }
1148
1149 /* Unknown languages just use the cplus demangler.  */
1150 static char *unk_lang_demangle (const char *mangled, int options)
1151 {
1152   return cplus_demangle (mangled, options);
1153 }
1154
1155 static char *unk_lang_class_name (const char *mangled)
1156 {
1157   return NULL;
1158 }
1159
1160 static const struct op_print unk_op_print_tab[] =
1161 {
1162   {NULL, OP_NULL, PREC_NULL, 0}
1163 };
1164
1165 static void
1166 unknown_language_arch_info (struct gdbarch *gdbarch,
1167                             struct language_arch_info *lai)
1168 {
1169   lai->string_char_type = builtin_type (gdbarch)->builtin_char;
1170   lai->bool_type_default = builtin_type (gdbarch)->builtin_int;
1171   lai->primitive_type_vector = GDBARCH_OBSTACK_CALLOC (gdbarch, 1,
1172                                                        struct type *);
1173 }
1174
1175 const struct language_defn unknown_language_defn =
1176 {
1177   "unknown",
1178   language_unknown,
1179   range_check_off,
1180   type_check_off,
1181   case_sensitive_on,
1182   array_row_major,
1183   macro_expansion_no,
1184   &exp_descriptor_standard,
1185   unk_lang_parser,
1186   unk_lang_error,
1187   null_post_parser,
1188   unk_lang_printchar,           /* Print character constant */
1189   unk_lang_printstr,
1190   unk_lang_emit_char,
1191   unk_lang_print_type,          /* Print a type using appropriate syntax */
1192   default_print_typedef,        /* Print a typedef using appropriate syntax */
1193   unk_lang_val_print,           /* Print a value using appropriate syntax */
1194   unk_lang_value_print,         /* Print a top-level value */
1195   unk_lang_trampoline,          /* Language specific skip_trampoline */
1196   "this",                       /* name_of_this */
1197   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1198   basic_lookup_transparent_type,/* lookup_transparent_type */
1199   unk_lang_demangle,            /* Language specific symbol demangler */
1200   unk_lang_class_name,          /* Language specific class_name_from_physname */
1201   unk_op_print_tab,             /* expression operators for printing */
1202   1,                            /* c-style arrays */
1203   0,                            /* String lower bound */
1204   default_word_break_characters,
1205   default_make_symbol_completion_list,
1206   unknown_language_arch_info,   /* la_language_arch_info.  */
1207   default_print_array_index,
1208   default_pass_by_reference,
1209   default_get_string,
1210   LANG_MAGIC
1211 };
1212
1213 /* These two structs define fake entries for the "local" and "auto" options. */
1214 const struct language_defn auto_language_defn =
1215 {
1216   "auto",
1217   language_auto,
1218   range_check_off,
1219   type_check_off,
1220   case_sensitive_on,
1221   array_row_major,
1222   macro_expansion_no,
1223   &exp_descriptor_standard,
1224   unk_lang_parser,
1225   unk_lang_error,
1226   null_post_parser,
1227   unk_lang_printchar,           /* Print character constant */
1228   unk_lang_printstr,
1229   unk_lang_emit_char,
1230   unk_lang_print_type,          /* Print a type using appropriate syntax */
1231   default_print_typedef,        /* Print a typedef using appropriate syntax */
1232   unk_lang_val_print,           /* Print a value using appropriate syntax */
1233   unk_lang_value_print,         /* Print a top-level value */
1234   unk_lang_trampoline,          /* Language specific skip_trampoline */
1235   "this",                       /* name_of_this */
1236   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1237   basic_lookup_transparent_type,/* lookup_transparent_type */
1238   unk_lang_demangle,            /* Language specific symbol demangler */
1239   unk_lang_class_name,          /* Language specific class_name_from_physname */
1240   unk_op_print_tab,             /* expression operators for printing */
1241   1,                            /* c-style arrays */
1242   0,                            /* String lower bound */
1243   default_word_break_characters,
1244   default_make_symbol_completion_list,
1245   unknown_language_arch_info,   /* la_language_arch_info.  */
1246   default_print_array_index,
1247   default_pass_by_reference,
1248   default_get_string,
1249   LANG_MAGIC
1250 };
1251
1252 const struct language_defn local_language_defn =
1253 {
1254   "local",
1255   language_auto,
1256   range_check_off,
1257   type_check_off,
1258   case_sensitive_on,
1259   array_row_major,
1260   macro_expansion_no,
1261   &exp_descriptor_standard,
1262   unk_lang_parser,
1263   unk_lang_error,
1264   null_post_parser,
1265   unk_lang_printchar,           /* Print character constant */
1266   unk_lang_printstr,
1267   unk_lang_emit_char,
1268   unk_lang_print_type,          /* Print a type using appropriate syntax */
1269   default_print_typedef,        /* Print a typedef using appropriate syntax */
1270   unk_lang_val_print,           /* Print a value using appropriate syntax */
1271   unk_lang_value_print,         /* Print a top-level value */
1272   unk_lang_trampoline,          /* Language specific skip_trampoline */
1273   "this",                       /* name_of_this */
1274   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1275   basic_lookup_transparent_type,/* lookup_transparent_type */
1276   unk_lang_demangle,            /* Language specific symbol demangler */
1277   unk_lang_class_name,          /* Language specific class_name_from_physname */
1278   unk_op_print_tab,             /* expression operators for printing */
1279   1,                            /* c-style arrays */
1280   0,                            /* String lower bound */
1281   default_word_break_characters,
1282   default_make_symbol_completion_list,
1283   unknown_language_arch_info,   /* la_language_arch_info.  */
1284   default_print_array_index,
1285   default_pass_by_reference,
1286   default_get_string,
1287   LANG_MAGIC
1288 };
1289 \f
1290 /* Per-architecture language information.  */
1291
1292 static struct gdbarch_data *language_gdbarch_data;
1293
1294 struct language_gdbarch
1295 {
1296   /* A vector of per-language per-architecture info.  Indexed by "enum
1297      language".  */
1298   struct language_arch_info arch_info[nr_languages];
1299 };
1300
1301 static void *
1302 language_gdbarch_post_init (struct gdbarch *gdbarch)
1303 {
1304   struct language_gdbarch *l;
1305   int i;
1306
1307   l = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct language_gdbarch);
1308   for (i = 0; i < languages_size; i++)
1309     {
1310       if (languages[i] != NULL
1311           && languages[i]->la_language_arch_info != NULL)
1312         languages[i]->la_language_arch_info
1313           (gdbarch, l->arch_info + languages[i]->la_language);
1314     }
1315   return l;
1316 }
1317
1318 struct type *
1319 language_string_char_type (const struct language_defn *la,
1320                            struct gdbarch *gdbarch)
1321 {
1322   struct language_gdbarch *ld = gdbarch_data (gdbarch,
1323                                               language_gdbarch_data);
1324   return ld->arch_info[la->la_language].string_char_type;
1325 }
1326
1327 struct type *
1328 language_bool_type (const struct language_defn *la,
1329                     struct gdbarch *gdbarch)
1330 {
1331   struct language_gdbarch *ld = gdbarch_data (gdbarch,
1332                                               language_gdbarch_data);
1333
1334   if (ld->arch_info[la->la_language].bool_type_symbol)
1335     {
1336       struct symbol *sym;
1337       sym = lookup_symbol (ld->arch_info[la->la_language].bool_type_symbol,
1338                            NULL, VAR_DOMAIN, NULL);
1339       if (sym)
1340         {
1341           struct type *type = SYMBOL_TYPE (sym);
1342           if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
1343             return type;
1344         }
1345     }
1346
1347   return ld->arch_info[la->la_language].bool_type_default;
1348 }
1349
1350 struct type *
1351 language_lookup_primitive_type_by_name (const struct language_defn *la,
1352                                         struct gdbarch *gdbarch,
1353                                         const char *name)
1354 {
1355   struct language_gdbarch *ld = gdbarch_data (gdbarch,
1356                                               language_gdbarch_data);
1357   struct type *const *p;
1358   for (p = ld->arch_info[la->la_language].primitive_type_vector;
1359        (*p) != NULL;
1360        p++)
1361     {
1362       if (strcmp (TYPE_NAME (*p), name) == 0)
1363         return (*p);
1364     }
1365   return (NULL);
1366 }
1367
1368 /* Initialize the language routines */
1369
1370 void
1371 _initialize_language (void)
1372 {
1373   struct cmd_list_element *command;
1374
1375   language_gdbarch_data
1376     = gdbarch_data_register_post_init (language_gdbarch_post_init);
1377
1378   /* GDB commands for language specific stuff */
1379
1380   command = add_setshow_string_noescape_cmd ("language", class_support,
1381                                              &language, _("\
1382 Set the current source language."), _("\
1383 Show the current source language."), NULL,
1384                                              set_language_command,
1385                                              show_language_command,
1386                                              &setlist, &showlist);
1387   set_cmd_completer (command, language_completer);
1388
1389   add_prefix_cmd ("check", no_class, set_check,
1390                   _("Set the status of the type/range checker."),
1391                   &setchecklist, "set check ", 0, &setlist);
1392   add_alias_cmd ("c", "check", no_class, 1, &setlist);
1393   add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1394
1395   add_prefix_cmd ("check", no_class, show_check,
1396                   _("Show the status of the type/range checker."),
1397                   &showchecklist, "show check ", 0, &showlist);
1398   add_alias_cmd ("c", "check", no_class, 1, &showlist);
1399   add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1400
1401   command =
1402     add_setshow_string_noescape_cmd ("type", class_support,
1403                                      &type, _("\
1404 Set type checking.  (on/warn/off/auto)"), _("\
1405 Show type checking.  (on/warn/off/auto)"), NULL,
1406                                      set_type_command,
1407                                      show_type_command,
1408                                      &setchecklist, &showchecklist);
1409   set_cmd_completer (command, range_or_type_completer);
1410
1411   command =
1412     add_setshow_string_noescape_cmd ("range", class_support,
1413                                      &range, _("\
1414 Set range checking.  (on/warn/off/auto)"), _("\
1415 Show range checking.  (on/warn/off/auto)"), NULL,
1416                                      set_range_command,
1417                                      show_range_command,
1418                                      &setchecklist, &showchecklist);
1419   set_cmd_completer (command, range_or_type_completer);
1420
1421   command =
1422     add_setshow_string_noescape_cmd ("case-sensitive", class_support,
1423                                      &case_sensitive, _("\
1424 Set case sensitivity in name search.  (on/off/auto)"), _("\
1425 Show case sensitivity in name search.  (on/off/auto)"), _("\
1426 For Fortran the default is off; for other languages the default is on."),
1427                                      set_case_command,
1428                                      show_case_command,
1429                                      &setlist, &showlist);
1430   set_cmd_completer (command, case_completer);
1431
1432   add_language (&unknown_language_defn);
1433   add_language (&local_language_defn);
1434   add_language (&auto_language_defn);
1435
1436   language = xstrdup ("auto");
1437   type = xstrdup ("auto");
1438   range = xstrdup ("auto");
1439   case_sensitive = xstrdup ("auto");
1440
1441   /* Have the above take effect */
1442   set_language (language_auto);
1443 }