language.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /* Source-language-related definitions 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. #if !defined (LANGUAGE_H)
  17. #define LANGUAGE_H 1
  18. #include "symtab.h"
  19. #include "gdbsupport/function-view.h"
  20. #include "expression.h"
  21. /* Forward decls for prototypes. */
  22. struct value;
  23. struct objfile;
  24. struct frame_info;
  25. struct ui_file;
  26. struct value_print_options;
  27. struct type_print_options;
  28. struct lang_varobj_ops;
  29. struct parser_state;
  30. class compile_instance;
  31. struct completion_match_for_lcd;
  32. class innermost_block_tracker;
  33. #define MAX_FORTRAN_DIMS 7 /* Maximum number of F77 array dims. */
  34. /* range_check ==
  35. range_check_on: Ranges are checked in GDB expressions, producing errors.
  36. range_check_warn: Ranges are checked, producing warnings.
  37. range_check_off: Ranges are not checked in GDB expressions. */
  38. extern enum range_check
  39. {
  40. range_check_off, range_check_warn, range_check_on
  41. }
  42. range_check;
  43. /* array_ordering ==
  44. array_row_major: Arrays are in row major order.
  45. array_column_major: Arrays are in column major order. */
  46. extern enum array_ordering
  47. {
  48. array_row_major, array_column_major
  49. }
  50. array_ordering;
  51. /* case_sensitivity ==
  52. case_sensitive_on: Case sensitivity in name matching is used.
  53. case_sensitive_off: Case sensitivity in name matching is not used. */
  54. extern enum case_sensitivity
  55. {
  56. case_sensitive_on, case_sensitive_off
  57. }
  58. case_sensitivity;
  59. /* macro_expansion ==
  60. macro_expansion_no: No macro expansion is available.
  61. macro_expansion_c: C-like macro expansion is available. */
  62. enum macro_expansion
  63. {
  64. macro_expansion_no, macro_expansion_c
  65. };
  66. /* Per architecture (OS/ABI) language information. */
  67. struct language_arch_info
  68. {
  69. /* A default constructor. */
  70. language_arch_info () = default;
  71. DISABLE_COPY_AND_ASSIGN (language_arch_info);
  72. /* Set the default boolean type to be TYPE. If NAME is not nullptr then
  73. before using TYPE a symbol called NAME will be looked up, and the type
  74. of this symbol will be used instead. Should only be called once when
  75. performing setup for a particular language in combination with a
  76. particular gdbarch. */
  77. void set_bool_type (struct type *type, const char *name = nullptr)
  78. {
  79. gdb_assert (m_bool_type_default == nullptr);
  80. gdb_assert (m_bool_type_name == nullptr);
  81. gdb_assert (type != nullptr);
  82. m_bool_type_default = type;
  83. m_bool_type_name = name;
  84. }
  85. /* Set the type to be used for characters within a string. Should only
  86. be called once when performing setup for a particular language in
  87. combination with a particular gdbarch. */
  88. void set_string_char_type (struct type *type)
  89. {
  90. gdb_assert (m_string_char_type == nullptr);
  91. gdb_assert (type != nullptr);
  92. m_string_char_type = type;
  93. }
  94. /* Return the type for characters within a string. */
  95. struct type *string_char_type () const
  96. { return m_string_char_type; }
  97. /* Return the type to be used for booleans. */
  98. struct type *bool_type () const;
  99. /* Add TYPE to the list of primitive types for this particular language,
  100. with this OS/ABI combination. */
  101. void add_primitive_type (struct type *type)
  102. {
  103. gdb_assert (type != nullptr);
  104. primitive_types_and_symbols.push_back (type_and_symbol (type));
  105. }
  106. /* Lookup a primitive type called NAME. Will return nullptr if no
  107. matching type is found. */
  108. struct type *lookup_primitive_type (const char *name);
  109. /* Lookup a primitive type for which FILTER returns true. Will return
  110. nullptr if no matching type is found. */
  111. struct type *lookup_primitive_type
  112. (gdb::function_view<bool (struct type *)> filter);
  113. /* Lookup a primitive type called NAME and return the type as a symbol.
  114. LANG is the language for which type is being looked up. */
  115. struct symbol *lookup_primitive_type_as_symbol (const char *name,
  116. enum language lang);
  117. private:
  118. /* A structure storing a type and a corresponding symbol. The type is
  119. defined at construction time, while the symbol is lazily created only
  120. when asked for, but is then cached for future use. */
  121. struct type_and_symbol
  122. {
  123. /* Constructor. */
  124. explicit type_and_symbol (struct type *type)
  125. : m_type (type)
  126. { /* Nothing. */ }
  127. /* Default move constructor. */
  128. type_and_symbol (type_and_symbol&&) = default;
  129. DISABLE_COPY_AND_ASSIGN (type_and_symbol);
  130. /* Return the type from this object. */
  131. struct type *type () const
  132. { return m_type; }
  133. /* Create and return a symbol wrapping M_TYPE from this object. */
  134. struct symbol *symbol (enum language lang)
  135. {
  136. if (m_symbol == nullptr)
  137. m_symbol = alloc_type_symbol (lang, m_type);
  138. return m_symbol;
  139. }
  140. private:
  141. /* The type primitive type. */
  142. struct type *m_type = nullptr;
  143. /* A symbol wrapping M_TYPE, only created when first asked for. */
  144. struct symbol *m_symbol = nullptr;
  145. /* Helper function for type lookup as a symbol. Create the symbol
  146. corresponding to type TYPE in language LANG. */
  147. static struct symbol *alloc_type_symbol (enum language lang,
  148. struct type *type);
  149. };
  150. /* Lookup a type_and_symbol entry from the primitive_types_and_symbols
  151. vector for a type matching NAME. Return a pointer to the
  152. type_and_symbol object from the vector. This will return nullptr if
  153. there is no type matching NAME found. */
  154. type_and_symbol *lookup_primitive_type_and_symbol (const char *name);
  155. /* Vector of the primitive types added through add_primitive_type. These
  156. types can be specified by name in parsing types in expressions,
  157. regardless of whether the program being debugged actually defines such
  158. a type.
  159. Within the vector each type is paired with a lazily created symbol,
  160. which can be fetched by the symbol lookup machinery, should they be
  161. needed. */
  162. std::vector<type_and_symbol> primitive_types_and_symbols;
  163. /* Type of elements of strings. */
  164. struct type *m_string_char_type = nullptr;
  165. /* Symbol name of type to use as boolean type, if defined. */
  166. const char *m_bool_type_name = nullptr;
  167. /* Otherwise, this is the default boolean builtin type. */
  168. struct type *m_bool_type_default = nullptr;
  169. };
  170. /* In a language (particularly C++) a function argument of an aggregate
  171. type (i.e. class/struct/union) may be implicitly passed by reference
  172. even though it is declared a call-by-value argument in the source.
  173. The struct below puts together necessary information for GDB to be
  174. able to detect and carry out pass-by-reference semantics for a
  175. particular type. This type is referred as T in the inlined comments
  176. below.
  177. The default values of the fields are chosen to give correct semantics
  178. for primitive types and for simple aggregate types, such as
  179. class T {
  180. int x;
  181. }; */
  182. struct language_pass_by_ref_info
  183. {
  184. /* True if an argument of type T can be passed to a function by value
  185. (i.e. not through an implicit reference). False, otherwise. */
  186. bool trivially_copyable = true;
  187. /* True if a copy of a value of type T can be initialized by
  188. memcpy'ing the value bit-by-bit. False, otherwise.
  189. E.g. If T has a user-defined copy ctor, this should be false. */
  190. bool trivially_copy_constructible = true;
  191. /* True if a value of type T can be destructed simply by reclaiming
  192. the memory area occupied by the value. False, otherwise.
  193. E.g. If T has a user-defined destructor, this should be false. */
  194. bool trivially_destructible = true;
  195. /* True if it is allowed to create a copy of a value of type T.
  196. False, otherwise.
  197. E.g. If T has a deleted copy ctor, this should be false. */
  198. bool copy_constructible = true;
  199. /* True if a value of type T can be destructed. False, otherwise.
  200. E.g. If T has a deleted destructor, this should be false. */
  201. bool destructible = true;
  202. };
  203. /* Splitting strings into words. */
  204. extern const char *default_word_break_characters (void);
  205. /* Base class from which all other language classes derive. */
  206. struct language_defn
  207. {
  208. language_defn (enum language lang)
  209. : la_language (lang)
  210. {
  211. /* We should only ever create one instance of each language. */
  212. gdb_assert (languages[lang] == nullptr);
  213. languages[lang] = this;
  214. }
  215. /* Which language this is. */
  216. const enum language la_language;
  217. /* Name of the language. */
  218. virtual const char *name () const = 0;
  219. /* Natural or official name of the language. */
  220. virtual const char *natural_name () const = 0;
  221. /* Return a vector of file extensions for this language. The extension
  222. must include the ".", like ".c". If this language doesn't need to
  223. provide any filename extensions, this may be an empty vector (which is
  224. the default). */
  225. virtual const std::vector<const char *> &filename_extensions () const
  226. {
  227. static const std::vector<const char *> no_extensions;
  228. return no_extensions;
  229. }
  230. /* Print the index of an element of an array. This default
  231. implementation prints using C99 syntax. */
  232. virtual void print_array_index (struct type *index_type,
  233. LONGEST index_value,
  234. struct ui_file *stream,
  235. const value_print_options *options) const;
  236. /* Given a symbol VAR, the corresponding block VAR_BLOCK (if any) and a
  237. stack frame id FRAME, read the value of the variable and return (pointer
  238. to a) struct value containing the value.
  239. VAR_BLOCK is needed if there's a possibility for VAR to be outside
  240. FRAME. This is what happens if FRAME correspond to a nested function
  241. and VAR is defined in the outer function. If callers know that VAR is
  242. located in FRAME or is global/static, NULL can be passed as VAR_BLOCK.
  243. Throw an error if the variable cannot be found. */
  244. virtual struct value *read_var_value (struct symbol *var,
  245. const struct block *var_block,
  246. struct frame_info *frame) const;
  247. /* Return information about whether TYPE should be passed
  248. (and returned) by reference at the language level. The default
  249. implementation returns a LANGUAGE_PASS_BY_REF_INFO initialised in its
  250. default state. */
  251. virtual struct language_pass_by_ref_info pass_by_reference_info
  252. (struct type *type) const
  253. {
  254. return {};
  255. }
  256. /* Return true if SYMBOL represents an entity that is not
  257. supposed to be seen by the user. To be used to filter symbols
  258. during printing. */
  259. virtual bool symbol_printing_suppressed (struct symbol *symbol) const
  260. {
  261. return false;
  262. }
  263. /* The per-architecture (OS/ABI) language information. */
  264. virtual void language_arch_info (struct gdbarch *,
  265. struct language_arch_info *) const = 0;
  266. /* Find the definition of the type with the given name. */
  267. virtual struct type *lookup_transparent_type (const char *name) const
  268. {
  269. return basic_lookup_transparent_type (name);
  270. }
  271. /* Find all symbols in the current program space matching NAME in
  272. DOMAIN, according to this language's rules.
  273. The search is done in BLOCK only.
  274. The caller is responsible for iterating up through superblocks
  275. if desired.
  276. For each one, call CALLBACK with the symbol. If CALLBACK
  277. returns false, the iteration ends at that point.
  278. This field may not be NULL. If the language does not need any
  279. special processing here, 'iterate_over_symbols' should be
  280. used as the definition. */
  281. virtual bool iterate_over_symbols
  282. (const struct block *block, const lookup_name_info &name,
  283. domain_enum domain,
  284. gdb::function_view<symbol_found_callback_ftype> callback) const
  285. {
  286. return ::iterate_over_symbols (block, name, domain, callback);
  287. }
  288. /* Return a pointer to the function that should be used to match a
  289. symbol name against LOOKUP_NAME, according to this language's
  290. rules. The matching algorithm depends on LOOKUP_NAME. For
  291. example, on Ada, the matching algorithm depends on the symbol
  292. name (wild/full/verbatim matching), and on whether we're doing
  293. a normal lookup or a completion match lookup.
  294. As Ada wants to capture symbol matching for all languages in some
  295. cases, then this method is a non-overridable interface. Languages
  296. should override GET_SYMBOL_NAME_MATCHER_INNER if they need to. */
  297. symbol_name_matcher_ftype *get_symbol_name_matcher
  298. (const lookup_name_info &lookup_name) const;
  299. /* If this language allows compilation from the gdb command line,
  300. then this method will return an instance of struct gcc_context
  301. appropriate to the language. If compilation for this language is
  302. generally supported, but something goes wrong then an exception
  303. is thrown. If compilation is not supported for this language
  304. then this method returns NULL. */
  305. virtual std::unique_ptr<compile_instance> get_compile_instance () const;
  306. /* This method must be overridden if 'get_compile_instance' is
  307. overridden.
  308. This takes the user-supplied text and returns a new bit of code
  309. to compile.
  310. INST is the compiler instance being used.
  311. INPUT is the user's input text.
  312. GDBARCH is the architecture to use.
  313. EXPR_BLOCK is the block in which the expression is being
  314. parsed.
  315. EXPR_PC is the PC at which the expression is being parsed. */
  316. virtual std::string compute_program (compile_instance *inst,
  317. const char *input,
  318. struct gdbarch *gdbarch,
  319. const struct block *expr_block,
  320. CORE_ADDR expr_pc) const
  321. {
  322. gdb_assert_not_reached ("language_defn::compute_program");
  323. }
  324. /* Hash the given symbol search name. */
  325. virtual unsigned int search_name_hash (const char *name) const;
  326. /* Demangle a symbol according to this language's rules. Unlike
  327. la_demangle, this does not take any options.
  328. *DEMANGLED will be set by this function.
  329. If this function returns false, then *DEMANGLED must always be set
  330. to NULL.
  331. If this function returns true, the implementation may set this to
  332. a xmalloc'd string holding the demangled form. However, it is
  333. not required to. The string, if any, is owned by the caller.
  334. The resulting string should be of the form that will be
  335. installed into a symbol. */
  336. virtual bool sniff_from_mangled_name
  337. (const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled) const
  338. {
  339. *demangled = nullptr;
  340. return false;
  341. }
  342. /* Return demangled language symbol version of MANGLED, or NULL. */
  343. virtual gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
  344. int options) const
  345. {
  346. return nullptr;
  347. }
  348. /* Print TYPE to STREAM using syntax appropriate for this language.
  349. LEVEL is the depth to indent lines by. VARSTRING, if not NULL or the
  350. empty string, is the name of a variable and TYPE should be printed in
  351. the form of a declaration of a variable named VARSTRING. */
  352. virtual void print_type (struct type *type, const char *varstring,
  353. struct ui_file *stream, int show, int level,
  354. const struct type_print_options *flags) const = 0;
  355. /* PC is possibly an unknown languages trampoline.
  356. If that PC falls in a trampoline belonging to this language, return
  357. the address of the first pc in the real function, or 0 if it isn't a
  358. language tramp for this language. */
  359. virtual CORE_ADDR skip_trampoline (struct frame_info *fi, CORE_ADDR pc) const
  360. {
  361. return (CORE_ADDR) 0;
  362. }
  363. /* Return class name of a mangled method name or NULL. */
  364. virtual char *class_name_from_physname (const char *physname) const
  365. {
  366. return nullptr;
  367. }
  368. /* The list of characters forming word boundaries. */
  369. virtual const char *word_break_characters (void) const
  370. {
  371. return default_word_break_characters ();
  372. }
  373. /* Add to the completion tracker all symbols which are possible
  374. completions for TEXT. WORD is the entire command on which the
  375. completion is being made. If CODE is TYPE_CODE_UNDEF, then all
  376. symbols should be examined; otherwise, only STRUCT_DOMAIN symbols
  377. whose type has a code of CODE should be matched. */
  378. virtual void collect_symbol_completion_matches
  379. (completion_tracker &tracker,
  380. complete_symbol_mode mode,
  381. symbol_name_match_type name_match_type,
  382. const char *text,
  383. const char *word,
  384. enum type_code code) const
  385. {
  386. return default_collect_symbol_completion_matches_break_on
  387. (tracker, mode, name_match_type, text, word, "", code);
  388. }
  389. /* This is a function that lookup_symbol will call when it gets to
  390. the part of symbol lookup where C looks up static and global
  391. variables. This default implements the basic C lookup rules. */
  392. virtual struct block_symbol lookup_symbol_nonlocal
  393. (const char *name,
  394. const struct block *block,
  395. const domain_enum domain) const;
  396. /* Return an expression that can be used for a location
  397. watchpoint. TYPE is a pointer type that points to the memory
  398. to watch, and ADDR is the address of the watched memory. */
  399. virtual gdb::unique_xmalloc_ptr<char> watch_location_expression
  400. (struct type *type, CORE_ADDR addr) const;
  401. /* List of all known languages. */
  402. static const struct language_defn *languages[nr_languages];
  403. /* Print a top-level value using syntax appropriate for this language. */
  404. virtual void value_print (struct value *val, struct ui_file *stream,
  405. const struct value_print_options *options) const;
  406. /* Print a value using syntax appropriate for this language. RECURSE is
  407. the recursion depth. It is zero-based. */
  408. virtual void value_print_inner
  409. (struct value *val, struct ui_file *stream, int recurse,
  410. const struct value_print_options *options) const;
  411. /* Parser function. */
  412. virtual int parser (struct parser_state *ps) const;
  413. /* Print the character CH (of type CHTYPE) on STREAM as part of the
  414. contents of a literal string whose delimiter is QUOTER. */
  415. virtual void emitchar (int ch, struct type *chtype,
  416. struct ui_file *stream, int quoter) const;
  417. virtual void printchar (int ch, struct type *chtype,
  418. struct ui_file * stream) const;
  419. /* Print the character string STRING, printing at most LENGTH characters.
  420. Printing stops early if the number hits print_max; repeat counts
  421. are printed as appropriate. Print ellipses at the end if we
  422. had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */
  423. virtual void printstr (struct ui_file *stream, struct type *elttype,
  424. const gdb_byte *string, unsigned int length,
  425. const char *encoding, int force_ellipses,
  426. const struct value_print_options *options) const;
  427. /* Print a typedef using syntax appropriate for this language.
  428. TYPE is the underlying type. NEW_SYMBOL is the symbol naming
  429. the type. STREAM is the output stream on which to print. */
  430. virtual void print_typedef (struct type *type, struct symbol *new_symbol,
  431. struct ui_file *stream) const;
  432. /* Return true if TYPE is a string type. */
  433. virtual bool is_string_type_p (struct type *type) const;
  434. /* Return a string that is used by the 'set print max-depth' setting.
  435. When GDB replaces a struct or union (during value printing) that is
  436. "too deep" this string is displayed instead. The default value here
  437. suits most languages. If overriding then the string here should
  438. ideally be similar in style to the default; an opener, three '.', and
  439. a closer. */
  440. virtual const char *struct_too_deep_ellipsis () const
  441. { return "{...}"; }
  442. /* If this returns non-NULL then the string returned specifies the name
  443. of the implicit local variable that refers to the current object
  444. instance. Return NULL (the default) for languages that have no name
  445. for the current object instance. */
  446. virtual const char *name_of_this () const
  447. { return nullptr; }
  448. /* Return false if the language has first-class arrays. Return true if
  449. there are no array values, and array objects decay to pointers, as in
  450. C. The default is true as currently most supported languages behave
  451. in this manner. */
  452. virtual bool c_style_arrays_p () const
  453. { return true; }
  454. /* Return the index to use for extracting the first element of a string,
  455. or as the lower bound when creating a new string. The default of
  456. choosing 0 or 1 based on C_STYLE_ARRAYS_P works for all currently
  457. supported languages except Modula-2. */
  458. virtual char string_lower_bound () const
  459. { return c_style_arrays_p () ? 0 : 1; }
  460. /* Returns true if the symbols names should be stored in GDB's data
  461. structures for minimal/partial/full symbols using their linkage (aka
  462. mangled) form; false if the symbol names should be demangled first.
  463. Most languages implement symbol lookup by comparing the demangled
  464. names, in which case it is advantageous to store that information
  465. already demangled, and so would return false, which is the default.
  466. On the other hand, some languages have opted for doing symbol lookups
  467. by comparing mangled names instead, for reasons usually specific to
  468. the language. Those languages should override this function and
  469. return true.
  470. And finally, other languages such as C or Asm do not have the concept
  471. of mangled vs demangled name, so those languages should also override
  472. this function and return true, to prevent any accidental demangling
  473. through an unrelated language's demangler. */
  474. virtual bool store_sym_names_in_linkage_form_p () const
  475. { return false; }
  476. /* Default range checking preference. The return value from this
  477. function provides the automatic setting for 'set check range'. As a
  478. consequence a user is free to override this setting if they want. */
  479. virtual bool range_checking_on_by_default () const
  480. { return false; }
  481. /* Is this language case sensitive? The return value from this function
  482. provides the automativ setting for 'set case-sensitive', as a
  483. consequence, a user is free to override this setting if they want. */
  484. virtual enum case_sensitivity case_sensitivity () const
  485. { return case_sensitive_on; }
  486. /* Multi-dimensional array ordering. */
  487. virtual enum array_ordering array_ordering () const
  488. { return array_row_major; }
  489. /* Style of macro expansion, if any, supported by this language. The
  490. default is no macro expansion. */
  491. virtual enum macro_expansion macro_expansion () const
  492. { return macro_expansion_no; }
  493. /* Return a structure containing various operations on varobj specific
  494. for this language. */
  495. virtual const struct lang_varobj_ops *varobj_ops () const;
  496. protected:
  497. /* This is the overridable part of the GET_SYMBOL_NAME_MATCHER method.
  498. See that method for a description of the arguments. */
  499. virtual symbol_name_matcher_ftype *get_symbol_name_matcher_inner
  500. (const lookup_name_info &lookup_name) const;
  501. };
  502. /* Pointer to the language_defn for our current language. This pointer
  503. always points to *some* valid struct; it can be used without checking
  504. it for validity.
  505. The current language affects expression parsing and evaluation
  506. (FIXME: it might be cleaner to make the evaluation-related stuff
  507. separate exp_opcodes for each different set of semantics. We
  508. should at least think this through more clearly with respect to
  509. what happens if the language is changed between parsing and
  510. evaluation) and printing of things like types and arrays. It does
  511. *not* affect symbol-reading-- each source file in a symbol-file has
  512. its own language and we should keep track of that regardless of the
  513. language when symbols are read. If we want some manual setting for
  514. the language of symbol files (e.g. detecting when ".c" files are
  515. C++), it should be a separate setting from the current_language. */
  516. extern const struct language_defn *current_language;
  517. /* Pointer to the language_defn expected by the user, e.g. the language
  518. of main(), or the language we last mentioned in a message, or C. */
  519. extern const struct language_defn *expected_language;
  520. /* Warning issued when current_language and the language of the current
  521. frame do not match. */
  522. extern const char lang_frame_mismatch_warn[];
  523. /* language_mode ==
  524. language_mode_auto: current_language automatically set upon selection
  525. of scope (e.g. stack frame)
  526. language_mode_manual: current_language set only by user. */
  527. extern enum language_mode
  528. {
  529. language_mode_auto, language_mode_manual
  530. }
  531. language_mode;
  532. /* Return the type that should be used for booleans for language L in
  533. GDBARCH. */
  534. struct type *language_bool_type (const struct language_defn *l,
  535. struct gdbarch *gdbarch);
  536. /* Return the type that should be used for characters within a string for
  537. language L in GDBARCH. */
  538. struct type *language_string_char_type (const struct language_defn *l,
  539. struct gdbarch *gdbarch);
  540. /* Look up a type from the set of OS/ABI specific types defined in
  541. GDBARCH for language L. NAME is used for selecting the matching
  542. type, and is passed through to the corresponding
  543. lookup_primitive_type member function inside the language_arch_info
  544. class. */
  545. struct type *language_lookup_primitive_type (const struct language_defn *l,
  546. struct gdbarch *gdbarch,
  547. const char *name);
  548. /* Look up a type from the set of OS/ABI specific types defined in
  549. GDBARCH for language L. FILTER is used for selecting the matching
  550. type, and is passed through to the corresponding
  551. lookup_primitive_type member function inside the language_arch_info
  552. class. */
  553. struct type *language_lookup_primitive_type
  554. (const struct language_defn *la,
  555. struct gdbarch *gdbarch,
  556. gdb::function_view<bool (struct type *)> filter);
  557. /* Wrapper around language_lookup_primitive_type to return the
  558. corresponding symbol. */
  559. struct symbol *
  560. language_lookup_primitive_type_as_symbol (const struct language_defn *l,
  561. struct gdbarch *gdbarch,
  562. const char *name);
  563. /* These macros define the behaviour of the expression
  564. evaluator. */
  565. /* Should we range check values against the domain of their type? */
  566. #define RANGE_CHECK (range_check != range_check_off)
  567. /* "cast" really means conversion. */
  568. /* FIXME -- should be a setting in language_defn. */
  569. #define CAST_IS_CONVERSION(LANG) ((LANG)->la_language == language_c || \
  570. (LANG)->la_language == language_cplus || \
  571. (LANG)->la_language == language_objc)
  572. /* Print out the current language settings: language, range and
  573. type checking. */
  574. extern void language_info ();
  575. extern enum language set_language (enum language);
  576. /* Test a character to decide whether it can be printed in literal form
  577. or needs to be printed in another representation. For example,
  578. in C the literal form of the character with octal value 141 is 'a'
  579. and the "other representation" is '\141'. The "other representation"
  580. is program language dependent. */
  581. #define PRINT_LITERAL_FORM(c) \
  582. ((c) >= 0x20 \
  583. && ((c) < 0x7F || (c) >= 0xA0) \
  584. && (!sevenbit_strings || (c) < 0x80))
  585. /* Error messages */
  586. extern void range_error (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
  587. /* Misc: The string representing a particular enum language. */
  588. extern enum language language_enum (const char *str);
  589. extern const struct language_defn *language_def (enum language);
  590. extern const char *language_str (enum language);
  591. /* Check for a language-specific trampoline. */
  592. extern CORE_ADDR skip_language_trampoline (struct frame_info *, CORE_ADDR pc);
  593. /* Return demangled language symbol, or NULL. */
  594. extern gdb::unique_xmalloc_ptr<char> language_demangle
  595. (const struct language_defn *current_language,
  596. const char *mangled, int options);
  597. /* Return information about whether TYPE should be passed
  598. (and returned) by reference at the language level. */
  599. struct language_pass_by_ref_info language_pass_by_reference (struct type *type);
  600. void c_get_string (struct value *value,
  601. gdb::unique_xmalloc_ptr<gdb_byte> *buffer,
  602. int *length, struct type **char_type,
  603. const char **charset);
  604. /* Get LANG's symbol_name_matcher method for LOOKUP_NAME. Returns
  605. default_symbol_name_matcher if not set. LANG is used as a hint;
  606. the function may ignore it depending on the current language and
  607. LOOKUP_NAME. Specifically, if the current language is Ada, this
  608. may return an Ada matcher regardless of LANG. */
  609. symbol_name_matcher_ftype *get_symbol_name_matcher
  610. (const language_defn *lang, const lookup_name_info &lookup_name);
  611. /* Save the current language and restore it upon destruction. */
  612. class scoped_restore_current_language
  613. {
  614. public:
  615. explicit scoped_restore_current_language ()
  616. : m_lang (current_language->la_language)
  617. {
  618. }
  619. ~scoped_restore_current_language ()
  620. {
  621. set_language (m_lang);
  622. }
  623. scoped_restore_current_language (const scoped_restore_current_language &)
  624. = delete;
  625. scoped_restore_current_language &operator=
  626. (const scoped_restore_current_language &) = delete;
  627. private:
  628. enum language m_lang;
  629. };
  630. /* If language_mode is language_mode_auto,
  631. then switch current language to the language of SYM
  632. and restore current language upon destruction.
  633. Else do nothing. */
  634. class scoped_switch_to_sym_language_if_auto
  635. {
  636. public:
  637. explicit scoped_switch_to_sym_language_if_auto (const struct symbol *sym)
  638. {
  639. if (language_mode == language_mode_auto)
  640. {
  641. m_lang = current_language->la_language;
  642. m_switched = true;
  643. set_language (sym->language ());
  644. }
  645. else
  646. {
  647. m_switched = false;
  648. /* Assign to m_lang to silence a GCC warning. See
  649. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635. */
  650. m_lang = language_unknown;
  651. }
  652. }
  653. ~scoped_switch_to_sym_language_if_auto ()
  654. {
  655. if (m_switched)
  656. set_language (m_lang);
  657. }
  658. DISABLE_COPY_AND_ASSIGN (scoped_switch_to_sym_language_if_auto);
  659. private:
  660. bool m_switched;
  661. enum language m_lang;
  662. };
  663. #endif /* defined (LANGUAGE_H) */