1 /* Multiple source language support for GDB.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Contributed by the Department of Computer Science at the State University
5 of New York at Buffalo.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
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. */
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. */
34 #include "gdb_string.h"
40 #include "expression.h"
43 #include "parser-defs.h"
46 extern void _initialize_language (void);
48 static void show_language_command (char *, int);
50 static void set_language_command (char *, int);
52 static void show_type_command (char *, int);
54 static void set_type_command (char *, int);
56 static void show_range_command (char *, int);
58 static void set_range_command (char *, int);
60 static void show_case_command (char *, int);
62 static void set_case_command (char *, int);
64 static void set_case_str (void);
66 static void set_range_str (void);
68 static void set_type_str (void);
70 static void set_lang_str (void);
72 static void unk_lang_error (char *);
74 static int unk_lang_parser (void);
76 static void show_check (char *, int);
78 static void set_check (char *, int);
80 static void set_type_range_case (void);
82 static void unk_lang_emit_char (int c
, struct ui_file
*stream
, int quoter
);
84 static void unk_lang_printchar (int c
, struct ui_file
*stream
);
86 static void unk_lang_printstr (struct ui_file
* stream
, char *string
,
87 unsigned int length
, int width
,
90 static struct type
*unk_lang_create_fundamental_type (struct objfile
*, int);
92 static void unk_lang_print_type (struct type
*, char *, struct ui_file
*,
95 static int unk_lang_val_print (struct type
*, char *, int, CORE_ADDR
,
96 struct ui_file
*, int, int, int,
97 enum val_prettyprint
);
99 static int unk_lang_value_print (struct value
*, struct ui_file
*, int, enum val_prettyprint
);
101 /* Forward declaration */
102 extern const struct language_defn unknown_language_defn
;
104 /* The current (default at startup) state of type and range checking.
105 (If the modes are set to "auto", though, these are changed based
106 on the default language at startup, and then again based on the
107 language of the first source file. */
109 enum range_mode range_mode
= range_mode_auto
;
110 enum range_check range_check
= range_check_off
;
111 enum type_mode type_mode
= type_mode_auto
;
112 enum type_check type_check
= type_check_off
;
113 enum case_mode case_mode
= case_mode_auto
;
114 enum case_sensitivity case_sensitivity
= case_sensitive_on
;
116 /* The current language and language_mode (see language.h) */
118 const struct language_defn
*current_language
= &unknown_language_defn
;
119 enum language_mode language_mode
= language_mode_auto
;
121 /* The language that the user expects to be typing in (the language
122 of main(), or the last language we notified them about, or C). */
124 const struct language_defn
*expected_language
;
126 /* The list of supported languages. The list itself is malloc'd. */
128 static const struct language_defn
**languages
;
129 static unsigned languages_size
;
130 static unsigned languages_allocsize
;
131 #define DEFAULT_ALLOCSIZE 4
133 /* The "set language/type/range" commands all put stuff in these
134 buffers. This is to make them work as set/show commands. The
135 user's string is copied here, then the set_* commands look at
136 them and update them to something that looks nice when it is
139 static char *language
;
142 static char *case_sensitive
;
144 /* Warning issued when current_language and the language of the current
145 frame do not match. */
146 char lang_frame_mismatch_warn
[] =
147 "Warning: the current language does not match this frame.";
150 /* This page contains the functions corresponding to GDB commands
151 and their helpers. */
153 /* Show command. Display a warning if the language set
154 does not match the frame. */
156 show_language_command (char *ignore
, int from_tty
)
158 enum language flang
; /* The language of the current frame */
160 flang
= get_frame_language ();
161 if (flang
!= language_unknown
&&
162 language_mode
== language_mode_manual
&&
163 current_language
->la_language
!= flang
)
164 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
167 /* Set command. Change the current working language. */
169 set_language_command (char *ignore
, int from_tty
)
175 if (!language
|| !language
[0])
177 printf_unfiltered ("The currently understood settings are:\n\n");
178 printf_unfiltered ("local or auto Automatic setting based on source file\n");
180 for (i
= 0; i
< languages_size
; ++i
)
182 /* Already dealt with these above. */
183 if (languages
[i
]->la_language
== language_unknown
184 || languages
[i
]->la_language
== language_auto
)
187 /* FIXME for now assume that the human-readable name is just
188 a capitalization of the internal name. */
189 printf_unfiltered ("%-16s Use the %c%s language\n",
190 languages
[i
]->la_name
,
191 /* Capitalize first letter of language
193 toupper (languages
[i
]->la_name
[0]),
194 languages
[i
]->la_name
+ 1);
196 /* Restore the silly string. */
197 set_language (current_language
->la_language
);
201 /* Search the list of languages for a match. */
202 for (i
= 0; i
< languages_size
; i
++)
204 if (STREQ (languages
[i
]->la_name
, language
))
206 /* Found it! Go into manual mode, and use this language. */
207 if (languages
[i
]->la_language
== language_auto
)
209 /* Enter auto mode. Set to the current frame's language, if known. */
210 language_mode
= language_mode_auto
;
211 flang
= get_frame_language ();
212 if (flang
!= language_unknown
)
213 set_language (flang
);
214 expected_language
= current_language
;
219 /* Enter manual mode. Set the specified language. */
220 language_mode
= language_mode_manual
;
221 current_language
= languages
[i
];
222 set_type_range_case ();
224 expected_language
= current_language
;
230 /* Reset the language (esp. the global string "language") to the
232 err_lang
= savestring (language
, strlen (language
));
233 make_cleanup (xfree
, err_lang
); /* Free it after error */
234 set_language (current_language
->la_language
);
235 error ("Unknown language `%s'.", err_lang
);
238 /* Show command. Display a warning if the type setting does
239 not match the current language. */
241 show_type_command (char *ignore
, int from_tty
)
243 if (type_check
!= current_language
->la_type_check
)
245 "Warning: the current type check setting does not match the language.\n");
248 /* Set command. Change the setting for type checking. */
250 set_type_command (char *ignore
, int from_tty
)
252 if (STREQ (type
, "on"))
254 type_check
= type_check_on
;
255 type_mode
= type_mode_manual
;
257 else if (STREQ (type
, "warn"))
259 type_check
= type_check_warn
;
260 type_mode
= type_mode_manual
;
262 else if (STREQ (type
, "off"))
264 type_check
= type_check_off
;
265 type_mode
= type_mode_manual
;
267 else if (STREQ (type
, "auto"))
269 type_mode
= type_mode_auto
;
270 set_type_range_case ();
271 /* Avoid hitting the set_type_str call below. We
272 did it in set_type_range_case. */
277 warning ("Unrecognized type check setting: \"%s\"", type
);
280 show_type_command ((char *) NULL
, from_tty
);
283 /* Show command. Display a warning if the range setting does
284 not match the current language. */
286 show_range_command (char *ignore
, int from_tty
)
289 if (range_check
!= current_language
->la_range_check
)
291 "Warning: the current range check setting does not match the language.\n");
294 /* Set command. Change the setting for range checking. */
296 set_range_command (char *ignore
, int from_tty
)
298 if (STREQ (range
, "on"))
300 range_check
= range_check_on
;
301 range_mode
= range_mode_manual
;
303 else if (STREQ (range
, "warn"))
305 range_check
= range_check_warn
;
306 range_mode
= range_mode_manual
;
308 else if (STREQ (range
, "off"))
310 range_check
= range_check_off
;
311 range_mode
= range_mode_manual
;
313 else if (STREQ (range
, "auto"))
315 range_mode
= range_mode_auto
;
316 set_type_range_case ();
317 /* Avoid hitting the set_range_str call below. We
318 did it in set_type_range_case. */
323 warning ("Unrecognized range check setting: \"%s\"", range
);
326 show_range_command ((char *) 0, from_tty
);
329 /* Show command. Display a warning if the case sensitivity setting does
330 not match the current language. */
332 show_case_command (char *ignore
, int from_tty
)
334 if (case_sensitivity
!= current_language
->la_case_sensitivity
)
336 "Warning: the current case sensitivity setting does not match the language.\n");
339 /* Set command. Change the setting for case sensitivity. */
341 set_case_command (char *ignore
, int from_tty
)
343 if (STREQ (case_sensitive
, "on"))
345 case_sensitivity
= case_sensitive_on
;
346 case_mode
= case_mode_manual
;
348 else if (STREQ (case_sensitive
, "off"))
350 case_sensitivity
= case_sensitive_off
;
351 case_mode
= case_mode_manual
;
353 else if (STREQ (case_sensitive
, "auto"))
355 case_mode
= case_mode_auto
;
356 set_type_range_case ();
357 /* Avoid hitting the set_case_str call below. We
358 did it in set_type_range_case. */
363 warning ("Unrecognized case-sensitive setting: \"%s\"", case_sensitive
);
366 show_case_command ((char *) NULL
, from_tty
);
369 /* Set the status of range and type checking and case sensitivity based on
370 the current modes and the current language.
371 If SHOW is non-zero, then print out the current language,
372 type and range checking status. */
374 set_type_range_case (void)
377 if (range_mode
== range_mode_auto
)
378 range_check
= current_language
->la_range_check
;
380 if (type_mode
== type_mode_auto
)
381 type_check
= current_language
->la_type_check
;
383 if (case_mode
== case_mode_auto
)
384 case_sensitivity
= current_language
->la_case_sensitivity
;
391 /* Set current language to (enum language) LANG. Returns previous language. */
394 set_language (enum language lang
)
397 enum language prev_language
;
399 prev_language
= current_language
->la_language
;
401 for (i
= 0; i
< languages_size
; i
++)
403 if (languages
[i
]->la_language
== lang
)
405 current_language
= languages
[i
];
406 set_type_range_case ();
412 return prev_language
;
415 /* This page contains functions that update the global vars
416 language, type and range. */
424 if (language_mode
== language_mode_auto
)
425 prefix
= "auto; currently ";
427 language
= concat (prefix
, current_language
->la_name
, NULL
);
433 char *tmp
= NULL
, *prefix
= "";
437 if (type_mode
== type_mode_auto
)
438 prefix
= "auto; currently ";
448 case type_check_warn
:
452 error ("Unrecognized type check setting.");
455 type
= concat (prefix
, tmp
, NULL
);
461 char *tmp
, *pref
= "";
463 if (range_mode
== range_mode_auto
)
464 pref
= "auto; currently ";
471 case range_check_off
:
474 case range_check_warn
:
478 error ("Unrecognized range check setting.");
483 range
= concat (pref
, tmp
, NULL
);
489 char *tmp
= NULL
, *prefix
= "";
491 if (case_mode
==case_mode_auto
)
492 prefix
= "auto; currently ";
494 switch (case_sensitivity
)
496 case case_sensitive_on
:
499 case case_sensitive_off
:
503 error ("Unrecognized case-sensitive setting.");
506 xfree (case_sensitive
);
507 case_sensitive
= concat (prefix
, tmp
, NULL
);
510 /* Print out the current language settings: language, range and
511 type checking. If QUIETLY, print only what has changed. */
514 language_info (int quietly
)
516 if (quietly
&& expected_language
== current_language
)
519 expected_language
= current_language
;
520 printf_unfiltered ("Current language: %s\n", language
);
521 show_language_command ((char *) 0, 1);
525 printf_unfiltered ("Type checking: %s\n", type
);
526 show_type_command ((char *) 0, 1);
527 printf_unfiltered ("Range checking: %s\n", range
);
528 show_range_command ((char *) 0, 1);
529 printf_unfiltered ("Case sensitivity: %s\n", case_sensitive
);
530 show_case_command ((char *) 0, 1);
534 /* Return the result of a binary operation. */
536 #if 0 /* Currently unused */
539 binop_result_type (struct value
*v1
, struct value
*v2
)
542 struct type
*t1
= check_typedef (VALUE_TYPE (v1
));
543 struct type
*t2
= check_typedef (VALUE_TYPE (v2
));
545 int l1
= TYPE_LENGTH (t1
);
546 int l2
= TYPE_LENGTH (t2
);
548 switch (current_language
->la_language
)
553 if (TYPE_CODE (t1
) == TYPE_CODE_FLT
)
554 return TYPE_CODE (t2
) == TYPE_CODE_FLT
&& l2
> l1
?
555 VALUE_TYPE (v2
) : VALUE_TYPE (v1
);
556 else if (TYPE_CODE (t2
) == TYPE_CODE_FLT
)
557 return TYPE_CODE (t1
) == TYPE_CODE_FLT
&& l1
> l2
?
558 VALUE_TYPE (v1
) : VALUE_TYPE (v2
);
559 else if (TYPE_UNSIGNED (t1
) && l1
> l2
)
560 return VALUE_TYPE (v1
);
561 else if (TYPE_UNSIGNED (t2
) && l2
> l1
)
562 return VALUE_TYPE (v2
);
563 else /* Both are signed. Result is the longer type */
564 return l1
> l2
? VALUE_TYPE (v1
) : VALUE_TYPE (v2
);
567 /* If we are doing type-checking, l1 should equal l2, so this is
569 return l1
> l2
? VALUE_TYPE (v1
) : VALUE_TYPE (v2
);
571 /* OBSOLETE case language_chill: */
572 /* OBSOLETE error ("Missing Chill support in function binop_result_check."); */ /*FIXME */
574 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
575 return (struct type
*) 0; /* For lint */
581 /* This page contains functions that return format strings for
582 printf for printing out numbers in different formats */
584 /* Returns the appropriate printf format for hexadecimal
587 local_hex_format_custom (char *pre
)
589 static char form
[50];
591 strcpy (form
, local_hex_format_prefix ());
594 strcat (form
, local_hex_format_specifier ());
595 strcat (form
, local_hex_format_suffix ());
599 /* Converts a LONGEST to custom hexadecimal and stores it in a static
600 string. Returns a pointer to this string. */
602 local_hex_string (LONGEST num
)
604 return local_hex_string_custom (num
, "l");
607 /* Converts a LONGEST number to custom hexadecimal and stores it in a static
608 string. Returns a pointer to this string. Note that the width parameter
609 should end with "l", e.g. "08l" as with calls to local_hex_string_custom */
612 local_hex_string_custom (LONGEST num
, char *width
)
614 #define RESULT_BUF_LEN 50
615 static char res2
[RESULT_BUF_LEN
];
616 char format
[RESULT_BUF_LEN
];
620 char *pad_char
; /* string with one character */
623 char temp_nbr_buf
[RESULT_BUF_LEN
];
625 /* Use phex_nz to print the number into a string, then
626 build the result string from local_hex_format_prefix, padding and
627 the hex representation as indicated by "width". */
628 strcpy (temp_nbr_buf
, phex_nz (num
, sizeof (num
)));
633 if (*parse_ptr
== '-')
638 if (*parse_ptr
== '0')
642 pad_char
= "0"; /* If padding is on the right, it is blank */
644 field_width
= atoi (parse_ptr
);
645 num_len
= strlen (temp_nbr_buf
);
646 num_pad_chars
= field_width
- strlen (temp_nbr_buf
); /* possibly negative */
648 if (strlen (local_hex_format_prefix ()) + num_len
+ num_pad_chars
649 >= RESULT_BUF_LEN
) /* paranoia */
650 internal_error (__FILE__
, __LINE__
,
651 "local_hex_string_custom: insufficient space to store result");
653 strcpy (res2
, local_hex_format_prefix ());
656 while (num_pad_chars
> 0)
658 strcat (res2
, pad_char
);
662 strcat (res2
, temp_nbr_buf
);
665 while (num_pad_chars
> 0)
667 strcat (res2
, pad_char
);
673 } /* local_hex_string_custom */
675 /* Returns the appropriate printf format for octal
678 local_octal_format_custom (char *pre
)
680 static char form
[50];
682 strcpy (form
, local_octal_format_prefix ());
685 strcat (form
, local_octal_format_specifier ());
686 strcat (form
, local_octal_format_suffix ());
690 /* Returns the appropriate printf format for decimal numbers. */
692 local_decimal_format_custom (char *pre
)
694 static char form
[50];
696 strcpy (form
, local_decimal_format_prefix ());
699 strcat (form
, local_decimal_format_specifier ());
700 strcat (form
, local_decimal_format_suffix ());
705 /* This page contains functions that are used in type/range checking.
706 They all return zero if the type/range check fails.
708 It is hoped that these will make extending GDB to parse different
709 languages a little easier. These are primarily used in eval.c when
710 evaluating expressions and making sure that their types are correct.
711 Instead of having a mess of conjucted/disjuncted expressions in an "if",
712 the ideas of type can be wrapped up in the following functions.
714 Note that some of them are not currently dependent upon which language
715 is currently being parsed. For example, floats are the same in
716 C and Modula-2 (ie. the only floating point type has TYPE_CODE of
717 TYPE_CODE_FLT), while booleans are different. */
719 /* Returns non-zero if its argument is a simple type. This is the same for
720 both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur,
721 and thus will never cause the failure of the test. */
723 simple_type (struct type
*type
)
725 CHECK_TYPEDEF (type
);
726 switch (TYPE_CODE (type
))
732 case TYPE_CODE_RANGE
:
741 /* Returns non-zero if its argument is of an ordered type.
742 An ordered type is one in which the elements can be tested for the
743 properties of "greater than", "less than", etc, or for which the
744 operations "increment" or "decrement" make sense. */
746 ordered_type (struct type
*type
)
748 CHECK_TYPEDEF (type
);
749 switch (TYPE_CODE (type
))
755 case TYPE_CODE_RANGE
:
763 /* Returns non-zero if the two types are the same */
765 same_type (struct type
*arg1
, struct type
*arg2
)
767 CHECK_TYPEDEF (type
);
768 if (structured_type (arg1
) ? !structured_type (arg2
) : structured_type (arg2
))
769 /* One is structured and one isn't */
771 else if (structured_type (arg1
) && structured_type (arg2
))
773 else if (numeric_type (arg1
) && numeric_type (arg2
))
774 return (TYPE_CODE (arg2
) == TYPE_CODE (arg1
)) &&
775 (TYPE_UNSIGNED (arg1
) == TYPE_UNSIGNED (arg2
))
781 /* Returns non-zero if the type is integral */
783 integral_type (struct type
*type
)
785 CHECK_TYPEDEF (type
);
786 switch (current_language
->la_language
)
791 return (TYPE_CODE (type
) != TYPE_CODE_INT
) &&
792 (TYPE_CODE (type
) != TYPE_CODE_ENUM
) ? 0 : 1;
794 case language_pascal
:
795 return TYPE_CODE (type
) != TYPE_CODE_INT
? 0 : 1;
796 /* OBSOLETE case language_chill: */
797 /* OBSOLETE error ("Missing Chill support in function integral_type."); *//*FIXME */
799 error ("Language not supported.");
803 /* Returns non-zero if the value is numeric */
805 numeric_type (struct type
*type
)
807 CHECK_TYPEDEF (type
);
808 switch (TYPE_CODE (type
))
819 /* Returns non-zero if the value is a character type */
821 character_type (struct type
*type
)
823 CHECK_TYPEDEF (type
);
824 switch (current_language
->la_language
)
826 /* OBSOLETE case language_chill: */
828 case language_pascal
:
829 return TYPE_CODE (type
) != TYPE_CODE_CHAR
? 0 : 1;
834 return (TYPE_CODE (type
) == TYPE_CODE_INT
) &&
835 TYPE_LENGTH (type
) == sizeof (char)
842 /* Returns non-zero if the value is a string type */
844 string_type (struct type
*type
)
846 CHECK_TYPEDEF (type
);
847 switch (current_language
->la_language
)
849 /* OBSOLETE case language_chill: */
851 case language_pascal
:
852 return TYPE_CODE (type
) != TYPE_CODE_STRING
? 0 : 1;
857 /* C does not have distinct string type. */
864 /* Returns non-zero if the value is a boolean type */
866 boolean_type (struct type
*type
)
868 CHECK_TYPEDEF (type
);
869 if (TYPE_CODE (type
) == TYPE_CODE_BOOL
)
871 switch (current_language
->la_language
)
876 /* Might be more cleanly handled by having a
877 TYPE_CODE_INT_NOT_BOOL for (OBSOLETE) CHILL and such
878 languages, or a TYPE_CODE_INT_OR_BOOL for C. */
879 if (TYPE_CODE (type
) == TYPE_CODE_INT
)
887 /* Returns non-zero if the value is a floating-point type */
889 float_type (struct type
*type
)
891 CHECK_TYPEDEF (type
);
892 return TYPE_CODE (type
) == TYPE_CODE_FLT
;
895 /* Returns non-zero if the value is a pointer type */
897 pointer_type (struct type
*type
)
899 return TYPE_CODE (type
) == TYPE_CODE_PTR
||
900 TYPE_CODE (type
) == TYPE_CODE_REF
;
903 /* Returns non-zero if the value is a structured type */
905 structured_type (struct type
*type
)
907 CHECK_TYPEDEF (type
);
908 switch (current_language
->la_language
)
913 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
) ||
914 (TYPE_CODE (type
) == TYPE_CODE_UNION
) ||
915 (TYPE_CODE (type
) == TYPE_CODE_ARRAY
);
916 case language_pascal
:
917 return (TYPE_CODE(type
) == TYPE_CODE_STRUCT
) ||
918 (TYPE_CODE(type
) == TYPE_CODE_UNION
) ||
919 (TYPE_CODE(type
) == TYPE_CODE_SET
) ||
920 (TYPE_CODE(type
) == TYPE_CODE_ARRAY
);
922 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
) ||
923 (TYPE_CODE (type
) == TYPE_CODE_SET
) ||
924 (TYPE_CODE (type
) == TYPE_CODE_ARRAY
);
925 /* OBSOLETE case language_chill: */
926 /* OBSOLETE error ("Missing Chill support in function structured_type."); *//*FIXME */
934 lang_bool_type (void)
938 switch (current_language
->la_language
)
941 /* OBSOLETE case language_chill: */
942 /* OBSOLETE return builtin_type_chill_bool; */
944 case language_fortran
:
945 sym
= lookup_symbol ("logical", NULL
, VAR_NAMESPACE
, NULL
, NULL
);
948 type
= SYMBOL_TYPE (sym
);
949 if (type
&& TYPE_CODE (type
) == TYPE_CODE_BOOL
)
952 return builtin_type_f_logical_s2
;
954 case language_pascal
:
955 if (current_language
->la_language
==language_cplus
)
956 {sym
= lookup_symbol ("bool", NULL
, VAR_NAMESPACE
, NULL
, NULL
);}
958 {sym
= lookup_symbol ("boolean", NULL
, VAR_NAMESPACE
, NULL
, NULL
);}
961 type
= SYMBOL_TYPE (sym
);
962 if (type
&& TYPE_CODE (type
) == TYPE_CODE_BOOL
)
965 return builtin_type_bool
;
967 sym
= lookup_symbol ("boolean", NULL
, VAR_NAMESPACE
, NULL
, NULL
);
970 type
= SYMBOL_TYPE (sym
);
971 if (type
&& TYPE_CODE (type
) == TYPE_CODE_BOOL
)
974 return java_boolean_type
;
976 return builtin_type_int
;
980 /* This page contains functions that return info about
981 (struct value) values used in GDB. */
983 /* Returns non-zero if the value VAL represents a true value. */
985 value_true (struct value
*val
)
987 /* It is possible that we should have some sort of error if a non-boolean
988 value is used in this context. Possibly dependent on some kind of
989 "boolean-checking" option like range checking. But it should probably
990 not depend on the language except insofar as is necessary to identify
991 a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
992 should be an error, probably). */
993 return !value_logical_not (val
);
996 /* Returns non-zero if the operator OP is defined on
997 the values ARG1 and ARG2. */
999 #if 0 /* Currently unused */
1002 binop_type_check (struct value
*arg1
, struct value
*arg2
, int op
)
1004 struct type
*t1
, *t2
;
1006 /* If we're not checking types, always return success. */
1010 t1
= VALUE_TYPE (arg1
);
1012 t2
= VALUE_TYPE (arg2
);
1020 if ((numeric_type (t1
) && pointer_type (t2
)) ||
1021 (pointer_type (t1
) && numeric_type (t2
)))
1023 warning ("combining pointer and integer.\n");
1029 if (!numeric_type (t1
) || !numeric_type (t2
))
1030 type_op_error ("Arguments to %s must be numbers.", op
);
1031 else if (!same_type (t1
, t2
))
1032 type_op_error ("Arguments to %s must be of the same type.", op
);
1035 case BINOP_LOGICAL_AND
:
1036 case BINOP_LOGICAL_OR
:
1037 if (!boolean_type (t1
) || !boolean_type (t2
))
1038 type_op_error ("Arguments to %s must be of boolean type.", op
);
1042 if ((pointer_type (t1
) && !(pointer_type (t2
) || integral_type (t2
))) ||
1043 (pointer_type (t2
) && !(pointer_type (t1
) || integral_type (t1
))))
1044 type_op_error ("A pointer can only be compared to an integer or pointer.", op
);
1045 else if ((pointer_type (t1
) && integral_type (t2
)) ||
1046 (integral_type (t1
) && pointer_type (t2
)))
1048 warning ("combining integer and pointer.\n");
1051 else if (!simple_type (t1
) || !simple_type (t2
))
1052 type_op_error ("Arguments to %s must be of simple type.", op
);
1053 else if (!same_type (t1
, t2
))
1054 type_op_error ("Arguments to %s must be of the same type.", op
);
1059 if (!integral_type (t1
) || !integral_type (t2
))
1060 type_op_error ("Arguments to %s must be of integral type.", op
);
1067 if (!ordered_type (t1
) || !ordered_type (t2
))
1068 type_op_error ("Arguments to %s must be of ordered type.", op
);
1069 else if (!same_type (t1
, t2
))
1070 type_op_error ("Arguments to %s must be of the same type.", op
);
1074 if (pointer_type (t1
) && !integral_type (t2
))
1075 type_op_error ("A pointer can only be assigned an integer.", op
);
1076 else if (pointer_type (t1
) && integral_type (t2
))
1078 warning ("combining integer and pointer.");
1081 else if (!simple_type (t1
) || !simple_type (t2
))
1082 type_op_error ("Arguments to %s must be of simple type.", op
);
1083 else if (!same_type (t1
, t2
))
1084 type_op_error ("Arguments to %s must be of the same type.", op
);
1088 /* FIXME: Needs to handle bitstrings as well. */
1089 if (!(string_type (t1
) || character_type (t1
) || integral_type (t1
))
1090 || !(string_type (t2
) || character_type (t2
) || integral_type (t2
)))
1091 type_op_error ("Arguments to %s must be strings or characters.", op
);
1094 /* Unary checks -- arg2 is null */
1096 case UNOP_LOGICAL_NOT
:
1097 if (!boolean_type (t1
))
1098 type_op_error ("Argument to %s must be of boolean type.", op
);
1103 if (!numeric_type (t1
))
1104 type_op_error ("Argument to %s must be of numeric type.", op
);
1108 if (integral_type (t1
))
1110 warning ("combining pointer and integer.\n");
1113 else if (!pointer_type (t1
))
1114 type_op_error ("Argument to %s must be a pointer.", op
);
1117 case UNOP_PREINCREMENT
:
1118 case UNOP_POSTINCREMENT
:
1119 case UNOP_PREDECREMENT
:
1120 case UNOP_POSTDECREMENT
:
1121 if (!ordered_type (t1
))
1122 type_op_error ("Argument to %s must be of an ordered type.", op
);
1126 /* Ok. The following operators have different meanings in
1127 different languages. */
1128 switch (current_language
->la_language
)
1132 case language_cplus
:
1137 if (!numeric_type (t1
) || !numeric_type (t2
))
1138 type_op_error ("Arguments to %s must be numbers.", op
);
1149 if (!float_type (t1
) || !float_type (t2
))
1150 type_op_error ("Arguments to %s must be floating point numbers.", op
);
1153 if (!integral_type (t1
) || !integral_type (t2
))
1154 type_op_error ("Arguments to %s must be of integral type.", op
);
1160 case language_pascal
:
1164 if (!float_type(t1
) && !float_type(t2
))
1165 type_op_error ("Arguments to %s must be floating point numbers.",op
);
1168 if (!integral_type(t1
) || !integral_type(t2
))
1169 type_op_error ("Arguments to %s must be of integral type.",op
);
1174 #ifdef _LANG_chill /* OBSOLETE */
1175 /* OBSOLETE case language_chill: */
1176 /* OBSOLETE error ("Missing Chill support in function binop_type_check."); *//*FIXME */
1186 /* This page contains functions for the printing out of
1187 error messages that occur during type- and range-
1190 /* Prints the format string FMT with the operator as a string
1191 corresponding to the opcode OP. If FATAL is non-zero, then
1192 this is an error and error () is called. Otherwise, it is
1193 a warning and printf() is called. */
1195 op_error (char *fmt
, enum exp_opcode op
, int fatal
)
1198 error (fmt
, op_string (op
));
1201 warning (fmt
, op_string (op
));
1205 /* These are called when a language fails a type- or range-check. The
1206 first argument should be a printf()-style format string, and the
1207 rest of the arguments should be its arguments. If
1208 [type|range]_check is [type|range]_check_on, an error is printed;
1209 if [type|range]_check_warn, a warning; otherwise just the
1213 type_error (const char *string
,...)
1216 va_start (args
, string
);
1220 case type_check_warn
:
1221 vwarning (string
, args
);
1224 verror (string
, args
);
1226 case type_check_off
:
1227 /* FIXME: cagney/2002-01-30: Should this function print anything
1228 when type error is off? */
1229 vfprintf_filtered (gdb_stderr
, string
, args
);
1230 fprintf_filtered (gdb_stderr
, "\n");
1233 internal_error (__FILE__
, __LINE__
, "bad switch");
1239 range_error (const char *string
,...)
1242 va_start (args
, string
);
1244 switch (range_check
)
1246 case range_check_warn
:
1247 vwarning (string
, args
);
1249 case range_check_on
:
1250 verror (string
, args
);
1252 case range_check_off
:
1253 /* FIXME: cagney/2002-01-30: Should this function print anything
1254 when range error is off? */
1255 vfprintf_filtered (gdb_stderr
, string
, args
);
1256 fprintf_filtered (gdb_stderr
, "\n");
1259 internal_error (__FILE__
, __LINE__
, "bad switch");
1265 /* This page contains miscellaneous functions */
1267 /* Return the language enum for a given language string. */
1270 language_enum (char *str
)
1274 for (i
= 0; i
< languages_size
; i
++)
1275 if (STREQ (languages
[i
]->la_name
, str
))
1276 return languages
[i
]->la_language
;
1278 return language_unknown
;
1281 /* Return the language struct for a given language enum. */
1283 const struct language_defn
*
1284 language_def (enum language lang
)
1288 for (i
= 0; i
< languages_size
; i
++)
1290 if (languages
[i
]->la_language
== lang
)
1292 return languages
[i
];
1298 /* Return the language as a string */
1300 language_str (enum language lang
)
1304 for (i
= 0; i
< languages_size
; i
++)
1306 if (languages
[i
]->la_language
== lang
)
1308 return languages
[i
]->la_name
;
1315 set_check (char *ignore
, int from_tty
)
1318 "\"set check\" must be followed by the name of a check subcommand.\n");
1319 help_list (setchecklist
, "set check ", -1, gdb_stdout
);
1323 show_check (char *ignore
, int from_tty
)
1325 cmd_show_list (showchecklist
, from_tty
, "");
1328 /* Add a language to the set of known languages. */
1331 add_language (const struct language_defn
*lang
)
1333 if (lang
->la_magic
!= LANG_MAGIC
)
1335 fprintf_unfiltered (gdb_stderr
, "Magic number of %s language struct wrong\n",
1337 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
1342 languages_allocsize
= DEFAULT_ALLOCSIZE
;
1343 languages
= (const struct language_defn
**) xmalloc
1344 (languages_allocsize
* sizeof (*languages
));
1346 if (languages_size
>= languages_allocsize
)
1348 languages_allocsize
*= 2;
1349 languages
= (const struct language_defn
**) xrealloc ((char *) languages
,
1350 languages_allocsize
* sizeof (*languages
));
1352 languages
[languages_size
++] = lang
;
1355 /* Define the language that is no language. */
1358 unk_lang_parser (void)
1364 unk_lang_error (char *msg
)
1366 error ("Attempted to parse an expression with unknown language");
1370 unk_lang_emit_char (register int c
, struct ui_file
*stream
, int quoter
)
1372 error ("internal error - unimplemented function unk_lang_emit_char called.");
1376 unk_lang_printchar (register int c
, struct ui_file
*stream
)
1378 error ("internal error - unimplemented function unk_lang_printchar called.");
1382 unk_lang_printstr (struct ui_file
*stream
, char *string
, unsigned int length
,
1383 int width
, int force_ellipses
)
1385 error ("internal error - unimplemented function unk_lang_printstr called.");
1388 static struct type
*
1389 unk_lang_create_fundamental_type (struct objfile
*objfile
, int typeid)
1391 error ("internal error - unimplemented function unk_lang_create_fundamental_type called.");
1395 unk_lang_print_type (struct type
*type
, char *varstring
, struct ui_file
*stream
,
1396 int show
, int level
)
1398 error ("internal error - unimplemented function unk_lang_print_type called.");
1402 unk_lang_val_print (struct type
*type
, char *valaddr
, int embedded_offset
,
1403 CORE_ADDR address
, struct ui_file
*stream
, int format
,
1404 int deref_ref
, int recurse
, enum val_prettyprint pretty
)
1406 error ("internal error - unimplemented function unk_lang_val_print called.");
1410 unk_lang_value_print (struct value
*val
, struct ui_file
*stream
, int format
,
1411 enum val_prettyprint pretty
)
1413 error ("internal error - unimplemented function unk_lang_value_print called.");
1416 static struct type
**const (unknown_builtin_types
[]) =
1420 static const struct op_print unk_op_print_tab
[] =
1422 {NULL
, OP_NULL
, PREC_NULL
, 0}
1425 const struct language_defn unknown_language_defn
=
1429 &unknown_builtin_types
[0],
1435 evaluate_subexp_standard
,
1436 unk_lang_printchar
, /* Print character constant */
1439 unk_lang_create_fundamental_type
,
1440 unk_lang_print_type
, /* Print a type using appropriate syntax */
1441 unk_lang_val_print
, /* Print a value using appropriate syntax */
1442 unk_lang_value_print
, /* Print a top-level value */
1443 {"", "", "", ""}, /* Binary format info */
1444 {"0%lo", "0", "o", ""}, /* Octal format info */
1445 {"%ld", "", "d", ""}, /* Decimal format info */
1446 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1447 unk_op_print_tab
, /* expression operators for printing */
1448 1, /* c-style arrays */
1449 0, /* String lower bound */
1450 &builtin_type_char
, /* Type of string elements */
1454 /* These two structs define fake entries for the "local" and "auto" options. */
1455 const struct language_defn auto_language_defn
=
1459 &unknown_builtin_types
[0],
1465 evaluate_subexp_standard
,
1466 unk_lang_printchar
, /* Print character constant */
1469 unk_lang_create_fundamental_type
,
1470 unk_lang_print_type
, /* Print a type using appropriate syntax */
1471 unk_lang_val_print
, /* Print a value using appropriate syntax */
1472 unk_lang_value_print
, /* Print a top-level value */
1473 {"", "", "", ""}, /* Binary format info */
1474 {"0%lo", "0", "o", ""}, /* Octal format info */
1475 {"%ld", "", "d", ""}, /* Decimal format info */
1476 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1477 unk_op_print_tab
, /* expression operators for printing */
1478 1, /* c-style arrays */
1479 0, /* String lower bound */
1480 &builtin_type_char
, /* Type of string elements */
1484 const struct language_defn local_language_defn
=
1488 &unknown_builtin_types
[0],
1494 evaluate_subexp_standard
,
1495 unk_lang_printchar
, /* Print character constant */
1498 unk_lang_create_fundamental_type
,
1499 unk_lang_print_type
, /* Print a type using appropriate syntax */
1500 unk_lang_val_print
, /* Print a value using appropriate syntax */
1501 unk_lang_value_print
, /* Print a top-level value */
1502 {"", "", "", ""}, /* Binary format info */
1503 {"0%lo", "0", "o", ""}, /* Octal format info */
1504 {"%ld", "", "d", ""}, /* Decimal format info */
1505 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1506 unk_op_print_tab
, /* expression operators for printing */
1507 1, /* c-style arrays */
1508 0, /* String lower bound */
1509 &builtin_type_char
, /* Type of string elements */
1513 /* Initialize the language routines */
1516 _initialize_language (void)
1518 struct cmd_list_element
*set
, *show
;
1520 /* GDB commands for language specific stuff */
1522 set
= add_set_cmd ("language", class_support
, var_string_noescape
,
1524 "Set the current source language.",
1526 show
= add_show_from_set (set
, &showlist
);
1527 set_cmd_cfunc (set
, set_language_command
);
1528 set_cmd_cfunc (show
, show_language_command
);
1530 add_prefix_cmd ("check", no_class
, set_check
,
1531 "Set the status of the type/range checker.",
1532 &setchecklist
, "set check ", 0, &setlist
);
1533 add_alias_cmd ("c", "check", no_class
, 1, &setlist
);
1534 add_alias_cmd ("ch", "check", no_class
, 1, &setlist
);
1536 add_prefix_cmd ("check", no_class
, show_check
,
1537 "Show the status of the type/range checker.",
1538 &showchecklist
, "show check ", 0, &showlist
);
1539 add_alias_cmd ("c", "check", no_class
, 1, &showlist
);
1540 add_alias_cmd ("ch", "check", no_class
, 1, &showlist
);
1542 set
= add_set_cmd ("type", class_support
, var_string_noescape
,
1544 "Set type checking. (on/warn/off/auto)",
1546 show
= add_show_from_set (set
, &showchecklist
);
1547 set_cmd_cfunc (set
, set_type_command
);
1548 set_cmd_cfunc (show
, show_type_command
);
1550 set
= add_set_cmd ("range", class_support
, var_string_noescape
,
1552 "Set range checking. (on/warn/off/auto)",
1554 show
= add_show_from_set (set
, &showchecklist
);
1555 set_cmd_cfunc (set
, set_range_command
);
1556 set_cmd_cfunc (show
, show_range_command
);
1558 set
= add_set_cmd ("case-sensitive", class_support
, var_string_noescape
,
1559 (char *) &case_sensitive
,
1560 "Set case sensitivity in name search. (on/off/auto)\n\
1561 For Fortran the default is off; for other languages the default is on.",
1563 show
= add_show_from_set (set
, &showlist
);
1564 set_cmd_cfunc (set
, set_case_command
);
1565 set_cmd_cfunc (show
, show_case_command
);
1567 add_language (&unknown_language_defn
);
1568 add_language (&local_language_defn
);
1569 add_language (&auto_language_defn
);
1571 language
= savestring ("auto", strlen ("auto"));
1572 type
= savestring ("auto", strlen ("auto"));
1573 range
= savestring ("auto", strlen ("auto"));
1574 case_sensitive
= savestring ("auto",strlen ("auto"));
1576 /* Have the above take effect */
1577 set_language (language_auto
);