Work around GCC 6.3.1 bug
[deliverable/binutils-gdb.git] / gdb / language.c
CommitLineData
c906108c 1/* Multiple source language support for GDB.
1bac305b 2
61baf725 3 Copyright (C) 1991-2017 Free Software Foundation, Inc.
1bac305b 4
c906108c
SS
5 Contributed by the Department of Computer Science at the State University
6 of New York at Buffalo.
7
c5aa993b 8 This file is part of GDB.
c906108c 9
c5aa993b
JM
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
c5aa993b 13 (at your option) any later version.
c906108c 14
c5aa993b
JM
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
c906108c 19
c5aa993b 20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
22
23/* This file contains functions that return things that are specific
24 to languages. Each function should examine current_language if necessary,
1777feb0 25 and return the appropriate result. */
c906108c
SS
26
27/* FIXME: Most of these would be better organized as macros which
28 return data out of a "language-specific" struct pointer that is set
29 whenever the working language changes. That would be a lot faster. */
30
31#include "defs.h"
32#include <ctype.h>
c906108c
SS
33#include "symtab.h"
34#include "gdbtypes.h"
35#include "value.h"
36#include "gdbcmd.h"
c906108c
SS
37#include "expression.h"
38#include "language.h"
a53b64ea 39#include "varobj.h"
c906108c
SS
40#include "target.h"
41#include "parser-defs.h"
9a3d7dfd 42#include "demangle.h"
8b60591b 43#include "symfile.h"
8de20a37 44#include "cp-support.h"
06096720 45#include "frame.h"
43cc5389 46#include "c-lang.h"
47e77640 47#include <algorithm>
c906108c 48
a121b7c1 49static void unk_lang_error (const char *);
c906108c 50
410a0ff2 51static int unk_lang_parser (struct parser_state *);
c906108c 52
a451cb65 53static void set_range_case (void);
c906108c 54
6c7a06a3
TT
55static void unk_lang_emit_char (int c, struct type *type,
56 struct ui_file *stream, int quoter);
c906108c 57
6c7a06a3
TT
58static void unk_lang_printchar (int c, struct type *type,
59 struct ui_file *stream);
c906108c 60
8e069a98
TT
61static void unk_lang_value_print (struct value *, struct ui_file *,
62 const struct value_print_options *);
c906108c 63
52f729a7 64static CORE_ADDR unk_lang_trampoline (struct frame_info *, CORE_ADDR pc);
f636b87d 65
c906108c
SS
66/* Forward declaration */
67extern const struct language_defn unknown_language_defn;
c5aa993b 68
c906108c 69/* The current (default at startup) state of type and range checking.
c5aa993b
JM
70 (If the modes are set to "auto", though, these are changed based
71 on the default language at startup, and then again based on the
72 language of the first source file. */
c906108c
SS
73
74enum range_mode range_mode = range_mode_auto;
75enum range_check range_check = range_check_off;
63872f9d
JG
76enum case_mode case_mode = case_mode_auto;
77enum case_sensitivity case_sensitivity = case_sensitive_on;
c906108c 78
1777feb0 79/* The current language and language_mode (see language.h). */
c906108c
SS
80
81const struct language_defn *current_language = &unknown_language_defn;
82enum language_mode language_mode = language_mode_auto;
83
84/* The language that the user expects to be typing in (the language
85 of main(), or the last language we notified them about, or C). */
86
87const struct language_defn *expected_language;
88
47e77640
PA
89/* The list of supported languages. Keep this in the same order as
90 the 'enum language' values. */
91
92static const struct language_defn *languages[] = {
93 &unknown_language_defn,
94 &auto_language_defn,
95 &c_language_defn,
96 &objc_language_defn,
97 &cplus_language_defn,
98 &d_language_defn,
99 &go_language_defn,
100 &f_language_defn,
101 &m2_language_defn,
102 &asm_language_defn,
103 &pascal_language_defn,
104 &opencl_language_defn,
105 &rust_language_defn,
106 &minimal_language_defn,
107 &ada_language_defn,
108};
c906108c 109
b84aa90a
PA
110/* The current values of the "set language/type/range" enum
111 commands. */
112static const char *language;
113static const char *type;
114static const char *range;
115static const char *case_sensitive;
c906108c
SS
116
117/* Warning issued when current_language and the language of the current
1777feb0 118 frame do not match. */
c906108c 119char lang_frame_mismatch_warn[] =
c5aa993b 120"Warning: the current language does not match this frame.";
c906108c
SS
121\f
122/* This page contains the functions corresponding to GDB commands
1777feb0 123 and their helpers. */
c906108c
SS
124
125/* Show command. Display a warning if the language set
1777feb0 126 does not match the frame. */
c906108c 127static void
4d28ad1e
AC
128show_language_command (struct ui_file *file, int from_tty,
129 struct cmd_list_element *c, const char *value)
c906108c 130{
7ff38b1c 131 enum language flang; /* The language of the frame. */
c906108c 132
b84aa90a
PA
133 if (language_mode == language_mode_auto)
134 fprintf_filtered (gdb_stdout,
135 _("The current source language is "
136 "\"auto; currently %s\".\n"),
137 current_language->la_name);
138 else
3e43a32a
MS
139 fprintf_filtered (gdb_stdout,
140 _("The current source language is \"%s\".\n"),
b84aa90a
PA
141 current_language->la_name);
142
7ff38b1c
AB
143 if (has_stack_frames ())
144 {
145 struct frame_info *frame;
146
147 frame = get_selected_frame (NULL);
148 flang = get_frame_language (frame);
149 if (flang != language_unknown
150 && language_mode == language_mode_manual
151 && current_language->la_language != flang)
152 printf_filtered ("%s\n", lang_frame_mismatch_warn);
153 }
c906108c
SS
154}
155
1777feb0 156/* Set command. Change the current working language. */
c906108c 157static void
4d28ad1e 158set_language_command (char *ignore, int from_tty, struct cmd_list_element *c)
c906108c 159{
7ff38b1c 160 enum language flang = language_unknown;
c906108c 161
47e77640
PA
162 /* "local" is a synonym of "auto". */
163 if (strcmp (language, "local") == 0)
164 language = "auto";
165
c906108c 166 /* Search the list of languages for a match. */
47e77640 167 for (const auto &lang : languages)
c5aa993b 168 {
47e77640 169 if (strcmp (lang->la_name, language) == 0)
c5aa993b
JM
170 {
171 /* Found it! Go into manual mode, and use this language. */
47e77640 172 if (lang->la_language == language_auto)
c5aa993b 173 {
8b60591b
JB
174 /* Enter auto mode. Set to the current frame's language, if
175 known, or fallback to the initial language. */
c5aa993b 176 language_mode = language_mode_auto;
7ff38b1c
AB
177 TRY
178 {
179 struct frame_info *frame;
180
181 frame = get_selected_frame (NULL);
182 flang = get_frame_language (frame);
183 }
184 CATCH (ex, RETURN_MASK_ERROR)
185 {
186 flang = language_unknown;
187 }
188 END_CATCH
189
c5aa993b
JM
190 if (flang != language_unknown)
191 set_language (flang);
8b60591b
JB
192 else
193 set_initial_language ();
c5aa993b
JM
194 expected_language = current_language;
195 return;
196 }
197 else
198 {
199 /* Enter manual mode. Set the specified language. */
200 language_mode = language_mode_manual;
47e77640 201 current_language = lang;
a451cb65 202 set_range_case ();
c5aa993b
JM
203 expected_language = current_language;
204 return;
205 }
206 }
c906108c 207 }
c906108c 208
b84aa90a
PA
209 internal_error (__FILE__, __LINE__,
210 "Couldn't find language `%s' in known languages list.",
211 language);
c906108c
SS
212}
213
c906108c 214/* Show command. Display a warning if the range setting does
1777feb0 215 not match the current language. */
c906108c 216static void
4d28ad1e
AC
217show_range_command (struct ui_file *file, int from_tty,
218 struct cmd_list_element *c, const char *value)
c906108c 219{
b84aa90a
PA
220 if (range_mode == range_mode_auto)
221 {
a121b7c1 222 const char *tmp;
b84aa90a
PA
223
224 switch (range_check)
225 {
226 case range_check_on:
227 tmp = "on";
228 break;
229 case range_check_off:
230 tmp = "off";
231 break;
232 case range_check_warn:
233 tmp = "warn";
234 break;
235 default:
236 internal_error (__FILE__, __LINE__,
237 "Unrecognized range check setting.");
238 }
239
240 fprintf_filtered (gdb_stdout,
241 _("Range checking is \"auto; currently %s\".\n"),
242 tmp);
243 }
244 else
245 fprintf_filtered (gdb_stdout, _("Range checking is \"%s\".\n"),
246 value);
247
c5aa993b 248 if (range_check != current_language->la_range_check)
b84aa90a
PA
249 warning (_("the current range check setting "
250 "does not match the language.\n"));
c906108c
SS
251}
252
1777feb0 253/* Set command. Change the setting for range checking. */
c906108c 254static void
4d28ad1e 255set_range_command (char *ignore, int from_tty, struct cmd_list_element *c)
c906108c 256{
6314a349 257 if (strcmp (range, "on") == 0)
c5aa993b 258 {
c906108c
SS
259 range_check = range_check_on;
260 range_mode = range_mode_manual;
c5aa993b 261 }
6314a349 262 else if (strcmp (range, "warn") == 0)
c5aa993b 263 {
c906108c
SS
264 range_check = range_check_warn;
265 range_mode = range_mode_manual;
c5aa993b 266 }
6314a349 267 else if (strcmp (range, "off") == 0)
c5aa993b 268 {
c906108c
SS
269 range_check = range_check_off;
270 range_mode = range_mode_manual;
c5aa993b 271 }
6314a349 272 else if (strcmp (range, "auto") == 0)
c5aa993b 273 {
c906108c 274 range_mode = range_mode_auto;
a451cb65 275 set_range_case ();
c906108c 276 return;
c5aa993b 277 }
c4093a6a
JM
278 else
279 {
b84aa90a
PA
280 internal_error (__FILE__, __LINE__,
281 _("Unrecognized range check setting: \"%s\""), range);
c4093a6a 282 }
b84aa90a
PA
283 if (range_check != current_language->la_range_check)
284 warning (_("the current range check setting "
285 "does not match the language.\n"));
c906108c
SS
286}
287
63872f9d 288/* Show command. Display a warning if the case sensitivity setting does
1777feb0 289 not match the current language. */
63872f9d 290static void
4d28ad1e
AC
291show_case_command (struct ui_file *file, int from_tty,
292 struct cmd_list_element *c, const char *value)
63872f9d 293{
b84aa90a
PA
294 if (case_mode == case_mode_auto)
295 {
a121b7c1 296 const char *tmp = NULL;
b84aa90a
PA
297
298 switch (case_sensitivity)
299 {
300 case case_sensitive_on:
301 tmp = "on";
302 break;
303 case case_sensitive_off:
304 tmp = "off";
305 break;
306 default:
307 internal_error (__FILE__, __LINE__,
308 "Unrecognized case-sensitive setting.");
309 }
310
311 fprintf_filtered (gdb_stdout,
312 _("Case sensitivity in "
313 "name search is \"auto; currently %s\".\n"),
314 tmp);
315 }
316 else
3e43a32a
MS
317 fprintf_filtered (gdb_stdout,
318 _("Case sensitivity in name search is \"%s\".\n"),
b84aa90a
PA
319 value);
320
4d28ad1e 321 if (case_sensitivity != current_language->la_case_sensitivity)
b84aa90a
PA
322 warning (_("the current case sensitivity setting does not match "
323 "the language.\n"));
63872f9d
JG
324}
325
591e78ff
MK
326/* Set command. Change the setting for case sensitivity. */
327
63872f9d 328static void
4d28ad1e 329set_case_command (char *ignore, int from_tty, struct cmd_list_element *c)
63872f9d 330{
591e78ff
MK
331 if (strcmp (case_sensitive, "on") == 0)
332 {
333 case_sensitivity = case_sensitive_on;
334 case_mode = case_mode_manual;
335 }
336 else if (strcmp (case_sensitive, "off") == 0)
337 {
338 case_sensitivity = case_sensitive_off;
339 case_mode = case_mode_manual;
340 }
341 else if (strcmp (case_sensitive, "auto") == 0)
342 {
343 case_mode = case_mode_auto;
a451cb65 344 set_range_case ();
591e78ff
MK
345 return;
346 }
63872f9d 347 else
591e78ff 348 {
b84aa90a
PA
349 internal_error (__FILE__, __LINE__,
350 "Unrecognized case-sensitive setting: \"%s\"",
351 case_sensitive);
591e78ff 352 }
b84aa90a
PA
353
354 if (case_sensitivity != current_language->la_case_sensitivity)
355 warning (_("the current case sensitivity setting does not match "
356 "the language.\n"));
63872f9d
JG
357}
358
359/* Set the status of range and type checking and case sensitivity based on
c906108c
SS
360 the current modes and the current language.
361 If SHOW is non-zero, then print out the current language,
1777feb0 362 type and range checking status. */
c906108c 363static void
a451cb65 364set_range_case (void)
c906108c 365{
c906108c
SS
366 if (range_mode == range_mode_auto)
367 range_check = current_language->la_range_check;
368
63872f9d
JG
369 if (case_mode == case_mode_auto)
370 case_sensitivity = current_language->la_case_sensitivity;
c906108c
SS
371}
372
1777feb0
MS
373/* Set current language to (enum language) LANG. Returns previous
374 language. */
c906108c
SS
375
376enum language
fba45db2 377set_language (enum language lang)
c906108c 378{
c906108c
SS
379 enum language prev_language;
380
381 prev_language = current_language->la_language;
47e77640
PA
382 current_language = languages[lang];
383 set_range_case ();
c906108c
SS
384 return prev_language;
385}
386\f
c906108c
SS
387
388/* Print out the current language settings: language, range and
389 type checking. If QUIETLY, print only what has changed. */
390
391void
fba45db2 392language_info (int quietly)
c906108c
SS
393{
394 if (quietly && expected_language == current_language)
395 return;
396
397 expected_language = current_language;
a3f17187 398 printf_unfiltered (_("Current language: %s\n"), language);
4d28ad1e 399 show_language_command (NULL, 1, NULL, NULL);
c906108c
SS
400
401 if (!quietly)
402 {
a3f17187 403 printf_unfiltered (_("Range checking: %s\n"), range);
4d28ad1e 404 show_range_command (NULL, 1, NULL, NULL);
a3f17187 405 printf_unfiltered (_("Case sensitivity: %s\n"), case_sensitive);
4d28ad1e 406 show_case_command (NULL, 1, NULL, NULL);
c906108c
SS
407 }
408}
409\f
c906108c 410
1777feb0 411/* Returns non-zero if the value is a pointer type. */
c906108c 412int
fba45db2 413pointer_type (struct type *type)
c906108c 414{
aa006118 415 return TYPE_CODE (type) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type);
c906108c
SS
416}
417
c906108c 418\f
c906108c 419/* This page contains functions that return info about
1777feb0 420 (struct value) values used in GDB. */
c906108c 421
1777feb0 422/* Returns non-zero if the value VAL represents a true value. */
c906108c 423int
3d6d86c6 424value_true (struct value *val)
c906108c
SS
425{
426 /* It is possible that we should have some sort of error if a non-boolean
427 value is used in this context. Possibly dependent on some kind of
428 "boolean-checking" option like range checking. But it should probably
429 not depend on the language except insofar as is necessary to identify
430 a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
431 should be an error, probably). */
432 return !value_logical_not (val);
433}
434\f
c906108c
SS
435/* This page contains functions for the printing out of
436 error messages that occur during type- and range-
1777feb0 437 checking. */
c906108c 438
a451cb65 439/* This is called when a language fails a range-check. The
ddfe3c15 440 first argument should be a printf()-style format string, and the
a451cb65
KS
441 rest of the arguments should be its arguments. If range_check is
442 range_check_on, an error is printed; if range_check_warn, a warning;
443 otherwise just the message. */
c906108c
SS
444
445void
ddfe3c15 446range_error (const char *string,...)
c906108c 447{
c5aa993b 448 va_list args;
c906108c 449
e0881a8e 450 va_start (args, string);
ddfe3c15
AC
451 switch (range_check)
452 {
453 case range_check_warn:
454 vwarning (string, args);
455 break;
456 case range_check_on:
457 verror (string, args);
458 break;
459 case range_check_off:
460 /* FIXME: cagney/2002-01-30: Should this function print anything
461 when range error is off? */
462 vfprintf_filtered (gdb_stderr, string, args);
463 fprintf_filtered (gdb_stderr, "\n");
464 break;
465 default:
e2e0b3e5 466 internal_error (__FILE__, __LINE__, _("bad switch"));
ddfe3c15 467 }
c5aa993b 468 va_end (args);
c906108c 469}
c906108c 470\f
c5aa993b 471
1777feb0 472/* This page contains miscellaneous functions. */
c906108c 473
1777feb0 474/* Return the language enum for a given language string. */
c906108c
SS
475
476enum language
2039bd9f 477language_enum (const char *str)
c906108c 478{
47e77640
PA
479 for (const auto &lang : languages)
480 if (strcmp (lang->la_name, str) == 0)
481 return lang->la_language;
c906108c 482
47e77640
PA
483 if (strcmp (str, "local") == 0)
484 return language_auto;
c906108c
SS
485
486 return language_unknown;
487}
488
1777feb0 489/* Return the language struct for a given language enum. */
c906108c
SS
490
491const struct language_defn *
fba45db2 492language_def (enum language lang)
c906108c 493{
47e77640 494 return languages[lang];
c906108c
SS
495}
496
1777feb0 497/* Return the language as a string. */
47e77640 498
27cd387b 499const char *
fba45db2 500language_str (enum language lang)
c906108c 501{
47e77640 502 return languages[lang]->la_name;
c906108c
SS
503}
504
505static void
981a3fb3 506set_check (const char *ignore, int from_tty)
c906108c 507{
c5aa993b
JM
508 printf_unfiltered (
509 "\"set check\" must be followed by the name of a check subcommand.\n");
635c7e8a 510 help_list (setchecklist, "set check ", all_commands, gdb_stdout);
c906108c
SS
511}
512
513static void
981a3fb3 514show_check (const char *ignore, int from_tty)
c906108c 515{
c5aa993b 516 cmd_show_list (showchecklist, from_tty, "");
c906108c
SS
517}
518\f
c906108c 519
47e77640 520/* Build and install the "set language LANG" command. */
b84aa90a 521
47e77640
PA
522static void
523add_set_language_command ()
524{
525 static const char **language_names;
c906108c 526
47e77640
PA
527 /* Build the language names array, to be used as enumeration in the
528 "set language" enum command. +1 for "local" and +1 for NULL
529 termination. */
530 language_names = new const char *[ARRAY_SIZE (languages) + 2];
531
532 /* Display "auto", "local" and "unknown" first, and then the rest,
533 alpha sorted. */
534 const char **language_names_p = language_names;
535 *language_names_p++ = auto_language_defn.la_name;
536 *language_names_p++ = "local";
537 *language_names_p++ = unknown_language_defn.la_name;
538 const char **sort_begin = language_names_p;
539 for (const auto &lang : languages)
c906108c 540 {
47e77640
PA
541 /* Already handled above. */
542 if (lang->la_language == language_auto
543 || lang->la_language == language_unknown)
544 continue;
545 *language_names_p++ = lang->la_name;
c906108c 546 }
47e77640
PA
547 *language_names_p = NULL;
548 std::sort (sort_begin, language_names_p, compare_cstrings);
b84aa90a 549
56618e20 550 /* Add the filename extensions. */
47e77640
PA
551 for (const auto &lang : languages)
552 if (lang->la_filename_extensions != NULL)
553 {
554 for (size_t i = 0; lang->la_filename_extensions[i] != NULL; ++i)
555 add_filename_language (lang->la_filename_extensions[i],
556 lang->la_language);
557 }
56618e20 558
b84aa90a 559 /* Build the "help set language" docs. */
d7e74731 560 string_file doc;
b84aa90a 561
d7e74731
PA
562 doc.printf (_("Set the current source language.\n"
563 "The currently understood settings are:\n\nlocal or "
564 "auto Automatic setting based on source file\n"));
b84aa90a 565
47e77640 566 for (const auto &lang : languages)
b84aa90a
PA
567 {
568 /* Already dealt with these above. */
47e77640
PA
569 if (lang->la_language == language_unknown
570 || lang->la_language == language_auto)
b84aa90a
PA
571 continue;
572
d7e74731
PA
573 /* FIXME: i18n: for now assume that the human-readable name is
574 just a capitalization of the internal name. */
575 doc.printf ("%-16s Use the %c%s language\n",
47e77640 576 lang->la_name,
d7e74731 577 /* Capitalize first letter of language name. */
47e77640
PA
578 toupper (lang->la_name[0]),
579 lang->la_name + 1);
b84aa90a
PA
580 }
581
b84aa90a 582 add_setshow_enum_cmd ("language", class_support,
47e77640 583 language_names,
b84aa90a 584 &language,
d7e74731 585 doc.c_str (),
3e43a32a
MS
586 _("Show the current source language."),
587 NULL, set_language_command,
b84aa90a
PA
588 show_language_command,
589 &setlist, &showlist);
c906108c
SS
590}
591
f636b87d
AF
592/* Iterate through all registered languages looking for and calling
593 any non-NULL struct language_defn.skip_trampoline() functions.
594 Return the result from the first that returns non-zero, or 0 if all
595 `fail'. */
596CORE_ADDR
52f729a7 597skip_language_trampoline (struct frame_info *frame, CORE_ADDR pc)
f636b87d 598{
47e77640 599 for (const auto &lang : languages)
f636b87d 600 {
47e77640 601 if (lang->skip_trampoline != NULL)
f636b87d 602 {
47e77640 603 CORE_ADDR real_pc = lang->skip_trampoline (frame, pc);
e0881a8e 604
f636b87d
AF
605 if (real_pc)
606 return real_pc;
607 }
608 }
609
610 return 0;
611}
612
1777feb0 613/* Return demangled language symbol, or NULL.
9a3d7dfd
AF
614 FIXME: Options are only useful for certain languages and ignored
615 by others, so it would be better to remove them here and have a
1777feb0 616 more flexible demangler for the languages that need it.
9a3d7dfd
AF
617 FIXME: Sometimes the demangler is invoked when we don't know the
618 language, so we can't use this everywhere. */
619char *
620language_demangle (const struct language_defn *current_language,
621 const char *mangled, int options)
622{
623 if (current_language != NULL && current_language->la_demangle)
624 return current_language->la_demangle (mangled, options);
625 return NULL;
626}
627
8b302db8
TT
628/* See langauge.h. */
629
630int
631language_sniff_from_mangled_name (const struct language_defn *lang,
632 const char *mangled, char **demangled)
633{
634 gdb_assert (lang != NULL);
635
636 if (lang->la_sniff_from_mangled_name == NULL)
637 {
638 *demangled = NULL;
639 return 0;
640 }
641
642 return lang->la_sniff_from_mangled_name (mangled, demangled);
643}
644
31c27f77
JJ
645/* Return class name from physname or NULL. */
646char *
79b97fa8 647language_class_name_from_physname (const struct language_defn *lang,
31c27f77
JJ
648 const char *physname)
649{
79b97fa8
TT
650 if (lang != NULL && lang->la_class_name_from_physname)
651 return lang->la_class_name_from_physname (physname);
31c27f77
JJ
652 return NULL;
653}
654
41f1b697
DJ
655/* Return non-zero if TYPE should be passed (and returned) by
656 reference at the language level. */
657int
658language_pass_by_reference (struct type *type)
659{
660 return current_language->la_pass_by_reference (type);
661}
662
663/* Return zero; by default, types are passed by value at the language
664 level. The target ABI may pass or return some structs by reference
665 independent of this. */
666int
667default_pass_by_reference (struct type *type)
668{
669 return 0;
670}
671
9f0a5303
JB
672/* Return the default string containing the list of characters
673 delimiting words. This is a reasonable default value that
674 most languages should be able to use. */
675
67cb5b2d 676const char *
9f0a5303
JB
677default_word_break_characters (void)
678{
679 return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
680}
f636b87d 681
e79af960
JB
682/* Print the index of array elements using the C99 syntax. */
683
684void
685default_print_array_index (struct value *index_value, struct ui_file *stream,
79a45b7d 686 const struct value_print_options *options)
e79af960
JB
687{
688 fprintf_filtered (stream, "[");
79a45b7d 689 LA_VALUE_PRINT (index_value, stream, options);
e79af960
JB
690 fprintf_filtered (stream, "] = ");
691}
692
ae6a3a4c
TJB
693void
694default_get_string (struct value *value, gdb_byte **buffer, int *length,
96c07c5b 695 struct type **char_type, const char **charset)
ae6a3a4c
TJB
696{
697 error (_("Getting a string is unsupported in this language."));
698}
699
c906108c
SS
700/* Define the language that is no language. */
701
702static int
410a0ff2 703unk_lang_parser (struct parser_state *ps)
c906108c
SS
704{
705 return 1;
706}
707
708static void
a121b7c1 709unk_lang_error (const char *msg)
c906108c 710{
8a3fe4f8 711 error (_("Attempted to parse an expression with unknown language"));
c906108c
SS
712}
713
714static void
6c7a06a3
TT
715unk_lang_emit_char (int c, struct type *type, struct ui_file *stream,
716 int quoter)
c906108c 717{
3e43a32a
MS
718 error (_("internal error - unimplemented "
719 "function unk_lang_emit_char called."));
c906108c
SS
720}
721
722static void
6c7a06a3 723unk_lang_printchar (int c, struct type *type, struct ui_file *stream)
c906108c 724{
3e43a32a
MS
725 error (_("internal error - unimplemented "
726 "function unk_lang_printchar called."));
c906108c
SS
727}
728
729static void
6c7a06a3
TT
730unk_lang_printstr (struct ui_file *stream, struct type *type,
731 const gdb_byte *string, unsigned int length,
be759fcf 732 const char *encoding, int force_ellipses,
79a45b7d 733 const struct value_print_options *options)
c906108c 734{
3e43a32a
MS
735 error (_("internal error - unimplemented "
736 "function unk_lang_printstr called."));
c906108c
SS
737}
738
c906108c 739static void
25b524e8 740unk_lang_print_type (struct type *type, const char *varstring,
79d43c61
TT
741 struct ui_file *stream, int show, int level,
742 const struct type_print_options *flags)
c906108c 743{
3e43a32a
MS
744 error (_("internal error - unimplemented "
745 "function unk_lang_print_type called."));
c906108c
SS
746}
747
d3eab38a 748static void
e8b24d9f 749unk_lang_val_print (struct type *type,
a2bd3dcd 750 int embedded_offset, CORE_ADDR address,
79a45b7d 751 struct ui_file *stream, int recurse,
e8b24d9f 752 struct value *val,
79a45b7d 753 const struct value_print_options *options)
c906108c 754{
3e43a32a
MS
755 error (_("internal error - unimplemented "
756 "function unk_lang_val_print called."));
c906108c
SS
757}
758
8e069a98 759static void
79a45b7d
TT
760unk_lang_value_print (struct value *val, struct ui_file *stream,
761 const struct value_print_options *options)
c906108c 762{
3e43a32a
MS
763 error (_("internal error - unimplemented "
764 "function unk_lang_value_print called."));
c906108c
SS
765}
766
52f729a7 767static CORE_ADDR unk_lang_trampoline (struct frame_info *frame, CORE_ADDR pc)
f636b87d
AF
768{
769 return 0;
770}
771
9a3d7dfd
AF
772/* Unknown languages just use the cplus demangler. */
773static char *unk_lang_demangle (const char *mangled, int options)
774{
8de20a37 775 return gdb_demangle (mangled, options);
9a3d7dfd
AF
776}
777
31c27f77
JJ
778static char *unk_lang_class_name (const char *mangled)
779{
780 return NULL;
781}
9a3d7dfd 782
c5aa993b
JM
783static const struct op_print unk_op_print_tab[] =
784{
785 {NULL, OP_NULL, PREC_NULL, 0}
c906108c
SS
786};
787
f290d38e
AC
788static void
789unknown_language_arch_info (struct gdbarch *gdbarch,
790 struct language_arch_info *lai)
791{
792 lai->string_char_type = builtin_type (gdbarch)->builtin_char;
fbb06eb1 793 lai->bool_type_default = builtin_type (gdbarch)->builtin_int;
5a44ea29 794 lai->primitive_type_vector = GDBARCH_OBSTACK_CALLOC (gdbarch, 1,
f290d38e
AC
795 struct type *);
796}
797
c5aa993b
JM
798const struct language_defn unknown_language_defn =
799{
c906108c 800 "unknown",
6abde28f 801 "Unknown",
c906108c 802 language_unknown,
c906108c 803 range_check_off,
63872f9d 804 case_sensitive_on,
9a044a89
TT
805 array_row_major,
806 macro_expansion_no,
56618e20 807 NULL,
5f9769d1 808 &exp_descriptor_standard,
c906108c
SS
809 unk_lang_parser,
810 unk_lang_error,
e85c3284 811 null_post_parser,
c906108c
SS
812 unk_lang_printchar, /* Print character constant */
813 unk_lang_printstr,
814 unk_lang_emit_char,
c906108c 815 unk_lang_print_type, /* Print a type using appropriate syntax */
5c6ce71d 816 default_print_typedef, /* Print a typedef using appropriate syntax */
c906108c
SS
817 unk_lang_val_print, /* Print a value using appropriate syntax */
818 unk_lang_value_print, /* Print a top-level value */
a5ee536b 819 default_read_var_value, /* la_read_var_value */
f636b87d 820 unk_lang_trampoline, /* Language specific skip_trampoline */
2b2d9e11 821 "this", /* name_of_this */
5f9a71c3 822 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 823 basic_lookup_transparent_type,/* lookup_transparent_type */
9a3d7dfd 824 unk_lang_demangle, /* Language specific symbol demangler */
8b302db8 825 NULL,
3e43a32a
MS
826 unk_lang_class_name, /* Language specific
827 class_name_from_physname */
c906108c
SS
828 unk_op_print_tab, /* expression operators for printing */
829 1, /* c-style arrays */
830 0, /* String lower bound */
6084f43a 831 default_word_break_characters,
eb3ff9a5 832 default_collect_symbol_completion_matches,
f290d38e 833 unknown_language_arch_info, /* la_language_arch_info. */
e79af960 834 default_print_array_index,
41f1b697 835 default_pass_by_reference,
ae6a3a4c 836 default_get_string,
43cc5389 837 c_watch_location_expression,
1a119f36 838 NULL, /* la_get_symbol_name_cmp */
f8eba3c6 839 iterate_over_symbols,
a53b64ea 840 &default_varobj_ops,
bb2ec1b3
TT
841 NULL,
842 NULL,
c906108c
SS
843 LANG_MAGIC
844};
845
1777feb0
MS
846/* These two structs define fake entries for the "local" and "auto"
847 options. */
c5aa993b
JM
848const struct language_defn auto_language_defn =
849{
c906108c 850 "auto",
6abde28f 851 "Auto",
c906108c 852 language_auto,
c906108c 853 range_check_off,
63872f9d 854 case_sensitive_on,
9a044a89
TT
855 array_row_major,
856 macro_expansion_no,
56618e20 857 NULL,
5f9769d1 858 &exp_descriptor_standard,
c906108c
SS
859 unk_lang_parser,
860 unk_lang_error,
e85c3284 861 null_post_parser,
c906108c
SS
862 unk_lang_printchar, /* Print character constant */
863 unk_lang_printstr,
864 unk_lang_emit_char,
c906108c 865 unk_lang_print_type, /* Print a type using appropriate syntax */
5c6ce71d 866 default_print_typedef, /* Print a typedef using appropriate syntax */
c906108c
SS
867 unk_lang_val_print, /* Print a value using appropriate syntax */
868 unk_lang_value_print, /* Print a top-level value */
a5ee536b 869 default_read_var_value, /* la_read_var_value */
f636b87d 870 unk_lang_trampoline, /* Language specific skip_trampoline */
2b2d9e11 871 "this", /* name_of_this */
5f9a71c3 872 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 873 basic_lookup_transparent_type,/* lookup_transparent_type */
9a3d7dfd 874 unk_lang_demangle, /* Language specific symbol demangler */
8b302db8 875 NULL,
3e43a32a
MS
876 unk_lang_class_name, /* Language specific
877 class_name_from_physname */
c906108c
SS
878 unk_op_print_tab, /* expression operators for printing */
879 1, /* c-style arrays */
880 0, /* String lower bound */
6084f43a 881 default_word_break_characters,
eb3ff9a5 882 default_collect_symbol_completion_matches,
f290d38e 883 unknown_language_arch_info, /* la_language_arch_info. */
e79af960 884 default_print_array_index,
41f1b697 885 default_pass_by_reference,
ae6a3a4c 886 default_get_string,
43cc5389 887 c_watch_location_expression,
1a119f36 888 NULL, /* la_get_symbol_name_cmp */
f8eba3c6 889 iterate_over_symbols,
a53b64ea 890 &default_varobj_ops,
bb2ec1b3
TT
891 NULL,
892 NULL,
c906108c
SS
893 LANG_MAGIC
894};
895
c906108c 896\f
f290d38e
AC
897/* Per-architecture language information. */
898
899static struct gdbarch_data *language_gdbarch_data;
900
901struct language_gdbarch
902{
903 /* A vector of per-language per-architecture info. Indexed by "enum
904 language". */
905 struct language_arch_info arch_info[nr_languages];
906};
907
908static void *
909language_gdbarch_post_init (struct gdbarch *gdbarch)
910{
911 struct language_gdbarch *l;
f290d38e
AC
912
913 l = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct language_gdbarch);
47e77640
PA
914 for (const auto &lang : languages)
915 if (lang != NULL && lang->la_language_arch_info != NULL)
916 {
917 lang->la_language_arch_info (gdbarch,
918 l->arch_info + lang->la_language);
919 }
920
f290d38e
AC
921 return l;
922}
923
924struct type *
925language_string_char_type (const struct language_defn *la,
926 struct gdbarch *gdbarch)
927{
9a3c8263
SM
928 struct language_gdbarch *ld
929 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
e0881a8e 930
aba2dd37 931 return ld->arch_info[la->la_language].string_char_type;
f290d38e
AC
932}
933
fbb06eb1
UW
934struct type *
935language_bool_type (const struct language_defn *la,
936 struct gdbarch *gdbarch)
937{
9a3c8263
SM
938 struct language_gdbarch *ld
939 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
fbb06eb1
UW
940
941 if (ld->arch_info[la->la_language].bool_type_symbol)
942 {
943 struct symbol *sym;
e0881a8e 944
fbb06eb1 945 sym = lookup_symbol (ld->arch_info[la->la_language].bool_type_symbol,
d12307c1 946 NULL, VAR_DOMAIN, NULL).symbol;
fbb06eb1
UW
947 if (sym)
948 {
949 struct type *type = SYMBOL_TYPE (sym);
e0881a8e 950
fbb06eb1
UW
951 if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
952 return type;
953 }
954 }
955
956 return ld->arch_info[la->la_language].bool_type_default;
957}
958
1994afbf
DE
959/* Helper function for primitive type lookup. */
960
961static struct type **
962language_lookup_primitive_type_1 (const struct language_arch_info *lai,
963 const char *name)
964{
965 struct type **p;
966
967 for (p = lai->primitive_type_vector; (*p) != NULL; p++)
968 {
969 if (strcmp (TYPE_NAME (*p), name) == 0)
970 return p;
971 }
972 return NULL;
973}
974
975/* See language.h. */
976
f290d38e 977struct type *
46b0da17
DE
978language_lookup_primitive_type (const struct language_defn *la,
979 struct gdbarch *gdbarch,
980 const char *name)
f290d38e 981{
9a3c8263
SM
982 struct language_gdbarch *ld =
983 (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1994afbf
DE
984 struct type **typep;
985
986 typep = language_lookup_primitive_type_1 (&ld->arch_info[la->la_language],
987 name);
988 if (typep == NULL)
989 return NULL;
990 return *typep;
991}
992
993/* Helper function for type lookup as a symbol.
994 Create the symbol corresponding to type TYPE in language LANG. */
995
996static struct symbol *
997language_alloc_type_symbol (enum language lang, struct type *type)
998{
999 struct symbol *symbol;
1000 struct gdbarch *gdbarch;
1001
1002 gdb_assert (!TYPE_OBJFILE_OWNED (type));
1003
1004 gdbarch = TYPE_OWNER (type).gdbarch;
1005 symbol = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct symbol);
1006
1007 symbol->ginfo.name = TYPE_NAME (type);
1008 symbol->ginfo.language = lang;
1009 symbol->owner.arch = gdbarch;
1010 SYMBOL_OBJFILE_OWNED (symbol) = 0;
1011 SYMBOL_TYPE (symbol) = type;
1012 SYMBOL_DOMAIN (symbol) = VAR_DOMAIN;
1013 SYMBOL_ACLASS_INDEX (symbol) = LOC_TYPEDEF;
1014
1015 return symbol;
1016}
1017
1018/* Initialize the primitive type symbols of language LD.
1019 The primitive type vector must have already been initialized. */
1020
1021static void
1022language_init_primitive_type_symbols (struct language_arch_info *lai,
1023 const struct language_defn *la,
1024 struct gdbarch *gdbarch)
1025{
1026 int n;
1994afbf
DE
1027
1028 gdb_assert (lai->primitive_type_vector != NULL);
1029
1030 for (n = 0; lai->primitive_type_vector[n] != NULL; ++n)
1031 continue;
1032
1033 lai->primitive_type_symbols
1034 = GDBARCH_OBSTACK_CALLOC (gdbarch, n + 1, struct symbol *);
1035
1036 for (n = 0; lai->primitive_type_vector[n] != NULL; ++n)
1037 {
1038 lai->primitive_type_symbols[n]
1039 = language_alloc_type_symbol (la->la_language,
1040 lai->primitive_type_vector[n]);
1041 }
1042
1043 /* Note: The result of symbol lookup is normally a symbol *and* the block
d12307c1
PMR
1044 it was found in. Builtin types don't live in blocks. We *could* give
1045 them one, but there is no current need so to keep things simple symbol
1046 lookup is extended to allow for BLOCK_FOUND to be NULL. */
1994afbf
DE
1047}
1048
1049/* See language.h. */
1050
1051struct symbol *
1052language_lookup_primitive_type_as_symbol (const struct language_defn *la,
1053 struct gdbarch *gdbarch,
1054 const char *name)
1055{
9a3c8263
SM
1056 struct language_gdbarch *ld
1057 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1994afbf
DE
1058 struct language_arch_info *lai = &ld->arch_info[la->la_language];
1059 struct type **typep;
1060 struct symbol *sym;
e0881a8e 1061
cc485e62
DE
1062 if (symbol_lookup_debug)
1063 {
1064 fprintf_unfiltered (gdb_stdlog,
1994afbf
DE
1065 "language_lookup_primitive_type_as_symbol"
1066 " (%s, %s, %s)",
cc485e62
DE
1067 la->la_name, host_address_to_string (gdbarch), name);
1068 }
1069
1994afbf
DE
1070 typep = language_lookup_primitive_type_1 (lai, name);
1071 if (typep == NULL)
f290d38e 1072 {
1994afbf
DE
1073 if (symbol_lookup_debug)
1074 fprintf_unfiltered (gdb_stdlog, " = NULL\n");
1075 return NULL;
f290d38e 1076 }
cc485e62 1077
1994afbf
DE
1078 /* The set of symbols is lazily initialized. */
1079 if (lai->primitive_type_symbols == NULL)
1080 language_init_primitive_type_symbols (lai, la, gdbarch);
1081
1082 sym = lai->primitive_type_symbols[typep - lai->primitive_type_vector];
1083
cc485e62 1084 if (symbol_lookup_debug)
1994afbf
DE
1085 fprintf_unfiltered (gdb_stdlog, " = %s\n", host_address_to_string (sym));
1086 return sym;
f290d38e
AC
1087}
1088
1777feb0 1089/* Initialize the language routines. */
c906108c
SS
1090
1091void
fba45db2 1092_initialize_language (void)
c906108c 1093{
40478521 1094 static const char *const type_or_range_names[]
b84aa90a
PA
1095 = { "on", "off", "warn", "auto", NULL };
1096
40478521 1097 static const char *const case_sensitive_names[]
b84aa90a 1098 = { "on", "off", "auto", NULL };
c5aa993b 1099
f290d38e
AC
1100 language_gdbarch_data
1101 = gdbarch_data_register_post_init (language_gdbarch_post_init);
1102
1777feb0 1103 /* GDB commands for language specific stuff. */
c5aa993b 1104
c5aa993b 1105 add_prefix_cmd ("check", no_class, set_check,
1bedd215 1106 _("Set the status of the type/range checker."),
c5aa993b
JM
1107 &setchecklist, "set check ", 0, &setlist);
1108 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1109 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1110
1111 add_prefix_cmd ("check", no_class, show_check,
1bedd215 1112 _("Show the status of the type/range checker."),
c5aa993b
JM
1113 &showchecklist, "show check ", 0, &showlist);
1114 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1115 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1116
b84aa90a 1117 add_setshow_enum_cmd ("range", class_support, type_or_range_names,
3e43a32a
MS
1118 &range,
1119 _("Set range checking. (on/warn/off/auto)"),
1120 _("Show range checking. (on/warn/off/auto)"),
1121 NULL, set_range_command,
b84aa90a
PA
1122 show_range_command,
1123 &setchecklist, &showchecklist);
1124
1125 add_setshow_enum_cmd ("case-sensitive", class_support, case_sensitive_names,
1126 &case_sensitive, _("\
4d28ad1e
AC
1127Set case sensitivity in name search. (on/off/auto)"), _("\
1128Show case sensitivity in name search. (on/off/auto)"), _("\
1129For Fortran the default is off; for other languages the default is on."),
b84aa90a
PA
1130 set_case_command,
1131 show_case_command,
1132 &setlist, &showlist);
63872f9d 1133
47e77640 1134 add_set_language_command ();
c5aa993b 1135
1b36a34b
JK
1136 language = xstrdup ("auto");
1137 type = xstrdup ("auto");
1138 range = xstrdup ("auto");
1139 case_sensitive = xstrdup ("auto");
63872f9d 1140
1777feb0 1141 /* Have the above take effect. */
63872f9d 1142 set_language (language_auto);
c906108c 1143}
This page took 1.543682 seconds and 4 git commands to generate.