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