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