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