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