4c59b285c3c52b8309eb04c7e5e8f95219e474d1
[deliverable/binutils-gdb.git] / gdb / language.c
1 /* Multiple source language support for GDB.
2
3 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5
6 Contributed by the Department of Computer Science at the State University
7 of New York at Buffalo.
8
9 This file is part of GDB.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA. */
25
26 /* This file contains functions that return things that are specific
27 to languages. Each function should examine current_language if necessary,
28 and return the appropriate result. */
29
30 /* FIXME: Most of these would be better organized as macros which
31 return data out of a "language-specific" struct pointer that is set
32 whenever the working language changes. That would be a lot faster. */
33
34 #include "defs.h"
35 #include <ctype.h>
36 #include "gdb_string.h"
37
38 #include "symtab.h"
39 #include "gdbtypes.h"
40 #include "value.h"
41 #include "gdbcmd.h"
42 #include "expression.h"
43 #include "language.h"
44 #include "target.h"
45 #include "parser-defs.h"
46 #include "jv-lang.h"
47 #include "demangle.h"
48
49 extern void _initialize_language (void);
50
51 static void show_language_command (char *, int);
52
53 static void set_language_command (char *, int);
54
55 static void show_type_command (char *, int);
56
57 static void set_type_command (char *, int);
58
59 static void show_range_command (char *, int);
60
61 static void set_range_command (char *, int);
62
63 static void show_case_command (char *, int);
64
65 static void set_case_command (char *, int);
66
67 static void set_case_str (void);
68
69 static void set_range_str (void);
70
71 static void set_type_str (void);
72
73 static void set_lang_str (void);
74
75 static void unk_lang_error (char *);
76
77 static int unk_lang_parser (void);
78
79 static void show_check (char *, int);
80
81 static void set_check (char *, int);
82
83 static void set_type_range_case (void);
84
85 static void unk_lang_emit_char (int c, struct ui_file *stream, int quoter);
86
87 static void unk_lang_printchar (int c, struct ui_file *stream);
88
89 static void unk_lang_printstr (struct ui_file * stream, char *string,
90 unsigned int length, int width,
91 int force_ellipses);
92
93 static struct type *unk_lang_create_fundamental_type (struct objfile *, int);
94
95 static void unk_lang_print_type (struct type *, char *, struct ui_file *,
96 int, int);
97
98 static int unk_lang_val_print (struct type *, char *, int, CORE_ADDR,
99 struct ui_file *, int, int, int,
100 enum val_prettyprint);
101
102 static int unk_lang_value_print (struct value *, struct ui_file *, int, enum val_prettyprint);
103
104 static CORE_ADDR unk_lang_trampoline (CORE_ADDR pc);
105
106 /* Forward declaration */
107 extern const struct language_defn unknown_language_defn;
108
109 /* The current (default at startup) state of type and range checking.
110 (If the modes are set to "auto", though, these are changed based
111 on the default language at startup, and then again based on the
112 language of the first source file. */
113
114 enum range_mode range_mode = range_mode_auto;
115 enum range_check range_check = range_check_off;
116 enum type_mode type_mode = type_mode_auto;
117 enum type_check type_check = type_check_off;
118 enum case_mode case_mode = case_mode_auto;
119 enum case_sensitivity case_sensitivity = case_sensitive_on;
120
121 /* The current language and language_mode (see language.h) */
122
123 const struct language_defn *current_language = &unknown_language_defn;
124 enum language_mode language_mode = language_mode_auto;
125
126 /* The language that the user expects to be typing in (the language
127 of main(), or the last language we notified them about, or C). */
128
129 const struct language_defn *expected_language;
130
131 /* The list of supported languages. The list itself is malloc'd. */
132
133 static const struct language_defn **languages;
134 static unsigned languages_size;
135 static unsigned languages_allocsize;
136 #define DEFAULT_ALLOCSIZE 4
137
138 /* The "set language/type/range" commands all put stuff in these
139 buffers. This is to make them work as set/show commands. The
140 user's string is copied here, then the set_* commands look at
141 them and update them to something that looks nice when it is
142 printed out. */
143
144 static char *language;
145 static char *type;
146 static char *range;
147 static char *case_sensitive;
148
149 /* Warning issued when current_language and the language of the current
150 frame do not match. */
151 char lang_frame_mismatch_warn[] =
152 "Warning: the current language does not match this frame.";
153 \f
154 /* This page contains the functions corresponding to GDB commands
155 and their helpers. */
156
157 /* Show command. Display a warning if the language set
158 does not match the frame. */
159 static void
160 show_language_command (char *ignore, int from_tty)
161 {
162 enum language flang; /* The language of the current frame */
163
164 flang = get_frame_language ();
165 if (flang != language_unknown &&
166 language_mode == language_mode_manual &&
167 current_language->la_language != flang)
168 printf_filtered ("%s\n", lang_frame_mismatch_warn);
169 }
170
171 /* Set command. Change the current working language. */
172 static void
173 set_language_command (char *ignore, int from_tty)
174 {
175 int i;
176 enum language flang;
177 char *err_lang;
178
179 if (!language || !language[0])
180 {
181 printf_unfiltered ("The currently understood settings are:\n\n");
182 printf_unfiltered ("local or auto Automatic setting based on source file\n");
183
184 for (i = 0; i < languages_size; ++i)
185 {
186 /* Already dealt with these above. */
187 if (languages[i]->la_language == language_unknown
188 || languages[i]->la_language == language_auto)
189 continue;
190
191 /* FIXME for now assume that the human-readable name is just
192 a capitalization of the internal name. */
193 printf_unfiltered ("%-16s Use the %c%s language\n",
194 languages[i]->la_name,
195 /* Capitalize first letter of language
196 name. */
197 toupper (languages[i]->la_name[0]),
198 languages[i]->la_name + 1);
199 }
200 /* Restore the silly string. */
201 set_language (current_language->la_language);
202 return;
203 }
204
205 /* Search the list of languages for a match. */
206 for (i = 0; i < languages_size; i++)
207 {
208 if (strcmp (languages[i]->la_name, language) == 0)
209 {
210 /* Found it! Go into manual mode, and use this language. */
211 if (languages[i]->la_language == language_auto)
212 {
213 /* Enter auto mode. Set to the current frame's language, if known. */
214 language_mode = language_mode_auto;
215 flang = get_frame_language ();
216 if (flang != language_unknown)
217 set_language (flang);
218 expected_language = current_language;
219 return;
220 }
221 else
222 {
223 /* Enter manual mode. Set the specified language. */
224 language_mode = language_mode_manual;
225 current_language = languages[i];
226 set_type_range_case ();
227 set_lang_str ();
228 expected_language = current_language;
229 return;
230 }
231 }
232 }
233
234 /* Reset the language (esp. the global string "language") to the
235 correct values. */
236 err_lang = savestring (language, strlen (language));
237 make_cleanup (xfree, err_lang); /* Free it after error */
238 set_language (current_language->la_language);
239 error ("Unknown language `%s'.", err_lang);
240 }
241
242 /* Show command. Display a warning if the type setting does
243 not match the current language. */
244 static void
245 show_type_command (char *ignore, int from_tty)
246 {
247 if (type_check != current_language->la_type_check)
248 printf_unfiltered (
249 "Warning: the current type check setting does not match the language.\n");
250 }
251
252 /* Set command. Change the setting for type checking. */
253 static void
254 set_type_command (char *ignore, int from_tty)
255 {
256 if (strcmp (type, "on") == 0)
257 {
258 type_check = type_check_on;
259 type_mode = type_mode_manual;
260 }
261 else if (strcmp (type, "warn") == 0)
262 {
263 type_check = type_check_warn;
264 type_mode = type_mode_manual;
265 }
266 else if (strcmp (type, "off") == 0)
267 {
268 type_check = type_check_off;
269 type_mode = type_mode_manual;
270 }
271 else if (strcmp (type, "auto") == 0)
272 {
273 type_mode = type_mode_auto;
274 set_type_range_case ();
275 /* Avoid hitting the set_type_str call below. We
276 did it in set_type_range_case. */
277 return;
278 }
279 else
280 {
281 warning ("Unrecognized type check setting: \"%s\"", type);
282 }
283 set_type_str ();
284 show_type_command ((char *) NULL, from_tty);
285 }
286
287 /* Show command. Display a warning if the range setting does
288 not match the current language. */
289 static void
290 show_range_command (char *ignore, int from_tty)
291 {
292
293 if (range_check != current_language->la_range_check)
294 printf_unfiltered (
295 "Warning: the current range check setting does not match the language.\n");
296 }
297
298 /* Set command. Change the setting for range checking. */
299 static void
300 set_range_command (char *ignore, int from_tty)
301 {
302 if (strcmp (range, "on") == 0)
303 {
304 range_check = range_check_on;
305 range_mode = range_mode_manual;
306 }
307 else if (strcmp (range, "warn") == 0)
308 {
309 range_check = range_check_warn;
310 range_mode = range_mode_manual;
311 }
312 else if (strcmp (range, "off") == 0)
313 {
314 range_check = range_check_off;
315 range_mode = range_mode_manual;
316 }
317 else if (strcmp (range, "auto") == 0)
318 {
319 range_mode = range_mode_auto;
320 set_type_range_case ();
321 /* Avoid hitting the set_range_str call below. We
322 did it in set_type_range_case. */
323 return;
324 }
325 else
326 {
327 warning ("Unrecognized range check setting: \"%s\"", range);
328 }
329 set_range_str ();
330 show_range_command ((char *) 0, from_tty);
331 }
332
333 /* Show command. Display a warning if the case sensitivity setting does
334 not match the current language. */
335 static void
336 show_case_command (char *ignore, int from_tty)
337 {
338 if (case_sensitivity != current_language->la_case_sensitivity)
339 printf_unfiltered(
340 "Warning: the current case sensitivity setting does not match the language.\n");
341 }
342
343 /* Set command. Change the setting for case sensitivity. */
344 static void
345 set_case_command (char *ignore, int from_tty)
346 {
347 if (DEPRECATED_STREQ (case_sensitive, "on"))
348 {
349 case_sensitivity = case_sensitive_on;
350 case_mode = case_mode_manual;
351 }
352 else if (DEPRECATED_STREQ (case_sensitive, "off"))
353 {
354 case_sensitivity = case_sensitive_off;
355 case_mode = case_mode_manual;
356 }
357 else if (DEPRECATED_STREQ (case_sensitive, "auto"))
358 {
359 case_mode = case_mode_auto;
360 set_type_range_case ();
361 /* Avoid hitting the set_case_str call below. We
362 did it in set_type_range_case. */
363 return;
364 }
365 else
366 {
367 warning ("Unrecognized case-sensitive setting: \"%s\"", case_sensitive);
368 }
369 set_case_str();
370 show_case_command ((char *) NULL, from_tty);
371 }
372
373 /* Set the status of range and type checking and case sensitivity based on
374 the current modes and the current language.
375 If SHOW is non-zero, then print out the current language,
376 type and range checking status. */
377 static void
378 set_type_range_case (void)
379 {
380
381 if (range_mode == range_mode_auto)
382 range_check = current_language->la_range_check;
383
384 if (type_mode == type_mode_auto)
385 type_check = current_language->la_type_check;
386
387 if (case_mode == case_mode_auto)
388 case_sensitivity = current_language->la_case_sensitivity;
389
390 set_type_str ();
391 set_range_str ();
392 set_case_str ();
393 }
394
395 /* Set current language to (enum language) LANG. Returns previous language. */
396
397 enum language
398 set_language (enum language lang)
399 {
400 int i;
401 enum language prev_language;
402
403 prev_language = current_language->la_language;
404
405 for (i = 0; i < languages_size; i++)
406 {
407 if (languages[i]->la_language == lang)
408 {
409 current_language = languages[i];
410 set_type_range_case ();
411 set_lang_str ();
412 break;
413 }
414 }
415
416 return prev_language;
417 }
418 \f
419 /* This page contains functions that update the global vars
420 language, type and range. */
421 static void
422 set_lang_str (void)
423 {
424 char *prefix = "";
425
426 if (language)
427 xfree (language);
428 if (language_mode == language_mode_auto)
429 prefix = "auto; currently ";
430
431 language = concat (prefix, current_language->la_name, NULL);
432 }
433
434 static void
435 set_type_str (void)
436 {
437 char *tmp = NULL, *prefix = "";
438
439 if (type)
440 xfree (type);
441 if (type_mode == type_mode_auto)
442 prefix = "auto; currently ";
443
444 switch (type_check)
445 {
446 case type_check_on:
447 tmp = "on";
448 break;
449 case type_check_off:
450 tmp = "off";
451 break;
452 case type_check_warn:
453 tmp = "warn";
454 break;
455 default:
456 error ("Unrecognized type check setting.");
457 }
458
459 type = concat (prefix, tmp, NULL);
460 }
461
462 static void
463 set_range_str (void)
464 {
465 char *tmp, *pref = "";
466
467 if (range_mode == range_mode_auto)
468 pref = "auto; currently ";
469
470 switch (range_check)
471 {
472 case range_check_on:
473 tmp = "on";
474 break;
475 case range_check_off:
476 tmp = "off";
477 break;
478 case range_check_warn:
479 tmp = "warn";
480 break;
481 default:
482 error ("Unrecognized range check setting.");
483 }
484
485 if (range)
486 xfree (range);
487 range = concat (pref, tmp, NULL);
488 }
489
490 static void
491 set_case_str (void)
492 {
493 char *tmp = NULL, *prefix = "";
494
495 if (case_mode==case_mode_auto)
496 prefix = "auto; currently ";
497
498 switch (case_sensitivity)
499 {
500 case case_sensitive_on:
501 tmp = "on";
502 break;
503 case case_sensitive_off:
504 tmp = "off";
505 break;
506 default:
507 error ("Unrecognized case-sensitive setting.");
508 }
509
510 xfree (case_sensitive);
511 case_sensitive = concat (prefix, tmp, NULL);
512 }
513
514 /* Print out the current language settings: language, range and
515 type checking. If QUIETLY, print only what has changed. */
516
517 void
518 language_info (int quietly)
519 {
520 if (quietly && expected_language == current_language)
521 return;
522
523 expected_language = current_language;
524 printf_unfiltered ("Current language: %s\n", language);
525 show_language_command ((char *) 0, 1);
526
527 if (!quietly)
528 {
529 printf_unfiltered ("Type checking: %s\n", type);
530 show_type_command ((char *) 0, 1);
531 printf_unfiltered ("Range checking: %s\n", range);
532 show_range_command ((char *) 0, 1);
533 printf_unfiltered ("Case sensitivity: %s\n", case_sensitive);
534 show_case_command ((char *) 0, 1);
535 }
536 }
537 \f
538 /* Return the result of a binary operation. */
539
540 #if 0 /* Currently unused */
541
542 struct type *
543 binop_result_type (struct value *v1, struct value *v2)
544 {
545 int size, uns;
546 struct type *t1 = check_typedef (VALUE_TYPE (v1));
547 struct type *t2 = check_typedef (VALUE_TYPE (v2));
548
549 int l1 = TYPE_LENGTH (t1);
550 int l2 = TYPE_LENGTH (t2);
551
552 switch (current_language->la_language)
553 {
554 case language_c:
555 case language_cplus:
556 case language_objc:
557 if (TYPE_CODE (t1) == TYPE_CODE_FLT)
558 return TYPE_CODE (t2) == TYPE_CODE_FLT && l2 > l1 ?
559 VALUE_TYPE (v2) : VALUE_TYPE (v1);
560 else if (TYPE_CODE (t2) == TYPE_CODE_FLT)
561 return TYPE_CODE (t1) == TYPE_CODE_FLT && l1 > l2 ?
562 VALUE_TYPE (v1) : VALUE_TYPE (v2);
563 else if (TYPE_UNSIGNED (t1) && l1 > l2)
564 return VALUE_TYPE (v1);
565 else if (TYPE_UNSIGNED (t2) && l2 > l1)
566 return VALUE_TYPE (v2);
567 else /* Both are signed. Result is the longer type */
568 return l1 > l2 ? VALUE_TYPE (v1) : VALUE_TYPE (v2);
569 break;
570 case language_m2:
571 /* If we are doing type-checking, l1 should equal l2, so this is
572 not needed. */
573 return l1 > l2 ? VALUE_TYPE (v1) : VALUE_TYPE (v2);
574 break;
575 }
576 internal_error (__FILE__, __LINE__, "failed internal consistency check");
577 return (struct type *) 0; /* For lint */
578 }
579
580 #endif /* 0 */
581 \f
582
583 /* This page contains functions that return format strings for
584 printf for printing out numbers in different formats */
585
586 #define MAX_NUM_STRING_LEN 50
587
588 /* Converts a LONGEST to a C-format hexadecimal literal and stores it in
589 a static string. Returns a pointer to this string. */
590 char *
591 hex_string (LONGEST num)
592 {
593 static char result[MAX_NUM_STRING_LEN];
594 sprintf (result, "0x%s", phex_nz (num, sizeof (num)));
595 return result;
596 }
597
598 /* Converts a LONGEST number to a C-format hexadecimal literal and stores
599 it in a static string. Returns a pointer to this string that is
600 valid until the next call. The number is padded on the left with
601 0s to at least WIDTH characters. */
602
603 char *
604 hex_string_custom (LONGEST num, int width)
605 {
606 static char result[MAX_NUM_STRING_LEN];
607 char *result_end = result + MAX_NUM_STRING_LEN - 1;
608 const char* hex = phex_nz (num, sizeof (num));
609 int hex_len = strlen (hex);
610
611 if (hex_len > width)
612 width = hex_len;
613 if (width + 2 >= MAX_NUM_STRING_LEN)
614 internal_error (__FILE__, __LINE__,
615 "hex_string_custom: insufficient space to store result");
616
617 strcpy (result_end - width - 2, "0x");
618 memset (result_end - width, '0', width);
619 strcpy (result_end - hex_len, hex);
620 return result_end - width - 2;
621 }
622 \f
623 #if 0
624 /* This page contains functions that are used in type/range checking.
625 They all return zero if the type/range check fails.
626
627 It is hoped that these will make extending GDB to parse different
628 languages a little easier. These are primarily used in eval.c when
629 evaluating expressions and making sure that their types are correct.
630 Instead of having a mess of conjucted/disjuncted expressions in an "if",
631 the ideas of type can be wrapped up in the following functions.
632
633 Note that some of them are not currently dependent upon which language
634 is currently being parsed. For example, floats are the same in
635 C and Modula-2 (ie. the only floating point type has TYPE_CODE of
636 TYPE_CODE_FLT), while booleans are different. */
637
638 /* Returns non-zero if its argument is a simple type. This is the same for
639 both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur,
640 and thus will never cause the failure of the test. */
641 int
642 simple_type (struct type *type)
643 {
644 CHECK_TYPEDEF (type);
645 switch (TYPE_CODE (type))
646 {
647 case TYPE_CODE_INT:
648 case TYPE_CODE_CHAR:
649 case TYPE_CODE_ENUM:
650 case TYPE_CODE_FLT:
651 case TYPE_CODE_RANGE:
652 case TYPE_CODE_BOOL:
653 return 1;
654
655 default:
656 return 0;
657 }
658 }
659
660 /* Returns non-zero if its argument is of an ordered type.
661 An ordered type is one in which the elements can be tested for the
662 properties of "greater than", "less than", etc, or for which the
663 operations "increment" or "decrement" make sense. */
664 int
665 ordered_type (struct type *type)
666 {
667 CHECK_TYPEDEF (type);
668 switch (TYPE_CODE (type))
669 {
670 case TYPE_CODE_INT:
671 case TYPE_CODE_CHAR:
672 case TYPE_CODE_ENUM:
673 case TYPE_CODE_FLT:
674 case TYPE_CODE_RANGE:
675 return 1;
676
677 default:
678 return 0;
679 }
680 }
681
682 /* Returns non-zero if the two types are the same */
683 int
684 same_type (struct type *arg1, struct type *arg2)
685 {
686 CHECK_TYPEDEF (type);
687 if (structured_type (arg1) ? !structured_type (arg2) : structured_type (arg2))
688 /* One is structured and one isn't */
689 return 0;
690 else if (structured_type (arg1) && structured_type (arg2))
691 return arg1 == arg2;
692 else if (numeric_type (arg1) && numeric_type (arg2))
693 return (TYPE_CODE (arg2) == TYPE_CODE (arg1)) &&
694 (TYPE_UNSIGNED (arg1) == TYPE_UNSIGNED (arg2))
695 ? 1 : 0;
696 else
697 return arg1 == arg2;
698 }
699
700 /* Returns non-zero if the type is integral */
701 int
702 integral_type (struct type *type)
703 {
704 CHECK_TYPEDEF (type);
705 switch (current_language->la_language)
706 {
707 case language_c:
708 case language_cplus:
709 case language_objc:
710 return (TYPE_CODE (type) != TYPE_CODE_INT) &&
711 (TYPE_CODE (type) != TYPE_CODE_ENUM) ? 0 : 1;
712 case language_m2:
713 case language_pascal:
714 return TYPE_CODE (type) != TYPE_CODE_INT ? 0 : 1;
715 default:
716 error ("Language not supported.");
717 }
718 }
719
720 /* Returns non-zero if the value is numeric */
721 int
722 numeric_type (struct type *type)
723 {
724 CHECK_TYPEDEF (type);
725 switch (TYPE_CODE (type))
726 {
727 case TYPE_CODE_INT:
728 case TYPE_CODE_FLT:
729 return 1;
730
731 default:
732 return 0;
733 }
734 }
735
736 /* Returns non-zero if the value is a character type */
737 int
738 character_type (struct type *type)
739 {
740 CHECK_TYPEDEF (type);
741 switch (current_language->la_language)
742 {
743 case language_m2:
744 case language_pascal:
745 return TYPE_CODE (type) != TYPE_CODE_CHAR ? 0 : 1;
746
747 case language_c:
748 case language_cplus:
749 case language_objc:
750 return (TYPE_CODE (type) == TYPE_CODE_INT) &&
751 TYPE_LENGTH (type) == sizeof (char)
752 ? 1 : 0;
753 default:
754 return (0);
755 }
756 }
757
758 /* Returns non-zero if the value is a string type */
759 int
760 string_type (struct type *type)
761 {
762 CHECK_TYPEDEF (type);
763 switch (current_language->la_language)
764 {
765 case language_m2:
766 case language_pascal:
767 return TYPE_CODE (type) != TYPE_CODE_STRING ? 0 : 1;
768
769 case language_c:
770 case language_cplus:
771 case language_objc:
772 /* C does not have distinct string type. */
773 return (0);
774 default:
775 return (0);
776 }
777 }
778
779 /* Returns non-zero if the value is a boolean type */
780 int
781 boolean_type (struct type *type)
782 {
783 CHECK_TYPEDEF (type);
784 if (TYPE_CODE (type) == TYPE_CODE_BOOL)
785 return 1;
786 switch (current_language->la_language)
787 {
788 case language_c:
789 case language_cplus:
790 case language_objc:
791 /* Might be more cleanly handled by having a
792 TYPE_CODE_INT_NOT_BOOL for (the deleted) CHILL and such
793 languages, or a TYPE_CODE_INT_OR_BOOL for C. */
794 if (TYPE_CODE (type) == TYPE_CODE_INT)
795 return 1;
796 default:
797 break;
798 }
799 return 0;
800 }
801
802 /* Returns non-zero if the value is a floating-point type */
803 int
804 float_type (struct type *type)
805 {
806 CHECK_TYPEDEF (type);
807 return TYPE_CODE (type) == TYPE_CODE_FLT;
808 }
809
810 /* Returns non-zero if the value is a pointer type */
811 int
812 pointer_type (struct type *type)
813 {
814 return TYPE_CODE (type) == TYPE_CODE_PTR ||
815 TYPE_CODE (type) == TYPE_CODE_REF;
816 }
817
818 /* Returns non-zero if the value is a structured type */
819 int
820 structured_type (struct type *type)
821 {
822 CHECK_TYPEDEF (type);
823 switch (current_language->la_language)
824 {
825 case language_c:
826 case language_cplus:
827 case language_objc:
828 return (TYPE_CODE (type) == TYPE_CODE_STRUCT) ||
829 (TYPE_CODE (type) == TYPE_CODE_UNION) ||
830 (TYPE_CODE (type) == TYPE_CODE_ARRAY);
831 case language_pascal:
832 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
833 (TYPE_CODE(type) == TYPE_CODE_UNION) ||
834 (TYPE_CODE(type) == TYPE_CODE_SET) ||
835 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
836 case language_m2:
837 return (TYPE_CODE (type) == TYPE_CODE_STRUCT) ||
838 (TYPE_CODE (type) == TYPE_CODE_SET) ||
839 (TYPE_CODE (type) == TYPE_CODE_ARRAY);
840 default:
841 return (0);
842 }
843 }
844 #endif
845 \f
846 struct type *
847 lang_bool_type (void)
848 {
849 struct symbol *sym;
850 struct type *type;
851 switch (current_language->la_language)
852 {
853 case language_fortran:
854 sym = lookup_symbol ("logical", NULL, VAR_DOMAIN, NULL, NULL);
855 if (sym)
856 {
857 type = SYMBOL_TYPE (sym);
858 if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
859 return type;
860 }
861 return builtin_type_f_logical_s2;
862 case language_cplus:
863 case language_pascal:
864 if (current_language->la_language==language_cplus)
865 {sym = lookup_symbol ("bool", NULL, VAR_DOMAIN, NULL, NULL);}
866 else
867 {sym = lookup_symbol ("boolean", NULL, VAR_DOMAIN, NULL, NULL);}
868 if (sym)
869 {
870 type = SYMBOL_TYPE (sym);
871 if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
872 return type;
873 }
874 return builtin_type_bool;
875 case language_java:
876 sym = lookup_symbol ("boolean", NULL, VAR_DOMAIN, NULL, NULL);
877 if (sym)
878 {
879 type = SYMBOL_TYPE (sym);
880 if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
881 return type;
882 }
883 return java_boolean_type;
884 default:
885 return builtin_type_int;
886 }
887 }
888 \f
889 /* This page contains functions that return info about
890 (struct value) values used in GDB. */
891
892 /* Returns non-zero if the value VAL represents a true value. */
893 int
894 value_true (struct value *val)
895 {
896 /* It is possible that we should have some sort of error if a non-boolean
897 value is used in this context. Possibly dependent on some kind of
898 "boolean-checking" option like range checking. But it should probably
899 not depend on the language except insofar as is necessary to identify
900 a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
901 should be an error, probably). */
902 return !value_logical_not (val);
903 }
904 \f
905 /* This page contains functions for the printing out of
906 error messages that occur during type- and range-
907 checking. */
908
909 /* These are called when a language fails a type- or range-check. The
910 first argument should be a printf()-style format string, and the
911 rest of the arguments should be its arguments. If
912 [type|range]_check is [type|range]_check_on, an error is printed;
913 if [type|range]_check_warn, a warning; otherwise just the
914 message. */
915
916 void
917 type_error (const char *string,...)
918 {
919 va_list args;
920 va_start (args, string);
921
922 switch (type_check)
923 {
924 case type_check_warn:
925 vwarning (string, args);
926 break;
927 case type_check_on:
928 verror (string, args);
929 break;
930 case type_check_off:
931 /* FIXME: cagney/2002-01-30: Should this function print anything
932 when type error is off? */
933 vfprintf_filtered (gdb_stderr, string, args);
934 fprintf_filtered (gdb_stderr, "\n");
935 break;
936 default:
937 internal_error (__FILE__, __LINE__, "bad switch");
938 }
939 va_end (args);
940 }
941
942 void
943 range_error (const char *string,...)
944 {
945 va_list args;
946 va_start (args, string);
947
948 switch (range_check)
949 {
950 case range_check_warn:
951 vwarning (string, args);
952 break;
953 case range_check_on:
954 verror (string, args);
955 break;
956 case range_check_off:
957 /* FIXME: cagney/2002-01-30: Should this function print anything
958 when range error is off? */
959 vfprintf_filtered (gdb_stderr, string, args);
960 fprintf_filtered (gdb_stderr, "\n");
961 break;
962 default:
963 internal_error (__FILE__, __LINE__, "bad switch");
964 }
965 va_end (args);
966 }
967 \f
968
969 /* This page contains miscellaneous functions */
970
971 /* Return the language enum for a given language string. */
972
973 enum language
974 language_enum (char *str)
975 {
976 int i;
977
978 for (i = 0; i < languages_size; i++)
979 if (DEPRECATED_STREQ (languages[i]->la_name, str))
980 return languages[i]->la_language;
981
982 return language_unknown;
983 }
984
985 /* Return the language struct for a given language enum. */
986
987 const struct language_defn *
988 language_def (enum language lang)
989 {
990 int i;
991
992 for (i = 0; i < languages_size; i++)
993 {
994 if (languages[i]->la_language == lang)
995 {
996 return languages[i];
997 }
998 }
999 return NULL;
1000 }
1001
1002 /* Return the language as a string */
1003 char *
1004 language_str (enum language lang)
1005 {
1006 int i;
1007
1008 for (i = 0; i < languages_size; i++)
1009 {
1010 if (languages[i]->la_language == lang)
1011 {
1012 return languages[i]->la_name;
1013 }
1014 }
1015 return "Unknown";
1016 }
1017
1018 static void
1019 set_check (char *ignore, int from_tty)
1020 {
1021 printf_unfiltered (
1022 "\"set check\" must be followed by the name of a check subcommand.\n");
1023 help_list (setchecklist, "set check ", -1, gdb_stdout);
1024 }
1025
1026 static void
1027 show_check (char *ignore, int from_tty)
1028 {
1029 cmd_show_list (showchecklist, from_tty, "");
1030 }
1031 \f
1032 /* Add a language to the set of known languages. */
1033
1034 void
1035 add_language (const struct language_defn *lang)
1036 {
1037 if (lang->la_magic != LANG_MAGIC)
1038 {
1039 fprintf_unfiltered (gdb_stderr, "Magic number of %s language struct wrong\n",
1040 lang->la_name);
1041 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1042 }
1043
1044 if (!languages)
1045 {
1046 languages_allocsize = DEFAULT_ALLOCSIZE;
1047 languages = (const struct language_defn **) xmalloc
1048 (languages_allocsize * sizeof (*languages));
1049 }
1050 if (languages_size >= languages_allocsize)
1051 {
1052 languages_allocsize *= 2;
1053 languages = (const struct language_defn **) xrealloc ((char *) languages,
1054 languages_allocsize * sizeof (*languages));
1055 }
1056 languages[languages_size++] = lang;
1057 }
1058
1059 /* Iterate through all registered languages looking for and calling
1060 any non-NULL struct language_defn.skip_trampoline() functions.
1061 Return the result from the first that returns non-zero, or 0 if all
1062 `fail'. */
1063 CORE_ADDR
1064 skip_language_trampoline (CORE_ADDR pc)
1065 {
1066 int i;
1067
1068 for (i = 0; i < languages_size; i++)
1069 {
1070 if (languages[i]->skip_trampoline)
1071 {
1072 CORE_ADDR real_pc = (languages[i]->skip_trampoline) (pc);
1073 if (real_pc)
1074 return real_pc;
1075 }
1076 }
1077
1078 return 0;
1079 }
1080
1081 /* Return demangled language symbol, or NULL.
1082 FIXME: Options are only useful for certain languages and ignored
1083 by others, so it would be better to remove them here and have a
1084 more flexible demangler for the languages that need it.
1085 FIXME: Sometimes the demangler is invoked when we don't know the
1086 language, so we can't use this everywhere. */
1087 char *
1088 language_demangle (const struct language_defn *current_language,
1089 const char *mangled, int options)
1090 {
1091 if (current_language != NULL && current_language->la_demangle)
1092 return current_language->la_demangle (mangled, options);
1093 return NULL;
1094 }
1095
1096 /* Return class name from physname or NULL. */
1097 char *
1098 language_class_name_from_physname (const struct language_defn *current_language,
1099 const char *physname)
1100 {
1101 if (current_language != NULL && current_language->la_class_name_from_physname)
1102 return current_language->la_class_name_from_physname (physname);
1103 return NULL;
1104 }
1105
1106 /* Return the default string containing the list of characters
1107 delimiting words. This is a reasonable default value that
1108 most languages should be able to use. */
1109
1110 char *
1111 default_word_break_characters (void)
1112 {
1113 return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1114 }
1115
1116 /* Define the language that is no language. */
1117
1118 static int
1119 unk_lang_parser (void)
1120 {
1121 return 1;
1122 }
1123
1124 static void
1125 unk_lang_error (char *msg)
1126 {
1127 error ("Attempted to parse an expression with unknown language");
1128 }
1129
1130 static void
1131 unk_lang_emit_char (int c, struct ui_file *stream, int quoter)
1132 {
1133 error ("internal error - unimplemented function unk_lang_emit_char called.");
1134 }
1135
1136 static void
1137 unk_lang_printchar (int c, struct ui_file *stream)
1138 {
1139 error ("internal error - unimplemented function unk_lang_printchar called.");
1140 }
1141
1142 static void
1143 unk_lang_printstr (struct ui_file *stream, char *string, unsigned int length,
1144 int width, int force_ellipses)
1145 {
1146 error ("internal error - unimplemented function unk_lang_printstr called.");
1147 }
1148
1149 static struct type *
1150 unk_lang_create_fundamental_type (struct objfile *objfile, int typeid)
1151 {
1152 error ("internal error - unimplemented function unk_lang_create_fundamental_type called.");
1153 }
1154
1155 static void
1156 unk_lang_print_type (struct type *type, char *varstring, struct ui_file *stream,
1157 int show, int level)
1158 {
1159 error ("internal error - unimplemented function unk_lang_print_type called.");
1160 }
1161
1162 static int
1163 unk_lang_val_print (struct type *type, char *valaddr, int embedded_offset,
1164 CORE_ADDR address, struct ui_file *stream, int format,
1165 int deref_ref, int recurse, enum val_prettyprint pretty)
1166 {
1167 error ("internal error - unimplemented function unk_lang_val_print called.");
1168 }
1169
1170 static int
1171 unk_lang_value_print (struct value *val, struct ui_file *stream, int format,
1172 enum val_prettyprint pretty)
1173 {
1174 error ("internal error - unimplemented function unk_lang_value_print called.");
1175 }
1176
1177 static CORE_ADDR unk_lang_trampoline (CORE_ADDR pc)
1178 {
1179 return 0;
1180 }
1181
1182 /* Unknown languages just use the cplus demangler. */
1183 static char *unk_lang_demangle (const char *mangled, int options)
1184 {
1185 return cplus_demangle (mangled, options);
1186 }
1187
1188 static char *unk_lang_class_name (const char *mangled)
1189 {
1190 return NULL;
1191 }
1192
1193 static const struct op_print unk_op_print_tab[] =
1194 {
1195 {NULL, OP_NULL, PREC_NULL, 0}
1196 };
1197
1198 static void
1199 unknown_language_arch_info (struct gdbarch *gdbarch,
1200 struct language_arch_info *lai)
1201 {
1202 lai->string_char_type = builtin_type (gdbarch)->builtin_char;
1203 lai->primitive_type_vector = GDBARCH_OBSTACK_CALLOC (gdbarch, 1,
1204 struct type *);
1205 }
1206
1207 const struct language_defn unknown_language_defn =
1208 {
1209 "unknown",
1210 language_unknown,
1211 NULL,
1212 range_check_off,
1213 type_check_off,
1214 array_row_major,
1215 case_sensitive_on,
1216 &exp_descriptor_standard,
1217 unk_lang_parser,
1218 unk_lang_error,
1219 null_post_parser,
1220 unk_lang_printchar, /* Print character constant */
1221 unk_lang_printstr,
1222 unk_lang_emit_char,
1223 unk_lang_create_fundamental_type,
1224 unk_lang_print_type, /* Print a type using appropriate syntax */
1225 unk_lang_val_print, /* Print a value using appropriate syntax */
1226 unk_lang_value_print, /* Print a top-level value */
1227 unk_lang_trampoline, /* Language specific skip_trampoline */
1228 value_of_this, /* value_of_this */
1229 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1230 basic_lookup_transparent_type,/* lookup_transparent_type */
1231 unk_lang_demangle, /* Language specific symbol demangler */
1232 unk_lang_class_name, /* Language specific class_name_from_physname */
1233 unk_op_print_tab, /* expression operators for printing */
1234 1, /* c-style arrays */
1235 0, /* String lower bound */
1236 NULL,
1237 default_word_break_characters,
1238 unknown_language_arch_info, /* la_language_arch_info. */
1239 LANG_MAGIC
1240 };
1241
1242 /* These two structs define fake entries for the "local" and "auto" options. */
1243 const struct language_defn auto_language_defn =
1244 {
1245 "auto",
1246 language_auto,
1247 NULL,
1248 range_check_off,
1249 type_check_off,
1250 array_row_major,
1251 case_sensitive_on,
1252 &exp_descriptor_standard,
1253 unk_lang_parser,
1254 unk_lang_error,
1255 null_post_parser,
1256 unk_lang_printchar, /* Print character constant */
1257 unk_lang_printstr,
1258 unk_lang_emit_char,
1259 unk_lang_create_fundamental_type,
1260 unk_lang_print_type, /* Print a type using appropriate syntax */
1261 unk_lang_val_print, /* Print a value using appropriate syntax */
1262 unk_lang_value_print, /* Print a top-level value */
1263 unk_lang_trampoline, /* Language specific skip_trampoline */
1264 value_of_this, /* value_of_this */
1265 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1266 basic_lookup_transparent_type,/* lookup_transparent_type */
1267 unk_lang_demangle, /* Language specific symbol demangler */
1268 unk_lang_class_name, /* Language specific class_name_from_physname */
1269 unk_op_print_tab, /* expression operators for printing */
1270 1, /* c-style arrays */
1271 0, /* String lower bound */
1272 NULL,
1273 default_word_break_characters,
1274 unknown_language_arch_info, /* la_language_arch_info. */
1275 LANG_MAGIC
1276 };
1277
1278 const struct language_defn local_language_defn =
1279 {
1280 "local",
1281 language_auto,
1282 NULL,
1283 range_check_off,
1284 type_check_off,
1285 case_sensitive_on,
1286 array_row_major,
1287 &exp_descriptor_standard,
1288 unk_lang_parser,
1289 unk_lang_error,
1290 null_post_parser,
1291 unk_lang_printchar, /* Print character constant */
1292 unk_lang_printstr,
1293 unk_lang_emit_char,
1294 unk_lang_create_fundamental_type,
1295 unk_lang_print_type, /* Print a type using appropriate syntax */
1296 unk_lang_val_print, /* Print a value using appropriate syntax */
1297 unk_lang_value_print, /* Print a top-level value */
1298 unk_lang_trampoline, /* Language specific skip_trampoline */
1299 value_of_this, /* value_of_this */
1300 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1301 basic_lookup_transparent_type,/* lookup_transparent_type */
1302 unk_lang_demangle, /* Language specific symbol demangler */
1303 unk_lang_class_name, /* Language specific class_name_from_physname */
1304 unk_op_print_tab, /* expression operators for printing */
1305 1, /* c-style arrays */
1306 0, /* String lower bound */
1307 NULL,
1308 default_word_break_characters,
1309 unknown_language_arch_info, /* la_language_arch_info. */
1310 LANG_MAGIC
1311 };
1312 \f
1313 /* Per-architecture language information. */
1314
1315 static struct gdbarch_data *language_gdbarch_data;
1316
1317 struct language_gdbarch
1318 {
1319 /* A vector of per-language per-architecture info. Indexed by "enum
1320 language". */
1321 struct language_arch_info arch_info[nr_languages];
1322 };
1323
1324 static void *
1325 language_gdbarch_post_init (struct gdbarch *gdbarch)
1326 {
1327 struct language_gdbarch *l;
1328 int i;
1329
1330 l = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct language_gdbarch);
1331 for (i = 0; i <= languages_size; i++)
1332 {
1333 if (languages[i] != NULL
1334 && languages[i]->la_language_arch_info != NULL)
1335 languages[i]->la_language_arch_info
1336 (gdbarch, l->arch_info + languages[i]->la_language);
1337 }
1338 return l;
1339 }
1340
1341 struct type *
1342 language_string_char_type (const struct language_defn *la,
1343 struct gdbarch *gdbarch)
1344 {
1345 struct language_gdbarch *ld = gdbarch_data (gdbarch,
1346 language_gdbarch_data);
1347 if (ld->arch_info[la->la_language].string_char_type != NULL)
1348 return ld->arch_info[la->la_language].string_char_type;
1349 else
1350 return (*la->string_char_type);
1351 }
1352
1353 struct type *
1354 language_lookup_primitive_type_by_name (const struct language_defn *la,
1355 struct gdbarch *gdbarch,
1356 const char *name)
1357 {
1358 struct language_gdbarch *ld = gdbarch_data (gdbarch,
1359 language_gdbarch_data);
1360 if (ld->arch_info[la->la_language].primitive_type_vector != NULL)
1361 {
1362 struct type *const *p;
1363 for (p = ld->arch_info[la->la_language].primitive_type_vector;
1364 (*p) != NULL;
1365 p++)
1366 {
1367 if (strcmp (TYPE_NAME (*p), name) == 0)
1368 return (*p);
1369 }
1370 }
1371 else
1372 {
1373 struct type **const *p;
1374 for (p = current_language->la_builtin_type_vector; *p != NULL; p++)
1375 {
1376 if (strcmp (TYPE_NAME (**p), name) == 0)
1377 return (**p);
1378 }
1379 }
1380 return (NULL);
1381 }
1382
1383 /* Initialize the language routines */
1384
1385 void
1386 _initialize_language (void)
1387 {
1388 struct cmd_list_element *set, *show;
1389
1390 language_gdbarch_data
1391 = gdbarch_data_register_post_init (language_gdbarch_post_init);
1392
1393 /* GDB commands for language specific stuff */
1394
1395 set = add_set_cmd ("language", class_support, var_string_noescape,
1396 (char *) &language,
1397 "Set the current source language.",
1398 &setlist);
1399 show = deprecated_add_show_from_set (set, &showlist);
1400 set_cmd_cfunc (set, set_language_command);
1401 set_cmd_cfunc (show, show_language_command);
1402
1403 add_prefix_cmd ("check", no_class, set_check,
1404 "Set the status of the type/range checker.",
1405 &setchecklist, "set check ", 0, &setlist);
1406 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1407 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1408
1409 add_prefix_cmd ("check", no_class, show_check,
1410 "Show the status of the type/range checker.",
1411 &showchecklist, "show check ", 0, &showlist);
1412 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1413 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1414
1415 set = add_set_cmd ("type", class_support, var_string_noescape,
1416 (char *) &type,
1417 "Set type checking. (on/warn/off/auto)",
1418 &setchecklist);
1419 show = deprecated_add_show_from_set (set, &showchecklist);
1420 set_cmd_cfunc (set, set_type_command);
1421 set_cmd_cfunc (show, show_type_command);
1422
1423 set = add_set_cmd ("range", class_support, var_string_noescape,
1424 (char *) &range,
1425 "Set range checking. (on/warn/off/auto)",
1426 &setchecklist);
1427 show = deprecated_add_show_from_set (set, &showchecklist);
1428 set_cmd_cfunc (set, set_range_command);
1429 set_cmd_cfunc (show, show_range_command);
1430
1431 set = add_set_cmd ("case-sensitive", class_support, var_string_noescape,
1432 (char *) &case_sensitive,
1433 "Set case sensitivity in name search. (on/off/auto)\n\
1434 For Fortran the default is off; for other languages the default is on.",
1435 &setlist);
1436 show = deprecated_add_show_from_set (set, &showlist);
1437 set_cmd_cfunc (set, set_case_command);
1438 set_cmd_cfunc (show, show_case_command);
1439
1440 add_language (&unknown_language_defn);
1441 add_language (&local_language_defn);
1442 add_language (&auto_language_defn);
1443
1444 language = savestring ("auto", strlen ("auto"));
1445 type = savestring ("auto", strlen ("auto"));
1446 range = savestring ("auto", strlen ("auto"));
1447 case_sensitive = savestring ("auto",strlen ("auto"));
1448
1449 /* Have the above take effect */
1450 set_language (language_auto);
1451 }
This page took 0.067425 seconds and 4 git commands to generate.