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