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