1 /* Ada language support routines for GDB, the GNU debugger.
3 Copyright (C) 1992-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "event-top.h"
23 #include "exceptions.h"
24 #include "extract-store-integer.h"
25 #include "gdbsupport/gdb_regex.h"
29 #include "cli/cli-cmds.h"
30 #include "expression.h"
31 #include "parser-defs.h"
37 #include "breakpoint.h"
40 #include "gdbsupport/gdb_obstack.h"
42 #include "completer.h"
49 #include "observable.h"
51 #include "typeprint.h"
52 #include "namespace.h"
53 #include "cli/cli-style.h"
54 #include "cli/cli-decode.h"
57 #include "mi/mi-common.h"
58 #include "arch-utils.h"
59 #include "cli/cli-utils.h"
60 #include "gdbsupport/function-view.h"
61 #include "gdbsupport/byte-vector.h"
62 #include "gdbsupport/selftest.h"
68 static struct type
*desc_base_type (struct type
*);
70 static struct type
*desc_bounds_type (struct type
*);
72 static struct value
*desc_bounds (struct value
*);
74 static int fat_pntr_bounds_bitpos (struct type
*);
76 static int fat_pntr_bounds_bitsize (struct type
*);
78 static struct type
*desc_data_target_type (struct type
*);
80 static struct value
*desc_data (struct value
*);
82 static int fat_pntr_data_bitpos (struct type
*);
84 static int fat_pntr_data_bitsize (struct type
*);
86 static struct value
*desc_one_bound (struct value
*, int, int);
88 static int desc_bound_bitpos (struct type
*, int, int);
90 static int desc_bound_bitsize (struct type
*, int, int);
92 static struct type
*desc_index_type (struct type
*, int);
94 static int desc_arity (struct type
*);
96 static int ada_args_match (struct symbol
*, struct value
**, int);
98 static struct value
*make_array_descriptor (struct type
*, struct value
*);
100 static void ada_add_block_symbols (std::vector
<struct block_symbol
> &,
101 const struct block
*,
102 const lookup_name_info
&lookup_name
,
103 domain_search_flags
, struct objfile
*);
105 static void ada_add_all_symbols (std::vector
<struct block_symbol
> &,
106 const struct block
*,
107 const lookup_name_info
&lookup_name
,
108 domain_search_flags
, int, int *);
110 static int is_nonfunction (const std::vector
<struct block_symbol
> &);
112 static void add_defn_to_vec (std::vector
<struct block_symbol
> &,
114 const struct block
*);
116 static int possible_user_operator_p (enum exp_opcode
, struct value
**);
118 static const char *ada_decoded_op_name (enum exp_opcode
);
120 static int numeric_type_p (struct type
*);
122 static int integer_type_p (struct type
*);
124 static int scalar_type_p (struct type
*);
126 static int discrete_type_p (struct type
*);
128 static struct type
*ada_lookup_struct_elt_type (struct type
*, const char *,
131 static struct type
*ada_find_parallel_type_with_name (struct type
*,
134 static int is_dynamic_field (struct type
*, int);
136 static struct type
*to_fixed_variant_branch_type (struct type
*,
138 CORE_ADDR
, struct value
*);
140 static struct type
*to_fixed_array_type (struct type
*, struct value
*, int);
142 static struct type
*to_fixed_range_type (struct type
*, struct value
*);
144 static struct type
*to_static_fixed_type (struct type
*);
145 static struct type
*static_unwrap_type (struct type
*type
);
147 static struct value
*unwrap_value (struct value
*);
149 static struct type
*constrained_packed_array_type (struct type
*, long *);
151 static struct type
*decode_constrained_packed_array_type (struct type
*);
153 static long decode_packed_array_bitsize (struct type
*);
155 static struct value
*decode_constrained_packed_array (struct value
*);
157 static int ada_is_unconstrained_packed_array_type (struct type
*);
159 static struct value
*value_subscript_packed (struct value
*, int,
162 static struct value
*coerce_unspec_val_to_type (struct value
*,
165 static int lesseq_defined_than (struct symbol
*, struct symbol
*);
167 static int equiv_types (struct type
*, struct type
*);
169 static int is_name_suffix (const char *);
171 static int advance_wild_match (const char **, const char *, char);
173 static bool wild_match (const char *name
, const char *patn
);
175 static struct value
*ada_coerce_ref (struct value
*);
177 static LONGEST
pos_atr (struct value
*);
179 static struct value
*val_atr (struct type
*, LONGEST
);
181 static struct value
*ada_search_struct_field (const char *, struct value
*, int,
184 static int find_struct_field (const char *, struct type
*, int,
185 struct type
**, int *, int *, int *, int *);
187 static int ada_resolve_function (std::vector
<struct block_symbol
> &,
188 struct value
**, int, const char *,
189 struct type
*, bool);
191 static int ada_is_direct_array_type (struct type
*);
193 static struct value
*ada_index_struct_field (int, struct value
*, int,
196 static struct type
*ada_find_any_type (const char *name
);
198 static symbol_name_matcher_ftype
*ada_get_symbol_name_matcher
199 (const lookup_name_info
&lookup_name
);
201 static int symbols_are_identical_enums
202 (const std::vector
<struct block_symbol
> &syms
);
204 static bool ada_identical_enum_types_p (struct type
*type1
,
208 /* The character set used for source files. */
209 static const char *ada_source_charset
;
211 /* The string "UTF-8". This is here so we can check for the UTF-8
212 charset using == rather than strcmp. */
213 static const char ada_utf8
[] = "UTF-8";
215 /* Each entry in the UTF-32 case-folding table is of this form. */
218 /* The start and end, inclusive, of this range of codepoints. */
220 /* The delta to apply to get the upper-case form. 0 if this is
221 already upper-case. */
223 /* The delta to apply to get the lower-case form. 0 if this is
224 already lower-case. */
227 bool operator< (uint32_t val
) const
233 static const utf8_entry ada_case_fold
[] =
235 #include "ada-casefold.h"
240 static const char ada_completer_word_break_characters
[] =
242 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
244 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
247 /* The name of the symbol to use to get the name of the main subprogram. */
248 static const char ADA_MAIN_PROGRAM_SYMBOL_NAME
[]
249 = "__gnat_ada_main_program_name";
251 /* Limit on the number of warnings to raise per expression evaluation. */
252 static int warning_limit
= 2;
254 /* Number of warning messages issued; reset to 0 by cleanups after
255 expression evaluation. */
256 static int warnings_issued
= 0;
258 static const char * const known_runtime_file_name_patterns
[] = {
259 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
262 static const char * const known_auxiliary_function_name_patterns
[] = {
263 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
266 /* Maintenance-related settings for this module. */
268 static struct cmd_list_element
*maint_set_ada_cmdlist
;
269 static struct cmd_list_element
*maint_show_ada_cmdlist
;
271 /* The "maintenance ada set/show ignore-descriptive-type" value. */
273 static bool ada_ignore_descriptive_types_p
= false;
275 /* Inferior-specific data. */
277 /* Per-inferior data for this module. */
279 struct ada_inferior_data
281 /* The ada__tags__type_specific_data type, which is used when decoding
282 tagged types. With older versions of GNAT, this type was directly
283 accessible through a component ("tsd") in the object tag. But this
284 is no longer the case, so we cache it for each inferior. */
285 struct type
*tsd_type
= nullptr;
287 /* The exception_support_info data. This data is used to determine
288 how to implement support for Ada exception catchpoints in a given
290 const struct exception_support_info
*exception_info
= nullptr;
293 /* Our key to this module's inferior data. */
294 static const registry
<inferior
>::key
<ada_inferior_data
> ada_inferior_data
;
296 /* Return our inferior data for the given inferior (INF).
298 This function always returns a valid pointer to an allocated
299 ada_inferior_data structure. If INF's inferior data has not
300 been previously set, this functions creates a new one with all
301 fields set to zero, sets INF's inferior to it, and then returns
302 a pointer to that newly allocated ada_inferior_data. */
304 static struct ada_inferior_data
*
305 get_ada_inferior_data (struct inferior
*inf
)
307 struct ada_inferior_data
*data
;
309 data
= ada_inferior_data
.get (inf
);
311 data
= ada_inferior_data
.emplace (inf
);
316 /* Perform all necessary cleanups regarding our module's inferior data
317 that is required after the inferior INF just exited. */
320 ada_inferior_exit (struct inferior
*inf
)
322 ada_inferior_data
.clear (inf
);
326 /* program-space-specific data. */
328 /* The result of a symbol lookup to be stored in our symbol cache. */
332 /* The name used to perform the lookup. */
334 /* The namespace used during the lookup. */
335 domain_search_flags domain
= 0;
336 /* The symbol returned by the lookup, or NULL if no matching symbol
338 struct symbol
*sym
= nullptr;
339 /* The block where the symbol was found, or NULL if no matching
341 const struct block
*block
= nullptr;
344 /* The symbol cache uses this type when searching. */
346 struct cache_entry_search
349 domain_search_flags domain
;
351 hashval_t
hash () const
353 /* This must agree with hash_cache_entry, below. */
354 return htab_hash_string (name
);
358 /* Hash function for cache_entry. */
361 hash_cache_entry (const void *v
)
363 const cache_entry
*entry
= (const cache_entry
*) v
;
364 return htab_hash_string (entry
->name
.c_str ());
367 /* Equality function for cache_entry. */
370 eq_cache_entry (const void *a
, const void *b
)
372 const cache_entry
*entrya
= (const cache_entry
*) a
;
373 const cache_entry_search
*entryb
= (const cache_entry_search
*) b
;
375 return entrya
->domain
== entryb
->domain
&& entrya
->name
== entryb
->name
;
378 /* Key to our per-program-space data. */
379 static const registry
<program_space
>::key
<htab
, htab_deleter
>
380 ada_pspace_data_handle
;
382 /* Return this module's data for the given program space (PSPACE).
383 If not is found, add a zero'ed one now.
385 This function always returns a valid object. */
388 get_ada_pspace_data (struct program_space
*pspace
)
390 htab_t data
= ada_pspace_data_handle
.get (pspace
);
393 data
= htab_create_alloc (10, hash_cache_entry
, eq_cache_entry
,
394 htab_delete_entry
<cache_entry
>,
396 ada_pspace_data_handle
.set (pspace
, data
);
404 /* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
405 all typedef layers have been peeled. Otherwise, return TYPE.
407 Normally, we really expect a typedef type to only have 1 typedef layer.
408 In other words, we really expect the target type of a typedef type to be
409 a non-typedef type. This is particularly true for Ada units, because
410 the language does not have a typedef vs not-typedef distinction.
411 In that respect, the Ada compiler has been trying to eliminate as many
412 typedef definitions in the debugging information, since they generally
413 do not bring any extra information (we still use typedef under certain
414 circumstances related mostly to the GNAT encoding).
416 Unfortunately, we have seen situations where the debugging information
417 generated by the compiler leads to such multiple typedef layers. For
418 instance, consider the following example with stabs:
420 .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
421 .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
423 This is an error in the debugging information which causes type
424 pck__float_array___XUP to be defined twice, and the second time,
425 it is defined as a typedef of a typedef.
427 This is on the fringe of legality as far as debugging information is
428 concerned, and certainly unexpected. But it is easy to handle these
429 situations correctly, so we can afford to be lenient in this case. */
432 ada_typedef_target_type (struct type
*type
)
434 while (type
->code () == TYPE_CODE_TYPEDEF
)
435 type
= type
->target_type ();
439 /* Given DECODED_NAME a string holding a symbol name in its
440 decoded form (ie using the Ada dotted notation), returns
441 its unqualified name. */
444 ada_unqualified_name (const char *decoded_name
)
448 /* If the decoded name starts with '<', it means that the encoded
449 name does not follow standard naming conventions, and thus that
450 it is not your typical Ada symbol name. Trying to unqualify it
451 is therefore pointless and possibly erroneous. */
452 if (decoded_name
[0] == '<')
455 result
= strrchr (decoded_name
, '.');
457 result
++; /* Skip the dot... */
459 result
= decoded_name
;
464 /* Return a string starting with '<', followed by STR, and '>'. */
467 add_angle_brackets (const char *str
)
469 return string_printf ("<%s>", str
);
472 /* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
473 suffix of FIELD_NAME beginning "___". */
476 field_name_match (const char *field_name
, const char *target
)
478 int len
= strlen (target
);
481 (strncmp (field_name
, target
, len
) == 0
482 && (field_name
[len
] == '\0'
483 || (startswith (field_name
+ len
, "___")
484 && strcmp (field_name
+ strlen (field_name
) - 6,
489 /* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
490 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
491 and return its index. This function also handles fields whose name
492 have ___ suffixes because the compiler sometimes alters their name
493 by adding such a suffix to represent fields with certain constraints.
494 If the field could not be found, return a negative number if
495 MAYBE_MISSING is set. Otherwise raise an error. */
498 ada_get_field_index (const struct type
*type
, const char *field_name
,
502 struct type
*struct_type
= check_typedef ((struct type
*) type
);
504 for (fieldno
= 0; fieldno
< struct_type
->num_fields (); fieldno
++)
505 if (field_name_match (struct_type
->field (fieldno
).name (), field_name
))
509 error (_("Unable to find field %s in struct %s. Aborting"),
510 field_name
, struct_type
->name ());
515 /* The length of the prefix of NAME prior to any "___" suffix. */
518 ada_name_prefix_len (const char *name
)
524 const char *p
= strstr (name
, "___");
527 return strlen (name
);
533 /* Return non-zero if SUFFIX is a suffix of STR.
534 Return zero if STR is null. */
537 is_suffix (const char *str
, const char *suffix
)
544 len2
= strlen (suffix
);
545 return (len1
>= len2
&& strcmp (str
+ len1
- len2
, suffix
) == 0);
548 /* The contents of value VAL, treated as a value of type TYPE. The
549 result is an lval in memory if VAL is. */
551 static struct value
*
552 coerce_unspec_val_to_type (struct value
*val
, struct type
*type
)
554 type
= ada_check_typedef (type
);
555 if (val
->type () == type
)
559 struct value
*result
;
561 if (val
->optimized_out ())
562 result
= value::allocate_optimized_out (type
);
563 else if (val
->lazy ()
564 /* Be careful not to make a lazy not_lval value. */
565 || (val
->lval () != not_lval
566 && type
->length () > val
->type ()->length ()))
567 result
= value::allocate_lazy (type
);
570 result
= value::allocate (type
);
571 val
->contents_copy (result
, 0, 0, type
->length ());
573 result
->set_component_location (val
);
574 result
->set_bitsize (val
->bitsize ());
575 result
->set_bitpos (val
->bitpos ());
576 if (result
->lval () == lval_memory
)
577 result
->set_address (val
->address ());
582 static const gdb_byte
*
583 cond_offset_host (const gdb_byte
*valaddr
, long offset
)
588 return valaddr
+ offset
;
592 cond_offset_target (CORE_ADDR address
, long offset
)
597 return address
+ offset
;
600 /* Issue a warning (as for the definition of warning in utils.c, but
601 with exactly one argument rather than ...), unless the limit on the
602 number of warnings has passed during the evaluation of the current
605 /* FIXME: cagney/2004-10-10: This function is mimicking the behavior
606 provided by "complaint". */
607 static void lim_warning (const char *format
, ...) ATTRIBUTE_PRINTF (1, 2);
610 lim_warning (const char *format
, ...)
614 va_start (args
, format
);
615 warnings_issued
+= 1;
616 if (warnings_issued
<= warning_limit
)
617 vwarning (format
, args
);
622 /* Maximum value of a SIZE-byte signed integer type. */
624 max_of_size (int size
)
626 LONGEST top_bit
= (LONGEST
) 1 << (size
* 8 - 2);
628 return top_bit
| (top_bit
- 1);
631 /* Minimum value of a SIZE-byte signed integer type. */
633 min_of_size (int size
)
635 return -max_of_size (size
) - 1;
638 /* Maximum value of a SIZE-byte unsigned integer type. */
640 umax_of_size (int size
)
642 ULONGEST top_bit
= (ULONGEST
) 1 << (size
* 8 - 1);
644 return top_bit
| (top_bit
- 1);
647 /* Maximum value of integral type T, as a signed quantity. */
649 max_of_type (struct type
*t
)
651 if (t
->is_unsigned ())
652 return (LONGEST
) umax_of_size (t
->length ());
654 return max_of_size (t
->length ());
657 /* Minimum value of integral type T, as a signed quantity. */
659 min_of_type (struct type
*t
)
661 if (t
->is_unsigned ())
664 return min_of_size (t
->length ());
667 /* The largest value in the domain of TYPE, a discrete type, as an integer. */
669 ada_discrete_type_high_bound (struct type
*type
)
671 type
= resolve_dynamic_type (type
, {}, 0);
672 switch (type
->code ())
674 case TYPE_CODE_RANGE
:
676 const dynamic_prop
&high
= type
->bounds ()->high
;
678 if (high
.is_constant ())
679 return high
.const_val ();
682 gdb_assert (!high
.is_available ());
684 /* This happens when trying to evaluate a type's dynamic bound
685 without a live target. There is nothing relevant for us to
686 return here, so return 0. */
691 return type
->field (type
->num_fields () - 1).loc_enumval ();
696 return max_of_type (type
);
698 error (_("Unexpected type in ada_discrete_type_high_bound."));
702 /* The smallest value in the domain of TYPE, a discrete type, as an integer. */
704 ada_discrete_type_low_bound (struct type
*type
)
706 type
= resolve_dynamic_type (type
, {}, 0);
707 switch (type
->code ())
709 case TYPE_CODE_RANGE
:
711 const dynamic_prop
&low
= type
->bounds ()->low
;
713 if (low
.is_constant ())
714 return low
.const_val ();
717 gdb_assert (!low
.is_available ());
719 /* This happens when trying to evaluate a type's dynamic bound
720 without a live target. There is nothing relevant for us to
721 return here, so return 0. */
726 return type
->field (0).loc_enumval ();
731 return min_of_type (type
);
733 error (_("Unexpected type in ada_discrete_type_low_bound."));
737 /* The identity on non-range types. For range types, the underlying
738 non-range scalar type. */
741 get_base_type (struct type
*type
)
743 while (type
!= NULL
&& type
->code () == TYPE_CODE_RANGE
)
745 if (type
== type
->target_type () || type
->target_type () == NULL
)
747 type
= type
->target_type ();
752 /* Return a decoded version of the given VALUE. This means returning
753 a value whose type is obtained by applying all the GNAT-specific
754 encodings, making the resulting type a static but standard description
755 of the initial type. */
758 ada_get_decoded_value (struct value
*value
)
760 struct type
*type
= ada_check_typedef (value
->type ());
762 if (ada_is_array_descriptor_type (type
)
763 || (ada_is_constrained_packed_array_type (type
)
764 && type
->code () != TYPE_CODE_PTR
))
766 if (type
->code () == TYPE_CODE_TYPEDEF
) /* array access type. */
767 value
= ada_coerce_to_simple_array_ptr (value
);
769 value
= ada_coerce_to_simple_array (value
);
772 value
= ada_to_fixed_value (value
);
777 /* Same as ada_get_decoded_value, but with the given TYPE.
778 Because there is no associated actual value for this type,
779 the resulting type might be a best-effort approximation in
780 the case of dynamic types. */
783 ada_get_decoded_type (struct type
*type
)
785 type
= to_static_fixed_type (type
);
786 if (ada_is_constrained_packed_array_type (type
))
787 type
= ada_coerce_to_simple_array_type (type
);
793 /* Language Selection */
795 /* If the main procedure is written in Ada, then return its name.
796 The result is good until the next call. Return NULL if the main
797 procedure doesn't appear to be in Ada. */
802 static gdb::unique_xmalloc_ptr
<char> main_program_name
;
804 /* For Ada, the name of the main procedure is stored in a specific
805 string constant, generated by the binder. Look for that symbol,
806 extract its address, and then read that string. If we didn't find
807 that string, then most probably the main procedure is not written
809 bound_minimal_symbol msym
810 = lookup_minimal_symbol (current_program_space
,
811 ADA_MAIN_PROGRAM_SYMBOL_NAME
);
813 if (msym
.minsym
!= NULL
)
815 CORE_ADDR main_program_name_addr
= msym
.value_address ();
816 if (main_program_name_addr
== 0)
817 error (_("Invalid address for Ada main program name."));
819 /* Force trust_readonly, because we always want to fetch this
820 string from the executable, not from inferior memory. If the
821 user changes the exec-file and invokes "start", we want to
822 pick the "main" from the new executable, not one that may
823 come from the still-live inferior. */
824 scoped_restore save_trust_readonly
825 = make_scoped_restore (&trust_readonly
, true);
826 main_program_name
= target_read_string (main_program_name_addr
, 1024);
827 return main_program_name
.get ();
830 /* The main procedure doesn't seem to be in Ada. */
836 /* Table of Ada operators and their GNAT-encoded names. Last entry is pair
839 const struct ada_opname_map ada_opname_table
[] = {
840 {"Oadd", "\"+\"", BINOP_ADD
},
841 {"Osubtract", "\"-\"", BINOP_SUB
},
842 {"Omultiply", "\"*\"", BINOP_MUL
},
843 {"Odivide", "\"/\"", BINOP_DIV
},
844 {"Omod", "\"mod\"", BINOP_MOD
},
845 {"Orem", "\"rem\"", BINOP_REM
},
846 {"Oexpon", "\"**\"", BINOP_EXP
},
847 {"Olt", "\"<\"", BINOP_LESS
},
848 {"Ole", "\"<=\"", BINOP_LEQ
},
849 {"Ogt", "\">\"", BINOP_GTR
},
850 {"Oge", "\">=\"", BINOP_GEQ
},
851 {"Oeq", "\"=\"", BINOP_EQUAL
},
852 {"One", "\"/=\"", BINOP_NOTEQUAL
},
853 {"Oand", "\"and\"", BINOP_BITWISE_AND
},
854 {"Oor", "\"or\"", BINOP_BITWISE_IOR
},
855 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR
},
856 {"Oconcat", "\"&\"", BINOP_CONCAT
},
857 {"Oabs", "\"abs\"", UNOP_ABS
},
858 {"Onot", "\"not\"", UNOP_LOGICAL_NOT
},
859 {"Oadd", "\"+\"", UNOP_PLUS
},
860 {"Osubtract", "\"-\"", UNOP_NEG
},
864 /* If STR is a decoded version of a compiler-provided suffix (like the
865 "[cold]" in "symbol[cold]"), return true. Otherwise, return
869 is_compiler_suffix (const char *str
)
871 gdb_assert (*str
== '[');
873 while (*str
!= '\0' && isalpha (*str
))
875 /* We accept a missing "]" in order to support completion. */
876 return *str
== '\0' || (str
[0] == ']' && str
[1] == '\0');
879 /* Append a non-ASCII character to RESULT. */
881 append_hex_encoded (std::string
&result
, uint32_t one_char
)
883 if (one_char
<= 0xff)
886 result
.append (phex (one_char
, 1));
888 else if (one_char
<= 0xffff)
891 result
.append (phex (one_char
, 2));
895 result
.append ("WW");
896 result
.append (phex (one_char
, 4));
900 /* Return a string that is a copy of the data in STORAGE, with
901 non-ASCII characters replaced by the appropriate hex encoding. A
902 template is used because, for UTF-8, we actually want to work with
903 UTF-32 codepoints. */
906 copy_and_hex_encode (struct obstack
*storage
)
908 const T
*chars
= (T
*) obstack_base (storage
);
909 int num_chars
= obstack_object_size (storage
) / sizeof (T
);
911 for (int i
= 0; i
< num_chars
; ++i
)
913 if (chars
[i
] <= 0x7f)
915 /* The host character set has to be a superset of ASCII, as
916 are all the other character sets we can use. */
917 result
.push_back (chars
[i
]);
920 append_hex_encoded (result
, chars
[i
]);
925 /* The "encoded" form of DECODED, according to GNAT conventions. If
926 THROW_ERRORS, throw an error if invalid operator name is found.
927 Otherwise, return the empty string in that case. */
930 ada_encode_1 (const char *decoded
, bool throw_errors
)
935 std::string encoding_buffer
;
936 bool saw_non_ascii
= false;
937 for (const char *p
= decoded
; *p
!= '\0'; p
+= 1)
939 if ((*p
& 0x80) != 0)
940 saw_non_ascii
= true;
943 encoding_buffer
.append ("__");
944 else if (*p
== '[' && is_compiler_suffix (p
))
946 encoding_buffer
= encoding_buffer
+ "." + (p
+ 1);
947 if (encoding_buffer
.back () == ']')
948 encoding_buffer
.pop_back ();
953 const struct ada_opname_map
*mapping
;
955 for (mapping
= ada_opname_table
;
956 mapping
->encoded
!= NULL
957 && !startswith (p
, mapping
->decoded
); mapping
+= 1)
959 if (mapping
->encoded
== NULL
)
962 error (_("invalid Ada operator name: %s"), p
);
966 encoding_buffer
.append (mapping
->encoded
);
970 encoding_buffer
.push_back (*p
);
973 /* If a non-ASCII character is seen, we must convert it to the
974 appropriate hex form. As this is more expensive, we keep track
975 of whether it is even necessary. */
978 auto_obstack storage
;
979 bool is_utf8
= ada_source_charset
== ada_utf8
;
982 convert_between_encodings
984 is_utf8
? HOST_UTF32
: ada_source_charset
,
985 (const gdb_byte
*) encoding_buffer
.c_str (),
986 encoding_buffer
.length (), 1,
987 &storage
, translit_none
);
989 catch (const gdb_exception
&)
991 static bool warned
= false;
993 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
994 might like to know why. */
998 warning (_("charset conversion failure for '%s'.\n"
999 "You may have the wrong value for 'set ada source-charset'."),
1000 encoding_buffer
.c_str ());
1003 /* We don't try to recover from errors. */
1004 return encoding_buffer
;
1008 return copy_and_hex_encode
<uint32_t> (&storage
);
1009 return copy_and_hex_encode
<gdb_byte
> (&storage
);
1012 return encoding_buffer
;
1015 /* Find the entry for C in the case-folding table. Return nullptr if
1016 the entry does not cover C. */
1017 static const utf8_entry
*
1018 find_case_fold_entry (uint32_t c
)
1020 auto iter
= std::lower_bound (std::begin (ada_case_fold
),
1021 std::end (ada_case_fold
),
1023 if (iter
== std::end (ada_case_fold
)
1030 /* Return NAME folded to lower case, or, if surrounded by single
1031 quotes, unfolded, but with the quotes stripped away. If
1032 THROW_ON_ERROR is true, encoding failures will throw an exception
1033 rather than emitting a warning. Result good to next call. */
1036 ada_fold_name (std::string_view name
, bool throw_on_error
= false)
1038 static std::string fold_storage
;
1040 if (!name
.empty () && name
[0] == '\'')
1041 fold_storage
= name
.substr (1, name
.size () - 2);
1044 /* Why convert to UTF-32 and implement our own case-folding,
1045 rather than convert to wchar_t and use the platform's
1046 functions? I'm glad you asked.
1048 The main problem is that GNAT implements an unusual rule for
1049 case folding. For ASCII letters, letters in single-byte
1050 encodings (such as ISO-8859-*), and Unicode letters that fit
1051 in a single byte (i.e., code point is <= 0xff), the letter is
1052 folded to lower case. Other Unicode letters are folded to
1055 This rule means that the code must be able to examine the
1056 value of the character. And, some hosts do not use Unicode
1057 for wchar_t, so examining the value of such characters is
1059 auto_obstack storage
;
1062 convert_between_encodings
1063 (host_charset (), HOST_UTF32
,
1064 (const gdb_byte
*) name
.data (),
1066 &storage
, translit_none
);
1068 catch (const gdb_exception
&)
1073 static bool warned
= false;
1075 /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
1076 might like to know why. */
1080 warning (_("could not convert '%s' from the host encoding (%s) to UTF-32.\n"
1081 "This normally should not happen, please file a bug report."),
1082 std::string (name
).c_str (), host_charset ());
1085 /* We don't try to recover from errors; just return the
1087 fold_storage
= name
;
1088 return fold_storage
.c_str ();
1091 bool is_utf8
= ada_source_charset
== ada_utf8
;
1092 uint32_t *chars
= (uint32_t *) obstack_base (&storage
);
1093 int num_chars
= obstack_object_size (&storage
) / sizeof (uint32_t);
1094 for (int i
= 0; i
< num_chars
; ++i
)
1096 const struct utf8_entry
*entry
= find_case_fold_entry (chars
[i
]);
1097 if (entry
!= nullptr)
1099 uint32_t low
= chars
[i
] + entry
->lower_delta
;
1100 if (!is_utf8
|| low
<= 0xff)
1103 chars
[i
] = chars
[i
] + entry
->upper_delta
;
1107 /* Now convert back to ordinary characters. */
1108 auto_obstack reconverted
;
1111 convert_between_encodings (HOST_UTF32
,
1113 (const gdb_byte
*) chars
,
1114 num_chars
* sizeof (uint32_t),
1118 obstack_1grow (&reconverted
, '\0');
1119 fold_storage
= std::string ((const char *) obstack_base (&reconverted
));
1121 catch (const gdb_exception
&)
1126 static bool warned
= false;
1128 /* Converting back from UTF-32 shouldn't normally fail, but
1129 there are some host encodings without upper/lower
1134 warning (_("could not convert the lower-cased variant of '%s'\n"
1135 "from UTF-32 to the host encoding (%s)."),
1136 std::string (name
).c_str (), host_charset ());
1139 /* We don't try to recover from errors; just return the
1141 fold_storage
= name
;
1145 return fold_storage
.c_str ();
1148 /* The "encoded" form of DECODED, according to GNAT conventions. If
1149 FOLD is true (the default), case-fold any ordinary symbol. Symbols
1150 with <...> quoting are not folded in any case. */
1153 ada_encode (const char *decoded
, bool fold
)
1155 if (fold
&& decoded
[0] != '<')
1156 decoded
= ada_fold_name (decoded
);
1157 return ada_encode_1 (decoded
, true);
1160 /* Return nonzero if C is either a digit or a lowercase alphabet character. */
1163 is_lower_alphanum (const char c
)
1165 return (isdigit (c
) || (isalpha (c
) && islower (c
)));
1168 /* ENCODED is the linkage name of a symbol and LEN contains its length.
1169 This function saves in LEN the length of that same symbol name but
1170 without either of these suffixes:
1176 These are suffixes introduced by the compiler for entities such as
1177 nested subprogram for instance, in order to avoid name clashes.
1178 They do not serve any purpose for the debugger. */
1181 ada_remove_trailing_digits (const char *encoded
, int *len
)
1183 if (*len
> 1 && isdigit (encoded
[*len
- 1]))
1187 while (i
> 0 && isdigit (encoded
[i
]))
1189 if (i
>= 0 && encoded
[i
] == '.')
1191 else if (i
>= 0 && encoded
[i
] == '$')
1193 else if (i
>= 2 && startswith (encoded
+ i
- 2, "___"))
1195 else if (i
>= 1 && startswith (encoded
+ i
- 1, "__"))
1200 /* Remove the suffix introduced by the compiler for protected object
1204 ada_remove_po_subprogram_suffix (const char *encoded
, int *len
)
1206 /* Remove trailing N. */
1208 /* Protected entry subprograms are broken into two
1209 separate subprograms: The first one is unprotected, and has
1210 a 'N' suffix; the second is the protected version, and has
1211 the 'P' suffix. The second calls the first one after handling
1212 the protection. Since the P subprograms are internally generated,
1213 we leave these names undecoded, giving the user a clue that this
1214 entity is internal. */
1217 && encoded
[*len
- 1] == 'N'
1218 && (isdigit (encoded
[*len
- 2]) || islower (encoded
[*len
- 2])))
1222 /* If ENCODED ends with a compiler-provided suffix (like ".cold"),
1223 then update *LEN to remove the suffix and return the offset of the
1224 character just past the ".". Otherwise, return -1. */
1227 remove_compiler_suffix (const char *encoded
, int *len
)
1229 int offset
= *len
- 1;
1230 while (offset
> 0 && isalpha (encoded
[offset
]))
1232 if (offset
> 0 && encoded
[offset
] == '.')
1240 /* Convert an ASCII hex string to a number. Reads exactly N
1241 characters from STR. Returns true on success, false if one of the
1242 digits was not a hex digit. */
1244 convert_hex (const char *str
, int n
, uint32_t *out
)
1246 uint32_t result
= 0;
1248 for (int i
= 0; i
< n
; ++i
)
1250 if (!isxdigit (str
[i
]))
1253 result
|= fromhex (str
[i
]);
1260 /* Convert a wide character from its ASCII hex representation in STR
1261 (consisting of exactly N characters) to the host encoding,
1262 appending the resulting bytes to OUT. If N==2 and the Ada source
1263 charset is not UTF-8, then hex refers to an encoding in the
1264 ADA_SOURCE_CHARSET; otherwise, use UTF-32. Return true on success.
1265 Return false and do not modify OUT on conversion failure. */
1267 convert_from_hex_encoded (std::string
&out
, const char *str
, int n
)
1271 if (!convert_hex (str
, n
, &value
))
1276 /* In the 'U' case, the hex digits encode the character in the
1277 Ada source charset. However, if the source charset is UTF-8,
1278 this really means it is a single-byte UTF-32 character. */
1279 if (n
== 2 && ada_source_charset
!= ada_utf8
)
1281 gdb_byte one_char
= (gdb_byte
) value
;
1283 convert_between_encodings (ada_source_charset
, host_charset (),
1285 sizeof (one_char
), sizeof (one_char
),
1286 &bytes
, translit_none
);
1289 convert_between_encodings (HOST_UTF32
, host_charset (),
1290 (const gdb_byte
*) &value
,
1291 sizeof (value
), sizeof (value
),
1292 &bytes
, translit_none
);
1293 obstack_1grow (&bytes
, '\0');
1294 out
.append ((const char *) obstack_base (&bytes
));
1296 catch (const gdb_exception
&)
1298 /* On failure, the caller will just let the encoded form
1299 through, which seems basically reasonable. */
1306 /* See ada-lang.h. */
1309 ada_decode (const char *encoded
, bool wrap
, bool operators
, bool wide
)
1315 std::string decoded
;
1318 /* With function descriptors on PPC64, the value of a symbol named
1319 ".FN", if it exists, is the entry point of the function "FN". */
1320 if (encoded
[0] == '.')
1323 /* The name of the Ada main procedure starts with "_ada_".
1324 This prefix is not part of the decoded name, so skip this part
1325 if we see this prefix. */
1326 if (startswith (encoded
, "_ada_"))
1328 /* The "___ghost_" prefix is used for ghost entities. Normally
1329 these aren't preserved but when they are, it's useful to see
1331 if (startswith (encoded
, "___ghost_"))
1334 /* If the name starts with '_', then it is not a properly encoded
1335 name, so do not attempt to decode it. Similarly, if the name
1336 starts with '<', the name should not be decoded. */
1337 if (encoded
[0] == '_' || encoded
[0] == '<')
1340 len0
= strlen (encoded
);
1342 suffix
= remove_compiler_suffix (encoded
, &len0
);
1344 ada_remove_trailing_digits (encoded
, &len0
);
1345 ada_remove_po_subprogram_suffix (encoded
, &len0
);
1347 /* Remove the ___X.* suffix if present. Do not forget to verify that
1348 the suffix is located before the current "end" of ENCODED. We want
1349 to avoid re-matching parts of ENCODED that have previously been
1350 marked as discarded (by decrementing LEN0). */
1351 p
= strstr (encoded
, "___");
1352 if (p
!= NULL
&& p
- encoded
< len0
- 3)
1360 /* Remove any trailing TKB suffix. It tells us that this symbol
1361 is for the body of a task, but that information does not actually
1362 appear in the decoded name. */
1364 if (len0
> 3 && startswith (encoded
+ len0
- 3, "TKB"))
1367 /* Remove any trailing TB suffix. The TB suffix is slightly different
1368 from the TKB suffix because it is used for non-anonymous task
1371 if (len0
> 2 && startswith (encoded
+ len0
- 2, "TB"))
1374 /* Remove trailing "B" suffixes. */
1375 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1377 if (len0
> 1 && startswith (encoded
+ len0
- 1, "B"))
1380 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1382 if (len0
> 1 && isdigit (encoded
[len0
- 1]))
1385 while ((i
>= 0 && isdigit (encoded
[i
]))
1386 || (i
>= 1 && encoded
[i
] == '_' && isdigit (encoded
[i
- 1])))
1388 if (i
> 1 && encoded
[i
] == '_' && encoded
[i
- 1] == '_')
1390 else if (i
>= 0 && encoded
[i
] == '$')
1394 /* The first few characters that are not alphabetic are not part
1395 of any encoding we use, so we can copy them over verbatim. */
1397 for (i
= 0; i
< len0
&& !isalpha (encoded
[i
]); i
+= 1)
1398 decoded
.push_back (encoded
[i
]);
1403 /* Is this a symbol function? */
1404 if (operators
&& at_start_name
&& encoded
[i
] == 'O')
1408 for (k
= 0; ada_opname_table
[k
].encoded
!= NULL
; k
+= 1)
1410 int op_len
= strlen (ada_opname_table
[k
].encoded
);
1411 if ((strncmp (ada_opname_table
[k
].encoded
+ 1, encoded
+ i
+ 1,
1413 && !isalnum (encoded
[i
+ op_len
]))
1415 decoded
.append (ada_opname_table
[k
].decoded
);
1421 if (ada_opname_table
[k
].encoded
!= NULL
)
1426 /* Replace "TK__" with "__", which will eventually be translated
1427 into "." (just below). */
1429 if (i
< len0
- 4 && startswith (encoded
+ i
, "TK__"))
1432 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
1433 be translated into "." (just below). These are internal names
1434 generated for anonymous blocks inside which our symbol is nested. */
1436 if (len0
- i
> 5 && encoded
[i
] == '_' && encoded
[i
+1] == '_'
1437 && encoded
[i
+2] == 'B' && encoded
[i
+3] == '_'
1438 && isdigit (encoded
[i
+4]))
1442 while (k
< len0
&& isdigit (encoded
[k
]))
1443 k
++; /* Skip any extra digit. */
1445 /* Double-check that the "__B_{DIGITS}+" sequence we found
1446 is indeed followed by "__". */
1447 if (len0
- k
> 2 && encoded
[k
] == '_' && encoded
[k
+1] == '_')
1451 /* Remove _E{DIGITS}+[sb] */
1453 /* Just as for protected object subprograms, there are 2 categories
1454 of subprograms created by the compiler for each entry. The first
1455 one implements the actual entry code, and has a suffix following
1456 the convention above; the second one implements the barrier and
1457 uses the same convention as above, except that the 'E' is replaced
1460 Just as above, we do not decode the name of barrier functions
1461 to give the user a clue that the code he is debugging has been
1462 internally generated. */
1464 if (len0
- i
> 3 && encoded
[i
] == '_' && encoded
[i
+1] == 'E'
1465 && isdigit (encoded
[i
+2]))
1469 while (k
< len0
&& isdigit (encoded
[k
]))
1473 && (encoded
[k
] == 'b' || encoded
[k
] == 's'))
1476 /* Just as an extra precaution, make sure that if this
1477 suffix is followed by anything else, it is a '_'.
1478 Otherwise, we matched this sequence by accident. */
1480 || (k
< len0
&& encoded
[k
] == '_'))
1485 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
1486 the GNAT front-end in protected object subprograms. */
1489 && encoded
[i
] == 'N' && encoded
[i
+1] == '_' && encoded
[i
+2] == '_')
1491 /* Backtrack a bit up until we reach either the beginning of
1492 the encoded name, or "__". Make sure that we only find
1493 digits or lowercase characters. */
1494 const char *ptr
= encoded
+ i
- 1;
1496 while (ptr
>= encoded
&& is_lower_alphanum (ptr
[0]))
1499 || (ptr
> encoded
&& ptr
[0] == '_' && ptr
[-1] == '_'))
1503 if (wide
&& i
< len0
+ 3 && encoded
[i
] == 'U' && isxdigit (encoded
[i
+ 1]))
1505 if (convert_from_hex_encoded (decoded
, &encoded
[i
+ 1], 2))
1511 else if (wide
&& i
< len0
+ 5 && encoded
[i
] == 'W' && isxdigit (encoded
[i
+ 1]))
1513 if (convert_from_hex_encoded (decoded
, &encoded
[i
+ 1], 4))
1519 else if (wide
&& i
< len0
+ 10 && encoded
[i
] == 'W' && encoded
[i
+ 1] == 'W'
1520 && isxdigit (encoded
[i
+ 2]))
1522 if (convert_from_hex_encoded (decoded
, &encoded
[i
+ 2], 8))
1529 if (encoded
[i
] == 'X' && i
!= 0 && isalnum (encoded
[i
- 1]))
1531 /* This is a X[bn]* sequence not separated from the previous
1532 part of the name with a non-alpha-numeric character (in other
1533 words, immediately following an alpha-numeric character), then
1534 verify that it is placed at the end of the encoded name. If
1535 not, then the encoding is not valid and we should abort the
1536 decoding. Otherwise, just skip it, it is used in body-nested
1540 while (i
< len0
&& (encoded
[i
] == 'b' || encoded
[i
] == 'n'));
1544 else if (i
< len0
- 2 && encoded
[i
] == '_' && encoded
[i
+ 1] == '_')
1546 /* Replace '__' by '.'. */
1547 decoded
.push_back ('.');
1553 /* It's a character part of the decoded name, so just copy it
1555 decoded
.push_back (encoded
[i
]);
1560 /* Decoded names should never contain any uppercase character.
1561 Double-check this, and abort the decoding if we find one. */
1565 for (i
= 0; i
< decoded
.length(); ++i
)
1566 if (isupper (decoded
[i
]) || decoded
[i
] == ' ')
1570 /* If the compiler added a suffix, append it now. */
1572 decoded
= decoded
+ "[" + &encoded
[suffix
] + "]";
1580 if (encoded
[0] == '<')
1583 decoded
= '<' + std::string(encoded
) + '>';
1587 #ifdef GDB_SELF_TEST
1592 /* This isn't valid, but used to cause a crash. PR gdb/30639. The
1593 result does not really matter very much. */
1594 SELF_CHECK (ada_decode ("44") == "44");
1599 /* Table for keeping permanent unique copies of decoded names. Once
1600 allocated, names in this table are never released. While this is a
1601 storage leak, it should not be significant unless there are massive
1602 changes in the set of decoded names in successive versions of a
1603 symbol table loaded during a single session. */
1604 static struct htab
*decoded_names_store
;
1606 /* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1607 in the language-specific part of GSYMBOL, if it has not been
1608 previously computed. Tries to save the decoded name in the same
1609 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1610 in any case, the decoded symbol has a lifetime at least that of
1612 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1613 const, but nevertheless modified to a semantically equivalent form
1614 when a decoded name is cached in it. */
1617 ada_decode_symbol (const struct general_symbol_info
*arg
)
1619 struct general_symbol_info
*gsymbol
= (struct general_symbol_info
*) arg
;
1620 const char **resultp
=
1621 &gsymbol
->language_specific
.demangled_name
;
1623 if (!gsymbol
->ada_mangled
)
1625 std::string decoded
= ada_decode (gsymbol
->linkage_name ());
1626 struct obstack
*obstack
= gsymbol
->language_specific
.obstack
;
1628 gsymbol
->ada_mangled
= 1;
1630 if (obstack
!= NULL
)
1631 *resultp
= obstack_strdup (obstack
, decoded
.c_str ());
1634 /* Sometimes, we can't find a corresponding objfile, in
1635 which case, we put the result on the heap. Since we only
1636 decode when needed, we hope this usually does not cause a
1637 significant memory leak (FIXME). */
1639 char **slot
= (char **) htab_find_slot (decoded_names_store
,
1640 decoded
.c_str (), INSERT
);
1643 *slot
= xstrdup (decoded
.c_str ());
1655 /* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1656 generated by the GNAT compiler to describe the index type used
1657 for each dimension of an array, check whether it follows the latest
1658 known encoding. If not, fix it up to conform to the latest encoding.
1659 Otherwise, do nothing. This function also does nothing if
1660 INDEX_DESC_TYPE is NULL.
1662 The GNAT encoding used to describe the array index type evolved a bit.
1663 Initially, the information would be provided through the name of each
1664 field of the structure type only, while the type of these fields was
1665 described as unspecified and irrelevant. The debugger was then expected
1666 to perform a global type lookup using the name of that field in order
1667 to get access to the full index type description. Because these global
1668 lookups can be very expensive, the encoding was later enhanced to make
1669 the global lookup unnecessary by defining the field type as being
1670 the full index type description.
1672 The purpose of this routine is to allow us to support older versions
1673 of the compiler by detecting the use of the older encoding, and by
1674 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1675 we essentially replace each field's meaningless type by the associated
1679 ada_fixup_array_indexes_type (struct type
*index_desc_type
)
1683 if (index_desc_type
== NULL
)
1685 gdb_assert (index_desc_type
->num_fields () > 0);
1687 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1688 to check one field only, no need to check them all). If not, return
1691 If our INDEX_DESC_TYPE was generated using the older encoding,
1692 the field type should be a meaningless integer type whose name
1693 is not equal to the field name. */
1694 if (index_desc_type
->field (0).type ()->name () != NULL
1695 && strcmp (index_desc_type
->field (0).type ()->name (),
1696 index_desc_type
->field (0).name ()) == 0)
1699 /* Fixup each field of INDEX_DESC_TYPE. */
1700 for (i
= 0; i
< index_desc_type
->num_fields (); i
++)
1702 const char *name
= index_desc_type
->field (i
).name ();
1703 struct type
*raw_type
= ada_check_typedef (ada_find_any_type (name
));
1706 index_desc_type
->field (i
).set_type (raw_type
);
1710 /* The desc_* routines return primitive portions of array descriptors
1713 /* The descriptor or array type, if any, indicated by TYPE; removes
1714 level of indirection, if needed. */
1716 static struct type
*
1717 desc_base_type (struct type
*type
)
1721 type
= ada_check_typedef (type
);
1722 if (type
->code () == TYPE_CODE_TYPEDEF
)
1723 type
= ada_typedef_target_type (type
);
1726 && (type
->code () == TYPE_CODE_PTR
1727 || type
->code () == TYPE_CODE_REF
))
1728 return ada_check_typedef (type
->target_type ());
1733 /* True iff TYPE indicates a "thin" array pointer type. */
1736 is_thin_pntr (struct type
*type
)
1739 is_suffix (ada_type_name (desc_base_type (type
)), "___XUT")
1740 || is_suffix (ada_type_name (desc_base_type (type
)), "___XUT___XVE");
1743 /* The descriptor type for thin pointer type TYPE. */
1745 static struct type
*
1746 thin_descriptor_type (struct type
*type
)
1748 struct type
*base_type
= desc_base_type (type
);
1750 if (base_type
== NULL
)
1752 if (is_suffix (ada_type_name (base_type
), "___XVE"))
1756 struct type
*alt_type
= ada_find_parallel_type (base_type
, "___XVE");
1758 if (alt_type
== NULL
)
1765 /* A pointer to the array data for thin-pointer value VAL. */
1767 static struct value
*
1768 thin_data_pntr (struct value
*val
)
1770 struct type
*type
= ada_check_typedef (val
->type ());
1771 struct type
*data_type
= desc_data_target_type (thin_descriptor_type (type
));
1773 data_type
= lookup_pointer_type (data_type
);
1775 if (type
->code () == TYPE_CODE_PTR
)
1776 return value_cast (data_type
, val
->copy ());
1778 return value_from_longest (data_type
, val
->address ());
1781 /* True iff TYPE indicates a "thick" array pointer type. */
1784 is_thick_pntr (struct type
*type
)
1786 type
= desc_base_type (type
);
1787 return (type
!= NULL
&& type
->code () == TYPE_CODE_STRUCT
1788 && lookup_struct_elt_type (type
, "P_BOUNDS", 1) != NULL
);
1791 /* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1792 pointer to one, the type of its bounds data; otherwise, NULL. */
1794 static struct type
*
1795 desc_bounds_type (struct type
*type
)
1799 type
= desc_base_type (type
);
1803 else if (is_thin_pntr (type
))
1805 type
= thin_descriptor_type (type
);
1808 r
= lookup_struct_elt_type (type
, "BOUNDS", 1);
1810 return ada_check_typedef (r
);
1812 else if (type
->code () == TYPE_CODE_STRUCT
)
1814 r
= lookup_struct_elt_type (type
, "P_BOUNDS", 1);
1816 return ada_check_typedef (ada_check_typedef (r
)->target_type ());
1821 /* If ARR is an array descriptor (fat or thin pointer), or pointer to
1822 one, a pointer to its bounds data. Otherwise NULL. */
1824 static struct value
*
1825 desc_bounds (struct value
*arr
)
1827 struct type
*type
= ada_check_typedef (arr
->type ());
1829 if (is_thin_pntr (type
))
1831 struct type
*bounds_type
=
1832 desc_bounds_type (thin_descriptor_type (type
));
1835 if (bounds_type
== NULL
)
1836 error (_("Bad GNAT array descriptor"));
1838 /* NOTE: The following calculation is not really kosher, but
1839 since desc_type is an XVE-encoded type (and shouldn't be),
1840 the correct calculation is a real pain. FIXME (and fix GCC). */
1841 if (type
->code () == TYPE_CODE_PTR
)
1842 addr
= value_as_long (arr
);
1844 addr
= arr
->address ();
1847 value_from_longest (lookup_pointer_type (bounds_type
),
1848 addr
- bounds_type
->length ());
1851 else if (is_thick_pntr (type
))
1853 struct value
*p_bounds
= value_struct_elt (&arr
, {}, "P_BOUNDS", NULL
,
1854 _("Bad GNAT array descriptor"));
1855 struct type
*p_bounds_type
= p_bounds
->type ();
1858 && p_bounds_type
->code () == TYPE_CODE_PTR
)
1860 struct type
*target_type
= p_bounds_type
->target_type ();
1862 if (target_type
->is_stub ())
1863 p_bounds
= value_cast (lookup_pointer_type
1864 (ada_check_typedef (target_type
)),
1868 error (_("Bad GNAT array descriptor"));
1876 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1877 position of the field containing the address of the bounds data. */
1880 fat_pntr_bounds_bitpos (struct type
*type
)
1882 return desc_base_type (type
)->field (1).loc_bitpos ();
1885 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1886 size of the field containing the address of the bounds data. */
1889 fat_pntr_bounds_bitsize (struct type
*type
)
1891 type
= desc_base_type (type
);
1893 if (type
->field (1).bitsize () > 0)
1894 return type
->field (1).bitsize ();
1896 return 8 * ada_check_typedef (type
->field (1).type ())->length ();
1899 /* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1900 pointer to one, the type of its array data (a array-with-no-bounds type);
1901 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1904 static struct type
*
1905 desc_data_target_type (struct type
*type
)
1907 type
= desc_base_type (type
);
1909 /* NOTE: The following is bogus; see comment in desc_bounds. */
1910 if (is_thin_pntr (type
))
1911 return desc_base_type (thin_descriptor_type (type
)->field (1).type ());
1912 else if (is_thick_pntr (type
))
1914 struct type
*data_type
= lookup_struct_elt_type (type
, "P_ARRAY", 1);
1917 && ada_check_typedef (data_type
)->code () == TYPE_CODE_PTR
)
1918 return ada_check_typedef (data_type
->target_type ());
1924 /* If ARR is an array descriptor (fat or thin pointer), a pointer to
1927 static struct value
*
1928 desc_data (struct value
*arr
)
1930 struct type
*type
= arr
->type ();
1932 if (is_thin_pntr (type
))
1933 return thin_data_pntr (arr
);
1934 else if (is_thick_pntr (type
))
1935 return value_struct_elt (&arr
, {}, "P_ARRAY", NULL
,
1936 _("Bad GNAT array descriptor"));
1942 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1943 position of the field containing the address of the data. */
1946 fat_pntr_data_bitpos (struct type
*type
)
1948 return desc_base_type (type
)->field (0).loc_bitpos ();
1951 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1952 size of the field containing the address of the data. */
1955 fat_pntr_data_bitsize (struct type
*type
)
1957 type
= desc_base_type (type
);
1959 if (type
->field (0).bitsize () > 0)
1960 return type
->field (0).bitsize ();
1962 return TARGET_CHAR_BIT
* type
->field (0).type ()->length ();
1965 /* If BOUNDS is an array-bounds structure (or pointer to one), return
1966 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
1967 bound, if WHICH is 1. The first bound is I=1. */
1969 static struct value
*
1970 desc_one_bound (struct value
*bounds
, int i
, int which
)
1972 char bound_name
[20];
1973 xsnprintf (bound_name
, sizeof (bound_name
), "%cB%d",
1974 which
? 'U' : 'L', i
- 1);
1975 return value_struct_elt (&bounds
, {}, bound_name
, NULL
,
1976 _("Bad GNAT array descriptor bounds"));
1979 /* If BOUNDS is an array-bounds structure type, return the bit position
1980 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
1981 bound, if WHICH is 1. The first bound is I=1. */
1984 desc_bound_bitpos (struct type
*type
, int i
, int which
)
1986 return desc_base_type (type
)->field (2 * i
+ which
- 2).loc_bitpos ();
1989 /* If BOUNDS is an array-bounds structure type, return the bit field size
1990 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
1991 bound, if WHICH is 1. The first bound is I=1. */
1994 desc_bound_bitsize (struct type
*type
, int i
, int which
)
1996 type
= desc_base_type (type
);
1998 if (type
->field (2 * i
+ which
- 2).bitsize () > 0)
1999 return type
->field (2 * i
+ which
- 2).bitsize ();
2001 return 8 * type
->field (2 * i
+ which
- 2).type ()->length ();
2004 /* If TYPE is the type of an array-bounds structure, the type of its
2005 Ith bound (numbering from 1). Otherwise, NULL. */
2007 static struct type
*
2008 desc_index_type (struct type
*type
, int i
)
2010 type
= desc_base_type (type
);
2012 if (type
->code () == TYPE_CODE_STRUCT
)
2014 char bound_name
[20];
2015 xsnprintf (bound_name
, sizeof (bound_name
), "LB%d", i
- 1);
2016 return lookup_struct_elt_type (type
, bound_name
, 1);
2022 /* The number of index positions in the array-bounds type TYPE.
2023 Return 0 if TYPE is NULL. */
2026 desc_arity (struct type
*type
)
2028 type
= desc_base_type (type
);
2031 return type
->num_fields () / 2;
2035 /* Non-zero iff TYPE is a simple array type (not a pointer to one) or
2036 an array descriptor type (representing an unconstrained array
2040 ada_is_direct_array_type (struct type
*type
)
2044 type
= ada_check_typedef (type
);
2045 return (type
->code () == TYPE_CODE_ARRAY
2046 || ada_is_array_descriptor_type (type
));
2049 /* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
2053 ada_is_array_type (struct type
*type
)
2056 && (type
->code () == TYPE_CODE_PTR
2057 || type
->code () == TYPE_CODE_REF
))
2058 type
= type
->target_type ();
2059 return ada_is_direct_array_type (type
);
2062 /* Non-zero iff TYPE is a simple array type or pointer to one. */
2065 ada_is_simple_array_type (struct type
*type
)
2069 type
= ada_check_typedef (type
);
2070 return (type
->code () == TYPE_CODE_ARRAY
2071 || (type
->code () == TYPE_CODE_PTR
2072 && (ada_check_typedef (type
->target_type ())->code ()
2073 == TYPE_CODE_ARRAY
)));
2076 /* Non-zero iff TYPE belongs to a GNAT array descriptor. */
2079 ada_is_array_descriptor_type (struct type
*type
)
2081 struct type
*data_type
= desc_data_target_type (type
);
2085 type
= ada_check_typedef (type
);
2086 return (data_type
!= NULL
2087 && data_type
->code () == TYPE_CODE_ARRAY
2088 && desc_arity (desc_bounds_type (type
)) > 0);
2091 /* If ARR has a record type in the form of a standard GNAT array descriptor,
2092 (fat pointer) returns the type of the array data described---specifically,
2093 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
2094 in from the descriptor; otherwise, they are left unspecified. If
2095 the ARR denotes a null array descriptor and BOUNDS is non-zero,
2096 returns NULL. The result is simply the type of ARR if ARR is not
2099 static struct type
*
2100 ada_type_of_array (struct value
*arr
, int bounds
)
2102 if (ada_is_constrained_packed_array_type (arr
->type ()))
2103 return decode_constrained_packed_array_type (arr
->type ());
2105 if (!ada_is_array_descriptor_type (arr
->type ()))
2106 return arr
->type ();
2110 struct type
*array_type
=
2111 ada_check_typedef (desc_data_target_type (arr
->type ()));
2113 if (ada_is_unconstrained_packed_array_type (arr
->type ()))
2114 array_type
->field (0).set_bitsize
2115 (decode_packed_array_bitsize (arr
->type ()));
2121 struct type
*elt_type
;
2123 struct value
*descriptor
;
2125 elt_type
= ada_array_element_type (arr
->type (), -1);
2126 arity
= ada_array_arity (arr
->type ());
2128 if (elt_type
== NULL
|| arity
== 0)
2129 return ada_check_typedef (arr
->type ());
2131 descriptor
= desc_bounds (arr
);
2132 if (value_as_long (descriptor
) == 0)
2136 type_allocator
alloc (arr
->type ());
2137 struct value
*low
= desc_one_bound (descriptor
, arity
, 0);
2138 struct value
*high
= desc_one_bound (descriptor
, arity
, 1);
2141 struct type
*range_type
2142 = create_static_range_type (alloc
, low
->type (),
2143 longest_to_int (value_as_long (low
)),
2144 longest_to_int (value_as_long (high
)));
2145 elt_type
= create_array_type (alloc
, elt_type
, range_type
);
2146 INIT_GNAT_SPECIFIC (elt_type
);
2148 if (ada_is_unconstrained_packed_array_type (arr
->type ()))
2150 /* We need to store the element packed bitsize, as well as
2151 recompute the array size, because it was previously
2152 computed based on the unpacked element size. */
2153 LONGEST lo
= value_as_long (low
);
2154 LONGEST hi
= value_as_long (high
);
2156 elt_type
->field (0).set_bitsize
2157 (decode_packed_array_bitsize (arr
->type ()));
2159 /* If the array has no element, then the size is already
2160 zero, and does not need to be recomputed. */
2164 (hi
- lo
+ 1) * elt_type
->field (0).bitsize ();
2166 elt_type
->set_length ((array_bitsize
+ 7) / 8);
2171 return lookup_pointer_type (elt_type
);
2175 /* If ARR does not represent an array, returns ARR unchanged.
2176 Otherwise, returns either a standard GDB array with bounds set
2177 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
2178 GDB array. Returns NULL if ARR is a null fat pointer. */
2181 ada_coerce_to_simple_array_ptr (struct value
*arr
)
2183 if (ada_is_array_descriptor_type (arr
->type ()))
2185 struct type
*arrType
= ada_type_of_array (arr
, 1);
2187 if (arrType
== NULL
)
2189 return value_cast (arrType
, desc_data (arr
)->copy ());
2191 else if (ada_is_constrained_packed_array_type (arr
->type ()))
2192 return decode_constrained_packed_array (arr
);
2197 /* If ARR does not represent an array, returns ARR unchanged.
2198 Otherwise, returns a standard GDB array describing ARR (which may
2199 be ARR itself if it already is in the proper form). */
2202 ada_coerce_to_simple_array (struct value
*arr
)
2204 if (ada_is_array_descriptor_type (arr
->type ()))
2206 struct value
*arrVal
= ada_coerce_to_simple_array_ptr (arr
);
2209 error (_("Bounds unavailable for null array pointer."));
2210 return value_ind (arrVal
);
2212 else if (ada_is_constrained_packed_array_type (arr
->type ()))
2213 return decode_constrained_packed_array (arr
);
2218 /* If TYPE represents a GNAT array type, return it translated to an
2219 ordinary GDB array type (possibly with BITSIZE fields indicating
2220 packing). For other types, is the identity. */
2223 ada_coerce_to_simple_array_type (struct type
*type
)
2225 if (ada_is_constrained_packed_array_type (type
))
2226 return decode_constrained_packed_array_type (type
);
2228 if (ada_is_array_descriptor_type (type
))
2229 return ada_check_typedef (desc_data_target_type (type
));
2234 /* Non-zero iff TYPE represents a standard GNAT packed-array type. */
2237 ada_is_gnat_encoded_packed_array_type (struct type
*type
)
2241 type
= desc_base_type (type
);
2242 type
= ada_check_typedef (type
);
2244 ada_type_name (type
) != NULL
2245 && strstr (ada_type_name (type
), "___XP") != NULL
;
2248 /* Non-zero iff TYPE represents a standard GNAT constrained
2249 packed-array type. */
2252 ada_is_constrained_packed_array_type (struct type
*type
)
2254 return ada_is_gnat_encoded_packed_array_type (type
)
2255 && !ada_is_array_descriptor_type (type
);
2258 /* Non-zero iff TYPE represents an array descriptor for a
2259 unconstrained packed-array type. */
2262 ada_is_unconstrained_packed_array_type (struct type
*type
)
2264 if (!ada_is_array_descriptor_type (type
))
2267 if (ada_is_gnat_encoded_packed_array_type (type
))
2270 /* If we saw GNAT encodings, then the above code is sufficient.
2271 However, with minimal encodings, we will just have a thick
2273 if (is_thick_pntr (type
))
2275 type
= desc_base_type (type
);
2276 /* The structure's first field is a pointer to an array, so this
2277 fetches the array type. */
2278 type
= type
->field (0).type ()->target_type ();
2279 if (type
->code () == TYPE_CODE_TYPEDEF
)
2280 type
= ada_typedef_target_type (type
);
2281 /* Now we can see if the array elements are packed. */
2282 return type
->field (0).bitsize () > 0;
2288 /* Return true if TYPE is a (Gnat-encoded) constrained packed array
2289 type, or if it is an ordinary (non-Gnat-encoded) packed array. */
2292 ada_is_any_packed_array_type (struct type
*type
)
2294 return (ada_is_constrained_packed_array_type (type
)
2295 || (type
->code () == TYPE_CODE_ARRAY
2296 && type
->field (0).bitsize () % 8 != 0));
2299 /* Given that TYPE encodes a packed array type (constrained or unconstrained),
2300 return the size of its elements in bits. */
2303 decode_packed_array_bitsize (struct type
*type
)
2305 const char *raw_name
;
2309 /* Access to arrays implemented as fat pointers are encoded as a typedef
2310 of the fat pointer type. We need the name of the fat pointer type
2311 to do the decoding, so strip the typedef layer. */
2312 if (type
->code () == TYPE_CODE_TYPEDEF
)
2313 type
= ada_typedef_target_type (type
);
2315 raw_name
= ada_type_name (ada_check_typedef (type
));
2317 raw_name
= ada_type_name (desc_base_type (type
));
2322 tail
= strstr (raw_name
, "___XP");
2323 if (tail
== nullptr)
2325 gdb_assert (is_thick_pntr (type
));
2326 /* The structure's first field is a pointer to an array, so this
2327 fetches the array type. */
2328 type
= type
->field (0).type ()->target_type ();
2329 /* Now we can see if the array elements are packed. */
2330 return type
->field (0).bitsize ();
2333 if (sscanf (tail
+ sizeof ("___XP") - 1, "%ld", &bits
) != 1)
2336 (_("could not understand bit size information on packed array"));
2343 /* Given that TYPE is a standard GDB array type with all bounds filled
2344 in, and that the element size of its ultimate scalar constituents
2345 (that is, either its elements, or, if it is an array of arrays, its
2346 elements' elements, etc.) is *ELT_BITS, return an identical type,
2347 but with the bit sizes of its elements (and those of any
2348 constituent arrays) recorded in the BITSIZE components of its
2349 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
2352 Note that, for arrays whose index type has an XA encoding where
2353 a bound references a record discriminant, getting that discriminant,
2354 and therefore the actual value of that bound, is not possible
2355 because none of the given parameters gives us access to the record.
2356 This function assumes that it is OK in the context where it is being
2357 used to return an array whose bounds are still dynamic and where
2358 the length is arbitrary. */
2360 static struct type
*
2361 constrained_packed_array_type (struct type
*type
, long *elt_bits
)
2363 struct type
*new_elt_type
;
2364 struct type
*new_type
;
2365 struct type
*index_type_desc
;
2366 struct type
*index_type
;
2367 LONGEST low_bound
, high_bound
;
2369 type
= ada_check_typedef (type
);
2370 if (type
->code () != TYPE_CODE_ARRAY
)
2373 index_type_desc
= ada_find_parallel_type (type
, "___XA");
2374 if (index_type_desc
)
2375 index_type
= to_fixed_range_type (index_type_desc
->field (0).type (),
2378 index_type
= type
->index_type ();
2380 type_allocator
alloc (type
);
2382 constrained_packed_array_type (ada_check_typedef (type
->target_type ()),
2384 new_type
= create_array_type (alloc
, new_elt_type
, index_type
);
2385 new_type
->field (0).set_bitsize (*elt_bits
);
2386 new_type
->set_name (ada_type_name (type
));
2388 if ((check_typedef (index_type
)->code () == TYPE_CODE_RANGE
2389 && is_dynamic_type (check_typedef (index_type
)))
2390 || !get_discrete_bounds (index_type
, &low_bound
, &high_bound
))
2391 low_bound
= high_bound
= 0;
2392 if (high_bound
< low_bound
)
2395 new_type
->set_length (0);
2399 *elt_bits
*= (high_bound
- low_bound
+ 1);
2400 new_type
->set_length ((*elt_bits
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
);
2403 new_type
->set_is_fixed_instance (true);
2407 /* The array type encoded by TYPE, where
2408 ada_is_constrained_packed_array_type (TYPE). */
2410 static struct type
*
2411 decode_constrained_packed_array_type (struct type
*type
)
2413 const char *raw_name
= ada_type_name (ada_check_typedef (type
));
2416 struct type
*shadow_type
;
2420 raw_name
= ada_type_name (desc_base_type (type
));
2425 name
= (char *) alloca (strlen (raw_name
) + 1);
2426 tail
= strstr (raw_name
, "___XP");
2427 type
= desc_base_type (type
);
2429 memcpy (name
, raw_name
, tail
- raw_name
);
2430 name
[tail
- raw_name
] = '\000';
2432 shadow_type
= ada_find_parallel_type_with_name (type
, name
);
2434 if (shadow_type
== NULL
)
2436 lim_warning (_("could not find bounds information on packed array"));
2439 shadow_type
= check_typedef (shadow_type
);
2441 if (shadow_type
->code () != TYPE_CODE_ARRAY
)
2443 lim_warning (_("could not understand bounds "
2444 "information on packed array"));
2448 bits
= decode_packed_array_bitsize (type
);
2449 return constrained_packed_array_type (shadow_type
, &bits
);
2452 /* Helper function for decode_constrained_packed_array. Set the field
2453 bitsize on a series of packed arrays. Returns the number of
2454 elements in TYPE. */
2457 recursively_update_array_bitsize (struct type
*type
)
2459 gdb_assert (type
->code () == TYPE_CODE_ARRAY
);
2462 if (!get_discrete_bounds (type
->index_type (), &low
, &high
)
2465 LONGEST our_len
= high
- low
+ 1;
2467 struct type
*elt_type
= type
->target_type ();
2468 if (elt_type
->code () == TYPE_CODE_ARRAY
)
2470 LONGEST elt_len
= recursively_update_array_bitsize (elt_type
);
2471 LONGEST elt_bitsize
= elt_len
* elt_type
->field (0).bitsize ();
2472 type
->field (0).set_bitsize (elt_bitsize
);
2474 type
->set_length (((our_len
* elt_bitsize
+ HOST_CHAR_BIT
- 1)
2481 /* Given that ARR is a struct value *indicating a GNAT constrained packed
2482 array, returns a simple array that denotes that array. Its type is a
2483 standard GDB array type except that the BITSIZEs of the array
2484 target types are set to the number of bits in each element, and the
2485 type length is set appropriately. */
2487 static struct value
*
2488 decode_constrained_packed_array (struct value
*arr
)
2492 /* If our value is a pointer, then dereference it. Likewise if
2493 the value is a reference. Make sure that this operation does not
2494 cause the target type to be fixed, as this would indirectly cause
2495 this array to be decoded. The rest of the routine assumes that
2496 the array hasn't been decoded yet, so we use the basic "coerce_ref"
2497 and "value_ind" routines to perform the dereferencing, as opposed
2498 to using "ada_coerce_ref" or "ada_value_ind". */
2499 arr
= coerce_ref (arr
);
2500 if (ada_check_typedef (arr
->type ())->code () == TYPE_CODE_PTR
)
2501 arr
= value_ind (arr
);
2503 type
= decode_constrained_packed_array_type (arr
->type ());
2506 error (_("can't unpack array"));
2510 /* Decoding the packed array type could not correctly set the field
2511 bitsizes for any dimension except the innermost, because the
2512 bounds may be variable and were not passed to that function. So,
2513 we further resolve the array bounds here and then update the
2515 const gdb_byte
*valaddr
= arr
->contents_for_printing ().data ();
2516 CORE_ADDR address
= arr
->address ();
2517 gdb::array_view
<const gdb_byte
> view
2518 = gdb::make_array_view (valaddr
, type
->length ());
2519 type
= resolve_dynamic_type (type
, view
, address
);
2520 recursively_update_array_bitsize (type
);
2522 if (type_byte_order (arr
->type ()) == BFD_ENDIAN_BIG
2523 && ada_is_modular_type (arr
->type ()))
2525 /* This is a (right-justified) modular type representing a packed
2526 array with no wrapper. In order to interpret the value through
2527 the (left-justified) packed array type we just built, we must
2528 first left-justify it. */
2529 int bit_size
, bit_pos
;
2532 mod
= ada_modulus (arr
->type ()) - 1;
2539 bit_pos
= HOST_CHAR_BIT
* arr
->type ()->length () - bit_size
;
2540 arr
= ada_value_primitive_packed_val (arr
, NULL
,
2541 bit_pos
/ HOST_CHAR_BIT
,
2542 bit_pos
% HOST_CHAR_BIT
,
2547 return coerce_unspec_val_to_type (arr
, type
);
2551 /* The value of the element of packed array ARR at the ARITY indices
2552 given in IND. ARR must be a simple array. */
2554 static struct value
*
2555 value_subscript_packed (struct value
*arr
, int arity
, struct value
**ind
)
2558 int bits
, elt_off
, bit_off
;
2559 long elt_total_bit_offset
;
2560 struct type
*elt_type
;
2564 elt_total_bit_offset
= 0;
2565 elt_type
= ada_check_typedef (arr
->type ());
2566 for (i
= 0; i
< arity
; i
+= 1)
2568 if (elt_type
->code () != TYPE_CODE_ARRAY
2569 || elt_type
->field (0).bitsize () == 0)
2571 (_("attempt to do packed indexing of "
2572 "something other than a packed array"));
2575 struct type
*range_type
= elt_type
->index_type ();
2576 LONGEST lowerbound
, upperbound
;
2579 if (!get_discrete_bounds (range_type
, &lowerbound
, &upperbound
))
2581 lim_warning (_("don't know bounds of array"));
2582 lowerbound
= upperbound
= 0;
2585 idx
= pos_atr (ind
[i
]);
2586 if (idx
< lowerbound
|| idx
> upperbound
)
2587 lim_warning (_("packed array index %ld out of bounds"),
2589 bits
= elt_type
->field (0).bitsize ();
2590 elt_total_bit_offset
+= (idx
- lowerbound
) * bits
;
2591 elt_type
= ada_check_typedef (elt_type
->target_type ());
2594 elt_off
= elt_total_bit_offset
/ HOST_CHAR_BIT
;
2595 bit_off
= elt_total_bit_offset
% HOST_CHAR_BIT
;
2597 v
= ada_value_primitive_packed_val (arr
, NULL
, elt_off
, bit_off
,
2602 /* Non-zero iff TYPE includes negative integer values. */
2605 has_negatives (struct type
*type
)
2607 switch (type
->code ())
2612 return !type
->is_unsigned ();
2613 case TYPE_CODE_RANGE
:
2614 return type
->bounds ()->low
.const_val () - type
->bounds ()->bias
< 0;
2618 /* With SRC being a buffer containing BIT_SIZE bits of data at BIT_OFFSET,
2619 unpack that data into UNPACKED. UNPACKED_LEN is the size in bytes of
2620 the unpacked buffer.
2622 The size of the unpacked buffer (UNPACKED_LEN) is expected to be large
2623 enough to contain at least BIT_OFFSET bits. If not, an error is raised.
2625 IS_BIG_ENDIAN is nonzero if the data is stored in big endian mode,
2628 IS_SIGNED_TYPE is nonzero if the data corresponds to a signed type.
2630 IS_SCALAR is nonzero if the data corresponds to a signed type. */
2633 ada_unpack_from_contents (const gdb_byte
*src
, int bit_offset
, int bit_size
,
2634 gdb_byte
*unpacked
, int unpacked_len
,
2635 int is_big_endian
, int is_signed_type
,
2638 int src_len
= (bit_size
+ bit_offset
+ HOST_CHAR_BIT
- 1) / 8;
2639 int src_idx
; /* Index into the source area */
2640 int src_bytes_left
; /* Number of source bytes left to process. */
2641 int srcBitsLeft
; /* Number of source bits left to move */
2642 int unusedLS
; /* Number of bits in next significant
2643 byte of source that are unused */
2645 int unpacked_idx
; /* Index into the unpacked buffer */
2646 int unpacked_bytes_left
; /* Number of bytes left to set in unpacked. */
2648 unsigned long accum
; /* Staging area for bits being transferred */
2649 int accumSize
; /* Number of meaningful bits in accum */
2652 /* Transmit bytes from least to most significant; delta is the direction
2653 the indices move. */
2654 int delta
= is_big_endian
? -1 : 1;
2656 /* Make sure that unpacked is large enough to receive the BIT_SIZE
2658 if ((bit_size
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
> unpacked_len
)
2659 error (_("Cannot unpack %d bits into buffer of %d bytes"),
2660 bit_size
, unpacked_len
);
2662 srcBitsLeft
= bit_size
;
2663 src_bytes_left
= src_len
;
2664 unpacked_bytes_left
= unpacked_len
;
2669 src_idx
= src_len
- 1;
2671 && ((src
[0] << bit_offset
) & (1 << (HOST_CHAR_BIT
- 1))))
2675 (HOST_CHAR_BIT
- (bit_size
+ bit_offset
) % HOST_CHAR_BIT
)
2681 unpacked_idx
= unpacked_len
- 1;
2685 /* Non-scalar values must be aligned at a byte boundary... */
2687 (HOST_CHAR_BIT
- bit_size
% HOST_CHAR_BIT
) % HOST_CHAR_BIT
;
2688 /* ... And are placed at the beginning (most-significant) bytes
2690 unpacked_idx
= (bit_size
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
- 1;
2691 unpacked_bytes_left
= unpacked_idx
+ 1;
2696 int sign_bit_offset
= (bit_size
+ bit_offset
- 1) % 8;
2698 src_idx
= unpacked_idx
= 0;
2699 unusedLS
= bit_offset
;
2702 if (is_signed_type
&& (src
[src_len
- 1] & (1 << sign_bit_offset
)))
2707 while (src_bytes_left
> 0)
2709 /* Mask for removing bits of the next source byte that are not
2710 part of the value. */
2711 unsigned int unusedMSMask
=
2712 (1 << (srcBitsLeft
>= HOST_CHAR_BIT
? HOST_CHAR_BIT
: srcBitsLeft
)) -
2714 /* Sign-extend bits for this byte. */
2715 unsigned int signMask
= sign
& ~unusedMSMask
;
2718 (((src
[src_idx
] >> unusedLS
) & unusedMSMask
) | signMask
) << accumSize
;
2719 accumSize
+= HOST_CHAR_BIT
- unusedLS
;
2720 if (accumSize
>= HOST_CHAR_BIT
)
2722 unpacked
[unpacked_idx
] = accum
& ~(~0UL << HOST_CHAR_BIT
);
2723 accumSize
-= HOST_CHAR_BIT
;
2724 accum
>>= HOST_CHAR_BIT
;
2725 unpacked_bytes_left
-= 1;
2726 unpacked_idx
+= delta
;
2728 srcBitsLeft
-= HOST_CHAR_BIT
- unusedLS
;
2730 src_bytes_left
-= 1;
2733 while (unpacked_bytes_left
> 0)
2735 accum
|= sign
<< accumSize
;
2736 unpacked
[unpacked_idx
] = accum
& ~(~0UL << HOST_CHAR_BIT
);
2737 accumSize
-= HOST_CHAR_BIT
;
2740 accum
>>= HOST_CHAR_BIT
;
2741 unpacked_bytes_left
-= 1;
2742 unpacked_idx
+= delta
;
2746 /* Create a new value of type TYPE from the contents of OBJ starting
2747 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2748 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2749 assigning through the result will set the field fetched from.
2750 VALADDR is ignored unless OBJ is NULL, in which case,
2751 VALADDR+OFFSET must address the start of storage containing the
2752 packed value. The value returned in this case is never an lval.
2753 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2756 ada_value_primitive_packed_val (struct value
*obj
, const gdb_byte
*valaddr
,
2757 long offset
, int bit_offset
, int bit_size
,
2761 const gdb_byte
*src
; /* First byte containing data to unpack */
2763 const int is_scalar
= is_scalar_type (type
);
2764 const int is_big_endian
= type_byte_order (type
) == BFD_ENDIAN_BIG
;
2765 gdb::byte_vector staging
;
2767 type
= ada_check_typedef (type
);
2770 src
= valaddr
+ offset
;
2772 src
= obj
->contents ().data () + offset
;
2774 if (is_dynamic_type (type
))
2776 /* The length of TYPE might by dynamic, so we need to resolve
2777 TYPE in order to know its actual size, which we then use
2778 to create the contents buffer of the value we return.
2779 The difficulty is that the data containing our object is
2780 packed, and therefore maybe not at a byte boundary. So, what
2781 we do, is unpack the data into a byte-aligned buffer, and then
2782 use that buffer as our object's value for resolving the type. */
2783 int staging_len
= (bit_size
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
2784 staging
.resize (staging_len
);
2786 ada_unpack_from_contents (src
, bit_offset
, bit_size
,
2787 staging
.data (), staging
.size (),
2788 is_big_endian
, has_negatives (type
),
2790 type
= resolve_dynamic_type (type
, staging
, 0);
2791 if (type
->length () < (bit_size
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
)
2793 /* This happens when the length of the object is dynamic,
2794 and is actually smaller than the space reserved for it.
2795 For instance, in an array of variant records, the bit_size
2796 we're given is the array stride, which is constant and
2797 normally equal to the maximum size of its element.
2798 But, in reality, each element only actually spans a portion
2800 bit_size
= type
->length () * HOST_CHAR_BIT
;
2806 v
= value::allocate (type
);
2807 src
= valaddr
+ offset
;
2809 else if (obj
->lval () == lval_memory
&& obj
->lazy ())
2811 int src_len
= (bit_size
+ bit_offset
+ HOST_CHAR_BIT
- 1) / 8;
2814 v
= value_at (type
, obj
->address () + offset
);
2815 buf
= (gdb_byte
*) alloca (src_len
);
2816 read_memory (v
->address (), buf
, src_len
);
2821 v
= value::allocate (type
);
2822 src
= obj
->contents ().data () + offset
;
2827 long new_offset
= offset
;
2829 v
->set_component_location (obj
);
2830 v
->set_bitpos (bit_offset
+ obj
->bitpos ());
2831 v
->set_bitsize (bit_size
);
2832 if (v
->bitpos () >= HOST_CHAR_BIT
)
2835 v
->set_bitpos (v
->bitpos () - HOST_CHAR_BIT
);
2837 v
->set_offset (new_offset
);
2839 /* Also set the parent value. This is needed when trying to
2840 assign a new value (in inferior memory). */
2841 v
->set_parent (obj
);
2844 v
->set_bitsize (bit_size
);
2845 unpacked
= v
->contents_writeable ().data ();
2849 memset (unpacked
, 0, type
->length ());
2853 if (staging
.size () == type
->length ())
2855 /* Small short-cut: If we've unpacked the data into a buffer
2856 of the same size as TYPE's length, then we can reuse that,
2857 instead of doing the unpacking again. */
2858 memcpy (unpacked
, staging
.data (), staging
.size ());
2861 ada_unpack_from_contents (src
, bit_offset
, bit_size
,
2862 unpacked
, type
->length (),
2863 is_big_endian
, has_negatives (type
), is_scalar
);
2868 /* Store the contents of FROMVAL into the location of TOVAL.
2869 Return a new value with the location of TOVAL and contents of
2870 FROMVAL. Handles assignment into packed fields that have
2871 floating-point or non-scalar types. */
2873 static struct value
*
2874 ada_value_assign (struct value
*toval
, struct value
*fromval
)
2876 struct type
*type
= toval
->type ();
2877 int bits
= toval
->bitsize ();
2879 toval
= ada_coerce_ref (toval
);
2880 fromval
= ada_coerce_ref (fromval
);
2882 if (ada_is_direct_array_type (toval
->type ()))
2883 toval
= ada_coerce_to_simple_array (toval
);
2884 if (ada_is_direct_array_type (fromval
->type ()))
2885 fromval
= ada_coerce_to_simple_array (fromval
);
2887 if (!toval
->deprecated_modifiable ())
2888 error (_("Left operand of assignment is not a modifiable lvalue."));
2890 if (toval
->lval () == lval_memory
2892 && (type
->code () == TYPE_CODE_FLT
2893 || type
->code () == TYPE_CODE_STRUCT
))
2895 int len
= (toval
->bitpos ()
2896 + bits
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
2898 gdb_byte
*buffer
= (gdb_byte
*) alloca (len
);
2900 CORE_ADDR to_addr
= toval
->address ();
2902 if (type
->code () == TYPE_CODE_FLT
)
2903 fromval
= value_cast (type
, fromval
);
2905 read_memory (to_addr
, buffer
, len
);
2906 from_size
= fromval
->bitsize ();
2908 from_size
= fromval
->type ()->length () * TARGET_CHAR_BIT
;
2910 const int is_big_endian
= type_byte_order (type
) == BFD_ENDIAN_BIG
;
2911 ULONGEST from_offset
= 0;
2912 if (is_big_endian
&& is_scalar_type (fromval
->type ()))
2913 from_offset
= from_size
- bits
;
2914 copy_bitwise (buffer
, toval
->bitpos (),
2915 fromval
->contents ().data (), from_offset
,
2916 bits
, is_big_endian
);
2917 write_memory_with_notification (to_addr
, buffer
, len
);
2919 val
= toval
->copy ();
2920 memcpy (val
->contents_raw ().data (),
2921 fromval
->contents ().data (),
2923 val
->deprecated_set_type (type
);
2928 return value_assign (toval
, fromval
);
2932 /* Given that COMPONENT is a memory lvalue that is part of the lvalue
2933 CONTAINER, assign the contents of VAL to COMPONENTS's place in
2934 CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2935 COMPONENT, and not the inferior's memory. The current contents
2936 of COMPONENT are ignored.
2938 Although not part of the initial design, this function also works
2939 when CONTAINER and COMPONENT are not_lval's: it works as if CONTAINER
2940 had a null address, and COMPONENT had an address which is equal to
2941 its offset inside CONTAINER. */
2944 value_assign_to_component (struct value
*container
, struct value
*component
,
2947 LONGEST offset_in_container
=
2948 (LONGEST
) (component
->address () - container
->address ());
2949 int bit_offset_in_container
=
2950 component
->bitpos () - container
->bitpos ();
2953 val
= value_cast (component
->type (), val
);
2955 if (component
->bitsize () == 0)
2956 bits
= TARGET_CHAR_BIT
* component
->type ()->length ();
2958 bits
= component
->bitsize ();
2960 if (type_byte_order (container
->type ()) == BFD_ENDIAN_BIG
)
2964 if (is_scalar_type (check_typedef (component
->type ())))
2966 = component
->type ()->length () * TARGET_CHAR_BIT
- bits
;
2969 copy_bitwise ((container
->contents_writeable ().data ()
2970 + offset_in_container
),
2971 container
->bitpos () + bit_offset_in_container
,
2972 val
->contents ().data (), src_offset
, bits
, 1);
2975 copy_bitwise ((container
->contents_writeable ().data ()
2976 + offset_in_container
),
2977 container
->bitpos () + bit_offset_in_container
,
2978 val
->contents ().data (), 0, bits
, 0);
2981 /* Determine if TYPE is an access to an unconstrained array. */
2984 ada_is_access_to_unconstrained_array (struct type
*type
)
2986 return (type
->code () == TYPE_CODE_TYPEDEF
2987 && is_thick_pntr (ada_typedef_target_type (type
)));
2990 /* The value of the element of array ARR at the ARITY indices given in IND.
2991 ARR may be either a simple array, GNAT array descriptor, or pointer
2995 ada_value_subscript (struct value
*arr
, int arity
, struct value
**ind
)
2999 struct type
*elt_type
;
3001 elt
= ada_coerce_to_simple_array (arr
);
3003 elt_type
= ada_check_typedef (elt
->type ());
3004 if (elt_type
->code () == TYPE_CODE_ARRAY
3005 && elt_type
->field (0).bitsize () > 0)
3006 return value_subscript_packed (elt
, arity
, ind
);
3008 for (k
= 0; k
< arity
; k
+= 1)
3010 struct type
*saved_elt_type
= elt_type
->target_type ();
3012 if (elt_type
->code () != TYPE_CODE_ARRAY
)
3013 error (_("too many subscripts (%d expected)"), k
);
3015 elt
= value_subscript (elt
, pos_atr (ind
[k
]));
3017 if (ada_is_access_to_unconstrained_array (saved_elt_type
)
3018 && elt
->type ()->code () != TYPE_CODE_TYPEDEF
)
3020 /* The element is a typedef to an unconstrained array,
3021 except that the value_subscript call stripped the
3022 typedef layer. The typedef layer is GNAT's way to
3023 specify that the element is, at the source level, an
3024 access to the unconstrained array, rather than the
3025 unconstrained array. So, we need to restore that
3026 typedef layer, which we can do by forcing the element's
3027 type back to its original type. Otherwise, the returned
3028 value is going to be printed as the array, rather
3029 than as an access. Another symptom of the same issue
3030 would be that an expression trying to dereference the
3031 element would also be improperly rejected. */
3032 elt
->deprecated_set_type (saved_elt_type
);
3035 elt_type
= ada_check_typedef (elt
->type ());
3041 /* Assuming ARR is a pointer to a GDB array, the value of the element
3042 of *ARR at the ARITY indices given in IND.
3043 Does not read the entire array into memory.
3045 Note: Unlike what one would expect, this function is used instead of
3046 ada_value_subscript for basically all non-packed array types. The reason
3047 for this is that a side effect of doing our own pointer arithmetics instead
3048 of relying on value_subscript is that there is no implicit typedef peeling.
3049 This is important for arrays of array accesses, where it allows us to
3050 preserve the fact that the array's element is an array access, where the
3051 access part os encoded in a typedef layer. */
3053 static struct value
*
3054 ada_value_ptr_subscript (struct value
*arr
, int arity
, struct value
**ind
)
3057 struct value
*array_ind
= ada_value_ind (arr
);
3059 = check_typedef (array_ind
->enclosing_type ());
3061 if (type
->code () == TYPE_CODE_ARRAY
3062 && type
->field (0).bitsize () > 0)
3063 return value_subscript_packed (array_ind
, arity
, ind
);
3065 for (k
= 0; k
< arity
; k
+= 1)
3069 if (type
->code () != TYPE_CODE_ARRAY
)
3070 error (_("too many subscripts (%d expected)"), k
);
3071 arr
= value_cast (lookup_pointer_type (type
->target_type ()),
3073 get_discrete_bounds (type
->index_type (), &lwb
, &upb
);
3074 arr
= value_ptradd (arr
, pos_atr (ind
[k
]) - lwb
);
3075 type
= type
->target_type ();
3078 return value_ind (arr
);
3081 /* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
3082 actual type of ARRAY_PTR is ignored), returns the Ada slice of
3083 HIGH'Pos-LOW'Pos+1 elements starting at index LOW. The lower bound of
3084 this array is LOW, as per Ada rules. */
3085 static struct value
*
3086 ada_value_slice_from_ptr (struct value
*array_ptr
, struct type
*type
,
3089 struct type
*type0
= ada_check_typedef (type
);
3090 struct type
*base_index_type
= type0
->index_type ()->target_type ();
3091 type_allocator
alloc (base_index_type
);
3092 struct type
*index_type
3093 = create_static_range_type (alloc
, base_index_type
, low
, high
);
3094 struct type
*slice_type
= create_array_type_with_stride
3095 (alloc
, type0
->target_type (), index_type
,
3096 type0
->dyn_prop (DYN_PROP_BYTE_STRIDE
),
3097 type0
->field (0).bitsize ());
3098 int base_low
= ada_discrete_type_low_bound (type0
->index_type ());
3099 std::optional
<LONGEST
> base_low_pos
, low_pos
;
3102 low_pos
= discrete_position (base_index_type
, low
);
3103 base_low_pos
= discrete_position (base_index_type
, base_low
);
3105 if (!low_pos
.has_value () || !base_low_pos
.has_value ())
3107 warning (_("unable to get positions in slice, use bounds instead"));
3109 base_low_pos
= base_low
;
3112 ULONGEST stride
= slice_type
->field (0).bitsize () / 8;
3114 stride
= type0
->target_type ()->length ();
3116 base
= value_as_address (array_ptr
) + (*low_pos
- *base_low_pos
) * stride
;
3117 return value_at_lazy (slice_type
, base
);
3121 static struct value
*
3122 ada_value_slice (struct value
*array
, int low
, int high
)
3124 struct type
*type
= ada_check_typedef (array
->type ());
3125 struct type
*base_index_type
= type
->index_type ()->target_type ();
3126 type_allocator
alloc (type
->index_type ());
3127 struct type
*index_type
3128 = create_static_range_type (alloc
, type
->index_type (), low
, high
);
3129 struct type
*slice_type
= create_array_type_with_stride
3130 (alloc
, type
->target_type (), index_type
,
3131 type
->dyn_prop (DYN_PROP_BYTE_STRIDE
),
3132 type
->field (0).bitsize ());
3133 std::optional
<LONGEST
> low_pos
, high_pos
;
3136 low_pos
= discrete_position (base_index_type
, low
);
3137 high_pos
= discrete_position (base_index_type
, high
);
3139 if (!low_pos
.has_value () || !high_pos
.has_value ())
3141 warning (_("unable to get positions in slice, use bounds instead"));
3146 return value_cast (slice_type
,
3147 value_slice (array
, low
, *high_pos
- *low_pos
+ 1));
3150 /* If type is a record type in the form of a standard GNAT array
3151 descriptor, returns the number of dimensions for type. If arr is a
3152 simple array, returns the number of "array of"s that prefix its
3153 type designation. Otherwise, returns 0. */
3156 ada_array_arity (struct type
*type
)
3163 type
= desc_base_type (type
);
3166 if (type
->code () == TYPE_CODE_STRUCT
)
3167 return desc_arity (desc_bounds_type (type
));
3169 while (type
->code () == TYPE_CODE_ARRAY
)
3172 type
= ada_check_typedef (type
->target_type ());
3178 /* If TYPE is a record type in the form of a standard GNAT array
3179 descriptor or a simple array type, returns the element type for
3180 TYPE after indexing by NINDICES indices, or by all indices if
3181 NINDICES is -1. Otherwise, returns NULL. */
3184 ada_array_element_type (struct type
*type
, int nindices
)
3186 type
= desc_base_type (type
);
3188 if (type
->code () == TYPE_CODE_STRUCT
)
3191 struct type
*p_array_type
;
3193 p_array_type
= desc_data_target_type (type
);
3195 k
= ada_array_arity (type
);
3199 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
3200 if (nindices
>= 0 && k
> nindices
)
3202 while (k
> 0 && p_array_type
!= NULL
)
3204 p_array_type
= ada_check_typedef (p_array_type
->target_type ());
3207 return p_array_type
;
3209 else if (type
->code () == TYPE_CODE_ARRAY
)
3211 while (nindices
!= 0 && type
->code () == TYPE_CODE_ARRAY
)
3213 type
= type
->target_type ();
3214 /* A multi-dimensional array is represented using a sequence
3215 of array types. If one of these types has a name, then
3216 it is not another dimension of the outer array, but
3217 rather the element type of the outermost array. */
3218 if (type
->name () != nullptr)
3228 /* See ada-lang.h. */
3231 ada_index_type (struct type
*type
, int n
, const char *name
)
3233 struct type
*result_type
;
3235 type
= desc_base_type (type
);
3237 if (n
< 0 || n
> ada_array_arity (type
))
3238 error (_("invalid dimension number to '%s"), name
);
3240 if (ada_is_simple_array_type (type
))
3244 for (i
= 1; i
< n
; i
+= 1)
3246 type
= ada_check_typedef (type
);
3247 type
= type
->target_type ();
3249 result_type
= ada_check_typedef (type
)->index_type ()->target_type ();
3250 /* FIXME: The stabs type r(0,0);bound;bound in an array type
3251 has a target type of TYPE_CODE_UNDEF. We compensate here, but
3252 perhaps stabsread.c would make more sense. */
3253 if (result_type
&& result_type
->code () == TYPE_CODE_UNDEF
)
3258 result_type
= desc_index_type (desc_bounds_type (type
), n
);
3259 if (result_type
== NULL
)
3260 error (_("attempt to take bound of something that is not an array"));
3266 /* Given that arr is an array type, returns the lower bound of the
3267 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
3268 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
3269 array-descriptor type. It works for other arrays with bounds supplied
3270 by run-time quantities other than discriminants. */
3273 ada_array_bound_from_type (struct type
*arr_type
, int n
, int which
)
3275 struct type
*type
, *index_type_desc
, *index_type
;
3278 gdb_assert (which
== 0 || which
== 1);
3280 if (ada_is_constrained_packed_array_type (arr_type
))
3281 arr_type
= decode_constrained_packed_array_type (arr_type
);
3283 if (arr_type
== NULL
|| !ada_is_simple_array_type (arr_type
))
3286 if (arr_type
->code () == TYPE_CODE_PTR
)
3287 type
= arr_type
->target_type ();
3291 if (type
->is_fixed_instance ())
3293 /* The array has already been fixed, so we do not need to
3294 check the parallel ___XA type again. That encoding has
3295 already been applied, so ignore it now. */
3296 index_type_desc
= NULL
;
3300 index_type_desc
= ada_find_parallel_type (type
, "___XA");
3301 ada_fixup_array_indexes_type (index_type_desc
);
3304 if (index_type_desc
!= NULL
)
3305 index_type
= to_fixed_range_type (index_type_desc
->field (n
- 1).type (),
3309 struct type
*elt_type
= check_typedef (type
);
3311 for (i
= 1; i
< n
; i
++)
3312 elt_type
= check_typedef (elt_type
->target_type ());
3314 index_type
= elt_type
->index_type ();
3318 ? ada_discrete_type_low_bound (index_type
)
3319 : ada_discrete_type_high_bound (index_type
));
3322 /* Given that arr is an array value, returns the lower bound of the
3323 nth index (numbering from 1) if WHICH is 0, and the upper bound if
3324 WHICH is 1. This routine will also work for arrays with bounds
3325 supplied by run-time quantities other than discriminants. */
3328 ada_array_bound (struct value
*arr
, int n
, int which
)
3330 struct type
*arr_type
;
3332 if (check_typedef (arr
->type ())->code () == TYPE_CODE_PTR
)
3333 arr
= value_ind (arr
);
3334 arr_type
= arr
->enclosing_type ();
3336 if (ada_is_constrained_packed_array_type (arr_type
))
3337 return ada_array_bound (decode_constrained_packed_array (arr
), n
, which
);
3338 else if (ada_is_simple_array_type (arr_type
))
3339 return ada_array_bound_from_type (arr_type
, n
, which
);
3341 return value_as_long (desc_one_bound (desc_bounds (arr
), n
, which
));
3344 /* Given that arr is an array value, returns the length of the
3345 nth index. This routine will also work for arrays with bounds
3346 supplied by run-time quantities other than discriminants.
3347 Does not work for arrays indexed by enumeration types with representation
3348 clauses at the moment. */
3351 ada_array_length (struct value
*arr
, int n
)
3353 struct type
*arr_type
, *index_type
;
3356 if (check_typedef (arr
->type ())->code () == TYPE_CODE_PTR
)
3357 arr
= value_ind (arr
);
3358 arr_type
= arr
->enclosing_type ();
3360 if (ada_is_constrained_packed_array_type (arr_type
))
3361 return ada_array_length (decode_constrained_packed_array (arr
), n
);
3363 if (ada_is_simple_array_type (arr_type
))
3365 low
= ada_array_bound_from_type (arr_type
, n
, 0);
3366 high
= ada_array_bound_from_type (arr_type
, n
, 1);
3370 low
= value_as_long (desc_one_bound (desc_bounds (arr
), n
, 0));
3371 high
= value_as_long (desc_one_bound (desc_bounds (arr
), n
, 1));
3374 arr_type
= check_typedef (arr_type
);
3375 index_type
= ada_index_type (arr_type
, n
, "length");
3376 if (index_type
!= NULL
)
3378 struct type
*base_type
;
3379 if (index_type
->code () == TYPE_CODE_RANGE
)
3380 base_type
= index_type
->target_type ();
3382 base_type
= index_type
;
3384 low
= pos_atr (value_from_longest (base_type
, low
));
3385 high
= pos_atr (value_from_longest (base_type
, high
));
3387 return high
- low
+ 1;
3390 /* An array whose type is that of ARR_TYPE (an array type), with
3391 bounds LOW to HIGH, but whose contents are unimportant. If HIGH is
3392 less than LOW, then LOW-1 is used. */
3394 static struct value
*
3395 empty_array (struct type
*arr_type
, int low
, int high
)
3397 struct type
*arr_type0
= ada_check_typedef (arr_type
);
3398 type_allocator
alloc (arr_type0
->index_type ()->target_type ());
3399 struct type
*index_type
3400 = create_static_range_type
3401 (alloc
, arr_type0
->index_type ()->target_type (), low
,
3402 high
< low
? low
- 1 : high
);
3403 struct type
*elt_type
= ada_array_element_type (arr_type0
, 1);
3405 return value::allocate (create_array_type (alloc
, elt_type
, index_type
));
3409 /* Name resolution */
3411 /* The "decoded" name for the user-definable Ada operator corresponding
3415 ada_decoded_op_name (enum exp_opcode op
)
3419 for (i
= 0; ada_opname_table
[i
].encoded
!= NULL
; i
+= 1)
3421 if (ada_opname_table
[i
].op
== op
)
3422 return ada_opname_table
[i
].decoded
;
3424 error (_("Could not find operator name for opcode"));
3427 /* Sort SYMS to put the choices in a canonical order by the encoded
3431 sort_choices (std::vector
<struct block_symbol
> &syms
)
3433 std::sort (syms
.begin (), syms
.end (),
3434 [] (const block_symbol
&a
, const block_symbol
&b
)
3436 if (!a
.symbol
->is_objfile_owned ())
3438 if (!b
.symbol
->is_objfile_owned ())
3441 const char *fna
= a
.symbol
->symtab ()->filename
;
3442 const char *fnb
= b
.symbol
->symtab ()->filename
;
3444 /* First sort by basename. This is done because,
3445 depending on how GNAT was invoked, different sources
3446 might have relative or absolute paths, but we'd like
3447 similar ones to appear together. */
3448 int cmp
= strcmp (lbasename (fna
), lbasename (fnb
));
3452 /* The basenames are the same, so group identical paths
3454 cmp
= strcmp (fna
, fnb
);
3458 if (a
.symbol
->line () < b
.symbol
->line ())
3460 if (a
.symbol
->line () > b
.symbol
->line ())
3463 return strcmp (a
.symbol
->natural_name (),
3464 b
.symbol
->natural_name ()) < 0;
3468 /* Whether GDB should display formals and return types for functions in the
3469 overloads selection menu. */
3470 static bool print_signatures
= true;
3472 /* Print the signature for SYM on STREAM according to the FLAGS options. For
3473 all but functions, the signature is just the name of the symbol. For
3474 functions, this is the name of the function, the list of types for formals
3475 and the return type (if any). */
3478 ada_print_symbol_signature (struct ui_file
*stream
, struct symbol
*sym
,
3479 const struct type_print_options
*flags
)
3481 struct type
*type
= sym
->type ();
3483 gdb_printf (stream
, "%s", sym
->print_name ());
3484 if (!print_signatures
3486 || type
->code () != TYPE_CODE_FUNC
)
3489 if (type
->num_fields () > 0)
3493 gdb_printf (stream
, " (");
3494 for (i
= 0; i
< type
->num_fields (); ++i
)
3497 gdb_printf (stream
, "; ");
3498 ada_print_type (type
->field (i
).type (), NULL
, stream
, -1, 0,
3501 gdb_printf (stream
, ")");
3503 if (type
->target_type () != NULL
3504 && type
->target_type ()->code () != TYPE_CODE_VOID
)
3506 gdb_printf (stream
, " return ");
3507 ada_print_type (type
->target_type (), NULL
, stream
, -1, 0, flags
);
3511 /* Read and validate a set of numeric choices from the user in the
3512 range 0 .. N_CHOICES-1. Place the results in increasing
3513 order in CHOICES[0 .. N-1], and return N.
3515 The user types choices as a sequence of numbers on one line
3516 separated by blanks, encoding them as follows:
3518 + A choice of 0 means to cancel the selection, throwing an error.
3519 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3520 + The user chooses k by typing k+IS_ALL_CHOICE+1.
3522 The user is not allowed to choose more than MAX_RESULTS values.
3524 ANNOTATION_SUFFIX, if present, is used to annotate the input
3525 prompts (for use with the -f switch). */
3528 get_selections (int *choices
, int n_choices
, int max_results
,
3529 int is_all_choice
, const char *annotation_suffix
)
3534 int first_choice
= is_all_choice
? 2 : 1;
3536 prompt
= getenv ("PS2");
3541 args
= command_line_input (buffer
, prompt
, annotation_suffix
);
3544 error_no_arg (_("one or more choice numbers"));
3548 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3549 order, as given in args. Choices are validated. */
3555 args
= skip_spaces (args
);
3556 if (*args
== '\0' && n_chosen
== 0)
3557 error_no_arg (_("one or more choice numbers"));
3558 else if (*args
== '\0')
3561 choice
= strtol (args
, &args2
, 10);
3562 if (args
== args2
|| choice
< 0
3563 || choice
> n_choices
+ first_choice
- 1)
3564 error (_("Argument must be choice number"));
3568 error (_("cancelled"));
3570 if (choice
< first_choice
)
3572 n_chosen
= n_choices
;
3573 for (j
= 0; j
< n_choices
; j
+= 1)
3577 choice
-= first_choice
;
3579 for (j
= n_chosen
- 1; j
>= 0 && choice
< choices
[j
]; j
-= 1)
3583 if (j
< 0 || choice
!= choices
[j
])
3587 for (k
= n_chosen
- 1; k
> j
; k
-= 1)
3588 choices
[k
+ 1] = choices
[k
];
3589 choices
[j
+ 1] = choice
;
3594 if (n_chosen
> max_results
)
3595 error (_("Select no more than %d of the above"), max_results
);
3600 /* Given a list symbols in SYMS, select up to MAX_RESULTS>0
3601 by asking the user (if necessary), returning the number selected,
3602 and setting the first elements of SYMS items. Error if no symbols
3605 /* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
3606 to be re-integrated one of these days. */
3609 user_select_syms (std::vector
<struct block_symbol
> &syms
, int max_results
)
3612 int first_choice
= (max_results
== 1) ? 1 : 2;
3613 const char *select_mode
= multiple_symbols_select_mode ();
3615 if (max_results
< 1)
3616 error (_("Request to select 0 symbols!"));
3617 if (syms
.size () <= 1)
3618 return syms
.size ();
3620 if (select_mode
== multiple_symbols_cancel
)
3622 canceled because the command is ambiguous\n\
3623 See set/show multiple-symbol."));
3625 /* If select_mode is "all", then return all possible symbols.
3626 Only do that if more than one symbol can be selected, of course.
3627 Otherwise, display the menu as usual. */
3628 if (select_mode
== multiple_symbols_all
&& max_results
> 1)
3629 return syms
.size ();
3631 gdb_printf (_("[0] cancel\n"));
3632 if (max_results
> 1)
3633 gdb_printf (_("[1] all\n"));
3635 sort_choices (syms
);
3637 for (i
= 0; i
< syms
.size (); i
+= 1)
3639 if (syms
[i
].symbol
== NULL
)
3642 if (syms
[i
].symbol
->aclass () == LOC_BLOCK
)
3644 struct symtab_and_line sal
=
3645 find_function_start_sal (syms
[i
].symbol
, 1);
3647 gdb_printf ("[%d] ", i
+ first_choice
);
3648 ada_print_symbol_signature (gdb_stdout
, syms
[i
].symbol
,
3649 &type_print_raw_options
);
3650 if (sal
.symtab
== NULL
)
3651 gdb_printf (_(" at %p[<no source file available>%p]:%d\n"),
3652 metadata_style
.style ().ptr (), nullptr, sal
.line
);
3656 styled_string (file_name_style
.style (),
3657 symtab_to_filename_for_display (sal
.symtab
)),
3664 (syms
[i
].symbol
->aclass () == LOC_CONST
3665 && syms
[i
].symbol
->type () != NULL
3666 && syms
[i
].symbol
->type ()->code () == TYPE_CODE_ENUM
);
3667 struct symtab
*symtab
= NULL
;
3669 if (syms
[i
].symbol
->is_objfile_owned ())
3670 symtab
= syms
[i
].symbol
->symtab ();
3672 if (syms
[i
].symbol
->line () != 0 && symtab
!= NULL
)
3674 gdb_printf ("[%d] ", i
+ first_choice
);
3675 ada_print_symbol_signature (gdb_stdout
, syms
[i
].symbol
,
3676 &type_print_raw_options
);
3677 gdb_printf (_(" at %ps:%ps\n"),
3678 styled_string (file_name_style
.style (),
3679 symtab_to_filename_for_display (symtab
)),
3680 styled_string (line_number_style
.style (),
3681 pulongest (syms
[i
].symbol
->line ())));
3683 else if (is_enumeral
3684 && syms
[i
].symbol
->type ()->name () != NULL
)
3686 gdb_printf (("[%d] "), i
+ first_choice
);
3687 ada_print_type (syms
[i
].symbol
->type (), NULL
,
3688 gdb_stdout
, -1, 0, &type_print_raw_options
);
3689 gdb_printf (_("'(%s) (enumeral)\n"),
3690 syms
[i
].symbol
->print_name ());
3694 gdb_printf ("[%d] ", i
+ first_choice
);
3695 ada_print_symbol_signature (gdb_stdout
, syms
[i
].symbol
,
3696 &type_print_raw_options
);
3699 gdb_printf (is_enumeral
3700 ? _(" in %ps (enumeral)\n")
3702 styled_string (file_name_style
.style (),
3703 symtab_to_filename_for_display (symtab
)));
3705 gdb_printf (is_enumeral
3706 ? _(" (enumeral)\n")
3712 int *chosen
= XALLOCAVEC (int , syms
.size ());
3713 int n_chosen
= get_selections (chosen
, syms
.size (),
3714 max_results
, max_results
> 1,
3717 for (i
= 0; i
< n_chosen
; i
+= 1)
3718 syms
[i
] = syms
[chosen
[i
]];
3723 /* See ada-lang.h. */
3726 ada_find_operator_symbol (enum exp_opcode op
, bool parse_completion
,
3727 int nargs
, value
*argvec
[])
3729 if (possible_user_operator_p (op
, argvec
))
3731 std::vector
<struct block_symbol
> candidates
3732 = ada_lookup_symbol_list (ada_decoded_op_name (op
),
3735 int i
= ada_resolve_function (candidates
, argvec
,
3736 nargs
, ada_decoded_op_name (op
), NULL
,
3739 return candidates
[i
];
3744 /* See ada-lang.h. */
3747 ada_resolve_funcall (struct symbol
*sym
, const struct block
*block
,
3748 struct type
*context_type
,
3749 bool parse_completion
,
3750 int nargs
, value
*argvec
[],
3751 innermost_block_tracker
*tracker
)
3753 std::vector
<struct block_symbol
> candidates
3754 = ada_lookup_symbol_list (sym
->linkage_name (), block
, SEARCH_VFT
);
3757 if (candidates
.size () == 1)
3761 i
= ada_resolve_function
3764 sym
->linkage_name (),
3765 context_type
, parse_completion
);
3767 error (_("Could not find a match for %s"), sym
->print_name ());
3770 tracker
->update (candidates
[i
]);
3771 return candidates
[i
];
3774 /* Resolve a mention of a name where the context type is an
3775 enumeration type. */
3778 ada_resolve_enum (std::vector
<struct block_symbol
> &syms
,
3779 const char *name
, struct type
*context_type
,
3780 bool parse_completion
)
3782 gdb_assert (context_type
->code () == TYPE_CODE_ENUM
);
3783 context_type
= ada_check_typedef (context_type
);
3785 /* We already know the name matches, so we're just looking for
3786 an element of the correct enum type. */
3787 struct type
*type1
= context_type
;
3788 for (int i
= 0; i
< syms
.size (); ++i
)
3790 struct type
*type2
= ada_check_typedef (syms
[i
].symbol
->type ());
3795 for (int i
= 0; i
< syms
.size (); ++i
)
3797 struct type
*type2
= ada_check_typedef (syms
[i
].symbol
->type ());
3798 if (strcmp (type1
->name (), type2
->name ()) != 0)
3800 if (ada_identical_enum_types_p (type1
, type2
))
3804 error (_("No name '%s' in enumeration type '%s'"), name
,
3805 ada_type_name (context_type
));
3808 /* See ada-lang.h. */
3811 ada_resolve_variable (struct symbol
*sym
, const struct block
*block
,
3812 struct type
*context_type
,
3813 bool parse_completion
,
3815 innermost_block_tracker
*tracker
)
3817 std::vector
<struct block_symbol
> candidates
3818 = ada_lookup_symbol_list (sym
->linkage_name (), block
, SEARCH_VFT
);
3820 if (std::any_of (candidates
.begin (),
3822 [] (block_symbol
&bsym
)
3824 switch (bsym
.symbol
->aclass ())
3829 case LOC_REGPARM_ADDR
:
3838 /* Types tend to get re-introduced locally, so if there
3839 are any local symbols that are not types, first filter
3843 (candidates
.begin (),
3845 [] (block_symbol
&bsym
)
3847 return bsym
.symbol
->aclass () == LOC_TYPEDEF
;
3852 /* Filter out artificial symbols. */
3855 (candidates
.begin (),
3857 [] (block_symbol
&bsym
)
3859 return bsym
.symbol
->is_artificial ();
3864 if (candidates
.empty ())
3865 error (_("No definition found for %s"), sym
->print_name ());
3866 else if (candidates
.size () == 1)
3868 else if (context_type
!= nullptr
3869 && context_type
->code () == TYPE_CODE_ENUM
)
3870 i
= ada_resolve_enum (candidates
, sym
->linkage_name (), context_type
,
3872 else if (context_type
== nullptr
3873 && symbols_are_identical_enums (candidates
))
3875 /* If all the remaining symbols are identical enumerals, then
3876 just keep the first one and discard the rest.
3878 Unlike what we did previously, we do not discard any entry
3879 unless they are ALL identical. This is because the symbol
3880 comparison is not a strict comparison, but rather a practical
3881 comparison. If all symbols are considered identical, then
3882 we can just go ahead and use the first one and discard the rest.
3883 But if we cannot reduce the list to a single element, we have
3884 to ask the user to disambiguate anyways. And if we have to
3885 present a multiple-choice menu, it's less confusing if the list
3886 isn't missing some choices that were identical and yet distinct. */
3887 candidates
.resize (1);
3890 else if (deprocedure_p
&& !is_nonfunction (candidates
))
3892 i
= ada_resolve_function
3893 (candidates
, NULL
, 0,
3894 sym
->linkage_name (),
3895 context_type
, parse_completion
);
3897 error (_("Could not find a match for %s"), sym
->print_name ());
3901 gdb_printf (_("Multiple matches for %s\n"), sym
->print_name ());
3902 user_select_syms (candidates
, 1);
3906 tracker
->update (candidates
[i
]);
3907 return candidates
[i
];
3910 static bool ada_type_match (struct type
*ftype
, struct type
*atype
);
3912 /* Helper for ada_type_match that checks that two array types are
3913 compatible. As with that function, FTYPE is the formal type and
3914 ATYPE is the actual type. */
3917 ada_type_match_arrays (struct type
*ftype
, struct type
*atype
)
3919 if (ftype
->code () != TYPE_CODE_ARRAY
3920 && !ada_is_array_descriptor_type (ftype
))
3922 if (atype
->code () != TYPE_CODE_ARRAY
3923 && !ada_is_array_descriptor_type (atype
))
3926 if (ada_array_arity (ftype
) != ada_array_arity (atype
))
3929 struct type
*f_elt_type
= ada_array_element_type (ftype
, -1);
3930 struct type
*a_elt_type
= ada_array_element_type (atype
, -1);
3931 return ada_type_match (f_elt_type
, a_elt_type
);
3934 /* Return non-zero if formal type FTYPE matches actual type ATYPE.
3935 The term "match" here is rather loose. The match is heuristic and
3936 liberal -- while it tries to reject matches that are obviously
3937 incorrect, it may still let through some that do not strictly
3938 correspond to Ada rules. */
3941 ada_type_match (struct type
*ftype
, struct type
*atype
)
3943 ftype
= ada_check_typedef (ftype
);
3944 atype
= ada_check_typedef (atype
);
3946 if (ftype
->code () == TYPE_CODE_REF
)
3947 ftype
= ftype
->target_type ();
3948 if (atype
->code () == TYPE_CODE_REF
)
3949 atype
= atype
->target_type ();
3951 switch (ftype
->code ())
3954 return ftype
->code () == atype
->code ();
3956 if (atype
->code () != TYPE_CODE_PTR
)
3958 atype
= atype
->target_type ();
3959 /* This can only happen if the actual argument is 'null'. */
3960 if (atype
->code () == TYPE_CODE_INT
&& atype
->length () == 0)
3962 return ada_type_match (ftype
->target_type (), atype
);
3964 case TYPE_CODE_ENUM
:
3965 case TYPE_CODE_RANGE
:
3966 switch (atype
->code ())
3969 case TYPE_CODE_ENUM
:
3970 case TYPE_CODE_RANGE
:
3976 case TYPE_CODE_STRUCT
:
3977 if (!ada_is_array_descriptor_type (ftype
))
3978 return (atype
->code () == TYPE_CODE_STRUCT
3979 && !ada_is_array_descriptor_type (atype
));
3982 case TYPE_CODE_ARRAY
:
3983 return ada_type_match_arrays (ftype
, atype
);
3985 case TYPE_CODE_UNION
:
3987 return (atype
->code () == ftype
->code ());
3991 /* Return non-zero if the formals of FUNC "sufficiently match" the
3992 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
3993 may also be an enumeral, in which case it is treated as a 0-
3994 argument function. */
3997 ada_args_match (struct symbol
*func
, struct value
**actuals
, int n_actuals
)
4000 struct type
*func_type
= func
->type ();
4002 if (func
->aclass () == LOC_CONST
4003 && func_type
->code () == TYPE_CODE_ENUM
)
4004 return (n_actuals
== 0);
4005 else if (func_type
== NULL
|| func_type
->code () != TYPE_CODE_FUNC
)
4008 if (func_type
->num_fields () != n_actuals
)
4011 for (i
= 0; i
< n_actuals
; i
+= 1)
4013 if (actuals
[i
] == NULL
)
4017 struct type
*ftype
= ada_check_typedef (func_type
->field (i
).type ());
4018 struct type
*atype
= ada_check_typedef (actuals
[i
]->type ());
4020 if (!ada_type_match (ftype
, atype
))
4027 /* False iff function type FUNC_TYPE definitely does not produce a value
4028 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
4029 FUNC_TYPE is not a valid function type with a non-null return type
4030 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
4033 return_match (struct type
*func_type
, struct type
*context_type
)
4035 struct type
*return_type
;
4037 if (func_type
== NULL
)
4040 if (func_type
->code () == TYPE_CODE_FUNC
)
4041 return_type
= get_base_type (func_type
->target_type ());
4043 return_type
= get_base_type (func_type
);
4044 if (return_type
== NULL
)
4047 context_type
= get_base_type (context_type
);
4049 if (return_type
->code () == TYPE_CODE_ENUM
)
4050 return context_type
== NULL
|| return_type
== context_type
;
4051 else if (context_type
== NULL
)
4052 return return_type
->code () != TYPE_CODE_VOID
;
4054 return return_type
->code () == context_type
->code ();
4058 /* Returns the index in SYMS that contains the symbol for the
4059 function (if any) that matches the types of the NARGS arguments in
4060 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
4061 that returns that type, then eliminate matches that don't. If
4062 CONTEXT_TYPE is void and there is at least one match that does not
4063 return void, eliminate all matches that do.
4065 Asks the user if there is more than one match remaining. Returns -1
4066 if there is no such symbol or none is selected. NAME is used
4067 solely for messages. May re-arrange and modify SYMS in
4068 the process; the index returned is for the modified vector. */
4071 ada_resolve_function (std::vector
<struct block_symbol
> &syms
,
4072 struct value
**args
, int nargs
,
4073 const char *name
, struct type
*context_type
,
4074 bool parse_completion
)
4078 int m
; /* Number of hits */
4081 /* In the first pass of the loop, we only accept functions matching
4082 context_type. If none are found, we add a second pass of the loop
4083 where every function is accepted. */
4084 for (fallback
= 0; m
== 0 && fallback
< 2; fallback
++)
4086 for (k
= 0; k
< syms
.size (); k
+= 1)
4088 struct type
*type
= ada_check_typedef (syms
[k
].symbol
->type ());
4090 if (ada_args_match (syms
[k
].symbol
, args
, nargs
)
4091 && (fallback
|| return_match (type
, context_type
)))
4099 /* If we got multiple matches, ask the user which one to use. Don't do this
4100 interactive thing during completion, though, as the purpose of the
4101 completion is providing a list of all possible matches. Prompting the
4102 user to filter it down would be completely unexpected in this case. */
4105 else if (m
> 1 && !parse_completion
)
4107 gdb_printf (_("Multiple matches for %s\n"), name
);
4109 user_select_syms (syms
, 1);
4115 /* Type-class predicates */
4117 /* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
4121 numeric_type_p (struct type
*type
)
4127 switch (type
->code ())
4131 case TYPE_CODE_FIXED_POINT
:
4133 case TYPE_CODE_RANGE
:
4134 return (type
== type
->target_type ()
4135 || numeric_type_p (type
->target_type ()));
4142 /* True iff TYPE is integral (an INT or RANGE of INTs). */
4145 integer_type_p (struct type
*type
)
4151 switch (type
->code ())
4155 case TYPE_CODE_RANGE
:
4156 return (type
== type
->target_type ()
4157 || integer_type_p (type
->target_type ()));
4164 /* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
4167 scalar_type_p (struct type
*type
)
4173 switch (type
->code ())
4176 case TYPE_CODE_RANGE
:
4177 case TYPE_CODE_ENUM
:
4179 case TYPE_CODE_FIXED_POINT
:
4187 /* True iff TYPE is discrete, as defined in the Ada Reference Manual.
4188 This essentially means one of (INT, RANGE, ENUM) -- but note that
4189 "enum" includes character and boolean as well. */
4192 discrete_type_p (struct type
*type
)
4198 switch (type
->code ())
4201 case TYPE_CODE_RANGE
:
4202 case TYPE_CODE_ENUM
:
4203 case TYPE_CODE_BOOL
:
4204 case TYPE_CODE_CHAR
:
4212 /* Returns non-zero if OP with operands in the vector ARGS could be
4213 a user-defined function. Errs on the side of pre-defined operators
4214 (i.e., result 0). */
4217 possible_user_operator_p (enum exp_opcode op
, struct value
*args
[])
4219 struct type
*type0
=
4220 (args
[0] == NULL
) ? NULL
: ada_check_typedef (args
[0]->type ());
4221 struct type
*type1
=
4222 (args
[1] == NULL
) ? NULL
: ada_check_typedef (args
[1]->type ());
4236 return (!(numeric_type_p (type0
) && numeric_type_p (type1
)));
4240 case BINOP_BITWISE_AND
:
4241 case BINOP_BITWISE_IOR
:
4242 case BINOP_BITWISE_XOR
:
4243 return (!(integer_type_p (type0
) && integer_type_p (type1
)));
4246 case BINOP_NOTEQUAL
:
4251 return (!(scalar_type_p (type0
) && scalar_type_p (type1
)));
4254 return !ada_is_array_type (type0
) || !ada_is_array_type (type1
);
4257 return (!(numeric_type_p (type0
) && integer_type_p (type1
)));
4261 case UNOP_LOGICAL_NOT
:
4263 return (!numeric_type_p (type0
));
4272 1. In the following, we assume that a renaming type's name may
4273 have an ___XD suffix. It would be nice if this went away at some
4275 2. We handle both the (old) purely type-based representation of
4276 renamings and the (new) variable-based encoding. At some point,
4277 it is devoutly to be hoped that the former goes away
4278 (FIXME: hilfinger-2007-07-09).
4279 3. Subprogram renamings are not implemented, although the XRS
4280 suffix is recognized (FIXME: hilfinger-2007-07-09). */
4282 /* If SYM encodes a renaming,
4284 <renaming> renames <renamed entity>,
4286 sets *LEN to the length of the renamed entity's name,
4287 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
4288 the string describing the subcomponent selected from the renamed
4289 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
4290 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
4291 are undefined). Otherwise, returns a value indicating the category
4292 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
4293 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
4294 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
4295 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
4296 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
4297 may be NULL, in which case they are not assigned.
4299 [Currently, however, GCC does not generate subprogram renamings.] */
4301 enum ada_renaming_category
4302 ada_parse_renaming (struct symbol
*sym
,
4303 const char **renamed_entity
, int *len
,
4304 const char **renaming_expr
)
4306 enum ada_renaming_category kind
;
4311 return ADA_NOT_RENAMING
;
4312 switch (sym
->aclass ())
4315 return ADA_NOT_RENAMING
;
4319 case LOC_OPTIMIZED_OUT
:
4320 info
= strstr (sym
->linkage_name (), "___XR");
4322 return ADA_NOT_RENAMING
;
4326 kind
= ADA_OBJECT_RENAMING
;
4330 kind
= ADA_EXCEPTION_RENAMING
;
4334 kind
= ADA_PACKAGE_RENAMING
;
4338 kind
= ADA_SUBPROGRAM_RENAMING
;
4342 return ADA_NOT_RENAMING
;
4346 if (renamed_entity
!= NULL
)
4347 *renamed_entity
= info
;
4348 suffix
= strstr (info
, "___XE");
4349 if (suffix
== NULL
|| suffix
== info
)
4350 return ADA_NOT_RENAMING
;
4352 *len
= strlen (info
) - strlen (suffix
);
4354 if (renaming_expr
!= NULL
)
4355 *renaming_expr
= suffix
;
4359 /* Compute the value of the given RENAMING_SYM, which is expected to
4360 be a symbol encoding a renaming expression. BLOCK is the block
4361 used to evaluate the renaming. */
4363 static struct value
*
4364 ada_read_renaming_var_value (struct symbol
*renaming_sym
,
4365 const struct block
*block
)
4367 const char *sym_name
;
4369 sym_name
= renaming_sym
->linkage_name ();
4370 expression_up expr
= parse_exp_1 (&sym_name
, 0, block
, 0);
4371 return expr
->evaluate ();
4375 /* Evaluation: Function Calls */
4377 /* Return an lvalue containing the value VAL. This is the identity on
4378 lvalues, and otherwise has the side-effect of allocating memory
4379 in the inferior where a copy of the value contents is copied. */
4381 static struct value
*
4382 ensure_lval (struct value
*val
)
4384 if (val
->lval () == not_lval
4385 || val
->lval () == lval_internalvar
)
4387 int len
= ada_check_typedef (val
->type ())->length ();
4388 const CORE_ADDR addr
=
4389 value_as_long (value_allocate_space_in_inferior (len
));
4391 val
->set_lval (lval_memory
);
4392 val
->set_address (addr
);
4393 write_memory (addr
, val
->contents ().data (), len
);
4399 /* Given ARG, a value of type (pointer or reference to a)*
4400 structure/union, extract the component named NAME from the ultimate
4401 target structure/union and return it as a value with its
4404 The routine searches for NAME among all members of the structure itself
4405 and (recursively) among all members of any wrapper members
4408 If NO_ERR, then simply return NULL in case of error, rather than
4411 static struct value
*
4412 ada_value_struct_elt (struct value
*arg
, const char *name
, int no_err
)
4414 struct type
*t
, *t1
;
4419 t1
= t
= ada_check_typedef (arg
->type ());
4420 if (t
->code () == TYPE_CODE_REF
)
4422 t1
= t
->target_type ();
4425 t1
= ada_check_typedef (t1
);
4426 if (t1
->code () == TYPE_CODE_PTR
)
4428 arg
= coerce_ref (arg
);
4433 while (t
->code () == TYPE_CODE_PTR
)
4435 t1
= t
->target_type ();
4438 t1
= ada_check_typedef (t1
);
4439 if (t1
->code () == TYPE_CODE_PTR
)
4441 arg
= value_ind (arg
);
4448 if (t1
->code () != TYPE_CODE_STRUCT
&& t1
->code () != TYPE_CODE_UNION
)
4452 v
= ada_search_struct_field (name
, arg
, 0, t
);
4455 int bit_offset
, bit_size
, byte_offset
;
4456 struct type
*field_type
;
4459 if (t
->code () == TYPE_CODE_PTR
)
4460 address
= ada_value_ind (arg
)->address ();
4462 address
= ada_coerce_ref (arg
)->address ();
4464 /* Check to see if this is a tagged type. We also need to handle
4465 the case where the type is a reference to a tagged type, but
4466 we have to be careful to exclude pointers to tagged types.
4467 The latter should be shown as usual (as a pointer), whereas
4468 a reference should mostly be transparent to the user. */
4470 if (ada_is_tagged_type (t1
, 0)
4471 || (t1
->code () == TYPE_CODE_REF
4472 && ada_is_tagged_type (t1
->target_type (), 0)))
4474 /* We first try to find the searched field in the current type.
4475 If not found then let's look in the fixed type. */
4477 if (!find_struct_field (name
, t1
, 0,
4478 nullptr, nullptr, nullptr,
4487 /* Convert to fixed type in all cases, so that we have proper
4488 offsets to each field in unconstrained record types. */
4489 t1
= ada_to_fixed_type (ada_get_base_type (t1
), NULL
,
4490 address
, NULL
, check_tag
);
4492 /* Resolve the dynamic type as well. */
4493 arg
= value_from_contents_and_address (t1
, nullptr, address
);
4496 if (find_struct_field (name
, t1
, 0,
4497 &field_type
, &byte_offset
, &bit_offset
,
4502 if (t
->code () == TYPE_CODE_REF
)
4503 arg
= ada_coerce_ref (arg
);
4505 arg
= ada_value_ind (arg
);
4506 v
= ada_value_primitive_packed_val (arg
, NULL
, byte_offset
,
4507 bit_offset
, bit_size
,
4511 v
= value_at_lazy (field_type
, address
+ byte_offset
);
4515 if (v
!= NULL
|| no_err
)
4518 error (_("There is no member named %s."), name
);
4524 error (_("Attempt to extract a component of "
4525 "a value that is not a record."));
4528 /* Return the value ACTUAL, converted to be an appropriate value for a
4529 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
4530 allocating any necessary descriptors (fat pointers), or copies of
4531 values not residing in memory, updating it as needed. */
4534 ada_convert_actual (struct value
*actual
, struct type
*formal_type0
)
4536 struct type
*actual_type
= ada_check_typedef (actual
->type ());
4537 struct type
*formal_type
= ada_check_typedef (formal_type0
);
4538 struct type
*formal_target
=
4539 formal_type
->code () == TYPE_CODE_PTR
4540 ? ada_check_typedef (formal_type
->target_type ()) : formal_type
;
4541 struct type
*actual_target
=
4542 actual_type
->code () == TYPE_CODE_PTR
4543 ? ada_check_typedef (actual_type
->target_type ()) : actual_type
;
4545 if (ada_is_array_descriptor_type (formal_target
)
4546 && actual_target
->code () == TYPE_CODE_ARRAY
)
4547 return make_array_descriptor (formal_type
, actual
);
4548 else if (formal_type
->code () == TYPE_CODE_PTR
4549 || formal_type
->code () == TYPE_CODE_REF
)
4551 struct value
*result
;
4553 if (formal_target
->code () == TYPE_CODE_ARRAY
4554 && ada_is_array_descriptor_type (actual_target
))
4555 result
= desc_data (actual
);
4556 else if (formal_type
->code () != TYPE_CODE_PTR
)
4558 if (actual
->lval () != lval_memory
)
4562 actual_type
= ada_check_typedef (actual
->type ());
4563 val
= value::allocate (actual_type
);
4564 copy (actual
->contents (), val
->contents_raw ());
4565 actual
= ensure_lval (val
);
4567 result
= value_addr (actual
);
4571 return value_cast_pointers (formal_type
, result
, 0);
4573 else if (actual_type
->code () == TYPE_CODE_PTR
)
4574 return ada_value_ind (actual
);
4575 else if (ada_is_aligner_type (formal_type
))
4577 /* We need to turn this parameter into an aligner type
4579 struct value
*aligner
= value::allocate (formal_type
);
4580 struct value
*component
= ada_value_struct_elt (aligner
, "F", 0);
4582 value_assign_to_component (aligner
, component
, actual
);
4589 /* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4590 type TYPE. This is usually an inefficient no-op except on some targets
4591 (such as AVR) where the representation of a pointer and an address
4595 value_pointer (struct value
*value
, struct type
*type
)
4597 unsigned len
= type
->length ();
4598 gdb_byte
*buf
= (gdb_byte
*) alloca (len
);
4601 addr
= value
->address ();
4602 gdbarch_address_to_pointer (type
->arch (), type
, buf
, addr
);
4603 addr
= extract_unsigned_integer (buf
, len
, type_byte_order (type
));
4608 /* Push a descriptor of type TYPE for array value ARR on the stack at
4609 *SP, updating *SP to reflect the new descriptor. Return either
4610 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4611 to-descriptor type rather than a descriptor type), a struct value *
4612 representing a pointer to this descriptor. */
4614 static struct value
*
4615 make_array_descriptor (struct type
*type
, struct value
*arr
)
4617 struct type
*bounds_type
= desc_bounds_type (type
);
4618 struct type
*desc_type
= desc_base_type (type
);
4619 struct value
*descriptor
= value::allocate (desc_type
);
4620 struct value
*bounds
= value::allocate (bounds_type
);
4623 for (i
= ada_array_arity (ada_check_typedef (arr
->type ()));
4626 modify_field (bounds
->type (),
4627 bounds
->contents_writeable ().data (),
4628 ada_array_bound (arr
, i
, 0),
4629 desc_bound_bitpos (bounds_type
, i
, 0),
4630 desc_bound_bitsize (bounds_type
, i
, 0));
4631 modify_field (bounds
->type (),
4632 bounds
->contents_writeable ().data (),
4633 ada_array_bound (arr
, i
, 1),
4634 desc_bound_bitpos (bounds_type
, i
, 1),
4635 desc_bound_bitsize (bounds_type
, i
, 1));
4638 bounds
= ensure_lval (bounds
);
4640 modify_field (descriptor
->type (),
4641 descriptor
->contents_writeable ().data (),
4642 value_pointer (ensure_lval (arr
),
4643 desc_type
->field (0).type ()),
4644 fat_pntr_data_bitpos (desc_type
),
4645 fat_pntr_data_bitsize (desc_type
));
4647 modify_field (descriptor
->type (),
4648 descriptor
->contents_writeable ().data (),
4649 value_pointer (bounds
,
4650 desc_type
->field (1).type ()),
4651 fat_pntr_bounds_bitpos (desc_type
),
4652 fat_pntr_bounds_bitsize (desc_type
));
4654 descriptor
= ensure_lval (descriptor
);
4656 if (type
->code () == TYPE_CODE_PTR
)
4657 return value_addr (descriptor
);
4662 /* Symbol Cache Module */
4664 /* Performance measurements made as of 2010-01-15 indicate that
4665 this cache does bring some noticeable improvements. Depending
4666 on the type of entity being printed, the cache can make it as much
4667 as an order of magnitude faster than without it.
4669 The descriptive type DWARF extension has significantly reduced
4670 the need for this cache, at least when DWARF is being used. However,
4671 even in this case, some expensive name-based symbol searches are still
4672 sometimes necessary - to find an XVZ variable, mostly. */
4674 /* See ada-lang.h. */
4677 ada_clear_symbol_cache (program_space
*pspace
)
4679 ada_pspace_data_handle
.clear (pspace
);
4682 /* Search the symbol cache for an entry matching NAME and DOMAIN.
4683 Return 1 if found, 0 otherwise.
4685 If an entry was found and SYM is not NULL, set *SYM to the entry's
4686 SYM. Same principle for BLOCK if not NULL. */
4689 lookup_cached_symbol (const char *name
, domain_search_flags domain
,
4690 struct symbol
**sym
, const struct block
**block
)
4692 htab_t tab
= get_ada_pspace_data (current_program_space
);
4693 cache_entry_search search
;
4695 search
.domain
= domain
;
4697 cache_entry
*e
= (cache_entry
*) htab_find_with_hash (tab
, &search
,
4703 if (block
!= nullptr)
4708 /* Assuming that (SYM, BLOCK) is the result of the lookup of NAME
4709 in domain DOMAIN, save this result in our symbol cache. */
4712 cache_symbol (const char *name
, domain_search_flags domain
,
4713 struct symbol
*sym
, const struct block
*block
)
4715 /* Symbols for builtin types don't have a block.
4716 For now don't cache such symbols. */
4717 if (sym
!= NULL
&& !sym
->is_objfile_owned ())
4720 /* If the symbol is a local symbol, then do not cache it, as a search
4721 for that symbol depends on the context. To determine whether
4722 the symbol is local or not, we check the block where we found it
4723 against the global and static blocks of its associated symtab. */
4726 const blockvector
&bv
= *sym
->symtab ()->compunit ()->blockvector ();
4728 if (bv
.global_block () != block
&& bv
.static_block () != block
)
4732 htab_t tab
= get_ada_pspace_data (current_program_space
);
4733 cache_entry_search search
;
4735 search
.domain
= domain
;
4737 void **slot
= htab_find_slot_with_hash (tab
, &search
,
4738 search
.hash (), INSERT
);
4740 cache_entry
*e
= new cache_entry
;
4751 /* Return the symbol name match type that should be used used when
4752 searching for all symbols matching LOOKUP_NAME.
4754 LOOKUP_NAME is expected to be a symbol name after transformation
4757 static symbol_name_match_type
4758 name_match_type_from_name (const char *lookup_name
)
4760 return (strstr (lookup_name
, "__") == NULL
4761 ? symbol_name_match_type::WILD
4762 : symbol_name_match_type::FULL
);
4765 /* Return the result of a standard (literal, C-like) lookup of NAME in
4766 given DOMAIN, visible from lexical block BLOCK. */
4768 static struct symbol
*
4769 standard_lookup (const char *name
, const struct block
*block
,
4770 domain_search_flags domain
)
4772 /* Initialize it just to avoid a GCC false warning. */
4773 struct block_symbol sym
= {};
4775 if (lookup_cached_symbol (name
, domain
, &sym
.symbol
, NULL
))
4777 sym
= ada_lookup_encoded_symbol (name
, block
, domain
);
4778 cache_symbol (name
, domain
, sym
.symbol
, sym
.block
);
4783 /* Non-zero iff there is at least one non-function/non-enumeral symbol
4784 in the symbol fields of SYMS. We treat enumerals as functions,
4785 since they contend in overloading in the same way. */
4787 is_nonfunction (const std::vector
<struct block_symbol
> &syms
)
4789 for (const block_symbol
&sym
: syms
)
4790 if (sym
.symbol
->type ()->code () != TYPE_CODE_FUNC
4791 && (sym
.symbol
->type ()->code () != TYPE_CODE_ENUM
4792 || sym
.symbol
->aclass () != LOC_CONST
))
4798 /* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4799 struct types. Otherwise, they may not. */
4802 equiv_types (struct type
*type0
, struct type
*type1
)
4806 if (type0
== NULL
|| type1
== NULL
4807 || type0
->code () != type1
->code ())
4809 if ((type0
->code () == TYPE_CODE_STRUCT
4810 || type0
->code () == TYPE_CODE_ENUM
)
4811 && ada_type_name (type0
) != NULL
&& ada_type_name (type1
) != NULL
4812 && strcmp (ada_type_name (type0
), ada_type_name (type1
)) == 0)
4818 /* True iff SYM0 represents the same entity as SYM1, or one that is
4819 no more defined than that of SYM1. */
4822 lesseq_defined_than (struct symbol
*sym0
, struct symbol
*sym1
)
4826 if (sym0
->domain () != sym1
->domain ()
4827 || sym0
->aclass () != sym1
->aclass ())
4830 switch (sym0
->aclass ())
4836 struct type
*type0
= sym0
->type ();
4837 struct type
*type1
= sym1
->type ();
4838 const char *name0
= sym0
->linkage_name ();
4839 const char *name1
= sym1
->linkage_name ();
4840 int len0
= strlen (name0
);
4843 type0
->code () == type1
->code ()
4844 && (equiv_types (type0
, type1
)
4845 || (len0
< strlen (name1
) && strncmp (name0
, name1
, len0
) == 0
4846 && startswith (name1
+ len0
, "___XV")));
4849 return sym0
->value_longest () == sym1
->value_longest ()
4850 && equiv_types (sym0
->type (), sym1
->type ());
4854 const char *name0
= sym0
->linkage_name ();
4855 const char *name1
= sym1
->linkage_name ();
4856 return (strcmp (name0
, name1
) == 0
4857 && sym0
->value_address () == sym1
->value_address ());
4865 /* Append (SYM,BLOCK) to the end of the array of struct block_symbol
4866 records in RESULT. Do nothing if SYM is a duplicate. */
4869 add_defn_to_vec (std::vector
<struct block_symbol
> &result
,
4871 const struct block
*block
)
4873 /* Do not try to complete stub types, as the debugger is probably
4874 already scanning all symbols matching a certain name at the
4875 time when this function is called. Trying to replace the stub
4876 type by its associated full type will cause us to restart a scan
4877 which may lead to an infinite recursion. Instead, the client
4878 collecting the matching symbols will end up collecting several
4879 matches, with at least one of them complete. It can then filter
4880 out the stub ones if needed. */
4882 for (int i
= result
.size () - 1; i
>= 0; i
-= 1)
4884 if (lesseq_defined_than (sym
, result
[i
].symbol
))
4886 else if (lesseq_defined_than (result
[i
].symbol
, sym
))
4888 result
[i
].symbol
= sym
;
4889 result
[i
].block
= block
;
4894 struct block_symbol info
;
4897 result
.push_back (info
);
4900 /* Return a bound minimal symbol matching NAME according to Ada
4901 decoding rules. Returns an invalid symbol if there is no such
4902 minimal symbol. Names prefixed with "standard__" are handled
4903 specially: "standard__" is first stripped off, and only static and
4904 global symbols are searched. */
4906 bound_minimal_symbol
4907 ada_lookup_simple_minsym (const char *name
, struct objfile
*objfile
)
4909 bound_minimal_symbol result
;
4911 symbol_name_match_type match_type
= name_match_type_from_name (name
);
4912 lookup_name_info
lookup_name (name
, match_type
);
4914 symbol_name_matcher_ftype
*match_name
4915 = ada_get_symbol_name_matcher (lookup_name
);
4917 gdbarch_iterate_over_objfiles_in_search_order
4918 (objfile
!= NULL
? objfile
->arch () : current_inferior ()->arch (),
4919 [&result
, lookup_name
, match_name
] (struct objfile
*obj
)
4921 for (minimal_symbol
*msymbol
: obj
->msymbols ())
4923 if (match_name (msymbol
->linkage_name (), lookup_name
, nullptr)
4924 && msymbol
->type () != mst_solib_trampoline
)
4926 result
.minsym
= msymbol
;
4927 result
.objfile
= obj
;
4938 /* True if TYPE is definitely an artificial type supplied to a symbol
4939 for which no debugging information was given in the symbol file. */
4942 is_nondebugging_type (struct type
*type
)
4944 const char *name
= ada_type_name (type
);
4946 return (name
!= NULL
&& strcmp (name
, "<variable, no debug info>") == 0);
4949 /* Return true if TYPE1 and TYPE2 are two enumeration types
4950 that are deemed "identical" for practical purposes.
4952 This function assumes that TYPE1 and TYPE2 are both TYPE_CODE_ENUM
4956 ada_identical_enum_types_p (struct type
*type1
, struct type
*type2
)
4958 /* The heuristic we use here is fairly conservative. We consider
4959 that 2 enumerate types are identical if they have the same
4960 number of enumerals and that all enumerals have the same
4961 underlying value and name. */
4963 if (type1
->num_fields () != type2
->num_fields ())
4966 /* All enums in the type should have an identical underlying value. */
4967 for (int i
= 0; i
< type1
->num_fields (); i
++)
4968 if (type1
->field (i
).loc_enumval () != type2
->field (i
).loc_enumval ())
4971 /* All enumerals should also have the same name (modulo any numerical
4973 for (int i
= 0; i
< type1
->num_fields (); i
++)
4975 const char *name_1
= type1
->field (i
).name ();
4976 const char *name_2
= type2
->field (i
).name ();
4977 int len_1
= strlen (name_1
);
4978 int len_2
= strlen (name_2
);
4980 ada_remove_trailing_digits (name_1
, &len_1
);
4981 ada_remove_trailing_digits (name_2
, &len_2
);
4982 if (len_1
!= len_2
|| strncmp (name_1
, name_2
, len_1
) != 0)
4989 /* Return nonzero if all the symbols in SYMS are all enumeral symbols
4990 that are deemed "identical" for practical purposes. Sometimes,
4991 enumerals are not strictly identical, but their types are so similar
4992 that they can be considered identical.
4994 For instance, consider the following code:
4996 type Color is (Black, Red, Green, Blue, White);
4997 type RGB_Color is new Color range Red .. Blue;
4999 Type RGB_Color is a subrange of an implicit type which is a copy
5000 of type Color. If we call that implicit type RGB_ColorB ("B" is
5001 for "Base Type"), then type RGB_ColorB is a copy of type Color.
5002 As a result, when an expression references any of the enumeral
5003 by name (Eg. "print green"), the expression is technically
5004 ambiguous and the user should be asked to disambiguate. But
5005 doing so would only hinder the user, since it wouldn't matter
5006 what choice he makes, the outcome would always be the same.
5007 So, for practical purposes, we consider them as the same. */
5010 symbols_are_identical_enums (const std::vector
<struct block_symbol
> &syms
)
5014 /* Before performing a thorough comparison check of each type,
5015 we perform a series of inexpensive checks. We expect that these
5016 checks will quickly fail in the vast majority of cases, and thus
5017 help prevent the unnecessary use of a more expensive comparison.
5018 Said comparison also expects us to make some of these checks
5019 (see ada_identical_enum_types_p). */
5021 /* Quick check: All symbols should have an enum type. */
5022 for (i
= 0; i
< syms
.size (); i
++)
5023 if (syms
[i
].symbol
->type ()->code () != TYPE_CODE_ENUM
)
5026 /* Quick check: They should all have the same value. */
5027 for (i
= 1; i
< syms
.size (); i
++)
5028 if (syms
[i
].symbol
->value_longest () != syms
[0].symbol
->value_longest ())
5031 /* All the sanity checks passed, so we might have a set of
5032 identical enumeration types. Perform a more complete
5033 comparison of the type of each symbol. */
5034 for (i
= 1; i
< syms
.size (); i
++)
5035 if (!ada_identical_enum_types_p (syms
[i
].symbol
->type (),
5036 syms
[0].symbol
->type ()))
5042 /* Remove any non-debugging symbols in SYMS that definitely
5043 duplicate other symbols in the list (The only case I know of where
5044 this happens is when object files containing stabs-in-ecoff are
5045 linked with files containing ordinary ecoff debugging symbols (or no
5046 debugging symbols)). Modifies SYMS to squeeze out deleted entries. */
5049 remove_extra_symbols (std::vector
<struct block_symbol
> &syms
)
5053 /* We should never be called with less than 2 symbols, as there
5054 cannot be any extra symbol in that case. But it's easy to
5055 handle, since we have nothing to do in that case. */
5056 if (syms
.size () < 2)
5060 while (i
< syms
.size ())
5062 bool remove_p
= false;
5064 /* If two symbols have the same name and one of them is a stub type,
5065 the get rid of the stub. */
5067 if (syms
[i
].symbol
->type ()->is_stub ()
5068 && syms
[i
].symbol
->linkage_name () != NULL
)
5070 for (j
= 0; !remove_p
&& j
< syms
.size (); j
++)
5073 && !syms
[j
].symbol
->type ()->is_stub ()
5074 && syms
[j
].symbol
->linkage_name () != NULL
5075 && strcmp (syms
[i
].symbol
->linkage_name (),
5076 syms
[j
].symbol
->linkage_name ()) == 0)
5081 /* Two symbols with the same name, same class and same address
5082 should be identical. */
5084 else if (syms
[i
].symbol
->linkage_name () != NULL
5085 && syms
[i
].symbol
->aclass () == LOC_STATIC
5086 && is_nondebugging_type (syms
[i
].symbol
->type ()))
5088 for (j
= 0; !remove_p
&& j
< syms
.size (); j
+= 1)
5091 && syms
[j
].symbol
->linkage_name () != NULL
5092 && strcmp (syms
[i
].symbol
->linkage_name (),
5093 syms
[j
].symbol
->linkage_name ()) == 0
5094 && (syms
[i
].symbol
->aclass ()
5095 == syms
[j
].symbol
->aclass ())
5096 && syms
[i
].symbol
->value_address ()
5097 == syms
[j
].symbol
->value_address ())
5102 /* Two functions with the same block are identical. */
5104 else if (syms
[i
].symbol
->aclass () == LOC_BLOCK
)
5106 for (j
= 0; !remove_p
&& j
< syms
.size (); j
+= 1)
5109 && syms
[j
].symbol
->aclass () == LOC_BLOCK
5110 && (syms
[i
].symbol
->value_block ()
5111 == syms
[j
].symbol
->value_block ()))
5117 syms
.erase (syms
.begin () + i
);
5123 /* Given a type that corresponds to a renaming entity, use the type name
5124 to extract the scope (package name or function name, fully qualified,
5125 and following the GNAT encoding convention) where this renaming has been
5129 xget_renaming_scope (struct type
*renaming_type
)
5131 /* The renaming types adhere to the following convention:
5132 <scope>__<rename>___<XR extension>.
5133 So, to extract the scope, we search for the "___XR" extension,
5134 and then backtrack until we find the first "__". */
5136 const char *name
= renaming_type
->name ();
5137 const char *suffix
= strstr (name
, "___XR");
5140 /* Now, backtrack a bit until we find the first "__". Start looking
5141 at suffix - 3, as the <rename> part is at least one character long. */
5143 for (last
= suffix
- 3; last
> name
; last
--)
5144 if (last
[0] == '_' && last
[1] == '_')
5147 /* Make a copy of scope and return it. */
5148 return std::string (name
, last
);
5151 /* Return nonzero if NAME corresponds to a package name. */
5154 is_package_name (const char *name
)
5156 /* Here, We take advantage of the fact that no symbols are generated
5157 for packages, while symbols are generated for each function.
5158 So the condition for NAME represent a package becomes equivalent
5159 to NAME not existing in our list of symbols. There is only one
5160 small complication with library-level functions (see below). */
5162 /* If it is a function that has not been defined at library level,
5163 then we should be able to look it up in the symbols. */
5164 if (standard_lookup (name
, NULL
, SEARCH_VFT
) != NULL
)
5167 /* Library-level function names start with "_ada_". See if function
5168 "_ada_" followed by NAME can be found. */
5170 /* Do a quick check that NAME does not contain "__", since library-level
5171 functions names cannot contain "__" in them. */
5172 if (strstr (name
, "__") != NULL
)
5175 std::string fun_name
= string_printf ("_ada_%s", name
);
5177 return (standard_lookup (fun_name
.c_str (), NULL
, SEARCH_VFT
) == NULL
);
5180 /* Return nonzero if SYM corresponds to a renaming entity that is
5181 not visible from FUNCTION_NAME. */
5184 old_renaming_is_invisible (const struct symbol
*sym
, const char *function_name
)
5186 if (sym
->aclass () != LOC_TYPEDEF
)
5189 std::string scope
= xget_renaming_scope (sym
->type ());
5191 /* If the rename has been defined in a package, then it is visible. */
5192 if (is_package_name (scope
.c_str ()))
5195 /* Check that the rename is in the current function scope by checking
5196 that its name starts with SCOPE. */
5198 /* If the function name starts with "_ada_", it means that it is
5199 a library-level function. Strip this prefix before doing the
5200 comparison, as the encoding for the renaming does not contain
5202 if (startswith (function_name
, "_ada_"))
5205 return !startswith (function_name
, scope
.c_str ());
5208 /* Remove entries from SYMS that corresponds to a renaming entity that
5209 is not visible from the function associated with CURRENT_BLOCK or
5210 that is superfluous due to the presence of more specific renaming
5211 information. Places surviving symbols in the initial entries of
5215 First, in cases where an object renaming is implemented as a
5216 reference variable, GNAT may produce both the actual reference
5217 variable and the renaming encoding. In this case, we discard the
5220 Second, GNAT emits a type following a specified encoding for each renaming
5221 entity. Unfortunately, STABS currently does not support the definition
5222 of types that are local to a given lexical block, so all renamings types
5223 are emitted at library level. As a consequence, if an application
5224 contains two renaming entities using the same name, and a user tries to
5225 print the value of one of these entities, the result of the ada symbol
5226 lookup will also contain the wrong renaming type.
5228 This function partially covers for this limitation by attempting to
5229 remove from the SYMS list renaming symbols that should be visible
5230 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
5231 method with the current information available. The implementation
5232 below has a couple of limitations (FIXME: brobecker-2003-05-12):
5234 - When the user tries to print a rename in a function while there
5235 is another rename entity defined in a package: Normally, the
5236 rename in the function has precedence over the rename in the
5237 package, so the latter should be removed from the list. This is
5238 currently not the case.
5240 - This function will incorrectly remove valid renames if
5241 the CURRENT_BLOCK corresponds to a function which symbol name
5242 has been changed by an "Export" pragma. As a consequence,
5243 the user will be unable to print such rename entities. */
5246 remove_irrelevant_renamings (std::vector
<struct block_symbol
> *syms
,
5247 const struct block
*current_block
)
5249 struct symbol
*current_function
;
5250 const char *current_function_name
;
5252 int is_new_style_renaming
;
5254 /* If there is both a renaming foo___XR... encoded as a variable and
5255 a simple variable foo in the same block, discard the latter.
5256 First, zero out such symbols, then compress. */
5257 is_new_style_renaming
= 0;
5258 for (i
= 0; i
< syms
->size (); i
+= 1)
5260 struct symbol
*sym
= (*syms
)[i
].symbol
;
5261 const struct block
*block
= (*syms
)[i
].block
;
5265 if (sym
== NULL
|| sym
->aclass () == LOC_TYPEDEF
)
5267 name
= sym
->linkage_name ();
5268 suffix
= strstr (name
, "___XR");
5272 int name_len
= suffix
- name
;
5275 is_new_style_renaming
= 1;
5276 for (j
= 0; j
< syms
->size (); j
+= 1)
5277 if (i
!= j
&& (*syms
)[j
].symbol
!= NULL
5278 && strncmp (name
, (*syms
)[j
].symbol
->linkage_name (),
5280 && block
== (*syms
)[j
].block
)
5281 (*syms
)[j
].symbol
= NULL
;
5284 if (is_new_style_renaming
)
5288 for (j
= k
= 0; j
< syms
->size (); j
+= 1)
5289 if ((*syms
)[j
].symbol
!= NULL
)
5291 (*syms
)[k
] = (*syms
)[j
];
5298 /* Extract the function name associated to CURRENT_BLOCK.
5299 Abort if unable to do so. */
5301 if (current_block
== NULL
)
5304 current_function
= current_block
->linkage_function ();
5305 if (current_function
== NULL
)
5308 current_function_name
= current_function
->linkage_name ();
5309 if (current_function_name
== NULL
)
5312 /* Check each of the symbols, and remove it from the list if it is
5313 a type corresponding to a renaming that is out of the scope of
5314 the current block. */
5317 while (i
< syms
->size ())
5319 if (ada_parse_renaming ((*syms
)[i
].symbol
, NULL
, NULL
, NULL
)
5320 == ADA_OBJECT_RENAMING
5321 && old_renaming_is_invisible ((*syms
)[i
].symbol
,
5322 current_function_name
))
5323 syms
->erase (syms
->begin () + i
);
5329 /* Add to RESULT all symbols from BLOCK (and its super-blocks)
5330 whose name and domain match LOOKUP_NAME and DOMAIN respectively.
5332 Note: This function assumes that RESULT is empty. */
5335 ada_add_local_symbols (std::vector
<struct block_symbol
> &result
,
5336 const lookup_name_info
&lookup_name
,
5337 const struct block
*block
, domain_search_flags domain
)
5339 while (block
!= NULL
)
5341 ada_add_block_symbols (result
, block
, lookup_name
, domain
, NULL
);
5343 /* If we found a non-function match, assume that's the one. We
5344 only check this when finding a function boundary, so that we
5345 can accumulate all results from intervening blocks first. */
5346 if (block
->function () != nullptr && is_nonfunction (result
))
5349 block
= block
->superblock ();
5353 /* An object of this type is used as the callback argument when
5354 calling the map_matching_symbols method. */
5358 explicit match_data (std::vector
<struct block_symbol
> *rp
)
5362 DISABLE_COPY_AND_ASSIGN (match_data
);
5364 bool operator() (struct block_symbol
*bsym
);
5366 struct objfile
*objfile
= nullptr;
5367 std::vector
<struct block_symbol
> *resultp
;
5368 struct symbol
*arg_sym
= nullptr;
5369 bool found_sym
= false;
5372 /* A callback for add_nonlocal_symbols that adds symbol, found in
5373 BSYM, to a list of symbols. */
5376 match_data::operator() (struct block_symbol
*bsym
)
5378 const struct block
*block
= bsym
->block
;
5379 struct symbol
*sym
= bsym
->symbol
;
5383 if (!found_sym
&& arg_sym
!= NULL
)
5384 add_defn_to_vec (*resultp
, arg_sym
, block
);
5390 if (sym
->aclass () == LOC_UNRESOLVED
)
5392 else if (sym
->is_argument ())
5397 add_defn_to_vec (*resultp
, sym
, block
);
5403 /* Helper for add_nonlocal_symbols. Find symbols in DOMAIN which are
5404 targeted by renamings matching LOOKUP_NAME in BLOCK. Add these
5405 symbols to RESULT. Return whether we found such symbols. */
5408 ada_add_block_renamings (std::vector
<struct block_symbol
> &result
,
5409 const struct block
*block
,
5410 const lookup_name_info
&lookup_name
,
5411 domain_search_flags domain
)
5413 int defns_mark
= result
.size ();
5415 symbol_name_matcher_ftype
*name_match
5416 = ada_get_symbol_name_matcher (lookup_name
);
5418 for (using_direct
*renaming
: block
->get_using ())
5422 /* Avoid infinite recursions: skip this renaming if we are actually
5423 already traversing it.
5425 Currently, symbol lookup in Ada don't use the namespace machinery from
5426 C++/Fortran support: skip namespace imports that use them. */
5427 if (renaming
->searched
5428 || (renaming
->import_src
!= NULL
5429 && renaming
->import_src
[0] != '\0')
5430 || (renaming
->import_dest
!= NULL
5431 && renaming
->import_dest
[0] != '\0'))
5433 renaming
->searched
= 1;
5435 /* TODO: here, we perform another name-based symbol lookup, which can
5436 pull its own multiple overloads. In theory, we should be able to do
5437 better in this case since, in DWARF, DW_AT_import is a DIE reference,
5438 not a simple name. But in order to do this, we would need to enhance
5439 the DWARF reader to associate a symbol to this renaming, instead of a
5440 name. So, for now, we do something simpler: re-use the C++/Fortran
5441 namespace machinery. */
5442 r_name
= (renaming
->alias
!= NULL
5444 : renaming
->declaration
);
5445 if (name_match (r_name
, lookup_name
, NULL
))
5447 lookup_name_info
decl_lookup_name (renaming
->declaration
,
5448 lookup_name
.match_type ());
5449 ada_add_all_symbols (result
, block
, decl_lookup_name
, domain
,
5452 renaming
->searched
= 0;
5454 return result
.size () != defns_mark
;
5457 /* Convenience function to get at the Ada encoded lookup name for
5458 LOOKUP_NAME, as a C string. */
5461 ada_lookup_name (const lookup_name_info
&lookup_name
)
5463 return lookup_name
.ada ().lookup_name ().c_str ();
5466 /* A helper for add_nonlocal_symbols. Expand all necessary symtabs
5467 for OBJFILE, then walk the objfile's symtabs and update the
5471 map_matching_symbols (struct objfile
*objfile
,
5472 const lookup_name_info
&lookup_name
,
5473 domain_search_flags domain
,
5477 data
.objfile
= objfile
;
5478 objfile
->expand_symtabs_matching (nullptr, &lookup_name
,
5481 ? SEARCH_GLOBAL_BLOCK
5482 : SEARCH_STATIC_BLOCK
,
5485 const int block_kind
= global
? GLOBAL_BLOCK
: STATIC_BLOCK
;
5486 for (compunit_symtab
*symtab
: objfile
->compunits ())
5488 const struct block
*block
5489 = symtab
->blockvector ()->block (block_kind
);
5490 if (!iterate_over_symbols_terminated (block
, lookup_name
,
5496 /* Add to RESULT all non-local symbols whose name and domain match
5497 LOOKUP_NAME and DOMAIN respectively. The search is performed on
5498 GLOBAL_BLOCK symbols if GLOBAL is non-zero, or on STATIC_BLOCK
5499 symbols otherwise. */
5502 add_nonlocal_symbols (std::vector
<struct block_symbol
> &result
,
5503 const lookup_name_info
&lookup_name
,
5504 domain_search_flags domain
, int global
)
5506 struct match_data
data (&result
);
5508 bool is_wild_match
= lookup_name
.ada ().wild_match_p ();
5510 for (objfile
*objfile
: current_program_space
->objfiles ())
5512 map_matching_symbols (objfile
, lookup_name
, domain
, global
, data
);
5514 for (compunit_symtab
*cu
: objfile
->compunits ())
5516 const struct block
*global_block
5517 = cu
->blockvector ()->global_block ();
5519 if (ada_add_block_renamings (result
, global_block
, lookup_name
,
5521 data
.found_sym
= true;
5525 if (result
.empty () && global
&& !is_wild_match
)
5527 const char *name
= ada_lookup_name (lookup_name
);
5528 std::string bracket_name
= std::string ("<_ada_") + name
+ '>';
5529 lookup_name_info
name1 (bracket_name
, symbol_name_match_type::FULL
);
5531 for (objfile
*objfile
: current_program_space
->objfiles ())
5532 map_matching_symbols (objfile
, name1
, domain
, global
, data
);
5536 /* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if
5537 FULL_SEARCH is non-zero, enclosing scope and in global scopes,
5538 returning the number of matches. Add these to RESULT.
5540 When FULL_SEARCH is non-zero, any non-function/non-enumeral
5541 symbol match within the nest of blocks whose innermost member is BLOCK,
5542 is the one match returned (no other matches in that or
5543 enclosing blocks is returned). If there are any matches in or
5544 surrounding BLOCK, then these alone are returned.
5546 Names prefixed with "standard__" are handled specially:
5547 "standard__" is first stripped off (by the lookup_name
5548 constructor), and only static and global symbols are searched.
5550 If MADE_GLOBAL_LOOKUP_P is non-null, set it before return to whether we had
5551 to lookup global symbols. */
5554 ada_add_all_symbols (std::vector
<struct block_symbol
> &result
,
5555 const struct block
*block
,
5556 const lookup_name_info
&lookup_name
,
5557 domain_search_flags domain
,
5559 int *made_global_lookup_p
)
5563 if (made_global_lookup_p
)
5564 *made_global_lookup_p
= 0;
5566 /* Special case: If the user specifies a symbol name inside package
5567 Standard, do a non-wild matching of the symbol name without
5568 the "standard__" prefix. This was primarily introduced in order
5569 to allow the user to specifically access the standard exceptions
5570 using, for instance, Standard.Constraint_Error when Constraint_Error
5571 is ambiguous (due to the user defining its own Constraint_Error
5572 entity inside its program). */
5573 if (lookup_name
.ada ().standard_p ())
5576 /* Check the non-global symbols. If we have ANY match, then we're done. */
5581 ada_add_local_symbols (result
, lookup_name
, block
, domain
);
5584 /* In the !full_search case we're are being called by
5585 iterate_over_symbols, and we don't want to search
5587 ada_add_block_symbols (result
, block
, lookup_name
, domain
, NULL
);
5589 if (!result
.empty () || !full_search
)
5593 /* No non-global symbols found. Check our cache to see if we have
5594 already performed this search before. If we have, then return
5597 if (lookup_cached_symbol (ada_lookup_name (lookup_name
),
5598 domain
, &sym
, &block
))
5601 add_defn_to_vec (result
, sym
, block
);
5605 if (made_global_lookup_p
)
5606 *made_global_lookup_p
= 1;
5608 /* Search symbols from all global blocks. */
5610 add_nonlocal_symbols (result
, lookup_name
, domain
, 1);
5612 /* Now add symbols from all per-file blocks if we've gotten no hits
5613 (not strictly correct, but perhaps better than an error). */
5615 if (result
.empty ())
5616 add_nonlocal_symbols (result
, lookup_name
, domain
, 0);
5619 /* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if FULL_SEARCH
5620 is non-zero, enclosing scope and in global scopes.
5622 Returns (SYM,BLOCK) tuples, indicating the symbols found and the
5623 blocks and symbol tables (if any) in which they were found.
5625 When full_search is non-zero, any non-function/non-enumeral
5626 symbol match within the nest of blocks whose innermost member is BLOCK,
5627 is the one match returned (no other matches in that or
5628 enclosing blocks is returned). If there are any matches in or
5629 surrounding BLOCK, then these alone are returned.
5631 Names prefixed with "standard__" are handled specially: "standard__"
5632 is first stripped off, and only static and global symbols are searched. */
5634 static std::vector
<struct block_symbol
>
5635 ada_lookup_symbol_list_worker (const lookup_name_info
&lookup_name
,
5636 const struct block
*block
,
5637 domain_search_flags domain
,
5640 int syms_from_global_search
;
5641 std::vector
<struct block_symbol
> results
;
5643 ada_add_all_symbols (results
, block
, lookup_name
,
5644 domain
, full_search
, &syms_from_global_search
);
5646 remove_extra_symbols (results
);
5648 if (results
.empty () && full_search
&& syms_from_global_search
)
5649 cache_symbol (ada_lookup_name (lookup_name
), domain
, NULL
, NULL
);
5651 if (results
.size () == 1 && full_search
&& syms_from_global_search
)
5652 cache_symbol (ada_lookup_name (lookup_name
), domain
,
5653 results
[0].symbol
, results
[0].block
);
5655 remove_irrelevant_renamings (&results
, block
);
5659 /* Find symbols in DOMAIN matching NAME, in BLOCK and enclosing scope and
5660 in global scopes, returning (SYM,BLOCK) tuples.
5662 See ada_lookup_symbol_list_worker for further details. */
5664 std::vector
<struct block_symbol
>
5665 ada_lookup_symbol_list (const char *name
, const struct block
*block
,
5666 domain_search_flags domain
)
5668 symbol_name_match_type name_match_type
= name_match_type_from_name (name
);
5669 lookup_name_info
lookup_name (name
, name_match_type
);
5671 return ada_lookup_symbol_list_worker (lookup_name
, block
, domain
, 1);
5674 /* The result is as for ada_lookup_symbol_list with FULL_SEARCH set
5675 to 1, but choosing the first symbol found if there are multiple
5679 ada_lookup_encoded_symbol (const char *name
, const struct block
*block
,
5680 domain_search_flags domain
)
5682 /* Since we already have an encoded name, wrap it in '<>' to force a
5683 verbatim match. Otherwise, if the name happens to not look like
5684 an encoded name (because it doesn't include a "__"),
5685 ada_lookup_name_info would re-encode/fold it again, and that
5686 would e.g., incorrectly lowercase object renaming names like
5687 "R28b" -> "r28b". */
5688 std::string verbatim
= add_angle_brackets (name
);
5689 return ada_lookup_symbol (verbatim
.c_str (), block
, domain
);
5692 /* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
5693 scope and in global scopes, or NULL if none. NAME is folded and
5694 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
5695 choosing the first symbol if there are multiple choices. */
5698 ada_lookup_symbol (const char *name
, const struct block
*block0
,
5699 domain_search_flags domain
)
5701 std::vector
<struct block_symbol
> candidates
5702 = ada_lookup_symbol_list (name
, block0
, domain
);
5704 if (candidates
.empty ())
5707 return candidates
[0];
5711 /* True iff STR is a possible encoded suffix of a normal Ada name
5712 that is to be ignored for matching purposes. Suffixes of parallel
5713 names (e.g., XVE) are not included here. Currently, the possible suffixes
5714 are given by any of the regular expressions:
5716 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
5717 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
5718 TKB [subprogram suffix for task bodies]
5719 _E[0-9]+[bs]$ [protected object entry suffixes]
5720 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
5722 Also, any leading "__[0-9]+" sequence is skipped before the suffix
5723 match is performed. This sequence is used to differentiate homonyms,
5724 is an optional part of a valid name suffix. */
5727 is_name_suffix (const char *str
)
5730 const char *matching
;
5731 const int len
= strlen (str
);
5733 /* Skip optional leading __[0-9]+. */
5735 if (len
> 3 && str
[0] == '_' && str
[1] == '_' && isdigit (str
[2]))
5738 while (isdigit (str
[0]))
5744 if (str
[0] == '.' || str
[0] == '$')
5747 while (isdigit (matching
[0]))
5749 if (matching
[0] == '\0')
5755 if (len
> 3 && str
[0] == '_' && str
[1] == '_' && str
[2] == '_')
5758 while (isdigit (matching
[0]))
5760 if (matching
[0] == '\0')
5764 /* "TKB" suffixes are used for subprograms implementing task bodies. */
5766 if (strcmp (str
, "TKB") == 0)
5770 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
5771 with a N at the end. Unfortunately, the compiler uses the same
5772 convention for other internal types it creates. So treating
5773 all entity names that end with an "N" as a name suffix causes
5774 some regressions. For instance, consider the case of an enumerated
5775 type. To support the 'Image attribute, it creates an array whose
5777 Having a single character like this as a suffix carrying some
5778 information is a bit risky. Perhaps we should change the encoding
5779 to be something like "_N" instead. In the meantime, do not do
5780 the following check. */
5781 /* Protected Object Subprograms */
5782 if (len
== 1 && str
[0] == 'N')
5787 if (len
> 3 && str
[0] == '_' && str
[1] == 'E' && isdigit (str
[2]))
5790 while (isdigit (matching
[0]))
5792 if ((matching
[0] == 'b' || matching
[0] == 's')
5793 && matching
[1] == '\0')
5797 /* ??? We should not modify STR directly, as we are doing below. This
5798 is fine in this case, but may become problematic later if we find
5799 that this alternative did not work, and want to try matching
5800 another one from the beginning of STR. Since we modified it, we
5801 won't be able to find the beginning of the string anymore! */
5805 while (str
[0] != '_' && str
[0] != '\0')
5807 if (str
[0] != 'n' && str
[0] != 'b')
5813 if (str
[0] == '\000')
5818 if (str
[1] != '_' || str
[2] == '\000')
5822 if (strcmp (str
+ 3, "JM") == 0)
5824 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
5825 the LJM suffix in favor of the JM one. But we will
5826 still accept LJM as a valid suffix for a reasonable
5827 amount of time, just to allow ourselves to debug programs
5828 compiled using an older version of GNAT. */
5829 if (strcmp (str
+ 3, "LJM") == 0)
5833 if (str
[4] == 'F' || str
[4] == 'D' || str
[4] == 'B'
5834 || str
[4] == 'U' || str
[4] == 'P')
5836 if (str
[4] == 'R' && str
[5] != 'T')
5840 if (!isdigit (str
[2]))
5842 for (k
= 3; str
[k
] != '\0'; k
+= 1)
5843 if (!isdigit (str
[k
]) && str
[k
] != '_')
5847 if (str
[0] == '$' && isdigit (str
[1]))
5849 for (k
= 2; str
[k
] != '\0'; k
+= 1)
5850 if (!isdigit (str
[k
]) && str
[k
] != '_')
5857 /* Return non-zero if the string starting at NAME and ending before
5858 NAME_END contains no capital letters. */
5861 is_valid_name_for_wild_match (const char *name0
)
5863 std::string decoded_name
= ada_decode (name0
);
5866 /* If the decoded name starts with an angle bracket, it means that
5867 NAME0 does not follow the GNAT encoding format. It should then
5868 not be allowed as a possible wild match. */
5869 if (decoded_name
[0] == '<')
5872 for (i
=0; decoded_name
[i
] != '\0'; i
++)
5873 if (isalpha (decoded_name
[i
]) && !islower (decoded_name
[i
]))
5879 /* Advance *NAMEP to next occurrence in the string NAME0 of the TARGET0
5880 character which could start a simple name. Assumes that *NAMEP points
5881 somewhere inside the string beginning at NAME0. */
5884 advance_wild_match (const char **namep
, const char *name0
, char target0
)
5886 const char *name
= *namep
;
5896 if ((t1
>= 'a' && t1
<= 'z') || (t1
>= '0' && t1
<= '9'))
5899 if (name
== name0
+ 5 && startswith (name0
, "_ada"))
5904 else if (t1
== '_' && ((name
[2] >= 'a' && name
[2] <= 'z')
5905 || name
[2] == target0
))
5910 else if (t1
== '_' && name
[2] == 'B' && name
[3] == '_')
5912 /* Names like "pkg__B_N__name", where N is a number, are
5913 block-local. We can handle these by simply skipping
5920 else if ((t0
>= 'a' && t0
<= 'z') || (t0
>= '0' && t0
<= '9'))
5930 /* Return true iff NAME encodes a name of the form prefix.PATN.
5931 Ignores any informational suffixes of NAME (i.e., for which
5932 is_name_suffix is true). Assumes that PATN is a lower-cased Ada
5936 wild_match (const char *name
, const char *patn
)
5939 const char *name0
= name
;
5941 if (startswith (name
, "___ghost_"))
5946 const char *match
= name
;
5950 for (name
+= 1, p
= patn
+ 1; *p
!= '\0'; name
+= 1, p
+= 1)
5953 if (*p
== '\0' && is_name_suffix (name
))
5954 return match
== name0
|| is_valid_name_for_wild_match (name0
);
5956 if (name
[-1] == '_')
5959 if (!advance_wild_match (&name
, name0
, *patn
))
5964 /* Add symbols from BLOCK matching LOOKUP_NAME in DOMAIN to RESULT (if
5965 necessary). OBJFILE is the section containing BLOCK. */
5968 ada_add_block_symbols (std::vector
<struct block_symbol
> &result
,
5969 const struct block
*block
,
5970 const lookup_name_info
&lookup_name
,
5971 domain_search_flags domain
, struct objfile
*objfile
)
5973 /* A matching argument symbol, if any. */
5974 struct symbol
*arg_sym
;
5975 /* Set true when we find a matching non-argument symbol. */
5980 for (struct symbol
*sym
: block_iterator_range (block
, &lookup_name
))
5982 if (sym
->matches (domain
))
5984 if (sym
->aclass () != LOC_UNRESOLVED
)
5986 if (sym
->is_argument ())
5991 add_defn_to_vec (result
, sym
, block
);
5997 /* Handle renamings. */
5999 if (ada_add_block_renamings (result
, block
, lookup_name
, domain
))
6002 if (!found_sym
&& arg_sym
!= NULL
)
6004 add_defn_to_vec (result
, arg_sym
, block
);
6007 if (!lookup_name
.ada ().wild_match_p ())
6011 const std::string
&ada_lookup_name
= lookup_name
.ada ().lookup_name ();
6012 const char *name
= ada_lookup_name
.c_str ();
6013 size_t name_len
= ada_lookup_name
.size ();
6015 for (struct symbol
*sym
: block_iterator_range (block
))
6017 if (sym
->matches (domain
))
6021 cmp
= (int) '_' - (int) sym
->linkage_name ()[0];
6024 cmp
= !startswith (sym
->linkage_name (), "_ada_");
6026 cmp
= strncmp (name
, sym
->linkage_name () + 5,
6031 && is_name_suffix (sym
->linkage_name () + name_len
+ 5))
6033 if (sym
->aclass () != LOC_UNRESOLVED
)
6035 if (sym
->is_argument ())
6040 add_defn_to_vec (result
, sym
, block
);
6047 /* NOTE: This really shouldn't be needed for _ada_ symbols.
6048 They aren't parameters, right? */
6049 if (!found_sym
&& arg_sym
!= NULL
)
6051 add_defn_to_vec (result
, arg_sym
, block
);
6057 /* Symbol Completion */
6062 ada_lookup_name_info::matches
6063 (const char *sym_name
,
6064 symbol_name_match_type match_type
,
6065 completion_match_result
*comp_match_res
) const
6068 const char *text
= m_encoded_name
.c_str ();
6069 size_t text_len
= m_encoded_name
.size ();
6071 /* First, test against the fully qualified name of the symbol. */
6073 if (strncmp (sym_name
, text
, text_len
) == 0)
6076 std::string decoded_name
= ada_decode (sym_name
);
6077 if (match
&& !m_encoded_p
)
6079 /* One needed check before declaring a positive match is to verify
6080 that iff we are doing a verbatim match, the decoded version
6081 of the symbol name starts with '<'. Otherwise, this symbol name
6082 is not a suitable completion. */
6084 bool has_angle_bracket
= (decoded_name
[0] == '<');
6085 match
= (has_angle_bracket
== m_verbatim_p
);
6088 if (match
&& !m_verbatim_p
)
6090 /* When doing non-verbatim match, another check that needs to
6091 be done is to verify that the potentially matching symbol name
6092 does not include capital letters, because the ada-mode would
6093 not be able to understand these symbol names without the
6094 angle bracket notation. */
6097 for (tmp
= sym_name
; *tmp
!= '\0' && !isupper (*tmp
); tmp
++);
6102 /* Second: Try wild matching... */
6104 if (!match
&& m_wild_match_p
)
6106 /* Since we are doing wild matching, this means that TEXT
6107 may represent an unqualified symbol name. We therefore must
6108 also compare TEXT against the unqualified name of the symbol. */
6109 sym_name
= ada_unqualified_name (decoded_name
.c_str ());
6111 if (strncmp (sym_name
, text
, text_len
) == 0)
6115 /* Finally: If we found a match, prepare the result to return. */
6120 if (comp_match_res
!= NULL
)
6122 std::string
&match_str
= comp_match_res
->match
.storage ();
6125 match_str
= ada_decode (sym_name
);
6129 match_str
= add_angle_brackets (sym_name
);
6131 match_str
= sym_name
;
6135 comp_match_res
->set_match (match_str
.c_str ());
6143 /* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
6144 for tagged types. */
6147 ada_is_dispatch_table_ptr_type (struct type
*type
)
6151 if (type
->code () != TYPE_CODE_PTR
)
6154 name
= type
->target_type ()->name ();
6158 return (strcmp (name
, "ada__tags__dispatch_table") == 0);
6161 /* Return non-zero if TYPE is an interface tag. */
6164 ada_is_interface_tag (struct type
*type
)
6166 const char *name
= type
->name ();
6171 return (strcmp (name
, "ada__tags__interface_tag") == 0);
6174 /* True if field number FIELD_NUM in struct or union type TYPE is supposed
6175 to be invisible to users. */
6178 ada_is_ignored_field (struct type
*type
, int field_num
)
6180 if (field_num
< 0 || field_num
> type
->num_fields ())
6183 /* Check the name of that field. */
6185 const char *name
= type
->field (field_num
).name ();
6187 /* Anonymous field names should not be printed.
6188 brobecker/2007-02-20: I don't think this can actually happen
6189 but we don't want to print the value of anonymous fields anyway. */
6193 /* Normally, fields whose name start with an underscore ("_")
6194 are fields that have been internally generated by the compiler,
6195 and thus should not be printed. The "_parent" field is special,
6196 however: This is a field internally generated by the compiler
6197 for tagged types, and it contains the components inherited from
6198 the parent type. This field should not be printed as is, but
6199 should not be ignored either. */
6200 if (name
[0] == '_' && !startswith (name
, "_parent"))
6203 /* The compiler doesn't document this, but sometimes it emits
6204 a field whose name starts with a capital letter, like 'V148s'.
6205 These aren't marked as artificial in any way, but we know they
6206 should be ignored. However, wrapper fields should not be
6208 if (name
[0] == 'S' || name
[0] == 'R' || name
[0] == 'O')
6210 /* Wrapper field. */
6212 else if (isupper (name
[0]))
6216 /* If this is the dispatch table of a tagged type or an interface tag,
6218 if (ada_is_tagged_type (type
, 1)
6219 && (ada_is_dispatch_table_ptr_type (type
->field (field_num
).type ())
6220 || ada_is_interface_tag (type
->field (field_num
).type ())))
6223 /* Not a special field, so it should not be ignored. */
6227 /* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
6228 pointer or reference type whose ultimate target has a tag field. */
6231 ada_is_tagged_type (struct type
*type
, int refok
)
6233 return (ada_lookup_struct_elt_type (type
, "_tag", refok
, 1) != NULL
);
6236 /* True iff TYPE represents the type of X'Tag */
6239 ada_is_tag_type (struct type
*type
)
6241 type
= ada_check_typedef (type
);
6243 if (type
== NULL
|| type
->code () != TYPE_CODE_PTR
)
6247 const char *name
= ada_type_name (type
->target_type ());
6249 return (name
!= NULL
6250 && strcmp (name
, "ada__tags__dispatch_table") == 0);
6254 /* The type of the tag on VAL. */
6256 static struct type
*
6257 ada_tag_type (struct value
*val
)
6259 return ada_lookup_struct_elt_type (val
->type (), "_tag", 1, 0);
6262 /* Return 1 if TAG follows the old scheme for Ada tags (used for Ada 95,
6263 retired at Ada 05). */
6266 is_ada95_tag (struct value
*tag
)
6268 return ada_value_struct_elt (tag
, "tsd", 1) != NULL
;
6271 /* The value of the tag on VAL. */
6273 static struct value
*
6274 ada_value_tag (struct value
*val
)
6276 return ada_value_struct_elt (val
, "_tag", 0);
6279 /* The value of the tag on the object of type TYPE whose contents are
6280 saved at VALADDR, if it is non-null, or is at memory address
6283 static struct value
*
6284 value_tag_from_contents_and_address (struct type
*type
,
6285 const gdb_byte
*valaddr
,
6288 int tag_byte_offset
;
6289 struct type
*tag_type
;
6291 gdb::array_view
<const gdb_byte
> contents
;
6292 if (valaddr
!= nullptr)
6293 contents
= gdb::make_array_view (valaddr
, type
->length ());
6294 struct type
*resolved_type
= resolve_dynamic_type (type
, contents
, address
);
6295 if (find_struct_field ("_tag", resolved_type
, 0, &tag_type
, &tag_byte_offset
,
6298 const gdb_byte
*valaddr1
= ((valaddr
== NULL
)
6300 : valaddr
+ tag_byte_offset
);
6301 CORE_ADDR address1
= (address
== 0) ? 0 : address
+ tag_byte_offset
;
6303 return value_from_contents_and_address (tag_type
, valaddr1
, address1
);
6308 static struct type
*
6309 type_from_tag (struct value
*tag
)
6311 gdb::unique_xmalloc_ptr
<char> type_name
= ada_tag_name (tag
);
6313 if (type_name
!= NULL
)
6314 return ada_find_any_type (ada_encode (type_name
.get ()).c_str ());
6318 /* Given a value OBJ of a tagged type, return a value of this
6319 type at the base address of the object. The base address, as
6320 defined in Ada.Tags, it is the address of the primary tag of
6321 the object, and therefore where the field values of its full
6322 view can be fetched. */
6325 ada_tag_value_at_base_address (struct value
*obj
)
6328 LONGEST offset_to_top
= 0;
6329 struct type
*ptr_type
, *obj_type
;
6331 CORE_ADDR base_address
;
6333 obj_type
= obj
->type ();
6335 /* It is the responsibility of the caller to deref pointers. */
6337 if (obj_type
->code () == TYPE_CODE_PTR
|| obj_type
->code () == TYPE_CODE_REF
)
6340 tag
= ada_value_tag (obj
);
6344 /* Base addresses only appeared with Ada 05 and multiple inheritance. */
6346 if (is_ada95_tag (tag
))
6349 struct type
*offset_type
6350 = language_lookup_primitive_type (language_def (language_ada
),
6351 current_inferior ()->arch (),
6353 ptr_type
= lookup_pointer_type (offset_type
);
6354 val
= value_cast (ptr_type
, tag
);
6358 /* It is perfectly possible that an exception be raised while
6359 trying to determine the base address, just like for the tag;
6360 see ada_tag_name for more details. We do not print the error
6361 message for the same reason. */
6365 offset_to_top
= value_as_long (value_ind (value_ptradd (val
, -2)));
6368 catch (const gdb_exception_error
&e
)
6373 /* If offset is null, nothing to do. */
6375 if (offset_to_top
== 0)
6378 /* -1 is a special case in Ada.Tags; however, what should be done
6379 is not quite clear from the documentation. So do nothing for
6382 if (offset_to_top
== -1)
6385 /* Storage_Offset'Last is used to indicate that a dynamic offset to
6386 top is used. In this situation the offset is stored just after
6387 the tag, in the object itself. */
6388 ULONGEST last
= (((ULONGEST
) 1) << (8 * offset_type
->length () - 1)) - 1;
6389 if (offset_to_top
== last
)
6391 struct value
*tem
= value_addr (tag
);
6392 tem
= value_ptradd (tem
, 1);
6393 tem
= value_cast (ptr_type
, tem
);
6394 offset_to_top
= value_as_long (value_ind (tem
));
6397 if (offset_to_top
> 0)
6399 /* OFFSET_TO_TOP used to be a positive value to be subtracted
6400 from the base address. This was however incompatible with
6401 C++ dispatch table: C++ uses a *negative* value to *add*
6402 to the base address. Ada's convention has therefore been
6403 changed in GNAT 19.0w 20171023: since then, C++ and Ada
6404 use the same convention. Here, we support both cases by
6405 checking the sign of OFFSET_TO_TOP. */
6406 offset_to_top
= -offset_to_top
;
6409 base_address
= obj
->address () + offset_to_top
;
6410 tag
= value_tag_from_contents_and_address (obj_type
, NULL
, base_address
);
6412 /* Make sure that we have a proper tag at the new address.
6413 Otherwise, offset_to_top is bogus (which can happen when
6414 the object is not initialized yet). */
6419 obj_type
= type_from_tag (tag
);
6424 return value_from_contents_and_address (obj_type
, NULL
, base_address
);
6427 /* Return the "ada__tags__type_specific_data" type. */
6429 static struct type
*
6430 ada_get_tsd_type (struct inferior
*inf
)
6432 struct ada_inferior_data
*data
= get_ada_inferior_data (inf
);
6434 if (data
->tsd_type
== 0)
6436 = lookup_transparent_type ("<ada__tags__type_specific_data>",
6437 SEARCH_TYPE_DOMAIN
);
6438 return data
->tsd_type
;
6441 /* Return the TSD (type-specific data) associated to the given TAG.
6442 TAG is assumed to be the tag of a tagged-type entity.
6444 May return NULL if we are unable to get the TSD. */
6446 static struct value
*
6447 ada_get_tsd_from_tag (struct value
*tag
)
6452 /* First option: The TSD is simply stored as a field of our TAG.
6453 Only older versions of GNAT would use this format, but we have
6454 to test it first, because there are no visible markers for
6455 the current approach except the absence of that field. */
6457 val
= ada_value_struct_elt (tag
, "tsd", 1);
6461 /* Try the second representation for the dispatch table (in which
6462 there is no explicit 'tsd' field in the referent of the tag pointer,
6463 and instead the tsd pointer is stored just before the dispatch
6466 type
= ada_get_tsd_type (current_inferior());
6469 type
= lookup_pointer_type (lookup_pointer_type (type
));
6470 val
= value_cast (type
, tag
);
6473 return value_ind (value_ptradd (val
, -1));
6476 /* Given the TSD of a tag (type-specific data), return a string
6477 containing the name of the associated type.
6479 May return NULL if we are unable to determine the tag name. */
6481 static gdb::unique_xmalloc_ptr
<char>
6482 ada_tag_name_from_tsd (struct value
*tsd
)
6486 val
= ada_value_struct_elt (tsd
, "expanded_name", 1);
6489 gdb::unique_xmalloc_ptr
<char> buffer
6490 = target_read_string (value_as_address (val
), INT_MAX
);
6491 if (buffer
== nullptr)
6496 /* Let this throw an exception on error. If the data is
6497 uninitialized, we'd rather not have the user see a
6499 const char *folded
= ada_fold_name (buffer
.get (), true);
6500 return make_unique_xstrdup (folded
);
6502 catch (const gdb_exception
&)
6508 /* The type name of the dynamic type denoted by the 'tag value TAG, as
6511 Return NULL if the TAG is not an Ada tag, or if we were unable to
6512 determine the name of that tag. */
6514 gdb::unique_xmalloc_ptr
<char>
6515 ada_tag_name (struct value
*tag
)
6517 gdb::unique_xmalloc_ptr
<char> name
;
6519 if (!ada_is_tag_type (tag
->type ()))
6522 /* It is perfectly possible that an exception be raised while trying
6523 to determine the TAG's name, even under normal circumstances:
6524 The associated variable may be uninitialized or corrupted, for
6525 instance. We do not let any exception propagate past this point.
6526 instead we return NULL.
6528 We also do not print the error message either (which often is very
6529 low-level (Eg: "Cannot read memory at 0x[...]"), but instead let
6530 the caller print a more meaningful message if necessary. */
6533 struct value
*tsd
= ada_get_tsd_from_tag (tag
);
6536 name
= ada_tag_name_from_tsd (tsd
);
6538 catch (const gdb_exception_error
&e
)
6545 /* The parent type of TYPE, or NULL if none. */
6548 ada_parent_type (struct type
*type
)
6552 type
= ada_check_typedef (type
);
6554 if (type
== NULL
|| type
->code () != TYPE_CODE_STRUCT
)
6557 for (i
= 0; i
< type
->num_fields (); i
+= 1)
6558 if (ada_is_parent_field (type
, i
))
6560 struct type
*parent_type
= type
->field (i
).type ();
6562 /* If the _parent field is a pointer, then dereference it. */
6563 if (parent_type
->code () == TYPE_CODE_PTR
)
6564 parent_type
= parent_type
->target_type ();
6565 /* If there is a parallel XVS type, get the actual base type. */
6566 parent_type
= ada_get_base_type (parent_type
);
6568 return ada_check_typedef (parent_type
);
6574 /* True iff field number FIELD_NUM of structure type TYPE contains the
6575 parent-type (inherited) fields of a derived type. Assumes TYPE is
6576 a structure type with at least FIELD_NUM+1 fields. */
6579 ada_is_parent_field (struct type
*type
, int field_num
)
6581 const char *name
= ada_check_typedef (type
)->field (field_num
).name ();
6583 return (name
!= NULL
6584 && (startswith (name
, "PARENT")
6585 || startswith (name
, "_parent")));
6588 /* True iff field number FIELD_NUM of structure type TYPE is a
6589 transparent wrapper field (which should be silently traversed when doing
6590 field selection and flattened when printing). Assumes TYPE is a
6591 structure type with at least FIELD_NUM+1 fields. Such fields are always
6595 ada_is_wrapper_field (struct type
*type
, int field_num
)
6597 const char *name
= type
->field (field_num
).name ();
6599 if (name
!= NULL
&& strcmp (name
, "RETVAL") == 0)
6601 /* This happens in functions with "out" or "in out" parameters
6602 which are passed by copy. For such functions, GNAT describes
6603 the function's return type as being a struct where the return
6604 value is in a field called RETVAL, and where the other "out"
6605 or "in out" parameters are fields of that struct. This is not
6610 return (name
!= NULL
6611 && (startswith (name
, "PARENT")
6612 || strcmp (name
, "REP") == 0
6613 || startswith (name
, "_parent")
6614 || name
[0] == 'S' || name
[0] == 'R' || name
[0] == 'O'));
6617 /* True iff field number FIELD_NUM of structure or union type TYPE
6618 is a variant wrapper. Assumes TYPE is a structure type with at least
6619 FIELD_NUM+1 fields. */
6622 ada_is_variant_part (struct type
*type
, int field_num
)
6624 /* Only Ada types are eligible. */
6625 if (!ADA_TYPE_P (type
))
6628 struct type
*field_type
= type
->field (field_num
).type ();
6630 return (field_type
->code () == TYPE_CODE_UNION
6631 || (is_dynamic_field (type
, field_num
)
6632 && (field_type
->target_type ()->code ()
6633 == TYPE_CODE_UNION
)));
6636 /* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
6637 whose discriminants are contained in the record type OUTER_TYPE,
6638 returns the type of the controlling discriminant for the variant.
6639 May return NULL if the type could not be found. */
6642 ada_variant_discrim_type (struct type
*var_type
, struct type
*outer_type
)
6644 const char *name
= ada_variant_discrim_name (var_type
);
6646 return ada_lookup_struct_elt_type (outer_type
, name
, 1, 1);
6649 /* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
6650 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
6651 represents a 'when others' clause; otherwise 0. */
6654 ada_is_others_clause (struct type
*type
, int field_num
)
6656 const char *name
= type
->field (field_num
).name ();
6658 return (name
!= NULL
&& name
[0] == 'O');
6661 /* Assuming that TYPE0 is the type of the variant part of a record,
6662 returns the name of the discriminant controlling the variant.
6663 The value is valid until the next call to ada_variant_discrim_name. */
6666 ada_variant_discrim_name (struct type
*type0
)
6668 static std::string result
;
6671 const char *discrim_end
;
6672 const char *discrim_start
;
6674 if (type0
->code () == TYPE_CODE_PTR
)
6675 type
= type0
->target_type ();
6679 name
= ada_type_name (type
);
6681 if (name
== NULL
|| name
[0] == '\000')
6684 for (discrim_end
= name
+ strlen (name
) - 6; discrim_end
!= name
;
6687 if (startswith (discrim_end
, "___XVN"))
6690 if (discrim_end
== name
)
6693 for (discrim_start
= discrim_end
; discrim_start
!= name
+ 3;
6696 if (discrim_start
== name
+ 1)
6698 if ((discrim_start
> name
+ 3
6699 && startswith (discrim_start
- 3, "___"))
6700 || discrim_start
[-1] == '.')
6704 result
= std::string (discrim_start
, discrim_end
- discrim_start
);
6705 return result
.c_str ();
6708 /* Scan STR for a subtype-encoded number, beginning at position K.
6709 Put the position of the character just past the number scanned in
6710 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
6711 Return 1 if there was a valid number at the given position, and 0
6712 otherwise. A "subtype-encoded" number consists of the absolute value
6713 in decimal, followed by the letter 'm' to indicate a negative number.
6714 Assumes 0m does not occur. */
6717 ada_scan_number (const char str
[], int k
, LONGEST
* R
, int *new_k
)
6721 if (!isdigit (str
[k
]))
6724 /* Do it the hard way so as not to make any assumption about
6725 the relationship of unsigned long (%lu scan format code) and
6728 while (isdigit (str
[k
]))
6730 RU
= RU
* 10 + (str
[k
] - '0');
6737 *R
= (-(LONGEST
) (RU
- 1)) - 1;
6743 /* NOTE on the above: Technically, C does not say what the results of
6744 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
6745 number representable as a LONGEST (although either would probably work
6746 in most implementations). When RU>0, the locution in the then branch
6747 above is always equivalent to the negative of RU. */
6754 /* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
6755 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
6756 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
6759 ada_in_variant (LONGEST val
, struct type
*type
, int field_num
)
6761 const char *name
= type
->field (field_num
).name ();
6775 if (!ada_scan_number (name
, p
+ 1, &W
, &p
))
6785 if (!ada_scan_number (name
, p
+ 1, &L
, &p
)
6786 || name
[p
] != 'T' || !ada_scan_number (name
, p
+ 1, &U
, &p
))
6788 if (val
>= L
&& val
<= U
)
6800 /* FIXME: Lots of redundancy below. Try to consolidate. */
6802 /* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6803 ARG_TYPE, extract and return the value of one of its (non-static)
6804 fields. FIELDNO says which field. Differs from value_primitive_field
6805 only in that it can handle packed values of arbitrary type. */
6808 ada_value_primitive_field (struct value
*arg1
, int offset
, int fieldno
,
6809 struct type
*arg_type
)
6813 arg_type
= ada_check_typedef (arg_type
);
6814 type
= arg_type
->field (fieldno
).type ();
6816 /* Handle packed fields. It might be that the field is not packed
6817 relative to its containing structure, but the structure itself is
6818 packed; in this case we must take the bit-field path. */
6819 if (arg_type
->field (fieldno
).bitsize () != 0 || arg1
->bitpos () != 0)
6821 int bit_pos
= arg_type
->field (fieldno
).loc_bitpos ();
6822 int bit_size
= arg_type
->field (fieldno
).bitsize ();
6824 return ada_value_primitive_packed_val (arg1
,
6825 arg1
->contents ().data (),
6826 offset
+ bit_pos
/ 8,
6827 bit_pos
% 8, bit_size
, type
);
6830 return arg1
->primitive_field (offset
, fieldno
, arg_type
);
6833 /* Find field with name NAME in object of type TYPE. If found,
6834 set the following for each argument that is non-null:
6835 - *FIELD_TYPE_P to the field's type;
6836 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6837 an object of that type;
6838 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6839 - *BIT_SIZE_P to its size in bits if the field is packed, and
6841 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6842 fields up to but not including the desired field, or by the total
6843 number of fields if not found. A NULL value of NAME never
6844 matches; the function just counts visible fields in this case.
6846 Notice that we need to handle when a tagged record hierarchy
6847 has some components with the same name, like in this scenario:
6849 type Top_T is tagged record
6855 type Middle_T is new Top.Top_T with record
6856 N : Character := 'a';
6860 type Bottom_T is new Middle.Middle_T with record
6862 C : Character := '5';
6864 A : Character := 'J';
6867 Let's say we now have a variable declared and initialized as follow:
6869 TC : Top_A := new Bottom_T;
6871 And then we use this variable to call this function
6873 procedure Assign (Obj: in out Top_T; TV : Integer);
6877 Assign (Top_T (B), 12);
6879 Now, we're in the debugger, and we're inside that procedure
6880 then and we want to print the value of obj.c:
6882 Usually, the tagged record or one of the parent type owns the
6883 component to print and there's no issue but in this particular
6884 case, what does it mean to ask for Obj.C? Since the actual
6885 type for object is type Bottom_T, it could mean two things: type
6886 component C from the Middle_T view, but also component C from
6887 Bottom_T. So in that "undefined" case, when the component is
6888 not found in the non-resolved type (which includes all the
6889 components of the parent type), then resolve it and see if we
6890 get better luck once expanded.
6892 In the case of homonyms in the derived tagged type, we don't
6893 guaranty anything, and pick the one that's easiest for us
6896 Returns 1 if found, 0 otherwise. */
6899 find_struct_field (const char *name
, struct type
*type
, int offset
,
6900 struct type
**field_type_p
,
6901 int *byte_offset_p
, int *bit_offset_p
, int *bit_size_p
,
6905 int parent_offset
= -1;
6907 type
= ada_check_typedef (type
);
6909 if (field_type_p
!= NULL
)
6910 *field_type_p
= NULL
;
6911 if (byte_offset_p
!= NULL
)
6913 if (bit_offset_p
!= NULL
)
6915 if (bit_size_p
!= NULL
)
6918 for (i
= 0; i
< type
->num_fields (); i
+= 1)
6920 /* These can't be computed using TYPE_FIELD_BITPOS for a dynamic
6921 type. However, we only need the values to be correct when
6922 the caller asks for them. */
6923 int bit_pos
= 0, fld_offset
= 0;
6924 if (byte_offset_p
!= nullptr || bit_offset_p
!= nullptr)
6926 bit_pos
= type
->field (i
).loc_bitpos ();
6927 fld_offset
= offset
+ bit_pos
/ 8;
6930 const char *t_field_name
= type
->field (i
).name ();
6932 if (t_field_name
== NULL
)
6935 else if (ada_is_parent_field (type
, i
))
6937 /* This is a field pointing us to the parent type of a tagged
6938 type. As hinted in this function's documentation, we give
6939 preference to fields in the current record first, so what
6940 we do here is just record the index of this field before
6941 we skip it. If it turns out we couldn't find our field
6942 in the current record, then we'll get back to it and search
6943 inside it whether the field might exist in the parent. */
6949 else if (name
!= NULL
&& field_name_match (t_field_name
, name
))
6951 int bit_size
= type
->field (i
).bitsize ();
6953 if (field_type_p
!= NULL
)
6954 *field_type_p
= type
->field (i
).type ();
6955 if (byte_offset_p
!= NULL
)
6956 *byte_offset_p
= fld_offset
;
6957 if (bit_offset_p
!= NULL
)
6958 *bit_offset_p
= bit_pos
% 8;
6959 if (bit_size_p
!= NULL
)
6960 *bit_size_p
= bit_size
;
6963 else if (ada_is_wrapper_field (type
, i
))
6965 if (find_struct_field (name
, type
->field (i
).type (), fld_offset
,
6966 field_type_p
, byte_offset_p
, bit_offset_p
,
6967 bit_size_p
, index_p
))
6970 else if (ada_is_variant_part (type
, i
))
6972 /* PNH: Wait. Do we ever execute this section, or is ARG always of
6975 struct type
*field_type
6976 = ada_check_typedef (type
->field (i
).type ());
6978 for (j
= 0; j
< field_type
->num_fields (); j
+= 1)
6980 if (find_struct_field (name
, field_type
->field (j
).type (),
6982 + field_type
->field (j
).loc_bitpos () / 8,
6983 field_type_p
, byte_offset_p
,
6984 bit_offset_p
, bit_size_p
, index_p
))
6988 else if (index_p
!= NULL
)
6992 /* Field not found so far. If this is a tagged type which
6993 has a parent, try finding that field in the parent now. */
6995 if (parent_offset
!= -1)
6997 /* As above, only compute the offset when truly needed. */
6998 int fld_offset
= offset
;
6999 if (byte_offset_p
!= nullptr || bit_offset_p
!= nullptr)
7001 int bit_pos
= type
->field (parent_offset
).loc_bitpos ();
7002 fld_offset
+= bit_pos
/ 8;
7005 if (find_struct_field (name
, type
->field (parent_offset
).type (),
7006 fld_offset
, field_type_p
, byte_offset_p
,
7007 bit_offset_p
, bit_size_p
, index_p
))
7014 /* Number of user-visible fields in record type TYPE. */
7017 num_visible_fields (struct type
*type
)
7022 find_struct_field (NULL
, type
, 0, NULL
, NULL
, NULL
, NULL
, &n
);
7026 /* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
7027 and search in it assuming it has (class) type TYPE.
7028 If found, return value, else return NULL.
7030 Searches recursively through wrapper fields (e.g., '_parent').
7032 In the case of homonyms in the tagged types, please refer to the
7033 long explanation in find_struct_field's function documentation. */
7035 static struct value
*
7036 ada_search_struct_field (const char *name
, struct value
*arg
, int offset
,
7040 int parent_offset
= -1;
7042 type
= ada_check_typedef (type
);
7043 for (i
= 0; i
< type
->num_fields (); i
+= 1)
7045 const char *t_field_name
= type
->field (i
).name ();
7047 if (t_field_name
== NULL
)
7050 else if (ada_is_parent_field (type
, i
))
7052 /* This is a field pointing us to the parent type of a tagged
7053 type. As hinted in this function's documentation, we give
7054 preference to fields in the current record first, so what
7055 we do here is just record the index of this field before
7056 we skip it. If it turns out we couldn't find our field
7057 in the current record, then we'll get back to it and search
7058 inside it whether the field might exist in the parent. */
7064 else if (field_name_match (t_field_name
, name
))
7065 return ada_value_primitive_field (arg
, offset
, i
, type
);
7067 else if (ada_is_wrapper_field (type
, i
))
7069 struct value
*v
= /* Do not let indent join lines here. */
7070 ada_search_struct_field (name
, arg
,
7071 offset
+ type
->field (i
).loc_bitpos () / 8,
7072 type
->field (i
).type ());
7078 else if (ada_is_variant_part (type
, i
))
7080 /* PNH: Do we ever get here? See find_struct_field. */
7082 struct type
*field_type
= ada_check_typedef (type
->field (i
).type ());
7083 int var_offset
= offset
+ type
->field (i
).loc_bitpos () / 8;
7085 for (j
= 0; j
< field_type
->num_fields (); j
+= 1)
7087 struct value
*v
= ada_search_struct_field
/* Force line
7090 var_offset
+ field_type
->field (j
).loc_bitpos () / 8,
7091 field_type
->field (j
).type ());
7099 /* Field not found so far. If this is a tagged type which
7100 has a parent, try finding that field in the parent now. */
7102 if (parent_offset
!= -1)
7104 struct value
*v
= ada_search_struct_field (
7105 name
, arg
, offset
+ type
->field (parent_offset
).loc_bitpos () / 8,
7106 type
->field (parent_offset
).type ());
7115 static struct value
*ada_index_struct_field_1 (int *, struct value
*,
7116 int, struct type
*);
7119 /* Return field #INDEX in ARG, where the index is that returned by
7120 * find_struct_field through its INDEX_P argument. Adjust the address
7121 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
7122 * If found, return value, else return NULL. */
7124 static struct value
*
7125 ada_index_struct_field (int index
, struct value
*arg
, int offset
,
7128 return ada_index_struct_field_1 (&index
, arg
, offset
, type
);
7132 /* Auxiliary function for ada_index_struct_field. Like
7133 * ada_index_struct_field, but takes index from *INDEX_P and modifies
7136 static struct value
*
7137 ada_index_struct_field_1 (int *index_p
, struct value
*arg
, int offset
,
7141 type
= ada_check_typedef (type
);
7143 for (i
= 0; i
< type
->num_fields (); i
+= 1)
7145 if (type
->field (i
).name () == NULL
)
7147 else if (ada_is_wrapper_field (type
, i
))
7149 struct value
*v
= /* Do not let indent join lines here. */
7150 ada_index_struct_field_1 (index_p
, arg
,
7151 offset
+ type
->field (i
).loc_bitpos () / 8,
7152 type
->field (i
).type ());
7158 else if (ada_is_variant_part (type
, i
))
7160 /* PNH: Do we ever get here? See ada_search_struct_field,
7161 find_struct_field. */
7162 error (_("Cannot assign this kind of variant record"));
7164 else if (*index_p
== 0)
7165 return ada_value_primitive_field (arg
, offset
, i
, type
);
7172 /* Return a string representation of type TYPE. */
7175 type_as_string (struct type
*type
)
7177 string_file tmp_stream
;
7179 type_print (type
, "", &tmp_stream
, -1);
7181 return tmp_stream
.release ();
7184 /* Given a type TYPE, look up the type of the component of type named NAME.
7186 Matches any field whose name has NAME as a prefix, possibly
7189 TYPE can be either a struct or union. If REFOK, TYPE may also
7190 be a (pointer or reference)+ to a struct or union, and the
7191 ultimate target type will be searched.
7193 Looks recursively into variant clauses and parent types.
7195 In the case of homonyms in the tagged types, please refer to the
7196 long explanation in find_struct_field's function documentation.
7198 If NOERR is nonzero, return NULL if NAME is not suitably defined or
7199 TYPE is not a type of the right kind. */
7201 static struct type
*
7202 ada_lookup_struct_elt_type (struct type
*type
, const char *name
, int refok
,
7208 if (refok
&& type
!= NULL
)
7211 type
= ada_check_typedef (type
);
7212 if (type
->code () != TYPE_CODE_PTR
&& type
->code () != TYPE_CODE_REF
)
7214 type
= type
->target_type ();
7218 || (type
->code () != TYPE_CODE_STRUCT
7219 && type
->code () != TYPE_CODE_UNION
))
7224 error (_("Type %s is not a structure or union type"),
7225 type
!= NULL
? type_as_string (type
).c_str () : _("(null)"));
7228 type
= to_static_fixed_type (type
);
7230 struct type
*result
;
7231 find_struct_field (name
, type
, 0, &result
, nullptr, nullptr, nullptr,
7233 if (result
!= nullptr)
7239 const char *name_str
= name
!= NULL
? name
: _("<null>");
7241 error (_("Type %s has no component named %s"),
7242 type_as_string (type
).c_str (), name_str
);
7248 /* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7249 within a value of type OUTER_TYPE, return true iff VAR_TYPE
7250 represents an unchecked union (that is, the variant part of a
7251 record that is named in an Unchecked_Union pragma). */
7254 is_unchecked_variant (struct type
*var_type
, struct type
*outer_type
)
7256 const char *discrim_name
= ada_variant_discrim_name (var_type
);
7258 return (ada_lookup_struct_elt_type (outer_type
, discrim_name
, 0, 1) == NULL
);
7262 /* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7263 within OUTER, determine which variant clause (field number in VAR_TYPE,
7264 numbering from 0) is applicable. Returns -1 if none are. */
7267 ada_which_variant_applies (struct type
*var_type
, struct value
*outer
)
7271 const char *discrim_name
= ada_variant_discrim_name (var_type
);
7272 struct value
*discrim
;
7273 LONGEST discrim_val
;
7275 /* Using plain value_from_contents_and_address here causes problems
7276 because we will end up trying to resolve a type that is currently
7277 being constructed. */
7278 discrim
= ada_value_struct_elt (outer
, discrim_name
, 1);
7279 if (discrim
== NULL
)
7281 discrim_val
= value_as_long (discrim
);
7284 for (i
= 0; i
< var_type
->num_fields (); i
+= 1)
7286 if (ada_is_others_clause (var_type
, i
))
7288 else if (ada_in_variant (discrim_val
, var_type
, i
))
7292 return others_clause
;
7297 /* Dynamic-Sized Records */
7299 /* Strategy: The type ostensibly attached to a value with dynamic size
7300 (i.e., a size that is not statically recorded in the debugging
7301 data) does not accurately reflect the size or layout of the value.
7302 Our strategy is to convert these values to values with accurate,
7303 conventional types that are constructed on the fly. */
7305 /* There is a subtle and tricky problem here. In general, we cannot
7306 determine the size of dynamic records without its data. However,
7307 the 'struct value' data structure, which GDB uses to represent
7308 quantities in the inferior process (the target), requires the size
7309 of the type at the time of its allocation in order to reserve space
7310 for GDB's internal copy of the data. That's why the
7311 'to_fixed_xxx_type' routines take (target) addresses as parameters,
7312 rather than struct value*s.
7314 However, GDB's internal history variables ($1, $2, etc.) are
7315 struct value*s containing internal copies of the data that are not, in
7316 general, the same as the data at their corresponding addresses in
7317 the target. Fortunately, the types we give to these values are all
7318 conventional, fixed-size types (as per the strategy described
7319 above), so that we don't usually have to perform the
7320 'to_fixed_xxx_type' conversions to look at their values.
7321 Unfortunately, there is one exception: if one of the internal
7322 history variables is an array whose elements are unconstrained
7323 records, then we will need to create distinct fixed types for each
7324 element selected. */
7326 /* The upshot of all of this is that many routines take a (type, host
7327 address, target address) triple as arguments to represent a value.
7328 The host address, if non-null, is supposed to contain an internal
7329 copy of the relevant data; otherwise, the program is to consult the
7330 target at the target address. */
7332 /* Assuming that VAL0 represents a pointer value, the result of
7333 dereferencing it. Differs from value_ind in its treatment of
7334 dynamic-sized types. */
7337 ada_value_ind (struct value
*val0
)
7339 struct value
*val
= value_ind (val0
);
7341 if (ada_is_tagged_type (val
->type (), 0))
7342 val
= ada_tag_value_at_base_address (val
);
7344 return ada_to_fixed_value (val
);
7347 /* The value resulting from dereferencing any "reference to"
7348 qualifiers on VAL0. */
7350 static struct value
*
7351 ada_coerce_ref (struct value
*val0
)
7353 if (val0
->type ()->code () == TYPE_CODE_REF
)
7355 struct value
*val
= val0
;
7357 val
= coerce_ref (val
);
7359 if (ada_is_tagged_type (val
->type (), 0))
7360 val
= ada_tag_value_at_base_address (val
);
7362 return ada_to_fixed_value (val
);
7368 /* Return the bit alignment required for field #F of template type TYPE. */
7371 field_alignment (struct type
*type
, int f
)
7373 const char *name
= type
->field (f
).name ();
7377 /* The field name should never be null, unless the debugging information
7378 is somehow malformed. In this case, we assume the field does not
7379 require any alignment. */
7383 len
= strlen (name
);
7385 if (!isdigit (name
[len
- 1]))
7388 if (isdigit (name
[len
- 2]))
7389 align_offset
= len
- 2;
7391 align_offset
= len
- 1;
7393 if (align_offset
< 7 || !startswith (name
+ align_offset
- 6, "___XV"))
7394 return TARGET_CHAR_BIT
;
7396 return atoi (name
+ align_offset
) * TARGET_CHAR_BIT
;
7399 /* Find a typedef or tag symbol named NAME. Ignores ambiguity. */
7401 static struct symbol
*
7402 ada_find_any_type_symbol (const char *name
)
7404 return standard_lookup (name
, get_selected_block (nullptr),
7405 SEARCH_TYPE_DOMAIN
);
7408 /* Find a type named NAME. Ignores ambiguity. This routine will look
7409 solely for types defined by debug info, it will not search the GDB
7412 static struct type
*
7413 ada_find_any_type (const char *name
)
7415 struct symbol
*sym
= ada_find_any_type_symbol (name
);
7418 return sym
->type ();
7423 /* Given NAME_SYM and an associated BLOCK, find a "renaming" symbol
7424 associated with NAME_SYM's name. NAME_SYM may itself be a renaming
7425 symbol, in which case it is returned. Otherwise, this looks for
7426 symbols whose name is that of NAME_SYM suffixed with "___XR".
7427 Return symbol if found, and NULL otherwise. */
7430 ada_is_renaming_symbol (struct symbol
*name_sym
)
7432 const char *name
= name_sym
->linkage_name ();
7433 return strstr (name
, "___XR") != NULL
;
7436 /* Because of GNAT encoding conventions, several GDB symbols may match a
7437 given type name. If the type denoted by TYPE0 is to be preferred to
7438 that of TYPE1 for purposes of type printing, return non-zero;
7439 otherwise return 0. */
7442 ada_prefer_type (struct type
*type0
, struct type
*type1
)
7446 else if (type0
== NULL
)
7448 else if (type1
->code () == TYPE_CODE_VOID
)
7450 else if (type0
->code () == TYPE_CODE_VOID
)
7452 else if (type1
->name () == NULL
&& type0
->name () != NULL
)
7454 else if (ada_is_constrained_packed_array_type (type0
))
7456 else if (ada_is_array_descriptor_type (type0
)
7457 && !ada_is_array_descriptor_type (type1
))
7461 const char *type0_name
= type0
->name ();
7462 const char *type1_name
= type1
->name ();
7464 if (type0_name
!= NULL
&& strstr (type0_name
, "___XR") != NULL
7465 && (type1_name
== NULL
|| strstr (type1_name
, "___XR") == NULL
))
7471 /* The name of TYPE, which is its TYPE_NAME. Null if TYPE is
7475 ada_type_name (struct type
*type
)
7479 return type
->name ();
7482 /* Search the list of "descriptive" types associated to TYPE for a type
7483 whose name is NAME. */
7485 static struct type
*
7486 find_parallel_type_by_descriptive_type (struct type
*type
, const char *name
)
7488 struct type
*result
, *tmp
;
7490 if (ada_ignore_descriptive_types_p
)
7493 /* If there no descriptive-type info, then there is no parallel type
7495 if (!HAVE_GNAT_AUX_INFO (type
))
7498 result
= TYPE_DESCRIPTIVE_TYPE (type
);
7499 while (result
!= NULL
)
7501 const char *result_name
= ada_type_name (result
);
7503 if (result_name
== NULL
)
7505 warning (_("unexpected null name on descriptive type"));
7509 /* If the names match, stop. */
7510 if (strcmp (result_name
, name
) == 0)
7513 /* Otherwise, look at the next item on the list, if any. */
7514 if (HAVE_GNAT_AUX_INFO (result
))
7515 tmp
= TYPE_DESCRIPTIVE_TYPE (result
);
7519 /* If not found either, try after having resolved the typedef. */
7524 result
= check_typedef (result
);
7525 if (HAVE_GNAT_AUX_INFO (result
))
7526 result
= TYPE_DESCRIPTIVE_TYPE (result
);
7532 /* If we didn't find a match, see whether this is a packed array. With
7533 older compilers, the descriptive type information is either absent or
7534 irrelevant when it comes to packed arrays so the above lookup fails.
7535 Fall back to using a parallel lookup by name in this case. */
7536 if (result
== NULL
&& ada_is_constrained_packed_array_type (type
))
7537 return ada_find_any_type (name
);
7542 /* Find a parallel type to TYPE with the specified NAME, using the
7543 descriptive type taken from the debugging information, if available,
7544 and otherwise using the (slower) name-based method. */
7546 static struct type
*
7547 ada_find_parallel_type_with_name (struct type
*type
, const char *name
)
7549 struct type
*result
= NULL
;
7551 if (HAVE_GNAT_AUX_INFO (type
))
7552 result
= find_parallel_type_by_descriptive_type (type
, name
);
7554 result
= ada_find_any_type (name
);
7559 /* Same as above, but specify the name of the parallel type by appending
7560 SUFFIX to the name of TYPE. */
7563 ada_find_parallel_type (struct type
*type
, const char *suffix
)
7566 const char *type_name
= ada_type_name (type
);
7569 if (type_name
== NULL
)
7572 len
= strlen (type_name
);
7574 name
= (char *) alloca (len
+ strlen (suffix
) + 1);
7576 strcpy (name
, type_name
);
7577 strcpy (name
+ len
, suffix
);
7579 return ada_find_parallel_type_with_name (type
, name
);
7582 /* If TYPE is a variable-size record type, return the corresponding template
7583 type describing its fields. Otherwise, return NULL. */
7585 static struct type
*
7586 dynamic_template_type (struct type
*type
)
7588 type
= ada_check_typedef (type
);
7590 if (type
== NULL
|| type
->code () != TYPE_CODE_STRUCT
7591 || ada_type_name (type
) == NULL
)
7595 int len
= strlen (ada_type_name (type
));
7597 if (len
> 6 && strcmp (ada_type_name (type
) + len
- 6, "___XVE") == 0)
7600 return ada_find_parallel_type (type
, "___XVE");
7604 /* Assuming that TEMPL_TYPE is a union or struct type, returns
7605 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
7608 is_dynamic_field (struct type
*templ_type
, int field_num
)
7610 const char *name
= templ_type
->field (field_num
).name ();
7613 && templ_type
->field (field_num
).type ()->code () == TYPE_CODE_PTR
7614 && strstr (name
, "___XVL") != NULL
;
7617 /* The index of the variant field of TYPE, or -1 if TYPE does not
7618 represent a variant record type. */
7621 variant_field_index (struct type
*type
)
7625 if (type
== NULL
|| type
->code () != TYPE_CODE_STRUCT
)
7628 for (f
= 0; f
< type
->num_fields (); f
+= 1)
7630 if (ada_is_variant_part (type
, f
))
7636 /* A record type with no fields. */
7638 static struct type
*
7639 empty_record (struct type
*templ
)
7641 struct type
*type
= type_allocator (templ
).new_type ();
7643 type
->set_code (TYPE_CODE_STRUCT
);
7644 INIT_NONE_SPECIFIC (type
);
7645 type
->set_name ("<empty>");
7646 type
->set_length (0);
7650 /* An ordinary record type (with fixed-length fields) that describes
7651 the value of type TYPE at VALADDR or ADDRESS (see comments at
7652 the beginning of this section) VAL according to GNAT conventions.
7653 DVAL0 should describe the (portion of a) record that contains any
7654 necessary discriminants. It should be NULL if VAL->type () is
7655 an outer-level type (i.e., as opposed to a branch of a variant.) A
7656 variant field (unless unchecked) is replaced by a particular branch
7659 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
7660 length are not statically known are discarded. As a consequence,
7661 VALADDR, ADDRESS and DVAL0 are ignored.
7663 NOTE: Limitations: For now, we assume that dynamic fields and
7664 variants occupy whole numbers of bytes. However, they need not be
7668 ada_template_to_fixed_record_type_1 (struct type
*type
,
7669 const gdb_byte
*valaddr
,
7670 CORE_ADDR address
, struct value
*dval0
,
7671 int keep_dynamic_fields
)
7675 int nfields
, bit_len
;
7681 scoped_value_mark mark
;
7683 /* Compute the number of fields in this record type that are going
7684 to be processed: unless keep_dynamic_fields, this includes only
7685 fields whose position and length are static will be processed. */
7686 if (keep_dynamic_fields
)
7687 nfields
= type
->num_fields ();
7691 while (nfields
< type
->num_fields ()
7692 && !ada_is_variant_part (type
, nfields
)
7693 && !is_dynamic_field (type
, nfields
))
7697 rtype
= type_allocator (type
).new_type ();
7698 rtype
->set_code (TYPE_CODE_STRUCT
);
7699 INIT_NONE_SPECIFIC (rtype
);
7700 rtype
->alloc_fields (nfields
);
7701 rtype
->set_name (ada_type_name (type
));
7702 rtype
->set_is_fixed_instance (true);
7708 for (f
= 0; f
< nfields
; f
+= 1)
7710 off
= align_up (off
, field_alignment (type
, f
))
7711 + type
->field (f
).loc_bitpos ();
7712 rtype
->field (f
).set_loc_bitpos (off
);
7713 rtype
->field (f
).set_bitsize (0);
7715 if (ada_is_variant_part (type
, f
))
7720 else if (is_dynamic_field (type
, f
))
7722 const gdb_byte
*field_valaddr
= valaddr
;
7723 CORE_ADDR field_address
= address
;
7724 struct type
*field_type
= type
->field (f
).type ()->target_type ();
7728 /* Using plain value_from_contents_and_address here
7729 causes problems because we will end up trying to
7730 resolve a type that is currently being
7732 dval
= value_from_contents_and_address_unresolved (rtype
,
7735 rtype
= dval
->type ();
7740 /* If the type referenced by this field is an aligner type, we need
7741 to unwrap that aligner type, because its size might not be set.
7742 Keeping the aligner type would cause us to compute the wrong
7743 size for this field, impacting the offset of the all the fields
7744 that follow this one. */
7745 if (ada_is_aligner_type (field_type
))
7747 long field_offset
= type
->field (f
).loc_bitpos ();
7749 field_valaddr
= cond_offset_host (field_valaddr
, field_offset
);
7750 field_address
= cond_offset_target (field_address
, field_offset
);
7751 field_type
= ada_aligned_type (field_type
);
7754 field_valaddr
= cond_offset_host (field_valaddr
,
7755 off
/ TARGET_CHAR_BIT
);
7756 field_address
= cond_offset_target (field_address
,
7757 off
/ TARGET_CHAR_BIT
);
7759 /* Get the fixed type of the field. Note that, in this case,
7760 we do not want to get the real type out of the tag: if
7761 the current field is the parent part of a tagged record,
7762 we will get the tag of the object. Clearly wrong: the real
7763 type of the parent is not the real type of the child. We
7764 would end up in an infinite loop. */
7765 field_type
= ada_get_base_type (field_type
);
7766 field_type
= ada_to_fixed_type (field_type
, field_valaddr
,
7767 field_address
, dval
, 0);
7769 rtype
->field (f
).set_type (field_type
);
7770 rtype
->field (f
).set_name (type
->field (f
).name ());
7771 /* The multiplication can potentially overflow. But because
7772 the field length has been size-checked just above, and
7773 assuming that the maximum size is a reasonable value,
7774 an overflow should not happen in practice. So rather than
7775 adding overflow recovery code to this already complex code,
7776 we just assume that it's not going to happen. */
7777 fld_bit_len
= rtype
->field (f
).type ()->length () * TARGET_CHAR_BIT
;
7781 /* Note: If this field's type is a typedef, it is important
7782 to preserve the typedef layer.
7784 Otherwise, we might be transforming a typedef to a fat
7785 pointer (encoding a pointer to an unconstrained array),
7786 into a basic fat pointer (encoding an unconstrained
7787 array). As both types are implemented using the same
7788 structure, the typedef is the only clue which allows us
7789 to distinguish between the two options. Stripping it
7790 would prevent us from printing this field appropriately. */
7791 rtype
->field (f
).set_type (type
->field (f
).type ());
7792 rtype
->field (f
).set_name (type
->field (f
).name ());
7793 if (type
->field (f
).bitsize () > 0)
7795 fld_bit_len
= type
->field (f
).bitsize ();
7796 rtype
->field (f
).set_bitsize (fld_bit_len
);
7800 struct type
*field_type
= type
->field (f
).type ();
7802 /* We need to be careful of typedefs when computing
7803 the length of our field. If this is a typedef,
7804 get the length of the target type, not the length
7806 if (field_type
->code () == TYPE_CODE_TYPEDEF
)
7807 field_type
= ada_typedef_target_type (field_type
);
7810 ada_check_typedef (field_type
)->length () * TARGET_CHAR_BIT
;
7813 if (off
+ fld_bit_len
> bit_len
)
7814 bit_len
= off
+ fld_bit_len
;
7816 rtype
->set_length (align_up (bit_len
, TARGET_CHAR_BIT
) / TARGET_CHAR_BIT
);
7819 /* We handle the variant part, if any, at the end because of certain
7820 odd cases in which it is re-ordered so as NOT to be the last field of
7821 the record. This can happen in the presence of representation
7823 if (variant_field
>= 0)
7825 struct type
*branch_type
;
7827 off
= rtype
->field (variant_field
).loc_bitpos ();
7831 /* Using plain value_from_contents_and_address here causes
7832 problems because we will end up trying to resolve a type
7833 that is currently being constructed. */
7834 dval
= value_from_contents_and_address_unresolved (rtype
, valaddr
,
7836 rtype
= dval
->type ();
7842 to_fixed_variant_branch_type
7843 (type
->field (variant_field
).type (),
7844 cond_offset_host (valaddr
, off
/ TARGET_CHAR_BIT
),
7845 cond_offset_target (address
, off
/ TARGET_CHAR_BIT
), dval
);
7846 if (branch_type
== NULL
)
7848 for (f
= variant_field
+ 1; f
< rtype
->num_fields (); f
+= 1)
7849 rtype
->field (f
- 1) = rtype
->field (f
);
7850 rtype
->set_num_fields (rtype
->num_fields () - 1);
7854 rtype
->field (variant_field
).set_type (branch_type
);
7855 rtype
->field (variant_field
).set_name ("S");
7857 rtype
->field (variant_field
).type ()->length () * TARGET_CHAR_BIT
;
7858 if (off
+ fld_bit_len
> bit_len
)
7859 bit_len
= off
+ fld_bit_len
;
7862 (align_up (bit_len
, TARGET_CHAR_BIT
) / TARGET_CHAR_BIT
);
7866 /* According to exp_dbug.ads, the size of TYPE for variable-size records
7867 should contain the alignment of that record, which should be a strictly
7868 positive value. If null or negative, then something is wrong, most
7869 probably in the debug info. In that case, we don't round up the size
7870 of the resulting type. If this record is not part of another structure,
7871 the current RTYPE length might be good enough for our purposes. */
7872 if (type
->length () <= 0)
7875 warning (_("Invalid type size for `%s' detected: %s."),
7876 rtype
->name (), pulongest (type
->length ()));
7878 warning (_("Invalid type size for <unnamed> detected: %s."),
7879 pulongest (type
->length ()));
7882 rtype
->set_length (align_up (rtype
->length (), type
->length ()));
7887 /* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
7890 static struct type
*
7891 template_to_fixed_record_type (struct type
*type
, const gdb_byte
*valaddr
,
7892 CORE_ADDR address
, struct value
*dval0
)
7894 return ada_template_to_fixed_record_type_1 (type
, valaddr
,
7898 /* An ordinary record type in which ___XVL-convention fields and
7899 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
7900 static approximations, containing all possible fields. Uses
7901 no runtime values. Useless for use in values, but that's OK,
7902 since the results are used only for type determinations. Works on both
7903 structs and unions. Representation note: to save space, we memorize
7904 the result of this function in the type::target_type of the
7907 static struct type
*
7908 template_to_static_fixed_type (struct type
*type0
)
7914 /* No need no do anything if the input type is already fixed. */
7915 if (type0
->is_fixed_instance ())
7918 /* Likewise if we already have computed the static approximation. */
7919 if (type0
->target_type () != NULL
)
7920 return type0
->target_type ();
7922 /* Don't clone TYPE0 until we are sure we are going to need a copy. */
7924 nfields
= type0
->num_fields ();
7926 /* Whether or not we cloned TYPE0, cache the result so that we don't do
7927 recompute all over next time. */
7928 type0
->set_target_type (type
);
7930 for (f
= 0; f
< nfields
; f
+= 1)
7932 struct type
*field_type
= type0
->field (f
).type ();
7933 struct type
*new_type
;
7935 if (is_dynamic_field (type0
, f
))
7937 field_type
= ada_check_typedef (field_type
);
7938 new_type
= to_static_fixed_type (field_type
->target_type ());
7941 new_type
= static_unwrap_type (field_type
);
7943 if (new_type
!= field_type
)
7945 /* Clone TYPE0 only the first time we get a new field type. */
7948 type
= type_allocator (type0
).new_type ();
7949 type0
->set_target_type (type
);
7950 type
->set_code (type0
->code ());
7951 INIT_NONE_SPECIFIC (type
);
7953 type
->copy_fields (type0
);
7955 type
->set_name (ada_type_name (type0
));
7956 type
->set_is_fixed_instance (true);
7957 type
->set_length (0);
7959 type
->field (f
).set_type (new_type
);
7960 type
->field (f
).set_name (type0
->field (f
).name ());
7967 /* Given an object of type TYPE whose contents are at VALADDR and
7968 whose address in memory is ADDRESS, returns a revision of TYPE,
7969 which should be a non-dynamic-sized record, in which the variant
7970 part, if any, is replaced with the appropriate branch. Looks
7971 for discriminant values in DVAL0, which can be NULL if the record
7972 contains the necessary discriminant values. */
7974 static struct type
*
7975 to_record_with_fixed_variant_part (struct type
*type
, const gdb_byte
*valaddr
,
7976 CORE_ADDR address
, struct value
*dval0
)
7980 struct type
*branch_type
;
7981 int nfields
= type
->num_fields ();
7982 int variant_field
= variant_field_index (type
);
7984 if (variant_field
== -1)
7987 scoped_value_mark mark
;
7990 dval
= value_from_contents_and_address (type
, valaddr
, address
);
7991 type
= dval
->type ();
7996 rtype
= type_allocator (type
).new_type ();
7997 rtype
->set_code (TYPE_CODE_STRUCT
);
7998 INIT_NONE_SPECIFIC (rtype
);
7999 rtype
->copy_fields (type
);
8001 rtype
->set_name (ada_type_name (type
));
8002 rtype
->set_is_fixed_instance (true);
8003 rtype
->set_length (type
->length ());
8005 branch_type
= to_fixed_variant_branch_type
8006 (type
->field (variant_field
).type (),
8007 cond_offset_host (valaddr
,
8008 type
->field (variant_field
).loc_bitpos ()
8010 cond_offset_target (address
,
8011 type
->field (variant_field
).loc_bitpos ()
8012 / TARGET_CHAR_BIT
), dval
);
8013 if (branch_type
== NULL
)
8017 for (f
= variant_field
+ 1; f
< nfields
; f
+= 1)
8018 rtype
->field (f
- 1) = rtype
->field (f
);
8019 rtype
->set_num_fields (rtype
->num_fields () - 1);
8023 rtype
->field (variant_field
).set_type (branch_type
);
8024 rtype
->field (variant_field
).set_name ("S");
8025 rtype
->field (variant_field
).set_bitsize (0);
8026 rtype
->set_length (rtype
->length () + branch_type
->length ());
8029 rtype
->set_length (rtype
->length ()
8030 - type
->field (variant_field
).type ()->length ());
8035 /* An ordinary record type (with fixed-length fields) that describes
8036 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
8037 beginning of this section]. Any necessary discriminants' values
8038 should be in DVAL, a record value; it may be NULL if the object
8039 at ADDR itself contains any necessary discriminant values.
8040 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
8041 values from the record are needed. Except in the case that DVAL,
8042 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
8043 unchecked) is replaced by a particular branch of the variant.
8045 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
8046 is questionable and may be removed. It can arise during the
8047 processing of an unconstrained-array-of-record type where all the
8048 variant branches have exactly the same size. This is because in
8049 such cases, the compiler does not bother to use the XVS convention
8050 when encoding the record. I am currently dubious of this
8051 shortcut and suspect the compiler should be altered. FIXME. */
8053 static struct type
*
8054 to_fixed_record_type (struct type
*type0
, const gdb_byte
*valaddr
,
8055 CORE_ADDR address
, struct value
*dval
)
8057 struct type
*templ_type
;
8059 if (type0
->is_fixed_instance ())
8062 templ_type
= dynamic_template_type (type0
);
8064 if (templ_type
!= NULL
)
8065 return template_to_fixed_record_type (templ_type
, valaddr
, address
, dval
);
8066 else if (variant_field_index (type0
) >= 0)
8068 if (dval
== NULL
&& valaddr
== NULL
&& address
== 0)
8070 return to_record_with_fixed_variant_part (type0
, valaddr
, address
,
8075 type0
->set_is_fixed_instance (true);
8081 /* An ordinary record type (with fixed-length fields) that describes
8082 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
8083 union type. Any necessary discriminants' values should be in DVAL,
8084 a record value. That is, this routine selects the appropriate
8085 branch of the union at ADDR according to the discriminant value
8086 indicated in the union's type name. Returns VAR_TYPE0 itself if
8087 it represents a variant subject to a pragma Unchecked_Union. */
8089 static struct type
*
8090 to_fixed_variant_branch_type (struct type
*var_type0
, const gdb_byte
*valaddr
,
8091 CORE_ADDR address
, struct value
*dval
)
8094 struct type
*templ_type
;
8095 struct type
*var_type
;
8097 if (var_type0
->code () == TYPE_CODE_PTR
)
8098 var_type
= var_type0
->target_type ();
8100 var_type
= var_type0
;
8102 templ_type
= ada_find_parallel_type (var_type
, "___XVU");
8104 if (templ_type
!= NULL
)
8105 var_type
= templ_type
;
8107 if (is_unchecked_variant (var_type
, dval
->type ()))
8109 which
= ada_which_variant_applies (var_type
, dval
);
8112 return empty_record (var_type
);
8113 else if (is_dynamic_field (var_type
, which
))
8114 return to_fixed_record_type
8115 (var_type
->field (which
).type ()->target_type(), valaddr
, address
, dval
);
8116 else if (variant_field_index (var_type
->field (which
).type ()) >= 0)
8118 to_fixed_record_type
8119 (var_type
->field (which
).type (), valaddr
, address
, dval
);
8121 return var_type
->field (which
).type ();
8124 /* Assuming RANGE_TYPE is a TYPE_CODE_RANGE, return nonzero if
8125 ENCODING_TYPE, a type following the GNAT conventions for discrete
8126 type encodings, only carries redundant information. */
8129 ada_is_redundant_range_encoding (struct type
*range_type
,
8130 struct type
*encoding_type
)
8132 const char *bounds_str
;
8136 gdb_assert (range_type
->code () == TYPE_CODE_RANGE
);
8138 if (get_base_type (range_type
)->code ()
8139 != get_base_type (encoding_type
)->code ())
8141 /* The compiler probably used a simple base type to describe
8142 the range type instead of the range's actual base type,
8143 expecting us to get the real base type from the encoding
8144 anyway. In this situation, the encoding cannot be ignored
8149 if (is_dynamic_type (range_type
))
8152 if (encoding_type
->name () == NULL
)
8155 bounds_str
= strstr (encoding_type
->name (), "___XDLU_");
8156 if (bounds_str
== NULL
)
8159 n
= 8; /* Skip "___XDLU_". */
8160 if (!ada_scan_number (bounds_str
, n
, &lo
, &n
))
8162 if (range_type
->bounds ()->low
.const_val () != lo
)
8165 n
+= 2; /* Skip the "__" separator between the two bounds. */
8166 if (!ada_scan_number (bounds_str
, n
, &hi
, &n
))
8168 if (range_type
->bounds ()->high
.const_val () != hi
)
8174 /* Given the array type ARRAY_TYPE, return nonzero if DESC_TYPE,
8175 a type following the GNAT encoding for describing array type
8176 indices, only carries redundant information. */
8179 ada_is_redundant_index_type_desc (struct type
*array_type
,
8180 struct type
*desc_type
)
8182 struct type
*this_layer
= check_typedef (array_type
);
8185 for (i
= 0; i
< desc_type
->num_fields (); i
++)
8187 if (!ada_is_redundant_range_encoding (this_layer
->index_type (),
8188 desc_type
->field (i
).type ()))
8190 this_layer
= check_typedef (this_layer
->target_type ());
8196 /* Assuming that TYPE0 is an array type describing the type of a value
8197 at ADDR, and that DVAL describes a record containing any
8198 discriminants used in TYPE0, returns a type for the value that
8199 contains no dynamic components (that is, no components whose sizes
8200 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
8201 true, gives an error message if the resulting type's size is over
8204 static struct type
*
8205 to_fixed_array_type (struct type
*type0
, struct value
*dval
,
8208 struct type
*index_type_desc
;
8209 struct type
*result
;
8210 int constrained_packed_array_p
;
8211 static const char *xa_suffix
= "___XA";
8213 type0
= ada_check_typedef (type0
);
8214 if (type0
->is_fixed_instance ())
8217 constrained_packed_array_p
= ada_is_constrained_packed_array_type (type0
);
8218 if (constrained_packed_array_p
)
8220 type0
= decode_constrained_packed_array_type (type0
);
8221 if (type0
== nullptr)
8222 error (_("could not decode constrained packed array type"));
8225 index_type_desc
= ada_find_parallel_type (type0
, xa_suffix
);
8227 /* As mentioned in exp_dbug.ads, for non bit-packed arrays an
8228 encoding suffixed with 'P' may still be generated. If so,
8229 it should be used to find the XA type. */
8231 if (index_type_desc
== NULL
)
8233 const char *type_name
= ada_type_name (type0
);
8235 if (type_name
!= NULL
)
8237 const int len
= strlen (type_name
);
8238 char *name
= (char *) alloca (len
+ strlen (xa_suffix
));
8240 if (type_name
[len
- 1] == 'P')
8242 strcpy (name
, type_name
);
8243 strcpy (name
+ len
- 1, xa_suffix
);
8244 index_type_desc
= ada_find_parallel_type_with_name (type0
, name
);
8249 ada_fixup_array_indexes_type (index_type_desc
);
8250 if (index_type_desc
!= NULL
8251 && ada_is_redundant_index_type_desc (type0
, index_type_desc
))
8253 /* Ignore this ___XA parallel type, as it does not bring any
8254 useful information. This allows us to avoid creating fixed
8255 versions of the array's index types, which would be identical
8256 to the original ones. This, in turn, can also help avoid
8257 the creation of fixed versions of the array itself. */
8258 index_type_desc
= NULL
;
8261 if (index_type_desc
== NULL
)
8263 struct type
*elt_type0
= ada_check_typedef (type0
->target_type ());
8265 /* NOTE: elt_type---the fixed version of elt_type0---should never
8266 depend on the contents of the array in properly constructed
8268 /* Create a fixed version of the array element type.
8269 We're not providing the address of an element here,
8270 and thus the actual object value cannot be inspected to do
8271 the conversion. This should not be a problem, since arrays of
8272 unconstrained objects are not allowed. In particular, all
8273 the elements of an array of a tagged type should all be of
8274 the same type specified in the debugging info. No need to
8275 consult the object tag. */
8276 struct type
*elt_type
= ada_to_fixed_type (elt_type0
, 0, 0, dval
, 1);
8278 /* Make sure we always create a new array type when dealing with
8279 packed array types, since we're going to fix-up the array
8280 type length and element bitsize a little further down. */
8281 if (elt_type0
== elt_type
&& !constrained_packed_array_p
)
8285 type_allocator
alloc (type0
);
8286 result
= create_array_type (alloc
, elt_type
, type0
->index_type ());
8292 struct type
*elt_type0
;
8295 for (i
= index_type_desc
->num_fields (); i
> 0; i
-= 1)
8296 elt_type0
= elt_type0
->target_type ();
8298 /* NOTE: result---the fixed version of elt_type0---should never
8299 depend on the contents of the array in properly constructed
8301 /* Create a fixed version of the array element type.
8302 We're not providing the address of an element here,
8303 and thus the actual object value cannot be inspected to do
8304 the conversion. This should not be a problem, since arrays of
8305 unconstrained objects are not allowed. In particular, all
8306 the elements of an array of a tagged type should all be of
8307 the same type specified in the debugging info. No need to
8308 consult the object tag. */
8310 ada_to_fixed_type (ada_check_typedef (elt_type0
), 0, 0, dval
, 1);
8313 for (i
= index_type_desc
->num_fields () - 1; i
>= 0; i
-= 1)
8315 struct type
*range_type
=
8316 to_fixed_range_type (index_type_desc
->field (i
).type (), dval
);
8318 type_allocator
alloc (elt_type0
);
8319 result
= create_array_type (alloc
, result
, range_type
);
8320 elt_type0
= elt_type0
->target_type ();
8324 /* We want to preserve the type name. This can be useful when
8325 trying to get the type name of a value that has already been
8326 printed (for instance, if the user did "print VAR; whatis $". */
8327 result
->set_name (type0
->name ());
8329 if (constrained_packed_array_p
)
8331 /* So far, the resulting type has been created as if the original
8332 type was a regular (non-packed) array type. As a result, the
8333 bitsize of the array elements needs to be set again, and the array
8334 length needs to be recomputed based on that bitsize. */
8335 int len
= result
->length () / result
->target_type ()->length ();
8336 int elt_bitsize
= type0
->field (0).bitsize ();
8338 result
->field (0).set_bitsize (elt_bitsize
);
8339 result
->set_length (len
* elt_bitsize
/ HOST_CHAR_BIT
);
8340 if (result
->length () * HOST_CHAR_BIT
< len
* elt_bitsize
)
8341 result
->set_length (result
->length () + 1);
8344 result
->set_is_fixed_instance (true);
8349 /* A standard type (containing no dynamically sized components)
8350 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
8351 DVAL describes a record containing any discriminants used in TYPE0,
8352 and may be NULL if there are none, or if the object of type TYPE at
8353 ADDRESS or in VALADDR contains these discriminants.
8355 If CHECK_TAG is not null, in the case of tagged types, this function
8356 attempts to locate the object's tag and use it to compute the actual
8357 type. However, when ADDRESS is null, we cannot use it to determine the
8358 location of the tag, and therefore compute the tagged type's actual type.
8359 So we return the tagged type without consulting the tag. */
8361 static struct type
*
8362 ada_to_fixed_type_1 (struct type
*type
, const gdb_byte
*valaddr
,
8363 CORE_ADDR address
, struct value
*dval
, int check_tag
)
8365 type
= ada_check_typedef (type
);
8367 /* Only un-fixed types need to be handled here. */
8368 if (!HAVE_GNAT_AUX_INFO (type
))
8371 switch (type
->code ())
8375 case TYPE_CODE_STRUCT
:
8377 struct type
*static_type
= to_static_fixed_type (type
);
8378 struct type
*fixed_record_type
=
8379 to_fixed_record_type (type
, valaddr
, address
, NULL
);
8381 /* If STATIC_TYPE is a tagged type and we know the object's address,
8382 then we can determine its tag, and compute the object's actual
8383 type from there. Note that we have to use the fixed record
8384 type (the parent part of the record may have dynamic fields
8385 and the way the location of _tag is expressed may depend on
8388 if (check_tag
&& address
!= 0 && ada_is_tagged_type (static_type
, 0))
8391 value_tag_from_contents_and_address
8395 struct type
*real_type
= type_from_tag (tag
);
8397 value_from_contents_and_address (fixed_record_type
,
8400 fixed_record_type
= obj
->type ();
8401 if (real_type
!= NULL
)
8402 return to_fixed_record_type
8404 ada_tag_value_at_base_address (obj
)->address (), NULL
);
8407 /* Check to see if there is a parallel ___XVZ variable.
8408 If there is, then it provides the actual size of our type. */
8409 else if (ada_type_name (fixed_record_type
) != NULL
)
8411 const char *name
= ada_type_name (fixed_record_type
);
8413 = (char *) alloca (strlen (name
) + 7 /* "___XVZ\0" */);
8414 bool xvz_found
= false;
8417 xsnprintf (xvz_name
, strlen (name
) + 7, "%s___XVZ", name
);
8420 xvz_found
= get_int_var_value (xvz_name
, size
);
8422 catch (const gdb_exception_error
&except
)
8424 /* We found the variable, but somehow failed to read
8425 its value. Rethrow the same error, but with a little
8426 bit more information, to help the user understand
8427 what went wrong (Eg: the variable might have been
8429 throw_error (except
.error
,
8430 _("unable to read value of %s (%s)"),
8431 xvz_name
, except
.what ());
8434 if (xvz_found
&& fixed_record_type
->length () != size
)
8436 fixed_record_type
= copy_type (fixed_record_type
);
8437 fixed_record_type
->set_length (size
);
8439 /* The FIXED_RECORD_TYPE may have be a stub. We have
8440 observed this when the debugging info is STABS, and
8441 apparently it is something that is hard to fix.
8443 In practice, we don't need the actual type definition
8444 at all, because the presence of the XVZ variable allows us
8445 to assume that there must be a XVS type as well, which we
8446 should be able to use later, when we need the actual type
8449 In the meantime, pretend that the "fixed" type we are
8450 returning is NOT a stub, because this can cause trouble
8451 when using this type to create new types targeting it.
8452 Indeed, the associated creation routines often check
8453 whether the target type is a stub and will try to replace
8454 it, thus using a type with the wrong size. This, in turn,
8455 might cause the new type to have the wrong size too.
8456 Consider the case of an array, for instance, where the size
8457 of the array is computed from the number of elements in
8458 our array multiplied by the size of its element. */
8459 fixed_record_type
->set_is_stub (false);
8462 return fixed_record_type
;
8464 case TYPE_CODE_ARRAY
:
8465 return to_fixed_array_type (type
, dval
, 1);
8466 case TYPE_CODE_UNION
:
8470 return to_fixed_variant_branch_type (type
, valaddr
, address
, dval
);
8474 /* The same as ada_to_fixed_type_1, except that it preserves the type
8475 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
8477 The typedef layer needs be preserved in order to differentiate between
8478 arrays and array pointers when both types are implemented using the same
8479 fat pointer. In the array pointer case, the pointer is encoded as
8480 a typedef of the pointer type. For instance, considering:
8482 type String_Access is access String;
8483 S1 : String_Access := null;
8485 To the debugger, S1 is defined as a typedef of type String. But
8486 to the user, it is a pointer. So if the user tries to print S1,
8487 we should not dereference the array, but print the array address
8490 If we didn't preserve the typedef layer, we would lose the fact that
8491 the type is to be presented as a pointer (needs de-reference before
8492 being printed). And we would also use the source-level type name. */
8495 ada_to_fixed_type (struct type
*type
, const gdb_byte
*valaddr
,
8496 CORE_ADDR address
, struct value
*dval
, int check_tag
)
8499 struct type
*fixed_type
=
8500 ada_to_fixed_type_1 (type
, valaddr
, address
, dval
, check_tag
);
8502 /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
8503 then preserve the typedef layer.
8505 Implementation note: We can only check the main-type portion of
8506 the TYPE and FIXED_TYPE, because eliminating the typedef layer
8507 from TYPE now returns a type that has the same instance flags
8508 as TYPE. For instance, if TYPE is a "typedef const", and its
8509 target type is a "struct", then the typedef elimination will return
8510 a "const" version of the target type. See check_typedef for more
8511 details about how the typedef layer elimination is done.
8513 brobecker/2010-11-19: It seems to me that the only case where it is
8514 useful to preserve the typedef layer is when dealing with fat pointers.
8515 Perhaps, we could add a check for that and preserve the typedef layer
8516 only in that situation. But this seems unnecessary so far, probably
8517 because we call check_typedef/ada_check_typedef pretty much everywhere.
8519 if (type
->code () == TYPE_CODE_TYPEDEF
8520 && (TYPE_MAIN_TYPE (ada_typedef_target_type (type
))
8521 == TYPE_MAIN_TYPE (fixed_type
)))
8527 /* A standard (static-sized) type corresponding as well as possible to
8528 TYPE0, but based on no runtime data. */
8530 static struct type
*
8531 to_static_fixed_type (struct type
*type0
)
8538 if (type0
->is_fixed_instance ())
8541 type0
= ada_check_typedef (type0
);
8543 switch (type0
->code ())
8547 case TYPE_CODE_STRUCT
:
8548 type
= dynamic_template_type (type0
);
8550 return template_to_static_fixed_type (type
);
8552 return template_to_static_fixed_type (type0
);
8553 case TYPE_CODE_UNION
:
8554 type
= ada_find_parallel_type (type0
, "___XVU");
8556 return template_to_static_fixed_type (type
);
8558 return template_to_static_fixed_type (type0
);
8562 /* A static approximation of TYPE with all type wrappers removed. */
8564 static struct type
*
8565 static_unwrap_type (struct type
*type
)
8567 if (ada_is_aligner_type (type
))
8569 struct type
*type1
= ada_check_typedef (type
)->field (0).type ();
8570 if (ada_type_name (type1
) == NULL
)
8571 type1
->set_name (ada_type_name (type
));
8573 return static_unwrap_type (type1
);
8577 struct type
*raw_real_type
= ada_get_base_type (type
);
8579 if (raw_real_type
== type
)
8582 return to_static_fixed_type (raw_real_type
);
8586 /* In some cases, incomplete and private types require
8587 cross-references that are not resolved as records (for example,
8589 type FooP is access Foo;
8591 type Foo is array ...;
8592 ). In these cases, since there is no mechanism for producing
8593 cross-references to such types, we instead substitute for FooP a
8594 stub enumeration type that is nowhere resolved, and whose tag is
8595 the name of the actual type. Call these types "non-record stubs". */
8597 /* A type equivalent to TYPE that is not a non-record stub, if one
8598 exists, otherwise TYPE. */
8601 ada_check_typedef (struct type
*type
)
8606 /* If our type is an access to an unconstrained array, which is encoded
8607 as a TYPE_CODE_TYPEDEF of a fat pointer, then we're done.
8608 We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
8609 what allows us to distinguish between fat pointers that represent
8610 array types, and fat pointers that represent array access types
8611 (in both cases, the compiler implements them as fat pointers). */
8612 if (ada_is_access_to_unconstrained_array (type
))
8615 type
= check_typedef (type
);
8616 if (type
== NULL
|| type
->code () != TYPE_CODE_ENUM
8617 || !type
->is_stub ()
8618 || type
->name () == NULL
)
8622 const char *name
= type
->name ();
8623 struct type
*type1
= ada_find_any_type (name
);
8628 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
8629 stubs pointing to arrays, as we don't create symbols for array
8630 types, only for the typedef-to-array types). If that's the case,
8631 strip the typedef layer. */
8632 if (type1
->code () == TYPE_CODE_TYPEDEF
)
8633 type1
= ada_check_typedef (type1
);
8639 /* A value representing the data at VALADDR/ADDRESS as described by
8640 type TYPE0, but with a standard (static-sized) type that correctly
8641 describes it. If VAL0 is not NULL and TYPE0 already is a standard
8642 type, then return VAL0 [this feature is simply to avoid redundant
8643 creation of struct values]. */
8645 static struct value
*
8646 ada_to_fixed_value_create (struct type
*type0
, CORE_ADDR address
,
8649 struct type
*type
= ada_to_fixed_type (type0
, 0, address
, NULL
, 1);
8651 if (type
== type0
&& val0
!= NULL
)
8654 if (val0
->lval () != lval_memory
)
8656 /* Our value does not live in memory; it could be a convenience
8657 variable, for instance. Create a not_lval value using val0's
8659 return value_from_contents (type
, val0
->contents ().data ());
8662 return value_from_contents_and_address (type
, 0, address
);
8665 /* A value representing VAL, but with a standard (static-sized) type
8666 that correctly describes it. Does not necessarily create a new
8670 ada_to_fixed_value (struct value
*val
)
8672 val
= unwrap_value (val
);
8673 val
= ada_to_fixed_value_create (val
->type (), val
->address (), val
);
8680 /* Evaluate the 'POS attribute applied to ARG. */
8683 pos_atr (struct value
*arg
)
8685 struct value
*val
= coerce_ref (arg
);
8686 struct type
*type
= val
->type ();
8688 if (!discrete_type_p (type
))
8689 error (_("'POS only defined on discrete types"));
8691 std::optional
<LONGEST
> result
= discrete_position (type
, value_as_long (val
));
8692 if (!result
.has_value ())
8693 error (_("enumeration value is invalid: can't find 'POS"));
8699 ada_pos_atr (struct type
*expect_type
,
8700 struct expression
*exp
,
8701 enum noside noside
, enum exp_opcode op
,
8704 struct type
*type
= builtin_type (exp
->gdbarch
)->builtin_int
;
8705 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
8706 return value::zero (type
, not_lval
);
8707 return value_from_longest (type
, pos_atr (arg
));
8710 /* Evaluate the TYPE'VAL attribute applied to ARG. */
8712 static struct value
*
8713 val_atr (struct type
*type
, LONGEST val
)
8715 gdb_assert (discrete_type_p (type
));
8716 if (type
->code () == TYPE_CODE_RANGE
)
8717 type
= type
->target_type ();
8718 if (type
->code () == TYPE_CODE_ENUM
)
8720 if (val
< 0 || val
>= type
->num_fields ())
8721 error (_("argument to 'VAL out of range"));
8722 val
= type
->field (val
).loc_enumval ();
8724 return value_from_longest (type
, val
);
8728 ada_val_atr (struct expression
*exp
, enum noside noside
, struct type
*type
,
8731 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
8732 return value::zero (type
, not_lval
);
8734 if (!discrete_type_p (type
))
8735 error (_("'VAL only defined on discrete types"));
8736 if (!integer_type_p (arg
->type ()))
8737 error (_("'VAL requires integral argument"));
8739 return val_atr (type
, value_as_long (arg
));
8742 /* Implementation of the enum_rep attribute. */
8744 ada_atr_enum_rep (struct expression
*exp
, enum noside noside
, struct type
*type
,
8747 struct type
*inttype
= builtin_type (exp
->gdbarch
)->builtin_int
;
8748 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
8749 return value::zero (inttype
, not_lval
);
8751 if (type
->code () == TYPE_CODE_RANGE
)
8752 type
= type
->target_type ();
8753 if (type
->code () != TYPE_CODE_ENUM
)
8754 error (_("'Enum_Rep only defined on enum types"));
8755 if (!types_equal (type
, arg
->type ()))
8756 error (_("'Enum_Rep requires argument to have same type as enum"));
8758 return value_cast (inttype
, arg
);
8761 /* Implementation of the enum_val attribute. */
8763 ada_atr_enum_val (struct expression
*exp
, enum noside noside
, struct type
*type
,
8766 struct type
*original_type
= type
;
8767 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
8768 return value::zero (original_type
, not_lval
);
8770 if (type
->code () == TYPE_CODE_RANGE
)
8771 type
= type
->target_type ();
8772 if (type
->code () != TYPE_CODE_ENUM
)
8773 error (_("'Enum_Val only defined on enum types"));
8774 if (!integer_type_p (arg
->type ()))
8775 error (_("'Enum_Val requires integral argument"));
8777 LONGEST value
= value_as_long (arg
);
8778 for (int i
= 0; i
< type
->num_fields (); ++i
)
8780 if (type
->field (i
).loc_enumval () == value
)
8781 return value_from_longest (original_type
, value
);
8784 error (_("value %s not found in enum"), plongest (value
));
8791 /* True if TYPE appears to be an Ada character type.
8792 [At the moment, this is true only for Character and Wide_Character;
8793 It is a heuristic test that could stand improvement]. */
8796 ada_is_character_type (struct type
*type
)
8800 /* If the type code says it's a character, then assume it really is,
8801 and don't check any further. */
8802 if (type
->code () == TYPE_CODE_CHAR
)
8805 /* Otherwise, assume it's a character type iff it is a discrete type
8806 with a known character type name. */
8807 name
= ada_type_name (type
);
8808 return (name
!= NULL
8809 && (type
->code () == TYPE_CODE_INT
8810 || type
->code () == TYPE_CODE_RANGE
)
8811 && (strcmp (name
, "character") == 0
8812 || strcmp (name
, "wide_character") == 0
8813 || strcmp (name
, "wide_wide_character") == 0
8814 || strcmp (name
, "unsigned char") == 0));
8817 /* True if TYPE appears to be an Ada string type. */
8820 ada_is_string_type (struct type
*type
)
8822 type
= ada_check_typedef (type
);
8824 && type
->code () != TYPE_CODE_PTR
8825 && (ada_is_simple_array_type (type
)
8826 || ada_is_array_descriptor_type (type
))
8827 && ada_array_arity (type
) == 1)
8829 struct type
*elttype
= ada_array_element_type (type
, 1);
8831 return ada_is_character_type (elttype
);
8837 /* The compiler sometimes provides a parallel XVS type for a given
8838 PAD type. Normally, it is safe to follow the PAD type directly,
8839 but older versions of the compiler have a bug that causes the offset
8840 of its "F" field to be wrong. Following that field in that case
8841 would lead to incorrect results, but this can be worked around
8842 by ignoring the PAD type and using the associated XVS type instead.
8844 Set to True if the debugger should trust the contents of PAD types.
8845 Otherwise, ignore the PAD type if there is a parallel XVS type. */
8846 static bool trust_pad_over_xvs
= true;
8848 /* True if TYPE is a struct type introduced by the compiler to force the
8849 alignment of a value. Such types have a single field with a
8850 distinctive name. */
8853 ada_is_aligner_type (struct type
*type
)
8855 type
= ada_check_typedef (type
);
8857 if (!trust_pad_over_xvs
&& ada_find_parallel_type (type
, "___XVS") != NULL
)
8860 return (type
->code () == TYPE_CODE_STRUCT
8861 && type
->num_fields () == 1
8862 && strcmp (type
->field (0).name (), "F") == 0);
8865 /* If there is an ___XVS-convention type parallel to SUBTYPE, return
8866 the parallel type. */
8869 ada_get_base_type (struct type
*raw_type
)
8871 struct type
*real_type_namer
;
8872 struct type
*raw_real_type
;
8874 if (raw_type
== NULL
|| raw_type
->code () != TYPE_CODE_STRUCT
)
8877 if (ada_is_aligner_type (raw_type
))
8878 /* The encoding specifies that we should always use the aligner type.
8879 So, even if this aligner type has an associated XVS type, we should
8882 According to the compiler gurus, an XVS type parallel to an aligner
8883 type may exist because of a stabs limitation. In stabs, aligner
8884 types are empty because the field has a variable-sized type, and
8885 thus cannot actually be used as an aligner type. As a result,
8886 we need the associated parallel XVS type to decode the type.
8887 Since the policy in the compiler is to not change the internal
8888 representation based on the debugging info format, we sometimes
8889 end up having a redundant XVS type parallel to the aligner type. */
8892 real_type_namer
= ada_find_parallel_type (raw_type
, "___XVS");
8893 if (real_type_namer
== NULL
8894 || real_type_namer
->code () != TYPE_CODE_STRUCT
8895 || real_type_namer
->num_fields () != 1)
8898 if (real_type_namer
->field (0).type ()->code () != TYPE_CODE_REF
)
8900 /* This is an older encoding form where the base type needs to be
8901 looked up by name. We prefer the newer encoding because it is
8903 raw_real_type
= ada_find_any_type (real_type_namer
->field (0).name ());
8904 if (raw_real_type
== NULL
)
8907 return raw_real_type
;
8910 /* The field in our XVS type is a reference to the base type. */
8911 return real_type_namer
->field (0).type ()->target_type ();
8914 /* The type of value designated by TYPE, with all aligners removed. */
8917 ada_aligned_type (struct type
*type
)
8919 if (ada_is_aligner_type (type
))
8920 return ada_aligned_type (type
->field (0).type ());
8922 return ada_get_base_type (type
);
8926 /* The address of the aligned value in an object at address VALADDR
8927 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
8930 ada_aligned_value_addr (struct type
*type
, const gdb_byte
*valaddr
)
8932 if (ada_is_aligner_type (type
))
8933 return ada_aligned_value_addr
8934 (type
->field (0).type (),
8935 valaddr
+ type
->field (0).loc_bitpos () / TARGET_CHAR_BIT
);
8942 /* The printed representation of an enumeration literal with encoded
8943 name NAME. The value is good to the next call of ada_enum_name. */
8945 ada_enum_name (const char *name
)
8947 static std::string storage
;
8950 /* First, unqualify the enumeration name:
8951 1. Search for the last '.' character. If we find one, then skip
8952 all the preceding characters, the unqualified name starts
8953 right after that dot.
8954 2. Otherwise, we may be debugging on a target where the compiler
8955 translates dots into "__". Search forward for double underscores,
8956 but stop searching when we hit an overloading suffix, which is
8957 of the form "__" followed by digits. */
8959 tmp
= strrchr (name
, '.');
8964 while ((tmp
= strstr (name
, "__")) != NULL
)
8966 if (isdigit (tmp
[2]))
8977 if (name
[1] == 'U' || name
[1] == 'W')
8980 if (name
[1] == 'W' && name
[2] == 'W')
8982 /* Also handle the QWW case. */
8985 if (sscanf (name
+ offset
, "%x", &v
) != 1)
8988 else if (((name
[1] >= '0' && name
[1] <= '9')
8989 || (name
[1] >= 'a' && name
[1] <= 'z'))
8992 storage
= string_printf ("'%c'", name
[1]);
8993 return storage
.c_str ();
8998 if (isascii (v
) && isprint (v
))
8999 storage
= string_printf ("'%c'", v
);
9000 else if (name
[1] == 'U')
9001 storage
= string_printf ("'[\"%02x\"]'", v
);
9002 else if (name
[2] != 'W')
9003 storage
= string_printf ("'[\"%04x\"]'", v
);
9005 storage
= string_printf ("'[\"%06x\"]'", v
);
9007 return storage
.c_str ();
9011 tmp
= strstr (name
, "__");
9013 tmp
= strstr (name
, "$");
9016 storage
= std::string (name
, tmp
- name
);
9017 return storage
.c_str ();
9024 /* If TYPE is a dynamic type, return the base type. Otherwise, if
9025 there is no parallel type, return nullptr. */
9027 static struct type
*
9028 find_base_type (struct type
*type
)
9030 struct type
*raw_real_type
9031 = ada_check_typedef (ada_get_base_type (type
));
9033 /* No parallel XVS or XVE type. */
9034 if (type
== raw_real_type
9035 && ada_find_parallel_type (type
, "___XVE") == nullptr)
9038 return raw_real_type
;
9041 /* If VAL is wrapped in an aligner or subtype wrapper, return the
9044 static struct value
*
9045 unwrap_value (struct value
*val
)
9047 struct type
*type
= ada_check_typedef (val
->type ());
9049 if (ada_is_aligner_type (type
))
9051 struct value
*v
= ada_value_struct_elt (val
, "F", 0);
9052 struct type
*val_type
= ada_check_typedef (v
->type ());
9054 if (ada_type_name (val_type
) == NULL
)
9055 val_type
->set_name (ada_type_name (type
));
9057 return unwrap_value (v
);
9061 struct type
*raw_real_type
= find_base_type (type
);
9062 if (raw_real_type
== nullptr)
9066 coerce_unspec_val_to_type
9067 (val
, ada_to_fixed_type (raw_real_type
, 0,
9073 /* Given two array types T1 and T2, return nonzero iff both arrays
9074 contain the same number of elements. */
9077 ada_same_array_size_p (struct type
*t1
, struct type
*t2
)
9079 LONGEST lo1
, hi1
, lo2
, hi2
;
9081 /* Get the array bounds in order to verify that the size of
9082 the two arrays match. */
9083 if (!get_array_bounds (t1
, &lo1
, &hi1
)
9084 || !get_array_bounds (t2
, &lo2
, &hi2
))
9085 error (_("unable to determine array bounds"));
9087 /* To make things easier for size comparison, normalize a bit
9088 the case of empty arrays by making sure that the difference
9089 between upper bound and lower bound is always -1. */
9095 return (hi1
- lo1
== hi2
- lo2
);
9098 /* Assuming that VAL is an array of integrals, and TYPE represents
9099 an array with the same number of elements, but with wider integral
9100 elements, return an array "casted" to TYPE. In practice, this
9101 means that the returned array is built by casting each element
9102 of the original array into TYPE's (wider) element type. */
9104 static struct value
*
9105 ada_promote_array_of_integrals (struct type
*type
, struct value
*val
)
9107 struct type
*elt_type
= type
->target_type ();
9111 /* Verify that both val and type are arrays of scalars, and
9112 that the size of val's elements is smaller than the size
9113 of type's element. */
9114 gdb_assert (type
->code () == TYPE_CODE_ARRAY
);
9115 gdb_assert (is_integral_type (type
->target_type ()));
9116 gdb_assert (val
->type ()->code () == TYPE_CODE_ARRAY
);
9117 gdb_assert (is_integral_type (val
->type ()->target_type ()));
9118 gdb_assert (type
->target_type ()->length ()
9119 > val
->type ()->target_type ()->length ());
9121 if (!get_array_bounds (type
, &lo
, &hi
))
9122 error (_("unable to determine array bounds"));
9124 value
*res
= value::allocate (type
);
9125 gdb::array_view
<gdb_byte
> res_contents
= res
->contents_writeable ();
9127 /* Promote each array element. */
9128 for (i
= 0; i
< hi
- lo
+ 1; i
++)
9130 struct value
*elt
= value_cast (elt_type
, value_subscript (val
, lo
+ i
));
9131 int elt_len
= elt_type
->length ();
9133 copy (elt
->contents_all (), res_contents
.slice (elt_len
* i
, elt_len
));
9139 /* Coerce VAL as necessary for assignment to an lval of type TYPE, and
9140 return the converted value. */
9142 static struct value
*
9143 coerce_for_assign (struct type
*type
, struct value
*val
)
9145 struct type
*type2
= val
->type ();
9150 type2
= ada_check_typedef (type2
);
9151 type
= ada_check_typedef (type
);
9153 if (type2
->code () == TYPE_CODE_PTR
9154 && type
->code () == TYPE_CODE_ARRAY
)
9156 val
= ada_value_ind (val
);
9157 type2
= val
->type ();
9160 if (type2
->code () == TYPE_CODE_ARRAY
9161 && type
->code () == TYPE_CODE_ARRAY
)
9163 if (!ada_same_array_size_p (type
, type2
))
9164 error (_("cannot assign arrays of different length"));
9166 if (is_integral_type (type
->target_type ())
9167 && is_integral_type (type2
->target_type ())
9168 && type2
->target_type ()->length () < type
->target_type ()->length ())
9170 /* Allow implicit promotion of the array elements to
9172 return ada_promote_array_of_integrals (type
, val
);
9175 if (type2
->target_type ()->length () != type
->target_type ()->length ())
9176 error (_("Incompatible types in assignment"));
9177 val
->deprecated_set_type (type
);
9182 static struct value
*
9183 ada_value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
9185 struct type
*type1
, *type2
;
9187 arg1
= coerce_ref (arg1
);
9188 arg2
= coerce_ref (arg2
);
9189 type1
= get_base_type (ada_check_typedef (arg1
->type ()));
9190 type2
= get_base_type (ada_check_typedef (arg2
->type ()));
9192 if (type1
->code () != TYPE_CODE_INT
9193 || type2
->code () != TYPE_CODE_INT
)
9194 return value_binop (arg1
, arg2
, op
);
9203 return value_binop (arg1
, arg2
, op
);
9206 gdb_mpz v2
= value_as_mpz (arg2
);
9210 if (op
== BINOP_MOD
)
9212 else if (op
== BINOP_DIV
)
9216 gdb_assert (op
== BINOP_REM
);
9220 error (_("second operand of %s must not be zero."), name
);
9223 if (type1
->is_unsigned () || op
== BINOP_MOD
)
9224 return value_binop (arg1
, arg2
, op
);
9226 gdb_mpz v1
= value_as_mpz (arg1
);
9239 /* Should not reach this point. */
9240 gdb_assert_not_reached ("invalid operator");
9243 return value_from_mpz (type1
, v
);
9247 ada_value_equal (struct value
*arg1
, struct value
*arg2
)
9249 if (ada_is_direct_array_type (arg1
->type ())
9250 || ada_is_direct_array_type (arg2
->type ()))
9252 struct type
*arg1_type
, *arg2_type
;
9254 /* Automatically dereference any array reference before
9255 we attempt to perform the comparison. */
9256 arg1
= ada_coerce_ref (arg1
);
9257 arg2
= ada_coerce_ref (arg2
);
9259 arg1
= ada_coerce_to_simple_array (arg1
);
9260 arg2
= ada_coerce_to_simple_array (arg2
);
9262 arg1_type
= ada_check_typedef (arg1
->type ());
9263 arg2_type
= ada_check_typedef (arg2
->type ());
9265 if (arg1_type
->code () != TYPE_CODE_ARRAY
9266 || arg2_type
->code () != TYPE_CODE_ARRAY
)
9267 error (_("Attempt to compare array with non-array"));
9268 /* FIXME: The following works only for types whose
9269 representations use all bits (no padding or undefined bits)
9270 and do not have user-defined equality. */
9271 return (arg1_type
->length () == arg2_type
->length ()
9272 && memcmp (arg1
->contents ().data (),
9273 arg2
->contents ().data (),
9274 arg1_type
->length ()) == 0);
9276 return value_equal (arg1
, arg2
);
9283 check_objfile (const std::unique_ptr
<ada_component
> &comp
,
9284 struct objfile
*objfile
)
9286 return comp
->uses_objfile (objfile
);
9289 /* See ada-exp.h. */
9292 aggregate_assigner::assign (LONGEST index
, operation_up
&arg
)
9294 scoped_value_mark mark
;
9297 struct type
*lhs_type
= check_typedef (lhs
->type ());
9299 if (lhs_type
->code () == TYPE_CODE_ARRAY
)
9301 struct type
*index_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
9302 struct value
*index_val
= value_from_longest (index_type
, index
);
9304 elt
= unwrap_value (ada_value_subscript (lhs
, 1, &index_val
));
9308 elt
= ada_index_struct_field (index
, lhs
, 0, lhs
->type ());
9309 elt
= ada_to_fixed_value (elt
);
9312 scoped_restore save_index
= make_scoped_restore (&m_current_index
, index
);
9314 ada_aggregate_operation
*ag_op
9315 = dynamic_cast<ada_aggregate_operation
*> (arg
.get ());
9316 if (ag_op
!= nullptr)
9317 ag_op
->assign_aggregate (container
, elt
, exp
);
9319 value_assign_to_component (container
, elt
,
9320 arg
->evaluate (nullptr, exp
,
9324 /* See ada-exp.h. */
9327 aggregate_assigner::current_value () const
9329 /* Note that using an integer type here is incorrect -- the type
9330 should be the array's index type. Unfortunately, though, this
9331 isn't currently available during parsing and type resolution. */
9332 struct type
*index_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
9333 return value_from_longest (index_type
, m_current_index
);
9337 ada_aggregate_component::uses_objfile (struct objfile
*objfile
)
9339 if (m_base
!= nullptr && m_base
->uses_objfile (objfile
))
9341 for (const auto &item
: m_components
)
9342 if (item
->uses_objfile (objfile
))
9348 ada_aggregate_component::dump (ui_file
*stream
, int depth
)
9350 gdb_printf (stream
, _("%*sAggregate\n"), depth
, "");
9351 if (m_base
!= nullptr)
9353 gdb_printf (stream
, _("%*swith delta\n"), depth
+ 1, "");
9354 m_base
->dump (stream
, depth
+ 2);
9356 for (const auto &item
: m_components
)
9357 item
->dump (stream
, depth
+ 1);
9361 ada_aggregate_component::assign (aggregate_assigner
&assigner
)
9363 if (m_base
!= nullptr)
9365 value
*base
= m_base
->evaluate (nullptr, assigner
.exp
, EVAL_NORMAL
);
9366 if (ada_is_direct_array_type (base
->type ()))
9367 base
= ada_coerce_to_simple_array (base
);
9368 if (!types_deeply_equal (assigner
.container
->type (), base
->type ()))
9369 error (_("Type mismatch in delta aggregate"));
9370 value_assign_to_component (assigner
.container
, assigner
.container
,
9374 for (auto &item
: m_components
)
9375 item
->assign (assigner
);
9378 /* See ada-exp.h. */
9380 ada_aggregate_component::ada_aggregate_component
9381 (operation_up
&&base
, std::vector
<ada_component_up
> &&components
)
9382 : m_base (std::move (base
)),
9383 m_components (std::move (components
))
9385 for (const auto &component
: m_components
)
9386 if (dynamic_cast<const ada_others_component
*> (component
.get ())
9389 /* It's invalid and nonsensical to have 'others => ...' with a
9390 delta aggregate. It was simpler to enforce this
9391 restriction here as opposed to in the parser. */
9392 error (_("'others' invalid in delta aggregate"));
9396 /* See ada-exp.h. */
9399 ada_aggregate_operation::assign_aggregate (struct value
*container
,
9401 struct expression
*exp
)
9403 struct type
*lhs_type
;
9404 aggregate_assigner assigner
;
9406 container
= ada_coerce_ref (container
);
9407 if (ada_is_direct_array_type (container
->type ()))
9408 container
= ada_coerce_to_simple_array (container
);
9409 lhs
= ada_coerce_ref (lhs
);
9410 if (!lhs
->deprecated_modifiable ())
9411 error (_("Left operand of assignment is not a modifiable lvalue."));
9413 lhs_type
= check_typedef (lhs
->type ());
9414 if (ada_is_direct_array_type (lhs_type
))
9416 lhs
= ada_coerce_to_simple_array (lhs
);
9417 lhs_type
= check_typedef (lhs
->type ());
9418 assigner
.low
= lhs_type
->bounds ()->low
.const_val ();
9419 assigner
.high
= lhs_type
->bounds ()->high
.const_val ();
9421 else if (lhs_type
->code () == TYPE_CODE_STRUCT
)
9424 assigner
.high
= num_visible_fields (lhs_type
) - 1;
9427 error (_("Left-hand side must be array or record."));
9429 assigner
.indices
.push_back (assigner
.low
- 1);
9430 assigner
.indices
.push_back (assigner
.low
- 1);
9431 assigner
.indices
.push_back (assigner
.high
+ 1);
9432 assigner
.indices
.push_back (assigner
.high
+ 1);
9434 assigner
.container
= container
;
9438 std::get
<0> (m_storage
)->assign (assigner
);
9444 ada_positional_component::uses_objfile (struct objfile
*objfile
)
9446 return m_op
->uses_objfile (objfile
);
9450 ada_positional_component::dump (ui_file
*stream
, int depth
)
9452 gdb_printf (stream
, _("%*sPositional, index = %d\n"),
9453 depth
, "", m_index
);
9454 m_op
->dump (stream
, depth
+ 1);
9457 /* Assign into the component of LHS indexed by the OP_POSITIONAL
9458 construct, given that the positions are relative to lower bound
9459 LOW, where HIGH is the upper bound. Record the position in
9460 INDICES. CONTAINER is as for assign_aggregate. */
9462 ada_positional_component::assign (aggregate_assigner
&assigner
)
9464 LONGEST ind
= m_index
+ assigner
.low
;
9466 if (ind
- 1 == assigner
.high
)
9467 warning (_("Extra components in aggregate ignored."));
9468 if (ind
<= assigner
.high
)
9470 assigner
.add_interval (ind
, ind
);
9471 assigner
.assign (ind
, m_op
);
9476 ada_discrete_range_association::uses_objfile (struct objfile
*objfile
)
9478 return m_low
->uses_objfile (objfile
) || m_high
->uses_objfile (objfile
);
9482 ada_discrete_range_association::dump (ui_file
*stream
, int depth
)
9484 gdb_printf (stream
, _("%*sDiscrete range:\n"), depth
, "");
9485 m_low
->dump (stream
, depth
+ 1);
9486 m_high
->dump (stream
, depth
+ 1);
9490 ada_discrete_range_association::assign (aggregate_assigner
&assigner
,
9493 LONGEST lower
= value_as_long (m_low
->evaluate (nullptr, assigner
.exp
,
9495 LONGEST upper
= value_as_long (m_high
->evaluate (nullptr, assigner
.exp
,
9498 if (lower
<= upper
&& (lower
< assigner
.low
|| upper
> assigner
.high
))
9499 error (_("Index in component association out of bounds."));
9501 assigner
.add_interval (lower
, upper
);
9502 while (lower
<= upper
)
9504 assigner
.assign (lower
, op
);
9510 ada_name_association::uses_objfile (struct objfile
*objfile
)
9512 return m_val
->uses_objfile (objfile
);
9516 ada_name_association::dump (ui_file
*stream
, int depth
)
9518 gdb_printf (stream
, _("%*sName:\n"), depth
, "");
9519 m_val
->dump (stream
, depth
+ 1);
9523 ada_name_association::assign (aggregate_assigner
&assigner
,
9528 if (ada_is_direct_array_type (assigner
.lhs
->type ()))
9530 value
*tem
= m_val
->evaluate (nullptr, assigner
.exp
, EVAL_NORMAL
);
9531 index
= longest_to_int (value_as_long (tem
));
9535 ada_string_operation
*strop
9536 = dynamic_cast<ada_string_operation
*> (m_val
.get ());
9539 if (strop
!= nullptr)
9540 name
= strop
->get_name ();
9543 ada_var_value_operation
*vvo
9544 = dynamic_cast<ada_var_value_operation
*> (m_val
.get ());
9546 error (_("Invalid record component association."));
9547 name
= vvo
->get_symbol ()->natural_name ();
9548 /* In this scenario, the user wrote (name => expr), but
9549 write_name_assoc found some fully-qualified name and
9550 substituted it. This happens because, at parse time, the
9551 meaning of the expression isn't known; but here we know
9552 that just the base name was supplied and it refers to the
9554 name
= ada_unqualified_name (name
);
9558 if (! find_struct_field (name
, assigner
.lhs
->type (), 0,
9559 NULL
, NULL
, NULL
, NULL
, &index
))
9560 error (_("Unknown component name: %s."), name
);
9563 assigner
.add_interval (index
, index
);
9564 assigner
.assign (index
, op
);
9568 ada_choices_component::uses_objfile (struct objfile
*objfile
)
9570 if (m_op
->uses_objfile (objfile
))
9572 for (const auto &item
: m_assocs
)
9573 if (item
->uses_objfile (objfile
))
9579 ada_choices_component::dump (ui_file
*stream
, int depth
)
9581 if (m_name
.empty ())
9582 gdb_printf (stream
, _("%*sChoices:\n"), depth
, "");
9585 gdb_printf (stream
, _("%*sIterated choices:\n"), depth
, "");
9586 gdb_printf (stream
, _("%*sName: %s\n"), depth
+ 1, "", m_name
.c_str ());
9588 m_op
->dump (stream
, depth
+ 1);
9590 for (const auto &item
: m_assocs
)
9591 item
->dump (stream
, depth
+ 1);
9594 /* Assign into the components of LHS indexed by the OP_CHOICES
9595 construct at *POS, updating *POS past the construct, given that
9596 the allowable indices are LOW..HIGH. Record the indices assigned
9597 to in INDICES. CONTAINER is as for assign_aggregate. */
9599 ada_choices_component::assign (aggregate_assigner
&assigner
)
9601 scoped_restore save_index
= make_scoped_restore (&m_assigner
, &assigner
);
9602 for (auto &item
: m_assocs
)
9603 item
->assign (assigner
, m_op
);
9607 ada_index_var_operation::dump (struct ui_file
*stream
, int depth
) const
9609 gdb_printf (stream
, _("%*sIndex variable: %s\n"), depth
, "",
9610 m_var
->name ().c_str ());
9614 ada_index_var_operation::evaluate (struct type
*expect_type
,
9615 struct expression
*exp
,
9618 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
9620 /* Note that using an integer type here is incorrect -- the type
9621 should be the array's index type. Unfortunately, though,
9622 this isn't currently available during parsing and type
9624 struct type
*index_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
9625 return value::zero (index_type
, not_lval
);
9628 return m_var
->current_value ();
9632 ada_others_component::uses_objfile (struct objfile
*objfile
)
9634 return m_op
->uses_objfile (objfile
);
9638 ada_others_component::dump (ui_file
*stream
, int depth
)
9640 gdb_printf (stream
, _("%*sOthers:\n"), depth
, "");
9641 m_op
->dump (stream
, depth
+ 1);
9644 /* Assign the value of the expression in the OP_OTHERS construct in
9645 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
9646 have not been previously assigned. The index intervals already assigned
9647 are in INDICES. CONTAINER is as for assign_aggregate. */
9649 ada_others_component::assign (aggregate_assigner
&assigner
)
9651 int num_indices
= assigner
.indices
.size ();
9652 for (int i
= 0; i
< num_indices
- 2; i
+= 2)
9654 for (LONGEST ind
= assigner
.indices
[i
+ 1] + 1;
9655 ind
< assigner
.indices
[i
+ 2];
9657 assigner
.assign (ind
, m_op
);
9662 ada_assign_operation::evaluate (struct type
*expect_type
,
9663 struct expression
*exp
,
9666 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
9667 scoped_restore save_lhs
= make_scoped_restore (&m_current
, arg1
);
9669 ada_aggregate_operation
*ag_op
9670 = dynamic_cast<ada_aggregate_operation
*> (std::get
<1> (m_storage
).get ());
9671 if (ag_op
!= nullptr)
9673 if (noside
!= EVAL_NORMAL
)
9676 arg1
= ag_op
->assign_aggregate (arg1
, arg1
, exp
);
9677 return ada_value_assign (arg1
, arg1
);
9679 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
9680 except if the lhs of our assignment is a convenience variable.
9681 In the case of assigning to a convenience variable, the lhs
9682 should be exactly the result of the evaluation of the rhs. */
9683 struct type
*type
= arg1
->type ();
9684 if (arg1
->lval () == lval_internalvar
)
9686 value
*arg2
= std::get
<1> (m_storage
)->evaluate (type
, exp
, noside
);
9687 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
9689 if (arg1
->lval () == lval_internalvar
)
9694 arg2
= coerce_for_assign (arg1
->type (), arg2
);
9695 return ada_value_assign (arg1
, arg2
);
9698 /* See ada-exp.h. */
9701 aggregate_assigner::add_interval (LONGEST from
, LONGEST to
)
9705 int size
= indices
.size ();
9706 for (i
= 0; i
< size
; i
+= 2) {
9707 if (to
>= indices
[i
] && from
<= indices
[i
+ 1])
9711 for (kh
= i
+ 2; kh
< size
; kh
+= 2)
9712 if (to
< indices
[kh
])
9714 if (from
< indices
[i
])
9716 indices
[i
+ 1] = indices
[kh
- 1];
9717 if (to
> indices
[i
+ 1])
9718 indices
[i
+ 1] = to
;
9719 memcpy (indices
.data () + i
+ 2, indices
.data () + kh
, size
- kh
);
9720 indices
.resize (kh
- i
- 2);
9723 else if (to
< indices
[i
])
9727 indices
.resize (indices
.size () + 2);
9728 for (j
= indices
.size () - 1; j
>= i
+ 2; j
-= 1)
9729 indices
[j
] = indices
[j
- 2];
9731 indices
[i
+ 1] = to
;
9734 } /* namespace expr */
9736 /* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
9739 static struct value
*
9740 ada_value_cast (struct type
*type
, struct value
*arg2
)
9742 if (type
== ada_check_typedef (arg2
->type ()))
9745 return value_cast (type
, arg2
);
9748 /* Evaluating Ada expressions, and printing their result.
9749 ------------------------------------------------------
9754 We usually evaluate an Ada expression in order to print its value.
9755 We also evaluate an expression in order to print its type, which
9756 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
9757 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
9758 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
9759 the evaluation compared to the EVAL_NORMAL, but is otherwise very
9762 Evaluating expressions is a little more complicated for Ada entities
9763 than it is for entities in languages such as C. The main reason for
9764 this is that Ada provides types whose definition might be dynamic.
9765 One example of such types is variant records. Or another example
9766 would be an array whose bounds can only be known at run time.
9768 The following description is a general guide as to what should be
9769 done (and what should NOT be done) in order to evaluate an expression
9770 involving such types, and when. This does not cover how the semantic
9771 information is encoded by GNAT as this is covered separatly. For the
9772 document used as the reference for the GNAT encoding, see exp_dbug.ads
9773 in the GNAT sources.
9775 Ideally, we should embed each part of this description next to its
9776 associated code. Unfortunately, the amount of code is so vast right
9777 now that it's hard to see whether the code handling a particular
9778 situation might be duplicated or not. One day, when the code is
9779 cleaned up, this guide might become redundant with the comments
9780 inserted in the code, and we might want to remove it.
9782 2. ``Fixing'' an Entity, the Simple Case:
9783 -----------------------------------------
9785 When evaluating Ada expressions, the tricky issue is that they may
9786 reference entities whose type contents and size are not statically
9787 known. Consider for instance a variant record:
9789 type Rec (Empty : Boolean := True) is record
9792 when False => Value : Integer;
9795 Yes : Rec := (Empty => False, Value => 1);
9796 No : Rec := (empty => True);
9798 The size and contents of that record depends on the value of the
9799 discriminant (Rec.Empty). At this point, neither the debugging
9800 information nor the associated type structure in GDB are able to
9801 express such dynamic types. So what the debugger does is to create
9802 "fixed" versions of the type that applies to the specific object.
9803 We also informally refer to this operation as "fixing" an object,
9804 which means creating its associated fixed type.
9806 Example: when printing the value of variable "Yes" above, its fixed
9807 type would look like this:
9814 On the other hand, if we printed the value of "No", its fixed type
9821 Things become a little more complicated when trying to fix an entity
9822 with a dynamic type that directly contains another dynamic type,
9823 such as an array of variant records, for instance. There are
9824 two possible cases: Arrays, and records.
9826 3. ``Fixing'' Arrays:
9827 ---------------------
9829 The type structure in GDB describes an array in terms of its bounds,
9830 and the type of its elements. By design, all elements in the array
9831 have the same type and we cannot represent an array of variant elements
9832 using the current type structure in GDB. When fixing an array,
9833 we cannot fix the array element, as we would potentially need one
9834 fixed type per element of the array. As a result, the best we can do
9835 when fixing an array is to produce an array whose bounds and size
9836 are correct (allowing us to read it from memory), but without having
9837 touched its element type. Fixing each element will be done later,
9838 when (if) necessary.
9840 Arrays are a little simpler to handle than records, because the same
9841 amount of memory is allocated for each element of the array, even if
9842 the amount of space actually used by each element differs from element
9843 to element. Consider for instance the following array of type Rec:
9845 type Rec_Array is array (1 .. 2) of Rec;
9847 The actual amount of memory occupied by each element might be different
9848 from element to element, depending on the value of their discriminant.
9849 But the amount of space reserved for each element in the array remains
9850 fixed regardless. So we simply need to compute that size using
9851 the debugging information available, from which we can then determine
9852 the array size (we multiply the number of elements of the array by
9853 the size of each element).
9855 The simplest case is when we have an array of a constrained element
9856 type. For instance, consider the following type declarations:
9858 type Bounded_String (Max_Size : Integer) is
9860 Buffer : String (1 .. Max_Size);
9862 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
9864 In this case, the compiler describes the array as an array of
9865 variable-size elements (identified by its XVS suffix) for which
9866 the size can be read in the parallel XVZ variable.
9868 In the case of an array of an unconstrained element type, the compiler
9869 wraps the array element inside a private PAD type. This type should not
9870 be shown to the user, and must be "unwrap"'ed before printing. Note
9871 that we also use the adjective "aligner" in our code to designate
9872 these wrapper types.
9874 In some cases, the size allocated for each element is statically
9875 known. In that case, the PAD type already has the correct size,
9876 and the array element should remain unfixed.
9878 But there are cases when this size is not statically known.
9879 For instance, assuming that "Five" is an integer variable:
9881 type Dynamic is array (1 .. Five) of Integer;
9882 type Wrapper (Has_Length : Boolean := False) is record
9885 when True => Length : Integer;
9889 type Wrapper_Array is array (1 .. 2) of Wrapper;
9891 Hello : Wrapper_Array := (others => (Has_Length => True,
9892 Data => (others => 17),
9896 The debugging info would describe variable Hello as being an
9897 array of a PAD type. The size of that PAD type is not statically
9898 known, but can be determined using a parallel XVZ variable.
9899 In that case, a copy of the PAD type with the correct size should
9900 be used for the fixed array.
9902 3. ``Fixing'' record type objects:
9903 ----------------------------------
9905 Things are slightly different from arrays in the case of dynamic
9906 record types. In this case, in order to compute the associated
9907 fixed type, we need to determine the size and offset of each of
9908 its components. This, in turn, requires us to compute the fixed
9909 type of each of these components.
9911 Consider for instance the example:
9913 type Bounded_String (Max_Size : Natural) is record
9914 Str : String (1 .. Max_Size);
9917 My_String : Bounded_String (Max_Size => 10);
9919 In that case, the position of field "Length" depends on the size
9920 of field Str, which itself depends on the value of the Max_Size
9921 discriminant. In order to fix the type of variable My_String,
9922 we need to fix the type of field Str. Therefore, fixing a variant
9923 record requires us to fix each of its components.
9925 However, if a component does not have a dynamic size, the component
9926 should not be fixed. In particular, fields that use a PAD type
9927 should not fixed. Here is an example where this might happen
9928 (assuming type Rec above):
9930 type Container (Big : Boolean) is record
9934 when True => Another : Integer;
9938 My_Container : Container := (Big => False,
9939 First => (Empty => True),
9942 In that example, the compiler creates a PAD type for component First,
9943 whose size is constant, and then positions the component After just
9944 right after it. The offset of component After is therefore constant
9947 The debugger computes the position of each field based on an algorithm
9948 that uses, among other things, the actual position and size of the field
9949 preceding it. Let's now imagine that the user is trying to print
9950 the value of My_Container. If the type fixing was recursive, we would
9951 end up computing the offset of field After based on the size of the
9952 fixed version of field First. And since in our example First has
9953 only one actual field, the size of the fixed type is actually smaller
9954 than the amount of space allocated to that field, and thus we would
9955 compute the wrong offset of field After.
9957 To make things more complicated, we need to watch out for dynamic
9958 components of variant records (identified by the ___XVL suffix in
9959 the component name). Even if the target type is a PAD type, the size
9960 of that type might not be statically known. So the PAD type needs
9961 to be unwrapped and the resulting type needs to be fixed. Otherwise,
9962 we might end up with the wrong size for our component. This can be
9963 observed with the following type declarations:
9965 type Octal is new Integer range 0 .. 7;
9966 type Octal_Array is array (Positive range <>) of Octal;
9967 pragma Pack (Octal_Array);
9969 type Octal_Buffer (Size : Positive) is record
9970 Buffer : Octal_Array (1 .. Size);
9974 In that case, Buffer is a PAD type whose size is unset and needs
9975 to be computed by fixing the unwrapped type.
9977 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
9978 ----------------------------------------------------------
9980 Lastly, when should the sub-elements of an entity that remained unfixed
9981 thus far, be actually fixed?
9983 The answer is: Only when referencing that element. For instance
9984 when selecting one component of a record, this specific component
9985 should be fixed at that point in time. Or when printing the value
9986 of a record, each component should be fixed before its value gets
9987 printed. Similarly for arrays, the element of the array should be
9988 fixed when printing each element of the array, or when extracting
9989 one element out of that array. On the other hand, fixing should
9990 not be performed on the elements when taking a slice of an array!
9992 Note that one of the side effects of miscomputing the offset and
9993 size of each field is that we end up also miscomputing the size
9994 of the containing type. This can have adverse results when computing
9995 the value of an entity. GDB fetches the value of an entity based
9996 on the size of its type, and thus a wrong size causes GDB to fetch
9997 the wrong amount of memory. In the case where the computed size is
9998 too small, GDB fetches too little data to print the value of our
9999 entity. Results in this case are unpredictable, as we usually read
10000 past the buffer containing the data =:-o. */
10002 /* A helper function for TERNOP_IN_RANGE. */
10005 eval_ternop_in_range (struct type
*expect_type
, struct expression
*exp
,
10006 enum noside noside
,
10007 value
*arg1
, value
*arg2
, value
*arg3
)
10009 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10010 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg3
);
10011 struct type
*type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
10013 value_from_longest (type
,
10014 (value_less (arg1
, arg3
)
10015 || value_equal (arg1
, arg3
))
10016 && (value_less (arg2
, arg1
)
10017 || value_equal (arg2
, arg1
)));
10020 /* A helper function for UNOP_NEG. */
10023 ada_unop_neg (struct type
*expect_type
,
10024 struct expression
*exp
,
10025 enum noside noside
, enum exp_opcode op
,
10026 struct value
*arg1
)
10028 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
10029 return value_neg (arg1
);
10032 /* A helper function for UNOP_IN_RANGE. */
10035 ada_unop_in_range (struct type
*expect_type
,
10036 struct expression
*exp
,
10037 enum noside noside
, enum exp_opcode op
,
10038 struct value
*arg1
, struct type
*type
)
10040 struct value
*arg2
, *arg3
;
10041 switch (type
->code ())
10044 lim_warning (_("Membership test incompletely implemented; "
10045 "always returns true"));
10046 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
10047 return value_from_longest (type
, 1);
10049 case TYPE_CODE_RANGE
:
10050 arg2
= value_from_longest (type
,
10051 type
->bounds ()->low
.const_val ());
10052 arg3
= value_from_longest (type
,
10053 type
->bounds ()->high
.const_val ());
10054 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10055 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg3
);
10056 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
10058 value_from_longest (type
,
10059 (value_less (arg1
, arg3
)
10060 || value_equal (arg1
, arg3
))
10061 && (value_less (arg2
, arg1
)
10062 || value_equal (arg2
, arg1
)));
10066 /* A helper function for OP_ATR_TAG. */
10069 ada_atr_tag (struct type
*expect_type
,
10070 struct expression
*exp
,
10071 enum noside noside
, enum exp_opcode op
,
10072 struct value
*arg1
)
10074 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10075 return value::zero (ada_tag_type (arg1
), not_lval
);
10077 return ada_value_tag (arg1
);
10084 ada_atr_size_operation::evaluate (struct type
*expect_type
,
10085 struct expression
*exp
,
10086 enum noside noside
)
10088 bool is_type
= std::get
<0> (m_storage
)->opcode () == OP_TYPE
;
10089 bool is_size
= std::get
<1> (m_storage
);
10091 enum noside sub_noside
= is_type
? EVAL_AVOID_SIDE_EFFECTS
: noside
;
10092 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, sub_noside
);
10093 struct type
*type
= ada_check_typedef (val
->type ());
10098 error (_("gdb cannot apply 'Size to a type"));
10099 if (is_dynamic_type (type
) || find_base_type (type
) != nullptr)
10100 error (_("cannot apply 'Object_Size to dynamic type"));
10103 /* If the argument is a reference, then dereference its type, since
10104 the user is really asking for the size of the actual object,
10105 not the size of the pointer. */
10106 if (type
->code () == TYPE_CODE_REF
)
10107 type
= type
->target_type ();
10109 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10110 return value::zero (builtin_type (exp
->gdbarch
)->builtin_int
, not_lval
);
10112 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
10113 TARGET_CHAR_BIT
* type
->length ());
10116 } /* namespace expr */
10118 /* A helper function for UNOP_ABS. */
10121 ada_abs (struct type
*expect_type
,
10122 struct expression
*exp
,
10123 enum noside noside
, enum exp_opcode op
,
10124 struct value
*arg1
)
10126 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
10127 if (value_less (arg1
, value::zero (arg1
->type (), not_lval
)))
10128 return value_neg (arg1
);
10133 /* A helper function for BINOP_MUL. */
10136 ada_mult_binop (struct type
*expect_type
,
10137 struct expression
*exp
,
10138 enum noside noside
, enum exp_opcode op
,
10139 struct value
*arg1
, struct value
*arg2
)
10141 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10143 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10144 return value::zero (arg1
->type (), not_lval
);
10148 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10149 return ada_value_binop (arg1
, arg2
, op
);
10153 /* A helper function for BINOP_EQUAL and BINOP_NOTEQUAL. */
10156 ada_equal_binop (struct type
*expect_type
,
10157 struct expression
*exp
,
10158 enum noside noside
, enum exp_opcode op
,
10159 struct value
*arg1
, struct value
*arg2
)
10162 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10166 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10167 tem
= ada_value_equal (arg1
, arg2
);
10169 if (op
== BINOP_NOTEQUAL
)
10171 struct type
*type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
10172 return value_from_longest (type
, tem
);
10175 /* A helper function for TERNOP_SLICE. */
10178 ada_ternop_slice (struct expression
*exp
,
10179 enum noside noside
,
10180 struct value
*array
, struct value
*low_bound_val
,
10181 struct value
*high_bound_val
)
10184 LONGEST high_bound
;
10186 low_bound_val
= coerce_ref (low_bound_val
);
10187 high_bound_val
= coerce_ref (high_bound_val
);
10188 low_bound
= value_as_long (low_bound_val
);
10189 high_bound
= value_as_long (high_bound_val
);
10191 /* If this is a reference to an aligner type, then remove all
10193 if (array
->type ()->code () == TYPE_CODE_REF
10194 && ada_is_aligner_type (array
->type ()->target_type ()))
10195 array
->type ()->set_target_type
10196 (ada_aligned_type (array
->type ()->target_type ()));
10198 if (ada_is_any_packed_array_type (array
->type ()))
10199 error (_("cannot slice a packed array"));
10201 /* If this is a reference to an array or an array lvalue,
10202 convert to a pointer. */
10203 if (array
->type ()->code () == TYPE_CODE_REF
10204 || (array
->type ()->code () == TYPE_CODE_ARRAY
10205 && array
->lval () == lval_memory
))
10206 array
= value_addr (array
);
10208 if (noside
== EVAL_AVOID_SIDE_EFFECTS
10209 && ada_is_array_descriptor_type (ada_check_typedef
10211 return empty_array (ada_type_of_array (array
, 0), low_bound
,
10214 array
= ada_coerce_to_simple_array_ptr (array
);
10216 /* If we have more than one level of pointer indirection,
10217 dereference the value until we get only one level. */
10218 while (array
->type ()->code () == TYPE_CODE_PTR
10219 && (array
->type ()->target_type ()->code ()
10221 array
= value_ind (array
);
10223 /* Make sure we really do have an array type before going further,
10224 to avoid a SEGV when trying to get the index type or the target
10225 type later down the road if the debug info generated by
10226 the compiler is incorrect or incomplete. */
10227 if (!ada_is_simple_array_type (array
->type ()))
10228 error (_("cannot take slice of non-array"));
10230 if (ada_check_typedef (array
->type ())->code ()
10233 struct type
*type0
= ada_check_typedef (array
->type ());
10235 if (high_bound
< low_bound
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
10236 return empty_array (type0
->target_type (), low_bound
, high_bound
);
10239 struct type
*arr_type0
=
10240 to_fixed_array_type (type0
->target_type (), NULL
, 1);
10242 return ada_value_slice_from_ptr (array
, arr_type0
,
10243 longest_to_int (low_bound
),
10244 longest_to_int (high_bound
));
10247 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10249 else if (high_bound
< low_bound
)
10250 return empty_array (array
->type (), low_bound
, high_bound
);
10252 return ada_value_slice (array
, longest_to_int (low_bound
),
10253 longest_to_int (high_bound
));
10256 /* A helper function for BINOP_IN_BOUNDS. */
10259 ada_binop_in_bounds (struct expression
*exp
, enum noside noside
,
10260 struct value
*arg1
, struct value
*arg2
, int n
)
10262 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10264 struct type
*type
= language_bool_type (exp
->language_defn
,
10266 return value::zero (type
, not_lval
);
10269 struct type
*type
= ada_index_type (arg2
->type (), n
, "range");
10271 type
= arg1
->type ();
10273 value
*arg3
= value_from_longest (type
, ada_array_bound (arg2
, n
, 1));
10274 arg2
= value_from_longest (type
, ada_array_bound (arg2
, n
, 0));
10276 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10277 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg3
);
10278 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
10279 return value_from_longest (type
,
10280 (value_less (arg1
, arg3
)
10281 || value_equal (arg1
, arg3
))
10282 && (value_less (arg2
, arg1
)
10283 || value_equal (arg2
, arg1
)));
10286 /* A helper function for some attribute operations. */
10289 ada_unop_atr (struct expression
*exp
, enum noside noside
, enum exp_opcode op
,
10290 struct value
*arg1
, struct type
*type_arg
, int tem
)
10292 const char *attr_name
= nullptr;
10293 if (op
== OP_ATR_FIRST
)
10294 attr_name
= "first";
10295 else if (op
== OP_ATR_LAST
)
10296 attr_name
= "last";
10298 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10300 if (type_arg
== NULL
)
10301 type_arg
= arg1
->type ();
10303 if (ada_is_constrained_packed_array_type (type_arg
))
10304 type_arg
= decode_constrained_packed_array_type (type_arg
);
10306 if (!discrete_type_p (type_arg
))
10310 default: /* Should never happen. */
10311 error (_("unexpected attribute encountered"));
10314 type_arg
= ada_index_type (type_arg
, tem
,
10317 case OP_ATR_LENGTH
:
10318 type_arg
= builtin_type (exp
->gdbarch
)->builtin_int
;
10323 return value::zero (type_arg
, not_lval
);
10325 else if (type_arg
== NULL
)
10327 arg1
= ada_coerce_ref (arg1
);
10329 if (ada_is_constrained_packed_array_type (arg1
->type ()))
10330 arg1
= ada_coerce_to_simple_array (arg1
);
10333 if (op
== OP_ATR_LENGTH
)
10334 type
= builtin_type (exp
->gdbarch
)->builtin_int
;
10337 type
= ada_index_type (arg1
->type (), tem
,
10340 type
= builtin_type (exp
->gdbarch
)->builtin_int
;
10345 default: /* Should never happen. */
10346 error (_("unexpected attribute encountered"));
10348 return value_from_longest
10349 (type
, ada_array_bound (arg1
, tem
, 0));
10351 return value_from_longest
10352 (type
, ada_array_bound (arg1
, tem
, 1));
10353 case OP_ATR_LENGTH
:
10354 return value_from_longest
10355 (type
, ada_array_length (arg1
, tem
));
10358 else if (discrete_type_p (type_arg
))
10360 struct type
*range_type
;
10361 const char *name
= ada_type_name (type_arg
);
10364 if (name
!= NULL
&& type_arg
->code () != TYPE_CODE_ENUM
)
10365 range_type
= to_fixed_range_type (type_arg
, NULL
);
10366 if (range_type
== NULL
)
10367 range_type
= type_arg
;
10371 error (_("unexpected attribute encountered"));
10373 return value_from_longest
10374 (range_type
, ada_discrete_type_low_bound (range_type
));
10376 return value_from_longest
10377 (range_type
, ada_discrete_type_high_bound (range_type
));
10378 case OP_ATR_LENGTH
:
10379 error (_("the 'length attribute applies only to array types"));
10382 else if (type_arg
->code () == TYPE_CODE_FLT
)
10383 error (_("unimplemented type attribute"));
10388 if (ada_is_constrained_packed_array_type (type_arg
))
10389 type_arg
= decode_constrained_packed_array_type (type_arg
);
10392 if (op
== OP_ATR_LENGTH
)
10393 type
= builtin_type (exp
->gdbarch
)->builtin_int
;
10396 type
= ada_index_type (type_arg
, tem
, attr_name
);
10398 type
= builtin_type (exp
->gdbarch
)->builtin_int
;
10404 error (_("unexpected attribute encountered"));
10406 low
= ada_array_bound_from_type (type_arg
, tem
, 0);
10407 return value_from_longest (type
, low
);
10409 high
= ada_array_bound_from_type (type_arg
, tem
, 1);
10410 return value_from_longest (type
, high
);
10411 case OP_ATR_LENGTH
:
10412 low
= ada_array_bound_from_type (type_arg
, tem
, 0);
10413 high
= ada_array_bound_from_type (type_arg
, tem
, 1);
10414 return value_from_longest (type
, high
- low
+ 1);
10419 /* A helper function for OP_ATR_MIN and OP_ATR_MAX. */
10422 ada_binop_minmax (struct type
*expect_type
,
10423 struct expression
*exp
,
10424 enum noside noside
, enum exp_opcode op
,
10425 struct value
*arg1
, struct value
*arg2
)
10427 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10428 return value::zero (arg1
->type (), not_lval
);
10431 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10432 return value_binop (arg1
, arg2
, op
);
10436 /* A helper function for BINOP_EXP. */
10439 ada_binop_exp (struct type
*expect_type
,
10440 struct expression
*exp
,
10441 enum noside noside
, enum exp_opcode op
,
10442 struct value
*arg1
, struct value
*arg2
)
10444 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10445 return value::zero (arg1
->type (), not_lval
);
10448 /* For integer exponentiation operations,
10449 only promote the first argument. */
10450 if (is_integral_type (arg2
->type ()))
10451 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
10453 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10455 return value_binop (arg1
, arg2
, op
);
10462 /* See ada-exp.h. */
10465 ada_resolvable::replace (operation_up
&&owner
,
10466 struct expression
*exp
,
10467 bool deprocedure_p
,
10468 bool parse_completion
,
10469 innermost_block_tracker
*tracker
,
10470 struct type
*context_type
)
10472 if (resolve (exp
, deprocedure_p
, parse_completion
, tracker
, context_type
))
10473 return (make_operation
<ada_funcall_operation
>
10474 (std::move (owner
),
10475 std::vector
<operation_up
> ()));
10476 return std::move (owner
);
10479 /* Convert the character literal whose value would be VAL to the
10480 appropriate value of type TYPE, if there is a translation.
10481 Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
10482 the literal 'A' (VAL == 65), returns 0. */
10485 convert_char_literal (struct type
*type
, LONGEST val
)
10492 type
= check_typedef (type
);
10493 if (type
->code () != TYPE_CODE_ENUM
)
10496 if ((val
>= 'a' && val
<= 'z') || (val
>= '0' && val
<= '9'))
10497 xsnprintf (name
, sizeof (name
), "Q%c", (int) val
);
10498 else if (val
>= 0 && val
< 256)
10499 xsnprintf (name
, sizeof (name
), "QU%02x", (unsigned) val
);
10500 else if (val
>= 0 && val
< 0x10000)
10501 xsnprintf (name
, sizeof (name
), "QW%04x", (unsigned) val
);
10503 xsnprintf (name
, sizeof (name
), "QWW%08lx", (unsigned long) val
);
10504 size_t len
= strlen (name
);
10505 for (f
= 0; f
< type
->num_fields (); f
+= 1)
10507 /* Check the suffix because an enum constant in a package will
10508 have a name like "pkg__QUxx". This is safe enough because we
10509 already have the correct type, and because mangling means
10510 there can't be clashes. */
10511 const char *ename
= type
->field (f
).name ();
10512 size_t elen
= strlen (ename
);
10514 if (elen
>= len
&& strcmp (name
, ename
+ elen
- len
) == 0)
10515 return type
->field (f
).loc_enumval ();
10521 ada_char_operation::evaluate (struct type
*expect_type
,
10522 struct expression
*exp
,
10523 enum noside noside
)
10525 value
*result
= long_const_operation::evaluate (expect_type
, exp
, noside
);
10526 if (expect_type
!= nullptr)
10527 result
= ada_value_cast (expect_type
, result
);
10531 /* See ada-exp.h. */
10534 ada_char_operation::replace (operation_up
&&owner
,
10535 struct expression
*exp
,
10536 bool deprocedure_p
,
10537 bool parse_completion
,
10538 innermost_block_tracker
*tracker
,
10539 struct type
*context_type
)
10541 operation_up result
= std::move (owner
);
10543 if (context_type
!= nullptr && context_type
->code () == TYPE_CODE_ENUM
)
10545 LONGEST val
= as_longest ();
10546 gdb_assert (result
.get () == this);
10547 std::get
<0> (m_storage
) = context_type
;
10548 std::get
<1> (m_storage
) = convert_char_literal (context_type
, val
);
10555 ada_wrapped_operation::evaluate (struct type
*expect_type
,
10556 struct expression
*exp
,
10557 enum noside noside
)
10559 value
*result
= std::get
<0> (m_storage
)->evaluate (expect_type
, exp
, noside
);
10560 if (noside
== EVAL_NORMAL
)
10561 result
= unwrap_value (result
);
10563 /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
10564 then we need to perform the conversion manually, because
10565 evaluate_subexp_standard doesn't do it. This conversion is
10566 necessary in Ada because the different kinds of float/fixed
10567 types in Ada have different representations.
10569 Similarly, we need to perform the conversion from OP_LONG
10571 if ((opcode () == OP_FLOAT
|| opcode () == OP_LONG
) && expect_type
!= NULL
)
10572 result
= ada_value_cast (expect_type
, result
);
10578 ada_wrapped_operation::do_generate_ax (struct expression
*exp
,
10579 struct agent_expr
*ax
,
10580 struct axs_value
*value
,
10581 struct type
*cast_type
)
10583 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
, cast_type
);
10585 struct type
*type
= value
->type
;
10586 if (ada_is_aligner_type (type
))
10587 error (_("Aligner types cannot be handled in agent expressions"));
10588 else if (find_base_type (type
) != nullptr)
10589 error (_("Dynamic types cannot be handled in agent expressions"));
10593 ada_string_operation::evaluate (struct type
*expect_type
,
10594 struct expression
*exp
,
10595 enum noside noside
)
10597 struct type
*char_type
;
10598 if (expect_type
!= nullptr && ada_is_string_type (expect_type
))
10599 char_type
= ada_array_element_type (expect_type
, 1);
10601 char_type
= language_string_char_type (exp
->language_defn
, exp
->gdbarch
);
10603 const std::string
&str
= std::get
<0> (m_storage
);
10604 const char *encoding
;
10605 switch (char_type
->length ())
10609 /* Simply copy over the data -- this isn't perhaps strictly
10610 correct according to the encodings, but it is gdb's
10611 historical behavior. */
10612 struct type
*stringtype
10613 = lookup_array_range_type (char_type
, 1, str
.length ());
10614 struct value
*val
= value::allocate (stringtype
);
10615 memcpy (val
->contents_raw ().data (), str
.c_str (),
10621 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
10622 encoding
= "UTF-16BE";
10624 encoding
= "UTF-16LE";
10628 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
10629 encoding
= "UTF-32BE";
10631 encoding
= "UTF-32LE";
10635 error (_("unexpected character type size %s"),
10636 pulongest (char_type
->length ()));
10639 auto_obstack converted
;
10640 convert_between_encodings (host_charset (), encoding
,
10641 (const gdb_byte
*) str
.c_str (),
10643 &converted
, translit_none
);
10645 struct type
*stringtype
10646 = lookup_array_range_type (char_type
, 1,
10647 obstack_object_size (&converted
)
10648 / char_type
->length ());
10649 struct value
*val
= value::allocate (stringtype
);
10650 memcpy (val
->contents_raw ().data (),
10651 obstack_base (&converted
),
10652 obstack_object_size (&converted
));
10657 ada_concat_operation::evaluate (struct type
*expect_type
,
10658 struct expression
*exp
,
10659 enum noside noside
)
10661 /* If one side is a literal, evaluate the other side first so that
10662 the expected type can be set properly. */
10663 const operation_up
&lhs_expr
= std::get
<0> (m_storage
);
10664 const operation_up
&rhs_expr
= std::get
<1> (m_storage
);
10667 if (dynamic_cast<ada_string_operation
*> (lhs_expr
.get ()) != nullptr)
10669 rhs
= rhs_expr
->evaluate (nullptr, exp
, noside
);
10670 lhs
= lhs_expr
->evaluate (rhs
->type (), exp
, noside
);
10672 else if (dynamic_cast<ada_char_operation
*> (lhs_expr
.get ()) != nullptr)
10674 rhs
= rhs_expr
->evaluate (nullptr, exp
, noside
);
10675 struct type
*rhs_type
= check_typedef (rhs
->type ());
10676 struct type
*elt_type
= nullptr;
10677 if (rhs_type
->code () == TYPE_CODE_ARRAY
)
10678 elt_type
= rhs_type
->target_type ();
10679 lhs
= lhs_expr
->evaluate (elt_type
, exp
, noside
);
10681 else if (dynamic_cast<ada_string_operation
*> (rhs_expr
.get ()) != nullptr)
10683 lhs
= lhs_expr
->evaluate (nullptr, exp
, noside
);
10684 rhs
= rhs_expr
->evaluate (lhs
->type (), exp
, noside
);
10686 else if (dynamic_cast<ada_char_operation
*> (rhs_expr
.get ()) != nullptr)
10688 lhs
= lhs_expr
->evaluate (nullptr, exp
, noside
);
10689 struct type
*lhs_type
= check_typedef (lhs
->type ());
10690 struct type
*elt_type
= nullptr;
10691 if (lhs_type
->code () == TYPE_CODE_ARRAY
)
10692 elt_type
= lhs_type
->target_type ();
10693 rhs
= rhs_expr
->evaluate (elt_type
, exp
, noside
);
10696 return concat_operation::evaluate (expect_type
, exp
, noside
);
10698 return value_concat (lhs
, rhs
);
10702 ada_qual_operation::evaluate (struct type
*expect_type
,
10703 struct expression
*exp
,
10704 enum noside noside
)
10706 struct type
*type
= std::get
<1> (m_storage
);
10707 return std::get
<0> (m_storage
)->evaluate (type
, exp
, noside
);
10711 ada_ternop_range_operation::evaluate (struct type
*expect_type
,
10712 struct expression
*exp
,
10713 enum noside noside
)
10715 value
*arg0
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
10716 value
*arg1
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
10717 value
*arg2
= std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
10718 return eval_ternop_in_range (expect_type
, exp
, noside
, arg0
, arg1
, arg2
);
10722 ada_binop_addsub_operation::evaluate (struct type
*expect_type
,
10723 struct expression
*exp
,
10724 enum noside noside
)
10726 value
*arg1
= std::get
<1> (m_storage
)->evaluate_with_coercion (exp
, noside
);
10727 value
*arg2
= std::get
<2> (m_storage
)->evaluate_with_coercion (exp
, noside
);
10729 auto do_op
= [this] (LONGEST x
, LONGEST y
)
10731 if (std::get
<0> (m_storage
) == BINOP_ADD
)
10736 if (arg1
->type ()->code () == TYPE_CODE_PTR
)
10737 return (value_from_longest
10739 do_op (value_as_long (arg1
), value_as_long (arg2
))));
10740 if (arg2
->type ()->code () == TYPE_CODE_PTR
)
10741 return (value_from_longest
10743 do_op (value_as_long (arg1
), value_as_long (arg2
))));
10744 /* Preserve the original type for use by the range case below.
10745 We cannot cast the result to a reference type, so if ARG1 is
10746 a reference type, find its underlying type. */
10747 struct type
*type
= arg1
->type ();
10748 while (type
->code () == TYPE_CODE_REF
)
10749 type
= type
->target_type ();
10750 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10751 arg1
= value_binop (arg1
, arg2
, std::get
<0> (m_storage
));
10752 /* We need to special-case the result with a range.
10753 This is done for the benefit of "ptype". gdb's Ada support
10754 historically used the LHS to set the result type here, so
10755 preserve this behavior. */
10756 if (type
->code () == TYPE_CODE_RANGE
)
10757 arg1
= value_cast (type
, arg1
);
10762 ada_unop_atr_operation::evaluate (struct type
*expect_type
,
10763 struct expression
*exp
,
10764 enum noside noside
)
10766 struct type
*type_arg
= nullptr;
10767 value
*val
= nullptr;
10769 if (std::get
<0> (m_storage
)->type_p ())
10771 value
*tem
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
10772 EVAL_AVOID_SIDE_EFFECTS
);
10773 type_arg
= tem
->type ();
10776 val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
10778 return ada_unop_atr (exp
, noside
, std::get
<1> (m_storage
),
10779 val
, type_arg
, std::get
<2> (m_storage
));
10783 ada_var_msym_value_operation::evaluate_for_cast (struct type
*expect_type
,
10784 struct expression
*exp
,
10785 enum noside noside
)
10787 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10788 return value::zero (expect_type
, not_lval
);
10790 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
10791 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
10793 val
= ada_value_cast (expect_type
, val
);
10795 /* Follow the Ada language semantics that do not allow taking
10796 an address of the result of a cast (view conversion in Ada). */
10797 if (val
->lval () == lval_memory
)
10800 val
->fetch_lazy ();
10801 val
->set_lval (not_lval
);
10807 ada_var_value_operation::evaluate_for_cast (struct type
*expect_type
,
10808 struct expression
*exp
,
10809 enum noside noside
)
10811 value
*val
= evaluate_var_value (noside
,
10812 std::get
<0> (m_storage
).block
,
10813 std::get
<0> (m_storage
).symbol
);
10815 val
= ada_value_cast (expect_type
, val
);
10817 /* Follow the Ada language semantics that do not allow taking
10818 an address of the result of a cast (view conversion in Ada). */
10819 if (val
->lval () == lval_memory
)
10822 val
->fetch_lazy ();
10823 val
->set_lval (not_lval
);
10829 ada_var_value_operation::evaluate (struct type
*expect_type
,
10830 struct expression
*exp
,
10831 enum noside noside
)
10833 symbol
*sym
= std::get
<0> (m_storage
).symbol
;
10835 if (sym
->domain () == UNDEF_DOMAIN
)
10836 /* Only encountered when an unresolved symbol occurs in a
10837 context other than a function call, in which case, it is
10839 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10840 sym
->print_name ());
10842 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10844 struct type
*type
= static_unwrap_type (sym
->type ());
10845 /* Check to see if this is a tagged type. We also need to handle
10846 the case where the type is a reference to a tagged type, but
10847 we have to be careful to exclude pointers to tagged types.
10848 The latter should be shown as usual (as a pointer), whereas
10849 a reference should mostly be transparent to the user. */
10850 if (ada_is_tagged_type (type
, 0)
10851 || (type
->code () == TYPE_CODE_REF
10852 && ada_is_tagged_type (type
->target_type (), 0)))
10854 /* Tagged types are a little special in the fact that the real
10855 type is dynamic and can only be determined by inspecting the
10856 object's tag. This means that we need to get the object's
10857 value first (EVAL_NORMAL) and then extract the actual object
10860 Note that we cannot skip the final step where we extract
10861 the object type from its tag, because the EVAL_NORMAL phase
10862 results in dynamic components being resolved into fixed ones.
10863 This can cause problems when trying to print the type
10864 description of tagged types whose parent has a dynamic size:
10865 We use the type name of the "_parent" component in order
10866 to print the name of the ancestor type in the type description.
10867 If that component had a dynamic size, the resolution into
10868 a fixed type would result in the loss of that type name,
10869 thus preventing us from printing the name of the ancestor
10870 type in the type description. */
10871 value
*arg1
= evaluate (nullptr, exp
, EVAL_NORMAL
);
10873 if (type
->code () != TYPE_CODE_REF
)
10875 struct type
*actual_type
;
10877 actual_type
= type_from_tag (ada_value_tag (arg1
));
10878 if (actual_type
== NULL
)
10879 /* If, for some reason, we were unable to determine
10880 the actual type from the tag, then use the static
10881 approximation that we just computed as a fallback.
10882 This can happen if the debugging information is
10883 incomplete, for instance. */
10884 actual_type
= type
;
10885 return value::zero (actual_type
, not_lval
);
10889 /* In the case of a ref, ada_coerce_ref takes care
10890 of determining the actual type. But the evaluation
10891 should return a ref as it should be valid to ask
10892 for its address; so rebuild a ref after coerce. */
10893 arg1
= ada_coerce_ref (arg1
);
10894 return value_ref (arg1
, TYPE_CODE_REF
);
10898 /* Records and unions for which GNAT encodings have been
10899 generated need to be statically fixed as well.
10900 Otherwise, non-static fixing produces a type where
10901 all dynamic properties are removed, which prevents "ptype"
10902 from being able to completely describe the type.
10903 For instance, a case statement in a variant record would be
10904 replaced by the relevant components based on the actual
10905 value of the discriminants. */
10906 if ((type
->code () == TYPE_CODE_STRUCT
10907 && dynamic_template_type (type
) != NULL
)
10908 || (type
->code () == TYPE_CODE_UNION
10909 && ada_find_parallel_type (type
, "___XVU") != NULL
))
10910 return value::zero (to_static_fixed_type (type
), not_lval
);
10913 value
*arg1
= var_value_operation::evaluate (expect_type
, exp
, noside
);
10914 return ada_to_fixed_value (arg1
);
10918 ada_var_value_operation::resolve (struct expression
*exp
,
10919 bool deprocedure_p
,
10920 bool parse_completion
,
10921 innermost_block_tracker
*tracker
,
10922 struct type
*context_type
)
10924 symbol
*sym
= std::get
<0> (m_storage
).symbol
;
10925 if (sym
->domain () == UNDEF_DOMAIN
)
10927 block_symbol resolved
10928 = ada_resolve_variable (sym
, std::get
<0> (m_storage
).block
,
10929 context_type
, parse_completion
,
10930 deprocedure_p
, tracker
);
10931 std::get
<0> (m_storage
) = resolved
;
10935 && (std::get
<0> (m_storage
).symbol
->type ()->code ()
10936 == TYPE_CODE_FUNC
))
10943 ada_var_value_operation::do_generate_ax (struct expression
*exp
,
10944 struct agent_expr
*ax
,
10945 struct axs_value
*value
,
10946 struct type
*cast_type
)
10948 symbol
*sym
= std::get
<0> (m_storage
).symbol
;
10950 if (sym
->domain () == UNDEF_DOMAIN
)
10951 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10952 sym
->print_name ());
10954 struct type
*type
= static_unwrap_type (sym
->type ());
10955 if (ada_is_tagged_type (type
, 0)
10956 || (type
->code () == TYPE_CODE_REF
10957 && ada_is_tagged_type (type
->target_type (), 0)))
10958 error (_("Tagged types cannot be handled in agent expressions"));
10960 if ((type
->code () == TYPE_CODE_STRUCT
10961 && dynamic_template_type (type
) != NULL
)
10962 || (type
->code () == TYPE_CODE_UNION
10963 && ada_find_parallel_type (type
, "___XVU") != NULL
))
10964 error (_("Dynamic types cannot be handled in agent expressions"));
10966 var_value_operation::do_generate_ax (exp
, ax
, value
, cast_type
);
10970 ada_unop_ind_operation::evaluate (struct type
*expect_type
,
10971 struct expression
*exp
,
10972 enum noside noside
)
10974 value
*arg1
= std::get
<0> (m_storage
)->evaluate (expect_type
, exp
, noside
);
10976 struct type
*type
= ada_check_typedef (arg1
->type ());
10977 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10979 if (ada_is_array_descriptor_type (type
))
10981 /* GDB allows dereferencing GNAT array descriptors.
10982 However, for 'ptype' we don't want to try to
10983 "dereference" a thick pointer here -- that will end up
10984 giving us an array with (1 .. 0) for bounds, which is
10985 less clear than (<>). */
10986 struct type
*arrType
= ada_type_of_array (arg1
, 0);
10988 if (arrType
== NULL
)
10989 error (_("Attempt to dereference null array pointer."));
10990 if (is_thick_pntr (type
))
10992 return value_at_lazy (arrType
, 0);
10994 else if (type
->code () == TYPE_CODE_PTR
10995 || type
->code () == TYPE_CODE_REF
10996 /* In C you can dereference an array to get the 1st elt. */
10997 || type
->code () == TYPE_CODE_ARRAY
)
10999 /* As mentioned in the OP_VAR_VALUE case, tagged types can
11000 only be determined by inspecting the object's tag.
11001 This means that we need to evaluate completely the
11002 expression in order to get its type. */
11004 if ((type
->code () == TYPE_CODE_REF
11005 || type
->code () == TYPE_CODE_PTR
)
11006 && ada_is_tagged_type (type
->target_type (), 0))
11008 arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
11010 type
= ada_value_ind (arg1
)->type ();
11014 type
= to_static_fixed_type
11016 (ada_check_typedef (type
->target_type ())));
11018 return value::zero (type
, lval_memory
);
11020 else if (type
->code () == TYPE_CODE_INT
)
11022 /* GDB allows dereferencing an int. */
11023 if (expect_type
== NULL
)
11024 return value::zero (builtin_type (exp
->gdbarch
)->builtin_int
,
11029 to_static_fixed_type (ada_aligned_type (expect_type
));
11030 return value::zero (expect_type
, lval_memory
);
11034 error (_("Attempt to take contents of a non-pointer value."));
11036 arg1
= ada_coerce_ref (arg1
); /* FIXME: What is this for?? */
11037 type
= ada_check_typedef (arg1
->type ());
11039 if (type
->code () == TYPE_CODE_INT
)
11040 /* GDB allows dereferencing an int. If we were given
11041 the expect_type, then use that as the target type.
11042 Otherwise, assume that the target type is an int. */
11044 if (expect_type
!= NULL
)
11045 return ada_value_ind (value_cast (lookup_pointer_type (expect_type
),
11048 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
11049 value_as_address (arg1
));
11052 if (ada_is_array_descriptor_type (type
))
11053 /* GDB allows dereferencing GNAT array descriptors. */
11054 return ada_coerce_to_simple_array (arg1
);
11056 return ada_value_ind (arg1
);
11060 ada_structop_operation::evaluate (struct type
*expect_type
,
11061 struct expression
*exp
,
11062 enum noside noside
)
11064 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
11065 const char *str
= std::get
<1> (m_storage
).c_str ();
11066 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11069 struct type
*type1
= arg1
->type ();
11071 if (ada_is_tagged_type (type1
, 1))
11073 type
= ada_lookup_struct_elt_type (type1
, str
, 1, 1);
11075 /* If the field is not found, check if it exists in the
11076 extension of this object's type. This means that we
11077 need to evaluate completely the expression. */
11081 arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
11083 arg1
= ada_value_struct_elt (arg1
, str
, 0);
11084 arg1
= unwrap_value (arg1
);
11085 type
= ada_to_fixed_value (arg1
)->type ();
11089 type
= ada_lookup_struct_elt_type (type1
, str
, 1, 0);
11091 return value::zero (ada_aligned_type (type
), lval_memory
);
11095 arg1
= ada_value_struct_elt (arg1
, str
, 0);
11096 arg1
= unwrap_value (arg1
);
11097 return ada_to_fixed_value (arg1
);
11102 ada_funcall_operation::evaluate (struct type
*expect_type
,
11103 struct expression
*exp
,
11104 enum noside noside
)
11106 const std::vector
<operation_up
> &args_up
= std::get
<1> (m_storage
);
11107 int nargs
= args_up
.size ();
11108 std::vector
<value
*> argvec (nargs
);
11109 operation_up
&callee_op
= std::get
<0> (m_storage
);
11111 ada_var_value_operation
*avv
11112 = dynamic_cast<ada_var_value_operation
*> (callee_op
.get ());
11114 && avv
->get_symbol ()->domain () == UNDEF_DOMAIN
)
11115 error (_("Unexpected unresolved symbol, %s, during evaluation"),
11116 avv
->get_symbol ()->print_name ());
11118 value
*callee
= callee_op
->evaluate (nullptr, exp
, noside
);
11119 for (int i
= 0; i
< args_up
.size (); ++i
)
11120 argvec
[i
] = args_up
[i
]->evaluate (nullptr, exp
, noside
);
11122 if (ada_is_constrained_packed_array_type
11123 (desc_base_type (callee
->type ())))
11124 callee
= ada_coerce_to_simple_array (callee
);
11125 else if (callee
->type ()->code () == TYPE_CODE_ARRAY
11126 && callee
->type ()->field (0).bitsize () != 0)
11127 /* This is a packed array that has already been fixed, and
11128 therefore already coerced to a simple array. Nothing further
11131 else if (callee
->type ()->code () == TYPE_CODE_REF
)
11133 /* Make sure we dereference references so that all the code below
11134 feels like it's really handling the referenced value. Wrapping
11135 types (for alignment) may be there, so make sure we strip them as
11137 callee
= ada_to_fixed_value (coerce_ref (callee
));
11139 else if (callee
->type ()->code () == TYPE_CODE_ARRAY
11140 && callee
->lval () == lval_memory
)
11141 callee
= value_addr (callee
);
11143 struct type
*type
= ada_check_typedef (callee
->type ());
11145 /* Ada allows us to implicitly dereference arrays when subscripting
11146 them. So, if this is an array typedef (encoding use for array
11147 access types encoded as fat pointers), strip it now. */
11148 if (type
->code () == TYPE_CODE_TYPEDEF
)
11149 type
= ada_typedef_target_type (type
);
11151 if (type
->code () == TYPE_CODE_PTR
)
11153 switch (ada_check_typedef (type
->target_type ())->code ())
11155 case TYPE_CODE_FUNC
:
11156 type
= ada_check_typedef (type
->target_type ());
11158 case TYPE_CODE_ARRAY
:
11160 case TYPE_CODE_STRUCT
:
11161 if (noside
!= EVAL_AVOID_SIDE_EFFECTS
)
11162 callee
= ada_value_ind (callee
);
11163 type
= ada_check_typedef (type
->target_type ());
11166 error (_("cannot subscript or call something of type `%s'"),
11167 ada_type_name (callee
->type ()));
11172 switch (type
->code ())
11174 case TYPE_CODE_FUNC
:
11175 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11177 if (type
->target_type () == NULL
)
11178 error_call_unknown_return_type (NULL
);
11179 return value::allocate (type
->target_type ());
11181 return call_function_by_hand (callee
, expect_type
, argvec
);
11182 case TYPE_CODE_INTERNAL_FUNCTION
:
11183 return call_internal_function (exp
->gdbarch
, exp
->language_defn
,
11185 argvec
.data (), noside
);
11187 case TYPE_CODE_STRUCT
:
11191 arity
= ada_array_arity (type
);
11192 type
= ada_array_element_type (type
, nargs
);
11194 error (_("cannot subscript or call a record"));
11195 if (arity
!= nargs
)
11196 error (_("wrong number of subscripts; expecting %d"), arity
);
11197 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11198 return value::zero (ada_aligned_type (type
), lval_memory
);
11200 unwrap_value (ada_value_subscript
11201 (callee
, nargs
, argvec
.data ()));
11203 case TYPE_CODE_ARRAY
:
11204 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11206 type
= ada_array_element_type (type
, nargs
);
11208 error (_("element type of array unknown"));
11210 return value::zero (ada_aligned_type (type
), lval_memory
);
11213 unwrap_value (ada_value_subscript
11214 (ada_coerce_to_simple_array (callee
),
11215 nargs
, argvec
.data ()));
11216 case TYPE_CODE_PTR
: /* Pointer to array */
11217 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11219 type
= to_fixed_array_type (type
->target_type (), NULL
, 1);
11220 type
= ada_array_element_type (type
, nargs
);
11222 error (_("element type of array unknown"));
11224 return value::zero (ada_aligned_type (type
), lval_memory
);
11227 unwrap_value (ada_value_ptr_subscript (callee
, nargs
,
11231 error (_("Attempt to index or call something other than an "
11232 "array or function"));
11237 ada_funcall_operation::resolve (struct expression
*exp
,
11238 bool deprocedure_p
,
11239 bool parse_completion
,
11240 innermost_block_tracker
*tracker
,
11241 struct type
*context_type
)
11243 operation_up
&callee_op
= std::get
<0> (m_storage
);
11245 ada_var_value_operation
*avv
11246 = dynamic_cast<ada_var_value_operation
*> (callee_op
.get ());
11247 if (avv
== nullptr)
11250 symbol
*sym
= avv
->get_symbol ();
11251 if (sym
->domain () != UNDEF_DOMAIN
)
11254 const std::vector
<operation_up
> &args_up
= std::get
<1> (m_storage
);
11255 int nargs
= args_up
.size ();
11256 std::vector
<value
*> argvec (nargs
);
11258 for (int i
= 0; i
< args_up
.size (); ++i
)
11259 argvec
[i
] = args_up
[i
]->evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
11261 const block
*block
= avv
->get_block ();
11262 block_symbol resolved
11263 = ada_resolve_funcall (sym
, block
,
11264 context_type
, parse_completion
,
11265 nargs
, argvec
.data (),
11268 std::get
<0> (m_storage
)
11269 = make_operation
<ada_var_value_operation
> (resolved
);
11274 ada_ternop_slice_operation::resolve (struct expression
*exp
,
11275 bool deprocedure_p
,
11276 bool parse_completion
,
11277 innermost_block_tracker
*tracker
,
11278 struct type
*context_type
)
11280 /* Historically this check was done during resolution, so we
11281 continue that here. */
11282 value
*v
= std::get
<0> (m_storage
)->evaluate (context_type
, exp
,
11283 EVAL_AVOID_SIDE_EFFECTS
);
11284 if (ada_is_any_packed_array_type (v
->type ()))
11285 error (_("cannot slice a packed array"));
11293 /* Return non-zero iff TYPE represents a System.Address type. */
11296 ada_is_system_address_type (struct type
*type
)
11298 return (type
->name () && strcmp (type
->name (), "system__address") == 0);
11305 /* Scan STR beginning at position K for a discriminant name, and
11306 return the value of that discriminant field of DVAL in *PX. If
11307 PNEW_K is not null, put the position of the character beyond the
11308 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
11309 not alter *PX and *PNEW_K if unsuccessful. */
11312 scan_discrim_bound (const char *str
, int k
, struct value
*dval
, LONGEST
* px
,
11315 static std::string storage
;
11316 const char *pstart
, *pend
, *bound
;
11317 struct value
*bound_val
;
11319 if (dval
== NULL
|| str
== NULL
|| str
[k
] == '\0')
11323 pend
= strstr (pstart
, "__");
11327 k
+= strlen (bound
);
11331 int len
= pend
- pstart
;
11333 /* Strip __ and beyond. */
11334 storage
= std::string (pstart
, len
);
11335 bound
= storage
.c_str ();
11339 bound_val
= ada_search_struct_field (bound
, dval
, 0, dval
->type ());
11340 if (bound_val
== NULL
)
11343 *px
= value_as_long (bound_val
);
11344 if (pnew_k
!= NULL
)
11349 /* Value of variable named NAME. Only exact matches are considered.
11350 If no such variable found, then if ERR_MSG is null, returns 0, and
11351 otherwise causes an error with message ERR_MSG. */
11353 static struct value
*
11354 get_var_value (const char *name
, const char *err_msg
)
11356 std::string quoted_name
= add_angle_brackets (name
);
11358 lookup_name_info
lookup_name (quoted_name
, symbol_name_match_type::FULL
);
11360 std::vector
<struct block_symbol
> syms
11361 = ada_lookup_symbol_list_worker (lookup_name
,
11362 get_selected_block (0),
11365 if (syms
.size () != 1)
11367 if (err_msg
== NULL
)
11370 error (("%s"), err_msg
);
11373 return value_of_variable (syms
[0].symbol
, syms
[0].block
);
11376 /* Value of integer variable named NAME in the current environment.
11377 If no such variable is found, returns false. Otherwise, sets VALUE
11378 to the variable's value and returns true. */
11381 get_int_var_value (const char *name
, LONGEST
&value
)
11383 struct value
*var_val
= get_var_value (name
, 0);
11388 value
= value_as_long (var_val
);
11393 /* Return a range type whose base type is that of the range type named
11394 NAME in the current environment, and whose bounds are calculated
11395 from NAME according to the GNAT range encoding conventions.
11396 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
11397 corresponding range type from debug information; fall back to using it
11398 if symbol lookup fails. If a new type must be created, allocate it
11399 like ORIG_TYPE was. The bounds information, in general, is encoded
11400 in NAME, the base type given in the named range type. */
11402 static struct type
*
11403 to_fixed_range_type (struct type
*raw_type
, struct value
*dval
)
11406 struct type
*base_type
;
11407 const char *subtype_info
;
11409 gdb_assert (raw_type
!= NULL
);
11410 gdb_assert (raw_type
->name () != NULL
);
11412 if (raw_type
->code () == TYPE_CODE_RANGE
)
11413 base_type
= raw_type
->target_type ();
11415 base_type
= raw_type
;
11417 name
= raw_type
->name ();
11418 subtype_info
= strstr (name
, "___XD");
11419 if (subtype_info
== NULL
)
11421 LONGEST L
= ada_discrete_type_low_bound (raw_type
);
11422 LONGEST U
= ada_discrete_type_high_bound (raw_type
);
11424 if (L
< INT_MIN
|| U
> INT_MAX
)
11428 type_allocator
alloc (raw_type
);
11429 return create_static_range_type (alloc
, raw_type
, L
, U
);
11434 int prefix_len
= subtype_info
- name
;
11437 const char *bounds_str
;
11441 bounds_str
= strchr (subtype_info
, '_');
11444 if (*subtype_info
== 'L')
11446 if (!ada_scan_number (bounds_str
, n
, &L
, &n
)
11447 && !scan_discrim_bound (bounds_str
, n
, dval
, &L
, &n
))
11449 if (bounds_str
[n
] == '_')
11451 else if (bounds_str
[n
] == '.') /* FIXME? SGI Workshop kludge. */
11457 std::string name_buf
= std::string (name
, prefix_len
) + "___L";
11458 if (!get_int_var_value (name_buf
.c_str (), L
))
11460 lim_warning (_("Unknown lower bound, using 1."));
11465 if (*subtype_info
== 'U')
11467 if (!ada_scan_number (bounds_str
, n
, &U
, &n
)
11468 && !scan_discrim_bound (bounds_str
, n
, dval
, &U
, &n
))
11473 std::string name_buf
= std::string (name
, prefix_len
) + "___U";
11474 if (!get_int_var_value (name_buf
.c_str (), U
))
11476 lim_warning (_("Unknown upper bound, using %ld."), (long) L
);
11481 type_allocator
alloc (raw_type
);
11482 type
= create_static_range_type (alloc
, base_type
, L
, U
);
11483 /* create_static_range_type alters the resulting type's length
11484 to match the size of the base_type, which is not what we want.
11485 Set it back to the original range type's length. */
11486 type
->set_length (raw_type
->length ());
11487 type
->set_name (name
);
11492 /* True iff NAME is the name of a range type. */
11495 ada_is_range_type_name (const char *name
)
11497 return (name
!= NULL
&& strstr (name
, "___XD"));
11501 /* Modular types */
11503 /* True iff TYPE is an Ada modular type. */
11506 ada_is_modular_type (struct type
*type
)
11508 struct type
*subranged_type
= get_base_type (type
);
11510 return (subranged_type
!= NULL
&& type
->code () == TYPE_CODE_RANGE
11511 && subranged_type
->code () == TYPE_CODE_INT
11512 && subranged_type
->is_unsigned ());
11515 /* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
11518 ada_modulus (struct type
*type
)
11520 const dynamic_prop
&high
= type
->bounds ()->high
;
11522 if (high
.is_constant ())
11523 return (ULONGEST
) high
.const_val () + 1;
11525 /* If TYPE is unresolved, the high bound might be a location list. Return
11526 0, for lack of a better value to return. */
11531 /* Ada exception catchpoint support:
11532 ---------------------------------
11534 We support 3 kinds of exception catchpoints:
11535 . catchpoints on Ada exceptions
11536 . catchpoints on unhandled Ada exceptions
11537 . catchpoints on failed assertions
11539 Exceptions raised during failed assertions, or unhandled exceptions
11540 could perfectly be caught with the general catchpoint on Ada exceptions.
11541 However, we can easily differentiate these two special cases, and having
11542 the option to distinguish these two cases from the rest can be useful
11543 to zero-in on certain situations.
11545 Exception catchpoints are a specialized form of breakpoint,
11546 since they rely on inserting breakpoints inside known routines
11547 of the GNAT runtime. The implementation therefore uses a standard
11548 breakpoint structure of the BP_BREAKPOINT type, but with its own set
11551 Support in the runtime for exception catchpoints have been changed
11552 a few times already, and these changes affect the implementation
11553 of these catchpoints. In order to be able to support several
11554 variants of the runtime, we use a sniffer that will determine
11555 the runtime variant used by the program being debugged. */
11557 /* Ada's standard exceptions.
11559 The Ada 83 standard also defined Numeric_Error. But there so many
11560 situations where it was unclear from the Ada 83 Reference Manual
11561 (RM) whether Constraint_Error or Numeric_Error should be raised,
11562 that the ARG (Ada Rapporteur Group) eventually issued a Binding
11563 Interpretation saying that anytime the RM says that Numeric_Error
11564 should be raised, the implementation may raise Constraint_Error.
11565 Ada 95 went one step further and pretty much removed Numeric_Error
11566 from the list of standard exceptions (it made it a renaming of
11567 Constraint_Error, to help preserve compatibility when compiling
11568 an Ada83 compiler). As such, we do not include Numeric_Error from
11569 this list of standard exceptions. */
11571 static const char * const standard_exc
[] = {
11572 "constraint_error",
11578 typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype
) (void);
11580 /* A structure that describes how to support exception catchpoints
11581 for a given executable. */
11583 struct exception_support_info
11585 /* The name of the symbol to break on in order to insert
11586 a catchpoint on exceptions. */
11587 const char *catch_exception_sym
;
11589 /* The name of the symbol to break on in order to insert
11590 a catchpoint on unhandled exceptions. */
11591 const char *catch_exception_unhandled_sym
;
11593 /* The name of the symbol to break on in order to insert
11594 a catchpoint on failed assertions. */
11595 const char *catch_assert_sym
;
11597 /* The name of the symbol to break on in order to insert
11598 a catchpoint on exception handling. */
11599 const char *catch_handlers_sym
;
11601 /* Assuming that the inferior just triggered an unhandled exception
11602 catchpoint, this function is responsible for returning the address
11603 in inferior memory where the name of that exception is stored.
11604 Return zero if the address could not be computed. */
11605 ada_unhandled_exception_name_addr_ftype
*unhandled_exception_name_addr
;
11608 static CORE_ADDR
ada_unhandled_exception_name_addr (void);
11609 static CORE_ADDR
ada_unhandled_exception_name_addr_from_raise (void);
11611 /* The following exception support info structure describes how to
11612 implement exception catchpoints with the latest version of the
11613 Ada runtime (as of 2019-08-??). */
11615 static const struct exception_support_info default_exception_support_info
=
11617 "__gnat_debug_raise_exception", /* catch_exception_sym */
11618 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11619 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11620 "__gnat_begin_handler_v1", /* catch_handlers_sym */
11621 ada_unhandled_exception_name_addr
11624 /* The following exception support info structure describes how to
11625 implement exception catchpoints with an earlier version of the
11626 Ada runtime (as of 2007-03-06) using v0 of the EH ABI. */
11628 static const struct exception_support_info exception_support_info_v0
=
11630 "__gnat_debug_raise_exception", /* catch_exception_sym */
11631 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11632 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11633 "__gnat_begin_handler", /* catch_handlers_sym */
11634 ada_unhandled_exception_name_addr
11637 /* The following exception support info structure describes how to
11638 implement exception catchpoints with a slightly older version
11639 of the Ada runtime. */
11641 static const struct exception_support_info exception_support_info_fallback
=
11643 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
11644 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11645 "system__assertions__raise_assert_failure", /* catch_assert_sym */
11646 "__gnat_begin_handler", /* catch_handlers_sym */
11647 ada_unhandled_exception_name_addr_from_raise
11650 /* Return nonzero if we can detect the exception support routines
11651 described in EINFO.
11653 This function errors out if an abnormal situation is detected
11654 (for instance, if we find the exception support routines, but
11655 that support is found to be incomplete). */
11658 ada_has_this_exception_support (const struct exception_support_info
*einfo
)
11660 struct symbol
*sym
;
11662 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11663 that should be compiled with debugging information. As a result, we
11664 expect to find that symbol in the symtabs. */
11666 sym
= standard_lookup (einfo
->catch_exception_sym
, NULL
,
11667 SEARCH_FUNCTION_DOMAIN
);
11670 /* Perhaps we did not find our symbol because the Ada runtime was
11671 compiled without debugging info, or simply stripped of it.
11672 It happens on some GNU/Linux distributions for instance, where
11673 users have to install a separate debug package in order to get
11674 the runtime's debugging info. In that situation, let the user
11675 know why we cannot insert an Ada exception catchpoint.
11677 Note: Just for the purpose of inserting our Ada exception
11678 catchpoint, we could rely purely on the associated minimal symbol.
11679 But we would be operating in degraded mode anyway, since we are
11680 still lacking the debugging info needed later on to extract
11681 the name of the exception being raised (this name is printed in
11682 the catchpoint message, and is also used when trying to catch
11683 a specific exception). We do not handle this case for now. */
11684 bound_minimal_symbol msym
11685 = lookup_minimal_symbol (current_program_space
,
11686 einfo
->catch_exception_sym
);
11688 if (msym
.minsym
&& msym
.minsym
->type () != mst_solib_trampoline
)
11689 error (_("Your Ada runtime appears to be missing some debugging "
11690 "information.\nCannot insert Ada exception catchpoint "
11691 "in this configuration."));
11696 /* Make sure that the symbol we found corresponds to a function. */
11698 if (sym
->aclass () != LOC_BLOCK
)
11699 error (_("Symbol \"%s\" is not a function (class = %d)"),
11700 sym
->linkage_name (), sym
->aclass ());
11702 sym
= standard_lookup (einfo
->catch_handlers_sym
, NULL
,
11703 SEARCH_FUNCTION_DOMAIN
);
11706 bound_minimal_symbol msym
11707 = lookup_minimal_symbol (current_program_space
,
11708 einfo
->catch_handlers_sym
);
11710 if (msym
.minsym
&& msym
.minsym
->type () != mst_solib_trampoline
)
11711 error (_("Your Ada runtime appears to be missing some debugging "
11712 "information.\nCannot insert Ada exception catchpoint "
11713 "in this configuration."));
11718 /* Make sure that the symbol we found corresponds to a function. */
11720 if (sym
->aclass () != LOC_BLOCK
)
11721 error (_("Symbol \"%s\" is not a function (class = %d)"),
11722 sym
->linkage_name (), sym
->aclass ());
11727 /* Inspect the Ada runtime and determine which exception info structure
11728 should be used to provide support for exception catchpoints.
11730 This function will always set the per-inferior exception_info,
11731 or raise an error. */
11734 ada_exception_support_info_sniffer (void)
11736 struct ada_inferior_data
*data
= get_ada_inferior_data (current_inferior ());
11738 /* If the exception info is already known, then no need to recompute it. */
11739 if (data
->exception_info
!= NULL
)
11742 /* Check the latest (default) exception support info. */
11743 if (ada_has_this_exception_support (&default_exception_support_info
))
11745 data
->exception_info
= &default_exception_support_info
;
11749 /* Try the v0 exception suport info. */
11750 if (ada_has_this_exception_support (&exception_support_info_v0
))
11752 data
->exception_info
= &exception_support_info_v0
;
11756 /* Try our fallback exception suport info. */
11757 if (ada_has_this_exception_support (&exception_support_info_fallback
))
11759 data
->exception_info
= &exception_support_info_fallback
;
11763 throw_error (NOT_FOUND_ERROR
,
11764 _("Could not find Ada runtime exception support"));
11767 /* True iff FRAME is very likely to be that of a function that is
11768 part of the runtime system. This is all very heuristic, but is
11769 intended to be used as advice as to what frames are uninteresting
11773 is_known_support_routine (const frame_info_ptr
&frame
)
11775 enum language func_lang
;
11777 const char *fullname
;
11779 /* If this code does not have any debugging information (no symtab),
11780 This cannot be any user code. */
11782 symtab_and_line sal
= find_frame_sal (frame
);
11783 if (sal
.symtab
== NULL
)
11786 /* If there is a symtab, but the associated source file cannot be
11787 located, then assume this is not user code: Selecting a frame
11788 for which we cannot display the code would not be very helpful
11789 for the user. This should also take care of case such as VxWorks
11790 where the kernel has some debugging info provided for a few units. */
11792 fullname
= symtab_to_fullname (sal
.symtab
);
11793 if (access (fullname
, R_OK
) != 0)
11796 /* Check the unit filename against the Ada runtime file naming.
11797 We also check the name of the objfile against the name of some
11798 known system libraries that sometimes come with debugging info
11801 for (i
= 0; known_runtime_file_name_patterns
[i
] != NULL
; i
+= 1)
11803 re_comp (known_runtime_file_name_patterns
[i
]);
11804 if (re_exec (lbasename (sal
.symtab
->filename
)))
11806 if (sal
.symtab
->compunit ()->objfile () != NULL
11807 && re_exec (objfile_name (sal
.symtab
->compunit ()->objfile ())))
11811 /* Check whether the function is a GNAT-generated entity. */
11813 gdb::unique_xmalloc_ptr
<char> func_name
11814 = find_frame_funname (frame
, &func_lang
, NULL
);
11815 if (func_name
== NULL
)
11818 for (i
= 0; known_auxiliary_function_name_patterns
[i
] != NULL
; i
+= 1)
11820 re_comp (known_auxiliary_function_name_patterns
[i
]);
11821 if (re_exec (func_name
.get ()))
11828 /* Find the first frame that contains debugging information and that is not
11829 part of the Ada run-time, starting from FI and moving upward. */
11832 ada_find_printable_frame (const frame_info_ptr
&initial_fi
)
11834 for (frame_info_ptr fi
= initial_fi
; fi
!= nullptr; fi
= get_prev_frame (fi
))
11836 if (!is_known_support_routine (fi
))
11845 /* Assuming that the inferior just triggered an unhandled exception
11846 catchpoint, return the address in inferior memory where the name
11847 of the exception is stored.
11849 Return zero if the address could not be computed. */
11852 ada_unhandled_exception_name_addr (void)
11854 return parse_and_eval_address ("e.full_name");
11857 /* Same as ada_unhandled_exception_name_addr, except that this function
11858 should be used when the inferior uses an older version of the runtime,
11859 where the exception name needs to be extracted from a specific frame
11860 several frames up in the callstack. */
11863 ada_unhandled_exception_name_addr_from_raise (void)
11867 struct ada_inferior_data
*data
= get_ada_inferior_data (current_inferior ());
11869 /* To determine the name of this exception, we need to select
11870 the frame corresponding to RAISE_SYM_NAME. This frame is
11871 at least 3 levels up, so we simply skip the first 3 frames
11872 without checking the name of their associated function. */
11873 fi
= get_current_frame ();
11874 for (frame_level
= 0; frame_level
< 3; frame_level
+= 1)
11876 fi
= get_prev_frame (fi
);
11880 enum language func_lang
;
11882 gdb::unique_xmalloc_ptr
<char> func_name
11883 = find_frame_funname (fi
, &func_lang
, NULL
);
11884 if (func_name
!= NULL
)
11886 if (strcmp (func_name
.get (),
11887 data
->exception_info
->catch_exception_sym
) == 0)
11888 break; /* We found the frame we were looking for... */
11890 fi
= get_prev_frame (fi
);
11897 return parse_and_eval_address ("id.full_name");
11900 /* Assuming the inferior just triggered an Ada exception catchpoint
11901 (of any type), return the address in inferior memory where the name
11902 of the exception is stored, if applicable.
11904 Assumes the selected frame is the current frame.
11906 Return zero if the address could not be computed, or if not relevant. */
11909 ada_exception_name_addr_1 (enum ada_exception_catchpoint_kind ex
)
11911 struct ada_inferior_data
*data
= get_ada_inferior_data (current_inferior ());
11915 case ada_catch_exception
:
11916 return (parse_and_eval_address ("e.full_name"));
11919 case ada_catch_exception_unhandled
:
11920 return data
->exception_info
->unhandled_exception_name_addr ();
11923 case ada_catch_handlers
:
11924 return 0; /* The runtimes does not provide access to the exception
11928 case ada_catch_assert
:
11929 return 0; /* Exception name is not relevant in this case. */
11933 internal_error (_("unexpected catchpoint type"));
11937 return 0; /* Should never be reached. */
11940 /* Assuming the inferior is stopped at an exception catchpoint,
11941 return the message which was associated to the exception, if
11942 available. Return NULL if the message could not be retrieved.
11944 Note: The exception message can be associated to an exception
11945 either through the use of the Raise_Exception function, or
11946 more simply (Ada 2005 and later), via:
11948 raise Exception_Name with "exception message";
11952 static gdb::unique_xmalloc_ptr
<char>
11953 ada_exception_message_1 (void)
11955 struct value
*e_msg_val
;
11958 /* For runtimes that support this feature, the exception message
11959 is passed as an unbounded string argument called "message". */
11960 e_msg_val
= parse_and_eval ("message");
11961 if (e_msg_val
== NULL
)
11962 return NULL
; /* Exception message not supported. */
11964 e_msg_val
= ada_coerce_to_simple_array (e_msg_val
);
11965 gdb_assert (e_msg_val
!= NULL
);
11966 e_msg_len
= e_msg_val
->type ()->length ();
11968 /* If the message string is empty, then treat it as if there was
11969 no exception message. */
11970 if (e_msg_len
<= 0)
11973 gdb::unique_xmalloc_ptr
<char> e_msg ((char *) xmalloc (e_msg_len
+ 1));
11974 read_memory (e_msg_val
->address (), (gdb_byte
*) e_msg
.get (),
11976 e_msg
.get ()[e_msg_len
] = '\0';
11981 /* Same as ada_exception_message_1, except that all exceptions are
11982 contained here (returning NULL instead). */
11984 static gdb::unique_xmalloc_ptr
<char>
11985 ada_exception_message (void)
11987 gdb::unique_xmalloc_ptr
<char> e_msg
;
11991 e_msg
= ada_exception_message_1 ();
11993 catch (const gdb_exception_error
&e
)
11995 e_msg
.reset (nullptr);
12001 /* Same as ada_exception_name_addr_1, except that it intercepts and contains
12002 any error that ada_exception_name_addr_1 might cause to be thrown.
12003 When an error is intercepted, a warning with the error message is printed,
12004 and zero is returned. */
12007 ada_exception_name_addr (enum ada_exception_catchpoint_kind ex
)
12009 CORE_ADDR result
= 0;
12013 result
= ada_exception_name_addr_1 (ex
);
12016 catch (const gdb_exception_error
&e
)
12018 warning (_("failed to get exception name: %s"), e
.what ());
12025 static std::string ada_exception_catchpoint_cond_string
12026 (const char *excep_string
,
12027 enum ada_exception_catchpoint_kind ex
);
12029 /* Ada catchpoints.
12031 In the case of catchpoints on Ada exceptions, the catchpoint will
12032 stop the target on every exception the program throws. When a user
12033 specifies the name of a specific exception, we translate this
12034 request into a condition expression (in text form), and then parse
12035 it into an expression stored in each of the catchpoint's locations.
12036 We then use this condition to check whether the exception that was
12037 raised is the one the user is interested in. If not, then the
12038 target is resumed again. We store the name of the requested
12039 exception, in order to be able to re-set the condition expression
12040 when symbols change. */
12042 /* An instance of this type is used to represent an Ada catchpoint. */
12044 struct ada_catchpoint
: public code_breakpoint
12046 ada_catchpoint (struct gdbarch
*gdbarch_
,
12047 enum ada_exception_catchpoint_kind kind
,
12048 const char *cond_string
,
12052 std::string
&&excep_string_
)
12053 : code_breakpoint (gdbarch_
, bp_catchpoint
, tempflag
, cond_string
),
12054 m_excep_string (std::move (excep_string_
)),
12057 /* Unlike most code_breakpoint types, Ada catchpoints are
12058 pspace-specific. */
12059 pspace
= current_program_space
;
12060 enable_state
= enabled
? bp_enabled
: bp_disabled
;
12061 language
= language_ada
;
12066 struct bp_location
*allocate_location () override
;
12067 void re_set (program_space
*pspace
) override
;
12068 void check_status (struct bpstat
*bs
) override
;
12069 enum print_stop_action
print_it (const bpstat
*bs
) const override
;
12070 bool print_one (const bp_location
**) const override
;
12071 void print_mention () const override
;
12072 void print_recreate (struct ui_file
*fp
) const override
;
12076 /* A helper function for check_status. Returns true if we should
12077 stop for this breakpoint hit. If the user specified a specific
12078 exception, we only want to cause a stop if the program thrown
12080 bool should_stop_exception (const struct bp_location
*bl
) const;
12082 /* The name of the specific exception the user specified. */
12083 std::string m_excep_string
;
12085 /* What kind of catchpoint this is. */
12086 enum ada_exception_catchpoint_kind m_kind
;
12089 /* An instance of this type is used to represent an Ada catchpoint
12090 breakpoint location. */
12092 class ada_catchpoint_location
: public bp_location
12095 explicit ada_catchpoint_location (ada_catchpoint
*owner
)
12096 : bp_location (owner
, bp_loc_software_breakpoint
)
12099 /* The condition that checks whether the exception that was raised
12100 is the specific exception the user specified on catchpoint
12102 expression_up excep_cond_expr
;
12105 static struct symtab_and_line ada_exception_sal
12106 (enum ada_exception_catchpoint_kind ex
);
12108 /* Implement the RE_SET method in the structure for all exception
12109 catchpoint kinds. */
12112 ada_catchpoint::re_set (program_space
*pspace
)
12114 std::vector
<symtab_and_line
> sals
;
12117 struct symtab_and_line sal
= ada_exception_sal (m_kind
);
12118 sals
.push_back (sal
);
12120 catch (const gdb_exception_error
&ex
)
12122 /* For NOT_FOUND_ERROR, the breakpoint will be pending. */
12123 if (ex
.error
!= NOT_FOUND_ERROR
)
12127 update_breakpoint_locations (this, pspace
, sals
, {});
12129 /* Reparse the exception conditional expressions. One for each
12132 /* Nothing to do if there's no specific exception to catch. */
12133 if (m_excep_string
.empty ())
12136 /* Same if there are no locations... */
12137 if (!has_locations ())
12140 /* Compute the condition expression in text form, from the specific
12141 exception we want to catch. */
12142 std::string cond_string
12143 = ada_exception_catchpoint_cond_string (m_excep_string
.c_str (), m_kind
);
12145 /* Iterate over all the catchpoint's locations, and parse an
12146 expression for each. */
12147 for (bp_location
&bl
: locations ())
12149 ada_catchpoint_location
&ada_loc
12150 = static_cast<ada_catchpoint_location
&> (bl
);
12153 if (!bl
.shlib_disabled
)
12157 s
= cond_string
.c_str ();
12160 exp
= parse_exp_1 (&s
, bl
.address
, block_for_pc (bl
.address
), 0);
12162 catch (const gdb_exception_error
&e
)
12164 warning (_("failed to reevaluate internal exception condition "
12165 "for catchpoint %d: %s"),
12166 number
, e
.what ());
12170 ada_loc
.excep_cond_expr
= std::move (exp
);
12174 /* Implement the ALLOCATE_LOCATION method in the structure for all
12175 exception catchpoint kinds. */
12177 struct bp_location
*
12178 ada_catchpoint::allocate_location ()
12180 return new ada_catchpoint_location (this);
12183 /* See declaration. */
12186 ada_catchpoint::should_stop_exception (const struct bp_location
*bl
) const
12188 ada_catchpoint
*c
= gdb::checked_static_cast
<ada_catchpoint
*> (bl
->owner
);
12189 const struct ada_catchpoint_location
*ada_loc
12190 = (const struct ada_catchpoint_location
*) bl
;
12193 struct internalvar
*var
= lookup_internalvar ("_ada_exception");
12194 if (c
->m_kind
== ada_catch_assert
)
12195 clear_internalvar (var
);
12202 if (c
->m_kind
== ada_catch_handlers
)
12203 expr
= ("GNAT_GCC_exception_Access(gcc_exception)"
12204 ".all.occurrence.id");
12208 struct value
*exc
= parse_and_eval (expr
);
12209 set_internalvar (var
, exc
);
12211 catch (const gdb_exception_error
&ex
)
12213 clear_internalvar (var
);
12217 /* With no specific exception, should always stop. */
12218 if (c
->m_excep_string
.empty ())
12221 if (ada_loc
->excep_cond_expr
== NULL
)
12223 /* We will have a NULL expression if back when we were creating
12224 the expressions, this location's had failed to parse. */
12231 scoped_value_mark mark
;
12232 stop
= value_true (ada_loc
->excep_cond_expr
->evaluate ());
12234 catch (const gdb_exception_error
&ex
)
12236 exception_fprintf (gdb_stderr
, ex
,
12237 _("Error in testing exception condition:\n"));
12243 /* Implement the CHECK_STATUS method in the structure for all
12244 exception catchpoint kinds. */
12247 ada_catchpoint::check_status (bpstat
*bs
)
12249 bs
->stop
= should_stop_exception (bs
->bp_location_at
.get ());
12252 /* Implement the PRINT_IT method in the structure for all exception
12253 catchpoint kinds. */
12255 enum print_stop_action
12256 ada_catchpoint::print_it (const bpstat
*bs
) const
12258 struct ui_out
*uiout
= current_uiout
;
12260 annotate_catchpoint (number
);
12262 if (uiout
->is_mi_like_p ())
12264 uiout
->field_string ("reason",
12265 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT
));
12266 uiout
->field_string ("disp", bpdisp_text (disposition
));
12269 uiout
->text (disposition
== disp_del
12270 ? "\nTemporary catchpoint " : "\nCatchpoint ");
12271 print_num_locno (bs
, uiout
);
12272 uiout
->text (", ");
12274 /* ada_exception_name_addr relies on the selected frame being the
12275 current frame. Need to do this here because this function may be
12276 called more than once when printing a stop, and below, we'll
12277 select the first frame past the Ada run-time (see
12278 ada_find_printable_frame). */
12279 select_frame (get_current_frame ());
12283 case ada_catch_exception
:
12284 case ada_catch_exception_unhandled
:
12285 case ada_catch_handlers
:
12287 const CORE_ADDR addr
= ada_exception_name_addr (m_kind
);
12288 char exception_name
[256];
12292 read_memory (addr
, (gdb_byte
*) exception_name
,
12293 sizeof (exception_name
) - 1);
12294 exception_name
[sizeof (exception_name
) - 1] = '\0';
12298 /* For some reason, we were unable to read the exception
12299 name. This could happen if the Runtime was compiled
12300 without debugging info, for instance. In that case,
12301 just replace the exception name by the generic string
12302 "exception" - it will read as "an exception" in the
12303 notification we are about to print. */
12304 memcpy (exception_name
, "exception", sizeof ("exception"));
12306 /* In the case of unhandled exception breakpoints, we print
12307 the exception name as "unhandled EXCEPTION_NAME", to make
12308 it clearer to the user which kind of catchpoint just got
12309 hit. We used ui_out_text to make sure that this extra
12310 info does not pollute the exception name in the MI case. */
12311 if (m_kind
== ada_catch_exception_unhandled
)
12312 uiout
->text ("unhandled ");
12313 uiout
->field_string ("exception-name", exception_name
);
12316 case ada_catch_assert
:
12317 /* In this case, the name of the exception is not really
12318 important. Just print "failed assertion" to make it clearer
12319 that his program just hit an assertion-failure catchpoint.
12320 We used ui_out_text because this info does not belong in
12322 uiout
->text ("failed assertion");
12326 gdb::unique_xmalloc_ptr
<char> exception_message
= ada_exception_message ();
12327 if (exception_message
!= NULL
)
12329 uiout
->text (" (");
12330 uiout
->field_string ("exception-message", exception_message
.get ());
12334 uiout
->text (" at ");
12335 ada_find_printable_frame (get_current_frame ());
12337 return PRINT_SRC_AND_LOC
;
12340 /* Implement the PRINT_ONE method in the structure for all exception
12341 catchpoint kinds. */
12344 ada_catchpoint::print_one (const bp_location
**last_loc
) const
12346 struct ui_out
*uiout
= current_uiout
;
12347 struct value_print_options opts
;
12349 get_user_print_options (&opts
);
12351 if (opts
.addressprint
)
12352 uiout
->field_skip ("addr");
12354 annotate_field (5);
12357 case ada_catch_exception
:
12358 if (!m_excep_string
.empty ())
12360 std::string msg
= string_printf (_("`%s' Ada exception"),
12361 m_excep_string
.c_str ());
12363 uiout
->field_string ("what", msg
);
12366 uiout
->field_string ("what", "all Ada exceptions");
12370 case ada_catch_exception_unhandled
:
12371 uiout
->field_string ("what", "unhandled Ada exceptions");
12374 case ada_catch_handlers
:
12375 if (!m_excep_string
.empty ())
12377 uiout
->field_fmt ("what",
12378 _("`%s' Ada exception handlers"),
12379 m_excep_string
.c_str ());
12382 uiout
->field_string ("what", "all Ada exceptions handlers");
12385 case ada_catch_assert
:
12386 uiout
->field_string ("what", "failed Ada assertions");
12390 internal_error (_("unexpected catchpoint type"));
12397 /* Implement the PRINT_MENTION method in the breakpoint_ops structure
12398 for all exception catchpoint kinds. */
12401 ada_catchpoint::print_mention () const
12403 struct ui_out
*uiout
= current_uiout
;
12405 uiout
->text (disposition
== disp_del
? _("Temporary catchpoint ")
12406 : _("Catchpoint "));
12407 uiout
->field_signed ("bkptno", number
);
12408 uiout
->text (": ");
12412 case ada_catch_exception
:
12413 if (!m_excep_string
.empty ())
12415 std::string info
= string_printf (_("`%s' Ada exception"),
12416 m_excep_string
.c_str ());
12417 uiout
->text (info
);
12420 uiout
->text (_("all Ada exceptions"));
12423 case ada_catch_exception_unhandled
:
12424 uiout
->text (_("unhandled Ada exceptions"));
12427 case ada_catch_handlers
:
12428 if (!m_excep_string
.empty ())
12431 = string_printf (_("`%s' Ada exception handlers"),
12432 m_excep_string
.c_str ());
12433 uiout
->text (info
);
12436 uiout
->text (_("all Ada exceptions handlers"));
12439 case ada_catch_assert
:
12440 uiout
->text (_("failed Ada assertions"));
12444 internal_error (_("unexpected catchpoint type"));
12449 /* Implement the PRINT_RECREATE method in the structure for all
12450 exception catchpoint kinds. */
12453 ada_catchpoint::print_recreate (struct ui_file
*fp
) const
12457 case ada_catch_exception
:
12458 gdb_printf (fp
, "catch exception");
12459 if (!m_excep_string
.empty ())
12460 gdb_printf (fp
, " %s", m_excep_string
.c_str ());
12463 case ada_catch_exception_unhandled
:
12464 gdb_printf (fp
, "catch exception unhandled");
12467 case ada_catch_handlers
:
12468 gdb_printf (fp
, "catch handlers");
12471 case ada_catch_assert
:
12472 gdb_printf (fp
, "catch assert");
12476 internal_error (_("unexpected catchpoint type"));
12478 print_recreate_thread (fp
);
12481 /* See ada-lang.h. */
12484 is_ada_exception_catchpoint (breakpoint
*bp
)
12486 return dynamic_cast<ada_catchpoint
*> (bp
) != nullptr;
12489 /* Split the arguments specified in a "catch exception" command.
12490 Set EX to the appropriate catchpoint type.
12491 Set EXCEP_STRING to the name of the specific exception if
12492 specified by the user.
12493 IS_CATCH_HANDLERS_CMD: True if the arguments are for a
12494 "catch handlers" command. False otherwise.
12495 If a condition is found at the end of the arguments, the condition
12496 expression is stored in COND_STRING (memory must be deallocated
12497 after use). Otherwise COND_STRING is set to NULL. */
12500 catch_ada_exception_command_split (const char *args
,
12501 bool is_catch_handlers_cmd
,
12502 enum ada_exception_catchpoint_kind
*ex
,
12503 std::string
*excep_string
,
12504 std::string
*cond_string
)
12506 std::string exception_name
;
12508 exception_name
= extract_arg (&args
);
12509 if (exception_name
== "if")
12511 /* This is not an exception name; this is the start of a condition
12512 expression for a catchpoint on all exceptions. So, "un-get"
12513 this token, and set exception_name to NULL. */
12514 exception_name
.clear ();
12518 /* Check to see if we have a condition. */
12520 args
= skip_spaces (args
);
12521 if (startswith (args
, "if")
12522 && (isspace (args
[2]) || args
[2] == '\0'))
12525 args
= skip_spaces (args
);
12527 if (args
[0] == '\0')
12528 error (_("Condition missing after `if' keyword"));
12529 *cond_string
= args
;
12531 args
+= strlen (args
);
12534 /* Check that we do not have any more arguments. Anything else
12537 if (args
[0] != '\0')
12538 error (_("Junk at end of expression"));
12540 if (is_catch_handlers_cmd
)
12542 /* Catch handling of exceptions. */
12543 *ex
= ada_catch_handlers
;
12544 *excep_string
= exception_name
;
12546 else if (exception_name
.empty ())
12548 /* Catch all exceptions. */
12549 *ex
= ada_catch_exception
;
12550 excep_string
->clear ();
12552 else if (exception_name
== "unhandled")
12554 /* Catch unhandled exceptions. */
12555 *ex
= ada_catch_exception_unhandled
;
12556 excep_string
->clear ();
12560 /* Catch a specific exception. */
12561 *ex
= ada_catch_exception
;
12562 *excep_string
= exception_name
;
12566 /* Return the name of the symbol on which we should break in order to
12567 implement a catchpoint of the EX kind. */
12569 static const char *
12570 ada_exception_sym_name (enum ada_exception_catchpoint_kind ex
)
12572 struct ada_inferior_data
*data
= get_ada_inferior_data (current_inferior ());
12574 gdb_assert (data
->exception_info
!= NULL
);
12578 case ada_catch_exception
:
12579 return (data
->exception_info
->catch_exception_sym
);
12581 case ada_catch_exception_unhandled
:
12582 return (data
->exception_info
->catch_exception_unhandled_sym
);
12584 case ada_catch_assert
:
12585 return (data
->exception_info
->catch_assert_sym
);
12587 case ada_catch_handlers
:
12588 return (data
->exception_info
->catch_handlers_sym
);
12591 internal_error (_("unexpected catchpoint kind (%d)"), ex
);
12595 /* Return the condition that will be used to match the current exception
12596 being raised with the exception that the user wants to catch. This
12597 assumes that this condition is used when the inferior just triggered
12598 an exception catchpoint.
12599 EX: the type of catchpoints used for catching Ada exceptions. */
12602 ada_exception_catchpoint_cond_string (const char *excep_string
,
12603 enum ada_exception_catchpoint_kind ex
)
12605 bool is_standard_exc
= false;
12606 std::string result
;
12608 if (ex
== ada_catch_handlers
)
12610 /* For exception handlers catchpoints, the condition string does
12611 not use the same parameter as for the other exceptions. */
12612 result
= ("long_integer (GNAT_GCC_exception_Access"
12613 "(gcc_exception).all.occurrence.id)");
12616 result
= "long_integer (e)";
12618 /* The standard exceptions are a special case. They are defined in
12619 runtime units that have been compiled without debugging info; if
12620 EXCEP_STRING is the not-fully-qualified name of a standard
12621 exception (e.g. "constraint_error") then, during the evaluation
12622 of the condition expression, the symbol lookup on this name would
12623 *not* return this standard exception. The catchpoint condition
12624 may then be set only on user-defined exceptions which have the
12625 same not-fully-qualified name (e.g. my_package.constraint_error).
12627 To avoid this unexpected behavior, these standard exceptions are
12628 systematically prefixed by "standard". This means that "catch
12629 exception constraint_error" is rewritten into "catch exception
12630 standard.constraint_error".
12632 If an exception named constraint_error is defined in another package of
12633 the inferior program, then the only way to specify this exception as a
12634 breakpoint condition is to use its fully-qualified named:
12635 e.g. my_package.constraint_error. */
12637 for (const char *name
: standard_exc
)
12639 if (strcmp (name
, excep_string
) == 0)
12641 is_standard_exc
= true;
12648 if (is_standard_exc
)
12649 string_appendf (result
, "long_integer (&standard.%s)", excep_string
);
12651 string_appendf (result
, "long_integer (&%s)", excep_string
);
12656 /* Return the symtab_and_line that should be used to insert an
12657 exception catchpoint of the TYPE kind. */
12659 static struct symtab_and_line
12660 ada_exception_sal (enum ada_exception_catchpoint_kind ex
)
12662 const char *sym_name
;
12663 struct symbol
*sym
;
12665 /* First, find out which exception support info to use. */
12666 ada_exception_support_info_sniffer ();
12668 /* Then lookup the function on which we will break in order to catch
12669 the Ada exceptions requested by the user. */
12670 sym_name
= ada_exception_sym_name (ex
);
12671 sym
= standard_lookup (sym_name
, NULL
, SEARCH_VFT
);
12674 throw_error (NOT_FOUND_ERROR
, _("Catchpoint symbol not found: %s"),
12677 if (sym
->aclass () != LOC_BLOCK
)
12678 error (_("Unable to insert catchpoint. %s is not a function."), sym_name
);
12680 return find_function_start_sal (sym
, 1);
12683 /* Create an Ada exception catchpoint.
12685 EX_KIND is the kind of exception catchpoint to be created.
12687 If EXCEPT_STRING is empty, this catchpoint is expected to trigger
12688 for all exceptions. Otherwise, EXCEPT_STRING indicates the name
12689 of the exception to which this catchpoint applies.
12691 COND_STRING, if not empty, is the catchpoint condition.
12693 TEMPFLAG, if nonzero, means that the underlying breakpoint
12694 should be temporary.
12696 FROM_TTY is the usual argument passed to all commands implementations. */
12699 create_ada_exception_catchpoint (struct gdbarch
*gdbarch
,
12700 enum ada_exception_catchpoint_kind ex_kind
,
12701 std::string
&&excep_string
,
12702 const std::string
&cond_string
,
12707 /* This works around an obscure issue when an Ada program is
12708 compiled with LTO. */
12709 scoped_restore_current_language
save_language (language_ada
);
12711 std::unique_ptr
<ada_catchpoint
> c
12712 (new ada_catchpoint (gdbarch
, ex_kind
,
12713 cond_string
.empty () ? nullptr : cond_string
.c_str (),
12714 tempflag
, enabled
, from_tty
,
12715 std::move (excep_string
)));
12716 install_breakpoint (0, std::move (c
), 1);
12719 /* Implement the "catch exception" command. */
12722 catch_ada_exception_command (const char *arg_entry
, int from_tty
,
12723 struct cmd_list_element
*command
)
12725 const char *arg
= arg_entry
;
12726 struct gdbarch
*gdbarch
= get_current_arch ();
12728 enum ada_exception_catchpoint_kind ex_kind
;
12729 std::string excep_string
;
12730 std::string cond_string
;
12732 tempflag
= command
->context () == CATCH_TEMPORARY
;
12736 catch_ada_exception_command_split (arg
, false, &ex_kind
, &excep_string
,
12738 create_ada_exception_catchpoint (gdbarch
, ex_kind
,
12739 std::move (excep_string
), cond_string
,
12740 tempflag
, 1 /* enabled */,
12744 /* Implement the "catch handlers" command. */
12747 catch_ada_handlers_command (const char *arg_entry
, int from_tty
,
12748 struct cmd_list_element
*command
)
12750 const char *arg
= arg_entry
;
12751 struct gdbarch
*gdbarch
= get_current_arch ();
12753 enum ada_exception_catchpoint_kind ex_kind
;
12754 std::string excep_string
;
12755 std::string cond_string
;
12757 tempflag
= command
->context () == CATCH_TEMPORARY
;
12761 catch_ada_exception_command_split (arg
, true, &ex_kind
, &excep_string
,
12763 create_ada_exception_catchpoint (gdbarch
, ex_kind
,
12764 std::move (excep_string
), cond_string
,
12765 tempflag
, 1 /* enabled */,
12769 /* Completion function for the Ada "catch" commands. */
12772 catch_ada_completer (struct cmd_list_element
*cmd
, completion_tracker
&tracker
,
12773 const char *text
, const char *word
)
12775 std::vector
<ada_exc_info
> exceptions
= ada_exceptions_list (NULL
);
12777 for (const ada_exc_info
&info
: exceptions
)
12779 if (startswith (info
.name
, word
))
12780 tracker
.add_completion (make_unique_xstrdup (info
.name
));
12784 /* Split the arguments specified in a "catch assert" command.
12786 ARGS contains the command's arguments (or the empty string if
12787 no arguments were passed).
12789 If ARGS contains a condition, set COND_STRING to that condition
12790 (the memory needs to be deallocated after use). */
12793 catch_ada_assert_command_split (const char *args
, std::string
&cond_string
)
12795 args
= skip_spaces (args
);
12797 /* Check whether a condition was provided. */
12798 if (startswith (args
, "if")
12799 && (isspace (args
[2]) || args
[2] == '\0'))
12802 args
= skip_spaces (args
);
12803 if (args
[0] == '\0')
12804 error (_("condition missing after `if' keyword"));
12805 cond_string
.assign (args
);
12808 /* Otherwise, there should be no other argument at the end of
12810 else if (args
[0] != '\0')
12811 error (_("Junk at end of arguments."));
12814 /* Implement the "catch assert" command. */
12817 catch_assert_command (const char *arg_entry
, int from_tty
,
12818 struct cmd_list_element
*command
)
12820 const char *arg
= arg_entry
;
12821 struct gdbarch
*gdbarch
= get_current_arch ();
12823 std::string cond_string
;
12825 tempflag
= command
->context () == CATCH_TEMPORARY
;
12829 catch_ada_assert_command_split (arg
, cond_string
);
12830 create_ada_exception_catchpoint (gdbarch
, ada_catch_assert
,
12832 tempflag
, 1 /* enabled */,
12836 /* Return non-zero if the symbol SYM is an Ada exception object. */
12839 ada_is_exception_sym (struct symbol
*sym
)
12841 const char *type_name
= sym
->type ()->name ();
12843 return (sym
->aclass () != LOC_TYPEDEF
12844 && sym
->aclass () != LOC_BLOCK
12845 && sym
->aclass () != LOC_CONST
12846 && sym
->aclass () != LOC_UNRESOLVED
12847 && type_name
!= NULL
&& strcmp (type_name
, "exception") == 0);
12850 /* Given a global symbol SYM, return non-zero iff SYM is a non-standard
12851 Ada exception object. This matches all exceptions except the ones
12852 defined by the Ada language. */
12855 ada_is_non_standard_exception_sym (struct symbol
*sym
)
12857 if (!ada_is_exception_sym (sym
))
12860 for (const char *name
: standard_exc
)
12861 if (strcmp (sym
->linkage_name (), name
) == 0)
12862 return 0; /* A standard exception. */
12864 /* Numeric_Error is also a standard exception, so exclude it.
12865 See the STANDARD_EXC description for more details as to why
12866 this exception is not listed in that array. */
12867 if (strcmp (sym
->linkage_name (), "numeric_error") == 0)
12873 /* A helper function for std::sort, comparing two struct ada_exc_info
12876 The comparison is determined first by exception name, and then
12877 by exception address. */
12880 ada_exc_info::operator< (const ada_exc_info
&other
) const
12884 result
= strcmp (name
, other
.name
);
12887 if (result
== 0 && addr
< other
.addr
)
12893 ada_exc_info::operator== (const ada_exc_info
&other
) const
12895 return addr
== other
.addr
&& strcmp (name
, other
.name
) == 0;
12898 /* Sort EXCEPTIONS using compare_ada_exception_info as the comparison
12899 routine, but keeping the first SKIP elements untouched.
12901 All duplicates are also removed. */
12904 sort_remove_dups_ada_exceptions_list (std::vector
<ada_exc_info
> *exceptions
,
12907 std::sort (exceptions
->begin () + skip
, exceptions
->end ());
12908 exceptions
->erase (std::unique (exceptions
->begin () + skip
, exceptions
->end ()),
12909 exceptions
->end ());
12912 /* Add all exceptions defined by the Ada standard whose name match
12913 a regular expression.
12915 If PREG is not NULL, then this regexp_t object is used to
12916 perform the symbol name matching. Otherwise, no name-based
12917 filtering is performed.
12919 EXCEPTIONS is a vector of exceptions to which matching exceptions
12923 ada_add_standard_exceptions (compiled_regex
*preg
,
12924 std::vector
<ada_exc_info
> *exceptions
)
12926 for (const char *name
: standard_exc
)
12928 if (preg
== NULL
|| preg
->exec (name
, 0, NULL
, 0) == 0)
12930 symbol_name_match_type match_type
= name_match_type_from_name (name
);
12931 lookup_name_info
lookup_name (name
, match_type
);
12933 symbol_name_matcher_ftype
*match_name
12934 = ada_get_symbol_name_matcher (lookup_name
);
12936 /* Iterate over all objfiles irrespective of scope or linker
12937 namespaces so we get all exceptions anywhere in the
12939 for (objfile
*objfile
: current_program_space
->objfiles ())
12941 for (minimal_symbol
*msymbol
: objfile
->msymbols ())
12943 if (match_name (msymbol
->linkage_name (), lookup_name
,
12945 && msymbol
->type () != mst_solib_trampoline
)
12948 = {name
, msymbol
->value_address (objfile
)};
12950 exceptions
->push_back (info
);
12958 /* Add all Ada exceptions defined locally and accessible from the given
12961 If PREG is not NULL, then this regexp_t object is used to
12962 perform the symbol name matching. Otherwise, no name-based
12963 filtering is performed.
12965 EXCEPTIONS is a vector of exceptions to which matching exceptions
12969 ada_add_exceptions_from_frame (compiled_regex
*preg
,
12970 const frame_info_ptr
&frame
,
12971 std::vector
<ada_exc_info
> *exceptions
)
12973 const struct block
*block
= get_frame_block (frame
, 0);
12977 for (struct symbol
*sym
: block_iterator_range (block
))
12979 switch (sym
->aclass ())
12986 if (ada_is_exception_sym (sym
))
12988 struct ada_exc_info info
= {sym
->print_name (),
12989 sym
->value_address ()};
12991 exceptions
->push_back (info
);
12995 if (block
->function () != NULL
)
12997 block
= block
->superblock ();
13001 /* Return true if NAME matches PREG or if PREG is NULL. */
13004 name_matches_regex (const char *name
, compiled_regex
*preg
)
13006 return (preg
== NULL
13007 || preg
->exec (ada_decode (name
).c_str (), 0, NULL
, 0) == 0);
13010 /* Add all exceptions defined globally whose name name match
13011 a regular expression, excluding standard exceptions.
13013 The reason we exclude standard exceptions is that they need
13014 to be handled separately: Standard exceptions are defined inside
13015 a runtime unit which is normally not compiled with debugging info,
13016 and thus usually do not show up in our symbol search. However,
13017 if the unit was in fact built with debugging info, we need to
13018 exclude them because they would duplicate the entry we found
13019 during the special loop that specifically searches for those
13020 standard exceptions.
13022 If PREG is not NULL, then this regexp_t object is used to
13023 perform the symbol name matching. Otherwise, no name-based
13024 filtering is performed.
13026 EXCEPTIONS is a vector of exceptions to which matching exceptions
13030 ada_add_global_exceptions (compiled_regex
*preg
,
13031 std::vector
<ada_exc_info
> *exceptions
)
13033 /* In Ada, the symbol "search name" is a linkage name, whereas the
13034 regular expression used to do the matching refers to the natural
13035 name. So match against the decoded name. */
13036 expand_symtabs_matching (NULL
,
13037 lookup_name_info::match_any (),
13038 [&] (const char *search_name
)
13040 std::string decoded
= ada_decode (search_name
);
13041 return name_matches_regex (decoded
.c_str (), preg
);
13044 SEARCH_GLOBAL_BLOCK
| SEARCH_STATIC_BLOCK
,
13046 [&] (enum language lang
)
13048 /* Try to skip non-Ada CUs. */
13049 return lang
== language_ada
;
13052 /* Iterate over all objfiles irrespective of scope or linker namespaces
13053 so we get all exceptions anywhere in the progspace. */
13054 for (objfile
*objfile
: current_program_space
->objfiles ())
13056 for (compunit_symtab
*s
: objfile
->compunits ())
13058 const struct blockvector
*bv
= s
->blockvector ();
13061 for (i
= GLOBAL_BLOCK
; i
<= STATIC_BLOCK
; i
++)
13063 const struct block
*b
= bv
->block (i
);
13065 for (struct symbol
*sym
: block_iterator_range (b
))
13066 if (ada_is_non_standard_exception_sym (sym
)
13067 && name_matches_regex (sym
->natural_name (), preg
))
13069 struct ada_exc_info info
13070 = {sym
->print_name (), sym
->value_address ()};
13072 exceptions
->push_back (info
);
13079 /* Implements ada_exceptions_list with the regular expression passed
13080 as a regex_t, rather than a string.
13082 If not NULL, PREG is used to filter out exceptions whose names
13083 do not match. Otherwise, all exceptions are listed. */
13085 static std::vector
<ada_exc_info
>
13086 ada_exceptions_list_1 (compiled_regex
*preg
)
13088 std::vector
<ada_exc_info
> result
;
13091 /* First, list the known standard exceptions. These exceptions
13092 need to be handled separately, as they are usually defined in
13093 runtime units that have been compiled without debugging info. */
13095 ada_add_standard_exceptions (preg
, &result
);
13097 /* Next, find all exceptions whose scope is local and accessible
13098 from the currently selected frame. */
13100 if (has_stack_frames ())
13102 prev_len
= result
.size ();
13103 ada_add_exceptions_from_frame (preg
, get_selected_frame (NULL
),
13105 if (result
.size () > prev_len
)
13106 sort_remove_dups_ada_exceptions_list (&result
, prev_len
);
13109 /* Add all exceptions whose scope is global. */
13111 prev_len
= result
.size ();
13112 ada_add_global_exceptions (preg
, &result
);
13113 if (result
.size () > prev_len
)
13114 sort_remove_dups_ada_exceptions_list (&result
, prev_len
);
13119 /* Return a vector of ada_exc_info.
13121 If REGEXP is NULL, all exceptions are included in the result.
13122 Otherwise, it should contain a valid regular expression,
13123 and only the exceptions whose names match that regular expression
13124 are included in the result.
13126 The exceptions are sorted in the following order:
13127 - Standard exceptions (defined by the Ada language), in
13128 alphabetical order;
13129 - Exceptions only visible from the current frame, in
13130 alphabetical order;
13131 - Exceptions whose scope is global, in alphabetical order. */
13133 std::vector
<ada_exc_info
>
13134 ada_exceptions_list (const char *regexp
)
13136 if (regexp
== NULL
)
13137 return ada_exceptions_list_1 (NULL
);
13139 compiled_regex
reg (regexp
, REG_NOSUB
, _("invalid regular expression"));
13140 return ada_exceptions_list_1 (®
);
13143 /* Implement the "info exceptions" command. */
13146 info_exceptions_command (const char *regexp
, int from_tty
)
13148 struct gdbarch
*gdbarch
= get_current_arch ();
13150 std::vector
<ada_exc_info
> exceptions
= ada_exceptions_list (regexp
);
13152 if (regexp
!= NULL
)
13154 (_("All Ada exceptions matching regular expression \"%s\":\n"), regexp
);
13156 gdb_printf (_("All defined Ada exceptions:\n"));
13158 for (const ada_exc_info
&info
: exceptions
)
13159 gdb_printf ("%s: %s\n", info
.name
, paddress (gdbarch
, info
.addr
));
13163 /* Language vector */
13165 /* symbol_name_matcher_ftype adapter for wild_match. */
13168 do_wild_match (const char *symbol_search_name
,
13169 const lookup_name_info
&lookup_name
,
13170 completion_match_result
*comp_match_res
)
13172 return wild_match (symbol_search_name
, ada_lookup_name (lookup_name
));
13175 /* symbol_name_matcher_ftype adapter for full_match. */
13178 do_full_match (const char *symbol_search_name
,
13179 const lookup_name_info
&lookup_name
,
13180 completion_match_result
*comp_match_res
)
13182 const char *lname
= lookup_name
.ada ().lookup_name ().c_str ();
13184 /* If both symbols start with "_ada_", just let the loop below
13185 handle the comparison. However, if only the symbol name starts
13186 with "_ada_", skip the prefix and let the match proceed as
13188 if (startswith (symbol_search_name
, "_ada_")
13189 && !startswith (lname
, "_ada"))
13190 symbol_search_name
+= 5;
13191 /* Likewise for ghost entities. */
13192 if (startswith (symbol_search_name
, "___ghost_")
13193 && !startswith (lname
, "___ghost_"))
13194 symbol_search_name
+= 9;
13196 int uscore_count
= 0;
13197 while (*lname
!= '\0')
13199 if (*symbol_search_name
!= *lname
)
13201 if (*symbol_search_name
== 'B' && uscore_count
== 2
13202 && symbol_search_name
[1] == '_')
13204 symbol_search_name
+= 2;
13205 while (isdigit (*symbol_search_name
))
13206 ++symbol_search_name
;
13207 if (symbol_search_name
[0] == '_'
13208 && symbol_search_name
[1] == '_')
13210 symbol_search_name
+= 2;
13217 if (*symbol_search_name
== '_')
13222 ++symbol_search_name
;
13226 return is_name_suffix (symbol_search_name
);
13229 /* symbol_name_matcher_ftype for exact (verbatim) matches. */
13232 do_exact_match (const char *symbol_search_name
,
13233 const lookup_name_info
&lookup_name
,
13234 completion_match_result
*comp_match_res
)
13236 return strcmp (symbol_search_name
, ada_lookup_name (lookup_name
)) == 0;
13239 /* Build the Ada lookup name for LOOKUP_NAME. */
13241 ada_lookup_name_info::ada_lookup_name_info (const lookup_name_info
&lookup_name
)
13243 std::string_view user_name
= lookup_name
.name ();
13245 if (!user_name
.empty () && user_name
[0] == '<')
13247 if (user_name
.back () == '>')
13248 m_encoded_name
= user_name
.substr (1, user_name
.size () - 2);
13250 m_encoded_name
= user_name
.substr (1, user_name
.size () - 1);
13251 m_encoded_p
= true;
13252 m_verbatim_p
= true;
13253 m_wild_match_p
= false;
13254 m_standard_p
= false;
13258 m_verbatim_p
= false;
13260 m_encoded_p
= user_name
.find ("__") != std::string_view::npos
;
13264 const char *folded
= ada_fold_name (user_name
);
13265 m_encoded_name
= ada_encode_1 (folded
, false);
13266 if (m_encoded_name
.empty ())
13267 m_encoded_name
= user_name
;
13270 m_encoded_name
= user_name
;
13272 /* Handle the 'package Standard' special case. See description
13273 of m_standard_p. */
13274 if (startswith (m_encoded_name
.c_str (), "standard__"))
13276 m_encoded_name
= m_encoded_name
.substr (sizeof ("standard__") - 1);
13277 m_standard_p
= true;
13280 m_standard_p
= false;
13282 m_decoded_name
= ada_decode (m_encoded_name
.c_str (), true, false, false);
13284 /* If the name contains a ".", then the user is entering a fully
13285 qualified entity name, and the match must not be done in wild
13286 mode. Similarly, if the user wants to complete what looks
13287 like an encoded name, the match must not be done in wild
13288 mode. Also, in the standard__ special case always do
13289 non-wild matching. */
13291 = (lookup_name
.match_type () != symbol_name_match_type::FULL
13294 && user_name
.find ('.') == std::string::npos
);
13298 /* symbol_name_matcher_ftype method for Ada. This only handles
13299 completion mode. */
13302 ada_symbol_name_matches (const char *symbol_search_name
,
13303 const lookup_name_info
&lookup_name
,
13304 completion_match_result
*comp_match_res
)
13306 return lookup_name
.ada ().matches (symbol_search_name
,
13307 lookup_name
.match_type (),
13311 /* A name matcher that matches the symbol name exactly, with
13315 literal_symbol_name_matcher (const char *symbol_search_name
,
13316 const lookup_name_info
&lookup_name
,
13317 completion_match_result
*comp_match_res
)
13319 std::string_view name_view
= lookup_name
.name ();
13321 if (lookup_name
.completion_mode ()
13322 ? (strncmp (symbol_search_name
, name_view
.data (),
13323 name_view
.size ()) == 0)
13324 : symbol_search_name
== name_view
)
13326 if (comp_match_res
!= NULL
)
13327 comp_match_res
->set_match (symbol_search_name
);
13334 /* Implement the "get_symbol_name_matcher" language_defn method for
13337 static symbol_name_matcher_ftype
*
13338 ada_get_symbol_name_matcher (const lookup_name_info
&lookup_name
)
13340 if (lookup_name
.match_type () == symbol_name_match_type::SEARCH_NAME
)
13341 return literal_symbol_name_matcher
;
13343 if (lookup_name
.completion_mode ())
13344 return ada_symbol_name_matches
;
13347 if (lookup_name
.ada ().wild_match_p ())
13348 return do_wild_match
;
13349 else if (lookup_name
.ada ().verbatim_p ())
13350 return do_exact_match
;
13352 return do_full_match
;
13356 /* Class representing the Ada language. */
13358 class ada_language
: public language_defn
13362 : language_defn (language_ada
)
13365 /* See language.h. */
13367 const char *name () const override
13370 /* See language.h. */
13372 const char *natural_name () const override
13375 /* See language.h. */
13377 const std::vector
<const char *> &filename_extensions () const override
13379 static const std::vector
<const char *> extensions
13380 = { ".adb", ".ads", ".a", ".ada", ".dg" };
13384 /* Print an array element index using the Ada syntax. */
13386 void print_array_index (struct type
*index_type
,
13388 struct ui_file
*stream
,
13389 const value_print_options
*options
) const override
13391 struct value
*index_value
= val_atr (index_type
, index
);
13393 value_print (index_value
, stream
, options
);
13394 gdb_printf (stream
, " => ");
13397 /* Implement the "read_var_value" language_defn method for Ada. */
13399 struct value
*read_var_value (struct symbol
*var
,
13400 const struct block
*var_block
,
13401 const frame_info_ptr
&frame
) const override
13403 /* The only case where default_read_var_value is not sufficient
13404 is when VAR is a renaming... */
13405 if (frame
!= nullptr)
13407 const struct block
*frame_block
= get_frame_block (frame
, NULL
);
13408 if (frame_block
!= nullptr && ada_is_renaming_symbol (var
))
13409 return ada_read_renaming_var_value (var
, frame_block
);
13412 /* This is a typical case where we expect the default_read_var_value
13413 function to work. */
13414 return language_defn::read_var_value (var
, var_block
, frame
);
13417 /* See language.h. */
13418 bool symbol_printing_suppressed (struct symbol
*symbol
) const override
13420 return symbol
->is_artificial ();
13423 /* See language.h. */
13424 struct value
*value_string (struct gdbarch
*gdbarch
,
13425 const char *ptr
, ssize_t len
) const override
13427 struct type
*type
= language_string_char_type (this, gdbarch
);
13428 value
*val
= ::value_string (ptr
, len
, type
);
13429 /* VAL will be a TYPE_CODE_STRING, but Ada only knows how to print
13430 strings that are arrays of characters, so fix the type now. */
13431 gdb_assert (val
->type ()->code () == TYPE_CODE_STRING
);
13432 val
->type ()->set_code (TYPE_CODE_ARRAY
);
13436 /* See language.h. */
13437 void language_arch_info (struct gdbarch
*gdbarch
,
13438 struct language_arch_info
*lai
) const override
13440 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
13442 /* Helper function to allow shorter lines below. */
13443 auto add
= [&] (struct type
*t
)
13445 lai
->add_primitive_type (t
);
13448 type_allocator
alloc (gdbarch
);
13449 add (init_integer_type (alloc
, gdbarch_int_bit (gdbarch
),
13451 add (init_integer_type (alloc
, gdbarch_long_bit (gdbarch
),
13452 0, "long_integer"));
13453 add (init_integer_type (alloc
, gdbarch_short_bit (gdbarch
),
13454 0, "short_integer"));
13455 struct type
*char_type
= init_character_type (alloc
, TARGET_CHAR_BIT
,
13457 lai
->set_string_char_type (char_type
);
13459 add (init_character_type (alloc
, 16, 1, "wide_character"));
13460 add (init_character_type (alloc
, 32, 1, "wide_wide_character"));
13461 add (init_float_type (alloc
, gdbarch_float_bit (gdbarch
),
13462 "float", gdbarch_float_format (gdbarch
)));
13463 add (init_float_type (alloc
, gdbarch_double_bit (gdbarch
),
13464 "long_float", gdbarch_double_format (gdbarch
)));
13465 add (init_integer_type (alloc
, gdbarch_long_long_bit (gdbarch
),
13466 0, "long_long_integer"));
13467 add (init_integer_type (alloc
, 128, 0, "long_long_long_integer"));
13468 add (init_integer_type (alloc
, 128, 1, "unsigned_long_long_long_integer"));
13469 add (init_float_type (alloc
, gdbarch_long_double_bit (gdbarch
),
13471 gdbarch_long_double_format (gdbarch
)));
13472 add (init_integer_type (alloc
, gdbarch_int_bit (gdbarch
),
13474 add (init_integer_type (alloc
, gdbarch_int_bit (gdbarch
),
13476 add (builtin
->builtin_void
);
13478 struct type
*system_addr_ptr
13479 = lookup_pointer_type (alloc
.new_type (TYPE_CODE_VOID
, TARGET_CHAR_BIT
,
13481 system_addr_ptr
->set_name ("system__address");
13482 add (system_addr_ptr
);
13484 /* Create the equivalent of the System.Storage_Elements.Storage_Offset
13485 type. This is a signed integral type whose size is the same as
13486 the size of addresses. */
13487 unsigned int addr_length
= system_addr_ptr
->length ();
13488 add (init_integer_type (alloc
, addr_length
* HOST_CHAR_BIT
, 0,
13489 "storage_offset"));
13491 lai
->set_bool_type (builtin
->builtin_bool
);
13494 /* See language.h. */
13496 bool iterate_over_symbols
13497 (const struct block
*block
, const lookup_name_info
&name
,
13498 domain_search_flags domain
,
13499 gdb::function_view
<symbol_found_callback_ftype
> callback
) const override
13501 std::vector
<struct block_symbol
> results
13502 = ada_lookup_symbol_list_worker (name
, block
, domain
, 0);
13503 for (block_symbol
&sym
: results
)
13505 if (!callback (&sym
))
13512 /* See language.h. */
13513 bool sniff_from_mangled_name
13514 (const char *mangled
,
13515 gdb::unique_xmalloc_ptr
<char> *out
) const override
13517 std::string demangled
= ada_decode (mangled
);
13521 if (demangled
!= mangled
&& demangled
[0] != '<')
13523 /* Set the gsymbol language to Ada, but still return 0.
13524 Two reasons for that:
13526 1. For Ada, we prefer computing the symbol's decoded name
13527 on the fly rather than pre-compute it, in order to save
13528 memory (Ada projects are typically very large).
13530 2. There are some areas in the definition of the GNAT
13531 encoding where, with a bit of bad luck, we might be able
13532 to decode a non-Ada symbol, generating an incorrect
13533 demangled name (Eg: names ending with "TB" for instance
13534 are identified as task bodies and so stripped from
13535 the decoded name returned).
13537 Returning true, here, but not setting *DEMANGLED, helps us get
13538 a little bit of the best of both worlds. Because we're last,
13539 we should not affect any of the other languages that were
13540 able to demangle the symbol before us; we get to correctly
13541 tag Ada symbols as such; and even if we incorrectly tagged a
13542 non-Ada symbol, which should be rare, any routing through the
13543 Ada language should be transparent (Ada tries to behave much
13544 like C/C++ with non-Ada symbols). */
13551 /* See language.h. */
13553 gdb::unique_xmalloc_ptr
<char> demangle_symbol (const char *mangled
,
13554 int options
) const override
13556 return make_unique_xstrdup (ada_decode (mangled
).c_str ());
13559 /* See language.h. */
13561 void print_type (struct type
*type
, const char *varstring
,
13562 struct ui_file
*stream
, int show
, int level
,
13563 const struct type_print_options
*flags
) const override
13565 ada_print_type (type
, varstring
, stream
, show
, level
, flags
);
13568 /* See language.h. */
13570 const char *word_break_characters (void) const override
13572 return ada_completer_word_break_characters
;
13575 /* See language.h. */
13577 void collect_symbol_completion_matches (completion_tracker
&tracker
,
13578 complete_symbol_mode mode
,
13579 symbol_name_match_type name_match_type
,
13580 const char *text
, const char *word
,
13581 enum type_code code
) const override
13583 const struct block
*b
, *surrounding_static_block
= 0;
13585 gdb_assert (code
== TYPE_CODE_UNDEF
);
13587 lookup_name_info
lookup_name (text
, name_match_type
, true);
13589 /* First, look at the partial symtab symbols. */
13590 expand_symtabs_matching (NULL
,
13594 SEARCH_GLOBAL_BLOCK
| SEARCH_STATIC_BLOCK
,
13595 SEARCH_ALL_DOMAINS
);
13597 /* At this point scan through the misc symbol vectors and add each
13598 symbol you find to the list. Eventually we want to ignore
13599 anything that isn't a text symbol (everything else will be
13600 handled by the psymtab code above). */
13602 for (objfile
*objfile
: current_program_space
->objfiles ())
13604 for (minimal_symbol
*msymbol
: objfile
->msymbols ())
13608 if (completion_skip_symbol (mode
, msymbol
))
13611 language symbol_language
= msymbol
->language ();
13613 /* Ada minimal symbols won't have their language set to Ada. If
13614 we let completion_list_add_name compare using the
13615 default/C-like matcher, then when completing e.g., symbols in a
13616 package named "pck", we'd match internal Ada symbols like
13617 "pckS", which are invalid in an Ada expression, unless you wrap
13618 them in '<' '>' to request a verbatim match.
13620 Unfortunately, some Ada encoded names successfully demangle as
13621 C++ symbols (using an old mangling scheme), such as "name__2Xn"
13622 -> "Xn::name(void)" and thus some Ada minimal symbols end up
13623 with the wrong language set. Paper over that issue here. */
13624 if (symbol_language
== language_unknown
13625 || symbol_language
== language_cplus
)
13626 symbol_language
= language_ada
;
13628 completion_list_add_name (tracker
,
13630 msymbol
->linkage_name (),
13631 lookup_name
, text
, word
);
13635 /* Search upwards from currently selected frame (so that we can
13636 complete on local vars. */
13638 for (b
= get_selected_block (0); b
!= NULL
; b
= b
->superblock ())
13640 if (!b
->superblock ())
13641 surrounding_static_block
= b
; /* For elmin of dups */
13643 for (struct symbol
*sym
: block_iterator_range (b
))
13645 if (completion_skip_symbol (mode
, sym
))
13648 completion_list_add_name (tracker
,
13650 sym
->linkage_name (),
13651 lookup_name
, text
, word
);
13655 /* Go through the symtabs and check the externs and statics for
13656 symbols which match. */
13658 for (objfile
*objfile
: current_program_space
->objfiles ())
13660 for (compunit_symtab
*s
: objfile
->compunits ())
13663 b
= s
->blockvector ()->global_block ();
13664 for (struct symbol
*sym
: block_iterator_range (b
))
13666 if (completion_skip_symbol (mode
, sym
))
13669 completion_list_add_name (tracker
,
13671 sym
->linkage_name (),
13672 lookup_name
, text
, word
);
13677 for (objfile
*objfile
: current_program_space
->objfiles ())
13679 for (compunit_symtab
*s
: objfile
->compunits ())
13682 b
= s
->blockvector ()->static_block ();
13683 /* Don't do this block twice. */
13684 if (b
== surrounding_static_block
)
13686 for (struct symbol
*sym
: block_iterator_range (b
))
13688 if (completion_skip_symbol (mode
, sym
))
13691 completion_list_add_name (tracker
,
13693 sym
->linkage_name (),
13694 lookup_name
, text
, word
);
13700 /* See language.h. */
13702 gdb::unique_xmalloc_ptr
<char> watch_location_expression
13703 (struct type
*type
, CORE_ADDR addr
) const override
13705 type
= check_typedef (check_typedef (type
)->target_type ());
13706 std::string name
= type_to_string (type
);
13707 return xstrprintf ("{%s} %s", name
.c_str (), core_addr_to_string (addr
));
13710 /* See language.h. */
13712 void value_print (struct value
*val
, struct ui_file
*stream
,
13713 const struct value_print_options
*options
) const override
13715 return ada_value_print (val
, stream
, options
);
13718 /* See language.h. */
13720 void value_print_inner
13721 (struct value
*val
, struct ui_file
*stream
, int recurse
,
13722 const struct value_print_options
*options
) const override
13724 return ada_value_print_inner (val
, stream
, recurse
, options
);
13727 /* See language.h. */
13729 struct block_symbol lookup_symbol_nonlocal
13730 (const char *name
, const struct block
*block
,
13731 const domain_search_flags domain
) const override
13733 struct block_symbol sym
;
13735 sym
= ada_lookup_symbol (name
,
13738 : block
->static_block ()),
13740 if (sym
.symbol
!= NULL
)
13743 /* If we haven't found a match at this point, try the primitive
13744 types. In other languages, this search is performed before
13745 searching for global symbols in order to short-circuit that
13746 global-symbol search if it happens that the name corresponds
13747 to a primitive type. But we cannot do the same in Ada, because
13748 it is perfectly legitimate for a program to declare a type which
13749 has the same name as a standard type. If looking up a type in
13750 that situation, we have traditionally ignored the primitive type
13751 in favor of user-defined types. This is why, unlike most other
13752 languages, we search the primitive types this late and only after
13753 having searched the global symbols without success. */
13755 if ((domain
& SEARCH_TYPE_DOMAIN
) != 0)
13757 struct gdbarch
*gdbarch
;
13760 gdbarch
= current_inferior ()->arch ();
13762 gdbarch
= block
->gdbarch ();
13764 = language_lookup_primitive_type_as_symbol (this, gdbarch
, name
);
13765 if (sym
.symbol
!= NULL
)
13772 /* See language.h. */
13774 int parser (struct parser_state
*ps
) const override
13776 warnings_issued
= 0;
13777 return ada_parse (ps
);
13780 /* See language.h. */
13782 void emitchar (int ch
, struct type
*chtype
,
13783 struct ui_file
*stream
, int quoter
) const override
13785 ada_emit_char (ch
, chtype
, stream
, quoter
, 1);
13788 /* See language.h. */
13790 void printchar (int ch
, struct type
*chtype
,
13791 struct ui_file
*stream
) const override
13793 ada_printchar (ch
, chtype
, stream
);
13796 /* See language.h. */
13798 void printstr (struct ui_file
*stream
, struct type
*elttype
,
13799 const gdb_byte
*string
, unsigned int length
,
13800 const char *encoding
, int force_ellipses
,
13801 const struct value_print_options
*options
) const override
13803 ada_printstr (stream
, elttype
, string
, length
, encoding
,
13804 force_ellipses
, options
);
13807 /* See language.h. */
13809 void print_typedef (struct type
*type
, struct symbol
*new_symbol
,
13810 struct ui_file
*stream
) const override
13812 ada_print_typedef (type
, new_symbol
, stream
);
13815 /* See language.h. */
13817 bool is_string_type_p (struct type
*type
) const override
13819 return ada_is_string_type (type
);
13822 /* See language.h. */
13824 bool is_array_like (struct type
*type
) const override
13826 return (ada_is_constrained_packed_array_type (type
)
13827 || ada_is_array_descriptor_type (type
));
13830 /* See language.h. */
13832 struct value
*to_array (struct value
*val
) const override
13833 { return ada_coerce_to_simple_array (val
); }
13835 /* See language.h. */
13837 const char *struct_too_deep_ellipsis () const override
13838 { return "(...)"; }
13840 /* See language.h. */
13842 bool c_style_arrays_p () const override
13845 /* See language.h. */
13847 bool store_sym_names_in_linkage_form_p () const override
13850 /* See language.h. */
13852 const struct lang_varobj_ops
*varobj_ops () const override
13853 { return &ada_varobj_ops
; }
13856 /* See language.h. */
13858 symbol_name_matcher_ftype
*get_symbol_name_matcher_inner
13859 (const lookup_name_info
&lookup_name
) const override
13861 return ada_get_symbol_name_matcher (lookup_name
);
13865 /* Single instance of the Ada language class. */
13867 static ada_language ada_language_defn
;
13869 /* Command-list for the "set/show ada" prefix command. */
13870 static struct cmd_list_element
*set_ada_list
;
13871 static struct cmd_list_element
*show_ada_list
;
13873 /* This module's 'new_objfile' observer. */
13876 ada_new_objfile_observer (struct objfile
*objfile
)
13878 ada_clear_symbol_cache (objfile
->pspace ());
13881 /* This module's 'free_objfile' observer. */
13884 ada_free_objfile_observer (struct objfile
*objfile
)
13886 ada_clear_symbol_cache (objfile
->pspace ());
13889 /* Charsets known to GNAT. */
13890 static const char * const gnat_source_charsets
[] =
13892 /* Note that code below assumes that the default comes first.
13893 Latin-1 is the default here, because that is also GNAT's
13903 /* Note that this value is special-cased in the encoder and
13909 void _initialize_ada_language ();
13911 _initialize_ada_language ()
13913 add_setshow_prefix_cmd
13915 _("Prefix command for changing Ada-specific settings."),
13916 _("Generic command for showing Ada-specific settings."),
13917 &set_ada_list
, &show_ada_list
,
13918 &setlist
, &showlist
);
13920 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure
,
13921 &trust_pad_over_xvs
, _("\
13922 Enable or disable an optimization trusting PAD types over XVS types."), _("\
13923 Show whether an optimization trusting PAD types over XVS types is activated."),
13925 This is related to the encoding used by the GNAT compiler. The debugger\n\
13926 should normally trust the contents of PAD types, but certain older versions\n\
13927 of GNAT have a bug that sometimes causes the information in the PAD type\n\
13928 to be incorrect. Turning this setting \"off\" allows the debugger to\n\
13929 work around this bug. It is always safe to turn this option \"off\", but\n\
13930 this incurs a slight performance penalty, so it is recommended to NOT change\n\
13931 this option to \"off\" unless necessary."),
13932 NULL
, NULL
, &set_ada_list
, &show_ada_list
);
13934 add_setshow_boolean_cmd ("print-signatures", class_vars
,
13935 &print_signatures
, _("\
13936 Control the display of functions in overloads selection menu."), _("\
13937 Show how functions in overloads selection menu will be displayed."),
13939 When enabled, formal and return types are shown."),
13940 NULL
, NULL
, &set_ada_list
, &show_ada_list
);
13942 ada_source_charset
= gnat_source_charsets
[0];
13943 add_setshow_enum_cmd ("source-charset", class_files
,
13944 gnat_source_charsets
,
13945 &ada_source_charset
, _("\
13946 Set the Ada source character set."), _("\
13947 Show the Ada source character set."), _("\
13948 The character set used for Ada source files.\n\
13949 This must correspond to the '-gnati' or '-gnatW' option passed to GNAT."),
13951 &set_ada_list
, &show_ada_list
);
13953 add_catch_command ("exception", _("\
13954 Catch Ada exceptions, when raised.\n\
13955 Usage: catch exception [ARG] [if CONDITION]\n\
13956 Without any argument, stop when any Ada exception is raised.\n\
13957 If ARG is \"unhandled\" (without the quotes), only stop when the exception\n\
13958 being raised does not have a handler (and will therefore lead to the task's\n\
13960 Otherwise, the catchpoint only stops when the name of the exception being\n\
13961 raised is the same as ARG.\n\
13962 CONDITION is a boolean expression that is evaluated to see whether the\n\
13963 exception should cause a stop."),
13964 catch_ada_exception_command
,
13965 catch_ada_completer
,
13969 add_catch_command ("handlers", _("\
13970 Catch Ada exceptions, when handled.\n\
13971 Usage: catch handlers [ARG] [if CONDITION]\n\
13972 Without any argument, stop when any Ada exception is handled.\n\
13973 With an argument, catch only exceptions with the given name.\n\
13974 CONDITION is a boolean expression that is evaluated to see whether the\n\
13975 exception should cause a stop."),
13976 catch_ada_handlers_command
,
13977 catch_ada_completer
,
13980 add_catch_command ("assert", _("\
13981 Catch failed Ada assertions, when raised.\n\
13982 Usage: catch assert [if CONDITION]\n\
13983 CONDITION is a boolean expression that is evaluated to see whether the\n\
13984 exception should cause a stop."),
13985 catch_assert_command
,
13990 add_info ("exceptions", info_exceptions_command
,
13992 List all Ada exception names.\n\
13993 Usage: info exceptions [REGEXP]\n\
13994 If a regular expression is passed as an argument, only those matching\n\
13995 the regular expression are listed."));
13997 add_setshow_prefix_cmd ("ada", class_maintenance
,
13998 _("Set Ada maintenance-related variables."),
13999 _("Show Ada maintenance-related variables."),
14000 &maint_set_ada_cmdlist
, &maint_show_ada_cmdlist
,
14001 &maintenance_set_cmdlist
, &maintenance_show_cmdlist
);
14003 add_setshow_boolean_cmd
14004 ("ignore-descriptive-types", class_maintenance
,
14005 &ada_ignore_descriptive_types_p
,
14006 _("Set whether descriptive types generated by GNAT should be ignored."),
14007 _("Show whether descriptive types generated by GNAT should be ignored."),
14009 When enabled, the debugger will stop using the DW_AT_GNAT_descriptive_type\n\
14010 DWARF attribute."),
14011 NULL
, NULL
, &maint_set_ada_cmdlist
, &maint_show_ada_cmdlist
);
14013 decoded_names_store
= htab_create_alloc (256, htab_hash_string
,
14015 NULL
, xcalloc
, xfree
);
14017 /* The ada-lang observers. */
14018 gdb::observers::new_objfile
.attach (ada_new_objfile_observer
, "ada-lang");
14019 gdb::observers::all_objfiles_removed
.attach (ada_clear_symbol_cache
,
14021 gdb::observers::free_objfile
.attach (ada_free_objfile_observer
, "ada-lang");
14022 gdb::observers::inferior_exit
.attach (ada_inferior_exit
, "ada-lang");
14024 #ifdef GDB_SELF_TEST
14025 selftests::register_test ("ada-decode", ada_decode_tests
);