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