1 /* Source-language-related definitions for GDB.
3 Copyright (C) 1991-2020 Free Software Foundation, Inc.
5 Contributed by the Department of Computer Science at the State University
6 of New York at Buffalo.
8 This file is part of GDB.
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.
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.
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/>. */
23 #if !defined (LANGUAGE_H)
27 #include "gdbsupport/function-view.h"
28 #include "expression.h"
30 /* Forward decls for prototypes. */
35 struct value_print_options
;
36 struct type_print_options
;
37 struct lang_varobj_ops
;
39 class compile_instance
;
40 struct completion_match_for_lcd
;
41 class innermost_block_tracker
;
43 #define MAX_FORTRAN_DIMS 7 /* Maximum number of F77 array dims. */
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. */
50 extern enum range_check
52 range_check_off
, range_check_warn
, range_check_on
57 array_row_major: Arrays are in row major order.
58 array_column_major: Arrays are in column major order. */
60 extern enum array_ordering
62 array_row_major
, array_column_major
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. */
71 extern enum case_sensitivity
73 case_sensitive_on
, case_sensitive_off
79 macro_expansion_no: No macro expansion is available.
80 macro_expansion_c: C-like macro expansion is available. */
84 macro_expansion_no
, macro_expansion_c
88 /* Per architecture (OS/ABI) language information. */
90 struct language_arch_info
92 /* Its primitive types. This is a vector ended by a NULL pointer.
93 These types can be specified by name in parsing types in
94 expressions, regardless of whether the program being debugged
95 actually defines such a type. */
96 struct type
**primitive_type_vector
;
98 /* Symbol wrappers around primitive_type_vector, so that the symbol lookup
99 machinery can return them. */
100 struct symbol
**primitive_type_symbols
;
102 /* Type of elements of strings. */
103 struct type
*string_char_type
;
105 /* Symbol name of type to use as boolean type, if defined. */
106 const char *bool_type_symbol
;
107 /* Otherwise, this is the default boolean builtin type. */
108 struct type
*bool_type_default
;
111 /* In a language (particularly C++) a function argument of an aggregate
112 type (i.e. class/struct/union) may be implicitly passed by reference
113 even though it is declared a call-by-value argument in the source.
114 The struct below puts together necessary information for GDB to be
115 able to detect and carry out pass-by-reference semantics for a
116 particular type. This type is referred as T in the inlined comments
119 The default values of the fields are chosen to give correct semantics
120 for primitive types and for simple aggregate types, such as
126 struct language_pass_by_ref_info
128 /* True if an argument of type T can be passed to a function by value
129 (i.e. not through an implicit reference). False, otherwise. */
130 bool trivially_copyable
= true;
132 /* True if a copy of a value of type T can be initialized by
133 memcpy'ing the value bit-by-bit. False, otherwise.
134 E.g. If T has a user-defined copy ctor, this should be false. */
135 bool trivially_copy_constructible
= true;
137 /* True if a value of type T can be destructed simply by reclaiming
138 the memory area occupied by the value. False, otherwise.
139 E.g. If T has a user-defined destructor, this should be false. */
140 bool trivially_destructible
= true;
142 /* True if it is allowed to create a copy of a value of type T.
144 E.g. If T has a deleted copy ctor, this should be false. */
145 bool copy_constructible
= true;
147 /* True if a value of type T can be destructed. False, otherwise.
148 E.g. If T has a deleted destructor, this should be false. */
149 bool destructible
= true;
152 /* Splitting strings into words. */
153 extern const char *default_word_break_characters (void);
155 /* Structure tying together assorted information about a language.
157 As we move over from the old structure based languages to a class
158 hierarchy of languages this structure will continue to contain a
159 mixture of both data and function pointers.
161 Once the class hierarchy of languages in place the first task is to
162 remove the function pointers from this structure and convert them into
163 member functions on the different language classes.
165 The current plan it to keep the constant data that describes a language
166 in this structure, and have each language pass in an instance of this
167 structure at construction time. */
171 /* Definitions related to expression printing, prefixifying, and
174 const struct exp_descriptor
*la_exp_desc
;
176 /* Table for printing expressions. */
178 const struct op_print
*la_op_print_tab
;
181 /* Base class from which all other language classes derive. */
183 struct language_defn
: language_data
185 language_defn (enum language lang
, const language_data
&init_data
)
186 : language_data (init_data
),
189 /* We should only ever create one instance of each language. */
190 gdb_assert (languages
[lang
] == nullptr);
191 languages
[lang
] = this;
194 /* Which language this is. */
196 const enum language la_language
;
198 /* Name of the language. */
200 virtual const char *name () const = 0;
202 /* Natural or official name of the language. */
204 virtual const char *natural_name () const = 0;
206 /* Return a vector of file extensions for this language. The extension
207 must include the ".", like ".c". If this language doesn't need to
208 provide any filename extensions, this may be an empty vector (which is
211 virtual const std::vector
<const char *> &filename_extensions () const
213 static const std::vector
<const char *> no_extensions
;
214 return no_extensions
;
217 /* Print the index of an element of an array. This default
218 implementation prints using C99 syntax. */
220 virtual void print_array_index (struct type
*index_type
,
222 struct ui_file
*stream
,
223 const value_print_options
*options
) const;
225 /* Given a symbol VAR, the corresponding block VAR_BLOCK (if any) and a
226 stack frame id FRAME, read the value of the variable and return (pointer
227 to a) struct value containing the value.
229 VAR_BLOCK is needed if there's a possibility for VAR to be outside
230 FRAME. This is what happens if FRAME correspond to a nested function
231 and VAR is defined in the outer function. If callers know that VAR is
232 located in FRAME or is global/static, NULL can be passed as VAR_BLOCK.
234 Throw an error if the variable cannot be found. */
236 virtual struct value
*read_var_value (struct symbol
*var
,
237 const struct block
*var_block
,
238 struct frame_info
*frame
) const;
240 /* Return information about whether TYPE should be passed
241 (and returned) by reference at the language level. The default
242 implementation returns a LANGUAGE_PASS_BY_REF_INFO initialised in its
245 virtual struct language_pass_by_ref_info pass_by_reference_info
246 (struct type
*type
) const
251 /* The per-architecture (OS/ABI) language information. */
253 virtual void language_arch_info (struct gdbarch
*,
254 struct language_arch_info
*) const = 0;
256 /* Find the definition of the type with the given name. */
258 virtual struct type
*lookup_transparent_type (const char *name
) const
260 return basic_lookup_transparent_type (name
);
263 /* Find all symbols in the current program space matching NAME in
264 DOMAIN, according to this language's rules.
266 The search is done in BLOCK only.
267 The caller is responsible for iterating up through superblocks
270 For each one, call CALLBACK with the symbol. If CALLBACK
271 returns false, the iteration ends at that point.
273 This field may not be NULL. If the language does not need any
274 special processing here, 'iterate_over_symbols' should be
275 used as the definition. */
276 virtual bool iterate_over_symbols
277 (const struct block
*block
, const lookup_name_info
&name
,
279 gdb::function_view
<symbol_found_callback_ftype
> callback
) const
281 return ::iterate_over_symbols (block
, name
, domain
, callback
);
284 /* Return a pointer to the function that should be used to match a
285 symbol name against LOOKUP_NAME, according to this language's
286 rules. The matching algorithm depends on LOOKUP_NAME. For
287 example, on Ada, the matching algorithm depends on the symbol
288 name (wild/full/verbatim matching), and on whether we're doing
289 a normal lookup or a completion match lookup.
291 As Ada wants to capture symbol matching for all languages in some
292 cases, then this method is a non-overridable interface. Languages
293 should override GET_SYMBOL_NAME_MATCHER_INNER if they need to. */
295 symbol_name_matcher_ftype
*get_symbol_name_matcher
296 (const lookup_name_info
&lookup_name
) const;
298 /* If this language allows compilation from the gdb command line, then
299 this method will return an instance of struct gcc_context appropriate
300 to the language. If compilation for this language is generally
301 supported, but something goes wrong then an exception is thrown. The
302 returned compiler instance is owned by its caller and must be
303 deallocated by the caller. If compilation is not supported for this
304 language then this method returns NULL. */
306 virtual compile_instance
*get_compile_instance () const
311 /* This method must be overridden if 'get_compile_instance' is
314 This takes the user-supplied text and returns a new bit of code
317 INST is the compiler instance being used.
318 INPUT is the user's input text.
319 GDBARCH is the architecture to use.
320 EXPR_BLOCK is the block in which the expression is being
322 EXPR_PC is the PC at which the expression is being parsed. */
324 virtual std::string
compute_program (compile_instance
*inst
,
326 struct gdbarch
*gdbarch
,
327 const struct block
*expr_block
,
328 CORE_ADDR expr_pc
) const
330 gdb_assert_not_reached ("language_defn::compute_program");
333 /* Hash the given symbol search name. */
334 virtual unsigned int search_name_hash (const char *name
) const;
336 /* Demangle a symbol according to this language's rules. Unlike
337 la_demangle, this does not take any options.
339 *DEMANGLED will be set by this function.
341 If this function returns false, then *DEMANGLED must always be set
344 If this function returns true, the implementation may set this to
345 a xmalloc'd string holding the demangled form. However, it is
346 not required to. The string, if any, is owned by the caller.
348 The resulting string should be of the form that will be
349 installed into a symbol. */
350 virtual bool sniff_from_mangled_name (const char *mangled
,
351 char **demangled
) const
353 *demangled
= nullptr;
357 /* Return demangled language symbol version of MANGLED, or NULL. */
358 virtual char *demangle (const char *mangled
, int options
) const
363 /* Print a type using syntax appropriate for this language. */
365 virtual void print_type (struct type
*, const char *, struct ui_file
*, int,
366 int, const struct type_print_options
*) const = 0;
368 /* PC is possibly an unknown languages trampoline.
369 If that PC falls in a trampoline belonging to this language, return
370 the address of the first pc in the real function, or 0 if it isn't a
371 language tramp for this language. */
372 virtual CORE_ADDR
skip_trampoline (struct frame_info
*fi
, CORE_ADDR pc
) const
374 return (CORE_ADDR
) 0;
377 /* Return class name of a mangled method name or NULL. */
378 virtual char *class_name_from_physname (const char *physname
) const
383 /* The list of characters forming word boundaries. */
384 virtual const char *word_break_characters (void) const
386 return default_word_break_characters ();
389 /* Add to the completion tracker all symbols which are possible
390 completions for TEXT. WORD is the entire command on which the
391 completion is being made. If CODE is TYPE_CODE_UNDEF, then all
392 symbols should be examined; otherwise, only STRUCT_DOMAIN symbols
393 whose type has a code of CODE should be matched. */
395 virtual void collect_symbol_completion_matches
396 (completion_tracker
&tracker
,
397 complete_symbol_mode mode
,
398 symbol_name_match_type name_match_type
,
401 enum type_code code
) const
403 return default_collect_symbol_completion_matches_break_on
404 (tracker
, mode
, name_match_type
, text
, word
, "", code
);
407 /* This is a function that lookup_symbol will call when it gets to
408 the part of symbol lookup where C looks up static and global
409 variables. This default implements the basic C lookup rules. */
411 virtual struct block_symbol lookup_symbol_nonlocal
413 const struct block
*block
,
414 const domain_enum domain
) const;
416 /* Return an expression that can be used for a location
417 watchpoint. TYPE is a pointer type that points to the memory
418 to watch, and ADDR is the address of the watched memory. */
419 virtual gdb::unique_xmalloc_ptr
<char> watch_location_expression
420 (struct type
*type
, CORE_ADDR addr
) const;
422 /* List of all known languages. */
423 static const struct language_defn
*languages
[nr_languages
];
425 /* Print a top-level value using syntax appropriate for this language. */
426 virtual void value_print (struct value
*val
, struct ui_file
*stream
,
427 const struct value_print_options
*options
) const;
429 /* Print a value using syntax appropriate for this language. RECURSE is
430 the recursion depth. It is zero-based. */
431 virtual void value_print_inner
432 (struct value
*val
, struct ui_file
*stream
, int recurse
,
433 const struct value_print_options
*options
) const;
435 /* Parser function. */
437 virtual int parser (struct parser_state
*ps
) const;
439 /* Given an expression *EXPP created by prefixifying the result of
440 la_parser, perform any remaining processing necessary to complete its
441 translation. *EXPP may change; la_post_parser is responsible for
442 releasing its previous contents, if necessary. If VOID_CONTEXT_P,
443 then no value is expected from the expression. If COMPLETING is
444 non-zero, then the expression has been parsed for completion, not
447 virtual void post_parser (expression_up
*expp
, int void_context_p
,
449 innermost_block_tracker
*tracker
) const
451 /* By default the post-parser does nothing. */
454 /* Print the character CH (of type CHTYPE) on STREAM as part of the
455 contents of a literal string whose delimiter is QUOTER. */
457 virtual void emitchar (int ch
, struct type
*chtype
,
458 struct ui_file
*stream
, int quoter
) const;
460 virtual void printchar (int ch
, struct type
*chtype
,
461 struct ui_file
* stream
) const;
463 /* Print the character string STRING, printing at most LENGTH characters.
464 Printing stops early if the number hits print_max; repeat counts
465 are printed as appropriate. Print ellipses at the end if we
466 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */
468 virtual void printstr (struct ui_file
*stream
, struct type
*elttype
,
469 const gdb_byte
*string
, unsigned int length
,
470 const char *encoding
, int force_ellipses
,
471 const struct value_print_options
*options
) const;
474 /* Print a typedef using syntax appropriate for this language.
475 TYPE is the underlying type. NEW_SYMBOL is the symbol naming
476 the type. STREAM is the output stream on which to print. */
478 virtual void print_typedef (struct type
*type
, struct symbol
*new_symbol
,
479 struct ui_file
*stream
) const;
481 /* Return true if TYPE is a string type. */
482 virtual bool is_string_type_p (struct type
*type
) const;
484 /* Return a string that is used by the 'set print max-depth' setting.
485 When GDB replaces a struct or union (during value printing) that is
486 "too deep" this string is displayed instead. The default value here
487 suits most languages. If overriding then the string here should
488 ideally be similar in style to the default; an opener, three '.', and
491 virtual const char *struct_too_deep_ellipsis () const
494 /* If this returns non-NULL then the string returned specifies the name
495 of the implicit local variable that refers to the current object
496 instance. Return NULL (the default) for languages that have no name
497 for the current object instance. */
499 virtual const char *name_of_this () const
502 /* Return false if the language has first-class arrays. Return true if
503 there are no array values, and array objects decay to pointers, as in
504 C. The default is true as currently most supported languages behave
507 virtual bool c_style_arrays_p () const
510 /* Return the index to use for extracting the first element of a string,
511 or as the lower bound when creating a new string. The default of
512 choosing 0 or 1 based on C_STYLE_ARRAYS_P works for all currently
513 supported languages except Modula-2. */
515 virtual char string_lower_bound () const
516 { return c_style_arrays_p () ? 0 : 1; }
518 /* Returns true if the symbols names should be stored in GDB's data
519 structures for minimal/partial/full symbols using their linkage (aka
520 mangled) form; false if the symbol names should be demangled first.
522 Most languages implement symbol lookup by comparing the demangled
523 names, in which case it is advantageous to store that information
524 already demangled, and so would return false, which is the default.
526 On the other hand, some languages have opted for doing symbol lookups
527 by comparing mangled names instead, for reasons usually specific to
528 the language. Those languages should override this function and
531 And finally, other languages such as C or Asm do not have the concept
532 of mangled vs demangled name, so those languages should also override
533 this function and return true, to prevent any accidental demangling
534 through an unrelated language's demangler. */
536 virtual bool store_sym_names_in_linkage_form_p () const
539 /* Default range checking preference. The return value from this
540 function provides the automatic setting for 'set check range'. As a
541 consequence a user is free to override this setting if they want. */
543 virtual bool range_checking_on_by_default () const
546 /* Is this language case sensitive? The return value from this function
547 provides the automativ setting for 'set case-sensitive', as a
548 consequence, a user is free to override this setting if they want. */
550 virtual enum case_sensitivity
case_sensitivity () const
551 { return case_sensitive_on
; }
554 /* Multi-dimensional array ordering. */
556 virtual enum array_ordering
array_ordering () const
557 { return array_row_major
; }
559 /* Style of macro expansion, if any, supported by this language. The
560 default is no macro expansion. */
562 virtual enum macro_expansion
macro_expansion () const
563 { return macro_expansion_no
; }
565 /* Return a structure containing various operations on varobj specific
566 for this language. */
568 virtual const struct lang_varobj_ops
*varobj_ops () const;
572 /* This is the overridable part of the GET_SYMBOL_NAME_MATCHER method.
573 See that method for a description of the arguments. */
575 virtual symbol_name_matcher_ftype
*get_symbol_name_matcher_inner
576 (const lookup_name_info
&lookup_name
) const;
579 /* Pointer to the language_defn for our current language. This pointer
580 always points to *some* valid struct; it can be used without checking
583 The current language affects expression parsing and evaluation
584 (FIXME: it might be cleaner to make the evaluation-related stuff
585 separate exp_opcodes for each different set of semantics. We
586 should at least think this through more clearly with respect to
587 what happens if the language is changed between parsing and
588 evaluation) and printing of things like types and arrays. It does
589 *not* affect symbol-reading-- each source file in a symbol-file has
590 its own language and we should keep track of that regardless of the
591 language when symbols are read. If we want some manual setting for
592 the language of symbol files (e.g. detecting when ".c" files are
593 C++), it should be a separate setting from the current_language. */
595 extern const struct language_defn
*current_language
;
597 /* Pointer to the language_defn expected by the user, e.g. the language
598 of main(), or the language we last mentioned in a message, or C. */
600 extern const struct language_defn
*expected_language
;
602 /* Warning issued when current_language and the language of the current
603 frame do not match. */
605 extern const char lang_frame_mismatch_warn
[];
608 language_mode_auto: current_language automatically set upon selection
609 of scope (e.g. stack frame)
610 language_mode_manual: current_language set only by user. */
612 extern enum language_mode
614 language_mode_auto
, language_mode_manual
618 struct type
*language_bool_type (const struct language_defn
*l
,
619 struct gdbarch
*gdbarch
);
621 struct type
*language_string_char_type (const struct language_defn
*l
,
622 struct gdbarch
*gdbarch
);
624 /* Look up type NAME in language L, and return its definition for architecture
625 GDBARCH. Returns NULL if not found. */
627 struct type
*language_lookup_primitive_type (const struct language_defn
*l
,
628 struct gdbarch
*gdbarch
,
631 /* Wrapper around language_lookup_primitive_type to return the
632 corresponding symbol. */
635 language_lookup_primitive_type_as_symbol (const struct language_defn
*l
,
636 struct gdbarch
*gdbarch
,
640 /* These macros define the behaviour of the expression
643 /* Should we range check values against the domain of their type? */
644 #define RANGE_CHECK (range_check != range_check_off)
646 /* "cast" really means conversion. */
647 /* FIXME -- should be a setting in language_defn. */
648 #define CAST_IS_CONVERSION(LANG) ((LANG)->la_language == language_c || \
649 (LANG)->la_language == language_cplus || \
650 (LANG)->la_language == language_objc)
652 extern void language_info (int);
654 extern enum language
set_language (enum language
);
657 /* This page contains functions that return things that are
658 specific to languages. Each of these functions is based on
659 the current setting of working_lang, which the user sets
660 with the "set language" command. */
662 #define LA_PRINT_TYPE(type,varstring,stream,show,level,flags) \
663 (current_language->print_type(type,varstring,stream,show,level,flags))
665 #define LA_PRINT_TYPEDEF(type,new_symbol,stream) \
666 (current_language->print_typedef (type,new_symbol,stream))
668 #define LA_VALUE_PRINT(val,stream,options) \
669 (current_language->value_print (val,stream,options))
671 #define LA_PRINT_CHAR(ch, type, stream) \
672 (current_language->printchar (ch, type, stream))
673 #define LA_PRINT_STRING(stream, elttype, string, length, encoding, force_ellipses, options) \
674 (current_language->printstr (stream, elttype, string, length, \
675 encoding, force_ellipses,options))
676 #define LA_EMIT_CHAR(ch, type, stream, quoter) \
677 (current_language->emitchar (ch, type, stream, quoter))
679 #define LA_PRINT_ARRAY_INDEX(index_type, index_value, stream, options) \
680 (current_language->print_array_index(index_type, index_value, stream, \
683 #define LA_ITERATE_OVER_SYMBOLS(BLOCK, NAME, DOMAIN, CALLBACK) \
684 (current_language->iterate_over_symbols (BLOCK, NAME, DOMAIN, CALLBACK))
686 /* Test a character to decide whether it can be printed in literal form
687 or needs to be printed in another representation. For example,
688 in C the literal form of the character with octal value 141 is 'a'
689 and the "other representation" is '\141'. The "other representation"
690 is program language dependent. */
692 #define PRINT_LITERAL_FORM(c) \
694 && ((c) < 0x7F || (c) >= 0xA0) \
695 && (!sevenbit_strings || (c) < 0x80))
697 /* Type predicates */
699 extern int pointer_type (struct type
*);
703 extern void range_error (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
705 /* Data: Does this value represent "truth" to the current language? */
707 extern int value_true (struct value
*);
709 /* Misc: The string representing a particular enum language. */
711 extern enum language
language_enum (const char *str
);
713 extern const struct language_defn
*language_def (enum language
);
715 extern const char *language_str (enum language
);
717 /* Check for a language-specific trampoline. */
719 extern CORE_ADDR
skip_language_trampoline (struct frame_info
*, CORE_ADDR pc
);
721 /* Return demangled language symbol, or NULL. */
722 extern char *language_demangle (const struct language_defn
*current_language
,
723 const char *mangled
, int options
);
725 /* Return information about whether TYPE should be passed
726 (and returned) by reference at the language level. */
727 struct language_pass_by_ref_info
language_pass_by_reference (struct type
*type
);
729 void c_get_string (struct value
*value
,
730 gdb::unique_xmalloc_ptr
<gdb_byte
> *buffer
,
731 int *length
, struct type
**char_type
,
732 const char **charset
);
734 /* Get LANG's symbol_name_matcher method for LOOKUP_NAME. Returns
735 default_symbol_name_matcher if not set. LANG is used as a hint;
736 the function may ignore it depending on the current language and
737 LOOKUP_NAME. Specifically, if the current language is Ada, this
738 may return an Ada matcher regardless of LANG. */
739 symbol_name_matcher_ftype
*get_symbol_name_matcher
740 (const language_defn
*lang
, const lookup_name_info
&lookup_name
);
742 /* Save the current language and restore it upon destruction. */
744 class scoped_restore_current_language
748 explicit scoped_restore_current_language ()
749 : m_lang (current_language
->la_language
)
753 ~scoped_restore_current_language ()
755 set_language (m_lang
);
758 scoped_restore_current_language (const scoped_restore_current_language
&)
760 scoped_restore_current_language
&operator=
761 (const scoped_restore_current_language
&) = delete;
765 enum language m_lang
;
768 /* If language_mode is language_mode_auto,
769 then switch current language to the language of SYM
770 and restore current language upon destruction.
774 class scoped_switch_to_sym_language_if_auto
778 explicit scoped_switch_to_sym_language_if_auto (const struct symbol
*sym
)
780 if (language_mode
== language_mode_auto
)
782 m_lang
= current_language
->la_language
;
784 set_language (sym
->language ());
789 /* Assign to m_lang to silence a GCC warning. See
790 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635. */
791 m_lang
= language_unknown
;
795 ~scoped_switch_to_sym_language_if_auto ()
798 set_language (m_lang
);
801 DISABLE_COPY_AND_ASSIGN (scoped_switch_to_sym_language_if_auto
);
805 enum language m_lang
;
808 #endif /* defined (LANGUAGE_H) */