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 /* Forward function declarations */
45 static void set_type_range ();
47 /* Forward declaration */
48 extern struct language_defn unknown_language_defn;
50 /* The current (default at startup) state of type and range checking.
51 (If the modes are set to "auto", though, these are changed based
52 on the default language at startup, and then again based on the
53 language of the first source file. */
55 enum range_mode range_mode = range_mode_auto;
56 enum range_check range_check = range_check_off;
57 enum type_mode type_mode = type_mode_auto;
58 enum type_check type_check = type_check_off;
60 /* The current language and language_mode (see language.h) */
62 struct language_defn *current_language = &unknown_language_defn;
63 enum language_mode language_mode = language_mode_auto;
65 /* The list of supported languages. The list itself is malloc'd. */
67 static struct language_defn **languages;
68 static unsigned languages_size;
69 static unsigned languages_allocsize;
70 #define DEFAULT_ALLOCSIZE 3
72 /* The "set language/type/range" commands all put stuff in these
73 buffers. This is to make them work as set/show commands. The
74 user's string is copied here, then the set_* commands look at
75 them and update them to something that looks nice when it is
78 static char *language;
82 /* Warning issued when current_language and the language of the current
83 frame do not match. */
84 char lang_frame_mismatch_warn[] =
85 "Warning: the current language does not match this frame.";
91 /* This page contains the functions corresponding to GDB commands
94 /* Show command. Display a warning if the language set
95 does not match the frame. */
97 show_language_command (ignore, from_tty)
101 enum language flang; /* The language of the current frame */
103 flang = get_frame_language();
104 if (flang != language_unknown &&
105 language_mode == language_mode_manual &&
106 current_language->la_language != flang)
107 printf_filtered("%s\n",lang_frame_mismatch_warn);
110 /* Set command. Change the current working language. */
112 set_language_command (ignore, from_tty)
120 /* FIXME -- do this from the list, with HELP. */
121 if (!language || !language[0]) {
122 printf("The currently understood settings are:\n\n\
123 local or auto Automatic setting based on source file\n\
124 c Use the C language\n\
125 c++ Use the C++ language\n\
126 modula-2 Use the Modula-2 language\n");
127 /* Restore the silly string. */
128 set_language(current_language->la_language);
132 /* Search the list of languages for a match. */
133 for (i = 0; i < languages_size; i++) {
134 if (!strcmp (languages[i]->la_name, language)) {
135 /* Found it! Go into manual mode, and use this language. */
136 if (languages[i]->la_language == language_auto) {
137 /* Enter auto mode. Set to the current frame's language, if known. */
138 language_mode = language_mode_auto;
139 flang = get_frame_language();
140 if (flang!=language_unknown)
144 /* Enter manual mode. Set the specified language. */
145 language_mode = language_mode_manual;
146 current_language = languages[i];
154 /* Reset the language (esp. the global string "language") to the
156 err_lang=savestring(language,strlen(language));
157 make_cleanup (free, err_lang); /* Free it after error */
158 set_language(current_language->la_language);
159 error ("Unknown language `%s'.",err_lang);
162 /* Show command. Display a warning if the type setting does
163 not match the current language. */
165 show_type_command(ignore, from_tty)
169 if (type_check != current_language->la_type_check)
171 "Warning: the current type check setting does not match the language.\n");
174 /* Set command. Change the setting for type checking. */
176 set_type_command(ignore, from_tty)
180 if (!strcmp(type,"on"))
182 type_check = type_check_on;
183 type_mode = type_mode_manual;
185 else if (!strcmp(type,"warn"))
187 type_check = type_check_warn;
188 type_mode = type_mode_manual;
190 else if (!strcmp(type,"off"))
192 type_check = type_check_off;
193 type_mode = type_mode_manual;
195 else if (!strcmp(type,"auto"))
197 type_mode = type_mode_auto;
199 /* Avoid hitting the set_type_str call below. We
200 did it in set_type_range. */
204 show_type_command((char *)NULL, from_tty);
207 /* Show command. Display a warning if the range setting does
208 not match the current language. */
210 show_range_command(ignore, from_tty)
215 if (range_check != current_language->la_range_check)
217 "Warning: the current range check setting does not match the language.\n");
220 /* Set command. Change the setting for range checking. */
222 set_range_command(ignore, from_tty)
226 if (!strcmp(range,"on"))
228 range_check = range_check_on;
229 range_mode = range_mode_manual;
231 else if (!strcmp(range,"warn"))
233 range_check = range_check_warn;
234 range_mode = range_mode_manual;
236 else if (!strcmp(range,"off"))
238 range_check = range_check_off;
239 range_mode = range_mode_manual;
241 else if (!strcmp(range,"auto"))
243 range_mode = range_mode_auto;
245 /* Avoid hitting the set_range_str call below. We
246 did it in set_type_range. */
250 show_range_command((char *)0, from_tty);
253 /* Set the status of range and type checking based on
254 the current modes and the current language.
255 If SHOW is non-zero, then print out the current language,
256 type and range checking status. */
261 if (range_mode == range_mode_auto)
262 range_check = current_language->la_range_check;
264 if (type_mode == type_mode_auto)
265 type_check = current_language->la_type_check;
271 /* Set current language to (enum language) LANG. */
279 for (i = 0; i < languages_size; i++) {
280 if (languages[i]->la_language == lang) {
281 current_language = languages[i];
289 /* This page contains functions that update the global vars
290 language, type and range. */
297 if (language_mode == language_mode_auto)
298 prefix = "auto; currently ";
300 language = concat(prefix, current_language->la_name, "");
306 char *tmp, *prefix = "";
309 if (type_mode==type_mode_auto)
310 prefix = "auto; currently ";
320 case type_check_warn:
324 error ("Unrecognized type check setting.");
327 type = concat(prefix,tmp,"");
333 char *tmp, *pref = "";
336 if (range_mode==range_mode_auto)
337 pref = "auto; currently ";
344 case range_check_off:
347 case range_check_warn:
351 error ("Unrecognized range check setting.");
354 range = concat(pref,tmp,"");
358 /* Print out the current language settings: language, range and
363 printf("Current Language: %s\n",language);
364 show_language_command((char *)0, 1);
365 printf("Type checking: %s\n",type);
366 show_type_command((char *)0, 1);
367 printf("Range checking: %s\n",range);
368 show_range_command((char *)0, 1);
371 /* Return the result of a binary operation. */
373 binop_result_type(v1,v2)
378 l1 = TYPE_LENGTH(VALUE_TYPE(v1));
379 l2 = TYPE_LENGTH(VALUE_TYPE(v2));
381 switch(current_language->la_language)
385 if (TYPE_CODE(VALUE_TYPE(v1))==TYPE_CODE_FLT)
386 return TYPE_CODE(VALUE_TYPE(v2)) == TYPE_CODE_FLT && l2 > l1 ?
387 VALUE_TYPE(v2) : VALUE_TYPE(v1);
388 else if (TYPE_CODE(VALUE_TYPE(v2))==TYPE_CODE_FLT)
389 return TYPE_CODE(VALUE_TYPE(v1)) == TYPE_CODE_FLT && l1 > l2 ?
390 VALUE_TYPE(v1) : VALUE_TYPE(v2);
391 else if (TYPE_UNSIGNED(VALUE_TYPE(v1)) && l1 > l2)
392 return VALUE_TYPE(v1);
393 else if (TYPE_UNSIGNED(VALUE_TYPE(v2)) && l2 > l1)
394 return VALUE_TYPE(v2);
395 else /* Both are signed. Result is the longer type */
396 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
399 /* If we are doing type-checking, l1 should equal l2, so this is
401 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
405 return (struct type *)0; /* For lint */
408 /* This page contains functions that return format strings for
409 printf for printing out numbers in different formats */
411 /* Returns the appropriate printf format for hexadecimal
414 local_hex_format_custom(pre)
417 static char form[50];
419 strcpy (form, current_language->la_hex_format_pre);
421 strcat (form, current_language->la_hex_format_suf);
425 /* Converts a number to hexadecimal and stores it in a static
426 string. Returns a pointer to this string. */
428 local_hex_string (num)
433 sprintf (res, current_language->la_hex_format, num);
437 /* Converts a number to custom hexadecimal and stores it in a static
438 string. Returns a pointer to this string. */
440 local_hex_string_custom(num,pre)
446 sprintf (res, local_hex_format_custom(pre), num);
450 /* Returns the appropriate printf format for octal
453 local_octal_format_custom(pre)
456 static char form[50];
458 strcpy (form, current_language->la_octal_format_pre);
460 strcat (form, current_language->la_octal_format_suf);
464 /* This page contains functions that are used in type/range checking.
465 They all return zero if the type/range check fails.
467 It is hoped that these will make extending GDB to parse different
468 languages a little easier. These are primarily used in eval.c when
469 evaluating expressions and making sure that their types are correct.
470 Instead of having a mess of conjucted/disjuncted expressions in an "if",
471 the ideas of type can be wrapped up in the following functions.
473 Note that some of them are not currently dependent upon which language
474 is currently being parsed. For example, floats are the same in
475 C and Modula-2 (ie. the only floating point type has TYPE_CODE of
476 TYPE_CODE_FLT), while booleans are different. */
478 /* Returns non-zero if its argument is a simple type. This is the same for
479 both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur,
480 and thus will never cause the failure of the test. */
485 switch (TYPE_CODE (type)) {
490 case TYPE_CODE_RANGE:
499 /* Returns non-zero if its argument is of an ordered type. */
504 switch (TYPE_CODE (type)) {
509 case TYPE_CODE_RANGE:
517 /* Returns non-zero if the two types are the same */
519 same_type (arg1, arg2)
520 struct type *arg1, *arg2;
522 if (structured_type(arg1) ? !structured_type(arg2) : structured_type(arg2))
523 /* One is structured and one isn't */
525 else if (structured_type(arg1) && structured_type(arg2))
527 else if (numeric_type(arg1) && numeric_type(arg2))
528 return (TYPE_CODE(arg2) == TYPE_CODE(arg1)) &&
529 (TYPE_UNSIGNED(arg1) == TYPE_UNSIGNED(arg2))
535 /* Returns non-zero if the type is integral */
540 switch(current_language->la_language)
544 return (TYPE_CODE(type) != TYPE_CODE_INT) &&
545 (TYPE_CODE(type) != TYPE_CODE_ENUM) ? 0 : 1;
547 return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
549 error ("Language not supported.");
553 /* Returns non-zero if the value is numeric */
558 switch (TYPE_CODE (type)) {
568 /* Returns non-zero if the value is a character type */
570 character_type (type)
573 switch(current_language->la_language)
576 return TYPE_CODE(type) != TYPE_CODE_CHAR ? 0 : 1;
580 return (TYPE_CODE(type) == TYPE_CODE_INT) &&
581 TYPE_LENGTH(type) == sizeof(char)
586 /* Returns non-zero if the value is a boolean type */
591 switch(current_language->la_language)
594 return TYPE_CODE(type) != TYPE_CODE_BOOL ? 0 : 1;
598 return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
602 /* Returns non-zero if the value is a floating-point type */
607 return TYPE_CODE(type) == TYPE_CODE_FLT;
610 /* Returns non-zero if the value is a pointer type */
615 return TYPE_CODE(type) == TYPE_CODE_PTR ||
616 TYPE_CODE(type) == TYPE_CODE_REF;
619 /* Returns non-zero if the value is a structured type */
621 structured_type(type)
624 switch(current_language->la_language)
628 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
629 (TYPE_CODE(type) == TYPE_CODE_UNION) ||
630 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
632 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
633 (TYPE_CODE(type) == TYPE_CODE_SET) ||
634 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
638 /* This page contains functions that return info about
639 (struct value) values used in GDB. */
641 /* Returns non-zero if the value VAL represents a true value. */
650 switch (current_language->la_language) {
654 return !value_zerop (val);
657 type = VALUE_TYPE(val);
658 if (TYPE_CODE (type) != TYPE_CODE_BOOL)
659 return 0; /* Not a BOOLEAN at all */
660 /* Search the fields for one that matches the current value. */
661 len = TYPE_NFIELDS (type);
662 v = value_as_long (val);
663 for (i = 0; i < len; i++)
666 if (v == TYPE_FIELD_BITPOS (type, i))
670 return 0; /* Not a valid BOOLEAN value */
671 if (!strcmp ("TRUE", TYPE_FIELD_NAME(VALUE_TYPE(val), i)))
672 return 1; /* BOOLEAN with value TRUE */
674 return 0; /* BOOLEAN with value FALSE */
678 error ("Language not supported.");
682 /* Returns non-zero if the operator OP is defined on
683 the values ARG1 and ARG2. */
685 binop_type_check(arg1,arg2,op)
689 struct type *t1, *t2;
691 /* If we're not checking types, always return success. */
696 if (arg2!=(value)NULL)
705 if ((numeric_type(t1) && pointer_type(t2)) ||
706 (pointer_type(t1) && numeric_type(t2)))
708 printf("warning: combining pointer and integer.\n");
714 if (!numeric_type(t1) || !numeric_type(t2))
715 type_op_error ("Arguments to %s must be numbers.",op);
716 else if (!same_type(t1,t2))
717 type_op_error ("Arguments to %s must be of the same type.",op);
722 if (!boolean_type(t1) || !boolean_type(t2))
723 type_op_error ("Arguments to %s must be of boolean type.",op);
727 if ((pointer_type(t1) && !(pointer_type(t2) || integral_type(t2))) ||
728 (pointer_type(t2) && !(pointer_type(t1) || integral_type(t1))))
729 type_op_error ("A pointer can only be compared to an integer or pointer.",op);
730 else if ((pointer_type(t1) && integral_type(t2)) ||
731 (integral_type(t1) && pointer_type(t2)))
733 printf("warning: combining integer and pointer.\n");
736 else if (!simple_type(t1) || !simple_type(t2))
737 type_op_error ("Arguments to %s must be of simple type.",op);
738 else if (!same_type(t1,t2))
739 type_op_error ("Arguments to %s must be of the same type.",op);
743 if (!integral_type(t1) || !integral_type(t2))
744 type_op_error ("Arguments to %s must be of integral type.",op);
751 if (!ordered_type(t1) || !ordered_type(t2))
752 type_op_error ("Arguments to %s must be of ordered type.",op);
753 else if (!same_type(t1,t2))
754 type_op_error ("Arguments to %s must be of the same type.",op);
758 if (pointer_type(t1) && !integral_type(t2))
759 type_op_error ("A pointer can only be assigned an integer.",op);
760 else if (pointer_type(t1) && integral_type(t2))
762 printf("warning: combining integer and pointer.");
765 else if (!simple_type(t1) || !simple_type(t2))
766 type_op_error ("Arguments to %s must be of simple type.",op);
767 else if (!same_type(t1,t2))
768 type_op_error ("Arguments to %s must be of the same type.",op);
771 /* Unary checks -- arg2 is null */
774 if (!boolean_type(t1))
775 type_op_error ("Argument to %s must be of boolean type.",op);
780 if (!numeric_type(t1))
781 type_op_error ("Argument to %s must be of numeric type.",op);
785 if (integral_type(t1))
787 printf("warning: combining pointer and integer.\n");
790 else if (!pointer_type(t1))
791 type_op_error ("Argument to %s must be a pointer.",op);
794 case UNOP_PREINCREMENT:
795 case UNOP_POSTINCREMENT:
796 case UNOP_PREDECREMENT:
797 case UNOP_POSTDECREMENT:
798 if (!ordered_type(t1))
799 type_op_error ("Argument to %s must be of an ordered type.",op);
803 /* Ok. The following operators have different meanings in
804 different languages. */
805 switch(current_language->la_language)
813 if (!numeric_type(t1) || !numeric_type(t2))
814 type_op_error ("Arguments to %s must be numbers.",op);
825 if (!float_type(t1) || !float_type(t2))
826 type_op_error ("Arguments to %s must be floating point numbers.",op);
829 if (!integral_type(t1) || !integral_type(t2))
830 type_op_error ("Arguments to %s must be of integral type.",op);
838 /* This page contains functions for the printing out of
839 error messages that occur during type- and range-
842 /* Prints the format string FMT with the operator as a string
843 corresponding to the opcode OP. If FATAL is non-zero, then
844 this is an error and error () is called. Otherwise, it is
845 a warning and printf() is called. */
847 op_error (fmt,op,fatal)
853 error (fmt,op_string(op));
857 printf(fmt,op_string(op));
862 /* These are called when a language fails a type- or range-check.
863 The first argument should be a printf()-style format string, and
864 the rest of the arguments should be its arguments. If
865 [type|range]_check is [type|range]_check_on, then return_to_top_level()
866 is called in the style of error (). Otherwise, the message is prefixed
867 by "warning: " and we do not return to the top level. */
869 type_error (va_alist)
875 if (type_check==type_check_warn)
876 fprintf(stderr,"warning: ");
878 target_terminal_ours();
881 string = va_arg (args, char *);
882 vfprintf (stderr, string, args);
883 fprintf (stderr, "\n");
885 if (type_check==type_check_on)
886 return_to_top_level();
890 range_error (va_alist)
896 if (range_check==range_check_warn)
897 fprintf(stderr,"warning: ");
899 target_terminal_ours();
902 string = va_arg (args, char *);
903 vfprintf (stderr, string, args);
904 fprintf (stderr, "\n");
906 if (range_check==range_check_on)
907 return_to_top_level();
911 /* This page contains miscellaneous functions */
913 /* Return the language as a string */
920 for (i = 0; i < languages_size; i++) {
921 if (languages[i]->la_language == lang) {
922 return languages[i]->la_name;
928 struct cmd_list_element *setchecklist = NULL;
929 struct cmd_list_element *showchecklist = NULL;
932 set_check (ignore, from_tty)
937 "\"set check\" must be followed by the name of a check subcommand.\n");
938 help_list(setchecklist, "set check ", -1, stdout);
942 show_check (arg, from_tty)
946 cmd_show_list(showchecklist, from_tty, "");
949 /* Add a language to the set of known languages. */
953 struct language_defn *lang;
955 if (lang->la_magic != LANG_MAGIC)
957 fprintf(stderr, "Magic number of %s language struct wrong\n",
964 languages_allocsize = DEFAULT_ALLOCSIZE;
965 languages = (struct language_defn **) xmalloc
966 (languages_allocsize * sizeof (*languages));
968 if (languages_size >= languages_allocsize)
970 languages_allocsize *= 2;
971 languages = (struct language_defn **) xrealloc (languages,
972 languages_allocsize * sizeof (*languages));
974 languages[languages_size++] = lang;
977 if (targetlist == NULL)
978 add_prefix_cmd ("target", class_run, target_command,
979 "Connect to a target machine or process.\n\
980 The first argument is the type or protocol of the target machine.\n\
981 Remaining arguments are interpreted by the target protocol. For more\n\
982 information on the arguments for a particular protocol, type\n\
983 `help target ' followed by the protocol name.",
984 &targetlist, "target ", 0, &cmdlist);
985 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
989 /* Define the language that is no language. */
1000 error ("Attempted to parse an expression with unknown language");
1003 static struct type ** const (unknown_builtin_types[]) = { 0 };
1004 static const struct op_print unk_op_print_tab[] = { 0 };
1006 const struct language_defn unknown_language_defn = {
1009 &unknown_builtin_types[0],
1014 &builtin_type_error, /* longest signed integral type */
1015 &builtin_type_error, /* longest unsigned integral type */
1016 &builtin_type_error, /* longest floating point type */
1017 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1018 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1019 unk_op_print_tab, /* expression operators for printing */
1023 /* These two structs define fake entries for the "local" and "auto" options. */
1024 const struct language_defn auto_language_defn = {
1027 &unknown_builtin_types[0],
1032 &builtin_type_error, /* longest signed integral type */
1033 &builtin_type_error, /* longest unsigned integral type */
1034 &builtin_type_error, /* longest floating point type */
1035 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1036 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1037 unk_op_print_tab, /* expression operators for printing */
1041 const struct language_defn local_language_defn = {
1044 &unknown_builtin_types[0],
1049 &builtin_type_error, /* longest signed integral type */
1050 &builtin_type_error, /* longest unsigned integral type */
1051 &builtin_type_error, /* longest floating point type */
1052 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1053 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1054 unk_op_print_tab, /* expression operators for printing */
1058 /* Initialize the language routines */
1061 _initialize_language()
1063 struct cmd_list_element *set, *show;
1065 /* GDB commands for language specific stuff */
1067 set = add_set_cmd ("language", class_support, var_string_noescape,
1069 "Set the current source language.",
1071 show = add_show_from_set (set, &showlist);
1072 set->function = set_language_command;
1073 show->function = show_language_command;
1075 add_prefix_cmd ("check", no_class, set_check,
1076 "Set the status of the type/range checker",
1077 &setchecklist, "set check ", 0, &setlist);
1078 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1079 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1081 add_prefix_cmd ("check", no_class, show_check,
1082 "Show the status of the type/range checker",
1083 &showchecklist, "show check ", 0, &showlist);
1084 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1085 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1087 set = add_set_cmd ("type", class_support, var_string_noescape,
1089 "Set type checking. (on/warn/off/auto)",
1091 show = add_show_from_set (set, &showchecklist);
1092 set->function = set_type_command;
1093 show->function = show_type_command;
1095 set = add_set_cmd ("range", class_support, var_string_noescape,
1097 "Set range checking. (on/warn/off/auto)",
1099 show = add_show_from_set (set, &showchecklist);
1100 set->function = set_range_command;
1101 show->function = show_range_command;
1103 add_language (&unknown_language_defn);
1104 add_language (&local_language_defn);
1105 add_language (&auto_language_defn);
1107 language = savestring ("auto",strlen("auto"));
1108 range = savestring ("auto",strlen("auto"));
1109 type = savestring ("auto",strlen("auto"));
1111 /* Have the above take effect */
1113 set_language_command (language, 0);
1114 set_type_command (NULL, 0);
1115 set_range_command (NULL, 0);