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