]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/language.c
Only enable compilation of debugging functions when IBM6000 is defined
[thirdparty/binutils-gdb.git] / gdb / language.c
CommitLineData
c8023e66 1/* Multiple source language support for GDB.
7ed0f002 2 Copyright 1991, 1992 Free Software Foundation, Inc.
c8023e66
JG
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., 675 Mass Ave, Cambridge, MA 02139, 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
d747e0af 30#include "defs.h"
c8023e66
JG
31#include <string.h>
32#include <varargs.h>
33
c8023e66 34#include "symtab.h"
1ab3bf1b 35#include "gdbtypes.h"
c8023e66
JG
36#include "value.h"
37#include "gdbcmd.h"
38#include "frame.h"
c8023e66 39#include "expression.h"
7ed0f002 40#include "language.h"
c8023e66
JG
41#include "target.h"
42#include "parser-defs.h"
43
7ed0f002
JG
44static void
45show_language_command PARAMS ((char *, int));
46
47static void
48set_language_command PARAMS ((char *, int));
49
50static void
51show_type_command PARAMS ((char *, int));
c4668207 52
7ed0f002
JG
53static void
54set_type_command PARAMS ((char *, int));
55
56static void
57show_range_command PARAMS ((char *, int));
58
59static void
60set_range_command PARAMS ((char *, int));
61
62static void
63set_range_str PARAMS ((void));
64
65static void
66set_type_str PARAMS ((void));
67
68static void
69set_lang_str PARAMS ((void));
70
71static void
72unk_lang_error PARAMS ((char *));
73
74static int
75unk_lang_parser PARAMS ((void));
76
77static void
78show_check PARAMS ((char *, int));
79
80static void
81set_check PARAMS ((char *, int));
82
83static void
84set_type_range PARAMS ((void));
c8023e66
JG
85
86/* Forward declaration */
0c6efbcc 87extern const struct language_defn unknown_language_defn;
7ed0f002 88
c8023e66
JG
89/* The current (default at startup) state of type and range checking.
90 (If the modes are set to "auto", though, these are changed based
91 on the default language at startup, and then again based on the
92 language of the first source file. */
93
94enum range_mode range_mode = range_mode_auto;
95enum range_check range_check = range_check_off;
96enum type_mode type_mode = type_mode_auto;
97enum type_check type_check = type_check_off;
98
99/* The current language and language_mode (see language.h) */
100
0c6efbcc 101const struct language_defn *current_language = &unknown_language_defn;
c8023e66
JG
102enum language_mode language_mode = language_mode_auto;
103
104/* The list of supported languages. The list itself is malloc'd. */
105
7ed0f002 106static const struct language_defn **languages;
c8023e66
JG
107static unsigned languages_size;
108static unsigned languages_allocsize;
109#define DEFAULT_ALLOCSIZE 3
110
111/* The "set language/type/range" commands all put stuff in these
112 buffers. This is to make them work as set/show commands. The
113 user's string is copied here, then the set_* commands look at
114 them and update them to something that looks nice when it is
115 printed out. */
116
117static char *language;
118static char *type;
119static char *range;
120
121/* Warning issued when current_language and the language of the current
122 frame do not match. */
123char lang_frame_mismatch_warn[] =
124 "Warning: the current language does not match this frame.";
125
c8023e66
JG
126\f
127/* This page contains the functions corresponding to GDB commands
128 and their helpers. */
129
130/* Show command. Display a warning if the language set
131 does not match the frame. */
7ed0f002 132static void
d8ce1326
JG
133show_language_command (ignore, from_tty)
134 char *ignore;
c8023e66
JG
135 int from_tty;
136{
137 enum language flang; /* The language of the current frame */
138
139 flang = get_frame_language();
140 if (flang != language_unknown &&
141 language_mode == language_mode_manual &&
142 current_language->la_language != flang)
143 printf_filtered("%s\n",lang_frame_mismatch_warn);
144}
145
146/* Set command. Change the current working language. */
7ed0f002 147static void
d8ce1326
JG
148set_language_command (ignore, from_tty)
149 char *ignore;
c8023e66
JG
150 int from_tty;
151{
152 int i;
153 enum language flang;
5f3d478e 154 char *err_lang;
c8023e66
JG
155
156 /* FIXME -- do this from the list, with HELP. */
157 if (!language || !language[0]) {
158 printf("The currently understood settings are:\n\n\
159local or auto Automatic setting based on source file\n\
0b798409 160c Use the C language\n\
545af6ce 161c++ Use the C++ language\n\
0b798409
JG
162modula-2 Use the Modula-2 language\n");
163 /* Restore the silly string. */
164 set_language(current_language->la_language);
c8023e66
JG
165 return;
166 }
167
168 /* Search the list of languages for a match. */
169 for (i = 0; i < languages_size; i++) {
170 if (!strcmp (languages[i]->la_name, language)) {
171 /* Found it! Go into manual mode, and use this language. */
172 if (languages[i]->la_language == language_auto) {
173 /* Enter auto mode. Set to the current frame's language, if known. */
174 language_mode = language_mode_auto;
175 flang = get_frame_language();
176 if (flang!=language_unknown)
177 set_language(flang);
178 return;
179 } else {
180 /* Enter manual mode. Set the specified language. */
181 language_mode = language_mode_manual;
182 current_language = languages[i];
183 set_type_range ();
184 set_lang_str();
185 return;
186 }
187 }
188 }
189
5f3d478e
JG
190 /* Reset the language (esp. the global string "language") to the
191 correct values. */
192 err_lang=savestring(language,strlen(language));
193 make_cleanup (free, err_lang); /* Free it after error */
194 set_language(current_language->la_language);
195 error ("Unknown language `%s'.",err_lang);
c8023e66
JG
196}
197
198/* Show command. Display a warning if the type setting does
199 not match the current language. */
7ed0f002 200static void
d8ce1326
JG
201show_type_command(ignore, from_tty)
202 char *ignore;
c8023e66
JG
203 int from_tty;
204{
205 if (type_check != current_language->la_type_check)
206 printf(
207"Warning: the current type check setting does not match the language.\n");
208}
209
210/* Set command. Change the setting for type checking. */
7ed0f002 211static void
d8ce1326
JG
212set_type_command(ignore, from_tty)
213 char *ignore;
c8023e66
JG
214 int from_tty;
215{
216 if (!strcmp(type,"on"))
217 {
218 type_check = type_check_on;
219 type_mode = type_mode_manual;
220 }
221 else if (!strcmp(type,"warn"))
222 {
223 type_check = type_check_warn;
224 type_mode = type_mode_manual;
225 }
226 else if (!strcmp(type,"off"))
227 {
228 type_check = type_check_off;
229 type_mode = type_mode_manual;
230 }
231 else if (!strcmp(type,"auto"))
232 {
233 type_mode = type_mode_auto;
234 set_type_range();
235 /* Avoid hitting the set_type_str call below. We
236 did it in set_type_range. */
237 return;
238 }
239 set_type_str();
d8ce1326 240 show_type_command((char *)NULL, from_tty);
c8023e66
JG
241}
242
243/* Show command. Display a warning if the range setting does
244 not match the current language. */
7ed0f002 245static void
d8ce1326
JG
246show_range_command(ignore, from_tty)
247 char *ignore;
c8023e66
JG
248 int from_tty;
249{
250
251 if (range_check != current_language->la_range_check)
252 printf(
253"Warning: the current range check setting does not match the language.\n");
254}
255
256/* Set command. Change the setting for range checking. */
7ed0f002 257static void
d8ce1326
JG
258set_range_command(ignore, from_tty)
259 char *ignore;
c8023e66
JG
260 int from_tty;
261{
262 if (!strcmp(range,"on"))
263 {
264 range_check = range_check_on;
265 range_mode = range_mode_manual;
266 }
267 else if (!strcmp(range,"warn"))
268 {
269 range_check = range_check_warn;
270 range_mode = range_mode_manual;
271 }
272 else if (!strcmp(range,"off"))
273 {
274 range_check = range_check_off;
275 range_mode = range_mode_manual;
276 }
277 else if (!strcmp(range,"auto"))
278 {
279 range_mode = range_mode_auto;
280 set_type_range();
281 /* Avoid hitting the set_range_str call below. We
282 did it in set_type_range. */
283 return;
284 }
285 set_range_str();
d8ce1326 286 show_range_command((char *)0, from_tty);
c8023e66
JG
287}
288
289/* Set the status of range and type checking based on
290 the current modes and the current language.
291 If SHOW is non-zero, then print out the current language,
292 type and range checking status. */
293static void
294set_type_range()
295{
c8023e66
JG
296
297 if (range_mode == range_mode_auto)
298 range_check = current_language->la_range_check;
299
300 if (type_mode == type_mode_auto)
301 type_check = current_language->la_type_check;
302
303 set_type_str();
304 set_range_str();
305}
306
307/* Set current language to (enum language) LANG. */
308
309void
310set_language(lang)
311 enum language lang;
312{
313 int i;
314
315 for (i = 0; i < languages_size; i++) {
316 if (languages[i]->la_language == lang) {
317 current_language = languages[i];
318 set_type_range ();
319 set_lang_str();
545af6ce 320 break;
c8023e66
JG
321 }
322 }
323}
324\f
325/* This page contains functions that update the global vars
326 language, type and range. */
7ed0f002 327static void
c8023e66
JG
328set_lang_str()
329{
d8ce1326 330 char *prefix = "";
c8023e66
JG
331
332 free (language);
333 if (language_mode == language_mode_auto)
334 prefix = "auto; currently ";
335
58ae87f6 336 language = concat(prefix, current_language->la_name, NULL);
c8023e66
JG
337}
338
7ed0f002 339static void
c8023e66
JG
340set_type_str()
341{
342 char *tmp, *prefix = "";
343
344 free (type);
345 if (type_mode==type_mode_auto)
346 prefix = "auto; currently ";
347
348 switch(type_check)
349 {
350 case type_check_on:
351 tmp = "on";
352 break;
353 case type_check_off:
354 tmp = "off";
355 break;
356 case type_check_warn:
357 tmp = "warn";
358 break;
359 default:
360 error ("Unrecognized type check setting.");
361 }
362
58ae87f6 363 type = concat(prefix,tmp,NULL);
c8023e66
JG
364}
365
7ed0f002 366static void
c8023e66
JG
367set_range_str()
368{
369 char *tmp, *pref = "";
370
371 free (range);
372 if (range_mode==range_mode_auto)
373 pref = "auto; currently ";
374
375 switch(range_check)
376 {
377 case range_check_on:
378 tmp = "on";
379 break;
380 case range_check_off:
381 tmp = "off";
382 break;
383 case range_check_warn:
384 tmp = "warn";
385 break;
386 default:
387 error ("Unrecognized range check setting.");
388 }
389
58ae87f6 390 range = concat(pref,tmp,NULL);
c8023e66
JG
391}
392
393
394/* Print out the current language settings: language, range and
7ed0f002 395 type checking. If QUIETLY, print only what has changed. */
c8023e66 396void
7ed0f002
JG
397language_info (quietly)
398 int quietly;
c8023e66 399{
7ed0f002 400 /* FIXME: quietly is ignored at the moment. */
c8023e66 401 printf("Current Language: %s\n",language);
d8ce1326 402 show_language_command((char *)0, 1);
c8023e66 403 printf("Type checking: %s\n",type);
d8ce1326 404 show_type_command((char *)0, 1);
c8023e66 405 printf("Range checking: %s\n",range);
d8ce1326 406 show_range_command((char *)0, 1);
c8023e66
JG
407}
408\f
409/* Return the result of a binary operation. */
7ed0f002
JG
410
411#if 0 /* Currently unused */
412
c8023e66
JG
413struct type *
414binop_result_type(v1,v2)
415 value v1,v2;
416{
417 int l1,l2,size,uns;
418
d8ce1326
JG
419 l1 = TYPE_LENGTH(VALUE_TYPE(v1));
420 l2 = TYPE_LENGTH(VALUE_TYPE(v2));
c8023e66
JG
421
422 switch(current_language->la_language)
423 {
424 case language_c:
545af6ce 425 case language_cplus:
c8023e66
JG
426 if (TYPE_CODE(VALUE_TYPE(v1))==TYPE_CODE_FLT)
427 return TYPE_CODE(VALUE_TYPE(v2)) == TYPE_CODE_FLT && l2 > l1 ?
428 VALUE_TYPE(v2) : VALUE_TYPE(v1);
429 else if (TYPE_CODE(VALUE_TYPE(v2))==TYPE_CODE_FLT)
430 return TYPE_CODE(VALUE_TYPE(v1)) == TYPE_CODE_FLT && l1 > l2 ?
431 VALUE_TYPE(v1) : VALUE_TYPE(v2);
432 else if (TYPE_UNSIGNED(VALUE_TYPE(v1)) && l1 > l2)
433 return VALUE_TYPE(v1);
434 else if (TYPE_UNSIGNED(VALUE_TYPE(v2)) && l2 > l1)
435 return VALUE_TYPE(v2);
436 else /* Both are signed. Result is the longer type */
437 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
438 break;
439 case language_m2:
440 /* If we are doing type-checking, l1 should equal l2, so this is
441 not needed. */
442 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
443 break;
444 }
d8ce1326
JG
445 abort();
446 return (struct type *)0; /* For lint */
c8023e66 447}
7ed0f002
JG
448
449#endif /* 0 */
450
c8023e66
JG
451\f
452/* This page contains functions that return format strings for
453 printf for printing out numbers in different formats */
454
455/* Returns the appropriate printf format for hexadecimal
456 numbers. */
457char *
458local_hex_format_custom(pre)
459 char *pre;
460{
461 static char form[50];
462
463 strcpy (form, current_language->la_hex_format_pre);
464 strcat (form, pre);
465 strcat (form, current_language->la_hex_format_suf);
466 return form;
467}
468
469/* Converts a number to hexadecimal and stores it in a static
470 string. Returns a pointer to this string. */
471char *
472local_hex_string (num)
473 int num;
474{
475 static char res[50];
476
477 sprintf (res, current_language->la_hex_format, num);
478 return res;
479}
480
481/* Converts a number to custom hexadecimal and stores it in a static
482 string. Returns a pointer to this string. */
483char *
484local_hex_string_custom(num,pre)
485 int num;
486 char *pre;
487{
488 static char res[50];
489
490 sprintf (res, local_hex_format_custom(pre), num);
491 return res;
492}
493
494/* Returns the appropriate printf format for octal
495 numbers. */
496char *
497local_octal_format_custom(pre)
498 char *pre;
499{
500 static char form[50];
501
502 strcpy (form, current_language->la_octal_format_pre);
503 strcat (form, pre);
504 strcat (form, current_language->la_octal_format_suf);
505 return form;
506}
507\f
508/* This page contains functions that are used in type/range checking.
509 They all return zero if the type/range check fails.
510
511 It is hoped that these will make extending GDB to parse different
512 languages a little easier. These are primarily used in eval.c when
513 evaluating expressions and making sure that their types are correct.
514 Instead of having a mess of conjucted/disjuncted expressions in an "if",
515 the ideas of type can be wrapped up in the following functions.
516
517 Note that some of them are not currently dependent upon which language
518 is currently being parsed. For example, floats are the same in
519 C and Modula-2 (ie. the only floating point type has TYPE_CODE of
520 TYPE_CODE_FLT), while booleans are different. */
521
522/* Returns non-zero if its argument is a simple type. This is the same for
523 both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur,
524 and thus will never cause the failure of the test. */
525int
526simple_type(type)
527 struct type *type;
528{
529 switch (TYPE_CODE (type)) {
530 case TYPE_CODE_INT:
531 case TYPE_CODE_CHAR:
532 case TYPE_CODE_ENUM:
533 case TYPE_CODE_FLT:
534 case TYPE_CODE_RANGE:
535 case TYPE_CODE_BOOL:
536 return 1;
537
538 default:
539 return 0;
540 }
541}
542
543/* Returns non-zero if its argument is of an ordered type. */
544int
545ordered_type (type)
546 struct type *type;
547{
548 switch (TYPE_CODE (type)) {
549 case TYPE_CODE_INT:
550 case TYPE_CODE_CHAR:
551 case TYPE_CODE_ENUM:
552 case TYPE_CODE_FLT:
553 case TYPE_CODE_RANGE:
554 return 1;
555
556 default:
557 return 0;
558 }
559}
560
561/* Returns non-zero if the two types are the same */
562int
563same_type (arg1, arg2)
564 struct type *arg1, *arg2;
565{
566 if (structured_type(arg1) ? !structured_type(arg2) : structured_type(arg2))
567 /* One is structured and one isn't */
568 return 0;
569 else if (structured_type(arg1) && structured_type(arg2))
570 return arg1 == arg2;
571 else if (numeric_type(arg1) && numeric_type(arg2))
572 return (TYPE_CODE(arg2) == TYPE_CODE(arg1)) &&
573 (TYPE_UNSIGNED(arg1) == TYPE_UNSIGNED(arg2))
574 ? 1 : 0;
575 else
576 return arg1==arg2;
577}
578
579/* Returns non-zero if the type is integral */
580int
581integral_type (type)
582 struct type *type;
583{
584 switch(current_language->la_language)
585 {
586 case language_c:
545af6ce 587 case language_cplus:
c8023e66
JG
588 return (TYPE_CODE(type) != TYPE_CODE_INT) &&
589 (TYPE_CODE(type) != TYPE_CODE_ENUM) ? 0 : 1;
590 case language_m2:
591 return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
592 default:
593 error ("Language not supported.");
594 }
595}
596
597/* Returns non-zero if the value is numeric */
598int
599numeric_type (type)
600 struct type *type;
601{
602 switch (TYPE_CODE (type)) {
603 case TYPE_CODE_INT:
604 case TYPE_CODE_FLT:
605 return 1;
606
607 default:
608 return 0;
609 }
610}
611
612/* Returns non-zero if the value is a character type */
613int
614character_type (type)
615 struct type *type;
616{
617 switch(current_language->la_language)
618 {
619 case language_m2:
620 return TYPE_CODE(type) != TYPE_CODE_CHAR ? 0 : 1;
621
622 case language_c:
545af6ce 623 case language_cplus:
c8023e66
JG
624 return (TYPE_CODE(type) == TYPE_CODE_INT) &&
625 TYPE_LENGTH(type) == sizeof(char)
626 ? 1 : 0;
627 }
7ed0f002 628 return (0);
c8023e66
JG
629}
630
631/* Returns non-zero if the value is a boolean type */
632int
633boolean_type (type)
634 struct type *type;
635{
636 switch(current_language->la_language)
637 {
638 case language_m2:
639 return TYPE_CODE(type) != TYPE_CODE_BOOL ? 0 : 1;
640
641 case language_c:
545af6ce 642 case language_cplus:
c8023e66
JG
643 return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
644 }
7ed0f002 645 return (0);
c8023e66
JG
646}
647
648/* Returns non-zero if the value is a floating-point type */
649int
650float_type (type)
651 struct type *type;
652{
653 return TYPE_CODE(type) == TYPE_CODE_FLT;
654}
655
656/* Returns non-zero if the value is a pointer type */
657int
658pointer_type(type)
659 struct type *type;
660{
661 return TYPE_CODE(type) == TYPE_CODE_PTR ||
662 TYPE_CODE(type) == TYPE_CODE_REF;
663}
664
665/* Returns non-zero if the value is a structured type */
666int
667structured_type(type)
668 struct type *type;
669{
670 switch(current_language->la_language)
671 {
672 case language_c:
545af6ce 673 case language_cplus:
c8023e66
JG
674 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
675 (TYPE_CODE(type) == TYPE_CODE_UNION) ||
676 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
677 case language_m2:
678 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
679 (TYPE_CODE(type) == TYPE_CODE_SET) ||
680 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
681 }
7ed0f002 682 return (0);
c8023e66
JG
683}
684\f
685/* This page contains functions that return info about
686 (struct value) values used in GDB. */
687
688/* Returns non-zero if the value VAL represents a true value. */
689int
690value_true(val)
691 value val;
692{
693 int len, i;
694 struct type *type;
695 LONGEST v;
696
697 switch (current_language->la_language) {
698
699 case language_c:
545af6ce 700 case language_cplus:
c8023e66
JG
701 return !value_zerop (val);
702
703 case language_m2:
704 type = VALUE_TYPE(val);
705 if (TYPE_CODE (type) != TYPE_CODE_BOOL)
706 return 0; /* Not a BOOLEAN at all */
707 /* Search the fields for one that matches the current value. */
708 len = TYPE_NFIELDS (type);
709 v = value_as_long (val);
710 for (i = 0; i < len; i++)
711 {
712 QUIT;
713 if (v == TYPE_FIELD_BITPOS (type, i))
714 break;
715 }
716 if (i >= len)
717 return 0; /* Not a valid BOOLEAN value */
718 if (!strcmp ("TRUE", TYPE_FIELD_NAME(VALUE_TYPE(val), i)))
719 return 1; /* BOOLEAN with value TRUE */
720 else
721 return 0; /* BOOLEAN with value FALSE */
722 break;
723
724 default:
725 error ("Language not supported.");
726 }
727}
728\f
729/* Returns non-zero if the operator OP is defined on
730 the values ARG1 and ARG2. */
7ed0f002
JG
731
732#if 0 /* Currently unused */
733
c8023e66
JG
734void
735binop_type_check(arg1,arg2,op)
736 value arg1,arg2;
737 int op;
738{
739 struct type *t1, *t2;
740
741 /* If we're not checking types, always return success. */
742 if (!STRICT_TYPE)
743 return;
744
745 t1=VALUE_TYPE(arg1);
746 if (arg2!=(value)NULL)
747 t2=VALUE_TYPE(arg2);
748 else
749 t2=NULL;
750
751 switch(op)
752 {
753 case BINOP_ADD:
754 case BINOP_SUB:
755 if ((numeric_type(t1) && pointer_type(t2)) ||
756 (pointer_type(t1) && numeric_type(t2)))
757 {
758 printf("warning: combining pointer and integer.\n");
759 break;
760 }
761 case BINOP_MUL:
762 case BINOP_LSH:
763 case BINOP_RSH:
764 if (!numeric_type(t1) || !numeric_type(t2))
765 type_op_error ("Arguments to %s must be numbers.",op);
766 else if (!same_type(t1,t2))
767 type_op_error ("Arguments to %s must be of the same type.",op);
768 break;
769
770 case BINOP_AND:
771 case BINOP_OR:
772 if (!boolean_type(t1) || !boolean_type(t2))
773 type_op_error ("Arguments to %s must be of boolean type.",op);
774 break;
775
776 case BINOP_EQUAL:
777 if ((pointer_type(t1) && !(pointer_type(t2) || integral_type(t2))) ||
778 (pointer_type(t2) && !(pointer_type(t1) || integral_type(t1))))
779 type_op_error ("A pointer can only be compared to an integer or pointer.",op);
780 else if ((pointer_type(t1) && integral_type(t2)) ||
781 (integral_type(t1) && pointer_type(t2)))
782 {
783 printf("warning: combining integer and pointer.\n");
784 break;
785 }
786 else if (!simple_type(t1) || !simple_type(t2))
787 type_op_error ("Arguments to %s must be of simple type.",op);
788 else if (!same_type(t1,t2))
789 type_op_error ("Arguments to %s must be of the same type.",op);
790 break;
791
792 case BINOP_REM:
793 if (!integral_type(t1) || !integral_type(t2))
794 type_op_error ("Arguments to %s must be of integral type.",op);
795 break;
796
797 case BINOP_LESS:
798 case BINOP_GTR:
799 case BINOP_LEQ:
800 case BINOP_GEQ:
801 if (!ordered_type(t1) || !ordered_type(t2))
802 type_op_error ("Arguments to %s must be of ordered type.",op);
803 else if (!same_type(t1,t2))
804 type_op_error ("Arguments to %s must be of the same type.",op);
805 break;
806
807 case BINOP_ASSIGN:
808 if (pointer_type(t1) && !integral_type(t2))
809 type_op_error ("A pointer can only be assigned an integer.",op);
810 else if (pointer_type(t1) && integral_type(t2))
811 {
812 printf("warning: combining integer and pointer.");
813 break;
814 }
815 else if (!simple_type(t1) || !simple_type(t2))
816 type_op_error ("Arguments to %s must be of simple type.",op);
817 else if (!same_type(t1,t2))
818 type_op_error ("Arguments to %s must be of the same type.",op);
819 break;
820
821 /* Unary checks -- arg2 is null */
822
823 case UNOP_ZEROP:
824 if (!boolean_type(t1))
825 type_op_error ("Argument to %s must be of boolean type.",op);
826 break;
827
828 case UNOP_PLUS:
829 case UNOP_NEG:
830 if (!numeric_type(t1))
831 type_op_error ("Argument to %s must be of numeric type.",op);
832 break;
833
834 case UNOP_IND:
835 if (integral_type(t1))
836 {
837 printf("warning: combining pointer and integer.\n");
838 break;
839 }
840 else if (!pointer_type(t1))
841 type_op_error ("Argument to %s must be a pointer.",op);
842 break;
843
844 case UNOP_PREINCREMENT:
845 case UNOP_POSTINCREMENT:
846 case UNOP_PREDECREMENT:
847 case UNOP_POSTDECREMENT:
848 if (!ordered_type(t1))
849 type_op_error ("Argument to %s must be of an ordered type.",op);
850 break;
851
852 default:
853 /* Ok. The following operators have different meanings in
854 different languages. */
855 switch(current_language->la_language)
856 {
857#ifdef _LANG_c
858 case language_c:
545af6ce 859 case language_cplus:
c8023e66
JG
860 switch(op)
861 {
862 case BINOP_DIV:
863 if (!numeric_type(t1) || !numeric_type(t2))
864 type_op_error ("Arguments to %s must be numbers.",op);
865 break;
866 }
867 break;
868#endif
869
870#ifdef _LANG_m2
871 case language_m2:
872 switch(op)
873 {
874 case BINOP_DIV:
875 if (!float_type(t1) || !float_type(t2))
876 type_op_error ("Arguments to %s must be floating point numbers.",op);
877 break;
878 case BINOP_INTDIV:
879 if (!integral_type(t1) || !integral_type(t2))
880 type_op_error ("Arguments to %s must be of integral type.",op);
881 break;
882 }
883#endif
884 }
885 }
886}
7ed0f002
JG
887
888#endif /* 0 */
889
c8023e66
JG
890\f
891/* This page contains functions for the printing out of
892 error messages that occur during type- and range-
893 checking. */
894
895/* Prints the format string FMT with the operator as a string
896 corresponding to the opcode OP. If FATAL is non-zero, then
897 this is an error and error () is called. Otherwise, it is
898 a warning and printf() is called. */
899void
900op_error (fmt,op,fatal)
901 char *fmt;
902 enum exp_opcode op;
903 int fatal;
904{
905 if (fatal)
906 error (fmt,op_string(op));
907 else
908 {
909 printf("warning: ");
910 printf(fmt,op_string(op));
911 printf("\n");
912 }
913}
914
915/* These are called when a language fails a type- or range-check.
916 The first argument should be a printf()-style format string, and
917 the rest of the arguments should be its arguments. If
918 [type|range]_check is [type|range]_check_on, then return_to_top_level()
919 is called in the style of error (). Otherwise, the message is prefixed
920 by "warning: " and we do not return to the top level. */
7ed0f002 921
c8023e66
JG
922void
923type_error (va_alist)
7ed0f002 924 va_dcl
c8023e66
JG
925{
926 va_list args;
927 char *string;
928
929 if (type_check==type_check_warn)
930 fprintf(stderr,"warning: ");
931 else
932 target_terminal_ours();
933
934 va_start (args);
935 string = va_arg (args, char *);
936 vfprintf (stderr, string, args);
937 fprintf (stderr, "\n");
938 va_end (args);
939 if (type_check==type_check_on)
940 return_to_top_level();
941}
942
943void
944range_error (va_alist)
7ed0f002 945 va_dcl
c8023e66
JG
946{
947 va_list args;
948 char *string;
949
950 if (range_check==range_check_warn)
951 fprintf(stderr,"warning: ");
952 else
953 target_terminal_ours();
954
955 va_start (args);
956 string = va_arg (args, char *);
957 vfprintf (stderr, string, args);
958 fprintf (stderr, "\n");
959 va_end (args);
960 if (range_check==range_check_on)
961 return_to_top_level();
962}
963
964\f
965/* This page contains miscellaneous functions */
966
967/* Return the language as a string */
968char *
969language_str(lang)
970 enum language lang;
971{
972 int i;
973
974 for (i = 0; i < languages_size; i++) {
975 if (languages[i]->la_language == lang) {
976 return languages[i]->la_name;
977 }
978 }
979 return "Unknown";
980}
981
982struct cmd_list_element *setchecklist = NULL;
983struct cmd_list_element *showchecklist = NULL;
984
985static void
d8ce1326
JG
986set_check (ignore, from_tty)
987 char *ignore;
c8023e66
JG
988 int from_tty;
989{
990 printf(
991"\"set check\" must be followed by the name of a check subcommand.\n");
992 help_list(setchecklist, "set check ", -1, stdout);
993}
994
995static void
996show_check (arg, from_tty)
997 char *arg;
998 int from_tty;
999{
1000 cmd_show_list(showchecklist, from_tty, "");
1001}
1002\f
1003/* Add a language to the set of known languages. */
1004
1005void
1006add_language (lang)
7ed0f002 1007 const struct language_defn *lang;
c8023e66
JG
1008{
1009 if (lang->la_magic != LANG_MAGIC)
1010 {
1011 fprintf(stderr, "Magic number of %s language struct wrong\n",
1012 lang->la_name);
1013 abort();
1014 }
1015
1016 if (!languages)
1017 {
1018 languages_allocsize = DEFAULT_ALLOCSIZE;
7ed0f002 1019 languages = (const struct language_defn **) xmalloc
c8023e66
JG
1020 (languages_allocsize * sizeof (*languages));
1021 }
1022 if (languages_size >= languages_allocsize)
1023 {
1024 languages_allocsize *= 2;
7ed0f002 1025 languages = (const struct language_defn **) xrealloc ((char *) languages,
c8023e66
JG
1026 languages_allocsize * sizeof (*languages));
1027 }
1028 languages[languages_size++] = lang;
c8023e66
JG
1029}
1030
1031/* Define the language that is no language. */
1032
7ed0f002 1033static int
c8023e66
JG
1034unk_lang_parser ()
1035{
1036 return 1;
1037}
1038
7ed0f002
JG
1039static void
1040unk_lang_error (msg)
1041 char *msg;
c8023e66
JG
1042{
1043 error ("Attempted to parse an expression with unknown language");
1044}
1045
1046static struct type ** const (unknown_builtin_types[]) = { 0 };
1047static const struct op_print unk_op_print_tab[] = { 0 };
1048
1049const struct language_defn unknown_language_defn = {
1050 "unknown",
1051 language_unknown,
1052 &unknown_builtin_types[0],
1053 range_check_off,
1054 type_check_off,
1055 unk_lang_parser,
1056 unk_lang_error,
1057 &builtin_type_error, /* longest signed integral type */
1058 &builtin_type_error, /* longest unsigned integral type */
1059 &builtin_type_error, /* longest floating point type */
1060 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1061 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1062 unk_op_print_tab, /* expression operators for printing */
1063 LANG_MAGIC
1064};
1065
1066/* These two structs define fake entries for the "local" and "auto" options. */
1067const struct language_defn auto_language_defn = {
1068 "auto",
1069 language_auto,
1070 &unknown_builtin_types[0],
1071 range_check_off,
1072 type_check_off,
1073 unk_lang_parser,
1074 unk_lang_error,
1075 &builtin_type_error, /* longest signed integral type */
1076 &builtin_type_error, /* longest unsigned integral type */
1077 &builtin_type_error, /* longest floating point type */
1078 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1079 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1080 unk_op_print_tab, /* expression operators for printing */
1081 LANG_MAGIC
1082};
1083
1084const struct language_defn local_language_defn = {
1085 "local",
1086 language_auto,
1087 &unknown_builtin_types[0],
1088 range_check_off,
1089 type_check_off,
1090 unk_lang_parser,
1091 unk_lang_error,
1092 &builtin_type_error, /* longest signed integral type */
1093 &builtin_type_error, /* longest unsigned integral type */
1094 &builtin_type_error, /* longest floating point type */
1095 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1096 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1097 unk_op_print_tab, /* expression operators for printing */
1098 LANG_MAGIC
1099};
1100\f
1101/* Initialize the language routines */
1102
1103void
1104_initialize_language()
1105{
1106 struct cmd_list_element *set, *show;
1107
1108 /* GDB commands for language specific stuff */
1109
1110 set = add_set_cmd ("language", class_support, var_string_noescape,
1111 (char *)&language,
0b798409 1112 "Set the current source language.",
c8023e66
JG
1113 &setlist);
1114 show = add_show_from_set (set, &showlist);
1ab3bf1b
JG
1115 set->function.cfunc = set_language_command;
1116 show->function.cfunc = show_language_command;
c8023e66
JG
1117
1118 add_prefix_cmd ("check", no_class, set_check,
1119 "Set the status of the type/range checker",
1120 &setchecklist, "set check ", 0, &setlist);
1121 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1122 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1123
1124 add_prefix_cmd ("check", no_class, show_check,
1125 "Show the status of the type/range checker",
7cb83757 1126 &showchecklist, "show check ", 0, &showlist);
c8023e66
JG
1127 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1128 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1129
1130 set = add_set_cmd ("type", class_support, var_string_noescape,
1131 (char *)&type,
7cb83757 1132 "Set type checking. (on/warn/off/auto)",
c8023e66
JG
1133 &setchecklist);
1134 show = add_show_from_set (set, &showchecklist);
1ab3bf1b
JG
1135 set->function.cfunc = set_type_command;
1136 show->function.cfunc = show_type_command;
c8023e66
JG
1137
1138 set = add_set_cmd ("range", class_support, var_string_noescape,
1139 (char *)&range,
7cb83757 1140 "Set range checking. (on/warn/off/auto)",
c8023e66
JG
1141 &setchecklist);
1142 show = add_show_from_set (set, &showchecklist);
1ab3bf1b
JG
1143 set->function.cfunc = set_range_command;
1144 show->function.cfunc = show_range_command;
c8023e66
JG
1145
1146 add_language (&unknown_language_defn);
1147 add_language (&local_language_defn);
1148 add_language (&auto_language_defn);
1149
1150 language = savestring ("auto",strlen("auto"));
1151 range = savestring ("auto",strlen("auto"));
1152 type = savestring ("auto",strlen("auto"));
1153
1154 /* Have the above take effect */
1155
5f3d478e 1156 set_language_command (language, 0);
c8023e66
JG
1157 set_type_command (NULL, 0);
1158 set_range_command (NULL, 0);
1159}