language.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /* Multiple source language support for GDB.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. Contributed by the Department of Computer Science at the State University
  4. of New York at Buffalo.
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. /* This file contains functions that return things that are specific
  17. to languages. Each function should examine current_language if necessary,
  18. and return the appropriate result. */
  19. /* FIXME: Most of these would be better organized as macros which
  20. return data out of a "language-specific" struct pointer that is set
  21. whenever the working language changes. That would be a lot faster. */
  22. #include "defs.h"
  23. #include <ctype.h>
  24. #include "symtab.h"
  25. #include "gdbtypes.h"
  26. #include "value.h"
  27. #include "gdbcmd.h"
  28. #include "expression.h"
  29. #include "language.h"
  30. #include "varobj.h"
  31. #include "target.h"
  32. #include "parser-defs.h"
  33. #include "demangle.h"
  34. #include "symfile.h"
  35. #include "cp-support.h"
  36. #include "frame.h"
  37. #include "c-lang.h"
  38. #include <algorithm>
  39. #include "gdbarch.h"
  40. #include "compile/compile-internal.h"
  41. static void set_range_case (void);
  42. /* range_mode ==
  43. range_mode_auto: range_check set automatically to default of language.
  44. range_mode_manual: range_check set manually by user. */
  45. enum range_mode
  46. {
  47. range_mode_auto, range_mode_manual
  48. };
  49. /* case_mode ==
  50. case_mode_auto: case_sensitivity set upon selection of scope.
  51. case_mode_manual: case_sensitivity set only by user. */
  52. enum case_mode
  53. {
  54. case_mode_auto, case_mode_manual
  55. };
  56. /* The current (default at startup) state of type and range checking.
  57. (If the modes are set to "auto", though, these are changed based
  58. on the default language at startup, and then again based on the
  59. language of the first source file. */
  60. static enum range_mode range_mode = range_mode_auto;
  61. enum range_check range_check = range_check_off;
  62. static enum case_mode case_mode = case_mode_auto;
  63. enum case_sensitivity case_sensitivity = case_sensitive_on;
  64. /* The current language and language_mode (see language.h). */
  65. const struct language_defn *current_language = nullptr;
  66. enum language_mode language_mode = language_mode_auto;
  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. const struct language_defn *expected_language;
  70. /* Define the array containing all languages. */
  71. const struct language_defn *language_defn::languages[nr_languages];
  72. /* The current values of the "set language/range/case-sensitive" enum
  73. commands. */
  74. static const char *language;
  75. static const char *range;
  76. static const char *case_sensitive;
  77. /* See language.h. */
  78. const char lang_frame_mismatch_warn[] =
  79. N_("Warning: the current language does not match this frame.");
  80. /* This page contains the functions corresponding to GDB commands
  81. and their helpers. */
  82. /* Show command. Display a warning if the language set
  83. does not match the frame. */
  84. static void
  85. show_language_command (struct ui_file *file, int from_tty,
  86. struct cmd_list_element *c, const char *value)
  87. {
  88. enum language flang; /* The language of the frame. */
  89. if (language_mode == language_mode_auto)
  90. gdb_printf (file,
  91. _("The current source language is "
  92. "\"auto; currently %s\".\n"),
  93. current_language->name ());
  94. else
  95. gdb_printf (file,
  96. _("The current source language is \"%s\".\n"),
  97. current_language->name ());
  98. if (has_stack_frames ())
  99. {
  100. struct frame_info *frame;
  101. frame = get_selected_frame (NULL);
  102. flang = get_frame_language (frame);
  103. if (flang != language_unknown
  104. && language_mode == language_mode_manual
  105. && current_language->la_language != flang)
  106. gdb_printf (file, "%s\n", _(lang_frame_mismatch_warn));
  107. }
  108. }
  109. /* Set command. Change the current working language. */
  110. static void
  111. set_language_command (const char *ignore,
  112. int from_tty, struct cmd_list_element *c)
  113. {
  114. enum language flang = language_unknown;
  115. /* "local" is a synonym of "auto". */
  116. if (strcmp (language, "local") == 0)
  117. language = "auto";
  118. /* Search the list of languages for a match. */
  119. for (const auto &lang : language_defn::languages)
  120. {
  121. if (strcmp (lang->name (), language) == 0)
  122. {
  123. /* Found it! Go into manual mode, and use this language. */
  124. if (lang->la_language == language_auto)
  125. {
  126. /* Enter auto mode. Set to the current frame's language, if
  127. known, or fallback to the initial language. */
  128. language_mode = language_mode_auto;
  129. try
  130. {
  131. struct frame_info *frame;
  132. frame = get_selected_frame (NULL);
  133. flang = get_frame_language (frame);
  134. }
  135. catch (const gdb_exception_error &ex)
  136. {
  137. flang = language_unknown;
  138. }
  139. if (flang != language_unknown)
  140. set_language (flang);
  141. else
  142. set_initial_language ();
  143. expected_language = current_language;
  144. return;
  145. }
  146. else
  147. {
  148. /* Enter manual mode. Set the specified language. */
  149. language_mode = language_mode_manual;
  150. current_language = lang;
  151. set_range_case ();
  152. expected_language = current_language;
  153. return;
  154. }
  155. }
  156. }
  157. internal_error (__FILE__, __LINE__,
  158. "Couldn't find language `%s' in known languages list.",
  159. language);
  160. }
  161. /* Show command. Display a warning if the range setting does
  162. not match the current language. */
  163. static void
  164. show_range_command (struct ui_file *file, int from_tty,
  165. struct cmd_list_element *c, const char *value)
  166. {
  167. if (range_mode == range_mode_auto)
  168. {
  169. const char *tmp;
  170. switch (range_check)
  171. {
  172. case range_check_on:
  173. tmp = "on";
  174. break;
  175. case range_check_off:
  176. tmp = "off";
  177. break;
  178. case range_check_warn:
  179. tmp = "warn";
  180. break;
  181. default:
  182. internal_error (__FILE__, __LINE__,
  183. "Unrecognized range check setting.");
  184. }
  185. gdb_printf (file,
  186. _("Range checking is \"auto; currently %s\".\n"),
  187. tmp);
  188. }
  189. else
  190. gdb_printf (file, _("Range checking is \"%s\".\n"),
  191. value);
  192. if (range_check == range_check_warn
  193. || ((range_check == range_check_on)
  194. != current_language->range_checking_on_by_default ()))
  195. warning (_("the current range check setting "
  196. "does not match the language.\n"));
  197. }
  198. /* Set command. Change the setting for range checking. */
  199. static void
  200. set_range_command (const char *ignore,
  201. int from_tty, struct cmd_list_element *c)
  202. {
  203. if (strcmp (range, "on") == 0)
  204. {
  205. range_check = range_check_on;
  206. range_mode = range_mode_manual;
  207. }
  208. else if (strcmp (range, "warn") == 0)
  209. {
  210. range_check = range_check_warn;
  211. range_mode = range_mode_manual;
  212. }
  213. else if (strcmp (range, "off") == 0)
  214. {
  215. range_check = range_check_off;
  216. range_mode = range_mode_manual;
  217. }
  218. else if (strcmp (range, "auto") == 0)
  219. {
  220. range_mode = range_mode_auto;
  221. set_range_case ();
  222. return;
  223. }
  224. else
  225. {
  226. internal_error (__FILE__, __LINE__,
  227. _("Unrecognized range check setting: \"%s\""), range);
  228. }
  229. if (range_check == range_check_warn
  230. || ((range_check == range_check_on)
  231. != current_language->range_checking_on_by_default ()))
  232. warning (_("the current range check setting "
  233. "does not match the language.\n"));
  234. }
  235. /* Show command. Display a warning if the case sensitivity setting does
  236. not match the current language. */
  237. static void
  238. show_case_command (struct ui_file *file, int from_tty,
  239. struct cmd_list_element *c, const char *value)
  240. {
  241. if (case_mode == case_mode_auto)
  242. {
  243. const char *tmp = NULL;
  244. switch (case_sensitivity)
  245. {
  246. case case_sensitive_on:
  247. tmp = "on";
  248. break;
  249. case case_sensitive_off:
  250. tmp = "off";
  251. break;
  252. default:
  253. internal_error (__FILE__, __LINE__,
  254. "Unrecognized case-sensitive setting.");
  255. }
  256. gdb_printf (file,
  257. _("Case sensitivity in "
  258. "name search is \"auto; currently %s\".\n"),
  259. tmp);
  260. }
  261. else
  262. gdb_printf (file,
  263. _("Case sensitivity in name search is \"%s\".\n"),
  264. value);
  265. if (case_sensitivity != current_language->case_sensitivity ())
  266. warning (_("the current case sensitivity setting does not match "
  267. "the language.\n"));
  268. }
  269. /* Set command. Change the setting for case sensitivity. */
  270. static void
  271. set_case_command (const char *ignore, int from_tty, struct cmd_list_element *c)
  272. {
  273. if (strcmp (case_sensitive, "on") == 0)
  274. {
  275. case_sensitivity = case_sensitive_on;
  276. case_mode = case_mode_manual;
  277. }
  278. else if (strcmp (case_sensitive, "off") == 0)
  279. {
  280. case_sensitivity = case_sensitive_off;
  281. case_mode = case_mode_manual;
  282. }
  283. else if (strcmp (case_sensitive, "auto") == 0)
  284. {
  285. case_mode = case_mode_auto;
  286. set_range_case ();
  287. return;
  288. }
  289. else
  290. {
  291. internal_error (__FILE__, __LINE__,
  292. "Unrecognized case-sensitive setting: \"%s\"",
  293. case_sensitive);
  294. }
  295. if (case_sensitivity != current_language->case_sensitivity ())
  296. warning (_("the current case sensitivity setting does not match "
  297. "the language.\n"));
  298. }
  299. /* Set the status of range and type checking and case sensitivity based on
  300. the current modes and the current language.
  301. If SHOW is non-zero, then print out the current language,
  302. type and range checking status. */
  303. static void
  304. set_range_case (void)
  305. {
  306. if (range_mode == range_mode_auto)
  307. range_check = (current_language->range_checking_on_by_default ()
  308. ? range_check_on : range_check_off);
  309. if (case_mode == case_mode_auto)
  310. case_sensitivity = current_language->case_sensitivity ();
  311. }
  312. /* Set current language to (enum language) LANG. Returns previous
  313. language. */
  314. enum language
  315. set_language (enum language lang)
  316. {
  317. enum language prev_language;
  318. prev_language = current_language->la_language;
  319. current_language = language_def (lang);
  320. set_range_case ();
  321. return prev_language;
  322. }
  323. /* See language.h. */
  324. void
  325. language_info ()
  326. {
  327. if (expected_language == current_language)
  328. return;
  329. expected_language = current_language;
  330. gdb_printf (_("Current language: %s\n"), language);
  331. show_language_command (gdb_stdout, 1, NULL, NULL);
  332. }
  333. /* This page contains functions for the printing out of
  334. error messages that occur during type- and range-
  335. checking. */
  336. /* This is called when a language fails a range-check. The
  337. first argument should be a printf()-style format string, and the
  338. rest of the arguments should be its arguments. If range_check is
  339. range_check_on, an error is printed; if range_check_warn, a warning;
  340. otherwise just the message. */
  341. void
  342. range_error (const char *string,...)
  343. {
  344. va_list args;
  345. va_start (args, string);
  346. switch (range_check)
  347. {
  348. case range_check_warn:
  349. vwarning (string, args);
  350. break;
  351. case range_check_on:
  352. verror (string, args);
  353. break;
  354. case range_check_off:
  355. /* FIXME: cagney/2002-01-30: Should this function print anything
  356. when range error is off? */
  357. gdb_vprintf (gdb_stderr, string, args);
  358. gdb_printf (gdb_stderr, "\n");
  359. break;
  360. default:
  361. internal_error (__FILE__, __LINE__, _("bad switch"));
  362. }
  363. va_end (args);
  364. }
  365. /* This page contains miscellaneous functions. */
  366. /* Return the language enum for a given language string. */
  367. enum language
  368. language_enum (const char *str)
  369. {
  370. for (const auto &lang : language_defn::languages)
  371. if (strcmp (lang->name (), str) == 0)
  372. return lang->la_language;
  373. if (strcmp (str, "local") == 0)
  374. return language_auto;
  375. return language_unknown;
  376. }
  377. /* Return the language struct for a given language enum. */
  378. const struct language_defn *
  379. language_def (enum language lang)
  380. {
  381. const struct language_defn *l = language_defn::languages[lang];
  382. gdb_assert (l != nullptr);
  383. return l;
  384. }
  385. /* Return the language as a string. */
  386. const char *
  387. language_str (enum language lang)
  388. {
  389. return language_def (lang)->name ();
  390. }
  391. /* Build and install the "set language LANG" command. */
  392. static void
  393. add_set_language_command ()
  394. {
  395. static const char **language_names;
  396. /* Build the language names array, to be used as enumeration in the
  397. "set language" enum command. +1 for "local" and +1 for NULL
  398. termination. */
  399. language_names = new const char *[ARRAY_SIZE (language_defn::languages) + 2];
  400. /* Display "auto", "local" and "unknown" first, and then the rest,
  401. alpha sorted. */
  402. const char **language_names_p = language_names;
  403. language = language_def (language_auto)->name ();
  404. *language_names_p++ = language;
  405. *language_names_p++ = "local";
  406. *language_names_p++ = language_def (language_unknown)->name ();
  407. const char **sort_begin = language_names_p;
  408. for (const auto &lang : language_defn::languages)
  409. {
  410. /* Already handled above. */
  411. if (lang->la_language == language_auto
  412. || lang->la_language == language_unknown)
  413. continue;
  414. *language_names_p++ = lang->name ();
  415. }
  416. *language_names_p = NULL;
  417. std::sort (sort_begin, language_names_p, compare_cstrings);
  418. /* Add the filename extensions. */
  419. for (const auto &lang : language_defn::languages)
  420. for (const char * const &ext : lang->filename_extensions ())
  421. add_filename_language (ext, lang->la_language);
  422. /* Build the "help set language" docs. */
  423. string_file doc;
  424. doc.printf (_("Set the current source language.\n"
  425. "The currently understood settings are:\n\nlocal or "
  426. "auto Automatic setting based on source file"));
  427. for (const auto &lang : language_defn::languages)
  428. {
  429. /* Already dealt with these above. */
  430. if (lang->la_language == language_unknown
  431. || lang->la_language == language_auto)
  432. continue;
  433. /* Note that we add the newline at the front, so we don't wind
  434. up with a trailing newline. */
  435. doc.printf ("\n%-16s Use the %s language",
  436. lang->name (),
  437. lang->natural_name ());
  438. }
  439. add_setshow_enum_cmd ("language", class_support,
  440. language_names,
  441. &language,
  442. doc.c_str (),
  443. _("Show the current source language."),
  444. NULL, set_language_command,
  445. show_language_command,
  446. &setlist, &showlist);
  447. }
  448. /* Iterate through all registered languages looking for and calling
  449. any non-NULL struct language_defn.skip_trampoline() functions.
  450. Return the result from the first that returns non-zero, or 0 if all
  451. `fail'. */
  452. CORE_ADDR
  453. skip_language_trampoline (struct frame_info *frame, CORE_ADDR pc)
  454. {
  455. for (const auto &lang : language_defn::languages)
  456. {
  457. CORE_ADDR real_pc = lang->skip_trampoline (frame, pc);
  458. if (real_pc != 0)
  459. return real_pc;
  460. }
  461. return 0;
  462. }
  463. /* Return demangled language symbol, or NULL.
  464. FIXME: Options are only useful for certain languages and ignored
  465. by others, so it would be better to remove them here and have a
  466. more flexible demangler for the languages that need it.
  467. FIXME: Sometimes the demangler is invoked when we don't know the
  468. language, so we can't use this everywhere. */
  469. gdb::unique_xmalloc_ptr<char>
  470. language_demangle (const struct language_defn *current_language,
  471. const char *mangled, int options)
  472. {
  473. if (current_language != NULL)
  474. return current_language->demangle_symbol (mangled, options);
  475. return NULL;
  476. }
  477. /* Return information about whether TYPE should be passed
  478. (and returned) by reference at the language level. */
  479. struct language_pass_by_ref_info
  480. language_pass_by_reference (struct type *type)
  481. {
  482. return current_language->pass_by_reference_info (type);
  483. }
  484. /* Return the default string containing the list of characters
  485. delimiting words. This is a reasonable default value that
  486. most languages should be able to use. */
  487. const char *
  488. default_word_break_characters (void)
  489. {
  490. return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
  491. }
  492. /* See language.h. */
  493. void
  494. language_defn::print_array_index (struct type *index_type, LONGEST index,
  495. struct ui_file *stream,
  496. const value_print_options *options) const
  497. {
  498. struct value *index_value = value_from_longest (index_type, index);
  499. gdb_printf (stream, "[");
  500. value_print (index_value, stream, options);
  501. gdb_printf (stream, "] = ");
  502. }
  503. /* See language.h. */
  504. gdb::unique_xmalloc_ptr<char>
  505. language_defn::watch_location_expression (struct type *type,
  506. CORE_ADDR addr) const
  507. {
  508. /* Generates an expression that assumes a C like syntax is valid. */
  509. type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
  510. std::string name = type_to_string (type);
  511. return xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr));
  512. }
  513. /* See language.h. */
  514. void
  515. language_defn::value_print (struct value *val, struct ui_file *stream,
  516. const struct value_print_options *options) const
  517. {
  518. return c_value_print (val, stream, options);
  519. }
  520. /* See language.h. */
  521. int
  522. language_defn::parser (struct parser_state *ps) const
  523. {
  524. return c_parse (ps);
  525. }
  526. /* See language.h. */
  527. void
  528. language_defn::value_print_inner
  529. (struct value *val, struct ui_file *stream, int recurse,
  530. const struct value_print_options *options) const
  531. {
  532. return c_value_print_inner (val, stream, recurse, options);
  533. }
  534. /* See language.h. */
  535. void
  536. language_defn::emitchar (int ch, struct type *chtype,
  537. struct ui_file * stream, int quoter) const
  538. {
  539. c_emit_char (ch, chtype, stream, quoter);
  540. }
  541. /* See language.h. */
  542. void
  543. language_defn::printstr (struct ui_file *stream, struct type *elttype,
  544. const gdb_byte *string, unsigned int length,
  545. const char *encoding, int force_ellipses,
  546. const struct value_print_options *options) const
  547. {
  548. c_printstr (stream, elttype, string, length, encoding, force_ellipses,
  549. options);
  550. }
  551. /* See language.h. */
  552. void
  553. language_defn::print_typedef (struct type *type, struct symbol *new_symbol,
  554. struct ui_file *stream) const
  555. {
  556. c_print_typedef (type, new_symbol, stream);
  557. }
  558. /* See language.h. */
  559. bool
  560. language_defn::is_string_type_p (struct type *type) const
  561. {
  562. return c_is_string_type_p (type);
  563. }
  564. /* See language.h. */
  565. std::unique_ptr<compile_instance>
  566. language_defn::get_compile_instance () const
  567. {
  568. return {};
  569. }
  570. /* The default implementation of the get_symbol_name_matcher_inner method
  571. from the language_defn class. Matches with strncmp_iw. */
  572. static bool
  573. default_symbol_name_matcher (const char *symbol_search_name,
  574. const lookup_name_info &lookup_name,
  575. completion_match_result *comp_match_res)
  576. {
  577. gdb::string_view name = lookup_name.name ();
  578. completion_match_for_lcd *match_for_lcd
  579. = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
  580. strncmp_iw_mode mode = (lookup_name.completion_mode ()
  581. ? strncmp_iw_mode::NORMAL
  582. : strncmp_iw_mode::MATCH_PARAMS);
  583. if (strncmp_iw_with_mode (symbol_search_name, name.data (), name.size (),
  584. mode, language_minimal, match_for_lcd) == 0)
  585. {
  586. if (comp_match_res != NULL)
  587. comp_match_res->set_match (symbol_search_name);
  588. return true;
  589. }
  590. else
  591. return false;
  592. }
  593. /* See language.h. */
  594. symbol_name_matcher_ftype *
  595. language_defn::get_symbol_name_matcher
  596. (const lookup_name_info &lookup_name) const
  597. {
  598. /* If currently in Ada mode, and the lookup name is wrapped in
  599. '<...>', hijack all symbol name comparisons using the Ada
  600. matcher, which handles the verbatim matching. */
  601. if (current_language->la_language == language_ada
  602. && lookup_name.ada ().verbatim_p ())
  603. return current_language->get_symbol_name_matcher_inner (lookup_name);
  604. return this->get_symbol_name_matcher_inner (lookup_name);
  605. }
  606. /* See language.h. */
  607. symbol_name_matcher_ftype *
  608. language_defn::get_symbol_name_matcher_inner
  609. (const lookup_name_info &lookup_name) const
  610. {
  611. return default_symbol_name_matcher;
  612. }
  613. /* See language.h. */
  614. const struct lang_varobj_ops *
  615. language_defn::varobj_ops () const
  616. {
  617. /* The ops for the C language are suitable for the vast majority of the
  618. supported languages. */
  619. return &c_varobj_ops;
  620. }
  621. /* Parent class for both the "auto" and "unknown" languages. These two
  622. pseudo-languages are very similar so merging their implementations like
  623. this makes sense. */
  624. class auto_or_unknown_language : public language_defn
  625. {
  626. public:
  627. auto_or_unknown_language (enum language lang)
  628. : language_defn (lang)
  629. { /* Nothing. */ }
  630. /* See language.h. */
  631. void language_arch_info (struct gdbarch *gdbarch,
  632. struct language_arch_info *lai) const override
  633. {
  634. lai->set_string_char_type (builtin_type (gdbarch)->builtin_char);
  635. lai->set_bool_type (builtin_type (gdbarch)->builtin_int);
  636. }
  637. /* See language.h. */
  638. void print_type (struct type *type, const char *varstring,
  639. struct ui_file *stream, int show, int level,
  640. const struct type_print_options *flags) const override
  641. {
  642. error (_("type printing not implemented for language \"%s\""),
  643. natural_name ());
  644. }
  645. /* See language.h. */
  646. gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
  647. int options) const override
  648. {
  649. /* The auto language just uses the C++ demangler. */
  650. return gdb_demangle (mangled, options);
  651. }
  652. /* See language.h. */
  653. void value_print (struct value *val, struct ui_file *stream,
  654. const struct value_print_options *options) const override
  655. {
  656. error (_("value printing not implemented for language \"%s\""),
  657. natural_name ());
  658. }
  659. /* See language.h. */
  660. void value_print_inner
  661. (struct value *val, struct ui_file *stream, int recurse,
  662. const struct value_print_options *options) const override
  663. {
  664. error (_("inner value printing not implemented for language \"%s\""),
  665. natural_name ());
  666. }
  667. /* See language.h. */
  668. int parser (struct parser_state *ps) const override
  669. {
  670. error (_("expression parsing not implemented for language \"%s\""),
  671. natural_name ());
  672. }
  673. /* See language.h. */
  674. void emitchar (int ch, struct type *chtype,
  675. struct ui_file *stream, int quoter) const override
  676. {
  677. error (_("emit character not implemented for language \"%s\""),
  678. natural_name ());
  679. }
  680. /* See language.h. */
  681. void printchar (int ch, struct type *chtype,
  682. struct ui_file *stream) const override
  683. {
  684. error (_("print character not implemented for language \"%s\""),
  685. natural_name ());
  686. }
  687. /* See language.h. */
  688. void printstr (struct ui_file *stream, struct type *elttype,
  689. const gdb_byte *string, unsigned int length,
  690. const char *encoding, int force_ellipses,
  691. const struct value_print_options *options) const override
  692. {
  693. error (_("print string not implemented for language \"%s\""),
  694. natural_name ());
  695. }
  696. /* See language.h. */
  697. void print_typedef (struct type *type, struct symbol *new_symbol,
  698. struct ui_file *stream) const override
  699. {
  700. error (_("print typedef not implemented for language \"%s\""),
  701. natural_name ());
  702. }
  703. /* See language.h. */
  704. bool is_string_type_p (struct type *type) const override
  705. {
  706. type = check_typedef (type);
  707. while (type->code () == TYPE_CODE_REF)
  708. {
  709. type = TYPE_TARGET_TYPE (type);
  710. type = check_typedef (type);
  711. }
  712. return (type->code () == TYPE_CODE_STRING);
  713. }
  714. /* See language.h. */
  715. const char *name_of_this () const override
  716. { return "this"; }
  717. };
  718. /* Class representing the fake "auto" language. */
  719. class auto_language : public auto_or_unknown_language
  720. {
  721. public:
  722. auto_language ()
  723. : auto_or_unknown_language (language_auto)
  724. { /* Nothing. */ }
  725. /* See language.h. */
  726. const char *name () const override
  727. { return "auto"; }
  728. /* See language.h. */
  729. const char *natural_name () const override
  730. { return "Auto"; }
  731. };
  732. /* Single instance of the fake "auto" language. */
  733. static auto_language auto_language_defn;
  734. /* Class representing the unknown language. */
  735. class unknown_language : public auto_or_unknown_language
  736. {
  737. public:
  738. unknown_language ()
  739. : auto_or_unknown_language (language_unknown)
  740. { /* Nothing. */ }
  741. /* See language.h. */
  742. const char *name () const override
  743. { return "unknown"; }
  744. /* See language.h. */
  745. const char *natural_name () const override
  746. { return "Unknown"; }
  747. /* See language.h. */
  748. bool store_sym_names_in_linkage_form_p () const override
  749. { return true; }
  750. };
  751. /* Single instance of the unknown language class. */
  752. static unknown_language unknown_language_defn;
  753. /* Per-architecture language information. */
  754. static struct gdbarch_data *language_gdbarch_data;
  755. struct language_gdbarch
  756. {
  757. /* A vector of per-language per-architecture info. Indexed by "enum
  758. language". */
  759. struct language_arch_info arch_info[nr_languages];
  760. };
  761. static void *
  762. language_gdbarch_post_init (struct gdbarch *gdbarch)
  763. {
  764. struct language_gdbarch *l
  765. = obstack_new<struct language_gdbarch> (gdbarch_obstack (gdbarch));
  766. for (const auto &lang : language_defn::languages)
  767. {
  768. gdb_assert (lang != nullptr);
  769. lang->language_arch_info (gdbarch, &l->arch_info[lang->la_language]);
  770. }
  771. return l;
  772. }
  773. /* See language.h. */
  774. struct type *
  775. language_string_char_type (const struct language_defn *la,
  776. struct gdbarch *gdbarch)
  777. {
  778. struct language_gdbarch *ld
  779. = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
  780. return ld->arch_info[la->la_language].string_char_type ();
  781. }
  782. /* See language.h. */
  783. struct type *
  784. language_bool_type (const struct language_defn *la,
  785. struct gdbarch *gdbarch)
  786. {
  787. struct language_gdbarch *ld
  788. = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
  789. return ld->arch_info[la->la_language].bool_type ();
  790. }
  791. /* See language.h. */
  792. struct type *
  793. language_arch_info::bool_type () const
  794. {
  795. if (m_bool_type_name != nullptr)
  796. {
  797. struct symbol *sym;
  798. sym = lookup_symbol (m_bool_type_name, NULL, VAR_DOMAIN, NULL).symbol;
  799. if (sym != nullptr)
  800. {
  801. struct type *type = sym->type ();
  802. if (type != nullptr && type->code () == TYPE_CODE_BOOL)
  803. return type;
  804. }
  805. }
  806. return m_bool_type_default;
  807. }
  808. /* See language.h. */
  809. struct symbol *
  810. language_arch_info::type_and_symbol::alloc_type_symbol
  811. (enum language lang, struct type *type)
  812. {
  813. struct symbol *symbol;
  814. struct gdbarch *gdbarch;
  815. gdb_assert (!type->is_objfile_owned ());
  816. gdbarch = type->arch_owner ();
  817. symbol = new (gdbarch_obstack (gdbarch)) struct symbol ();
  818. symbol->m_name = type->name ();
  819. symbol->set_language (lang, nullptr);
  820. symbol->owner.arch = gdbarch;
  821. symbol->set_is_objfile_owned (0);
  822. symbol->set_section_index (0);
  823. symbol->set_type (type);
  824. symbol->set_domain (VAR_DOMAIN);
  825. symbol->set_aclass_index (LOC_TYPEDEF);
  826. return symbol;
  827. }
  828. /* See language.h. */
  829. language_arch_info::type_and_symbol *
  830. language_arch_info::lookup_primitive_type_and_symbol (const char *name)
  831. {
  832. for (struct type_and_symbol &tas : primitive_types_and_symbols)
  833. {
  834. if (strcmp (tas.type ()->name (), name) == 0)
  835. return &tas;
  836. }
  837. return nullptr;
  838. }
  839. /* See language.h. */
  840. struct type *
  841. language_arch_info::lookup_primitive_type (const char *name)
  842. {
  843. type_and_symbol *tas = lookup_primitive_type_and_symbol (name);
  844. if (tas != nullptr)
  845. return tas->type ();
  846. return nullptr;
  847. }
  848. /* See language.h. */
  849. struct type *
  850. language_arch_info::lookup_primitive_type
  851. (gdb::function_view<bool (struct type *)> filter)
  852. {
  853. for (struct type_and_symbol &tas : primitive_types_and_symbols)
  854. {
  855. if (filter (tas.type ()))
  856. return tas.type ();
  857. }
  858. return nullptr;
  859. }
  860. /* See language.h. */
  861. struct symbol *
  862. language_arch_info::lookup_primitive_type_as_symbol (const char *name,
  863. enum language lang)
  864. {
  865. type_and_symbol *tas = lookup_primitive_type_and_symbol (name);
  866. if (tas != nullptr)
  867. return tas->symbol (lang);
  868. return nullptr;
  869. }
  870. /* Helper for the language_lookup_primitive_type overloads to forward
  871. to the corresponding language's lookup_primitive_type overload. */
  872. template<typename T>
  873. static struct type *
  874. language_lookup_primitive_type_1 (const struct language_defn *la,
  875. struct gdbarch *gdbarch,
  876. T arg)
  877. {
  878. struct language_gdbarch *ld =
  879. (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
  880. return ld->arch_info[la->la_language].lookup_primitive_type (arg);
  881. }
  882. /* See language.h. */
  883. struct type *
  884. language_lookup_primitive_type (const struct language_defn *la,
  885. struct gdbarch *gdbarch,
  886. const char *name)
  887. {
  888. return language_lookup_primitive_type_1 (la, gdbarch, name);
  889. }
  890. /* See language.h. */
  891. struct type *
  892. language_lookup_primitive_type (const struct language_defn *la,
  893. struct gdbarch *gdbarch,
  894. gdb::function_view<bool (struct type *)> filter)
  895. {
  896. return language_lookup_primitive_type_1 (la, gdbarch, filter);
  897. }
  898. /* See language.h. */
  899. struct symbol *
  900. language_lookup_primitive_type_as_symbol (const struct language_defn *la,
  901. struct gdbarch *gdbarch,
  902. const char *name)
  903. {
  904. struct language_gdbarch *ld
  905. = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
  906. struct language_arch_info *lai = &ld->arch_info[la->la_language];
  907. if (symbol_lookup_debug)
  908. gdb_printf (gdb_stdlog,
  909. "language_lookup_primitive_type_as_symbol"
  910. " (%s, %s, %s)",
  911. la->name (), host_address_to_string (gdbarch), name);
  912. struct symbol *sym
  913. = lai->lookup_primitive_type_as_symbol (name, la->la_language);
  914. if (symbol_lookup_debug)
  915. gdb_printf (gdb_stdlog, " = %s\n", host_address_to_string (sym));
  916. /* Note: The result of symbol lookup is normally a symbol *and* the block
  917. it was found in. Builtin types don't live in blocks. We *could* give
  918. them one, but there is no current need so to keep things simple symbol
  919. lookup is extended to allow for BLOCK_FOUND to be NULL. */
  920. return sym;
  921. }
  922. /* Initialize the language routines. */
  923. void _initialize_language ();
  924. void
  925. _initialize_language ()
  926. {
  927. static const char *const type_or_range_names[]
  928. = { "on", "off", "warn", "auto", NULL };
  929. static const char *const case_sensitive_names[]
  930. = { "on", "off", "auto", NULL };
  931. language_gdbarch_data
  932. = gdbarch_data_register_post_init (language_gdbarch_post_init);
  933. /* GDB commands for language specific stuff. */
  934. set_show_commands setshow_check_cmds
  935. = add_setshow_prefix_cmd ("check", no_class,
  936. _("Set the status of the type/range checker."),
  937. _("Show the status of the type/range checker."),
  938. &setchecklist, &showchecklist,
  939. &setlist, &showlist);
  940. add_alias_cmd ("c", setshow_check_cmds.set, no_class, 1, &setlist);
  941. add_alias_cmd ("ch", setshow_check_cmds.set, no_class, 1, &setlist);
  942. add_alias_cmd ("c", setshow_check_cmds.show, no_class, 1, &showlist);
  943. add_alias_cmd ("ch", setshow_check_cmds.show, no_class, 1, &showlist);
  944. range = type_or_range_names[3];
  945. gdb_assert (strcmp (range, "auto") == 0);
  946. add_setshow_enum_cmd ("range", class_support, type_or_range_names,
  947. &range,
  948. _("Set range checking (on/warn/off/auto)."),
  949. _("Show range checking (on/warn/off/auto)."),
  950. NULL, set_range_command,
  951. show_range_command,
  952. &setchecklist, &showchecklist);
  953. case_sensitive = case_sensitive_names[2];
  954. gdb_assert (strcmp (case_sensitive, "auto") == 0);
  955. add_setshow_enum_cmd ("case-sensitive", class_support, case_sensitive_names,
  956. &case_sensitive, _("\
  957. Set case sensitivity in name search (on/off/auto)."), _("\
  958. Show case sensitivity in name search (on/off/auto)."), _("\
  959. For Fortran the default is off; for other languages the default is on."),
  960. set_case_command,
  961. show_case_command,
  962. &setlist, &showlist);
  963. /* In order to call SET_LANGUAGE (below) we need to make sure that
  964. CURRENT_LANGUAGE is not NULL. So first set the language to unknown,
  965. then we can change the language to 'auto'. */
  966. current_language = language_def (language_unknown);
  967. add_set_language_command ();
  968. /* Have the above take effect. */
  969. set_language (language_auto);
  970. }