solib_target_free_so memory leak
[deliverable/binutils-gdb.git] / gdb / language.c
CommitLineData
c906108c 1/* Multiple source language support for GDB.
1bac305b 2
b811d2c2 3 Copyright (C) 1991-2020 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>
0d12e84c 48#include "gdbarch.h"
c906108c 49
410a0ff2 50static int unk_lang_parser (struct parser_state *);
c906108c 51
a451cb65 52static void set_range_case (void);
c906108c 53
6c7a06a3
TT
54static void unk_lang_emit_char (int c, struct type *type,
55 struct ui_file *stream, int quoter);
c906108c 56
6c7a06a3
TT
57static void unk_lang_printchar (int c, struct type *type,
58 struct ui_file *stream);
c906108c 59
8e069a98
TT
60static void unk_lang_value_print (struct value *, struct ui_file *,
61 const struct value_print_options *);
c906108c 62
52f729a7 63static CORE_ADDR unk_lang_trampoline (struct frame_info *, CORE_ADDR pc);
f636b87d 64
c906108c
SS
65/* Forward declaration */
66extern const struct language_defn unknown_language_defn;
c5aa993b 67
c906108c 68/* The current (default at startup) state of type and range checking.
c5aa993b
JM
69 (If the modes are set to "auto", though, these are changed based
70 on the default language at startup, and then again based on the
71 language of the first source file. */
c906108c
SS
72
73enum range_mode range_mode = range_mode_auto;
74enum range_check range_check = range_check_off;
63872f9d
JG
75enum case_mode case_mode = case_mode_auto;
76enum case_sensitivity case_sensitivity = case_sensitive_on;
c906108c 77
1777feb0 78/* The current language and language_mode (see language.h). */
c906108c
SS
79
80const struct language_defn *current_language = &unknown_language_defn;
81enum language_mode language_mode = language_mode_auto;
82
83/* The language that the user expects to be typing in (the language
84 of main(), or the last language we notified them about, or C). */
85
86const struct language_defn *expected_language;
87
47e77640
PA
88/* The list of supported languages. Keep this in the same order as
89 the 'enum language' values. */
90
91static const struct language_defn *languages[] = {
92 &unknown_language_defn,
93 &auto_language_defn,
94 &c_language_defn,
95 &objc_language_defn,
96 &cplus_language_defn,
97 &d_language_defn,
98 &go_language_defn,
99 &f_language_defn,
100 &m2_language_defn,
101 &asm_language_defn,
102 &pascal_language_defn,
103 &opencl_language_defn,
104 &rust_language_defn,
105 &minimal_language_defn,
106 &ada_language_defn,
107};
c906108c 108
814fa4f6 109/* The current values of the "set language/range/case-sensitive" enum
b84aa90a
PA
110 commands. */
111static const char *language;
b84aa90a
PA
112static const char *range;
113static const char *case_sensitive;
c906108c 114
34916edc
CB
115/* See language.h. */
116const char lang_frame_mismatch_warn[] =
117N_("Warning: the current language does not match this frame.");
c906108c
SS
118\f
119/* This page contains the functions corresponding to GDB commands
1777feb0 120 and their helpers. */
c906108c
SS
121
122/* Show command. Display a warning if the language set
1777feb0 123 does not match the frame. */
c906108c 124static void
4d28ad1e
AC
125show_language_command (struct ui_file *file, int from_tty,
126 struct cmd_list_element *c, const char *value)
c906108c 127{
7ff38b1c 128 enum language flang; /* The language of the frame. */
c906108c 129
b84aa90a
PA
130 if (language_mode == language_mode_auto)
131 fprintf_filtered (gdb_stdout,
132 _("The current source language is "
133 "\"auto; currently %s\".\n"),
134 current_language->la_name);
135 else
3e43a32a
MS
136 fprintf_filtered (gdb_stdout,
137 _("The current source language is \"%s\".\n"),
b84aa90a
PA
138 current_language->la_name);
139
7ff38b1c
AB
140 if (has_stack_frames ())
141 {
142 struct frame_info *frame;
143
144 frame = get_selected_frame (NULL);
145 flang = get_frame_language (frame);
146 if (flang != language_unknown
147 && language_mode == language_mode_manual
148 && current_language->la_language != flang)
34916edc 149 printf_filtered ("%s\n", _(lang_frame_mismatch_warn));
7ff38b1c 150 }
c906108c
SS
151}
152
1777feb0 153/* Set command. Change the current working language. */
c906108c 154static void
eb4c3f4a
TT
155set_language_command (const char *ignore,
156 int from_tty, struct cmd_list_element *c)
c906108c 157{
7ff38b1c 158 enum language flang = language_unknown;
c906108c 159
47e77640
PA
160 /* "local" is a synonym of "auto". */
161 if (strcmp (language, "local") == 0)
162 language = "auto";
163
c906108c 164 /* Search the list of languages for a match. */
47e77640 165 for (const auto &lang : languages)
c5aa993b 166 {
47e77640 167 if (strcmp (lang->la_name, language) == 0)
c5aa993b
JM
168 {
169 /* Found it! Go into manual mode, and use this language. */
47e77640 170 if (lang->la_language == language_auto)
c5aa993b 171 {
8b60591b
JB
172 /* Enter auto mode. Set to the current frame's language, if
173 known, or fallback to the initial language. */
c5aa993b 174 language_mode = language_mode_auto;
a70b8144 175 try
7ff38b1c
AB
176 {
177 struct frame_info *frame;
178
179 frame = get_selected_frame (NULL);
180 flang = get_frame_language (frame);
181 }
230d2906 182 catch (const gdb_exception_error &ex)
7ff38b1c
AB
183 {
184 flang = language_unknown;
185 }
7ff38b1c 186
c5aa993b
JM
187 if (flang != language_unknown)
188 set_language (flang);
8b60591b
JB
189 else
190 set_initial_language ();
c5aa993b
JM
191 expected_language = current_language;
192 return;
193 }
194 else
195 {
196 /* Enter manual mode. Set the specified language. */
197 language_mode = language_mode_manual;
47e77640 198 current_language = lang;
a451cb65 199 set_range_case ();
c5aa993b
JM
200 expected_language = current_language;
201 return;
202 }
203 }
c906108c 204 }
c906108c 205
b84aa90a
PA
206 internal_error (__FILE__, __LINE__,
207 "Couldn't find language `%s' in known languages list.",
208 language);
c906108c
SS
209}
210
c906108c 211/* Show command. Display a warning if the range setting does
1777feb0 212 not match the current language. */
c906108c 213static void
4d28ad1e
AC
214show_range_command (struct ui_file *file, int from_tty,
215 struct cmd_list_element *c, const char *value)
c906108c 216{
b84aa90a
PA
217 if (range_mode == range_mode_auto)
218 {
a121b7c1 219 const char *tmp;
b84aa90a
PA
220
221 switch (range_check)
222 {
223 case range_check_on:
224 tmp = "on";
225 break;
226 case range_check_off:
227 tmp = "off";
228 break;
229 case range_check_warn:
230 tmp = "warn";
231 break;
232 default:
233 internal_error (__FILE__, __LINE__,
234 "Unrecognized range check setting.");
235 }
236
237 fprintf_filtered (gdb_stdout,
238 _("Range checking is \"auto; currently %s\".\n"),
239 tmp);
240 }
241 else
242 fprintf_filtered (gdb_stdout, _("Range checking is \"%s\".\n"),
243 value);
244
c5aa993b 245 if (range_check != current_language->la_range_check)
b84aa90a
PA
246 warning (_("the current range check setting "
247 "does not match the language.\n"));
c906108c
SS
248}
249
1777feb0 250/* Set command. Change the setting for range checking. */
c906108c 251static void
eb4c3f4a
TT
252set_range_command (const char *ignore,
253 int from_tty, struct cmd_list_element *c)
c906108c 254{
6314a349 255 if (strcmp (range, "on") == 0)
c5aa993b 256 {
c906108c
SS
257 range_check = range_check_on;
258 range_mode = range_mode_manual;
c5aa993b 259 }
6314a349 260 else if (strcmp (range, "warn") == 0)
c5aa993b 261 {
c906108c
SS
262 range_check = range_check_warn;
263 range_mode = range_mode_manual;
c5aa993b 264 }
6314a349 265 else if (strcmp (range, "off") == 0)
c5aa993b 266 {
c906108c
SS
267 range_check = range_check_off;
268 range_mode = range_mode_manual;
c5aa993b 269 }
6314a349 270 else if (strcmp (range, "auto") == 0)
c5aa993b 271 {
c906108c 272 range_mode = range_mode_auto;
a451cb65 273 set_range_case ();
c906108c 274 return;
c5aa993b 275 }
c4093a6a
JM
276 else
277 {
b84aa90a
PA
278 internal_error (__FILE__, __LINE__,
279 _("Unrecognized range check setting: \"%s\""), range);
c4093a6a 280 }
b84aa90a
PA
281 if (range_check != current_language->la_range_check)
282 warning (_("the current range check setting "
283 "does not match the language.\n"));
c906108c
SS
284}
285
63872f9d 286/* Show command. Display a warning if the case sensitivity setting does
1777feb0 287 not match the current language. */
63872f9d 288static void
4d28ad1e
AC
289show_case_command (struct ui_file *file, int from_tty,
290 struct cmd_list_element *c, const char *value)
63872f9d 291{
b84aa90a
PA
292 if (case_mode == case_mode_auto)
293 {
a121b7c1 294 const char *tmp = NULL;
b84aa90a
PA
295
296 switch (case_sensitivity)
297 {
298 case case_sensitive_on:
299 tmp = "on";
300 break;
301 case case_sensitive_off:
302 tmp = "off";
303 break;
304 default:
305 internal_error (__FILE__, __LINE__,
306 "Unrecognized case-sensitive setting.");
307 }
308
309 fprintf_filtered (gdb_stdout,
310 _("Case sensitivity in "
311 "name search is \"auto; currently %s\".\n"),
312 tmp);
313 }
314 else
3e43a32a
MS
315 fprintf_filtered (gdb_stdout,
316 _("Case sensitivity in name search is \"%s\".\n"),
b84aa90a
PA
317 value);
318
4d28ad1e 319 if (case_sensitivity != current_language->la_case_sensitivity)
b84aa90a
PA
320 warning (_("the current case sensitivity setting does not match "
321 "the language.\n"));
63872f9d
JG
322}
323
591e78ff
MK
324/* Set command. Change the setting for case sensitivity. */
325
63872f9d 326static void
eb4c3f4a 327set_case_command (const char *ignore, int from_tty, struct cmd_list_element *c)
63872f9d 328{
591e78ff
MK
329 if (strcmp (case_sensitive, "on") == 0)
330 {
331 case_sensitivity = case_sensitive_on;
332 case_mode = case_mode_manual;
333 }
334 else if (strcmp (case_sensitive, "off") == 0)
335 {
336 case_sensitivity = case_sensitive_off;
337 case_mode = case_mode_manual;
338 }
339 else if (strcmp (case_sensitive, "auto") == 0)
340 {
341 case_mode = case_mode_auto;
a451cb65 342 set_range_case ();
591e78ff
MK
343 return;
344 }
63872f9d 345 else
591e78ff 346 {
b84aa90a
PA
347 internal_error (__FILE__, __LINE__,
348 "Unrecognized case-sensitive setting: \"%s\"",
349 case_sensitive);
591e78ff 350 }
b84aa90a
PA
351
352 if (case_sensitivity != current_language->la_case_sensitivity)
353 warning (_("the current case sensitivity setting does not match "
354 "the language.\n"));
63872f9d
JG
355}
356
357/* Set the status of range and type checking and case sensitivity based on
c906108c
SS
358 the current modes and the current language.
359 If SHOW is non-zero, then print out the current language,
1777feb0 360 type and range checking status. */
c906108c 361static void
a451cb65 362set_range_case (void)
c906108c 363{
c906108c
SS
364 if (range_mode == range_mode_auto)
365 range_check = current_language->la_range_check;
366
63872f9d
JG
367 if (case_mode == case_mode_auto)
368 case_sensitivity = current_language->la_case_sensitivity;
c906108c
SS
369}
370
1777feb0
MS
371/* Set current language to (enum language) LANG. Returns previous
372 language. */
c906108c
SS
373
374enum language
fba45db2 375set_language (enum language lang)
c906108c 376{
c906108c
SS
377 enum language prev_language;
378
379 prev_language = current_language->la_language;
47e77640
PA
380 current_language = languages[lang];
381 set_range_case ();
c906108c
SS
382 return prev_language;
383}
384\f
c906108c
SS
385
386/* Print out the current language settings: language, range and
387 type checking. If QUIETLY, print only what has changed. */
388
389void
fba45db2 390language_info (int quietly)
c906108c
SS
391{
392 if (quietly && expected_language == current_language)
393 return;
394
395 expected_language = current_language;
a3f17187 396 printf_unfiltered (_("Current language: %s\n"), language);
4d28ad1e 397 show_language_command (NULL, 1, NULL, NULL);
c906108c
SS
398
399 if (!quietly)
400 {
a3f17187 401 printf_unfiltered (_("Range checking: %s\n"), range);
4d28ad1e 402 show_range_command (NULL, 1, NULL, NULL);
a3f17187 403 printf_unfiltered (_("Case sensitivity: %s\n"), case_sensitive);
4d28ad1e 404 show_case_command (NULL, 1, NULL, NULL);
c906108c
SS
405 }
406}
407\f
c906108c 408
1777feb0 409/* Returns non-zero if the value is a pointer type. */
c906108c 410int
fba45db2 411pointer_type (struct type *type)
c906108c 412{
aa006118 413 return TYPE_CODE (type) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type);
c906108c
SS
414}
415
c906108c 416\f
c906108c 417/* This page contains functions that return info about
1777feb0 418 (struct value) values used in GDB. */
c906108c 419
1777feb0 420/* Returns non-zero if the value VAL represents a true value. */
c906108c 421int
3d6d86c6 422value_true (struct value *val)
c906108c
SS
423{
424 /* It is possible that we should have some sort of error if a non-boolean
425 value is used in this context. Possibly dependent on some kind of
426 "boolean-checking" option like range checking. But it should probably
427 not depend on the language except insofar as is necessary to identify
428 a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
429 should be an error, probably). */
430 return !value_logical_not (val);
431}
432\f
c906108c
SS
433/* This page contains functions for the printing out of
434 error messages that occur during type- and range-
1777feb0 435 checking. */
c906108c 436
a451cb65 437/* This is called when a language fails a range-check. The
ddfe3c15 438 first argument should be a printf()-style format string, and the
a451cb65
KS
439 rest of the arguments should be its arguments. If range_check is
440 range_check_on, an error is printed; if range_check_warn, a warning;
441 otherwise just the message. */
c906108c
SS
442
443void
ddfe3c15 444range_error (const char *string,...)
c906108c 445{
c5aa993b 446 va_list args;
c906108c 447
e0881a8e 448 va_start (args, string);
ddfe3c15
AC
449 switch (range_check)
450 {
451 case range_check_warn:
452 vwarning (string, args);
453 break;
454 case range_check_on:
455 verror (string, args);
456 break;
457 case range_check_off:
458 /* FIXME: cagney/2002-01-30: Should this function print anything
459 when range error is off? */
460 vfprintf_filtered (gdb_stderr, string, args);
461 fprintf_filtered (gdb_stderr, "\n");
462 break;
463 default:
e2e0b3e5 464 internal_error (__FILE__, __LINE__, _("bad switch"));
ddfe3c15 465 }
c5aa993b 466 va_end (args);
c906108c 467}
c906108c 468\f
c5aa993b 469
1777feb0 470/* This page contains miscellaneous functions. */
c906108c 471
1777feb0 472/* Return the language enum for a given language string. */
c906108c
SS
473
474enum language
2039bd9f 475language_enum (const char *str)
c906108c 476{
47e77640
PA
477 for (const auto &lang : languages)
478 if (strcmp (lang->la_name, str) == 0)
479 return lang->la_language;
c906108c 480
47e77640
PA
481 if (strcmp (str, "local") == 0)
482 return language_auto;
c906108c
SS
483
484 return language_unknown;
485}
486
1777feb0 487/* Return the language struct for a given language enum. */
c906108c
SS
488
489const struct language_defn *
fba45db2 490language_def (enum language lang)
c906108c 491{
47e77640 492 return languages[lang];
c906108c
SS
493}
494
1777feb0 495/* Return the language as a string. */
47e77640 496
27cd387b 497const char *
fba45db2 498language_str (enum language lang)
c906108c 499{
47e77640 500 return languages[lang]->la_name;
c906108c
SS
501}
502
503static void
981a3fb3 504set_check (const char *ignore, int from_tty)
c906108c 505{
c5aa993b
JM
506 printf_unfiltered (
507 "\"set check\" must be followed by the name of a check subcommand.\n");
635c7e8a 508 help_list (setchecklist, "set check ", all_commands, gdb_stdout);
c906108c
SS
509}
510
511static void
981a3fb3 512show_check (const char *ignore, int from_tty)
c906108c 513{
c5aa993b 514 cmd_show_list (showchecklist, from_tty, "");
c906108c
SS
515}
516\f
c906108c 517
47e77640 518/* Build and install the "set language LANG" command. */
b84aa90a 519
47e77640
PA
520static void
521add_set_language_command ()
522{
523 static const char **language_names;
c906108c 524
47e77640
PA
525 /* Build the language names array, to be used as enumeration in the
526 "set language" enum command. +1 for "local" and +1 for NULL
527 termination. */
528 language_names = new const char *[ARRAY_SIZE (languages) + 2];
529
530 /* Display "auto", "local" and "unknown" first, and then the rest,
531 alpha sorted. */
532 const char **language_names_p = language_names;
533 *language_names_p++ = auto_language_defn.la_name;
534 *language_names_p++ = "local";
535 *language_names_p++ = unknown_language_defn.la_name;
536 const char **sort_begin = language_names_p;
537 for (const auto &lang : languages)
c906108c 538 {
47e77640
PA
539 /* Already handled above. */
540 if (lang->la_language == language_auto
541 || lang->la_language == language_unknown)
542 continue;
543 *language_names_p++ = lang->la_name;
c906108c 544 }
47e77640
PA
545 *language_names_p = NULL;
546 std::sort (sort_begin, language_names_p, compare_cstrings);
b84aa90a 547
56618e20 548 /* Add the filename extensions. */
47e77640
PA
549 for (const auto &lang : languages)
550 if (lang->la_filename_extensions != NULL)
551 {
552 for (size_t i = 0; lang->la_filename_extensions[i] != NULL; ++i)
553 add_filename_language (lang->la_filename_extensions[i],
554 lang->la_language);
555 }
56618e20 556
b84aa90a 557 /* Build the "help set language" docs. */
d7e74731 558 string_file doc;
b84aa90a 559
d7e74731
PA
560 doc.printf (_("Set the current source language.\n"
561 "The currently understood settings are:\n\nlocal or "
89549d7f 562 "auto Automatic setting based on source file"));
b84aa90a 563
47e77640 564 for (const auto &lang : languages)
b84aa90a
PA
565 {
566 /* Already dealt with these above. */
47e77640
PA
567 if (lang->la_language == language_unknown
568 || lang->la_language == language_auto)
b84aa90a
PA
569 continue;
570
d7e74731
PA
571 /* FIXME: i18n: for now assume that the human-readable name is
572 just a capitalization of the internal name. */
89549d7f
TT
573 /* Note that we add the newline at the front, so we don't wind
574 up with a trailing newline. */
575 doc.printf ("\n%-16s Use the %c%s language",
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
66b8bb74 628/* See language.h. */
8b302db8
TT
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
9d084466
TBA
655/* Return information about whether TYPE should be passed
656 (and returned) by reference at the language level. */
657
658struct language_pass_by_ref_info
41f1b697
DJ
659language_pass_by_reference (struct type *type)
660{
661 return current_language->la_pass_by_reference (type);
662}
663
9d084466
TBA
664/* Return a default struct that provides pass-by-reference information
665 about the given TYPE. Languages should update the default values
666 as appropriate. */
667
668struct language_pass_by_ref_info
41f1b697
DJ
669default_pass_by_reference (struct type *type)
670{
9d084466 671 return {};
41f1b697
DJ
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
b5ec771e
PA
695/* See language.h. */
696
697bool
698default_symbol_name_matcher (const char *symbol_search_name,
699 const lookup_name_info &lookup_name,
a207cff2 700 completion_match_result *comp_match_res)
b5ec771e
PA
701{
702 const std::string &name = lookup_name.name ();
bd69330d
PA
703 completion_match_for_lcd *match_for_lcd
704 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
b5ec771e
PA
705 strncmp_iw_mode mode = (lookup_name.completion_mode ()
706 ? strncmp_iw_mode::NORMAL
707 : strncmp_iw_mode::MATCH_PARAMS);
708
709 if (strncmp_iw_with_mode (symbol_search_name, name.c_str (), name.size (),
bd69330d 710 mode, language_minimal, match_for_lcd) == 0)
b5ec771e 711 {
a207cff2
PA
712 if (comp_match_res != NULL)
713 comp_match_res->set_match (symbol_search_name);
b5ec771e
PA
714 return true;
715 }
716 else
717 return false;
718}
719
720/* See language.h. */
721
4be290b2
AB
722bool
723default_is_string_type_p (struct type *type)
724{
725 type = check_typedef (type);
726 while (TYPE_CODE (type) == TYPE_CODE_REF)
727 {
728 type = TYPE_TARGET_TYPE (type);
729 type = check_typedef (type);
730 }
731 return (TYPE_CODE (type) == TYPE_CODE_STRING);
732}
733
734/* See language.h. */
735
b5ec771e 736symbol_name_matcher_ftype *
618daa93
PA
737get_symbol_name_matcher (const language_defn *lang,
738 const lookup_name_info &lookup_name)
b5ec771e 739{
c63d3e8d
PA
740 /* If currently in Ada mode, and the lookup name is wrapped in
741 '<...>', hijack all symbol name comparisons using the Ada
742 matcher, which handles the verbatim matching. */
743 if (current_language->la_language == language_ada
744 && lookup_name.ada ().verbatim_p ())
745 return current_language->la_get_symbol_name_matcher (lookup_name);
746
b5ec771e
PA
747 if (lang->la_get_symbol_name_matcher != nullptr)
748 return lang->la_get_symbol_name_matcher (lookup_name);
749 return default_symbol_name_matcher;
750}
751
c906108c
SS
752/* Define the language that is no language. */
753
754static int
410a0ff2 755unk_lang_parser (struct parser_state *ps)
c906108c
SS
756{
757 return 1;
758}
759
c906108c 760static void
6c7a06a3
TT
761unk_lang_emit_char (int c, struct type *type, struct ui_file *stream,
762 int quoter)
c906108c 763{
3e43a32a
MS
764 error (_("internal error - unimplemented "
765 "function unk_lang_emit_char called."));
c906108c
SS
766}
767
768static void
6c7a06a3 769unk_lang_printchar (int c, struct type *type, struct ui_file *stream)
c906108c 770{
3e43a32a
MS
771 error (_("internal error - unimplemented "
772 "function unk_lang_printchar called."));
c906108c
SS
773}
774
775static void
6c7a06a3
TT
776unk_lang_printstr (struct ui_file *stream, struct type *type,
777 const gdb_byte *string, unsigned int length,
be759fcf 778 const char *encoding, int force_ellipses,
79a45b7d 779 const struct value_print_options *options)
c906108c 780{
3e43a32a
MS
781 error (_("internal error - unimplemented "
782 "function unk_lang_printstr called."));
c906108c
SS
783}
784
c906108c 785static void
25b524e8 786unk_lang_print_type (struct type *type, const char *varstring,
79d43c61
TT
787 struct ui_file *stream, int show, int level,
788 const struct type_print_options *flags)
c906108c 789{
3e43a32a
MS
790 error (_("internal error - unimplemented "
791 "function unk_lang_print_type called."));
c906108c
SS
792}
793
d3eab38a 794static void
e8b24d9f 795unk_lang_val_print (struct type *type,
a2bd3dcd 796 int embedded_offset, CORE_ADDR address,
79a45b7d 797 struct ui_file *stream, int recurse,
e8b24d9f 798 struct value *val,
79a45b7d 799 const struct value_print_options *options)
c906108c 800{
3e43a32a
MS
801 error (_("internal error - unimplemented "
802 "function unk_lang_val_print called."));
c906108c
SS
803}
804
8e069a98 805static void
79a45b7d
TT
806unk_lang_value_print (struct value *val, struct ui_file *stream,
807 const struct value_print_options *options)
c906108c 808{
3e43a32a
MS
809 error (_("internal error - unimplemented "
810 "function unk_lang_value_print called."));
c906108c
SS
811}
812
52f729a7 813static CORE_ADDR unk_lang_trampoline (struct frame_info *frame, CORE_ADDR pc)
f636b87d
AF
814{
815 return 0;
816}
817
9a3d7dfd
AF
818/* Unknown languages just use the cplus demangler. */
819static char *unk_lang_demangle (const char *mangled, int options)
820{
8de20a37 821 return gdb_demangle (mangled, options);
9a3d7dfd
AF
822}
823
31c27f77
JJ
824static char *unk_lang_class_name (const char *mangled)
825{
826 return NULL;
827}
9a3d7dfd 828
c5aa993b
JM
829static const struct op_print unk_op_print_tab[] =
830{
831 {NULL, OP_NULL, PREC_NULL, 0}
c906108c
SS
832};
833
f290d38e
AC
834static void
835unknown_language_arch_info (struct gdbarch *gdbarch,
836 struct language_arch_info *lai)
837{
838 lai->string_char_type = builtin_type (gdbarch)->builtin_char;
fbb06eb1 839 lai->bool_type_default = builtin_type (gdbarch)->builtin_int;
5a44ea29 840 lai->primitive_type_vector = GDBARCH_OBSTACK_CALLOC (gdbarch, 1,
f290d38e
AC
841 struct type *);
842}
843
c5aa993b
JM
844const struct language_defn unknown_language_defn =
845{
c906108c 846 "unknown",
6abde28f 847 "Unknown",
c906108c 848 language_unknown,
c906108c 849 range_check_off,
63872f9d 850 case_sensitive_on,
9a044a89
TT
851 array_row_major,
852 macro_expansion_no,
56618e20 853 NULL,
5f9769d1 854 &exp_descriptor_standard,
c906108c 855 unk_lang_parser,
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 */
59cc4834 867 true, /* store_sym_names_in_linkage_form_p */
5f9a71c3 868 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 869 basic_lookup_transparent_type,/* lookup_transparent_type */
9a3d7dfd 870 unk_lang_demangle, /* Language specific symbol demangler */
8b302db8 871 NULL,
3e43a32a
MS
872 unk_lang_class_name, /* Language specific
873 class_name_from_physname */
c906108c
SS
874 unk_op_print_tab, /* expression operators for printing */
875 1, /* c-style arrays */
876 0, /* String lower bound */
6084f43a 877 default_word_break_characters,
eb3ff9a5 878 default_collect_symbol_completion_matches,
f290d38e 879 unknown_language_arch_info, /* la_language_arch_info. */
e79af960 880 default_print_array_index,
41f1b697 881 default_pass_by_reference,
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 887 NULL,
721b08c6 888 NULL,
4be290b2 889 default_is_string_type_p,
721b08c6 890 "{...}" /* la_struct_too_deep_ellipsis */
c906108c
SS
891};
892
1777feb0
MS
893/* These two structs define fake entries for the "local" and "auto"
894 options. */
c5aa993b
JM
895const struct language_defn auto_language_defn =
896{
c906108c 897 "auto",
6abde28f 898 "Auto",
c906108c 899 language_auto,
c906108c 900 range_check_off,
63872f9d 901 case_sensitive_on,
9a044a89
TT
902 array_row_major,
903 macro_expansion_no,
56618e20 904 NULL,
5f9769d1 905 &exp_descriptor_standard,
c906108c 906 unk_lang_parser,
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 */
59cc4834 918 false, /* store_sym_names_in_linkage_form_p */
5f9a71c3 919 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 920 basic_lookup_transparent_type,/* lookup_transparent_type */
9a3d7dfd 921 unk_lang_demangle, /* Language specific symbol demangler */
8b302db8 922 NULL,
3e43a32a
MS
923 unk_lang_class_name, /* Language specific
924 class_name_from_physname */
c906108c
SS
925 unk_op_print_tab, /* expression operators for printing */
926 1, /* c-style arrays */
927 0, /* String lower bound */
6084f43a 928 default_word_break_characters,
eb3ff9a5 929 default_collect_symbol_completion_matches,
f290d38e 930 unknown_language_arch_info, /* la_language_arch_info. */
e79af960 931 default_print_array_index,
41f1b697 932 default_pass_by_reference,
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 938 NULL,
721b08c6 939 NULL,
4be290b2 940 default_is_string_type_p,
721b08c6 941 "{...}" /* la_struct_too_deep_ellipsis */
c906108c
SS
942};
943
c906108c 944\f
f290d38e
AC
945/* Per-architecture language information. */
946
947static struct gdbarch_data *language_gdbarch_data;
948
949struct language_gdbarch
950{
951 /* A vector of per-language per-architecture info. Indexed by "enum
952 language". */
953 struct language_arch_info arch_info[nr_languages];
954};
955
956static void *
957language_gdbarch_post_init (struct gdbarch *gdbarch)
958{
959 struct language_gdbarch *l;
f290d38e
AC
960
961 l = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct language_gdbarch);
47e77640
PA
962 for (const auto &lang : languages)
963 if (lang != NULL && lang->la_language_arch_info != NULL)
964 {
965 lang->la_language_arch_info (gdbarch,
966 l->arch_info + lang->la_language);
967 }
968
f290d38e
AC
969 return l;
970}
971
972struct type *
973language_string_char_type (const struct language_defn *la,
974 struct gdbarch *gdbarch)
975{
9a3c8263
SM
976 struct language_gdbarch *ld
977 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
e0881a8e 978
aba2dd37 979 return ld->arch_info[la->la_language].string_char_type;
f290d38e
AC
980}
981
fbb06eb1
UW
982struct type *
983language_bool_type (const struct language_defn *la,
984 struct gdbarch *gdbarch)
985{
9a3c8263
SM
986 struct language_gdbarch *ld
987 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
fbb06eb1
UW
988
989 if (ld->arch_info[la->la_language].bool_type_symbol)
990 {
991 struct symbol *sym;
e0881a8e 992
fbb06eb1 993 sym = lookup_symbol (ld->arch_info[la->la_language].bool_type_symbol,
d12307c1 994 NULL, VAR_DOMAIN, NULL).symbol;
fbb06eb1
UW
995 if (sym)
996 {
997 struct type *type = SYMBOL_TYPE (sym);
e0881a8e 998
fbb06eb1
UW
999 if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
1000 return type;
1001 }
1002 }
1003
1004 return ld->arch_info[la->la_language].bool_type_default;
1005}
1006
1994afbf
DE
1007/* Helper function for primitive type lookup. */
1008
1009static struct type **
1010language_lookup_primitive_type_1 (const struct language_arch_info *lai,
1011 const char *name)
1012{
1013 struct type **p;
1014
1015 for (p = lai->primitive_type_vector; (*p) != NULL; p++)
1016 {
1017 if (strcmp (TYPE_NAME (*p), name) == 0)
1018 return p;
1019 }
1020 return NULL;
1021}
1022
1023/* See language.h. */
1024
f290d38e 1025struct type *
46b0da17
DE
1026language_lookup_primitive_type (const struct language_defn *la,
1027 struct gdbarch *gdbarch,
1028 const char *name)
f290d38e 1029{
9a3c8263
SM
1030 struct language_gdbarch *ld =
1031 (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1994afbf
DE
1032 struct type **typep;
1033
1034 typep = language_lookup_primitive_type_1 (&ld->arch_info[la->la_language],
1035 name);
1036 if (typep == NULL)
1037 return NULL;
1038 return *typep;
1039}
1040
1041/* Helper function for type lookup as a symbol.
1042 Create the symbol corresponding to type TYPE in language LANG. */
1043
1044static struct symbol *
1045language_alloc_type_symbol (enum language lang, struct type *type)
1046{
1047 struct symbol *symbol;
1048 struct gdbarch *gdbarch;
1049
1050 gdb_assert (!TYPE_OBJFILE_OWNED (type));
1051
1052 gdbarch = TYPE_OWNER (type).gdbarch;
468c0cbb 1053 symbol = new (gdbarch_obstack (gdbarch)) struct symbol ();
1994afbf 1054
4d4eaa30 1055 symbol->m_name = TYPE_NAME (type);
d3ecddab 1056 symbol->set_language (lang, nullptr);
1994afbf
DE
1057 symbol->owner.arch = gdbarch;
1058 SYMBOL_OBJFILE_OWNED (symbol) = 0;
1059 SYMBOL_TYPE (symbol) = type;
1060 SYMBOL_DOMAIN (symbol) = VAR_DOMAIN;
1061 SYMBOL_ACLASS_INDEX (symbol) = LOC_TYPEDEF;
1062
1063 return symbol;
1064}
1065
1066/* Initialize the primitive type symbols of language LD.
1067 The primitive type vector must have already been initialized. */
1068
1069static void
1070language_init_primitive_type_symbols (struct language_arch_info *lai,
1071 const struct language_defn *la,
1072 struct gdbarch *gdbarch)
1073{
1074 int n;
1994afbf
DE
1075
1076 gdb_assert (lai->primitive_type_vector != NULL);
1077
1078 for (n = 0; lai->primitive_type_vector[n] != NULL; ++n)
1079 continue;
1080
1081 lai->primitive_type_symbols
1082 = GDBARCH_OBSTACK_CALLOC (gdbarch, n + 1, struct symbol *);
1083
1084 for (n = 0; lai->primitive_type_vector[n] != NULL; ++n)
1085 {
1086 lai->primitive_type_symbols[n]
1087 = language_alloc_type_symbol (la->la_language,
1088 lai->primitive_type_vector[n]);
1089 }
1090
1091 /* Note: The result of symbol lookup is normally a symbol *and* the block
d12307c1
PMR
1092 it was found in. Builtin types don't live in blocks. We *could* give
1093 them one, but there is no current need so to keep things simple symbol
1094 lookup is extended to allow for BLOCK_FOUND to be NULL. */
1994afbf
DE
1095}
1096
1097/* See language.h. */
1098
1099struct symbol *
1100language_lookup_primitive_type_as_symbol (const struct language_defn *la,
1101 struct gdbarch *gdbarch,
1102 const char *name)
1103{
9a3c8263
SM
1104 struct language_gdbarch *ld
1105 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1994afbf
DE
1106 struct language_arch_info *lai = &ld->arch_info[la->la_language];
1107 struct type **typep;
1108 struct symbol *sym;
e0881a8e 1109
cc485e62
DE
1110 if (symbol_lookup_debug)
1111 {
1112 fprintf_unfiltered (gdb_stdlog,
1994afbf
DE
1113 "language_lookup_primitive_type_as_symbol"
1114 " (%s, %s, %s)",
cc485e62
DE
1115 la->la_name, host_address_to_string (gdbarch), name);
1116 }
1117
1994afbf
DE
1118 typep = language_lookup_primitive_type_1 (lai, name);
1119 if (typep == NULL)
f290d38e 1120 {
1994afbf
DE
1121 if (symbol_lookup_debug)
1122 fprintf_unfiltered (gdb_stdlog, " = NULL\n");
1123 return NULL;
f290d38e 1124 }
cc485e62 1125
1994afbf
DE
1126 /* The set of symbols is lazily initialized. */
1127 if (lai->primitive_type_symbols == NULL)
1128 language_init_primitive_type_symbols (lai, la, gdbarch);
1129
1130 sym = lai->primitive_type_symbols[typep - lai->primitive_type_vector];
1131
cc485e62 1132 if (symbol_lookup_debug)
1994afbf
DE
1133 fprintf_unfiltered (gdb_stdlog, " = %s\n", host_address_to_string (sym));
1134 return sym;
f290d38e
AC
1135}
1136
1777feb0 1137/* Initialize the language routines. */
c906108c
SS
1138
1139void
fba45db2 1140_initialize_language (void)
c906108c 1141{
40478521 1142 static const char *const type_or_range_names[]
b84aa90a
PA
1143 = { "on", "off", "warn", "auto", NULL };
1144
40478521 1145 static const char *const case_sensitive_names[]
b84aa90a 1146 = { "on", "off", "auto", NULL };
c5aa993b 1147
f290d38e
AC
1148 language_gdbarch_data
1149 = gdbarch_data_register_post_init (language_gdbarch_post_init);
1150
1777feb0 1151 /* GDB commands for language specific stuff. */
c5aa993b 1152
c5aa993b 1153 add_prefix_cmd ("check", no_class, set_check,
1bedd215 1154 _("Set the status of the type/range checker."),
c5aa993b
JM
1155 &setchecklist, "set check ", 0, &setlist);
1156 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1157 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1158
1159 add_prefix_cmd ("check", no_class, show_check,
1bedd215 1160 _("Show the status of the type/range checker."),
c5aa993b
JM
1161 &showchecklist, "show check ", 0, &showlist);
1162 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1163 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1164
b84aa90a 1165 add_setshow_enum_cmd ("range", class_support, type_or_range_names,
3e43a32a 1166 &range,
590042fc
PW
1167 _("Set range checking (on/warn/off/auto)."),
1168 _("Show range checking (on/warn/off/auto)."),
3e43a32a 1169 NULL, set_range_command,
b84aa90a
PA
1170 show_range_command,
1171 &setchecklist, &showchecklist);
1172
1173 add_setshow_enum_cmd ("case-sensitive", class_support, case_sensitive_names,
1174 &case_sensitive, _("\
590042fc
PW
1175Set case sensitivity in name search (on/off/auto)."), _("\
1176Show case sensitivity in name search (on/off/auto)."), _("\
4d28ad1e 1177For Fortran the default is off; for other languages the default is on."),
b84aa90a
PA
1178 set_case_command,
1179 show_case_command,
1180 &setlist, &showlist);
63872f9d 1181
47e77640 1182 add_set_language_command ();
c5aa993b 1183
4a811000
PW
1184 language = "auto";
1185 range = "auto";
1186 case_sensitive = "auto";
63872f9d 1187
1777feb0 1188 /* Have the above take effect. */
63872f9d 1189 set_language (language_auto);
c906108c 1190}
This page took 1.748427 seconds and 4 git commands to generate.