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