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