Extend "x" and "print" commands to support memory tagging
[deliverable/binutils-gdb.git] / gdb / language.c
CommitLineData
c906108c 1/* Multiple source language support for GDB.
1bac305b 2
3666a048 3 Copyright (C) 1991-2021 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"
bdfea17e 49#include "compile/compile-internal.h"
c906108c 50
a451cb65 51static void set_range_case (void);
c906108c 52
efdf6a73
AB
53/* range_mode ==
54 range_mode_auto: range_check set automatically to default of language.
55 range_mode_manual: range_check set manually by user. */
56
57enum range_mode
58 {
59 range_mode_auto, range_mode_manual
60 };
61
0d201fa4
AB
62/* case_mode ==
63 case_mode_auto: case_sensitivity set upon selection of scope.
64 case_mode_manual: case_sensitivity set only by user. */
65
66enum case_mode
67 {
68 case_mode_auto, case_mode_manual
69 };
70
c906108c 71/* The current (default at startup) state of type and range checking.
c5aa993b
JM
72 (If the modes are set to "auto", though, these are changed based
73 on the default language at startup, and then again based on the
74 language of the first source file. */
c906108c 75
efdf6a73 76static enum range_mode range_mode = range_mode_auto;
c906108c 77enum range_check range_check = range_check_off;
0d201fa4 78static enum case_mode case_mode = case_mode_auto;
63872f9d 79enum case_sensitivity case_sensitivity = case_sensitive_on;
c906108c 80
1777feb0 81/* The current language and language_mode (see language.h). */
c906108c 82
0874fd07 83const struct language_defn *current_language = nullptr;
c906108c
SS
84enum language_mode language_mode = language_mode_auto;
85
86/* The language that the user expects to be typing in (the language
87 of main(), or the last language we notified them about, or C). */
88
89const struct language_defn *expected_language;
90
0874fd07
AB
91/* Define the array containing all languages. */
92
93const struct language_defn *language_defn::languages[nr_languages];
c906108c 94
814fa4f6 95/* The current values of the "set language/range/case-sensitive" enum
b84aa90a
PA
96 commands. */
97static const char *language;
b84aa90a
PA
98static const char *range;
99static const char *case_sensitive;
c906108c 100
34916edc
CB
101/* See language.h. */
102const char lang_frame_mismatch_warn[] =
103N_("Warning: the current language does not match this frame.");
c906108c
SS
104\f
105/* This page contains the functions corresponding to GDB commands
1777feb0 106 and their helpers. */
c906108c
SS
107
108/* Show command. Display a warning if the language set
1777feb0 109 does not match the frame. */
c906108c 110static void
4d28ad1e
AC
111show_language_command (struct ui_file *file, int from_tty,
112 struct cmd_list_element *c, const char *value)
c906108c 113{
7ff38b1c 114 enum language flang; /* The language of the frame. */
c906108c 115
b84aa90a
PA
116 if (language_mode == language_mode_auto)
117 fprintf_filtered (gdb_stdout,
118 _("The current source language is "
119 "\"auto; currently %s\".\n"),
6f7664a9 120 current_language->name ());
b84aa90a 121 else
3e43a32a
MS
122 fprintf_filtered (gdb_stdout,
123 _("The current source language is \"%s\".\n"),
6f7664a9 124 current_language->name ());
b84aa90a 125
7ff38b1c
AB
126 if (has_stack_frames ())
127 {
128 struct frame_info *frame;
129
130 frame = get_selected_frame (NULL);
131 flang = get_frame_language (frame);
132 if (flang != language_unknown
133 && language_mode == language_mode_manual
134 && current_language->la_language != flang)
34916edc 135 printf_filtered ("%s\n", _(lang_frame_mismatch_warn));
7ff38b1c 136 }
c906108c
SS
137}
138
1777feb0 139/* Set command. Change the current working language. */
c906108c 140static void
eb4c3f4a
TT
141set_language_command (const char *ignore,
142 int from_tty, struct cmd_list_element *c)
c906108c 143{
7ff38b1c 144 enum language flang = language_unknown;
c906108c 145
47e77640
PA
146 /* "local" is a synonym of "auto". */
147 if (strcmp (language, "local") == 0)
148 language = "auto";
149
c906108c 150 /* Search the list of languages for a match. */
0874fd07 151 for (const auto &lang : language_defn::languages)
c5aa993b 152 {
6f7664a9 153 if (strcmp (lang->name (), language) == 0)
c5aa993b
JM
154 {
155 /* Found it! Go into manual mode, and use this language. */
47e77640 156 if (lang->la_language == language_auto)
c5aa993b 157 {
8b60591b 158 /* Enter auto mode. Set to the current frame's language, if
dda83cd7 159 known, or fallback to the initial language. */
c5aa993b 160 language_mode = language_mode_auto;
a70b8144 161 try
7ff38b1c
AB
162 {
163 struct frame_info *frame;
164
165 frame = get_selected_frame (NULL);
166 flang = get_frame_language (frame);
167 }
230d2906 168 catch (const gdb_exception_error &ex)
7ff38b1c
AB
169 {
170 flang = language_unknown;
171 }
7ff38b1c 172
c5aa993b
JM
173 if (flang != language_unknown)
174 set_language (flang);
8b60591b
JB
175 else
176 set_initial_language ();
c5aa993b
JM
177 expected_language = current_language;
178 return;
179 }
180 else
181 {
182 /* Enter manual mode. Set the specified language. */
183 language_mode = language_mode_manual;
47e77640 184 current_language = lang;
a451cb65 185 set_range_case ();
c5aa993b
JM
186 expected_language = current_language;
187 return;
188 }
189 }
c906108c 190 }
c906108c 191
b84aa90a
PA
192 internal_error (__FILE__, __LINE__,
193 "Couldn't find language `%s' in known languages list.",
194 language);
c906108c
SS
195}
196
c906108c 197/* Show command. Display a warning if the range setting does
1777feb0 198 not match the current language. */
c906108c 199static void
4d28ad1e
AC
200show_range_command (struct ui_file *file, int from_tty,
201 struct cmd_list_element *c, const char *value)
c906108c 202{
b84aa90a
PA
203 if (range_mode == range_mode_auto)
204 {
a121b7c1 205 const char *tmp;
b84aa90a
PA
206
207 switch (range_check)
208 {
209 case range_check_on:
210 tmp = "on";
211 break;
212 case range_check_off:
213 tmp = "off";
214 break;
215 case range_check_warn:
216 tmp = "warn";
217 break;
218 default:
219 internal_error (__FILE__, __LINE__,
220 "Unrecognized range check setting.");
221 }
222
223 fprintf_filtered (gdb_stdout,
224 _("Range checking is \"auto; currently %s\".\n"),
225 tmp);
226 }
227 else
228 fprintf_filtered (gdb_stdout, _("Range checking is \"%s\".\n"),
229 value);
230
efdf6a73
AB
231 if (range_check == range_check_warn
232 || ((range_check == range_check_on)
233 != current_language->range_checking_on_by_default ()))
b84aa90a
PA
234 warning (_("the current range check setting "
235 "does not match the language.\n"));
c906108c
SS
236}
237
1777feb0 238/* Set command. Change the setting for range checking. */
c906108c 239static void
eb4c3f4a
TT
240set_range_command (const char *ignore,
241 int from_tty, struct cmd_list_element *c)
c906108c 242{
6314a349 243 if (strcmp (range, "on") == 0)
c5aa993b 244 {
c906108c
SS
245 range_check = range_check_on;
246 range_mode = range_mode_manual;
c5aa993b 247 }
6314a349 248 else if (strcmp (range, "warn") == 0)
c5aa993b 249 {
c906108c
SS
250 range_check = range_check_warn;
251 range_mode = range_mode_manual;
c5aa993b 252 }
6314a349 253 else if (strcmp (range, "off") == 0)
c5aa993b 254 {
c906108c
SS
255 range_check = range_check_off;
256 range_mode = range_mode_manual;
c5aa993b 257 }
6314a349 258 else if (strcmp (range, "auto") == 0)
c5aa993b 259 {
c906108c 260 range_mode = range_mode_auto;
a451cb65 261 set_range_case ();
c906108c 262 return;
c5aa993b 263 }
c4093a6a
JM
264 else
265 {
b84aa90a
PA
266 internal_error (__FILE__, __LINE__,
267 _("Unrecognized range check setting: \"%s\""), range);
c4093a6a 268 }
efdf6a73
AB
269 if (range_check == range_check_warn
270 || ((range_check == range_check_on)
271 != current_language->range_checking_on_by_default ()))
b84aa90a
PA
272 warning (_("the current range check setting "
273 "does not match the language.\n"));
c906108c
SS
274}
275
63872f9d 276/* Show command. Display a warning if the case sensitivity setting does
1777feb0 277 not match the current language. */
63872f9d 278static void
4d28ad1e
AC
279show_case_command (struct ui_file *file, int from_tty,
280 struct cmd_list_element *c, const char *value)
63872f9d 281{
b84aa90a
PA
282 if (case_mode == case_mode_auto)
283 {
a121b7c1 284 const char *tmp = NULL;
b84aa90a
PA
285
286 switch (case_sensitivity)
287 {
288 case case_sensitive_on:
289 tmp = "on";
290 break;
291 case case_sensitive_off:
292 tmp = "off";
293 break;
294 default:
295 internal_error (__FILE__, __LINE__,
296 "Unrecognized case-sensitive setting.");
297 }
298
299 fprintf_filtered (gdb_stdout,
300 _("Case sensitivity in "
301 "name search is \"auto; currently %s\".\n"),
302 tmp);
303 }
304 else
3e43a32a
MS
305 fprintf_filtered (gdb_stdout,
306 _("Case sensitivity in name search is \"%s\".\n"),
b84aa90a
PA
307 value);
308
0d201fa4 309 if (case_sensitivity != current_language->case_sensitivity ())
b84aa90a
PA
310 warning (_("the current case sensitivity setting does not match "
311 "the language.\n"));
63872f9d
JG
312}
313
591e78ff
MK
314/* Set command. Change the setting for case sensitivity. */
315
63872f9d 316static void
eb4c3f4a 317set_case_command (const char *ignore, int from_tty, struct cmd_list_element *c)
63872f9d 318{
591e78ff
MK
319 if (strcmp (case_sensitive, "on") == 0)
320 {
321 case_sensitivity = case_sensitive_on;
322 case_mode = case_mode_manual;
323 }
324 else if (strcmp (case_sensitive, "off") == 0)
325 {
326 case_sensitivity = case_sensitive_off;
327 case_mode = case_mode_manual;
328 }
329 else if (strcmp (case_sensitive, "auto") == 0)
330 {
331 case_mode = case_mode_auto;
a451cb65 332 set_range_case ();
591e78ff
MK
333 return;
334 }
63872f9d 335 else
591e78ff 336 {
b84aa90a
PA
337 internal_error (__FILE__, __LINE__,
338 "Unrecognized case-sensitive setting: \"%s\"",
339 case_sensitive);
591e78ff 340 }
b84aa90a 341
0d201fa4 342 if (case_sensitivity != current_language->case_sensitivity ())
b84aa90a
PA
343 warning (_("the current case sensitivity setting does not match "
344 "the language.\n"));
63872f9d
JG
345}
346
347/* Set the status of range and type checking and case sensitivity based on
c906108c
SS
348 the current modes and the current language.
349 If SHOW is non-zero, then print out the current language,
1777feb0 350 type and range checking status. */
c906108c 351static void
a451cb65 352set_range_case (void)
c906108c 353{
c906108c 354 if (range_mode == range_mode_auto)
efdf6a73
AB
355 range_check = (current_language->range_checking_on_by_default ()
356 ? range_check_on : range_check_off);
c906108c 357
63872f9d 358 if (case_mode == case_mode_auto)
0d201fa4 359 case_sensitivity = current_language->case_sensitivity ();
c906108c
SS
360}
361
1777feb0
MS
362/* Set current language to (enum language) LANG. Returns previous
363 language. */
c906108c
SS
364
365enum language
fba45db2 366set_language (enum language lang)
c906108c 367{
c906108c
SS
368 enum language prev_language;
369
370 prev_language = current_language->la_language;
0874fd07 371 current_language = language_def (lang);
47e77640 372 set_range_case ();
c906108c
SS
373 return prev_language;
374}
375\f
c906108c
SS
376
377/* Print out the current language settings: language, range and
378 type checking. If QUIETLY, print only what has changed. */
379
380void
fba45db2 381language_info (int quietly)
c906108c
SS
382{
383 if (quietly && expected_language == current_language)
384 return;
385
386 expected_language = current_language;
a3f17187 387 printf_unfiltered (_("Current language: %s\n"), language);
4d28ad1e 388 show_language_command (NULL, 1, NULL, NULL);
c906108c
SS
389
390 if (!quietly)
391 {
a3f17187 392 printf_unfiltered (_("Range checking: %s\n"), range);
4d28ad1e 393 show_range_command (NULL, 1, NULL, NULL);
a3f17187 394 printf_unfiltered (_("Case sensitivity: %s\n"), case_sensitive);
4d28ad1e 395 show_case_command (NULL, 1, NULL, NULL);
c906108c
SS
396 }
397}
398\f
c906108c 399
1777feb0 400/* Returns non-zero if the value is a pointer type. */
c906108c 401int
fba45db2 402pointer_type (struct type *type)
c906108c 403{
78134374 404 return type->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type);
c906108c
SS
405}
406
c906108c 407\f
c906108c 408/* This page contains functions that return info about
1777feb0 409 (struct value) values used in GDB. */
c906108c 410
1777feb0 411/* Returns non-zero if the value VAL represents a true value. */
c906108c 412int
3d6d86c6 413value_true (struct value *val)
c906108c
SS
414{
415 /* It is possible that we should have some sort of error if a non-boolean
416 value is used in this context. Possibly dependent on some kind of
417 "boolean-checking" option like range checking. But it should probably
418 not depend on the language except insofar as is necessary to identify
419 a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
420 should be an error, probably). */
421 return !value_logical_not (val);
422}
423\f
c906108c
SS
424/* This page contains functions for the printing out of
425 error messages that occur during type- and range-
1777feb0 426 checking. */
c906108c 427
a451cb65 428/* This is called when a language fails a range-check. The
ddfe3c15 429 first argument should be a printf()-style format string, and the
a451cb65
KS
430 rest of the arguments should be its arguments. If range_check is
431 range_check_on, an error is printed; if range_check_warn, a warning;
432 otherwise just the message. */
c906108c
SS
433
434void
ddfe3c15 435range_error (const char *string,...)
c906108c 436{
c5aa993b 437 va_list args;
c906108c 438
e0881a8e 439 va_start (args, string);
ddfe3c15
AC
440 switch (range_check)
441 {
442 case range_check_warn:
443 vwarning (string, args);
444 break;
445 case range_check_on:
446 verror (string, args);
447 break;
448 case range_check_off:
449 /* FIXME: cagney/2002-01-30: Should this function print anything
dda83cd7 450 when range error is off? */
ddfe3c15
AC
451 vfprintf_filtered (gdb_stderr, string, args);
452 fprintf_filtered (gdb_stderr, "\n");
453 break;
454 default:
e2e0b3e5 455 internal_error (__FILE__, __LINE__, _("bad switch"));
ddfe3c15 456 }
c5aa993b 457 va_end (args);
c906108c 458}
c906108c 459\f
c5aa993b 460
1777feb0 461/* This page contains miscellaneous functions. */
c906108c 462
1777feb0 463/* Return the language enum for a given language string. */
c906108c
SS
464
465enum language
2039bd9f 466language_enum (const char *str)
c906108c 467{
0874fd07 468 for (const auto &lang : language_defn::languages)
6f7664a9 469 if (strcmp (lang->name (), str) == 0)
47e77640 470 return lang->la_language;
c906108c 471
47e77640
PA
472 if (strcmp (str, "local") == 0)
473 return language_auto;
c906108c
SS
474
475 return language_unknown;
476}
477
1777feb0 478/* Return the language struct for a given language enum. */
c906108c
SS
479
480const struct language_defn *
fba45db2 481language_def (enum language lang)
c906108c 482{
0874fd07
AB
483 const struct language_defn *l = language_defn::languages[lang];
484 gdb_assert (l != nullptr);
485 return l;
c906108c
SS
486}
487
1777feb0 488/* Return the language as a string. */
47e77640 489
27cd387b 490const char *
fba45db2 491language_str (enum language lang)
c906108c 492{
6f7664a9 493 return language_def (lang)->name ();
c906108c
SS
494}
495
c906108c 496\f
c906108c 497
47e77640 498/* Build and install the "set language LANG" command. */
b84aa90a 499
47e77640
PA
500static void
501add_set_language_command ()
502{
503 static const char **language_names;
c906108c 504
47e77640
PA
505 /* Build the language names array, to be used as enumeration in the
506 "set language" enum command. +1 for "local" and +1 for NULL
507 termination. */
0874fd07 508 language_names = new const char *[ARRAY_SIZE (language_defn::languages) + 2];
47e77640
PA
509
510 /* Display "auto", "local" and "unknown" first, and then the rest,
511 alpha sorted. */
512 const char **language_names_p = language_names;
6f7664a9 513 *language_names_p++ = language_def (language_auto)->name ();
47e77640 514 *language_names_p++ = "local";
6f7664a9 515 *language_names_p++ = language_def (language_unknown)->name ();
47e77640 516 const char **sort_begin = language_names_p;
0874fd07 517 for (const auto &lang : language_defn::languages)
c906108c 518 {
47e77640
PA
519 /* Already handled above. */
520 if (lang->la_language == language_auto
521 || lang->la_language == language_unknown)
522 continue;
6f7664a9 523 *language_names_p++ = lang->name ();
c906108c 524 }
47e77640
PA
525 *language_names_p = NULL;
526 std::sort (sort_begin, language_names_p, compare_cstrings);
b84aa90a 527
56618e20 528 /* Add the filename extensions. */
0874fd07 529 for (const auto &lang : language_defn::languages)
e171d6f1
AB
530 for (const char * const &ext : lang->filename_extensions ())
531 add_filename_language (ext, lang->la_language);
56618e20 532
b84aa90a 533 /* Build the "help set language" docs. */
d7e74731 534 string_file doc;
b84aa90a 535
d7e74731
PA
536 doc.printf (_("Set the current source language.\n"
537 "The currently understood settings are:\n\nlocal or "
89549d7f 538 "auto Automatic setting based on source file"));
b84aa90a 539
0874fd07 540 for (const auto &lang : language_defn::languages)
b84aa90a
PA
541 {
542 /* Already dealt with these above. */
47e77640
PA
543 if (lang->la_language == language_unknown
544 || lang->la_language == language_auto)
b84aa90a
PA
545 continue;
546
89549d7f
TT
547 /* Note that we add the newline at the front, so we don't wind
548 up with a trailing newline. */
6f7664a9
AB
549 doc.printf ("\n%-16s Use the %s language",
550 lang->name (),
551 lang->natural_name ());
b84aa90a
PA
552 }
553
b84aa90a 554 add_setshow_enum_cmd ("language", class_support,
47e77640 555 language_names,
b84aa90a 556 &language,
d7e74731 557 doc.c_str (),
3e43a32a
MS
558 _("Show the current source language."),
559 NULL, set_language_command,
b84aa90a
PA
560 show_language_command,
561 &setlist, &showlist);
c906108c
SS
562}
563
f636b87d
AF
564/* Iterate through all registered languages looking for and calling
565 any non-NULL struct language_defn.skip_trampoline() functions.
566 Return the result from the first that returns non-zero, or 0 if all
567 `fail'. */
568CORE_ADDR
52f729a7 569skip_language_trampoline (struct frame_info *frame, CORE_ADDR pc)
f636b87d 570{
0874fd07 571 for (const auto &lang : language_defn::languages)
f636b87d 572 {
f6eee2d0 573 CORE_ADDR real_pc = lang->skip_trampoline (frame, pc);
e0881a8e 574
f6eee2d0
AB
575 if (real_pc != 0)
576 return real_pc;
f636b87d
AF
577 }
578
579 return 0;
580}
581
1777feb0 582/* Return demangled language symbol, or NULL.
9a3d7dfd
AF
583 FIXME: Options are only useful for certain languages and ignored
584 by others, so it would be better to remove them here and have a
1777feb0 585 more flexible demangler for the languages that need it.
9a3d7dfd
AF
586 FIXME: Sometimes the demangler is invoked when we don't know the
587 language, so we can't use this everywhere. */
588char *
589language_demangle (const struct language_defn *current_language,
590 const char *mangled, int options)
591{
0a50df5d 592 if (current_language != NULL)
5399db93 593 return current_language->demangle_symbol (mangled, options);
9a3d7dfd
AF
594 return NULL;
595}
596
9d084466
TBA
597/* Return information about whether TYPE should be passed
598 (and returned) by reference at the language level. */
599
600struct language_pass_by_ref_info
41f1b697
DJ
601language_pass_by_reference (struct type *type)
602{
48448202 603 return current_language->pass_by_reference_info (type);
41f1b697
DJ
604}
605
9f0a5303
JB
606/* Return the default string containing the list of characters
607 delimiting words. This is a reasonable default value that
608 most languages should be able to use. */
609
67cb5b2d 610const char *
9f0a5303
JB
611default_word_break_characters (void)
612{
613 return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
614}
f636b87d 615
5bd40f2a 616/* See language.h. */
e79af960
JB
617
618void
5bd40f2a
AB
619language_defn::print_array_index (struct type *index_type, LONGEST index,
620 struct ui_file *stream,
621 const value_print_options *options) const
e79af960 622{
53a47a3e
TT
623 struct value *index_value = value_from_longest (index_type, index);
624
e79af960 625 fprintf_filtered (stream, "[");
00c696a6 626 value_print (index_value, stream, options);
e79af960
JB
627 fprintf_filtered (stream, "] = ");
628}
629
f16a9f57
AB
630/* See language.h. */
631
632gdb::unique_xmalloc_ptr<char>
633language_defn::watch_location_expression (struct type *type,
634 CORE_ADDR addr) const
635{
636 /* Generates an expression that assumes a C like syntax is valid. */
637 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
638 std::string name = type_to_string (type);
639 return gdb::unique_xmalloc_ptr<char>
640 (xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr)));
641}
642
a1d1fa3e
AB
643/* See language.h. */
644
645void
646language_defn::value_print (struct value *val, struct ui_file *stream,
647 const struct value_print_options *options) const
648{
649 return c_value_print (val, stream, options);
650}
651
ebe2334e
AB
652/* See language.h. */
653
87afa652
AB
654int
655language_defn::parser (struct parser_state *ps) const
656{
657 return c_parse (ps);
658}
659
660/* See language.h. */
661
ebe2334e
AB
662void
663language_defn::value_print_inner
664 (struct value *val, struct ui_file *stream, int recurse,
665 const struct value_print_options *options) const
666{
667 return c_value_print_inner (val, stream, recurse, options);
668}
669
ec8cec5b
AB
670/* See language.h. */
671
672void
673language_defn::emitchar (int ch, struct type *chtype,
674 struct ui_file * stream, int quoter) const
675{
676 c_emit_char (ch, chtype, stream, quoter);
677}
678
52b50f2c
AB
679/* See language.h. */
680
d711ee67
AB
681void
682language_defn::printstr (struct ui_file *stream, struct type *elttype,
683 const gdb_byte *string, unsigned int length,
684 const char *encoding, int force_ellipses,
685 const struct value_print_options *options) const
686{
687 c_printstr (stream, elttype, string, length, encoding, force_ellipses,
688 options);
689}
690
4ffc13fb
AB
691/* See language.h. */
692
693void
694language_defn::print_typedef (struct type *type, struct symbol *new_symbol,
695 struct ui_file *stream) const
696{
697 c_print_typedef (type, new_symbol, stream);
698}
699
39e7ecca
AB
700/* See language.h. */
701
702bool
703language_defn::is_string_type_p (struct type *type) const
704{
705 return c_is_string_type_p (type);
706}
707
bdfea17e
TT
708/* See language.h. */
709
710std::unique_ptr<compile_instance>
711language_defn::get_compile_instance () const
712{
713 return {};
714}
715
c9debfb9
AB
716/* The default implementation of the get_symbol_name_matcher_inner method
717 from the language_defn class. Matches with strncmp_iw. */
b5ec771e 718
c9debfb9 719static bool
b5ec771e
PA
720default_symbol_name_matcher (const char *symbol_search_name,
721 const lookup_name_info &lookup_name,
a207cff2 722 completion_match_result *comp_match_res)
b5ec771e 723{
e0802d59 724 gdb::string_view name = lookup_name.name ();
bd69330d
PA
725 completion_match_for_lcd *match_for_lcd
726 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
b5ec771e
PA
727 strncmp_iw_mode mode = (lookup_name.completion_mode ()
728 ? strncmp_iw_mode::NORMAL
729 : strncmp_iw_mode::MATCH_PARAMS);
730
e0802d59 731 if (strncmp_iw_with_mode (symbol_search_name, name.data (), name.size (),
bd69330d 732 mode, language_minimal, match_for_lcd) == 0)
b5ec771e 733 {
a207cff2
PA
734 if (comp_match_res != NULL)
735 comp_match_res->set_match (symbol_search_name);
b5ec771e
PA
736 return true;
737 }
738 else
739 return false;
740}
741
742/* See language.h. */
743
c9debfb9
AB
744symbol_name_matcher_ftype *
745language_defn::get_symbol_name_matcher
746 (const lookup_name_info &lookup_name) const
747{
748 /* If currently in Ada mode, and the lookup name is wrapped in
749 '<...>', hijack all symbol name comparisons using the Ada
750 matcher, which handles the verbatim matching. */
751 if (current_language->la_language == language_ada
752 && lookup_name.ada ().verbatim_p ())
753 return current_language->get_symbol_name_matcher_inner (lookup_name);
754
755 return this->get_symbol_name_matcher_inner (lookup_name);
756}
757
758/* See language.h. */
759
760symbol_name_matcher_ftype *
761language_defn::get_symbol_name_matcher_inner
762 (const lookup_name_info &lookup_name) const
763{
764 return default_symbol_name_matcher;
765}
766
b63a3f3f
AB
767/* See language.h. */
768
769const struct lang_varobj_ops *
770language_defn::varobj_ops () const
771{
772 /* The ops for the C language are suitable for the vast majority of the
773 supported languages. */
774 return &c_varobj_ops;
775}
776
b01175fc
AB
777/* Parent class for both the "auto" and "unknown" languages. These two
778 pseudo-languages are very similar so merging their implementations like
779 this makes sense. */
c9debfb9 780
b01175fc 781class auto_or_unknown_language : public language_defn
0874fd07
AB
782{
783public:
b01175fc
AB
784 auto_or_unknown_language (enum language lang)
785 : language_defn (lang)
0874fd07 786 { /* Nothing. */ }
1fb314aa
AB
787
788 /* See language.h. */
789 void language_arch_info (struct gdbarch *gdbarch,
790 struct language_arch_info *lai) const override
791 {
7bea47f0
AB
792 lai->set_string_char_type (builtin_type (gdbarch)->builtin_char);
793 lai->set_bool_type (builtin_type (gdbarch)->builtin_int);
1fb314aa 794 }
fbfb0a46
AB
795
796 /* See language.h. */
797
798 void print_type (struct type *type, const char *varstring,
799 struct ui_file *stream, int show, int level,
800 const struct type_print_options *flags) const override
801 {
b01175fc
AB
802 error (_("type printing not implemented for language \"%s\""),
803 natural_name ());
fbfb0a46 804 }
0a50df5d
AB
805
806 /* See language.h. */
807
5399db93 808 char *demangle_symbol (const char *mangled, int options) const override
0a50df5d 809 {
b01175fc 810 /* The auto language just uses the C++ demangler. */
0a50df5d
AB
811 return gdb_demangle (mangled, options);
812 }
a1d1fa3e
AB
813
814 /* See language.h. */
815
816 void value_print (struct value *val, struct ui_file *stream,
817 const struct value_print_options *options) const override
818 {
b01175fc
AB
819 error (_("value printing not implemented for language \"%s\""),
820 natural_name ());
a1d1fa3e 821 }
ebe2334e
AB
822
823 /* See language.h. */
824
825 void value_print_inner
826 (struct value *val, struct ui_file *stream, int recurse,
827 const struct value_print_options *options) const override
828 {
b01175fc
AB
829 error (_("inner value printing not implemented for language \"%s\""),
830 natural_name ());
ebe2334e 831 }
87afa652
AB
832
833 /* See language.h. */
834
835 int parser (struct parser_state *ps) const override
836 {
837 /* No parsing is done, just claim success. */
838 return 1;
839 }
ec8cec5b
AB
840
841 /* See language.h. */
842
843 void emitchar (int ch, struct type *chtype,
844 struct ui_file *stream, int quoter) const override
845 {
b01175fc
AB
846 error (_("emit character not implemented for language \"%s\""),
847 natural_name ());
ec8cec5b 848 }
52b50f2c
AB
849
850 /* See language.h. */
851
852 void printchar (int ch, struct type *chtype,
853 struct ui_file *stream) const override
854 {
b01175fc
AB
855 error (_("print character not implemented for language \"%s\""),
856 natural_name ());
52b50f2c 857 }
d711ee67
AB
858
859 /* See language.h. */
860
861 void printstr (struct ui_file *stream, struct type *elttype,
862 const gdb_byte *string, unsigned int length,
863 const char *encoding, int force_ellipses,
864 const struct value_print_options *options) const override
865 {
b01175fc
AB
866 error (_("print string not implemented for language \"%s\""),
867 natural_name ());
d711ee67 868 }
4ffc13fb
AB
869
870 /* See language.h. */
871
872 void print_typedef (struct type *type, struct symbol *new_symbol,
873 struct ui_file *stream) const override
874 {
b01175fc
AB
875 error (_("print typedef not implemented for language \"%s\""),
876 natural_name ());
4ffc13fb 877 }
39e7ecca
AB
878
879 /* See language.h. */
880
881 bool is_string_type_p (struct type *type) const override
882 {
b01175fc
AB
883 type = check_typedef (type);
884 while (type->code () == TYPE_CODE_REF)
885 {
886 type = TYPE_TARGET_TYPE (type);
887 type = check_typedef (type);
888 }
889 return (type->code () == TYPE_CODE_STRING);
39e7ecca 890 }
5bae7c4e
AB
891
892 /* See language.h. */
893
894 const char *name_of_this () const override
895 { return "this"; }
b01175fc 896};
0874fd07 897
0874fd07
AB
898/* Class representing the fake "auto" language. */
899
b01175fc 900class auto_language : public auto_or_unknown_language
0874fd07
AB
901{
902public:
903 auto_language ()
b01175fc 904 : auto_or_unknown_language (language_auto)
0874fd07 905 { /* Nothing. */ }
1fb314aa 906
6f7664a9
AB
907 /* See language.h. */
908
909 const char *name () const override
910 { return "auto"; }
911
912 /* See language.h. */
913
914 const char *natural_name () const override
915 { return "Auto"; }
b01175fc 916};
6f7664a9 917
b01175fc 918/* Single instance of the fake "auto" language. */
d711ee67 919
b01175fc 920static auto_language auto_language_defn;
4ffc13fb 921
b01175fc 922/* Class representing the unknown language. */
4ffc13fb 923
b01175fc
AB
924class unknown_language : public auto_or_unknown_language
925{
926public:
927 unknown_language ()
928 : auto_or_unknown_language (language_unknown)
929 { /* Nothing. */ }
39e7ecca
AB
930
931 /* See language.h. */
932
b01175fc
AB
933 const char *name () const override
934 { return "unknown"; }
5bae7c4e
AB
935
936 /* See language.h. */
937
b01175fc
AB
938 const char *natural_name () const override
939 { return "Unknown"; }
b7c6e27d
AB
940
941 /* See language.h. */
942
b01175fc
AB
943 bool store_sym_names_in_linkage_form_p () const override
944 { return true; }
0874fd07
AB
945};
946
b01175fc 947/* Single instance of the unknown language class. */
0874fd07 948
b01175fc 949static unknown_language unknown_language_defn;
0874fd07 950
c906108c 951\f
f290d38e
AC
952/* Per-architecture language information. */
953
954static struct gdbarch_data *language_gdbarch_data;
955
956struct language_gdbarch
957{
958 /* A vector of per-language per-architecture info. Indexed by "enum
959 language". */
960 struct language_arch_info arch_info[nr_languages];
961};
962
963static void *
964language_gdbarch_post_init (struct gdbarch *gdbarch)
965{
7bea47f0
AB
966 struct language_gdbarch *l
967 = obstack_new<struct language_gdbarch> (gdbarch_obstack (gdbarch));
0874fd07 968 for (const auto &lang : language_defn::languages)
1fb314aa
AB
969 {
970 gdb_assert (lang != nullptr);
7bea47f0 971 lang->language_arch_info (gdbarch, &l->arch_info[lang->la_language]);
1fb314aa 972 }
47e77640 973
f290d38e
AC
974 return l;
975}
976
7bea47f0
AB
977/* See language.h. */
978
f290d38e
AC
979struct type *
980language_string_char_type (const struct language_defn *la,
981 struct gdbarch *gdbarch)
982{
9a3c8263
SM
983 struct language_gdbarch *ld
984 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
7bea47f0 985 return ld->arch_info[la->la_language].string_char_type ();
f290d38e
AC
986}
987
7bea47f0
AB
988/* See language.h. */
989
fbb06eb1
UW
990struct type *
991language_bool_type (const struct language_defn *la,
992 struct gdbarch *gdbarch)
993{
9a3c8263
SM
994 struct language_gdbarch *ld
995 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
7bea47f0
AB
996 return ld->arch_info[la->la_language].bool_type ();
997}
998
999/* See language.h. */
fbb06eb1 1000
7bea47f0
AB
1001struct type *
1002language_arch_info::bool_type () const
1003{
1004 if (m_bool_type_name != nullptr)
fbb06eb1
UW
1005 {
1006 struct symbol *sym;
e0881a8e 1007
7bea47f0
AB
1008 sym = lookup_symbol (m_bool_type_name, NULL, VAR_DOMAIN, NULL).symbol;
1009 if (sym != nullptr)
fbb06eb1
UW
1010 {
1011 struct type *type = SYMBOL_TYPE (sym);
7bea47f0 1012 if (type != nullptr && type->code () == TYPE_CODE_BOOL)
fbb06eb1
UW
1013 return type;
1014 }
1015 }
1016
7bea47f0 1017 return m_bool_type_default;
1994afbf
DE
1018}
1019
1020/* See language.h. */
1021
7bea47f0
AB
1022struct symbol *
1023language_arch_info::type_and_symbol::alloc_type_symbol
1024 (enum language lang, struct type *type)
1994afbf
DE
1025{
1026 struct symbol *symbol;
1027 struct gdbarch *gdbarch;
30625020 1028 gdb_assert (!type->is_objfile_owned ());
6ac37371 1029 gdbarch = type->arch_owner ();
468c0cbb 1030 symbol = new (gdbarch_obstack (gdbarch)) struct symbol ();
7d93a1e0 1031 symbol->m_name = type->name ();
d3ecddab 1032 symbol->set_language (lang, nullptr);
1994afbf
DE
1033 symbol->owner.arch = gdbarch;
1034 SYMBOL_OBJFILE_OWNED (symbol) = 0;
a52d653e 1035 symbol->set_section_index (0);
1994afbf
DE
1036 SYMBOL_TYPE (symbol) = type;
1037 SYMBOL_DOMAIN (symbol) = VAR_DOMAIN;
1038 SYMBOL_ACLASS_INDEX (symbol) = LOC_TYPEDEF;
1994afbf
DE
1039 return symbol;
1040}
1041
7bea47f0 1042/* See language.h. */
1994afbf 1043
7bea47f0
AB
1044language_arch_info::type_and_symbol *
1045language_arch_info::lookup_primitive_type_and_symbol (const char *name)
1994afbf 1046{
7bea47f0
AB
1047 for (struct type_and_symbol &tas : primitive_types_and_symbols)
1048 {
1049 if (strcmp (tas.type ()->name (), name) == 0)
1050 return &tas;
1051 }
1994afbf 1052
7bea47f0
AB
1053 return nullptr;
1054}
1055
1056/* See language.h. */
1994afbf 1057
7bea47f0
AB
1058struct type *
1059language_arch_info::lookup_primitive_type (const char *name)
1060{
1061 type_and_symbol *tas = lookup_primitive_type_and_symbol (name);
1062 if (tas != nullptr)
1063 return tas->type ();
1064 return nullptr;
1065}
1994afbf 1066
7bea47f0 1067/* See language.h. */
1994afbf 1068
7bea47f0
AB
1069struct type *
1070language_arch_info::lookup_primitive_type
cbbcd7a7 1071 (gdb::function_view<bool (struct type *)> filter)
7bea47f0
AB
1072{
1073 for (struct type_and_symbol &tas : primitive_types_and_symbols)
1994afbf 1074 {
7bea47f0
AB
1075 if (filter (tas.type ()))
1076 return tas.type ();
1994afbf
DE
1077 }
1078
7bea47f0
AB
1079 return nullptr;
1080}
1081
1082/* See language.h. */
1083
1084struct symbol *
1085language_arch_info::lookup_primitive_type_as_symbol (const char *name,
1086 enum language lang)
1087{
1088 type_and_symbol *tas = lookup_primitive_type_and_symbol (name);
1089 if (tas != nullptr)
1090 return tas->symbol (lang);
1091 return nullptr;
1092}
1093
cbbcd7a7
PA
1094/* Helper for the language_lookup_primitive_type overloads to forward
1095 to the corresponding language's lookup_primitive_type overload. */
7bea47f0
AB
1096
1097template<typename T>
cbbcd7a7
PA
1098static struct type *
1099language_lookup_primitive_type_1 (const struct language_defn *la,
1100 struct gdbarch *gdbarch,
1101 T arg)
7bea47f0
AB
1102{
1103 struct language_gdbarch *ld =
1104 (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1105 return ld->arch_info[la->la_language].lookup_primitive_type (arg);
1994afbf
DE
1106}
1107
cbbcd7a7 1108/* See language.h. */
7bea47f0 1109
cbbcd7a7 1110struct type *
7bea47f0
AB
1111language_lookup_primitive_type (const struct language_defn *la,
1112 struct gdbarch *gdbarch,
cbbcd7a7
PA
1113 const char *name)
1114{
1115 return language_lookup_primitive_type_1 (la, gdbarch, name);
1116}
7bea47f0 1117
cbbcd7a7 1118/* See language.h. */
7bea47f0 1119
cbbcd7a7 1120struct type *
7bea47f0
AB
1121language_lookup_primitive_type (const struct language_defn *la,
1122 struct gdbarch *gdbarch,
cbbcd7a7
PA
1123 gdb::function_view<bool (struct type *)> filter)
1124{
1125 return language_lookup_primitive_type_1 (la, gdbarch, filter);
1126}
7bea47f0 1127
1994afbf
DE
1128/* See language.h. */
1129
1130struct symbol *
1131language_lookup_primitive_type_as_symbol (const struct language_defn *la,
1132 struct gdbarch *gdbarch,
1133 const char *name)
1134{
9a3c8263
SM
1135 struct language_gdbarch *ld
1136 = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1994afbf 1137 struct language_arch_info *lai = &ld->arch_info[la->la_language];
e0881a8e 1138
cc485e62 1139 if (symbol_lookup_debug)
7bea47f0
AB
1140 fprintf_unfiltered (gdb_stdlog,
1141 "language_lookup_primitive_type_as_symbol"
1142 " (%s, %s, %s)",
1143 la->name (), host_address_to_string (gdbarch), name);
1994afbf 1144
7bea47f0
AB
1145 struct symbol *sym
1146 = lai->lookup_primitive_type_as_symbol (name, la->la_language);
1994afbf 1147
cc485e62 1148 if (symbol_lookup_debug)
1994afbf 1149 fprintf_unfiltered (gdb_stdlog, " = %s\n", host_address_to_string (sym));
7bea47f0
AB
1150
1151 /* Note: The result of symbol lookup is normally a symbol *and* the block
1152 it was found in. Builtin types don't live in blocks. We *could* give
1153 them one, but there is no current need so to keep things simple symbol
1154 lookup is extended to allow for BLOCK_FOUND to be NULL. */
1155
1994afbf 1156 return sym;
f290d38e
AC
1157}
1158
1777feb0 1159/* Initialize the language routines. */
c906108c 1160
6c265988 1161void _initialize_language ();
c906108c 1162void
6c265988 1163_initialize_language ()
c906108c 1164{
40478521 1165 static const char *const type_or_range_names[]
b84aa90a
PA
1166 = { "on", "off", "warn", "auto", NULL };
1167
40478521 1168 static const char *const case_sensitive_names[]
b84aa90a 1169 = { "on", "off", "auto", NULL };
c5aa993b 1170
f290d38e
AC
1171 language_gdbarch_data
1172 = gdbarch_data_register_post_init (language_gdbarch_post_init);
1173
1777feb0 1174 /* GDB commands for language specific stuff. */
c5aa993b 1175
0743fc83
TT
1176 add_basic_prefix_cmd ("check", no_class,
1177 _("Set the status of the type/range checker."),
1178 &setchecklist, "set check ", 0, &setlist);
c5aa993b
JM
1179 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1180 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1181
0743fc83
TT
1182 add_show_prefix_cmd ("check", no_class,
1183 _("Show the status of the type/range checker."),
1184 &showchecklist, "show check ", 0, &showlist);
c5aa993b
JM
1185 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1186 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1187
b84aa90a 1188 add_setshow_enum_cmd ("range", class_support, type_or_range_names,
3e43a32a 1189 &range,
590042fc
PW
1190 _("Set range checking (on/warn/off/auto)."),
1191 _("Show range checking (on/warn/off/auto)."),
3e43a32a 1192 NULL, set_range_command,
b84aa90a
PA
1193 show_range_command,
1194 &setchecklist, &showchecklist);
1195
1196 add_setshow_enum_cmd ("case-sensitive", class_support, case_sensitive_names,
1197 &case_sensitive, _("\
590042fc
PW
1198Set case sensitivity in name search (on/off/auto)."), _("\
1199Show case sensitivity in name search (on/off/auto)."), _("\
4d28ad1e 1200For Fortran the default is off; for other languages the default is on."),
b84aa90a
PA
1201 set_case_command,
1202 show_case_command,
1203 &setlist, &showlist);
63872f9d 1204
0874fd07
AB
1205 /* In order to call SET_LANGUAGE (below) we need to make sure that
1206 CURRENT_LANGUAGE is not NULL. So first set the language to unknown,
1207 then we can change the language to 'auto'. */
1208 current_language = language_def (language_unknown);
1209
47e77640 1210 add_set_language_command ();
c5aa993b 1211
4a811000
PW
1212 language = "auto";
1213 range = "auto";
1214 case_sensitive = "auto";
63872f9d 1215
1777feb0 1216 /* Have the above take effect. */
63872f9d 1217 set_language (language_auto);
c906108c 1218}
This page took 2.433896 seconds and 4 git commands to generate.