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