]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/language.c
* ch-valprint.c (chill_val_print): On TYPE_CODE_STRING, don't
[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;
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]) {
199b2450
TL
164 printf_unfiltered("The currently understood settings are:\n\n");
165 printf_unfiltered ("local or auto Automatic setting based on source file\n");
166 printf_unfiltered ("c Use the C language\n");
167 printf_unfiltered ("c++ Use the C++ language\n");
168 printf_unfiltered ("chill Use the Chill language\n");
a91a6192 169 printf_unfiltered ("fortran Use the Fortran language\n");
199b2450 170 printf_unfiltered ("modula-2 Use the Modula-2 language\n");
0b798409
JG
171 /* Restore the silly string. */
172 set_language(current_language->la_language);
c8023e66
JG
173 return;
174 }
175
176 /* Search the list of languages for a match. */
177 for (i = 0; i < languages_size; i++) {
2e4964ad 178 if (STREQ (languages[i]->la_name, language)) {
c8023e66
JG
179 /* Found it! Go into manual mode, and use this language. */
180 if (languages[i]->la_language == language_auto) {
181 /* Enter auto mode. Set to the current frame's language, if known. */
182 language_mode = language_mode_auto;
183 flang = get_frame_language();
184 if (flang!=language_unknown)
185 set_language(flang);
b5af69c3 186 expected_language = current_language;
c8023e66
JG
187 return;
188 } else {
189 /* Enter manual mode. Set the specified language. */
190 language_mode = language_mode_manual;
191 current_language = languages[i];
192 set_type_range ();
193 set_lang_str();
b5af69c3 194 expected_language = current_language;
c8023e66
JG
195 return;
196 }
197 }
198 }
199
5f3d478e
JG
200 /* Reset the language (esp. the global string "language") to the
201 correct values. */
202 err_lang=savestring(language,strlen(language));
203 make_cleanup (free, err_lang); /* Free it after error */
204 set_language(current_language->la_language);
205 error ("Unknown language `%s'.",err_lang);
c8023e66
JG
206}
207
208/* Show command. Display a warning if the type setting does
209 not match the current language. */
7ed0f002 210static void
d8ce1326
JG
211show_type_command(ignore, from_tty)
212 char *ignore;
c8023e66
JG
213 int from_tty;
214{
215 if (type_check != current_language->la_type_check)
199b2450 216 printf_unfiltered(
c8023e66
JG
217"Warning: the current type check setting does not match the language.\n");
218}
219
220/* Set command. Change the setting for type checking. */
7ed0f002 221static void
d8ce1326
JG
222set_type_command(ignore, from_tty)
223 char *ignore;
c8023e66
JG
224 int from_tty;
225{
2e4964ad 226 if (STREQ(type,"on"))
c8023e66
JG
227 {
228 type_check = type_check_on;
229 type_mode = type_mode_manual;
230 }
2e4964ad 231 else if (STREQ(type,"warn"))
c8023e66
JG
232 {
233 type_check = type_check_warn;
234 type_mode = type_mode_manual;
235 }
2e4964ad 236 else if (STREQ(type,"off"))
c8023e66
JG
237 {
238 type_check = type_check_off;
239 type_mode = type_mode_manual;
240 }
2e4964ad 241 else if (STREQ(type,"auto"))
c8023e66
JG
242 {
243 type_mode = type_mode_auto;
244 set_type_range();
245 /* Avoid hitting the set_type_str call below. We
246 did it in set_type_range. */
247 return;
248 }
249 set_type_str();
d8ce1326 250 show_type_command((char *)NULL, from_tty);
c8023e66
JG
251}
252
253/* Show command. Display a warning if the range setting does
254 not match the current language. */
7ed0f002 255static void
d8ce1326
JG
256show_range_command(ignore, from_tty)
257 char *ignore;
c8023e66
JG
258 int from_tty;
259{
260
261 if (range_check != current_language->la_range_check)
199b2450 262 printf_unfiltered(
c8023e66
JG
263"Warning: the current range check setting does not match the language.\n");
264}
265
266/* Set command. Change the setting for range checking. */
7ed0f002 267static void
d8ce1326
JG
268set_range_command(ignore, from_tty)
269 char *ignore;
c8023e66
JG
270 int from_tty;
271{
2e4964ad 272 if (STREQ(range,"on"))
c8023e66
JG
273 {
274 range_check = range_check_on;
275 range_mode = range_mode_manual;
276 }
2e4964ad 277 else if (STREQ(range,"warn"))
c8023e66
JG
278 {
279 range_check = range_check_warn;
280 range_mode = range_mode_manual;
281 }
2e4964ad 282 else if (STREQ(range,"off"))
c8023e66
JG
283 {
284 range_check = range_check_off;
285 range_mode = range_mode_manual;
286 }
2e4964ad 287 else if (STREQ(range,"auto"))
c8023e66
JG
288 {
289 range_mode = range_mode_auto;
290 set_type_range();
291 /* Avoid hitting the set_range_str call below. We
292 did it in set_type_range. */
293 return;
294 }
295 set_range_str();
d8ce1326 296 show_range_command((char *)0, from_tty);
c8023e66
JG
297}
298
299/* Set the status of range and type checking based on
300 the current modes and the current language.
301 If SHOW is non-zero, then print out the current language,
302 type and range checking status. */
303static void
304set_type_range()
305{
c8023e66
JG
306
307 if (range_mode == range_mode_auto)
308 range_check = current_language->la_range_check;
309
310 if (type_mode == type_mode_auto)
311 type_check = current_language->la_type_check;
312
313 set_type_str();
314 set_range_str();
315}
316
317/* Set current language to (enum language) LANG. */
318
319void
320set_language(lang)
321 enum language lang;
322{
323 int i;
324
325 for (i = 0; i < languages_size; i++) {
326 if (languages[i]->la_language == lang) {
327 current_language = languages[i];
328 set_type_range ();
329 set_lang_str();
545af6ce 330 break;
c8023e66
JG
331 }
332 }
333}
334\f
335/* This page contains functions that update the global vars
336 language, type and range. */
7ed0f002 337static void
c8023e66
JG
338set_lang_str()
339{
d8ce1326 340 char *prefix = "";
c8023e66
JG
341
342 free (language);
343 if (language_mode == language_mode_auto)
344 prefix = "auto; currently ";
345
58ae87f6 346 language = concat(prefix, current_language->la_name, NULL);
c8023e66
JG
347}
348
7ed0f002 349static void
c8023e66
JG
350set_type_str()
351{
352 char *tmp, *prefix = "";
353
354 free (type);
355 if (type_mode==type_mode_auto)
356 prefix = "auto; currently ";
357
358 switch(type_check)
359 {
360 case type_check_on:
361 tmp = "on";
362 break;
363 case type_check_off:
364 tmp = "off";
365 break;
366 case type_check_warn:
367 tmp = "warn";
368 break;
369 default:
370 error ("Unrecognized type check setting.");
371 }
372
58ae87f6 373 type = concat(prefix,tmp,NULL);
c8023e66
JG
374}
375
7ed0f002 376static void
c8023e66
JG
377set_range_str()
378{
379 char *tmp, *pref = "";
380
381 free (range);
382 if (range_mode==range_mode_auto)
383 pref = "auto; currently ";
384
385 switch(range_check)
386 {
387 case range_check_on:
388 tmp = "on";
389 break;
390 case range_check_off:
391 tmp = "off";
392 break;
393 case range_check_warn:
394 tmp = "warn";
395 break;
396 default:
397 error ("Unrecognized range check setting.");
398 }
399
58ae87f6 400 range = concat(pref,tmp,NULL);
c8023e66
JG
401}
402
403
404/* Print out the current language settings: language, range and
7ed0f002 405 type checking. If QUIETLY, print only what has changed. */
b5af69c3 406
c8023e66 407void
7ed0f002
JG
408language_info (quietly)
409 int quietly;
c8023e66 410{
b5af69c3
JG
411 if (quietly && expected_language == current_language)
412 return;
413
414 expected_language = current_language;
199b2450 415 printf_unfiltered("Current language: %s\n",language);
b5af69c3
JG
416 show_language_command((char *)0, 1);
417
418 if (!quietly)
419 {
199b2450 420 printf_unfiltered("Type checking: %s\n",type);
b5af69c3 421 show_type_command((char *)0, 1);
199b2450 422 printf_unfiltered("Range checking: %s\n",range);
b5af69c3
JG
423 show_range_command((char *)0, 1);
424 }
c8023e66
JG
425}
426\f
427/* Return the result of a binary operation. */
7ed0f002
JG
428
429#if 0 /* Currently unused */
430
c8023e66 431struct type *
82a2edfb
JK
432binop_result_type (v1, v2)
433 value_ptr v1, v2;
c8023e66
JG
434{
435 int l1,l2,size,uns;
436
d8ce1326
JG
437 l1 = TYPE_LENGTH(VALUE_TYPE(v1));
438 l2 = TYPE_LENGTH(VALUE_TYPE(v2));
c8023e66
JG
439
440 switch(current_language->la_language)
441 {
442 case language_c:
545af6ce 443 case language_cplus:
c8023e66
JG
444 if (TYPE_CODE(VALUE_TYPE(v1))==TYPE_CODE_FLT)
445 return TYPE_CODE(VALUE_TYPE(v2)) == TYPE_CODE_FLT && l2 > l1 ?
446 VALUE_TYPE(v2) : VALUE_TYPE(v1);
447 else if (TYPE_CODE(VALUE_TYPE(v2))==TYPE_CODE_FLT)
448 return TYPE_CODE(VALUE_TYPE(v1)) == TYPE_CODE_FLT && l1 > l2 ?
449 VALUE_TYPE(v1) : VALUE_TYPE(v2);
450 else if (TYPE_UNSIGNED(VALUE_TYPE(v1)) && l1 > l2)
451 return VALUE_TYPE(v1);
452 else if (TYPE_UNSIGNED(VALUE_TYPE(v2)) && l2 > l1)
453 return VALUE_TYPE(v2);
454 else /* Both are signed. Result is the longer type */
455 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
456 break;
457 case language_m2:
458 /* If we are doing type-checking, l1 should equal l2, so this is
459 not needed. */
460 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
461 break;
5aefc1ca 462 case language_chill:
e58de8a2 463 error ("Missing Chill support in function binop_result_check.");/*FIXME*/
c8023e66 464 }
d8ce1326
JG
465 abort();
466 return (struct type *)0; /* For lint */
c8023e66 467}
7ed0f002
JG
468
469#endif /* 0 */
470
c8023e66
JG
471\f
472/* This page contains functions that return format strings for
473 printf for printing out numbers in different formats */
474
475/* Returns the appropriate printf format for hexadecimal
476 numbers. */
477char *
478local_hex_format_custom(pre)
479 char *pre;
480{
481 static char form[50];
482
2e66cf7d
FF
483 strcpy (form, local_hex_format_prefix ());
484 strcat (form, "%");
c8023e66 485 strcat (form, pre);
2e66cf7d
FF
486 strcat (form, local_hex_format_specifier ());
487 strcat (form, local_hex_format_suffix ());
c8023e66
JG
488 return form;
489}
490
491/* Converts a number to hexadecimal and stores it in a static
492 string. Returns a pointer to this string. */
493char *
494local_hex_string (num)
5573d7d4 495 unsigned long num;
c8023e66
JG
496{
497 static char res[50];
498
2e66cf7d 499 sprintf (res, local_hex_format(), num);
c8023e66
JG
500 return res;
501}
502
503/* Converts a number to custom hexadecimal and stores it in a static
504 string. Returns a pointer to this string. */
505char *
506local_hex_string_custom(num,pre)
5573d7d4 507 unsigned long num;
c8023e66
JG
508 char *pre;
509{
510 static char res[50];
511
512 sprintf (res, local_hex_format_custom(pre), num);
513 return res;
514}
515
516/* Returns the appropriate printf format for octal
517 numbers. */
518char *
519local_octal_format_custom(pre)
520 char *pre;
521{
522 static char form[50];
523
2e66cf7d
FF
524 strcpy (form, local_octal_format_prefix ());
525 strcat (form, "%");
c8023e66 526 strcat (form, pre);
2e66cf7d
FF
527 strcat (form, local_octal_format_specifier ());
528 strcat (form, local_octal_format_suffix ());
c8023e66
JG
529 return form;
530}
58a66e24
FF
531
532/* Returns the appropriate printf format for decimal numbers. */
533char *
534local_decimal_format_custom(pre)
535 char *pre;
536{
537 static char form[50];
538
539 strcpy (form, local_decimal_format_prefix ());
540 strcat (form, "%");
541 strcat (form, pre);
542 strcat (form, local_decimal_format_specifier ());
543 strcat (form, local_decimal_format_suffix ());
544 return form;
545}
c8023e66
JG
546\f
547/* This page contains functions that are used in type/range checking.
548 They all return zero if the type/range check fails.
549
550 It is hoped that these will make extending GDB to parse different
551 languages a little easier. These are primarily used in eval.c when
552 evaluating expressions and making sure that their types are correct.
553 Instead of having a mess of conjucted/disjuncted expressions in an "if",
554 the ideas of type can be wrapped up in the following functions.
555
556 Note that some of them are not currently dependent upon which language
557 is currently being parsed. For example, floats are the same in
558 C and Modula-2 (ie. the only floating point type has TYPE_CODE of
559 TYPE_CODE_FLT), while booleans are different. */
560
561/* Returns non-zero if its argument is a simple type. This is the same for
562 both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur,
563 and thus will never cause the failure of the test. */
564int
565simple_type(type)
566 struct type *type;
567{
568 switch (TYPE_CODE (type)) {
569 case TYPE_CODE_INT:
570 case TYPE_CODE_CHAR:
571 case TYPE_CODE_ENUM:
572 case TYPE_CODE_FLT:
573 case TYPE_CODE_RANGE:
574 case TYPE_CODE_BOOL:
575 return 1;
576
577 default:
578 return 0;
579 }
580}
581
2e66cf7d
FF
582/* Returns non-zero if its argument is of an ordered type.
583 An ordered type is one in which the elements can be tested for the
584 properties of "greater than", "less than", etc, or for which the
585 operations "increment" or "decrement" make sense. */
c8023e66
JG
586int
587ordered_type (type)
588 struct type *type;
589{
590 switch (TYPE_CODE (type)) {
591 case TYPE_CODE_INT:
592 case TYPE_CODE_CHAR:
593 case TYPE_CODE_ENUM:
594 case TYPE_CODE_FLT:
595 case TYPE_CODE_RANGE:
596 return 1;
597
598 default:
599 return 0;
600 }
601}
602
603/* Returns non-zero if the two types are the same */
604int
605same_type (arg1, arg2)
606 struct type *arg1, *arg2;
607{
608 if (structured_type(arg1) ? !structured_type(arg2) : structured_type(arg2))
609 /* One is structured and one isn't */
610 return 0;
611 else if (structured_type(arg1) && structured_type(arg2))
612 return arg1 == arg2;
613 else if (numeric_type(arg1) && numeric_type(arg2))
614 return (TYPE_CODE(arg2) == TYPE_CODE(arg1)) &&
615 (TYPE_UNSIGNED(arg1) == TYPE_UNSIGNED(arg2))
616 ? 1 : 0;
617 else
618 return arg1==arg2;
619}
620
621/* Returns non-zero if the type is integral */
622int
623integral_type (type)
624 struct type *type;
625{
626 switch(current_language->la_language)
627 {
628 case language_c:
545af6ce 629 case language_cplus:
c8023e66
JG
630 return (TYPE_CODE(type) != TYPE_CODE_INT) &&
631 (TYPE_CODE(type) != TYPE_CODE_ENUM) ? 0 : 1;
632 case language_m2:
633 return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
e58de8a2
FF
634 case language_chill:
635 error ("Missing Chill support in function integral_type."); /*FIXME*/
c8023e66
JG
636 default:
637 error ("Language not supported.");
638 }
639}
640
641/* Returns non-zero if the value is numeric */
642int
643numeric_type (type)
644 struct type *type;
645{
646 switch (TYPE_CODE (type)) {
647 case TYPE_CODE_INT:
648 case TYPE_CODE_FLT:
649 return 1;
650
651 default:
652 return 0;
653 }
654}
655
656/* Returns non-zero if the value is a character type */
657int
658character_type (type)
659 struct type *type;
660{
661 switch(current_language->la_language)
662 {
2e66cf7d 663 case language_chill:
c8023e66
JG
664 case language_m2:
665 return TYPE_CODE(type) != TYPE_CODE_CHAR ? 0 : 1;
666
667 case language_c:
545af6ce 668 case language_cplus:
c8023e66
JG
669 return (TYPE_CODE(type) == TYPE_CODE_INT) &&
670 TYPE_LENGTH(type) == sizeof(char)
671 ? 1 : 0;
51b57ded
FF
672 default:
673 return (0);
c8023e66
JG
674 }
675}
676
fcbadaee
FF
677/* Returns non-zero if the value is a string type */
678int
679string_type (type)
680 struct type *type;
681{
682 switch(current_language->la_language)
683 {
fcbadaee 684 case language_chill:
fcbadaee
FF
685 case language_m2:
686 return TYPE_CODE(type) != TYPE_CODE_STRING ? 0 : 1;
687
688 case language_c:
689 case language_cplus:
690 /* C does not have distinct string type. */
691 return (0);
692 default:
693 return (0);
694 }
695}
696
c8023e66
JG
697/* Returns non-zero if the value is a boolean type */
698int
699boolean_type (type)
700 struct type *type;
701{
61932a8e
JK
702 if (TYPE_CODE (type) == TYPE_CODE_BOOL)
703 return 1;
704 switch(current_language->la_language)
705 {
706 case language_c:
707 case language_cplus:
708 /* Might be more cleanly handled by having a TYPE_CODE_INT_NOT_BOOL
709 for CHILL and such languages, or a TYPE_CODE_INT_OR_BOOL for C. */
710 if (TYPE_CODE (type) == TYPE_CODE_INT)
711 return 1;
51b57ded 712 default:
61932a8e 713 break;
c8023e66 714 }
61932a8e 715 return 0;
c8023e66
JG
716}
717
718/* Returns non-zero if the value is a floating-point type */
719int
720float_type (type)
721 struct type *type;
722{
723 return TYPE_CODE(type) == TYPE_CODE_FLT;
724}
725
726/* Returns non-zero if the value is a pointer type */
727int
728pointer_type(type)
729 struct type *type;
730{
731 return TYPE_CODE(type) == TYPE_CODE_PTR ||
732 TYPE_CODE(type) == TYPE_CODE_REF;
733}
734
735/* Returns non-zero if the value is a structured type */
736int
737structured_type(type)
738 struct type *type;
739{
740 switch(current_language->la_language)
741 {
742 case language_c:
545af6ce 743 case language_cplus:
c8023e66
JG
744 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
745 (TYPE_CODE(type) == TYPE_CODE_UNION) ||
746 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
747 case language_m2:
748 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
749 (TYPE_CODE(type) == TYPE_CODE_SET) ||
750 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
e58de8a2
FF
751 case language_chill:
752 error ("Missing Chill support in function structured_type."); /*FIXME*/
51b57ded
FF
753 default:
754 return (0);
c8023e66
JG
755 }
756}
757\f
758/* This page contains functions that return info about
759 (struct value) values used in GDB. */
760
761/* Returns non-zero if the value VAL represents a true value. */
762int
61932a8e 763value_true (val)
82a2edfb 764 value_ptr val;
c8023e66 765{
61932a8e
JK
766 /* It is possible that we should have some sort of error if a non-boolean
767 value is used in this context. Possibly dependent on some kind of
768 "boolean-checking" option like range checking. But it should probably
769 not depend on the language except insofar as is necessary to identify
770 a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
771 should be an error, probably). */
772 return !value_logical_not (val);
c8023e66
JG
773}
774\f
775/* Returns non-zero if the operator OP is defined on
776 the values ARG1 and ARG2. */
7ed0f002
JG
777
778#if 0 /* Currently unused */
779
c8023e66
JG
780void
781binop_type_check(arg1,arg2,op)
82a2edfb 782 value_ptr arg1,arg2;
c8023e66
JG
783 int op;
784{
785 struct type *t1, *t2;
786
787 /* If we're not checking types, always return success. */
788 if (!STRICT_TYPE)
789 return;
790
791 t1=VALUE_TYPE(arg1);
82a2edfb 792 if (arg2 != NULL)
c8023e66
JG
793 t2=VALUE_TYPE(arg2);
794 else
795 t2=NULL;
796
797 switch(op)
798 {
799 case BINOP_ADD:
800 case BINOP_SUB:
801 if ((numeric_type(t1) && pointer_type(t2)) ||
802 (pointer_type(t1) && numeric_type(t2)))
803 {
318bf84f 804 warning ("combining pointer and integer.\n");
c8023e66
JG
805 break;
806 }
807 case BINOP_MUL:
808 case BINOP_LSH:
809 case BINOP_RSH:
810 if (!numeric_type(t1) || !numeric_type(t2))
811 type_op_error ("Arguments to %s must be numbers.",op);
812 else if (!same_type(t1,t2))
813 type_op_error ("Arguments to %s must be of the same type.",op);
814 break;
815
e58de8a2
FF
816 case BINOP_LOGICAL_AND:
817 case BINOP_LOGICAL_OR:
c8023e66
JG
818 if (!boolean_type(t1) || !boolean_type(t2))
819 type_op_error ("Arguments to %s must be of boolean type.",op);
820 break;
821
822 case BINOP_EQUAL:
823 if ((pointer_type(t1) && !(pointer_type(t2) || integral_type(t2))) ||
824 (pointer_type(t2) && !(pointer_type(t1) || integral_type(t1))))
825 type_op_error ("A pointer can only be compared to an integer or pointer.",op);
826 else if ((pointer_type(t1) && integral_type(t2)) ||
827 (integral_type(t1) && pointer_type(t2)))
828 {
318bf84f 829 warning ("combining integer and pointer.\n");
c8023e66
JG
830 break;
831 }
832 else if (!simple_type(t1) || !simple_type(t2))
833 type_op_error ("Arguments to %s must be of simple type.",op);
834 else if (!same_type(t1,t2))
835 type_op_error ("Arguments to %s must be of the same type.",op);
836 break;
837
838 case BINOP_REM:
76a0ffb4 839 case BINOP_MOD:
c8023e66
JG
840 if (!integral_type(t1) || !integral_type(t2))
841 type_op_error ("Arguments to %s must be of integral type.",op);
842 break;
843
844 case BINOP_LESS:
845 case BINOP_GTR:
846 case BINOP_LEQ:
847 case BINOP_GEQ:
848 if (!ordered_type(t1) || !ordered_type(t2))
849 type_op_error ("Arguments to %s must be of ordered type.",op);
850 else if (!same_type(t1,t2))
851 type_op_error ("Arguments to %s must be of the same type.",op);
852 break;
853
854 case BINOP_ASSIGN:
855 if (pointer_type(t1) && !integral_type(t2))
856 type_op_error ("A pointer can only be assigned an integer.",op);
857 else if (pointer_type(t1) && integral_type(t2))
858 {
318bf84f 859 warning ("combining integer and pointer.");
c8023e66
JG
860 break;
861 }
862 else if (!simple_type(t1) || !simple_type(t2))
863 type_op_error ("Arguments to %s must be of simple type.",op);
864 else if (!same_type(t1,t2))
865 type_op_error ("Arguments to %s must be of the same type.",op);
866 break;
867
fcbadaee 868 case BINOP_CONCAT:
2fcc38b8
FF
869 /* FIXME: Needs to handle bitstrings as well. */
870 if (!(string_type(t1) || character_type(t1) || integral_type(t1))
871 || !(string_type(t2) || character_type(t2) || integral_type(t2)))
fcbadaee
FF
872 type_op_error ("Arguments to %s must be strings or characters.", op);
873 break;
874
c8023e66
JG
875 /* Unary checks -- arg2 is null */
876
e58de8a2 877 case UNOP_LOGICAL_NOT:
c8023e66
JG
878 if (!boolean_type(t1))
879 type_op_error ("Argument to %s must be of boolean type.",op);
880 break;
881
882 case UNOP_PLUS:
883 case UNOP_NEG:
884 if (!numeric_type(t1))
885 type_op_error ("Argument to %s must be of numeric type.",op);
886 break;
887
888 case UNOP_IND:
889 if (integral_type(t1))
890 {
318bf84f 891 warning ("combining pointer and integer.\n");
c8023e66
JG
892 break;
893 }
894 else if (!pointer_type(t1))
895 type_op_error ("Argument to %s must be a pointer.",op);
896 break;
897
898 case UNOP_PREINCREMENT:
899 case UNOP_POSTINCREMENT:
900 case UNOP_PREDECREMENT:
901 case UNOP_POSTDECREMENT:
902 if (!ordered_type(t1))
903 type_op_error ("Argument to %s must be of an ordered type.",op);
904 break;
905
906 default:
907 /* Ok. The following operators have different meanings in
908 different languages. */
909 switch(current_language->la_language)
910 {
911#ifdef _LANG_c
912 case language_c:
545af6ce 913 case language_cplus:
c8023e66
JG
914 switch(op)
915 {
916 case BINOP_DIV:
917 if (!numeric_type(t1) || !numeric_type(t2))
918 type_op_error ("Arguments to %s must be numbers.",op);
919 break;
920 }
921 break;
922#endif
923
924#ifdef _LANG_m2
925 case language_m2:
926 switch(op)
927 {
928 case BINOP_DIV:
929 if (!float_type(t1) || !float_type(t2))
930 type_op_error ("Arguments to %s must be floating point numbers.",op);
931 break;
932 case BINOP_INTDIV:
933 if (!integral_type(t1) || !integral_type(t2))
934 type_op_error ("Arguments to %s must be of integral type.",op);
935 break;
936 }
937#endif
e58de8a2
FF
938
939#ifdef _LANG_chill
940 case language_chill:
941 error ("Missing Chill support in function binop_type_check.");/*FIXME*/
942#endif
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
a0cf4681 987 if (type_check == type_check_warn)
e16b9023 988 fprintf_filtered (gdb_stderr, warning_pre_print);
c8023e66 989 else
a0cf4681 990 error_begin ();
c8023e66
JG
991
992 va_start (args);
993 string = va_arg (args, char *);
e16b9023
JK
994 vfprintf_filtered (gdb_stderr, string, args);
995 fprintf_filtered (gdb_stderr, "\n");
c8023e66 996 va_end (args);
a0cf4681
JK
997 if (type_check == type_check_on)
998 return_to_top_level (RETURN_ERROR);
c8023e66
JG
999}
1000
1001void
1002range_error (va_alist)
7ed0f002 1003 va_dcl
c8023e66
JG
1004{
1005 va_list args;
1006 char *string;
1007
a0cf4681 1008 if (range_check == range_check_warn)
e16b9023 1009 fprintf_filtered (gdb_stderr, warning_pre_print);
c8023e66 1010 else
a0cf4681 1011 error_begin ();
c8023e66
JG
1012
1013 va_start (args);
1014 string = va_arg (args, char *);
e16b9023
JK
1015 vfprintf_filtered (gdb_stderr, string, args);
1016 fprintf_filtered (gdb_stderr, "\n");
c8023e66 1017 va_end (args);
a0cf4681
JK
1018 if (range_check == range_check_on)
1019 return_to_top_level (RETURN_ERROR);
c8023e66
JG
1020}
1021
1022\f
1023/* This page contains miscellaneous functions */
1024
bf229b4e
FF
1025/* Return the language struct for a given language enum. */
1026
1027const struct language_defn *
1028language_def(lang)
1029 enum language lang;
1030{
1031 int i;
1032
1033 for (i = 0; i < languages_size; i++) {
1034 if (languages[i]->la_language == lang) {
1035 return languages[i];
1036 }
1037 }
1038 return NULL;
1039}
1040
c8023e66
JG
1041/* Return the language as a string */
1042char *
1043language_str(lang)
1044 enum language lang;
1045{
1046 int i;
1047
1048 for (i = 0; i < languages_size; i++) {
1049 if (languages[i]->la_language == lang) {
1050 return languages[i]->la_name;
1051 }
1052 }
1053 return "Unknown";
1054}
1055
c8023e66 1056static void
d8ce1326
JG
1057set_check (ignore, from_tty)
1058 char *ignore;
c8023e66
JG
1059 int from_tty;
1060{
199b2450 1061 printf_unfiltered(
c8023e66 1062"\"set check\" must be followed by the name of a check subcommand.\n");
199b2450 1063 help_list(setchecklist, "set check ", -1, gdb_stdout);
c8023e66
JG
1064}
1065
1066static void
be772100
JG
1067show_check (ignore, from_tty)
1068 char *ignore;
c8023e66
JG
1069 int from_tty;
1070{
1071 cmd_show_list(showchecklist, from_tty, "");
1072}
1073\f
1074/* Add a language to the set of known languages. */
1075
1076void
1077add_language (lang)
7ed0f002 1078 const struct language_defn *lang;
c8023e66
JG
1079{
1080 if (lang->la_magic != LANG_MAGIC)
1081 {
199b2450 1082 fprintf_unfiltered(gdb_stderr, "Magic number of %s language struct wrong\n",
c8023e66
JG
1083 lang->la_name);
1084 abort();
1085 }
1086
1087 if (!languages)
1088 {
1089 languages_allocsize = DEFAULT_ALLOCSIZE;
7ed0f002 1090 languages = (const struct language_defn **) xmalloc
c8023e66
JG
1091 (languages_allocsize * sizeof (*languages));
1092 }
1093 if (languages_size >= languages_allocsize)
1094 {
1095 languages_allocsize *= 2;
7ed0f002 1096 languages = (const struct language_defn **) xrealloc ((char *) languages,
c8023e66
JG
1097 languages_allocsize * sizeof (*languages));
1098 }
1099 languages[languages_size++] = lang;
c8023e66
JG
1100}
1101
1102/* Define the language that is no language. */
1103
7ed0f002 1104static int
c8023e66
JG
1105unk_lang_parser ()
1106{
1107 return 1;
1108}
1109
7ed0f002
JG
1110static void
1111unk_lang_error (msg)
1112 char *msg;
c8023e66
JG
1113{
1114 error ("Attempted to parse an expression with unknown language");
1115}
1116
5d074aa9
FF
1117static void
1118unk_lang_printchar (c, stream)
1119 register int c;
199b2450 1120 GDB_FILE *stream;
5d074aa9
FF
1121{
1122 error ("internal error - unimplemented function unk_lang_printchar called.");
1123}
1124
1125static void
1126unk_lang_printstr (stream, string, length, force_ellipses)
199b2450 1127 GDB_FILE *stream;
5d074aa9
FF
1128 char *string;
1129 unsigned int length;
1130 int force_ellipses;
1131{
1132 error ("internal error - unimplemented function unk_lang_printstr called.");
1133}
1134
bf229b4e
FF
1135static struct type *
1136unk_lang_create_fundamental_type (objfile, typeid)
1137 struct objfile *objfile;
1138 int typeid;
1139{
1140 error ("internal error - unimplemented function unk_lang_create_fundamental_type called.");
1141}
1142
a8a69e63
FF
1143void
1144unk_lang_print_type (type, varstring, stream, show, level)
1145 struct type *type;
1146 char *varstring;
199b2450 1147 GDB_FILE *stream;
a8a69e63
FF
1148 int show;
1149 int level;
1150{
1151 error ("internal error - unimplemented function unk_lang_print_type called.");
1152}
1153
1154int
1155unk_lang_val_print (type, valaddr, address, stream, format, deref_ref,
1156 recurse, pretty)
1157 struct type *type;
1158 char *valaddr;
1159 CORE_ADDR address;
199b2450 1160 GDB_FILE *stream;
a8a69e63
FF
1161 int format;
1162 int deref_ref;
1163 int recurse;
1164 enum val_prettyprint pretty;
1165{
1166 error ("internal error - unimplemented function unk_lang_val_print called.");
1167}
1168
e10cfcaa
PB
1169int
1170unk_lang_value_print (val, stream, format, pretty)
1171 value_ptr val;
1172 GDB_FILE *stream;
1173 int format;
1174 enum val_prettyprint pretty;
1175{
1176 error ("internal error - unimplemented function unk_lang_value_print called.");
1177}
1178
c8023e66 1179static struct type ** const (unknown_builtin_types[]) = { 0 };
a8a69e63 1180static const struct op_print unk_op_print_tab[] = {
8631194d 1181 {NULL, OP_NULL, PREC_NULL, 0}
a8a69e63 1182};
c8023e66
JG
1183
1184const struct language_defn unknown_language_defn = {
1185 "unknown",
1186 language_unknown,
1187 &unknown_builtin_types[0],
1188 range_check_off,
1189 type_check_off,
1190 unk_lang_parser,
1191 unk_lang_error,
5d074aa9
FF
1192 unk_lang_printchar, /* Print character constant */
1193 unk_lang_printstr,
bf229b4e 1194 unk_lang_create_fundamental_type,
a8a69e63
FF
1195 unk_lang_print_type, /* Print a type using appropriate syntax */
1196 unk_lang_val_print, /* Print a value using appropriate syntax */
e10cfcaa 1197 unk_lang_value_print, /* Print a top-level value */
2e66cf7d 1198 {"", "", "", ""}, /* Binary format info */
5573d7d4
JK
1199 {"0%lo", "0", "o", ""}, /* Octal format info */
1200 {"%ld", "", "d", ""}, /* Decimal format info */
1201 {"0x%lx", "0x", "x", ""}, /* Hex format info */
c8023e66 1202 unk_op_print_tab, /* expression operators for printing */
acc4efde 1203 1, /* c-style arrays */
c8023e66
JG
1204 LANG_MAGIC
1205};
1206
1207/* These two structs define fake entries for the "local" and "auto" options. */
1208const struct language_defn auto_language_defn = {
1209 "auto",
1210 language_auto,
1211 &unknown_builtin_types[0],
1212 range_check_off,
1213 type_check_off,
1214 unk_lang_parser,
1215 unk_lang_error,
5d074aa9
FF
1216 unk_lang_printchar, /* Print character constant */
1217 unk_lang_printstr,
bf229b4e 1218 unk_lang_create_fundamental_type,
a8a69e63
FF
1219 unk_lang_print_type, /* Print a type using appropriate syntax */
1220 unk_lang_val_print, /* Print a value using appropriate syntax */
e10cfcaa 1221 unk_lang_value_print, /* Print a top-level value */
2e66cf7d 1222 {"", "", "", ""}, /* Binary format info */
5573d7d4
JK
1223 {"0%lo", "0", "o", ""}, /* Octal format info */
1224 {"%ld", "", "d", ""}, /* Decimal format info */
1225 {"0x%lx", "0x", "x", ""}, /* Hex format info */
c8023e66 1226 unk_op_print_tab, /* expression operators for printing */
acc4efde 1227 1, /* c-style arrays */
c8023e66
JG
1228 LANG_MAGIC
1229};
1230
1231const struct language_defn local_language_defn = {
1232 "local",
1233 language_auto,
1234 &unknown_builtin_types[0],
1235 range_check_off,
1236 type_check_off,
1237 unk_lang_parser,
1238 unk_lang_error,
5d074aa9
FF
1239 unk_lang_printchar, /* Print character constant */
1240 unk_lang_printstr,
bf229b4e 1241 unk_lang_create_fundamental_type,
a8a69e63
FF
1242 unk_lang_print_type, /* Print a type using appropriate syntax */
1243 unk_lang_val_print, /* Print a value using appropriate syntax */
e10cfcaa 1244 unk_lang_value_print, /* Print a top-level value */
2e66cf7d 1245 {"", "", "", ""}, /* Binary format info */
5573d7d4
JK
1246 {"0%lo", "0", "o", ""}, /* Octal format info */
1247 {"%ld", "", "d", ""}, /* Decimal format info */
1248 {"0x%lx", "0x", "x", ""}, /* Hex format info */
c8023e66 1249 unk_op_print_tab, /* expression operators for printing */
acc4efde 1250 1, /* c-style arrays */
c8023e66
JG
1251 LANG_MAGIC
1252};
1253\f
1254/* Initialize the language routines */
1255
1256void
1257_initialize_language()
1258{
1259 struct cmd_list_element *set, *show;
1260
1261 /* GDB commands for language specific stuff */
1262
1263 set = add_set_cmd ("language", class_support, var_string_noescape,
1264 (char *)&language,
0b798409 1265 "Set the current source language.",
c8023e66
JG
1266 &setlist);
1267 show = add_show_from_set (set, &showlist);
1ab3bf1b
JG
1268 set->function.cfunc = set_language_command;
1269 show->function.cfunc = show_language_command;
c8023e66
JG
1270
1271 add_prefix_cmd ("check", no_class, set_check,
1272 "Set the status of the type/range checker",
1273 &setchecklist, "set check ", 0, &setlist);
1274 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1275 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1276
1277 add_prefix_cmd ("check", no_class, show_check,
1278 "Show the status of the type/range checker",
7cb83757 1279 &showchecklist, "show check ", 0, &showlist);
c8023e66
JG
1280 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1281 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1282
1283 set = add_set_cmd ("type", class_support, var_string_noescape,
1284 (char *)&type,
7cb83757 1285 "Set type checking. (on/warn/off/auto)",
c8023e66
JG
1286 &setchecklist);
1287 show = add_show_from_set (set, &showchecklist);
1ab3bf1b
JG
1288 set->function.cfunc = set_type_command;
1289 show->function.cfunc = show_type_command;
c8023e66
JG
1290
1291 set = add_set_cmd ("range", class_support, var_string_noescape,
1292 (char *)&range,
7cb83757 1293 "Set range checking. (on/warn/off/auto)",
c8023e66
JG
1294 &setchecklist);
1295 show = add_show_from_set (set, &showchecklist);
1ab3bf1b
JG
1296 set->function.cfunc = set_range_command;
1297 show->function.cfunc = show_range_command;
c8023e66
JG
1298
1299 add_language (&unknown_language_defn);
1300 add_language (&local_language_defn);
1301 add_language (&auto_language_defn);
1302
1303 language = savestring ("auto",strlen("auto"));
1304 range = savestring ("auto",strlen("auto"));
1305 type = savestring ("auto",strlen("auto"));
1306
1307 /* Have the above take effect */
1308
5f3d478e 1309 set_language_command (language, 0);
c8023e66
JG
1310 set_type_command (NULL, 0);
1311 set_range_command (NULL, 0);
1312}