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