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