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