1 /* Ada language support routines for GDB, the GNU debugger.
3 Copyright (C) 1992-2021 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/>. */
23 #include "gdb_regex.h"
28 #include "expression.h"
29 #include "parser-defs.h"
35 #include "breakpoint.h"
38 #include "gdb_obstack.h"
40 #include "completer.h"
47 #include "observable.h"
49 #include "typeprint.h"
50 #include "namespace.h"
51 #include "cli/cli-style.h"
54 #include "mi/mi-common.h"
55 #include "arch-utils.h"
56 #include "cli/cli-utils.h"
57 #include "gdbsupport/function-view.h"
58 #include "gdbsupport/byte-vector.h"
62 /* Define whether or not the C operator '/' truncates towards zero for
63 differently signed operands (truncation direction is undefined in C).
64 Copied from valarith.c. */
66 #ifndef TRUNCATION_TOWARDS_ZERO
67 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
70 static struct type
*desc_base_type (struct type
*);
72 static struct type
*desc_bounds_type (struct type
*);
74 static struct value
*desc_bounds (struct value
*);
76 static int fat_pntr_bounds_bitpos (struct type
*);
78 static int fat_pntr_bounds_bitsize (struct type
*);
80 static struct type
*desc_data_target_type (struct type
*);
82 static struct value
*desc_data (struct value
*);
84 static int fat_pntr_data_bitpos (struct type
*);
86 static int fat_pntr_data_bitsize (struct type
*);
88 static struct value
*desc_one_bound (struct value
*, int, int);
90 static int desc_bound_bitpos (struct type
*, int, int);
92 static int desc_bound_bitsize (struct type
*, int, int);
94 static struct type
*desc_index_type (struct type
*, int);
96 static int desc_arity (struct type
*);
98 static int ada_type_match (struct type
*, struct type
*, int);
100 static int ada_args_match (struct symbol
*, struct value
**, int);
102 static struct value
*make_array_descriptor (struct type
*, struct value
*);
104 static void ada_add_block_symbols (std::vector
<struct block_symbol
> &,
105 const struct block
*,
106 const lookup_name_info
&lookup_name
,
107 domain_enum
, struct objfile
*);
109 static void ada_add_all_symbols (std::vector
<struct block_symbol
> &,
110 const struct block
*,
111 const lookup_name_info
&lookup_name
,
112 domain_enum
, int, int *);
114 static int is_nonfunction (const std::vector
<struct block_symbol
> &);
116 static void add_defn_to_vec (std::vector
<struct block_symbol
> &,
118 const struct block
*);
120 static struct value
*resolve_subexp (expression_up
*, int *, int,
122 innermost_block_tracker
*);
124 static void replace_operator_with_call (expression_up
*, int, int, int,
125 struct symbol
*, const struct block
*);
127 static int possible_user_operator_p (enum exp_opcode
, struct value
**);
129 static const char *ada_decoded_op_name (enum exp_opcode
);
131 static int numeric_type_p (struct type
*);
133 static int integer_type_p (struct type
*);
135 static int scalar_type_p (struct type
*);
137 static int discrete_type_p (struct type
*);
139 static struct type
*ada_lookup_struct_elt_type (struct type
*, const char *,
142 static struct value
*evaluate_subexp_type (struct expression
*, int *);
144 static struct type
*ada_find_parallel_type_with_name (struct type
*,
147 static int is_dynamic_field (struct type
*, int);
149 static struct type
*to_fixed_variant_branch_type (struct type
*,
151 CORE_ADDR
, struct value
*);
153 static struct type
*to_fixed_array_type (struct type
*, struct value
*, int);
155 static struct type
*to_fixed_range_type (struct type
*, struct value
*);
157 static struct type
*to_static_fixed_type (struct type
*);
158 static struct type
*static_unwrap_type (struct type
*type
);
160 static struct value
*unwrap_value (struct value
*);
162 static struct type
*constrained_packed_array_type (struct type
*, long *);
164 static struct type
*decode_constrained_packed_array_type (struct type
*);
166 static long decode_packed_array_bitsize (struct type
*);
168 static struct value
*decode_constrained_packed_array (struct value
*);
170 static int ada_is_unconstrained_packed_array_type (struct type
*);
172 static struct value
*value_subscript_packed (struct value
*, int,
175 static struct value
*coerce_unspec_val_to_type (struct value
*,
178 static int lesseq_defined_than (struct symbol
*, struct symbol
*);
180 static int equiv_types (struct type
*, struct type
*);
182 static int is_name_suffix (const char *);
184 static int advance_wild_match (const char **, const char *, char);
186 static bool wild_match (const char *name
, const char *patn
);
188 static struct value
*ada_coerce_ref (struct value
*);
190 static LONGEST
pos_atr (struct value
*);
192 static struct value
*val_atr (struct type
*, LONGEST
);
194 static struct symbol
*standard_lookup (const char *, const struct block
*,
197 static struct value
*ada_search_struct_field (const char *, struct value
*, int,
200 static int find_struct_field (const char *, struct type
*, int,
201 struct type
**, int *, int *, int *, int *);
203 static int ada_resolve_function (std::vector
<struct block_symbol
> &,
204 struct value
**, int, const char *,
207 static int ada_is_direct_array_type (struct type
*);
209 static struct value
*ada_index_struct_field (int, struct value
*, int,
212 static struct value
*assign_aggregate (struct value
*, struct value
*,
216 static void aggregate_assign_from_choices (struct value
*, struct value
*,
218 int *, std::vector
<LONGEST
> &,
221 static void aggregate_assign_positional (struct value
*, struct value
*,
223 int *, std::vector
<LONGEST
> &,
227 static void aggregate_assign_others (struct value
*, struct value
*,
229 int *, std::vector
<LONGEST
> &,
233 static void add_component_interval (LONGEST
, LONGEST
, std::vector
<LONGEST
> &);
236 static struct value
*ada_evaluate_subexp (struct type
*, struct expression
*,
239 static void ada_forward_operator_length (struct expression
*, int, int *,
242 static struct type
*ada_find_any_type (const char *name
);
244 static symbol_name_matcher_ftype
*ada_get_symbol_name_matcher
245 (const lookup_name_info
&lookup_name
);
249 /* The result of a symbol lookup to be stored in our symbol cache. */
253 /* The name used to perform the lookup. */
255 /* The namespace used during the lookup. */
257 /* The symbol returned by the lookup, or NULL if no matching symbol
260 /* The block where the symbol was found, or NULL if no matching
262 const struct block
*block
;
263 /* A pointer to the next entry with the same hash. */
264 struct cache_entry
*next
;
267 /* The Ada symbol cache, used to store the result of Ada-mode symbol
268 lookups in the course of executing the user's commands.
270 The cache is implemented using a simple, fixed-sized hash.
271 The size is fixed on the grounds that there are not likely to be
272 all that many symbols looked up during any given session, regardless
273 of the size of the symbol table. If we decide to go to a resizable
274 table, let's just use the stuff from libiberty instead. */
276 #define HASH_SIZE 1009
278 struct ada_symbol_cache
280 /* An obstack used to store the entries in our cache. */
281 struct auto_obstack cache_space
;
283 /* The root of the hash table used to implement our symbol cache. */
284 struct cache_entry
*root
[HASH_SIZE
] {};
287 /* Maximum-sized dynamic type. */
288 static unsigned int varsize_limit
;
290 static const char ada_completer_word_break_characters
[] =
292 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
294 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
297 /* The name of the symbol to use to get the name of the main subprogram. */
298 static const char ADA_MAIN_PROGRAM_SYMBOL_NAME
[]
299 = "__gnat_ada_main_program_name";
301 /* Limit on the number of warnings to raise per expression evaluation. */
302 static int warning_limit
= 2;
304 /* Number of warning messages issued; reset to 0 by cleanups after
305 expression evaluation. */
306 static int warnings_issued
= 0;
308 static const char * const known_runtime_file_name_patterns
[] = {
309 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
312 static const char * const known_auxiliary_function_name_patterns
[] = {
313 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
316 /* Maintenance-related settings for this module. */
318 static struct cmd_list_element
*maint_set_ada_cmdlist
;
319 static struct cmd_list_element
*maint_show_ada_cmdlist
;
321 /* The "maintenance ada set/show ignore-descriptive-type" value. */
323 static bool ada_ignore_descriptive_types_p
= false;
325 /* Inferior-specific data. */
327 /* Per-inferior data for this module. */
329 struct ada_inferior_data
331 /* The ada__tags__type_specific_data type, which is used when decoding
332 tagged types. With older versions of GNAT, this type was directly
333 accessible through a component ("tsd") in the object tag. But this
334 is no longer the case, so we cache it for each inferior. */
335 struct type
*tsd_type
= nullptr;
337 /* The exception_support_info data. This data is used to determine
338 how to implement support for Ada exception catchpoints in a given
340 const struct exception_support_info
*exception_info
= nullptr;
343 /* Our key to this module's inferior data. */
344 static const struct inferior_key
<ada_inferior_data
> ada_inferior_data
;
346 /* Return our inferior data for the given inferior (INF).
348 This function always returns a valid pointer to an allocated
349 ada_inferior_data structure. If INF's inferior data has not
350 been previously set, this functions creates a new one with all
351 fields set to zero, sets INF's inferior to it, and then returns
352 a pointer to that newly allocated ada_inferior_data. */
354 static struct ada_inferior_data
*
355 get_ada_inferior_data (struct inferior
*inf
)
357 struct ada_inferior_data
*data
;
359 data
= ada_inferior_data
.get (inf
);
361 data
= ada_inferior_data
.emplace (inf
);
366 /* Perform all necessary cleanups regarding our module's inferior data
367 that is required after the inferior INF just exited. */
370 ada_inferior_exit (struct inferior
*inf
)
372 ada_inferior_data
.clear (inf
);
376 /* program-space-specific data. */
378 /* This module's per-program-space data. */
379 struct ada_pspace_data
381 /* The Ada symbol cache. */
382 std::unique_ptr
<ada_symbol_cache
> sym_cache
;
385 /* Key to our per-program-space data. */
386 static const struct program_space_key
<ada_pspace_data
> ada_pspace_data_handle
;
388 /* Return this module's data for the given program space (PSPACE).
389 If not is found, add a zero'ed one now.
391 This function always returns a valid object. */
393 static struct ada_pspace_data
*
394 get_ada_pspace_data (struct program_space
*pspace
)
396 struct ada_pspace_data
*data
;
398 data
= ada_pspace_data_handle
.get (pspace
);
400 data
= ada_pspace_data_handle
.emplace (pspace
);
407 /* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
408 all typedef layers have been peeled. Otherwise, return TYPE.
410 Normally, we really expect a typedef type to only have 1 typedef layer.
411 In other words, we really expect the target type of a typedef type to be
412 a non-typedef type. This is particularly true for Ada units, because
413 the language does not have a typedef vs not-typedef distinction.
414 In that respect, the Ada compiler has been trying to eliminate as many
415 typedef definitions in the debugging information, since they generally
416 do not bring any extra information (we still use typedef under certain
417 circumstances related mostly to the GNAT encoding).
419 Unfortunately, we have seen situations where the debugging information
420 generated by the compiler leads to such multiple typedef layers. For
421 instance, consider the following example with stabs:
423 .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
424 .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
426 This is an error in the debugging information which causes type
427 pck__float_array___XUP to be defined twice, and the second time,
428 it is defined as a typedef of a typedef.
430 This is on the fringe of legality as far as debugging information is
431 concerned, and certainly unexpected. But it is easy to handle these
432 situations correctly, so we can afford to be lenient in this case. */
435 ada_typedef_target_type (struct type
*type
)
437 while (type
->code () == TYPE_CODE_TYPEDEF
)
438 type
= TYPE_TARGET_TYPE (type
);
442 /* Given DECODED_NAME a string holding a symbol name in its
443 decoded form (ie using the Ada dotted notation), returns
444 its unqualified name. */
447 ada_unqualified_name (const char *decoded_name
)
451 /* If the decoded name starts with '<', it means that the encoded
452 name does not follow standard naming conventions, and thus that
453 it is not your typical Ada symbol name. Trying to unqualify it
454 is therefore pointless and possibly erroneous. */
455 if (decoded_name
[0] == '<')
458 result
= strrchr (decoded_name
, '.');
460 result
++; /* Skip the dot... */
462 result
= decoded_name
;
467 /* Return a string starting with '<', followed by STR, and '>'. */
470 add_angle_brackets (const char *str
)
472 return string_printf ("<%s>", str
);
475 /* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
476 suffix of FIELD_NAME beginning "___". */
479 field_name_match (const char *field_name
, const char *target
)
481 int len
= strlen (target
);
484 (strncmp (field_name
, target
, len
) == 0
485 && (field_name
[len
] == '\0'
486 || (startswith (field_name
+ len
, "___")
487 && strcmp (field_name
+ strlen (field_name
) - 6,
492 /* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
493 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
494 and return its index. This function also handles fields whose name
495 have ___ suffixes because the compiler sometimes alters their name
496 by adding such a suffix to represent fields with certain constraints.
497 If the field could not be found, return a negative number if
498 MAYBE_MISSING is set. Otherwise raise an error. */
501 ada_get_field_index (const struct type
*type
, const char *field_name
,
505 struct type
*struct_type
= check_typedef ((struct type
*) type
);
507 for (fieldno
= 0; fieldno
< struct_type
->num_fields (); fieldno
++)
508 if (field_name_match (TYPE_FIELD_NAME (struct_type
, fieldno
), field_name
))
512 error (_("Unable to find field %s in struct %s. Aborting"),
513 field_name
, struct_type
->name ());
518 /* The length of the prefix of NAME prior to any "___" suffix. */
521 ada_name_prefix_len (const char *name
)
527 const char *p
= strstr (name
, "___");
530 return strlen (name
);
536 /* Return non-zero if SUFFIX is a suffix of STR.
537 Return zero if STR is null. */
540 is_suffix (const char *str
, const char *suffix
)
547 len2
= strlen (suffix
);
548 return (len1
>= len2
&& strcmp (str
+ len1
- len2
, suffix
) == 0);
551 /* The contents of value VAL, treated as a value of type TYPE. The
552 result is an lval in memory if VAL is. */
554 static struct value
*
555 coerce_unspec_val_to_type (struct value
*val
, struct type
*type
)
557 type
= ada_check_typedef (type
);
558 if (value_type (val
) == type
)
562 struct value
*result
;
564 /* Make sure that the object size is not unreasonable before
565 trying to allocate some memory for it. */
566 ada_ensure_varsize_limit (type
);
568 if (value_optimized_out (val
))
569 result
= allocate_optimized_out_value (type
);
570 else if (value_lazy (val
)
571 /* Be careful not to make a lazy not_lval value. */
572 || (VALUE_LVAL (val
) != not_lval
573 && TYPE_LENGTH (type
) > TYPE_LENGTH (value_type (val
))))
574 result
= allocate_value_lazy (type
);
577 result
= allocate_value (type
);
578 value_contents_copy (result
, 0, val
, 0, TYPE_LENGTH (type
));
580 set_value_component_location (result
, val
);
581 set_value_bitsize (result
, value_bitsize (val
));
582 set_value_bitpos (result
, value_bitpos (val
));
583 if (VALUE_LVAL (result
) == lval_memory
)
584 set_value_address (result
, value_address (val
));
589 static const gdb_byte
*
590 cond_offset_host (const gdb_byte
*valaddr
, long offset
)
595 return valaddr
+ offset
;
599 cond_offset_target (CORE_ADDR address
, long offset
)
604 return address
+ offset
;
607 /* Issue a warning (as for the definition of warning in utils.c, but
608 with exactly one argument rather than ...), unless the limit on the
609 number of warnings has passed during the evaluation of the current
612 /* FIXME: cagney/2004-10-10: This function is mimicking the behavior
613 provided by "complaint". */
614 static void lim_warning (const char *format
, ...) ATTRIBUTE_PRINTF (1, 2);
617 lim_warning (const char *format
, ...)
621 va_start (args
, format
);
622 warnings_issued
+= 1;
623 if (warnings_issued
<= warning_limit
)
624 vwarning (format
, args
);
629 /* Issue an error if the size of an object of type T is unreasonable,
630 i.e. if it would be a bad idea to allocate a value of this type in
634 ada_ensure_varsize_limit (const struct type
*type
)
636 if (TYPE_LENGTH (type
) > varsize_limit
)
637 error (_("object size is larger than varsize-limit"));
640 /* Maximum value of a SIZE-byte signed integer type. */
642 max_of_size (int size
)
644 LONGEST top_bit
= (LONGEST
) 1 << (size
* 8 - 2);
646 return top_bit
| (top_bit
- 1);
649 /* Minimum value of a SIZE-byte signed integer type. */
651 min_of_size (int size
)
653 return -max_of_size (size
) - 1;
656 /* Maximum value of a SIZE-byte unsigned integer type. */
658 umax_of_size (int size
)
660 ULONGEST top_bit
= (ULONGEST
) 1 << (size
* 8 - 1);
662 return top_bit
| (top_bit
- 1);
665 /* Maximum value of integral type T, as a signed quantity. */
667 max_of_type (struct type
*t
)
669 if (t
->is_unsigned ())
670 return (LONGEST
) umax_of_size (TYPE_LENGTH (t
));
672 return max_of_size (TYPE_LENGTH (t
));
675 /* Minimum value of integral type T, as a signed quantity. */
677 min_of_type (struct type
*t
)
679 if (t
->is_unsigned ())
682 return min_of_size (TYPE_LENGTH (t
));
685 /* The largest value in the domain of TYPE, a discrete type, as an integer. */
687 ada_discrete_type_high_bound (struct type
*type
)
689 type
= resolve_dynamic_type (type
, {}, 0);
690 switch (type
->code ())
692 case TYPE_CODE_RANGE
:
694 const dynamic_prop
&high
= type
->bounds ()->high
;
696 if (high
.kind () == PROP_CONST
)
697 return high
.const_val ();
700 gdb_assert (high
.kind () == PROP_UNDEFINED
);
702 /* This happens when trying to evaluate a type's dynamic bound
703 without a live target. There is nothing relevant for us to
704 return here, so return 0. */
709 return TYPE_FIELD_ENUMVAL (type
, type
->num_fields () - 1);
714 return max_of_type (type
);
716 error (_("Unexpected type in ada_discrete_type_high_bound."));
720 /* The smallest value in the domain of TYPE, a discrete type, as an integer. */
722 ada_discrete_type_low_bound (struct type
*type
)
724 type
= resolve_dynamic_type (type
, {}, 0);
725 switch (type
->code ())
727 case TYPE_CODE_RANGE
:
729 const dynamic_prop
&low
= type
->bounds ()->low
;
731 if (low
.kind () == PROP_CONST
)
732 return low
.const_val ();
735 gdb_assert (low
.kind () == PROP_UNDEFINED
);
737 /* This happens when trying to evaluate a type's dynamic bound
738 without a live target. There is nothing relevant for us to
739 return here, so return 0. */
744 return TYPE_FIELD_ENUMVAL (type
, 0);
749 return min_of_type (type
);
751 error (_("Unexpected type in ada_discrete_type_low_bound."));
755 /* The identity on non-range types. For range types, the underlying
756 non-range scalar type. */
759 get_base_type (struct type
*type
)
761 while (type
!= NULL
&& type
->code () == TYPE_CODE_RANGE
)
763 if (type
== TYPE_TARGET_TYPE (type
) || TYPE_TARGET_TYPE (type
) == NULL
)
765 type
= TYPE_TARGET_TYPE (type
);
770 /* Return a decoded version of the given VALUE. This means returning
771 a value whose type is obtained by applying all the GNAT-specific
772 encodings, making the resulting type a static but standard description
773 of the initial type. */
776 ada_get_decoded_value (struct value
*value
)
778 struct type
*type
= ada_check_typedef (value_type (value
));
780 if (ada_is_array_descriptor_type (type
)
781 || (ada_is_constrained_packed_array_type (type
)
782 && type
->code () != TYPE_CODE_PTR
))
784 if (type
->code () == TYPE_CODE_TYPEDEF
) /* array access type. */
785 value
= ada_coerce_to_simple_array_ptr (value
);
787 value
= ada_coerce_to_simple_array (value
);
790 value
= ada_to_fixed_value (value
);
795 /* Same as ada_get_decoded_value, but with the given TYPE.
796 Because there is no associated actual value for this type,
797 the resulting type might be a best-effort approximation in
798 the case of dynamic types. */
801 ada_get_decoded_type (struct type
*type
)
803 type
= to_static_fixed_type (type
);
804 if (ada_is_constrained_packed_array_type (type
))
805 type
= ada_coerce_to_simple_array_type (type
);
811 /* Language Selection */
813 /* If the main program is in Ada, return language_ada, otherwise return LANG
814 (the main program is in Ada iif the adainit symbol is found). */
817 ada_update_initial_language (enum language lang
)
819 if (lookup_minimal_symbol ("adainit", NULL
, NULL
).minsym
!= NULL
)
825 /* If the main procedure is written in Ada, then return its name.
826 The result is good until the next call. Return NULL if the main
827 procedure doesn't appear to be in Ada. */
832 struct bound_minimal_symbol msym
;
833 static gdb::unique_xmalloc_ptr
<char> main_program_name
;
835 /* For Ada, the name of the main procedure is stored in a specific
836 string constant, generated by the binder. Look for that symbol,
837 extract its address, and then read that string. If we didn't find
838 that string, then most probably the main procedure is not written
840 msym
= lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME
, NULL
, NULL
);
842 if (msym
.minsym
!= NULL
)
844 CORE_ADDR main_program_name_addr
= BMSYMBOL_VALUE_ADDRESS (msym
);
845 if (main_program_name_addr
== 0)
846 error (_("Invalid address for Ada main program name."));
848 main_program_name
= target_read_string (main_program_name_addr
, 1024);
849 return main_program_name
.get ();
852 /* The main procedure doesn't seem to be in Ada. */
858 /* Table of Ada operators and their GNAT-encoded names. Last entry is pair
861 const struct ada_opname_map ada_opname_table
[] = {
862 {"Oadd", "\"+\"", BINOP_ADD
},
863 {"Osubtract", "\"-\"", BINOP_SUB
},
864 {"Omultiply", "\"*\"", BINOP_MUL
},
865 {"Odivide", "\"/\"", BINOP_DIV
},
866 {"Omod", "\"mod\"", BINOP_MOD
},
867 {"Orem", "\"rem\"", BINOP_REM
},
868 {"Oexpon", "\"**\"", BINOP_EXP
},
869 {"Olt", "\"<\"", BINOP_LESS
},
870 {"Ole", "\"<=\"", BINOP_LEQ
},
871 {"Ogt", "\">\"", BINOP_GTR
},
872 {"Oge", "\">=\"", BINOP_GEQ
},
873 {"Oeq", "\"=\"", BINOP_EQUAL
},
874 {"One", "\"/=\"", BINOP_NOTEQUAL
},
875 {"Oand", "\"and\"", BINOP_BITWISE_AND
},
876 {"Oor", "\"or\"", BINOP_BITWISE_IOR
},
877 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR
},
878 {"Oconcat", "\"&\"", BINOP_CONCAT
},
879 {"Oabs", "\"abs\"", UNOP_ABS
},
880 {"Onot", "\"not\"", UNOP_LOGICAL_NOT
},
881 {"Oadd", "\"+\"", UNOP_PLUS
},
882 {"Osubtract", "\"-\"", UNOP_NEG
},
886 /* The "encoded" form of DECODED, according to GNAT conventions. If
887 THROW_ERRORS, throw an error if invalid operator name is found.
888 Otherwise, return the empty string in that case. */
891 ada_encode_1 (const char *decoded
, bool throw_errors
)
896 std::string encoding_buffer
;
897 for (const char *p
= decoded
; *p
!= '\0'; p
+= 1)
900 encoding_buffer
.append ("__");
903 const struct ada_opname_map
*mapping
;
905 for (mapping
= ada_opname_table
;
906 mapping
->encoded
!= NULL
907 && !startswith (p
, mapping
->decoded
); mapping
+= 1)
909 if (mapping
->encoded
== NULL
)
912 error (_("invalid Ada operator name: %s"), p
);
916 encoding_buffer
.append (mapping
->encoded
);
920 encoding_buffer
.push_back (*p
);
923 return encoding_buffer
;
926 /* The "encoded" form of DECODED, according to GNAT conventions. */
929 ada_encode (const char *decoded
)
931 return ada_encode_1 (decoded
, true);
934 /* Return NAME folded to lower case, or, if surrounded by single
935 quotes, unfolded, but with the quotes stripped away. Result good
939 ada_fold_name (gdb::string_view name
)
941 static std::string fold_storage
;
943 if (!name
.empty () && name
[0] == '\'')
944 fold_storage
= gdb::to_string (name
.substr (1, name
.size () - 2));
947 fold_storage
= gdb::to_string (name
);
948 for (int i
= 0; i
< name
.size (); i
+= 1)
949 fold_storage
[i
] = tolower (fold_storage
[i
]);
952 return fold_storage
.c_str ();
955 /* Return nonzero if C is either a digit or a lowercase alphabet character. */
958 is_lower_alphanum (const char c
)
960 return (isdigit (c
) || (isalpha (c
) && islower (c
)));
963 /* ENCODED is the linkage name of a symbol and LEN contains its length.
964 This function saves in LEN the length of that same symbol name but
965 without either of these suffixes:
971 These are suffixes introduced by the compiler for entities such as
972 nested subprogram for instance, in order to avoid name clashes.
973 They do not serve any purpose for the debugger. */
976 ada_remove_trailing_digits (const char *encoded
, int *len
)
978 if (*len
> 1 && isdigit (encoded
[*len
- 1]))
982 while (i
> 0 && isdigit (encoded
[i
]))
984 if (i
>= 0 && encoded
[i
] == '.')
986 else if (i
>= 0 && encoded
[i
] == '$')
988 else if (i
>= 2 && startswith (encoded
+ i
- 2, "___"))
990 else if (i
>= 1 && startswith (encoded
+ i
- 1, "__"))
995 /* Remove the suffix introduced by the compiler for protected object
999 ada_remove_po_subprogram_suffix (const char *encoded
, int *len
)
1001 /* Remove trailing N. */
1003 /* Protected entry subprograms are broken into two
1004 separate subprograms: The first one is unprotected, and has
1005 a 'N' suffix; the second is the protected version, and has
1006 the 'P' suffix. The second calls the first one after handling
1007 the protection. Since the P subprograms are internally generated,
1008 we leave these names undecoded, giving the user a clue that this
1009 entity is internal. */
1012 && encoded
[*len
- 1] == 'N'
1013 && (isdigit (encoded
[*len
- 2]) || islower (encoded
[*len
- 2])))
1017 /* If ENCODED follows the GNAT entity encoding conventions, then return
1018 the decoded form of ENCODED. Otherwise, return "<%s>" where "%s" is
1019 replaced by ENCODED. */
1022 ada_decode (const char *encoded
)
1028 std::string decoded
;
1030 /* With function descriptors on PPC64, the value of a symbol named
1031 ".FN", if it exists, is the entry point of the function "FN". */
1032 if (encoded
[0] == '.')
1035 /* The name of the Ada main procedure starts with "_ada_".
1036 This prefix is not part of the decoded name, so skip this part
1037 if we see this prefix. */
1038 if (startswith (encoded
, "_ada_"))
1041 /* If the name starts with '_', then it is not a properly encoded
1042 name, so do not attempt to decode it. Similarly, if the name
1043 starts with '<', the name should not be decoded. */
1044 if (encoded
[0] == '_' || encoded
[0] == '<')
1047 len0
= strlen (encoded
);
1049 ada_remove_trailing_digits (encoded
, &len0
);
1050 ada_remove_po_subprogram_suffix (encoded
, &len0
);
1052 /* Remove the ___X.* suffix if present. Do not forget to verify that
1053 the suffix is located before the current "end" of ENCODED. We want
1054 to avoid re-matching parts of ENCODED that have previously been
1055 marked as discarded (by decrementing LEN0). */
1056 p
= strstr (encoded
, "___");
1057 if (p
!= NULL
&& p
- encoded
< len0
- 3)
1065 /* Remove any trailing TKB suffix. It tells us that this symbol
1066 is for the body of a task, but that information does not actually
1067 appear in the decoded name. */
1069 if (len0
> 3 && startswith (encoded
+ len0
- 3, "TKB"))
1072 /* Remove any trailing TB suffix. The TB suffix is slightly different
1073 from the TKB suffix because it is used for non-anonymous task
1076 if (len0
> 2 && startswith (encoded
+ len0
- 2, "TB"))
1079 /* Remove trailing "B" suffixes. */
1080 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1082 if (len0
> 1 && startswith (encoded
+ len0
- 1, "B"))
1085 /* Make decoded big enough for possible expansion by operator name. */
1087 decoded
.resize (2 * len0
+ 1, 'X');
1089 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1091 if (len0
> 1 && isdigit (encoded
[len0
- 1]))
1094 while ((i
>= 0 && isdigit (encoded
[i
]))
1095 || (i
>= 1 && encoded
[i
] == '_' && isdigit (encoded
[i
- 1])))
1097 if (i
> 1 && encoded
[i
] == '_' && encoded
[i
- 1] == '_')
1099 else if (encoded
[i
] == '$')
1103 /* The first few characters that are not alphabetic are not part
1104 of any encoding we use, so we can copy them over verbatim. */
1106 for (i
= 0, j
= 0; i
< len0
&& !isalpha (encoded
[i
]); i
+= 1, j
+= 1)
1107 decoded
[j
] = encoded
[i
];
1112 /* Is this a symbol function? */
1113 if (at_start_name
&& encoded
[i
] == 'O')
1117 for (k
= 0; ada_opname_table
[k
].encoded
!= NULL
; k
+= 1)
1119 int op_len
= strlen (ada_opname_table
[k
].encoded
);
1120 if ((strncmp (ada_opname_table
[k
].encoded
+ 1, encoded
+ i
+ 1,
1122 && !isalnum (encoded
[i
+ op_len
]))
1124 strcpy (&decoded
.front() + j
, ada_opname_table
[k
].decoded
);
1127 j
+= strlen (ada_opname_table
[k
].decoded
);
1131 if (ada_opname_table
[k
].encoded
!= NULL
)
1136 /* Replace "TK__" with "__", which will eventually be translated
1137 into "." (just below). */
1139 if (i
< len0
- 4 && startswith (encoded
+ i
, "TK__"))
1142 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
1143 be translated into "." (just below). These are internal names
1144 generated for anonymous blocks inside which our symbol is nested. */
1146 if (len0
- i
> 5 && encoded
[i
] == '_' && encoded
[i
+1] == '_'
1147 && encoded
[i
+2] == 'B' && encoded
[i
+3] == '_'
1148 && isdigit (encoded
[i
+4]))
1152 while (k
< len0
&& isdigit (encoded
[k
]))
1153 k
++; /* Skip any extra digit. */
1155 /* Double-check that the "__B_{DIGITS}+" sequence we found
1156 is indeed followed by "__". */
1157 if (len0
- k
> 2 && encoded
[k
] == '_' && encoded
[k
+1] == '_')
1161 /* Remove _E{DIGITS}+[sb] */
1163 /* Just as for protected object subprograms, there are 2 categories
1164 of subprograms created by the compiler for each entry. The first
1165 one implements the actual entry code, and has a suffix following
1166 the convention above; the second one implements the barrier and
1167 uses the same convention as above, except that the 'E' is replaced
1170 Just as above, we do not decode the name of barrier functions
1171 to give the user a clue that the code he is debugging has been
1172 internally generated. */
1174 if (len0
- i
> 3 && encoded
[i
] == '_' && encoded
[i
+1] == 'E'
1175 && isdigit (encoded
[i
+2]))
1179 while (k
< len0
&& isdigit (encoded
[k
]))
1183 && (encoded
[k
] == 'b' || encoded
[k
] == 's'))
1186 /* Just as an extra precaution, make sure that if this
1187 suffix is followed by anything else, it is a '_'.
1188 Otherwise, we matched this sequence by accident. */
1190 || (k
< len0
&& encoded
[k
] == '_'))
1195 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
1196 the GNAT front-end in protected object subprograms. */
1199 && encoded
[i
] == 'N' && encoded
[i
+1] == '_' && encoded
[i
+2] == '_')
1201 /* Backtrack a bit up until we reach either the begining of
1202 the encoded name, or "__". Make sure that we only find
1203 digits or lowercase characters. */
1204 const char *ptr
= encoded
+ i
- 1;
1206 while (ptr
>= encoded
&& is_lower_alphanum (ptr
[0]))
1209 || (ptr
> encoded
&& ptr
[0] == '_' && ptr
[-1] == '_'))
1213 if (encoded
[i
] == 'X' && i
!= 0 && isalnum (encoded
[i
- 1]))
1215 /* This is a X[bn]* sequence not separated from the previous
1216 part of the name with a non-alpha-numeric character (in other
1217 words, immediately following an alpha-numeric character), then
1218 verify that it is placed at the end of the encoded name. If
1219 not, then the encoding is not valid and we should abort the
1220 decoding. Otherwise, just skip it, it is used in body-nested
1224 while (i
< len0
&& (encoded
[i
] == 'b' || encoded
[i
] == 'n'));
1228 else if (i
< len0
- 2 && encoded
[i
] == '_' && encoded
[i
+ 1] == '_')
1230 /* Replace '__' by '.'. */
1238 /* It's a character part of the decoded name, so just copy it
1240 decoded
[j
] = encoded
[i
];
1247 /* Decoded names should never contain any uppercase character.
1248 Double-check this, and abort the decoding if we find one. */
1250 for (i
= 0; i
< decoded
.length(); ++i
)
1251 if (isupper (decoded
[i
]) || decoded
[i
] == ' ')
1257 if (encoded
[0] == '<')
1260 decoded
= '<' + std::string(encoded
) + '>';
1265 /* Table for keeping permanent unique copies of decoded names. Once
1266 allocated, names in this table are never released. While this is a
1267 storage leak, it should not be significant unless there are massive
1268 changes in the set of decoded names in successive versions of a
1269 symbol table loaded during a single session. */
1270 static struct htab
*decoded_names_store
;
1272 /* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1273 in the language-specific part of GSYMBOL, if it has not been
1274 previously computed. Tries to save the decoded name in the same
1275 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1276 in any case, the decoded symbol has a lifetime at least that of
1278 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1279 const, but nevertheless modified to a semantically equivalent form
1280 when a decoded name is cached in it. */
1283 ada_decode_symbol (const struct general_symbol_info
*arg
)
1285 struct general_symbol_info
*gsymbol
= (struct general_symbol_info
*) arg
;
1286 const char **resultp
=
1287 &gsymbol
->language_specific
.demangled_name
;
1289 if (!gsymbol
->ada_mangled
)
1291 std::string decoded
= ada_decode (gsymbol
->linkage_name ());
1292 struct obstack
*obstack
= gsymbol
->language_specific
.obstack
;
1294 gsymbol
->ada_mangled
= 1;
1296 if (obstack
!= NULL
)
1297 *resultp
= obstack_strdup (obstack
, decoded
.c_str ());
1300 /* Sometimes, we can't find a corresponding objfile, in
1301 which case, we put the result on the heap. Since we only
1302 decode when needed, we hope this usually does not cause a
1303 significant memory leak (FIXME). */
1305 char **slot
= (char **) htab_find_slot (decoded_names_store
,
1306 decoded
.c_str (), INSERT
);
1309 *slot
= xstrdup (decoded
.c_str ());
1318 ada_la_decode (const char *encoded
, int options
)
1320 return xstrdup (ada_decode (encoded
).c_str ());
1327 /* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1328 generated by the GNAT compiler to describe the index type used
1329 for each dimension of an array, check whether it follows the latest
1330 known encoding. If not, fix it up to conform to the latest encoding.
1331 Otherwise, do nothing. This function also does nothing if
1332 INDEX_DESC_TYPE is NULL.
1334 The GNAT encoding used to describe the array index type evolved a bit.
1335 Initially, the information would be provided through the name of each
1336 field of the structure type only, while the type of these fields was
1337 described as unspecified and irrelevant. The debugger was then expected
1338 to perform a global type lookup using the name of that field in order
1339 to get access to the full index type description. Because these global
1340 lookups can be very expensive, the encoding was later enhanced to make
1341 the global lookup unnecessary by defining the field type as being
1342 the full index type description.
1344 The purpose of this routine is to allow us to support older versions
1345 of the compiler by detecting the use of the older encoding, and by
1346 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1347 we essentially replace each field's meaningless type by the associated
1351 ada_fixup_array_indexes_type (struct type
*index_desc_type
)
1355 if (index_desc_type
== NULL
)
1357 gdb_assert (index_desc_type
->num_fields () > 0);
1359 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1360 to check one field only, no need to check them all). If not, return
1363 If our INDEX_DESC_TYPE was generated using the older encoding,
1364 the field type should be a meaningless integer type whose name
1365 is not equal to the field name. */
1366 if (index_desc_type
->field (0).type ()->name () != NULL
1367 && strcmp (index_desc_type
->field (0).type ()->name (),
1368 TYPE_FIELD_NAME (index_desc_type
, 0)) == 0)
1371 /* Fixup each field of INDEX_DESC_TYPE. */
1372 for (i
= 0; i
< index_desc_type
->num_fields (); i
++)
1374 const char *name
= TYPE_FIELD_NAME (index_desc_type
, i
);
1375 struct type
*raw_type
= ada_check_typedef (ada_find_any_type (name
));
1378 index_desc_type
->field (i
).set_type (raw_type
);
1382 /* The desc_* routines return primitive portions of array descriptors
1385 /* The descriptor or array type, if any, indicated by TYPE; removes
1386 level of indirection, if needed. */
1388 static struct type
*
1389 desc_base_type (struct type
*type
)
1393 type
= ada_check_typedef (type
);
1394 if (type
->code () == TYPE_CODE_TYPEDEF
)
1395 type
= ada_typedef_target_type (type
);
1398 && (type
->code () == TYPE_CODE_PTR
1399 || type
->code () == TYPE_CODE_REF
))
1400 return ada_check_typedef (TYPE_TARGET_TYPE (type
));
1405 /* True iff TYPE indicates a "thin" array pointer type. */
1408 is_thin_pntr (struct type
*type
)
1411 is_suffix (ada_type_name (desc_base_type (type
)), "___XUT")
1412 || is_suffix (ada_type_name (desc_base_type (type
)), "___XUT___XVE");
1415 /* The descriptor type for thin pointer type TYPE. */
1417 static struct type
*
1418 thin_descriptor_type (struct type
*type
)
1420 struct type
*base_type
= desc_base_type (type
);
1422 if (base_type
== NULL
)
1424 if (is_suffix (ada_type_name (base_type
), "___XVE"))
1428 struct type
*alt_type
= ada_find_parallel_type (base_type
, "___XVE");
1430 if (alt_type
== NULL
)
1437 /* A pointer to the array data for thin-pointer value VAL. */
1439 static struct value
*
1440 thin_data_pntr (struct value
*val
)
1442 struct type
*type
= ada_check_typedef (value_type (val
));
1443 struct type
*data_type
= desc_data_target_type (thin_descriptor_type (type
));
1445 data_type
= lookup_pointer_type (data_type
);
1447 if (type
->code () == TYPE_CODE_PTR
)
1448 return value_cast (data_type
, value_copy (val
));
1450 return value_from_longest (data_type
, value_address (val
));
1453 /* True iff TYPE indicates a "thick" array pointer type. */
1456 is_thick_pntr (struct type
*type
)
1458 type
= desc_base_type (type
);
1459 return (type
!= NULL
&& type
->code () == TYPE_CODE_STRUCT
1460 && lookup_struct_elt_type (type
, "P_BOUNDS", 1) != NULL
);
1463 /* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1464 pointer to one, the type of its bounds data; otherwise, NULL. */
1466 static struct type
*
1467 desc_bounds_type (struct type
*type
)
1471 type
= desc_base_type (type
);
1475 else if (is_thin_pntr (type
))
1477 type
= thin_descriptor_type (type
);
1480 r
= lookup_struct_elt_type (type
, "BOUNDS", 1);
1482 return ada_check_typedef (r
);
1484 else if (type
->code () == TYPE_CODE_STRUCT
)
1486 r
= lookup_struct_elt_type (type
, "P_BOUNDS", 1);
1488 return ada_check_typedef (TYPE_TARGET_TYPE (ada_check_typedef (r
)));
1493 /* If ARR is an array descriptor (fat or thin pointer), or pointer to
1494 one, a pointer to its bounds data. Otherwise NULL. */
1496 static struct value
*
1497 desc_bounds (struct value
*arr
)
1499 struct type
*type
= ada_check_typedef (value_type (arr
));
1501 if (is_thin_pntr (type
))
1503 struct type
*bounds_type
=
1504 desc_bounds_type (thin_descriptor_type (type
));
1507 if (bounds_type
== NULL
)
1508 error (_("Bad GNAT array descriptor"));
1510 /* NOTE: The following calculation is not really kosher, but
1511 since desc_type is an XVE-encoded type (and shouldn't be),
1512 the correct calculation is a real pain. FIXME (and fix GCC). */
1513 if (type
->code () == TYPE_CODE_PTR
)
1514 addr
= value_as_long (arr
);
1516 addr
= value_address (arr
);
1519 value_from_longest (lookup_pointer_type (bounds_type
),
1520 addr
- TYPE_LENGTH (bounds_type
));
1523 else if (is_thick_pntr (type
))
1525 struct value
*p_bounds
= value_struct_elt (&arr
, NULL
, "P_BOUNDS", NULL
,
1526 _("Bad GNAT array descriptor"));
1527 struct type
*p_bounds_type
= value_type (p_bounds
);
1530 && p_bounds_type
->code () == TYPE_CODE_PTR
)
1532 struct type
*target_type
= TYPE_TARGET_TYPE (p_bounds_type
);
1534 if (target_type
->is_stub ())
1535 p_bounds
= value_cast (lookup_pointer_type
1536 (ada_check_typedef (target_type
)),
1540 error (_("Bad GNAT array descriptor"));
1548 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1549 position of the field containing the address of the bounds data. */
1552 fat_pntr_bounds_bitpos (struct type
*type
)
1554 return TYPE_FIELD_BITPOS (desc_base_type (type
), 1);
1557 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1558 size of the field containing the address of the bounds data. */
1561 fat_pntr_bounds_bitsize (struct type
*type
)
1563 type
= desc_base_type (type
);
1565 if (TYPE_FIELD_BITSIZE (type
, 1) > 0)
1566 return TYPE_FIELD_BITSIZE (type
, 1);
1568 return 8 * TYPE_LENGTH (ada_check_typedef (type
->field (1).type ()));
1571 /* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1572 pointer to one, the type of its array data (a array-with-no-bounds type);
1573 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1576 static struct type
*
1577 desc_data_target_type (struct type
*type
)
1579 type
= desc_base_type (type
);
1581 /* NOTE: The following is bogus; see comment in desc_bounds. */
1582 if (is_thin_pntr (type
))
1583 return desc_base_type (thin_descriptor_type (type
)->field (1).type ());
1584 else if (is_thick_pntr (type
))
1586 struct type
*data_type
= lookup_struct_elt_type (type
, "P_ARRAY", 1);
1589 && ada_check_typedef (data_type
)->code () == TYPE_CODE_PTR
)
1590 return ada_check_typedef (TYPE_TARGET_TYPE (data_type
));
1596 /* If ARR is an array descriptor (fat or thin pointer), a pointer to
1599 static struct value
*
1600 desc_data (struct value
*arr
)
1602 struct type
*type
= value_type (arr
);
1604 if (is_thin_pntr (type
))
1605 return thin_data_pntr (arr
);
1606 else if (is_thick_pntr (type
))
1607 return value_struct_elt (&arr
, NULL
, "P_ARRAY", NULL
,
1608 _("Bad GNAT array descriptor"));
1614 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1615 position of the field containing the address of the data. */
1618 fat_pntr_data_bitpos (struct type
*type
)
1620 return TYPE_FIELD_BITPOS (desc_base_type (type
), 0);
1623 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1624 size of the field containing the address of the data. */
1627 fat_pntr_data_bitsize (struct type
*type
)
1629 type
= desc_base_type (type
);
1631 if (TYPE_FIELD_BITSIZE (type
, 0) > 0)
1632 return TYPE_FIELD_BITSIZE (type
, 0);
1634 return TARGET_CHAR_BIT
* TYPE_LENGTH (type
->field (0).type ());
1637 /* If BOUNDS is an array-bounds structure (or pointer to one), return
1638 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
1639 bound, if WHICH is 1. The first bound is I=1. */
1641 static struct value
*
1642 desc_one_bound (struct value
*bounds
, int i
, int which
)
1644 char bound_name
[20];
1645 xsnprintf (bound_name
, sizeof (bound_name
), "%cB%d",
1646 which
? 'U' : 'L', i
- 1);
1647 return value_struct_elt (&bounds
, NULL
, bound_name
, NULL
,
1648 _("Bad GNAT array descriptor bounds"));
1651 /* If BOUNDS is an array-bounds structure type, return the bit position
1652 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
1653 bound, if WHICH is 1. The first bound is I=1. */
1656 desc_bound_bitpos (struct type
*type
, int i
, int which
)
1658 return TYPE_FIELD_BITPOS (desc_base_type (type
), 2 * i
+ which
- 2);
1661 /* If BOUNDS is an array-bounds structure type, return the bit field size
1662 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
1663 bound, if WHICH is 1. The first bound is I=1. */
1666 desc_bound_bitsize (struct type
*type
, int i
, int which
)
1668 type
= desc_base_type (type
);
1670 if (TYPE_FIELD_BITSIZE (type
, 2 * i
+ which
- 2) > 0)
1671 return TYPE_FIELD_BITSIZE (type
, 2 * i
+ which
- 2);
1673 return 8 * TYPE_LENGTH (type
->field (2 * i
+ which
- 2).type ());
1676 /* If TYPE is the type of an array-bounds structure, the type of its
1677 Ith bound (numbering from 1). Otherwise, NULL. */
1679 static struct type
*
1680 desc_index_type (struct type
*type
, int i
)
1682 type
= desc_base_type (type
);
1684 if (type
->code () == TYPE_CODE_STRUCT
)
1686 char bound_name
[20];
1687 xsnprintf (bound_name
, sizeof (bound_name
), "LB%d", i
- 1);
1688 return lookup_struct_elt_type (type
, bound_name
, 1);
1694 /* The number of index positions in the array-bounds type TYPE.
1695 Return 0 if TYPE is NULL. */
1698 desc_arity (struct type
*type
)
1700 type
= desc_base_type (type
);
1703 return type
->num_fields () / 2;
1707 /* Non-zero iff TYPE is a simple array type (not a pointer to one) or
1708 an array descriptor type (representing an unconstrained array
1712 ada_is_direct_array_type (struct type
*type
)
1716 type
= ada_check_typedef (type
);
1717 return (type
->code () == TYPE_CODE_ARRAY
1718 || ada_is_array_descriptor_type (type
));
1721 /* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
1725 ada_is_array_type (struct type
*type
)
1728 && (type
->code () == TYPE_CODE_PTR
1729 || type
->code () == TYPE_CODE_REF
))
1730 type
= TYPE_TARGET_TYPE (type
);
1731 return ada_is_direct_array_type (type
);
1734 /* Non-zero iff TYPE is a simple array type or pointer to one. */
1737 ada_is_simple_array_type (struct type
*type
)
1741 type
= ada_check_typedef (type
);
1742 return (type
->code () == TYPE_CODE_ARRAY
1743 || (type
->code () == TYPE_CODE_PTR
1744 && (ada_check_typedef (TYPE_TARGET_TYPE (type
))->code ()
1745 == TYPE_CODE_ARRAY
)));
1748 /* Non-zero iff TYPE belongs to a GNAT array descriptor. */
1751 ada_is_array_descriptor_type (struct type
*type
)
1753 struct type
*data_type
= desc_data_target_type (type
);
1757 type
= ada_check_typedef (type
);
1758 return (data_type
!= NULL
1759 && data_type
->code () == TYPE_CODE_ARRAY
1760 && desc_arity (desc_bounds_type (type
)) > 0);
1763 /* Non-zero iff type is a partially mal-formed GNAT array
1764 descriptor. FIXME: This is to compensate for some problems with
1765 debugging output from GNAT. Re-examine periodically to see if it
1769 ada_is_bogus_array_descriptor (struct type
*type
)
1773 && type
->code () == TYPE_CODE_STRUCT
1774 && (lookup_struct_elt_type (type
, "P_BOUNDS", 1) != NULL
1775 || lookup_struct_elt_type (type
, "P_ARRAY", 1) != NULL
)
1776 && !ada_is_array_descriptor_type (type
);
1780 /* If ARR has a record type in the form of a standard GNAT array descriptor,
1781 (fat pointer) returns the type of the array data described---specifically,
1782 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
1783 in from the descriptor; otherwise, they are left unspecified. If
1784 the ARR denotes a null array descriptor and BOUNDS is non-zero,
1785 returns NULL. The result is simply the type of ARR if ARR is not
1788 static struct type
*
1789 ada_type_of_array (struct value
*arr
, int bounds
)
1791 if (ada_is_constrained_packed_array_type (value_type (arr
)))
1792 return decode_constrained_packed_array_type (value_type (arr
));
1794 if (!ada_is_array_descriptor_type (value_type (arr
)))
1795 return value_type (arr
);
1799 struct type
*array_type
=
1800 ada_check_typedef (desc_data_target_type (value_type (arr
)));
1802 if (ada_is_unconstrained_packed_array_type (value_type (arr
)))
1803 TYPE_FIELD_BITSIZE (array_type
, 0) =
1804 decode_packed_array_bitsize (value_type (arr
));
1810 struct type
*elt_type
;
1812 struct value
*descriptor
;
1814 elt_type
= ada_array_element_type (value_type (arr
), -1);
1815 arity
= ada_array_arity (value_type (arr
));
1817 if (elt_type
== NULL
|| arity
== 0)
1818 return ada_check_typedef (value_type (arr
));
1820 descriptor
= desc_bounds (arr
);
1821 if (value_as_long (descriptor
) == 0)
1825 struct type
*range_type
= alloc_type_copy (value_type (arr
));
1826 struct type
*array_type
= alloc_type_copy (value_type (arr
));
1827 struct value
*low
= desc_one_bound (descriptor
, arity
, 0);
1828 struct value
*high
= desc_one_bound (descriptor
, arity
, 1);
1831 create_static_range_type (range_type
, value_type (low
),
1832 longest_to_int (value_as_long (low
)),
1833 longest_to_int (value_as_long (high
)));
1834 elt_type
= create_array_type (array_type
, elt_type
, range_type
);
1836 if (ada_is_unconstrained_packed_array_type (value_type (arr
)))
1838 /* We need to store the element packed bitsize, as well as
1839 recompute the array size, because it was previously
1840 computed based on the unpacked element size. */
1841 LONGEST lo
= value_as_long (low
);
1842 LONGEST hi
= value_as_long (high
);
1844 TYPE_FIELD_BITSIZE (elt_type
, 0) =
1845 decode_packed_array_bitsize (value_type (arr
));
1846 /* If the array has no element, then the size is already
1847 zero, and does not need to be recomputed. */
1851 (hi
- lo
+ 1) * TYPE_FIELD_BITSIZE (elt_type
, 0);
1853 TYPE_LENGTH (array_type
) = (array_bitsize
+ 7) / 8;
1858 return lookup_pointer_type (elt_type
);
1862 /* If ARR does not represent an array, returns ARR unchanged.
1863 Otherwise, returns either a standard GDB array with bounds set
1864 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
1865 GDB array. Returns NULL if ARR is a null fat pointer. */
1868 ada_coerce_to_simple_array_ptr (struct value
*arr
)
1870 if (ada_is_array_descriptor_type (value_type (arr
)))
1872 struct type
*arrType
= ada_type_of_array (arr
, 1);
1874 if (arrType
== NULL
)
1876 return value_cast (arrType
, value_copy (desc_data (arr
)));
1878 else if (ada_is_constrained_packed_array_type (value_type (arr
)))
1879 return decode_constrained_packed_array (arr
);
1884 /* If ARR does not represent an array, returns ARR unchanged.
1885 Otherwise, returns a standard GDB array describing ARR (which may
1886 be ARR itself if it already is in the proper form). */
1889 ada_coerce_to_simple_array (struct value
*arr
)
1891 if (ada_is_array_descriptor_type (value_type (arr
)))
1893 struct value
*arrVal
= ada_coerce_to_simple_array_ptr (arr
);
1896 error (_("Bounds unavailable for null array pointer."));
1897 ada_ensure_varsize_limit (TYPE_TARGET_TYPE (value_type (arrVal
)));
1898 return value_ind (arrVal
);
1900 else if (ada_is_constrained_packed_array_type (value_type (arr
)))
1901 return decode_constrained_packed_array (arr
);
1906 /* If TYPE represents a GNAT array type, return it translated to an
1907 ordinary GDB array type (possibly with BITSIZE fields indicating
1908 packing). For other types, is the identity. */
1911 ada_coerce_to_simple_array_type (struct type
*type
)
1913 if (ada_is_constrained_packed_array_type (type
))
1914 return decode_constrained_packed_array_type (type
);
1916 if (ada_is_array_descriptor_type (type
))
1917 return ada_check_typedef (desc_data_target_type (type
));
1922 /* Non-zero iff TYPE represents a standard GNAT packed-array type. */
1925 ada_is_gnat_encoded_packed_array_type (struct type
*type
)
1929 type
= desc_base_type (type
);
1930 type
= ada_check_typedef (type
);
1932 ada_type_name (type
) != NULL
1933 && strstr (ada_type_name (type
), "___XP") != NULL
;
1936 /* Non-zero iff TYPE represents a standard GNAT constrained
1937 packed-array type. */
1940 ada_is_constrained_packed_array_type (struct type
*type
)
1942 return ada_is_gnat_encoded_packed_array_type (type
)
1943 && !ada_is_array_descriptor_type (type
);
1946 /* Non-zero iff TYPE represents an array descriptor for a
1947 unconstrained packed-array type. */
1950 ada_is_unconstrained_packed_array_type (struct type
*type
)
1952 if (!ada_is_array_descriptor_type (type
))
1955 if (ada_is_gnat_encoded_packed_array_type (type
))
1958 /* If we saw GNAT encodings, then the above code is sufficient.
1959 However, with minimal encodings, we will just have a thick
1961 if (is_thick_pntr (type
))
1963 type
= desc_base_type (type
);
1964 /* The structure's first field is a pointer to an array, so this
1965 fetches the array type. */
1966 type
= TYPE_TARGET_TYPE (type
->field (0).type ());
1967 /* Now we can see if the array elements are packed. */
1968 return TYPE_FIELD_BITSIZE (type
, 0) > 0;
1974 /* Return true if TYPE is a (Gnat-encoded) constrained packed array
1975 type, or if it is an ordinary (non-Gnat-encoded) packed array. */
1978 ada_is_any_packed_array_type (struct type
*type
)
1980 return (ada_is_constrained_packed_array_type (type
)
1981 || (type
->code () == TYPE_CODE_ARRAY
1982 && TYPE_FIELD_BITSIZE (type
, 0) % 8 != 0));
1985 /* Given that TYPE encodes a packed array type (constrained or unconstrained),
1986 return the size of its elements in bits. */
1989 decode_packed_array_bitsize (struct type
*type
)
1991 const char *raw_name
;
1995 /* Access to arrays implemented as fat pointers are encoded as a typedef
1996 of the fat pointer type. We need the name of the fat pointer type
1997 to do the decoding, so strip the typedef layer. */
1998 if (type
->code () == TYPE_CODE_TYPEDEF
)
1999 type
= ada_typedef_target_type (type
);
2001 raw_name
= ada_type_name (ada_check_typedef (type
));
2003 raw_name
= ada_type_name (desc_base_type (type
));
2008 tail
= strstr (raw_name
, "___XP");
2009 if (tail
== nullptr)
2011 gdb_assert (is_thick_pntr (type
));
2012 /* The structure's first field is a pointer to an array, so this
2013 fetches the array type. */
2014 type
= TYPE_TARGET_TYPE (type
->field (0).type ());
2015 /* Now we can see if the array elements are packed. */
2016 return TYPE_FIELD_BITSIZE (type
, 0);
2019 if (sscanf (tail
+ sizeof ("___XP") - 1, "%ld", &bits
) != 1)
2022 (_("could not understand bit size information on packed array"));
2029 /* Given that TYPE is a standard GDB array type with all bounds filled
2030 in, and that the element size of its ultimate scalar constituents
2031 (that is, either its elements, or, if it is an array of arrays, its
2032 elements' elements, etc.) is *ELT_BITS, return an identical type,
2033 but with the bit sizes of its elements (and those of any
2034 constituent arrays) recorded in the BITSIZE components of its
2035 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
2038 Note that, for arrays whose index type has an XA encoding where
2039 a bound references a record discriminant, getting that discriminant,
2040 and therefore the actual value of that bound, is not possible
2041 because none of the given parameters gives us access to the record.
2042 This function assumes that it is OK in the context where it is being
2043 used to return an array whose bounds are still dynamic and where
2044 the length is arbitrary. */
2046 static struct type
*
2047 constrained_packed_array_type (struct type
*type
, long *elt_bits
)
2049 struct type
*new_elt_type
;
2050 struct type
*new_type
;
2051 struct type
*index_type_desc
;
2052 struct type
*index_type
;
2053 LONGEST low_bound
, high_bound
;
2055 type
= ada_check_typedef (type
);
2056 if (type
->code () != TYPE_CODE_ARRAY
)
2059 index_type_desc
= ada_find_parallel_type (type
, "___XA");
2060 if (index_type_desc
)
2061 index_type
= to_fixed_range_type (index_type_desc
->field (0).type (),
2064 index_type
= type
->index_type ();
2066 new_type
= alloc_type_copy (type
);
2068 constrained_packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type
)),
2070 create_array_type (new_type
, new_elt_type
, index_type
);
2071 TYPE_FIELD_BITSIZE (new_type
, 0) = *elt_bits
;
2072 new_type
->set_name (ada_type_name (type
));
2074 if ((check_typedef (index_type
)->code () == TYPE_CODE_RANGE
2075 && is_dynamic_type (check_typedef (index_type
)))
2076 || !get_discrete_bounds (index_type
, &low_bound
, &high_bound
))
2077 low_bound
= high_bound
= 0;
2078 if (high_bound
< low_bound
)
2079 *elt_bits
= TYPE_LENGTH (new_type
) = 0;
2082 *elt_bits
*= (high_bound
- low_bound
+ 1);
2083 TYPE_LENGTH (new_type
) =
2084 (*elt_bits
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
2087 new_type
->set_is_fixed_instance (true);
2091 /* The array type encoded by TYPE, where
2092 ada_is_constrained_packed_array_type (TYPE). */
2094 static struct type
*
2095 decode_constrained_packed_array_type (struct type
*type
)
2097 const char *raw_name
= ada_type_name (ada_check_typedef (type
));
2100 struct type
*shadow_type
;
2104 raw_name
= ada_type_name (desc_base_type (type
));
2109 name
= (char *) alloca (strlen (raw_name
) + 1);
2110 tail
= strstr (raw_name
, "___XP");
2111 type
= desc_base_type (type
);
2113 memcpy (name
, raw_name
, tail
- raw_name
);
2114 name
[tail
- raw_name
] = '\000';
2116 shadow_type
= ada_find_parallel_type_with_name (type
, name
);
2118 if (shadow_type
== NULL
)
2120 lim_warning (_("could not find bounds information on packed array"));
2123 shadow_type
= check_typedef (shadow_type
);
2125 if (shadow_type
->code () != TYPE_CODE_ARRAY
)
2127 lim_warning (_("could not understand bounds "
2128 "information on packed array"));
2132 bits
= decode_packed_array_bitsize (type
);
2133 return constrained_packed_array_type (shadow_type
, &bits
);
2136 /* Helper function for decode_constrained_packed_array. Set the field
2137 bitsize on a series of packed arrays. Returns the number of
2138 elements in TYPE. */
2141 recursively_update_array_bitsize (struct type
*type
)
2143 gdb_assert (type
->code () == TYPE_CODE_ARRAY
);
2146 if (!get_discrete_bounds (type
->index_type (), &low
, &high
)
2149 LONGEST our_len
= high
- low
+ 1;
2151 struct type
*elt_type
= TYPE_TARGET_TYPE (type
);
2152 if (elt_type
->code () == TYPE_CODE_ARRAY
)
2154 LONGEST elt_len
= recursively_update_array_bitsize (elt_type
);
2155 LONGEST elt_bitsize
= elt_len
* TYPE_FIELD_BITSIZE (elt_type
, 0);
2156 TYPE_FIELD_BITSIZE (type
, 0) = elt_bitsize
;
2158 TYPE_LENGTH (type
) = ((our_len
* elt_bitsize
+ HOST_CHAR_BIT
- 1)
2165 /* Given that ARR is a struct value *indicating a GNAT constrained packed
2166 array, returns a simple array that denotes that array. Its type is a
2167 standard GDB array type except that the BITSIZEs of the array
2168 target types are set to the number of bits in each element, and the
2169 type length is set appropriately. */
2171 static struct value
*
2172 decode_constrained_packed_array (struct value
*arr
)
2176 /* If our value is a pointer, then dereference it. Likewise if
2177 the value is a reference. Make sure that this operation does not
2178 cause the target type to be fixed, as this would indirectly cause
2179 this array to be decoded. The rest of the routine assumes that
2180 the array hasn't been decoded yet, so we use the basic "coerce_ref"
2181 and "value_ind" routines to perform the dereferencing, as opposed
2182 to using "ada_coerce_ref" or "ada_value_ind". */
2183 arr
= coerce_ref (arr
);
2184 if (ada_check_typedef (value_type (arr
))->code () == TYPE_CODE_PTR
)
2185 arr
= value_ind (arr
);
2187 type
= decode_constrained_packed_array_type (value_type (arr
));
2190 error (_("can't unpack array"));
2194 /* Decoding the packed array type could not correctly set the field
2195 bitsizes for any dimension except the innermost, because the
2196 bounds may be variable and were not passed to that function. So,
2197 we further resolve the array bounds here and then update the
2199 const gdb_byte
*valaddr
= value_contents_for_printing (arr
);
2200 CORE_ADDR address
= value_address (arr
);
2201 gdb::array_view
<const gdb_byte
> view
2202 = gdb::make_array_view (valaddr
, TYPE_LENGTH (type
));
2203 type
= resolve_dynamic_type (type
, view
, address
);
2204 recursively_update_array_bitsize (type
);
2206 if (type_byte_order (value_type (arr
)) == BFD_ENDIAN_BIG
2207 && ada_is_modular_type (value_type (arr
)))
2209 /* This is a (right-justified) modular type representing a packed
2210 array with no wrapper. In order to interpret the value through
2211 the (left-justified) packed array type we just built, we must
2212 first left-justify it. */
2213 int bit_size
, bit_pos
;
2216 mod
= ada_modulus (value_type (arr
)) - 1;
2223 bit_pos
= HOST_CHAR_BIT
* TYPE_LENGTH (value_type (arr
)) - bit_size
;
2224 arr
= ada_value_primitive_packed_val (arr
, NULL
,
2225 bit_pos
/ HOST_CHAR_BIT
,
2226 bit_pos
% HOST_CHAR_BIT
,
2231 return coerce_unspec_val_to_type (arr
, type
);
2235 /* The value of the element of packed array ARR at the ARITY indices
2236 given in IND. ARR must be a simple array. */
2238 static struct value
*
2239 value_subscript_packed (struct value
*arr
, int arity
, struct value
**ind
)
2242 int bits
, elt_off
, bit_off
;
2243 long elt_total_bit_offset
;
2244 struct type
*elt_type
;
2248 elt_total_bit_offset
= 0;
2249 elt_type
= ada_check_typedef (value_type (arr
));
2250 for (i
= 0; i
< arity
; i
+= 1)
2252 if (elt_type
->code () != TYPE_CODE_ARRAY
2253 || TYPE_FIELD_BITSIZE (elt_type
, 0) == 0)
2255 (_("attempt to do packed indexing of "
2256 "something other than a packed array"));
2259 struct type
*range_type
= elt_type
->index_type ();
2260 LONGEST lowerbound
, upperbound
;
2263 if (!get_discrete_bounds (range_type
, &lowerbound
, &upperbound
))
2265 lim_warning (_("don't know bounds of array"));
2266 lowerbound
= upperbound
= 0;
2269 idx
= pos_atr (ind
[i
]);
2270 if (idx
< lowerbound
|| idx
> upperbound
)
2271 lim_warning (_("packed array index %ld out of bounds"),
2273 bits
= TYPE_FIELD_BITSIZE (elt_type
, 0);
2274 elt_total_bit_offset
+= (idx
- lowerbound
) * bits
;
2275 elt_type
= ada_check_typedef (TYPE_TARGET_TYPE (elt_type
));
2278 elt_off
= elt_total_bit_offset
/ HOST_CHAR_BIT
;
2279 bit_off
= elt_total_bit_offset
% HOST_CHAR_BIT
;
2281 v
= ada_value_primitive_packed_val (arr
, NULL
, elt_off
, bit_off
,
2286 /* Non-zero iff TYPE includes negative integer values. */
2289 has_negatives (struct type
*type
)
2291 switch (type
->code ())
2296 return !type
->is_unsigned ();
2297 case TYPE_CODE_RANGE
:
2298 return type
->bounds ()->low
.const_val () - type
->bounds ()->bias
< 0;
2302 /* With SRC being a buffer containing BIT_SIZE bits of data at BIT_OFFSET,
2303 unpack that data into UNPACKED. UNPACKED_LEN is the size in bytes of
2304 the unpacked buffer.
2306 The size of the unpacked buffer (UNPACKED_LEN) is expected to be large
2307 enough to contain at least BIT_OFFSET bits. If not, an error is raised.
2309 IS_BIG_ENDIAN is nonzero if the data is stored in big endian mode,
2312 IS_SIGNED_TYPE is nonzero if the data corresponds to a signed type.
2314 IS_SCALAR is nonzero if the data corresponds to a signed type. */
2317 ada_unpack_from_contents (const gdb_byte
*src
, int bit_offset
, int bit_size
,
2318 gdb_byte
*unpacked
, int unpacked_len
,
2319 int is_big_endian
, int is_signed_type
,
2322 int src_len
= (bit_size
+ bit_offset
+ HOST_CHAR_BIT
- 1) / 8;
2323 int src_idx
; /* Index into the source area */
2324 int src_bytes_left
; /* Number of source bytes left to process. */
2325 int srcBitsLeft
; /* Number of source bits left to move */
2326 int unusedLS
; /* Number of bits in next significant
2327 byte of source that are unused */
2329 int unpacked_idx
; /* Index into the unpacked buffer */
2330 int unpacked_bytes_left
; /* Number of bytes left to set in unpacked. */
2332 unsigned long accum
; /* Staging area for bits being transferred */
2333 int accumSize
; /* Number of meaningful bits in accum */
2336 /* Transmit bytes from least to most significant; delta is the direction
2337 the indices move. */
2338 int delta
= is_big_endian
? -1 : 1;
2340 /* Make sure that unpacked is large enough to receive the BIT_SIZE
2342 if ((bit_size
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
> unpacked_len
)
2343 error (_("Cannot unpack %d bits into buffer of %d bytes"),
2344 bit_size
, unpacked_len
);
2346 srcBitsLeft
= bit_size
;
2347 src_bytes_left
= src_len
;
2348 unpacked_bytes_left
= unpacked_len
;
2353 src_idx
= src_len
- 1;
2355 && ((src
[0] << bit_offset
) & (1 << (HOST_CHAR_BIT
- 1))))
2359 (HOST_CHAR_BIT
- (bit_size
+ bit_offset
) % HOST_CHAR_BIT
)
2365 unpacked_idx
= unpacked_len
- 1;
2369 /* Non-scalar values must be aligned at a byte boundary... */
2371 (HOST_CHAR_BIT
- bit_size
% HOST_CHAR_BIT
) % HOST_CHAR_BIT
;
2372 /* ... And are placed at the beginning (most-significant) bytes
2374 unpacked_idx
= (bit_size
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
- 1;
2375 unpacked_bytes_left
= unpacked_idx
+ 1;
2380 int sign_bit_offset
= (bit_size
+ bit_offset
- 1) % 8;
2382 src_idx
= unpacked_idx
= 0;
2383 unusedLS
= bit_offset
;
2386 if (is_signed_type
&& (src
[src_len
- 1] & (1 << sign_bit_offset
)))
2391 while (src_bytes_left
> 0)
2393 /* Mask for removing bits of the next source byte that are not
2394 part of the value. */
2395 unsigned int unusedMSMask
=
2396 (1 << (srcBitsLeft
>= HOST_CHAR_BIT
? HOST_CHAR_BIT
: srcBitsLeft
)) -
2398 /* Sign-extend bits for this byte. */
2399 unsigned int signMask
= sign
& ~unusedMSMask
;
2402 (((src
[src_idx
] >> unusedLS
) & unusedMSMask
) | signMask
) << accumSize
;
2403 accumSize
+= HOST_CHAR_BIT
- unusedLS
;
2404 if (accumSize
>= HOST_CHAR_BIT
)
2406 unpacked
[unpacked_idx
] = accum
& ~(~0UL << HOST_CHAR_BIT
);
2407 accumSize
-= HOST_CHAR_BIT
;
2408 accum
>>= HOST_CHAR_BIT
;
2409 unpacked_bytes_left
-= 1;
2410 unpacked_idx
+= delta
;
2412 srcBitsLeft
-= HOST_CHAR_BIT
- unusedLS
;
2414 src_bytes_left
-= 1;
2417 while (unpacked_bytes_left
> 0)
2419 accum
|= sign
<< accumSize
;
2420 unpacked
[unpacked_idx
] = accum
& ~(~0UL << HOST_CHAR_BIT
);
2421 accumSize
-= HOST_CHAR_BIT
;
2424 accum
>>= HOST_CHAR_BIT
;
2425 unpacked_bytes_left
-= 1;
2426 unpacked_idx
+= delta
;
2430 /* Create a new value of type TYPE from the contents of OBJ starting
2431 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2432 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2433 assigning through the result will set the field fetched from.
2434 VALADDR is ignored unless OBJ is NULL, in which case,
2435 VALADDR+OFFSET must address the start of storage containing the
2436 packed value. The value returned in this case is never an lval.
2437 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2440 ada_value_primitive_packed_val (struct value
*obj
, const gdb_byte
*valaddr
,
2441 long offset
, int bit_offset
, int bit_size
,
2445 const gdb_byte
*src
; /* First byte containing data to unpack */
2447 const int is_scalar
= is_scalar_type (type
);
2448 const int is_big_endian
= type_byte_order (type
) == BFD_ENDIAN_BIG
;
2449 gdb::byte_vector staging
;
2451 type
= ada_check_typedef (type
);
2454 src
= valaddr
+ offset
;
2456 src
= value_contents (obj
) + offset
;
2458 if (is_dynamic_type (type
))
2460 /* The length of TYPE might by dynamic, so we need to resolve
2461 TYPE in order to know its actual size, which we then use
2462 to create the contents buffer of the value we return.
2463 The difficulty is that the data containing our object is
2464 packed, and therefore maybe not at a byte boundary. So, what
2465 we do, is unpack the data into a byte-aligned buffer, and then
2466 use that buffer as our object's value for resolving the type. */
2467 int staging_len
= (bit_size
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
2468 staging
.resize (staging_len
);
2470 ada_unpack_from_contents (src
, bit_offset
, bit_size
,
2471 staging
.data (), staging
.size (),
2472 is_big_endian
, has_negatives (type
),
2474 type
= resolve_dynamic_type (type
, staging
, 0);
2475 if (TYPE_LENGTH (type
) < (bit_size
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
)
2477 /* This happens when the length of the object is dynamic,
2478 and is actually smaller than the space reserved for it.
2479 For instance, in an array of variant records, the bit_size
2480 we're given is the array stride, which is constant and
2481 normally equal to the maximum size of its element.
2482 But, in reality, each element only actually spans a portion
2484 bit_size
= TYPE_LENGTH (type
) * HOST_CHAR_BIT
;
2490 v
= allocate_value (type
);
2491 src
= valaddr
+ offset
;
2493 else if (VALUE_LVAL (obj
) == lval_memory
&& value_lazy (obj
))
2495 int src_len
= (bit_size
+ bit_offset
+ HOST_CHAR_BIT
- 1) / 8;
2498 v
= value_at (type
, value_address (obj
) + offset
);
2499 buf
= (gdb_byte
*) alloca (src_len
);
2500 read_memory (value_address (v
), buf
, src_len
);
2505 v
= allocate_value (type
);
2506 src
= value_contents (obj
) + offset
;
2511 long new_offset
= offset
;
2513 set_value_component_location (v
, obj
);
2514 set_value_bitpos (v
, bit_offset
+ value_bitpos (obj
));
2515 set_value_bitsize (v
, bit_size
);
2516 if (value_bitpos (v
) >= HOST_CHAR_BIT
)
2519 set_value_bitpos (v
, value_bitpos (v
) - HOST_CHAR_BIT
);
2521 set_value_offset (v
, new_offset
);
2523 /* Also set the parent value. This is needed when trying to
2524 assign a new value (in inferior memory). */
2525 set_value_parent (v
, obj
);
2528 set_value_bitsize (v
, bit_size
);
2529 unpacked
= value_contents_writeable (v
);
2533 memset (unpacked
, 0, TYPE_LENGTH (type
));
2537 if (staging
.size () == TYPE_LENGTH (type
))
2539 /* Small short-cut: If we've unpacked the data into a buffer
2540 of the same size as TYPE's length, then we can reuse that,
2541 instead of doing the unpacking again. */
2542 memcpy (unpacked
, staging
.data (), staging
.size ());
2545 ada_unpack_from_contents (src
, bit_offset
, bit_size
,
2546 unpacked
, TYPE_LENGTH (type
),
2547 is_big_endian
, has_negatives (type
), is_scalar
);
2552 /* Store the contents of FROMVAL into the location of TOVAL.
2553 Return a new value with the location of TOVAL and contents of
2554 FROMVAL. Handles assignment into packed fields that have
2555 floating-point or non-scalar types. */
2557 static struct value
*
2558 ada_value_assign (struct value
*toval
, struct value
*fromval
)
2560 struct type
*type
= value_type (toval
);
2561 int bits
= value_bitsize (toval
);
2563 toval
= ada_coerce_ref (toval
);
2564 fromval
= ada_coerce_ref (fromval
);
2566 if (ada_is_direct_array_type (value_type (toval
)))
2567 toval
= ada_coerce_to_simple_array (toval
);
2568 if (ada_is_direct_array_type (value_type (fromval
)))
2569 fromval
= ada_coerce_to_simple_array (fromval
);
2571 if (!deprecated_value_modifiable (toval
))
2572 error (_("Left operand of assignment is not a modifiable lvalue."));
2574 if (VALUE_LVAL (toval
) == lval_memory
2576 && (type
->code () == TYPE_CODE_FLT
2577 || type
->code () == TYPE_CODE_STRUCT
))
2579 int len
= (value_bitpos (toval
)
2580 + bits
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
2582 gdb_byte
*buffer
= (gdb_byte
*) alloca (len
);
2584 CORE_ADDR to_addr
= value_address (toval
);
2586 if (type
->code () == TYPE_CODE_FLT
)
2587 fromval
= value_cast (type
, fromval
);
2589 read_memory (to_addr
, buffer
, len
);
2590 from_size
= value_bitsize (fromval
);
2592 from_size
= TYPE_LENGTH (value_type (fromval
)) * TARGET_CHAR_BIT
;
2594 const int is_big_endian
= type_byte_order (type
) == BFD_ENDIAN_BIG
;
2595 ULONGEST from_offset
= 0;
2596 if (is_big_endian
&& is_scalar_type (value_type (fromval
)))
2597 from_offset
= from_size
- bits
;
2598 copy_bitwise (buffer
, value_bitpos (toval
),
2599 value_contents (fromval
), from_offset
,
2600 bits
, is_big_endian
);
2601 write_memory_with_notification (to_addr
, buffer
, len
);
2603 val
= value_copy (toval
);
2604 memcpy (value_contents_raw (val
), value_contents (fromval
),
2605 TYPE_LENGTH (type
));
2606 deprecated_set_value_type (val
, type
);
2611 return value_assign (toval
, fromval
);
2615 /* Given that COMPONENT is a memory lvalue that is part of the lvalue
2616 CONTAINER, assign the contents of VAL to COMPONENTS's place in
2617 CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2618 COMPONENT, and not the inferior's memory. The current contents
2619 of COMPONENT are ignored.
2621 Although not part of the initial design, this function also works
2622 when CONTAINER and COMPONENT are not_lval's: it works as if CONTAINER
2623 had a null address, and COMPONENT had an address which is equal to
2624 its offset inside CONTAINER. */
2627 value_assign_to_component (struct value
*container
, struct value
*component
,
2630 LONGEST offset_in_container
=
2631 (LONGEST
) (value_address (component
) - value_address (container
));
2632 int bit_offset_in_container
=
2633 value_bitpos (component
) - value_bitpos (container
);
2636 val
= value_cast (value_type (component
), val
);
2638 if (value_bitsize (component
) == 0)
2639 bits
= TARGET_CHAR_BIT
* TYPE_LENGTH (value_type (component
));
2641 bits
= value_bitsize (component
);
2643 if (type_byte_order (value_type (container
)) == BFD_ENDIAN_BIG
)
2647 if (is_scalar_type (check_typedef (value_type (component
))))
2649 = TYPE_LENGTH (value_type (component
)) * TARGET_CHAR_BIT
- bits
;
2652 copy_bitwise (value_contents_writeable (container
) + offset_in_container
,
2653 value_bitpos (container
) + bit_offset_in_container
,
2654 value_contents (val
), src_offset
, bits
, 1);
2657 copy_bitwise (value_contents_writeable (container
) + offset_in_container
,
2658 value_bitpos (container
) + bit_offset_in_container
,
2659 value_contents (val
), 0, bits
, 0);
2662 /* Determine if TYPE is an access to an unconstrained array. */
2665 ada_is_access_to_unconstrained_array (struct type
*type
)
2667 return (type
->code () == TYPE_CODE_TYPEDEF
2668 && is_thick_pntr (ada_typedef_target_type (type
)));
2671 /* The value of the element of array ARR at the ARITY indices given in IND.
2672 ARR may be either a simple array, GNAT array descriptor, or pointer
2676 ada_value_subscript (struct value
*arr
, int arity
, struct value
**ind
)
2680 struct type
*elt_type
;
2682 elt
= ada_coerce_to_simple_array (arr
);
2684 elt_type
= ada_check_typedef (value_type (elt
));
2685 if (elt_type
->code () == TYPE_CODE_ARRAY
2686 && TYPE_FIELD_BITSIZE (elt_type
, 0) > 0)
2687 return value_subscript_packed (elt
, arity
, ind
);
2689 for (k
= 0; k
< arity
; k
+= 1)
2691 struct type
*saved_elt_type
= TYPE_TARGET_TYPE (elt_type
);
2693 if (elt_type
->code () != TYPE_CODE_ARRAY
)
2694 error (_("too many subscripts (%d expected)"), k
);
2696 elt
= value_subscript (elt
, pos_atr (ind
[k
]));
2698 if (ada_is_access_to_unconstrained_array (saved_elt_type
)
2699 && value_type (elt
)->code () != TYPE_CODE_TYPEDEF
)
2701 /* The element is a typedef to an unconstrained array,
2702 except that the value_subscript call stripped the
2703 typedef layer. The typedef layer is GNAT's way to
2704 specify that the element is, at the source level, an
2705 access to the unconstrained array, rather than the
2706 unconstrained array. So, we need to restore that
2707 typedef layer, which we can do by forcing the element's
2708 type back to its original type. Otherwise, the returned
2709 value is going to be printed as the array, rather
2710 than as an access. Another symptom of the same issue
2711 would be that an expression trying to dereference the
2712 element would also be improperly rejected. */
2713 deprecated_set_value_type (elt
, saved_elt_type
);
2716 elt_type
= ada_check_typedef (value_type (elt
));
2722 /* Assuming ARR is a pointer to a GDB array, the value of the element
2723 of *ARR at the ARITY indices given in IND.
2724 Does not read the entire array into memory.
2726 Note: Unlike what one would expect, this function is used instead of
2727 ada_value_subscript for basically all non-packed array types. The reason
2728 for this is that a side effect of doing our own pointer arithmetics instead
2729 of relying on value_subscript is that there is no implicit typedef peeling.
2730 This is important for arrays of array accesses, where it allows us to
2731 preserve the fact that the array's element is an array access, where the
2732 access part os encoded in a typedef layer. */
2734 static struct value
*
2735 ada_value_ptr_subscript (struct value
*arr
, int arity
, struct value
**ind
)
2738 struct value
*array_ind
= ada_value_ind (arr
);
2740 = check_typedef (value_enclosing_type (array_ind
));
2742 if (type
->code () == TYPE_CODE_ARRAY
2743 && TYPE_FIELD_BITSIZE (type
, 0) > 0)
2744 return value_subscript_packed (array_ind
, arity
, ind
);
2746 for (k
= 0; k
< arity
; k
+= 1)
2750 if (type
->code () != TYPE_CODE_ARRAY
)
2751 error (_("too many subscripts (%d expected)"), k
);
2752 arr
= value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
2754 get_discrete_bounds (type
->index_type (), &lwb
, &upb
);
2755 arr
= value_ptradd (arr
, pos_atr (ind
[k
]) - lwb
);
2756 type
= TYPE_TARGET_TYPE (type
);
2759 return value_ind (arr
);
2762 /* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
2763 actual type of ARRAY_PTR is ignored), returns the Ada slice of
2764 HIGH'Pos-LOW'Pos+1 elements starting at index LOW. The lower bound of
2765 this array is LOW, as per Ada rules. */
2766 static struct value
*
2767 ada_value_slice_from_ptr (struct value
*array_ptr
, struct type
*type
,
2770 struct type
*type0
= ada_check_typedef (type
);
2771 struct type
*base_index_type
= TYPE_TARGET_TYPE (type0
->index_type ());
2772 struct type
*index_type
2773 = create_static_range_type (NULL
, base_index_type
, low
, high
);
2774 struct type
*slice_type
= create_array_type_with_stride
2775 (NULL
, TYPE_TARGET_TYPE (type0
), index_type
,
2776 type0
->dyn_prop (DYN_PROP_BYTE_STRIDE
),
2777 TYPE_FIELD_BITSIZE (type0
, 0));
2778 int base_low
= ada_discrete_type_low_bound (type0
->index_type ());
2779 gdb::optional
<LONGEST
> base_low_pos
, low_pos
;
2782 low_pos
= discrete_position (base_index_type
, low
);
2783 base_low_pos
= discrete_position (base_index_type
, base_low
);
2785 if (!low_pos
.has_value () || !base_low_pos
.has_value ())
2787 warning (_("unable to get positions in slice, use bounds instead"));
2789 base_low_pos
= base_low
;
2792 ULONGEST stride
= TYPE_FIELD_BITSIZE (slice_type
, 0) / 8;
2794 stride
= TYPE_LENGTH (TYPE_TARGET_TYPE (type0
));
2796 base
= value_as_address (array_ptr
) + (*low_pos
- *base_low_pos
) * stride
;
2797 return value_at_lazy (slice_type
, base
);
2801 static struct value
*
2802 ada_value_slice (struct value
*array
, int low
, int high
)
2804 struct type
*type
= ada_check_typedef (value_type (array
));
2805 struct type
*base_index_type
= TYPE_TARGET_TYPE (type
->index_type ());
2806 struct type
*index_type
2807 = create_static_range_type (NULL
, type
->index_type (), low
, high
);
2808 struct type
*slice_type
= create_array_type_with_stride
2809 (NULL
, TYPE_TARGET_TYPE (type
), index_type
,
2810 type
->dyn_prop (DYN_PROP_BYTE_STRIDE
),
2811 TYPE_FIELD_BITSIZE (type
, 0));
2812 gdb::optional
<LONGEST
> low_pos
, high_pos
;
2815 low_pos
= discrete_position (base_index_type
, low
);
2816 high_pos
= discrete_position (base_index_type
, high
);
2818 if (!low_pos
.has_value () || !high_pos
.has_value ())
2820 warning (_("unable to get positions in slice, use bounds instead"));
2825 return value_cast (slice_type
,
2826 value_slice (array
, low
, *high_pos
- *low_pos
+ 1));
2829 /* If type is a record type in the form of a standard GNAT array
2830 descriptor, returns the number of dimensions for type. If arr is a
2831 simple array, returns the number of "array of"s that prefix its
2832 type designation. Otherwise, returns 0. */
2835 ada_array_arity (struct type
*type
)
2842 type
= desc_base_type (type
);
2845 if (type
->code () == TYPE_CODE_STRUCT
)
2846 return desc_arity (desc_bounds_type (type
));
2848 while (type
->code () == TYPE_CODE_ARRAY
)
2851 type
= ada_check_typedef (TYPE_TARGET_TYPE (type
));
2857 /* If TYPE is a record type in the form of a standard GNAT array
2858 descriptor or a simple array type, returns the element type for
2859 TYPE after indexing by NINDICES indices, or by all indices if
2860 NINDICES is -1. Otherwise, returns NULL. */
2863 ada_array_element_type (struct type
*type
, int nindices
)
2865 type
= desc_base_type (type
);
2867 if (type
->code () == TYPE_CODE_STRUCT
)
2870 struct type
*p_array_type
;
2872 p_array_type
= desc_data_target_type (type
);
2874 k
= ada_array_arity (type
);
2878 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
2879 if (nindices
>= 0 && k
> nindices
)
2881 while (k
> 0 && p_array_type
!= NULL
)
2883 p_array_type
= ada_check_typedef (TYPE_TARGET_TYPE (p_array_type
));
2886 return p_array_type
;
2888 else if (type
->code () == TYPE_CODE_ARRAY
)
2890 while (nindices
!= 0 && type
->code () == TYPE_CODE_ARRAY
)
2892 type
= TYPE_TARGET_TYPE (type
);
2901 /* The type of nth index in arrays of given type (n numbering from 1).
2902 Does not examine memory. Throws an error if N is invalid or TYPE
2903 is not an array type. NAME is the name of the Ada attribute being
2904 evaluated ('range, 'first, 'last, or 'length); it is used in building
2905 the error message. */
2907 static struct type
*
2908 ada_index_type (struct type
*type
, int n
, const char *name
)
2910 struct type
*result_type
;
2912 type
= desc_base_type (type
);
2914 if (n
< 0 || n
> ada_array_arity (type
))
2915 error (_("invalid dimension number to '%s"), name
);
2917 if (ada_is_simple_array_type (type
))
2921 for (i
= 1; i
< n
; i
+= 1)
2922 type
= TYPE_TARGET_TYPE (type
);
2923 result_type
= TYPE_TARGET_TYPE (type
->index_type ());
2924 /* FIXME: The stabs type r(0,0);bound;bound in an array type
2925 has a target type of TYPE_CODE_UNDEF. We compensate here, but
2926 perhaps stabsread.c would make more sense. */
2927 if (result_type
&& result_type
->code () == TYPE_CODE_UNDEF
)
2932 result_type
= desc_index_type (desc_bounds_type (type
), n
);
2933 if (result_type
== NULL
)
2934 error (_("attempt to take bound of something that is not an array"));
2940 /* Given that arr is an array type, returns the lower bound of the
2941 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
2942 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
2943 array-descriptor type. It works for other arrays with bounds supplied
2944 by run-time quantities other than discriminants. */
2947 ada_array_bound_from_type (struct type
*arr_type
, int n
, int which
)
2949 struct type
*type
, *index_type_desc
, *index_type
;
2952 gdb_assert (which
== 0 || which
== 1);
2954 if (ada_is_constrained_packed_array_type (arr_type
))
2955 arr_type
= decode_constrained_packed_array_type (arr_type
);
2957 if (arr_type
== NULL
|| !ada_is_simple_array_type (arr_type
))
2958 return (LONGEST
) - which
;
2960 if (arr_type
->code () == TYPE_CODE_PTR
)
2961 type
= TYPE_TARGET_TYPE (arr_type
);
2965 if (type
->is_fixed_instance ())
2967 /* The array has already been fixed, so we do not need to
2968 check the parallel ___XA type again. That encoding has
2969 already been applied, so ignore it now. */
2970 index_type_desc
= NULL
;
2974 index_type_desc
= ada_find_parallel_type (type
, "___XA");
2975 ada_fixup_array_indexes_type (index_type_desc
);
2978 if (index_type_desc
!= NULL
)
2979 index_type
= to_fixed_range_type (index_type_desc
->field (n
- 1).type (),
2983 struct type
*elt_type
= check_typedef (type
);
2985 for (i
= 1; i
< n
; i
++)
2986 elt_type
= check_typedef (TYPE_TARGET_TYPE (elt_type
));
2988 index_type
= elt_type
->index_type ();
2992 (LONGEST
) (which
== 0
2993 ? ada_discrete_type_low_bound (index_type
)
2994 : ada_discrete_type_high_bound (index_type
));
2997 /* Given that arr is an array value, returns the lower bound of the
2998 nth index (numbering from 1) if WHICH is 0, and the upper bound if
2999 WHICH is 1. This routine will also work for arrays with bounds
3000 supplied by run-time quantities other than discriminants. */
3003 ada_array_bound (struct value
*arr
, int n
, int which
)
3005 struct type
*arr_type
;
3007 if (check_typedef (value_type (arr
))->code () == TYPE_CODE_PTR
)
3008 arr
= value_ind (arr
);
3009 arr_type
= value_enclosing_type (arr
);
3011 if (ada_is_constrained_packed_array_type (arr_type
))
3012 return ada_array_bound (decode_constrained_packed_array (arr
), n
, which
);
3013 else if (ada_is_simple_array_type (arr_type
))
3014 return ada_array_bound_from_type (arr_type
, n
, which
);
3016 return value_as_long (desc_one_bound (desc_bounds (arr
), n
, which
));
3019 /* Given that arr is an array value, returns the length of the
3020 nth index. This routine will also work for arrays with bounds
3021 supplied by run-time quantities other than discriminants.
3022 Does not work for arrays indexed by enumeration types with representation
3023 clauses at the moment. */
3026 ada_array_length (struct value
*arr
, int n
)
3028 struct type
*arr_type
, *index_type
;
3031 if (check_typedef (value_type (arr
))->code () == TYPE_CODE_PTR
)
3032 arr
= value_ind (arr
);
3033 arr_type
= value_enclosing_type (arr
);
3035 if (ada_is_constrained_packed_array_type (arr_type
))
3036 return ada_array_length (decode_constrained_packed_array (arr
), n
);
3038 if (ada_is_simple_array_type (arr_type
))
3040 low
= ada_array_bound_from_type (arr_type
, n
, 0);
3041 high
= ada_array_bound_from_type (arr_type
, n
, 1);
3045 low
= value_as_long (desc_one_bound (desc_bounds (arr
), n
, 0));
3046 high
= value_as_long (desc_one_bound (desc_bounds (arr
), n
, 1));
3049 arr_type
= check_typedef (arr_type
);
3050 index_type
= ada_index_type (arr_type
, n
, "length");
3051 if (index_type
!= NULL
)
3053 struct type
*base_type
;
3054 if (index_type
->code () == TYPE_CODE_RANGE
)
3055 base_type
= TYPE_TARGET_TYPE (index_type
);
3057 base_type
= index_type
;
3059 low
= pos_atr (value_from_longest (base_type
, low
));
3060 high
= pos_atr (value_from_longest (base_type
, high
));
3062 return high
- low
+ 1;
3065 /* An array whose type is that of ARR_TYPE (an array type), with
3066 bounds LOW to HIGH, but whose contents are unimportant. If HIGH is
3067 less than LOW, then LOW-1 is used. */
3069 static struct value
*
3070 empty_array (struct type
*arr_type
, int low
, int high
)
3072 struct type
*arr_type0
= ada_check_typedef (arr_type
);
3073 struct type
*index_type
3074 = create_static_range_type
3075 (NULL
, TYPE_TARGET_TYPE (arr_type0
->index_type ()), low
,
3076 high
< low
? low
- 1 : high
);
3077 struct type
*elt_type
= ada_array_element_type (arr_type0
, 1);
3079 return allocate_value (create_array_type (NULL
, elt_type
, index_type
));
3083 /* Name resolution */
3085 /* The "decoded" name for the user-definable Ada operator corresponding
3089 ada_decoded_op_name (enum exp_opcode op
)
3093 for (i
= 0; ada_opname_table
[i
].encoded
!= NULL
; i
+= 1)
3095 if (ada_opname_table
[i
].op
== op
)
3096 return ada_opname_table
[i
].decoded
;
3098 error (_("Could not find operator name for opcode"));
3101 /* Returns true (non-zero) iff decoded name N0 should appear before N1
3102 in a listing of choices during disambiguation (see sort_choices, below).
3103 The idea is that overloadings of a subprogram name from the
3104 same package should sort in their source order. We settle for ordering
3105 such symbols by their trailing number (__N or $N). */
3108 encoded_ordered_before (const char *N0
, const char *N1
)
3112 else if (N0
== NULL
)
3118 for (k0
= strlen (N0
) - 1; k0
> 0 && isdigit (N0
[k0
]); k0
-= 1)
3120 for (k1
= strlen (N1
) - 1; k1
> 0 && isdigit (N1
[k1
]); k1
-= 1)
3122 if ((N0
[k0
] == '_' || N0
[k0
] == '$') && N0
[k0
+ 1] != '\000'
3123 && (N1
[k1
] == '_' || N1
[k1
] == '$') && N1
[k1
+ 1] != '\000')
3128 while (N0
[n0
] == '_' && n0
> 0 && N0
[n0
- 1] == '_')
3131 while (N1
[n1
] == '_' && n1
> 0 && N1
[n1
- 1] == '_')
3133 if (n0
== n1
&& strncmp (N0
, N1
, n0
) == 0)
3134 return (atoi (N0
+ k0
+ 1) < atoi (N1
+ k1
+ 1));
3136 return (strcmp (N0
, N1
) < 0);
3140 /* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3144 sort_choices (struct block_symbol syms
[], int nsyms
)
3148 for (i
= 1; i
< nsyms
; i
+= 1)
3150 struct block_symbol sym
= syms
[i
];
3153 for (j
= i
- 1; j
>= 0; j
-= 1)
3155 if (encoded_ordered_before (syms
[j
].symbol
->linkage_name (),
3156 sym
.symbol
->linkage_name ()))
3158 syms
[j
+ 1] = syms
[j
];
3164 /* Whether GDB should display formals and return types for functions in the
3165 overloads selection menu. */
3166 static bool print_signatures
= true;
3168 /* Print the signature for SYM on STREAM according to the FLAGS options. For
3169 all but functions, the signature is just the name of the symbol. For
3170 functions, this is the name of the function, the list of types for formals
3171 and the return type (if any). */
3174 ada_print_symbol_signature (struct ui_file
*stream
, struct symbol
*sym
,
3175 const struct type_print_options
*flags
)
3177 struct type
*type
= SYMBOL_TYPE (sym
);
3179 fprintf_filtered (stream
, "%s", sym
->print_name ());
3180 if (!print_signatures
3182 || type
->code () != TYPE_CODE_FUNC
)
3185 if (type
->num_fields () > 0)
3189 fprintf_filtered (stream
, " (");
3190 for (i
= 0; i
< type
->num_fields (); ++i
)
3193 fprintf_filtered (stream
, "; ");
3194 ada_print_type (type
->field (i
).type (), NULL
, stream
, -1, 0,
3197 fprintf_filtered (stream
, ")");
3199 if (TYPE_TARGET_TYPE (type
) != NULL
3200 && TYPE_TARGET_TYPE (type
)->code () != TYPE_CODE_VOID
)
3202 fprintf_filtered (stream
, " return ");
3203 ada_print_type (TYPE_TARGET_TYPE (type
), NULL
, stream
, -1, 0, flags
);
3207 /* Read and validate a set of numeric choices from the user in the
3208 range 0 .. N_CHOICES-1. Place the results in increasing
3209 order in CHOICES[0 .. N-1], and return N.
3211 The user types choices as a sequence of numbers on one line
3212 separated by blanks, encoding them as follows:
3214 + A choice of 0 means to cancel the selection, throwing an error.
3215 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3216 + The user chooses k by typing k+IS_ALL_CHOICE+1.
3218 The user is not allowed to choose more than MAX_RESULTS values.
3220 ANNOTATION_SUFFIX, if present, is used to annotate the input
3221 prompts (for use with the -f switch). */
3224 get_selections (int *choices
, int n_choices
, int max_results
,
3225 int is_all_choice
, const char *annotation_suffix
)
3230 int first_choice
= is_all_choice
? 2 : 1;
3232 prompt
= getenv ("PS2");
3236 args
= command_line_input (prompt
, annotation_suffix
);
3239 error_no_arg (_("one or more choice numbers"));
3243 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3244 order, as given in args. Choices are validated. */
3250 args
= skip_spaces (args
);
3251 if (*args
== '\0' && n_chosen
== 0)
3252 error_no_arg (_("one or more choice numbers"));
3253 else if (*args
== '\0')
3256 choice
= strtol (args
, &args2
, 10);
3257 if (args
== args2
|| choice
< 0
3258 || choice
> n_choices
+ first_choice
- 1)
3259 error (_("Argument must be choice number"));
3263 error (_("cancelled"));
3265 if (choice
< first_choice
)
3267 n_chosen
= n_choices
;
3268 for (j
= 0; j
< n_choices
; j
+= 1)
3272 choice
-= first_choice
;
3274 for (j
= n_chosen
- 1; j
>= 0 && choice
< choices
[j
]; j
-= 1)
3278 if (j
< 0 || choice
!= choices
[j
])
3282 for (k
= n_chosen
- 1; k
> j
; k
-= 1)
3283 choices
[k
+ 1] = choices
[k
];
3284 choices
[j
+ 1] = choice
;
3289 if (n_chosen
> max_results
)
3290 error (_("Select no more than %d of the above"), max_results
);
3295 /* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3296 by asking the user (if necessary), returning the number selected,
3297 and setting the first elements of SYMS items. Error if no symbols
3300 /* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
3301 to be re-integrated one of these days. */
3304 user_select_syms (struct block_symbol
*syms
, int nsyms
, int max_results
)
3307 int *chosen
= XALLOCAVEC (int , nsyms
);
3309 int first_choice
= (max_results
== 1) ? 1 : 2;
3310 const char *select_mode
= multiple_symbols_select_mode ();
3312 if (max_results
< 1)
3313 error (_("Request to select 0 symbols!"));
3317 if (select_mode
== multiple_symbols_cancel
)
3319 canceled because the command is ambiguous\n\
3320 See set/show multiple-symbol."));
3322 /* If select_mode is "all", then return all possible symbols.
3323 Only do that if more than one symbol can be selected, of course.
3324 Otherwise, display the menu as usual. */
3325 if (select_mode
== multiple_symbols_all
&& max_results
> 1)
3328 printf_filtered (_("[0] cancel\n"));
3329 if (max_results
> 1)
3330 printf_filtered (_("[1] all\n"));
3332 sort_choices (syms
, nsyms
);
3334 for (i
= 0; i
< nsyms
; i
+= 1)
3336 if (syms
[i
].symbol
== NULL
)
3339 if (SYMBOL_CLASS (syms
[i
].symbol
) == LOC_BLOCK
)
3341 struct symtab_and_line sal
=
3342 find_function_start_sal (syms
[i
].symbol
, 1);
3344 printf_filtered ("[%d] ", i
+ first_choice
);
3345 ada_print_symbol_signature (gdb_stdout
, syms
[i
].symbol
,
3346 &type_print_raw_options
);
3347 if (sal
.symtab
== NULL
)
3348 printf_filtered (_(" at %p[<no source file available>%p]:%d\n"),
3349 metadata_style
.style ().ptr (), nullptr, sal
.line
);
3353 styled_string (file_name_style
.style (),
3354 symtab_to_filename_for_display (sal
.symtab
)),
3361 (SYMBOL_CLASS (syms
[i
].symbol
) == LOC_CONST
3362 && SYMBOL_TYPE (syms
[i
].symbol
) != NULL
3363 && SYMBOL_TYPE (syms
[i
].symbol
)->code () == TYPE_CODE_ENUM
);
3364 struct symtab
*symtab
= NULL
;
3366 if (SYMBOL_OBJFILE_OWNED (syms
[i
].symbol
))
3367 symtab
= symbol_symtab (syms
[i
].symbol
);
3369 if (SYMBOL_LINE (syms
[i
].symbol
) != 0 && symtab
!= NULL
)
3371 printf_filtered ("[%d] ", i
+ first_choice
);
3372 ada_print_symbol_signature (gdb_stdout
, syms
[i
].symbol
,
3373 &type_print_raw_options
);
3374 printf_filtered (_(" at %s:%d\n"),
3375 symtab_to_filename_for_display (symtab
),
3376 SYMBOL_LINE (syms
[i
].symbol
));
3378 else if (is_enumeral
3379 && SYMBOL_TYPE (syms
[i
].symbol
)->name () != NULL
)
3381 printf_filtered (("[%d] "), i
+ first_choice
);
3382 ada_print_type (SYMBOL_TYPE (syms
[i
].symbol
), NULL
,
3383 gdb_stdout
, -1, 0, &type_print_raw_options
);
3384 printf_filtered (_("'(%s) (enumeral)\n"),
3385 syms
[i
].symbol
->print_name ());
3389 printf_filtered ("[%d] ", i
+ first_choice
);
3390 ada_print_symbol_signature (gdb_stdout
, syms
[i
].symbol
,
3391 &type_print_raw_options
);
3394 printf_filtered (is_enumeral
3395 ? _(" in %s (enumeral)\n")
3397 symtab_to_filename_for_display (symtab
));
3399 printf_filtered (is_enumeral
3400 ? _(" (enumeral)\n")
3406 n_chosen
= get_selections (chosen
, nsyms
, max_results
, max_results
> 1,
3409 for (i
= 0; i
< n_chosen
; i
+= 1)
3410 syms
[i
] = syms
[chosen
[i
]];
3415 /* See ada-lang.h. */
3418 ada_find_operator_symbol (enum exp_opcode op
, int parse_completion
,
3419 int nargs
, value
*argvec
[])
3421 if (possible_user_operator_p (op
, argvec
))
3423 std::vector
<struct block_symbol
> candidates
3424 = ada_lookup_symbol_list (ada_decoded_op_name (op
),
3427 int i
= ada_resolve_function (candidates
, argvec
,
3428 nargs
, ada_decoded_op_name (op
), NULL
,
3431 return candidates
[i
];
3436 /* See ada-lang.h. */
3439 ada_resolve_funcall (struct symbol
*sym
, const struct block
*block
,
3440 struct type
*context_type
,
3441 int parse_completion
,
3442 int nargs
, value
*argvec
[],
3443 innermost_block_tracker
*tracker
)
3445 std::vector
<struct block_symbol
> candidates
3446 = ada_lookup_symbol_list (sym
->linkage_name (), block
, VAR_DOMAIN
);
3449 if (candidates
.size () == 1)
3453 i
= ada_resolve_function
3456 sym
->linkage_name (),
3457 context_type
, parse_completion
);
3459 error (_("Could not find a match for %s"), sym
->print_name ());
3462 tracker
->update (candidates
[i
]);
3463 return candidates
[i
];
3466 /* See ada-lang.h. */
3469 ada_resolve_variable (struct symbol
*sym
, const struct block
*block
,
3470 struct type
*context_type
,
3471 int parse_completion
,
3473 innermost_block_tracker
*tracker
)
3475 std::vector
<struct block_symbol
> candidates
3476 = ada_lookup_symbol_list (sym
->linkage_name (), block
, VAR_DOMAIN
);
3478 if (std::any_of (candidates
.begin (),
3480 [] (block_symbol
&bsym
)
3482 switch (SYMBOL_CLASS (bsym
.symbol
))
3487 case LOC_REGPARM_ADDR
:
3496 /* Types tend to get re-introduced locally, so if there
3497 are any local symbols that are not types, first filter
3501 (candidates
.begin (),
3503 [] (block_symbol
&bsym
)
3505 return SYMBOL_CLASS (bsym
.symbol
) == LOC_TYPEDEF
;
3511 if (candidates
.empty ())
3512 error (_("No definition found for %s"), sym
->print_name ());
3513 else if (candidates
.size () == 1)
3515 else if (deprocedure_p
&& !is_nonfunction (candidates
))
3517 i
= ada_resolve_function
3518 (candidates
, NULL
, 0,
3519 sym
->linkage_name (),
3520 context_type
, parse_completion
);
3522 error (_("Could not find a match for %s"), sym
->print_name ());
3526 printf_filtered (_("Multiple matches for %s\n"), sym
->print_name ());
3527 user_select_syms (candidates
.data (), candidates
.size (), 1);
3531 tracker
->update (candidates
[i
]);
3532 return candidates
[i
];
3535 /* Resolve the operator of the subexpression beginning at
3536 position *POS of *EXPP. "Resolving" consists of replacing
3537 the symbols that have undefined namespaces in OP_VAR_VALUE nodes
3538 with their resolutions, replacing built-in operators with
3539 function calls to user-defined operators, where appropriate, and,
3540 when DEPROCEDURE_P is non-zero, converting function-valued variables
3541 into parameterless calls. May expand *EXPP. The CONTEXT_TYPE functions
3542 are as in ada_resolve, above. */
3544 static struct value
*
3545 resolve_subexp (expression_up
*expp
, int *pos
, int deprocedure_p
,
3546 struct type
*context_type
, int parse_completion
,
3547 innermost_block_tracker
*tracker
)
3551 struct expression
*exp
; /* Convenience: == *expp. */
3552 enum exp_opcode op
= (*expp
)->elts
[pc
].opcode
;
3553 struct value
**argvec
; /* Vector of operand types (alloca'ed). */
3554 int nargs
; /* Number of operands. */
3556 /* If we're resolving an expression like ARRAY(ARG...), then we set
3557 this to the type of the array, so we can use the index types as
3558 the expected types for resolution. */
3559 struct type
*array_type
= nullptr;
3560 /* The arity of ARRAY_TYPE. */
3561 int array_arity
= 0;
3567 /* Pass one: resolve operands, saving their types and updating *pos,
3572 if (exp
->elts
[pc
+ 3].opcode
== OP_VAR_VALUE
3573 && SYMBOL_DOMAIN (exp
->elts
[pc
+ 5].symbol
) == UNDEF_DOMAIN
)
3578 struct value
*lhs
= resolve_subexp (expp
, pos
, 0, NULL
,
3579 parse_completion
, tracker
);
3580 struct type
*lhstype
= ada_check_typedef (value_type (lhs
));
3581 array_arity
= ada_array_arity (lhstype
);
3582 if (array_arity
> 0)
3583 array_type
= lhstype
;
3585 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
3590 resolve_subexp (expp
, pos
, 0, NULL
, parse_completion
, tracker
);
3595 resolve_subexp (expp
, pos
, 1, check_typedef (exp
->elts
[pc
+ 1].type
),
3596 parse_completion
, tracker
);
3599 case OP_ATR_MODULUS
:
3609 case TERNOP_IN_RANGE
:
3610 case BINOP_IN_BOUNDS
:
3616 case OP_DISCRETE_RANGE
:
3618 ada_forward_operator_length (exp
, pc
, &oplen
, &nargs
);
3627 arg1
= resolve_subexp (expp
, pos
, 0, NULL
, parse_completion
, tracker
);
3629 resolve_subexp (expp
, pos
, 1, NULL
, parse_completion
, tracker
);
3631 resolve_subexp (expp
, pos
, 1, value_type (arg1
), parse_completion
,
3649 case BINOP_LOGICAL_AND
:
3650 case BINOP_LOGICAL_OR
:
3651 case BINOP_BITWISE_AND
:
3652 case BINOP_BITWISE_IOR
:
3653 case BINOP_BITWISE_XOR
:
3656 case BINOP_NOTEQUAL
:
3663 case BINOP_SUBSCRIPT
:
3671 case UNOP_LOGICAL_NOT
:
3681 case OP_VAR_MSYM_VALUE
:
3688 case OP_INTERNALVAR
:
3698 *pos
+= 4 + BYTES_TO_EXP_ELEM (exp
->elts
[pc
+ 1].longconst
+ 1);
3701 case STRUCTOP_STRUCT
:
3702 *pos
+= 4 + BYTES_TO_EXP_ELEM (exp
->elts
[pc
+ 1].longconst
+ 1);
3715 error (_("Unexpected operator during name resolution"));
3718 argvec
= XALLOCAVEC (struct value
*, nargs
+ 1);
3719 for (i
= 0; i
< nargs
; i
+= 1)
3721 struct type
*subtype
= nullptr;
3722 if (i
< array_arity
)
3723 subtype
= ada_index_type (array_type
, i
+ 1, "array type");
3724 argvec
[i
] = resolve_subexp (expp
, pos
, 1, subtype
, parse_completion
,
3730 /* Pass two: perform any resolution on principal operator. */
3737 if (SYMBOL_DOMAIN (exp
->elts
[pc
+ 2].symbol
) == UNDEF_DOMAIN
)
3739 block_symbol resolved
3740 = ada_resolve_variable (exp
->elts
[pc
+ 2].symbol
,
3741 exp
->elts
[pc
+ 1].block
,
3742 context_type
, parse_completion
,
3743 deprocedure_p
, tracker
);
3744 exp
->elts
[pc
+ 1].block
= resolved
.block
;
3745 exp
->elts
[pc
+ 2].symbol
= resolved
.symbol
;
3749 && (SYMBOL_TYPE (exp
->elts
[pc
+ 2].symbol
)->code ()
3752 replace_operator_with_call (expp
, pc
, 0, 4,
3753 exp
->elts
[pc
+ 2].symbol
,
3754 exp
->elts
[pc
+ 1].block
);
3761 if (exp
->elts
[pc
+ 3].opcode
== OP_VAR_VALUE
3762 && SYMBOL_DOMAIN (exp
->elts
[pc
+ 5].symbol
) == UNDEF_DOMAIN
)
3764 block_symbol resolved
3765 = ada_resolve_funcall (exp
->elts
[pc
+ 5].symbol
,
3766 exp
->elts
[pc
+ 4].block
,
3767 context_type
, parse_completion
,
3770 exp
->elts
[pc
+ 4].block
= resolved
.block
;
3771 exp
->elts
[pc
+ 5].symbol
= resolved
.symbol
;
3782 case BINOP_BITWISE_AND
:
3783 case BINOP_BITWISE_IOR
:
3784 case BINOP_BITWISE_XOR
:
3786 case BINOP_NOTEQUAL
:
3794 case UNOP_LOGICAL_NOT
:
3797 block_symbol found
= ada_find_operator_symbol (op
, parse_completion
,
3799 if (found
.symbol
== nullptr)
3802 replace_operator_with_call (expp
, pc
, nargs
, 1,
3803 found
.symbol
, found
.block
);
3814 if (exp
->elts
[pc
].opcode
== OP_VAR_MSYM_VALUE
)
3815 return evaluate_var_msym_value (EVAL_AVOID_SIDE_EFFECTS
,
3816 exp
->elts
[pc
+ 1].objfile
,
3817 exp
->elts
[pc
+ 2].msymbol
);
3819 return evaluate_subexp_type (exp
, pos
);
3822 /* Return non-zero if formal type FTYPE matches actual type ATYPE. If
3823 MAY_DEREF is non-zero, the formal may be a pointer and the actual
3825 /* The term "match" here is rather loose. The match is heuristic and
3829 ada_type_match (struct type
*ftype
, struct type
*atype
, int may_deref
)
3831 ftype
= ada_check_typedef (ftype
);
3832 atype
= ada_check_typedef (atype
);
3834 if (ftype
->code () == TYPE_CODE_REF
)
3835 ftype
= TYPE_TARGET_TYPE (ftype
);
3836 if (atype
->code () == TYPE_CODE_REF
)
3837 atype
= TYPE_TARGET_TYPE (atype
);
3839 switch (ftype
->code ())
3842 return ftype
->code () == atype
->code ();
3844 if (atype
->code () == TYPE_CODE_PTR
)
3845 return ada_type_match (TYPE_TARGET_TYPE (ftype
),
3846 TYPE_TARGET_TYPE (atype
), 0);
3849 && ada_type_match (TYPE_TARGET_TYPE (ftype
), atype
, 0));
3851 case TYPE_CODE_ENUM
:
3852 case TYPE_CODE_RANGE
:
3853 switch (atype
->code ())
3856 case TYPE_CODE_ENUM
:
3857 case TYPE_CODE_RANGE
:
3863 case TYPE_CODE_ARRAY
:
3864 return (atype
->code () == TYPE_CODE_ARRAY
3865 || ada_is_array_descriptor_type (atype
));
3867 case TYPE_CODE_STRUCT
:
3868 if (ada_is_array_descriptor_type (ftype
))
3869 return (atype
->code () == TYPE_CODE_ARRAY
3870 || ada_is_array_descriptor_type (atype
));
3872 return (atype
->code () == TYPE_CODE_STRUCT
3873 && !ada_is_array_descriptor_type (atype
));
3875 case TYPE_CODE_UNION
:
3877 return (atype
->code () == ftype
->code ());
3881 /* Return non-zero if the formals of FUNC "sufficiently match" the
3882 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
3883 may also be an enumeral, in which case it is treated as a 0-
3884 argument function. */
3887 ada_args_match (struct symbol
*func
, struct value
**actuals
, int n_actuals
)
3890 struct type
*func_type
= SYMBOL_TYPE (func
);
3892 if (SYMBOL_CLASS (func
) == LOC_CONST
3893 && func_type
->code () == TYPE_CODE_ENUM
)
3894 return (n_actuals
== 0);
3895 else if (func_type
== NULL
|| func_type
->code () != TYPE_CODE_FUNC
)
3898 if (func_type
->num_fields () != n_actuals
)
3901 for (i
= 0; i
< n_actuals
; i
+= 1)
3903 if (actuals
[i
] == NULL
)
3907 struct type
*ftype
= ada_check_typedef (func_type
->field (i
).type ());
3908 struct type
*atype
= ada_check_typedef (value_type (actuals
[i
]));
3910 if (!ada_type_match (ftype
, atype
, 1))
3917 /* False iff function type FUNC_TYPE definitely does not produce a value
3918 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
3919 FUNC_TYPE is not a valid function type with a non-null return type
3920 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
3923 return_match (struct type
*func_type
, struct type
*context_type
)
3925 struct type
*return_type
;
3927 if (func_type
== NULL
)
3930 if (func_type
->code () == TYPE_CODE_FUNC
)
3931 return_type
= get_base_type (TYPE_TARGET_TYPE (func_type
));
3933 return_type
= get_base_type (func_type
);
3934 if (return_type
== NULL
)
3937 context_type
= get_base_type (context_type
);
3939 if (return_type
->code () == TYPE_CODE_ENUM
)
3940 return context_type
== NULL
|| return_type
== context_type
;
3941 else if (context_type
== NULL
)
3942 return return_type
->code () != TYPE_CODE_VOID
;
3944 return return_type
->code () == context_type
->code ();
3948 /* Returns the index in SYMS that contains the symbol for the
3949 function (if any) that matches the types of the NARGS arguments in
3950 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
3951 that returns that type, then eliminate matches that don't. If
3952 CONTEXT_TYPE is void and there is at least one match that does not
3953 return void, eliminate all matches that do.
3955 Asks the user if there is more than one match remaining. Returns -1
3956 if there is no such symbol or none is selected. NAME is used
3957 solely for messages. May re-arrange and modify SYMS in
3958 the process; the index returned is for the modified vector. */
3961 ada_resolve_function (std::vector
<struct block_symbol
> &syms
,
3962 struct value
**args
, int nargs
,
3963 const char *name
, struct type
*context_type
,
3964 int parse_completion
)
3968 int m
; /* Number of hits */
3971 /* In the first pass of the loop, we only accept functions matching
3972 context_type. If none are found, we add a second pass of the loop
3973 where every function is accepted. */
3974 for (fallback
= 0; m
== 0 && fallback
< 2; fallback
++)
3976 for (k
= 0; k
< syms
.size (); k
+= 1)
3978 struct type
*type
= ada_check_typedef (SYMBOL_TYPE (syms
[k
].symbol
));
3980 if (ada_args_match (syms
[k
].symbol
, args
, nargs
)
3981 && (fallback
|| return_match (type
, context_type
)))
3989 /* If we got multiple matches, ask the user which one to use. Don't do this
3990 interactive thing during completion, though, as the purpose of the
3991 completion is providing a list of all possible matches. Prompting the
3992 user to filter it down would be completely unexpected in this case. */
3995 else if (m
> 1 && !parse_completion
)
3997 printf_filtered (_("Multiple matches for %s\n"), name
);
3998 user_select_syms (syms
.data (), m
, 1);
4004 /* Replace the operator of length OPLEN at position PC in *EXPP with a call
4005 on the function identified by SYM and BLOCK, and taking NARGS
4006 arguments. Update *EXPP as needed to hold more space. */
4009 replace_operator_with_call (expression_up
*expp
, int pc
, int nargs
,
4010 int oplen
, struct symbol
*sym
,
4011 const struct block
*block
)
4013 /* We want to add 6 more elements (3 for funcall, 4 for function
4014 symbol, -OPLEN for operator being replaced) to the
4016 struct expression
*exp
= expp
->get ();
4017 int save_nelts
= exp
->nelts
;
4018 int extra_elts
= 7 - oplen
;
4019 exp
->nelts
+= extra_elts
;
4022 exp
->resize (exp
->nelts
);
4023 memmove (exp
->elts
+ pc
+ 7, exp
->elts
+ pc
+ oplen
,
4024 EXP_ELEM_TO_BYTES (save_nelts
- pc
- oplen
));
4026 exp
->resize (exp
->nelts
);
4028 exp
->elts
[pc
].opcode
= exp
->elts
[pc
+ 2].opcode
= OP_FUNCALL
;
4029 exp
->elts
[pc
+ 1].longconst
= (LONGEST
) nargs
;
4031 exp
->elts
[pc
+ 3].opcode
= exp
->elts
[pc
+ 6].opcode
= OP_VAR_VALUE
;
4032 exp
->elts
[pc
+ 4].block
= block
;
4033 exp
->elts
[pc
+ 5].symbol
= sym
;
4036 /* Type-class predicates */
4038 /* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
4042 numeric_type_p (struct type
*type
)
4048 switch (type
->code ())
4053 case TYPE_CODE_RANGE
:
4054 return (type
== TYPE_TARGET_TYPE (type
)
4055 || numeric_type_p (TYPE_TARGET_TYPE (type
)));
4062 /* True iff TYPE is integral (an INT or RANGE of INTs). */
4065 integer_type_p (struct type
*type
)
4071 switch (type
->code ())
4075 case TYPE_CODE_RANGE
:
4076 return (type
== TYPE_TARGET_TYPE (type
)
4077 || integer_type_p (TYPE_TARGET_TYPE (type
)));
4084 /* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
4087 scalar_type_p (struct type
*type
)
4093 switch (type
->code ())
4096 case TYPE_CODE_RANGE
:
4097 case TYPE_CODE_ENUM
:
4106 /* True iff TYPE is discrete (INT, RANGE, ENUM). */
4109 discrete_type_p (struct type
*type
)
4115 switch (type
->code ())
4118 case TYPE_CODE_RANGE
:
4119 case TYPE_CODE_ENUM
:
4120 case TYPE_CODE_BOOL
:
4128 /* Returns non-zero if OP with operands in the vector ARGS could be
4129 a user-defined function. Errs on the side of pre-defined operators
4130 (i.e., result 0). */
4133 possible_user_operator_p (enum exp_opcode op
, struct value
*args
[])
4135 struct type
*type0
=
4136 (args
[0] == NULL
) ? NULL
: ada_check_typedef (value_type (args
[0]));
4137 struct type
*type1
=
4138 (args
[1] == NULL
) ? NULL
: ada_check_typedef (value_type (args
[1]));
4152 return (!(numeric_type_p (type0
) && numeric_type_p (type1
)));
4156 case BINOP_BITWISE_AND
:
4157 case BINOP_BITWISE_IOR
:
4158 case BINOP_BITWISE_XOR
:
4159 return (!(integer_type_p (type0
) && integer_type_p (type1
)));
4162 case BINOP_NOTEQUAL
:
4167 return (!(scalar_type_p (type0
) && scalar_type_p (type1
)));
4170 return !ada_is_array_type (type0
) || !ada_is_array_type (type1
);
4173 return (!(numeric_type_p (type0
) && integer_type_p (type1
)));
4177 case UNOP_LOGICAL_NOT
:
4179 return (!numeric_type_p (type0
));
4188 1. In the following, we assume that a renaming type's name may
4189 have an ___XD suffix. It would be nice if this went away at some
4191 2. We handle both the (old) purely type-based representation of
4192 renamings and the (new) variable-based encoding. At some point,
4193 it is devoutly to be hoped that the former goes away
4194 (FIXME: hilfinger-2007-07-09).
4195 3. Subprogram renamings are not implemented, although the XRS
4196 suffix is recognized (FIXME: hilfinger-2007-07-09). */
4198 /* If SYM encodes a renaming,
4200 <renaming> renames <renamed entity>,
4202 sets *LEN to the length of the renamed entity's name,
4203 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
4204 the string describing the subcomponent selected from the renamed
4205 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
4206 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
4207 are undefined). Otherwise, returns a value indicating the category
4208 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
4209 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
4210 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
4211 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
4212 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
4213 may be NULL, in which case they are not assigned.
4215 [Currently, however, GCC does not generate subprogram renamings.] */
4217 enum ada_renaming_category
4218 ada_parse_renaming (struct symbol
*sym
,
4219 const char **renamed_entity
, int *len
,
4220 const char **renaming_expr
)
4222 enum ada_renaming_category kind
;
4227 return ADA_NOT_RENAMING
;
4228 switch (SYMBOL_CLASS (sym
))
4231 return ADA_NOT_RENAMING
;
4235 case LOC_OPTIMIZED_OUT
:
4236 info
= strstr (sym
->linkage_name (), "___XR");
4238 return ADA_NOT_RENAMING
;
4242 kind
= ADA_OBJECT_RENAMING
;
4246 kind
= ADA_EXCEPTION_RENAMING
;
4250 kind
= ADA_PACKAGE_RENAMING
;
4254 kind
= ADA_SUBPROGRAM_RENAMING
;
4258 return ADA_NOT_RENAMING
;
4262 if (renamed_entity
!= NULL
)
4263 *renamed_entity
= info
;
4264 suffix
= strstr (info
, "___XE");
4265 if (suffix
== NULL
|| suffix
== info
)
4266 return ADA_NOT_RENAMING
;
4268 *len
= strlen (info
) - strlen (suffix
);
4270 if (renaming_expr
!= NULL
)
4271 *renaming_expr
= suffix
;
4275 /* Compute the value of the given RENAMING_SYM, which is expected to
4276 be a symbol encoding a renaming expression. BLOCK is the block
4277 used to evaluate the renaming. */
4279 static struct value
*
4280 ada_read_renaming_var_value (struct symbol
*renaming_sym
,
4281 const struct block
*block
)
4283 const char *sym_name
;
4285 sym_name
= renaming_sym
->linkage_name ();
4286 expression_up expr
= parse_exp_1 (&sym_name
, 0, block
, 0);
4287 return evaluate_expression (expr
.get ());
4291 /* Evaluation: Function Calls */
4293 /* Return an lvalue containing the value VAL. This is the identity on
4294 lvalues, and otherwise has the side-effect of allocating memory
4295 in the inferior where a copy of the value contents is copied. */
4297 static struct value
*
4298 ensure_lval (struct value
*val
)
4300 if (VALUE_LVAL (val
) == not_lval
4301 || VALUE_LVAL (val
) == lval_internalvar
)
4303 int len
= TYPE_LENGTH (ada_check_typedef (value_type (val
)));
4304 const CORE_ADDR addr
=
4305 value_as_long (value_allocate_space_in_inferior (len
));
4307 VALUE_LVAL (val
) = lval_memory
;
4308 set_value_address (val
, addr
);
4309 write_memory (addr
, value_contents (val
), len
);
4315 /* Given ARG, a value of type (pointer or reference to a)*
4316 structure/union, extract the component named NAME from the ultimate
4317 target structure/union and return it as a value with its
4320 The routine searches for NAME among all members of the structure itself
4321 and (recursively) among all members of any wrapper members
4324 If NO_ERR, then simply return NULL in case of error, rather than
4327 static struct value
*
4328 ada_value_struct_elt (struct value
*arg
, const char *name
, int no_err
)
4330 struct type
*t
, *t1
;
4335 t1
= t
= ada_check_typedef (value_type (arg
));
4336 if (t
->code () == TYPE_CODE_REF
)
4338 t1
= TYPE_TARGET_TYPE (t
);
4341 t1
= ada_check_typedef (t1
);
4342 if (t1
->code () == TYPE_CODE_PTR
)
4344 arg
= coerce_ref (arg
);
4349 while (t
->code () == TYPE_CODE_PTR
)
4351 t1
= TYPE_TARGET_TYPE (t
);
4354 t1
= ada_check_typedef (t1
);
4355 if (t1
->code () == TYPE_CODE_PTR
)
4357 arg
= value_ind (arg
);
4364 if (t1
->code () != TYPE_CODE_STRUCT
&& t1
->code () != TYPE_CODE_UNION
)
4368 v
= ada_search_struct_field (name
, arg
, 0, t
);
4371 int bit_offset
, bit_size
, byte_offset
;
4372 struct type
*field_type
;
4375 if (t
->code () == TYPE_CODE_PTR
)
4376 address
= value_address (ada_value_ind (arg
));
4378 address
= value_address (ada_coerce_ref (arg
));
4380 /* Check to see if this is a tagged type. We also need to handle
4381 the case where the type is a reference to a tagged type, but
4382 we have to be careful to exclude pointers to tagged types.
4383 The latter should be shown as usual (as a pointer), whereas
4384 a reference should mostly be transparent to the user. */
4386 if (ada_is_tagged_type (t1
, 0)
4387 || (t1
->code () == TYPE_CODE_REF
4388 && ada_is_tagged_type (TYPE_TARGET_TYPE (t1
), 0)))
4390 /* We first try to find the searched field in the current type.
4391 If not found then let's look in the fixed type. */
4393 if (!find_struct_field (name
, t1
, 0,
4394 &field_type
, &byte_offset
, &bit_offset
,
4403 /* Convert to fixed type in all cases, so that we have proper
4404 offsets to each field in unconstrained record types. */
4405 t1
= ada_to_fixed_type (ada_get_base_type (t1
), NULL
,
4406 address
, NULL
, check_tag
);
4408 /* Resolve the dynamic type as well. */
4409 arg
= value_from_contents_and_address (t1
, nullptr, address
);
4410 t1
= value_type (arg
);
4412 if (find_struct_field (name
, t1
, 0,
4413 &field_type
, &byte_offset
, &bit_offset
,
4418 if (t
->code () == TYPE_CODE_REF
)
4419 arg
= ada_coerce_ref (arg
);
4421 arg
= ada_value_ind (arg
);
4422 v
= ada_value_primitive_packed_val (arg
, NULL
, byte_offset
,
4423 bit_offset
, bit_size
,
4427 v
= value_at_lazy (field_type
, address
+ byte_offset
);
4431 if (v
!= NULL
|| no_err
)
4434 error (_("There is no member named %s."), name
);
4440 error (_("Attempt to extract a component of "
4441 "a value that is not a record."));
4444 /* Return the value ACTUAL, converted to be an appropriate value for a
4445 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
4446 allocating any necessary descriptors (fat pointers), or copies of
4447 values not residing in memory, updating it as needed. */
4450 ada_convert_actual (struct value
*actual
, struct type
*formal_type0
)
4452 struct type
*actual_type
= ada_check_typedef (value_type (actual
));
4453 struct type
*formal_type
= ada_check_typedef (formal_type0
);
4454 struct type
*formal_target
=
4455 formal_type
->code () == TYPE_CODE_PTR
4456 ? ada_check_typedef (TYPE_TARGET_TYPE (formal_type
)) : formal_type
;
4457 struct type
*actual_target
=
4458 actual_type
->code () == TYPE_CODE_PTR
4459 ? ada_check_typedef (TYPE_TARGET_TYPE (actual_type
)) : actual_type
;
4461 if (ada_is_array_descriptor_type (formal_target
)
4462 && actual_target
->code () == TYPE_CODE_ARRAY
)
4463 return make_array_descriptor (formal_type
, actual
);
4464 else if (formal_type
->code () == TYPE_CODE_PTR
4465 || formal_type
->code () == TYPE_CODE_REF
)
4467 struct value
*result
;
4469 if (formal_target
->code () == TYPE_CODE_ARRAY
4470 && ada_is_array_descriptor_type (actual_target
))
4471 result
= desc_data (actual
);
4472 else if (formal_type
->code () != TYPE_CODE_PTR
)
4474 if (VALUE_LVAL (actual
) != lval_memory
)
4478 actual_type
= ada_check_typedef (value_type (actual
));
4479 val
= allocate_value (actual_type
);
4480 memcpy ((char *) value_contents_raw (val
),
4481 (char *) value_contents (actual
),
4482 TYPE_LENGTH (actual_type
));
4483 actual
= ensure_lval (val
);
4485 result
= value_addr (actual
);
4489 return value_cast_pointers (formal_type
, result
, 0);
4491 else if (actual_type
->code () == TYPE_CODE_PTR
)
4492 return ada_value_ind (actual
);
4493 else if (ada_is_aligner_type (formal_type
))
4495 /* We need to turn this parameter into an aligner type
4497 struct value
*aligner
= allocate_value (formal_type
);
4498 struct value
*component
= ada_value_struct_elt (aligner
, "F", 0);
4500 value_assign_to_component (aligner
, component
, actual
);
4507 /* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4508 type TYPE. This is usually an inefficient no-op except on some targets
4509 (such as AVR) where the representation of a pointer and an address
4513 value_pointer (struct value
*value
, struct type
*type
)
4515 unsigned len
= TYPE_LENGTH (type
);
4516 gdb_byte
*buf
= (gdb_byte
*) alloca (len
);
4519 addr
= value_address (value
);
4520 gdbarch_address_to_pointer (type
->arch (), type
, buf
, addr
);
4521 addr
= extract_unsigned_integer (buf
, len
, type_byte_order (type
));
4526 /* Push a descriptor of type TYPE for array value ARR on the stack at
4527 *SP, updating *SP to reflect the new descriptor. Return either
4528 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4529 to-descriptor type rather than a descriptor type), a struct value *
4530 representing a pointer to this descriptor. */
4532 static struct value
*
4533 make_array_descriptor (struct type
*type
, struct value
*arr
)
4535 struct type
*bounds_type
= desc_bounds_type (type
);
4536 struct type
*desc_type
= desc_base_type (type
);
4537 struct value
*descriptor
= allocate_value (desc_type
);
4538 struct value
*bounds
= allocate_value (bounds_type
);
4541 for (i
= ada_array_arity (ada_check_typedef (value_type (arr
)));
4544 modify_field (value_type (bounds
), value_contents_writeable (bounds
),
4545 ada_array_bound (arr
, i
, 0),
4546 desc_bound_bitpos (bounds_type
, i
, 0),
4547 desc_bound_bitsize (bounds_type
, i
, 0));
4548 modify_field (value_type (bounds
), value_contents_writeable (bounds
),
4549 ada_array_bound (arr
, i
, 1),
4550 desc_bound_bitpos (bounds_type
, i
, 1),
4551 desc_bound_bitsize (bounds_type
, i
, 1));
4554 bounds
= ensure_lval (bounds
);
4556 modify_field (value_type (descriptor
),
4557 value_contents_writeable (descriptor
),
4558 value_pointer (ensure_lval (arr
),
4559 desc_type
->field (0).type ()),
4560 fat_pntr_data_bitpos (desc_type
),
4561 fat_pntr_data_bitsize (desc_type
));
4563 modify_field (value_type (descriptor
),
4564 value_contents_writeable (descriptor
),
4565 value_pointer (bounds
,
4566 desc_type
->field (1).type ()),
4567 fat_pntr_bounds_bitpos (desc_type
),
4568 fat_pntr_bounds_bitsize (desc_type
));
4570 descriptor
= ensure_lval (descriptor
);
4572 if (type
->code () == TYPE_CODE_PTR
)
4573 return value_addr (descriptor
);
4578 /* Symbol Cache Module */
4580 /* Performance measurements made as of 2010-01-15 indicate that
4581 this cache does bring some noticeable improvements. Depending
4582 on the type of entity being printed, the cache can make it as much
4583 as an order of magnitude faster than without it.
4585 The descriptive type DWARF extension has significantly reduced
4586 the need for this cache, at least when DWARF is being used. However,
4587 even in this case, some expensive name-based symbol searches are still
4588 sometimes necessary - to find an XVZ variable, mostly. */
4590 /* Return the symbol cache associated to the given program space PSPACE.
4591 If not allocated for this PSPACE yet, allocate and initialize one. */
4593 static struct ada_symbol_cache
*
4594 ada_get_symbol_cache (struct program_space
*pspace
)
4596 struct ada_pspace_data
*pspace_data
= get_ada_pspace_data (pspace
);
4598 if (pspace_data
->sym_cache
== nullptr)
4599 pspace_data
->sym_cache
.reset (new ada_symbol_cache
);
4601 return pspace_data
->sym_cache
.get ();
4604 /* Clear all entries from the symbol cache. */
4607 ada_clear_symbol_cache ()
4609 struct ada_pspace_data
*pspace_data
4610 = get_ada_pspace_data (current_program_space
);
4612 if (pspace_data
->sym_cache
!= nullptr)
4613 pspace_data
->sym_cache
.reset ();
4616 /* Search our cache for an entry matching NAME and DOMAIN.
4617 Return it if found, or NULL otherwise. */
4619 static struct cache_entry
**
4620 find_entry (const char *name
, domain_enum domain
)
4622 struct ada_symbol_cache
*sym_cache
4623 = ada_get_symbol_cache (current_program_space
);
4624 int h
= msymbol_hash (name
) % HASH_SIZE
;
4625 struct cache_entry
**e
;
4627 for (e
= &sym_cache
->root
[h
]; *e
!= NULL
; e
= &(*e
)->next
)
4629 if (domain
== (*e
)->domain
&& strcmp (name
, (*e
)->name
) == 0)
4635 /* Search the symbol cache for an entry matching NAME and DOMAIN.
4636 Return 1 if found, 0 otherwise.
4638 If an entry was found and SYM is not NULL, set *SYM to the entry's
4639 SYM. Same principle for BLOCK if not NULL. */
4642 lookup_cached_symbol (const char *name
, domain_enum domain
,
4643 struct symbol
**sym
, const struct block
**block
)
4645 struct cache_entry
**e
= find_entry (name
, domain
);
4652 *block
= (*e
)->block
;
4656 /* Assuming that (SYM, BLOCK) is the result of the lookup of NAME
4657 in domain DOMAIN, save this result in our symbol cache. */
4660 cache_symbol (const char *name
, domain_enum domain
, struct symbol
*sym
,
4661 const struct block
*block
)
4663 struct ada_symbol_cache
*sym_cache
4664 = ada_get_symbol_cache (current_program_space
);
4666 struct cache_entry
*e
;
4668 /* Symbols for builtin types don't have a block.
4669 For now don't cache such symbols. */
4670 if (sym
!= NULL
&& !SYMBOL_OBJFILE_OWNED (sym
))
4673 /* If the symbol is a local symbol, then do not cache it, as a search
4674 for that symbol depends on the context. To determine whether
4675 the symbol is local or not, we check the block where we found it
4676 against the global and static blocks of its associated symtab. */
4678 && BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symbol_symtab (sym
)),
4679 GLOBAL_BLOCK
) != block
4680 && BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symbol_symtab (sym
)),
4681 STATIC_BLOCK
) != block
)
4684 h
= msymbol_hash (name
) % HASH_SIZE
;
4685 e
= XOBNEW (&sym_cache
->cache_space
, cache_entry
);
4686 e
->next
= sym_cache
->root
[h
];
4687 sym_cache
->root
[h
] = e
;
4688 e
->name
= obstack_strdup (&sym_cache
->cache_space
, name
);
4696 /* Return the symbol name match type that should be used used when
4697 searching for all symbols matching LOOKUP_NAME.
4699 LOOKUP_NAME is expected to be a symbol name after transformation
4702 static symbol_name_match_type
4703 name_match_type_from_name (const char *lookup_name
)
4705 return (strstr (lookup_name
, "__") == NULL
4706 ? symbol_name_match_type::WILD
4707 : symbol_name_match_type::FULL
);
4710 /* Return the result of a standard (literal, C-like) lookup of NAME in
4711 given DOMAIN, visible from lexical block BLOCK. */
4713 static struct symbol
*
4714 standard_lookup (const char *name
, const struct block
*block
,
4717 /* Initialize it just to avoid a GCC false warning. */
4718 struct block_symbol sym
= {};
4720 if (lookup_cached_symbol (name
, domain
, &sym
.symbol
, NULL
))
4722 ada_lookup_encoded_symbol (name
, block
, domain
, &sym
);
4723 cache_symbol (name
, domain
, sym
.symbol
, sym
.block
);
4728 /* Non-zero iff there is at least one non-function/non-enumeral symbol
4729 in the symbol fields of SYMS. We treat enumerals as functions,
4730 since they contend in overloading in the same way. */
4732 is_nonfunction (const std::vector
<struct block_symbol
> &syms
)
4734 for (const block_symbol
&sym
: syms
)
4735 if (SYMBOL_TYPE (sym
.symbol
)->code () != TYPE_CODE_FUNC
4736 && (SYMBOL_TYPE (sym
.symbol
)->code () != TYPE_CODE_ENUM
4737 || SYMBOL_CLASS (sym
.symbol
) != LOC_CONST
))
4743 /* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4744 struct types. Otherwise, they may not. */
4747 equiv_types (struct type
*type0
, struct type
*type1
)
4751 if (type0
== NULL
|| type1
== NULL
4752 || type0
->code () != type1
->code ())
4754 if ((type0
->code () == TYPE_CODE_STRUCT
4755 || type0
->code () == TYPE_CODE_ENUM
)
4756 && ada_type_name (type0
) != NULL
&& ada_type_name (type1
) != NULL
4757 && strcmp (ada_type_name (type0
), ada_type_name (type1
)) == 0)
4763 /* True iff SYM0 represents the same entity as SYM1, or one that is
4764 no more defined than that of SYM1. */
4767 lesseq_defined_than (struct symbol
*sym0
, struct symbol
*sym1
)
4771 if (SYMBOL_DOMAIN (sym0
) != SYMBOL_DOMAIN (sym1
)
4772 || SYMBOL_CLASS (sym0
) != SYMBOL_CLASS (sym1
))
4775 switch (SYMBOL_CLASS (sym0
))
4781 struct type
*type0
= SYMBOL_TYPE (sym0
);
4782 struct type
*type1
= SYMBOL_TYPE (sym1
);
4783 const char *name0
= sym0
->linkage_name ();
4784 const char *name1
= sym1
->linkage_name ();
4785 int len0
= strlen (name0
);
4788 type0
->code () == type1
->code ()
4789 && (equiv_types (type0
, type1
)
4790 || (len0
< strlen (name1
) && strncmp (name0
, name1
, len0
) == 0
4791 && startswith (name1
+ len0
, "___XV")));
4794 return SYMBOL_VALUE (sym0
) == SYMBOL_VALUE (sym1
)
4795 && equiv_types (SYMBOL_TYPE (sym0
), SYMBOL_TYPE (sym1
));
4799 const char *name0
= sym0
->linkage_name ();
4800 const char *name1
= sym1
->linkage_name ();
4801 return (strcmp (name0
, name1
) == 0
4802 && SYMBOL_VALUE_ADDRESS (sym0
) == SYMBOL_VALUE_ADDRESS (sym1
));
4810 /* Append (SYM,BLOCK) to the end of the array of struct block_symbol
4811 records in RESULT. Do nothing if SYM is a duplicate. */
4814 add_defn_to_vec (std::vector
<struct block_symbol
> &result
,
4816 const struct block
*block
)
4818 /* Do not try to complete stub types, as the debugger is probably
4819 already scanning all symbols matching a certain name at the
4820 time when this function is called. Trying to replace the stub
4821 type by its associated full type will cause us to restart a scan
4822 which may lead to an infinite recursion. Instead, the client
4823 collecting the matching symbols will end up collecting several
4824 matches, with at least one of them complete. It can then filter
4825 out the stub ones if needed. */
4827 for (int i
= result
.size () - 1; i
>= 0; i
-= 1)
4829 if (lesseq_defined_than (sym
, result
[i
].symbol
))
4831 else if (lesseq_defined_than (result
[i
].symbol
, sym
))
4833 result
[i
].symbol
= sym
;
4834 result
[i
].block
= block
;
4839 struct block_symbol info
;
4842 result
.push_back (info
);
4845 /* Return a bound minimal symbol matching NAME according to Ada
4846 decoding rules. Returns an invalid symbol if there is no such
4847 minimal symbol. Names prefixed with "standard__" are handled
4848 specially: "standard__" is first stripped off, and only static and
4849 global symbols are searched. */
4851 struct bound_minimal_symbol
4852 ada_lookup_simple_minsym (const char *name
)
4854 struct bound_minimal_symbol result
;
4856 memset (&result
, 0, sizeof (result
));
4858 symbol_name_match_type match_type
= name_match_type_from_name (name
);
4859 lookup_name_info
lookup_name (name
, match_type
);
4861 symbol_name_matcher_ftype
*match_name
4862 = ada_get_symbol_name_matcher (lookup_name
);
4864 for (objfile
*objfile
: current_program_space
->objfiles ())
4866 for (minimal_symbol
*msymbol
: objfile
->msymbols ())
4868 if (match_name (msymbol
->linkage_name (), lookup_name
, NULL
)
4869 && MSYMBOL_TYPE (msymbol
) != mst_solib_trampoline
)
4871 result
.minsym
= msymbol
;
4872 result
.objfile
= objfile
;
4881 /* For all subprograms that statically enclose the subprogram of the
4882 selected frame, add symbols matching identifier NAME in DOMAIN
4883 and their blocks to the list of data in RESULT, as for
4884 ada_add_block_symbols (q.v.). If WILD_MATCH_P, treat as NAME
4885 with a wildcard prefix. */
4888 add_symbols_from_enclosing_procs (std::vector
<struct block_symbol
> &result
,
4889 const lookup_name_info
&lookup_name
,
4894 /* True if TYPE is definitely an artificial type supplied to a symbol
4895 for which no debugging information was given in the symbol file. */
4898 is_nondebugging_type (struct type
*type
)
4900 const char *name
= ada_type_name (type
);
4902 return (name
!= NULL
&& strcmp (name
, "<variable, no debug info>") == 0);
4905 /* Return nonzero if TYPE1 and TYPE2 are two enumeration types
4906 that are deemed "identical" for practical purposes.
4908 This function assumes that TYPE1 and TYPE2 are both TYPE_CODE_ENUM
4909 types and that their number of enumerals is identical (in other
4910 words, type1->num_fields () == type2->num_fields ()). */
4913 ada_identical_enum_types_p (struct type
*type1
, struct type
*type2
)
4917 /* The heuristic we use here is fairly conservative. We consider
4918 that 2 enumerate types are identical if they have the same
4919 number of enumerals and that all enumerals have the same
4920 underlying value and name. */
4922 /* All enums in the type should have an identical underlying value. */
4923 for (i
= 0; i
< type1
->num_fields (); i
++)
4924 if (TYPE_FIELD_ENUMVAL (type1
, i
) != TYPE_FIELD_ENUMVAL (type2
, i
))
4927 /* All enumerals should also have the same name (modulo any numerical
4929 for (i
= 0; i
< type1
->num_fields (); i
++)
4931 const char *name_1
= TYPE_FIELD_NAME (type1
, i
);
4932 const char *name_2
= TYPE_FIELD_NAME (type2
, i
);
4933 int len_1
= strlen (name_1
);
4934 int len_2
= strlen (name_2
);
4936 ada_remove_trailing_digits (TYPE_FIELD_NAME (type1
, i
), &len_1
);
4937 ada_remove_trailing_digits (TYPE_FIELD_NAME (type2
, i
), &len_2
);
4939 || strncmp (TYPE_FIELD_NAME (type1
, i
),
4940 TYPE_FIELD_NAME (type2
, i
),
4948 /* Return nonzero if all the symbols in SYMS are all enumeral symbols
4949 that are deemed "identical" for practical purposes. Sometimes,
4950 enumerals are not strictly identical, but their types are so similar
4951 that they can be considered identical.
4953 For instance, consider the following code:
4955 type Color is (Black, Red, Green, Blue, White);
4956 type RGB_Color is new Color range Red .. Blue;
4958 Type RGB_Color is a subrange of an implicit type which is a copy
4959 of type Color. If we call that implicit type RGB_ColorB ("B" is
4960 for "Base Type"), then type RGB_ColorB is a copy of type Color.
4961 As a result, when an expression references any of the enumeral
4962 by name (Eg. "print green"), the expression is technically
4963 ambiguous and the user should be asked to disambiguate. But
4964 doing so would only hinder the user, since it wouldn't matter
4965 what choice he makes, the outcome would always be the same.
4966 So, for practical purposes, we consider them as the same. */
4969 symbols_are_identical_enums (const std::vector
<struct block_symbol
> &syms
)
4973 /* Before performing a thorough comparison check of each type,
4974 we perform a series of inexpensive checks. We expect that these
4975 checks will quickly fail in the vast majority of cases, and thus
4976 help prevent the unnecessary use of a more expensive comparison.
4977 Said comparison also expects us to make some of these checks
4978 (see ada_identical_enum_types_p). */
4980 /* Quick check: All symbols should have an enum type. */
4981 for (i
= 0; i
< syms
.size (); i
++)
4982 if (SYMBOL_TYPE (syms
[i
].symbol
)->code () != TYPE_CODE_ENUM
)
4985 /* Quick check: They should all have the same value. */
4986 for (i
= 1; i
< syms
.size (); i
++)
4987 if (SYMBOL_VALUE (syms
[i
].symbol
) != SYMBOL_VALUE (syms
[0].symbol
))
4990 /* Quick check: They should all have the same number of enumerals. */
4991 for (i
= 1; i
< syms
.size (); i
++)
4992 if (SYMBOL_TYPE (syms
[i
].symbol
)->num_fields ()
4993 != SYMBOL_TYPE (syms
[0].symbol
)->num_fields ())
4996 /* All the sanity checks passed, so we might have a set of
4997 identical enumeration types. Perform a more complete
4998 comparison of the type of each symbol. */
4999 for (i
= 1; i
< syms
.size (); i
++)
5000 if (!ada_identical_enum_types_p (SYMBOL_TYPE (syms
[i
].symbol
),
5001 SYMBOL_TYPE (syms
[0].symbol
)))
5007 /* Remove any non-debugging symbols in SYMS that definitely
5008 duplicate other symbols in the list (The only case I know of where
5009 this happens is when object files containing stabs-in-ecoff are
5010 linked with files containing ordinary ecoff debugging symbols (or no
5011 debugging symbols)). Modifies SYMS to squeeze out deleted entries. */
5014 remove_extra_symbols (std::vector
<struct block_symbol
> *syms
)
5018 /* We should never be called with less than 2 symbols, as there
5019 cannot be any extra symbol in that case. But it's easy to
5020 handle, since we have nothing to do in that case. */
5021 if (syms
->size () < 2)
5025 while (i
< syms
->size ())
5029 /* If two symbols have the same name and one of them is a stub type,
5030 the get rid of the stub. */
5032 if (SYMBOL_TYPE ((*syms
)[i
].symbol
)->is_stub ()
5033 && (*syms
)[i
].symbol
->linkage_name () != NULL
)
5035 for (j
= 0; j
< syms
->size (); j
++)
5038 && !SYMBOL_TYPE ((*syms
)[j
].symbol
)->is_stub ()
5039 && (*syms
)[j
].symbol
->linkage_name () != NULL
5040 && strcmp ((*syms
)[i
].symbol
->linkage_name (),
5041 (*syms
)[j
].symbol
->linkage_name ()) == 0)
5046 /* Two symbols with the same name, same class and same address
5047 should be identical. */
5049 else if ((*syms
)[i
].symbol
->linkage_name () != NULL
5050 && SYMBOL_CLASS ((*syms
)[i
].symbol
) == LOC_STATIC
5051 && is_nondebugging_type (SYMBOL_TYPE ((*syms
)[i
].symbol
)))
5053 for (j
= 0; j
< syms
->size (); j
+= 1)
5056 && (*syms
)[j
].symbol
->linkage_name () != NULL
5057 && strcmp ((*syms
)[i
].symbol
->linkage_name (),
5058 (*syms
)[j
].symbol
->linkage_name ()) == 0
5059 && SYMBOL_CLASS ((*syms
)[i
].symbol
)
5060 == SYMBOL_CLASS ((*syms
)[j
].symbol
)
5061 && SYMBOL_VALUE_ADDRESS ((*syms
)[i
].symbol
)
5062 == SYMBOL_VALUE_ADDRESS ((*syms
)[j
].symbol
))
5068 syms
->erase (syms
->begin () + i
);
5073 /* If all the remaining symbols are identical enumerals, then
5074 just keep the first one and discard the rest.
5076 Unlike what we did previously, we do not discard any entry
5077 unless they are ALL identical. This is because the symbol
5078 comparison is not a strict comparison, but rather a practical
5079 comparison. If all symbols are considered identical, then
5080 we can just go ahead and use the first one and discard the rest.
5081 But if we cannot reduce the list to a single element, we have
5082 to ask the user to disambiguate anyways. And if we have to
5083 present a multiple-choice menu, it's less confusing if the list
5084 isn't missing some choices that were identical and yet distinct. */
5085 if (symbols_are_identical_enums (*syms
))
5089 /* Given a type that corresponds to a renaming entity, use the type name
5090 to extract the scope (package name or function name, fully qualified,
5091 and following the GNAT encoding convention) where this renaming has been
5095 xget_renaming_scope (struct type
*renaming_type
)
5097 /* The renaming types adhere to the following convention:
5098 <scope>__<rename>___<XR extension>.
5099 So, to extract the scope, we search for the "___XR" extension,
5100 and then backtrack until we find the first "__". */
5102 const char *name
= renaming_type
->name ();
5103 const char *suffix
= strstr (name
, "___XR");
5106 /* Now, backtrack a bit until we find the first "__". Start looking
5107 at suffix - 3, as the <rename> part is at least one character long. */
5109 for (last
= suffix
- 3; last
> name
; last
--)
5110 if (last
[0] == '_' && last
[1] == '_')
5113 /* Make a copy of scope and return it. */
5114 return std::string (name
, last
);
5117 /* Return nonzero if NAME corresponds to a package name. */
5120 is_package_name (const char *name
)
5122 /* Here, We take advantage of the fact that no symbols are generated
5123 for packages, while symbols are generated for each function.
5124 So the condition for NAME represent a package becomes equivalent
5125 to NAME not existing in our list of symbols. There is only one
5126 small complication with library-level functions (see below). */
5128 /* If it is a function that has not been defined at library level,
5129 then we should be able to look it up in the symbols. */
5130 if (standard_lookup (name
, NULL
, VAR_DOMAIN
) != NULL
)
5133 /* Library-level function names start with "_ada_". See if function
5134 "_ada_" followed by NAME can be found. */
5136 /* Do a quick check that NAME does not contain "__", since library-level
5137 functions names cannot contain "__" in them. */
5138 if (strstr (name
, "__") != NULL
)
5141 std::string fun_name
= string_printf ("_ada_%s", name
);
5143 return (standard_lookup (fun_name
.c_str (), NULL
, VAR_DOMAIN
) == NULL
);
5146 /* Return nonzero if SYM corresponds to a renaming entity that is
5147 not visible from FUNCTION_NAME. */
5150 old_renaming_is_invisible (const struct symbol
*sym
, const char *function_name
)
5152 if (SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
5155 std::string scope
= xget_renaming_scope (SYMBOL_TYPE (sym
));
5157 /* If the rename has been defined in a package, then it is visible. */
5158 if (is_package_name (scope
.c_str ()))
5161 /* Check that the rename is in the current function scope by checking
5162 that its name starts with SCOPE. */
5164 /* If the function name starts with "_ada_", it means that it is
5165 a library-level function. Strip this prefix before doing the
5166 comparison, as the encoding for the renaming does not contain
5168 if (startswith (function_name
, "_ada_"))
5171 return !startswith (function_name
, scope
.c_str ());
5174 /* Remove entries from SYMS that corresponds to a renaming entity that
5175 is not visible from the function associated with CURRENT_BLOCK or
5176 that is superfluous due to the presence of more specific renaming
5177 information. Places surviving symbols in the initial entries of
5181 First, in cases where an object renaming is implemented as a
5182 reference variable, GNAT may produce both the actual reference
5183 variable and the renaming encoding. In this case, we discard the
5186 Second, GNAT emits a type following a specified encoding for each renaming
5187 entity. Unfortunately, STABS currently does not support the definition
5188 of types that are local to a given lexical block, so all renamings types
5189 are emitted at library level. As a consequence, if an application
5190 contains two renaming entities using the same name, and a user tries to
5191 print the value of one of these entities, the result of the ada symbol
5192 lookup will also contain the wrong renaming type.
5194 This function partially covers for this limitation by attempting to
5195 remove from the SYMS list renaming symbols that should be visible
5196 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
5197 method with the current information available. The implementation
5198 below has a couple of limitations (FIXME: brobecker-2003-05-12):
5200 - When the user tries to print a rename in a function while there
5201 is another rename entity defined in a package: Normally, the
5202 rename in the function has precedence over the rename in the
5203 package, so the latter should be removed from the list. This is
5204 currently not the case.
5206 - This function will incorrectly remove valid renames if
5207 the CURRENT_BLOCK corresponds to a function which symbol name
5208 has been changed by an "Export" pragma. As a consequence,
5209 the user will be unable to print such rename entities. */
5212 remove_irrelevant_renamings (std::vector
<struct block_symbol
> *syms
,
5213 const struct block
*current_block
)
5215 struct symbol
*current_function
;
5216 const char *current_function_name
;
5218 int is_new_style_renaming
;
5220 /* If there is both a renaming foo___XR... encoded as a variable and
5221 a simple variable foo in the same block, discard the latter.
5222 First, zero out such symbols, then compress. */
5223 is_new_style_renaming
= 0;
5224 for (i
= 0; i
< syms
->size (); i
+= 1)
5226 struct symbol
*sym
= (*syms
)[i
].symbol
;
5227 const struct block
*block
= (*syms
)[i
].block
;
5231 if (sym
== NULL
|| SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
5233 name
= sym
->linkage_name ();
5234 suffix
= strstr (name
, "___XR");
5238 int name_len
= suffix
- name
;
5241 is_new_style_renaming
= 1;
5242 for (j
= 0; j
< syms
->size (); j
+= 1)
5243 if (i
!= j
&& (*syms
)[j
].symbol
!= NULL
5244 && strncmp (name
, (*syms
)[j
].symbol
->linkage_name (),
5246 && block
== (*syms
)[j
].block
)
5247 (*syms
)[j
].symbol
= NULL
;
5250 if (is_new_style_renaming
)
5254 for (j
= k
= 0; j
< syms
->size (); j
+= 1)
5255 if ((*syms
)[j
].symbol
!= NULL
)
5257 (*syms
)[k
] = (*syms
)[j
];
5264 /* Extract the function name associated to CURRENT_BLOCK.
5265 Abort if unable to do so. */
5267 if (current_block
== NULL
)
5270 current_function
= block_linkage_function (current_block
);
5271 if (current_function
== NULL
)
5274 current_function_name
= current_function
->linkage_name ();
5275 if (current_function_name
== NULL
)
5278 /* Check each of the symbols, and remove it from the list if it is
5279 a type corresponding to a renaming that is out of the scope of
5280 the current block. */
5283 while (i
< syms
->size ())
5285 if (ada_parse_renaming ((*syms
)[i
].symbol
, NULL
, NULL
, NULL
)
5286 == ADA_OBJECT_RENAMING
5287 && old_renaming_is_invisible ((*syms
)[i
].symbol
,
5288 current_function_name
))
5289 syms
->erase (syms
->begin () + i
);
5295 /* Add to RESULT all symbols from BLOCK (and its super-blocks)
5296 whose name and domain match NAME and DOMAIN respectively.
5297 If no match was found, then extend the search to "enclosing"
5298 routines (in other words, if we're inside a nested function,
5299 search the symbols defined inside the enclosing functions).
5300 If WILD_MATCH_P is nonzero, perform the naming matching in
5301 "wild" mode (see function "wild_match" for more info).
5303 Note: This function assumes that RESULT has 0 (zero) element in it. */
5306 ada_add_local_symbols (std::vector
<struct block_symbol
> &result
,
5307 const lookup_name_info
&lookup_name
,
5308 const struct block
*block
, domain_enum domain
)
5310 int block_depth
= 0;
5312 while (block
!= NULL
)
5315 ada_add_block_symbols (result
, block
, lookup_name
, domain
, NULL
);
5317 /* If we found a non-function match, assume that's the one. */
5318 if (is_nonfunction (result
))
5321 block
= BLOCK_SUPERBLOCK (block
);
5324 /* If no luck so far, try to find NAME as a local symbol in some lexically
5325 enclosing subprogram. */
5326 if (result
.empty () && block_depth
> 2)
5327 add_symbols_from_enclosing_procs (result
, lookup_name
, domain
);
5330 /* An object of this type is used as the user_data argument when
5331 calling the map_matching_symbols method. */
5335 explicit match_data (std::vector
<struct block_symbol
> *rp
)
5339 DISABLE_COPY_AND_ASSIGN (match_data
);
5341 struct objfile
*objfile
= nullptr;
5342 std::vector
<struct block_symbol
> *resultp
;
5343 struct symbol
*arg_sym
= nullptr;
5344 bool found_sym
= false;
5347 /* A callback for add_nonlocal_symbols that adds symbol, found in BSYM,
5348 to a list of symbols. DATA is a pointer to a struct match_data *
5349 containing the vector that collects the symbol list, the file that SYM
5350 must come from, a flag indicating whether a non-argument symbol has
5351 been found in the current block, and the last argument symbol
5352 passed in SYM within the current block (if any). When SYM is null,
5353 marking the end of a block, the argument symbol is added if no
5354 other has been found. */
5357 aux_add_nonlocal_symbols (struct block_symbol
*bsym
,
5358 struct match_data
*data
)
5360 const struct block
*block
= bsym
->block
;
5361 struct symbol
*sym
= bsym
->symbol
;
5365 if (!data
->found_sym
&& data
->arg_sym
!= NULL
)
5366 add_defn_to_vec (*data
->resultp
,
5367 fixup_symbol_section (data
->arg_sym
, data
->objfile
),
5369 data
->found_sym
= false;
5370 data
->arg_sym
= NULL
;
5374 if (SYMBOL_CLASS (sym
) == LOC_UNRESOLVED
)
5376 else if (SYMBOL_IS_ARGUMENT (sym
))
5377 data
->arg_sym
= sym
;
5380 data
->found_sym
= true;
5381 add_defn_to_vec (*data
->resultp
,
5382 fixup_symbol_section (sym
, data
->objfile
),
5389 /* Helper for add_nonlocal_symbols. Find symbols in DOMAIN which are
5390 targeted by renamings matching LOOKUP_NAME in BLOCK. Add these
5391 symbols to RESULT. Return whether we found such symbols. */
5394 ada_add_block_renamings (std::vector
<struct block_symbol
> &result
,
5395 const struct block
*block
,
5396 const lookup_name_info
&lookup_name
,
5399 struct using_direct
*renaming
;
5400 int defns_mark
= result
.size ();
5402 symbol_name_matcher_ftype
*name_match
5403 = ada_get_symbol_name_matcher (lookup_name
);
5405 for (renaming
= block_using (block
);
5407 renaming
= renaming
->next
)
5411 /* Avoid infinite recursions: skip this renaming if we are actually
5412 already traversing it.
5414 Currently, symbol lookup in Ada don't use the namespace machinery from
5415 C++/Fortran support: skip namespace imports that use them. */
5416 if (renaming
->searched
5417 || (renaming
->import_src
!= NULL
5418 && renaming
->import_src
[0] != '\0')
5419 || (renaming
->import_dest
!= NULL
5420 && renaming
->import_dest
[0] != '\0'))
5422 renaming
->searched
= 1;
5424 /* TODO: here, we perform another name-based symbol lookup, which can
5425 pull its own multiple overloads. In theory, we should be able to do
5426 better in this case since, in DWARF, DW_AT_import is a DIE reference,
5427 not a simple name. But in order to do this, we would need to enhance
5428 the DWARF reader to associate a symbol to this renaming, instead of a
5429 name. So, for now, we do something simpler: re-use the C++/Fortran
5430 namespace machinery. */
5431 r_name
= (renaming
->alias
!= NULL
5433 : renaming
->declaration
);
5434 if (name_match (r_name
, lookup_name
, NULL
))
5436 lookup_name_info
decl_lookup_name (renaming
->declaration
,
5437 lookup_name
.match_type ());
5438 ada_add_all_symbols (result
, block
, decl_lookup_name
, domain
,
5441 renaming
->searched
= 0;
5443 return result
.size () != defns_mark
;
5446 /* Implements compare_names, but only applying the comparision using
5447 the given CASING. */
5450 compare_names_with_case (const char *string1
, const char *string2
,
5451 enum case_sensitivity casing
)
5453 while (*string1
!= '\0' && *string2
!= '\0')
5457 if (isspace (*string1
) || isspace (*string2
))
5458 return strcmp_iw_ordered (string1
, string2
);
5460 if (casing
== case_sensitive_off
)
5462 c1
= tolower (*string1
);
5463 c2
= tolower (*string2
);
5480 return strcmp_iw_ordered (string1
, string2
);
5482 if (*string2
== '\0')
5484 if (is_name_suffix (string1
))
5491 if (*string2
== '(')
5492 return strcmp_iw_ordered (string1
, string2
);
5495 if (casing
== case_sensitive_off
)
5496 return tolower (*string1
) - tolower (*string2
);
5498 return *string1
- *string2
;
5503 /* Compare STRING1 to STRING2, with results as for strcmp.
5504 Compatible with strcmp_iw_ordered in that...
5506 strcmp_iw_ordered (STRING1, STRING2) <= 0
5510 compare_names (STRING1, STRING2) <= 0
5512 (they may differ as to what symbols compare equal). */
5515 compare_names (const char *string1
, const char *string2
)
5519 /* Similar to what strcmp_iw_ordered does, we need to perform
5520 a case-insensitive comparison first, and only resort to
5521 a second, case-sensitive, comparison if the first one was
5522 not sufficient to differentiate the two strings. */
5524 result
= compare_names_with_case (string1
, string2
, case_sensitive_off
);
5526 result
= compare_names_with_case (string1
, string2
, case_sensitive_on
);
5531 /* Convenience function to get at the Ada encoded lookup name for
5532 LOOKUP_NAME, as a C string. */
5535 ada_lookup_name (const lookup_name_info
&lookup_name
)
5537 return lookup_name
.ada ().lookup_name ().c_str ();
5540 /* Add to RESULT all non-local symbols whose name and domain match
5541 LOOKUP_NAME and DOMAIN respectively. The search is performed on
5542 GLOBAL_BLOCK symbols if GLOBAL is non-zero, or on STATIC_BLOCK
5543 symbols otherwise. */
5546 add_nonlocal_symbols (std::vector
<struct block_symbol
> &result
,
5547 const lookup_name_info
&lookup_name
,
5548 domain_enum domain
, int global
)
5550 struct match_data
data (&result
);
5552 bool is_wild_match
= lookup_name
.ada ().wild_match_p ();
5554 auto callback
= [&] (struct block_symbol
*bsym
)
5556 return aux_add_nonlocal_symbols (bsym
, &data
);
5559 for (objfile
*objfile
: current_program_space
->objfiles ())
5561 data
.objfile
= objfile
;
5563 if (objfile
->sf
!= nullptr)
5564 objfile
->sf
->qf
->map_matching_symbols (objfile
, lookup_name
,
5565 domain
, global
, callback
,
5567 ? NULL
: compare_names
));
5569 for (compunit_symtab
*cu
: objfile
->compunits ())
5571 const struct block
*global_block
5572 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cu
), GLOBAL_BLOCK
);
5574 if (ada_add_block_renamings (result
, global_block
, lookup_name
,
5576 data
.found_sym
= true;
5580 if (result
.empty () && global
&& !is_wild_match
)
5582 const char *name
= ada_lookup_name (lookup_name
);
5583 std::string bracket_name
= std::string ("<_ada_") + name
+ '>';
5584 lookup_name_info
name1 (bracket_name
, symbol_name_match_type::FULL
);
5586 for (objfile
*objfile
: current_program_space
->objfiles ())
5588 data
.objfile
= objfile
;
5589 if (objfile
->sf
!= nullptr)
5590 objfile
->sf
->qf
->map_matching_symbols (objfile
, name1
,
5591 domain
, global
, callback
,
5597 /* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if
5598 FULL_SEARCH is non-zero, enclosing scope and in global scopes,
5599 returning the number of matches. Add these to RESULT.
5601 When FULL_SEARCH is non-zero, any non-function/non-enumeral
5602 symbol match within the nest of blocks whose innermost member is BLOCK,
5603 is the one match returned (no other matches in that or
5604 enclosing blocks is returned). If there are any matches in or
5605 surrounding BLOCK, then these alone are returned.
5607 Names prefixed with "standard__" are handled specially:
5608 "standard__" is first stripped off (by the lookup_name
5609 constructor), and only static and global symbols are searched.
5611 If MADE_GLOBAL_LOOKUP_P is non-null, set it before return to whether we had
5612 to lookup global symbols. */
5615 ada_add_all_symbols (std::vector
<struct block_symbol
> &result
,
5616 const struct block
*block
,
5617 const lookup_name_info
&lookup_name
,
5620 int *made_global_lookup_p
)
5624 if (made_global_lookup_p
)
5625 *made_global_lookup_p
= 0;
5627 /* Special case: If the user specifies a symbol name inside package
5628 Standard, do a non-wild matching of the symbol name without
5629 the "standard__" prefix. This was primarily introduced in order
5630 to allow the user to specifically access the standard exceptions
5631 using, for instance, Standard.Constraint_Error when Constraint_Error
5632 is ambiguous (due to the user defining its own Constraint_Error
5633 entity inside its program). */
5634 if (lookup_name
.ada ().standard_p ())
5637 /* Check the non-global symbols. If we have ANY match, then we're done. */
5642 ada_add_local_symbols (result
, lookup_name
, block
, domain
);
5645 /* In the !full_search case we're are being called by
5646 iterate_over_symbols, and we don't want to search
5648 ada_add_block_symbols (result
, block
, lookup_name
, domain
, NULL
);
5650 if (!result
.empty () || !full_search
)
5654 /* No non-global symbols found. Check our cache to see if we have
5655 already performed this search before. If we have, then return
5658 if (lookup_cached_symbol (ada_lookup_name (lookup_name
),
5659 domain
, &sym
, &block
))
5662 add_defn_to_vec (result
, sym
, block
);
5666 if (made_global_lookup_p
)
5667 *made_global_lookup_p
= 1;
5669 /* Search symbols from all global blocks. */
5671 add_nonlocal_symbols (result
, lookup_name
, domain
, 1);
5673 /* Now add symbols from all per-file blocks if we've gotten no hits
5674 (not strictly correct, but perhaps better than an error). */
5676 if (result
.empty ())
5677 add_nonlocal_symbols (result
, lookup_name
, domain
, 0);
5680 /* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if FULL_SEARCH
5681 is non-zero, enclosing scope and in global scopes.
5683 Returns (SYM,BLOCK) tuples, indicating the symbols found and the
5684 blocks and symbol tables (if any) in which they were found.
5686 When full_search is non-zero, any non-function/non-enumeral
5687 symbol match within the nest of blocks whose innermost member is BLOCK,
5688 is the one match returned (no other matches in that or
5689 enclosing blocks is returned). If there are any matches in or
5690 surrounding BLOCK, then these alone are returned.
5692 Names prefixed with "standard__" are handled specially: "standard__"
5693 is first stripped off, and only static and global symbols are searched. */
5695 static std::vector
<struct block_symbol
>
5696 ada_lookup_symbol_list_worker (const lookup_name_info
&lookup_name
,
5697 const struct block
*block
,
5701 int syms_from_global_search
;
5702 std::vector
<struct block_symbol
> results
;
5704 ada_add_all_symbols (results
, block
, lookup_name
,
5705 domain
, full_search
, &syms_from_global_search
);
5707 remove_extra_symbols (&results
);
5709 if (results
.empty () && full_search
&& syms_from_global_search
)
5710 cache_symbol (ada_lookup_name (lookup_name
), domain
, NULL
, NULL
);
5712 if (results
.size () == 1 && full_search
&& syms_from_global_search
)
5713 cache_symbol (ada_lookup_name (lookup_name
), domain
,
5714 results
[0].symbol
, results
[0].block
);
5716 remove_irrelevant_renamings (&results
, block
);
5720 /* Find symbols in DOMAIN matching NAME, in BLOCK and enclosing scope and
5721 in global scopes, returning (SYM,BLOCK) tuples.
5723 See ada_lookup_symbol_list_worker for further details. */
5725 std::vector
<struct block_symbol
>
5726 ada_lookup_symbol_list (const char *name
, const struct block
*block
,
5729 symbol_name_match_type name_match_type
= name_match_type_from_name (name
);
5730 lookup_name_info
lookup_name (name
, name_match_type
);
5732 return ada_lookup_symbol_list_worker (lookup_name
, block
, domain
, 1);
5735 /* The result is as for ada_lookup_symbol_list with FULL_SEARCH set
5736 to 1, but choosing the first symbol found if there are multiple
5739 The result is stored in *INFO, which must be non-NULL.
5740 If no match is found, INFO->SYM is set to NULL. */
5743 ada_lookup_encoded_symbol (const char *name
, const struct block
*block
,
5745 struct block_symbol
*info
)
5747 /* Since we already have an encoded name, wrap it in '<>' to force a
5748 verbatim match. Otherwise, if the name happens to not look like
5749 an encoded name (because it doesn't include a "__"),
5750 ada_lookup_name_info would re-encode/fold it again, and that
5751 would e.g., incorrectly lowercase object renaming names like
5752 "R28b" -> "r28b". */
5753 std::string verbatim
= add_angle_brackets (name
);
5755 gdb_assert (info
!= NULL
);
5756 *info
= ada_lookup_symbol (verbatim
.c_str (), block
, domain
);
5759 /* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
5760 scope and in global scopes, or NULL if none. NAME is folded and
5761 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
5762 choosing the first symbol if there are multiple choices. */
5765 ada_lookup_symbol (const char *name
, const struct block
*block0
,
5768 std::vector
<struct block_symbol
> candidates
5769 = ada_lookup_symbol_list (name
, block0
, domain
);
5771 if (candidates
.empty ())
5774 block_symbol info
= candidates
[0];
5775 info
.symbol
= fixup_symbol_section (info
.symbol
, NULL
);
5780 /* True iff STR is a possible encoded suffix of a normal Ada name
5781 that is to be ignored for matching purposes. Suffixes of parallel
5782 names (e.g., XVE) are not included here. Currently, the possible suffixes
5783 are given by any of the regular expressions:
5785 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
5786 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
5787 TKB [subprogram suffix for task bodies]
5788 _E[0-9]+[bs]$ [protected object entry suffixes]
5789 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
5791 Also, any leading "__[0-9]+" sequence is skipped before the suffix
5792 match is performed. This sequence is used to differentiate homonyms,
5793 is an optional part of a valid name suffix. */
5796 is_name_suffix (const char *str
)
5799 const char *matching
;
5800 const int len
= strlen (str
);
5802 /* Skip optional leading __[0-9]+. */
5804 if (len
> 3 && str
[0] == '_' && str
[1] == '_' && isdigit (str
[2]))
5807 while (isdigit (str
[0]))
5813 if (str
[0] == '.' || str
[0] == '$')
5816 while (isdigit (matching
[0]))
5818 if (matching
[0] == '\0')
5824 if (len
> 3 && str
[0] == '_' && str
[1] == '_' && str
[2] == '_')
5827 while (isdigit (matching
[0]))
5829 if (matching
[0] == '\0')
5833 /* "TKB" suffixes are used for subprograms implementing task bodies. */
5835 if (strcmp (str
, "TKB") == 0)
5839 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
5840 with a N at the end. Unfortunately, the compiler uses the same
5841 convention for other internal types it creates. So treating
5842 all entity names that end with an "N" as a name suffix causes
5843 some regressions. For instance, consider the case of an enumerated
5844 type. To support the 'Image attribute, it creates an array whose
5846 Having a single character like this as a suffix carrying some
5847 information is a bit risky. Perhaps we should change the encoding
5848 to be something like "_N" instead. In the meantime, do not do
5849 the following check. */
5850 /* Protected Object Subprograms */
5851 if (len
== 1 && str
[0] == 'N')
5856 if (len
> 3 && str
[0] == '_' && str
[1] == 'E' && isdigit (str
[2]))
5859 while (isdigit (matching
[0]))
5861 if ((matching
[0] == 'b' || matching
[0] == 's')
5862 && matching
[1] == '\0')
5866 /* ??? We should not modify STR directly, as we are doing below. This
5867 is fine in this case, but may become problematic later if we find
5868 that this alternative did not work, and want to try matching
5869 another one from the begining of STR. Since we modified it, we
5870 won't be able to find the begining of the string anymore! */
5874 while (str
[0] != '_' && str
[0] != '\0')
5876 if (str
[0] != 'n' && str
[0] != 'b')
5882 if (str
[0] == '\000')
5887 if (str
[1] != '_' || str
[2] == '\000')
5891 if (strcmp (str
+ 3, "JM") == 0)
5893 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
5894 the LJM suffix in favor of the JM one. But we will
5895 still accept LJM as a valid suffix for a reasonable
5896 amount of time, just to allow ourselves to debug programs
5897 compiled using an older version of GNAT. */
5898 if (strcmp (str
+ 3, "LJM") == 0)
5902 if (str
[4] == 'F' || str
[4] == 'D' || str
[4] == 'B'
5903 || str
[4] == 'U' || str
[4] == 'P')
5905 if (str
[4] == 'R' && str
[5] != 'T')
5909 if (!isdigit (str
[2]))
5911 for (k
= 3; str
[k
] != '\0'; k
+= 1)
5912 if (!isdigit (str
[k
]) && str
[k
] != '_')
5916 if (str
[0] == '$' && isdigit (str
[1]))
5918 for (k
= 2; str
[k
] != '\0'; k
+= 1)
5919 if (!isdigit (str
[k
]) && str
[k
] != '_')
5926 /* Return non-zero if the string starting at NAME and ending before
5927 NAME_END contains no capital letters. */
5930 is_valid_name_for_wild_match (const char *name0
)
5932 std::string decoded_name
= ada_decode (name0
);
5935 /* If the decoded name starts with an angle bracket, it means that
5936 NAME0 does not follow the GNAT encoding format. It should then
5937 not be allowed as a possible wild match. */
5938 if (decoded_name
[0] == '<')
5941 for (i
=0; decoded_name
[i
] != '\0'; i
++)
5942 if (isalpha (decoded_name
[i
]) && !islower (decoded_name
[i
]))
5948 /* Advance *NAMEP to next occurrence in the string NAME0 of the TARGET0
5949 character which could start a simple name. Assumes that *NAMEP points
5950 somewhere inside the string beginning at NAME0. */
5953 advance_wild_match (const char **namep
, const char *name0
, char target0
)
5955 const char *name
= *namep
;
5965 if ((t1
>= 'a' && t1
<= 'z') || (t1
>= '0' && t1
<= '9'))
5968 if (name
== name0
+ 5 && startswith (name0
, "_ada"))
5973 else if (t1
== '_' && ((name
[2] >= 'a' && name
[2] <= 'z')
5974 || name
[2] == target0
))
5979 else if (t1
== '_' && name
[2] == 'B' && name
[3] == '_')
5981 /* Names like "pkg__B_N__name", where N is a number, are
5982 block-local. We can handle these by simply skipping
5989 else if ((t0
>= 'a' && t0
<= 'z') || (t0
>= '0' && t0
<= '9'))
5999 /* Return true iff NAME encodes a name of the form prefix.PATN.
6000 Ignores any informational suffixes of NAME (i.e., for which
6001 is_name_suffix is true). Assumes that PATN is a lower-cased Ada
6005 wild_match (const char *name
, const char *patn
)
6008 const char *name0
= name
;
6012 const char *match
= name
;
6016 for (name
+= 1, p
= patn
+ 1; *p
!= '\0'; name
+= 1, p
+= 1)
6019 if (*p
== '\0' && is_name_suffix (name
))
6020 return match
== name0
|| is_valid_name_for_wild_match (name0
);
6022 if (name
[-1] == '_')
6025 if (!advance_wild_match (&name
, name0
, *patn
))
6030 /* Add symbols from BLOCK matching LOOKUP_NAME in DOMAIN to RESULT (if
6031 necessary). OBJFILE is the section containing BLOCK. */
6034 ada_add_block_symbols (std::vector
<struct block_symbol
> &result
,
6035 const struct block
*block
,
6036 const lookup_name_info
&lookup_name
,
6037 domain_enum domain
, struct objfile
*objfile
)
6039 struct block_iterator iter
;
6040 /* A matching argument symbol, if any. */
6041 struct symbol
*arg_sym
;
6042 /* Set true when we find a matching non-argument symbol. */
6048 for (sym
= block_iter_match_first (block
, lookup_name
, &iter
);
6050 sym
= block_iter_match_next (lookup_name
, &iter
))
6052 if (symbol_matches_domain (sym
->language (), SYMBOL_DOMAIN (sym
), domain
))
6054 if (SYMBOL_CLASS (sym
) != LOC_UNRESOLVED
)
6056 if (SYMBOL_IS_ARGUMENT (sym
))
6061 add_defn_to_vec (result
,
6062 fixup_symbol_section (sym
, objfile
),
6069 /* Handle renamings. */
6071 if (ada_add_block_renamings (result
, block
, lookup_name
, domain
))
6074 if (!found_sym
&& arg_sym
!= NULL
)
6076 add_defn_to_vec (result
,
6077 fixup_symbol_section (arg_sym
, objfile
),
6081 if (!lookup_name
.ada ().wild_match_p ())
6085 const std::string
&ada_lookup_name
= lookup_name
.ada ().lookup_name ();
6086 const char *name
= ada_lookup_name
.c_str ();
6087 size_t name_len
= ada_lookup_name
.size ();
6089 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
6091 if (symbol_matches_domain (sym
->language (),
6092 SYMBOL_DOMAIN (sym
), domain
))
6096 cmp
= (int) '_' - (int) sym
->linkage_name ()[0];
6099 cmp
= !startswith (sym
->linkage_name (), "_ada_");
6101 cmp
= strncmp (name
, sym
->linkage_name () + 5,
6106 && is_name_suffix (sym
->linkage_name () + name_len
+ 5))
6108 if (SYMBOL_CLASS (sym
) != LOC_UNRESOLVED
)
6110 if (SYMBOL_IS_ARGUMENT (sym
))
6115 add_defn_to_vec (result
,
6116 fixup_symbol_section (sym
, objfile
),
6124 /* NOTE: This really shouldn't be needed for _ada_ symbols.
6125 They aren't parameters, right? */
6126 if (!found_sym
&& arg_sym
!= NULL
)
6128 add_defn_to_vec (result
,
6129 fixup_symbol_section (arg_sym
, objfile
),
6136 /* Symbol Completion */
6141 ada_lookup_name_info::matches
6142 (const char *sym_name
,
6143 symbol_name_match_type match_type
,
6144 completion_match_result
*comp_match_res
) const
6147 const char *text
= m_encoded_name
.c_str ();
6148 size_t text_len
= m_encoded_name
.size ();
6150 /* First, test against the fully qualified name of the symbol. */
6152 if (strncmp (sym_name
, text
, text_len
) == 0)
6155 std::string decoded_name
= ada_decode (sym_name
);
6156 if (match
&& !m_encoded_p
)
6158 /* One needed check before declaring a positive match is to verify
6159 that iff we are doing a verbatim match, the decoded version
6160 of the symbol name starts with '<'. Otherwise, this symbol name
6161 is not a suitable completion. */
6163 bool has_angle_bracket
= (decoded_name
[0] == '<');
6164 match
= (has_angle_bracket
== m_verbatim_p
);
6167 if (match
&& !m_verbatim_p
)
6169 /* When doing non-verbatim match, another check that needs to
6170 be done is to verify that the potentially matching symbol name
6171 does not include capital letters, because the ada-mode would
6172 not be able to understand these symbol names without the
6173 angle bracket notation. */
6176 for (tmp
= sym_name
; *tmp
!= '\0' && !isupper (*tmp
); tmp
++);
6181 /* Second: Try wild matching... */
6183 if (!match
&& m_wild_match_p
)
6185 /* Since we are doing wild matching, this means that TEXT
6186 may represent an unqualified symbol name. We therefore must
6187 also compare TEXT against the unqualified name of the symbol. */
6188 sym_name
= ada_unqualified_name (decoded_name
.c_str ());
6190 if (strncmp (sym_name
, text
, text_len
) == 0)
6194 /* Finally: If we found a match, prepare the result to return. */
6199 if (comp_match_res
!= NULL
)
6201 std::string
&match_str
= comp_match_res
->match
.storage ();
6204 match_str
= ada_decode (sym_name
);
6208 match_str
= add_angle_brackets (sym_name
);
6210 match_str
= sym_name
;
6214 comp_match_res
->set_match (match_str
.c_str ());
6222 /* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
6223 for tagged types. */
6226 ada_is_dispatch_table_ptr_type (struct type
*type
)
6230 if (type
->code () != TYPE_CODE_PTR
)
6233 name
= TYPE_TARGET_TYPE (type
)->name ();
6237 return (strcmp (name
, "ada__tags__dispatch_table") == 0);
6240 /* Return non-zero if TYPE is an interface tag. */
6243 ada_is_interface_tag (struct type
*type
)
6245 const char *name
= type
->name ();
6250 return (strcmp (name
, "ada__tags__interface_tag") == 0);
6253 /* True if field number FIELD_NUM in struct or union type TYPE is supposed
6254 to be invisible to users. */
6257 ada_is_ignored_field (struct type
*type
, int field_num
)
6259 if (field_num
< 0 || field_num
> type
->num_fields ())
6262 /* Check the name of that field. */
6264 const char *name
= TYPE_FIELD_NAME (type
, field_num
);
6266 /* Anonymous field names should not be printed.
6267 brobecker/2007-02-20: I don't think this can actually happen
6268 but we don't want to print the value of anonymous fields anyway. */
6272 /* Normally, fields whose name start with an underscore ("_")
6273 are fields that have been internally generated by the compiler,
6274 and thus should not be printed. The "_parent" field is special,
6275 however: This is a field internally generated by the compiler
6276 for tagged types, and it contains the components inherited from
6277 the parent type. This field should not be printed as is, but
6278 should not be ignored either. */
6279 if (name
[0] == '_' && !startswith (name
, "_parent"))
6283 /* If this is the dispatch table of a tagged type or an interface tag,
6285 if (ada_is_tagged_type (type
, 1)
6286 && (ada_is_dispatch_table_ptr_type (type
->field (field_num
).type ())
6287 || ada_is_interface_tag (type
->field (field_num
).type ())))
6290 /* Not a special field, so it should not be ignored. */
6294 /* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
6295 pointer or reference type whose ultimate target has a tag field. */
6298 ada_is_tagged_type (struct type
*type
, int refok
)
6300 return (ada_lookup_struct_elt_type (type
, "_tag", refok
, 1) != NULL
);
6303 /* True iff TYPE represents the type of X'Tag */
6306 ada_is_tag_type (struct type
*type
)
6308 type
= ada_check_typedef (type
);
6310 if (type
== NULL
|| type
->code () != TYPE_CODE_PTR
)
6314 const char *name
= ada_type_name (TYPE_TARGET_TYPE (type
));
6316 return (name
!= NULL
6317 && strcmp (name
, "ada__tags__dispatch_table") == 0);
6321 /* The type of the tag on VAL. */
6323 static struct type
*
6324 ada_tag_type (struct value
*val
)
6326 return ada_lookup_struct_elt_type (value_type (val
), "_tag", 1, 0);
6329 /* Return 1 if TAG follows the old scheme for Ada tags (used for Ada 95,
6330 retired at Ada 05). */
6333 is_ada95_tag (struct value
*tag
)
6335 return ada_value_struct_elt (tag
, "tsd", 1) != NULL
;
6338 /* The value of the tag on VAL. */
6340 static struct value
*
6341 ada_value_tag (struct value
*val
)
6343 return ada_value_struct_elt (val
, "_tag", 0);
6346 /* The value of the tag on the object of type TYPE whose contents are
6347 saved at VALADDR, if it is non-null, or is at memory address
6350 static struct value
*
6351 value_tag_from_contents_and_address (struct type
*type
,
6352 const gdb_byte
*valaddr
,
6355 int tag_byte_offset
;
6356 struct type
*tag_type
;
6358 if (find_struct_field ("_tag", type
, 0, &tag_type
, &tag_byte_offset
,
6361 const gdb_byte
*valaddr1
= ((valaddr
== NULL
)
6363 : valaddr
+ tag_byte_offset
);
6364 CORE_ADDR address1
= (address
== 0) ? 0 : address
+ tag_byte_offset
;
6366 return value_from_contents_and_address (tag_type
, valaddr1
, address1
);
6371 static struct type
*
6372 type_from_tag (struct value
*tag
)
6374 gdb::unique_xmalloc_ptr
<char> type_name
= ada_tag_name (tag
);
6376 if (type_name
!= NULL
)
6377 return ada_find_any_type (ada_encode (type_name
.get ()).c_str ());
6381 /* Given a value OBJ of a tagged type, return a value of this
6382 type at the base address of the object. The base address, as
6383 defined in Ada.Tags, it is the address of the primary tag of
6384 the object, and therefore where the field values of its full
6385 view can be fetched. */
6388 ada_tag_value_at_base_address (struct value
*obj
)
6391 LONGEST offset_to_top
= 0;
6392 struct type
*ptr_type
, *obj_type
;
6394 CORE_ADDR base_address
;
6396 obj_type
= value_type (obj
);
6398 /* It is the responsability of the caller to deref pointers. */
6400 if (obj_type
->code () == TYPE_CODE_PTR
|| obj_type
->code () == TYPE_CODE_REF
)
6403 tag
= ada_value_tag (obj
);
6407 /* Base addresses only appeared with Ada 05 and multiple inheritance. */
6409 if (is_ada95_tag (tag
))
6412 ptr_type
= language_lookup_primitive_type
6413 (language_def (language_ada
), target_gdbarch(), "storage_offset");
6414 ptr_type
= lookup_pointer_type (ptr_type
);
6415 val
= value_cast (ptr_type
, tag
);
6419 /* It is perfectly possible that an exception be raised while
6420 trying to determine the base address, just like for the tag;
6421 see ada_tag_name for more details. We do not print the error
6422 message for the same reason. */
6426 offset_to_top
= value_as_long (value_ind (value_ptradd (val
, -2)));
6429 catch (const gdb_exception_error
&e
)
6434 /* If offset is null, nothing to do. */
6436 if (offset_to_top
== 0)
6439 /* -1 is a special case in Ada.Tags; however, what should be done
6440 is not quite clear from the documentation. So do nothing for
6443 if (offset_to_top
== -1)
6446 /* OFFSET_TO_TOP used to be a positive value to be subtracted
6447 from the base address. This was however incompatible with
6448 C++ dispatch table: C++ uses a *negative* value to *add*
6449 to the base address. Ada's convention has therefore been
6450 changed in GNAT 19.0w 20171023: since then, C++ and Ada
6451 use the same convention. Here, we support both cases by
6452 checking the sign of OFFSET_TO_TOP. */
6454 if (offset_to_top
> 0)
6455 offset_to_top
= -offset_to_top
;
6457 base_address
= value_address (obj
) + offset_to_top
;
6458 tag
= value_tag_from_contents_and_address (obj_type
, NULL
, base_address
);
6460 /* Make sure that we have a proper tag at the new address.
6461 Otherwise, offset_to_top is bogus (which can happen when
6462 the object is not initialized yet). */
6467 obj_type
= type_from_tag (tag
);
6472 return value_from_contents_and_address (obj_type
, NULL
, base_address
);
6475 /* Return the "ada__tags__type_specific_data" type. */
6477 static struct type
*
6478 ada_get_tsd_type (struct inferior
*inf
)
6480 struct ada_inferior_data
*data
= get_ada_inferior_data (inf
);
6482 if (data
->tsd_type
== 0)
6483 data
->tsd_type
= ada_find_any_type ("ada__tags__type_specific_data");
6484 return data
->tsd_type
;
6487 /* Return the TSD (type-specific data) associated to the given TAG.
6488 TAG is assumed to be the tag of a tagged-type entity.
6490 May return NULL if we are unable to get the TSD. */
6492 static struct value
*
6493 ada_get_tsd_from_tag (struct value
*tag
)
6498 /* First option: The TSD is simply stored as a field of our TAG.
6499 Only older versions of GNAT would use this format, but we have
6500 to test it first, because there are no visible markers for
6501 the current approach except the absence of that field. */
6503 val
= ada_value_struct_elt (tag
, "tsd", 1);
6507 /* Try the second representation for the dispatch table (in which
6508 there is no explicit 'tsd' field in the referent of the tag pointer,
6509 and instead the tsd pointer is stored just before the dispatch
6512 type
= ada_get_tsd_type (current_inferior());
6515 type
= lookup_pointer_type (lookup_pointer_type (type
));
6516 val
= value_cast (type
, tag
);
6519 return value_ind (value_ptradd (val
, -1));
6522 /* Given the TSD of a tag (type-specific data), return a string
6523 containing the name of the associated type.
6525 May return NULL if we are unable to determine the tag name. */
6527 static gdb::unique_xmalloc_ptr
<char>
6528 ada_tag_name_from_tsd (struct value
*tsd
)
6533 val
= ada_value_struct_elt (tsd
, "expanded_name", 1);
6536 gdb::unique_xmalloc_ptr
<char> buffer
6537 = target_read_string (value_as_address (val
), INT_MAX
);
6538 if (buffer
== nullptr)
6541 for (p
= buffer
.get (); *p
!= '\0'; ++p
)
6550 /* The type name of the dynamic type denoted by the 'tag value TAG, as
6553 Return NULL if the TAG is not an Ada tag, or if we were unable to
6554 determine the name of that tag. */
6556 gdb::unique_xmalloc_ptr
<char>
6557 ada_tag_name (struct value
*tag
)
6559 gdb::unique_xmalloc_ptr
<char> name
;
6561 if (!ada_is_tag_type (value_type (tag
)))
6564 /* It is perfectly possible that an exception be raised while trying
6565 to determine the TAG's name, even under normal circumstances:
6566 The associated variable may be uninitialized or corrupted, for
6567 instance. We do not let any exception propagate past this point.
6568 instead we return NULL.
6570 We also do not print the error message either (which often is very
6571 low-level (Eg: "Cannot read memory at 0x[...]"), but instead let
6572 the caller print a more meaningful message if necessary. */
6575 struct value
*tsd
= ada_get_tsd_from_tag (tag
);
6578 name
= ada_tag_name_from_tsd (tsd
);
6580 catch (const gdb_exception_error
&e
)
6587 /* The parent type of TYPE, or NULL if none. */
6590 ada_parent_type (struct type
*type
)
6594 type
= ada_check_typedef (type
);
6596 if (type
== NULL
|| type
->code () != TYPE_CODE_STRUCT
)
6599 for (i
= 0; i
< type
->num_fields (); i
+= 1)
6600 if (ada_is_parent_field (type
, i
))
6602 struct type
*parent_type
= type
->field (i
).type ();
6604 /* If the _parent field is a pointer, then dereference it. */
6605 if (parent_type
->code () == TYPE_CODE_PTR
)
6606 parent_type
= TYPE_TARGET_TYPE (parent_type
);
6607 /* If there is a parallel XVS type, get the actual base type. */
6608 parent_type
= ada_get_base_type (parent_type
);
6610 return ada_check_typedef (parent_type
);
6616 /* True iff field number FIELD_NUM of structure type TYPE contains the
6617 parent-type (inherited) fields of a derived type. Assumes TYPE is
6618 a structure type with at least FIELD_NUM+1 fields. */
6621 ada_is_parent_field (struct type
*type
, int field_num
)
6623 const char *name
= TYPE_FIELD_NAME (ada_check_typedef (type
), field_num
);
6625 return (name
!= NULL
6626 && (startswith (name
, "PARENT")
6627 || startswith (name
, "_parent")));
6630 /* True iff field number FIELD_NUM of structure type TYPE is a
6631 transparent wrapper field (which should be silently traversed when doing
6632 field selection and flattened when printing). Assumes TYPE is a
6633 structure type with at least FIELD_NUM+1 fields. Such fields are always
6637 ada_is_wrapper_field (struct type
*type
, int field_num
)
6639 const char *name
= TYPE_FIELD_NAME (type
, field_num
);
6641 if (name
!= NULL
&& strcmp (name
, "RETVAL") == 0)
6643 /* This happens in functions with "out" or "in out" parameters
6644 which are passed by copy. For such functions, GNAT describes
6645 the function's return type as being a struct where the return
6646 value is in a field called RETVAL, and where the other "out"
6647 or "in out" parameters are fields of that struct. This is not
6652 return (name
!= NULL
6653 && (startswith (name
, "PARENT")
6654 || strcmp (name
, "REP") == 0
6655 || startswith (name
, "_parent")
6656 || name
[0] == 'S' || name
[0] == 'R' || name
[0] == 'O'));
6659 /* True iff field number FIELD_NUM of structure or union type TYPE
6660 is a variant wrapper. Assumes TYPE is a structure type with at least
6661 FIELD_NUM+1 fields. */
6664 ada_is_variant_part (struct type
*type
, int field_num
)
6666 /* Only Ada types are eligible. */
6667 if (!ADA_TYPE_P (type
))
6670 struct type
*field_type
= type
->field (field_num
).type ();
6672 return (field_type
->code () == TYPE_CODE_UNION
6673 || (is_dynamic_field (type
, field_num
)
6674 && (TYPE_TARGET_TYPE (field_type
)->code ()
6675 == TYPE_CODE_UNION
)));
6678 /* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
6679 whose discriminants are contained in the record type OUTER_TYPE,
6680 returns the type of the controlling discriminant for the variant.
6681 May return NULL if the type could not be found. */
6684 ada_variant_discrim_type (struct type
*var_type
, struct type
*outer_type
)
6686 const char *name
= ada_variant_discrim_name (var_type
);
6688 return ada_lookup_struct_elt_type (outer_type
, name
, 1, 1);
6691 /* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
6692 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
6693 represents a 'when others' clause; otherwise 0. */
6696 ada_is_others_clause (struct type
*type
, int field_num
)
6698 const char *name
= TYPE_FIELD_NAME (type
, field_num
);
6700 return (name
!= NULL
&& name
[0] == 'O');
6703 /* Assuming that TYPE0 is the type of the variant part of a record,
6704 returns the name of the discriminant controlling the variant.
6705 The value is valid until the next call to ada_variant_discrim_name. */
6708 ada_variant_discrim_name (struct type
*type0
)
6710 static std::string result
;
6713 const char *discrim_end
;
6714 const char *discrim_start
;
6716 if (type0
->code () == TYPE_CODE_PTR
)
6717 type
= TYPE_TARGET_TYPE (type0
);
6721 name
= ada_type_name (type
);
6723 if (name
== NULL
|| name
[0] == '\000')
6726 for (discrim_end
= name
+ strlen (name
) - 6; discrim_end
!= name
;
6729 if (startswith (discrim_end
, "___XVN"))
6732 if (discrim_end
== name
)
6735 for (discrim_start
= discrim_end
; discrim_start
!= name
+ 3;
6738 if (discrim_start
== name
+ 1)
6740 if ((discrim_start
> name
+ 3
6741 && startswith (discrim_start
- 3, "___"))
6742 || discrim_start
[-1] == '.')
6746 result
= std::string (discrim_start
, discrim_end
- discrim_start
);
6747 return result
.c_str ();
6750 /* Scan STR for a subtype-encoded number, beginning at position K.
6751 Put the position of the character just past the number scanned in
6752 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
6753 Return 1 if there was a valid number at the given position, and 0
6754 otherwise. A "subtype-encoded" number consists of the absolute value
6755 in decimal, followed by the letter 'm' to indicate a negative number.
6756 Assumes 0m does not occur. */
6759 ada_scan_number (const char str
[], int k
, LONGEST
* R
, int *new_k
)
6763 if (!isdigit (str
[k
]))
6766 /* Do it the hard way so as not to make any assumption about
6767 the relationship of unsigned long (%lu scan format code) and
6770 while (isdigit (str
[k
]))
6772 RU
= RU
* 10 + (str
[k
] - '0');
6779 *R
= (-(LONGEST
) (RU
- 1)) - 1;
6785 /* NOTE on the above: Technically, C does not say what the results of
6786 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
6787 number representable as a LONGEST (although either would probably work
6788 in most implementations). When RU>0, the locution in the then branch
6789 above is always equivalent to the negative of RU. */
6796 /* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
6797 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
6798 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
6801 ada_in_variant (LONGEST val
, struct type
*type
, int field_num
)
6803 const char *name
= TYPE_FIELD_NAME (type
, field_num
);
6817 if (!ada_scan_number (name
, p
+ 1, &W
, &p
))
6827 if (!ada_scan_number (name
, p
+ 1, &L
, &p
)
6828 || name
[p
] != 'T' || !ada_scan_number (name
, p
+ 1, &U
, &p
))
6830 if (val
>= L
&& val
<= U
)
6842 /* FIXME: Lots of redundancy below. Try to consolidate. */
6844 /* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6845 ARG_TYPE, extract and return the value of one of its (non-static)
6846 fields. FIELDNO says which field. Differs from value_primitive_field
6847 only in that it can handle packed values of arbitrary type. */
6850 ada_value_primitive_field (struct value
*arg1
, int offset
, int fieldno
,
6851 struct type
*arg_type
)
6855 arg_type
= ada_check_typedef (arg_type
);
6856 type
= arg_type
->field (fieldno
).type ();
6858 /* Handle packed fields. It might be that the field is not packed
6859 relative to its containing structure, but the structure itself is
6860 packed; in this case we must take the bit-field path. */
6861 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
) != 0 || value_bitpos (arg1
) != 0)
6863 int bit_pos
= TYPE_FIELD_BITPOS (arg_type
, fieldno
);
6864 int bit_size
= TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
6866 return ada_value_primitive_packed_val (arg1
, value_contents (arg1
),
6867 offset
+ bit_pos
/ 8,
6868 bit_pos
% 8, bit_size
, type
);
6871 return value_primitive_field (arg1
, offset
, fieldno
, arg_type
);
6874 /* Find field with name NAME in object of type TYPE. If found,
6875 set the following for each argument that is non-null:
6876 - *FIELD_TYPE_P to the field's type;
6877 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6878 an object of that type;
6879 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6880 - *BIT_SIZE_P to its size in bits if the field is packed, and
6882 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6883 fields up to but not including the desired field, or by the total
6884 number of fields if not found. A NULL value of NAME never
6885 matches; the function just counts visible fields in this case.
6887 Notice that we need to handle when a tagged record hierarchy
6888 has some components with the same name, like in this scenario:
6890 type Top_T is tagged record
6896 type Middle_T is new Top.Top_T with record
6897 N : Character := 'a';
6901 type Bottom_T is new Middle.Middle_T with record
6903 C : Character := '5';
6905 A : Character := 'J';
6908 Let's say we now have a variable declared and initialized as follow:
6910 TC : Top_A := new Bottom_T;
6912 And then we use this variable to call this function
6914 procedure Assign (Obj: in out Top_T; TV : Integer);
6918 Assign (Top_T (B), 12);
6920 Now, we're in the debugger, and we're inside that procedure
6921 then and we want to print the value of obj.c:
6923 Usually, the tagged record or one of the parent type owns the
6924 component to print and there's no issue but in this particular
6925 case, what does it mean to ask for Obj.C? Since the actual
6926 type for object is type Bottom_T, it could mean two things: type
6927 component C from the Middle_T view, but also component C from
6928 Bottom_T. So in that "undefined" case, when the component is
6929 not found in the non-resolved type (which includes all the
6930 components of the parent type), then resolve it and see if we
6931 get better luck once expanded.
6933 In the case of homonyms in the derived tagged type, we don't
6934 guaranty anything, and pick the one that's easiest for us
6937 Returns 1 if found, 0 otherwise. */
6940 find_struct_field (const char *name
, struct type
*type
, int offset
,
6941 struct type
**field_type_p
,
6942 int *byte_offset_p
, int *bit_offset_p
, int *bit_size_p
,
6946 int parent_offset
= -1;
6948 type
= ada_check_typedef (type
);
6950 if (field_type_p
!= NULL
)
6951 *field_type_p
= NULL
;
6952 if (byte_offset_p
!= NULL
)
6954 if (bit_offset_p
!= NULL
)
6956 if (bit_size_p
!= NULL
)
6959 for (i
= 0; i
< type
->num_fields (); i
+= 1)
6961 int bit_pos
= TYPE_FIELD_BITPOS (type
, i
);
6962 int fld_offset
= offset
+ bit_pos
/ 8;
6963 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
6965 if (t_field_name
== NULL
)
6968 else if (ada_is_parent_field (type
, i
))
6970 /* This is a field pointing us to the parent type of a tagged
6971 type. As hinted in this function's documentation, we give
6972 preference to fields in the current record first, so what
6973 we do here is just record the index of this field before
6974 we skip it. If it turns out we couldn't find our field
6975 in the current record, then we'll get back to it and search
6976 inside it whether the field might exist in the parent. */
6982 else if (name
!= NULL
&& field_name_match (t_field_name
, name
))
6984 int bit_size
= TYPE_FIELD_BITSIZE (type
, i
);
6986 if (field_type_p
!= NULL
)
6987 *field_type_p
= type
->field (i
).type ();
6988 if (byte_offset_p
!= NULL
)
6989 *byte_offset_p
= fld_offset
;
6990 if (bit_offset_p
!= NULL
)
6991 *bit_offset_p
= bit_pos
% 8;
6992 if (bit_size_p
!= NULL
)
6993 *bit_size_p
= bit_size
;
6996 else if (ada_is_wrapper_field (type
, i
))
6998 if (find_struct_field (name
, type
->field (i
).type (), fld_offset
,
6999 field_type_p
, byte_offset_p
, bit_offset_p
,
7000 bit_size_p
, index_p
))
7003 else if (ada_is_variant_part (type
, i
))
7005 /* PNH: Wait. Do we ever execute this section, or is ARG always of
7008 struct type
*field_type
7009 = ada_check_typedef (type
->field (i
).type ());
7011 for (j
= 0; j
< field_type
->num_fields (); j
+= 1)
7013 if (find_struct_field (name
, field_type
->field (j
).type (),
7015 + TYPE_FIELD_BITPOS (field_type
, j
) / 8,
7016 field_type_p
, byte_offset_p
,
7017 bit_offset_p
, bit_size_p
, index_p
))
7021 else if (index_p
!= NULL
)
7025 /* Field not found so far. If this is a tagged type which
7026 has a parent, try finding that field in the parent now. */
7028 if (parent_offset
!= -1)
7030 int bit_pos
= TYPE_FIELD_BITPOS (type
, parent_offset
);
7031 int fld_offset
= offset
+ bit_pos
/ 8;
7033 if (find_struct_field (name
, type
->field (parent_offset
).type (),
7034 fld_offset
, field_type_p
, byte_offset_p
,
7035 bit_offset_p
, bit_size_p
, index_p
))
7042 /* Number of user-visible fields in record type TYPE. */
7045 num_visible_fields (struct type
*type
)
7050 find_struct_field (NULL
, type
, 0, NULL
, NULL
, NULL
, NULL
, &n
);
7054 /* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
7055 and search in it assuming it has (class) type TYPE.
7056 If found, return value, else return NULL.
7058 Searches recursively through wrapper fields (e.g., '_parent').
7060 In the case of homonyms in the tagged types, please refer to the
7061 long explanation in find_struct_field's function documentation. */
7063 static struct value
*
7064 ada_search_struct_field (const char *name
, struct value
*arg
, int offset
,
7068 int parent_offset
= -1;
7070 type
= ada_check_typedef (type
);
7071 for (i
= 0; i
< type
->num_fields (); i
+= 1)
7073 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
7075 if (t_field_name
== NULL
)
7078 else if (ada_is_parent_field (type
, i
))
7080 /* This is a field pointing us to the parent type of a tagged
7081 type. As hinted in this function's documentation, we give
7082 preference to fields in the current record first, so what
7083 we do here is just record the index of this field before
7084 we skip it. If it turns out we couldn't find our field
7085 in the current record, then we'll get back to it and search
7086 inside it whether the field might exist in the parent. */
7092 else if (field_name_match (t_field_name
, name
))
7093 return ada_value_primitive_field (arg
, offset
, i
, type
);
7095 else if (ada_is_wrapper_field (type
, i
))
7097 struct value
*v
= /* Do not let indent join lines here. */
7098 ada_search_struct_field (name
, arg
,
7099 offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
7100 type
->field (i
).type ());
7106 else if (ada_is_variant_part (type
, i
))
7108 /* PNH: Do we ever get here? See find_struct_field. */
7110 struct type
*field_type
= ada_check_typedef (type
->field (i
).type ());
7111 int var_offset
= offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8;
7113 for (j
= 0; j
< field_type
->num_fields (); j
+= 1)
7115 struct value
*v
= ada_search_struct_field
/* Force line
7118 var_offset
+ TYPE_FIELD_BITPOS (field_type
, j
) / 8,
7119 field_type
->field (j
).type ());
7127 /* Field not found so far. If this is a tagged type which
7128 has a parent, try finding that field in the parent now. */
7130 if (parent_offset
!= -1)
7132 struct value
*v
= ada_search_struct_field (
7133 name
, arg
, offset
+ TYPE_FIELD_BITPOS (type
, parent_offset
) / 8,
7134 type
->field (parent_offset
).type ());
7143 static struct value
*ada_index_struct_field_1 (int *, struct value
*,
7144 int, struct type
*);
7147 /* Return field #INDEX in ARG, where the index is that returned by
7148 * find_struct_field through its INDEX_P argument. Adjust the address
7149 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
7150 * If found, return value, else return NULL. */
7152 static struct value
*
7153 ada_index_struct_field (int index
, struct value
*arg
, int offset
,
7156 return ada_index_struct_field_1 (&index
, arg
, offset
, type
);
7160 /* Auxiliary function for ada_index_struct_field. Like
7161 * ada_index_struct_field, but takes index from *INDEX_P and modifies
7164 static struct value
*
7165 ada_index_struct_field_1 (int *index_p
, struct value
*arg
, int offset
,
7169 type
= ada_check_typedef (type
);
7171 for (i
= 0; i
< type
->num_fields (); i
+= 1)
7173 if (TYPE_FIELD_NAME (type
, i
) == NULL
)
7175 else if (ada_is_wrapper_field (type
, i
))
7177 struct value
*v
= /* Do not let indent join lines here. */
7178 ada_index_struct_field_1 (index_p
, arg
,
7179 offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
7180 type
->field (i
).type ());
7186 else if (ada_is_variant_part (type
, i
))
7188 /* PNH: Do we ever get here? See ada_search_struct_field,
7189 find_struct_field. */
7190 error (_("Cannot assign this kind of variant record"));
7192 else if (*index_p
== 0)
7193 return ada_value_primitive_field (arg
, offset
, i
, type
);
7200 /* Return a string representation of type TYPE. */
7203 type_as_string (struct type
*type
)
7205 string_file tmp_stream
;
7207 type_print (type
, "", &tmp_stream
, -1);
7209 return std::move (tmp_stream
.string ());
7212 /* Given a type TYPE, look up the type of the component of type named NAME.
7213 If DISPP is non-null, add its byte displacement from the beginning of a
7214 structure (pointed to by a value) of type TYPE to *DISPP (does not
7215 work for packed fields).
7217 Matches any field whose name has NAME as a prefix, possibly
7220 TYPE can be either a struct or union. If REFOK, TYPE may also
7221 be a (pointer or reference)+ to a struct or union, and the
7222 ultimate target type will be searched.
7224 Looks recursively into variant clauses and parent types.
7226 In the case of homonyms in the tagged types, please refer to the
7227 long explanation in find_struct_field's function documentation.
7229 If NOERR is nonzero, return NULL if NAME is not suitably defined or
7230 TYPE is not a type of the right kind. */
7232 static struct type
*
7233 ada_lookup_struct_elt_type (struct type
*type
, const char *name
, int refok
,
7237 int parent_offset
= -1;
7242 if (refok
&& type
!= NULL
)
7245 type
= ada_check_typedef (type
);
7246 if (type
->code () != TYPE_CODE_PTR
&& type
->code () != TYPE_CODE_REF
)
7248 type
= TYPE_TARGET_TYPE (type
);
7252 || (type
->code () != TYPE_CODE_STRUCT
7253 && type
->code () != TYPE_CODE_UNION
))
7258 error (_("Type %s is not a structure or union type"),
7259 type
!= NULL
? type_as_string (type
).c_str () : _("(null)"));
7262 type
= to_static_fixed_type (type
);
7264 for (i
= 0; i
< type
->num_fields (); i
+= 1)
7266 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
7269 if (t_field_name
== NULL
)
7272 else if (ada_is_parent_field (type
, i
))
7274 /* This is a field pointing us to the parent type of a tagged
7275 type. As hinted in this function's documentation, we give
7276 preference to fields in the current record first, so what
7277 we do here is just record the index of this field before
7278 we skip it. If it turns out we couldn't find our field
7279 in the current record, then we'll get back to it and search
7280 inside it whether the field might exist in the parent. */
7286 else if (field_name_match (t_field_name
, name
))
7287 return type
->field (i
).type ();
7289 else if (ada_is_wrapper_field (type
, i
))
7291 t
= ada_lookup_struct_elt_type (type
->field (i
).type (), name
,
7297 else if (ada_is_variant_part (type
, i
))
7300 struct type
*field_type
= ada_check_typedef (type
->field (i
).type ());
7302 for (j
= field_type
->num_fields () - 1; j
>= 0; j
-= 1)
7304 /* FIXME pnh 2008/01/26: We check for a field that is
7305 NOT wrapped in a struct, since the compiler sometimes
7306 generates these for unchecked variant types. Revisit
7307 if the compiler changes this practice. */
7308 const char *v_field_name
= TYPE_FIELD_NAME (field_type
, j
);
7310 if (v_field_name
!= NULL
7311 && field_name_match (v_field_name
, name
))
7312 t
= field_type
->field (j
).type ();
7314 t
= ada_lookup_struct_elt_type (field_type
->field (j
).type (),
7324 /* Field not found so far. If this is a tagged type which
7325 has a parent, try finding that field in the parent now. */
7327 if (parent_offset
!= -1)
7331 t
= ada_lookup_struct_elt_type (type
->field (parent_offset
).type (),
7340 const char *name_str
= name
!= NULL
? name
: _("<null>");
7342 error (_("Type %s has no component named %s"),
7343 type_as_string (type
).c_str (), name_str
);
7349 /* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7350 within a value of type OUTER_TYPE, return true iff VAR_TYPE
7351 represents an unchecked union (that is, the variant part of a
7352 record that is named in an Unchecked_Union pragma). */
7355 is_unchecked_variant (struct type
*var_type
, struct type
*outer_type
)
7357 const char *discrim_name
= ada_variant_discrim_name (var_type
);
7359 return (ada_lookup_struct_elt_type (outer_type
, discrim_name
, 0, 1) == NULL
);
7363 /* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
7364 within OUTER, determine which variant clause (field number in VAR_TYPE,
7365 numbering from 0) is applicable. Returns -1 if none are. */
7368 ada_which_variant_applies (struct type
*var_type
, struct value
*outer
)
7372 const char *discrim_name
= ada_variant_discrim_name (var_type
);
7373 struct value
*discrim
;
7374 LONGEST discrim_val
;
7376 /* Using plain value_from_contents_and_address here causes problems
7377 because we will end up trying to resolve a type that is currently
7378 being constructed. */
7379 discrim
= ada_value_struct_elt (outer
, discrim_name
, 1);
7380 if (discrim
== NULL
)
7382 discrim_val
= value_as_long (discrim
);
7385 for (i
= 0; i
< var_type
->num_fields (); i
+= 1)
7387 if (ada_is_others_clause (var_type
, i
))
7389 else if (ada_in_variant (discrim_val
, var_type
, i
))
7393 return others_clause
;
7398 /* Dynamic-Sized Records */
7400 /* Strategy: The type ostensibly attached to a value with dynamic size
7401 (i.e., a size that is not statically recorded in the debugging
7402 data) does not accurately reflect the size or layout of the value.
7403 Our strategy is to convert these values to values with accurate,
7404 conventional types that are constructed on the fly. */
7406 /* There is a subtle and tricky problem here. In general, we cannot
7407 determine the size of dynamic records without its data. However,
7408 the 'struct value' data structure, which GDB uses to represent
7409 quantities in the inferior process (the target), requires the size
7410 of the type at the time of its allocation in order to reserve space
7411 for GDB's internal copy of the data. That's why the
7412 'to_fixed_xxx_type' routines take (target) addresses as parameters,
7413 rather than struct value*s.
7415 However, GDB's internal history variables ($1, $2, etc.) are
7416 struct value*s containing internal copies of the data that are not, in
7417 general, the same as the data at their corresponding addresses in
7418 the target. Fortunately, the types we give to these values are all
7419 conventional, fixed-size types (as per the strategy described
7420 above), so that we don't usually have to perform the
7421 'to_fixed_xxx_type' conversions to look at their values.
7422 Unfortunately, there is one exception: if one of the internal
7423 history variables is an array whose elements are unconstrained
7424 records, then we will need to create distinct fixed types for each
7425 element selected. */
7427 /* The upshot of all of this is that many routines take a (type, host
7428 address, target address) triple as arguments to represent a value.
7429 The host address, if non-null, is supposed to contain an internal
7430 copy of the relevant data; otherwise, the program is to consult the
7431 target at the target address. */
7433 /* Assuming that VAL0 represents a pointer value, the result of
7434 dereferencing it. Differs from value_ind in its treatment of
7435 dynamic-sized types. */
7438 ada_value_ind (struct value
*val0
)
7440 struct value
*val
= value_ind (val0
);
7442 if (ada_is_tagged_type (value_type (val
), 0))
7443 val
= ada_tag_value_at_base_address (val
);
7445 return ada_to_fixed_value (val
);
7448 /* The value resulting from dereferencing any "reference to"
7449 qualifiers on VAL0. */
7451 static struct value
*
7452 ada_coerce_ref (struct value
*val0
)
7454 if (value_type (val0
)->code () == TYPE_CODE_REF
)
7456 struct value
*val
= val0
;
7458 val
= coerce_ref (val
);
7460 if (ada_is_tagged_type (value_type (val
), 0))
7461 val
= ada_tag_value_at_base_address (val
);
7463 return ada_to_fixed_value (val
);
7469 /* Return the bit alignment required for field #F of template type TYPE. */
7472 field_alignment (struct type
*type
, int f
)
7474 const char *name
= TYPE_FIELD_NAME (type
, f
);
7478 /* The field name should never be null, unless the debugging information
7479 is somehow malformed. In this case, we assume the field does not
7480 require any alignment. */
7484 len
= strlen (name
);
7486 if (!isdigit (name
[len
- 1]))
7489 if (isdigit (name
[len
- 2]))
7490 align_offset
= len
- 2;
7492 align_offset
= len
- 1;
7494 if (align_offset
< 7 || !startswith (name
+ align_offset
- 6, "___XV"))
7495 return TARGET_CHAR_BIT
;
7497 return atoi (name
+ align_offset
) * TARGET_CHAR_BIT
;
7500 /* Find a typedef or tag symbol named NAME. Ignores ambiguity. */
7502 static struct symbol
*
7503 ada_find_any_type_symbol (const char *name
)
7507 sym
= standard_lookup (name
, get_selected_block (NULL
), VAR_DOMAIN
);
7508 if (sym
!= NULL
&& SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
7511 sym
= standard_lookup (name
, NULL
, STRUCT_DOMAIN
);
7515 /* Find a type named NAME. Ignores ambiguity. This routine will look
7516 solely for types defined by debug info, it will not search the GDB
7519 static struct type
*
7520 ada_find_any_type (const char *name
)
7522 struct symbol
*sym
= ada_find_any_type_symbol (name
);
7525 return SYMBOL_TYPE (sym
);
7530 /* Given NAME_SYM and an associated BLOCK, find a "renaming" symbol
7531 associated with NAME_SYM's name. NAME_SYM may itself be a renaming
7532 symbol, in which case it is returned. Otherwise, this looks for
7533 symbols whose name is that of NAME_SYM suffixed with "___XR".
7534 Return symbol if found, and NULL otherwise. */
7537 ada_is_renaming_symbol (struct symbol
*name_sym
)
7539 const char *name
= name_sym
->linkage_name ();
7540 return strstr (name
, "___XR") != NULL
;
7543 /* Because of GNAT encoding conventions, several GDB symbols may match a
7544 given type name. If the type denoted by TYPE0 is to be preferred to
7545 that of TYPE1 for purposes of type printing, return non-zero;
7546 otherwise return 0. */
7549 ada_prefer_type (struct type
*type0
, struct type
*type1
)
7553 else if (type0
== NULL
)
7555 else if (type1
->code () == TYPE_CODE_VOID
)
7557 else if (type0
->code () == TYPE_CODE_VOID
)
7559 else if (type1
->name () == NULL
&& type0
->name () != NULL
)
7561 else if (ada_is_constrained_packed_array_type (type0
))
7563 else if (ada_is_array_descriptor_type (type0
)
7564 && !ada_is_array_descriptor_type (type1
))
7568 const char *type0_name
= type0
->name ();
7569 const char *type1_name
= type1
->name ();
7571 if (type0_name
!= NULL
&& strstr (type0_name
, "___XR") != NULL
7572 && (type1_name
== NULL
|| strstr (type1_name
, "___XR") == NULL
))
7578 /* The name of TYPE, which is its TYPE_NAME. Null if TYPE is
7582 ada_type_name (struct type
*type
)
7586 return type
->name ();
7589 /* Search the list of "descriptive" types associated to TYPE for a type
7590 whose name is NAME. */
7592 static struct type
*
7593 find_parallel_type_by_descriptive_type (struct type
*type
, const char *name
)
7595 struct type
*result
, *tmp
;
7597 if (ada_ignore_descriptive_types_p
)
7600 /* If there no descriptive-type info, then there is no parallel type
7602 if (!HAVE_GNAT_AUX_INFO (type
))
7605 result
= TYPE_DESCRIPTIVE_TYPE (type
);
7606 while (result
!= NULL
)
7608 const char *result_name
= ada_type_name (result
);
7610 if (result_name
== NULL
)
7612 warning (_("unexpected null name on descriptive type"));
7616 /* If the names match, stop. */
7617 if (strcmp (result_name
, name
) == 0)
7620 /* Otherwise, look at the next item on the list, if any. */
7621 if (HAVE_GNAT_AUX_INFO (result
))
7622 tmp
= TYPE_DESCRIPTIVE_TYPE (result
);
7626 /* If not found either, try after having resolved the typedef. */
7631 result
= check_typedef (result
);
7632 if (HAVE_GNAT_AUX_INFO (result
))
7633 result
= TYPE_DESCRIPTIVE_TYPE (result
);
7639 /* If we didn't find a match, see whether this is a packed array. With
7640 older compilers, the descriptive type information is either absent or
7641 irrelevant when it comes to packed arrays so the above lookup fails.
7642 Fall back to using a parallel lookup by name in this case. */
7643 if (result
== NULL
&& ada_is_constrained_packed_array_type (type
))
7644 return ada_find_any_type (name
);
7649 /* Find a parallel type to TYPE with the specified NAME, using the
7650 descriptive type taken from the debugging information, if available,
7651 and otherwise using the (slower) name-based method. */
7653 static struct type
*
7654 ada_find_parallel_type_with_name (struct type
*type
, const char *name
)
7656 struct type
*result
= NULL
;
7658 if (HAVE_GNAT_AUX_INFO (type
))
7659 result
= find_parallel_type_by_descriptive_type (type
, name
);
7661 result
= ada_find_any_type (name
);
7666 /* Same as above, but specify the name of the parallel type by appending
7667 SUFFIX to the name of TYPE. */
7670 ada_find_parallel_type (struct type
*type
, const char *suffix
)
7673 const char *type_name
= ada_type_name (type
);
7676 if (type_name
== NULL
)
7679 len
= strlen (type_name
);
7681 name
= (char *) alloca (len
+ strlen (suffix
) + 1);
7683 strcpy (name
, type_name
);
7684 strcpy (name
+ len
, suffix
);
7686 return ada_find_parallel_type_with_name (type
, name
);
7689 /* If TYPE is a variable-size record type, return the corresponding template
7690 type describing its fields. Otherwise, return NULL. */
7692 static struct type
*
7693 dynamic_template_type (struct type
*type
)
7695 type
= ada_check_typedef (type
);
7697 if (type
== NULL
|| type
->code () != TYPE_CODE_STRUCT
7698 || ada_type_name (type
) == NULL
)
7702 int len
= strlen (ada_type_name (type
));
7704 if (len
> 6 && strcmp (ada_type_name (type
) + len
- 6, "___XVE") == 0)
7707 return ada_find_parallel_type (type
, "___XVE");
7711 /* Assuming that TEMPL_TYPE is a union or struct type, returns
7712 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
7715 is_dynamic_field (struct type
*templ_type
, int field_num
)
7717 const char *name
= TYPE_FIELD_NAME (templ_type
, field_num
);
7720 && templ_type
->field (field_num
).type ()->code () == TYPE_CODE_PTR
7721 && strstr (name
, "___XVL") != NULL
;
7724 /* The index of the variant field of TYPE, or -1 if TYPE does not
7725 represent a variant record type. */
7728 variant_field_index (struct type
*type
)
7732 if (type
== NULL
|| type
->code () != TYPE_CODE_STRUCT
)
7735 for (f
= 0; f
< type
->num_fields (); f
+= 1)
7737 if (ada_is_variant_part (type
, f
))
7743 /* A record type with no fields. */
7745 static struct type
*
7746 empty_record (struct type
*templ
)
7748 struct type
*type
= alloc_type_copy (templ
);
7750 type
->set_code (TYPE_CODE_STRUCT
);
7751 INIT_NONE_SPECIFIC (type
);
7752 type
->set_name ("<empty>");
7753 TYPE_LENGTH (type
) = 0;
7757 /* An ordinary record type (with fixed-length fields) that describes
7758 the value of type TYPE at VALADDR or ADDRESS (see comments at
7759 the beginning of this section) VAL according to GNAT conventions.
7760 DVAL0 should describe the (portion of a) record that contains any
7761 necessary discriminants. It should be NULL if value_type (VAL) is
7762 an outer-level type (i.e., as opposed to a branch of a variant.) A
7763 variant field (unless unchecked) is replaced by a particular branch
7766 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
7767 length are not statically known are discarded. As a consequence,
7768 VALADDR, ADDRESS and DVAL0 are ignored.
7770 NOTE: Limitations: For now, we assume that dynamic fields and
7771 variants occupy whole numbers of bytes. However, they need not be
7775 ada_template_to_fixed_record_type_1 (struct type
*type
,
7776 const gdb_byte
*valaddr
,
7777 CORE_ADDR address
, struct value
*dval0
,
7778 int keep_dynamic_fields
)
7780 struct value
*mark
= value_mark ();
7783 int nfields
, bit_len
;
7789 /* Compute the number of fields in this record type that are going
7790 to be processed: unless keep_dynamic_fields, this includes only
7791 fields whose position and length are static will be processed. */
7792 if (keep_dynamic_fields
)
7793 nfields
= type
->num_fields ();
7797 while (nfields
< type
->num_fields ()
7798 && !ada_is_variant_part (type
, nfields
)
7799 && !is_dynamic_field (type
, nfields
))
7803 rtype
= alloc_type_copy (type
);
7804 rtype
->set_code (TYPE_CODE_STRUCT
);
7805 INIT_NONE_SPECIFIC (rtype
);
7806 rtype
->set_num_fields (nfields
);
7808 ((struct field
*) TYPE_ZALLOC (rtype
, nfields
* sizeof (struct field
)));
7809 rtype
->set_name (ada_type_name (type
));
7810 rtype
->set_is_fixed_instance (true);
7816 for (f
= 0; f
< nfields
; f
+= 1)
7818 off
= align_up (off
, field_alignment (type
, f
))
7819 + TYPE_FIELD_BITPOS (type
, f
);
7820 SET_FIELD_BITPOS (rtype
->field (f
), off
);
7821 TYPE_FIELD_BITSIZE (rtype
, f
) = 0;
7823 if (ada_is_variant_part (type
, f
))
7828 else if (is_dynamic_field (type
, f
))
7830 const gdb_byte
*field_valaddr
= valaddr
;
7831 CORE_ADDR field_address
= address
;
7832 struct type
*field_type
=
7833 TYPE_TARGET_TYPE (type
->field (f
).type ());
7837 /* rtype's length is computed based on the run-time
7838 value of discriminants. If the discriminants are not
7839 initialized, the type size may be completely bogus and
7840 GDB may fail to allocate a value for it. So check the
7841 size first before creating the value. */
7842 ada_ensure_varsize_limit (rtype
);
7843 /* Using plain value_from_contents_and_address here
7844 causes problems because we will end up trying to
7845 resolve a type that is currently being
7847 dval
= value_from_contents_and_address_unresolved (rtype
,
7850 rtype
= value_type (dval
);
7855 /* If the type referenced by this field is an aligner type, we need
7856 to unwrap that aligner type, because its size might not be set.
7857 Keeping the aligner type would cause us to compute the wrong
7858 size for this field, impacting the offset of the all the fields
7859 that follow this one. */
7860 if (ada_is_aligner_type (field_type
))
7862 long field_offset
= TYPE_FIELD_BITPOS (field_type
, f
);
7864 field_valaddr
= cond_offset_host (field_valaddr
, field_offset
);
7865 field_address
= cond_offset_target (field_address
, field_offset
);
7866 field_type
= ada_aligned_type (field_type
);
7869 field_valaddr
= cond_offset_host (field_valaddr
,
7870 off
/ TARGET_CHAR_BIT
);
7871 field_address
= cond_offset_target (field_address
,
7872 off
/ TARGET_CHAR_BIT
);
7874 /* Get the fixed type of the field. Note that, in this case,
7875 we do not want to get the real type out of the tag: if
7876 the current field is the parent part of a tagged record,
7877 we will get the tag of the object. Clearly wrong: the real
7878 type of the parent is not the real type of the child. We
7879 would end up in an infinite loop. */
7880 field_type
= ada_get_base_type (field_type
);
7881 field_type
= ada_to_fixed_type (field_type
, field_valaddr
,
7882 field_address
, dval
, 0);
7883 /* If the field size is already larger than the maximum
7884 object size, then the record itself will necessarily
7885 be larger than the maximum object size. We need to make
7886 this check now, because the size might be so ridiculously
7887 large (due to an uninitialized variable in the inferior)
7888 that it would cause an overflow when adding it to the
7890 ada_ensure_varsize_limit (field_type
);
7892 rtype
->field (f
).set_type (field_type
);
7893 TYPE_FIELD_NAME (rtype
, f
) = TYPE_FIELD_NAME (type
, f
);
7894 /* The multiplication can potentially overflow. But because
7895 the field length has been size-checked just above, and
7896 assuming that the maximum size is a reasonable value,
7897 an overflow should not happen in practice. So rather than
7898 adding overflow recovery code to this already complex code,
7899 we just assume that it's not going to happen. */
7901 TYPE_LENGTH (rtype
->field (f
).type ()) * TARGET_CHAR_BIT
;
7905 /* Note: If this field's type is a typedef, it is important
7906 to preserve the typedef layer.
7908 Otherwise, we might be transforming a typedef to a fat
7909 pointer (encoding a pointer to an unconstrained array),
7910 into a basic fat pointer (encoding an unconstrained
7911 array). As both types are implemented using the same
7912 structure, the typedef is the only clue which allows us
7913 to distinguish between the two options. Stripping it
7914 would prevent us from printing this field appropriately. */
7915 rtype
->field (f
).set_type (type
->field (f
).type ());
7916 TYPE_FIELD_NAME (rtype
, f
) = TYPE_FIELD_NAME (type
, f
);
7917 if (TYPE_FIELD_BITSIZE (type
, f
) > 0)
7919 TYPE_FIELD_BITSIZE (rtype
, f
) = TYPE_FIELD_BITSIZE (type
, f
);
7922 struct type
*field_type
= type
->field (f
).type ();
7924 /* We need to be careful of typedefs when computing
7925 the length of our field. If this is a typedef,
7926 get the length of the target type, not the length
7928 if (field_type
->code () == TYPE_CODE_TYPEDEF
)
7929 field_type
= ada_typedef_target_type (field_type
);
7932 TYPE_LENGTH (ada_check_typedef (field_type
)) * TARGET_CHAR_BIT
;
7935 if (off
+ fld_bit_len
> bit_len
)
7936 bit_len
= off
+ fld_bit_len
;
7938 TYPE_LENGTH (rtype
) =
7939 align_up (bit_len
, TARGET_CHAR_BIT
) / TARGET_CHAR_BIT
;
7942 /* We handle the variant part, if any, at the end because of certain
7943 odd cases in which it is re-ordered so as NOT to be the last field of
7944 the record. This can happen in the presence of representation
7946 if (variant_field
>= 0)
7948 struct type
*branch_type
;
7950 off
= TYPE_FIELD_BITPOS (rtype
, variant_field
);
7954 /* Using plain value_from_contents_and_address here causes
7955 problems because we will end up trying to resolve a type
7956 that is currently being constructed. */
7957 dval
= value_from_contents_and_address_unresolved (rtype
, valaddr
,
7959 rtype
= value_type (dval
);
7965 to_fixed_variant_branch_type
7966 (type
->field (variant_field
).type (),
7967 cond_offset_host (valaddr
, off
/ TARGET_CHAR_BIT
),
7968 cond_offset_target (address
, off
/ TARGET_CHAR_BIT
), dval
);
7969 if (branch_type
== NULL
)
7971 for (f
= variant_field
+ 1; f
< rtype
->num_fields (); f
+= 1)
7972 rtype
->field (f
- 1) = rtype
->field (f
);
7973 rtype
->set_num_fields (rtype
->num_fields () - 1);
7977 rtype
->field (variant_field
).set_type (branch_type
);
7978 TYPE_FIELD_NAME (rtype
, variant_field
) = "S";
7980 TYPE_LENGTH (rtype
->field (variant_field
).type ()) *
7982 if (off
+ fld_bit_len
> bit_len
)
7983 bit_len
= off
+ fld_bit_len
;
7984 TYPE_LENGTH (rtype
) =
7985 align_up (bit_len
, TARGET_CHAR_BIT
) / TARGET_CHAR_BIT
;
7989 /* According to exp_dbug.ads, the size of TYPE for variable-size records
7990 should contain the alignment of that record, which should be a strictly
7991 positive value. If null or negative, then something is wrong, most
7992 probably in the debug info. In that case, we don't round up the size
7993 of the resulting type. If this record is not part of another structure,
7994 the current RTYPE length might be good enough for our purposes. */
7995 if (TYPE_LENGTH (type
) <= 0)
7998 warning (_("Invalid type size for `%s' detected: %s."),
7999 rtype
->name (), pulongest (TYPE_LENGTH (type
)));
8001 warning (_("Invalid type size for <unnamed> detected: %s."),
8002 pulongest (TYPE_LENGTH (type
)));
8006 TYPE_LENGTH (rtype
) = align_up (TYPE_LENGTH (rtype
),
8007 TYPE_LENGTH (type
));
8010 value_free_to_mark (mark
);
8011 if (TYPE_LENGTH (rtype
) > varsize_limit
)
8012 error (_("record type with dynamic size is larger than varsize-limit"));
8016 /* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
8019 static struct type
*
8020 template_to_fixed_record_type (struct type
*type
, const gdb_byte
*valaddr
,
8021 CORE_ADDR address
, struct value
*dval0
)
8023 return ada_template_to_fixed_record_type_1 (type
, valaddr
,
8027 /* An ordinary record type in which ___XVL-convention fields and
8028 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
8029 static approximations, containing all possible fields. Uses
8030 no runtime values. Useless for use in values, but that's OK,
8031 since the results are used only for type determinations. Works on both
8032 structs and unions. Representation note: to save space, we memorize
8033 the result of this function in the TYPE_TARGET_TYPE of the
8036 static struct type
*
8037 template_to_static_fixed_type (struct type
*type0
)
8043 /* No need no do anything if the input type is already fixed. */
8044 if (type0
->is_fixed_instance ())
8047 /* Likewise if we already have computed the static approximation. */
8048 if (TYPE_TARGET_TYPE (type0
) != NULL
)
8049 return TYPE_TARGET_TYPE (type0
);
8051 /* Don't clone TYPE0 until we are sure we are going to need a copy. */
8053 nfields
= type0
->num_fields ();
8055 /* Whether or not we cloned TYPE0, cache the result so that we don't do
8056 recompute all over next time. */
8057 TYPE_TARGET_TYPE (type0
) = type
;
8059 for (f
= 0; f
< nfields
; f
+= 1)
8061 struct type
*field_type
= type0
->field (f
).type ();
8062 struct type
*new_type
;
8064 if (is_dynamic_field (type0
, f
))
8066 field_type
= ada_check_typedef (field_type
);
8067 new_type
= to_static_fixed_type (TYPE_TARGET_TYPE (field_type
));
8070 new_type
= static_unwrap_type (field_type
);
8072 if (new_type
!= field_type
)
8074 /* Clone TYPE0 only the first time we get a new field type. */
8077 TYPE_TARGET_TYPE (type0
) = type
= alloc_type_copy (type0
);
8078 type
->set_code (type0
->code ());
8079 INIT_NONE_SPECIFIC (type
);
8080 type
->set_num_fields (nfields
);
8084 TYPE_ALLOC (type
, nfields
* sizeof (struct field
)));
8085 memcpy (fields
, type0
->fields (),
8086 sizeof (struct field
) * nfields
);
8087 type
->set_fields (fields
);
8089 type
->set_name (ada_type_name (type0
));
8090 type
->set_is_fixed_instance (true);
8091 TYPE_LENGTH (type
) = 0;
8093 type
->field (f
).set_type (new_type
);
8094 TYPE_FIELD_NAME (type
, f
) = TYPE_FIELD_NAME (type0
, f
);
8101 /* Given an object of type TYPE whose contents are at VALADDR and
8102 whose address in memory is ADDRESS, returns a revision of TYPE,
8103 which should be a non-dynamic-sized record, in which the variant
8104 part, if any, is replaced with the appropriate branch. Looks
8105 for discriminant values in DVAL0, which can be NULL if the record
8106 contains the necessary discriminant values. */
8108 static struct type
*
8109 to_record_with_fixed_variant_part (struct type
*type
, const gdb_byte
*valaddr
,
8110 CORE_ADDR address
, struct value
*dval0
)
8112 struct value
*mark
= value_mark ();
8115 struct type
*branch_type
;
8116 int nfields
= type
->num_fields ();
8117 int variant_field
= variant_field_index (type
);
8119 if (variant_field
== -1)
8124 dval
= value_from_contents_and_address (type
, valaddr
, address
);
8125 type
= value_type (dval
);
8130 rtype
= alloc_type_copy (type
);
8131 rtype
->set_code (TYPE_CODE_STRUCT
);
8132 INIT_NONE_SPECIFIC (rtype
);
8133 rtype
->set_num_fields (nfields
);
8136 (struct field
*) TYPE_ALLOC (rtype
, nfields
* sizeof (struct field
));
8137 memcpy (fields
, type
->fields (), sizeof (struct field
) * nfields
);
8138 rtype
->set_fields (fields
);
8140 rtype
->set_name (ada_type_name (type
));
8141 rtype
->set_is_fixed_instance (true);
8142 TYPE_LENGTH (rtype
) = TYPE_LENGTH (type
);
8144 branch_type
= to_fixed_variant_branch_type
8145 (type
->field (variant_field
).type (),
8146 cond_offset_host (valaddr
,
8147 TYPE_FIELD_BITPOS (type
, variant_field
)
8149 cond_offset_target (address
,
8150 TYPE_FIELD_BITPOS (type
, variant_field
)
8151 / TARGET_CHAR_BIT
), dval
);
8152 if (branch_type
== NULL
)
8156 for (f
= variant_field
+ 1; f
< nfields
; f
+= 1)
8157 rtype
->field (f
- 1) = rtype
->field (f
);
8158 rtype
->set_num_fields (rtype
->num_fields () - 1);
8162 rtype
->field (variant_field
).set_type (branch_type
);
8163 TYPE_FIELD_NAME (rtype
, variant_field
) = "S";
8164 TYPE_FIELD_BITSIZE (rtype
, variant_field
) = 0;
8165 TYPE_LENGTH (rtype
) += TYPE_LENGTH (branch_type
);
8167 TYPE_LENGTH (rtype
) -= TYPE_LENGTH (type
->field (variant_field
).type ());
8169 value_free_to_mark (mark
);
8173 /* An ordinary record type (with fixed-length fields) that describes
8174 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
8175 beginning of this section]. Any necessary discriminants' values
8176 should be in DVAL, a record value; it may be NULL if the object
8177 at ADDR itself contains any necessary discriminant values.
8178 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
8179 values from the record are needed. Except in the case that DVAL,
8180 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
8181 unchecked) is replaced by a particular branch of the variant.
8183 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
8184 is questionable and may be removed. It can arise during the
8185 processing of an unconstrained-array-of-record type where all the
8186 variant branches have exactly the same size. This is because in
8187 such cases, the compiler does not bother to use the XVS convention
8188 when encoding the record. I am currently dubious of this
8189 shortcut and suspect the compiler should be altered. FIXME. */
8191 static struct type
*
8192 to_fixed_record_type (struct type
*type0
, const gdb_byte
*valaddr
,
8193 CORE_ADDR address
, struct value
*dval
)
8195 struct type
*templ_type
;
8197 if (type0
->is_fixed_instance ())
8200 templ_type
= dynamic_template_type (type0
);
8202 if (templ_type
!= NULL
)
8203 return template_to_fixed_record_type (templ_type
, valaddr
, address
, dval
);
8204 else if (variant_field_index (type0
) >= 0)
8206 if (dval
== NULL
&& valaddr
== NULL
&& address
== 0)
8208 return to_record_with_fixed_variant_part (type0
, valaddr
, address
,
8213 type0
->set_is_fixed_instance (true);
8219 /* An ordinary record type (with fixed-length fields) that describes
8220 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
8221 union type. Any necessary discriminants' values should be in DVAL,
8222 a record value. That is, this routine selects the appropriate
8223 branch of the union at ADDR according to the discriminant value
8224 indicated in the union's type name. Returns VAR_TYPE0 itself if
8225 it represents a variant subject to a pragma Unchecked_Union. */
8227 static struct type
*
8228 to_fixed_variant_branch_type (struct type
*var_type0
, const gdb_byte
*valaddr
,
8229 CORE_ADDR address
, struct value
*dval
)
8232 struct type
*templ_type
;
8233 struct type
*var_type
;
8235 if (var_type0
->code () == TYPE_CODE_PTR
)
8236 var_type
= TYPE_TARGET_TYPE (var_type0
);
8238 var_type
= var_type0
;
8240 templ_type
= ada_find_parallel_type (var_type
, "___XVU");
8242 if (templ_type
!= NULL
)
8243 var_type
= templ_type
;
8245 if (is_unchecked_variant (var_type
, value_type (dval
)))
8247 which
= ada_which_variant_applies (var_type
, dval
);
8250 return empty_record (var_type
);
8251 else if (is_dynamic_field (var_type
, which
))
8252 return to_fixed_record_type
8253 (TYPE_TARGET_TYPE (var_type
->field (which
).type ()),
8254 valaddr
, address
, dval
);
8255 else if (variant_field_index (var_type
->field (which
).type ()) >= 0)
8257 to_fixed_record_type
8258 (var_type
->field (which
).type (), valaddr
, address
, dval
);
8260 return var_type
->field (which
).type ();
8263 /* Assuming RANGE_TYPE is a TYPE_CODE_RANGE, return nonzero if
8264 ENCODING_TYPE, a type following the GNAT conventions for discrete
8265 type encodings, only carries redundant information. */
8268 ada_is_redundant_range_encoding (struct type
*range_type
,
8269 struct type
*encoding_type
)
8271 const char *bounds_str
;
8275 gdb_assert (range_type
->code () == TYPE_CODE_RANGE
);
8277 if (get_base_type (range_type
)->code ()
8278 != get_base_type (encoding_type
)->code ())
8280 /* The compiler probably used a simple base type to describe
8281 the range type instead of the range's actual base type,
8282 expecting us to get the real base type from the encoding
8283 anyway. In this situation, the encoding cannot be ignored
8288 if (is_dynamic_type (range_type
))
8291 if (encoding_type
->name () == NULL
)
8294 bounds_str
= strstr (encoding_type
->name (), "___XDLU_");
8295 if (bounds_str
== NULL
)
8298 n
= 8; /* Skip "___XDLU_". */
8299 if (!ada_scan_number (bounds_str
, n
, &lo
, &n
))
8301 if (range_type
->bounds ()->low
.const_val () != lo
)
8304 n
+= 2; /* Skip the "__" separator between the two bounds. */
8305 if (!ada_scan_number (bounds_str
, n
, &hi
, &n
))
8307 if (range_type
->bounds ()->high
.const_val () != hi
)
8313 /* Given the array type ARRAY_TYPE, return nonzero if DESC_TYPE,
8314 a type following the GNAT encoding for describing array type
8315 indices, only carries redundant information. */
8318 ada_is_redundant_index_type_desc (struct type
*array_type
,
8319 struct type
*desc_type
)
8321 struct type
*this_layer
= check_typedef (array_type
);
8324 for (i
= 0; i
< desc_type
->num_fields (); i
++)
8326 if (!ada_is_redundant_range_encoding (this_layer
->index_type (),
8327 desc_type
->field (i
).type ()))
8329 this_layer
= check_typedef (TYPE_TARGET_TYPE (this_layer
));
8335 /* Assuming that TYPE0 is an array type describing the type of a value
8336 at ADDR, and that DVAL describes a record containing any
8337 discriminants used in TYPE0, returns a type for the value that
8338 contains no dynamic components (that is, no components whose sizes
8339 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
8340 true, gives an error message if the resulting type's size is over
8343 static struct type
*
8344 to_fixed_array_type (struct type
*type0
, struct value
*dval
,
8347 struct type
*index_type_desc
;
8348 struct type
*result
;
8349 int constrained_packed_array_p
;
8350 static const char *xa_suffix
= "___XA";
8352 type0
= ada_check_typedef (type0
);
8353 if (type0
->is_fixed_instance ())
8356 constrained_packed_array_p
= ada_is_constrained_packed_array_type (type0
);
8357 if (constrained_packed_array_p
)
8359 type0
= decode_constrained_packed_array_type (type0
);
8360 if (type0
== nullptr)
8361 error (_("could not decode constrained packed array type"));
8364 index_type_desc
= ada_find_parallel_type (type0
, xa_suffix
);
8366 /* As mentioned in exp_dbug.ads, for non bit-packed arrays an
8367 encoding suffixed with 'P' may still be generated. If so,
8368 it should be used to find the XA type. */
8370 if (index_type_desc
== NULL
)
8372 const char *type_name
= ada_type_name (type0
);
8374 if (type_name
!= NULL
)
8376 const int len
= strlen (type_name
);
8377 char *name
= (char *) alloca (len
+ strlen (xa_suffix
));
8379 if (type_name
[len
- 1] == 'P')
8381 strcpy (name
, type_name
);
8382 strcpy (name
+ len
- 1, xa_suffix
);
8383 index_type_desc
= ada_find_parallel_type_with_name (type0
, name
);
8388 ada_fixup_array_indexes_type (index_type_desc
);
8389 if (index_type_desc
!= NULL
8390 && ada_is_redundant_index_type_desc (type0
, index_type_desc
))
8392 /* Ignore this ___XA parallel type, as it does not bring any
8393 useful information. This allows us to avoid creating fixed
8394 versions of the array's index types, which would be identical
8395 to the original ones. This, in turn, can also help avoid
8396 the creation of fixed versions of the array itself. */
8397 index_type_desc
= NULL
;
8400 if (index_type_desc
== NULL
)
8402 struct type
*elt_type0
= ada_check_typedef (TYPE_TARGET_TYPE (type0
));
8404 /* NOTE: elt_type---the fixed version of elt_type0---should never
8405 depend on the contents of the array in properly constructed
8407 /* Create a fixed version of the array element type.
8408 We're not providing the address of an element here,
8409 and thus the actual object value cannot be inspected to do
8410 the conversion. This should not be a problem, since arrays of
8411 unconstrained objects are not allowed. In particular, all
8412 the elements of an array of a tagged type should all be of
8413 the same type specified in the debugging info. No need to
8414 consult the object tag. */
8415 struct type
*elt_type
= ada_to_fixed_type (elt_type0
, 0, 0, dval
, 1);
8417 /* Make sure we always create a new array type when dealing with
8418 packed array types, since we're going to fix-up the array
8419 type length and element bitsize a little further down. */
8420 if (elt_type0
== elt_type
&& !constrained_packed_array_p
)
8423 result
= create_array_type (alloc_type_copy (type0
),
8424 elt_type
, type0
->index_type ());
8429 struct type
*elt_type0
;
8432 for (i
= index_type_desc
->num_fields (); i
> 0; i
-= 1)
8433 elt_type0
= TYPE_TARGET_TYPE (elt_type0
);
8435 /* NOTE: result---the fixed version of elt_type0---should never
8436 depend on the contents of the array in properly constructed
8438 /* Create a fixed version of the array element type.
8439 We're not providing the address of an element here,
8440 and thus the actual object value cannot be inspected to do
8441 the conversion. This should not be a problem, since arrays of
8442 unconstrained objects are not allowed. In particular, all
8443 the elements of an array of a tagged type should all be of
8444 the same type specified in the debugging info. No need to
8445 consult the object tag. */
8447 ada_to_fixed_type (ada_check_typedef (elt_type0
), 0, 0, dval
, 1);
8450 for (i
= index_type_desc
->num_fields () - 1; i
>= 0; i
-= 1)
8452 struct type
*range_type
=
8453 to_fixed_range_type (index_type_desc
->field (i
).type (), dval
);
8455 result
= create_array_type (alloc_type_copy (elt_type0
),
8456 result
, range_type
);
8457 elt_type0
= TYPE_TARGET_TYPE (elt_type0
);
8459 if (!ignore_too_big
&& TYPE_LENGTH (result
) > varsize_limit
)
8460 error (_("array type with dynamic size is larger than varsize-limit"));
8463 /* We want to preserve the type name. This can be useful when
8464 trying to get the type name of a value that has already been
8465 printed (for instance, if the user did "print VAR; whatis $". */
8466 result
->set_name (type0
->name ());
8468 if (constrained_packed_array_p
)
8470 /* So far, the resulting type has been created as if the original
8471 type was a regular (non-packed) array type. As a result, the
8472 bitsize of the array elements needs to be set again, and the array
8473 length needs to be recomputed based on that bitsize. */
8474 int len
= TYPE_LENGTH (result
) / TYPE_LENGTH (TYPE_TARGET_TYPE (result
));
8475 int elt_bitsize
= TYPE_FIELD_BITSIZE (type0
, 0);
8477 TYPE_FIELD_BITSIZE (result
, 0) = TYPE_FIELD_BITSIZE (type0
, 0);
8478 TYPE_LENGTH (result
) = len
* elt_bitsize
/ HOST_CHAR_BIT
;
8479 if (TYPE_LENGTH (result
) * HOST_CHAR_BIT
< len
* elt_bitsize
)
8480 TYPE_LENGTH (result
)++;
8483 result
->set_is_fixed_instance (true);
8488 /* A standard type (containing no dynamically sized components)
8489 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
8490 DVAL describes a record containing any discriminants used in TYPE0,
8491 and may be NULL if there are none, or if the object of type TYPE at
8492 ADDRESS or in VALADDR contains these discriminants.
8494 If CHECK_TAG is not null, in the case of tagged types, this function
8495 attempts to locate the object's tag and use it to compute the actual
8496 type. However, when ADDRESS is null, we cannot use it to determine the
8497 location of the tag, and therefore compute the tagged type's actual type.
8498 So we return the tagged type without consulting the tag. */
8500 static struct type
*
8501 ada_to_fixed_type_1 (struct type
*type
, const gdb_byte
*valaddr
,
8502 CORE_ADDR address
, struct value
*dval
, int check_tag
)
8504 type
= ada_check_typedef (type
);
8506 /* Only un-fixed types need to be handled here. */
8507 if (!HAVE_GNAT_AUX_INFO (type
))
8510 switch (type
->code ())
8514 case TYPE_CODE_STRUCT
:
8516 struct type
*static_type
= to_static_fixed_type (type
);
8517 struct type
*fixed_record_type
=
8518 to_fixed_record_type (type
, valaddr
, address
, NULL
);
8520 /* If STATIC_TYPE is a tagged type and we know the object's address,
8521 then we can determine its tag, and compute the object's actual
8522 type from there. Note that we have to use the fixed record
8523 type (the parent part of the record may have dynamic fields
8524 and the way the location of _tag is expressed may depend on
8527 if (check_tag
&& address
!= 0 && ada_is_tagged_type (static_type
, 0))
8530 value_tag_from_contents_and_address
8534 struct type
*real_type
= type_from_tag (tag
);
8536 value_from_contents_and_address (fixed_record_type
,
8539 fixed_record_type
= value_type (obj
);
8540 if (real_type
!= NULL
)
8541 return to_fixed_record_type
8543 value_address (ada_tag_value_at_base_address (obj
)), NULL
);
8546 /* Check to see if there is a parallel ___XVZ variable.
8547 If there is, then it provides the actual size of our type. */
8548 else if (ada_type_name (fixed_record_type
) != NULL
)
8550 const char *name
= ada_type_name (fixed_record_type
);
8552 = (char *) alloca (strlen (name
) + 7 /* "___XVZ\0" */);
8553 bool xvz_found
= false;
8556 xsnprintf (xvz_name
, strlen (name
) + 7, "%s___XVZ", name
);
8559 xvz_found
= get_int_var_value (xvz_name
, size
);
8561 catch (const gdb_exception_error
&except
)
8563 /* We found the variable, but somehow failed to read
8564 its value. Rethrow the same error, but with a little
8565 bit more information, to help the user understand
8566 what went wrong (Eg: the variable might have been
8568 throw_error (except
.error
,
8569 _("unable to read value of %s (%s)"),
8570 xvz_name
, except
.what ());
8573 if (xvz_found
&& TYPE_LENGTH (fixed_record_type
) != size
)
8575 fixed_record_type
= copy_type (fixed_record_type
);
8576 TYPE_LENGTH (fixed_record_type
) = size
;
8578 /* The FIXED_RECORD_TYPE may have be a stub. We have
8579 observed this when the debugging info is STABS, and
8580 apparently it is something that is hard to fix.
8582 In practice, we don't need the actual type definition
8583 at all, because the presence of the XVZ variable allows us
8584 to assume that there must be a XVS type as well, which we
8585 should be able to use later, when we need the actual type
8588 In the meantime, pretend that the "fixed" type we are
8589 returning is NOT a stub, because this can cause trouble
8590 when using this type to create new types targeting it.
8591 Indeed, the associated creation routines often check
8592 whether the target type is a stub and will try to replace
8593 it, thus using a type with the wrong size. This, in turn,
8594 might cause the new type to have the wrong size too.
8595 Consider the case of an array, for instance, where the size
8596 of the array is computed from the number of elements in
8597 our array multiplied by the size of its element. */
8598 fixed_record_type
->set_is_stub (false);
8601 return fixed_record_type
;
8603 case TYPE_CODE_ARRAY
:
8604 return to_fixed_array_type (type
, dval
, 1);
8605 case TYPE_CODE_UNION
:
8609 return to_fixed_variant_branch_type (type
, valaddr
, address
, dval
);
8613 /* The same as ada_to_fixed_type_1, except that it preserves the type
8614 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
8616 The typedef layer needs be preserved in order to differentiate between
8617 arrays and array pointers when both types are implemented using the same
8618 fat pointer. In the array pointer case, the pointer is encoded as
8619 a typedef of the pointer type. For instance, considering:
8621 type String_Access is access String;
8622 S1 : String_Access := null;
8624 To the debugger, S1 is defined as a typedef of type String. But
8625 to the user, it is a pointer. So if the user tries to print S1,
8626 we should not dereference the array, but print the array address
8629 If we didn't preserve the typedef layer, we would lose the fact that
8630 the type is to be presented as a pointer (needs de-reference before
8631 being printed). And we would also use the source-level type name. */
8634 ada_to_fixed_type (struct type
*type
, const gdb_byte
*valaddr
,
8635 CORE_ADDR address
, struct value
*dval
, int check_tag
)
8638 struct type
*fixed_type
=
8639 ada_to_fixed_type_1 (type
, valaddr
, address
, dval
, check_tag
);
8641 /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
8642 then preserve the typedef layer.
8644 Implementation note: We can only check the main-type portion of
8645 the TYPE and FIXED_TYPE, because eliminating the typedef layer
8646 from TYPE now returns a type that has the same instance flags
8647 as TYPE. For instance, if TYPE is a "typedef const", and its
8648 target type is a "struct", then the typedef elimination will return
8649 a "const" version of the target type. See check_typedef for more
8650 details about how the typedef layer elimination is done.
8652 brobecker/2010-11-19: It seems to me that the only case where it is
8653 useful to preserve the typedef layer is when dealing with fat pointers.
8654 Perhaps, we could add a check for that and preserve the typedef layer
8655 only in that situation. But this seems unnecessary so far, probably
8656 because we call check_typedef/ada_check_typedef pretty much everywhere.
8658 if (type
->code () == TYPE_CODE_TYPEDEF
8659 && (TYPE_MAIN_TYPE (ada_typedef_target_type (type
))
8660 == TYPE_MAIN_TYPE (fixed_type
)))
8666 /* A standard (static-sized) type corresponding as well as possible to
8667 TYPE0, but based on no runtime data. */
8669 static struct type
*
8670 to_static_fixed_type (struct type
*type0
)
8677 if (type0
->is_fixed_instance ())
8680 type0
= ada_check_typedef (type0
);
8682 switch (type0
->code ())
8686 case TYPE_CODE_STRUCT
:
8687 type
= dynamic_template_type (type0
);
8689 return template_to_static_fixed_type (type
);
8691 return template_to_static_fixed_type (type0
);
8692 case TYPE_CODE_UNION
:
8693 type
= ada_find_parallel_type (type0
, "___XVU");
8695 return template_to_static_fixed_type (type
);
8697 return template_to_static_fixed_type (type0
);
8701 /* A static approximation of TYPE with all type wrappers removed. */
8703 static struct type
*
8704 static_unwrap_type (struct type
*type
)
8706 if (ada_is_aligner_type (type
))
8708 struct type
*type1
= ada_check_typedef (type
)->field (0).type ();
8709 if (ada_type_name (type1
) == NULL
)
8710 type1
->set_name (ada_type_name (type
));
8712 return static_unwrap_type (type1
);
8716 struct type
*raw_real_type
= ada_get_base_type (type
);
8718 if (raw_real_type
== type
)
8721 return to_static_fixed_type (raw_real_type
);
8725 /* In some cases, incomplete and private types require
8726 cross-references that are not resolved as records (for example,
8728 type FooP is access Foo;
8730 type Foo is array ...;
8731 ). In these cases, since there is no mechanism for producing
8732 cross-references to such types, we instead substitute for FooP a
8733 stub enumeration type that is nowhere resolved, and whose tag is
8734 the name of the actual type. Call these types "non-record stubs". */
8736 /* A type equivalent to TYPE that is not a non-record stub, if one
8737 exists, otherwise TYPE. */
8740 ada_check_typedef (struct type
*type
)
8745 /* If our type is an access to an unconstrained array, which is encoded
8746 as a TYPE_CODE_TYPEDEF of a fat pointer, then we're done.
8747 We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
8748 what allows us to distinguish between fat pointers that represent
8749 array types, and fat pointers that represent array access types
8750 (in both cases, the compiler implements them as fat pointers). */
8751 if (ada_is_access_to_unconstrained_array (type
))
8754 type
= check_typedef (type
);
8755 if (type
== NULL
|| type
->code () != TYPE_CODE_ENUM
8756 || !type
->is_stub ()
8757 || type
->name () == NULL
)
8761 const char *name
= type
->name ();
8762 struct type
*type1
= ada_find_any_type (name
);
8767 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
8768 stubs pointing to arrays, as we don't create symbols for array
8769 types, only for the typedef-to-array types). If that's the case,
8770 strip the typedef layer. */
8771 if (type1
->code () == TYPE_CODE_TYPEDEF
)
8772 type1
= ada_check_typedef (type1
);
8778 /* A value representing the data at VALADDR/ADDRESS as described by
8779 type TYPE0, but with a standard (static-sized) type that correctly
8780 describes it. If VAL0 is not NULL and TYPE0 already is a standard
8781 type, then return VAL0 [this feature is simply to avoid redundant
8782 creation of struct values]. */
8784 static struct value
*
8785 ada_to_fixed_value_create (struct type
*type0
, CORE_ADDR address
,
8788 struct type
*type
= ada_to_fixed_type (type0
, 0, address
, NULL
, 1);
8790 if (type
== type0
&& val0
!= NULL
)
8793 if (VALUE_LVAL (val0
) != lval_memory
)
8795 /* Our value does not live in memory; it could be a convenience
8796 variable, for instance. Create a not_lval value using val0's
8798 return value_from_contents (type
, value_contents (val0
));
8801 return value_from_contents_and_address (type
, 0, address
);
8804 /* A value representing VAL, but with a standard (static-sized) type
8805 that correctly describes it. Does not necessarily create a new
8809 ada_to_fixed_value (struct value
*val
)
8811 val
= unwrap_value (val
);
8812 val
= ada_to_fixed_value_create (value_type (val
), value_address (val
), val
);
8819 /* Table mapping attribute numbers to names.
8820 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
8822 static const char * const attribute_names
[] = {
8840 ada_attribute_name (enum exp_opcode n
)
8842 if (n
>= OP_ATR_FIRST
&& n
<= (int) OP_ATR_VAL
)
8843 return attribute_names
[n
- OP_ATR_FIRST
+ 1];
8845 return attribute_names
[0];
8848 /* Evaluate the 'POS attribute applied to ARG. */
8851 pos_atr (struct value
*arg
)
8853 struct value
*val
= coerce_ref (arg
);
8854 struct type
*type
= value_type (val
);
8856 if (!discrete_type_p (type
))
8857 error (_("'POS only defined on discrete types"));
8859 gdb::optional
<LONGEST
> result
= discrete_position (type
, value_as_long (val
));
8860 if (!result
.has_value ())
8861 error (_("enumeration value is invalid: can't find 'POS"));
8867 ada_pos_atr (struct type
*expect_type
,
8868 struct expression
*exp
,
8869 enum noside noside
, enum exp_opcode op
,
8872 struct type
*type
= builtin_type (exp
->gdbarch
)->builtin_int
;
8873 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
8874 return value_zero (type
, not_lval
);
8875 return value_from_longest (type
, pos_atr (arg
));
8878 /* Evaluate the TYPE'VAL attribute applied to ARG. */
8880 static struct value
*
8881 val_atr (struct type
*type
, LONGEST val
)
8883 gdb_assert (discrete_type_p (type
));
8884 if (type
->code () == TYPE_CODE_RANGE
)
8885 type
= TYPE_TARGET_TYPE (type
);
8886 if (type
->code () == TYPE_CODE_ENUM
)
8888 if (val
< 0 || val
>= type
->num_fields ())
8889 error (_("argument to 'VAL out of range"));
8890 val
= TYPE_FIELD_ENUMVAL (type
, val
);
8892 return value_from_longest (type
, val
);
8896 ada_val_atr (enum noside noside
, struct type
*type
, struct value
*arg
)
8898 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
8899 return value_zero (type
, not_lval
);
8901 if (!discrete_type_p (type
))
8902 error (_("'VAL only defined on discrete types"));
8903 if (!integer_type_p (value_type (arg
)))
8904 error (_("'VAL requires integral argument"));
8906 return val_atr (type
, value_as_long (arg
));
8912 /* True if TYPE appears to be an Ada character type.
8913 [At the moment, this is true only for Character and Wide_Character;
8914 It is a heuristic test that could stand improvement]. */
8917 ada_is_character_type (struct type
*type
)
8921 /* If the type code says it's a character, then assume it really is,
8922 and don't check any further. */
8923 if (type
->code () == TYPE_CODE_CHAR
)
8926 /* Otherwise, assume it's a character type iff it is a discrete type
8927 with a known character type name. */
8928 name
= ada_type_name (type
);
8929 return (name
!= NULL
8930 && (type
->code () == TYPE_CODE_INT
8931 || type
->code () == TYPE_CODE_RANGE
)
8932 && (strcmp (name
, "character") == 0
8933 || strcmp (name
, "wide_character") == 0
8934 || strcmp (name
, "wide_wide_character") == 0
8935 || strcmp (name
, "unsigned char") == 0));
8938 /* True if TYPE appears to be an Ada string type. */
8941 ada_is_string_type (struct type
*type
)
8943 type
= ada_check_typedef (type
);
8945 && type
->code () != TYPE_CODE_PTR
8946 && (ada_is_simple_array_type (type
)
8947 || ada_is_array_descriptor_type (type
))
8948 && ada_array_arity (type
) == 1)
8950 struct type
*elttype
= ada_array_element_type (type
, 1);
8952 return ada_is_character_type (elttype
);
8958 /* The compiler sometimes provides a parallel XVS type for a given
8959 PAD type. Normally, it is safe to follow the PAD type directly,
8960 but older versions of the compiler have a bug that causes the offset
8961 of its "F" field to be wrong. Following that field in that case
8962 would lead to incorrect results, but this can be worked around
8963 by ignoring the PAD type and using the associated XVS type instead.
8965 Set to True if the debugger should trust the contents of PAD types.
8966 Otherwise, ignore the PAD type if there is a parallel XVS type. */
8967 static bool trust_pad_over_xvs
= true;
8969 /* True if TYPE is a struct type introduced by the compiler to force the
8970 alignment of a value. Such types have a single field with a
8971 distinctive name. */
8974 ada_is_aligner_type (struct type
*type
)
8976 type
= ada_check_typedef (type
);
8978 if (!trust_pad_over_xvs
&& ada_find_parallel_type (type
, "___XVS") != NULL
)
8981 return (type
->code () == TYPE_CODE_STRUCT
8982 && type
->num_fields () == 1
8983 && strcmp (TYPE_FIELD_NAME (type
, 0), "F") == 0);
8986 /* If there is an ___XVS-convention type parallel to SUBTYPE, return
8987 the parallel type. */
8990 ada_get_base_type (struct type
*raw_type
)
8992 struct type
*real_type_namer
;
8993 struct type
*raw_real_type
;
8995 if (raw_type
== NULL
|| raw_type
->code () != TYPE_CODE_STRUCT
)
8998 if (ada_is_aligner_type (raw_type
))
8999 /* The encoding specifies that we should always use the aligner type.
9000 So, even if this aligner type has an associated XVS type, we should
9003 According to the compiler gurus, an XVS type parallel to an aligner
9004 type may exist because of a stabs limitation. In stabs, aligner
9005 types are empty because the field has a variable-sized type, and
9006 thus cannot actually be used as an aligner type. As a result,
9007 we need the associated parallel XVS type to decode the type.
9008 Since the policy in the compiler is to not change the internal
9009 representation based on the debugging info format, we sometimes
9010 end up having a redundant XVS type parallel to the aligner type. */
9013 real_type_namer
= ada_find_parallel_type (raw_type
, "___XVS");
9014 if (real_type_namer
== NULL
9015 || real_type_namer
->code () != TYPE_CODE_STRUCT
9016 || real_type_namer
->num_fields () != 1)
9019 if (real_type_namer
->field (0).type ()->code () != TYPE_CODE_REF
)
9021 /* This is an older encoding form where the base type needs to be
9022 looked up by name. We prefer the newer encoding because it is
9024 raw_real_type
= ada_find_any_type (TYPE_FIELD_NAME (real_type_namer
, 0));
9025 if (raw_real_type
== NULL
)
9028 return raw_real_type
;
9031 /* The field in our XVS type is a reference to the base type. */
9032 return TYPE_TARGET_TYPE (real_type_namer
->field (0).type ());
9035 /* The type of value designated by TYPE, with all aligners removed. */
9038 ada_aligned_type (struct type
*type
)
9040 if (ada_is_aligner_type (type
))
9041 return ada_aligned_type (type
->field (0).type ());
9043 return ada_get_base_type (type
);
9047 /* The address of the aligned value in an object at address VALADDR
9048 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
9051 ada_aligned_value_addr (struct type
*type
, const gdb_byte
*valaddr
)
9053 if (ada_is_aligner_type (type
))
9054 return ada_aligned_value_addr (type
->field (0).type (),
9056 TYPE_FIELD_BITPOS (type
,
9057 0) / TARGET_CHAR_BIT
);
9064 /* The printed representation of an enumeration literal with encoded
9065 name NAME. The value is good to the next call of ada_enum_name. */
9067 ada_enum_name (const char *name
)
9069 static std::string storage
;
9072 /* First, unqualify the enumeration name:
9073 1. Search for the last '.' character. If we find one, then skip
9074 all the preceding characters, the unqualified name starts
9075 right after that dot.
9076 2. Otherwise, we may be debugging on a target where the compiler
9077 translates dots into "__". Search forward for double underscores,
9078 but stop searching when we hit an overloading suffix, which is
9079 of the form "__" followed by digits. */
9081 tmp
= strrchr (name
, '.');
9086 while ((tmp
= strstr (name
, "__")) != NULL
)
9088 if (isdigit (tmp
[2]))
9099 if (name
[1] == 'U' || name
[1] == 'W')
9101 if (sscanf (name
+ 2, "%x", &v
) != 1)
9104 else if (((name
[1] >= '0' && name
[1] <= '9')
9105 || (name
[1] >= 'a' && name
[1] <= 'z'))
9108 storage
= string_printf ("'%c'", name
[1]);
9109 return storage
.c_str ();
9114 if (isascii (v
) && isprint (v
))
9115 storage
= string_printf ("'%c'", v
);
9116 else if (name
[1] == 'U')
9117 storage
= string_printf ("[\"%02x\"]", v
);
9119 storage
= string_printf ("[\"%04x\"]", v
);
9121 return storage
.c_str ();
9125 tmp
= strstr (name
, "__");
9127 tmp
= strstr (name
, "$");
9130 storage
= std::string (name
, tmp
- name
);
9131 return storage
.c_str ();
9138 /* Evaluate the subexpression of EXP starting at *POS as for
9139 evaluate_type, updating *POS to point just past the evaluated
9142 static struct value
*
9143 evaluate_subexp_type (struct expression
*exp
, int *pos
)
9145 return evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
9148 /* If VAL is wrapped in an aligner or subtype wrapper, return the
9151 static struct value
*
9152 unwrap_value (struct value
*val
)
9154 struct type
*type
= ada_check_typedef (value_type (val
));
9156 if (ada_is_aligner_type (type
))
9158 struct value
*v
= ada_value_struct_elt (val
, "F", 0);
9159 struct type
*val_type
= ada_check_typedef (value_type (v
));
9161 if (ada_type_name (val_type
) == NULL
)
9162 val_type
->set_name (ada_type_name (type
));
9164 return unwrap_value (v
);
9168 struct type
*raw_real_type
=
9169 ada_check_typedef (ada_get_base_type (type
));
9171 /* If there is no parallel XVS or XVE type, then the value is
9172 already unwrapped. Return it without further modification. */
9173 if ((type
== raw_real_type
)
9174 && ada_find_parallel_type (type
, "___XVE") == NULL
)
9178 coerce_unspec_val_to_type
9179 (val
, ada_to_fixed_type (raw_real_type
, 0,
9180 value_address (val
),
9185 /* Given two array types T1 and T2, return nonzero iff both arrays
9186 contain the same number of elements. */
9189 ada_same_array_size_p (struct type
*t1
, struct type
*t2
)
9191 LONGEST lo1
, hi1
, lo2
, hi2
;
9193 /* Get the array bounds in order to verify that the size of
9194 the two arrays match. */
9195 if (!get_array_bounds (t1
, &lo1
, &hi1
)
9196 || !get_array_bounds (t2
, &lo2
, &hi2
))
9197 error (_("unable to determine array bounds"));
9199 /* To make things easier for size comparison, normalize a bit
9200 the case of empty arrays by making sure that the difference
9201 between upper bound and lower bound is always -1. */
9207 return (hi1
- lo1
== hi2
- lo2
);
9210 /* Assuming that VAL is an array of integrals, and TYPE represents
9211 an array with the same number of elements, but with wider integral
9212 elements, return an array "casted" to TYPE. In practice, this
9213 means that the returned array is built by casting each element
9214 of the original array into TYPE's (wider) element type. */
9216 static struct value
*
9217 ada_promote_array_of_integrals (struct type
*type
, struct value
*val
)
9219 struct type
*elt_type
= TYPE_TARGET_TYPE (type
);
9224 /* Verify that both val and type are arrays of scalars, and
9225 that the size of val's elements is smaller than the size
9226 of type's element. */
9227 gdb_assert (type
->code () == TYPE_CODE_ARRAY
);
9228 gdb_assert (is_integral_type (TYPE_TARGET_TYPE (type
)));
9229 gdb_assert (value_type (val
)->code () == TYPE_CODE_ARRAY
);
9230 gdb_assert (is_integral_type (TYPE_TARGET_TYPE (value_type (val
))));
9231 gdb_assert (TYPE_LENGTH (TYPE_TARGET_TYPE (type
))
9232 > TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (val
))));
9234 if (!get_array_bounds (type
, &lo
, &hi
))
9235 error (_("unable to determine array bounds"));
9237 res
= allocate_value (type
);
9239 /* Promote each array element. */
9240 for (i
= 0; i
< hi
- lo
+ 1; i
++)
9242 struct value
*elt
= value_cast (elt_type
, value_subscript (val
, lo
+ i
));
9244 memcpy (value_contents_writeable (res
) + (i
* TYPE_LENGTH (elt_type
)),
9245 value_contents_all (elt
), TYPE_LENGTH (elt_type
));
9251 /* Coerce VAL as necessary for assignment to an lval of type TYPE, and
9252 return the converted value. */
9254 static struct value
*
9255 coerce_for_assign (struct type
*type
, struct value
*val
)
9257 struct type
*type2
= value_type (val
);
9262 type2
= ada_check_typedef (type2
);
9263 type
= ada_check_typedef (type
);
9265 if (type2
->code () == TYPE_CODE_PTR
9266 && type
->code () == TYPE_CODE_ARRAY
)
9268 val
= ada_value_ind (val
);
9269 type2
= value_type (val
);
9272 if (type2
->code () == TYPE_CODE_ARRAY
9273 && type
->code () == TYPE_CODE_ARRAY
)
9275 if (!ada_same_array_size_p (type
, type2
))
9276 error (_("cannot assign arrays of different length"));
9278 if (is_integral_type (TYPE_TARGET_TYPE (type
))
9279 && is_integral_type (TYPE_TARGET_TYPE (type2
))
9280 && TYPE_LENGTH (TYPE_TARGET_TYPE (type2
))
9281 < TYPE_LENGTH (TYPE_TARGET_TYPE (type
)))
9283 /* Allow implicit promotion of the array elements to
9285 return ada_promote_array_of_integrals (type
, val
);
9288 if (TYPE_LENGTH (TYPE_TARGET_TYPE (type2
))
9289 != TYPE_LENGTH (TYPE_TARGET_TYPE (type
)))
9290 error (_("Incompatible types in assignment"));
9291 deprecated_set_value_type (val
, type
);
9296 static struct value
*
9297 ada_value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
9300 struct type
*type1
, *type2
;
9303 arg1
= coerce_ref (arg1
);
9304 arg2
= coerce_ref (arg2
);
9305 type1
= get_base_type (ada_check_typedef (value_type (arg1
)));
9306 type2
= get_base_type (ada_check_typedef (value_type (arg2
)));
9308 if (type1
->code () != TYPE_CODE_INT
9309 || type2
->code () != TYPE_CODE_INT
)
9310 return value_binop (arg1
, arg2
, op
);
9319 return value_binop (arg1
, arg2
, op
);
9322 v2
= value_as_long (arg2
);
9324 error (_("second operand of %s must not be zero."), op_string (op
));
9326 if (type1
->is_unsigned () || op
== BINOP_MOD
)
9327 return value_binop (arg1
, arg2
, op
);
9329 v1
= value_as_long (arg1
);
9334 if (!TRUNCATION_TOWARDS_ZERO
&& v1
* (v1
% v2
) < 0)
9335 v
+= v
> 0 ? -1 : 1;
9343 /* Should not reach this point. */
9347 val
= allocate_value (type1
);
9348 store_unsigned_integer (value_contents_raw (val
),
9349 TYPE_LENGTH (value_type (val
)),
9350 type_byte_order (type1
), v
);
9355 ada_value_equal (struct value
*arg1
, struct value
*arg2
)
9357 if (ada_is_direct_array_type (value_type (arg1
))
9358 || ada_is_direct_array_type (value_type (arg2
)))
9360 struct type
*arg1_type
, *arg2_type
;
9362 /* Automatically dereference any array reference before
9363 we attempt to perform the comparison. */
9364 arg1
= ada_coerce_ref (arg1
);
9365 arg2
= ada_coerce_ref (arg2
);
9367 arg1
= ada_coerce_to_simple_array (arg1
);
9368 arg2
= ada_coerce_to_simple_array (arg2
);
9370 arg1_type
= ada_check_typedef (value_type (arg1
));
9371 arg2_type
= ada_check_typedef (value_type (arg2
));
9373 if (arg1_type
->code () != TYPE_CODE_ARRAY
9374 || arg2_type
->code () != TYPE_CODE_ARRAY
)
9375 error (_("Attempt to compare array with non-array"));
9376 /* FIXME: The following works only for types whose
9377 representations use all bits (no padding or undefined bits)
9378 and do not have user-defined equality. */
9379 return (TYPE_LENGTH (arg1_type
) == TYPE_LENGTH (arg2_type
)
9380 && memcmp (value_contents (arg1
), value_contents (arg2
),
9381 TYPE_LENGTH (arg1_type
)) == 0);
9383 return value_equal (arg1
, arg2
);
9386 /* Assign the result of evaluating EXP starting at *POS to the INDEXth
9387 component of LHS (a simple array or a record), updating *POS past
9388 the expression, assuming that LHS is contained in CONTAINER. Does
9389 not modify the inferior's memory, nor does it modify LHS (unless
9390 LHS == CONTAINER). */
9393 assign_component (struct value
*container
, struct value
*lhs
, LONGEST index
,
9394 struct expression
*exp
, int *pos
)
9396 struct value
*mark
= value_mark ();
9398 struct type
*lhs_type
= check_typedef (value_type (lhs
));
9400 if (lhs_type
->code () == TYPE_CODE_ARRAY
)
9402 struct type
*index_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
9403 struct value
*index_val
= value_from_longest (index_type
, index
);
9405 elt
= unwrap_value (ada_value_subscript (lhs
, 1, &index_val
));
9409 elt
= ada_index_struct_field (index
, lhs
, 0, value_type (lhs
));
9410 elt
= ada_to_fixed_value (elt
);
9413 if (exp
->elts
[*pos
].opcode
== OP_AGGREGATE
)
9414 assign_aggregate (container
, elt
, exp
, pos
, EVAL_NORMAL
);
9416 value_assign_to_component (container
, elt
,
9417 ada_evaluate_subexp (NULL
, exp
, pos
,
9420 value_free_to_mark (mark
);
9423 /* Assuming that LHS represents an lvalue having a record or array
9424 type, and EXP->ELTS[*POS] is an OP_AGGREGATE, evaluate an assignment
9425 of that aggregate's value to LHS, advancing *POS past the
9426 aggregate. NOSIDE is as for evaluate_subexp. CONTAINER is an
9427 lvalue containing LHS (possibly LHS itself). Does not modify
9428 the inferior's memory, nor does it modify the contents of
9429 LHS (unless == CONTAINER). Returns the modified CONTAINER. */
9431 static struct value
*
9432 assign_aggregate (struct value
*container
,
9433 struct value
*lhs
, struct expression
*exp
,
9434 int *pos
, enum noside noside
)
9436 struct type
*lhs_type
;
9437 int n
= exp
->elts
[*pos
+1].longconst
;
9438 LONGEST low_index
, high_index
;
9442 if (noside
!= EVAL_NORMAL
)
9444 for (i
= 0; i
< n
; i
+= 1)
9445 ada_evaluate_subexp (NULL
, exp
, pos
, noside
);
9449 container
= ada_coerce_ref (container
);
9450 if (ada_is_direct_array_type (value_type (container
)))
9451 container
= ada_coerce_to_simple_array (container
);
9452 lhs
= ada_coerce_ref (lhs
);
9453 if (!deprecated_value_modifiable (lhs
))
9454 error (_("Left operand of assignment is not a modifiable lvalue."));
9456 lhs_type
= check_typedef (value_type (lhs
));
9457 if (ada_is_direct_array_type (lhs_type
))
9459 lhs
= ada_coerce_to_simple_array (lhs
);
9460 lhs_type
= check_typedef (value_type (lhs
));
9461 low_index
= lhs_type
->bounds ()->low
.const_val ();
9462 high_index
= lhs_type
->bounds ()->high
.const_val ();
9464 else if (lhs_type
->code () == TYPE_CODE_STRUCT
)
9467 high_index
= num_visible_fields (lhs_type
) - 1;
9470 error (_("Left-hand side must be array or record."));
9472 std::vector
<LONGEST
> indices (4);
9473 indices
[0] = indices
[1] = low_index
- 1;
9474 indices
[2] = indices
[3] = high_index
+ 1;
9476 for (i
= 0; i
< n
; i
+= 1)
9478 switch (exp
->elts
[*pos
].opcode
)
9481 aggregate_assign_from_choices (container
, lhs
, exp
, pos
, indices
,
9482 low_index
, high_index
);
9485 aggregate_assign_positional (container
, lhs
, exp
, pos
, indices
,
9486 low_index
, high_index
);
9490 error (_("Misplaced 'others' clause"));
9491 aggregate_assign_others (container
, lhs
, exp
, pos
, indices
,
9492 low_index
, high_index
);
9495 error (_("Internal error: bad aggregate clause"));
9502 /* Assign into the component of LHS indexed by the OP_POSITIONAL
9503 construct at *POS, updating *POS past the construct, given that
9504 the positions are relative to lower bound LOW, where HIGH is the
9505 upper bound. Record the position in INDICES. CONTAINER is as for
9506 assign_aggregate. */
9508 aggregate_assign_positional (struct value
*container
,
9509 struct value
*lhs
, struct expression
*exp
,
9510 int *pos
, std::vector
<LONGEST
> &indices
,
9511 LONGEST low
, LONGEST high
)
9513 LONGEST ind
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
) + low
;
9515 if (ind
- 1 == high
)
9516 warning (_("Extra components in aggregate ignored."));
9519 add_component_interval (ind
, ind
, indices
);
9521 assign_component (container
, lhs
, ind
, exp
, pos
);
9524 ada_evaluate_subexp (NULL
, exp
, pos
, EVAL_SKIP
);
9527 /* Assign into the components of LHS indexed by the OP_CHOICES
9528 construct at *POS, updating *POS past the construct, given that
9529 the allowable indices are LOW..HIGH. Record the indices assigned
9530 to in INDICES. CONTAINER is as for assign_aggregate. */
9532 aggregate_assign_from_choices (struct value
*container
,
9533 struct value
*lhs
, struct expression
*exp
,
9534 int *pos
, std::vector
<LONGEST
> &indices
,
9535 LONGEST low
, LONGEST high
)
9538 int n_choices
= longest_to_int (exp
->elts
[*pos
+1].longconst
);
9539 int choice_pos
, expr_pc
;
9540 int is_array
= ada_is_direct_array_type (value_type (lhs
));
9542 choice_pos
= *pos
+= 3;
9544 for (j
= 0; j
< n_choices
; j
+= 1)
9545 ada_evaluate_subexp (NULL
, exp
, pos
, EVAL_SKIP
);
9547 ada_evaluate_subexp (NULL
, exp
, pos
, EVAL_SKIP
);
9549 for (j
= 0; j
< n_choices
; j
+= 1)
9551 LONGEST lower
, upper
;
9552 enum exp_opcode op
= exp
->elts
[choice_pos
].opcode
;
9554 if (op
== OP_DISCRETE_RANGE
)
9557 lower
= value_as_long (ada_evaluate_subexp (NULL
, exp
, pos
,
9559 upper
= value_as_long (ada_evaluate_subexp (NULL
, exp
, pos
,
9564 lower
= value_as_long (ada_evaluate_subexp (NULL
, exp
, &choice_pos
,
9576 name
= &exp
->elts
[choice_pos
+ 2].string
;
9579 name
= exp
->elts
[choice_pos
+ 2].symbol
->natural_name ();
9582 error (_("Invalid record component association."));
9584 ada_evaluate_subexp (NULL
, exp
, &choice_pos
, EVAL_SKIP
);
9586 if (! find_struct_field (name
, value_type (lhs
), 0,
9587 NULL
, NULL
, NULL
, NULL
, &ind
))
9588 error (_("Unknown component name: %s."), name
);
9589 lower
= upper
= ind
;
9592 if (lower
<= upper
&& (lower
< low
|| upper
> high
))
9593 error (_("Index in component association out of bounds."));
9595 add_component_interval (lower
, upper
, indices
);
9596 while (lower
<= upper
)
9601 assign_component (container
, lhs
, lower
, exp
, &pos1
);
9607 /* Assign the value of the expression in the OP_OTHERS construct in
9608 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
9609 have not been previously assigned. The index intervals already assigned
9610 are in INDICES. Updates *POS to after the OP_OTHERS clause.
9611 CONTAINER is as for assign_aggregate. */
9613 aggregate_assign_others (struct value
*container
,
9614 struct value
*lhs
, struct expression
*exp
,
9615 int *pos
, std::vector
<LONGEST
> &indices
,
9616 LONGEST low
, LONGEST high
)
9619 int expr_pc
= *pos
+ 1;
9621 int num_indices
= indices
.size ();
9622 for (i
= 0; i
< num_indices
- 2; i
+= 2)
9626 for (ind
= indices
[i
+ 1] + 1; ind
< indices
[i
+ 2]; ind
+= 1)
9631 assign_component (container
, lhs
, ind
, exp
, &localpos
);
9634 ada_evaluate_subexp (NULL
, exp
, pos
, EVAL_SKIP
);
9637 /* Add the interval [LOW .. HIGH] to the sorted set of intervals
9638 [ INDICES[0] .. INDICES[1] ],... The resulting intervals do not
9641 add_component_interval (LONGEST low
, LONGEST high
,
9642 std::vector
<LONGEST
> &indices
)
9646 int size
= indices
.size ();
9647 for (i
= 0; i
< size
; i
+= 2) {
9648 if (high
>= indices
[i
] && low
<= indices
[i
+ 1])
9652 for (kh
= i
+ 2; kh
< size
; kh
+= 2)
9653 if (high
< indices
[kh
])
9655 if (low
< indices
[i
])
9657 indices
[i
+ 1] = indices
[kh
- 1];
9658 if (high
> indices
[i
+ 1])
9659 indices
[i
+ 1] = high
;
9660 memcpy (indices
.data () + i
+ 2, indices
.data () + kh
, size
- kh
);
9661 indices
.resize (kh
- i
- 2);
9664 else if (high
< indices
[i
])
9668 indices
.resize (indices
.size () + 2);
9669 for (j
= indices
.size () - 1; j
>= i
+ 2; j
-= 1)
9670 indices
[j
] = indices
[j
- 2];
9672 indices
[i
+ 1] = high
;
9675 /* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
9678 static struct value
*
9679 ada_value_cast (struct type
*type
, struct value
*arg2
)
9681 if (type
== ada_check_typedef (value_type (arg2
)))
9684 return value_cast (type
, arg2
);
9687 /* Evaluating Ada expressions, and printing their result.
9688 ------------------------------------------------------
9693 We usually evaluate an Ada expression in order to print its value.
9694 We also evaluate an expression in order to print its type, which
9695 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
9696 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
9697 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
9698 the evaluation compared to the EVAL_NORMAL, but is otherwise very
9701 Evaluating expressions is a little more complicated for Ada entities
9702 than it is for entities in languages such as C. The main reason for
9703 this is that Ada provides types whose definition might be dynamic.
9704 One example of such types is variant records. Or another example
9705 would be an array whose bounds can only be known at run time.
9707 The following description is a general guide as to what should be
9708 done (and what should NOT be done) in order to evaluate an expression
9709 involving such types, and when. This does not cover how the semantic
9710 information is encoded by GNAT as this is covered separatly. For the
9711 document used as the reference for the GNAT encoding, see exp_dbug.ads
9712 in the GNAT sources.
9714 Ideally, we should embed each part of this description next to its
9715 associated code. Unfortunately, the amount of code is so vast right
9716 now that it's hard to see whether the code handling a particular
9717 situation might be duplicated or not. One day, when the code is
9718 cleaned up, this guide might become redundant with the comments
9719 inserted in the code, and we might want to remove it.
9721 2. ``Fixing'' an Entity, the Simple Case:
9722 -----------------------------------------
9724 When evaluating Ada expressions, the tricky issue is that they may
9725 reference entities whose type contents and size are not statically
9726 known. Consider for instance a variant record:
9728 type Rec (Empty : Boolean := True) is record
9731 when False => Value : Integer;
9734 Yes : Rec := (Empty => False, Value => 1);
9735 No : Rec := (empty => True);
9737 The size and contents of that record depends on the value of the
9738 descriminant (Rec.Empty). At this point, neither the debugging
9739 information nor the associated type structure in GDB are able to
9740 express such dynamic types. So what the debugger does is to create
9741 "fixed" versions of the type that applies to the specific object.
9742 We also informally refer to this operation as "fixing" an object,
9743 which means creating its associated fixed type.
9745 Example: when printing the value of variable "Yes" above, its fixed
9746 type would look like this:
9753 On the other hand, if we printed the value of "No", its fixed type
9760 Things become a little more complicated when trying to fix an entity
9761 with a dynamic type that directly contains another dynamic type,
9762 such as an array of variant records, for instance. There are
9763 two possible cases: Arrays, and records.
9765 3. ``Fixing'' Arrays:
9766 ---------------------
9768 The type structure in GDB describes an array in terms of its bounds,
9769 and the type of its elements. By design, all elements in the array
9770 have the same type and we cannot represent an array of variant elements
9771 using the current type structure in GDB. When fixing an array,
9772 we cannot fix the array element, as we would potentially need one
9773 fixed type per element of the array. As a result, the best we can do
9774 when fixing an array is to produce an array whose bounds and size
9775 are correct (allowing us to read it from memory), but without having
9776 touched its element type. Fixing each element will be done later,
9777 when (if) necessary.
9779 Arrays are a little simpler to handle than records, because the same
9780 amount of memory is allocated for each element of the array, even if
9781 the amount of space actually used by each element differs from element
9782 to element. Consider for instance the following array of type Rec:
9784 type Rec_Array is array (1 .. 2) of Rec;
9786 The actual amount of memory occupied by each element might be different
9787 from element to element, depending on the value of their discriminant.
9788 But the amount of space reserved for each element in the array remains
9789 fixed regardless. So we simply need to compute that size using
9790 the debugging information available, from which we can then determine
9791 the array size (we multiply the number of elements of the array by
9792 the size of each element).
9794 The simplest case is when we have an array of a constrained element
9795 type. For instance, consider the following type declarations:
9797 type Bounded_String (Max_Size : Integer) is
9799 Buffer : String (1 .. Max_Size);
9801 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
9803 In this case, the compiler describes the array as an array of
9804 variable-size elements (identified by its XVS suffix) for which
9805 the size can be read in the parallel XVZ variable.
9807 In the case of an array of an unconstrained element type, the compiler
9808 wraps the array element inside a private PAD type. This type should not
9809 be shown to the user, and must be "unwrap"'ed before printing. Note
9810 that we also use the adjective "aligner" in our code to designate
9811 these wrapper types.
9813 In some cases, the size allocated for each element is statically
9814 known. In that case, the PAD type already has the correct size,
9815 and the array element should remain unfixed.
9817 But there are cases when this size is not statically known.
9818 For instance, assuming that "Five" is an integer variable:
9820 type Dynamic is array (1 .. Five) of Integer;
9821 type Wrapper (Has_Length : Boolean := False) is record
9824 when True => Length : Integer;
9828 type Wrapper_Array is array (1 .. 2) of Wrapper;
9830 Hello : Wrapper_Array := (others => (Has_Length => True,
9831 Data => (others => 17),
9835 The debugging info would describe variable Hello as being an
9836 array of a PAD type. The size of that PAD type is not statically
9837 known, but can be determined using a parallel XVZ variable.
9838 In that case, a copy of the PAD type with the correct size should
9839 be used for the fixed array.
9841 3. ``Fixing'' record type objects:
9842 ----------------------------------
9844 Things are slightly different from arrays in the case of dynamic
9845 record types. In this case, in order to compute the associated
9846 fixed type, we need to determine the size and offset of each of
9847 its components. This, in turn, requires us to compute the fixed
9848 type of each of these components.
9850 Consider for instance the example:
9852 type Bounded_String (Max_Size : Natural) is record
9853 Str : String (1 .. Max_Size);
9856 My_String : Bounded_String (Max_Size => 10);
9858 In that case, the position of field "Length" depends on the size
9859 of field Str, which itself depends on the value of the Max_Size
9860 discriminant. In order to fix the type of variable My_String,
9861 we need to fix the type of field Str. Therefore, fixing a variant
9862 record requires us to fix each of its components.
9864 However, if a component does not have a dynamic size, the component
9865 should not be fixed. In particular, fields that use a PAD type
9866 should not fixed. Here is an example where this might happen
9867 (assuming type Rec above):
9869 type Container (Big : Boolean) is record
9873 when True => Another : Integer;
9877 My_Container : Container := (Big => False,
9878 First => (Empty => True),
9881 In that example, the compiler creates a PAD type for component First,
9882 whose size is constant, and then positions the component After just
9883 right after it. The offset of component After is therefore constant
9886 The debugger computes the position of each field based on an algorithm
9887 that uses, among other things, the actual position and size of the field
9888 preceding it. Let's now imagine that the user is trying to print
9889 the value of My_Container. If the type fixing was recursive, we would
9890 end up computing the offset of field After based on the size of the
9891 fixed version of field First. And since in our example First has
9892 only one actual field, the size of the fixed type is actually smaller
9893 than the amount of space allocated to that field, and thus we would
9894 compute the wrong offset of field After.
9896 To make things more complicated, we need to watch out for dynamic
9897 components of variant records (identified by the ___XVL suffix in
9898 the component name). Even if the target type is a PAD type, the size
9899 of that type might not be statically known. So the PAD type needs
9900 to be unwrapped and the resulting type needs to be fixed. Otherwise,
9901 we might end up with the wrong size for our component. This can be
9902 observed with the following type declarations:
9904 type Octal is new Integer range 0 .. 7;
9905 type Octal_Array is array (Positive range <>) of Octal;
9906 pragma Pack (Octal_Array);
9908 type Octal_Buffer (Size : Positive) is record
9909 Buffer : Octal_Array (1 .. Size);
9913 In that case, Buffer is a PAD type whose size is unset and needs
9914 to be computed by fixing the unwrapped type.
9916 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
9917 ----------------------------------------------------------
9919 Lastly, when should the sub-elements of an entity that remained unfixed
9920 thus far, be actually fixed?
9922 The answer is: Only when referencing that element. For instance
9923 when selecting one component of a record, this specific component
9924 should be fixed at that point in time. Or when printing the value
9925 of a record, each component should be fixed before its value gets
9926 printed. Similarly for arrays, the element of the array should be
9927 fixed when printing each element of the array, or when extracting
9928 one element out of that array. On the other hand, fixing should
9929 not be performed on the elements when taking a slice of an array!
9931 Note that one of the side effects of miscomputing the offset and
9932 size of each field is that we end up also miscomputing the size
9933 of the containing type. This can have adverse results when computing
9934 the value of an entity. GDB fetches the value of an entity based
9935 on the size of its type, and thus a wrong size causes GDB to fetch
9936 the wrong amount of memory. In the case where the computed size is
9937 too small, GDB fetches too little data to print the value of our
9938 entity. Results in this case are unpredictable, as we usually read
9939 past the buffer containing the data =:-o. */
9941 /* Evaluate a subexpression of EXP, at index *POS, and return a value
9942 for that subexpression cast to TO_TYPE. Advance *POS over the
9946 ada_evaluate_subexp_for_cast (expression
*exp
, int *pos
,
9947 enum noside noside
, struct type
*to_type
)
9951 if (exp
->elts
[pc
].opcode
== OP_VAR_MSYM_VALUE
9952 || exp
->elts
[pc
].opcode
== OP_VAR_VALUE
)
9957 if (exp
->elts
[pc
].opcode
== OP_VAR_MSYM_VALUE
)
9959 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
9960 return value_zero (to_type
, not_lval
);
9962 val
= evaluate_var_msym_value (noside
,
9963 exp
->elts
[pc
+ 1].objfile
,
9964 exp
->elts
[pc
+ 2].msymbol
);
9967 val
= evaluate_var_value (noside
,
9968 exp
->elts
[pc
+ 1].block
,
9969 exp
->elts
[pc
+ 2].symbol
);
9971 if (noside
== EVAL_SKIP
)
9972 return eval_skip_value (exp
);
9974 val
= ada_value_cast (to_type
, val
);
9976 /* Follow the Ada language semantics that do not allow taking
9977 an address of the result of a cast (view conversion in Ada). */
9978 if (VALUE_LVAL (val
) == lval_memory
)
9980 if (value_lazy (val
))
9981 value_fetch_lazy (val
);
9982 VALUE_LVAL (val
) = not_lval
;
9987 value
*val
= evaluate_subexp (to_type
, exp
, pos
, noside
);
9988 if (noside
== EVAL_SKIP
)
9989 return eval_skip_value (exp
);
9990 return ada_value_cast (to_type
, val
);
9993 /* A helper function for TERNOP_IN_RANGE. */
9996 eval_ternop_in_range (struct type
*expect_type
, struct expression
*exp
,
9998 value
*arg1
, value
*arg2
, value
*arg3
)
10000 if (noside
== EVAL_SKIP
)
10001 return eval_skip_value (exp
);
10003 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10004 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg3
);
10005 struct type
*type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
10007 value_from_longest (type
,
10008 (value_less (arg1
, arg3
)
10009 || value_equal (arg1
, arg3
))
10010 && (value_less (arg2
, arg1
)
10011 || value_equal (arg2
, arg1
)));
10014 /* A helper function for UNOP_NEG. */
10017 ada_unop_neg (struct type
*expect_type
,
10018 struct expression
*exp
,
10019 enum noside noside
, enum exp_opcode op
,
10020 struct value
*arg1
)
10022 if (noside
== EVAL_SKIP
)
10023 return eval_skip_value (exp
);
10024 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
10025 return value_neg (arg1
);
10028 /* A helper function for UNOP_IN_RANGE. */
10031 ada_unop_in_range (struct type
*expect_type
,
10032 struct expression
*exp
,
10033 enum noside noside
, enum exp_opcode op
,
10034 struct value
*arg1
, struct type
*type
)
10036 if (noside
== EVAL_SKIP
)
10037 return eval_skip_value (exp
);
10039 struct value
*arg2
, *arg3
;
10040 switch (type
->code ())
10043 lim_warning (_("Membership test incompletely implemented; "
10044 "always returns true"));
10045 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
10046 return value_from_longest (type
, (LONGEST
) 1);
10048 case TYPE_CODE_RANGE
:
10049 arg2
= value_from_longest (type
,
10050 type
->bounds ()->low
.const_val ());
10051 arg3
= value_from_longest (type
,
10052 type
->bounds ()->high
.const_val ());
10053 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10054 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg3
);
10055 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
10057 value_from_longest (type
,
10058 (value_less (arg1
, arg3
)
10059 || value_equal (arg1
, arg3
))
10060 && (value_less (arg2
, arg1
)
10061 || value_equal (arg2
, arg1
)));
10065 /* A helper function for OP_ATR_TAG. */
10068 ada_atr_tag (struct type
*expect_type
,
10069 struct expression
*exp
,
10070 enum noside noside
, enum exp_opcode op
,
10071 struct value
*arg1
)
10073 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10074 return value_zero (ada_tag_type (arg1
), not_lval
);
10076 return ada_value_tag (arg1
);
10079 /* A helper function for OP_ATR_SIZE. */
10082 ada_atr_size (struct type
*expect_type
,
10083 struct expression
*exp
,
10084 enum noside noside
, enum exp_opcode op
,
10085 struct value
*arg1
)
10087 struct type
*type
= value_type (arg1
);
10089 /* If the argument is a reference, then dereference its type, since
10090 the user is really asking for the size of the actual object,
10091 not the size of the pointer. */
10092 if (type
->code () == TYPE_CODE_REF
)
10093 type
= TYPE_TARGET_TYPE (type
);
10095 if (noside
== EVAL_SKIP
)
10096 return eval_skip_value (exp
);
10097 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10098 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
, not_lval
);
10100 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
10101 TARGET_CHAR_BIT
* TYPE_LENGTH (type
));
10104 /* A helper function for UNOP_ABS. */
10107 ada_abs (struct type
*expect_type
,
10108 struct expression
*exp
,
10109 enum noside noside
, enum exp_opcode op
,
10110 struct value
*arg1
)
10112 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
10113 if (value_less (arg1
, value_zero (value_type (arg1
), not_lval
)))
10114 return value_neg (arg1
);
10119 /* A helper function for BINOP_MUL. */
10122 ada_mult_binop (struct type
*expect_type
,
10123 struct expression
*exp
,
10124 enum noside noside
, enum exp_opcode op
,
10125 struct value
*arg1
, struct value
*arg2
)
10127 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10129 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10130 return value_zero (value_type (arg1
), not_lval
);
10134 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10135 return ada_value_binop (arg1
, arg2
, op
);
10139 /* A helper function for BINOP_EQUAL and BINOP_NOTEQUAL. */
10142 ada_equal_binop (struct type
*expect_type
,
10143 struct expression
*exp
,
10144 enum noside noside
, enum exp_opcode op
,
10145 struct value
*arg1
, struct value
*arg2
)
10148 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10152 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10153 tem
= ada_value_equal (arg1
, arg2
);
10155 if (op
== BINOP_NOTEQUAL
)
10157 struct type
*type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
10158 return value_from_longest (type
, (LONGEST
) tem
);
10161 /* A helper function for TERNOP_SLICE. */
10164 ada_ternop_slice (struct expression
*exp
,
10165 enum noside noside
,
10166 struct value
*array
, struct value
*low_bound_val
,
10167 struct value
*high_bound_val
)
10170 LONGEST high_bound
;
10172 low_bound_val
= coerce_ref (low_bound_val
);
10173 high_bound_val
= coerce_ref (high_bound_val
);
10174 low_bound
= value_as_long (low_bound_val
);
10175 high_bound
= value_as_long (high_bound_val
);
10177 /* If this is a reference to an aligner type, then remove all
10179 if (value_type (array
)->code () == TYPE_CODE_REF
10180 && ada_is_aligner_type (TYPE_TARGET_TYPE (value_type (array
))))
10181 TYPE_TARGET_TYPE (value_type (array
)) =
10182 ada_aligned_type (TYPE_TARGET_TYPE (value_type (array
)));
10184 if (ada_is_any_packed_array_type (value_type (array
)))
10185 error (_("cannot slice a packed array"));
10187 /* If this is a reference to an array or an array lvalue,
10188 convert to a pointer. */
10189 if (value_type (array
)->code () == TYPE_CODE_REF
10190 || (value_type (array
)->code () == TYPE_CODE_ARRAY
10191 && VALUE_LVAL (array
) == lval_memory
))
10192 array
= value_addr (array
);
10194 if (noside
== EVAL_AVOID_SIDE_EFFECTS
10195 && ada_is_array_descriptor_type (ada_check_typedef
10196 (value_type (array
))))
10197 return empty_array (ada_type_of_array (array
, 0), low_bound
,
10200 array
= ada_coerce_to_simple_array_ptr (array
);
10202 /* If we have more than one level of pointer indirection,
10203 dereference the value until we get only one level. */
10204 while (value_type (array
)->code () == TYPE_CODE_PTR
10205 && (TYPE_TARGET_TYPE (value_type (array
))->code ()
10207 array
= value_ind (array
);
10209 /* Make sure we really do have an array type before going further,
10210 to avoid a SEGV when trying to get the index type or the target
10211 type later down the road if the debug info generated by
10212 the compiler is incorrect or incomplete. */
10213 if (!ada_is_simple_array_type (value_type (array
)))
10214 error (_("cannot take slice of non-array"));
10216 if (ada_check_typedef (value_type (array
))->code ()
10219 struct type
*type0
= ada_check_typedef (value_type (array
));
10221 if (high_bound
< low_bound
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
10222 return empty_array (TYPE_TARGET_TYPE (type0
), low_bound
, high_bound
);
10225 struct type
*arr_type0
=
10226 to_fixed_array_type (TYPE_TARGET_TYPE (type0
), NULL
, 1);
10228 return ada_value_slice_from_ptr (array
, arr_type0
,
10229 longest_to_int (low_bound
),
10230 longest_to_int (high_bound
));
10233 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10235 else if (high_bound
< low_bound
)
10236 return empty_array (value_type (array
), low_bound
, high_bound
);
10238 return ada_value_slice (array
, longest_to_int (low_bound
),
10239 longest_to_int (high_bound
));
10242 /* A helper function for BINOP_IN_BOUNDS. */
10245 ada_binop_in_bounds (struct expression
*exp
, enum noside noside
,
10246 struct value
*arg1
, struct value
*arg2
, int n
)
10248 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10250 struct type
*type
= language_bool_type (exp
->language_defn
,
10252 return value_zero (type
, not_lval
);
10255 struct type
*type
= ada_index_type (value_type (arg2
), n
, "range");
10257 type
= value_type (arg1
);
10259 value
*arg3
= value_from_longest (type
, ada_array_bound (arg2
, n
, 1));
10260 arg2
= value_from_longest (type
, ada_array_bound (arg2
, n
, 0));
10262 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10263 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg3
);
10264 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
10265 return value_from_longest (type
,
10266 (value_less (arg1
, arg3
)
10267 || value_equal (arg1
, arg3
))
10268 && (value_less (arg2
, arg1
)
10269 || value_equal (arg2
, arg1
)));
10272 /* A helper function for some attribute operations. */
10275 ada_unop_atr (struct expression
*exp
, enum noside noside
, enum exp_opcode op
,
10276 struct value
*arg1
, struct type
*type_arg
, int tem
)
10278 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10280 if (type_arg
== NULL
)
10281 type_arg
= value_type (arg1
);
10283 if (ada_is_constrained_packed_array_type (type_arg
))
10284 type_arg
= decode_constrained_packed_array_type (type_arg
);
10286 if (!discrete_type_p (type_arg
))
10290 default: /* Should never happen. */
10291 error (_("unexpected attribute encountered"));
10294 type_arg
= ada_index_type (type_arg
, tem
,
10295 ada_attribute_name (op
));
10297 case OP_ATR_LENGTH
:
10298 type_arg
= builtin_type (exp
->gdbarch
)->builtin_int
;
10303 return value_zero (type_arg
, not_lval
);
10305 else if (type_arg
== NULL
)
10307 arg1
= ada_coerce_ref (arg1
);
10309 if (ada_is_constrained_packed_array_type (value_type (arg1
)))
10310 arg1
= ada_coerce_to_simple_array (arg1
);
10313 if (op
== OP_ATR_LENGTH
)
10314 type
= builtin_type (exp
->gdbarch
)->builtin_int
;
10317 type
= ada_index_type (value_type (arg1
), tem
,
10318 ada_attribute_name (op
));
10320 type
= builtin_type (exp
->gdbarch
)->builtin_int
;
10325 default: /* Should never happen. */
10326 error (_("unexpected attribute encountered"));
10328 return value_from_longest
10329 (type
, ada_array_bound (arg1
, tem
, 0));
10331 return value_from_longest
10332 (type
, ada_array_bound (arg1
, tem
, 1));
10333 case OP_ATR_LENGTH
:
10334 return value_from_longest
10335 (type
, ada_array_length (arg1
, tem
));
10338 else if (discrete_type_p (type_arg
))
10340 struct type
*range_type
;
10341 const char *name
= ada_type_name (type_arg
);
10344 if (name
!= NULL
&& type_arg
->code () != TYPE_CODE_ENUM
)
10345 range_type
= to_fixed_range_type (type_arg
, NULL
);
10346 if (range_type
== NULL
)
10347 range_type
= type_arg
;
10351 error (_("unexpected attribute encountered"));
10353 return value_from_longest
10354 (range_type
, ada_discrete_type_low_bound (range_type
));
10356 return value_from_longest
10357 (range_type
, ada_discrete_type_high_bound (range_type
));
10358 case OP_ATR_LENGTH
:
10359 error (_("the 'length attribute applies only to array types"));
10362 else if (type_arg
->code () == TYPE_CODE_FLT
)
10363 error (_("unimplemented type attribute"));
10368 if (ada_is_constrained_packed_array_type (type_arg
))
10369 type_arg
= decode_constrained_packed_array_type (type_arg
);
10372 if (op
== OP_ATR_LENGTH
)
10373 type
= builtin_type (exp
->gdbarch
)->builtin_int
;
10376 type
= ada_index_type (type_arg
, tem
, ada_attribute_name (op
));
10378 type
= builtin_type (exp
->gdbarch
)->builtin_int
;
10384 error (_("unexpected attribute encountered"));
10386 low
= ada_array_bound_from_type (type_arg
, tem
, 0);
10387 return value_from_longest (type
, low
);
10389 high
= ada_array_bound_from_type (type_arg
, tem
, 1);
10390 return value_from_longest (type
, high
);
10391 case OP_ATR_LENGTH
:
10392 low
= ada_array_bound_from_type (type_arg
, tem
, 0);
10393 high
= ada_array_bound_from_type (type_arg
, tem
, 1);
10394 return value_from_longest (type
, high
- low
+ 1);
10399 /* A helper function for OP_ATR_MIN and OP_ATR_MAX. */
10402 ada_binop_minmax (struct type
*expect_type
,
10403 struct expression
*exp
,
10404 enum noside noside
, enum exp_opcode op
,
10405 struct value
*arg1
, struct value
*arg2
)
10407 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10408 return value_zero (value_type (arg1
), not_lval
);
10411 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10412 return value_binop (arg1
, arg2
,
10413 op
== OP_ATR_MIN
? BINOP_MIN
: BINOP_MAX
);
10417 /* A helper function for BINOP_EXP. */
10419 static struct value
*
10420 ada_binop_exp (struct type
*expect_type
,
10421 struct expression
*exp
,
10422 enum noside noside
, enum exp_opcode op
,
10423 struct value
*arg1
, struct value
*arg2
)
10425 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10426 return value_zero (value_type (arg1
), not_lval
);
10429 /* For integer exponentiation operations,
10430 only promote the first argument. */
10431 if (is_integral_type (value_type (arg2
)))
10432 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
10434 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10436 return value_binop (arg1
, arg2
, op
);
10444 ada_wrapped_operation::evaluate (struct type
*expect_type
,
10445 struct expression
*exp
,
10446 enum noside noside
)
10448 value
*result
= std::get
<0> (m_storage
)->evaluate (expect_type
, exp
, noside
);
10449 if (noside
== EVAL_NORMAL
)
10450 result
= unwrap_value (result
);
10452 /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
10453 then we need to perform the conversion manually, because
10454 evaluate_subexp_standard doesn't do it. This conversion is
10455 necessary in Ada because the different kinds of float/fixed
10456 types in Ada have different representations.
10458 Similarly, we need to perform the conversion from OP_LONG
10460 if ((opcode () == OP_FLOAT
|| opcode () == OP_LONG
) && expect_type
!= NULL
)
10461 result
= ada_value_cast (expect_type
, result
);
10467 ada_string_operation::evaluate (struct type
*expect_type
,
10468 struct expression
*exp
,
10469 enum noside noside
)
10471 value
*result
= string_operation::evaluate (expect_type
, exp
, noside
);
10472 /* The result type will have code OP_STRING, bashed there from
10473 OP_ARRAY. Bash it back. */
10474 if (value_type (result
)->code () == TYPE_CODE_STRING
)
10475 value_type (result
)->set_code (TYPE_CODE_ARRAY
);
10480 ada_qual_operation::evaluate (struct type
*expect_type
,
10481 struct expression
*exp
,
10482 enum noside noside
)
10484 struct type
*type
= std::get
<1> (m_storage
);
10485 return std::get
<0> (m_storage
)->evaluate (type
, exp
, noside
);
10489 ada_ternop_range_operation::evaluate (struct type
*expect_type
,
10490 struct expression
*exp
,
10491 enum noside noside
)
10493 value
*arg0
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
10494 value
*arg1
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
10495 value
*arg2
= std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
10496 return eval_ternop_in_range (expect_type
, exp
, noside
, arg0
, arg1
, arg2
);
10500 ada_binop_addsub_operation::evaluate (struct type
*expect_type
,
10501 struct expression
*exp
,
10502 enum noside noside
)
10504 value
*arg1
= std::get
<1> (m_storage
)->evaluate_with_coercion (exp
, noside
);
10505 value
*arg2
= std::get
<2> (m_storage
)->evaluate_with_coercion (exp
, noside
);
10507 auto do_op
= [=] (LONGEST x
, LONGEST y
)
10509 if (std::get
<0> (m_storage
) == BINOP_ADD
)
10514 if (value_type (arg1
)->code () == TYPE_CODE_PTR
)
10515 return (value_from_longest
10516 (value_type (arg1
),
10517 do_op (value_as_long (arg1
), value_as_long (arg2
))));
10518 if (value_type (arg2
)->code () == TYPE_CODE_PTR
)
10519 return (value_from_longest
10520 (value_type (arg2
),
10521 do_op (value_as_long (arg1
), value_as_long (arg2
))));
10522 /* Preserve the original type for use by the range case below.
10523 We cannot cast the result to a reference type, so if ARG1 is
10524 a reference type, find its underlying type. */
10525 struct type
*type
= value_type (arg1
);
10526 while (type
->code () == TYPE_CODE_REF
)
10527 type
= TYPE_TARGET_TYPE (type
);
10528 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10529 arg1
= value_binop (arg1
, arg2
, std::get
<0> (m_storage
));
10530 /* We need to special-case the result with a range.
10531 This is done for the benefit of "ptype". gdb's Ada support
10532 historically used the LHS to set the result type here, so
10533 preserve this behavior. */
10534 if (type
->code () == TYPE_CODE_RANGE
)
10535 arg1
= value_cast (type
, arg1
);
10540 ada_unop_atr_operation::evaluate (struct type
*expect_type
,
10541 struct expression
*exp
,
10542 enum noside noside
)
10544 struct type
*type_arg
= nullptr;
10545 value
*val
= nullptr;
10547 if (std::get
<0> (m_storage
)->opcode () == OP_TYPE
)
10549 value
*tem
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
10550 EVAL_AVOID_SIDE_EFFECTS
);
10551 type_arg
= value_type (tem
);
10554 val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
10556 return ada_unop_atr (exp
, noside
, std::get
<1> (m_storage
),
10557 val
, type_arg
, std::get
<2> (m_storage
));
10561 ada_var_msym_value_operation::evaluate_for_cast (struct type
*expect_type
,
10562 struct expression
*exp
,
10563 enum noside noside
)
10565 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10566 return value_zero (expect_type
, not_lval
);
10568 value
*val
= evaluate_var_msym_value (noside
,
10569 std::get
<1> (m_storage
),
10570 std::get
<0> (m_storage
));
10572 val
= ada_value_cast (expect_type
, val
);
10574 /* Follow the Ada language semantics that do not allow taking
10575 an address of the result of a cast (view conversion in Ada). */
10576 if (VALUE_LVAL (val
) == lval_memory
)
10578 if (value_lazy (val
))
10579 value_fetch_lazy (val
);
10580 VALUE_LVAL (val
) = not_lval
;
10586 ada_var_value_operation::evaluate_for_cast (struct type
*expect_type
,
10587 struct expression
*exp
,
10588 enum noside noside
)
10590 value
*val
= evaluate_var_value (noside
,
10591 std::get
<1> (m_storage
),
10592 std::get
<0> (m_storage
));
10594 val
= ada_value_cast (expect_type
, val
);
10596 /* Follow the Ada language semantics that do not allow taking
10597 an address of the result of a cast (view conversion in Ada). */
10598 if (VALUE_LVAL (val
) == lval_memory
)
10600 if (value_lazy (val
))
10601 value_fetch_lazy (val
);
10602 VALUE_LVAL (val
) = not_lval
;
10608 ada_var_value_operation::evaluate (struct type
*expect_type
,
10609 struct expression
*exp
,
10610 enum noside noside
)
10612 symbol
*sym
= std::get
<0> (m_storage
);
10614 if (SYMBOL_DOMAIN (sym
) == UNDEF_DOMAIN
)
10615 /* Only encountered when an unresolved symbol occurs in a
10616 context other than a function call, in which case, it is
10618 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10619 sym
->print_name ());
10621 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10623 struct type
*type
= static_unwrap_type (SYMBOL_TYPE (sym
));
10624 /* Check to see if this is a tagged type. We also need to handle
10625 the case where the type is a reference to a tagged type, but
10626 we have to be careful to exclude pointers to tagged types.
10627 The latter should be shown as usual (as a pointer), whereas
10628 a reference should mostly be transparent to the user. */
10629 if (ada_is_tagged_type (type
, 0)
10630 || (type
->code () == TYPE_CODE_REF
10631 && ada_is_tagged_type (TYPE_TARGET_TYPE (type
), 0)))
10633 /* Tagged types are a little special in the fact that the real
10634 type is dynamic and can only be determined by inspecting the
10635 object's tag. This means that we need to get the object's
10636 value first (EVAL_NORMAL) and then extract the actual object
10639 Note that we cannot skip the final step where we extract
10640 the object type from its tag, because the EVAL_NORMAL phase
10641 results in dynamic components being resolved into fixed ones.
10642 This can cause problems when trying to print the type
10643 description of tagged types whose parent has a dynamic size:
10644 We use the type name of the "_parent" component in order
10645 to print the name of the ancestor type in the type description.
10646 If that component had a dynamic size, the resolution into
10647 a fixed type would result in the loss of that type name,
10648 thus preventing us from printing the name of the ancestor
10649 type in the type description. */
10650 value
*arg1
= var_value_operation::evaluate (nullptr, exp
,
10653 if (type
->code () != TYPE_CODE_REF
)
10655 struct type
*actual_type
;
10657 actual_type
= type_from_tag (ada_value_tag (arg1
));
10658 if (actual_type
== NULL
)
10659 /* If, for some reason, we were unable to determine
10660 the actual type from the tag, then use the static
10661 approximation that we just computed as a fallback.
10662 This can happen if the debugging information is
10663 incomplete, for instance. */
10664 actual_type
= type
;
10665 return value_zero (actual_type
, not_lval
);
10669 /* In the case of a ref, ada_coerce_ref takes care
10670 of determining the actual type. But the evaluation
10671 should return a ref as it should be valid to ask
10672 for its address; so rebuild a ref after coerce. */
10673 arg1
= ada_coerce_ref (arg1
);
10674 return value_ref (arg1
, TYPE_CODE_REF
);
10678 /* Records and unions for which GNAT encodings have been
10679 generated need to be statically fixed as well.
10680 Otherwise, non-static fixing produces a type where
10681 all dynamic properties are removed, which prevents "ptype"
10682 from being able to completely describe the type.
10683 For instance, a case statement in a variant record would be
10684 replaced by the relevant components based on the actual
10685 value of the discriminants. */
10686 if ((type
->code () == TYPE_CODE_STRUCT
10687 && dynamic_template_type (type
) != NULL
)
10688 || (type
->code () == TYPE_CODE_UNION
10689 && ada_find_parallel_type (type
, "___XVU") != NULL
))
10690 return value_zero (to_static_fixed_type (type
), not_lval
);
10693 value
*arg1
= var_value_operation::evaluate (expect_type
, exp
, noside
);
10694 return ada_to_fixed_value (arg1
);
10698 ada_atr_val_operation::evaluate (struct type
*expect_type
,
10699 struct expression
*exp
,
10700 enum noside noside
)
10702 value
*arg
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
10703 return ada_val_atr (noside
, std::get
<0> (m_storage
), arg
);
10708 /* Implement the evaluate_exp routine in the exp_descriptor structure
10709 for the Ada language. */
10711 static struct value
*
10712 ada_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
10713 int *pos
, enum noside noside
)
10715 enum exp_opcode op
;
10719 struct value
*arg1
= NULL
, *arg2
= NULL
, *arg3
;
10722 struct value
**argvec
;
10726 op
= exp
->elts
[pc
].opcode
;
10732 arg1
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
10734 if (noside
== EVAL_NORMAL
)
10735 arg1
= unwrap_value (arg1
);
10737 /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
10738 then we need to perform the conversion manually, because
10739 evaluate_subexp_standard doesn't do it. This conversion is
10740 necessary in Ada because the different kinds of float/fixed
10741 types in Ada have different representations.
10743 Similarly, we need to perform the conversion from OP_LONG
10745 if ((op
== OP_FLOAT
|| op
== OP_LONG
) && expect_type
!= NULL
)
10746 arg1
= ada_value_cast (expect_type
, arg1
);
10752 struct value
*result
;
10755 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
10756 /* The result type will have code OP_STRING, bashed there from
10757 OP_ARRAY. Bash it back. */
10758 if (value_type (result
)->code () == TYPE_CODE_STRING
)
10759 value_type (result
)->set_code (TYPE_CODE_ARRAY
);
10765 type
= exp
->elts
[pc
+ 1].type
;
10766 return ada_evaluate_subexp_for_cast (exp
, pos
, noside
, type
);
10770 type
= exp
->elts
[pc
+ 1].type
;
10771 return ada_evaluate_subexp (type
, exp
, pos
, noside
);
10774 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
10775 if (exp
->elts
[*pos
].opcode
== OP_AGGREGATE
)
10777 arg1
= assign_aggregate (arg1
, arg1
, exp
, pos
, noside
);
10778 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
10780 return ada_value_assign (arg1
, arg1
);
10782 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
10783 except if the lhs of our assignment is a convenience variable.
10784 In the case of assigning to a convenience variable, the lhs
10785 should be exactly the result of the evaluation of the rhs. */
10786 type
= value_type (arg1
);
10787 if (VALUE_LVAL (arg1
) == lval_internalvar
)
10789 arg2
= evaluate_subexp (type
, exp
, pos
, noside
);
10790 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
10792 if (VALUE_LVAL (arg1
) == lval_internalvar
)
10797 arg2
= coerce_for_assign (value_type (arg1
), arg2
);
10798 return ada_value_assign (arg1
, arg2
);
10801 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
10802 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
10803 if (noside
== EVAL_SKIP
)
10805 if (value_type (arg1
)->code () == TYPE_CODE_PTR
)
10806 return (value_from_longest
10807 (value_type (arg1
),
10808 value_as_long (arg1
) + value_as_long (arg2
)));
10809 if (value_type (arg2
)->code () == TYPE_CODE_PTR
)
10810 return (value_from_longest
10811 (value_type (arg2
),
10812 value_as_long (arg1
) + value_as_long (arg2
)));
10813 /* Preserve the original type for use by the range case below.
10814 We cannot cast the result to a reference type, so if ARG1 is
10815 a reference type, find its underlying type. */
10816 type
= value_type (arg1
);
10817 while (type
->code () == TYPE_CODE_REF
)
10818 type
= TYPE_TARGET_TYPE (type
);
10819 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10820 arg1
= value_binop (arg1
, arg2
, BINOP_ADD
);
10821 /* We need to special-case the result of adding to a range.
10822 This is done for the benefit of "ptype". gdb's Ada support
10823 historically used the LHS to set the result type here, so
10824 preserve this behavior. */
10825 if (type
->code () == TYPE_CODE_RANGE
)
10826 arg1
= value_cast (type
, arg1
);
10830 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
10831 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
10832 if (noside
== EVAL_SKIP
)
10834 if (value_type (arg1
)->code () == TYPE_CODE_PTR
)
10835 return (value_from_longest
10836 (value_type (arg1
),
10837 value_as_long (arg1
) - value_as_long (arg2
)));
10838 if (value_type (arg2
)->code () == TYPE_CODE_PTR
)
10839 return (value_from_longest
10840 (value_type (arg2
),
10841 value_as_long (arg1
) - value_as_long (arg2
)));
10842 /* Preserve the original type for use by the range case below.
10843 We cannot cast the result to a reference type, so if ARG1 is
10844 a reference type, find its underlying type. */
10845 type
= value_type (arg1
);
10846 while (type
->code () == TYPE_CODE_REF
)
10847 type
= TYPE_TARGET_TYPE (type
);
10848 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
10849 arg1
= value_binop (arg1
, arg2
, BINOP_SUB
);
10850 /* We need to special-case the result of adding to a range.
10851 This is done for the benefit of "ptype". gdb's Ada support
10852 historically used the LHS to set the result type here, so
10853 preserve this behavior. */
10854 if (type
->code () == TYPE_CODE_RANGE
)
10855 arg1
= value_cast (type
, arg1
);
10862 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
10863 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
10864 if (noside
== EVAL_SKIP
)
10866 return ada_mult_binop (expect_type
, exp
, noside
, op
,
10870 case BINOP_NOTEQUAL
:
10871 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
10872 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
10873 if (noside
== EVAL_SKIP
)
10875 return ada_equal_binop (expect_type
, exp
, noside
, op
, arg1
, arg2
);
10878 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
10879 return ada_unop_neg (expect_type
, exp
, noside
, op
, arg1
);
10881 case BINOP_LOGICAL_AND
:
10882 case BINOP_LOGICAL_OR
:
10883 case UNOP_LOGICAL_NOT
:
10888 val
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
10889 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
10890 return value_cast (type
, val
);
10893 case BINOP_BITWISE_AND
:
10894 case BINOP_BITWISE_IOR
:
10895 case BINOP_BITWISE_XOR
:
10899 arg1
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
10901 val
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
10903 return value_cast (value_type (arg1
), val
);
10909 if (noside
== EVAL_SKIP
)
10915 if (SYMBOL_DOMAIN (exp
->elts
[pc
+ 2].symbol
) == UNDEF_DOMAIN
)
10916 /* Only encountered when an unresolved symbol occurs in a
10917 context other than a function call, in which case, it is
10919 error (_("Unexpected unresolved symbol, %s, during evaluation"),
10920 exp
->elts
[pc
+ 2].symbol
->print_name ());
10922 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
10924 type
= static_unwrap_type (SYMBOL_TYPE (exp
->elts
[pc
+ 2].symbol
));
10925 /* Check to see if this is a tagged type. We also need to handle
10926 the case where the type is a reference to a tagged type, but
10927 we have to be careful to exclude pointers to tagged types.
10928 The latter should be shown as usual (as a pointer), whereas
10929 a reference should mostly be transparent to the user. */
10930 if (ada_is_tagged_type (type
, 0)
10931 || (type
->code () == TYPE_CODE_REF
10932 && ada_is_tagged_type (TYPE_TARGET_TYPE (type
), 0)))
10934 /* Tagged types are a little special in the fact that the real
10935 type is dynamic and can only be determined by inspecting the
10936 object's tag. This means that we need to get the object's
10937 value first (EVAL_NORMAL) and then extract the actual object
10940 Note that we cannot skip the final step where we extract
10941 the object type from its tag, because the EVAL_NORMAL phase
10942 results in dynamic components being resolved into fixed ones.
10943 This can cause problems when trying to print the type
10944 description of tagged types whose parent has a dynamic size:
10945 We use the type name of the "_parent" component in order
10946 to print the name of the ancestor type in the type description.
10947 If that component had a dynamic size, the resolution into
10948 a fixed type would result in the loss of that type name,
10949 thus preventing us from printing the name of the ancestor
10950 type in the type description. */
10951 arg1
= evaluate_subexp (nullptr, exp
, pos
, EVAL_NORMAL
);
10953 if (type
->code () != TYPE_CODE_REF
)
10955 struct type
*actual_type
;
10957 actual_type
= type_from_tag (ada_value_tag (arg1
));
10958 if (actual_type
== NULL
)
10959 /* If, for some reason, we were unable to determine
10960 the actual type from the tag, then use the static
10961 approximation that we just computed as a fallback.
10962 This can happen if the debugging information is
10963 incomplete, for instance. */
10964 actual_type
= type
;
10965 return value_zero (actual_type
, not_lval
);
10969 /* In the case of a ref, ada_coerce_ref takes care
10970 of determining the actual type. But the evaluation
10971 should return a ref as it should be valid to ask
10972 for its address; so rebuild a ref after coerce. */
10973 arg1
= ada_coerce_ref (arg1
);
10974 return value_ref (arg1
, TYPE_CODE_REF
);
10978 /* Records and unions for which GNAT encodings have been
10979 generated need to be statically fixed as well.
10980 Otherwise, non-static fixing produces a type where
10981 all dynamic properties are removed, which prevents "ptype"
10982 from being able to completely describe the type.
10983 For instance, a case statement in a variant record would be
10984 replaced by the relevant components based on the actual
10985 value of the discriminants. */
10986 if ((type
->code () == TYPE_CODE_STRUCT
10987 && dynamic_template_type (type
) != NULL
)
10988 || (type
->code () == TYPE_CODE_UNION
10989 && ada_find_parallel_type (type
, "___XVU") != NULL
))
10992 return value_zero (to_static_fixed_type (type
), not_lval
);
10996 arg1
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
10997 return ada_to_fixed_value (arg1
);
11002 /* Allocate arg vector, including space for the function to be
11003 called in argvec[0] and a terminating NULL. */
11004 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
11005 argvec
= XALLOCAVEC (struct value
*, nargs
+ 2);
11007 if (exp
->elts
[*pos
].opcode
== OP_VAR_VALUE
11008 && SYMBOL_DOMAIN (exp
->elts
[pc
+ 5].symbol
) == UNDEF_DOMAIN
)
11009 error (_("Unexpected unresolved symbol, %s, during evaluation"),
11010 exp
->elts
[pc
+ 5].symbol
->print_name ());
11013 for (tem
= 0; tem
<= nargs
; tem
+= 1)
11014 argvec
[tem
] = evaluate_subexp (nullptr, exp
, pos
, noside
);
11017 if (noside
== EVAL_SKIP
)
11021 if (ada_is_constrained_packed_array_type
11022 (desc_base_type (value_type (argvec
[0]))))
11023 argvec
[0] = ada_coerce_to_simple_array (argvec
[0]);
11024 else if (value_type (argvec
[0])->code () == TYPE_CODE_ARRAY
11025 && TYPE_FIELD_BITSIZE (value_type (argvec
[0]), 0) != 0)
11026 /* This is a packed array that has already been fixed, and
11027 therefore already coerced to a simple array. Nothing further
11030 else if (value_type (argvec
[0])->code () == TYPE_CODE_REF
)
11032 /* Make sure we dereference references so that all the code below
11033 feels like it's really handling the referenced value. Wrapping
11034 types (for alignment) may be there, so make sure we strip them as
11036 argvec
[0] = ada_to_fixed_value (coerce_ref (argvec
[0]));
11038 else if (value_type (argvec
[0])->code () == TYPE_CODE_ARRAY
11039 && VALUE_LVAL (argvec
[0]) == lval_memory
)
11040 argvec
[0] = value_addr (argvec
[0]);
11042 type
= ada_check_typedef (value_type (argvec
[0]));
11044 /* Ada allows us to implicitly dereference arrays when subscripting
11045 them. So, if this is an array typedef (encoding use for array
11046 access types encoded as fat pointers), strip it now. */
11047 if (type
->code () == TYPE_CODE_TYPEDEF
)
11048 type
= ada_typedef_target_type (type
);
11050 if (type
->code () == TYPE_CODE_PTR
)
11052 switch (ada_check_typedef (TYPE_TARGET_TYPE (type
))->code ())
11054 case TYPE_CODE_FUNC
:
11055 type
= ada_check_typedef (TYPE_TARGET_TYPE (type
));
11057 case TYPE_CODE_ARRAY
:
11059 case TYPE_CODE_STRUCT
:
11060 if (noside
!= EVAL_AVOID_SIDE_EFFECTS
)
11061 argvec
[0] = ada_value_ind (argvec
[0]);
11062 type
= ada_check_typedef (TYPE_TARGET_TYPE (type
));
11065 error (_("cannot subscript or call something of type `%s'"),
11066 ada_type_name (value_type (argvec
[0])));
11071 switch (type
->code ())
11073 case TYPE_CODE_FUNC
:
11074 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11076 if (TYPE_TARGET_TYPE (type
) == NULL
)
11077 error_call_unknown_return_type (NULL
);
11078 return allocate_value (TYPE_TARGET_TYPE (type
));
11080 return call_function_by_hand (argvec
[0], NULL
,
11081 gdb::make_array_view (argvec
+ 1,
11083 case TYPE_CODE_INTERNAL_FUNCTION
:
11084 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11085 /* We don't know anything about what the internal
11086 function might return, but we have to return
11088 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
11091 return call_internal_function (exp
->gdbarch
, exp
->language_defn
,
11092 argvec
[0], nargs
, argvec
+ 1);
11094 case TYPE_CODE_STRUCT
:
11098 arity
= ada_array_arity (type
);
11099 type
= ada_array_element_type (type
, nargs
);
11101 error (_("cannot subscript or call a record"));
11102 if (arity
!= nargs
)
11103 error (_("wrong number of subscripts; expecting %d"), arity
);
11104 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11105 return value_zero (ada_aligned_type (type
), lval_memory
);
11107 unwrap_value (ada_value_subscript
11108 (argvec
[0], nargs
, argvec
+ 1));
11110 case TYPE_CODE_ARRAY
:
11111 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11113 type
= ada_array_element_type (type
, nargs
);
11115 error (_("element type of array unknown"));
11117 return value_zero (ada_aligned_type (type
), lval_memory
);
11120 unwrap_value (ada_value_subscript
11121 (ada_coerce_to_simple_array (argvec
[0]),
11122 nargs
, argvec
+ 1));
11123 case TYPE_CODE_PTR
: /* Pointer to array */
11124 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11126 type
= to_fixed_array_type (TYPE_TARGET_TYPE (type
), NULL
, 1);
11127 type
= ada_array_element_type (type
, nargs
);
11129 error (_("element type of array unknown"));
11131 return value_zero (ada_aligned_type (type
), lval_memory
);
11134 unwrap_value (ada_value_ptr_subscript (argvec
[0],
11135 nargs
, argvec
+ 1));
11138 error (_("Attempt to index or call something other than an "
11139 "array or function"));
11144 struct value
*array
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11145 struct value
*low_bound_val
11146 = evaluate_subexp (nullptr, exp
, pos
, noside
);
11147 struct value
*high_bound_val
11148 = evaluate_subexp (nullptr, exp
, pos
, noside
);
11150 if (noside
== EVAL_SKIP
)
11153 return ada_ternop_slice (exp
, noside
, array
, low_bound_val
,
11157 case UNOP_IN_RANGE
:
11159 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11160 type
= check_typedef (exp
->elts
[pc
+ 1].type
);
11161 return ada_unop_in_range (expect_type
, exp
, noside
, op
, arg1
, type
);
11163 case BINOP_IN_BOUNDS
:
11165 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11166 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11168 if (noside
== EVAL_SKIP
)
11171 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
11173 return ada_binop_in_bounds (exp
, noside
, arg1
, arg2
, tem
);
11175 case TERNOP_IN_RANGE
:
11176 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11177 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11178 arg3
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11180 return eval_ternop_in_range (expect_type
, exp
, noside
, arg1
, arg2
, arg3
);
11184 case OP_ATR_LENGTH
:
11186 struct type
*type_arg
;
11188 if (exp
->elts
[*pos
].opcode
== OP_TYPE
)
11190 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
11192 type_arg
= check_typedef (exp
->elts
[pc
+ 2].type
);
11196 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11200 if (exp
->elts
[*pos
].opcode
!= OP_LONG
)
11201 error (_("Invalid operand to '%s"), ada_attribute_name (op
));
11202 tem
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
11205 if (noside
== EVAL_SKIP
)
11208 return ada_unop_atr (exp
, noside
, op
, arg1
, type_arg
, tem
);
11212 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11213 if (noside
== EVAL_SKIP
)
11215 return ada_atr_tag (expect_type
, exp
, noside
, op
, arg1
);
11219 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
11220 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11221 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11222 if (noside
== EVAL_SKIP
)
11224 return ada_binop_minmax (expect_type
, exp
, noside
, op
, arg1
, arg2
);
11226 case OP_ATR_MODULUS
:
11228 struct type
*type_arg
= check_typedef (exp
->elts
[pc
+ 2].type
);
11230 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
11231 if (noside
== EVAL_SKIP
)
11234 if (!ada_is_modular_type (type_arg
))
11235 error (_("'modulus must be applied to modular type"));
11237 return value_from_longest (TYPE_TARGET_TYPE (type_arg
),
11238 ada_modulus (type_arg
));
11243 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
11244 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11245 if (noside
== EVAL_SKIP
)
11247 return ada_pos_atr (expect_type
, exp
, noside
, op
, arg1
);
11250 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11251 return ada_atr_size (expect_type
, exp
, noside
, op
, arg1
);
11254 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
11255 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11256 type
= exp
->elts
[pc
+ 2].type
;
11257 if (noside
== EVAL_SKIP
)
11259 return ada_val_atr (noside
, type
, arg1
);
11262 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11263 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11264 if (noside
== EVAL_SKIP
)
11266 return ada_binop_exp (expect_type
, exp
, noside
, op
, arg1
, arg2
);
11269 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11270 if (noside
== EVAL_SKIP
)
11276 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11277 if (noside
== EVAL_SKIP
)
11279 return ada_abs (expect_type
, exp
, noside
, op
, arg1
);
11282 preeval_pos
= *pos
;
11283 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11284 if (noside
== EVAL_SKIP
)
11286 type
= ada_check_typedef (value_type (arg1
));
11287 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11289 if (ada_is_array_descriptor_type (type
))
11290 /* GDB allows dereferencing GNAT array descriptors. */
11292 struct type
*arrType
= ada_type_of_array (arg1
, 0);
11294 if (arrType
== NULL
)
11295 error (_("Attempt to dereference null array pointer."));
11296 return value_at_lazy (arrType
, 0);
11298 else if (type
->code () == TYPE_CODE_PTR
11299 || type
->code () == TYPE_CODE_REF
11300 /* In C you can dereference an array to get the 1st elt. */
11301 || type
->code () == TYPE_CODE_ARRAY
)
11303 /* As mentioned in the OP_VAR_VALUE case, tagged types can
11304 only be determined by inspecting the object's tag.
11305 This means that we need to evaluate completely the
11306 expression in order to get its type. */
11308 if ((type
->code () == TYPE_CODE_REF
11309 || type
->code () == TYPE_CODE_PTR
)
11310 && ada_is_tagged_type (TYPE_TARGET_TYPE (type
), 0))
11313 = evaluate_subexp (nullptr, exp
, &preeval_pos
, EVAL_NORMAL
);
11314 type
= value_type (ada_value_ind (arg1
));
11318 type
= to_static_fixed_type
11320 (ada_check_typedef (TYPE_TARGET_TYPE (type
))));
11322 ada_ensure_varsize_limit (type
);
11323 return value_zero (type
, lval_memory
);
11325 else if (type
->code () == TYPE_CODE_INT
)
11327 /* GDB allows dereferencing an int. */
11328 if (expect_type
== NULL
)
11329 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
11334 to_static_fixed_type (ada_aligned_type (expect_type
));
11335 return value_zero (expect_type
, lval_memory
);
11339 error (_("Attempt to take contents of a non-pointer value."));
11341 arg1
= ada_coerce_ref (arg1
); /* FIXME: What is this for?? */
11342 type
= ada_check_typedef (value_type (arg1
));
11344 if (type
->code () == TYPE_CODE_INT
)
11345 /* GDB allows dereferencing an int. If we were given
11346 the expect_type, then use that as the target type.
11347 Otherwise, assume that the target type is an int. */
11349 if (expect_type
!= NULL
)
11350 return ada_value_ind (value_cast (lookup_pointer_type (expect_type
),
11353 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
11354 (CORE_ADDR
) value_as_address (arg1
));
11357 if (ada_is_array_descriptor_type (type
))
11358 /* GDB allows dereferencing GNAT array descriptors. */
11359 return ada_coerce_to_simple_array (arg1
);
11361 return ada_value_ind (arg1
);
11363 case STRUCTOP_STRUCT
:
11364 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
11365 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
11366 preeval_pos
= *pos
;
11367 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
11368 if (noside
== EVAL_SKIP
)
11370 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11372 struct type
*type1
= value_type (arg1
);
11374 if (ada_is_tagged_type (type1
, 1))
11376 type
= ada_lookup_struct_elt_type (type1
,
11377 &exp
->elts
[pc
+ 2].string
,
11380 /* If the field is not found, check if it exists in the
11381 extension of this object's type. This means that we
11382 need to evaluate completely the expression. */
11387 = evaluate_subexp (nullptr, exp
, &preeval_pos
, EVAL_NORMAL
);
11388 arg1
= ada_value_struct_elt (arg1
,
11389 &exp
->elts
[pc
+ 2].string
,
11391 arg1
= unwrap_value (arg1
);
11392 type
= value_type (ada_to_fixed_value (arg1
));
11397 ada_lookup_struct_elt_type (type1
, &exp
->elts
[pc
+ 2].string
, 1,
11400 return value_zero (ada_aligned_type (type
), lval_memory
);
11404 arg1
= ada_value_struct_elt (arg1
, &exp
->elts
[pc
+ 2].string
, 0);
11405 arg1
= unwrap_value (arg1
);
11406 return ada_to_fixed_value (arg1
);
11410 /* The value is not supposed to be used. This is here to make it
11411 easier to accommodate expressions that contain types. */
11413 if (noside
== EVAL_SKIP
)
11415 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
11416 return allocate_value (exp
->elts
[pc
+ 1].type
);
11418 error (_("Attempt to use a type name as an expression"));
11423 case OP_DISCRETE_RANGE
:
11424 case OP_POSITIONAL
:
11426 if (noside
== EVAL_NORMAL
)
11430 error (_("Undefined name, ambiguous name, or renaming used in "
11431 "component association: %s."), &exp
->elts
[pc
+2].string
);
11433 error (_("Aggregates only allowed on the right of an assignment"));
11435 internal_error (__FILE__
, __LINE__
,
11436 _("aggregate apparently mangled"));
11439 ada_forward_operator_length (exp
, pc
, &oplen
, &nargs
);
11441 for (tem
= 0; tem
< nargs
; tem
+= 1)
11442 ada_evaluate_subexp (NULL
, exp
, pos
, noside
);
11447 return eval_skip_value (exp
);
11451 /* Return non-zero iff TYPE represents a System.Address type. */
11454 ada_is_system_address_type (struct type
*type
)
11456 return (type
->name () && strcmp (type
->name (), "system__address") == 0);
11463 /* Scan STR beginning at position K for a discriminant name, and
11464 return the value of that discriminant field of DVAL in *PX. If
11465 PNEW_K is not null, put the position of the character beyond the
11466 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
11467 not alter *PX and *PNEW_K if unsuccessful. */
11470 scan_discrim_bound (const char *str
, int k
, struct value
*dval
, LONGEST
* px
,
11473 static std::string storage
;
11474 const char *pstart
, *pend
, *bound
;
11475 struct value
*bound_val
;
11477 if (dval
== NULL
|| str
== NULL
|| str
[k
] == '\0')
11481 pend
= strstr (pstart
, "__");
11485 k
+= strlen (bound
);
11489 int len
= pend
- pstart
;
11491 /* Strip __ and beyond. */
11492 storage
= std::string (pstart
, len
);
11493 bound
= storage
.c_str ();
11497 bound_val
= ada_search_struct_field (bound
, dval
, 0, value_type (dval
));
11498 if (bound_val
== NULL
)
11501 *px
= value_as_long (bound_val
);
11502 if (pnew_k
!= NULL
)
11507 /* Value of variable named NAME. Only exact matches are considered.
11508 If no such variable found, then if ERR_MSG is null, returns 0, and
11509 otherwise causes an error with message ERR_MSG. */
11511 static struct value
*
11512 get_var_value (const char *name
, const char *err_msg
)
11514 std::string quoted_name
= add_angle_brackets (name
);
11516 lookup_name_info
lookup_name (quoted_name
, symbol_name_match_type::FULL
);
11518 std::vector
<struct block_symbol
> syms
11519 = ada_lookup_symbol_list_worker (lookup_name
,
11520 get_selected_block (0),
11523 if (syms
.size () != 1)
11525 if (err_msg
== NULL
)
11528 error (("%s"), err_msg
);
11531 return value_of_variable (syms
[0].symbol
, syms
[0].block
);
11534 /* Value of integer variable named NAME in the current environment.
11535 If no such variable is found, returns false. Otherwise, sets VALUE
11536 to the variable's value and returns true. */
11539 get_int_var_value (const char *name
, LONGEST
&value
)
11541 struct value
*var_val
= get_var_value (name
, 0);
11546 value
= value_as_long (var_val
);
11551 /* Return a range type whose base type is that of the range type named
11552 NAME in the current environment, and whose bounds are calculated
11553 from NAME according to the GNAT range encoding conventions.
11554 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
11555 corresponding range type from debug information; fall back to using it
11556 if symbol lookup fails. If a new type must be created, allocate it
11557 like ORIG_TYPE was. The bounds information, in general, is encoded
11558 in NAME, the base type given in the named range type. */
11560 static struct type
*
11561 to_fixed_range_type (struct type
*raw_type
, struct value
*dval
)
11564 struct type
*base_type
;
11565 const char *subtype_info
;
11567 gdb_assert (raw_type
!= NULL
);
11568 gdb_assert (raw_type
->name () != NULL
);
11570 if (raw_type
->code () == TYPE_CODE_RANGE
)
11571 base_type
= TYPE_TARGET_TYPE (raw_type
);
11573 base_type
= raw_type
;
11575 name
= raw_type
->name ();
11576 subtype_info
= strstr (name
, "___XD");
11577 if (subtype_info
== NULL
)
11579 LONGEST L
= ada_discrete_type_low_bound (raw_type
);
11580 LONGEST U
= ada_discrete_type_high_bound (raw_type
);
11582 if (L
< INT_MIN
|| U
> INT_MAX
)
11585 return create_static_range_type (alloc_type_copy (raw_type
), raw_type
,
11590 int prefix_len
= subtype_info
- name
;
11593 const char *bounds_str
;
11597 bounds_str
= strchr (subtype_info
, '_');
11600 if (*subtype_info
== 'L')
11602 if (!ada_scan_number (bounds_str
, n
, &L
, &n
)
11603 && !scan_discrim_bound (bounds_str
, n
, dval
, &L
, &n
))
11605 if (bounds_str
[n
] == '_')
11607 else if (bounds_str
[n
] == '.') /* FIXME? SGI Workshop kludge. */
11613 std::string name_buf
= std::string (name
, prefix_len
) + "___L";
11614 if (!get_int_var_value (name_buf
.c_str (), L
))
11616 lim_warning (_("Unknown lower bound, using 1."));
11621 if (*subtype_info
== 'U')
11623 if (!ada_scan_number (bounds_str
, n
, &U
, &n
)
11624 && !scan_discrim_bound (bounds_str
, n
, dval
, &U
, &n
))
11629 std::string name_buf
= std::string (name
, prefix_len
) + "___U";
11630 if (!get_int_var_value (name_buf
.c_str (), U
))
11632 lim_warning (_("Unknown upper bound, using %ld."), (long) L
);
11637 type
= create_static_range_type (alloc_type_copy (raw_type
),
11639 /* create_static_range_type alters the resulting type's length
11640 to match the size of the base_type, which is not what we want.
11641 Set it back to the original range type's length. */
11642 TYPE_LENGTH (type
) = TYPE_LENGTH (raw_type
);
11643 type
->set_name (name
);
11648 /* True iff NAME is the name of a range type. */
11651 ada_is_range_type_name (const char *name
)
11653 return (name
!= NULL
&& strstr (name
, "___XD"));
11657 /* Modular types */
11659 /* True iff TYPE is an Ada modular type. */
11662 ada_is_modular_type (struct type
*type
)
11664 struct type
*subranged_type
= get_base_type (type
);
11666 return (subranged_type
!= NULL
&& type
->code () == TYPE_CODE_RANGE
11667 && subranged_type
->code () == TYPE_CODE_INT
11668 && subranged_type
->is_unsigned ());
11671 /* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
11674 ada_modulus (struct type
*type
)
11676 const dynamic_prop
&high
= type
->bounds ()->high
;
11678 if (high
.kind () == PROP_CONST
)
11679 return (ULONGEST
) high
.const_val () + 1;
11681 /* If TYPE is unresolved, the high bound might be a location list. Return
11682 0, for lack of a better value to return. */
11687 /* Ada exception catchpoint support:
11688 ---------------------------------
11690 We support 3 kinds of exception catchpoints:
11691 . catchpoints on Ada exceptions
11692 . catchpoints on unhandled Ada exceptions
11693 . catchpoints on failed assertions
11695 Exceptions raised during failed assertions, or unhandled exceptions
11696 could perfectly be caught with the general catchpoint on Ada exceptions.
11697 However, we can easily differentiate these two special cases, and having
11698 the option to distinguish these two cases from the rest can be useful
11699 to zero-in on certain situations.
11701 Exception catchpoints are a specialized form of breakpoint,
11702 since they rely on inserting breakpoints inside known routines
11703 of the GNAT runtime. The implementation therefore uses a standard
11704 breakpoint structure of the BP_BREAKPOINT type, but with its own set
11707 Support in the runtime for exception catchpoints have been changed
11708 a few times already, and these changes affect the implementation
11709 of these catchpoints. In order to be able to support several
11710 variants of the runtime, we use a sniffer that will determine
11711 the runtime variant used by the program being debugged. */
11713 /* Ada's standard exceptions.
11715 The Ada 83 standard also defined Numeric_Error. But there so many
11716 situations where it was unclear from the Ada 83 Reference Manual
11717 (RM) whether Constraint_Error or Numeric_Error should be raised,
11718 that the ARG (Ada Rapporteur Group) eventually issued a Binding
11719 Interpretation saying that anytime the RM says that Numeric_Error
11720 should be raised, the implementation may raise Constraint_Error.
11721 Ada 95 went one step further and pretty much removed Numeric_Error
11722 from the list of standard exceptions (it made it a renaming of
11723 Constraint_Error, to help preserve compatibility when compiling
11724 an Ada83 compiler). As such, we do not include Numeric_Error from
11725 this list of standard exceptions. */
11727 static const char * const standard_exc
[] = {
11728 "constraint_error",
11734 typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype
) (void);
11736 /* A structure that describes how to support exception catchpoints
11737 for a given executable. */
11739 struct exception_support_info
11741 /* The name of the symbol to break on in order to insert
11742 a catchpoint on exceptions. */
11743 const char *catch_exception_sym
;
11745 /* The name of the symbol to break on in order to insert
11746 a catchpoint on unhandled exceptions. */
11747 const char *catch_exception_unhandled_sym
;
11749 /* The name of the symbol to break on in order to insert
11750 a catchpoint on failed assertions. */
11751 const char *catch_assert_sym
;
11753 /* The name of the symbol to break on in order to insert
11754 a catchpoint on exception handling. */
11755 const char *catch_handlers_sym
;
11757 /* Assuming that the inferior just triggered an unhandled exception
11758 catchpoint, this function is responsible for returning the address
11759 in inferior memory where the name of that exception is stored.
11760 Return zero if the address could not be computed. */
11761 ada_unhandled_exception_name_addr_ftype
*unhandled_exception_name_addr
;
11764 static CORE_ADDR
ada_unhandled_exception_name_addr (void);
11765 static CORE_ADDR
ada_unhandled_exception_name_addr_from_raise (void);
11767 /* The following exception support info structure describes how to
11768 implement exception catchpoints with the latest version of the
11769 Ada runtime (as of 2019-08-??). */
11771 static const struct exception_support_info default_exception_support_info
=
11773 "__gnat_debug_raise_exception", /* catch_exception_sym */
11774 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11775 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11776 "__gnat_begin_handler_v1", /* catch_handlers_sym */
11777 ada_unhandled_exception_name_addr
11780 /* The following exception support info structure describes how to
11781 implement exception catchpoints with an earlier version of the
11782 Ada runtime (as of 2007-03-06) using v0 of the EH ABI. */
11784 static const struct exception_support_info exception_support_info_v0
=
11786 "__gnat_debug_raise_exception", /* catch_exception_sym */
11787 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11788 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
11789 "__gnat_begin_handler", /* catch_handlers_sym */
11790 ada_unhandled_exception_name_addr
11793 /* The following exception support info structure describes how to
11794 implement exception catchpoints with a slightly older version
11795 of the Ada runtime. */
11797 static const struct exception_support_info exception_support_info_fallback
=
11799 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
11800 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
11801 "system__assertions__raise_assert_failure", /* catch_assert_sym */
11802 "__gnat_begin_handler", /* catch_handlers_sym */
11803 ada_unhandled_exception_name_addr_from_raise
11806 /* Return nonzero if we can detect the exception support routines
11807 described in EINFO.
11809 This function errors out if an abnormal situation is detected
11810 (for instance, if we find the exception support routines, but
11811 that support is found to be incomplete). */
11814 ada_has_this_exception_support (const struct exception_support_info
*einfo
)
11816 struct symbol
*sym
;
11818 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11819 that should be compiled with debugging information. As a result, we
11820 expect to find that symbol in the symtabs. */
11822 sym
= standard_lookup (einfo
->catch_exception_sym
, NULL
, VAR_DOMAIN
);
11825 /* Perhaps we did not find our symbol because the Ada runtime was
11826 compiled without debugging info, or simply stripped of it.
11827 It happens on some GNU/Linux distributions for instance, where
11828 users have to install a separate debug package in order to get
11829 the runtime's debugging info. In that situation, let the user
11830 know why we cannot insert an Ada exception catchpoint.
11832 Note: Just for the purpose of inserting our Ada exception
11833 catchpoint, we could rely purely on the associated minimal symbol.
11834 But we would be operating in degraded mode anyway, since we are
11835 still lacking the debugging info needed later on to extract
11836 the name of the exception being raised (this name is printed in
11837 the catchpoint message, and is also used when trying to catch
11838 a specific exception). We do not handle this case for now. */
11839 struct bound_minimal_symbol msym
11840 = lookup_minimal_symbol (einfo
->catch_exception_sym
, NULL
, NULL
);
11842 if (msym
.minsym
&& MSYMBOL_TYPE (msym
.minsym
) != mst_solib_trampoline
)
11843 error (_("Your Ada runtime appears to be missing some debugging "
11844 "information.\nCannot insert Ada exception catchpoint "
11845 "in this configuration."));
11850 /* Make sure that the symbol we found corresponds to a function. */
11852 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
11854 error (_("Symbol \"%s\" is not a function (class = %d)"),
11855 sym
->linkage_name (), SYMBOL_CLASS (sym
));
11859 sym
= standard_lookup (einfo
->catch_handlers_sym
, NULL
, VAR_DOMAIN
);
11862 struct bound_minimal_symbol msym
11863 = lookup_minimal_symbol (einfo
->catch_handlers_sym
, NULL
, NULL
);
11865 if (msym
.minsym
&& MSYMBOL_TYPE (msym
.minsym
) != mst_solib_trampoline
)
11866 error (_("Your Ada runtime appears to be missing some debugging "
11867 "information.\nCannot insert Ada exception catchpoint "
11868 "in this configuration."));
11873 /* Make sure that the symbol we found corresponds to a function. */
11875 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
11877 error (_("Symbol \"%s\" is not a function (class = %d)"),
11878 sym
->linkage_name (), SYMBOL_CLASS (sym
));
11885 /* Inspect the Ada runtime and determine which exception info structure
11886 should be used to provide support for exception catchpoints.
11888 This function will always set the per-inferior exception_info,
11889 or raise an error. */
11892 ada_exception_support_info_sniffer (void)
11894 struct ada_inferior_data
*data
= get_ada_inferior_data (current_inferior ());
11896 /* If the exception info is already known, then no need to recompute it. */
11897 if (data
->exception_info
!= NULL
)
11900 /* Check the latest (default) exception support info. */
11901 if (ada_has_this_exception_support (&default_exception_support_info
))
11903 data
->exception_info
= &default_exception_support_info
;
11907 /* Try the v0 exception suport info. */
11908 if (ada_has_this_exception_support (&exception_support_info_v0
))
11910 data
->exception_info
= &exception_support_info_v0
;
11914 /* Try our fallback exception suport info. */
11915 if (ada_has_this_exception_support (&exception_support_info_fallback
))
11917 data
->exception_info
= &exception_support_info_fallback
;
11921 /* Sometimes, it is normal for us to not be able to find the routine
11922 we are looking for. This happens when the program is linked with
11923 the shared version of the GNAT runtime, and the program has not been
11924 started yet. Inform the user of these two possible causes if
11927 if (ada_update_initial_language (language_unknown
) != language_ada
)
11928 error (_("Unable to insert catchpoint. Is this an Ada main program?"));
11930 /* If the symbol does not exist, then check that the program is
11931 already started, to make sure that shared libraries have been
11932 loaded. If it is not started, this may mean that the symbol is
11933 in a shared library. */
11935 if (inferior_ptid
.pid () == 0)
11936 error (_("Unable to insert catchpoint. Try to start the program first."));
11938 /* At this point, we know that we are debugging an Ada program and
11939 that the inferior has been started, but we still are not able to
11940 find the run-time symbols. That can mean that we are in
11941 configurable run time mode, or that a-except as been optimized
11942 out by the linker... In any case, at this point it is not worth
11943 supporting this feature. */
11945 error (_("Cannot insert Ada exception catchpoints in this configuration."));
11948 /* True iff FRAME is very likely to be that of a function that is
11949 part of the runtime system. This is all very heuristic, but is
11950 intended to be used as advice as to what frames are uninteresting
11954 is_known_support_routine (struct frame_info
*frame
)
11956 enum language func_lang
;
11958 const char *fullname
;
11960 /* If this code does not have any debugging information (no symtab),
11961 This cannot be any user code. */
11963 symtab_and_line sal
= find_frame_sal (frame
);
11964 if (sal
.symtab
== NULL
)
11967 /* If there is a symtab, but the associated source file cannot be
11968 located, then assume this is not user code: Selecting a frame
11969 for which we cannot display the code would not be very helpful
11970 for the user. This should also take care of case such as VxWorks
11971 where the kernel has some debugging info provided for a few units. */
11973 fullname
= symtab_to_fullname (sal
.symtab
);
11974 if (access (fullname
, R_OK
) != 0)
11977 /* Check the unit filename against the Ada runtime file naming.
11978 We also check the name of the objfile against the name of some
11979 known system libraries that sometimes come with debugging info
11982 for (i
= 0; known_runtime_file_name_patterns
[i
] != NULL
; i
+= 1)
11984 re_comp (known_runtime_file_name_patterns
[i
]);
11985 if (re_exec (lbasename (sal
.symtab
->filename
)))
11987 if (SYMTAB_OBJFILE (sal
.symtab
) != NULL
11988 && re_exec (objfile_name (SYMTAB_OBJFILE (sal
.symtab
))))
11992 /* Check whether the function is a GNAT-generated entity. */
11994 gdb::unique_xmalloc_ptr
<char> func_name
11995 = find_frame_funname (frame
, &func_lang
, NULL
);
11996 if (func_name
== NULL
)
11999 for (i
= 0; known_auxiliary_function_name_patterns
[i
] != NULL
; i
+= 1)
12001 re_comp (known_auxiliary_function_name_patterns
[i
]);
12002 if (re_exec (func_name
.get ()))
12009 /* Find the first frame that contains debugging information and that is not
12010 part of the Ada run-time, starting from FI and moving upward. */
12013 ada_find_printable_frame (struct frame_info
*fi
)
12015 for (; fi
!= NULL
; fi
= get_prev_frame (fi
))
12017 if (!is_known_support_routine (fi
))
12026 /* Assuming that the inferior just triggered an unhandled exception
12027 catchpoint, return the address in inferior memory where the name
12028 of the exception is stored.
12030 Return zero if the address could not be computed. */
12033 ada_unhandled_exception_name_addr (void)
12035 return parse_and_eval_address ("e.full_name");
12038 /* Same as ada_unhandled_exception_name_addr, except that this function
12039 should be used when the inferior uses an older version of the runtime,
12040 where the exception name needs to be extracted from a specific frame
12041 several frames up in the callstack. */
12044 ada_unhandled_exception_name_addr_from_raise (void)
12047 struct frame_info
*fi
;
12048 struct ada_inferior_data
*data
= get_ada_inferior_data (current_inferior ());
12050 /* To determine the name of this exception, we need to select
12051 the frame corresponding to RAISE_SYM_NAME. This frame is
12052 at least 3 levels up, so we simply skip the first 3 frames
12053 without checking the name of their associated function. */
12054 fi
= get_current_frame ();
12055 for (frame_level
= 0; frame_level
< 3; frame_level
+= 1)
12057 fi
= get_prev_frame (fi
);
12061 enum language func_lang
;
12063 gdb::unique_xmalloc_ptr
<char> func_name
12064 = find_frame_funname (fi
, &func_lang
, NULL
);
12065 if (func_name
!= NULL
)
12067 if (strcmp (func_name
.get (),
12068 data
->exception_info
->catch_exception_sym
) == 0)
12069 break; /* We found the frame we were looking for... */
12071 fi
= get_prev_frame (fi
);
12078 return parse_and_eval_address ("id.full_name");
12081 /* Assuming the inferior just triggered an Ada exception catchpoint
12082 (of any type), return the address in inferior memory where the name
12083 of the exception is stored, if applicable.
12085 Assumes the selected frame is the current frame.
12087 Return zero if the address could not be computed, or if not relevant. */
12090 ada_exception_name_addr_1 (enum ada_exception_catchpoint_kind ex
,
12091 struct breakpoint
*b
)
12093 struct ada_inferior_data
*data
= get_ada_inferior_data (current_inferior ());
12097 case ada_catch_exception
:
12098 return (parse_and_eval_address ("e.full_name"));
12101 case ada_catch_exception_unhandled
:
12102 return data
->exception_info
->unhandled_exception_name_addr ();
12105 case ada_catch_handlers
:
12106 return 0; /* The runtimes does not provide access to the exception
12110 case ada_catch_assert
:
12111 return 0; /* Exception name is not relevant in this case. */
12115 internal_error (__FILE__
, __LINE__
, _("unexpected catchpoint type"));
12119 return 0; /* Should never be reached. */
12122 /* Assuming the inferior is stopped at an exception catchpoint,
12123 return the message which was associated to the exception, if
12124 available. Return NULL if the message could not be retrieved.
12126 Note: The exception message can be associated to an exception
12127 either through the use of the Raise_Exception function, or
12128 more simply (Ada 2005 and later), via:
12130 raise Exception_Name with "exception message";
12134 static gdb::unique_xmalloc_ptr
<char>
12135 ada_exception_message_1 (void)
12137 struct value
*e_msg_val
;
12140 /* For runtimes that support this feature, the exception message
12141 is passed as an unbounded string argument called "message". */
12142 e_msg_val
= parse_and_eval ("message");
12143 if (e_msg_val
== NULL
)
12144 return NULL
; /* Exception message not supported. */
12146 e_msg_val
= ada_coerce_to_simple_array (e_msg_val
);
12147 gdb_assert (e_msg_val
!= NULL
);
12148 e_msg_len
= TYPE_LENGTH (value_type (e_msg_val
));
12150 /* If the message string is empty, then treat it as if there was
12151 no exception message. */
12152 if (e_msg_len
<= 0)
12155 gdb::unique_xmalloc_ptr
<char> e_msg ((char *) xmalloc (e_msg_len
+ 1));
12156 read_memory (value_address (e_msg_val
), (gdb_byte
*) e_msg
.get (),
12158 e_msg
.get ()[e_msg_len
] = '\0';
12163 /* Same as ada_exception_message_1, except that all exceptions are
12164 contained here (returning NULL instead). */
12166 static gdb::unique_xmalloc_ptr
<char>
12167 ada_exception_message (void)
12169 gdb::unique_xmalloc_ptr
<char> e_msg
;
12173 e_msg
= ada_exception_message_1 ();
12175 catch (const gdb_exception_error
&e
)
12177 e_msg
.reset (nullptr);
12183 /* Same as ada_exception_name_addr_1, except that it intercepts and contains
12184 any error that ada_exception_name_addr_1 might cause to be thrown.
12185 When an error is intercepted, a warning with the error message is printed,
12186 and zero is returned. */
12189 ada_exception_name_addr (enum ada_exception_catchpoint_kind ex
,
12190 struct breakpoint
*b
)
12192 CORE_ADDR result
= 0;
12196 result
= ada_exception_name_addr_1 (ex
, b
);
12199 catch (const gdb_exception_error
&e
)
12201 warning (_("failed to get exception name: %s"), e
.what ());
12208 static std::string ada_exception_catchpoint_cond_string
12209 (const char *excep_string
,
12210 enum ada_exception_catchpoint_kind ex
);
12212 /* Ada catchpoints.
12214 In the case of catchpoints on Ada exceptions, the catchpoint will
12215 stop the target on every exception the program throws. When a user
12216 specifies the name of a specific exception, we translate this
12217 request into a condition expression (in text form), and then parse
12218 it into an expression stored in each of the catchpoint's locations.
12219 We then use this condition to check whether the exception that was
12220 raised is the one the user is interested in. If not, then the
12221 target is resumed again. We store the name of the requested
12222 exception, in order to be able to re-set the condition expression
12223 when symbols change. */
12225 /* An instance of this type is used to represent an Ada catchpoint
12226 breakpoint location. */
12228 class ada_catchpoint_location
: public bp_location
12231 ada_catchpoint_location (breakpoint
*owner
)
12232 : bp_location (owner
, bp_loc_software_breakpoint
)
12235 /* The condition that checks whether the exception that was raised
12236 is the specific exception the user specified on catchpoint
12238 expression_up excep_cond_expr
;
12241 /* An instance of this type is used to represent an Ada catchpoint. */
12243 struct ada_catchpoint
: public breakpoint
12245 explicit ada_catchpoint (enum ada_exception_catchpoint_kind kind
)
12250 /* The name of the specific exception the user specified. */
12251 std::string excep_string
;
12253 /* What kind of catchpoint this is. */
12254 enum ada_exception_catchpoint_kind m_kind
;
12257 /* Parse the exception condition string in the context of each of the
12258 catchpoint's locations, and store them for later evaluation. */
12261 create_excep_cond_exprs (struct ada_catchpoint
*c
,
12262 enum ada_exception_catchpoint_kind ex
)
12264 struct bp_location
*bl
;
12266 /* Nothing to do if there's no specific exception to catch. */
12267 if (c
->excep_string
.empty ())
12270 /* Same if there are no locations... */
12271 if (c
->loc
== NULL
)
12274 /* Compute the condition expression in text form, from the specific
12275 expection we want to catch. */
12276 std::string cond_string
12277 = ada_exception_catchpoint_cond_string (c
->excep_string
.c_str (), ex
);
12279 /* Iterate over all the catchpoint's locations, and parse an
12280 expression for each. */
12281 for (bl
= c
->loc
; bl
!= NULL
; bl
= bl
->next
)
12283 struct ada_catchpoint_location
*ada_loc
12284 = (struct ada_catchpoint_location
*) bl
;
12287 if (!bl
->shlib_disabled
)
12291 s
= cond_string
.c_str ();
12294 exp
= parse_exp_1 (&s
, bl
->address
,
12295 block_for_pc (bl
->address
),
12298 catch (const gdb_exception_error
&e
)
12300 warning (_("failed to reevaluate internal exception condition "
12301 "for catchpoint %d: %s"),
12302 c
->number
, e
.what ());
12306 ada_loc
->excep_cond_expr
= std::move (exp
);
12310 /* Implement the ALLOCATE_LOCATION method in the breakpoint_ops
12311 structure for all exception catchpoint kinds. */
12313 static struct bp_location
*
12314 allocate_location_exception (struct breakpoint
*self
)
12316 return new ada_catchpoint_location (self
);
12319 /* Implement the RE_SET method in the breakpoint_ops structure for all
12320 exception catchpoint kinds. */
12323 re_set_exception (struct breakpoint
*b
)
12325 struct ada_catchpoint
*c
= (struct ada_catchpoint
*) b
;
12327 /* Call the base class's method. This updates the catchpoint's
12329 bkpt_breakpoint_ops
.re_set (b
);
12331 /* Reparse the exception conditional expressions. One for each
12333 create_excep_cond_exprs (c
, c
->m_kind
);
12336 /* Returns true if we should stop for this breakpoint hit. If the
12337 user specified a specific exception, we only want to cause a stop
12338 if the program thrown that exception. */
12341 should_stop_exception (const struct bp_location
*bl
)
12343 struct ada_catchpoint
*c
= (struct ada_catchpoint
*) bl
->owner
;
12344 const struct ada_catchpoint_location
*ada_loc
12345 = (const struct ada_catchpoint_location
*) bl
;
12348 struct internalvar
*var
= lookup_internalvar ("_ada_exception");
12349 if (c
->m_kind
== ada_catch_assert
)
12350 clear_internalvar (var
);
12357 if (c
->m_kind
== ada_catch_handlers
)
12358 expr
= ("GNAT_GCC_exception_Access(gcc_exception)"
12359 ".all.occurrence.id");
12363 struct value
*exc
= parse_and_eval (expr
);
12364 set_internalvar (var
, exc
);
12366 catch (const gdb_exception_error
&ex
)
12368 clear_internalvar (var
);
12372 /* With no specific exception, should always stop. */
12373 if (c
->excep_string
.empty ())
12376 if (ada_loc
->excep_cond_expr
== NULL
)
12378 /* We will have a NULL expression if back when we were creating
12379 the expressions, this location's had failed to parse. */
12386 struct value
*mark
;
12388 mark
= value_mark ();
12389 stop
= value_true (evaluate_expression (ada_loc
->excep_cond_expr
.get ()));
12390 value_free_to_mark (mark
);
12392 catch (const gdb_exception
&ex
)
12394 exception_fprintf (gdb_stderr
, ex
,
12395 _("Error in testing exception condition:\n"));
12401 /* Implement the CHECK_STATUS method in the breakpoint_ops structure
12402 for all exception catchpoint kinds. */
12405 check_status_exception (bpstat bs
)
12407 bs
->stop
= should_stop_exception (bs
->bp_location_at
.get ());
12410 /* Implement the PRINT_IT method in the breakpoint_ops structure
12411 for all exception catchpoint kinds. */
12413 static enum print_stop_action
12414 print_it_exception (bpstat bs
)
12416 struct ui_out
*uiout
= current_uiout
;
12417 struct breakpoint
*b
= bs
->breakpoint_at
;
12419 annotate_catchpoint (b
->number
);
12421 if (uiout
->is_mi_like_p ())
12423 uiout
->field_string ("reason",
12424 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT
));
12425 uiout
->field_string ("disp", bpdisp_text (b
->disposition
));
12428 uiout
->text (b
->disposition
== disp_del
12429 ? "\nTemporary catchpoint " : "\nCatchpoint ");
12430 uiout
->field_signed ("bkptno", b
->number
);
12431 uiout
->text (", ");
12433 /* ada_exception_name_addr relies on the selected frame being the
12434 current frame. Need to do this here because this function may be
12435 called more than once when printing a stop, and below, we'll
12436 select the first frame past the Ada run-time (see
12437 ada_find_printable_frame). */
12438 select_frame (get_current_frame ());
12440 struct ada_catchpoint
*c
= (struct ada_catchpoint
*) b
;
12443 case ada_catch_exception
:
12444 case ada_catch_exception_unhandled
:
12445 case ada_catch_handlers
:
12447 const CORE_ADDR addr
= ada_exception_name_addr (c
->m_kind
, b
);
12448 char exception_name
[256];
12452 read_memory (addr
, (gdb_byte
*) exception_name
,
12453 sizeof (exception_name
) - 1);
12454 exception_name
[sizeof (exception_name
) - 1] = '\0';
12458 /* For some reason, we were unable to read the exception
12459 name. This could happen if the Runtime was compiled
12460 without debugging info, for instance. In that case,
12461 just replace the exception name by the generic string
12462 "exception" - it will read as "an exception" in the
12463 notification we are about to print. */
12464 memcpy (exception_name
, "exception", sizeof ("exception"));
12466 /* In the case of unhandled exception breakpoints, we print
12467 the exception name as "unhandled EXCEPTION_NAME", to make
12468 it clearer to the user which kind of catchpoint just got
12469 hit. We used ui_out_text to make sure that this extra
12470 info does not pollute the exception name in the MI case. */
12471 if (c
->m_kind
== ada_catch_exception_unhandled
)
12472 uiout
->text ("unhandled ");
12473 uiout
->field_string ("exception-name", exception_name
);
12476 case ada_catch_assert
:
12477 /* In this case, the name of the exception is not really
12478 important. Just print "failed assertion" to make it clearer
12479 that his program just hit an assertion-failure catchpoint.
12480 We used ui_out_text because this info does not belong in
12482 uiout
->text ("failed assertion");
12486 gdb::unique_xmalloc_ptr
<char> exception_message
= ada_exception_message ();
12487 if (exception_message
!= NULL
)
12489 uiout
->text (" (");
12490 uiout
->field_string ("exception-message", exception_message
.get ());
12494 uiout
->text (" at ");
12495 ada_find_printable_frame (get_current_frame ());
12497 return PRINT_SRC_AND_LOC
;
12500 /* Implement the PRINT_ONE method in the breakpoint_ops structure
12501 for all exception catchpoint kinds. */
12504 print_one_exception (struct breakpoint
*b
, struct bp_location
**last_loc
)
12506 struct ui_out
*uiout
= current_uiout
;
12507 struct ada_catchpoint
*c
= (struct ada_catchpoint
*) b
;
12508 struct value_print_options opts
;
12510 get_user_print_options (&opts
);
12512 if (opts
.addressprint
)
12513 uiout
->field_skip ("addr");
12515 annotate_field (5);
12518 case ada_catch_exception
:
12519 if (!c
->excep_string
.empty ())
12521 std::string msg
= string_printf (_("`%s' Ada exception"),
12522 c
->excep_string
.c_str ());
12524 uiout
->field_string ("what", msg
);
12527 uiout
->field_string ("what", "all Ada exceptions");
12531 case ada_catch_exception_unhandled
:
12532 uiout
->field_string ("what", "unhandled Ada exceptions");
12535 case ada_catch_handlers
:
12536 if (!c
->excep_string
.empty ())
12538 uiout
->field_fmt ("what",
12539 _("`%s' Ada exception handlers"),
12540 c
->excep_string
.c_str ());
12543 uiout
->field_string ("what", "all Ada exceptions handlers");
12546 case ada_catch_assert
:
12547 uiout
->field_string ("what", "failed Ada assertions");
12551 internal_error (__FILE__
, __LINE__
, _("unexpected catchpoint type"));
12556 /* Implement the PRINT_MENTION method in the breakpoint_ops structure
12557 for all exception catchpoint kinds. */
12560 print_mention_exception (struct breakpoint
*b
)
12562 struct ada_catchpoint
*c
= (struct ada_catchpoint
*) b
;
12563 struct ui_out
*uiout
= current_uiout
;
12565 uiout
->text (b
->disposition
== disp_del
? _("Temporary catchpoint ")
12566 : _("Catchpoint "));
12567 uiout
->field_signed ("bkptno", b
->number
);
12568 uiout
->text (": ");
12572 case ada_catch_exception
:
12573 if (!c
->excep_string
.empty ())
12575 std::string info
= string_printf (_("`%s' Ada exception"),
12576 c
->excep_string
.c_str ());
12577 uiout
->text (info
.c_str ());
12580 uiout
->text (_("all Ada exceptions"));
12583 case ada_catch_exception_unhandled
:
12584 uiout
->text (_("unhandled Ada exceptions"));
12587 case ada_catch_handlers
:
12588 if (!c
->excep_string
.empty ())
12591 = string_printf (_("`%s' Ada exception handlers"),
12592 c
->excep_string
.c_str ());
12593 uiout
->text (info
.c_str ());
12596 uiout
->text (_("all Ada exceptions handlers"));
12599 case ada_catch_assert
:
12600 uiout
->text (_("failed Ada assertions"));
12604 internal_error (__FILE__
, __LINE__
, _("unexpected catchpoint type"));
12609 /* Implement the PRINT_RECREATE method in the breakpoint_ops structure
12610 for all exception catchpoint kinds. */
12613 print_recreate_exception (struct breakpoint
*b
, struct ui_file
*fp
)
12615 struct ada_catchpoint
*c
= (struct ada_catchpoint
*) b
;
12619 case ada_catch_exception
:
12620 fprintf_filtered (fp
, "catch exception");
12621 if (!c
->excep_string
.empty ())
12622 fprintf_filtered (fp
, " %s", c
->excep_string
.c_str ());
12625 case ada_catch_exception_unhandled
:
12626 fprintf_filtered (fp
, "catch exception unhandled");
12629 case ada_catch_handlers
:
12630 fprintf_filtered (fp
, "catch handlers");
12633 case ada_catch_assert
:
12634 fprintf_filtered (fp
, "catch assert");
12638 internal_error (__FILE__
, __LINE__
, _("unexpected catchpoint type"));
12640 print_recreate_thread (b
, fp
);
12643 /* Virtual tables for various breakpoint types. */
12644 static struct breakpoint_ops catch_exception_breakpoint_ops
;
12645 static struct breakpoint_ops catch_exception_unhandled_breakpoint_ops
;
12646 static struct breakpoint_ops catch_assert_breakpoint_ops
;
12647 static struct breakpoint_ops catch_handlers_breakpoint_ops
;
12649 /* See ada-lang.h. */
12652 is_ada_exception_catchpoint (breakpoint
*bp
)
12654 return (bp
->ops
== &catch_exception_breakpoint_ops
12655 || bp
->ops
== &catch_exception_unhandled_breakpoint_ops
12656 || bp
->ops
== &catch_assert_breakpoint_ops
12657 || bp
->ops
== &catch_handlers_breakpoint_ops
);
12660 /* Split the arguments specified in a "catch exception" command.
12661 Set EX to the appropriate catchpoint type.
12662 Set EXCEP_STRING to the name of the specific exception if
12663 specified by the user.
12664 IS_CATCH_HANDLERS_CMD: True if the arguments are for a
12665 "catch handlers" command. False otherwise.
12666 If a condition is found at the end of the arguments, the condition
12667 expression is stored in COND_STRING (memory must be deallocated
12668 after use). Otherwise COND_STRING is set to NULL. */
12671 catch_ada_exception_command_split (const char *args
,
12672 bool is_catch_handlers_cmd
,
12673 enum ada_exception_catchpoint_kind
*ex
,
12674 std::string
*excep_string
,
12675 std::string
*cond_string
)
12677 std::string exception_name
;
12679 exception_name
= extract_arg (&args
);
12680 if (exception_name
== "if")
12682 /* This is not an exception name; this is the start of a condition
12683 expression for a catchpoint on all exceptions. So, "un-get"
12684 this token, and set exception_name to NULL. */
12685 exception_name
.clear ();
12689 /* Check to see if we have a condition. */
12691 args
= skip_spaces (args
);
12692 if (startswith (args
, "if")
12693 && (isspace (args
[2]) || args
[2] == '\0'))
12696 args
= skip_spaces (args
);
12698 if (args
[0] == '\0')
12699 error (_("Condition missing after `if' keyword"));
12700 *cond_string
= args
;
12702 args
+= strlen (args
);
12705 /* Check that we do not have any more arguments. Anything else
12708 if (args
[0] != '\0')
12709 error (_("Junk at end of expression"));
12711 if (is_catch_handlers_cmd
)
12713 /* Catch handling of exceptions. */
12714 *ex
= ada_catch_handlers
;
12715 *excep_string
= exception_name
;
12717 else if (exception_name
.empty ())
12719 /* Catch all exceptions. */
12720 *ex
= ada_catch_exception
;
12721 excep_string
->clear ();
12723 else if (exception_name
== "unhandled")
12725 /* Catch unhandled exceptions. */
12726 *ex
= ada_catch_exception_unhandled
;
12727 excep_string
->clear ();
12731 /* Catch a specific exception. */
12732 *ex
= ada_catch_exception
;
12733 *excep_string
= exception_name
;
12737 /* Return the name of the symbol on which we should break in order to
12738 implement a catchpoint of the EX kind. */
12740 static const char *
12741 ada_exception_sym_name (enum ada_exception_catchpoint_kind ex
)
12743 struct ada_inferior_data
*data
= get_ada_inferior_data (current_inferior ());
12745 gdb_assert (data
->exception_info
!= NULL
);
12749 case ada_catch_exception
:
12750 return (data
->exception_info
->catch_exception_sym
);
12752 case ada_catch_exception_unhandled
:
12753 return (data
->exception_info
->catch_exception_unhandled_sym
);
12755 case ada_catch_assert
:
12756 return (data
->exception_info
->catch_assert_sym
);
12758 case ada_catch_handlers
:
12759 return (data
->exception_info
->catch_handlers_sym
);
12762 internal_error (__FILE__
, __LINE__
,
12763 _("unexpected catchpoint kind (%d)"), ex
);
12767 /* Return the breakpoint ops "virtual table" used for catchpoints
12770 static const struct breakpoint_ops
*
12771 ada_exception_breakpoint_ops (enum ada_exception_catchpoint_kind ex
)
12775 case ada_catch_exception
:
12776 return (&catch_exception_breakpoint_ops
);
12778 case ada_catch_exception_unhandled
:
12779 return (&catch_exception_unhandled_breakpoint_ops
);
12781 case ada_catch_assert
:
12782 return (&catch_assert_breakpoint_ops
);
12784 case ada_catch_handlers
:
12785 return (&catch_handlers_breakpoint_ops
);
12788 internal_error (__FILE__
, __LINE__
,
12789 _("unexpected catchpoint kind (%d)"), ex
);
12793 /* Return the condition that will be used to match the current exception
12794 being raised with the exception that the user wants to catch. This
12795 assumes that this condition is used when the inferior just triggered
12796 an exception catchpoint.
12797 EX: the type of catchpoints used for catching Ada exceptions. */
12800 ada_exception_catchpoint_cond_string (const char *excep_string
,
12801 enum ada_exception_catchpoint_kind ex
)
12804 bool is_standard_exc
= false;
12805 std::string result
;
12807 if (ex
== ada_catch_handlers
)
12809 /* For exception handlers catchpoints, the condition string does
12810 not use the same parameter as for the other exceptions. */
12811 result
= ("long_integer (GNAT_GCC_exception_Access"
12812 "(gcc_exception).all.occurrence.id)");
12815 result
= "long_integer (e)";
12817 /* The standard exceptions are a special case. They are defined in
12818 runtime units that have been compiled without debugging info; if
12819 EXCEP_STRING is the not-fully-qualified name of a standard
12820 exception (e.g. "constraint_error") then, during the evaluation
12821 of the condition expression, the symbol lookup on this name would
12822 *not* return this standard exception. The catchpoint condition
12823 may then be set only on user-defined exceptions which have the
12824 same not-fully-qualified name (e.g. my_package.constraint_error).
12826 To avoid this unexcepted behavior, these standard exceptions are
12827 systematically prefixed by "standard". This means that "catch
12828 exception constraint_error" is rewritten into "catch exception
12829 standard.constraint_error".
12831 If an exception named constraint_error is defined in another package of
12832 the inferior program, then the only way to specify this exception as a
12833 breakpoint condition is to use its fully-qualified named:
12834 e.g. my_package.constraint_error. */
12836 for (i
= 0; i
< sizeof (standard_exc
) / sizeof (char *); i
++)
12838 if (strcmp (standard_exc
[i
], excep_string
) == 0)
12840 is_standard_exc
= true;
12847 if (is_standard_exc
)
12848 string_appendf (result
, "long_integer (&standard.%s)", excep_string
);
12850 string_appendf (result
, "long_integer (&%s)", excep_string
);
12855 /* Return the symtab_and_line that should be used to insert an exception
12856 catchpoint of the TYPE kind.
12858 ADDR_STRING returns the name of the function where the real
12859 breakpoint that implements the catchpoints is set, depending on the
12860 type of catchpoint we need to create. */
12862 static struct symtab_and_line
12863 ada_exception_sal (enum ada_exception_catchpoint_kind ex
,
12864 std::string
*addr_string
, const struct breakpoint_ops
**ops
)
12866 const char *sym_name
;
12867 struct symbol
*sym
;
12869 /* First, find out which exception support info to use. */
12870 ada_exception_support_info_sniffer ();
12872 /* Then lookup the function on which we will break in order to catch
12873 the Ada exceptions requested by the user. */
12874 sym_name
= ada_exception_sym_name (ex
);
12875 sym
= standard_lookup (sym_name
, NULL
, VAR_DOMAIN
);
12878 error (_("Catchpoint symbol not found: %s"), sym_name
);
12880 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
12881 error (_("Unable to insert catchpoint. %s is not a function."), sym_name
);
12883 /* Set ADDR_STRING. */
12884 *addr_string
= sym_name
;
12887 *ops
= ada_exception_breakpoint_ops (ex
);
12889 return find_function_start_sal (sym
, 1);
12892 /* Create an Ada exception catchpoint.
12894 EX_KIND is the kind of exception catchpoint to be created.
12896 If EXCEPT_STRING is empty, this catchpoint is expected to trigger
12897 for all exceptions. Otherwise, EXCEPT_STRING indicates the name
12898 of the exception to which this catchpoint applies.
12900 COND_STRING, if not empty, is the catchpoint condition.
12902 TEMPFLAG, if nonzero, means that the underlying breakpoint
12903 should be temporary.
12905 FROM_TTY is the usual argument passed to all commands implementations. */
12908 create_ada_exception_catchpoint (struct gdbarch
*gdbarch
,
12909 enum ada_exception_catchpoint_kind ex_kind
,
12910 const std::string
&excep_string
,
12911 const std::string
&cond_string
,
12916 std::string addr_string
;
12917 const struct breakpoint_ops
*ops
= NULL
;
12918 struct symtab_and_line sal
= ada_exception_sal (ex_kind
, &addr_string
, &ops
);
12920 std::unique_ptr
<ada_catchpoint
> c (new ada_catchpoint (ex_kind
));
12921 init_ada_exception_breakpoint (c
.get (), gdbarch
, sal
, addr_string
.c_str (),
12922 ops
, tempflag
, disabled
, from_tty
);
12923 c
->excep_string
= excep_string
;
12924 create_excep_cond_exprs (c
.get (), ex_kind
);
12925 if (!cond_string
.empty ())
12926 set_breakpoint_condition (c
.get (), cond_string
.c_str (), from_tty
, false);
12927 install_breakpoint (0, std::move (c
), 1);
12930 /* Implement the "catch exception" command. */
12933 catch_ada_exception_command (const char *arg_entry
, int from_tty
,
12934 struct cmd_list_element
*command
)
12936 const char *arg
= arg_entry
;
12937 struct gdbarch
*gdbarch
= get_current_arch ();
12939 enum ada_exception_catchpoint_kind ex_kind
;
12940 std::string excep_string
;
12941 std::string cond_string
;
12943 tempflag
= get_cmd_context (command
) == CATCH_TEMPORARY
;
12947 catch_ada_exception_command_split (arg
, false, &ex_kind
, &excep_string
,
12949 create_ada_exception_catchpoint (gdbarch
, ex_kind
,
12950 excep_string
, cond_string
,
12951 tempflag
, 1 /* enabled */,
12955 /* Implement the "catch handlers" command. */
12958 catch_ada_handlers_command (const char *arg_entry
, int from_tty
,
12959 struct cmd_list_element
*command
)
12961 const char *arg
= arg_entry
;
12962 struct gdbarch
*gdbarch
= get_current_arch ();
12964 enum ada_exception_catchpoint_kind ex_kind
;
12965 std::string excep_string
;
12966 std::string cond_string
;
12968 tempflag
= get_cmd_context (command
) == CATCH_TEMPORARY
;
12972 catch_ada_exception_command_split (arg
, true, &ex_kind
, &excep_string
,
12974 create_ada_exception_catchpoint (gdbarch
, ex_kind
,
12975 excep_string
, cond_string
,
12976 tempflag
, 1 /* enabled */,
12980 /* Completion function for the Ada "catch" commands. */
12983 catch_ada_completer (struct cmd_list_element
*cmd
, completion_tracker
&tracker
,
12984 const char *text
, const char *word
)
12986 std::vector
<ada_exc_info
> exceptions
= ada_exceptions_list (NULL
);
12988 for (const ada_exc_info
&info
: exceptions
)
12990 if (startswith (info
.name
, word
))
12991 tracker
.add_completion (make_unique_xstrdup (info
.name
));
12995 /* Split the arguments specified in a "catch assert" command.
12997 ARGS contains the command's arguments (or the empty string if
12998 no arguments were passed).
13000 If ARGS contains a condition, set COND_STRING to that condition
13001 (the memory needs to be deallocated after use). */
13004 catch_ada_assert_command_split (const char *args
, std::string
&cond_string
)
13006 args
= skip_spaces (args
);
13008 /* Check whether a condition was provided. */
13009 if (startswith (args
, "if")
13010 && (isspace (args
[2]) || args
[2] == '\0'))
13013 args
= skip_spaces (args
);
13014 if (args
[0] == '\0')
13015 error (_("condition missing after `if' keyword"));
13016 cond_string
.assign (args
);
13019 /* Otherwise, there should be no other argument at the end of
13021 else if (args
[0] != '\0')
13022 error (_("Junk at end of arguments."));
13025 /* Implement the "catch assert" command. */
13028 catch_assert_command (const char *arg_entry
, int from_tty
,
13029 struct cmd_list_element
*command
)
13031 const char *arg
= arg_entry
;
13032 struct gdbarch
*gdbarch
= get_current_arch ();
13034 std::string cond_string
;
13036 tempflag
= get_cmd_context (command
) == CATCH_TEMPORARY
;
13040 catch_ada_assert_command_split (arg
, cond_string
);
13041 create_ada_exception_catchpoint (gdbarch
, ada_catch_assert
,
13043 tempflag
, 1 /* enabled */,
13047 /* Return non-zero if the symbol SYM is an Ada exception object. */
13050 ada_is_exception_sym (struct symbol
*sym
)
13052 const char *type_name
= SYMBOL_TYPE (sym
)->name ();
13054 return (SYMBOL_CLASS (sym
) != LOC_TYPEDEF
13055 && SYMBOL_CLASS (sym
) != LOC_BLOCK
13056 && SYMBOL_CLASS (sym
) != LOC_CONST
13057 && SYMBOL_CLASS (sym
) != LOC_UNRESOLVED
13058 && type_name
!= NULL
&& strcmp (type_name
, "exception") == 0);
13061 /* Given a global symbol SYM, return non-zero iff SYM is a non-standard
13062 Ada exception object. This matches all exceptions except the ones
13063 defined by the Ada language. */
13066 ada_is_non_standard_exception_sym (struct symbol
*sym
)
13070 if (!ada_is_exception_sym (sym
))
13073 for (i
= 0; i
< ARRAY_SIZE (standard_exc
); i
++)
13074 if (strcmp (sym
->linkage_name (), standard_exc
[i
]) == 0)
13075 return 0; /* A standard exception. */
13077 /* Numeric_Error is also a standard exception, so exclude it.
13078 See the STANDARD_EXC description for more details as to why
13079 this exception is not listed in that array. */
13080 if (strcmp (sym
->linkage_name (), "numeric_error") == 0)
13086 /* A helper function for std::sort, comparing two struct ada_exc_info
13089 The comparison is determined first by exception name, and then
13090 by exception address. */
13093 ada_exc_info::operator< (const ada_exc_info
&other
) const
13097 result
= strcmp (name
, other
.name
);
13100 if (result
== 0 && addr
< other
.addr
)
13106 ada_exc_info::operator== (const ada_exc_info
&other
) const
13108 return addr
== other
.addr
&& strcmp (name
, other
.name
) == 0;
13111 /* Sort EXCEPTIONS using compare_ada_exception_info as the comparison
13112 routine, but keeping the first SKIP elements untouched.
13114 All duplicates are also removed. */
13117 sort_remove_dups_ada_exceptions_list (std::vector
<ada_exc_info
> *exceptions
,
13120 std::sort (exceptions
->begin () + skip
, exceptions
->end ());
13121 exceptions
->erase (std::unique (exceptions
->begin () + skip
, exceptions
->end ()),
13122 exceptions
->end ());
13125 /* Add all exceptions defined by the Ada standard whose name match
13126 a regular expression.
13128 If PREG is not NULL, then this regexp_t object is used to
13129 perform the symbol name matching. Otherwise, no name-based
13130 filtering is performed.
13132 EXCEPTIONS is a vector of exceptions to which matching exceptions
13136 ada_add_standard_exceptions (compiled_regex
*preg
,
13137 std::vector
<ada_exc_info
> *exceptions
)
13141 for (i
= 0; i
< ARRAY_SIZE (standard_exc
); i
++)
13144 || preg
->exec (standard_exc
[i
], 0, NULL
, 0) == 0)
13146 struct bound_minimal_symbol msymbol
13147 = ada_lookup_simple_minsym (standard_exc
[i
]);
13149 if (msymbol
.minsym
!= NULL
)
13151 struct ada_exc_info info
13152 = {standard_exc
[i
], BMSYMBOL_VALUE_ADDRESS (msymbol
)};
13154 exceptions
->push_back (info
);
13160 /* Add all Ada exceptions defined locally and accessible from the given
13163 If PREG is not NULL, then this regexp_t object is used to
13164 perform the symbol name matching. Otherwise, no name-based
13165 filtering is performed.
13167 EXCEPTIONS is a vector of exceptions to which matching exceptions
13171 ada_add_exceptions_from_frame (compiled_regex
*preg
,
13172 struct frame_info
*frame
,
13173 std::vector
<ada_exc_info
> *exceptions
)
13175 const struct block
*block
= get_frame_block (frame
, 0);
13179 struct block_iterator iter
;
13180 struct symbol
*sym
;
13182 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
13184 switch (SYMBOL_CLASS (sym
))
13191 if (ada_is_exception_sym (sym
))
13193 struct ada_exc_info info
= {sym
->print_name (),
13194 SYMBOL_VALUE_ADDRESS (sym
)};
13196 exceptions
->push_back (info
);
13200 if (BLOCK_FUNCTION (block
) != NULL
)
13202 block
= BLOCK_SUPERBLOCK (block
);
13206 /* Return true if NAME matches PREG or if PREG is NULL. */
13209 name_matches_regex (const char *name
, compiled_regex
*preg
)
13211 return (preg
== NULL
13212 || preg
->exec (ada_decode (name
).c_str (), 0, NULL
, 0) == 0);
13215 /* Add all exceptions defined globally whose name name match
13216 a regular expression, excluding standard exceptions.
13218 The reason we exclude standard exceptions is that they need
13219 to be handled separately: Standard exceptions are defined inside
13220 a runtime unit which is normally not compiled with debugging info,
13221 and thus usually do not show up in our symbol search. However,
13222 if the unit was in fact built with debugging info, we need to
13223 exclude them because they would duplicate the entry we found
13224 during the special loop that specifically searches for those
13225 standard exceptions.
13227 If PREG is not NULL, then this regexp_t object is used to
13228 perform the symbol name matching. Otherwise, no name-based
13229 filtering is performed.
13231 EXCEPTIONS is a vector of exceptions to which matching exceptions
13235 ada_add_global_exceptions (compiled_regex
*preg
,
13236 std::vector
<ada_exc_info
> *exceptions
)
13238 /* In Ada, the symbol "search name" is a linkage name, whereas the
13239 regular expression used to do the matching refers to the natural
13240 name. So match against the decoded name. */
13241 expand_symtabs_matching (NULL
,
13242 lookup_name_info::match_any (),
13243 [&] (const char *search_name
)
13245 std::string decoded
= ada_decode (search_name
);
13246 return name_matches_regex (decoded
.c_str (), preg
);
13251 for (objfile
*objfile
: current_program_space
->objfiles ())
13253 for (compunit_symtab
*s
: objfile
->compunits ())
13255 const struct blockvector
*bv
= COMPUNIT_BLOCKVECTOR (s
);
13258 for (i
= GLOBAL_BLOCK
; i
<= STATIC_BLOCK
; i
++)
13260 const struct block
*b
= BLOCKVECTOR_BLOCK (bv
, i
);
13261 struct block_iterator iter
;
13262 struct symbol
*sym
;
13264 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
13265 if (ada_is_non_standard_exception_sym (sym
)
13266 && name_matches_regex (sym
->natural_name (), preg
))
13268 struct ada_exc_info info
13269 = {sym
->print_name (), SYMBOL_VALUE_ADDRESS (sym
)};
13271 exceptions
->push_back (info
);
13278 /* Implements ada_exceptions_list with the regular expression passed
13279 as a regex_t, rather than a string.
13281 If not NULL, PREG is used to filter out exceptions whose names
13282 do not match. Otherwise, all exceptions are listed. */
13284 static std::vector
<ada_exc_info
>
13285 ada_exceptions_list_1 (compiled_regex
*preg
)
13287 std::vector
<ada_exc_info
> result
;
13290 /* First, list the known standard exceptions. These exceptions
13291 need to be handled separately, as they are usually defined in
13292 runtime units that have been compiled without debugging info. */
13294 ada_add_standard_exceptions (preg
, &result
);
13296 /* Next, find all exceptions whose scope is local and accessible
13297 from the currently selected frame. */
13299 if (has_stack_frames ())
13301 prev_len
= result
.size ();
13302 ada_add_exceptions_from_frame (preg
, get_selected_frame (NULL
),
13304 if (result
.size () > prev_len
)
13305 sort_remove_dups_ada_exceptions_list (&result
, prev_len
);
13308 /* Add all exceptions whose scope is global. */
13310 prev_len
= result
.size ();
13311 ada_add_global_exceptions (preg
, &result
);
13312 if (result
.size () > prev_len
)
13313 sort_remove_dups_ada_exceptions_list (&result
, prev_len
);
13318 /* Return a vector of ada_exc_info.
13320 If REGEXP is NULL, all exceptions are included in the result.
13321 Otherwise, it should contain a valid regular expression,
13322 and only the exceptions whose names match that regular expression
13323 are included in the result.
13325 The exceptions are sorted in the following order:
13326 - Standard exceptions (defined by the Ada language), in
13327 alphabetical order;
13328 - Exceptions only visible from the current frame, in
13329 alphabetical order;
13330 - Exceptions whose scope is global, in alphabetical order. */
13332 std::vector
<ada_exc_info
>
13333 ada_exceptions_list (const char *regexp
)
13335 if (regexp
== NULL
)
13336 return ada_exceptions_list_1 (NULL
);
13338 compiled_regex
reg (regexp
, REG_NOSUB
, _("invalid regular expression"));
13339 return ada_exceptions_list_1 (®
);
13342 /* Implement the "info exceptions" command. */
13345 info_exceptions_command (const char *regexp
, int from_tty
)
13347 struct gdbarch
*gdbarch
= get_current_arch ();
13349 std::vector
<ada_exc_info
> exceptions
= ada_exceptions_list (regexp
);
13351 if (regexp
!= NULL
)
13353 (_("All Ada exceptions matching regular expression \"%s\":\n"), regexp
);
13355 printf_filtered (_("All defined Ada exceptions:\n"));
13357 for (const ada_exc_info
&info
: exceptions
)
13358 printf_filtered ("%s: %s\n", info
.name
, paddress (gdbarch
, info
.addr
));
13362 /* Information about operators given special treatment in functions
13364 /* Format: OP_DEFN (<operator>, <operator length>, <# args>, <binop>). */
13366 #define ADA_OPERATORS \
13367 OP_DEFN (OP_VAR_VALUE, 4, 0, 0) \
13368 OP_DEFN (BINOP_IN_BOUNDS, 3, 2, 0) \
13369 OP_DEFN (TERNOP_IN_RANGE, 1, 3, 0) \
13370 OP_DEFN (OP_ATR_FIRST, 1, 2, 0) \
13371 OP_DEFN (OP_ATR_LAST, 1, 2, 0) \
13372 OP_DEFN (OP_ATR_LENGTH, 1, 2, 0) \
13373 OP_DEFN (OP_ATR_IMAGE, 1, 2, 0) \
13374 OP_DEFN (OP_ATR_MAX, 1, 3, 0) \
13375 OP_DEFN (OP_ATR_MIN, 1, 3, 0) \
13376 OP_DEFN (OP_ATR_MODULUS, 1, 1, 0) \
13377 OP_DEFN (OP_ATR_POS, 1, 2, 0) \
13378 OP_DEFN (OP_ATR_SIZE, 1, 1, 0) \
13379 OP_DEFN (OP_ATR_TAG, 1, 1, 0) \
13380 OP_DEFN (OP_ATR_VAL, 1, 2, 0) \
13381 OP_DEFN (UNOP_QUAL, 3, 1, 0) \
13382 OP_DEFN (UNOP_IN_RANGE, 3, 1, 0) \
13383 OP_DEFN (OP_OTHERS, 1, 1, 0) \
13384 OP_DEFN (OP_POSITIONAL, 3, 1, 0) \
13385 OP_DEFN (OP_DISCRETE_RANGE, 1, 2, 0)
13388 ada_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
13391 switch (exp
->elts
[pc
- 1].opcode
)
13394 operator_length_standard (exp
, pc
, oplenp
, argsp
);
13397 #define OP_DEFN(op, len, args, binop) \
13398 case op: *oplenp = len; *argsp = args; break;
13404 *argsp
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
13409 *argsp
= longest_to_int (exp
->elts
[pc
- 2].longconst
) + 1;
13414 /* Implementation of the exp_descriptor method operator_check. */
13417 ada_operator_check (struct expression
*exp
, int pos
,
13418 int (*objfile_func
) (struct objfile
*objfile
, void *data
),
13421 const union exp_element
*const elts
= exp
->elts
;
13422 struct type
*type
= NULL
;
13424 switch (elts
[pos
].opcode
)
13426 case UNOP_IN_RANGE
:
13428 type
= elts
[pos
+ 1].type
;
13432 return operator_check_standard (exp
, pos
, objfile_func
, data
);
13435 /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL. */
13437 if (type
!= nullptr && type
->objfile_owner () != nullptr
13438 && objfile_func (type
->objfile_owner (), data
))
13444 /* As for operator_length, but assumes PC is pointing at the first
13445 element of the operator, and gives meaningful results only for the
13446 Ada-specific operators, returning 0 for *OPLENP and *ARGSP otherwise. */
13449 ada_forward_operator_length (struct expression
*exp
, int pc
,
13450 int *oplenp
, int *argsp
)
13452 switch (exp
->elts
[pc
].opcode
)
13455 *oplenp
= *argsp
= 0;
13458 #define OP_DEFN(op, len, args, binop) \
13459 case op: *oplenp = len; *argsp = args; break;
13465 *argsp
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
13470 *argsp
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
13476 int len
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
13478 *oplenp
= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
13486 ada_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
, int elt
)
13488 enum exp_opcode op
= exp
->elts
[elt
].opcode
;
13493 ada_forward_operator_length (exp
, elt
, &oplen
, &nargs
);
13497 /* Ada attributes ('Foo). */
13500 case OP_ATR_LENGTH
:
13504 case OP_ATR_MODULUS
:
13511 case UNOP_IN_RANGE
:
13513 /* XXX: gdb_sprint_host_address, type_sprint */
13514 fprintf_filtered (stream
, _("Type @"));
13515 gdb_print_host_address (exp
->elts
[pc
+ 1].type
, stream
);
13516 fprintf_filtered (stream
, " (");
13517 type_print (exp
->elts
[pc
+ 1].type
, NULL
, stream
, 0);
13518 fprintf_filtered (stream
, ")");
13520 case BINOP_IN_BOUNDS
:
13521 fprintf_filtered (stream
, " (%d)",
13522 longest_to_int (exp
->elts
[pc
+ 2].longconst
));
13524 case TERNOP_IN_RANGE
:
13529 case OP_DISCRETE_RANGE
:
13530 case OP_POSITIONAL
:
13537 char *name
= &exp
->elts
[elt
+ 2].string
;
13538 int len
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
13540 fprintf_filtered (stream
, "Text: `%.*s'", len
, name
);
13545 return dump_subexp_body_standard (exp
, stream
, elt
);
13549 for (i
= 0; i
< nargs
; i
+= 1)
13550 elt
= dump_subexp (exp
, stream
, elt
);
13555 /* The Ada extension of print_subexp (q.v.). */
13558 ada_print_subexp (struct expression
*exp
, int *pos
,
13559 struct ui_file
*stream
, enum precedence prec
)
13561 int oplen
, nargs
, i
;
13563 enum exp_opcode op
= exp
->elts
[pc
].opcode
;
13565 ada_forward_operator_length (exp
, pc
, &oplen
, &nargs
);
13572 print_subexp_standard (exp
, pos
, stream
, prec
);
13576 fputs_filtered (exp
->elts
[pc
+ 2].symbol
->natural_name (), stream
);
13579 case BINOP_IN_BOUNDS
:
13580 /* XXX: sprint_subexp */
13581 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
13582 fputs_filtered (" in ", stream
);
13583 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
13584 fputs_filtered ("'range", stream
);
13585 if (exp
->elts
[pc
+ 1].longconst
> 1)
13586 fprintf_filtered (stream
, "(%ld)",
13587 (long) exp
->elts
[pc
+ 1].longconst
);
13590 case TERNOP_IN_RANGE
:
13591 if (prec
>= PREC_EQUAL
)
13592 fputs_filtered ("(", stream
);
13593 /* XXX: sprint_subexp */
13594 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
13595 fputs_filtered (" in ", stream
);
13596 print_subexp (exp
, pos
, stream
, PREC_EQUAL
);
13597 fputs_filtered (" .. ", stream
);
13598 print_subexp (exp
, pos
, stream
, PREC_EQUAL
);
13599 if (prec
>= PREC_EQUAL
)
13600 fputs_filtered (")", stream
);
13605 case OP_ATR_LENGTH
:
13609 case OP_ATR_MODULUS
:
13614 if (exp
->elts
[*pos
].opcode
== OP_TYPE
)
13616 if (exp
->elts
[*pos
+ 1].type
->code () != TYPE_CODE_VOID
)
13617 LA_PRINT_TYPE (exp
->elts
[*pos
+ 1].type
, "", stream
, 0, 0,
13618 &type_print_raw_options
);
13622 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
13623 fprintf_filtered (stream
, "'%s", ada_attribute_name (op
));
13628 for (tem
= 1; tem
< nargs
; tem
+= 1)
13630 fputs_filtered ((tem
== 1) ? " (" : ", ", stream
);
13631 print_subexp (exp
, pos
, stream
, PREC_ABOVE_COMMA
);
13633 fputs_filtered (")", stream
);
13638 type_print (exp
->elts
[pc
+ 1].type
, "", stream
, 0);
13639 fputs_filtered ("'(", stream
);
13640 print_subexp (exp
, pos
, stream
, PREC_PREFIX
);
13641 fputs_filtered (")", stream
);
13644 case UNOP_IN_RANGE
:
13645 /* XXX: sprint_subexp */
13646 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
13647 fputs_filtered (" in ", stream
);
13648 LA_PRINT_TYPE (exp
->elts
[pc
+ 1].type
, "", stream
, 1, 0,
13649 &type_print_raw_options
);
13652 case OP_DISCRETE_RANGE
:
13653 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
13654 fputs_filtered ("..", stream
);
13655 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
13659 fputs_filtered ("others => ", stream
);
13660 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
13664 for (i
= 0; i
< nargs
-1; i
+= 1)
13667 fputs_filtered ("|", stream
);
13668 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
13670 fputs_filtered (" => ", stream
);
13671 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
13674 case OP_POSITIONAL
:
13675 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
13679 fputs_filtered ("(", stream
);
13680 for (i
= 0; i
< nargs
; i
+= 1)
13683 fputs_filtered (", ", stream
);
13684 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
13686 fputs_filtered (")", stream
);
13691 /* Table mapping opcodes into strings for printing operators
13692 and precedences of the operators. */
13694 static const struct op_print ada_op_print_tab
[] = {
13695 {":=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
13696 {"or else", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
13697 {"and then", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
13698 {"or", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
13699 {"xor", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
13700 {"and", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
13701 {"=", BINOP_EQUAL
, PREC_EQUAL
, 0},
13702 {"/=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
13703 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
13704 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
13705 {">", BINOP_GTR
, PREC_ORDER
, 0},
13706 {"<", BINOP_LESS
, PREC_ORDER
, 0},
13707 {">>", BINOP_RSH
, PREC_SHIFT
, 0},
13708 {"<<", BINOP_LSH
, PREC_SHIFT
, 0},
13709 {"+", BINOP_ADD
, PREC_ADD
, 0},
13710 {"-", BINOP_SUB
, PREC_ADD
, 0},
13711 {"&", BINOP_CONCAT
, PREC_ADD
, 0},
13712 {"*", BINOP_MUL
, PREC_MUL
, 0},
13713 {"/", BINOP_DIV
, PREC_MUL
, 0},
13714 {"rem", BINOP_REM
, PREC_MUL
, 0},
13715 {"mod", BINOP_MOD
, PREC_MUL
, 0},
13716 {"**", BINOP_EXP
, PREC_REPEAT
, 0},
13717 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
13718 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
13719 {"+", UNOP_PLUS
, PREC_PREFIX
, 0},
13720 {"not ", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
13721 {"not ", UNOP_COMPLEMENT
, PREC_PREFIX
, 0},
13722 {"abs ", UNOP_ABS
, PREC_PREFIX
, 0},
13723 {".all", UNOP_IND
, PREC_SUFFIX
, 1},
13724 {"'access", UNOP_ADDR
, PREC_SUFFIX
, 1},
13725 {"'size", OP_ATR_SIZE
, PREC_SUFFIX
, 1},
13726 {NULL
, OP_NULL
, PREC_SUFFIX
, 0}
13729 /* Language vector */
13731 static const struct exp_descriptor ada_exp_descriptor
= {
13733 ada_operator_length
,
13734 ada_operator_check
,
13735 ada_dump_subexp_body
,
13736 ada_evaluate_subexp
13739 /* symbol_name_matcher_ftype adapter for wild_match. */
13742 do_wild_match (const char *symbol_search_name
,
13743 const lookup_name_info
&lookup_name
,
13744 completion_match_result
*comp_match_res
)
13746 return wild_match (symbol_search_name
, ada_lookup_name (lookup_name
));
13749 /* symbol_name_matcher_ftype adapter for full_match. */
13752 do_full_match (const char *symbol_search_name
,
13753 const lookup_name_info
&lookup_name
,
13754 completion_match_result
*comp_match_res
)
13756 const char *lname
= lookup_name
.ada ().lookup_name ().c_str ();
13758 /* If both symbols start with "_ada_", just let the loop below
13759 handle the comparison. However, if only the symbol name starts
13760 with "_ada_", skip the prefix and let the match proceed as
13762 if (startswith (symbol_search_name
, "_ada_")
13763 && !startswith (lname
, "_ada"))
13764 symbol_search_name
+= 5;
13766 int uscore_count
= 0;
13767 while (*lname
!= '\0')
13769 if (*symbol_search_name
!= *lname
)
13771 if (*symbol_search_name
== 'B' && uscore_count
== 2
13772 && symbol_search_name
[1] == '_')
13774 symbol_search_name
+= 2;
13775 while (isdigit (*symbol_search_name
))
13776 ++symbol_search_name
;
13777 if (symbol_search_name
[0] == '_'
13778 && symbol_search_name
[1] == '_')
13780 symbol_search_name
+= 2;
13787 if (*symbol_search_name
== '_')
13792 ++symbol_search_name
;
13796 return is_name_suffix (symbol_search_name
);
13799 /* symbol_name_matcher_ftype for exact (verbatim) matches. */
13802 do_exact_match (const char *symbol_search_name
,
13803 const lookup_name_info
&lookup_name
,
13804 completion_match_result
*comp_match_res
)
13806 return strcmp (symbol_search_name
, ada_lookup_name (lookup_name
)) == 0;
13809 /* Build the Ada lookup name for LOOKUP_NAME. */
13811 ada_lookup_name_info::ada_lookup_name_info (const lookup_name_info
&lookup_name
)
13813 gdb::string_view user_name
= lookup_name
.name ();
13815 if (!user_name
.empty () && user_name
[0] == '<')
13817 if (user_name
.back () == '>')
13819 = gdb::to_string (user_name
.substr (1, user_name
.size () - 2));
13822 = gdb::to_string (user_name
.substr (1, user_name
.size () - 1));
13823 m_encoded_p
= true;
13824 m_verbatim_p
= true;
13825 m_wild_match_p
= false;
13826 m_standard_p
= false;
13830 m_verbatim_p
= false;
13832 m_encoded_p
= user_name
.find ("__") != gdb::string_view::npos
;
13836 const char *folded
= ada_fold_name (user_name
);
13837 m_encoded_name
= ada_encode_1 (folded
, false);
13838 if (m_encoded_name
.empty ())
13839 m_encoded_name
= gdb::to_string (user_name
);
13842 m_encoded_name
= gdb::to_string (user_name
);
13844 /* Handle the 'package Standard' special case. See description
13845 of m_standard_p. */
13846 if (startswith (m_encoded_name
.c_str (), "standard__"))
13848 m_encoded_name
= m_encoded_name
.substr (sizeof ("standard__") - 1);
13849 m_standard_p
= true;
13852 m_standard_p
= false;
13854 /* If the name contains a ".", then the user is entering a fully
13855 qualified entity name, and the match must not be done in wild
13856 mode. Similarly, if the user wants to complete what looks
13857 like an encoded name, the match must not be done in wild
13858 mode. Also, in the standard__ special case always do
13859 non-wild matching. */
13861 = (lookup_name
.match_type () != symbol_name_match_type::FULL
13864 && user_name
.find ('.') == std::string::npos
);
13868 /* symbol_name_matcher_ftype method for Ada. This only handles
13869 completion mode. */
13872 ada_symbol_name_matches (const char *symbol_search_name
,
13873 const lookup_name_info
&lookup_name
,
13874 completion_match_result
*comp_match_res
)
13876 return lookup_name
.ada ().matches (symbol_search_name
,
13877 lookup_name
.match_type (),
13881 /* A name matcher that matches the symbol name exactly, with
13885 literal_symbol_name_matcher (const char *symbol_search_name
,
13886 const lookup_name_info
&lookup_name
,
13887 completion_match_result
*comp_match_res
)
13889 gdb::string_view name_view
= lookup_name
.name ();
13891 if (lookup_name
.completion_mode ()
13892 ? (strncmp (symbol_search_name
, name_view
.data (),
13893 name_view
.size ()) == 0)
13894 : symbol_search_name
== name_view
)
13896 if (comp_match_res
!= NULL
)
13897 comp_match_res
->set_match (symbol_search_name
);
13904 /* Implement the "get_symbol_name_matcher" language_defn method for
13907 static symbol_name_matcher_ftype
*
13908 ada_get_symbol_name_matcher (const lookup_name_info
&lookup_name
)
13910 if (lookup_name
.match_type () == symbol_name_match_type::SEARCH_NAME
)
13911 return literal_symbol_name_matcher
;
13913 if (lookup_name
.completion_mode ())
13914 return ada_symbol_name_matches
;
13917 if (lookup_name
.ada ().wild_match_p ())
13918 return do_wild_match
;
13919 else if (lookup_name
.ada ().verbatim_p ())
13920 return do_exact_match
;
13922 return do_full_match
;
13926 /* Class representing the Ada language. */
13928 class ada_language
: public language_defn
13932 : language_defn (language_ada
)
13935 /* See language.h. */
13937 const char *name () const override
13940 /* See language.h. */
13942 const char *natural_name () const override
13945 /* See language.h. */
13947 const std::vector
<const char *> &filename_extensions () const override
13949 static const std::vector
<const char *> extensions
13950 = { ".adb", ".ads", ".a", ".ada", ".dg" };
13954 /* Print an array element index using the Ada syntax. */
13956 void print_array_index (struct type
*index_type
,
13958 struct ui_file
*stream
,
13959 const value_print_options
*options
) const override
13961 struct value
*index_value
= val_atr (index_type
, index
);
13963 value_print (index_value
, stream
, options
);
13964 fprintf_filtered (stream
, " => ");
13967 /* Implement the "read_var_value" language_defn method for Ada. */
13969 struct value
*read_var_value (struct symbol
*var
,
13970 const struct block
*var_block
,
13971 struct frame_info
*frame
) const override
13973 /* The only case where default_read_var_value is not sufficient
13974 is when VAR is a renaming... */
13975 if (frame
!= nullptr)
13977 const struct block
*frame_block
= get_frame_block (frame
, NULL
);
13978 if (frame_block
!= nullptr && ada_is_renaming_symbol (var
))
13979 return ada_read_renaming_var_value (var
, frame_block
);
13982 /* This is a typical case where we expect the default_read_var_value
13983 function to work. */
13984 return language_defn::read_var_value (var
, var_block
, frame
);
13987 /* See language.h. */
13988 void language_arch_info (struct gdbarch
*gdbarch
,
13989 struct language_arch_info
*lai
) const override
13991 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
13993 /* Helper function to allow shorter lines below. */
13994 auto add
= [&] (struct type
*t
)
13996 lai
->add_primitive_type (t
);
13999 add (arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
14001 add (arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
14002 0, "long_integer"));
14003 add (arch_integer_type (gdbarch
, gdbarch_short_bit (gdbarch
),
14004 0, "short_integer"));
14005 struct type
*char_type
= arch_character_type (gdbarch
, TARGET_CHAR_BIT
,
14007 lai
->set_string_char_type (char_type
);
14009 add (arch_float_type (gdbarch
, gdbarch_float_bit (gdbarch
),
14010 "float", gdbarch_float_format (gdbarch
)));
14011 add (arch_float_type (gdbarch
, gdbarch_double_bit (gdbarch
),
14012 "long_float", gdbarch_double_format (gdbarch
)));
14013 add (arch_integer_type (gdbarch
, gdbarch_long_long_bit (gdbarch
),
14014 0, "long_long_integer"));
14015 add (arch_float_type (gdbarch
, gdbarch_long_double_bit (gdbarch
),
14017 gdbarch_long_double_format (gdbarch
)));
14018 add (arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
14020 add (arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
14022 add (builtin
->builtin_void
);
14024 struct type
*system_addr_ptr
14025 = lookup_pointer_type (arch_type (gdbarch
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
,
14027 system_addr_ptr
->set_name ("system__address");
14028 add (system_addr_ptr
);
14030 /* Create the equivalent of the System.Storage_Elements.Storage_Offset
14031 type. This is a signed integral type whose size is the same as
14032 the size of addresses. */
14033 unsigned int addr_length
= TYPE_LENGTH (system_addr_ptr
);
14034 add (arch_integer_type (gdbarch
, addr_length
* HOST_CHAR_BIT
, 0,
14035 "storage_offset"));
14037 lai
->set_bool_type (builtin
->builtin_bool
);
14040 /* See language.h. */
14042 bool iterate_over_symbols
14043 (const struct block
*block
, const lookup_name_info
&name
,
14044 domain_enum domain
,
14045 gdb::function_view
<symbol_found_callback_ftype
> callback
) const override
14047 std::vector
<struct block_symbol
> results
14048 = ada_lookup_symbol_list_worker (name
, block
, domain
, 0);
14049 for (block_symbol
&sym
: results
)
14051 if (!callback (&sym
))
14058 /* See language.h. */
14059 bool sniff_from_mangled_name (const char *mangled
,
14060 char **out
) const override
14062 std::string demangled
= ada_decode (mangled
);
14066 if (demangled
!= mangled
&& demangled
[0] != '<')
14068 /* Set the gsymbol language to Ada, but still return 0.
14069 Two reasons for that:
14071 1. For Ada, we prefer computing the symbol's decoded name
14072 on the fly rather than pre-compute it, in order to save
14073 memory (Ada projects are typically very large).
14075 2. There are some areas in the definition of the GNAT
14076 encoding where, with a bit of bad luck, we might be able
14077 to decode a non-Ada symbol, generating an incorrect
14078 demangled name (Eg: names ending with "TB" for instance
14079 are identified as task bodies and so stripped from
14080 the decoded name returned).
14082 Returning true, here, but not setting *DEMANGLED, helps us get
14083 a little bit of the best of both worlds. Because we're last,
14084 we should not affect any of the other languages that were
14085 able to demangle the symbol before us; we get to correctly
14086 tag Ada symbols as such; and even if we incorrectly tagged a
14087 non-Ada symbol, which should be rare, any routing through the
14088 Ada language should be transparent (Ada tries to behave much
14089 like C/C++ with non-Ada symbols). */
14096 /* See language.h. */
14098 char *demangle_symbol (const char *mangled
, int options
) const override
14100 return ada_la_decode (mangled
, options
);
14103 /* See language.h. */
14105 void print_type (struct type
*type
, const char *varstring
,
14106 struct ui_file
*stream
, int show
, int level
,
14107 const struct type_print_options
*flags
) const override
14109 ada_print_type (type
, varstring
, stream
, show
, level
, flags
);
14112 /* See language.h. */
14114 const char *word_break_characters (void) const override
14116 return ada_completer_word_break_characters
;
14119 /* See language.h. */
14121 void collect_symbol_completion_matches (completion_tracker
&tracker
,
14122 complete_symbol_mode mode
,
14123 symbol_name_match_type name_match_type
,
14124 const char *text
, const char *word
,
14125 enum type_code code
) const override
14127 struct symbol
*sym
;
14128 const struct block
*b
, *surrounding_static_block
= 0;
14129 struct block_iterator iter
;
14131 gdb_assert (code
== TYPE_CODE_UNDEF
);
14133 lookup_name_info
lookup_name (text
, name_match_type
, true);
14135 /* First, look at the partial symtab symbols. */
14136 expand_symtabs_matching (NULL
,
14142 /* At this point scan through the misc symbol vectors and add each
14143 symbol you find to the list. Eventually we want to ignore
14144 anything that isn't a text symbol (everything else will be
14145 handled by the psymtab code above). */
14147 for (objfile
*objfile
: current_program_space
->objfiles ())
14149 for (minimal_symbol
*msymbol
: objfile
->msymbols ())
14153 if (completion_skip_symbol (mode
, msymbol
))
14156 language symbol_language
= msymbol
->language ();
14158 /* Ada minimal symbols won't have their language set to Ada. If
14159 we let completion_list_add_name compare using the
14160 default/C-like matcher, then when completing e.g., symbols in a
14161 package named "pck", we'd match internal Ada symbols like
14162 "pckS", which are invalid in an Ada expression, unless you wrap
14163 them in '<' '>' to request a verbatim match.
14165 Unfortunately, some Ada encoded names successfully demangle as
14166 C++ symbols (using an old mangling scheme), such as "name__2Xn"
14167 -> "Xn::name(void)" and thus some Ada minimal symbols end up
14168 with the wrong language set. Paper over that issue here. */
14169 if (symbol_language
== language_auto
14170 || symbol_language
== language_cplus
)
14171 symbol_language
= language_ada
;
14173 completion_list_add_name (tracker
,
14175 msymbol
->linkage_name (),
14176 lookup_name
, text
, word
);
14180 /* Search upwards from currently selected frame (so that we can
14181 complete on local vars. */
14183 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
14185 if (!BLOCK_SUPERBLOCK (b
))
14186 surrounding_static_block
= b
; /* For elmin of dups */
14188 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
14190 if (completion_skip_symbol (mode
, sym
))
14193 completion_list_add_name (tracker
,
14195 sym
->linkage_name (),
14196 lookup_name
, text
, word
);
14200 /* Go through the symtabs and check the externs and statics for
14201 symbols which match. */
14203 for (objfile
*objfile
: current_program_space
->objfiles ())
14205 for (compunit_symtab
*s
: objfile
->compunits ())
14208 b
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (s
), GLOBAL_BLOCK
);
14209 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
14211 if (completion_skip_symbol (mode
, sym
))
14214 completion_list_add_name (tracker
,
14216 sym
->linkage_name (),
14217 lookup_name
, text
, word
);
14222 for (objfile
*objfile
: current_program_space
->objfiles ())
14224 for (compunit_symtab
*s
: objfile
->compunits ())
14227 b
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (s
), STATIC_BLOCK
);
14228 /* Don't do this block twice. */
14229 if (b
== surrounding_static_block
)
14231 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
14233 if (completion_skip_symbol (mode
, sym
))
14236 completion_list_add_name (tracker
,
14238 sym
->linkage_name (),
14239 lookup_name
, text
, word
);
14245 /* See language.h. */
14247 gdb::unique_xmalloc_ptr
<char> watch_location_expression
14248 (struct type
*type
, CORE_ADDR addr
) const override
14250 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
14251 std::string name
= type_to_string (type
);
14252 return gdb::unique_xmalloc_ptr
<char>
14253 (xstrprintf ("{%s} %s", name
.c_str (), core_addr_to_string (addr
)));
14256 /* See language.h. */
14258 void value_print (struct value
*val
, struct ui_file
*stream
,
14259 const struct value_print_options
*options
) const override
14261 return ada_value_print (val
, stream
, options
);
14264 /* See language.h. */
14266 void value_print_inner
14267 (struct value
*val
, struct ui_file
*stream
, int recurse
,
14268 const struct value_print_options
*options
) const override
14270 return ada_value_print_inner (val
, stream
, recurse
, options
);
14273 /* See language.h. */
14275 struct block_symbol lookup_symbol_nonlocal
14276 (const char *name
, const struct block
*block
,
14277 const domain_enum domain
) const override
14279 struct block_symbol sym
;
14281 sym
= ada_lookup_symbol (name
, block_static_block (block
), domain
);
14282 if (sym
.symbol
!= NULL
)
14285 /* If we haven't found a match at this point, try the primitive
14286 types. In other languages, this search is performed before
14287 searching for global symbols in order to short-circuit that
14288 global-symbol search if it happens that the name corresponds
14289 to a primitive type. But we cannot do the same in Ada, because
14290 it is perfectly legitimate for a program to declare a type which
14291 has the same name as a standard type. If looking up a type in
14292 that situation, we have traditionally ignored the primitive type
14293 in favor of user-defined types. This is why, unlike most other
14294 languages, we search the primitive types this late and only after
14295 having searched the global symbols without success. */
14297 if (domain
== VAR_DOMAIN
)
14299 struct gdbarch
*gdbarch
;
14302 gdbarch
= target_gdbarch ();
14304 gdbarch
= block_gdbarch (block
);
14306 = language_lookup_primitive_type_as_symbol (this, gdbarch
, name
);
14307 if (sym
.symbol
!= NULL
)
14314 /* See language.h. */
14316 int parser (struct parser_state
*ps
) const override
14318 warnings_issued
= 0;
14319 return ada_parse (ps
);
14324 Same as evaluate_type (*EXP), but resolves ambiguous symbol references
14325 (marked by OP_VAR_VALUE nodes in which the symbol has an undefined
14326 namespace) and converts operators that are user-defined into
14327 appropriate function calls. If CONTEXT_TYPE is non-null, it provides
14328 a preferred result type [at the moment, only type void has any
14329 effect---causing procedures to be preferred over functions in calls].
14330 A null CONTEXT_TYPE indicates that a non-void return type is
14331 preferred. May change (expand) *EXP. */
14333 void post_parser (expression_up
*expp
, struct parser_state
*ps
)
14336 struct type
*context_type
= NULL
;
14339 if (ps
->void_context_p
)
14340 context_type
= builtin_type ((*expp
)->gdbarch
)->builtin_void
;
14342 resolve_subexp (expp
, &pc
, 1, context_type
, ps
->parse_completion
,
14343 ps
->block_tracker
);
14346 /* See language.h. */
14348 void emitchar (int ch
, struct type
*chtype
,
14349 struct ui_file
*stream
, int quoter
) const override
14351 ada_emit_char (ch
, chtype
, stream
, quoter
, 1);
14354 /* See language.h. */
14356 void printchar (int ch
, struct type
*chtype
,
14357 struct ui_file
*stream
) const override
14359 ada_printchar (ch
, chtype
, stream
);
14362 /* See language.h. */
14364 void printstr (struct ui_file
*stream
, struct type
*elttype
,
14365 const gdb_byte
*string
, unsigned int length
,
14366 const char *encoding
, int force_ellipses
,
14367 const struct value_print_options
*options
) const override
14369 ada_printstr (stream
, elttype
, string
, length
, encoding
,
14370 force_ellipses
, options
);
14373 /* See language.h. */
14375 void print_typedef (struct type
*type
, struct symbol
*new_symbol
,
14376 struct ui_file
*stream
) const override
14378 ada_print_typedef (type
, new_symbol
, stream
);
14381 /* See language.h. */
14383 bool is_string_type_p (struct type
*type
) const override
14385 return ada_is_string_type (type
);
14388 /* See language.h. */
14390 const char *struct_too_deep_ellipsis () const override
14391 { return "(...)"; }
14393 /* See language.h. */
14395 bool c_style_arrays_p () const override
14398 /* See language.h. */
14400 bool store_sym_names_in_linkage_form_p () const override
14403 /* See language.h. */
14405 const struct lang_varobj_ops
*varobj_ops () const override
14406 { return &ada_varobj_ops
; }
14408 /* See language.h. */
14410 const struct exp_descriptor
*expression_ops () const override
14411 { return &ada_exp_descriptor
; }
14413 /* See language.h. */
14415 const struct op_print
*opcode_print_table () const override
14416 { return ada_op_print_tab
; }
14419 /* See language.h. */
14421 symbol_name_matcher_ftype
*get_symbol_name_matcher_inner
14422 (const lookup_name_info
&lookup_name
) const override
14424 return ada_get_symbol_name_matcher (lookup_name
);
14428 /* Single instance of the Ada language class. */
14430 static ada_language ada_language_defn
;
14432 /* Command-list for the "set/show ada" prefix command. */
14433 static struct cmd_list_element
*set_ada_list
;
14434 static struct cmd_list_element
*show_ada_list
;
14437 initialize_ada_catchpoint_ops (void)
14439 struct breakpoint_ops
*ops
;
14441 initialize_breakpoint_ops ();
14443 ops
= &catch_exception_breakpoint_ops
;
14444 *ops
= bkpt_breakpoint_ops
;
14445 ops
->allocate_location
= allocate_location_exception
;
14446 ops
->re_set
= re_set_exception
;
14447 ops
->check_status
= check_status_exception
;
14448 ops
->print_it
= print_it_exception
;
14449 ops
->print_one
= print_one_exception
;
14450 ops
->print_mention
= print_mention_exception
;
14451 ops
->print_recreate
= print_recreate_exception
;
14453 ops
= &catch_exception_unhandled_breakpoint_ops
;
14454 *ops
= bkpt_breakpoint_ops
;
14455 ops
->allocate_location
= allocate_location_exception
;
14456 ops
->re_set
= re_set_exception
;
14457 ops
->check_status
= check_status_exception
;
14458 ops
->print_it
= print_it_exception
;
14459 ops
->print_one
= print_one_exception
;
14460 ops
->print_mention
= print_mention_exception
;
14461 ops
->print_recreate
= print_recreate_exception
;
14463 ops
= &catch_assert_breakpoint_ops
;
14464 *ops
= bkpt_breakpoint_ops
;
14465 ops
->allocate_location
= allocate_location_exception
;
14466 ops
->re_set
= re_set_exception
;
14467 ops
->check_status
= check_status_exception
;
14468 ops
->print_it
= print_it_exception
;
14469 ops
->print_one
= print_one_exception
;
14470 ops
->print_mention
= print_mention_exception
;
14471 ops
->print_recreate
= print_recreate_exception
;
14473 ops
= &catch_handlers_breakpoint_ops
;
14474 *ops
= bkpt_breakpoint_ops
;
14475 ops
->allocate_location
= allocate_location_exception
;
14476 ops
->re_set
= re_set_exception
;
14477 ops
->check_status
= check_status_exception
;
14478 ops
->print_it
= print_it_exception
;
14479 ops
->print_one
= print_one_exception
;
14480 ops
->print_mention
= print_mention_exception
;
14481 ops
->print_recreate
= print_recreate_exception
;
14484 /* This module's 'new_objfile' observer. */
14487 ada_new_objfile_observer (struct objfile
*objfile
)
14489 ada_clear_symbol_cache ();
14492 /* This module's 'free_objfile' observer. */
14495 ada_free_objfile_observer (struct objfile
*objfile
)
14497 ada_clear_symbol_cache ();
14500 void _initialize_ada_language ();
14502 _initialize_ada_language ()
14504 initialize_ada_catchpoint_ops ();
14506 add_basic_prefix_cmd ("ada", no_class
,
14507 _("Prefix command for changing Ada-specific settings."),
14508 &set_ada_list
, "set ada ", 0, &setlist
);
14510 add_show_prefix_cmd ("ada", no_class
,
14511 _("Generic command for showing Ada-specific settings."),
14512 &show_ada_list
, "show ada ", 0, &showlist
);
14514 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure
,
14515 &trust_pad_over_xvs
, _("\
14516 Enable or disable an optimization trusting PAD types over XVS types."), _("\
14517 Show whether an optimization trusting PAD types over XVS types is activated."),
14519 This is related to the encoding used by the GNAT compiler. The debugger\n\
14520 should normally trust the contents of PAD types, but certain older versions\n\
14521 of GNAT have a bug that sometimes causes the information in the PAD type\n\
14522 to be incorrect. Turning this setting \"off\" allows the debugger to\n\
14523 work around this bug. It is always safe to turn this option \"off\", but\n\
14524 this incurs a slight performance penalty, so it is recommended to NOT change\n\
14525 this option to \"off\" unless necessary."),
14526 NULL
, NULL
, &set_ada_list
, &show_ada_list
);
14528 add_setshow_boolean_cmd ("print-signatures", class_vars
,
14529 &print_signatures
, _("\
14530 Enable or disable the output of formal and return types for functions in the \
14531 overloads selection menu."), _("\
14532 Show whether the output of formal and return types for functions in the \
14533 overloads selection menu is activated."),
14534 NULL
, NULL
, NULL
, &set_ada_list
, &show_ada_list
);
14536 add_catch_command ("exception", _("\
14537 Catch Ada exceptions, when raised.\n\
14538 Usage: catch exception [ARG] [if CONDITION]\n\
14539 Without any argument, stop when any Ada exception is raised.\n\
14540 If ARG is \"unhandled\" (without the quotes), only stop when the exception\n\
14541 being raised does not have a handler (and will therefore lead to the task's\n\
14543 Otherwise, the catchpoint only stops when the name of the exception being\n\
14544 raised is the same as ARG.\n\
14545 CONDITION is a boolean expression that is evaluated to see whether the\n\
14546 exception should cause a stop."),
14547 catch_ada_exception_command
,
14548 catch_ada_completer
,
14552 add_catch_command ("handlers", _("\
14553 Catch Ada exceptions, when handled.\n\
14554 Usage: catch handlers [ARG] [if CONDITION]\n\
14555 Without any argument, stop when any Ada exception is handled.\n\
14556 With an argument, catch only exceptions with the given name.\n\
14557 CONDITION is a boolean expression that is evaluated to see whether the\n\
14558 exception should cause a stop."),
14559 catch_ada_handlers_command
,
14560 catch_ada_completer
,
14563 add_catch_command ("assert", _("\
14564 Catch failed Ada assertions, when raised.\n\
14565 Usage: catch assert [if CONDITION]\n\
14566 CONDITION is a boolean expression that is evaluated to see whether the\n\
14567 exception should cause a stop."),
14568 catch_assert_command
,
14573 varsize_limit
= 65536;
14574 add_setshow_uinteger_cmd ("varsize-limit", class_support
,
14575 &varsize_limit
, _("\
14576 Set the maximum number of bytes allowed in a variable-size object."), _("\
14577 Show the maximum number of bytes allowed in a variable-size object."), _("\
14578 Attempts to access an object whose size is not a compile-time constant\n\
14579 and exceeds this limit will cause an error."),
14580 NULL
, NULL
, &setlist
, &showlist
);
14582 add_info ("exceptions", info_exceptions_command
,
14584 List all Ada exception names.\n\
14585 Usage: info exceptions [REGEXP]\n\
14586 If a regular expression is passed as an argument, only those matching\n\
14587 the regular expression are listed."));
14589 add_basic_prefix_cmd ("ada", class_maintenance
,
14590 _("Set Ada maintenance-related variables."),
14591 &maint_set_ada_cmdlist
, "maintenance set ada ",
14592 0/*allow-unknown*/, &maintenance_set_cmdlist
);
14594 add_show_prefix_cmd ("ada", class_maintenance
,
14595 _("Show Ada maintenance-related variables."),
14596 &maint_show_ada_cmdlist
, "maintenance show ada ",
14597 0/*allow-unknown*/, &maintenance_show_cmdlist
);
14599 add_setshow_boolean_cmd
14600 ("ignore-descriptive-types", class_maintenance
,
14601 &ada_ignore_descriptive_types_p
,
14602 _("Set whether descriptive types generated by GNAT should be ignored."),
14603 _("Show whether descriptive types generated by GNAT should be ignored."),
14605 When enabled, the debugger will stop using the DW_AT_GNAT_descriptive_type\n\
14606 DWARF attribute."),
14607 NULL
, NULL
, &maint_set_ada_cmdlist
, &maint_show_ada_cmdlist
);
14609 decoded_names_store
= htab_create_alloc (256, htab_hash_string
, streq_hash
,
14610 NULL
, xcalloc
, xfree
);
14612 /* The ada-lang observers. */
14613 gdb::observers::new_objfile
.attach (ada_new_objfile_observer
);
14614 gdb::observers::free_objfile
.attach (ada_free_objfile_observer
);
14615 gdb::observers::inferior_exit
.attach (ada_inferior_exit
);