1 /* Multiple source language support for GDB.
2 Copyright 1991 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 /* This file contains functions that return things that are specific
23 to languages. Each function should examine current_language if necessary,
24 and return the appropriate result. */
26 /* FIXME: Most of these would be better organized as macros which
27 return data out of a "language-specific" struct pointer that is set
28 whenever the working language changes. That would be a lot faster. */
40 #include "expression.h"
42 #include "parser-defs.h"
44 extern volatile void return_to_top_level ();
46 /* Forward function declarations */
47 static void set_type_range ();
49 /* Forward declaration */
50 extern struct language_defn unknown_language_defn;
52 /* The current (default at startup) state of type and range checking.
53 (If the modes are set to "auto", though, these are changed based
54 on the default language at startup, and then again based on the
55 language of the first source file. */
57 enum range_mode range_mode = range_mode_auto;
58 enum range_check range_check = range_check_off;
59 enum type_mode type_mode = type_mode_auto;
60 enum type_check type_check = type_check_off;
62 /* The current language and language_mode (see language.h) */
64 struct language_defn *current_language = &unknown_language_defn;
65 enum language_mode language_mode = language_mode_auto;
67 /* The list of supported languages. The list itself is malloc'd. */
69 static struct language_defn **languages;
70 static unsigned languages_size;
71 static unsigned languages_allocsize;
72 #define DEFAULT_ALLOCSIZE 3
74 /* The "set language/type/range" commands all put stuff in these
75 buffers. This is to make them work as set/show commands. The
76 user's string is copied here, then the set_* commands look at
77 them and update them to something that looks nice when it is
80 static char *language;
84 /* Warning issued when current_language and the language of the current
85 frame do not match. */
86 char lang_frame_mismatch_warn[] =
87 "Warning: the current language does not match this frame.";
93 /* This page contains the functions corresponding to GDB commands
96 /* Show command. Display a warning if the language set
97 does not match the frame. */
99 show_language_command (ignore, from_tty)
103 enum language flang; /* The language of the current frame */
105 flang = get_frame_language();
106 if (flang != language_unknown &&
107 language_mode == language_mode_manual &&
108 current_language->la_language != flang)
109 printf_filtered("%s\n",lang_frame_mismatch_warn);
112 /* Set command. Change the current working language. */
114 set_language_command (ignore, from_tty)
122 /* FIXME -- do this from the list, with HELP. */
123 if (!language || !language[0]) {
124 printf("The currently understood settings are:\n\n\
125 local or auto Automatic setting based on source file\n\
126 c Use the C language\n\
127 c++ Use the C++ language\n\
128 modula-2 Use the Modula-2 language\n");
129 /* Restore the silly string. */
130 set_language(current_language->la_language);
134 /* Search the list of languages for a match. */
135 for (i = 0; i < languages_size; i++) {
136 if (!strcmp (languages[i]->la_name, language)) {
137 /* Found it! Go into manual mode, and use this language. */
138 if (languages[i]->la_language == language_auto) {
139 /* Enter auto mode. Set to the current frame's language, if known. */
140 language_mode = language_mode_auto;
141 flang = get_frame_language();
142 if (flang!=language_unknown)
146 /* Enter manual mode. Set the specified language. */
147 language_mode = language_mode_manual;
148 current_language = languages[i];
156 /* Reset the language (esp. the global string "language") to the
158 err_lang=savestring(language,strlen(language));
159 make_cleanup (free, err_lang); /* Free it after error */
160 set_language(current_language->la_language);
161 error ("Unknown language `%s'.",err_lang);
164 /* Show command. Display a warning if the type setting does
165 not match the current language. */
167 show_type_command(ignore, from_tty)
171 if (type_check != current_language->la_type_check)
173 "Warning: the current type check setting does not match the language.\n");
176 /* Set command. Change the setting for type checking. */
178 set_type_command(ignore, from_tty)
182 if (!strcmp(type,"on"))
184 type_check = type_check_on;
185 type_mode = type_mode_manual;
187 else if (!strcmp(type,"warn"))
189 type_check = type_check_warn;
190 type_mode = type_mode_manual;
192 else if (!strcmp(type,"off"))
194 type_check = type_check_off;
195 type_mode = type_mode_manual;
197 else if (!strcmp(type,"auto"))
199 type_mode = type_mode_auto;
201 /* Avoid hitting the set_type_str call below. We
202 did it in set_type_range. */
206 show_type_command((char *)NULL, from_tty);
209 /* Show command. Display a warning if the range setting does
210 not match the current language. */
212 show_range_command(ignore, from_tty)
217 if (range_check != current_language->la_range_check)
219 "Warning: the current range check setting does not match the language.\n");
222 /* Set command. Change the setting for range checking. */
224 set_range_command(ignore, from_tty)
228 if (!strcmp(range,"on"))
230 range_check = range_check_on;
231 range_mode = range_mode_manual;
233 else if (!strcmp(range,"warn"))
235 range_check = range_check_warn;
236 range_mode = range_mode_manual;
238 else if (!strcmp(range,"off"))
240 range_check = range_check_off;
241 range_mode = range_mode_manual;
243 else if (!strcmp(range,"auto"))
245 range_mode = range_mode_auto;
247 /* Avoid hitting the set_range_str call below. We
248 did it in set_type_range. */
252 show_range_command((char *)0, from_tty);
255 /* Set the status of range and type checking based on
256 the current modes and the current language.
257 If SHOW is non-zero, then print out the current language,
258 type and range checking status. */
263 if (range_mode == range_mode_auto)
264 range_check = current_language->la_range_check;
266 if (type_mode == type_mode_auto)
267 type_check = current_language->la_type_check;
273 /* Set current language to (enum language) LANG. */
281 for (i = 0; i < languages_size; i++) {
282 if (languages[i]->la_language == lang) {
283 current_language = languages[i];
291 /* This page contains functions that update the global vars
292 language, type and range. */
299 if (language_mode == language_mode_auto)
300 prefix = "auto; currently ";
302 language = concat(prefix, current_language->la_name, NULL);
308 char *tmp, *prefix = "";
311 if (type_mode==type_mode_auto)
312 prefix = "auto; currently ";
322 case type_check_warn:
326 error ("Unrecognized type check setting.");
329 type = concat(prefix,tmp,NULL);
335 char *tmp, *pref = "";
338 if (range_mode==range_mode_auto)
339 pref = "auto; currently ";
346 case range_check_off:
349 case range_check_warn:
353 error ("Unrecognized range check setting.");
356 range = concat(pref,tmp,NULL);
360 /* Print out the current language settings: language, range and
365 printf("Current Language: %s\n",language);
366 show_language_command((char *)0, 1);
367 printf("Type checking: %s\n",type);
368 show_type_command((char *)0, 1);
369 printf("Range checking: %s\n",range);
370 show_range_command((char *)0, 1);
373 /* Return the result of a binary operation. */
375 binop_result_type(v1,v2)
380 l1 = TYPE_LENGTH(VALUE_TYPE(v1));
381 l2 = TYPE_LENGTH(VALUE_TYPE(v2));
383 switch(current_language->la_language)
387 if (TYPE_CODE(VALUE_TYPE(v1))==TYPE_CODE_FLT)
388 return TYPE_CODE(VALUE_TYPE(v2)) == TYPE_CODE_FLT && l2 > l1 ?
389 VALUE_TYPE(v2) : VALUE_TYPE(v1);
390 else if (TYPE_CODE(VALUE_TYPE(v2))==TYPE_CODE_FLT)
391 return TYPE_CODE(VALUE_TYPE(v1)) == TYPE_CODE_FLT && l1 > l2 ?
392 VALUE_TYPE(v1) : VALUE_TYPE(v2);
393 else if (TYPE_UNSIGNED(VALUE_TYPE(v1)) && l1 > l2)
394 return VALUE_TYPE(v1);
395 else if (TYPE_UNSIGNED(VALUE_TYPE(v2)) && l2 > l1)
396 return VALUE_TYPE(v2);
397 else /* Both are signed. Result is the longer type */
398 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
401 /* If we are doing type-checking, l1 should equal l2, so this is
403 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
407 return (struct type *)0; /* For lint */
410 /* This page contains functions that return format strings for
411 printf for printing out numbers in different formats */
413 /* Returns the appropriate printf format for hexadecimal
416 local_hex_format_custom(pre)
419 static char form[50];
421 strcpy (form, current_language->la_hex_format_pre);
423 strcat (form, current_language->la_hex_format_suf);
427 /* Converts a number to hexadecimal and stores it in a static
428 string. Returns a pointer to this string. */
430 local_hex_string (num)
435 sprintf (res, current_language->la_hex_format, num);
439 /* Converts a number to custom hexadecimal and stores it in a static
440 string. Returns a pointer to this string. */
442 local_hex_string_custom(num,pre)
448 sprintf (res, local_hex_format_custom(pre), num);
452 /* Returns the appropriate printf format for octal
455 local_octal_format_custom(pre)
458 static char form[50];
460 strcpy (form, current_language->la_octal_format_pre);
462 strcat (form, current_language->la_octal_format_suf);
466 /* This page contains functions that are used in type/range checking.
467 They all return zero if the type/range check fails.
469 It is hoped that these will make extending GDB to parse different
470 languages a little easier. These are primarily used in eval.c when
471 evaluating expressions and making sure that their types are correct.
472 Instead of having a mess of conjucted/disjuncted expressions in an "if",
473 the ideas of type can be wrapped up in the following functions.
475 Note that some of them are not currently dependent upon which language
476 is currently being parsed. For example, floats are the same in
477 C and Modula-2 (ie. the only floating point type has TYPE_CODE of
478 TYPE_CODE_FLT), while booleans are different. */
480 /* Returns non-zero if its argument is a simple type. This is the same for
481 both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur,
482 and thus will never cause the failure of the test. */
487 switch (TYPE_CODE (type)) {
492 case TYPE_CODE_RANGE:
501 /* Returns non-zero if its argument is of an ordered type. */
506 switch (TYPE_CODE (type)) {
511 case TYPE_CODE_RANGE:
519 /* Returns non-zero if the two types are the same */
521 same_type (arg1, arg2)
522 struct type *arg1, *arg2;
524 if (structured_type(arg1) ? !structured_type(arg2) : structured_type(arg2))
525 /* One is structured and one isn't */
527 else if (structured_type(arg1) && structured_type(arg2))
529 else if (numeric_type(arg1) && numeric_type(arg2))
530 return (TYPE_CODE(arg2) == TYPE_CODE(arg1)) &&
531 (TYPE_UNSIGNED(arg1) == TYPE_UNSIGNED(arg2))
537 /* Returns non-zero if the type is integral */
542 switch(current_language->la_language)
546 return (TYPE_CODE(type) != TYPE_CODE_INT) &&
547 (TYPE_CODE(type) != TYPE_CODE_ENUM) ? 0 : 1;
549 return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
551 error ("Language not supported.");
555 /* Returns non-zero if the value is numeric */
560 switch (TYPE_CODE (type)) {
570 /* Returns non-zero if the value is a character type */
572 character_type (type)
575 switch(current_language->la_language)
578 return TYPE_CODE(type) != TYPE_CODE_CHAR ? 0 : 1;
582 return (TYPE_CODE(type) == TYPE_CODE_INT) &&
583 TYPE_LENGTH(type) == sizeof(char)
588 /* Returns non-zero if the value is a boolean type */
593 switch(current_language->la_language)
596 return TYPE_CODE(type) != TYPE_CODE_BOOL ? 0 : 1;
600 return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
604 /* Returns non-zero if the value is a floating-point type */
609 return TYPE_CODE(type) == TYPE_CODE_FLT;
612 /* Returns non-zero if the value is a pointer type */
617 return TYPE_CODE(type) == TYPE_CODE_PTR ||
618 TYPE_CODE(type) == TYPE_CODE_REF;
621 /* Returns non-zero if the value is a structured type */
623 structured_type(type)
626 switch(current_language->la_language)
630 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
631 (TYPE_CODE(type) == TYPE_CODE_UNION) ||
632 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
634 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
635 (TYPE_CODE(type) == TYPE_CODE_SET) ||
636 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
640 /* This page contains functions that return info about
641 (struct value) values used in GDB. */
643 /* Returns non-zero if the value VAL represents a true value. */
652 switch (current_language->la_language) {
656 return !value_zerop (val);
659 type = VALUE_TYPE(val);
660 if (TYPE_CODE (type) != TYPE_CODE_BOOL)
661 return 0; /* Not a BOOLEAN at all */
662 /* Search the fields for one that matches the current value. */
663 len = TYPE_NFIELDS (type);
664 v = value_as_long (val);
665 for (i = 0; i < len; i++)
668 if (v == TYPE_FIELD_BITPOS (type, i))
672 return 0; /* Not a valid BOOLEAN value */
673 if (!strcmp ("TRUE", TYPE_FIELD_NAME(VALUE_TYPE(val), i)))
674 return 1; /* BOOLEAN with value TRUE */
676 return 0; /* BOOLEAN with value FALSE */
680 error ("Language not supported.");
684 /* Returns non-zero if the operator OP is defined on
685 the values ARG1 and ARG2. */
687 binop_type_check(arg1,arg2,op)
691 struct type *t1, *t2;
693 /* If we're not checking types, always return success. */
698 if (arg2!=(value)NULL)
707 if ((numeric_type(t1) && pointer_type(t2)) ||
708 (pointer_type(t1) && numeric_type(t2)))
710 printf("warning: combining pointer and integer.\n");
716 if (!numeric_type(t1) || !numeric_type(t2))
717 type_op_error ("Arguments to %s must be numbers.",op);
718 else if (!same_type(t1,t2))
719 type_op_error ("Arguments to %s must be of the same type.",op);
724 if (!boolean_type(t1) || !boolean_type(t2))
725 type_op_error ("Arguments to %s must be of boolean type.",op);
729 if ((pointer_type(t1) && !(pointer_type(t2) || integral_type(t2))) ||
730 (pointer_type(t2) && !(pointer_type(t1) || integral_type(t1))))
731 type_op_error ("A pointer can only be compared to an integer or pointer.",op);
732 else if ((pointer_type(t1) && integral_type(t2)) ||
733 (integral_type(t1) && pointer_type(t2)))
735 printf("warning: combining integer and pointer.\n");
738 else if (!simple_type(t1) || !simple_type(t2))
739 type_op_error ("Arguments to %s must be of simple type.",op);
740 else if (!same_type(t1,t2))
741 type_op_error ("Arguments to %s must be of the same type.",op);
745 if (!integral_type(t1) || !integral_type(t2))
746 type_op_error ("Arguments to %s must be of integral type.",op);
753 if (!ordered_type(t1) || !ordered_type(t2))
754 type_op_error ("Arguments to %s must be of ordered type.",op);
755 else if (!same_type(t1,t2))
756 type_op_error ("Arguments to %s must be of the same type.",op);
760 if (pointer_type(t1) && !integral_type(t2))
761 type_op_error ("A pointer can only be assigned an integer.",op);
762 else if (pointer_type(t1) && integral_type(t2))
764 printf("warning: combining integer and pointer.");
767 else if (!simple_type(t1) || !simple_type(t2))
768 type_op_error ("Arguments to %s must be of simple type.",op);
769 else if (!same_type(t1,t2))
770 type_op_error ("Arguments to %s must be of the same type.",op);
773 /* Unary checks -- arg2 is null */
776 if (!boolean_type(t1))
777 type_op_error ("Argument to %s must be of boolean type.",op);
782 if (!numeric_type(t1))
783 type_op_error ("Argument to %s must be of numeric type.",op);
787 if (integral_type(t1))
789 printf("warning: combining pointer and integer.\n");
792 else if (!pointer_type(t1))
793 type_op_error ("Argument to %s must be a pointer.",op);
796 case UNOP_PREINCREMENT:
797 case UNOP_POSTINCREMENT:
798 case UNOP_PREDECREMENT:
799 case UNOP_POSTDECREMENT:
800 if (!ordered_type(t1))
801 type_op_error ("Argument to %s must be of an ordered type.",op);
805 /* Ok. The following operators have different meanings in
806 different languages. */
807 switch(current_language->la_language)
815 if (!numeric_type(t1) || !numeric_type(t2))
816 type_op_error ("Arguments to %s must be numbers.",op);
827 if (!float_type(t1) || !float_type(t2))
828 type_op_error ("Arguments to %s must be floating point numbers.",op);
831 if (!integral_type(t1) || !integral_type(t2))
832 type_op_error ("Arguments to %s must be of integral type.",op);
840 /* This page contains functions for the printing out of
841 error messages that occur during type- and range-
844 /* Prints the format string FMT with the operator as a string
845 corresponding to the opcode OP. If FATAL is non-zero, then
846 this is an error and error () is called. Otherwise, it is
847 a warning and printf() is called. */
849 op_error (fmt,op,fatal)
855 error (fmt,op_string(op));
859 printf(fmt,op_string(op));
864 /* These are called when a language fails a type- or range-check.
865 The first argument should be a printf()-style format string, and
866 the rest of the arguments should be its arguments. If
867 [type|range]_check is [type|range]_check_on, then return_to_top_level()
868 is called in the style of error (). Otherwise, the message is prefixed
869 by "warning: " and we do not return to the top level. */
871 type_error (va_alist)
877 if (type_check==type_check_warn)
878 fprintf(stderr,"warning: ");
880 target_terminal_ours();
883 string = va_arg (args, char *);
884 vfprintf (stderr, string, args);
885 fprintf (stderr, "\n");
887 if (type_check==type_check_on)
888 return_to_top_level();
892 range_error (va_alist)
898 if (range_check==range_check_warn)
899 fprintf(stderr,"warning: ");
901 target_terminal_ours();
904 string = va_arg (args, char *);
905 vfprintf (stderr, string, args);
906 fprintf (stderr, "\n");
908 if (range_check==range_check_on)
909 return_to_top_level();
913 /* This page contains miscellaneous functions */
915 /* Return the language as a string */
922 for (i = 0; i < languages_size; i++) {
923 if (languages[i]->la_language == lang) {
924 return languages[i]->la_name;
930 struct cmd_list_element *setchecklist = NULL;
931 struct cmd_list_element *showchecklist = NULL;
934 set_check (ignore, from_tty)
939 "\"set check\" must be followed by the name of a check subcommand.\n");
940 help_list(setchecklist, "set check ", -1, stdout);
944 show_check (arg, from_tty)
948 cmd_show_list(showchecklist, from_tty, "");
951 /* Add a language to the set of known languages. */
955 struct language_defn *lang;
957 if (lang->la_magic != LANG_MAGIC)
959 fprintf(stderr, "Magic number of %s language struct wrong\n",
966 languages_allocsize = DEFAULT_ALLOCSIZE;
967 languages = (struct language_defn **) xmalloc
968 (languages_allocsize * sizeof (*languages));
970 if (languages_size >= languages_allocsize)
972 languages_allocsize *= 2;
973 languages = (struct language_defn **) xrealloc (languages,
974 languages_allocsize * sizeof (*languages));
976 languages[languages_size++] = lang;
979 /* Define the language that is no language. */
990 error ("Attempted to parse an expression with unknown language");
993 static struct type ** const (unknown_builtin_types[]) = { 0 };
994 static const struct op_print unk_op_print_tab[] = { 0 };
996 const struct language_defn unknown_language_defn = {
999 &unknown_builtin_types[0],
1004 &builtin_type_error, /* longest signed integral type */
1005 &builtin_type_error, /* longest unsigned integral type */
1006 &builtin_type_error, /* longest floating point type */
1007 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1008 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1009 unk_op_print_tab, /* expression operators for printing */
1013 /* These two structs define fake entries for the "local" and "auto" options. */
1014 const struct language_defn auto_language_defn = {
1017 &unknown_builtin_types[0],
1022 &builtin_type_error, /* longest signed integral type */
1023 &builtin_type_error, /* longest unsigned integral type */
1024 &builtin_type_error, /* longest floating point type */
1025 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1026 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1027 unk_op_print_tab, /* expression operators for printing */
1031 const struct language_defn local_language_defn = {
1034 &unknown_builtin_types[0],
1039 &builtin_type_error, /* longest signed integral type */
1040 &builtin_type_error, /* longest unsigned integral type */
1041 &builtin_type_error, /* longest floating point type */
1042 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1043 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1044 unk_op_print_tab, /* expression operators for printing */
1048 /* Initialize the language routines */
1051 _initialize_language()
1053 struct cmd_list_element *set, *show;
1055 /* GDB commands for language specific stuff */
1057 set = add_set_cmd ("language", class_support, var_string_noescape,
1059 "Set the current source language.",
1061 show = add_show_from_set (set, &showlist);
1062 set->function = set_language_command;
1063 show->function = show_language_command;
1065 add_prefix_cmd ("check", no_class, set_check,
1066 "Set the status of the type/range checker",
1067 &setchecklist, "set check ", 0, &setlist);
1068 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1069 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1071 add_prefix_cmd ("check", no_class, show_check,
1072 "Show the status of the type/range checker",
1073 &showchecklist, "show check ", 0, &showlist);
1074 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1075 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1077 set = add_set_cmd ("type", class_support, var_string_noescape,
1079 "Set type checking. (on/warn/off/auto)",
1081 show = add_show_from_set (set, &showchecklist);
1082 set->function = set_type_command;
1083 show->function = show_type_command;
1085 set = add_set_cmd ("range", class_support, var_string_noescape,
1087 "Set range checking. (on/warn/off/auto)",
1089 show = add_show_from_set (set, &showchecklist);
1090 set->function = set_range_command;
1091 show->function = show_range_command;
1093 add_language (&unknown_language_defn);
1094 add_language (&local_language_defn);
1095 add_language (&auto_language_defn);
1097 language = savestring ("auto",strlen("auto"));
1098 range = savestring ("auto",strlen("auto"));
1099 type = savestring ("auto",strlen("auto"));
1101 /* Have the above take effect */
1103 set_language_command (language, 0);
1104 set_type_command (NULL, 0);
1105 set_range_command (NULL, 0);