1 /* Source-language-related definitions for GDB. 2 3 Copyright (C) 1991-2024 Free Software Foundation, Inc. 4 5 Contributed by the Department of Computer Science at the State University 6 of New York at Buffalo. 7 8 This file is part of GDB. 9 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 12 the Free Software Foundation; either version 3 of the License, or 13 (at your option) any later version. 14 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. 19 20 You should have received a copy of the GNU General Public License 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 22 23 #if !defined (LANGUAGE_H) 24 #define LANGUAGE_H 1 25 26 #include "symtab.h" 27 #include "gdbsupport/function-view.h" 28 #include "expression.h" 29 30 /* Forward decls for prototypes. */ 31 struct value; 32 struct objfile; 33 class frame_info_ptr; 34 struct ui_file; 35 struct value_print_options; 36 struct type_print_options; 37 struct lang_varobj_ops; 38 struct parser_state; 39 class compile_instance; 40 struct completion_match_for_lcd; 41 class innermost_block_tracker; 42 43 #define MAX_FORTRAN_DIMS 7 /* Maximum number of F77 array dims. */ 44 45 /* range_check == 46 range_check_on: Ranges are checked in GDB expressions, producing errors. 47 range_check_warn: Ranges are checked, producing warnings. 48 range_check_off: Ranges are not checked in GDB expressions. */ 49 50 extern enum range_check 51 { 52 range_check_off, range_check_warn, range_check_on 53 } 54 range_check; 55 56 /* array_ordering == 57 array_row_major: Arrays are in row major order. 58 array_column_major: Arrays are in column major order. */ 59 60 extern enum array_ordering 61 { 62 array_row_major, array_column_major 63 } 64 array_ordering; 65 66 67 /* case_sensitivity == 68 case_sensitive_on: Case sensitivity in name matching is used. 69 case_sensitive_off: Case sensitivity in name matching is not used. */ 70 71 extern enum case_sensitivity 72 { 73 case_sensitive_on, case_sensitive_off 74 } 75 case_sensitivity; 76 77 78 /* macro_expansion == 79 macro_expansion_no: No macro expansion is available. 80 macro_expansion_c: C-like macro expansion is available. */ 81 82 enum macro_expansion 83 { 84 macro_expansion_no, macro_expansion_c 85 }; 86 87 88 /* Per architecture (OS/ABI) language information. */ 89 90 struct language_arch_info 91 { 92 /* A default constructor. */ 93 language_arch_info () = default; 94 95 DISABLE_COPY_AND_ASSIGN (language_arch_info); 96 97 /* Set the default boolean type to be TYPE. If NAME is not nullptr then 98 before using TYPE a symbol called NAME will be looked up, and the type 99 of this symbol will be used instead. Should only be called once when 100 performing setup for a particular language in combination with a 101 particular gdbarch. */ 102 void set_bool_type (struct type *type, const char *name = nullptr) 103 { 104 gdb_assert (m_bool_type_default == nullptr); 105 gdb_assert (m_bool_type_name == nullptr); 106 gdb_assert (type != nullptr); 107 m_bool_type_default = type; 108 m_bool_type_name = name; 109 } 110 111 /* Set the type to be used for characters within a string. Should only 112 be called once when performing setup for a particular language in 113 combination with a particular gdbarch. */ set_string_char_typelanguage_arch_info114 void set_string_char_type (struct type *type) 115 { 116 gdb_assert (m_string_char_type == nullptr); 117 gdb_assert (type != nullptr); 118 m_string_char_type = type; 119 } 120 121 /* Return the type for characters within a string. */ string_char_typelanguage_arch_info122 struct type *string_char_type () const 123 { return m_string_char_type; } 124 125 /* Return the type to be used for booleans. */ 126 struct type *bool_type () const; 127 128 /* Add TYPE to the list of primitive types for this particular language, 129 with this OS/ABI combination. */ add_primitive_typelanguage_arch_info130 void add_primitive_type (struct type *type) 131 { 132 gdb_assert (type != nullptr); 133 primitive_types_and_symbols.push_back (type_and_symbol (type)); 134 } 135 136 /* Lookup a primitive type called NAME. Will return nullptr if no 137 matching type is found. */ 138 struct type *lookup_primitive_type (const char *name); 139 140 /* Lookup a primitive type for which FILTER returns true. Will return 141 nullptr if no matching type is found. */ 142 struct type *lookup_primitive_type 143 (gdb::function_view<bool (struct type *)> filter); 144 145 /* Lookup a primitive type called NAME and return the type as a symbol. 146 LANG is the language for which type is being looked up. */ 147 struct symbol *lookup_primitive_type_as_symbol (const char *name, 148 enum language lang); 149 private: 150 151 /* A structure storing a type and a corresponding symbol. The type is 152 defined at construction time, while the symbol is lazily created only 153 when asked for, but is then cached for future use. */ 154 struct type_and_symbol 155 { 156 /* Constructor. */ type_and_symbollanguage_arch_info::type_and_symbol157 explicit type_and_symbol (struct type *type) 158 : m_type (type) 159 { /* Nothing. */ } 160 161 /* Default move constructor. */ 162 type_and_symbol (type_and_symbol&&) = default; 163 164 DISABLE_COPY_AND_ASSIGN (type_and_symbol); 165 166 /* Return the type from this object. */ typelanguage_arch_info::type_and_symbol167 struct type *type () const 168 { return m_type; } 169 170 /* Create and return a symbol wrapping M_TYPE from this object. */ symbollanguage_arch_info::type_and_symbol171 struct symbol *symbol (enum language lang) 172 { 173 if (m_symbol == nullptr) 174 m_symbol = alloc_type_symbol (lang, m_type); 175 return m_symbol; 176 } 177 178 private: 179 /* The type primitive type. */ 180 struct type *m_type = nullptr; 181 182 /* A symbol wrapping M_TYPE, only created when first asked for. */ 183 struct symbol *m_symbol = nullptr; 184 185 /* Helper function for type lookup as a symbol. Create the symbol 186 corresponding to type TYPE in language LANG. */ 187 static struct symbol *alloc_type_symbol (enum language lang, 188 struct type *type); 189 }; 190 191 /* Lookup a type_and_symbol entry from the primitive_types_and_symbols 192 vector for a type matching NAME. Return a pointer to the 193 type_and_symbol object from the vector. This will return nullptr if 194 there is no type matching NAME found. */ 195 type_and_symbol *lookup_primitive_type_and_symbol (const char *name); 196 197 /* Vector of the primitive types added through add_primitive_type. These 198 types can be specified by name in parsing types in expressions, 199 regardless of whether the program being debugged actually defines such 200 a type. 201 202 Within the vector each type is paired with a lazily created symbol, 203 which can be fetched by the symbol lookup machinery, should they be 204 needed. */ 205 std::vector<type_and_symbol> primitive_types_and_symbols; 206 207 /* Type of elements of strings. */ 208 struct type *m_string_char_type = nullptr; 209 210 /* Symbol name of type to use as boolean type, if defined. */ 211 const char *m_bool_type_name = nullptr; 212 213 /* Otherwise, this is the default boolean builtin type. */ 214 struct type *m_bool_type_default = nullptr; 215 }; 216 217 /* In a language (particularly C++) a function argument of an aggregate 218 type (i.e. class/struct/union) may be implicitly passed by reference 219 even though it is declared a call-by-value argument in the source. 220 The struct below puts together necessary information for GDB to be 221 able to detect and carry out pass-by-reference semantics for a 222 particular type. This type is referred as T in the inlined comments 223 below. 224 225 The default values of the fields are chosen to give correct semantics 226 for primitive types and for simple aggregate types, such as 227 228 class T { 229 int x; 230 }; */ 231 232 struct language_pass_by_ref_info 233 { 234 /* True if an argument of type T can be passed to a function by value 235 (i.e. not through an implicit reference). False, otherwise. */ 236 bool trivially_copyable = true; 237 238 /* True if a copy of a value of type T can be initialized by 239 memcpy'ing the value bit-by-bit. False, otherwise. 240 E.g. If T has a user-defined copy ctor, this should be false. */ 241 bool trivially_copy_constructible = true; 242 243 /* True if a value of type T can be destructed simply by reclaiming 244 the memory area occupied by the value. False, otherwise. 245 E.g. If T has a user-defined destructor, this should be false. */ 246 bool trivially_destructible = true; 247 248 /* True if it is allowed to create a copy of a value of type T. 249 False, otherwise. 250 E.g. If T has a deleted copy ctor, this should be false. */ 251 bool copy_constructible = true; 252 253 /* True if a value of type T can be destructed. False, otherwise. 254 E.g. If T has a deleted destructor, this should be false. */ 255 bool destructible = true; 256 }; 257 258 /* Splitting strings into words. */ 259 extern const char *default_word_break_characters (void); 260 261 /* Base class from which all other language classes derive. */ 262 263 struct language_defn 264 { language_defnlanguage_defn265 language_defn (enum language lang) 266 : la_language (lang) 267 { 268 /* We should only ever create one instance of each language. */ 269 gdb_assert (languages[lang] == nullptr); 270 languages[lang] = this; 271 } 272 273 /* Which language this is. */ 274 275 const enum language la_language; 276 277 /* Name of the language. */ 278 279 virtual const char *name () const = 0; 280 281 /* Natural or official name of the language. */ 282 283 virtual const char *natural_name () const = 0; 284 285 /* Digit separator of the language. */ 286 get_digit_separatorlanguage_defn287 virtual const char *get_digit_separator () const 288 { 289 return " "; 290 } 291 292 /* Return a vector of file extensions for this language. The extension 293 must include the ".", like ".c". If this language doesn't need to 294 provide any filename extensions, this may be an empty vector (which is 295 the default). */ 296 filename_extensionslanguage_defn297 virtual const std::vector<const char *> &filename_extensions () const 298 { 299 static const std::vector<const char *> no_extensions; 300 return no_extensions; 301 } 302 303 /* Print the index of an element of an array. This default 304 implementation prints using C99 syntax. */ 305 306 virtual void print_array_index (struct type *index_type, 307 LONGEST index_value, 308 struct ui_file *stream, 309 const value_print_options *options) const; 310 311 /* Given a symbol VAR, the corresponding block VAR_BLOCK (if any) and a 312 stack frame id FRAME, read the value of the variable and return (pointer 313 to a) struct value containing the value. 314 315 VAR_BLOCK is needed if there's a possibility for VAR to be outside 316 FRAME. This is what happens if FRAME correspond to a nested function 317 and VAR is defined in the outer function. If callers know that VAR is 318 located in FRAME or is global/static, NULL can be passed as VAR_BLOCK. 319 320 Throw an error if the variable cannot be found. */ 321 322 virtual struct value *read_var_value (struct symbol *var, 323 const struct block *var_block, 324 const frame_info_ptr &frame) const; 325 326 /* Return information about whether TYPE should be passed 327 (and returned) by reference at the language level. The default 328 implementation returns a LANGUAGE_PASS_BY_REF_INFO initialised in its 329 default state. */ 330 pass_by_reference_infolanguage_defn331 virtual struct language_pass_by_ref_info pass_by_reference_info 332 (struct type *type) const 333 { 334 return {}; 335 } 336 337 /* Return true if SYMBOL represents an entity that is not 338 supposed to be seen by the user. To be used to filter symbols 339 during printing. */ symbol_printing_suppressedlanguage_defn340 virtual bool symbol_printing_suppressed (struct symbol *symbol) const 341 { 342 return false; 343 } 344 345 /* The per-architecture (OS/ABI) language information. */ 346 347 virtual void language_arch_info (struct gdbarch *, 348 struct language_arch_info *) const = 0; 349 350 /* Find the definition of the type with the given name. */ 351 lookup_transparent_typelanguage_defn352 virtual struct type *lookup_transparent_type (const char *name, 353 domain_search_flags flags) const 354 { 355 return basic_lookup_transparent_type (name, flags); 356 } 357 358 /* Find all symbols in the current program space matching NAME in 359 DOMAIN, according to this language's rules. 360 361 The search is done in BLOCK only. 362 The caller is responsible for iterating up through superblocks 363 if desired. 364 365 For each one, call CALLBACK with the symbol. If CALLBACK 366 returns false, the iteration ends at that point. 367 368 This field may not be NULL. If the language does not need any 369 special processing here, 'iterate_over_symbols' should be 370 used as the definition. */ iterate_over_symbolslanguage_defn371 virtual bool iterate_over_symbols 372 (const struct block *block, const lookup_name_info &name, 373 domain_search_flags domain, 374 gdb::function_view<symbol_found_callback_ftype> callback) const 375 { 376 return ::iterate_over_symbols (block, name, domain, callback); 377 } 378 379 /* Return a pointer to the function that should be used to match a 380 symbol name against LOOKUP_NAME, according to this language's 381 rules. The matching algorithm depends on LOOKUP_NAME. For 382 example, on Ada, the matching algorithm depends on the symbol 383 name (wild/full/verbatim matching), and on whether we're doing 384 a normal lookup or a completion match lookup. 385 386 As Ada wants to capture symbol matching for all languages in some 387 cases, then this method is a non-overridable interface. Languages 388 should override GET_SYMBOL_NAME_MATCHER_INNER if they need to. */ 389 390 symbol_name_matcher_ftype *get_symbol_name_matcher 391 (const lookup_name_info &lookup_name) const; 392 393 /* If this language allows compilation from the gdb command line, 394 then this method will return an instance of struct gcc_context 395 appropriate to the language. If compilation for this language is 396 generally supported, but something goes wrong then an exception 397 is thrown. If compilation is not supported for this language 398 then this method returns NULL. */ 399 400 virtual std::unique_ptr<compile_instance> get_compile_instance () const; 401 402 /* This method must be overridden if 'get_compile_instance' is 403 overridden. 404 405 This takes the user-supplied text and returns a new bit of code 406 to compile. 407 408 INST is the compiler instance being used. 409 INPUT is the user's input text. 410 GDBARCH is the architecture to use. 411 EXPR_BLOCK is the block in which the expression is being 412 parsed. 413 EXPR_PC is the PC at which the expression is being parsed. */ 414 compute_programlanguage_defn415 virtual std::string compute_program (compile_instance *inst, 416 const char *input, 417 struct gdbarch *gdbarch, 418 const struct block *expr_block, 419 CORE_ADDR expr_pc) const 420 { 421 gdb_assert_not_reached ("language_defn::compute_program"); 422 } 423 424 /* Hash the given symbol search name. */ 425 virtual unsigned int search_name_hash (const char *name) const; 426 427 /* Demangle a symbol according to this language's rules. Unlike 428 la_demangle, this does not take any options. 429 430 *DEMANGLED will be set by this function. 431 432 If this function returns false, then *DEMANGLED must always be set 433 to NULL. 434 435 If this function returns true, the implementation may set this to 436 a xmalloc'd string holding the demangled form. However, it is 437 not required to. The string, if any, is owned by the caller. 438 439 The resulting string should be of the form that will be 440 installed into a symbol. */ sniff_from_mangled_namelanguage_defn441 virtual bool sniff_from_mangled_name 442 (const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled) const 443 { 444 *demangled = nullptr; 445 return false; 446 } 447 448 /* Return demangled language symbol version of MANGLED, or NULL. */ demangle_symbollanguage_defn449 virtual gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled, 450 int options) const 451 { 452 return nullptr; 453 } 454 455 /* Return true if this class' implementation of print_type can 456 handle the /o modifier. */ can_print_type_offsetslanguage_defn457 virtual bool can_print_type_offsets () const 458 { 459 return false; 460 } 461 462 /* Print TYPE to STREAM using syntax appropriate for this language. 463 LEVEL is the depth to indent lines by. VARSTRING, if not NULL or the 464 empty string, is the name of a variable and TYPE should be printed in 465 the form of a declaration of a variable named VARSTRING. */ 466 467 virtual void print_type (struct type *type, const char *varstring, 468 struct ui_file *stream, int show, int level, 469 const struct type_print_options *flags) const = 0; 470 471 /* PC is possibly an unknown languages trampoline. 472 If that PC falls in a trampoline belonging to this language, return 473 the address of the first pc in the real function, or 0 if it isn't a 474 language tramp for this language. */ skip_trampolinelanguage_defn475 virtual CORE_ADDR skip_trampoline (const frame_info_ptr &fi, CORE_ADDR pc) const 476 { 477 return (CORE_ADDR) 0; 478 } 479 480 /* Return class name of a mangled method name or NULL. */ class_name_from_physnamelanguage_defn481 virtual char *class_name_from_physname (const char *physname) const 482 { 483 return nullptr; 484 } 485 486 /* The list of characters forming word boundaries. */ word_break_characterslanguage_defn487 virtual const char *word_break_characters (void) const 488 { 489 return default_word_break_characters (); 490 } 491 492 /* Add to the completion tracker all symbols which are possible 493 completions for TEXT. WORD is the entire command on which the 494 completion is being made. If CODE is TYPE_CODE_UNDEF, then all 495 symbols should be examined; otherwise, only STRUCT_DOMAIN symbols 496 whose type has a code of CODE should be matched. */ 497 collect_symbol_completion_matcheslanguage_defn498 virtual void collect_symbol_completion_matches 499 (completion_tracker &tracker, 500 complete_symbol_mode mode, 501 symbol_name_match_type name_match_type, 502 const char *text, 503 const char *word, 504 enum type_code code) const 505 { 506 return default_collect_symbol_completion_matches_break_on 507 (tracker, mode, name_match_type, text, word, "", code); 508 } 509 510 /* This is a function that lookup_symbol will call when it gets to 511 the part of symbol lookup where C looks up static and global 512 variables. This default implements the basic C lookup rules. */ 513 514 virtual struct block_symbol lookup_symbol_nonlocal 515 (const char *name, 516 const struct block *block, 517 const domain_search_flags domain) const; 518 519 /* Return an expression that can be used for a location 520 watchpoint. TYPE is a pointer type that points to the memory 521 to watch, and ADDR is the address of the watched memory. */ 522 virtual gdb::unique_xmalloc_ptr<char> watch_location_expression 523 (struct type *type, CORE_ADDR addr) const; 524 525 /* List of all known languages. */ 526 static const struct language_defn *languages[nr_languages]; 527 528 /* Print a top-level value using syntax appropriate for this language. */ 529 virtual void value_print (struct value *val, struct ui_file *stream, 530 const struct value_print_options *options) const; 531 532 /* Print a value using syntax appropriate for this language. RECURSE is 533 the recursion depth. It is zero-based. */ 534 virtual void value_print_inner 535 (struct value *val, struct ui_file *stream, int recurse, 536 const struct value_print_options *options) const; 537 538 /* Parser function. */ 539 540 virtual int parser (struct parser_state *ps) const; 541 542 /* Print the character CH (of type CHTYPE) on STREAM as part of the 543 contents of a literal string whose delimiter is QUOTER. */ 544 545 virtual void emitchar (int ch, struct type *chtype, 546 struct ui_file *stream, int quoter) const; 547 548 virtual void printchar (int ch, struct type *chtype, 549 struct ui_file * stream) const; 550 551 /* Print the character string STRING, printing at most LENGTH characters. 552 Printing stops early if the number hits print_max_chars; repeat counts 553 are printed as appropriate. Print ellipses at the end if we 554 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */ 555 556 virtual void printstr (struct ui_file *stream, struct type *elttype, 557 const gdb_byte *string, unsigned int length, 558 const char *encoding, int force_ellipses, 559 const struct value_print_options *options) const; 560 561 562 /* Print a typedef using syntax appropriate for this language. 563 TYPE is the underlying type. NEW_SYMBOL is the symbol naming 564 the type. STREAM is the output stream on which to print. */ 565 566 virtual void print_typedef (struct type *type, struct symbol *new_symbol, 567 struct ui_file *stream) const; 568 569 /* Return true if TYPE is a string type. */ 570 virtual bool is_string_type_p (struct type *type) const; 571 572 /* Return true if TYPE is array-like. */ is_array_likelanguage_defn573 virtual bool is_array_like (struct type *type) const 574 { return false; } 575 576 /* Underlying implementation of value_to_array. Return a value of 577 array type that corresponds to VAL. The caller must ensure that 578 is_array_like is true for VAL's type. Return nullptr if the type 579 cannot be handled. */ to_arraylanguage_defn580 virtual struct value *to_array (struct value *val) const 581 { return nullptr; } 582 583 /* Return a string that is used by the 'set print max-depth' setting. 584 When GDB replaces a struct or union (during value printing) that is 585 "too deep" this string is displayed instead. The default value here 586 suits most languages. If overriding then the string here should 587 ideally be similar in style to the default; an opener, three '.', and 588 a closer. */ 589 struct_too_deep_ellipsislanguage_defn590 virtual const char *struct_too_deep_ellipsis () const 591 { return "{...}"; } 592 593 /* If this returns non-NULL then the string returned specifies the name 594 of the implicit local variable that refers to the current object 595 instance. Return NULL (the default) for languages that have no name 596 for the current object instance. */ 597 name_of_thislanguage_defn598 virtual const char *name_of_this () const 599 { return nullptr; } 600 601 /* Return false if the language has first-class arrays. Return true if 602 there are no array values, and array objects decay to pointers, as in 603 C. The default is true as currently most supported languages behave 604 in this manner. */ 605 c_style_arrays_planguage_defn606 virtual bool c_style_arrays_p () const 607 { return true; } 608 609 /* Return the index to use for extracting the first element of a string, 610 or as the lower bound when creating a new string. The default of 611 choosing 0 or 1 based on C_STYLE_ARRAYS_P works for all currently 612 supported languages except Modula-2. */ 613 string_lower_boundlanguage_defn614 virtual char string_lower_bound () const 615 { return c_style_arrays_p () ? 0 : 1; } 616 617 /* Return the LEN characters long string at PTR as a value suitable for 618 this language. GDBARCH is used to infer the character type. The 619 default implementation returns a null-terminated C string. */ 620 virtual struct value *value_string (struct gdbarch *gdbarch, 621 const char *ptr, ssize_t len) const; 622 623 /* Returns true if the symbols names should be stored in GDB's data 624 structures for minimal/partial/full symbols using their linkage (aka 625 mangled) form; false if the symbol names should be demangled first. 626 627 Most languages implement symbol lookup by comparing the demangled 628 names, in which case it is advantageous to store that information 629 already demangled, and so would return false, which is the default. 630 631 On the other hand, some languages have opted for doing symbol lookups 632 by comparing mangled names instead, for reasons usually specific to 633 the language. Those languages should override this function and 634 return true. 635 636 And finally, other languages such as C or Asm do not have the concept 637 of mangled vs demangled name, so those languages should also override 638 this function and return true, to prevent any accidental demangling 639 through an unrelated language's demangler. */ 640 store_sym_names_in_linkage_form_planguage_defn641 virtual bool store_sym_names_in_linkage_form_p () const 642 { return false; } 643 644 /* Default range checking preference. The return value from this 645 function provides the automatic setting for 'set check range'. As a 646 consequence a user is free to override this setting if they want. */ 647 range_checking_on_by_defaultlanguage_defn648 virtual bool range_checking_on_by_default () const 649 { return false; } 650 651 /* Is this language case sensitive? The return value from this function 652 provides the automatic setting for 'set case-sensitive', as a 653 consequence, a user is free to override this setting if they want. */ 654 case_sensitivitylanguage_defn655 virtual enum case_sensitivity case_sensitivity () const 656 { return case_sensitive_on; } 657 658 659 /* Multi-dimensional array ordering. */ 660 array_orderinglanguage_defn661 virtual enum array_ordering array_ordering () const 662 { return array_row_major; } 663 664 /* Style of macro expansion, if any, supported by this language. The 665 default is no macro expansion. */ 666 macro_expansionlanguage_defn667 virtual enum macro_expansion macro_expansion () const 668 { return macro_expansion_no; } 669 670 /* Return a structure containing various operations on varobj specific 671 for this language. */ 672 673 virtual const struct lang_varobj_ops *varobj_ops () const; 674 675 protected: 676 677 /* This is the overridable part of the GET_SYMBOL_NAME_MATCHER method. 678 See that method for a description of the arguments. */ 679 680 virtual symbol_name_matcher_ftype *get_symbol_name_matcher_inner 681 (const lookup_name_info &lookup_name) const; 682 }; 683 684 /* Return the current language. Normally code just uses the 685 'current_language' macro. */ 686 687 extern const struct language_defn *get_current_language (); 688 689 /* Pointer to the language_defn for our current language. This pointer 690 always points to *some* valid struct; it can be used without checking 691 it for validity. 692 693 The current language affects expression parsing and evaluation 694 (FIXME: it might be cleaner to make the evaluation-related stuff 695 separate exp_opcodes for each different set of semantics. We 696 should at least think this through more clearly with respect to 697 what happens if the language is changed between parsing and 698 evaluation) and printing of things like types and arrays. It does 699 *not* affect symbol-reading-- each source file in a symbol-file has 700 its own language and we should keep track of that regardless of the 701 language when symbols are read. If we want some manual setting for 702 the language of symbol files (e.g. detecting when ".c" files are 703 C++), it should be a separate setting from the current_language. */ 704 705 #define current_language (get_current_language ()) 706 707 /* Pointer to the language_defn expected by the user, e.g. the language 708 of main(), or the language we last mentioned in a message, or C. */ 709 710 extern const struct language_defn *expected_language; 711 712 /* Warning issued when current_language and the language of the current 713 frame do not match. */ 714 715 extern const char lang_frame_mismatch_warn[]; 716 717 /* language_mode == 718 language_mode_auto: current_language automatically set upon selection 719 of scope (e.g. stack frame) 720 language_mode_manual: current_language set only by user. */ 721 722 extern enum language_mode 723 { 724 language_mode_auto, language_mode_manual 725 } 726 language_mode; 727 728 /* Return the type that should be used for booleans for language L in 729 GDBARCH. */ 730 731 struct type *language_bool_type (const struct language_defn *l, 732 struct gdbarch *gdbarch); 733 734 /* Return the type that should be used for characters within a string for 735 language L in GDBARCH. */ 736 737 struct type *language_string_char_type (const struct language_defn *l, 738 struct gdbarch *gdbarch); 739 740 /* Look up a type from the set of OS/ABI specific types defined in 741 GDBARCH for language L. NAME is used for selecting the matching 742 type, and is passed through to the corresponding 743 lookup_primitive_type member function inside the language_arch_info 744 class. */ 745 746 struct type *language_lookup_primitive_type (const struct language_defn *l, 747 struct gdbarch *gdbarch, 748 const char *name); 749 750 /* Look up a type from the set of OS/ABI specific types defined in 751 GDBARCH for language L. FILTER is used for selecting the matching 752 type, and is passed through to the corresponding 753 lookup_primitive_type member function inside the language_arch_info 754 class. */ 755 756 struct type *language_lookup_primitive_type 757 (const struct language_defn *la, 758 struct gdbarch *gdbarch, 759 gdb::function_view<bool (struct type *)> filter); 760 761 /* Wrapper around language_lookup_primitive_type to return the 762 corresponding symbol. */ 763 764 struct symbol * 765 language_lookup_primitive_type_as_symbol (const struct language_defn *l, 766 struct gdbarch *gdbarch, 767 const char *name); 768 769 770 /* These macros define the behaviour of the expression 771 evaluator. */ 772 773 /* Should we range check values against the domain of their type? */ 774 #define RANGE_CHECK (range_check != range_check_off) 775 776 /* "cast" really means conversion. */ 777 /* FIXME -- should be a setting in language_defn. */ 778 #define CAST_IS_CONVERSION(LANG) ((LANG)->la_language == language_c || \ 779 (LANG)->la_language == language_cplus || \ 780 (LANG)->la_language == language_objc) 781 782 /* Print out the current language settings: language, range and 783 type checking. */ 784 785 extern void language_info (); 786 787 /* Set the current language to LANG. */ 788 789 extern void set_language (enum language lang); 790 791 typedef void lazily_set_language_ftype (); 792 extern void lazily_set_language (lazily_set_language_ftype *fun); 793 794 795 /* Test a character to decide whether it can be printed in literal form 796 or needs to be printed in another representation. For example, 797 in C the literal form of the character with octal value 141 is 'a' 798 and the "other representation" is '\141'. The "other representation" 799 is program language dependent. */ 800 801 #define PRINT_LITERAL_FORM(c) \ 802 ((c) >= 0x20 \ 803 && ((c) < 0x7F || (c) >= 0xA0) \ 804 && (!sevenbit_strings || (c) < 0x80)) 805 806 /* Error messages */ 807 808 extern void range_error (const char *, ...) ATTRIBUTE_PRINTF (1, 2); 809 810 /* Misc: The string representing a particular enum language. */ 811 812 extern enum language language_enum (const char *str); 813 814 extern const struct language_defn *language_def (enum language); 815 816 extern const char *language_str (enum language); 817 818 /* Check for a language-specific trampoline. */ 819 820 extern CORE_ADDR skip_language_trampoline (const frame_info_ptr &, CORE_ADDR pc); 821 822 /* Return information about whether TYPE should be passed 823 (and returned) by reference at the language level. */ 824 struct language_pass_by_ref_info language_pass_by_reference (struct type *type); 825 826 void c_get_string (struct value *value, 827 gdb::unique_xmalloc_ptr<gdb_byte> *buffer, 828 int *length, struct type **char_type, 829 const char **charset); 830 831 /* Get LANG's symbol_name_matcher method for LOOKUP_NAME. Returns 832 default_symbol_name_matcher if not set. LANG is used as a hint; 833 the function may ignore it depending on the current language and 834 LOOKUP_NAME. Specifically, if the current language is Ada, this 835 may return an Ada matcher regardless of LANG. */ 836 symbol_name_matcher_ftype *get_symbol_name_matcher 837 (const language_defn *lang, const lookup_name_info &lookup_name); 838 839 /* Save the current language and restore it upon destruction. */ 840 841 class scoped_restore_current_language 842 { 843 public: 844 845 scoped_restore_current_language (); 846 ~scoped_restore_current_language (); 847 scoped_restore_current_language(scoped_restore_current_language && other)848 scoped_restore_current_language (scoped_restore_current_language &&other) 849 { 850 m_lang = other.m_lang; 851 m_fun = other.m_fun; 852 other.dont_restore (); 853 } 854 855 scoped_restore_current_language (const scoped_restore_current_language &) 856 = delete; 857 scoped_restore_current_language &operator= 858 (const scoped_restore_current_language &) = delete; 859 860 /* Cancel restoring on scope exit. */ dont_restore()861 void dont_restore () 862 { 863 /* This is implemented using a sentinel value. */ 864 m_lang = nullptr; 865 m_fun = nullptr; 866 } 867 868 private: 869 870 const language_defn *m_lang; 871 lazily_set_language_ftype *m_fun; 872 }; 873 874 /* If language_mode is language_mode_auto, 875 then switch current language to the language of SYM 876 and restore current language upon destruction. 877 878 Else do nothing. */ 879 880 class scoped_switch_to_sym_language_if_auto 881 { 882 public: 883 scoped_switch_to_sym_language_if_auto(const struct symbol * sym)884 explicit scoped_switch_to_sym_language_if_auto (const struct symbol *sym) 885 { 886 if (language_mode == language_mode_auto) 887 { 888 m_lang = current_language->la_language; 889 m_switched = true; 890 set_language (sym->language ()); 891 } 892 else 893 { 894 m_switched = false; 895 /* Assign to m_lang to silence a GCC warning. See 896 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635. */ 897 m_lang = language_unknown; 898 } 899 } 900 ~scoped_switch_to_sym_language_if_auto()901 ~scoped_switch_to_sym_language_if_auto () 902 { 903 if (m_switched) 904 set_language (m_lang); 905 } 906 907 DISABLE_COPY_AND_ASSIGN (scoped_switch_to_sym_language_if_auto); 908 909 private: 910 bool m_switched; 911 enum language m_lang; 912 }; 913 914 #endif /* defined (LANGUAGE_H) */ 915