1 /* Multiple source language support for GDB.
2 Copyright 1991, 1992, 2000 Free Software Foundation, Inc.
3 Contributed by the Department of Computer Science at the State University
4 of New York at Buffalo.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* This file contains functions that return things that are specific
24 to languages. Each function should examine current_language if necessary,
25 and return the appropriate result. */
27 /* FIXME: Most of these would be better organized as macros which
28 return data out of a "language-specific" struct pointer that is set
29 whenever the working language changes. That would be a lot faster. */
33 #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 set_range_str (void);
62 static void set_type_str (void);
64 static void set_lang_str (void);
66 static void unk_lang_error (char *);
68 static int unk_lang_parser (void);
70 static void show_check (char *, int);
72 static void set_check (char *, int);
74 static void set_type_range (void);
76 static void unk_lang_emit_char (int c, struct ui_file *stream, int quoter);
78 static void unk_lang_printchar (int c, struct ui_file *stream);
80 static void unk_lang_printstr (struct ui_file * stream, char *string,
81 unsigned int length, int width,
84 static struct type *unk_lang_create_fundamental_type (struct objfile *, int);
86 static void unk_lang_print_type (struct type *, char *, struct ui_file *,
89 static int unk_lang_val_print (struct type *, char *, int, CORE_ADDR,
90 struct ui_file *, int, int, int,
91 enum val_prettyprint);
93 static int unk_lang_value_print (value_ptr, struct ui_file *, int, enum val_prettyprint);
95 /* Forward declaration */
96 extern const struct language_defn unknown_language_defn;
97 extern char *warning_pre_print;
99 /* The current (default at startup) state of type and range checking.
100 (If the modes are set to "auto", though, these are changed based
101 on the default language at startup, and then again based on the
102 language of the first source file. */
104 enum range_mode range_mode = range_mode_auto;
105 enum range_check range_check = range_check_off;
106 enum type_mode type_mode = type_mode_auto;
107 enum type_check type_check = type_check_off;
109 /* The current language and language_mode (see language.h) */
111 const struct language_defn *current_language = &unknown_language_defn;
112 enum language_mode language_mode = language_mode_auto;
114 /* The language that the user expects to be typing in (the language
115 of main(), or the last language we notified them about, or C). */
117 const struct language_defn *expected_language;
119 /* The list of supported languages. The list itself is malloc'd. */
121 static const struct language_defn **languages;
122 static unsigned languages_size;
123 static unsigned languages_allocsize;
124 #define DEFAULT_ALLOCSIZE 4
126 /* The "set language/type/range" commands all put stuff in these
127 buffers. This is to make them work as set/show commands. The
128 user's string is copied here, then the set_* commands look at
129 them and update them to something that looks nice when it is
132 static char *language;
136 /* Warning issued when current_language and the language of the current
137 frame do not match. */
138 char lang_frame_mismatch_warn[] =
139 "Warning: the current language does not match this frame.";
142 /* This page contains the functions corresponding to GDB commands
143 and their helpers. */
145 /* Show command. Display a warning if the language set
146 does not match the frame. */
148 show_language_command (char *ignore, int from_tty)
150 enum language flang; /* The language of the current frame */
152 flang = get_frame_language ();
153 if (flang != language_unknown &&
154 language_mode == language_mode_manual &&
155 current_language->la_language != flang)
156 printf_filtered ("%s\n", lang_frame_mismatch_warn);
159 /* Set command. Change the current working language. */
161 set_language_command (char *ignore, int from_tty)
167 if (!language || !language[0])
169 printf_unfiltered ("The currently understood settings are:\n\n");
170 printf_unfiltered ("local or auto Automatic setting based on source file\n");
172 for (i = 0; i < languages_size; ++i)
174 /* Already dealt with these above. */
175 if (languages[i]->la_language == language_unknown
176 || languages[i]->la_language == language_auto)
179 /* FIXME for now assume that the human-readable name is just
180 a capitalization of the internal name. */
181 printf_unfiltered ("%-16s Use the %c%s language\n",
182 languages[i]->la_name,
183 /* Capitalize first letter of language
185 toupper (languages[i]->la_name[0]),
186 languages[i]->la_name + 1);
188 /* Restore the silly string. */
189 set_language (current_language->la_language);
193 /* Search the list of languages for a match. */
194 for (i = 0; i < languages_size; i++)
196 if (STREQ (languages[i]->la_name, language))
198 /* Found it! Go into manual mode, and use this language. */
199 if (languages[i]->la_language == language_auto)
201 /* Enter auto mode. Set to the current frame's language, if known. */
202 language_mode = language_mode_auto;
203 flang = get_frame_language ();
204 if (flang != language_unknown)
205 set_language (flang);
206 expected_language = current_language;
211 /* Enter manual mode. Set the specified language. */
212 language_mode = language_mode_manual;
213 current_language = languages[i];
216 expected_language = current_language;
222 /* Reset the language (esp. the global string "language") to the
224 err_lang = savestring (language, strlen (language));
225 make_cleanup (free, err_lang); /* Free it after error */
226 set_language (current_language->la_language);
227 error ("Unknown language `%s'.", err_lang);
230 /* Show command. Display a warning if the type setting does
231 not match the current language. */
233 show_type_command (char *ignore, int from_tty)
235 if (type_check != current_language->la_type_check)
237 "Warning: the current type check setting does not match the language.\n");
240 /* Set command. Change the setting for type checking. */
242 set_type_command (char *ignore, int from_tty)
244 if (STREQ (type, "on"))
246 type_check = type_check_on;
247 type_mode = type_mode_manual;
249 else if (STREQ (type, "warn"))
251 type_check = type_check_warn;
252 type_mode = type_mode_manual;
254 else if (STREQ (type, "off"))
256 type_check = type_check_off;
257 type_mode = type_mode_manual;
259 else if (STREQ (type, "auto"))
261 type_mode = type_mode_auto;
263 /* Avoid hitting the set_type_str call below. We
264 did it in set_type_range. */
269 warning ("Unrecognized type check setting: \"%s\"", type);
272 show_type_command ((char *) NULL, from_tty);
275 /* Show command. Display a warning if the range setting does
276 not match the current language. */
278 show_range_command (char *ignore, int from_tty)
281 if (range_check != current_language->la_range_check)
283 "Warning: the current range check setting does not match the language.\n");
286 /* Set command. Change the setting for range checking. */
288 set_range_command (char *ignore, int from_tty)
290 if (STREQ (range, "on"))
292 range_check = range_check_on;
293 range_mode = range_mode_manual;
295 else if (STREQ (range, "warn"))
297 range_check = range_check_warn;
298 range_mode = range_mode_manual;
300 else if (STREQ (range, "off"))
302 range_check = range_check_off;
303 range_mode = range_mode_manual;
305 else if (STREQ (range, "auto"))
307 range_mode = range_mode_auto;
309 /* Avoid hitting the set_range_str call below. We
310 did it in set_type_range. */
315 warning ("Unrecognized range check setting: \"%s\"", range);
318 show_range_command ((char *) 0, from_tty);
321 /* Set the status of range and type checking based on
322 the current modes and the current language.
323 If SHOW is non-zero, then print out the current language,
324 type and range checking status. */
326 set_type_range (void)
329 if (range_mode == range_mode_auto)
330 range_check = current_language->la_range_check;
332 if (type_mode == type_mode_auto)
333 type_check = current_language->la_type_check;
339 /* Set current language to (enum language) LANG. Returns previous language. */
342 set_language (enum language lang)
345 enum language prev_language;
347 prev_language = current_language->la_language;
349 for (i = 0; i < languages_size; i++)
351 if (languages[i]->la_language == lang)
353 current_language = languages[i];
360 return prev_language;
363 /* This page contains functions that update the global vars
364 language, type and range. */
372 if (language_mode == language_mode_auto)
373 prefix = "auto; currently ";
375 language = concat (prefix, current_language->la_name, NULL);
381 char *tmp = NULL, *prefix = "";
385 if (type_mode == type_mode_auto)
386 prefix = "auto; currently ";
396 case type_check_warn:
400 error ("Unrecognized type check setting.");
403 type = concat (prefix, tmp, NULL);
409 char *tmp, *pref = "";
411 if (range_mode == range_mode_auto)
412 pref = "auto; currently ";
419 case range_check_off:
422 case range_check_warn:
426 error ("Unrecognized range check setting.");
431 range = concat (pref, tmp, NULL);
435 /* Print out the current language settings: language, range and
436 type checking. If QUIETLY, print only what has changed. */
439 language_info (int quietly)
441 if (quietly && expected_language == current_language)
444 expected_language = current_language;
445 printf_unfiltered ("Current language: %s\n", language);
446 show_language_command ((char *) 0, 1);
450 printf_unfiltered ("Type checking: %s\n", type);
451 show_type_command ((char *) 0, 1);
452 printf_unfiltered ("Range checking: %s\n", range);
453 show_range_command ((char *) 0, 1);
457 /* Return the result of a binary operation. */
459 #if 0 /* Currently unused */
462 binop_result_type (value_ptr v1, value_ptr v2)
465 struct type *t1 = check_typedef (VALUE_TYPE (v1));
466 struct type *t2 = check_typedef (VALUE_TYPE (v2));
468 int l1 = TYPE_LENGTH (t1);
469 int l2 = TYPE_LENGTH (t2);
471 switch (current_language->la_language)
475 if (TYPE_CODE (t1) == TYPE_CODE_FLT)
476 return TYPE_CODE (t2) == TYPE_CODE_FLT && l2 > l1 ?
477 VALUE_TYPE (v2) : VALUE_TYPE (v1);
478 else if (TYPE_CODE (t2) == TYPE_CODE_FLT)
479 return TYPE_CODE (t1) == TYPE_CODE_FLT && l1 > l2 ?
480 VALUE_TYPE (v1) : VALUE_TYPE (v2);
481 else if (TYPE_UNSIGNED (t1) && l1 > l2)
482 return VALUE_TYPE (v1);
483 else if (TYPE_UNSIGNED (t2) && l2 > l1)
484 return VALUE_TYPE (v2);
485 else /* Both are signed. Result is the longer type */
486 return l1 > l2 ? VALUE_TYPE (v1) : VALUE_TYPE (v2);
489 /* If we are doing type-checking, l1 should equal l2, so this is
491 return l1 > l2 ? VALUE_TYPE (v1) : VALUE_TYPE (v2);
494 error ("Missing Chill support in function binop_result_check."); /*FIXME */
497 return (struct type *) 0; /* For lint */
503 /* This page contains functions that return format strings for
504 printf for printing out numbers in different formats */
506 /* Returns the appropriate printf format for hexadecimal
509 local_hex_format_custom (char *pre)
511 static char form[50];
513 strcpy (form, local_hex_format_prefix ());
516 strcat (form, local_hex_format_specifier ());
517 strcat (form, local_hex_format_suffix ());
522 /* FIXME: cagney/2000-03-04: This function does not appear to be used.
523 It can be deleted once 5.0 has been released. */
524 /* FIXME: cagney/2000-03-04: This code assumes that the compiler
525 supports ``long long''. */
526 /* Converts a number to hexadecimal (without leading "0x") and stores it in a
527 static string. Returns a pointer to this string. */
530 longest_raw_hex_string (LONGEST num)
532 static char res_longest_raw_hex_string[50];
533 long long ll = num; /* MERGEBUG ?? see below */
534 res_longest_raw_hex_string[0] = 0;
535 /* MERGEBUG ?? As a quick fix I am replacing this with sprintf
536 strcat_address_numeric (num, 0, res_longest_raw_hex_string, 50);
539 sprintf (res_longest_raw_hex_string, "%llx", ll);
540 return res_longest_raw_hex_string;
544 /* Converts a number to hexadecimal and stores it in a static
545 string. Returns a pointer to this string. */
547 local_hex_string (unsigned long num)
551 sprintf (res, local_hex_format (), num);
555 /* Converts a LONGEST number to hexadecimal and stores it in a static
556 string. Returns a pointer to this string. */
558 longest_local_hex_string (LONGEST num)
560 return longest_local_hex_string_custom (num, "l");
563 /* Converts a number to custom hexadecimal and stores it in a static
564 string. Returns a pointer to this string. */
566 local_hex_string_custom (unsigned long num, char *pre)
570 sprintf (res, local_hex_format_custom (pre), num);
574 /* Converts a LONGEST number to custom hexadecimal and stores it in a static
575 string. Returns a pointer to this string. Note that the width parameter
576 should end with "l", e.g. "08l" as with calls to local_hex_string_custom */
579 longest_local_hex_string_custom (LONGEST num, char *width)
581 #define RESULT_BUF_LEN 50
582 static char res2[RESULT_BUF_LEN];
583 char format[RESULT_BUF_LEN];
584 #if !defined (PRINTF_HAS_LONG_LONG)
588 char *pad_char; /* string with one character */
591 char temp_nbr_buf[RESULT_BUF_LEN];
594 #ifndef CC_HAS_LONG_LONG
595 /* If there is no long long, then LONGEST should be just long and we
596 can use local_hex_string_custom
598 return local_hex_string_custom ((unsigned long) num, width);
599 #elif defined (PRINTF_HAS_LONG_LONG)
600 /* Just use printf. */
601 strcpy (format, local_hex_format_prefix ()); /* 0x */
602 strcat (format, "%");
603 strcat (format, width); /* e.g. "08l" */
604 strcat (format, "l"); /* need "ll" for long long */
605 strcat (format, local_hex_format_specifier ()); /* "x" */
606 strcat (format, local_hex_format_suffix ()); /* "" */
607 sprintf (res2, format, num);
609 #else /* !defined (PRINTF_HAS_LONG_LONG) */
610 /* Use strcat_address_numeric to print the number into a string, then
611 build the result string from local_hex_format_prefix, padding and
612 the hex representation as indicated by "width". */
615 /* With use_local == 0, we don't get the leading "0x" prefix. */
616 /* MERGEBUG ?? As a quick fix I am replacing this call to
617 strcat_address_numeric with sprintf
618 strcat_address_numeric(num, 0, temp_nbr_buf, RESULT_BUF_LEN);
623 sprintf (temp_nbr_buf, "%llx", ll);
629 if (*parse_ptr == '-')
634 if (*parse_ptr == '0')
638 pad_char = "0"; /* If padding is on the right, it is blank */
640 field_width = atoi (parse_ptr);
641 num_len = strlen (temp_nbr_buf);
642 num_pad_chars = field_width - strlen (temp_nbr_buf); /* possibly negative */
644 if (strlen (local_hex_format_prefix ()) + num_len + num_pad_chars
645 < RESULT_BUF_LEN) /* paranoia */
646 internal_error ("longest_local_hex_string_custom: insufficient space to store result");
648 strcpy (res2, local_hex_format_prefix ());
651 while (num_pad_chars > 0)
653 strcat (res2, pad_char);
657 strcat (res2, temp_nbr_buf);
660 while (num_pad_chars > 0)
662 strcat (res2, pad_char);
669 } /* longest_local_hex_string_custom */
671 /* Returns the appropriate printf format for octal
674 local_octal_format_custom (char *pre)
676 static char form[50];
678 strcpy (form, local_octal_format_prefix ());
681 strcat (form, local_octal_format_specifier ());
682 strcat (form, local_octal_format_suffix ());
686 /* Returns the appropriate printf format for decimal numbers. */
688 local_decimal_format_custom (char *pre)
690 static char form[50];
692 strcpy (form, local_decimal_format_prefix ());
695 strcat (form, local_decimal_format_specifier ());
696 strcat (form, local_decimal_format_suffix ());
701 /* This page contains functions that are used in type/range checking.
702 They all return zero if the type/range check fails.
704 It is hoped that these will make extending GDB to parse different
705 languages a little easier. These are primarily used in eval.c when
706 evaluating expressions and making sure that their types are correct.
707 Instead of having a mess of conjucted/disjuncted expressions in an "if",
708 the ideas of type can be wrapped up in the following functions.
710 Note that some of them are not currently dependent upon which language
711 is currently being parsed. For example, floats are the same in
712 C and Modula-2 (ie. the only floating point type has TYPE_CODE of
713 TYPE_CODE_FLT), while booleans are different. */
715 /* Returns non-zero if its argument is a simple type. This is the same for
716 both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur,
717 and thus will never cause the failure of the test. */
719 simple_type (struct type *type)
721 CHECK_TYPEDEF (type);
722 switch (TYPE_CODE (type))
728 case TYPE_CODE_RANGE:
737 /* Returns non-zero if its argument is of an ordered type.
738 An ordered type is one in which the elements can be tested for the
739 properties of "greater than", "less than", etc, or for which the
740 operations "increment" or "decrement" make sense. */
742 ordered_type (struct type *type)
744 CHECK_TYPEDEF (type);
745 switch (TYPE_CODE (type))
751 case TYPE_CODE_RANGE:
759 /* Returns non-zero if the two types are the same */
761 same_type (struct type *arg1, struct type *arg2)
763 CHECK_TYPEDEF (type);
764 if (structured_type (arg1) ? !structured_type (arg2) : structured_type (arg2))
765 /* One is structured and one isn't */
767 else if (structured_type (arg1) && structured_type (arg2))
769 else if (numeric_type (arg1) && numeric_type (arg2))
770 return (TYPE_CODE (arg2) == TYPE_CODE (arg1)) &&
771 (TYPE_UNSIGNED (arg1) == TYPE_UNSIGNED (arg2))
777 /* Returns non-zero if the type is integral */
779 integral_type (struct type *type)
781 CHECK_TYPEDEF (type);
782 switch (current_language->la_language)
786 return (TYPE_CODE (type) != TYPE_CODE_INT) &&
787 (TYPE_CODE (type) != TYPE_CODE_ENUM) ? 0 : 1;
789 case language_pascal:
790 return TYPE_CODE (type) != TYPE_CODE_INT ? 0 : 1;
792 error ("Missing Chill support in function integral_type."); /*FIXME */
794 error ("Language not supported.");
798 /* Returns non-zero if the value is numeric */
800 numeric_type (struct type *type)
802 CHECK_TYPEDEF (type);
803 switch (TYPE_CODE (type))
814 /* Returns non-zero if the value is a character type */
816 character_type (struct type *type)
818 CHECK_TYPEDEF (type);
819 switch (current_language->la_language)
823 case language_pascal:
824 return TYPE_CODE (type) != TYPE_CODE_CHAR ? 0 : 1;
828 return (TYPE_CODE (type) == TYPE_CODE_INT) &&
829 TYPE_LENGTH (type) == sizeof (char)
836 /* Returns non-zero if the value is a string type */
838 string_type (struct type *type)
840 CHECK_TYPEDEF (type);
841 switch (current_language->la_language)
845 case language_pascal:
846 return TYPE_CODE (type) != TYPE_CODE_STRING ? 0 : 1;
850 /* C does not have distinct string type. */
857 /* Returns non-zero if the value is a boolean type */
859 boolean_type (struct type *type)
861 CHECK_TYPEDEF (type);
862 if (TYPE_CODE (type) == TYPE_CODE_BOOL)
864 switch (current_language->la_language)
868 /* Might be more cleanly handled by having a TYPE_CODE_INT_NOT_BOOL
869 for CHILL and such languages, or a TYPE_CODE_INT_OR_BOOL for C. */
870 if (TYPE_CODE (type) == TYPE_CODE_INT)
878 /* Returns non-zero if the value is a floating-point type */
880 float_type (struct type *type)
882 CHECK_TYPEDEF (type);
883 return TYPE_CODE (type) == TYPE_CODE_FLT;
886 /* Returns non-zero if the value is a pointer type */
888 pointer_type (struct type *type)
890 return TYPE_CODE (type) == TYPE_CODE_PTR ||
891 TYPE_CODE (type) == TYPE_CODE_REF;
894 /* Returns non-zero if the value is a structured type */
896 structured_type (struct type *type)
898 CHECK_TYPEDEF (type);
899 switch (current_language->la_language)
903 return (TYPE_CODE (type) == TYPE_CODE_STRUCT) ||
904 (TYPE_CODE (type) == TYPE_CODE_UNION) ||
905 (TYPE_CODE (type) == TYPE_CODE_ARRAY);
906 case language_pascal:
907 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
908 (TYPE_CODE(type) == TYPE_CODE_UNION) ||
909 (TYPE_CODE(type) == TYPE_CODE_SET) ||
910 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
912 return (TYPE_CODE (type) == TYPE_CODE_STRUCT) ||
913 (TYPE_CODE (type) == TYPE_CODE_SET) ||
914 (TYPE_CODE (type) == TYPE_CODE_ARRAY);
916 error ("Missing Chill support in function structured_type."); /*FIXME */
924 lang_bool_type (void)
928 switch (current_language->la_language)
931 return builtin_type_chill_bool;
932 case language_fortran:
933 sym = lookup_symbol ("logical", NULL, VAR_NAMESPACE, NULL, NULL);
936 type = SYMBOL_TYPE (sym);
937 if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
940 return builtin_type_f_logical_s2;
942 case language_pascal:
943 if (current_language->la_language==language_cplus)
944 {sym = lookup_symbol ("bool", NULL, VAR_NAMESPACE, NULL, NULL);}
946 {sym = lookup_symbol ("boolean", NULL, VAR_NAMESPACE, NULL, NULL);}
949 type = SYMBOL_TYPE (sym);
950 if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
953 return builtin_type_bool;
955 sym = lookup_symbol ("boolean", NULL, VAR_NAMESPACE, NULL, NULL);
958 type = SYMBOL_TYPE (sym);
959 if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
962 return java_boolean_type;
964 return builtin_type_int;
968 /* This page contains functions that return info about
969 (struct value) values used in GDB. */
971 /* Returns non-zero if the value VAL represents a true value. */
973 value_true (value_ptr val)
975 /* It is possible that we should have some sort of error if a non-boolean
976 value is used in this context. Possibly dependent on some kind of
977 "boolean-checking" option like range checking. But it should probably
978 not depend on the language except insofar as is necessary to identify
979 a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
980 should be an error, probably). */
981 return !value_logical_not (val);
984 /* Returns non-zero if the operator OP is defined on
985 the values ARG1 and ARG2. */
987 #if 0 /* Currently unused */
990 binop_type_check (value_ptr arg1, value_ptr arg2, int op)
992 struct type *t1, *t2;
994 /* If we're not checking types, always return success. */
998 t1 = VALUE_TYPE (arg1);
1000 t2 = VALUE_TYPE (arg2);
1008 if ((numeric_type (t1) && pointer_type (t2)) ||
1009 (pointer_type (t1) && numeric_type (t2)))
1011 warning ("combining pointer and integer.\n");
1017 if (!numeric_type (t1) || !numeric_type (t2))
1018 type_op_error ("Arguments to %s must be numbers.", op);
1019 else if (!same_type (t1, t2))
1020 type_op_error ("Arguments to %s must be of the same type.", op);
1023 case BINOP_LOGICAL_AND:
1024 case BINOP_LOGICAL_OR:
1025 if (!boolean_type (t1) || !boolean_type (t2))
1026 type_op_error ("Arguments to %s must be of boolean type.", op);
1030 if ((pointer_type (t1) && !(pointer_type (t2) || integral_type (t2))) ||
1031 (pointer_type (t2) && !(pointer_type (t1) || integral_type (t1))))
1032 type_op_error ("A pointer can only be compared to an integer or pointer.", op);
1033 else if ((pointer_type (t1) && integral_type (t2)) ||
1034 (integral_type (t1) && pointer_type (t2)))
1036 warning ("combining integer and pointer.\n");
1039 else if (!simple_type (t1) || !simple_type (t2))
1040 type_op_error ("Arguments to %s must be of simple type.", op);
1041 else if (!same_type (t1, t2))
1042 type_op_error ("Arguments to %s must be of the same type.", op);
1047 if (!integral_type (t1) || !integral_type (t2))
1048 type_op_error ("Arguments to %s must be of integral type.", op);
1055 if (!ordered_type (t1) || !ordered_type (t2))
1056 type_op_error ("Arguments to %s must be of ordered type.", op);
1057 else if (!same_type (t1, t2))
1058 type_op_error ("Arguments to %s must be of the same type.", op);
1062 if (pointer_type (t1) && !integral_type (t2))
1063 type_op_error ("A pointer can only be assigned an integer.", op);
1064 else if (pointer_type (t1) && integral_type (t2))
1066 warning ("combining integer and pointer.");
1069 else if (!simple_type (t1) || !simple_type (t2))
1070 type_op_error ("Arguments to %s must be of simple type.", op);
1071 else if (!same_type (t1, t2))
1072 type_op_error ("Arguments to %s must be of the same type.", op);
1076 /* FIXME: Needs to handle bitstrings as well. */
1077 if (!(string_type (t1) || character_type (t1) || integral_type (t1))
1078 || !(string_type (t2) || character_type (t2) || integral_type (t2)))
1079 type_op_error ("Arguments to %s must be strings or characters.", op);
1082 /* Unary checks -- arg2 is null */
1084 case UNOP_LOGICAL_NOT:
1085 if (!boolean_type (t1))
1086 type_op_error ("Argument to %s must be of boolean type.", op);
1091 if (!numeric_type (t1))
1092 type_op_error ("Argument to %s must be of numeric type.", op);
1096 if (integral_type (t1))
1098 warning ("combining pointer and integer.\n");
1101 else if (!pointer_type (t1))
1102 type_op_error ("Argument to %s must be a pointer.", op);
1105 case UNOP_PREINCREMENT:
1106 case UNOP_POSTINCREMENT:
1107 case UNOP_PREDECREMENT:
1108 case UNOP_POSTDECREMENT:
1109 if (!ordered_type (t1))
1110 type_op_error ("Argument to %s must be of an ordered type.", op);
1114 /* Ok. The following operators have different meanings in
1115 different languages. */
1116 switch (current_language->la_language)
1120 case language_cplus:
1124 if (!numeric_type (t1) || !numeric_type (t2))
1125 type_op_error ("Arguments to %s must be numbers.", op);
1136 if (!float_type (t1) || !float_type (t2))
1137 type_op_error ("Arguments to %s must be floating point numbers.", op);
1140 if (!integral_type (t1) || !integral_type (t2))
1141 type_op_error ("Arguments to %s must be of integral type.", op);
1147 case language_pascal:
1151 if (!float_type(t1) && !float_type(t2))
1152 type_op_error ("Arguments to %s must be floating point numbers.",op);
1155 if (!integral_type(t1) || !integral_type(t2))
1156 type_op_error ("Arguments to %s must be of integral type.",op);
1162 case language_chill:
1163 error ("Missing Chill support in function binop_type_check."); /*FIXME */
1173 /* This page contains functions for the printing out of
1174 error messages that occur during type- and range-
1177 /* Prints the format string FMT with the operator as a string
1178 corresponding to the opcode OP. If FATAL is non-zero, then
1179 this is an error and error () is called. Otherwise, it is
1180 a warning and printf() is called. */
1182 op_error (char *fmt, enum exp_opcode op, int fatal)
1185 error (fmt, op_string (op));
1188 warning (fmt, op_string (op));
1192 /* These are called when a language fails a type- or range-check.
1193 The first argument should be a printf()-style format string, and
1194 the rest of the arguments should be its arguments. If
1195 [type|range]_check is [type|range]_check_on, then return_to_top_level()
1196 is called in the style of error (). Otherwise, the message is prefixed
1197 by the value of warning_pre_print and we do not return to the top level. */
1200 type_error (char *string,...)
1203 va_start (args, string);
1205 if (type_check == type_check_warn)
1206 fprintf_filtered (gdb_stderr, warning_pre_print);
1210 vfprintf_filtered (gdb_stderr, string, args);
1211 fprintf_filtered (gdb_stderr, "\n");
1213 if (type_check == type_check_on)
1214 return_to_top_level (RETURN_ERROR);
1218 range_error (char *string,...)
1221 va_start (args, string);
1223 if (range_check == range_check_warn)
1224 fprintf_filtered (gdb_stderr, warning_pre_print);
1228 vfprintf_filtered (gdb_stderr, string, args);
1229 fprintf_filtered (gdb_stderr, "\n");
1231 if (range_check == range_check_on)
1232 return_to_top_level (RETURN_ERROR);
1236 /* This page contains miscellaneous functions */
1238 /* Return the language enum for a given language string. */
1241 language_enum (char *str)
1245 for (i = 0; i < languages_size; i++)
1246 if (STREQ (languages[i]->la_name, str))
1247 return languages[i]->la_language;
1249 return language_unknown;
1252 /* Return the language struct for a given language enum. */
1254 const struct language_defn *
1255 language_def (enum language lang)
1259 for (i = 0; i < languages_size; i++)
1261 if (languages[i]->la_language == lang)
1263 return languages[i];
1269 /* Return the language as a string */
1271 language_str (enum language lang)
1275 for (i = 0; i < languages_size; i++)
1277 if (languages[i]->la_language == lang)
1279 return languages[i]->la_name;
1286 set_check (char *ignore, int from_tty)
1289 "\"set check\" must be followed by the name of a check subcommand.\n");
1290 help_list (setchecklist, "set check ", -1, gdb_stdout);
1294 show_check (char *ignore, int from_tty)
1296 cmd_show_list (showchecklist, from_tty, "");
1299 /* Add a language to the set of known languages. */
1302 add_language (const struct language_defn *lang)
1304 if (lang->la_magic != LANG_MAGIC)
1306 fprintf_unfiltered (gdb_stderr, "Magic number of %s language struct wrong\n",
1313 languages_allocsize = DEFAULT_ALLOCSIZE;
1314 languages = (const struct language_defn **) xmalloc
1315 (languages_allocsize * sizeof (*languages));
1317 if (languages_size >= languages_allocsize)
1319 languages_allocsize *= 2;
1320 languages = (const struct language_defn **) xrealloc ((char *) languages,
1321 languages_allocsize * sizeof (*languages));
1323 languages[languages_size++] = lang;
1326 /* Define the language that is no language. */
1329 unk_lang_parser (void)
1335 unk_lang_error (char *msg)
1337 error ("Attempted to parse an expression with unknown language");
1341 unk_lang_emit_char (register int c, struct ui_file *stream, int quoter)
1343 error ("internal error - unimplemented function unk_lang_emit_char called.");
1347 unk_lang_printchar (register int c, struct ui_file *stream)
1349 error ("internal error - unimplemented function unk_lang_printchar called.");
1353 unk_lang_printstr (struct ui_file *stream, char *string, unsigned int length,
1354 int width, int force_ellipses)
1356 error ("internal error - unimplemented function unk_lang_printstr called.");
1359 static struct type *
1360 unk_lang_create_fundamental_type (struct objfile *objfile, int typeid)
1362 error ("internal error - unimplemented function unk_lang_create_fundamental_type called.");
1366 unk_lang_print_type (struct type *type, char *varstring, struct ui_file *stream,
1367 int show, int level)
1369 error ("internal error - unimplemented function unk_lang_print_type called.");
1373 unk_lang_val_print (struct type *type, char *valaddr, int embedded_offset,
1374 CORE_ADDR address, struct ui_file *stream, int format,
1375 int deref_ref, int recurse, enum val_prettyprint pretty)
1377 error ("internal error - unimplemented function unk_lang_val_print called.");
1381 unk_lang_value_print (value_ptr val, struct ui_file *stream, int format,
1382 enum val_prettyprint pretty)
1384 error ("internal error - unimplemented function unk_lang_value_print called.");
1387 static struct type **CONST_PTR (unknown_builtin_types[]) =
1391 static const struct op_print unk_op_print_tab[] =
1393 {NULL, OP_NULL, PREC_NULL, 0}
1396 const struct language_defn unknown_language_defn =
1400 &unknown_builtin_types[0],
1405 evaluate_subexp_standard,
1406 unk_lang_printchar, /* Print character constant */
1409 unk_lang_create_fundamental_type,
1410 unk_lang_print_type, /* Print a type using appropriate syntax */
1411 unk_lang_val_print, /* Print a value using appropriate syntax */
1412 unk_lang_value_print, /* Print a top-level value */
1413 {"", "", "", ""}, /* Binary format info */
1414 {"0%lo", "0", "o", ""}, /* Octal format info */
1415 {"%ld", "", "d", ""}, /* Decimal format info */
1416 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1417 unk_op_print_tab, /* expression operators for printing */
1418 1, /* c-style arrays */
1419 0, /* String lower bound */
1420 &builtin_type_char, /* Type of string elements */
1424 /* These two structs define fake entries for the "local" and "auto" options. */
1425 const struct language_defn auto_language_defn =
1429 &unknown_builtin_types[0],
1434 evaluate_subexp_standard,
1435 unk_lang_printchar, /* Print character constant */
1438 unk_lang_create_fundamental_type,
1439 unk_lang_print_type, /* Print a type using appropriate syntax */
1440 unk_lang_val_print, /* Print a value using appropriate syntax */
1441 unk_lang_value_print, /* Print a top-level value */
1442 {"", "", "", ""}, /* Binary format info */
1443 {"0%lo", "0", "o", ""}, /* Octal format info */
1444 {"%ld", "", "d", ""}, /* Decimal format info */
1445 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1446 unk_op_print_tab, /* expression operators for printing */
1447 1, /* c-style arrays */
1448 0, /* String lower bound */
1449 &builtin_type_char, /* Type of string elements */
1453 const struct language_defn local_language_defn =
1457 &unknown_builtin_types[0],
1462 evaluate_subexp_standard,
1463 unk_lang_printchar, /* Print character constant */
1466 unk_lang_create_fundamental_type,
1467 unk_lang_print_type, /* Print a type using appropriate syntax */
1468 unk_lang_val_print, /* Print a value using appropriate syntax */
1469 unk_lang_value_print, /* Print a top-level value */
1470 {"", "", "", ""}, /* Binary format info */
1471 {"0%lo", "0", "o", ""}, /* Octal format info */
1472 {"%ld", "", "d", ""}, /* Decimal format info */
1473 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1474 unk_op_print_tab, /* expression operators for printing */
1475 1, /* c-style arrays */
1476 0, /* String lower bound */
1477 &builtin_type_char, /* Type of string elements */
1481 /* Initialize the language routines */
1484 _initialize_language (void)
1486 struct cmd_list_element *set, *show;
1488 /* GDB commands for language specific stuff */
1490 set = add_set_cmd ("language", class_support, var_string_noescape,
1492 "Set the current source language.",
1494 show = add_show_from_set (set, &showlist);
1495 set->function.cfunc = set_language_command;
1496 show->function.cfunc = show_language_command;
1498 add_prefix_cmd ("check", no_class, set_check,
1499 "Set the status of the type/range checker",
1500 &setchecklist, "set check ", 0, &setlist);
1501 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1502 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1504 add_prefix_cmd ("check", no_class, show_check,
1505 "Show the status of the type/range checker",
1506 &showchecklist, "show check ", 0, &showlist);
1507 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1508 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1510 set = add_set_cmd ("type", class_support, var_string_noescape,
1512 "Set type checking. (on/warn/off/auto)",
1514 show = add_show_from_set (set, &showchecklist);
1515 set->function.cfunc = set_type_command;
1516 show->function.cfunc = show_type_command;
1518 set = add_set_cmd ("range", class_support, var_string_noescape,
1520 "Set range checking. (on/warn/off/auto)",
1522 show = add_show_from_set (set, &showchecklist);
1523 set->function.cfunc = set_range_command;
1524 show->function.cfunc = show_range_command;
1526 add_language (&unknown_language_defn);
1527 add_language (&local_language_defn);
1528 add_language (&auto_language_defn);
1530 language = savestring ("auto", strlen ("auto"));
1531 set_language_command (language, 0);
1533 type = savestring ("auto", strlen ("auto"));
1534 set_type_command (NULL, 0);
1536 range = savestring ("auto", strlen ("auto"));
1537 set_range_command (NULL, 0);