1 /* Ada language support routines for GDB, the GNU debugger. Copyright (C)
3 1992, 1993, 1994, 1997, 1998, 1999, 2000, 2003, 2004, 2005, 2007, 2008,
4 2009 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdb_string.h"
28 #include "gdb_regex.h"
33 #include "expression.h"
34 #include "parser-defs.h"
40 #include "breakpoint.h"
43 #include "gdb_obstack.h"
45 #include "completer.h"
52 #include "dictionary.h"
53 #include "exceptions.h"
63 /* Define whether or not the C operator '/' truncates towards zero for
64 differently signed operands (truncation direction is undefined in C).
65 Copied from valarith.c. */
67 #ifndef TRUNCATION_TOWARDS_ZERO
68 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
71 static void modify_general_field (struct type *, char *, LONGEST, int, int);
73 static struct type *desc_base_type (struct type *);
75 static struct type *desc_bounds_type (struct type *);
77 static struct value *desc_bounds (struct value *);
79 static int fat_pntr_bounds_bitpos (struct type *);
81 static int fat_pntr_bounds_bitsize (struct type *);
83 static struct type *desc_data_target_type (struct type *);
85 static struct value *desc_data (struct value *);
87 static int fat_pntr_data_bitpos (struct type *);
89 static int fat_pntr_data_bitsize (struct type *);
91 static struct value *desc_one_bound (struct value *, int, int);
93 static int desc_bound_bitpos (struct type *, int, int);
95 static int desc_bound_bitsize (struct type *, int, int);
97 static struct type *desc_index_type (struct type *, int);
99 static int desc_arity (struct type *);
101 static int ada_type_match (struct type *, struct type *, int);
103 static int ada_args_match (struct symbol *, struct value **, int);
105 static struct value *ensure_lval (struct value *,
106 struct gdbarch *, CORE_ADDR *);
108 static struct value *make_array_descriptor (struct type *, struct value *,
109 struct gdbarch *, CORE_ADDR *);
111 static void ada_add_block_symbols (struct obstack *,
112 struct block *, const char *,
113 domain_enum, struct objfile *, int);
115 static int is_nonfunction (struct ada_symbol_info *, int);
117 static void add_defn_to_vec (struct obstack *, struct symbol *,
120 static int num_defns_collected (struct obstack *);
122 static struct ada_symbol_info *defns_collected (struct obstack *, int);
124 static struct value *resolve_subexp (struct expression **, int *, int,
127 static void replace_operator_with_call (struct expression **, int, int, int,
128 struct symbol *, struct block *);
130 static int possible_user_operator_p (enum exp_opcode, struct value **);
132 static char *ada_op_name (enum exp_opcode);
134 static const char *ada_decoded_op_name (enum exp_opcode);
136 static int numeric_type_p (struct type *);
138 static int integer_type_p (struct type *);
140 static int scalar_type_p (struct type *);
142 static int discrete_type_p (struct type *);
144 static enum ada_renaming_category parse_old_style_renaming (struct type *,
149 static struct symbol *find_old_style_renaming_symbol (const char *,
152 static struct type *ada_lookup_struct_elt_type (struct type *, char *,
155 static struct value *evaluate_subexp_type (struct expression *, int *);
157 static struct type *ada_find_parallel_type_with_name (struct type *,
160 static int is_dynamic_field (struct type *, int);
162 static struct type *to_fixed_variant_branch_type (struct type *,
164 CORE_ADDR, struct value *);
166 static struct type *to_fixed_array_type (struct type *, struct value *, int);
168 static struct type *to_fixed_range_type (struct type *, struct value *);
170 static struct type *to_static_fixed_type (struct type *);
171 static struct type *static_unwrap_type (struct type *type);
173 static struct value *unwrap_value (struct value *);
175 static struct type *constrained_packed_array_type (struct type *, long *);
177 static struct type *decode_constrained_packed_array_type (struct type *);
179 static long decode_packed_array_bitsize (struct type *);
181 static struct value *decode_constrained_packed_array (struct value *);
183 static int ada_is_packed_array_type (struct type *);
185 static int ada_is_unconstrained_packed_array_type (struct type *);
187 static struct value *value_subscript_packed (struct value *, int,
190 static void move_bits (gdb_byte *, int, const gdb_byte *, int, int, int);
192 static struct value *coerce_unspec_val_to_type (struct value *,
195 static struct value *get_var_value (char *, char *);
197 static int lesseq_defined_than (struct symbol *, struct symbol *);
199 static int equiv_types (struct type *, struct type *);
201 static int is_name_suffix (const char *);
203 static int advance_wild_match (const char **, const char *, int);
205 static int wild_match (const char *, const char *);
207 static struct value *ada_coerce_ref (struct value *);
209 static LONGEST pos_atr (struct value *);
211 static struct value *value_pos_atr (struct type *, struct value *);
213 static struct value *value_val_atr (struct type *, struct value *);
215 static struct symbol *standard_lookup (const char *, const struct block *,
218 static struct value *ada_search_struct_field (char *, struct value *, int,
221 static struct value *ada_value_primitive_field (struct value *, int, int,
224 static int find_struct_field (char *, struct type *, int,
225 struct type **, int *, int *, int *, int *);
227 static struct value *ada_to_fixed_value_create (struct type *, CORE_ADDR,
230 static int ada_resolve_function (struct ada_symbol_info *, int,
231 struct value **, int, const char *,
234 static struct value *ada_coerce_to_simple_array (struct value *);
236 static int ada_is_direct_array_type (struct type *);
238 static void ada_language_arch_info (struct gdbarch *,
239 struct language_arch_info *);
241 static void check_size (const struct type *);
243 static struct value *ada_index_struct_field (int, struct value *, int,
246 static struct value *assign_aggregate (struct value *, struct value *,
247 struct expression *, int *, enum noside);
249 static void aggregate_assign_from_choices (struct value *, struct value *,
251 int *, LONGEST *, int *,
252 int, LONGEST, LONGEST);
254 static void aggregate_assign_positional (struct value *, struct value *,
256 int *, LONGEST *, int *, int,
260 static void aggregate_assign_others (struct value *, struct value *,
262 int *, LONGEST *, int, LONGEST, LONGEST);
265 static void add_component_interval (LONGEST, LONGEST, LONGEST *, int *, int);
268 static struct value *ada_evaluate_subexp (struct type *, struct expression *,
271 static void ada_forward_operator_length (struct expression *, int, int *,
276 /* Maximum-sized dynamic type. */
277 static unsigned int varsize_limit;
279 /* FIXME: brobecker/2003-09-17: No longer a const because it is
280 returned by a function that does not return a const char *. */
281 static char *ada_completer_word_break_characters =
283 " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
285 " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
288 /* The name of the symbol to use to get the name of the main subprogram. */
289 static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
290 = "__gnat_ada_main_program_name";
292 /* Limit on the number of warnings to raise per expression evaluation. */
293 static int warning_limit = 2;
295 /* Number of warning messages issued; reset to 0 by cleanups after
296 expression evaluation. */
297 static int warnings_issued = 0;
299 static const char *known_runtime_file_name_patterns[] = {
300 ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
303 static const char *known_auxiliary_function_name_patterns[] = {
304 ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
307 /* Space for allocating results of ada_lookup_symbol_list. */
308 static struct obstack symbol_list_obstack;
310 /* Inferior-specific data. */
312 /* Per-inferior data for this module. */
314 struct ada_inferior_data
316 /* The ada__tags__type_specific_data type, which is used when decoding
317 tagged types. With older versions of GNAT, this type was directly
318 accessible through a component ("tsd") in the object tag. But this
319 is no longer the case, so we cache it for each inferior. */
320 struct type *tsd_type;
323 /* Our key to this module's inferior data. */
324 static const struct inferior_data *ada_inferior_data;
326 /* A cleanup routine for our inferior data. */
328 ada_inferior_data_cleanup (struct inferior *inf, void *arg)
330 struct ada_inferior_data *data;
332 data = inferior_data (inf, ada_inferior_data);
337 /* Return our inferior data for the given inferior (INF).
339 This function always returns a valid pointer to an allocated
340 ada_inferior_data structure. If INF's inferior data has not
341 been previously set, this functions creates a new one with all
342 fields set to zero, sets INF's inferior to it, and then returns
343 a pointer to that newly allocated ada_inferior_data. */
345 static struct ada_inferior_data *
346 get_ada_inferior_data (struct inferior *inf)
348 struct ada_inferior_data *data;
350 data = inferior_data (inf, ada_inferior_data);
353 data = XZALLOC (struct ada_inferior_data);
354 set_inferior_data (inf, ada_inferior_data, data);
360 /* Perform all necessary cleanups regarding our module's inferior data
361 that is required after the inferior INF just exited. */
364 ada_inferior_exit (struct inferior *inf)
366 ada_inferior_data_cleanup (inf, NULL);
367 set_inferior_data (inf, ada_inferior_data, NULL);
372 /* Given DECODED_NAME a string holding a symbol name in its
373 decoded form (ie using the Ada dotted notation), returns
374 its unqualified name. */
377 ada_unqualified_name (const char *decoded_name)
379 const char *result = strrchr (decoded_name, '.');
382 result++; /* Skip the dot... */
384 result = decoded_name;
389 /* Return a string starting with '<', followed by STR, and '>'.
390 The result is good until the next call. */
393 add_angle_brackets (const char *str)
395 static char *result = NULL;
398 result = xstrprintf ("<%s>", str);
403 ada_get_gdb_completer_word_break_characters (void)
405 return ada_completer_word_break_characters;
408 /* Print an array element index using the Ada syntax. */
411 ada_print_array_index (struct value *index_value, struct ui_file *stream,
412 const struct value_print_options *options)
414 LA_VALUE_PRINT (index_value, stream, options);
415 fprintf_filtered (stream, " => ");
418 /* Assuming VECT points to an array of *SIZE objects of size
419 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
420 updating *SIZE as necessary and returning the (new) array. */
423 grow_vect (void *vect, size_t *size, size_t min_size, int element_size)
425 if (*size < min_size)
428 if (*size < min_size)
430 vect = xrealloc (vect, *size * element_size);
435 /* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
436 suffix of FIELD_NAME beginning "___". */
439 field_name_match (const char *field_name, const char *target)
441 int len = strlen (target);
444 (strncmp (field_name, target, len) == 0
445 && (field_name[len] == '\0'
446 || (strncmp (field_name + len, "___", 3) == 0
447 && strcmp (field_name + strlen (field_name) - 6,
452 /* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
453 a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
454 and return its index. This function also handles fields whose name
455 have ___ suffixes because the compiler sometimes alters their name
456 by adding such a suffix to represent fields with certain constraints.
457 If the field could not be found, return a negative number if
458 MAYBE_MISSING is set. Otherwise raise an error. */
461 ada_get_field_index (const struct type *type, const char *field_name,
465 struct type *struct_type = check_typedef ((struct type *) type);
467 for (fieldno = 0; fieldno < TYPE_NFIELDS (struct_type); fieldno++)
468 if (field_name_match (TYPE_FIELD_NAME (struct_type, fieldno), field_name))
472 error (_("Unable to find field %s in struct %s. Aborting"),
473 field_name, TYPE_NAME (struct_type));
478 /* The length of the prefix of NAME prior to any "___" suffix. */
481 ada_name_prefix_len (const char *name)
487 const char *p = strstr (name, "___");
490 return strlen (name);
496 /* Return non-zero if SUFFIX is a suffix of STR.
497 Return zero if STR is null. */
500 is_suffix (const char *str, const char *suffix)
507 len2 = strlen (suffix);
508 return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
511 /* The contents of value VAL, treated as a value of type TYPE. The
512 result is an lval in memory if VAL is. */
514 static struct value *
515 coerce_unspec_val_to_type (struct value *val, struct type *type)
517 type = ada_check_typedef (type);
518 if (value_type (val) == type)
522 struct value *result;
524 /* Make sure that the object size is not unreasonable before
525 trying to allocate some memory for it. */
528 result = allocate_value (type);
529 set_value_component_location (result, val);
530 set_value_bitsize (result, value_bitsize (val));
531 set_value_bitpos (result, value_bitpos (val));
532 set_value_address (result, value_address (val));
534 || TYPE_LENGTH (type) > TYPE_LENGTH (value_type (val)))
535 set_value_lazy (result, 1);
537 memcpy (value_contents_raw (result), value_contents (val),
543 static const gdb_byte *
544 cond_offset_host (const gdb_byte *valaddr, long offset)
549 return valaddr + offset;
553 cond_offset_target (CORE_ADDR address, long offset)
558 return address + offset;
561 /* Issue a warning (as for the definition of warning in utils.c, but
562 with exactly one argument rather than ...), unless the limit on the
563 number of warnings has passed during the evaluation of the current
566 /* FIXME: cagney/2004-10-10: This function is mimicking the behavior
567 provided by "complaint". */
568 static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
571 lim_warning (const char *format, ...)
575 va_start (args, format);
576 warnings_issued += 1;
577 if (warnings_issued <= warning_limit)
578 vwarning (format, args);
583 /* Issue an error if the size of an object of type T is unreasonable,
584 i.e. if it would be a bad idea to allocate a value of this type in
588 check_size (const struct type *type)
590 if (TYPE_LENGTH (type) > varsize_limit)
591 error (_("object size is larger than varsize-limit"));
594 /* Maximum value of a SIZE-byte signed integer type. */
596 max_of_size (int size)
598 LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
600 return top_bit | (top_bit - 1);
603 /* Minimum value of a SIZE-byte signed integer type. */
605 min_of_size (int size)
607 return -max_of_size (size) - 1;
610 /* Maximum value of a SIZE-byte unsigned integer type. */
612 umax_of_size (int size)
614 ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
616 return top_bit | (top_bit - 1);
619 /* Maximum value of integral type T, as a signed quantity. */
621 max_of_type (struct type *t)
623 if (TYPE_UNSIGNED (t))
624 return (LONGEST) umax_of_size (TYPE_LENGTH (t));
626 return max_of_size (TYPE_LENGTH (t));
629 /* Minimum value of integral type T, as a signed quantity. */
631 min_of_type (struct type *t)
633 if (TYPE_UNSIGNED (t))
636 return min_of_size (TYPE_LENGTH (t));
639 /* The largest value in the domain of TYPE, a discrete type, as an integer. */
641 ada_discrete_type_high_bound (struct type *type)
643 switch (TYPE_CODE (type))
645 case TYPE_CODE_RANGE:
646 return TYPE_HIGH_BOUND (type);
648 return TYPE_FIELD_BITPOS (type, TYPE_NFIELDS (type) - 1);
653 return max_of_type (type);
655 error (_("Unexpected type in ada_discrete_type_high_bound."));
659 /* The largest value in the domain of TYPE, a discrete type, as an integer. */
661 ada_discrete_type_low_bound (struct type *type)
663 switch (TYPE_CODE (type))
665 case TYPE_CODE_RANGE:
666 return TYPE_LOW_BOUND (type);
668 return TYPE_FIELD_BITPOS (type, 0);
673 return min_of_type (type);
675 error (_("Unexpected type in ada_discrete_type_low_bound."));
679 /* The identity on non-range types. For range types, the underlying
680 non-range scalar type. */
683 base_type (struct type *type)
685 while (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE)
687 if (type == TYPE_TARGET_TYPE (type) || TYPE_TARGET_TYPE (type) == NULL)
689 type = TYPE_TARGET_TYPE (type);
695 /* Language Selection */
697 /* If the main program is in Ada, return language_ada, otherwise return LANG
698 (the main program is in Ada iif the adainit symbol is found). */
701 ada_update_initial_language (enum language lang)
703 if (lookup_minimal_symbol ("adainit", (const char *) NULL,
704 (struct objfile *) NULL) != NULL)
710 /* If the main procedure is written in Ada, then return its name.
711 The result is good until the next call. Return NULL if the main
712 procedure doesn't appear to be in Ada. */
717 struct minimal_symbol *msym;
718 static char *main_program_name = NULL;
720 /* For Ada, the name of the main procedure is stored in a specific
721 string constant, generated by the binder. Look for that symbol,
722 extract its address, and then read that string. If we didn't find
723 that string, then most probably the main procedure is not written
725 msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
729 CORE_ADDR main_program_name_addr;
732 main_program_name_addr = SYMBOL_VALUE_ADDRESS (msym);
733 if (main_program_name_addr == 0)
734 error (_("Invalid address for Ada main program name."));
736 xfree (main_program_name);
737 target_read_string (main_program_name_addr, &main_program_name,
742 return main_program_name;
745 /* The main procedure doesn't seem to be in Ada. */
751 /* Table of Ada operators and their GNAT-encoded names. Last entry is pair
754 const struct ada_opname_map ada_opname_table[] = {
755 {"Oadd", "\"+\"", BINOP_ADD},
756 {"Osubtract", "\"-\"", BINOP_SUB},
757 {"Omultiply", "\"*\"", BINOP_MUL},
758 {"Odivide", "\"/\"", BINOP_DIV},
759 {"Omod", "\"mod\"", BINOP_MOD},
760 {"Orem", "\"rem\"", BINOP_REM},
761 {"Oexpon", "\"**\"", BINOP_EXP},
762 {"Olt", "\"<\"", BINOP_LESS},
763 {"Ole", "\"<=\"", BINOP_LEQ},
764 {"Ogt", "\">\"", BINOP_GTR},
765 {"Oge", "\">=\"", BINOP_GEQ},
766 {"Oeq", "\"=\"", BINOP_EQUAL},
767 {"One", "\"/=\"", BINOP_NOTEQUAL},
768 {"Oand", "\"and\"", BINOP_BITWISE_AND},
769 {"Oor", "\"or\"", BINOP_BITWISE_IOR},
770 {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
771 {"Oconcat", "\"&\"", BINOP_CONCAT},
772 {"Oabs", "\"abs\"", UNOP_ABS},
773 {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
774 {"Oadd", "\"+\"", UNOP_PLUS},
775 {"Osubtract", "\"-\"", UNOP_NEG},
779 /* The "encoded" form of DECODED, according to GNAT conventions.
780 The result is valid until the next call to ada_encode. */
783 ada_encode (const char *decoded)
785 static char *encoding_buffer = NULL;
786 static size_t encoding_buffer_size = 0;
793 GROW_VECT (encoding_buffer, encoding_buffer_size,
794 2 * strlen (decoded) + 10);
797 for (p = decoded; *p != '\0'; p += 1)
801 encoding_buffer[k] = encoding_buffer[k + 1] = '_';
806 const struct ada_opname_map *mapping;
808 for (mapping = ada_opname_table;
809 mapping->encoded != NULL
810 && strncmp (mapping->decoded, p,
811 strlen (mapping->decoded)) != 0; mapping += 1)
813 if (mapping->encoded == NULL)
814 error (_("invalid Ada operator name: %s"), p);
815 strcpy (encoding_buffer + k, mapping->encoded);
816 k += strlen (mapping->encoded);
821 encoding_buffer[k] = *p;
826 encoding_buffer[k] = '\0';
827 return encoding_buffer;
830 /* Return NAME folded to lower case, or, if surrounded by single
831 quotes, unfolded, but with the quotes stripped away. Result good
835 ada_fold_name (const char *name)
837 static char *fold_buffer = NULL;
838 static size_t fold_buffer_size = 0;
840 int len = strlen (name);
841 GROW_VECT (fold_buffer, fold_buffer_size, len + 1);
845 strncpy (fold_buffer, name + 1, len - 2);
846 fold_buffer[len - 2] = '\000';
852 for (i = 0; i <= len; i += 1)
853 fold_buffer[i] = tolower (name[i]);
859 /* Return nonzero if C is either a digit or a lowercase alphabet character. */
862 is_lower_alphanum (const char c)
864 return (isdigit (c) || (isalpha (c) && islower (c)));
867 /* Remove either of these suffixes:
872 These are suffixes introduced by the compiler for entities such as
873 nested subprogram for instance, in order to avoid name clashes.
874 They do not serve any purpose for the debugger. */
877 ada_remove_trailing_digits (const char *encoded, int *len)
879 if (*len > 1 && isdigit (encoded[*len - 1]))
883 while (i > 0 && isdigit (encoded[i]))
885 if (i >= 0 && encoded[i] == '.')
887 else if (i >= 0 && encoded[i] == '$')
889 else if (i >= 2 && strncmp (encoded + i - 2, "___", 3) == 0)
891 else if (i >= 1 && strncmp (encoded + i - 1, "__", 2) == 0)
896 /* Remove the suffix introduced by the compiler for protected object
900 ada_remove_po_subprogram_suffix (const char *encoded, int *len)
902 /* Remove trailing N. */
904 /* Protected entry subprograms are broken into two
905 separate subprograms: The first one is unprotected, and has
906 a 'N' suffix; the second is the protected version, and has
907 the 'P' suffix. The second calls the first one after handling
908 the protection. Since the P subprograms are internally generated,
909 we leave these names undecoded, giving the user a clue that this
910 entity is internal. */
913 && encoded[*len - 1] == 'N'
914 && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
918 /* Remove trailing X[bn]* suffixes (indicating names in package bodies). */
921 ada_remove_Xbn_suffix (const char *encoded, int *len)
925 while (i > 0 && (encoded[i] == 'b' || encoded[i] == 'n'))
928 if (encoded[i] != 'X')
934 if (isalnum (encoded[i-1]))
938 /* If ENCODED follows the GNAT entity encoding conventions, then return
939 the decoded form of ENCODED. Otherwise, return "<%s>" where "%s" is
942 The resulting string is valid until the next call of ada_decode.
943 If the string is unchanged by decoding, the original string pointer
947 ada_decode (const char *encoded)
954 static char *decoding_buffer = NULL;
955 static size_t decoding_buffer_size = 0;
957 /* The name of the Ada main procedure starts with "_ada_".
958 This prefix is not part of the decoded name, so skip this part
959 if we see this prefix. */
960 if (strncmp (encoded, "_ada_", 5) == 0)
963 /* If the name starts with '_', then it is not a properly encoded
964 name, so do not attempt to decode it. Similarly, if the name
965 starts with '<', the name should not be decoded. */
966 if (encoded[0] == '_' || encoded[0] == '<')
969 len0 = strlen (encoded);
971 ada_remove_trailing_digits (encoded, &len0);
972 ada_remove_po_subprogram_suffix (encoded, &len0);
974 /* Remove the ___X.* suffix if present. Do not forget to verify that
975 the suffix is located before the current "end" of ENCODED. We want
976 to avoid re-matching parts of ENCODED that have previously been
977 marked as discarded (by decrementing LEN0). */
978 p = strstr (encoded, "___");
979 if (p != NULL && p - encoded < len0 - 3)
987 /* Remove any trailing TKB suffix. It tells us that this symbol
988 is for the body of a task, but that information does not actually
989 appear in the decoded name. */
991 if (len0 > 3 && strncmp (encoded + len0 - 3, "TKB", 3) == 0)
994 /* Remove any trailing TB suffix. The TB suffix is slightly different
995 from the TKB suffix because it is used for non-anonymous task
998 if (len0 > 2 && strncmp (encoded + len0 - 2, "TB", 2) == 0)
1001 /* Remove trailing "B" suffixes. */
1002 /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
1004 if (len0 > 1 && strncmp (encoded + len0 - 1, "B", 1) == 0)
1007 /* Make decoded big enough for possible expansion by operator name. */
1009 GROW_VECT (decoding_buffer, decoding_buffer_size, 2 * len0 + 1);
1010 decoded = decoding_buffer;
1012 /* Remove trailing __{digit}+ or trailing ${digit}+. */
1014 if (len0 > 1 && isdigit (encoded[len0 - 1]))
1017 while ((i >= 0 && isdigit (encoded[i]))
1018 || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
1020 if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
1022 else if (encoded[i] == '$')
1026 /* The first few characters that are not alphabetic are not part
1027 of any encoding we use, so we can copy them over verbatim. */
1029 for (i = 0, j = 0; i < len0 && !isalpha (encoded[i]); i += 1, j += 1)
1030 decoded[j] = encoded[i];
1035 /* Is this a symbol function? */
1036 if (at_start_name && encoded[i] == 'O')
1040 for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
1042 int op_len = strlen (ada_opname_table[k].encoded);
1043 if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
1045 && !isalnum (encoded[i + op_len]))
1047 strcpy (decoded + j, ada_opname_table[k].decoded);
1050 j += strlen (ada_opname_table[k].decoded);
1054 if (ada_opname_table[k].encoded != NULL)
1059 /* Replace "TK__" with "__", which will eventually be translated
1060 into "." (just below). */
1062 if (i < len0 - 4 && strncmp (encoded + i, "TK__", 4) == 0)
1065 /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
1066 be translated into "." (just below). These are internal names
1067 generated for anonymous blocks inside which our symbol is nested. */
1069 if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
1070 && encoded [i+2] == 'B' && encoded [i+3] == '_'
1071 && isdigit (encoded [i+4]))
1075 while (k < len0 && isdigit (encoded[k]))
1076 k++; /* Skip any extra digit. */
1078 /* Double-check that the "__B_{DIGITS}+" sequence we found
1079 is indeed followed by "__". */
1080 if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
1084 /* Remove _E{DIGITS}+[sb] */
1086 /* Just as for protected object subprograms, there are 2 categories
1087 of subprograms created by the compiler for each entry. The first
1088 one implements the actual entry code, and has a suffix following
1089 the convention above; the second one implements the barrier and
1090 uses the same convention as above, except that the 'E' is replaced
1093 Just as above, we do not decode the name of barrier functions
1094 to give the user a clue that the code he is debugging has been
1095 internally generated. */
1097 if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
1098 && isdigit (encoded[i+2]))
1102 while (k < len0 && isdigit (encoded[k]))
1106 && (encoded[k] == 'b' || encoded[k] == 's'))
1109 /* Just as an extra precaution, make sure that if this
1110 suffix is followed by anything else, it is a '_'.
1111 Otherwise, we matched this sequence by accident. */
1113 || (k < len0 && encoded[k] == '_'))
1118 /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
1119 the GNAT front-end in protected object subprograms. */
1122 && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
1124 /* Backtrack a bit up until we reach either the begining of
1125 the encoded name, or "__". Make sure that we only find
1126 digits or lowercase characters. */
1127 const char *ptr = encoded + i - 1;
1129 while (ptr >= encoded && is_lower_alphanum (ptr[0]))
1132 || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
1136 if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
1138 /* This is a X[bn]* sequence not separated from the previous
1139 part of the name with a non-alpha-numeric character (in other
1140 words, immediately following an alpha-numeric character), then
1141 verify that it is placed at the end of the encoded name. If
1142 not, then the encoding is not valid and we should abort the
1143 decoding. Otherwise, just skip it, it is used in body-nested
1147 while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
1151 else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
1153 /* Replace '__' by '.'. */
1161 /* It's a character part of the decoded name, so just copy it
1163 decoded[j] = encoded[i];
1168 decoded[j] = '\000';
1170 /* Decoded names should never contain any uppercase character.
1171 Double-check this, and abort the decoding if we find one. */
1173 for (i = 0; decoded[i] != '\0'; i += 1)
1174 if (isupper (decoded[i]) || decoded[i] == ' ')
1177 if (strcmp (decoded, encoded) == 0)
1183 GROW_VECT (decoding_buffer, decoding_buffer_size, strlen (encoded) + 3);
1184 decoded = decoding_buffer;
1185 if (encoded[0] == '<')
1186 strcpy (decoded, encoded);
1188 xsnprintf (decoded, decoding_buffer_size, "<%s>", encoded);
1193 /* Table for keeping permanent unique copies of decoded names. Once
1194 allocated, names in this table are never released. While this is a
1195 storage leak, it should not be significant unless there are massive
1196 changes in the set of decoded names in successive versions of a
1197 symbol table loaded during a single session. */
1198 static struct htab *decoded_names_store;
1200 /* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
1201 in the language-specific part of GSYMBOL, if it has not been
1202 previously computed. Tries to save the decoded name in the same
1203 obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
1204 in any case, the decoded symbol has a lifetime at least that of
1206 The GSYMBOL parameter is "mutable" in the C++ sense: logically
1207 const, but nevertheless modified to a semantically equivalent form
1208 when a decoded name is cached in it.
1212 ada_decode_symbol (const struct general_symbol_info *gsymbol)
1215 (char **) &gsymbol->language_specific.mangled_lang.demangled_name;
1217 if (*resultp == NULL)
1219 const char *decoded = ada_decode (gsymbol->name);
1221 if (gsymbol->obj_section != NULL)
1223 struct objfile *objf = gsymbol->obj_section->objfile;
1225 *resultp = obsavestring (decoded, strlen (decoded),
1226 &objf->objfile_obstack);
1228 /* Sometimes, we can't find a corresponding objfile, in which
1229 case, we put the result on the heap. Since we only decode
1230 when needed, we hope this usually does not cause a
1231 significant memory leak (FIXME). */
1232 if (*resultp == NULL)
1234 char **slot = (char **) htab_find_slot (decoded_names_store,
1238 *slot = xstrdup (decoded);
1247 ada_la_decode (const char *encoded, int options)
1249 return xstrdup (ada_decode (encoded));
1252 /* Returns non-zero iff SYM_NAME matches NAME, ignoring any trailing
1253 suffixes that encode debugging information or leading _ada_ on
1254 SYM_NAME (see is_name_suffix commentary for the debugging
1255 information that is ignored). If WILD, then NAME need only match a
1256 suffix of SYM_NAME minus the same suffixes. Also returns 0 if
1257 either argument is NULL. */
1260 ada_match_name (const char *sym_name, const char *name, int wild)
1262 if (sym_name == NULL || name == NULL)
1265 return wild_match (sym_name, name) == 0;
1268 int len_name = strlen (name);
1270 return (strncmp (sym_name, name, len_name) == 0
1271 && is_name_suffix (sym_name + len_name))
1272 || (strncmp (sym_name, "_ada_", 5) == 0
1273 && strncmp (sym_name + 5, name, len_name) == 0
1274 && is_name_suffix (sym_name + len_name + 5));
1281 /* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
1282 generated by the GNAT compiler to describe the index type used
1283 for each dimension of an array, check whether it follows the latest
1284 known encoding. If not, fix it up to conform to the latest encoding.
1285 Otherwise, do nothing. This function also does nothing if
1286 INDEX_DESC_TYPE is NULL.
1288 The GNAT encoding used to describle the array index type evolved a bit.
1289 Initially, the information would be provided through the name of each
1290 field of the structure type only, while the type of these fields was
1291 described as unspecified and irrelevant. The debugger was then expected
1292 to perform a global type lookup using the name of that field in order
1293 to get access to the full index type description. Because these global
1294 lookups can be very expensive, the encoding was later enhanced to make
1295 the global lookup unnecessary by defining the field type as being
1296 the full index type description.
1298 The purpose of this routine is to allow us to support older versions
1299 of the compiler by detecting the use of the older encoding, and by
1300 fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
1301 we essentially replace each field's meaningless type by the associated
1305 ada_fixup_array_indexes_type (struct type *index_desc_type)
1309 if (index_desc_type == NULL)
1311 gdb_assert (TYPE_NFIELDS (index_desc_type) > 0);
1313 /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
1314 to check one field only, no need to check them all). If not, return
1317 If our INDEX_DESC_TYPE was generated using the older encoding,
1318 the field type should be a meaningless integer type whose name
1319 is not equal to the field name. */
1320 if (TYPE_NAME (TYPE_FIELD_TYPE (index_desc_type, 0)) != NULL
1321 && strcmp (TYPE_NAME (TYPE_FIELD_TYPE (index_desc_type, 0)),
1322 TYPE_FIELD_NAME (index_desc_type, 0)) == 0)
1325 /* Fixup each field of INDEX_DESC_TYPE. */
1326 for (i = 0; i < TYPE_NFIELDS (index_desc_type); i++)
1328 char *name = TYPE_FIELD_NAME (index_desc_type, i);
1329 struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
1332 TYPE_FIELD_TYPE (index_desc_type, i) = raw_type;
1336 /* Names of MAX_ADA_DIMENS bounds in P_BOUNDS fields of array descriptors. */
1338 static char *bound_name[] = {
1339 "LB0", "UB0", "LB1", "UB1", "LB2", "UB2", "LB3", "UB3",
1340 "LB4", "UB4", "LB5", "UB5", "LB6", "UB6", "LB7", "UB7"
1343 /* Maximum number of array dimensions we are prepared to handle. */
1345 #define MAX_ADA_DIMENS (sizeof(bound_name) / (2*sizeof(char *)))
1347 /* Like modify_field, but allows bitpos > wordlength. */
1350 modify_general_field (struct type *type, char *addr,
1351 LONGEST fieldval, int bitpos, int bitsize)
1353 modify_field (type, addr + bitpos / 8, fieldval, bitpos % 8, bitsize);
1357 /* The desc_* routines return primitive portions of array descriptors
1360 /* The descriptor or array type, if any, indicated by TYPE; removes
1361 level of indirection, if needed. */
1363 static struct type *
1364 desc_base_type (struct type *type)
1368 type = ada_check_typedef (type);
1370 && (TYPE_CODE (type) == TYPE_CODE_PTR
1371 || TYPE_CODE (type) == TYPE_CODE_REF))
1372 return ada_check_typedef (TYPE_TARGET_TYPE (type));
1377 /* True iff TYPE indicates a "thin" array pointer type. */
1380 is_thin_pntr (struct type *type)
1383 is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
1384 || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
1387 /* The descriptor type for thin pointer type TYPE. */
1389 static struct type *
1390 thin_descriptor_type (struct type *type)
1392 struct type *base_type = desc_base_type (type);
1394 if (base_type == NULL)
1396 if (is_suffix (ada_type_name (base_type), "___XVE"))
1400 struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
1402 if (alt_type == NULL)
1409 /* A pointer to the array data for thin-pointer value VAL. */
1411 static struct value *
1412 thin_data_pntr (struct value *val)
1414 struct type *type = value_type (val);
1415 struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
1417 data_type = lookup_pointer_type (data_type);
1419 if (TYPE_CODE (type) == TYPE_CODE_PTR)
1420 return value_cast (data_type, value_copy (val));
1422 return value_from_longest (data_type, value_address (val));
1425 /* True iff TYPE indicates a "thick" array pointer type. */
1428 is_thick_pntr (struct type *type)
1430 type = desc_base_type (type);
1431 return (type != NULL && TYPE_CODE (type) == TYPE_CODE_STRUCT
1432 && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
1435 /* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1436 pointer to one, the type of its bounds data; otherwise, NULL. */
1438 static struct type *
1439 desc_bounds_type (struct type *type)
1443 type = desc_base_type (type);
1447 else if (is_thin_pntr (type))
1449 type = thin_descriptor_type (type);
1452 r = lookup_struct_elt_type (type, "BOUNDS", 1);
1454 return ada_check_typedef (r);
1456 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1458 r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
1460 return ada_check_typedef (TYPE_TARGET_TYPE (ada_check_typedef (r)));
1465 /* If ARR is an array descriptor (fat or thin pointer), or pointer to
1466 one, a pointer to its bounds data. Otherwise NULL. */
1468 static struct value *
1469 desc_bounds (struct value *arr)
1471 struct type *type = ada_check_typedef (value_type (arr));
1473 if (is_thin_pntr (type))
1475 struct type *bounds_type =
1476 desc_bounds_type (thin_descriptor_type (type));
1479 if (bounds_type == NULL)
1480 error (_("Bad GNAT array descriptor"));
1482 /* NOTE: The following calculation is not really kosher, but
1483 since desc_type is an XVE-encoded type (and shouldn't be),
1484 the correct calculation is a real pain. FIXME (and fix GCC). */
1485 if (TYPE_CODE (type) == TYPE_CODE_PTR)
1486 addr = value_as_long (arr);
1488 addr = value_address (arr);
1491 value_from_longest (lookup_pointer_type (bounds_type),
1492 addr - TYPE_LENGTH (bounds_type));
1495 else if (is_thick_pntr (type))
1497 struct value *p_bounds = value_struct_elt (&arr, NULL, "P_BOUNDS", NULL,
1498 _("Bad GNAT array descriptor"));
1499 struct type *p_bounds_type = value_type (p_bounds);
1502 && TYPE_CODE (p_bounds_type) == TYPE_CODE_PTR)
1504 struct type *target_type = TYPE_TARGET_TYPE (p_bounds_type);
1506 if (TYPE_STUB (target_type))
1507 p_bounds = value_cast (lookup_pointer_type
1508 (ada_check_typedef (target_type)),
1512 error (_("Bad GNAT array descriptor"));
1520 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1521 position of the field containing the address of the bounds data. */
1524 fat_pntr_bounds_bitpos (struct type *type)
1526 return TYPE_FIELD_BITPOS (desc_base_type (type), 1);
1529 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1530 size of the field containing the address of the bounds data. */
1533 fat_pntr_bounds_bitsize (struct type *type)
1535 type = desc_base_type (type);
1537 if (TYPE_FIELD_BITSIZE (type, 1) > 0)
1538 return TYPE_FIELD_BITSIZE (type, 1);
1540 return 8 * TYPE_LENGTH (ada_check_typedef (TYPE_FIELD_TYPE (type, 1)));
1543 /* If TYPE is the type of an array descriptor (fat or thin pointer) or a
1544 pointer to one, the type of its array data (a array-with-no-bounds type);
1545 otherwise, NULL. Use ada_type_of_array to get an array type with bounds
1548 static struct type *
1549 desc_data_target_type (struct type *type)
1551 type = desc_base_type (type);
1553 /* NOTE: The following is bogus; see comment in desc_bounds. */
1554 if (is_thin_pntr (type))
1555 return desc_base_type (TYPE_FIELD_TYPE (thin_descriptor_type (type), 1));
1556 else if (is_thick_pntr (type))
1558 struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
1561 && TYPE_CODE (ada_check_typedef (data_type)) == TYPE_CODE_PTR)
1562 return ada_check_typedef (TYPE_TARGET_TYPE (data_type));
1568 /* If ARR is an array descriptor (fat or thin pointer), a pointer to
1571 static struct value *
1572 desc_data (struct value *arr)
1574 struct type *type = value_type (arr);
1576 if (is_thin_pntr (type))
1577 return thin_data_pntr (arr);
1578 else if (is_thick_pntr (type))
1579 return value_struct_elt (&arr, NULL, "P_ARRAY", NULL,
1580 _("Bad GNAT array descriptor"));
1586 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1587 position of the field containing the address of the data. */
1590 fat_pntr_data_bitpos (struct type *type)
1592 return TYPE_FIELD_BITPOS (desc_base_type (type), 0);
1595 /* If TYPE is the type of an array-descriptor (fat pointer), the bit
1596 size of the field containing the address of the data. */
1599 fat_pntr_data_bitsize (struct type *type)
1601 type = desc_base_type (type);
1603 if (TYPE_FIELD_BITSIZE (type, 0) > 0)
1604 return TYPE_FIELD_BITSIZE (type, 0);
1606 return TARGET_CHAR_BIT * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0));
1609 /* If BOUNDS is an array-bounds structure (or pointer to one), return
1610 the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
1611 bound, if WHICH is 1. The first bound is I=1. */
1613 static struct value *
1614 desc_one_bound (struct value *bounds, int i, int which)
1616 return value_struct_elt (&bounds, NULL, bound_name[2 * i + which - 2], NULL,
1617 _("Bad GNAT array descriptor bounds"));
1620 /* If BOUNDS is an array-bounds structure type, return the bit position
1621 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
1622 bound, if WHICH is 1. The first bound is I=1. */
1625 desc_bound_bitpos (struct type *type, int i, int which)
1627 return TYPE_FIELD_BITPOS (desc_base_type (type), 2 * i + which - 2);
1630 /* If BOUNDS is an array-bounds structure type, return the bit field size
1631 of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
1632 bound, if WHICH is 1. The first bound is I=1. */
1635 desc_bound_bitsize (struct type *type, int i, int which)
1637 type = desc_base_type (type);
1639 if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
1640 return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
1642 return 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type, 2 * i + which - 2));
1645 /* If TYPE is the type of an array-bounds structure, the type of its
1646 Ith bound (numbering from 1). Otherwise, NULL. */
1648 static struct type *
1649 desc_index_type (struct type *type, int i)
1651 type = desc_base_type (type);
1653 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1654 return lookup_struct_elt_type (type, bound_name[2 * i - 2], 1);
1659 /* The number of index positions in the array-bounds type TYPE.
1660 Return 0 if TYPE is NULL. */
1663 desc_arity (struct type *type)
1665 type = desc_base_type (type);
1668 return TYPE_NFIELDS (type) / 2;
1672 /* Non-zero iff TYPE is a simple array type (not a pointer to one) or
1673 an array descriptor type (representing an unconstrained array
1677 ada_is_direct_array_type (struct type *type)
1681 type = ada_check_typedef (type);
1682 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
1683 || ada_is_array_descriptor_type (type));
1686 /* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
1690 ada_is_array_type (struct type *type)
1693 && (TYPE_CODE (type) == TYPE_CODE_PTR
1694 || TYPE_CODE (type) == TYPE_CODE_REF))
1695 type = TYPE_TARGET_TYPE (type);
1696 return ada_is_direct_array_type (type);
1699 /* Non-zero iff TYPE is a simple array type or pointer to one. */
1702 ada_is_simple_array_type (struct type *type)
1706 type = ada_check_typedef (type);
1707 return (TYPE_CODE (type) == TYPE_CODE_ARRAY
1708 || (TYPE_CODE (type) == TYPE_CODE_PTR
1709 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY));
1712 /* Non-zero iff TYPE belongs to a GNAT array descriptor. */
1715 ada_is_array_descriptor_type (struct type *type)
1717 struct type *data_type = desc_data_target_type (type);
1721 type = ada_check_typedef (type);
1722 return (data_type != NULL
1723 && TYPE_CODE (data_type) == TYPE_CODE_ARRAY
1724 && desc_arity (desc_bounds_type (type)) > 0);
1727 /* Non-zero iff type is a partially mal-formed GNAT array
1728 descriptor. FIXME: This is to compensate for some problems with
1729 debugging output from GNAT. Re-examine periodically to see if it
1733 ada_is_bogus_array_descriptor (struct type *type)
1737 && TYPE_CODE (type) == TYPE_CODE_STRUCT
1738 && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
1739 || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
1740 && !ada_is_array_descriptor_type (type);
1744 /* If ARR has a record type in the form of a standard GNAT array descriptor,
1745 (fat pointer) returns the type of the array data described---specifically,
1746 a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
1747 in from the descriptor; otherwise, they are left unspecified. If
1748 the ARR denotes a null array descriptor and BOUNDS is non-zero,
1749 returns NULL. The result is simply the type of ARR if ARR is not
1752 ada_type_of_array (struct value *arr, int bounds)
1754 if (ada_is_constrained_packed_array_type (value_type (arr)))
1755 return decode_constrained_packed_array_type (value_type (arr));
1757 if (!ada_is_array_descriptor_type (value_type (arr)))
1758 return value_type (arr);
1762 struct type *array_type =
1763 ada_check_typedef (desc_data_target_type (value_type (arr)));
1765 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
1766 TYPE_FIELD_BITSIZE (array_type, 0) =
1767 decode_packed_array_bitsize (value_type (arr));
1773 struct type *elt_type;
1775 struct value *descriptor;
1777 elt_type = ada_array_element_type (value_type (arr), -1);
1778 arity = ada_array_arity (value_type (arr));
1780 if (elt_type == NULL || arity == 0)
1781 return ada_check_typedef (value_type (arr));
1783 descriptor = desc_bounds (arr);
1784 if (value_as_long (descriptor) == 0)
1788 struct type *range_type = alloc_type_copy (value_type (arr));
1789 struct type *array_type = alloc_type_copy (value_type (arr));
1790 struct value *low = desc_one_bound (descriptor, arity, 0);
1791 struct value *high = desc_one_bound (descriptor, arity, 1);
1794 create_range_type (range_type, value_type (low),
1795 longest_to_int (value_as_long (low)),
1796 longest_to_int (value_as_long (high)));
1797 elt_type = create_array_type (array_type, elt_type, range_type);
1799 if (ada_is_unconstrained_packed_array_type (value_type (arr)))
1800 TYPE_FIELD_BITSIZE (elt_type, 0) =
1801 decode_packed_array_bitsize (value_type (arr));
1804 return lookup_pointer_type (elt_type);
1808 /* If ARR does not represent an array, returns ARR unchanged.
1809 Otherwise, returns either a standard GDB array with bounds set
1810 appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
1811 GDB array. Returns NULL if ARR is a null fat pointer. */
1814 ada_coerce_to_simple_array_ptr (struct value *arr)
1816 if (ada_is_array_descriptor_type (value_type (arr)))
1818 struct type *arrType = ada_type_of_array (arr, 1);
1820 if (arrType == NULL)
1822 return value_cast (arrType, value_copy (desc_data (arr)));
1824 else if (ada_is_constrained_packed_array_type (value_type (arr)))
1825 return decode_constrained_packed_array (arr);
1830 /* If ARR does not represent an array, returns ARR unchanged.
1831 Otherwise, returns a standard GDB array describing ARR (which may
1832 be ARR itself if it already is in the proper form). */
1834 static struct value *
1835 ada_coerce_to_simple_array (struct value *arr)
1837 if (ada_is_array_descriptor_type (value_type (arr)))
1839 struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
1842 error (_("Bounds unavailable for null array pointer."));
1843 check_size (TYPE_TARGET_TYPE (value_type (arrVal)));
1844 return value_ind (arrVal);
1846 else if (ada_is_constrained_packed_array_type (value_type (arr)))
1847 return decode_constrained_packed_array (arr);
1852 /* If TYPE represents a GNAT array type, return it translated to an
1853 ordinary GDB array type (possibly with BITSIZE fields indicating
1854 packing). For other types, is the identity. */
1857 ada_coerce_to_simple_array_type (struct type *type)
1859 if (ada_is_constrained_packed_array_type (type))
1860 return decode_constrained_packed_array_type (type);
1862 if (ada_is_array_descriptor_type (type))
1863 return ada_check_typedef (desc_data_target_type (type));
1868 /* Non-zero iff TYPE represents a standard GNAT packed-array type. */
1871 ada_is_packed_array_type (struct type *type)
1875 type = desc_base_type (type);
1876 type = ada_check_typedef (type);
1878 ada_type_name (type) != NULL
1879 && strstr (ada_type_name (type), "___XP") != NULL;
1882 /* Non-zero iff TYPE represents a standard GNAT constrained
1883 packed-array type. */
1886 ada_is_constrained_packed_array_type (struct type *type)
1888 return ada_is_packed_array_type (type)
1889 && !ada_is_array_descriptor_type (type);
1892 /* Non-zero iff TYPE represents an array descriptor for a
1893 unconstrained packed-array type. */
1896 ada_is_unconstrained_packed_array_type (struct type *type)
1898 return ada_is_packed_array_type (type)
1899 && ada_is_array_descriptor_type (type);
1902 /* Given that TYPE encodes a packed array type (constrained or unconstrained),
1903 return the size of its elements in bits. */
1906 decode_packed_array_bitsize (struct type *type)
1908 char *raw_name = ada_type_name (ada_check_typedef (type));
1913 raw_name = ada_type_name (desc_base_type (type));
1918 tail = strstr (raw_name, "___XP");
1920 if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
1923 (_("could not understand bit size information on packed array"));
1930 /* Given that TYPE is a standard GDB array type with all bounds filled
1931 in, and that the element size of its ultimate scalar constituents
1932 (that is, either its elements, or, if it is an array of arrays, its
1933 elements' elements, etc.) is *ELT_BITS, return an identical type,
1934 but with the bit sizes of its elements (and those of any
1935 constituent arrays) recorded in the BITSIZE components of its
1936 TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
1939 static struct type *
1940 constrained_packed_array_type (struct type *type, long *elt_bits)
1942 struct type *new_elt_type;
1943 struct type *new_type;
1944 LONGEST low_bound, high_bound;
1946 type = ada_check_typedef (type);
1947 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
1950 new_type = alloc_type_copy (type);
1952 constrained_packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type)),
1954 create_array_type (new_type, new_elt_type, TYPE_INDEX_TYPE (type));
1955 TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
1956 TYPE_NAME (new_type) = ada_type_name (type);
1958 if (get_discrete_bounds (TYPE_INDEX_TYPE (type),
1959 &low_bound, &high_bound) < 0)
1960 low_bound = high_bound = 0;
1961 if (high_bound < low_bound)
1962 *elt_bits = TYPE_LENGTH (new_type) = 0;
1965 *elt_bits *= (high_bound - low_bound + 1);
1966 TYPE_LENGTH (new_type) =
1967 (*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
1970 TYPE_FIXED_INSTANCE (new_type) = 1;
1974 /* The array type encoded by TYPE, where
1975 ada_is_constrained_packed_array_type (TYPE). */
1977 static struct type *
1978 decode_constrained_packed_array_type (struct type *type)
1980 char *raw_name = ada_type_name (ada_check_typedef (type));
1983 struct type *shadow_type;
1987 raw_name = ada_type_name (desc_base_type (type));
1992 name = (char *) alloca (strlen (raw_name) + 1);
1993 tail = strstr (raw_name, "___XP");
1994 type = desc_base_type (type);
1996 memcpy (name, raw_name, tail - raw_name);
1997 name[tail - raw_name] = '\000';
1999 shadow_type = ada_find_parallel_type_with_name (type, name);
2001 if (shadow_type == NULL)
2003 lim_warning (_("could not find bounds information on packed array"));
2006 CHECK_TYPEDEF (shadow_type);
2008 if (TYPE_CODE (shadow_type) != TYPE_CODE_ARRAY)
2010 lim_warning (_("could not understand bounds information on packed array"));
2014 bits = decode_packed_array_bitsize (type);
2015 return constrained_packed_array_type (shadow_type, &bits);
2018 /* Given that ARR is a struct value *indicating a GNAT constrained packed
2019 array, returns a simple array that denotes that array. Its type is a
2020 standard GDB array type except that the BITSIZEs of the array
2021 target types are set to the number of bits in each element, and the
2022 type length is set appropriately. */
2024 static struct value *
2025 decode_constrained_packed_array (struct value *arr)
2029 arr = ada_coerce_ref (arr);
2031 /* If our value is a pointer, then dererence it. Make sure that
2032 this operation does not cause the target type to be fixed, as
2033 this would indirectly cause this array to be decoded. The rest
2034 of the routine assumes that the array hasn't been decoded yet,
2035 so we use the basic "value_ind" routine to perform the dereferencing,
2036 as opposed to using "ada_value_ind". */
2037 if (TYPE_CODE (value_type (arr)) == TYPE_CODE_PTR)
2038 arr = value_ind (arr);
2040 type = decode_constrained_packed_array_type (value_type (arr));
2043 error (_("can't unpack array"));
2047 if (gdbarch_bits_big_endian (get_type_arch (value_type (arr)))
2048 && ada_is_modular_type (value_type (arr)))
2050 /* This is a (right-justified) modular type representing a packed
2051 array with no wrapper. In order to interpret the value through
2052 the (left-justified) packed array type we just built, we must
2053 first left-justify it. */
2054 int bit_size, bit_pos;
2057 mod = ada_modulus (value_type (arr)) - 1;
2064 bit_pos = HOST_CHAR_BIT * TYPE_LENGTH (value_type (arr)) - bit_size;
2065 arr = ada_value_primitive_packed_val (arr, NULL,
2066 bit_pos / HOST_CHAR_BIT,
2067 bit_pos % HOST_CHAR_BIT,
2072 return coerce_unspec_val_to_type (arr, type);
2076 /* The value of the element of packed array ARR at the ARITY indices
2077 given in IND. ARR must be a simple array. */
2079 static struct value *
2080 value_subscript_packed (struct value *arr, int arity, struct value **ind)
2083 int bits, elt_off, bit_off;
2084 long elt_total_bit_offset;
2085 struct type *elt_type;
2089 elt_total_bit_offset = 0;
2090 elt_type = ada_check_typedef (value_type (arr));
2091 for (i = 0; i < arity; i += 1)
2093 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY
2094 || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
2096 (_("attempt to do packed indexing of something other than a packed array"));
2099 struct type *range_type = TYPE_INDEX_TYPE (elt_type);
2100 LONGEST lowerbound, upperbound;
2103 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2105 lim_warning (_("don't know bounds of array"));
2106 lowerbound = upperbound = 0;
2109 idx = pos_atr (ind[i]);
2110 if (idx < lowerbound || idx > upperbound)
2111 lim_warning (_("packed array index %ld out of bounds"), (long) idx);
2112 bits = TYPE_FIELD_BITSIZE (elt_type, 0);
2113 elt_total_bit_offset += (idx - lowerbound) * bits;
2114 elt_type = ada_check_typedef (TYPE_TARGET_TYPE (elt_type));
2117 elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
2118 bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
2120 v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
2125 /* Non-zero iff TYPE includes negative integer values. */
2128 has_negatives (struct type *type)
2130 switch (TYPE_CODE (type))
2135 return !TYPE_UNSIGNED (type);
2136 case TYPE_CODE_RANGE:
2137 return TYPE_LOW_BOUND (type) < 0;
2142 /* Create a new value of type TYPE from the contents of OBJ starting
2143 at byte OFFSET, and bit offset BIT_OFFSET within that byte,
2144 proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
2145 assigning through the result will set the field fetched from.
2146 VALADDR is ignored unless OBJ is NULL, in which case,
2147 VALADDR+OFFSET must address the start of storage containing the
2148 packed value. The value returned in this case is never an lval.
2149 Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
2152 ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
2153 long offset, int bit_offset, int bit_size,
2157 int src, /* Index into the source area */
2158 targ, /* Index into the target area */
2159 srcBitsLeft, /* Number of source bits left to move */
2160 nsrc, ntarg, /* Number of source and target bytes */
2161 unusedLS, /* Number of bits in next significant
2162 byte of source that are unused */
2163 accumSize; /* Number of meaningful bits in accum */
2164 unsigned char *bytes; /* First byte containing data to unpack */
2165 unsigned char *unpacked;
2166 unsigned long accum; /* Staging area for bits being transferred */
2168 int len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
2169 /* Transmit bytes from least to most significant; delta is the direction
2170 the indices move. */
2171 int delta = gdbarch_bits_big_endian (get_type_arch (type)) ? -1 : 1;
2173 type = ada_check_typedef (type);
2177 v = allocate_value (type);
2178 bytes = (unsigned char *) (valaddr + offset);
2180 else if (VALUE_LVAL (obj) == lval_memory && value_lazy (obj))
2183 value_address (obj) + offset);
2184 bytes = (unsigned char *) alloca (len);
2185 read_memory (value_address (v), bytes, len);
2189 v = allocate_value (type);
2190 bytes = (unsigned char *) value_contents (obj) + offset;
2197 set_value_component_location (v, obj);
2198 new_addr = value_address (obj) + offset;
2199 set_value_bitpos (v, bit_offset + value_bitpos (obj));
2200 set_value_bitsize (v, bit_size);
2201 if (value_bitpos (v) >= HOST_CHAR_BIT)
2204 set_value_bitpos (v, value_bitpos (v) - HOST_CHAR_BIT);
2206 set_value_address (v, new_addr);
2209 set_value_bitsize (v, bit_size);
2210 unpacked = (unsigned char *) value_contents (v);
2212 srcBitsLeft = bit_size;
2214 ntarg = TYPE_LENGTH (type);
2218 memset (unpacked, 0, TYPE_LENGTH (type));
2221 else if (gdbarch_bits_big_endian (get_type_arch (type)))
2224 if (has_negatives (type)
2225 && ((bytes[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
2229 (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
2232 switch (TYPE_CODE (type))
2234 case TYPE_CODE_ARRAY:
2235 case TYPE_CODE_UNION:
2236 case TYPE_CODE_STRUCT:
2237 /* Non-scalar values must be aligned at a byte boundary... */
2239 (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
2240 /* ... And are placed at the beginning (most-significant) bytes
2242 targ = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
2247 targ = TYPE_LENGTH (type) - 1;
2253 int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
2256 unusedLS = bit_offset;
2259 if (has_negatives (type) && (bytes[len - 1] & (1 << sign_bit_offset)))
2266 /* Mask for removing bits of the next source byte that are not
2267 part of the value. */
2268 unsigned int unusedMSMask =
2269 (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
2271 /* Sign-extend bits for this byte. */
2272 unsigned int signMask = sign & ~unusedMSMask;
2275 (((bytes[src] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
2276 accumSize += HOST_CHAR_BIT - unusedLS;
2277 if (accumSize >= HOST_CHAR_BIT)
2279 unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT);
2280 accumSize -= HOST_CHAR_BIT;
2281 accum >>= HOST_CHAR_BIT;
2285 srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
2292 accum |= sign << accumSize;
2293 unpacked[targ] = accum & ~(~0L << HOST_CHAR_BIT);
2294 accumSize -= HOST_CHAR_BIT;
2295 accum >>= HOST_CHAR_BIT;
2303 /* Move N bits from SOURCE, starting at bit offset SRC_OFFSET to
2304 TARGET, starting at bit offset TARG_OFFSET. SOURCE and TARGET must
2307 move_bits (gdb_byte *target, int targ_offset, const gdb_byte *source,
2308 int src_offset, int n, int bits_big_endian_p)
2310 unsigned int accum, mask;
2311 int accum_bits, chunk_size;
2313 target += targ_offset / HOST_CHAR_BIT;
2314 targ_offset %= HOST_CHAR_BIT;
2315 source += src_offset / HOST_CHAR_BIT;
2316 src_offset %= HOST_CHAR_BIT;
2317 if (bits_big_endian_p)
2319 accum = (unsigned char) *source;
2321 accum_bits = HOST_CHAR_BIT - src_offset;
2327 accum = (accum << HOST_CHAR_BIT) + (unsigned char) *source;
2328 accum_bits += HOST_CHAR_BIT;
2330 chunk_size = HOST_CHAR_BIT - targ_offset;
2333 unused_right = HOST_CHAR_BIT - (chunk_size + targ_offset);
2334 mask = ((1 << chunk_size) - 1) << unused_right;
2337 | ((accum >> (accum_bits - chunk_size - unused_right)) & mask);
2339 accum_bits -= chunk_size;
2346 accum = (unsigned char) *source >> src_offset;
2348 accum_bits = HOST_CHAR_BIT - src_offset;
2352 accum = accum + ((unsigned char) *source << accum_bits);
2353 accum_bits += HOST_CHAR_BIT;
2355 chunk_size = HOST_CHAR_BIT - targ_offset;
2358 mask = ((1 << chunk_size) - 1) << targ_offset;
2359 *target = (*target & ~mask) | ((accum << targ_offset) & mask);
2361 accum_bits -= chunk_size;
2362 accum >>= chunk_size;
2369 /* Store the contents of FROMVAL into the location of TOVAL.
2370 Return a new value with the location of TOVAL and contents of
2371 FROMVAL. Handles assignment into packed fields that have
2372 floating-point or non-scalar types. */
2374 static struct value *
2375 ada_value_assign (struct value *toval, struct value *fromval)
2377 struct type *type = value_type (toval);
2378 int bits = value_bitsize (toval);
2380 toval = ada_coerce_ref (toval);
2381 fromval = ada_coerce_ref (fromval);
2383 if (ada_is_direct_array_type (value_type (toval)))
2384 toval = ada_coerce_to_simple_array (toval);
2385 if (ada_is_direct_array_type (value_type (fromval)))
2386 fromval = ada_coerce_to_simple_array (fromval);
2388 if (!deprecated_value_modifiable (toval))
2389 error (_("Left operand of assignment is not a modifiable lvalue."));
2391 if (VALUE_LVAL (toval) == lval_memory
2393 && (TYPE_CODE (type) == TYPE_CODE_FLT
2394 || TYPE_CODE (type) == TYPE_CODE_STRUCT))
2396 int len = (value_bitpos (toval)
2397 + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
2399 char *buffer = (char *) alloca (len);
2401 CORE_ADDR to_addr = value_address (toval);
2403 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2404 fromval = value_cast (type, fromval);
2406 read_memory (to_addr, buffer, len);
2407 from_size = value_bitsize (fromval);
2409 from_size = TYPE_LENGTH (value_type (fromval)) * TARGET_CHAR_BIT;
2410 if (gdbarch_bits_big_endian (get_type_arch (type)))
2411 move_bits (buffer, value_bitpos (toval),
2412 value_contents (fromval), from_size - bits, bits, 1);
2414 move_bits (buffer, value_bitpos (toval),
2415 value_contents (fromval), 0, bits, 0);
2416 write_memory (to_addr, buffer, len);
2417 observer_notify_memory_changed (to_addr, len, buffer);
2419 val = value_copy (toval);
2420 memcpy (value_contents_raw (val), value_contents (fromval),
2421 TYPE_LENGTH (type));
2422 deprecated_set_value_type (val, type);
2427 return value_assign (toval, fromval);
2431 /* Given that COMPONENT is a memory lvalue that is part of the lvalue
2432 * CONTAINER, assign the contents of VAL to COMPONENTS's place in
2433 * CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
2434 * COMPONENT, and not the inferior's memory. The current contents
2435 * of COMPONENT are ignored. */
2437 value_assign_to_component (struct value *container, struct value *component,
2440 LONGEST offset_in_container =
2441 (LONGEST) (value_address (component) - value_address (container));
2442 int bit_offset_in_container =
2443 value_bitpos (component) - value_bitpos (container);
2446 val = value_cast (value_type (component), val);
2448 if (value_bitsize (component) == 0)
2449 bits = TARGET_CHAR_BIT * TYPE_LENGTH (value_type (component));
2451 bits = value_bitsize (component);
2453 if (gdbarch_bits_big_endian (get_type_arch (value_type (container))))
2454 move_bits (value_contents_writeable (container) + offset_in_container,
2455 value_bitpos (container) + bit_offset_in_container,
2456 value_contents (val),
2457 TYPE_LENGTH (value_type (component)) * TARGET_CHAR_BIT - bits,
2460 move_bits (value_contents_writeable (container) + offset_in_container,
2461 value_bitpos (container) + bit_offset_in_container,
2462 value_contents (val), 0, bits, 0);
2465 /* The value of the element of array ARR at the ARITY indices given in IND.
2466 ARR may be either a simple array, GNAT array descriptor, or pointer
2470 ada_value_subscript (struct value *arr, int arity, struct value **ind)
2474 struct type *elt_type;
2476 elt = ada_coerce_to_simple_array (arr);
2478 elt_type = ada_check_typedef (value_type (elt));
2479 if (TYPE_CODE (elt_type) == TYPE_CODE_ARRAY
2480 && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
2481 return value_subscript_packed (elt, arity, ind);
2483 for (k = 0; k < arity; k += 1)
2485 if (TYPE_CODE (elt_type) != TYPE_CODE_ARRAY)
2486 error (_("too many subscripts (%d expected)"), k);
2487 elt = value_subscript (elt, pos_atr (ind[k]));
2492 /* Assuming ARR is a pointer to a standard GDB array of type TYPE, the
2493 value of the element of *ARR at the ARITY indices given in
2494 IND. Does not read the entire array into memory. */
2496 static struct value *
2497 ada_value_ptr_subscript (struct value *arr, struct type *type, int arity,
2502 for (k = 0; k < arity; k += 1)
2506 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
2507 error (_("too many subscripts (%d expected)"), k);
2508 arr = value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2510 get_discrete_bounds (TYPE_INDEX_TYPE (type), &lwb, &upb);
2511 arr = value_ptradd (arr, pos_atr (ind[k]) - lwb);
2512 type = TYPE_TARGET_TYPE (type);
2515 return value_ind (arr);
2518 /* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
2519 actual type of ARRAY_PTR is ignored), returns the Ada slice of HIGH-LOW+1
2520 elements starting at index LOW. The lower bound of this array is LOW, as
2522 static struct value *
2523 ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
2526 CORE_ADDR base = value_as_address (array_ptr)
2527 + ((low - ada_discrete_type_low_bound (TYPE_INDEX_TYPE (type)))
2528 * TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
2529 struct type *index_type =
2530 create_range_type (NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type)),
2532 struct type *slice_type =
2533 create_array_type (NULL, TYPE_TARGET_TYPE (type), index_type);
2535 return value_at_lazy (slice_type, base);
2539 static struct value *
2540 ada_value_slice (struct value *array, int low, int high)
2542 struct type *type = value_type (array);
2543 struct type *index_type =
2544 create_range_type (NULL, TYPE_INDEX_TYPE (type), low, high);
2545 struct type *slice_type =
2546 create_array_type (NULL, TYPE_TARGET_TYPE (type), index_type);
2548 return value_cast (slice_type, value_slice (array, low, high - low + 1));
2551 /* If type is a record type in the form of a standard GNAT array
2552 descriptor, returns the number of dimensions for type. If arr is a
2553 simple array, returns the number of "array of"s that prefix its
2554 type designation. Otherwise, returns 0. */
2557 ada_array_arity (struct type *type)
2564 type = desc_base_type (type);
2567 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
2568 return desc_arity (desc_bounds_type (type));
2570 while (TYPE_CODE (type) == TYPE_CODE_ARRAY)
2573 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
2579 /* If TYPE is a record type in the form of a standard GNAT array
2580 descriptor or a simple array type, returns the element type for
2581 TYPE after indexing by NINDICES indices, or by all indices if
2582 NINDICES is -1. Otherwise, returns NULL. */
2585 ada_array_element_type (struct type *type, int nindices)
2587 type = desc_base_type (type);
2589 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
2592 struct type *p_array_type;
2594 p_array_type = desc_data_target_type (type);
2596 k = ada_array_arity (type);
2600 /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
2601 if (nindices >= 0 && k > nindices)
2603 while (k > 0 && p_array_type != NULL)
2605 p_array_type = ada_check_typedef (TYPE_TARGET_TYPE (p_array_type));
2608 return p_array_type;
2610 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
2612 while (nindices != 0 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
2614 type = TYPE_TARGET_TYPE (type);
2623 /* The type of nth index in arrays of given type (n numbering from 1).
2624 Does not examine memory. Throws an error if N is invalid or TYPE
2625 is not an array type. NAME is the name of the Ada attribute being
2626 evaluated ('range, 'first, 'last, or 'length); it is used in building
2627 the error message. */
2629 static struct type *
2630 ada_index_type (struct type *type, int n, const char *name)
2632 struct type *result_type;
2634 type = desc_base_type (type);
2636 if (n < 0 || n > ada_array_arity (type))
2637 error (_("invalid dimension number to '%s"), name);
2639 if (ada_is_simple_array_type (type))
2643 for (i = 1; i < n; i += 1)
2644 type = TYPE_TARGET_TYPE (type);
2645 result_type = TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type));
2646 /* FIXME: The stabs type r(0,0);bound;bound in an array type
2647 has a target type of TYPE_CODE_UNDEF. We compensate here, but
2648 perhaps stabsread.c would make more sense. */
2649 if (result_type && TYPE_CODE (result_type) == TYPE_CODE_UNDEF)
2654 result_type = desc_index_type (desc_bounds_type (type), n);
2655 if (result_type == NULL)
2656 error (_("attempt to take bound of something that is not an array"));
2662 /* Given that arr is an array type, returns the lower bound of the
2663 Nth index (numbering from 1) if WHICH is 0, and the upper bound if
2664 WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
2665 array-descriptor type. It works for other arrays with bounds supplied
2666 by run-time quantities other than discriminants. */
2669 ada_array_bound_from_type (struct type * arr_type, int n, int which)
2671 struct type *type, *elt_type, *index_type_desc, *index_type;
2674 gdb_assert (which == 0 || which == 1);
2676 if (ada_is_constrained_packed_array_type (arr_type))
2677 arr_type = decode_constrained_packed_array_type (arr_type);
2679 if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
2680 return (LONGEST) - which;
2682 if (TYPE_CODE (arr_type) == TYPE_CODE_PTR)
2683 type = TYPE_TARGET_TYPE (arr_type);
2688 for (i = n; i > 1; i--)
2689 elt_type = TYPE_TARGET_TYPE (type);
2691 index_type_desc = ada_find_parallel_type (type, "___XA");
2692 ada_fixup_array_indexes_type (index_type_desc);
2693 if (index_type_desc != NULL)
2694 index_type = to_fixed_range_type (TYPE_FIELD_TYPE (index_type_desc, n - 1),
2697 index_type = TYPE_INDEX_TYPE (elt_type);
2700 (LONGEST) (which == 0
2701 ? ada_discrete_type_low_bound (index_type)
2702 : ada_discrete_type_high_bound (index_type));
2705 /* Given that arr is an array value, returns the lower bound of the
2706 nth index (numbering from 1) if WHICH is 0, and the upper bound if
2707 WHICH is 1. This routine will also work for arrays with bounds
2708 supplied by run-time quantities other than discriminants. */
2711 ada_array_bound (struct value *arr, int n, int which)
2713 struct type *arr_type = value_type (arr);
2715 if (ada_is_constrained_packed_array_type (arr_type))
2716 return ada_array_bound (decode_constrained_packed_array (arr), n, which);
2717 else if (ada_is_simple_array_type (arr_type))
2718 return ada_array_bound_from_type (arr_type, n, which);
2720 return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
2723 /* Given that arr is an array value, returns the length of the
2724 nth index. This routine will also work for arrays with bounds
2725 supplied by run-time quantities other than discriminants.
2726 Does not work for arrays indexed by enumeration types with representation
2727 clauses at the moment. */
2730 ada_array_length (struct value *arr, int n)
2732 struct type *arr_type = ada_check_typedef (value_type (arr));
2734 if (ada_is_constrained_packed_array_type (arr_type))
2735 return ada_array_length (decode_constrained_packed_array (arr), n);
2737 if (ada_is_simple_array_type (arr_type))
2738 return (ada_array_bound_from_type (arr_type, n, 1)
2739 - ada_array_bound_from_type (arr_type, n, 0) + 1);
2741 return (value_as_long (desc_one_bound (desc_bounds (arr), n, 1))
2742 - value_as_long (desc_one_bound (desc_bounds (arr), n, 0)) + 1);
2745 /* An empty array whose type is that of ARR_TYPE (an array type),
2746 with bounds LOW to LOW-1. */
2748 static struct value *
2749 empty_array (struct type *arr_type, int low)
2751 struct type *index_type =
2752 create_range_type (NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (arr_type)),
2754 struct type *elt_type = ada_array_element_type (arr_type, 1);
2756 return allocate_value (create_array_type (NULL, elt_type, index_type));
2760 /* Name resolution */
2762 /* The "decoded" name for the user-definable Ada operator corresponding
2766 ada_decoded_op_name (enum exp_opcode op)
2770 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
2772 if (ada_opname_table[i].op == op)
2773 return ada_opname_table[i].decoded;
2775 error (_("Could not find operator name for opcode"));
2779 /* Same as evaluate_type (*EXP), but resolves ambiguous symbol
2780 references (marked by OP_VAR_VALUE nodes in which the symbol has an
2781 undefined namespace) and converts operators that are
2782 user-defined into appropriate function calls. If CONTEXT_TYPE is
2783 non-null, it provides a preferred result type [at the moment, only
2784 type void has any effect---causing procedures to be preferred over
2785 functions in calls]. A null CONTEXT_TYPE indicates that a non-void
2786 return type is preferred. May change (expand) *EXP. */
2789 resolve (struct expression **expp, int void_context_p)
2791 struct type *context_type = NULL;
2795 context_type = builtin_type ((*expp)->gdbarch)->builtin_void;
2797 resolve_subexp (expp, &pc, 1, context_type);
2800 /* Resolve the operator of the subexpression beginning at
2801 position *POS of *EXPP. "Resolving" consists of replacing
2802 the symbols that have undefined namespaces in OP_VAR_VALUE nodes
2803 with their resolutions, replacing built-in operators with
2804 function calls to user-defined operators, where appropriate, and,
2805 when DEPROCEDURE_P is non-zero, converting function-valued variables
2806 into parameterless calls. May expand *EXPP. The CONTEXT_TYPE functions
2807 are as in ada_resolve, above. */
2809 static struct value *
2810 resolve_subexp (struct expression **expp, int *pos, int deprocedure_p,
2811 struct type *context_type)
2815 struct expression *exp; /* Convenience: == *expp. */
2816 enum exp_opcode op = (*expp)->elts[pc].opcode;
2817 struct value **argvec; /* Vector of operand types (alloca'ed). */
2818 int nargs; /* Number of operands. */
2825 /* Pass one: resolve operands, saving their types and updating *pos,
2830 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
2831 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
2836 resolve_subexp (expp, pos, 0, NULL);
2838 nargs = longest_to_int (exp->elts[pc + 1].longconst);
2843 resolve_subexp (expp, pos, 0, NULL);
2848 resolve_subexp (expp, pos, 1, check_typedef (exp->elts[pc + 1].type));
2851 case OP_ATR_MODULUS:
2861 case TERNOP_IN_RANGE:
2862 case BINOP_IN_BOUNDS:
2868 case OP_DISCRETE_RANGE:
2870 ada_forward_operator_length (exp, pc, &oplen, &nargs);
2879 arg1 = resolve_subexp (expp, pos, 0, NULL);
2881 resolve_subexp (expp, pos, 1, NULL);
2883 resolve_subexp (expp, pos, 1, value_type (arg1));
2900 case BINOP_LOGICAL_AND:
2901 case BINOP_LOGICAL_OR:
2902 case BINOP_BITWISE_AND:
2903 case BINOP_BITWISE_IOR:
2904 case BINOP_BITWISE_XOR:
2907 case BINOP_NOTEQUAL:
2914 case BINOP_SUBSCRIPT:
2922 case UNOP_LOGICAL_NOT:
2938 case OP_INTERNALVAR:
2948 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
2951 case STRUCTOP_STRUCT:
2952 *pos += 4 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
2965 error (_("Unexpected operator during name resolution"));
2968 argvec = (struct value * *) alloca (sizeof (struct value *) * (nargs + 1));
2969 for (i = 0; i < nargs; i += 1)
2970 argvec[i] = resolve_subexp (expp, pos, 1, NULL);
2974 /* Pass two: perform any resolution on principal operator. */
2981 if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
2983 struct ada_symbol_info *candidates;
2987 ada_lookup_symbol_list (SYMBOL_LINKAGE_NAME
2988 (exp->elts[pc + 2].symbol),
2989 exp->elts[pc + 1].block, VAR_DOMAIN,
2992 if (n_candidates > 1)
2994 /* Types tend to get re-introduced locally, so if there
2995 are any local symbols that are not types, first filter
2998 for (j = 0; j < n_candidates; j += 1)
2999 switch (SYMBOL_CLASS (candidates[j].sym))
3004 case LOC_REGPARM_ADDR:
3012 if (j < n_candidates)
3015 while (j < n_candidates)
3017 if (SYMBOL_CLASS (candidates[j].sym) == LOC_TYPEDEF)
3019 candidates[j] = candidates[n_candidates - 1];
3028 if (n_candidates == 0)
3029 error (_("No definition found for %s"),
3030 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
3031 else if (n_candidates == 1)
3033 else if (deprocedure_p
3034 && !is_nonfunction (candidates, n_candidates))
3036 i = ada_resolve_function
3037 (candidates, n_candidates, NULL, 0,
3038 SYMBOL_LINKAGE_NAME (exp->elts[pc + 2].symbol),
3041 error (_("Could not find a match for %s"),
3042 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
3046 printf_filtered (_("Multiple matches for %s\n"),
3047 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
3048 user_select_syms (candidates, n_candidates, 1);
3052 exp->elts[pc + 1].block = candidates[i].block;
3053 exp->elts[pc + 2].symbol = candidates[i].sym;
3054 if (innermost_block == NULL
3055 || contained_in (candidates[i].block, innermost_block))
3056 innermost_block = candidates[i].block;
3060 && (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 2].symbol))
3063 replace_operator_with_call (expp, pc, 0, 0,
3064 exp->elts[pc + 2].symbol,
3065 exp->elts[pc + 1].block);
3072 if (exp->elts[pc + 3].opcode == OP_VAR_VALUE
3073 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
3075 struct ada_symbol_info *candidates;
3079 ada_lookup_symbol_list (SYMBOL_LINKAGE_NAME
3080 (exp->elts[pc + 5].symbol),
3081 exp->elts[pc + 4].block, VAR_DOMAIN,
3083 if (n_candidates == 1)
3087 i = ada_resolve_function
3088 (candidates, n_candidates,
3090 SYMBOL_LINKAGE_NAME (exp->elts[pc + 5].symbol),
3093 error (_("Could not find a match for %s"),
3094 SYMBOL_PRINT_NAME (exp->elts[pc + 5].symbol));
3097 exp->elts[pc + 4].block = candidates[i].block;
3098 exp->elts[pc + 5].symbol = candidates[i].sym;
3099 if (innermost_block == NULL
3100 || contained_in (candidates[i].block, innermost_block))
3101 innermost_block = candidates[i].block;
3112 case BINOP_BITWISE_AND:
3113 case BINOP_BITWISE_IOR:
3114 case BINOP_BITWISE_XOR:
3116 case BINOP_NOTEQUAL:
3124 case UNOP_LOGICAL_NOT:
3126 if (possible_user_operator_p (op, argvec))
3128 struct ada_symbol_info *candidates;
3132 ada_lookup_symbol_list (ada_encode (ada_decoded_op_name (op)),
3133 (struct block *) NULL, VAR_DOMAIN,
3135 i = ada_resolve_function (candidates, n_candidates, argvec, nargs,
3136 ada_decoded_op_name (op), NULL);
3140 replace_operator_with_call (expp, pc, nargs, 1,
3141 candidates[i].sym, candidates[i].block);
3152 return evaluate_subexp_type (exp, pos);
3155 /* Return non-zero if formal type FTYPE matches actual type ATYPE. If
3156 MAY_DEREF is non-zero, the formal may be a pointer and the actual
3158 /* The term "match" here is rather loose. The match is heuristic and
3162 ada_type_match (struct type *ftype, struct type *atype, int may_deref)
3164 ftype = ada_check_typedef (ftype);
3165 atype = ada_check_typedef (atype);
3167 if (TYPE_CODE (ftype) == TYPE_CODE_REF)
3168 ftype = TYPE_TARGET_TYPE (ftype);
3169 if (TYPE_CODE (atype) == TYPE_CODE_REF)
3170 atype = TYPE_TARGET_TYPE (atype);
3172 switch (TYPE_CODE (ftype))
3175 return TYPE_CODE (ftype) == TYPE_CODE (atype);
3177 if (TYPE_CODE (atype) == TYPE_CODE_PTR)
3178 return ada_type_match (TYPE_TARGET_TYPE (ftype),
3179 TYPE_TARGET_TYPE (atype), 0);
3182 && ada_type_match (TYPE_TARGET_TYPE (ftype), atype, 0));
3184 case TYPE_CODE_ENUM:
3185 case TYPE_CODE_RANGE:
3186 switch (TYPE_CODE (atype))
3189 case TYPE_CODE_ENUM:
3190 case TYPE_CODE_RANGE:
3196 case TYPE_CODE_ARRAY:
3197 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
3198 || ada_is_array_descriptor_type (atype));
3200 case TYPE_CODE_STRUCT:
3201 if (ada_is_array_descriptor_type (ftype))
3202 return (TYPE_CODE (atype) == TYPE_CODE_ARRAY
3203 || ada_is_array_descriptor_type (atype));
3205 return (TYPE_CODE (atype) == TYPE_CODE_STRUCT
3206 && !ada_is_array_descriptor_type (atype));
3208 case TYPE_CODE_UNION:
3210 return (TYPE_CODE (atype) == TYPE_CODE (ftype));
3214 /* Return non-zero if the formals of FUNC "sufficiently match" the
3215 vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
3216 may also be an enumeral, in which case it is treated as a 0-
3217 argument function. */
3220 ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
3223 struct type *func_type = SYMBOL_TYPE (func);
3225 if (SYMBOL_CLASS (func) == LOC_CONST
3226 && TYPE_CODE (func_type) == TYPE_CODE_ENUM)
3227 return (n_actuals == 0);
3228 else if (func_type == NULL || TYPE_CODE (func_type) != TYPE_CODE_FUNC)
3231 if (TYPE_NFIELDS (func_type) != n_actuals)
3234 for (i = 0; i < n_actuals; i += 1)
3236 if (actuals[i] == NULL)
3240 struct type *ftype = ada_check_typedef (TYPE_FIELD_TYPE (func_type,
3242 struct type *atype = ada_check_typedef (value_type (actuals[i]));
3244 if (!ada_type_match (ftype, atype, 1))
3251 /* False iff function type FUNC_TYPE definitely does not produce a value
3252 compatible with type CONTEXT_TYPE. Conservatively returns 1 if
3253 FUNC_TYPE is not a valid function type with a non-null return type
3254 or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
3257 return_match (struct type *func_type, struct type *context_type)
3259 struct type *return_type;
3261 if (func_type == NULL)
3264 if (TYPE_CODE (func_type) == TYPE_CODE_FUNC)
3265 return_type = base_type (TYPE_TARGET_TYPE (func_type));
3267 return_type = base_type (func_type);
3268 if (return_type == NULL)
3271 context_type = base_type (context_type);
3273 if (TYPE_CODE (return_type) == TYPE_CODE_ENUM)
3274 return context_type == NULL || return_type == context_type;
3275 else if (context_type == NULL)
3276 return TYPE_CODE (return_type) != TYPE_CODE_VOID;
3278 return TYPE_CODE (return_type) == TYPE_CODE (context_type);
3282 /* Returns the index in SYMS[0..NSYMS-1] that contains the symbol for the
3283 function (if any) that matches the types of the NARGS arguments in
3284 ARGS. If CONTEXT_TYPE is non-null and there is at least one match
3285 that returns that type, then eliminate matches that don't. If
3286 CONTEXT_TYPE is void and there is at least one match that does not
3287 return void, eliminate all matches that do.
3289 Asks the user if there is more than one match remaining. Returns -1
3290 if there is no such symbol or none is selected. NAME is used
3291 solely for messages. May re-arrange and modify SYMS in
3292 the process; the index returned is for the modified vector. */
3295 ada_resolve_function (struct ada_symbol_info syms[],
3296 int nsyms, struct value **args, int nargs,
3297 const char *name, struct type *context_type)
3301 int m; /* Number of hits */
3304 /* In the first pass of the loop, we only accept functions matching
3305 context_type. If none are found, we add a second pass of the loop
3306 where every function is accepted. */
3307 for (fallback = 0; m == 0 && fallback < 2; fallback++)
3309 for (k = 0; k < nsyms; k += 1)
3311 struct type *type = ada_check_typedef (SYMBOL_TYPE (syms[k].sym));
3313 if (ada_args_match (syms[k].sym, args, nargs)
3314 && (fallback || return_match (type, context_type)))
3326 printf_filtered (_("Multiple matches for %s\n"), name);
3327 user_select_syms (syms, m, 1);
3333 /* Returns true (non-zero) iff decoded name N0 should appear before N1
3334 in a listing of choices during disambiguation (see sort_choices, below).
3335 The idea is that overloadings of a subprogram name from the
3336 same package should sort in their source order. We settle for ordering
3337 such symbols by their trailing number (__N or $N). */
3340 encoded_ordered_before (char *N0, char *N1)
3344 else if (N0 == NULL)
3350 for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
3352 for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
3354 if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
3355 && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
3360 while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
3363 while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
3365 if (n0 == n1 && strncmp (N0, N1, n0) == 0)
3366 return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
3368 return (strcmp (N0, N1) < 0);
3372 /* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
3376 sort_choices (struct ada_symbol_info syms[], int nsyms)
3380 for (i = 1; i < nsyms; i += 1)
3382 struct ada_symbol_info sym = syms[i];
3385 for (j = i - 1; j >= 0; j -= 1)
3387 if (encoded_ordered_before (SYMBOL_LINKAGE_NAME (syms[j].sym),
3388 SYMBOL_LINKAGE_NAME (sym.sym)))
3390 syms[j + 1] = syms[j];
3396 /* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
3397 by asking the user (if necessary), returning the number selected,
3398 and setting the first elements of SYMS items. Error if no symbols
3401 /* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
3402 to be re-integrated one of these days. */
3405 user_select_syms (struct ada_symbol_info *syms, int nsyms, int max_results)
3408 int *chosen = (int *) alloca (sizeof (int) * nsyms);
3410 int first_choice = (max_results == 1) ? 1 : 2;
3411 const char *select_mode = multiple_symbols_select_mode ();
3413 if (max_results < 1)
3414 error (_("Request to select 0 symbols!"));
3418 if (select_mode == multiple_symbols_cancel)
3420 canceled because the command is ambiguous\n\
3421 See set/show multiple-symbol."));
3423 /* If select_mode is "all", then return all possible symbols.
3424 Only do that if more than one symbol can be selected, of course.
3425 Otherwise, display the menu as usual. */
3426 if (select_mode == multiple_symbols_all && max_results > 1)
3429 printf_unfiltered (_("[0] cancel\n"));
3430 if (max_results > 1)
3431 printf_unfiltered (_("[1] all\n"));
3433 sort_choices (syms, nsyms);
3435 for (i = 0; i < nsyms; i += 1)
3437 if (syms[i].sym == NULL)
3440 if (SYMBOL_CLASS (syms[i].sym) == LOC_BLOCK)
3442 struct symtab_and_line sal =
3443 find_function_start_sal (syms[i].sym, 1);
3445 if (sal.symtab == NULL)
3446 printf_unfiltered (_("[%d] %s at <no source file available>:%d\n"),
3448 SYMBOL_PRINT_NAME (syms[i].sym),
3451 printf_unfiltered (_("[%d] %s at %s:%d\n"), i + first_choice,
3452 SYMBOL_PRINT_NAME (syms[i].sym),
3453 sal.symtab->filename, sal.line);
3459 (SYMBOL_CLASS (syms[i].sym) == LOC_CONST
3460 && SYMBOL_TYPE (syms[i].sym) != NULL
3461 && TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) == TYPE_CODE_ENUM);
3462 struct symtab *symtab = syms[i].sym->symtab;
3464 if (SYMBOL_LINE (syms[i].sym) != 0 && symtab != NULL)
3465 printf_unfiltered (_("[%d] %s at %s:%d\n"),
3467 SYMBOL_PRINT_NAME (syms[i].sym),
3468 symtab->filename, SYMBOL_LINE (syms[i].sym));
3469 else if (is_enumeral
3470 && TYPE_NAME (SYMBOL_TYPE (syms[i].sym)) != NULL)
3472 printf_unfiltered (("[%d] "), i + first_choice);
3473 ada_print_type (SYMBOL_TYPE (syms[i].sym), NULL,
3475 printf_unfiltered (_("'(%s) (enumeral)\n"),
3476 SYMBOL_PRINT_NAME (syms[i].sym));
3478 else if (symtab != NULL)
3479 printf_unfiltered (is_enumeral
3480 ? _("[%d] %s in %s (enumeral)\n")
3481 : _("[%d] %s at %s:?\n"),
3483 SYMBOL_PRINT_NAME (syms[i].sym),
3486 printf_unfiltered (is_enumeral
3487 ? _("[%d] %s (enumeral)\n")
3488 : _("[%d] %s at ?\n"),
3490 SYMBOL_PRINT_NAME (syms[i].sym));
3494 n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
3497 for (i = 0; i < n_chosen; i += 1)
3498 syms[i] = syms[chosen[i]];
3503 /* Read and validate a set of numeric choices from the user in the
3504 range 0 .. N_CHOICES-1. Place the results in increasing
3505 order in CHOICES[0 .. N-1], and return N.
3507 The user types choices as a sequence of numbers on one line
3508 separated by blanks, encoding them as follows:
3510 + A choice of 0 means to cancel the selection, throwing an error.
3511 + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
3512 + The user chooses k by typing k+IS_ALL_CHOICE+1.
3514 The user is not allowed to choose more than MAX_RESULTS values.
3516 ANNOTATION_SUFFIX, if present, is used to annotate the input
3517 prompts (for use with the -f switch). */
3520 get_selections (int *choices, int n_choices, int max_results,
3521 int is_all_choice, char *annotation_suffix)
3526 int first_choice = is_all_choice ? 2 : 1;
3528 prompt = getenv ("PS2");
3532 args = command_line_input (prompt, 0, annotation_suffix);
3535 error_no_arg (_("one or more choice numbers"));
3539 /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
3540 order, as given in args. Choices are validated. */
3546 while (isspace (*args))
3548 if (*args == '\0' && n_chosen == 0)
3549 error_no_arg (_("one or more choice numbers"));
3550 else if (*args == '\0')
3553 choice = strtol (args, &args2, 10);
3554 if (args == args2 || choice < 0
3555 || choice > n_choices + first_choice - 1)
3556 error (_("Argument must be choice number"));
3560 error (_("cancelled"));
3562 if (choice < first_choice)
3564 n_chosen = n_choices;
3565 for (j = 0; j < n_choices; j += 1)
3569 choice -= first_choice;
3571 for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
3575 if (j < 0 || choice != choices[j])
3579 for (k = n_chosen - 1; k > j; k -= 1)
3580 choices[k + 1] = choices[k];
3581 choices[j + 1] = choice;
3586 if (n_chosen > max_results)
3587 error (_("Select no more than %d of the above"), max_results);
3592 /* Replace the operator of length OPLEN at position PC in *EXPP with a call
3593 on the function identified by SYM and BLOCK, and taking NARGS
3594 arguments. Update *EXPP as needed to hold more space. */
3597 replace_operator_with_call (struct expression **expp, int pc, int nargs,
3598 int oplen, struct symbol *sym,
3599 struct block *block)
3601 /* A new expression, with 6 more elements (3 for funcall, 4 for function
3602 symbol, -oplen for operator being replaced). */
3603 struct expression *newexp = (struct expression *)
3604 xmalloc (sizeof (struct expression)
3605 + EXP_ELEM_TO_BYTES ((*expp)->nelts + 7 - oplen));
3606 struct expression *exp = *expp;
3608 newexp->nelts = exp->nelts + 7 - oplen;
3609 newexp->language_defn = exp->language_defn;
3610 memcpy (newexp->elts, exp->elts, EXP_ELEM_TO_BYTES (pc));
3611 memcpy (newexp->elts + pc + 7, exp->elts + pc + oplen,
3612 EXP_ELEM_TO_BYTES (exp->nelts - pc - oplen));
3614 newexp->elts[pc].opcode = newexp->elts[pc + 2].opcode = OP_FUNCALL;
3615 newexp->elts[pc + 1].longconst = (LONGEST) nargs;
3617 newexp->elts[pc + 3].opcode = newexp->elts[pc + 6].opcode = OP_VAR_VALUE;
3618 newexp->elts[pc + 4].block = block;
3619 newexp->elts[pc + 5].symbol = sym;
3625 /* Type-class predicates */
3627 /* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
3631 numeric_type_p (struct type *type)
3637 switch (TYPE_CODE (type))
3642 case TYPE_CODE_RANGE:
3643 return (type == TYPE_TARGET_TYPE (type)
3644 || numeric_type_p (TYPE_TARGET_TYPE (type)));
3651 /* True iff TYPE is integral (an INT or RANGE of INTs). */
3654 integer_type_p (struct type *type)
3660 switch (TYPE_CODE (type))
3664 case TYPE_CODE_RANGE:
3665 return (type == TYPE_TARGET_TYPE (type)
3666 || integer_type_p (TYPE_TARGET_TYPE (type)));
3673 /* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
3676 scalar_type_p (struct type *type)
3682 switch (TYPE_CODE (type))
3685 case TYPE_CODE_RANGE:
3686 case TYPE_CODE_ENUM:
3695 /* True iff TYPE is discrete (INT, RANGE, ENUM). */
3698 discrete_type_p (struct type *type)
3704 switch (TYPE_CODE (type))
3707 case TYPE_CODE_RANGE:
3708 case TYPE_CODE_ENUM:
3709 case TYPE_CODE_BOOL:
3717 /* Returns non-zero if OP with operands in the vector ARGS could be
3718 a user-defined function. Errs on the side of pre-defined operators
3719 (i.e., result 0). */
3722 possible_user_operator_p (enum exp_opcode op, struct value *args[])
3724 struct type *type0 =
3725 (args[0] == NULL) ? NULL : ada_check_typedef (value_type (args[0]));
3726 struct type *type1 =
3727 (args[1] == NULL) ? NULL : ada_check_typedef (value_type (args[1]));
3741 return (!(numeric_type_p (type0) && numeric_type_p (type1)));
3745 case BINOP_BITWISE_AND:
3746 case BINOP_BITWISE_IOR:
3747 case BINOP_BITWISE_XOR:
3748 return (!(integer_type_p (type0) && integer_type_p (type1)));
3751 case BINOP_NOTEQUAL:
3756 return (!(scalar_type_p (type0) && scalar_type_p (type1)));
3759 return !ada_is_array_type (type0) || !ada_is_array_type (type1);
3762 return (!(numeric_type_p (type0) && integer_type_p (type1)));
3766 case UNOP_LOGICAL_NOT:
3768 return (!numeric_type_p (type0));
3777 1. In the following, we assume that a renaming type's name may
3778 have an ___XD suffix. It would be nice if this went away at some
3780 2. We handle both the (old) purely type-based representation of
3781 renamings and the (new) variable-based encoding. At some point,
3782 it is devoutly to be hoped that the former goes away
3783 (FIXME: hilfinger-2007-07-09).
3784 3. Subprogram renamings are not implemented, although the XRS
3785 suffix is recognized (FIXME: hilfinger-2007-07-09). */
3787 /* If SYM encodes a renaming,
3789 <renaming> renames <renamed entity>,
3791 sets *LEN to the length of the renamed entity's name,
3792 *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
3793 the string describing the subcomponent selected from the renamed
3794 entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
3795 (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
3796 are undefined). Otherwise, returns a value indicating the category
3797 of entity renamed: an object (ADA_OBJECT_RENAMING), exception
3798 (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
3799 subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
3800 strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
3801 deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
3802 may be NULL, in which case they are not assigned.
3804 [Currently, however, GCC does not generate subprogram renamings.] */
3806 enum ada_renaming_category
3807 ada_parse_renaming (struct symbol *sym,
3808 const char **renamed_entity, int *len,
3809 const char **renaming_expr)
3811 enum ada_renaming_category kind;
3816 return ADA_NOT_RENAMING;
3817 switch (SYMBOL_CLASS (sym))
3820 return ADA_NOT_RENAMING;
3822 return parse_old_style_renaming (SYMBOL_TYPE (sym),
3823 renamed_entity, len, renaming_expr);
3827 case LOC_OPTIMIZED_OUT:
3828 info = strstr (SYMBOL_LINKAGE_NAME (sym), "___XR");
3830 return ADA_NOT_RENAMING;
3834 kind = ADA_OBJECT_RENAMING;
3838 kind = ADA_EXCEPTION_RENAMING;
3842 kind = ADA_PACKAGE_RENAMING;
3846 kind = ADA_SUBPROGRAM_RENAMING;
3850 return ADA_NOT_RENAMING;
3854 if (renamed_entity != NULL)
3855 *renamed_entity = info;
3856 suffix = strstr (info, "___XE");
3857 if (suffix == NULL || suffix == info)
3858 return ADA_NOT_RENAMING;
3860 *len = strlen (info) - strlen (suffix);
3862 if (renaming_expr != NULL)
3863 *renaming_expr = suffix;
3867 /* Assuming TYPE encodes a renaming according to the old encoding in
3868 exp_dbug.ads, returns details of that renaming in *RENAMED_ENTITY,
3869 *LEN, and *RENAMING_EXPR, as for ada_parse_renaming, above. Returns
3870 ADA_NOT_RENAMING otherwise. */
3871 static enum ada_renaming_category
3872 parse_old_style_renaming (struct type *type,
3873 const char **renamed_entity, int *len,
3874 const char **renaming_expr)
3876 enum ada_renaming_category kind;
3881 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_ENUM
3882 || TYPE_NFIELDS (type) != 1)
3883 return ADA_NOT_RENAMING;
3885 name = type_name_no_tag (type);
3887 return ADA_NOT_RENAMING;
3889 name = strstr (name, "___XR");
3891 return ADA_NOT_RENAMING;
3896 kind = ADA_OBJECT_RENAMING;
3899 kind = ADA_EXCEPTION_RENAMING;
3902 kind = ADA_PACKAGE_RENAMING;
3905 kind = ADA_SUBPROGRAM_RENAMING;
3908 return ADA_NOT_RENAMING;
3911 info = TYPE_FIELD_NAME (type, 0);
3913 return ADA_NOT_RENAMING;
3914 if (renamed_entity != NULL)
3915 *renamed_entity = info;
3916 suffix = strstr (info, "___XE");
3917 if (renaming_expr != NULL)
3918 *renaming_expr = suffix + 5;
3919 if (suffix == NULL || suffix == info)
3920 return ADA_NOT_RENAMING;
3922 *len = suffix - info;
3928 /* Evaluation: Function Calls */
3930 /* Return an lvalue containing the value VAL. This is the identity on
3931 lvalues, and otherwise has the side-effect of pushing a copy of VAL
3932 on the stack, using and updating *SP as the stack pointer, and
3933 returning an lvalue whose value_address points to the copy. */
3935 static struct value *
3936 ensure_lval (struct value *val, struct gdbarch *gdbarch, CORE_ADDR *sp)
3938 if (! VALUE_LVAL (val))
3940 int len = TYPE_LENGTH (ada_check_typedef (value_type (val)));
3942 /* The following is taken from the structure-return code in
3943 call_function_by_hand. FIXME: Therefore, some refactoring seems
3945 if (gdbarch_inner_than (gdbarch, 1, 2))
3947 /* Stack grows downward. Align SP and value_address (val) after
3948 reserving sufficient space. */
3950 if (gdbarch_frame_align_p (gdbarch))
3951 *sp = gdbarch_frame_align (gdbarch, *sp);
3952 set_value_address (val, *sp);
3956 /* Stack grows upward. Align the frame, allocate space, and
3957 then again, re-align the frame. */
3958 if (gdbarch_frame_align_p (gdbarch))
3959 *sp = gdbarch_frame_align (gdbarch, *sp);
3960 set_value_address (val, *sp);
3962 if (gdbarch_frame_align_p (gdbarch))
3963 *sp = gdbarch_frame_align (gdbarch, *sp);
3965 VALUE_LVAL (val) = lval_memory;
3967 write_memory (value_address (val), value_contents (val), len);
3973 /* Return the value ACTUAL, converted to be an appropriate value for a
3974 formal of type FORMAL_TYPE. Use *SP as a stack pointer for
3975 allocating any necessary descriptors (fat pointers), or copies of
3976 values not residing in memory, updating it as needed. */
3979 ada_convert_actual (struct value *actual, struct type *formal_type0,
3980 struct gdbarch *gdbarch, CORE_ADDR *sp)
3982 struct type *actual_type = ada_check_typedef (value_type (actual));
3983 struct type *formal_type = ada_check_typedef (formal_type0);
3984 struct type *formal_target =
3985 TYPE_CODE (formal_type) == TYPE_CODE_PTR
3986 ? ada_check_typedef (TYPE_TARGET_TYPE (formal_type)) : formal_type;
3987 struct type *actual_target =
3988 TYPE_CODE (actual_type) == TYPE_CODE_PTR
3989 ? ada_check_typedef (TYPE_TARGET_TYPE (actual_type)) : actual_type;
3991 if (ada_is_array_descriptor_type (formal_target)
3992 && TYPE_CODE (actual_target) == TYPE_CODE_ARRAY)
3993 return make_array_descriptor (formal_type, actual, gdbarch, sp);
3994 else if (TYPE_CODE (formal_type) == TYPE_CODE_PTR
3995 || TYPE_CODE (formal_type) == TYPE_CODE_REF)
3997 struct value *result;
3999 if (TYPE_CODE (formal_target) == TYPE_CODE_ARRAY
4000 && ada_is_array_descriptor_type (actual_target))
4001 result = desc_data (actual);
4002 else if (TYPE_CODE (actual_type) != TYPE_CODE_PTR)
4004 if (VALUE_LVAL (actual) != lval_memory)
4008 actual_type = ada_check_typedef (value_type (actual));
4009 val = allocate_value (actual_type);
4010 memcpy ((char *) value_contents_raw (val),
4011 (char *) value_contents (actual),
4012 TYPE_LENGTH (actual_type));
4013 actual = ensure_lval (val, gdbarch, sp);
4015 result = value_addr (actual);
4019 return value_cast_pointers (formal_type, result);
4021 else if (TYPE_CODE (actual_type) == TYPE_CODE_PTR)
4022 return ada_value_ind (actual);
4027 /* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
4028 type TYPE. This is usually an inefficient no-op except on some targets
4029 (such as AVR) where the representation of a pointer and an address
4033 value_pointer (struct value *value, struct type *type)
4035 struct gdbarch *gdbarch = get_type_arch (type);
4036 unsigned len = TYPE_LENGTH (type);
4037 gdb_byte *buf = alloca (len);
4040 addr = value_address (value);
4041 gdbarch_address_to_pointer (gdbarch, type, buf, addr);
4042 addr = extract_unsigned_integer (buf, len, gdbarch_byte_order (gdbarch));
4047 /* Push a descriptor of type TYPE for array value ARR on the stack at
4048 *SP, updating *SP to reflect the new descriptor. Return either
4049 an lvalue representing the new descriptor, or (if TYPE is a pointer-
4050 to-descriptor type rather than a descriptor type), a struct value *
4051 representing a pointer to this descriptor. */
4053 static struct value *
4054 make_array_descriptor (struct type *type, struct value *arr,
4055 struct gdbarch *gdbarch, CORE_ADDR *sp)
4057 struct type *bounds_type = desc_bounds_type (type);
4058 struct type *desc_type = desc_base_type (type);
4059 struct value *descriptor = allocate_value (desc_type);
4060 struct value *bounds = allocate_value (bounds_type);
4063 for (i = ada_array_arity (ada_check_typedef (value_type (arr))); i > 0; i -= 1)
4065 modify_general_field (value_type (bounds),
4066 value_contents_writeable (bounds),
4067 ada_array_bound (arr, i, 0),
4068 desc_bound_bitpos (bounds_type, i, 0),
4069 desc_bound_bitsize (bounds_type, i, 0));
4070 modify_general_field (value_type (bounds),
4071 value_contents_writeable (bounds),
4072 ada_array_bound (arr, i, 1),
4073 desc_bound_bitpos (bounds_type, i, 1),
4074 desc_bound_bitsize (bounds_type, i, 1));
4077 bounds = ensure_lval (bounds, gdbarch, sp);
4079 modify_general_field (value_type (descriptor),
4080 value_contents_writeable (descriptor),
4081 value_pointer (ensure_lval (arr, gdbarch, sp),
4082 TYPE_FIELD_TYPE (desc_type, 0)),
4083 fat_pntr_data_bitpos (desc_type),
4084 fat_pntr_data_bitsize (desc_type));
4086 modify_general_field (value_type (descriptor),
4087 value_contents_writeable (descriptor),
4088 value_pointer (bounds,
4089 TYPE_FIELD_TYPE (desc_type, 1)),
4090 fat_pntr_bounds_bitpos (desc_type),
4091 fat_pntr_bounds_bitsize (desc_type));
4093 descriptor = ensure_lval (descriptor, gdbarch, sp);
4095 if (TYPE_CODE (type) == TYPE_CODE_PTR)
4096 return value_addr (descriptor);
4101 /* Dummy definitions for an experimental caching module that is not
4102 * used in the public sources. */
4105 lookup_cached_symbol (const char *name, domain_enum namespace,
4106 struct symbol **sym, struct block **block)
4112 cache_symbol (const char *name, domain_enum namespace, struct symbol *sym,
4113 struct block *block)
4119 /* Return the result of a standard (literal, C-like) lookup of NAME in
4120 given DOMAIN, visible from lexical block BLOCK. */
4122 static struct symbol *
4123 standard_lookup (const char *name, const struct block *block,
4128 if (lookup_cached_symbol (name, domain, &sym, NULL))
4130 sym = lookup_symbol_in_language (name, block, domain, language_c, 0);
4131 cache_symbol (name, domain, sym, block_found);
4136 /* Non-zero iff there is at least one non-function/non-enumeral symbol
4137 in the symbol fields of SYMS[0..N-1]. We treat enumerals as functions,
4138 since they contend in overloading in the same way. */
4140 is_nonfunction (struct ada_symbol_info syms[], int n)
4144 for (i = 0; i < n; i += 1)
4145 if (TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) != TYPE_CODE_FUNC
4146 && (TYPE_CODE (SYMBOL_TYPE (syms[i].sym)) != TYPE_CODE_ENUM
4147 || SYMBOL_CLASS (syms[i].sym) != LOC_CONST))
4153 /* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
4154 struct types. Otherwise, they may not. */
4157 equiv_types (struct type *type0, struct type *type1)
4161 if (type0 == NULL || type1 == NULL
4162 || TYPE_CODE (type0) != TYPE_CODE (type1))
4164 if ((TYPE_CODE (type0) == TYPE_CODE_STRUCT
4165 || TYPE_CODE (type0) == TYPE_CODE_ENUM)
4166 && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
4167 && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
4173 /* True iff SYM0 represents the same entity as SYM1, or one that is
4174 no more defined than that of SYM1. */
4177 lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
4181 if (SYMBOL_DOMAIN (sym0) != SYMBOL_DOMAIN (sym1)
4182 || SYMBOL_CLASS (sym0) != SYMBOL_CLASS (sym1))
4185 switch (SYMBOL_CLASS (sym0))
4191 struct type *type0 = SYMBOL_TYPE (sym0);
4192 struct type *type1 = SYMBOL_TYPE (sym1);
4193 char *name0 = SYMBOL_LINKAGE_NAME (sym0);
4194 char *name1 = SYMBOL_LINKAGE_NAME (sym1);
4195 int len0 = strlen (name0);
4198 TYPE_CODE (type0) == TYPE_CODE (type1)
4199 && (equiv_types (type0, type1)
4200 || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
4201 && strncmp (name1 + len0, "___XV", 5) == 0));
4204 return SYMBOL_VALUE (sym0) == SYMBOL_VALUE (sym1)
4205 && equiv_types (SYMBOL_TYPE (sym0), SYMBOL_TYPE (sym1));
4211 /* Append (SYM,BLOCK,SYMTAB) to the end of the array of struct ada_symbol_info
4212 records in OBSTACKP. Do nothing if SYM is a duplicate. */
4215 add_defn_to_vec (struct obstack *obstackp,
4217 struct block *block)
4220 struct ada_symbol_info *prevDefns = defns_collected (obstackp, 0);
4222 /* Do not try to complete stub types, as the debugger is probably
4223 already scanning all symbols matching a certain name at the
4224 time when this function is called. Trying to replace the stub
4225 type by its associated full type will cause us to restart a scan
4226 which may lead to an infinite recursion. Instead, the client
4227 collecting the matching symbols will end up collecting several
4228 matches, with at least one of them complete. It can then filter
4229 out the stub ones if needed. */
4231 for (i = num_defns_collected (obstackp) - 1; i >= 0; i -= 1)
4233 if (lesseq_defined_than (sym, prevDefns[i].sym))
4235 else if (lesseq_defined_than (prevDefns[i].sym, sym))
4237 prevDefns[i].sym = sym;
4238 prevDefns[i].block = block;
4244 struct ada_symbol_info info;
4248 obstack_grow (obstackp, &info, sizeof (struct ada_symbol_info));
4252 /* Number of ada_symbol_info structures currently collected in
4253 current vector in *OBSTACKP. */
4256 num_defns_collected (struct obstack *obstackp)
4258 return obstack_object_size (obstackp) / sizeof (struct ada_symbol_info);
4261 /* Vector of ada_symbol_info structures currently collected in current
4262 vector in *OBSTACKP. If FINISH, close off the vector and return
4263 its final address. */
4265 static struct ada_symbol_info *
4266 defns_collected (struct obstack *obstackp, int finish)
4269 return obstack_finish (obstackp);
4271 return (struct ada_symbol_info *) obstack_base (obstackp);
4274 /* Return a minimal symbol matching NAME according to Ada decoding
4275 rules. Returns NULL if there is no such minimal symbol. Names
4276 prefixed with "standard__" are handled specially: "standard__" is
4277 first stripped off, and only static and global symbols are searched. */
4279 struct minimal_symbol *
4280 ada_lookup_simple_minsym (const char *name)
4282 struct objfile *objfile;
4283 struct minimal_symbol *msymbol;
4286 if (strncmp (name, "standard__", sizeof ("standard__") - 1) == 0)
4288 name += sizeof ("standard__") - 1;
4292 wild_match = (strstr (name, "__") == NULL);
4294 ALL_MSYMBOLS (objfile, msymbol)
4296 if (ada_match_name (SYMBOL_LINKAGE_NAME (msymbol), name, wild_match)
4297 && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
4304 /* For all subprograms that statically enclose the subprogram of the
4305 selected frame, add symbols matching identifier NAME in DOMAIN
4306 and their blocks to the list of data in OBSTACKP, as for
4307 ada_add_block_symbols (q.v.). If WILD, treat as NAME with a
4311 add_symbols_from_enclosing_procs (struct obstack *obstackp,
4312 const char *name, domain_enum namespace,
4317 /* True if TYPE is definitely an artificial type supplied to a symbol
4318 for which no debugging information was given in the symbol file. */
4321 is_nondebugging_type (struct type *type)
4323 char *name = ada_type_name (type);
4325 return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
4328 /* Remove any non-debugging symbols in SYMS[0 .. NSYMS-1] that definitely
4329 duplicate other symbols in the list (The only case I know of where
4330 this happens is when object files containing stabs-in-ecoff are
4331 linked with files containing ordinary ecoff debugging symbols (or no
4332 debugging symbols)). Modifies SYMS to squeeze out deleted entries.
4333 Returns the number of items in the modified list. */
4336 remove_extra_symbols (struct ada_symbol_info *syms, int nsyms)
4345 /* If two symbols have the same name and one of them is a stub type,
4346 the get rid of the stub. */
4348 if (TYPE_STUB (SYMBOL_TYPE (syms[i].sym))
4349 && SYMBOL_LINKAGE_NAME (syms[i].sym) != NULL)
4351 for (j = 0; j < nsyms; j++)
4354 && !TYPE_STUB (SYMBOL_TYPE (syms[j].sym))
4355 && SYMBOL_LINKAGE_NAME (syms[j].sym) != NULL
4356 && strcmp (SYMBOL_LINKAGE_NAME (syms[i].sym),
4357 SYMBOL_LINKAGE_NAME (syms[j].sym)) == 0)
4362 /* Two symbols with the same name, same class and same address
4363 should be identical. */
4365 else if (SYMBOL_LINKAGE_NAME (syms[i].sym) != NULL
4366 && SYMBOL_CLASS (syms[i].sym) == LOC_STATIC
4367 && is_nondebugging_type (SYMBOL_TYPE (syms[i].sym)))
4369 for (j = 0; j < nsyms; j += 1)
4372 && SYMBOL_LINKAGE_NAME (syms[j].sym) != NULL
4373 && strcmp (SYMBOL_LINKAGE_NAME (syms[i].sym),
4374 SYMBOL_LINKAGE_NAME (syms[j].sym)) == 0
4375 && SYMBOL_CLASS (syms[i].sym) == SYMBOL_CLASS (syms[j].sym)
4376 && SYMBOL_VALUE_ADDRESS (syms[i].sym)
4377 == SYMBOL_VALUE_ADDRESS (syms[j].sym))
4384 for (j = i + 1; j < nsyms; j += 1)
4385 syms[j - 1] = syms[j];
4394 /* Given a type that corresponds to a renaming entity, use the type name
4395 to extract the scope (package name or function name, fully qualified,
4396 and following the GNAT encoding convention) where this renaming has been
4397 defined. The string returned needs to be deallocated after use. */
4400 xget_renaming_scope (struct type *renaming_type)
4402 /* The renaming types adhere to the following convention:
4403 <scope>__<rename>___<XR extension>.
4404 So, to extract the scope, we search for the "___XR" extension,
4405 and then backtrack until we find the first "__". */
4407 const char *name = type_name_no_tag (renaming_type);
4408 char *suffix = strstr (name, "___XR");
4413 /* Now, backtrack a bit until we find the first "__". Start looking
4414 at suffix - 3, as the <rename> part is at least one character long. */
4416 for (last = suffix - 3; last > name; last--)
4417 if (last[0] == '_' && last[1] == '_')
4420 /* Make a copy of scope and return it. */
4422 scope_len = last - name;
4423 scope = (char *) xmalloc ((scope_len + 1) * sizeof (char));
4425 strncpy (scope, name, scope_len);
4426 scope[scope_len] = '\0';
4431 /* Return nonzero if NAME corresponds to a package name. */
4434 is_package_name (const char *name)
4436 /* Here, We take advantage of the fact that no symbols are generated
4437 for packages, while symbols are generated for each function.
4438 So the condition for NAME represent a package becomes equivalent
4439 to NAME not existing in our list of symbols. There is only one
4440 small complication with library-level functions (see below). */
4444 /* If it is a function that has not been defined at library level,
4445 then we should be able to look it up in the symbols. */
4446 if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
4449 /* Library-level function names start with "_ada_". See if function
4450 "_ada_" followed by NAME can be found. */
4452 /* Do a quick check that NAME does not contain "__", since library-level
4453 functions names cannot contain "__" in them. */
4454 if (strstr (name, "__") != NULL)
4457 fun_name = xstrprintf ("_ada_%s", name);
4459 return (standard_lookup (fun_name, NULL, VAR_DOMAIN) == NULL);
4462 /* Return nonzero if SYM corresponds to a renaming entity that is
4463 not visible from FUNCTION_NAME. */
4466 old_renaming_is_invisible (const struct symbol *sym, char *function_name)
4470 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
4473 scope = xget_renaming_scope (SYMBOL_TYPE (sym));
4475 make_cleanup (xfree, scope);
4477 /* If the rename has been defined in a package, then it is visible. */
4478 if (is_package_name (scope))
4481 /* Check that the rename is in the current function scope by checking
4482 that its name starts with SCOPE. */
4484 /* If the function name starts with "_ada_", it means that it is
4485 a library-level function. Strip this prefix before doing the
4486 comparison, as the encoding for the renaming does not contain
4488 if (strncmp (function_name, "_ada_", 5) == 0)
4491 return (strncmp (function_name, scope, strlen (scope)) != 0);
4494 /* Remove entries from SYMS that corresponds to a renaming entity that
4495 is not visible from the function associated with CURRENT_BLOCK or
4496 that is superfluous due to the presence of more specific renaming
4497 information. Places surviving symbols in the initial entries of
4498 SYMS and returns the number of surviving symbols.
4501 First, in cases where an object renaming is implemented as a
4502 reference variable, GNAT may produce both the actual reference
4503 variable and the renaming encoding. In this case, we discard the
4506 Second, GNAT emits a type following a specified encoding for each renaming
4507 entity. Unfortunately, STABS currently does not support the definition
4508 of types that are local to a given lexical block, so all renamings types
4509 are emitted at library level. As a consequence, if an application
4510 contains two renaming entities using the same name, and a user tries to
4511 print the value of one of these entities, the result of the ada symbol
4512 lookup will also contain the wrong renaming type.
4514 This function partially covers for this limitation by attempting to
4515 remove from the SYMS list renaming symbols that should be visible
4516 from CURRENT_BLOCK. However, there does not seem be a 100% reliable
4517 method with the current information available. The implementation
4518 below has a couple of limitations (FIXME: brobecker-2003-05-12):
4520 - When the user tries to print a rename in a function while there
4521 is another rename entity defined in a package: Normally, the
4522 rename in the function has precedence over the rename in the
4523 package, so the latter should be removed from the list. This is
4524 currently not the case.
4526 - This function will incorrectly remove valid renames if
4527 the CURRENT_BLOCK corresponds to a function which symbol name
4528 has been changed by an "Export" pragma. As a consequence,
4529 the user will be unable to print such rename entities. */
4532 remove_irrelevant_renamings (struct ada_symbol_info *syms,
4533 int nsyms, const struct block *current_block)
4535 struct symbol *current_function;
4536 char *current_function_name;
4538 int is_new_style_renaming;
4540 /* If there is both a renaming foo___XR... encoded as a variable and
4541 a simple variable foo in the same block, discard the latter.
4542 First, zero out such symbols, then compress. */
4543 is_new_style_renaming = 0;
4544 for (i = 0; i < nsyms; i += 1)
4546 struct symbol *sym = syms[i].sym;
4547 struct block *block = syms[i].block;
4551 if (sym == NULL || SYMBOL_CLASS (sym) == LOC_TYPEDEF)
4553 name = SYMBOL_LINKAGE_NAME (sym);
4554 suffix = strstr (name, "___XR");
4558 int name_len = suffix - name;
4561 is_new_style_renaming = 1;
4562 for (j = 0; j < nsyms; j += 1)
4563 if (i != j && syms[j].sym != NULL
4564 && strncmp (name, SYMBOL_LINKAGE_NAME (syms[j].sym),
4566 && block == syms[j].block)
4570 if (is_new_style_renaming)
4574 for (j = k = 0; j < nsyms; j += 1)
4575 if (syms[j].sym != NULL)
4583 /* Extract the function name associated to CURRENT_BLOCK.
4584 Abort if unable to do so. */
4586 if (current_block == NULL)
4589 current_function = block_linkage_function (current_block);
4590 if (current_function == NULL)
4593 current_function_name = SYMBOL_LINKAGE_NAME (current_function);
4594 if (current_function_name == NULL)
4597 /* Check each of the symbols, and remove it from the list if it is
4598 a type corresponding to a renaming that is out of the scope of
4599 the current block. */
4604 if (ada_parse_renaming (syms[i].sym, NULL, NULL, NULL)
4605 == ADA_OBJECT_RENAMING
4606 && old_renaming_is_invisible (syms[i].sym, current_function_name))
4610 for (j = i + 1; j < nsyms; j += 1)
4611 syms[j - 1] = syms[j];
4621 /* Add to OBSTACKP all symbols from BLOCK (and its super-blocks)
4622 whose name and domain match NAME and DOMAIN respectively.
4623 If no match was found, then extend the search to "enclosing"
4624 routines (in other words, if we're inside a nested function,
4625 search the symbols defined inside the enclosing functions).
4627 Note: This function assumes that OBSTACKP has 0 (zero) element in it. */
4630 ada_add_local_symbols (struct obstack *obstackp, const char *name,
4631 struct block *block, domain_enum domain,
4634 int block_depth = 0;
4636 while (block != NULL)
4639 ada_add_block_symbols (obstackp, block, name, domain, NULL, wild_match);
4641 /* If we found a non-function match, assume that's the one. */
4642 if (is_nonfunction (defns_collected (obstackp, 0),
4643 num_defns_collected (obstackp)))
4646 block = BLOCK_SUPERBLOCK (block);
4649 /* If no luck so far, try to find NAME as a local symbol in some lexically
4650 enclosing subprogram. */
4651 if (num_defns_collected (obstackp) == 0 && block_depth > 2)
4652 add_symbols_from_enclosing_procs (obstackp, name, domain, wild_match);
4655 /* An object of this type is used as the user_data argument when
4656 calling the map_ada_symtabs method. */
4658 struct ada_psym_data
4660 struct obstack *obstackp;
4667 /* Callback function for map_ada_symtabs. */
4670 ada_add_psyms (struct objfile *objfile, struct symtab *s, void *user_data)
4672 struct ada_psym_data *data = user_data;
4673 const int block_kind = data->global ? GLOBAL_BLOCK : STATIC_BLOCK;
4675 ada_add_block_symbols (data->obstackp,
4676 BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), block_kind),
4677 data->name, data->domain, objfile, data->wild_match);
4680 /* Add to OBSTACKP all non-local symbols whose name and domain match
4681 NAME and DOMAIN respectively. The search is performed on GLOBAL_BLOCK
4682 symbols if GLOBAL is non-zero, or on STATIC_BLOCK symbols otherwise. */
4685 ada_add_non_local_symbols (struct obstack *obstackp, const char *name,
4686 domain_enum domain, int global,
4689 struct objfile *objfile;
4690 struct ada_psym_data data;
4692 data.obstackp = obstackp;
4694 data.domain = domain;
4695 data.global = global;
4696 data.wild_match = is_wild_match;
4698 ALL_OBJFILES (objfile)
4701 objfile->sf->qf->map_ada_symtabs (objfile, wild_match, is_name_suffix,
4702 ada_add_psyms, name,
4704 is_wild_match, &data);
4708 /* Find symbols in DOMAIN matching NAME0, in BLOCK0 and enclosing
4709 scope and in global scopes, returning the number of matches. Sets
4710 *RESULTS to point to a vector of (SYM,BLOCK) tuples,
4711 indicating the symbols found and the blocks and symbol tables (if
4712 any) in which they were found. This vector are transient---good only to
4713 the next call of ada_lookup_symbol_list. Any non-function/non-enumeral
4714 symbol match within the nest of blocks whose innermost member is BLOCK0,
4715 is the one match returned (no other matches in that or
4716 enclosing blocks is returned). If there are any matches in or
4717 surrounding BLOCK0, then these alone are returned. Otherwise, the
4718 search extends to global and file-scope (static) symbol tables.
4719 Names prefixed with "standard__" are handled specially: "standard__"
4720 is first stripped off, and only static and global symbols are searched. */
4723 ada_lookup_symbol_list (const char *name0, const struct block *block0,
4724 domain_enum namespace,
4725 struct ada_symbol_info **results)
4728 struct block *block;
4734 obstack_free (&symbol_list_obstack, NULL);
4735 obstack_init (&symbol_list_obstack);
4739 /* Search specified block and its superiors. */
4741 wild_match = (strstr (name0, "__") == NULL);
4743 block = (struct block *) block0; /* FIXME: No cast ought to be
4744 needed, but adding const will
4745 have a cascade effect. */
4747 /* Special case: If the user specifies a symbol name inside package
4748 Standard, do a non-wild matching of the symbol name without
4749 the "standard__" prefix. This was primarily introduced in order
4750 to allow the user to specifically access the standard exceptions
4751 using, for instance, Standard.Constraint_Error when Constraint_Error
4752 is ambiguous (due to the user defining its own Constraint_Error
4753 entity inside its program). */
4754 if (strncmp (name0, "standard__", sizeof ("standard__") - 1) == 0)
4758 name = name0 + sizeof ("standard__") - 1;
4761 /* Check the non-global symbols. If we have ANY match, then we're done. */
4763 ada_add_local_symbols (&symbol_list_obstack, name, block, namespace,
4765 if (num_defns_collected (&symbol_list_obstack) > 0)
4768 /* No non-global symbols found. Check our cache to see if we have
4769 already performed this search before. If we have, then return
4773 if (lookup_cached_symbol (name0, namespace, &sym, &block))
4776 add_defn_to_vec (&symbol_list_obstack, sym, block);
4780 /* Search symbols from all global blocks. */
4782 ada_add_non_local_symbols (&symbol_list_obstack, name, namespace, 1,
4785 /* Now add symbols from all per-file blocks if we've gotten no hits
4786 (not strictly correct, but perhaps better than an error). */
4788 if (num_defns_collected (&symbol_list_obstack) == 0)
4789 ada_add_non_local_symbols (&symbol_list_obstack, name, namespace, 0,
4793 ndefns = num_defns_collected (&symbol_list_obstack);
4794 *results = defns_collected (&symbol_list_obstack, 1);
4796 ndefns = remove_extra_symbols (*results, ndefns);
4799 cache_symbol (name0, namespace, NULL, NULL);
4801 if (ndefns == 1 && cacheIfUnique)
4802 cache_symbol (name0, namespace, (*results)[0].sym, (*results)[0].block);
4804 ndefns = remove_irrelevant_renamings (*results, ndefns, block0);
4810 ada_lookup_encoded_symbol (const char *name, const struct block *block0,
4811 domain_enum namespace, struct block **block_found)
4813 struct ada_symbol_info *candidates;
4816 n_candidates = ada_lookup_symbol_list (name, block0, namespace, &candidates);
4818 if (n_candidates == 0)
4821 if (block_found != NULL)
4822 *block_found = candidates[0].block;
4824 return fixup_symbol_section (candidates[0].sym, NULL);
4827 /* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
4828 scope and in global scopes, or NULL if none. NAME is folded and
4829 encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
4830 choosing the first symbol if there are multiple choices.
4831 *IS_A_FIELD_OF_THIS is set to 0 and *SYMTAB is set to the symbol
4832 table in which the symbol was found (in both cases, these
4833 assignments occur only if the pointers are non-null). */
4835 ada_lookup_symbol (const char *name, const struct block *block0,
4836 domain_enum namespace, int *is_a_field_of_this)
4838 if (is_a_field_of_this != NULL)
4839 *is_a_field_of_this = 0;
4842 ada_lookup_encoded_symbol (ada_encode (ada_fold_name (name)),
4843 block0, namespace, NULL);
4846 static struct symbol *
4847 ada_lookup_symbol_nonlocal (const char *name,
4848 const struct block *block,
4849 const domain_enum domain)
4851 return ada_lookup_symbol (name, block_static_block (block), domain, NULL);
4855 /* True iff STR is a possible encoded suffix of a normal Ada name
4856 that is to be ignored for matching purposes. Suffixes of parallel
4857 names (e.g., XVE) are not included here. Currently, the possible suffixes
4858 are given by any of the regular expressions:
4860 [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
4861 ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
4862 _E[0-9]+[bs]$ [protected object entry suffixes]
4863 (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
4865 Also, any leading "__[0-9]+" sequence is skipped before the suffix
4866 match is performed. This sequence is used to differentiate homonyms,
4867 is an optional part of a valid name suffix. */
4870 is_name_suffix (const char *str)
4873 const char *matching;
4874 const int len = strlen (str);
4876 /* Skip optional leading __[0-9]+. */
4878 if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
4881 while (isdigit (str[0]))
4887 if (str[0] == '.' || str[0] == '$')
4890 while (isdigit (matching[0]))
4892 if (matching[0] == '\0')
4898 if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
4901 while (isdigit (matching[0]))
4903 if (matching[0] == '\0')
4908 /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
4909 with a N at the end. Unfortunately, the compiler uses the same
4910 convention for other internal types it creates. So treating
4911 all entity names that end with an "N" as a name suffix causes
4912 some regressions. For instance, consider the case of an enumerated
4913 type. To support the 'Image attribute, it creates an array whose
4915 Having a single character like this as a suffix carrying some
4916 information is a bit risky. Perhaps we should change the encoding
4917 to be something like "_N" instead. In the meantime, do not do
4918 the following check. */
4919 /* Protected Object Subprograms */
4920 if (len == 1 && str [0] == 'N')
4925 if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
4928 while (isdigit (matching[0]))
4930 if ((matching[0] == 'b' || matching[0] == 's')
4931 && matching [1] == '\0')
4935 /* ??? We should not modify STR directly, as we are doing below. This
4936 is fine in this case, but may become problematic later if we find
4937 that this alternative did not work, and want to try matching
4938 another one from the begining of STR. Since we modified it, we
4939 won't be able to find the begining of the string anymore! */
4943 while (str[0] != '_' && str[0] != '\0')
4945 if (str[0] != 'n' && str[0] != 'b')
4951 if (str[0] == '\000')
4956 if (str[1] != '_' || str[2] == '\000')
4960 if (strcmp (str + 3, "JM") == 0)
4962 /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
4963 the LJM suffix in favor of the JM one. But we will
4964 still accept LJM as a valid suffix for a reasonable
4965 amount of time, just to allow ourselves to debug programs
4966 compiled using an older version of GNAT. */
4967 if (strcmp (str + 3, "LJM") == 0)
4971 if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
4972 || str[4] == 'U' || str[4] == 'P')
4974 if (str[4] == 'R' && str[5] != 'T')
4978 if (!isdigit (str[2]))
4980 for (k = 3; str[k] != '\0'; k += 1)
4981 if (!isdigit (str[k]) && str[k] != '_')
4985 if (str[0] == '$' && isdigit (str[1]))
4987 for (k = 2; str[k] != '\0'; k += 1)
4988 if (!isdigit (str[k]) && str[k] != '_')
4995 /* Return non-zero if the string starting at NAME and ending before
4996 NAME_END contains no capital letters. */
4999 is_valid_name_for_wild_match (const char *name0)
5001 const char *decoded_name = ada_decode (name0);
5004 /* If the decoded name starts with an angle bracket, it means that
5005 NAME0 does not follow the GNAT encoding format. It should then
5006 not be allowed as a possible wild match. */
5007 if (decoded_name[0] == '<')
5010 for (i=0; decoded_name[i] != '\0'; i++)
5011 if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
5017 /* Advance *NAMEP to next occurrence of TARGET0 in the string NAME0
5018 that could start a simple name. Assumes that *NAMEP points into
5019 the string beginning at NAME0. */
5022 advance_wild_match (const char **namep, const char *name0, int target0)
5024 const char *name = *namep;
5034 if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
5037 if (name == name0 + 5 && strncmp (name0, "_ada", 4) == 0)
5042 else if (t1 == '_' &&
5043 (((t2 = name[2]) >= 'a' && t2 <= 'z') || t2 == target0))
5051 else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
5061 /* Return 0 iff NAME encodes a name of the form prefix.PATN. Ignores any
5062 informational suffixes of NAME (i.e., for which is_name_suffix is
5063 true). Assumes that PATN is a lower-cased Ada simple name. */
5066 wild_match (const char *name, const char *patn)
5069 const char *name0 = name;
5073 const char *match = name;
5077 for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
5080 if (*p == '\0' && is_name_suffix (name))
5081 return match != name0 && !is_valid_name_for_wild_match (name0);
5083 if (name[-1] == '_')
5086 if (!advance_wild_match (&name, name0, *patn))
5091 /* Add symbols from BLOCK matching identifier NAME in DOMAIN to
5092 vector *defn_symbols, updating the list of symbols in OBSTACKP
5093 (if necessary). If WILD, treat as NAME with a wildcard prefix.
5094 OBJFILE is the section containing BLOCK.
5095 SYMTAB is recorded with each symbol added. */
5098 ada_add_block_symbols (struct obstack *obstackp,
5099 struct block *block, const char *name,
5100 domain_enum domain, struct objfile *objfile,
5103 struct dict_iterator iter;
5104 int name_len = strlen (name);
5105 /* A matching argument symbol, if any. */
5106 struct symbol *arg_sym;
5107 /* Set true when we find a matching non-argument symbol. */
5117 ALL_BLOCK_SYMBOLS (block, iter, sym)
5119 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
5120 SYMBOL_DOMAIN (sym), domain)
5121 && wild_match (SYMBOL_LINKAGE_NAME (sym), name) == 0)
5123 if (SYMBOL_CLASS (sym) == LOC_UNRESOLVED)
5125 else if (SYMBOL_IS_ARGUMENT (sym))
5130 add_defn_to_vec (obstackp,
5131 fixup_symbol_section (sym, objfile),
5139 ALL_BLOCK_SYMBOLS (block, iter, sym)
5141 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
5142 SYMBOL_DOMAIN (sym), domain))
5144 int cmp = strncmp (name, SYMBOL_LINKAGE_NAME (sym), name_len);
5147 && is_name_suffix (SYMBOL_LINKAGE_NAME (sym) + name_len))
5149 if (SYMBOL_CLASS (sym) != LOC_UNRESOLVED)
5151 if (SYMBOL_IS_ARGUMENT (sym))
5156 add_defn_to_vec (obstackp,
5157 fixup_symbol_section (sym, objfile),
5166 if (!found_sym && arg_sym != NULL)
5168 add_defn_to_vec (obstackp,
5169 fixup_symbol_section (arg_sym, objfile),
5178 ALL_BLOCK_SYMBOLS (block, iter, sym)
5180 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
5181 SYMBOL_DOMAIN (sym), domain))
5185 cmp = (int) '_' - (int) SYMBOL_LINKAGE_NAME (sym)[0];
5188 cmp = strncmp ("_ada_", SYMBOL_LINKAGE_NAME (sym), 5);
5190 cmp = strncmp (name, SYMBOL_LINKAGE_NAME (sym) + 5,
5195 && is_name_suffix (SYMBOL_LINKAGE_NAME (sym) + name_len + 5))
5197 if (SYMBOL_CLASS (sym) != LOC_UNRESOLVED)
5199 if (SYMBOL_IS_ARGUMENT (sym))
5204 add_defn_to_vec (obstackp,
5205 fixup_symbol_section (sym, objfile),
5213 /* NOTE: This really shouldn't be needed for _ada_ symbols.
5214 They aren't parameters, right? */
5215 if (!found_sym && arg_sym != NULL)
5217 add_defn_to_vec (obstackp,
5218 fixup_symbol_section (arg_sym, objfile),
5225 /* Symbol Completion */
5227 /* If SYM_NAME is a completion candidate for TEXT, return this symbol
5228 name in a form that's appropriate for the completion. The result
5229 does not need to be deallocated, but is only good until the next call.
5231 TEXT_LEN is equal to the length of TEXT.
5232 Perform a wild match if WILD_MATCH is set.
5233 ENCODED should be set if TEXT represents the start of a symbol name
5234 in its encoded form. */
5237 symbol_completion_match (const char *sym_name,
5238 const char *text, int text_len,
5239 int wild_match, int encoded)
5241 const int verbatim_match = (text[0] == '<');
5246 /* Strip the leading angle bracket. */
5251 /* First, test against the fully qualified name of the symbol. */
5253 if (strncmp (sym_name, text, text_len) == 0)
5256 if (match && !encoded)
5258 /* One needed check before declaring a positive match is to verify
5259 that iff we are doing a verbatim match, the decoded version
5260 of the symbol name starts with '<'. Otherwise, this symbol name
5261 is not a suitable completion. */
5262 const char *sym_name_copy = sym_name;
5263 int has_angle_bracket;
5265 sym_name = ada_decode (sym_name);
5266 has_angle_bracket = (sym_name[0] == '<');
5267 match = (has_angle_bracket == verbatim_match);
5268 sym_name = sym_name_copy;
5271 if (match && !verbatim_match)
5273 /* When doing non-verbatim match, another check that needs to
5274 be done is to verify that the potentially matching symbol name
5275 does not include capital letters, because the ada-mode would
5276 not be able to understand these symbol names without the
5277 angle bracket notation. */
5280 for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
5285 /* Second: Try wild matching... */
5287 if (!match && wild_match)
5289 /* Since we are doing wild matching, this means that TEXT
5290 may represent an unqualified symbol name. We therefore must
5291 also compare TEXT against the unqualified name of the symbol. */
5292 sym_name = ada_unqualified_name (ada_decode (sym_name));
5294 if (strncmp (sym_name, text, text_len) == 0)
5298 /* Finally: If we found a mach, prepare the result to return. */
5304 sym_name = add_angle_brackets (sym_name);
5307 sym_name = ada_decode (sym_name);
5312 DEF_VEC_P (char_ptr);
5314 /* A companion function to ada_make_symbol_completion_list().
5315 Check if SYM_NAME represents a symbol which name would be suitable
5316 to complete TEXT (TEXT_LEN is the length of TEXT), in which case
5317 it is appended at the end of the given string vector SV.
5319 ORIG_TEXT is the string original string from the user command
5320 that needs to be completed. WORD is the entire command on which
5321 completion should be performed. These two parameters are used to
5322 determine which part of the symbol name should be added to the
5324 if WILD_MATCH is set, then wild matching is performed.
5325 ENCODED should be set if TEXT represents a symbol name in its
5326 encoded formed (in which case the completion should also be
5330 symbol_completion_add (VEC(char_ptr) **sv,
5331 const char *sym_name,
5332 const char *text, int text_len,
5333 const char *orig_text, const char *word,
5334 int wild_match, int encoded)
5336 const char *match = symbol_completion_match (sym_name, text, text_len,
5337 wild_match, encoded);
5343 /* We found a match, so add the appropriate completion to the given
5346 if (word == orig_text)
5348 completion = xmalloc (strlen (match) + 5);
5349 strcpy (completion, match);
5351 else if (word > orig_text)
5353 /* Return some portion of sym_name. */
5354 completion = xmalloc (strlen (match) + 5);
5355 strcpy (completion, match + (word - orig_text));
5359 /* Return some of ORIG_TEXT plus sym_name. */
5360 completion = xmalloc (strlen (match) + (orig_text - word) + 5);
5361 strncpy (completion, word, orig_text - word);
5362 completion[orig_text - word] = '\0';
5363 strcat (completion, match);
5366 VEC_safe_push (char_ptr, *sv, completion);
5369 /* An object of this type is passed as the user_data argument to the
5370 map_partial_symbol_names method. */
5371 struct add_partial_datum
5373 VEC(char_ptr) **completions;
5382 /* A callback for map_partial_symbol_names. */
5384 ada_add_partial_symbol_completions (const char *name, void *user_data)
5386 struct add_partial_datum *data = user_data;
5388 symbol_completion_add (data->completions, name,
5389 data->text, data->text_len, data->text0, data->word,
5390 data->wild_match, data->encoded);
5393 /* Return a list of possible symbol names completing TEXT0. The list
5394 is NULL terminated. WORD is the entire command on which completion
5398 ada_make_symbol_completion_list (char *text0, char *word)
5404 VEC(char_ptr) *completions = VEC_alloc (char_ptr, 128);
5407 struct minimal_symbol *msymbol;
5408 struct objfile *objfile;
5409 struct block *b, *surrounding_static_block = 0;
5411 struct dict_iterator iter;
5413 if (text0[0] == '<')
5415 text = xstrdup (text0);
5416 make_cleanup (xfree, text);
5417 text_len = strlen (text);
5423 text = xstrdup (ada_encode (text0));
5424 make_cleanup (xfree, text);
5425 text_len = strlen (text);
5426 for (i = 0; i < text_len; i++)
5427 text[i] = tolower (text[i]);
5429 encoded = (strstr (text0, "__") != NULL);
5430 /* If the name contains a ".", then the user is entering a fully
5431 qualified entity name, and the match must not be done in wild
5432 mode. Similarly, if the user wants to complete what looks like
5433 an encoded name, the match must not be done in wild mode. */
5434 wild_match = (strchr (text0, '.') == NULL && !encoded);
5437 /* First, look at the partial symtab symbols. */
5439 struct add_partial_datum data;
5441 data.completions = &completions;
5443 data.text_len = text_len;
5446 data.wild_match = wild_match;
5447 data.encoded = encoded;
5448 map_partial_symbol_names (ada_add_partial_symbol_completions, &data);
5451 /* At this point scan through the misc symbol vectors and add each
5452 symbol you find to the list. Eventually we want to ignore
5453 anything that isn't a text symbol (everything else will be
5454 handled by the psymtab code above). */
5456 ALL_MSYMBOLS (objfile, msymbol)
5459 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (msymbol),
5460 text, text_len, text0, word, wild_match, encoded);
5463 /* Search upwards from currently selected frame (so that we can
5464 complete on local vars. */
5466 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
5468 if (!BLOCK_SUPERBLOCK (b))
5469 surrounding_static_block = b; /* For elmin of dups */
5471 ALL_BLOCK_SYMBOLS (b, iter, sym)
5473 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (sym),
5474 text, text_len, text0, word,
5475 wild_match, encoded);
5479 /* Go through the symtabs and check the externs and statics for
5480 symbols which match. */
5482 ALL_SYMTABS (objfile, s)
5485 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
5486 ALL_BLOCK_SYMBOLS (b, iter, sym)
5488 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (sym),
5489 text, text_len, text0, word,
5490 wild_match, encoded);
5494 ALL_SYMTABS (objfile, s)
5497 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
5498 /* Don't do this block twice. */
5499 if (b == surrounding_static_block)
5501 ALL_BLOCK_SYMBOLS (b, iter, sym)
5503 symbol_completion_add (&completions, SYMBOL_LINKAGE_NAME (sym),
5504 text, text_len, text0, word,
5505 wild_match, encoded);
5509 /* Append the closing NULL entry. */
5510 VEC_safe_push (char_ptr, completions, NULL);
5512 /* Make a copy of the COMPLETIONS VEC before we free it, and then
5513 return the copy. It's unfortunate that we have to make a copy
5514 of an array that we're about to destroy, but there is nothing much
5515 we can do about it. Fortunately, it's typically not a very large
5518 const size_t completions_size =
5519 VEC_length (char_ptr, completions) * sizeof (char *);
5520 char **result = malloc (completions_size);
5522 memcpy (result, VEC_address (char_ptr, completions), completions_size);
5524 VEC_free (char_ptr, completions);
5531 /* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
5532 for tagged types. */
5535 ada_is_dispatch_table_ptr_type (struct type *type)
5539 if (TYPE_CODE (type) != TYPE_CODE_PTR)
5542 name = TYPE_NAME (TYPE_TARGET_TYPE (type));
5546 return (strcmp (name, "ada__tags__dispatch_table") == 0);
5549 /* True if field number FIELD_NUM in struct or union type TYPE is supposed
5550 to be invisible to users. */
5553 ada_is_ignored_field (struct type *type, int field_num)
5555 if (field_num < 0 || field_num > TYPE_NFIELDS (type))
5558 /* Check the name of that field. */
5560 const char *name = TYPE_FIELD_NAME (type, field_num);
5562 /* Anonymous field names should not be printed.
5563 brobecker/2007-02-20: I don't think this can actually happen
5564 but we don't want to print the value of annonymous fields anyway. */
5568 /* A field named "_parent" is internally generated by GNAT for
5569 tagged types, and should not be printed either. */
5570 if (name[0] == '_' && strncmp (name, "_parent", 7) != 0)
5574 /* If this is the dispatch table of a tagged type, then ignore. */
5575 if (ada_is_tagged_type (type, 1)
5576 && ada_is_dispatch_table_ptr_type (TYPE_FIELD_TYPE (type, field_num)))
5579 /* Not a special field, so it should not be ignored. */
5583 /* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
5584 pointer or reference type whose ultimate target has a tag field. */
5587 ada_is_tagged_type (struct type *type, int refok)
5589 return (ada_lookup_struct_elt_type (type, "_tag", refok, 1, NULL) != NULL);
5592 /* True iff TYPE represents the type of X'Tag */
5595 ada_is_tag_type (struct type *type)
5597 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_PTR)
5601 const char *name = ada_type_name (TYPE_TARGET_TYPE (type));
5603 return (name != NULL
5604 && strcmp (name, "ada__tags__dispatch_table") == 0);
5608 /* The type of the tag on VAL. */
5611 ada_tag_type (struct value *val)
5613 return ada_lookup_struct_elt_type (value_type (val), "_tag", 1, 0, NULL);
5616 /* The value of the tag on VAL. */
5619 ada_value_tag (struct value *val)
5621 return ada_value_struct_elt (val, "_tag", 0);
5624 /* The value of the tag on the object of type TYPE whose contents are
5625 saved at VALADDR, if it is non-null, or is at memory address
5628 static struct value *
5629 value_tag_from_contents_and_address (struct type *type,
5630 const gdb_byte *valaddr,
5633 int tag_byte_offset;
5634 struct type *tag_type;
5636 if (find_struct_field ("_tag", type, 0, &tag_type, &tag_byte_offset,
5639 const gdb_byte *valaddr1 = ((valaddr == NULL)
5641 : valaddr + tag_byte_offset);
5642 CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
5644 return value_from_contents_and_address (tag_type, valaddr1, address1);
5649 static struct type *
5650 type_from_tag (struct value *tag)
5652 const char *type_name = ada_tag_name (tag);
5654 if (type_name != NULL)
5655 return ada_find_any_type (ada_encode (type_name));
5666 static int ada_tag_name_1 (void *);
5667 static int ada_tag_name_2 (struct tag_args *);
5669 /* Wrapper function used by ada_tag_name. Given a struct tag_args*
5670 value ARGS, sets ARGS->name to the tag name of ARGS->tag.
5671 The value stored in ARGS->name is valid until the next call to
5675 ada_tag_name_1 (void *args0)
5677 struct tag_args *args = (struct tag_args *) args0;
5678 static char name[1024];
5683 val = ada_value_struct_elt (args->tag, "tsd", 1);
5685 return ada_tag_name_2 (args);
5686 val = ada_value_struct_elt (val, "expanded_name", 1);
5689 read_memory_string (value_as_address (val), name, sizeof (name) - 1);
5690 for (p = name; *p != '\0'; p += 1)
5697 /* Return the "ada__tags__type_specific_data" type. */
5699 static struct type *
5700 ada_get_tsd_type (struct inferior *inf)
5702 struct ada_inferior_data *data = get_ada_inferior_data (inf);
5704 if (data->tsd_type == 0)
5705 data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
5706 return data->tsd_type;
5709 /* Utility function for ada_tag_name_1 that tries the second
5710 representation for the dispatch table (in which there is no
5711 explicit 'tsd' field in the referent of the tag pointer, and instead
5712 the tsd pointer is stored just before the dispatch table. */
5715 ada_tag_name_2 (struct tag_args *args)
5717 struct type *info_type;
5718 static char name[1024];
5720 struct value *val, *valp;
5723 info_type = ada_get_tsd_type (current_inferior());
5724 if (info_type == NULL)
5726 info_type = lookup_pointer_type (lookup_pointer_type (info_type));
5727 valp = value_cast (info_type, args->tag);
5730 val = value_ind (value_ptradd (valp, -1));
5733 val = ada_value_struct_elt (val, "expanded_name", 1);
5736 read_memory_string (value_as_address (val), name, sizeof (name) - 1);
5737 for (p = name; *p != '\0'; p += 1)
5744 /* The type name of the dynamic type denoted by the 'tag value TAG, as
5748 ada_tag_name (struct value *tag)
5750 struct tag_args args;
5752 if (!ada_is_tag_type (value_type (tag)))
5756 catch_errors (ada_tag_name_1, &args, NULL, RETURN_MASK_ALL);
5760 /* The parent type of TYPE, or NULL if none. */
5763 ada_parent_type (struct type *type)
5767 type = ada_check_typedef (type);
5769 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
5772 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
5773 if (ada_is_parent_field (type, i))
5775 struct type *parent_type = TYPE_FIELD_TYPE (type, i);
5777 /* If the _parent field is a pointer, then dereference it. */
5778 if (TYPE_CODE (parent_type) == TYPE_CODE_PTR)
5779 parent_type = TYPE_TARGET_TYPE (parent_type);
5780 /* If there is a parallel XVS type, get the actual base type. */
5781 parent_type = ada_get_base_type (parent_type);
5783 return ada_check_typedef (parent_type);
5789 /* True iff field number FIELD_NUM of structure type TYPE contains the
5790 parent-type (inherited) fields of a derived type. Assumes TYPE is
5791 a structure type with at least FIELD_NUM+1 fields. */
5794 ada_is_parent_field (struct type *type, int field_num)
5796 const char *name = TYPE_FIELD_NAME (ada_check_typedef (type), field_num);
5798 return (name != NULL
5799 && (strncmp (name, "PARENT", 6) == 0
5800 || strncmp (name, "_parent", 7) == 0));
5803 /* True iff field number FIELD_NUM of structure type TYPE is a
5804 transparent wrapper field (which should be silently traversed when doing
5805 field selection and flattened when printing). Assumes TYPE is a
5806 structure type with at least FIELD_NUM+1 fields. Such fields are always
5810 ada_is_wrapper_field (struct type *type, int field_num)
5812 const char *name = TYPE_FIELD_NAME (type, field_num);
5814 return (name != NULL
5815 && (strncmp (name, "PARENT", 6) == 0
5816 || strcmp (name, "REP") == 0
5817 || strncmp (name, "_parent", 7) == 0
5818 || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
5821 /* True iff field number FIELD_NUM of structure or union type TYPE
5822 is a variant wrapper. Assumes TYPE is a structure type with at least
5823 FIELD_NUM+1 fields. */
5826 ada_is_variant_part (struct type *type, int field_num)
5828 struct type *field_type = TYPE_FIELD_TYPE (type, field_num);
5830 return (TYPE_CODE (field_type) == TYPE_CODE_UNION
5831 || (is_dynamic_field (type, field_num)
5832 && (TYPE_CODE (TYPE_TARGET_TYPE (field_type))
5833 == TYPE_CODE_UNION)));
5836 /* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
5837 whose discriminants are contained in the record type OUTER_TYPE,
5838 returns the type of the controlling discriminant for the variant.
5839 May return NULL if the type could not be found. */
5842 ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
5844 char *name = ada_variant_discrim_name (var_type);
5846 return ada_lookup_struct_elt_type (outer_type, name, 1, 1, NULL);
5849 /* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
5850 valid field number within it, returns 1 iff field FIELD_NUM of TYPE
5851 represents a 'when others' clause; otherwise 0. */
5854 ada_is_others_clause (struct type *type, int field_num)
5856 const char *name = TYPE_FIELD_NAME (type, field_num);
5858 return (name != NULL && name[0] == 'O');
5861 /* Assuming that TYPE0 is the type of the variant part of a record,
5862 returns the name of the discriminant controlling the variant.
5863 The value is valid until the next call to ada_variant_discrim_name. */
5866 ada_variant_discrim_name (struct type *type0)
5868 static char *result = NULL;
5869 static size_t result_len = 0;
5872 const char *discrim_end;
5873 const char *discrim_start;
5875 if (TYPE_CODE (type0) == TYPE_CODE_PTR)
5876 type = TYPE_TARGET_TYPE (type0);
5880 name = ada_type_name (type);
5882 if (name == NULL || name[0] == '\000')
5885 for (discrim_end = name + strlen (name) - 6; discrim_end != name;
5888 if (strncmp (discrim_end, "___XVN", 6) == 0)
5891 if (discrim_end == name)
5894 for (discrim_start = discrim_end; discrim_start != name + 3;
5897 if (discrim_start == name + 1)
5899 if ((discrim_start > name + 3
5900 && strncmp (discrim_start - 3, "___", 3) == 0)
5901 || discrim_start[-1] == '.')
5905 GROW_VECT (result, result_len, discrim_end - discrim_start + 1);
5906 strncpy (result, discrim_start, discrim_end - discrim_start);
5907 result[discrim_end - discrim_start] = '\0';
5911 /* Scan STR for a subtype-encoded number, beginning at position K.
5912 Put the position of the character just past the number scanned in
5913 *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
5914 Return 1 if there was a valid number at the given position, and 0
5915 otherwise. A "subtype-encoded" number consists of the absolute value
5916 in decimal, followed by the letter 'm' to indicate a negative number.
5917 Assumes 0m does not occur. */
5920 ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
5924 if (!isdigit (str[k]))
5927 /* Do it the hard way so as not to make any assumption about
5928 the relationship of unsigned long (%lu scan format code) and
5931 while (isdigit (str[k]))
5933 RU = RU * 10 + (str[k] - '0');
5940 *R = (-(LONGEST) (RU - 1)) - 1;
5946 /* NOTE on the above: Technically, C does not say what the results of
5947 - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
5948 number representable as a LONGEST (although either would probably work
5949 in most implementations). When RU>0, the locution in the then branch
5950 above is always equivalent to the negative of RU. */
5957 /* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
5958 and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
5959 in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
5962 ada_in_variant (LONGEST val, struct type *type, int field_num)
5964 const char *name = TYPE_FIELD_NAME (type, field_num);
5978 if (!ada_scan_number (name, p + 1, &W, &p))
5988 if (!ada_scan_number (name, p + 1, &L, &p)
5989 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
5991 if (val >= L && val <= U)
6003 /* FIXME: Lots of redundancy below. Try to consolidate. */
6005 /* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
6006 ARG_TYPE, extract and return the value of one of its (non-static)
6007 fields. FIELDNO says which field. Differs from value_primitive_field
6008 only in that it can handle packed values of arbitrary type. */
6010 static struct value *
6011 ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
6012 struct type *arg_type)
6016 arg_type = ada_check_typedef (arg_type);
6017 type = TYPE_FIELD_TYPE (arg_type, fieldno);
6019 /* Handle packed fields. */
6021 if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0)
6023 int bit_pos = TYPE_FIELD_BITPOS (arg_type, fieldno);
6024 int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
6026 return ada_value_primitive_packed_val (arg1, value_contents (arg1),
6027 offset + bit_pos / 8,
6028 bit_pos % 8, bit_size, type);
6031 return value_primitive_field (arg1, offset, fieldno, arg_type);
6034 /* Find field with name NAME in object of type TYPE. If found,
6035 set the following for each argument that is non-null:
6036 - *FIELD_TYPE_P to the field's type;
6037 - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
6038 an object of that type;
6039 - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
6040 - *BIT_SIZE_P to its size in bits if the field is packed, and
6042 If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
6043 fields up to but not including the desired field, or by the total
6044 number of fields if not found. A NULL value of NAME never
6045 matches; the function just counts visible fields in this case.
6047 Returns 1 if found, 0 otherwise. */
6050 find_struct_field (char *name, struct type *type, int offset,
6051 struct type **field_type_p,
6052 int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
6057 type = ada_check_typedef (type);
6059 if (field_type_p != NULL)
6060 *field_type_p = NULL;
6061 if (byte_offset_p != NULL)
6063 if (bit_offset_p != NULL)
6065 if (bit_size_p != NULL)
6068 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
6070 int bit_pos = TYPE_FIELD_BITPOS (type, i);
6071 int fld_offset = offset + bit_pos / 8;
6072 char *t_field_name = TYPE_FIELD_NAME (type, i);
6074 if (t_field_name == NULL)
6077 else if (name != NULL && field_name_match (t_field_name, name))
6079 int bit_size = TYPE_FIELD_BITSIZE (type, i);
6081 if (field_type_p != NULL)
6082 *field_type_p = TYPE_FIELD_TYPE (type, i);
6083 if (byte_offset_p != NULL)
6084 *byte_offset_p = fld_offset;
6085 if (bit_offset_p != NULL)
6086 *bit_offset_p = bit_pos % 8;
6087 if (bit_size_p != NULL)
6088 *bit_size_p = bit_size;
6091 else if (ada_is_wrapper_field (type, i))
6093 if (find_struct_field (name, TYPE_FIELD_TYPE (type, i), fld_offset,
6094 field_type_p, byte_offset_p, bit_offset_p,
6095 bit_size_p, index_p))
6098 else if (ada_is_variant_part (type, i))
6100 /* PNH: Wait. Do we ever execute this section, or is ARG always of
6103 struct type *field_type
6104 = ada_check_typedef (TYPE_FIELD_TYPE (type, i));
6106 for (j = 0; j < TYPE_NFIELDS (field_type); j += 1)
6108 if (find_struct_field (name, TYPE_FIELD_TYPE (field_type, j),
6110 + TYPE_FIELD_BITPOS (field_type, j) / 8,
6111 field_type_p, byte_offset_p,
6112 bit_offset_p, bit_size_p, index_p))
6116 else if (index_p != NULL)
6122 /* Number of user-visible fields in record type TYPE. */
6125 num_visible_fields (struct type *type)
6130 find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
6134 /* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
6135 and search in it assuming it has (class) type TYPE.
6136 If found, return value, else return NULL.
6138 Searches recursively through wrapper fields (e.g., '_parent'). */
6140 static struct value *
6141 ada_search_struct_field (char *name, struct value *arg, int offset,
6146 type = ada_check_typedef (type);
6147 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
6149 char *t_field_name = TYPE_FIELD_NAME (type, i);
6151 if (t_field_name == NULL)
6154 else if (field_name_match (t_field_name, name))
6155 return ada_value_primitive_field (arg, offset, i, type);
6157 else if (ada_is_wrapper_field (type, i))
6159 struct value *v = /* Do not let indent join lines here. */
6160 ada_search_struct_field (name, arg,
6161 offset + TYPE_FIELD_BITPOS (type, i) / 8,
6162 TYPE_FIELD_TYPE (type, i));
6168 else if (ada_is_variant_part (type, i))
6170 /* PNH: Do we ever get here? See find_struct_field. */
6172 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type,
6174 int var_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
6176 for (j = 0; j < TYPE_NFIELDS (field_type); j += 1)
6178 struct value *v = ada_search_struct_field /* Force line break. */
6180 var_offset + TYPE_FIELD_BITPOS (field_type, j) / 8,
6181 TYPE_FIELD_TYPE (field_type, j));
6191 static struct value *ada_index_struct_field_1 (int *, struct value *,
6192 int, struct type *);
6195 /* Return field #INDEX in ARG, where the index is that returned by
6196 * find_struct_field through its INDEX_P argument. Adjust the address
6197 * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
6198 * If found, return value, else return NULL. */
6200 static struct value *
6201 ada_index_struct_field (int index, struct value *arg, int offset,
6204 return ada_index_struct_field_1 (&index, arg, offset, type);
6208 /* Auxiliary function for ada_index_struct_field. Like
6209 * ada_index_struct_field, but takes index from *INDEX_P and modifies
6212 static struct value *
6213 ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
6217 type = ada_check_typedef (type);
6219 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
6221 if (TYPE_FIELD_NAME (type, i) == NULL)
6223 else if (ada_is_wrapper_field (type, i))
6225 struct value *v = /* Do not let indent join lines here. */
6226 ada_index_struct_field_1 (index_p, arg,
6227 offset + TYPE_FIELD_BITPOS (type, i) / 8,
6228 TYPE_FIELD_TYPE (type, i));
6234 else if (ada_is_variant_part (type, i))
6236 /* PNH: Do we ever get here? See ada_search_struct_field,
6237 find_struct_field. */
6238 error (_("Cannot assign this kind of variant record"));
6240 else if (*index_p == 0)
6241 return ada_value_primitive_field (arg, offset, i, type);
6248 /* Given ARG, a value of type (pointer or reference to a)*
6249 structure/union, extract the component named NAME from the ultimate
6250 target structure/union and return it as a value with its
6253 The routine searches for NAME among all members of the structure itself
6254 and (recursively) among all members of any wrapper members
6257 If NO_ERR, then simply return NULL in case of error, rather than
6261 ada_value_struct_elt (struct value *arg, char *name, int no_err)
6263 struct type *t, *t1;
6267 t1 = t = ada_check_typedef (value_type (arg));
6268 if (TYPE_CODE (t) == TYPE_CODE_REF)
6270 t1 = TYPE_TARGET_TYPE (t);
6273 t1 = ada_check_typedef (t1);
6274 if (TYPE_CODE (t1) == TYPE_CODE_PTR)
6276 arg = coerce_ref (arg);
6281 while (TYPE_CODE (t) == TYPE_CODE_PTR)
6283 t1 = TYPE_TARGET_TYPE (t);
6286 t1 = ada_check_typedef (t1);
6287 if (TYPE_CODE (t1) == TYPE_CODE_PTR)
6289 arg = value_ind (arg);
6296 if (TYPE_CODE (t1) != TYPE_CODE_STRUCT && TYPE_CODE (t1) != TYPE_CODE_UNION)
6300 v = ada_search_struct_field (name, arg, 0, t);
6303 int bit_offset, bit_size, byte_offset;
6304 struct type *field_type;
6307 if (TYPE_CODE (t) == TYPE_CODE_PTR)
6308 address = value_as_address (arg);
6310 address = unpack_pointer (t, value_contents (arg));
6312 t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL, address, NULL, 1);
6313 if (find_struct_field (name, t1, 0,
6314 &field_type, &byte_offset, &bit_offset,
6319 if (TYPE_CODE (t) == TYPE_CODE_REF)
6320 arg = ada_coerce_ref (arg);
6322 arg = ada_value_ind (arg);
6323 v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
6324 bit_offset, bit_size,
6328 v = value_at_lazy (field_type, address + byte_offset);
6332 if (v != NULL || no_err)
6335 error (_("There is no member named %s."), name);
6341 error (_("Attempt to extract a component of a value that is not a record."));
6344 /* Given a type TYPE, look up the type of the component of type named NAME.
6345 If DISPP is non-null, add its byte displacement from the beginning of a
6346 structure (pointed to by a value) of type TYPE to *DISPP (does not
6347 work for packed fields).
6349 Matches any field whose name has NAME as a prefix, possibly
6352 TYPE can be either a struct or union. If REFOK, TYPE may also
6353 be a (pointer or reference)+ to a struct or union, and the
6354 ultimate target type will be searched.
6356 Looks recursively into variant clauses and parent types.
6358 If NOERR is nonzero, return NULL if NAME is not suitably defined or
6359 TYPE is not a type of the right kind. */
6361 static struct type *
6362 ada_lookup_struct_elt_type (struct type *type, char *name, int refok,
6363 int noerr, int *dispp)
6370 if (refok && type != NULL)
6373 type = ada_check_typedef (type);
6374 if (TYPE_CODE (type) != TYPE_CODE_PTR
6375 && TYPE_CODE (type) != TYPE_CODE_REF)
6377 type = TYPE_TARGET_TYPE (type);
6381 || (TYPE_CODE (type) != TYPE_CODE_STRUCT
6382 && TYPE_CODE (type) != TYPE_CODE_UNION))
6388 target_terminal_ours ();
6389 gdb_flush (gdb_stdout);
6391 error (_("Type (null) is not a structure or union type"));
6394 /* XXX: type_sprint */
6395 fprintf_unfiltered (gdb_stderr, _("Type "));
6396 type_print (type, "", gdb_stderr, -1);
6397 error (_(" is not a structure or union type"));
6402 type = to_static_fixed_type (type);
6404 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
6406 char *t_field_name = TYPE_FIELD_NAME (type, i);
6410 if (t_field_name == NULL)
6413 else if (field_name_match (t_field_name, name))
6416 *dispp += TYPE_FIELD_BITPOS (type, i) / 8;
6417 return ada_check_typedef (TYPE_FIELD_TYPE (type, i));
6420 else if (ada_is_wrapper_field (type, i))
6423 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name,
6428 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
6433 else if (ada_is_variant_part (type, i))
6436 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type,
6439 for (j = TYPE_NFIELDS (field_type) - 1; j >= 0; j -= 1)
6441 /* FIXME pnh 2008/01/26: We check for a field that is
6442 NOT wrapped in a struct, since the compiler sometimes
6443 generates these for unchecked variant types. Revisit
6444 if the compiler changes this practice. */
6445 char *v_field_name = TYPE_FIELD_NAME (field_type, j);
6447 if (v_field_name != NULL
6448 && field_name_match (v_field_name, name))
6449 t = ada_check_typedef (TYPE_FIELD_TYPE (field_type, j));
6451 t = ada_lookup_struct_elt_type (TYPE_FIELD_TYPE (field_type, j),
6457 *dispp += disp + TYPE_FIELD_BITPOS (type, i) / 8;
6468 target_terminal_ours ();
6469 gdb_flush (gdb_stdout);
6472 /* XXX: type_sprint */
6473 fprintf_unfiltered (gdb_stderr, _("Type "));
6474 type_print (type, "", gdb_stderr, -1);
6475 error (_(" has no component named <null>"));
6479 /* XXX: type_sprint */
6480 fprintf_unfiltered (gdb_stderr, _("Type "));
6481 type_print (type, "", gdb_stderr, -1);
6482 error (_(" has no component named %s"), name);
6489 /* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
6490 within a value of type OUTER_TYPE, return true iff VAR_TYPE
6491 represents an unchecked union (that is, the variant part of a
6492 record that is named in an Unchecked_Union pragma). */
6495 is_unchecked_variant (struct type *var_type, struct type *outer_type)
6497 char *discrim_name = ada_variant_discrim_name (var_type);
6499 return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1, NULL)
6504 /* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
6505 within a value of type OUTER_TYPE that is stored in GDB at
6506 OUTER_VALADDR, determine which variant clause (field number in VAR_TYPE,
6507 numbering from 0) is applicable. Returns -1 if none are. */
6510 ada_which_variant_applies (struct type *var_type, struct type *outer_type,
6511 const gdb_byte *outer_valaddr)
6515 char *discrim_name = ada_variant_discrim_name (var_type);
6516 struct value *outer;
6517 struct value *discrim;
6518 LONGEST discrim_val;
6520 outer = value_from_contents_and_address (outer_type, outer_valaddr, 0);
6521 discrim = ada_value_struct_elt (outer, discrim_name, 1);
6522 if (discrim == NULL)
6524 discrim_val = value_as_long (discrim);
6527 for (i = 0; i < TYPE_NFIELDS (var_type); i += 1)
6529 if (ada_is_others_clause (var_type, i))
6531 else if (ada_in_variant (discrim_val, var_type, i))
6535 return others_clause;
6540 /* Dynamic-Sized Records */
6542 /* Strategy: The type ostensibly attached to a value with dynamic size
6543 (i.e., a size that is not statically recorded in the debugging
6544 data) does not accurately reflect the size or layout of the value.
6545 Our strategy is to convert these values to values with accurate,
6546 conventional types that are constructed on the fly. */
6548 /* There is a subtle and tricky problem here. In general, we cannot
6549 determine the size of dynamic records without its data. However,
6550 the 'struct value' data structure, which GDB uses to represent
6551 quantities in the inferior process (the target), requires the size
6552 of the type at the time of its allocation in order to reserve space
6553 for GDB's internal copy of the data. That's why the
6554 'to_fixed_xxx_type' routines take (target) addresses as parameters,
6555 rather than struct value*s.
6557 However, GDB's internal history variables ($1, $2, etc.) are
6558 struct value*s containing internal copies of the data that are not, in
6559 general, the same as the data at their corresponding addresses in
6560 the target. Fortunately, the types we give to these values are all
6561 conventional, fixed-size types (as per the strategy described
6562 above), so that we don't usually have to perform the
6563 'to_fixed_xxx_type' conversions to look at their values.
6564 Unfortunately, there is one exception: if one of the internal
6565 history variables is an array whose elements are unconstrained
6566 records, then we will need to create distinct fixed types for each
6567 element selected. */
6569 /* The upshot of all of this is that many routines take a (type, host
6570 address, target address) triple as arguments to represent a value.
6571 The host address, if non-null, is supposed to contain an internal
6572 copy of the relevant data; otherwise, the program is to consult the
6573 target at the target address. */
6575 /* Assuming that VAL0 represents a pointer value, the result of
6576 dereferencing it. Differs from value_ind in its treatment of
6577 dynamic-sized types. */
6580 ada_value_ind (struct value *val0)
6582 struct value *val = unwrap_value (value_ind (val0));
6584 return ada_to_fixed_value (val);
6587 /* The value resulting from dereferencing any "reference to"
6588 qualifiers on VAL0. */
6590 static struct value *
6591 ada_coerce_ref (struct value *val0)
6593 if (TYPE_CODE (value_type (val0)) == TYPE_CODE_REF)
6595 struct value *val = val0;
6597 val = coerce_ref (val);
6598 val = unwrap_value (val);
6599 return ada_to_fixed_value (val);
6605 /* Return OFF rounded upward if necessary to a multiple of
6606 ALIGNMENT (a power of 2). */
6609 align_value (unsigned int off, unsigned int alignment)
6611 return (off + alignment - 1) & ~(alignment - 1);
6614 /* Return the bit alignment required for field #F of template type TYPE. */
6617 field_alignment (struct type *type, int f)
6619 const char *name = TYPE_FIELD_NAME (type, f);
6623 /* The field name should never be null, unless the debugging information
6624 is somehow malformed. In this case, we assume the field does not
6625 require any alignment. */
6629 len = strlen (name);
6631 if (!isdigit (name[len - 1]))
6634 if (isdigit (name[len - 2]))
6635 align_offset = len - 2;
6637 align_offset = len - 1;
6639 if (align_offset < 7 || strncmp ("___XV", name + align_offset - 6, 5) != 0)
6640 return TARGET_CHAR_BIT;
6642 return atoi (name + align_offset) * TARGET_CHAR_BIT;
6645 /* Find a symbol named NAME. Ignores ambiguity. */
6648 ada_find_any_symbol (const char *name)
6652 sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
6653 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
6656 sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
6660 /* Find a type named NAME. Ignores ambiguity. This routine will look
6661 solely for types defined by debug info, it will not search the GDB
6665 ada_find_any_type (const char *name)
6667 struct symbol *sym = ada_find_any_symbol (name);
6670 return SYMBOL_TYPE (sym);
6675 /* Given NAME and an associated BLOCK, search all symbols for
6676 NAME suffixed with "___XR", which is the ``renaming'' symbol
6677 associated to NAME. Return this symbol if found, return
6681 ada_find_renaming_symbol (const char *name, struct block *block)
6685 sym = find_old_style_renaming_symbol (name, block);
6690 /* Not right yet. FIXME pnh 7/20/2007. */
6691 sym = ada_find_any_symbol (name);
6692 if (sym != NULL && strstr (SYMBOL_LINKAGE_NAME (sym), "___XR") != NULL)
6698 static struct symbol *
6699 find_old_style_renaming_symbol (const char *name, struct block *block)
6701 const struct symbol *function_sym = block_linkage_function (block);
6704 if (function_sym != NULL)
6706 /* If the symbol is defined inside a function, NAME is not fully
6707 qualified. This means we need to prepend the function name
6708 as well as adding the ``___XR'' suffix to build the name of
6709 the associated renaming symbol. */
6710 char *function_name = SYMBOL_LINKAGE_NAME (function_sym);
6711 /* Function names sometimes contain suffixes used
6712 for instance to qualify nested subprograms. When building
6713 the XR type name, we need to make sure that this suffix is
6714 not included. So do not include any suffix in the function
6715 name length below. */
6716 int function_name_len = ada_name_prefix_len (function_name);
6717 const int rename_len = function_name_len + 2 /* "__" */
6718 + strlen (name) + 6 /* "___XR\0" */ ;
6720 /* Strip the suffix if necessary. */
6721 ada_remove_trailing_digits (function_name, &function_name_len);
6722 ada_remove_po_subprogram_suffix (function_name, &function_name_len);
6723 ada_remove_Xbn_suffix (function_name, &function_name_len);
6725 /* Library-level functions are a special case, as GNAT adds
6726 a ``_ada_'' prefix to the function name to avoid namespace
6727 pollution. However, the renaming symbols themselves do not
6728 have this prefix, so we need to skip this prefix if present. */
6729 if (function_name_len > 5 /* "_ada_" */
6730 && strstr (function_name, "_ada_") == function_name)
6733 function_name_len -= 5;
6736 rename = (char *) alloca (rename_len * sizeof (char));
6737 strncpy (rename, function_name, function_name_len);
6738 xsnprintf (rename + function_name_len, rename_len - function_name_len,
6743 const int rename_len = strlen (name) + 6;
6745 rename = (char *) alloca (rename_len * sizeof (char));
6746 xsnprintf (rename, rename_len * sizeof (char), "%s___XR", name);
6749 return ada_find_any_symbol (rename);
6752 /* Because of GNAT encoding conventions, several GDB symbols may match a
6753 given type name. If the type denoted by TYPE0 is to be preferred to
6754 that of TYPE1 for purposes of type printing, return non-zero;
6755 otherwise return 0. */
6758 ada_prefer_type (struct type *type0, struct type *type1)
6762 else if (type0 == NULL)
6764 else if (TYPE_CODE (type1) == TYPE_CODE_VOID)
6766 else if (TYPE_CODE (type0) == TYPE_CODE_VOID)
6768 else if (TYPE_NAME (type1) == NULL && TYPE_NAME (type0) != NULL)
6770 else if (ada_is_constrained_packed_array_type (type0))
6772 else if (ada_is_array_descriptor_type (type0)
6773 && !ada_is_array_descriptor_type (type1))
6777 const char *type0_name = type_name_no_tag (type0);
6778 const char *type1_name = type_name_no_tag (type1);
6780 if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
6781 && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
6787 /* The name of TYPE, which is either its TYPE_NAME, or, if that is
6788 null, its TYPE_TAG_NAME. Null if TYPE is null. */
6791 ada_type_name (struct type *type)
6795 else if (TYPE_NAME (type) != NULL)
6796 return TYPE_NAME (type);
6798 return TYPE_TAG_NAME (type);
6801 /* Search the list of "descriptive" types associated to TYPE for a type
6802 whose name is NAME. */
6804 static struct type *
6805 find_parallel_type_by_descriptive_type (struct type *type, const char *name)
6807 struct type *result;
6809 /* If there no descriptive-type info, then there is no parallel type
6811 if (!HAVE_GNAT_AUX_INFO (type))
6814 result = TYPE_DESCRIPTIVE_TYPE (type);
6815 while (result != NULL)
6817 char *result_name = ada_type_name (result);
6819 if (result_name == NULL)
6821 warning (_("unexpected null name on descriptive type"));
6825 /* If the names match, stop. */
6826 if (strcmp (result_name, name) == 0)
6829 /* Otherwise, look at the next item on the list, if any. */
6830 if (HAVE_GNAT_AUX_INFO (result))
6831 result = TYPE_DESCRIPTIVE_TYPE (result);
6836 /* If we didn't find a match, see whether this is a packed array. With
6837 older compilers, the descriptive type information is either absent or
6838 irrelevant when it comes to packed arrays so the above lookup fails.
6839 Fall back to using a parallel lookup by name in this case. */
6840 if (result == NULL && ada_is_constrained_packed_array_type (type))
6841 return ada_find_any_type (name);
6846 /* Find a parallel type to TYPE with the specified NAME, using the
6847 descriptive type taken from the debugging information, if available,
6848 and otherwise using the (slower) name-based method. */
6850 static struct type *
6851 ada_find_parallel_type_with_name (struct type *type, const char *name)
6853 struct type *result = NULL;
6855 if (HAVE_GNAT_AUX_INFO (type))
6856 result = find_parallel_type_by_descriptive_type (type, name);
6858 result = ada_find_any_type (name);
6863 /* Same as above, but specify the name of the parallel type by appending
6864 SUFFIX to the name of TYPE. */
6867 ada_find_parallel_type (struct type *type, const char *suffix)
6869 char *name, *typename = ada_type_name (type);
6872 if (typename == NULL)
6875 len = strlen (typename);
6877 name = (char *) alloca (len + strlen (suffix) + 1);
6879 strcpy (name, typename);
6880 strcpy (name + len, suffix);
6882 return ada_find_parallel_type_with_name (type, name);
6885 /* If TYPE is a variable-size record type, return the corresponding template
6886 type describing its fields. Otherwise, return NULL. */
6888 static struct type *
6889 dynamic_template_type (struct type *type)
6891 type = ada_check_typedef (type);
6893 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT
6894 || ada_type_name (type) == NULL)
6898 int len = strlen (ada_type_name (type));
6900 if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
6903 return ada_find_parallel_type (type, "___XVE");
6907 /* Assuming that TEMPL_TYPE is a union or struct type, returns
6908 non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
6911 is_dynamic_field (struct type *templ_type, int field_num)
6913 const char *name = TYPE_FIELD_NAME (templ_type, field_num);
6916 && TYPE_CODE (TYPE_FIELD_TYPE (templ_type, field_num)) == TYPE_CODE_PTR
6917 && strstr (name, "___XVL") != NULL;
6920 /* The index of the variant field of TYPE, or -1 if TYPE does not
6921 represent a variant record type. */
6924 variant_field_index (struct type *type)
6928 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_STRUCT)
6931 for (f = 0; f < TYPE_NFIELDS (type); f += 1)
6933 if (ada_is_variant_part (type, f))
6939 /* A record type with no fields. */
6941 static struct type *
6942 empty_record (struct type *template)
6944 struct type *type = alloc_type_copy (template);
6946 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6947 TYPE_NFIELDS (type) = 0;
6948 TYPE_FIELDS (type) = NULL;
6949 INIT_CPLUS_SPECIFIC (type);
6950 TYPE_NAME (type) = "<empty>";
6951 TYPE_TAG_NAME (type) = NULL;
6952 TYPE_LENGTH (type) = 0;
6956 /* An ordinary record type (with fixed-length fields) that describes
6957 the value of type TYPE at VALADDR or ADDRESS (see comments at
6958 the beginning of this section) VAL according to GNAT conventions.
6959 DVAL0 should describe the (portion of a) record that contains any
6960 necessary discriminants. It should be NULL if value_type (VAL) is
6961 an outer-level type (i.e., as opposed to a branch of a variant.) A
6962 variant field (unless unchecked) is replaced by a particular branch
6965 If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
6966 length are not statically known are discarded. As a consequence,
6967 VALADDR, ADDRESS and DVAL0 are ignored.
6969 NOTE: Limitations: For now, we assume that dynamic fields and
6970 variants occupy whole numbers of bytes. However, they need not be
6974 ada_template_to_fixed_record_type_1 (struct type *type,
6975 const gdb_byte *valaddr,
6976 CORE_ADDR address, struct value *dval0,
6977 int keep_dynamic_fields)
6979 struct value *mark = value_mark ();
6982 int nfields, bit_len;
6985 int fld_bit_len, bit_incr;
6988 /* Compute the number of fields in this record type that are going
6989 to be processed: unless keep_dynamic_fields, this includes only
6990 fields whose position and length are static will be processed. */
6991 if (keep_dynamic_fields)
6992 nfields = TYPE_NFIELDS (type);
6996 while (nfields < TYPE_NFIELDS (type)
6997 && !ada_is_variant_part (type, nfields)
6998 && !is_dynamic_field (type, nfields))
7002 rtype = alloc_type_copy (type);
7003 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
7004 INIT_CPLUS_SPECIFIC (rtype);
7005 TYPE_NFIELDS (rtype) = nfields;
7006 TYPE_FIELDS (rtype) = (struct field *)
7007 TYPE_ALLOC (rtype, nfields * sizeof (struct field));
7008 memset (TYPE_FIELDS (rtype), 0, sizeof (struct field) * nfields);
7009 TYPE_NAME (rtype) = ada_type_name (type);
7010 TYPE_TAG_NAME (rtype) = NULL;
7011 TYPE_FIXED_INSTANCE (rtype) = 1;
7017 for (f = 0; f < nfields; f += 1)
7019 off = align_value (off, field_alignment (type, f))
7020 + TYPE_FIELD_BITPOS (type, f);
7021 TYPE_FIELD_BITPOS (rtype, f) = off;
7022 TYPE_FIELD_BITSIZE (rtype, f) = 0;
7024 if (ada_is_variant_part (type, f))
7027 fld_bit_len = bit_incr = 0;
7029 else if (is_dynamic_field (type, f))
7031 const gdb_byte *field_valaddr = valaddr;
7032 CORE_ADDR field_address = address;
7033 struct type *field_type =
7034 TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, f));
7038 /* rtype's length is computed based on the run-time
7039 value of discriminants. If the discriminants are not
7040 initialized, the type size may be completely bogus and
7041 GDB may fail to allocate a value for it. So check the
7042 size first before creating the value. */
7044 dval = value_from_contents_and_address (rtype, valaddr, address);
7049 /* If the type referenced by this field is an aligner type, we need
7050 to unwrap that aligner type, because its size might not be set.
7051 Keeping the aligner type would cause us to compute the wrong
7052 size for this field, impacting the offset of the all the fields
7053 that follow this one. */
7054 if (ada_is_aligner_type (field_type))
7056 long field_offset = TYPE_FIELD_BITPOS (field_type, f);
7058 field_valaddr = cond_offset_host (field_valaddr, field_offset);
7059 field_address = cond_offset_target (field_address, field_offset);
7060 field_type = ada_aligned_type (field_type);
7063 field_valaddr = cond_offset_host (field_valaddr,
7064 off / TARGET_CHAR_BIT);
7065 field_address = cond_offset_target (field_address,
7066 off / TARGET_CHAR_BIT);
7068 /* Get the fixed type of the field. Note that, in this case,
7069 we do not want to get the real type out of the tag: if
7070 the current field is the parent part of a tagged record,
7071 we will get the tag of the object. Clearly wrong: the real
7072 type of the parent is not the real type of the child. We
7073 would end up in an infinite loop. */
7074 field_type = ada_get_base_type (field_type);
7075 field_type = ada_to_fixed_type (field_type, field_valaddr,
7076 field_address, dval, 0);
7078 TYPE_FIELD_TYPE (rtype, f) = field_type;
7079 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
7080 bit_incr = fld_bit_len =
7081 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, f)) * TARGET_CHAR_BIT;
7085 struct type *field_type = TYPE_FIELD_TYPE (type, f);
7087 TYPE_FIELD_TYPE (rtype, f) = field_type;
7088 TYPE_FIELD_NAME (rtype, f) = TYPE_FIELD_NAME (type, f);
7089 if (TYPE_FIELD_BITSIZE (type, f) > 0)
7090 bit_incr = fld_bit_len =
7091 TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
7093 bit_incr = fld_bit_len =
7094 TYPE_LENGTH (ada_check_typedef (field_type)) * TARGET_CHAR_BIT;
7096 if (off + fld_bit_len > bit_len)
7097 bit_len = off + fld_bit_len;
7099 TYPE_LENGTH (rtype) =
7100 align_value (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
7103 /* We handle the variant part, if any, at the end because of certain
7104 odd cases in which it is re-ordered so as NOT to be the last field of
7105 the record. This can happen in the presence of representation
7107 if (variant_field >= 0)
7109 struct type *branch_type;
7111 off = TYPE_FIELD_BITPOS (rtype, variant_field);
7114 dval = value_from_contents_and_address (rtype, valaddr, address);
7119 to_fixed_variant_branch_type
7120 (TYPE_FIELD_TYPE (type, variant_field),
7121 cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
7122 cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
7123 if (branch_type == NULL)
7125 for (f = variant_field + 1; f < TYPE_NFIELDS (rtype); f += 1)
7126 TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
7127 TYPE_NFIELDS (rtype) -= 1;
7131 TYPE_FIELD_TYPE (rtype, variant_field) = branch_type;
7132 TYPE_FIELD_NAME (rtype, variant_field) = "S";
7134 TYPE_LENGTH (TYPE_FIELD_TYPE (rtype, variant_field)) *
7136 if (off + fld_bit_len > bit_len)
7137 bit_len = off + fld_bit_len;
7138 TYPE_LENGTH (rtype) =
7139 align_value (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
7143 /* According to exp_dbug.ads, the size of TYPE for variable-size records
7144 should contain the alignment of that record, which should be a strictly
7145 positive value. If null or negative, then something is wrong, most
7146 probably in the debug info. In that case, we don't round up the size
7147 of the resulting type. If this record is not part of another structure,
7148 the current RTYPE length might be good enough for our purposes. */
7149 if (TYPE_LENGTH (type) <= 0)
7151 if (TYPE_NAME (rtype))
7152 warning (_("Invalid type size for `%s' detected: %d."),
7153 TYPE_NAME (rtype), TYPE_LENGTH (type));
7155 warning (_("Invalid type size for <unnamed> detected: %d."),
7156 TYPE_LENGTH (type));
7160 TYPE_LENGTH (rtype) = align_value (TYPE_LENGTH (rtype),
7161 TYPE_LENGTH (type));
7164 value_free_to_mark (mark);
7165 if (TYPE_LENGTH (rtype) > varsize_limit)
7166 error (_("record type with dynamic size is larger than varsize-limit"));
7170 /* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
7173 static struct type *
7174 template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
7175 CORE_ADDR address, struct value *dval0)
7177 return ada_template_to_fixed_record_type_1 (type, valaddr,
7181 /* An ordinary record type in which ___XVL-convention fields and
7182 ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
7183 static approximations, containing all possible fields. Uses
7184 no runtime values. Useless for use in values, but that's OK,
7185 since the results are used only for type determinations. Works on both
7186 structs and unions. Representation note: to save space, we memorize
7187 the result of this function in the TYPE_TARGET_TYPE of the
7190 static struct type *
7191 template_to_static_fixed_type (struct type *type0)
7197 if (TYPE_TARGET_TYPE (type0) != NULL)
7198 return TYPE_TARGET_TYPE (type0);
7200 nfields = TYPE_NFIELDS (type0);
7203 for (f = 0; f < nfields; f += 1)
7205 struct type *field_type = ada_check_typedef (TYPE_FIELD_TYPE (type0, f));
7206 struct type *new_type;
7208 if (is_dynamic_field (type0, f))
7209 new_type = to_static_fixed_type (TYPE_TARGET_TYPE (field_type));
7211 new_type = static_unwrap_type (field_type);
7212 if (type == type0 && new_type != field_type)
7214 TYPE_TARGET_TYPE (type0) = type = alloc_type_copy (type0);
7215 TYPE_CODE (type) = TYPE_CODE (type0);
7216 INIT_CPLUS_SPECIFIC (type);
7217 TYPE_NFIELDS (type) = nfields;
7218 TYPE_FIELDS (type) = (struct field *)
7219 TYPE_ALLOC (type, nfields * sizeof (struct field));
7220 memcpy (TYPE_FIELDS (type), TYPE_FIELDS (type0),
7221 sizeof (struct field) * nfields);
7222 TYPE_NAME (type) = ada_type_name (type0);
7223 TYPE_TAG_NAME (type) = NULL;
7224 TYPE_FIXED_INSTANCE (type) = 1;
7225 TYPE_LENGTH (type) = 0;
7227 TYPE_FIELD_TYPE (type, f) = new_type;
7228 TYPE_FIELD_NAME (type, f) = TYPE_FIELD_NAME (type0, f);
7233 /* Given an object of type TYPE whose contents are at VALADDR and
7234 whose address in memory is ADDRESS, returns a revision of TYPE,
7235 which should be a non-dynamic-sized record, in which the variant
7236 part, if any, is replaced with the appropriate branch. Looks
7237 for discriminant values in DVAL0, which can be NULL if the record
7238 contains the necessary discriminant values. */
7240 static struct type *
7241 to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
7242 CORE_ADDR address, struct value *dval0)
7244 struct value *mark = value_mark ();
7247 struct type *branch_type;
7248 int nfields = TYPE_NFIELDS (type);
7249 int variant_field = variant_field_index (type);
7251 if (variant_field == -1)
7255 dval = value_from_contents_and_address (type, valaddr, address);
7259 rtype = alloc_type_copy (type);
7260 TYPE_CODE (rtype) = TYPE_CODE_STRUCT;
7261 INIT_CPLUS_SPECIFIC (rtype);
7262 TYPE_NFIELDS (rtype) = nfields;
7263 TYPE_FIELDS (rtype) =
7264 (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
7265 memcpy (TYPE_FIELDS (rtype), TYPE_FIELDS (type),
7266 sizeof (struct field) * nfields);
7267 TYPE_NAME (rtype) = ada_type_name (type);
7268 TYPE_TAG_NAME (rtype) = NULL;
7269 TYPE_FIXED_INSTANCE (rtype) = 1;
7270 TYPE_LENGTH (rtype) = TYPE_LENGTH (type);
7272 branch_type = to_fixed_variant_branch_type
7273 (TYPE_FIELD_TYPE (type, variant_field),
7274 cond_offset_host (valaddr,
7275 TYPE_FIELD_BITPOS (type, variant_field)
7277 cond_offset_target (address,
7278 TYPE_FIELD_BITPOS (type, variant_field)
7279 / TARGET_CHAR_BIT), dval);
7280 if (branch_type == NULL)
7284 for (f = variant_field + 1; f < nfields; f += 1)
7285 TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
7286 TYPE_NFIELDS (rtype) -= 1;
7290 TYPE_FIELD_TYPE (rtype, variant_field) = branch_type;
7291 TYPE_FIELD_NAME (rtype, variant_field) = "S";
7292 TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
7293 TYPE_LENGTH (rtype) += TYPE_LENGTH (branch_type);
7295 TYPE_LENGTH (rtype) -= TYPE_LENGTH (TYPE_FIELD_TYPE (type, variant_field));
7297 value_free_to_mark (mark);
7301 /* An ordinary record type (with fixed-length fields) that describes
7302 the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
7303 beginning of this section]. Any necessary discriminants' values
7304 should be in DVAL, a record value; it may be NULL if the object
7305 at ADDR itself contains any necessary discriminant values.
7306 Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
7307 values from the record are needed. Except in the case that DVAL,
7308 VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
7309 unchecked) is replaced by a particular branch of the variant.
7311 NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
7312 is questionable and may be removed. It can arise during the
7313 processing of an unconstrained-array-of-record type where all the
7314 variant branches have exactly the same size. This is because in
7315 such cases, the compiler does not bother to use the XVS convention
7316 when encoding the record. I am currently dubious of this
7317 shortcut and suspect the compiler should be altered. FIXME. */
7319 static struct type *
7320 to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
7321 CORE_ADDR address, struct value *dval)
7323 struct type *templ_type;
7325 if (TYPE_FIXED_INSTANCE (type0))
7328 templ_type = dynamic_template_type (type0);
7330 if (templ_type != NULL)
7331 return template_to_fixed_record_type (templ_type, valaddr, address, dval);
7332 else if (variant_field_index (type0) >= 0)
7334 if (dval == NULL && valaddr == NULL && address == 0)
7336 return to_record_with_fixed_variant_part (type0, valaddr, address,
7341 TYPE_FIXED_INSTANCE (type0) = 1;
7347 /* An ordinary record type (with fixed-length fields) that describes
7348 the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
7349 union type. Any necessary discriminants' values should be in DVAL,
7350 a record value. That is, this routine selects the appropriate
7351 branch of the union at ADDR according to the discriminant value
7352 indicated in the union's type name. Returns VAR_TYPE0 itself if
7353 it represents a variant subject to a pragma Unchecked_Union. */
7355 static struct type *
7356 to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
7357 CORE_ADDR address, struct value *dval)
7360 struct type *templ_type;
7361 struct type *var_type;
7363 if (TYPE_CODE (var_type0) == TYPE_CODE_PTR)
7364 var_type = TYPE_TARGET_TYPE (var_type0);
7366 var_type = var_type0;
7368 templ_type = ada_find_parallel_type (var_type, "___XVU");
7370 if (templ_type != NULL)
7371 var_type = templ_type;
7373 if (is_unchecked_variant (var_type, value_type (dval)))
7376 ada_which_variant_applies (var_type,
7377 value_type (dval), value_contents (dval));
7380 return empty_record (var_type);
7381 else if (is_dynamic_field (var_type, which))
7382 return to_fixed_record_type
7383 (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (var_type, which)),
7384 valaddr, address, dval);
7385 else if (variant_field_index (TYPE_FIELD_TYPE (var_type, which)) >= 0)
7387 to_fixed_record_type
7388 (TYPE_FIELD_TYPE (var_type, which), valaddr, address, dval);
7390 return TYPE_FIELD_TYPE (var_type, which);
7393 /* Assuming that TYPE0 is an array type describing the type of a value
7394 at ADDR, and that DVAL describes a record containing any
7395 discriminants used in TYPE0, returns a type for the value that
7396 contains no dynamic components (that is, no components whose sizes
7397 are determined by run-time quantities). Unless IGNORE_TOO_BIG is
7398 true, gives an error message if the resulting type's size is over
7401 static struct type *
7402 to_fixed_array_type (struct type *type0, struct value *dval,
7405 struct type *index_type_desc;
7406 struct type *result;
7407 int constrained_packed_array_p;
7409 if (TYPE_FIXED_INSTANCE (type0))
7412 constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
7413 if (constrained_packed_array_p)
7414 type0 = decode_constrained_packed_array_type (type0);
7416 index_type_desc = ada_find_parallel_type (type0, "___XA");
7417 ada_fixup_array_indexes_type (index_type_desc);
7418 if (index_type_desc == NULL)
7420 struct type *elt_type0 = ada_check_typedef (TYPE_TARGET_TYPE (type0));
7422 /* NOTE: elt_type---the fixed version of elt_type0---should never
7423 depend on the contents of the array in properly constructed
7425 /* Create a fixed version of the array element type.
7426 We're not providing the address of an element here,
7427 and thus the actual object value cannot be inspected to do
7428 the conversion. This should not be a problem, since arrays of
7429 unconstrained objects are not allowed. In particular, all
7430 the elements of an array of a tagged type should all be of
7431 the same type specified in the debugging info. No need to
7432 consult the object tag. */
7433 struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
7435 /* Make sure we always create a new array type when dealing with
7436 packed array types, since we're going to fix-up the array
7437 type length and element bitsize a little further down. */
7438 if (elt_type0 == elt_type && !constrained_packed_array_p)
7441 result = create_array_type (alloc_type_copy (type0),
7442 elt_type, TYPE_INDEX_TYPE (type0));
7447 struct type *elt_type0;
7450 for (i = TYPE_NFIELDS (index_type_desc); i > 0; i -= 1)
7451 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
7453 /* NOTE: result---the fixed version of elt_type0---should never
7454 depend on the contents of the array in properly constructed
7456 /* Create a fixed version of the array element type.
7457 We're not providing the address of an element here,
7458 and thus the actual object value cannot be inspected to do
7459 the conversion. This should not be a problem, since arrays of
7460 unconstrained objects are not allowed. In particular, all
7461 the elements of an array of a tagged type should all be of
7462 the same type specified in the debugging info. No need to
7463 consult the object tag. */
7465 ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
7468 for (i = TYPE_NFIELDS (index_type_desc) - 1; i >= 0; i -= 1)
7470 struct type *range_type =
7471 to_fixed_range_type (TYPE_FIELD_TYPE (index_type_desc, i), dval);
7473 result = create_array_type (alloc_type_copy (elt_type0),
7474 result, range_type);
7475 elt_type0 = TYPE_TARGET_TYPE (elt_type0);
7477 if (!ignore_too_big && TYPE_LENGTH (result) > varsize_limit)
7478 error (_("array type with dynamic size is larger than varsize-limit"));
7481 if (constrained_packed_array_p)
7483 /* So far, the resulting type has been created as if the original
7484 type was a regular (non-packed) array type. As a result, the
7485 bitsize of the array elements needs to be set again, and the array
7486 length needs to be recomputed based on that bitsize. */
7487 int len = TYPE_LENGTH (result) / TYPE_LENGTH (TYPE_TARGET_TYPE (result));
7488 int elt_bitsize = TYPE_FIELD_BITSIZE (type0, 0);
7490 TYPE_FIELD_BITSIZE (result, 0) = TYPE_FIELD_BITSIZE (type0, 0);
7491 TYPE_LENGTH (result) = len * elt_bitsize / HOST_CHAR_BIT;
7492 if (TYPE_LENGTH (result) * HOST_CHAR_BIT < len * elt_bitsize)
7493 TYPE_LENGTH (result)++;
7496 TYPE_FIXED_INSTANCE (result) = 1;
7501 /* A standard type (containing no dynamically sized components)
7502 corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
7503 DVAL describes a record containing any discriminants used in TYPE0,
7504 and may be NULL if there are none, or if the object of type TYPE at
7505 ADDRESS or in VALADDR contains these discriminants.
7507 If CHECK_TAG is not null, in the case of tagged types, this function
7508 attempts to locate the object's tag and use it to compute the actual
7509 type. However, when ADDRESS is null, we cannot use it to determine the
7510 location of the tag, and therefore compute the tagged type's actual type.
7511 So we return the tagged type without consulting the tag. */
7513 static struct type *
7514 ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
7515 CORE_ADDR address, struct value *dval, int check_tag)
7517 type = ada_check_typedef (type);
7518 switch (TYPE_CODE (type))
7522 case TYPE_CODE_STRUCT:
7524 struct type *static_type = to_static_fixed_type (type);
7525 struct type *fixed_record_type =
7526 to_fixed_record_type (type, valaddr, address, NULL);
7528 /* If STATIC_TYPE is a tagged type and we know the object's address,
7529 then we can determine its tag, and compute the object's actual
7530 type from there. Note that we have to use the fixed record
7531 type (the parent part of the record may have dynamic fields
7532 and the way the location of _tag is expressed may depend on
7535 if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
7537 struct type *real_type =
7538 type_from_tag (value_tag_from_contents_and_address
7543 if (real_type != NULL)
7544 return to_fixed_record_type (real_type, valaddr, address, NULL);
7547 /* Check to see if there is a parallel ___XVZ variable.
7548 If there is, then it provides the actual size of our type. */
7549 else if (ada_type_name (fixed_record_type) != NULL)
7551 char *name = ada_type_name (fixed_record_type);
7552 char *xvz_name = alloca (strlen (name) + 7 /* "___XVZ\0" */);
7556 xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
7557 size = get_int_var_value (xvz_name, &xvz_found);
7558 if (xvz_found && TYPE_LENGTH (fixed_record_type) != size)
7560 fixed_record_type = copy_type (fixed_record_type);
7561 TYPE_LENGTH (fixed_record_type) = size;
7563 /* The FIXED_RECORD_TYPE may have be a stub. We have
7564 observed this when the debugging info is STABS, and
7565 apparently it is something that is hard to fix.
7567 In practice, we don't need the actual type definition
7568 at all, because the presence of the XVZ variable allows us
7569 to assume that there must be a XVS type as well, which we
7570 should be able to use later, when we need the actual type
7573 In the meantime, pretend that the "fixed" type we are
7574 returning is NOT a stub, because this can cause trouble
7575 when using this type to create new types targeting it.
7576 Indeed, the associated creation routines often check
7577 whether the target type is a stub and will try to replace
7578 it, thus using a type with the wrong size. This, in turn,
7579 might cause the new type to have the wrong size too.
7580 Consider the case of an array, for instance, where the size
7581 of the array is computed from the number of elements in
7582 our array multiplied by the size of its element. */
7583 TYPE_STUB (fixed_record_type) = 0;
7586 return fixed_record_type;
7588 case TYPE_CODE_ARRAY:
7589 return to_fixed_array_type (type, dval, 1);
7590 case TYPE_CODE_UNION:
7594 return to_fixed_variant_branch_type (type, valaddr, address, dval);
7598 /* The same as ada_to_fixed_type_1, except that it preserves the type
7599 if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
7600 ada_to_fixed_type_1 would return the type referenced by TYPE. */
7603 ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
7604 CORE_ADDR address, struct value *dval, int check_tag)
7607 struct type *fixed_type =
7608 ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
7610 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF
7611 && TYPE_TARGET_TYPE (type) == fixed_type)
7617 /* A standard (static-sized) type corresponding as well as possible to
7618 TYPE0, but based on no runtime data. */
7620 static struct type *
7621 to_static_fixed_type (struct type *type0)
7628 if (TYPE_FIXED_INSTANCE (type0))
7631 type0 = ada_check_typedef (type0);
7633 switch (TYPE_CODE (type0))
7637 case TYPE_CODE_STRUCT:
7638 type = dynamic_template_type (type0);
7640 return template_to_static_fixed_type (type);
7642 return template_to_static_fixed_type (type0);
7643 case TYPE_CODE_UNION:
7644 type = ada_find_parallel_type (type0, "___XVU");
7646 return template_to_static_fixed_type (type);
7648 return template_to_static_fixed_type (type0);
7652 /* A static approximation of TYPE with all type wrappers removed. */
7654 static struct type *
7655 static_unwrap_type (struct type *type)
7657 if (ada_is_aligner_type (type))
7659 struct type *type1 = TYPE_FIELD_TYPE (ada_check_typedef (type), 0);
7660 if (ada_type_name (type1) == NULL)
7661 TYPE_NAME (type1) = ada_type_name (type);
7663 return static_unwrap_type (type1);
7667 struct type *raw_real_type = ada_get_base_type (type);
7669 if (raw_real_type == type)
7672 return to_static_fixed_type (raw_real_type);
7676 /* In some cases, incomplete and private types require
7677 cross-references that are not resolved as records (for example,
7679 type FooP is access Foo;
7681 type Foo is array ...;
7682 ). In these cases, since there is no mechanism for producing
7683 cross-references to such types, we instead substitute for FooP a
7684 stub enumeration type that is nowhere resolved, and whose tag is
7685 the name of the actual type. Call these types "non-record stubs". */
7687 /* A type equivalent to TYPE that is not a non-record stub, if one
7688 exists, otherwise TYPE. */
7691 ada_check_typedef (struct type *type)
7696 CHECK_TYPEDEF (type);
7697 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_ENUM
7698 || !TYPE_STUB (type)
7699 || TYPE_TAG_NAME (type) == NULL)
7703 char *name = TYPE_TAG_NAME (type);
7704 struct type *type1 = ada_find_any_type (name);
7709 /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
7710 stubs pointing to arrays, as we don't create symbols for array
7711 types, only for the typedef-to-array types). This is why
7712 we process TYPE1 with ada_check_typedef before returning
7714 return ada_check_typedef (type1);
7718 /* A value representing the data at VALADDR/ADDRESS as described by
7719 type TYPE0, but with a standard (static-sized) type that correctly
7720 describes it. If VAL0 is not NULL and TYPE0 already is a standard
7721 type, then return VAL0 [this feature is simply to avoid redundant
7722 creation of struct values]. */
7724 static struct value *
7725 ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
7728 struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
7730 if (type == type0 && val0 != NULL)
7733 return value_from_contents_and_address (type, 0, address);
7736 /* A value representing VAL, but with a standard (static-sized) type
7737 that correctly describes it. Does not necessarily create a new
7741 ada_to_fixed_value (struct value *val)
7743 return ada_to_fixed_value_create (value_type (val),
7744 value_address (val),
7751 /* Table mapping attribute numbers to names.
7752 NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
7754 static const char *attribute_names[] = {
7772 ada_attribute_name (enum exp_opcode n)
7774 if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
7775 return attribute_names[n - OP_ATR_FIRST + 1];
7777 return attribute_names[0];
7780 /* Evaluate the 'POS attribute applied to ARG. */
7783 pos_atr (struct value *arg)
7785 struct value *val = coerce_ref (arg);
7786 struct type *type = value_type (val);
7788 if (!discrete_type_p (type))
7789 error (_("'POS only defined on discrete types"));
7791 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
7794 LONGEST v = value_as_long (val);
7796 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
7798 if (v == TYPE_FIELD_BITPOS (type, i))
7801 error (_("enumeration value is invalid: can't find 'POS"));
7804 return value_as_long (val);
7807 static struct value *
7808 value_pos_atr (struct type *type, struct value *arg)
7810 return value_from_longest (type, pos_atr (arg));
7813 /* Evaluate the TYPE'VAL attribute applied to ARG. */
7815 static struct value *
7816 value_val_atr (struct type *type, struct value *arg)
7818 if (!discrete_type_p (type))
7819 error (_("'VAL only defined on discrete types"));
7820 if (!integer_type_p (value_type (arg)))
7821 error (_("'VAL requires integral argument"));
7823 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
7825 long pos = value_as_long (arg);
7827 if (pos < 0 || pos >= TYPE_NFIELDS (type))
7828 error (_("argument to 'VAL out of range"));
7829 return value_from_longest (type, TYPE_FIELD_BITPOS (type, pos));
7832 return value_from_longest (type, value_as_long (arg));
7838 /* True if TYPE appears to be an Ada character type.
7839 [At the moment, this is true only for Character and Wide_Character;
7840 It is a heuristic test that could stand improvement]. */
7843 ada_is_character_type (struct type *type)
7847 /* If the type code says it's a character, then assume it really is,
7848 and don't check any further. */
7849 if (TYPE_CODE (type) == TYPE_CODE_CHAR)
7852 /* Otherwise, assume it's a character type iff it is a discrete type
7853 with a known character type name. */
7854 name = ada_type_name (type);
7855 return (name != NULL
7856 && (TYPE_CODE (type) == TYPE_CODE_INT
7857 || TYPE_CODE (type) == TYPE_CODE_RANGE)
7858 && (strcmp (name, "character") == 0
7859 || strcmp (name, "wide_character") == 0
7860 || strcmp (name, "wide_wide_character") == 0
7861 || strcmp (name, "unsigned char") == 0));
7864 /* True if TYPE appears to be an Ada string type. */
7867 ada_is_string_type (struct type *type)
7869 type = ada_check_typedef (type);
7871 && TYPE_CODE (type) != TYPE_CODE_PTR
7872 && (ada_is_simple_array_type (type)
7873 || ada_is_array_descriptor_type (type))
7874 && ada_array_arity (type) == 1)
7876 struct type *elttype = ada_array_element_type (type, 1);
7878 return ada_is_character_type (elttype);
7884 /* The compiler sometimes provides a parallel XVS type for a given
7885 PAD type. Normally, it is safe to follow the PAD type directly,
7886 but older versions of the compiler have a bug that causes the offset
7887 of its "F" field to be wrong. Following that field in that case
7888 would lead to incorrect results, but this can be worked around
7889 by ignoring the PAD type and using the associated XVS type instead.
7891 Set to True if the debugger should trust the contents of PAD types.
7892 Otherwise, ignore the PAD type if there is a parallel XVS type. */
7893 static int trust_pad_over_xvs = 1;
7895 /* True if TYPE is a struct type introduced by the compiler to force the
7896 alignment of a value. Such types have a single field with a
7897 distinctive name. */
7900 ada_is_aligner_type (struct type *type)
7902 type = ada_check_typedef (type);
7904 if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
7907 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
7908 && TYPE_NFIELDS (type) == 1
7909 && strcmp (TYPE_FIELD_NAME (type, 0), "F") == 0);
7912 /* If there is an ___XVS-convention type parallel to SUBTYPE, return
7913 the parallel type. */
7916 ada_get_base_type (struct type *raw_type)
7918 struct type *real_type_namer;
7919 struct type *raw_real_type;
7921 if (raw_type == NULL || TYPE_CODE (raw_type) != TYPE_CODE_STRUCT)
7924 if (ada_is_aligner_type (raw_type))
7925 /* The encoding specifies that we should always use the aligner type.
7926 So, even if this aligner type has an associated XVS type, we should
7929 According to the compiler gurus, an XVS type parallel to an aligner
7930 type may exist because of a stabs limitation. In stabs, aligner
7931 types are empty because the field has a variable-sized type, and
7932 thus cannot actually be used as an aligner type. As a result,
7933 we need the associated parallel XVS type to decode the type.
7934 Since the policy in the compiler is to not change the internal
7935 representation based on the debugging info format, we sometimes
7936 end up having a redundant XVS type parallel to the aligner type. */
7939 real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
7940 if (real_type_namer == NULL
7941 || TYPE_CODE (real_type_namer) != TYPE_CODE_STRUCT
7942 || TYPE_NFIELDS (real_type_namer) != 1)
7945 if (TYPE_CODE (TYPE_FIELD_TYPE (real_type_namer, 0)) != TYPE_CODE_REF)
7947 /* This is an older encoding form where the base type needs to be
7948 looked up by name. We prefer the newer enconding because it is
7950 raw_real_type = ada_find_any_type (TYPE_FIELD_NAME (real_type_namer, 0));
7951 if (raw_real_type == NULL)
7954 return raw_real_type;
7957 /* The field in our XVS type is a reference to the base type. */
7958 return TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (real_type_namer, 0));
7961 /* The type of value designated by TYPE, with all aligners removed. */
7964 ada_aligned_type (struct type *type)
7966 if (ada_is_aligner_type (type))
7967 return ada_aligned_type (TYPE_FIELD_TYPE (type, 0));
7969 return ada_get_base_type (type);
7973 /* The address of the aligned value in an object at address VALADDR
7974 having type TYPE. Assumes ada_is_aligner_type (TYPE). */
7977 ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
7979 if (ada_is_aligner_type (type))
7980 return ada_aligned_value_addr (TYPE_FIELD_TYPE (type, 0),
7982 TYPE_FIELD_BITPOS (type,
7983 0) / TARGET_CHAR_BIT);
7990 /* The printed representation of an enumeration literal with encoded
7991 name NAME. The value is good to the next call of ada_enum_name. */
7993 ada_enum_name (const char *name)
7995 static char *result;
7996 static size_t result_len = 0;
7999 /* First, unqualify the enumeration name:
8000 1. Search for the last '.' character. If we find one, then skip
8001 all the preceeding characters, the unqualified name starts
8002 right after that dot.
8003 2. Otherwise, we may be debugging on a target where the compiler
8004 translates dots into "__". Search forward for double underscores,
8005 but stop searching when we hit an overloading suffix, which is
8006 of the form "__" followed by digits. */
8008 tmp = strrchr (name, '.');
8013 while ((tmp = strstr (name, "__")) != NULL)
8015 if (isdigit (tmp[2]))
8026 if (name[1] == 'U' || name[1] == 'W')
8028 if (sscanf (name + 2, "%x", &v) != 1)
8034 GROW_VECT (result, result_len, 16);
8035 if (isascii (v) && isprint (v))
8036 xsnprintf (result, result_len, "'%c'", v);
8037 else if (name[1] == 'U')
8038 xsnprintf (result, result_len, "[\"%02x\"]", v);
8040 xsnprintf (result, result_len, "[\"%04x\"]", v);
8046 tmp = strstr (name, "__");
8048 tmp = strstr (name, "$");
8051 GROW_VECT (result, result_len, tmp - name + 1);
8052 strncpy (result, name, tmp - name);
8053 result[tmp - name] = '\0';
8061 /* Evaluate the subexpression of EXP starting at *POS as for
8062 evaluate_type, updating *POS to point just past the evaluated
8065 static struct value *
8066 evaluate_subexp_type (struct expression *exp, int *pos)
8068 return evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
8071 /* If VAL is wrapped in an aligner or subtype wrapper, return the
8074 static struct value *
8075 unwrap_value (struct value *val)
8077 struct type *type = ada_check_typedef (value_type (val));
8079 if (ada_is_aligner_type (type))
8081 struct value *v = ada_value_struct_elt (val, "F", 0);
8082 struct type *val_type = ada_check_typedef (value_type (v));
8084 if (ada_type_name (val_type) == NULL)
8085 TYPE_NAME (val_type) = ada_type_name (type);
8087 return unwrap_value (v);
8091 struct type *raw_real_type =
8092 ada_check_typedef (ada_get_base_type (type));
8094 /* If there is no parallel XVS or XVE type, then the value is
8095 already unwrapped. Return it without further modification. */
8096 if ((type == raw_real_type)
8097 && ada_find_parallel_type (type, "___XVE") == NULL)
8101 coerce_unspec_val_to_type
8102 (val, ada_to_fixed_type (raw_real_type, 0,
8103 value_address (val),
8108 static struct value *
8109 cast_to_fixed (struct type *type, struct value *arg)
8113 if (type == value_type (arg))
8115 else if (ada_is_fixed_point_type (value_type (arg)))
8116 val = ada_float_to_fixed (type,
8117 ada_fixed_to_float (value_type (arg),
8118 value_as_long (arg)));
8121 DOUBLEST argd = value_as_double (arg);
8123 val = ada_float_to_fixed (type, argd);
8126 return value_from_longest (type, val);
8129 static struct value *
8130 cast_from_fixed (struct type *type, struct value *arg)
8132 DOUBLEST val = ada_fixed_to_float (value_type (arg),
8133 value_as_long (arg));
8135 return value_from_double (type, val);
8138 /* Coerce VAL as necessary for assignment to an lval of type TYPE, and
8139 return the converted value. */
8141 static struct value *
8142 coerce_for_assign (struct type *type, struct value *val)
8144 struct type *type2 = value_type (val);
8149 type2 = ada_check_typedef (type2);
8150 type = ada_check_typedef (type);
8152 if (TYPE_CODE (type2) == TYPE_CODE_PTR
8153 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
8155 val = ada_value_ind (val);
8156 type2 = value_type (val);
8159 if (TYPE_CODE (type2) == TYPE_CODE_ARRAY
8160 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
8162 if (TYPE_LENGTH (type2) != TYPE_LENGTH (type)
8163 || TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
8164 != TYPE_LENGTH (TYPE_TARGET_TYPE (type2)))
8165 error (_("Incompatible types in assignment"));
8166 deprecated_set_value_type (val, type);
8171 static struct value *
8172 ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
8175 struct type *type1, *type2;
8178 arg1 = coerce_ref (arg1);
8179 arg2 = coerce_ref (arg2);
8180 type1 = base_type (ada_check_typedef (value_type (arg1)));
8181 type2 = base_type (ada_check_typedef (value_type (arg2)));
8183 if (TYPE_CODE (type1) != TYPE_CODE_INT
8184 || TYPE_CODE (type2) != TYPE_CODE_INT)
8185 return value_binop (arg1, arg2, op);
8194 return value_binop (arg1, arg2, op);
8197 v2 = value_as_long (arg2);
8199 error (_("second operand of %s must not be zero."), op_string (op));
8201 if (TYPE_UNSIGNED (type1) || op == BINOP_MOD)
8202 return value_binop (arg1, arg2, op);
8204 v1 = value_as_long (arg1);
8209 if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
8210 v += v > 0 ? -1 : 1;
8218 /* Should not reach this point. */
8222 val = allocate_value (type1);
8223 store_unsigned_integer (value_contents_raw (val),
8224 TYPE_LENGTH (value_type (val)),
8225 gdbarch_byte_order (get_type_arch (type1)), v);
8230 ada_value_equal (struct value *arg1, struct value *arg2)
8232 if (ada_is_direct_array_type (value_type (arg1))
8233 || ada_is_direct_array_type (value_type (arg2)))
8235 /* Automatically dereference any array reference before
8236 we attempt to perform the comparison. */
8237 arg1 = ada_coerce_ref (arg1);
8238 arg2 = ada_coerce_ref (arg2);
8240 arg1 = ada_coerce_to_simple_array (arg1);
8241 arg2 = ada_coerce_to_simple_array (arg2);
8242 if (TYPE_CODE (value_type (arg1)) != TYPE_CODE_ARRAY
8243 || TYPE_CODE (value_type (arg2)) != TYPE_CODE_ARRAY)
8244 error (_("Attempt to compare array with non-array"));
8245 /* FIXME: The following works only for types whose
8246 representations use all bits (no padding or undefined bits)
8247 and do not have user-defined equality. */
8249 TYPE_LENGTH (value_type (arg1)) == TYPE_LENGTH (value_type (arg2))
8250 && memcmp (value_contents (arg1), value_contents (arg2),
8251 TYPE_LENGTH (value_type (arg1))) == 0;
8253 return value_equal (arg1, arg2);
8256 /* Total number of component associations in the aggregate starting at
8257 index PC in EXP. Assumes that index PC is the start of an
8261 num_component_specs (struct expression *exp, int pc)
8265 m = exp->elts[pc + 1].longconst;
8268 for (i = 0; i < m; i += 1)
8270 switch (exp->elts[pc].opcode)
8276 n += exp->elts[pc + 1].longconst;
8279 ada_evaluate_subexp (NULL, exp, &pc, EVAL_SKIP);
8284 /* Assign the result of evaluating EXP starting at *POS to the INDEXth
8285 component of LHS (a simple array or a record), updating *POS past
8286 the expression, assuming that LHS is contained in CONTAINER. Does
8287 not modify the inferior's memory, nor does it modify LHS (unless
8288 LHS == CONTAINER). */
8291 assign_component (struct value *container, struct value *lhs, LONGEST index,
8292 struct expression *exp, int *pos)
8294 struct value *mark = value_mark ();
8297 if (TYPE_CODE (value_type (lhs)) == TYPE_CODE_ARRAY)
8299 struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
8300 struct value *index_val = value_from_longest (index_type, index);
8302 elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
8306 elt = ada_index_struct_field (index, lhs, 0, value_type (lhs));
8307 elt = ada_to_fixed_value (unwrap_value (elt));
8310 if (exp->elts[*pos].opcode == OP_AGGREGATE)
8311 assign_aggregate (container, elt, exp, pos, EVAL_NORMAL);
8313 value_assign_to_component (container, elt,
8314 ada_evaluate_subexp (NULL, exp, pos,
8317 value_free_to_mark (mark);
8320 /* Assuming that LHS represents an lvalue having a record or array
8321 type, and EXP->ELTS[*POS] is an OP_AGGREGATE, evaluate an assignment
8322 of that aggregate's value to LHS, advancing *POS past the
8323 aggregate. NOSIDE is as for evaluate_subexp. CONTAINER is an
8324 lvalue containing LHS (possibly LHS itself). Does not modify
8325 the inferior's memory, nor does it modify the contents of
8326 LHS (unless == CONTAINER). Returns the modified CONTAINER. */
8328 static struct value *
8329 assign_aggregate (struct value *container,
8330 struct value *lhs, struct expression *exp,
8331 int *pos, enum noside noside)
8333 struct type *lhs_type;
8334 int n = exp->elts[*pos+1].longconst;
8335 LONGEST low_index, high_index;
8338 int max_indices, num_indices;
8339 int is_array_aggregate;
8343 if (noside != EVAL_NORMAL)
8347 for (i = 0; i < n; i += 1)
8348 ada_evaluate_subexp (NULL, exp, pos, noside);
8352 container = ada_coerce_ref (container);
8353 if (ada_is_direct_array_type (value_type (container)))
8354 container = ada_coerce_to_simple_array (container);
8355 lhs = ada_coerce_ref (lhs);
8356 if (!deprecated_value_modifiable (lhs))
8357 error (_("Left operand of assignment is not a modifiable lvalue."));
8359 lhs_type = value_type (lhs);
8360 if (ada_is_direct_array_type (lhs_type))
8362 lhs = ada_coerce_to_simple_array (lhs);
8363 lhs_type = value_type (lhs);
8364 low_index = TYPE_ARRAY_LOWER_BOUND_VALUE (lhs_type);
8365 high_index = TYPE_ARRAY_UPPER_BOUND_VALUE (lhs_type);
8366 is_array_aggregate = 1;
8368 else if (TYPE_CODE (lhs_type) == TYPE_CODE_STRUCT)
8371 high_index = num_visible_fields (lhs_type) - 1;
8372 is_array_aggregate = 0;
8375 error (_("Left-hand side must be array or record."));
8377 num_specs = num_component_specs (exp, *pos - 3);
8378 max_indices = 4 * num_specs + 4;
8379 indices = alloca (max_indices * sizeof (indices[0]));
8380 indices[0] = indices[1] = low_index - 1;
8381 indices[2] = indices[3] = high_index + 1;
8384 for (i = 0; i < n; i += 1)
8386 switch (exp->elts[*pos].opcode)
8389 aggregate_assign_from_choices (container, lhs, exp, pos, indices,
8390 &num_indices, max_indices,
8391 low_index, high_index);
8394 aggregate_assign_positional (container, lhs, exp, pos, indices,
8395 &num_indices, max_indices,
8396 low_index, high_index);
8400 error (_("Misplaced 'others' clause"));
8401 aggregate_assign_others (container, lhs, exp, pos, indices,
8402 num_indices, low_index, high_index);
8405 error (_("Internal error: bad aggregate clause"));
8412 /* Assign into the component of LHS indexed by the OP_POSITIONAL
8413 construct at *POS, updating *POS past the construct, given that
8414 the positions are relative to lower bound LOW, where HIGH is the
8415 upper bound. Record the position in INDICES[0 .. MAX_INDICES-1]
8416 updating *NUM_INDICES as needed. CONTAINER is as for
8417 assign_aggregate. */
8419 aggregate_assign_positional (struct value *container,
8420 struct value *lhs, struct expression *exp,
8421 int *pos, LONGEST *indices, int *num_indices,
8422 int max_indices, LONGEST low, LONGEST high)
8424 LONGEST ind = longest_to_int (exp->elts[*pos + 1].longconst) + low;
8426 if (ind - 1 == high)
8427 warning (_("Extra components in aggregate ignored."));
8430 add_component_interval (ind, ind, indices, num_indices, max_indices);
8432 assign_component (container, lhs, ind, exp, pos);
8435 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
8438 /* Assign into the components of LHS indexed by the OP_CHOICES
8439 construct at *POS, updating *POS past the construct, given that
8440 the allowable indices are LOW..HIGH. Record the indices assigned
8441 to in INDICES[0 .. MAX_INDICES-1], updating *NUM_INDICES as
8442 needed. CONTAINER is as for assign_aggregate. */
8444 aggregate_assign_from_choices (struct value *container,
8445 struct value *lhs, struct expression *exp,
8446 int *pos, LONGEST *indices, int *num_indices,
8447 int max_indices, LONGEST low, LONGEST high)
8450 int n_choices = longest_to_int (exp->elts[*pos+1].longconst);
8451 int choice_pos, expr_pc;
8452 int is_array = ada_is_direct_array_type (value_type (lhs));
8454 choice_pos = *pos += 3;
8456 for (j = 0; j < n_choices; j += 1)
8457 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
8459 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
8461 for (j = 0; j < n_choices; j += 1)
8463 LONGEST lower, upper;
8464 enum exp_opcode op = exp->elts[choice_pos].opcode;
8466 if (op == OP_DISCRETE_RANGE)
8469 lower = value_as_long (ada_evaluate_subexp (NULL, exp, pos,
8471 upper = value_as_long (ada_evaluate_subexp (NULL, exp, pos,
8476 lower = value_as_long (ada_evaluate_subexp (NULL, exp, &choice_pos,
8488 name = &exp->elts[choice_pos + 2].string;
8491 name = SYMBOL_NATURAL_NAME (exp->elts[choice_pos + 2].symbol);
8494 error (_("Invalid record component association."));
8496 ada_evaluate_subexp (NULL, exp, &choice_pos, EVAL_SKIP);
8498 if (! find_struct_field (name, value_type (lhs), 0,
8499 NULL, NULL, NULL, NULL, &ind))
8500 error (_("Unknown component name: %s."), name);
8501 lower = upper = ind;
8504 if (lower <= upper && (lower < low || upper > high))
8505 error (_("Index in component association out of bounds."));
8507 add_component_interval (lower, upper, indices, num_indices,
8509 while (lower <= upper)
8514 assign_component (container, lhs, lower, exp, &pos1);
8520 /* Assign the value of the expression in the OP_OTHERS construct in
8521 EXP at *POS into the components of LHS indexed from LOW .. HIGH that
8522 have not been previously assigned. The index intervals already assigned
8523 are in INDICES[0 .. NUM_INDICES-1]. Updates *POS to after the
8524 OP_OTHERS clause. CONTAINER is as for assign_aggregate*/
8526 aggregate_assign_others (struct value *container,
8527 struct value *lhs, struct expression *exp,
8528 int *pos, LONGEST *indices, int num_indices,
8529 LONGEST low, LONGEST high)
8532 int expr_pc = *pos+1;
8534 for (i = 0; i < num_indices - 2; i += 2)
8538 for (ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
8543 assign_component (container, lhs, ind, exp, &pos);
8546 ada_evaluate_subexp (NULL, exp, pos, EVAL_SKIP);
8549 /* Add the interval [LOW .. HIGH] to the sorted set of intervals
8550 [ INDICES[0] .. INDICES[1] ],..., [ INDICES[*SIZE-2] .. INDICES[*SIZE-1] ],
8551 modifying *SIZE as needed. It is an error if *SIZE exceeds
8552 MAX_SIZE. The resulting intervals do not overlap. */
8554 add_component_interval (LONGEST low, LONGEST high,
8555 LONGEST* indices, int *size, int max_size)
8559 for (i = 0; i < *size; i += 2) {
8560 if (high >= indices[i] && low <= indices[i + 1])
8564 for (kh = i + 2; kh < *size; kh += 2)
8565 if (high < indices[kh])
8567 if (low < indices[i])
8569 indices[i + 1] = indices[kh - 1];
8570 if (high > indices[i + 1])
8571 indices[i + 1] = high;
8572 memcpy (indices + i + 2, indices + kh, *size - kh);
8573 *size -= kh - i - 2;
8576 else if (high < indices[i])
8580 if (*size == max_size)
8581 error (_("Internal error: miscounted aggregate components."));
8583 for (j = *size-1; j >= i+2; j -= 1)
8584 indices[j] = indices[j - 2];
8586 indices[i + 1] = high;
8589 /* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
8592 static struct value *
8593 ada_value_cast (struct type *type, struct value *arg2, enum noside noside)
8595 if (type == ada_check_typedef (value_type (arg2)))
8598 if (ada_is_fixed_point_type (type))
8599 return (cast_to_fixed (type, arg2));
8601 if (ada_is_fixed_point_type (value_type (arg2)))
8602 return cast_from_fixed (type, arg2);
8604 return value_cast (type, arg2);
8607 /* Evaluating Ada expressions, and printing their result.
8608 ------------------------------------------------------
8613 We usually evaluate an Ada expression in order to print its value.
8614 We also evaluate an expression in order to print its type, which
8615 happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
8616 but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
8617 EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
8618 the evaluation compared to the EVAL_NORMAL, but is otherwise very
8621 Evaluating expressions is a little more complicated for Ada entities
8622 than it is for entities in languages such as C. The main reason for
8623 this is that Ada provides types whose definition might be dynamic.
8624 One example of such types is variant records. Or another example
8625 would be an array whose bounds can only be known at run time.
8627 The following description is a general guide as to what should be
8628 done (and what should NOT be done) in order to evaluate an expression
8629 involving such types, and when. This does not cover how the semantic
8630 information is encoded by GNAT as this is covered separatly. For the
8631 document used as the reference for the GNAT encoding, see exp_dbug.ads
8632 in the GNAT sources.
8634 Ideally, we should embed each part of this description next to its
8635 associated code. Unfortunately, the amount of code is so vast right
8636 now that it's hard to see whether the code handling a particular
8637 situation might be duplicated or not. One day, when the code is
8638 cleaned up, this guide might become redundant with the comments
8639 inserted in the code, and we might want to remove it.
8641 2. ``Fixing'' an Entity, the Simple Case:
8642 -----------------------------------------
8644 When evaluating Ada expressions, the tricky issue is that they may
8645 reference entities whose type contents and size are not statically
8646 known. Consider for instance a variant record:
8648 type Rec (Empty : Boolean := True) is record
8651 when False => Value : Integer;
8654 Yes : Rec := (Empty => False, Value => 1);
8655 No : Rec := (empty => True);
8657 The size and contents of that record depends on the value of the
8658 descriminant (Rec.Empty). At this point, neither the debugging
8659 information nor the associated type structure in GDB are able to
8660 express such dynamic types. So what the debugger does is to create
8661 "fixed" versions of the type that applies to the specific object.
8662 We also informally refer to this opperation as "fixing" an object,
8663 which means creating its associated fixed type.
8665 Example: when printing the value of variable "Yes" above, its fixed
8666 type would look like this:
8673 On the other hand, if we printed the value of "No", its fixed type
8680 Things become a little more complicated when trying to fix an entity
8681 with a dynamic type that directly contains another dynamic type,
8682 such as an array of variant records, for instance. There are
8683 two possible cases: Arrays, and records.
8685 3. ``Fixing'' Arrays:
8686 ---------------------
8688 The type structure in GDB describes an array in terms of its bounds,
8689 and the type of its elements. By design, all elements in the array
8690 have the same type and we cannot represent an array of variant elements
8691 using the current type structure in GDB. When fixing an array,
8692 we cannot fix the array element, as we would potentially need one
8693 fixed type per element of the array. As a result, the best we can do
8694 when fixing an array is to produce an array whose bounds and size
8695 are correct (allowing us to read it from memory), but without having
8696 touched its element type. Fixing each element will be done later,
8697 when (if) necessary.
8699 Arrays are a little simpler to handle than records, because the same
8700 amount of memory is allocated for each element of the array, even if
8701 the amount of space actually used by each element differs from element
8702 to element. Consider for instance the following array of type Rec:
8704 type Rec_Array is array (1 .. 2) of Rec;
8706 The actual amount of memory occupied by each element might be different
8707 from element to element, depending on the value of their discriminant.
8708 But the amount of space reserved for each element in the array remains
8709 fixed regardless. So we simply need to compute that size using
8710 the debugging information available, from which we can then determine
8711 the array size (we multiply the number of elements of the array by
8712 the size of each element).
8714 The simplest case is when we have an array of a constrained element
8715 type. For instance, consider the following type declarations:
8717 type Bounded_String (Max_Size : Integer) is
8719 Buffer : String (1 .. Max_Size);
8721 type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
8723 In this case, the compiler describes the array as an array of
8724 variable-size elements (identified by its XVS suffix) for which
8725 the size can be read in the parallel XVZ variable.
8727 In the case of an array of an unconstrained element type, the compiler
8728 wraps the array element inside a private PAD type. This type should not
8729 be shown to the user, and must be "unwrap"'ed before printing. Note
8730 that we also use the adjective "aligner" in our code to designate
8731 these wrapper types.
8733 In some cases, the size allocated for each element is statically
8734 known. In that case, the PAD type already has the correct size,
8735 and the array element should remain unfixed.
8737 But there are cases when this size is not statically known.
8738 For instance, assuming that "Five" is an integer variable:
8740 type Dynamic is array (1 .. Five) of Integer;
8741 type Wrapper (Has_Length : Boolean := False) is record
8744 when True => Length : Integer;
8748 type Wrapper_Array is array (1 .. 2) of Wrapper;
8750 Hello : Wrapper_Array := (others => (Has_Length => True,
8751 Data => (others => 17),
8755 The debugging info would describe variable Hello as being an
8756 array of a PAD type. The size of that PAD type is not statically
8757 known, but can be determined using a parallel XVZ variable.
8758 In that case, a copy of the PAD type with the correct size should
8759 be used for the fixed array.
8761 3. ``Fixing'' record type objects:
8762 ----------------------------------
8764 Things are slightly different from arrays in the case of dynamic
8765 record types. In this case, in order to compute the associated
8766 fixed type, we need to determine the size and offset of each of
8767 its components. This, in turn, requires us to compute the fixed
8768 type of each of these components.
8770 Consider for instance the example:
8772 type Bounded_String (Max_Size : Natural) is record
8773 Str : String (1 .. Max_Size);
8776 My_String : Bounded_String (Max_Size => 10);
8778 In that case, the position of field "Length" depends on the size
8779 of field Str, which itself depends on the value of the Max_Size
8780 discriminant. In order to fix the type of variable My_String,
8781 we need to fix the type of field Str. Therefore, fixing a variant
8782 record requires us to fix each of its components.
8784 However, if a component does not have a dynamic size, the component
8785 should not be fixed. In particular, fields that use a PAD type
8786 should not fixed. Here is an example where this might happen
8787 (assuming type Rec above):
8789 type Container (Big : Boolean) is record
8793 when True => Another : Integer;
8797 My_Container : Container := (Big => False,
8798 First => (Empty => True),
8801 In that example, the compiler creates a PAD type for component First,
8802 whose size is constant, and then positions the component After just
8803 right after it. The offset of component After is therefore constant
8806 The debugger computes the position of each field based on an algorithm
8807 that uses, among other things, the actual position and size of the field
8808 preceding it. Let's now imagine that the user is trying to print
8809 the value of My_Container. If the type fixing was recursive, we would
8810 end up computing the offset of field After based on the size of the
8811 fixed version of field First. And since in our example First has
8812 only one actual field, the size of the fixed type is actually smaller
8813 than the amount of space allocated to that field, and thus we would
8814 compute the wrong offset of field After.
8816 To make things more complicated, we need to watch out for dynamic
8817 components of variant records (identified by the ___XVL suffix in
8818 the component name). Even if the target type is a PAD type, the size
8819 of that type might not be statically known. So the PAD type needs
8820 to be unwrapped and the resulting type needs to be fixed. Otherwise,
8821 we might end up with the wrong size for our component. This can be
8822 observed with the following type declarations:
8824 type Octal is new Integer range 0 .. 7;
8825 type Octal_Array is array (Positive range <>) of Octal;
8826 pragma Pack (Octal_Array);
8828 type Octal_Buffer (Size : Positive) is record
8829 Buffer : Octal_Array (1 .. Size);
8833 In that case, Buffer is a PAD type whose size is unset and needs
8834 to be computed by fixing the unwrapped type.
8836 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
8837 ----------------------------------------------------------
8839 Lastly, when should the sub-elements of an entity that remained unfixed
8840 thus far, be actually fixed?
8842 The answer is: Only when referencing that element. For instance
8843 when selecting one component of a record, this specific component
8844 should be fixed at that point in time. Or when printing the value
8845 of a record, each component should be fixed before its value gets
8846 printed. Similarly for arrays, the element of the array should be
8847 fixed when printing each element of the array, or when extracting
8848 one element out of that array. On the other hand, fixing should
8849 not be performed on the elements when taking a slice of an array!
8851 Note that one of the side-effects of miscomputing the offset and
8852 size of each field is that we end up also miscomputing the size
8853 of the containing type. This can have adverse results when computing
8854 the value of an entity. GDB fetches the value of an entity based
8855 on the size of its type, and thus a wrong size causes GDB to fetch
8856 the wrong amount of memory. In the case where the computed size is
8857 too small, GDB fetches too little data to print the value of our
8858 entiry. Results in this case as unpredicatble, as we usually read
8859 past the buffer containing the data =:-o. */
8861 /* Implement the evaluate_exp routine in the exp_descriptor structure
8862 for the Ada language. */
8864 static struct value *
8865 ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
8866 int *pos, enum noside noside)
8871 struct value *arg1 = NULL, *arg2 = NULL, *arg3;
8874 struct value **argvec;
8878 op = exp->elts[pc].opcode;
8884 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
8885 arg1 = unwrap_value (arg1);
8887 /* If evaluating an OP_DOUBLE and an EXPECT_TYPE was provided,
8888 then we need to perform the conversion manually, because
8889 evaluate_subexp_standard doesn't do it. This conversion is
8890 necessary in Ada because the different kinds of float/fixed
8891 types in Ada have different representations.
8893 Similarly, we need to perform the conversion from OP_LONG
8895 if ((op == OP_DOUBLE || op == OP_LONG) && expect_type != NULL)
8896 arg1 = ada_value_cast (expect_type, arg1, noside);
8902 struct value *result;
8905 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
8906 /* The result type will have code OP_STRING, bashed there from
8907 OP_ARRAY. Bash it back. */
8908 if (TYPE_CODE (value_type (result)) == TYPE_CODE_STRING)
8909 TYPE_CODE (value_type (result)) = TYPE_CODE_ARRAY;
8915 type = exp->elts[pc + 1].type;
8916 arg1 = evaluate_subexp (type, exp, pos, noside);
8917 if (noside == EVAL_SKIP)
8919 arg1 = ada_value_cast (type, arg1, noside);
8924 type = exp->elts[pc + 1].type;
8925 return ada_evaluate_subexp (type, exp, pos, noside);
8928 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
8929 if (exp->elts[*pos].opcode == OP_AGGREGATE)
8931 arg1 = assign_aggregate (arg1, arg1, exp, pos, noside);
8932 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
8934 return ada_value_assign (arg1, arg1);
8936 /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
8937 except if the lhs of our assignment is a convenience variable.
8938 In the case of assigning to a convenience variable, the lhs
8939 should be exactly the result of the evaluation of the rhs. */
8940 type = value_type (arg1);
8941 if (VALUE_LVAL (arg1) == lval_internalvar)
8943 arg2 = evaluate_subexp (type, exp, pos, noside);
8944 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
8946 if (ada_is_fixed_point_type (value_type (arg1)))
8947 arg2 = cast_to_fixed (value_type (arg1), arg2);
8948 else if (ada_is_fixed_point_type (value_type (arg2)))
8950 (_("Fixed-point values must be assigned to fixed-point variables"));
8952 arg2 = coerce_for_assign (value_type (arg1), arg2);
8953 return ada_value_assign (arg1, arg2);
8956 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
8957 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
8958 if (noside == EVAL_SKIP)
8960 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_PTR)
8961 return (value_from_longest
8963 value_as_long (arg1) + value_as_long (arg2)));
8964 if ((ada_is_fixed_point_type (value_type (arg1))
8965 || ada_is_fixed_point_type (value_type (arg2)))
8966 && value_type (arg1) != value_type (arg2))
8967 error (_("Operands of fixed-point addition must have the same type"));
8968 /* Do the addition, and cast the result to the type of the first
8969 argument. We cannot cast the result to a reference type, so if
8970 ARG1 is a reference type, find its underlying type. */
8971 type = value_type (arg1);
8972 while (TYPE_CODE (type) == TYPE_CODE_REF)
8973 type = TYPE_TARGET_TYPE (type);
8974 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
8975 return value_cast (type, value_binop (arg1, arg2, BINOP_ADD));
8978 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
8979 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
8980 if (noside == EVAL_SKIP)
8982 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_PTR)
8983 return (value_from_longest
8985 value_as_long (arg1) - value_as_long (arg2)));
8986 if ((ada_is_fixed_point_type (value_type (arg1))
8987 || ada_is_fixed_point_type (value_type (arg2)))
8988 && value_type (arg1) != value_type (arg2))
8989 error (_("Operands of fixed-point subtraction must have the same type"));
8990 /* Do the substraction, and cast the result to the type of the first
8991 argument. We cannot cast the result to a reference type, so if
8992 ARG1 is a reference type, find its underlying type. */
8993 type = value_type (arg1);
8994 while (TYPE_CODE (type) == TYPE_CODE_REF)
8995 type = TYPE_TARGET_TYPE (type);
8996 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
8997 return value_cast (type, value_binop (arg1, arg2, BINOP_SUB));
9003 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9004 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9005 if (noside == EVAL_SKIP)
9007 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9009 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9010 return value_zero (value_type (arg1), not_lval);
9014 type = builtin_type (exp->gdbarch)->builtin_double;
9015 if (ada_is_fixed_point_type (value_type (arg1)))
9016 arg1 = cast_from_fixed (type, arg1);
9017 if (ada_is_fixed_point_type (value_type (arg2)))
9018 arg2 = cast_from_fixed (type, arg2);
9019 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9020 return ada_value_binop (arg1, arg2, op);
9024 case BINOP_NOTEQUAL:
9025 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9026 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
9027 if (noside == EVAL_SKIP)
9029 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9033 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9034 tem = ada_value_equal (arg1, arg2);
9036 if (op == BINOP_NOTEQUAL)
9038 type = language_bool_type (exp->language_defn, exp->gdbarch);
9039 return value_from_longest (type, (LONGEST) tem);
9042 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9043 if (noside == EVAL_SKIP)
9045 else if (ada_is_fixed_point_type (value_type (arg1)))
9046 return value_cast (value_type (arg1), value_neg (arg1));
9049 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
9050 return value_neg (arg1);
9053 case BINOP_LOGICAL_AND:
9054 case BINOP_LOGICAL_OR:
9055 case UNOP_LOGICAL_NOT:
9060 val = evaluate_subexp_standard (expect_type, exp, pos, noside);
9061 type = language_bool_type (exp->language_defn, exp->gdbarch);
9062 return value_cast (type, val);
9065 case BINOP_BITWISE_AND:
9066 case BINOP_BITWISE_IOR:
9067 case BINOP_BITWISE_XOR:
9071 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
9073 val = evaluate_subexp_standard (expect_type, exp, pos, noside);
9075 return value_cast (value_type (arg1), val);
9081 if (noside == EVAL_SKIP)
9086 else if (SYMBOL_DOMAIN (exp->elts[pc + 2].symbol) == UNDEF_DOMAIN)
9087 /* Only encountered when an unresolved symbol occurs in a
9088 context other than a function call, in which case, it is
9090 error (_("Unexpected unresolved symbol, %s, during evaluation"),
9091 SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol));
9092 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9094 type = static_unwrap_type (SYMBOL_TYPE (exp->elts[pc + 2].symbol));
9095 /* Check to see if this is a tagged type. We also need to handle
9096 the case where the type is a reference to a tagged type, but
9097 we have to be careful to exclude pointers to tagged types.
9098 The latter should be shown as usual (as a pointer), whereas
9099 a reference should mostly be transparent to the user. */
9100 if (ada_is_tagged_type (type, 0)
9101 || (TYPE_CODE(type) == TYPE_CODE_REF
9102 && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0)))
9104 /* Tagged types are a little special in the fact that the real
9105 type is dynamic and can only be determined by inspecting the
9106 object's tag. This means that we need to get the object's
9107 value first (EVAL_NORMAL) and then extract the actual object
9110 Note that we cannot skip the final step where we extract
9111 the object type from its tag, because the EVAL_NORMAL phase
9112 results in dynamic components being resolved into fixed ones.
9113 This can cause problems when trying to print the type
9114 description of tagged types whose parent has a dynamic size:
9115 We use the type name of the "_parent" component in order
9116 to print the name of the ancestor type in the type description.
9117 If that component had a dynamic size, the resolution into
9118 a fixed type would result in the loss of that type name,
9119 thus preventing us from printing the name of the ancestor
9120 type in the type description. */
9121 struct type *actual_type;
9123 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
9124 actual_type = type_from_tag (ada_value_tag (arg1));
9125 if (actual_type == NULL)
9126 /* If, for some reason, we were unable to determine
9127 the actual type from the tag, then use the static
9128 approximation that we just computed as a fallback.
9129 This can happen if the debugging information is
9130 incomplete, for instance. */
9133 return value_zero (actual_type, not_lval);
9138 (to_static_fixed_type
9139 (static_unwrap_type (SYMBOL_TYPE (exp->elts[pc + 2].symbol))),
9144 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
9145 arg1 = unwrap_value (arg1);
9146 return ada_to_fixed_value (arg1);
9152 /* Allocate arg vector, including space for the function to be
9153 called in argvec[0] and a terminating NULL. */
9154 nargs = longest_to_int (exp->elts[pc + 1].longconst);
9156 (struct value **) alloca (sizeof (struct value *) * (nargs + 2));
9158 if (exp->elts[*pos].opcode == OP_VAR_VALUE
9159 && SYMBOL_DOMAIN (exp->elts[pc + 5].symbol) == UNDEF_DOMAIN)
9160 error (_("Unexpected unresolved symbol, %s, during evaluation"),
9161 SYMBOL_PRINT_NAME (exp->elts[pc + 5].symbol));
9164 for (tem = 0; tem <= nargs; tem += 1)
9165 argvec[tem] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9168 if (noside == EVAL_SKIP)
9172 if (ada_is_constrained_packed_array_type
9173 (desc_base_type (value_type (argvec[0]))))
9174 argvec[0] = ada_coerce_to_simple_array (argvec[0]);
9175 else if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_ARRAY
9176 && TYPE_FIELD_BITSIZE (value_type (argvec[0]), 0) != 0)
9177 /* This is a packed array that has already been fixed, and
9178 therefore already coerced to a simple array. Nothing further
9181 else if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_REF
9182 || (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_ARRAY
9183 && VALUE_LVAL (argvec[0]) == lval_memory))
9184 argvec[0] = value_addr (argvec[0]);
9186 type = ada_check_typedef (value_type (argvec[0]));
9187 if (TYPE_CODE (type) == TYPE_CODE_PTR)
9189 switch (TYPE_CODE (ada_check_typedef (TYPE_TARGET_TYPE (type))))
9191 case TYPE_CODE_FUNC:
9192 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
9194 case TYPE_CODE_ARRAY:
9196 case TYPE_CODE_STRUCT:
9197 if (noside != EVAL_AVOID_SIDE_EFFECTS)
9198 argvec[0] = ada_value_ind (argvec[0]);
9199 type = ada_check_typedef (TYPE_TARGET_TYPE (type));
9202 error (_("cannot subscript or call something of type `%s'"),
9203 ada_type_name (value_type (argvec[0])));
9208 switch (TYPE_CODE (type))
9210 case TYPE_CODE_FUNC:
9211 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9212 return allocate_value (TYPE_TARGET_TYPE (type));
9213 return call_function_by_hand (argvec[0], nargs, argvec + 1);
9214 case TYPE_CODE_STRUCT:
9218 arity = ada_array_arity (type);
9219 type = ada_array_element_type (type, nargs);
9221 error (_("cannot subscript or call a record"));
9223 error (_("wrong number of subscripts; expecting %d"), arity);
9224 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9225 return value_zero (ada_aligned_type (type), lval_memory);
9227 unwrap_value (ada_value_subscript
9228 (argvec[0], nargs, argvec + 1));
9230 case TYPE_CODE_ARRAY:
9231 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9233 type = ada_array_element_type (type, nargs);
9235 error (_("element type of array unknown"));
9237 return value_zero (ada_aligned_type (type), lval_memory);
9240 unwrap_value (ada_value_subscript
9241 (ada_coerce_to_simple_array (argvec[0]),
9242 nargs, argvec + 1));
9243 case TYPE_CODE_PTR: /* Pointer to array */
9244 type = to_fixed_array_type (TYPE_TARGET_TYPE (type), NULL, 1);
9245 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9247 type = ada_array_element_type (type, nargs);
9249 error (_("element type of array unknown"));
9251 return value_zero (ada_aligned_type (type), lval_memory);
9254 unwrap_value (ada_value_ptr_subscript (argvec[0], type,
9255 nargs, argvec + 1));
9258 error (_("Attempt to index or call something other than an "
9259 "array or function"));
9264 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9265 struct value *low_bound_val =
9266 evaluate_subexp (NULL_TYPE, exp, pos, noside);
9267 struct value *high_bound_val =
9268 evaluate_subexp (NULL_TYPE, exp, pos, noside);
9272 low_bound_val = coerce_ref (low_bound_val);
9273 high_bound_val = coerce_ref (high_bound_val);
9274 low_bound = pos_atr (low_bound_val);
9275 high_bound = pos_atr (high_bound_val);
9277 if (noside == EVAL_SKIP)
9280 /* If this is a reference to an aligner type, then remove all
9282 if (TYPE_CODE (value_type (array)) == TYPE_CODE_REF
9283 && ada_is_aligner_type (TYPE_TARGET_TYPE (value_type (array))))
9284 TYPE_TARGET_TYPE (value_type (array)) =
9285 ada_aligned_type (TYPE_TARGET_TYPE (value_type (array)));
9287 if (ada_is_constrained_packed_array_type (value_type (array)))
9288 error (_("cannot slice a packed array"));
9290 /* If this is a reference to an array or an array lvalue,
9291 convert to a pointer. */
9292 if (TYPE_CODE (value_type (array)) == TYPE_CODE_REF
9293 || (TYPE_CODE (value_type (array)) == TYPE_CODE_ARRAY
9294 && VALUE_LVAL (array) == lval_memory))
9295 array = value_addr (array);
9297 if (noside == EVAL_AVOID_SIDE_EFFECTS
9298 && ada_is_array_descriptor_type (ada_check_typedef
9299 (value_type (array))))
9300 return empty_array (ada_type_of_array (array, 0), low_bound);
9302 array = ada_coerce_to_simple_array_ptr (array);
9304 /* If we have more than one level of pointer indirection,
9305 dereference the value until we get only one level. */
9306 while (TYPE_CODE (value_type (array)) == TYPE_CODE_PTR
9307 && (TYPE_CODE (TYPE_TARGET_TYPE (value_type (array)))
9309 array = value_ind (array);
9311 /* Make sure we really do have an array type before going further,
9312 to avoid a SEGV when trying to get the index type or the target
9313 type later down the road if the debug info generated by
9314 the compiler is incorrect or incomplete. */
9315 if (!ada_is_simple_array_type (value_type (array)))
9316 error (_("cannot take slice of non-array"));
9318 if (TYPE_CODE (value_type (array)) == TYPE_CODE_PTR)
9320 if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
9321 return empty_array (TYPE_TARGET_TYPE (value_type (array)),
9325 struct type *arr_type0 =
9326 to_fixed_array_type (TYPE_TARGET_TYPE (value_type (array)),
9329 return ada_value_slice_from_ptr (array, arr_type0,
9330 longest_to_int (low_bound),
9331 longest_to_int (high_bound));
9334 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9336 else if (high_bound < low_bound)
9337 return empty_array (value_type (array), low_bound);
9339 return ada_value_slice (array, longest_to_int (low_bound),
9340 longest_to_int (high_bound));
9345 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9346 type = check_typedef (exp->elts[pc + 1].type);
9348 if (noside == EVAL_SKIP)
9351 switch (TYPE_CODE (type))
9354 lim_warning (_("Membership test incompletely implemented; "
9355 "always returns true"));
9356 type = language_bool_type (exp->language_defn, exp->gdbarch);
9357 return value_from_longest (type, (LONGEST) 1);
9359 case TYPE_CODE_RANGE:
9360 arg2 = value_from_longest (type, TYPE_LOW_BOUND (type));
9361 arg3 = value_from_longest (type, TYPE_HIGH_BOUND (type));
9362 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9363 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
9364 type = language_bool_type (exp->language_defn, exp->gdbarch);
9366 value_from_longest (type,
9367 (value_less (arg1, arg3)
9368 || value_equal (arg1, arg3))
9369 && (value_less (arg2, arg1)
9370 || value_equal (arg2, arg1)));
9373 case BINOP_IN_BOUNDS:
9375 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9376 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9378 if (noside == EVAL_SKIP)
9381 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9383 type = language_bool_type (exp->language_defn, exp->gdbarch);
9384 return value_zero (type, not_lval);
9387 tem = longest_to_int (exp->elts[pc + 1].longconst);
9389 type = ada_index_type (value_type (arg2), tem, "range");
9391 type = value_type (arg1);
9393 arg3 = value_from_longest (type, ada_array_bound (arg2, tem, 1));
9394 arg2 = value_from_longest (type, ada_array_bound (arg2, tem, 0));
9396 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9397 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
9398 type = language_bool_type (exp->language_defn, exp->gdbarch);
9400 value_from_longest (type,
9401 (value_less (arg1, arg3)
9402 || value_equal (arg1, arg3))
9403 && (value_less (arg2, arg1)
9404 || value_equal (arg2, arg1)));
9406 case TERNOP_IN_RANGE:
9407 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9408 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9409 arg3 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9411 if (noside == EVAL_SKIP)
9414 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9415 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
9416 type = language_bool_type (exp->language_defn, exp->gdbarch);
9418 value_from_longest (type,
9419 (value_less (arg1, arg3)
9420 || value_equal (arg1, arg3))
9421 && (value_less (arg2, arg1)
9422 || value_equal (arg2, arg1)));
9428 struct type *type_arg;
9430 if (exp->elts[*pos].opcode == OP_TYPE)
9432 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
9434 type_arg = check_typedef (exp->elts[pc + 2].type);
9438 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9442 if (exp->elts[*pos].opcode != OP_LONG)
9443 error (_("Invalid operand to '%s"), ada_attribute_name (op));
9444 tem = longest_to_int (exp->elts[*pos + 2].longconst);
9447 if (noside == EVAL_SKIP)
9450 if (type_arg == NULL)
9452 arg1 = ada_coerce_ref (arg1);
9454 if (ada_is_constrained_packed_array_type (value_type (arg1)))
9455 arg1 = ada_coerce_to_simple_array (arg1);
9457 type = ada_index_type (value_type (arg1), tem,
9458 ada_attribute_name (op));
9460 type = builtin_type (exp->gdbarch)->builtin_int;
9462 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9463 return allocate_value (type);
9467 default: /* Should never happen. */
9468 error (_("unexpected attribute encountered"));
9470 return value_from_longest
9471 (type, ada_array_bound (arg1, tem, 0));
9473 return value_from_longest
9474 (type, ada_array_bound (arg1, tem, 1));
9476 return value_from_longest
9477 (type, ada_array_length (arg1, tem));
9480 else if (discrete_type_p (type_arg))
9482 struct type *range_type;
9483 char *name = ada_type_name (type_arg);
9486 if (name != NULL && TYPE_CODE (type_arg) != TYPE_CODE_ENUM)
9487 range_type = to_fixed_range_type (type_arg, NULL);
9488 if (range_type == NULL)
9489 range_type = type_arg;
9493 error (_("unexpected attribute encountered"));
9495 return value_from_longest
9496 (range_type, ada_discrete_type_low_bound (range_type));
9498 return value_from_longest
9499 (range_type, ada_discrete_type_high_bound (range_type));
9501 error (_("the 'length attribute applies only to array types"));
9504 else if (TYPE_CODE (type_arg) == TYPE_CODE_FLT)
9505 error (_("unimplemented type attribute"));
9510 if (ada_is_constrained_packed_array_type (type_arg))
9511 type_arg = decode_constrained_packed_array_type (type_arg);
9513 type = ada_index_type (type_arg, tem, ada_attribute_name (op));
9515 type = builtin_type (exp->gdbarch)->builtin_int;
9517 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9518 return allocate_value (type);
9523 error (_("unexpected attribute encountered"));
9525 low = ada_array_bound_from_type (type_arg, tem, 0);
9526 return value_from_longest (type, low);
9528 high = ada_array_bound_from_type (type_arg, tem, 1);
9529 return value_from_longest (type, high);
9531 low = ada_array_bound_from_type (type_arg, tem, 0);
9532 high = ada_array_bound_from_type (type_arg, tem, 1);
9533 return value_from_longest (type, high - low + 1);
9539 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9540 if (noside == EVAL_SKIP)
9543 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9544 return value_zero (ada_tag_type (arg1), not_lval);
9546 return ada_value_tag (arg1);
9550 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
9551 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9552 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9553 if (noside == EVAL_SKIP)
9555 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9556 return value_zero (value_type (arg1), not_lval);
9559 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9560 return value_binop (arg1, arg2,
9561 op == OP_ATR_MIN ? BINOP_MIN : BINOP_MAX);
9564 case OP_ATR_MODULUS:
9566 struct type *type_arg = check_typedef (exp->elts[pc + 2].type);
9568 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
9569 if (noside == EVAL_SKIP)
9572 if (!ada_is_modular_type (type_arg))
9573 error (_("'modulus must be applied to modular type"));
9575 return value_from_longest (TYPE_TARGET_TYPE (type_arg),
9576 ada_modulus (type_arg));
9581 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
9582 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9583 if (noside == EVAL_SKIP)
9585 type = builtin_type (exp->gdbarch)->builtin_int;
9586 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9587 return value_zero (type, not_lval);
9589 return value_pos_atr (type, arg1);
9592 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9593 type = value_type (arg1);
9595 /* If the argument is a reference, then dereference its type, since
9596 the user is really asking for the size of the actual object,
9597 not the size of the pointer. */
9598 if (TYPE_CODE (type) == TYPE_CODE_REF)
9599 type = TYPE_TARGET_TYPE (type);
9601 if (noside == EVAL_SKIP)
9603 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9604 return value_zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
9606 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
9607 TARGET_CHAR_BIT * TYPE_LENGTH (type));
9610 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
9611 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9612 type = exp->elts[pc + 2].type;
9613 if (noside == EVAL_SKIP)
9615 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9616 return value_zero (type, not_lval);
9618 return value_val_atr (type, arg1);
9621 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9622 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9623 if (noside == EVAL_SKIP)
9625 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9626 return value_zero (value_type (arg1), not_lval);
9629 /* For integer exponentiation operations,
9630 only promote the first argument. */
9631 if (is_integral_type (value_type (arg2)))
9632 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
9634 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
9636 return value_binop (arg1, arg2, op);
9640 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9641 if (noside == EVAL_SKIP)
9647 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9648 if (noside == EVAL_SKIP)
9650 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
9651 if (value_less (arg1, value_zero (value_type (arg1), not_lval)))
9652 return value_neg (arg1);
9657 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9658 if (noside == EVAL_SKIP)
9660 type = ada_check_typedef (value_type (arg1));
9661 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9663 if (ada_is_array_descriptor_type (type))
9664 /* GDB allows dereferencing GNAT array descriptors. */
9666 struct type *arrType = ada_type_of_array (arg1, 0);
9668 if (arrType == NULL)
9669 error (_("Attempt to dereference null array pointer."));
9670 return value_at_lazy (arrType, 0);
9672 else if (TYPE_CODE (type) == TYPE_CODE_PTR
9673 || TYPE_CODE (type) == TYPE_CODE_REF
9674 /* In C you can dereference an array to get the 1st elt. */
9675 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
9677 type = to_static_fixed_type
9679 (ada_check_typedef (TYPE_TARGET_TYPE (type))));
9681 return value_zero (type, lval_memory);
9683 else if (TYPE_CODE (type) == TYPE_CODE_INT)
9685 /* GDB allows dereferencing an int. */
9686 if (expect_type == NULL)
9687 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
9692 to_static_fixed_type (ada_aligned_type (expect_type));
9693 return value_zero (expect_type, lval_memory);
9697 error (_("Attempt to take contents of a non-pointer value."));
9699 arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
9700 type = ada_check_typedef (value_type (arg1));
9702 if (TYPE_CODE (type) == TYPE_CODE_INT)
9703 /* GDB allows dereferencing an int. If we were given
9704 the expect_type, then use that as the target type.
9705 Otherwise, assume that the target type is an int. */
9707 if (expect_type != NULL)
9708 return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
9711 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
9712 (CORE_ADDR) value_as_address (arg1));
9715 if (ada_is_array_descriptor_type (type))
9716 /* GDB allows dereferencing GNAT array descriptors. */
9717 return ada_coerce_to_simple_array (arg1);
9719 return ada_value_ind (arg1);
9721 case STRUCTOP_STRUCT:
9722 tem = longest_to_int (exp->elts[pc + 1].longconst);
9723 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
9724 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
9725 if (noside == EVAL_SKIP)
9727 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9729 struct type *type1 = value_type (arg1);
9731 if (ada_is_tagged_type (type1, 1))
9733 type = ada_lookup_struct_elt_type (type1,
9734 &exp->elts[pc + 2].string,
9737 /* In this case, we assume that the field COULD exist
9738 in some extension of the type. Return an object of
9739 "type" void, which will match any formal
9740 (see ada_type_match). */
9741 return value_zero (builtin_type (exp->gdbarch)->builtin_void,
9746 ada_lookup_struct_elt_type (type1, &exp->elts[pc + 2].string, 1,
9749 return value_zero (ada_aligned_type (type), lval_memory);
9752 arg1 = ada_value_struct_elt (arg1, &exp->elts[pc + 2].string, 0);
9753 arg1 = unwrap_value (arg1);
9754 return ada_to_fixed_value (arg1);
9757 /* The value is not supposed to be used. This is here to make it
9758 easier to accommodate expressions that contain types. */
9760 if (noside == EVAL_SKIP)
9762 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
9763 return allocate_value (exp->elts[pc + 1].type);
9765 error (_("Attempt to use a type name as an expression"));
9770 case OP_DISCRETE_RANGE:
9773 if (noside == EVAL_NORMAL)
9777 error (_("Undefined name, ambiguous name, or renaming used in "
9778 "component association: %s."), &exp->elts[pc+2].string);
9780 error (_("Aggregates only allowed on the right of an assignment"));
9782 internal_error (__FILE__, __LINE__, _("aggregate apparently mangled"));
9785 ada_forward_operator_length (exp, pc, &oplen, &nargs);
9787 for (tem = 0; tem < nargs; tem += 1)
9788 ada_evaluate_subexp (NULL, exp, pos, noside);
9793 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
9799 /* If TYPE encodes an Ada fixed-point type, return the suffix of the
9800 type name that encodes the 'small and 'delta information.
9801 Otherwise, return NULL. */
9804 fixed_type_info (struct type *type)
9806 const char *name = ada_type_name (type);
9807 enum type_code code = (type == NULL) ? TYPE_CODE_UNDEF : TYPE_CODE (type);
9809 if ((code == TYPE_CODE_INT || code == TYPE_CODE_RANGE) && name != NULL)
9811 const char *tail = strstr (name, "___XF_");
9818 else if (code == TYPE_CODE_RANGE && TYPE_TARGET_TYPE (type) != type)
9819 return fixed_type_info (TYPE_TARGET_TYPE (type));
9824 /* Returns non-zero iff TYPE represents an Ada fixed-point type. */
9827 ada_is_fixed_point_type (struct type *type)
9829 return fixed_type_info (type) != NULL;
9832 /* Return non-zero iff TYPE represents a System.Address type. */
9835 ada_is_system_address_type (struct type *type)
9837 return (TYPE_NAME (type)
9838 && strcmp (TYPE_NAME (type), "system__address") == 0);
9841 /* Assuming that TYPE is the representation of an Ada fixed-point
9842 type, return its delta, or -1 if the type is malformed and the
9843 delta cannot be determined. */
9846 ada_delta (struct type *type)
9848 const char *encoding = fixed_type_info (type);
9851 /* Strictly speaking, num and den are encoded as integer. However,
9852 they may not fit into a long, and they will have to be converted
9853 to DOUBLEST anyway. So scan them as DOUBLEST. */
9854 if (sscanf (encoding, "_%" DOUBLEST_SCAN_FORMAT "_%" DOUBLEST_SCAN_FORMAT,
9861 /* Assuming that ada_is_fixed_point_type (TYPE), return the scaling
9862 factor ('SMALL value) associated with the type. */
9865 scaling_factor (struct type *type)
9867 const char *encoding = fixed_type_info (type);
9868 DOUBLEST num0, den0, num1, den1;
9871 /* Strictly speaking, num's and den's are encoded as integer. However,
9872 they may not fit into a long, and they will have to be converted
9873 to DOUBLEST anyway. So scan them as DOUBLEST. */
9874 n = sscanf (encoding,
9875 "_%" DOUBLEST_SCAN_FORMAT "_%" DOUBLEST_SCAN_FORMAT
9876 "_%" DOUBLEST_SCAN_FORMAT "_%" DOUBLEST_SCAN_FORMAT,
9877 &num0, &den0, &num1, &den1);
9888 /* Assuming that X is the representation of a value of fixed-point
9889 type TYPE, return its floating-point equivalent. */
9892 ada_fixed_to_float (struct type *type, LONGEST x)
9894 return (DOUBLEST) x *scaling_factor (type);
9897 /* The representation of a fixed-point value of type TYPE
9898 corresponding to the value X. */
9901 ada_float_to_fixed (struct type *type, DOUBLEST x)
9903 return (LONGEST) (x / scaling_factor (type) + 0.5);
9910 /* Scan STR beginning at position K for a discriminant name, and
9911 return the value of that discriminant field of DVAL in *PX. If
9912 PNEW_K is not null, put the position of the character beyond the
9913 name scanned in *PNEW_K. Return 1 if successful; return 0 and do
9914 not alter *PX and *PNEW_K if unsuccessful. */
9917 scan_discrim_bound (char *str, int k, struct value *dval, LONGEST * px,
9920 static char *bound_buffer = NULL;
9921 static size_t bound_buffer_len = 0;
9924 struct value *bound_val;
9926 if (dval == NULL || str == NULL || str[k] == '\0')
9929 pend = strstr (str + k, "__");
9933 k += strlen (bound);
9937 GROW_VECT (bound_buffer, bound_buffer_len, pend - (str + k) + 1);
9938 bound = bound_buffer;
9939 strncpy (bound_buffer, str + k, pend - (str + k));
9940 bound[pend - (str + k)] = '\0';
9944 bound_val = ada_search_struct_field (bound, dval, 0, value_type (dval));
9945 if (bound_val == NULL)
9948 *px = value_as_long (bound_val);
9954 /* Value of variable named NAME in the current environment. If
9955 no such variable found, then if ERR_MSG is null, returns 0, and
9956 otherwise causes an error with message ERR_MSG. */
9958 static struct value *
9959 get_var_value (char *name, char *err_msg)
9961 struct ada_symbol_info *syms;
9964 nsyms = ada_lookup_symbol_list (name, get_selected_block (0), VAR_DOMAIN,
9969 if (err_msg == NULL)
9972 error (("%s"), err_msg);
9975 return value_of_variable (syms[0].sym, syms[0].block);
9978 /* Value of integer variable named NAME in the current environment. If
9979 no such variable found, returns 0, and sets *FLAG to 0. If
9980 successful, sets *FLAG to 1. */
9983 get_int_var_value (char *name, int *flag)
9985 struct value *var_val = get_var_value (name, 0);
9997 return value_as_long (var_val);
10002 /* Return a range type whose base type is that of the range type named
10003 NAME in the current environment, and whose bounds are calculated
10004 from NAME according to the GNAT range encoding conventions.
10005 Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
10006 corresponding range type from debug information; fall back to using it
10007 if symbol lookup fails. If a new type must be created, allocate it
10008 like ORIG_TYPE was. The bounds information, in general, is encoded
10009 in NAME, the base type given in the named range type. */
10011 static struct type *
10012 to_fixed_range_type (struct type *raw_type, struct value *dval)
10015 struct type *base_type;
10016 char *subtype_info;
10018 gdb_assert (raw_type != NULL);
10019 gdb_assert (TYPE_NAME (raw_type) != NULL);
10021 if (TYPE_CODE (raw_type) == TYPE_CODE_RANGE)
10022 base_type = TYPE_TARGET_TYPE (raw_type);
10024 base_type = raw_type;
10026 name = TYPE_NAME (raw_type);
10027 subtype_info = strstr (name, "___XD");
10028 if (subtype_info == NULL)
10030 LONGEST L = ada_discrete_type_low_bound (raw_type);
10031 LONGEST U = ada_discrete_type_high_bound (raw_type);
10033 if (L < INT_MIN || U > INT_MAX)
10036 return create_range_type (alloc_type_copy (raw_type), raw_type,
10037 ada_discrete_type_low_bound (raw_type),
10038 ada_discrete_type_high_bound (raw_type));
10042 static char *name_buf = NULL;
10043 static size_t name_len = 0;
10044 int prefix_len = subtype_info - name;
10050 GROW_VECT (name_buf, name_len, prefix_len + 5);
10051 strncpy (name_buf, name, prefix_len);
10052 name_buf[prefix_len] = '\0';
10055 bounds_str = strchr (subtype_info, '_');
10058 if (*subtype_info == 'L')
10060 if (!ada_scan_number (bounds_str, n, &L, &n)
10061 && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
10063 if (bounds_str[n] == '_')
10065 else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
10073 strcpy (name_buf + prefix_len, "___L");
10074 L = get_int_var_value (name_buf, &ok);
10077 lim_warning (_("Unknown lower bound, using 1."));
10082 if (*subtype_info == 'U')
10084 if (!ada_scan_number (bounds_str, n, &U, &n)
10085 && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
10092 strcpy (name_buf + prefix_len, "___U");
10093 U = get_int_var_value (name_buf, &ok);
10096 lim_warning (_("Unknown upper bound, using %ld."), (long) L);
10101 type = create_range_type (alloc_type_copy (raw_type), base_type, L, U);
10102 TYPE_NAME (type) = name;
10107 /* True iff NAME is the name of a range type. */
10110 ada_is_range_type_name (const char *name)
10112 return (name != NULL && strstr (name, "___XD"));
10116 /* Modular types */
10118 /* True iff TYPE is an Ada modular type. */
10121 ada_is_modular_type (struct type *type)
10123 struct type *subranged_type = base_type (type);
10125 return (subranged_type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
10126 && TYPE_CODE (subranged_type) == TYPE_CODE_INT
10127 && TYPE_UNSIGNED (subranged_type));
10130 /* Try to determine the lower and upper bounds of the given modular type
10131 using the type name only. Return non-zero and set L and U as the lower
10132 and upper bounds (respectively) if successful. */
10135 ada_modulus_from_name (struct type *type, ULONGEST *modulus)
10137 char *name = ada_type_name (type);
10145 /* Discrete type bounds are encoded using an __XD suffix. In our case,
10146 we are looking for static bounds, which means an __XDLU suffix.
10147 Moreover, we know that the lower bound of modular types is always
10148 zero, so the actual suffix should start with "__XDLU_0__", and
10149 then be followed by the upper bound value. */
10150 suffix = strstr (name, "__XDLU_0__");
10151 if (suffix == NULL)
10154 if (!ada_scan_number (suffix, k, &U, NULL))
10157 *modulus = (ULONGEST) U + 1;
10161 /* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
10164 ada_modulus (struct type *type)
10166 return (ULONGEST) TYPE_HIGH_BOUND (type) + 1;
10170 /* Ada exception catchpoint support:
10171 ---------------------------------
10173 We support 3 kinds of exception catchpoints:
10174 . catchpoints on Ada exceptions
10175 . catchpoints on unhandled Ada exceptions
10176 . catchpoints on failed assertions
10178 Exceptions raised during failed assertions, or unhandled exceptions
10179 could perfectly be caught with the general catchpoint on Ada exceptions.
10180 However, we can easily differentiate these two special cases, and having
10181 the option to distinguish these two cases from the rest can be useful
10182 to zero-in on certain situations.
10184 Exception catchpoints are a specialized form of breakpoint,
10185 since they rely on inserting breakpoints inside known routines
10186 of the GNAT runtime. The implementation therefore uses a standard
10187 breakpoint structure of the BP_BREAKPOINT type, but with its own set
10190 Support in the runtime for exception catchpoints have been changed
10191 a few times already, and these changes affect the implementation
10192 of these catchpoints. In order to be able to support several
10193 variants of the runtime, we use a sniffer that will determine
10194 the runtime variant used by the program being debugged.
10196 At this time, we do not support the use of conditions on Ada exception
10197 catchpoints. The COND and COND_STRING fields are therefore set
10198 to NULL (most of the time, see below).
10200 Conditions where EXP_STRING, COND, and COND_STRING are used:
10202 When a user specifies the name of a specific exception in the case
10203 of catchpoints on Ada exceptions, we store the name of that exception
10204 in the EXP_STRING. We then translate this request into an actual
10205 condition stored in COND_STRING, and then parse it into an expression
10208 /* The different types of catchpoints that we introduced for catching
10211 enum exception_catchpoint_kind
10213 ex_catch_exception,
10214 ex_catch_exception_unhandled,
10218 /* Ada's standard exceptions. */
10220 static char *standard_exc[] = {
10221 "constraint_error",
10227 typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
10229 /* A structure that describes how to support exception catchpoints
10230 for a given executable. */
10232 struct exception_support_info
10234 /* The name of the symbol to break on in order to insert
10235 a catchpoint on exceptions. */
10236 const char *catch_exception_sym;
10238 /* The name of the symbol to break on in order to insert
10239 a catchpoint on unhandled exceptions. */
10240 const char *catch_exception_unhandled_sym;
10242 /* The name of the symbol to break on in order to insert
10243 a catchpoint on failed assertions. */
10244 const char *catch_assert_sym;
10246 /* Assuming that the inferior just triggered an unhandled exception
10247 catchpoint, this function is responsible for returning the address
10248 in inferior memory where the name of that exception is stored.
10249 Return zero if the address could not be computed. */
10250 ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
10253 static CORE_ADDR ada_unhandled_exception_name_addr (void);
10254 static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
10256 /* The following exception support info structure describes how to
10257 implement exception catchpoints with the latest version of the
10258 Ada runtime (as of 2007-03-06). */
10260 static const struct exception_support_info default_exception_support_info =
10262 "__gnat_debug_raise_exception", /* catch_exception_sym */
10263 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
10264 "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
10265 ada_unhandled_exception_name_addr
10268 /* The following exception support info structure describes how to
10269 implement exception catchpoints with a slightly older version
10270 of the Ada runtime. */
10272 static const struct exception_support_info exception_support_info_fallback =
10274 "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
10275 "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
10276 "system__assertions__raise_assert_failure", /* catch_assert_sym */
10277 ada_unhandled_exception_name_addr_from_raise
10280 /* For each executable, we sniff which exception info structure to use
10281 and cache it in the following global variable. */
10283 static const struct exception_support_info *exception_info = NULL;
10285 /* Inspect the Ada runtime and determine which exception info structure
10286 should be used to provide support for exception catchpoints.
10288 This function will always set exception_info, or raise an error. */
10291 ada_exception_support_info_sniffer (void)
10293 struct symbol *sym;
10295 /* If the exception info is already known, then no need to recompute it. */
10296 if (exception_info != NULL)
10299 /* Check the latest (default) exception support info. */
10300 sym = standard_lookup (default_exception_support_info.catch_exception_sym,
10304 exception_info = &default_exception_support_info;
10308 /* Try our fallback exception suport info. */
10309 sym = standard_lookup (exception_support_info_fallback.catch_exception_sym,
10313 exception_info = &exception_support_info_fallback;
10317 /* Sometimes, it is normal for us to not be able to find the routine
10318 we are looking for. This happens when the program is linked with
10319 the shared version of the GNAT runtime, and the program has not been
10320 started yet. Inform the user of these two possible causes if
10323 if (ada_update_initial_language (language_unknown) != language_ada)
10324 error (_("Unable to insert catchpoint. Is this an Ada main program?"));
10326 /* If the symbol does not exist, then check that the program is
10327 already started, to make sure that shared libraries have been
10328 loaded. If it is not started, this may mean that the symbol is
10329 in a shared library. */
10331 if (ptid_get_pid (inferior_ptid) == 0)
10332 error (_("Unable to insert catchpoint. Try to start the program first."));
10334 /* At this point, we know that we are debugging an Ada program and
10335 that the inferior has been started, but we still are not able to
10336 find the run-time symbols. That can mean that we are in
10337 configurable run time mode, or that a-except as been optimized
10338 out by the linker... In any case, at this point it is not worth
10339 supporting this feature. */
10341 error (_("Cannot insert catchpoints in this configuration."));
10344 /* An observer of "executable_changed" events.
10345 Its role is to clear certain cached values that need to be recomputed
10346 each time a new executable is loaded by GDB. */
10349 ada_executable_changed_observer (void)
10351 /* If the executable changed, then it is possible that the Ada runtime
10352 is different. So we need to invalidate the exception support info
10354 exception_info = NULL;
10357 /* True iff FRAME is very likely to be that of a function that is
10358 part of the runtime system. This is all very heuristic, but is
10359 intended to be used as advice as to what frames are uninteresting
10363 is_known_support_routine (struct frame_info *frame)
10365 struct symtab_and_line sal;
10367 enum language func_lang;
10370 /* If this code does not have any debugging information (no symtab),
10371 This cannot be any user code. */
10373 find_frame_sal (frame, &sal);
10374 if (sal.symtab == NULL)
10377 /* If there is a symtab, but the associated source file cannot be
10378 located, then assume this is not user code: Selecting a frame
10379 for which we cannot display the code would not be very helpful
10380 for the user. This should also take care of case such as VxWorks
10381 where the kernel has some debugging info provided for a few units. */
10383 if (symtab_to_fullname (sal.symtab) == NULL)
10386 /* Check the unit filename againt the Ada runtime file naming.
10387 We also check the name of the objfile against the name of some
10388 known system libraries that sometimes come with debugging info
10391 for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
10393 re_comp (known_runtime_file_name_patterns[i]);
10394 if (re_exec (sal.symtab->filename))
10396 if (sal.symtab->objfile != NULL
10397 && re_exec (sal.symtab->objfile->name))
10401 /* Check whether the function is a GNAT-generated entity. */
10403 find_frame_funname (frame, &func_name, &func_lang, NULL);
10404 if (func_name == NULL)
10407 for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
10409 re_comp (known_auxiliary_function_name_patterns[i]);
10410 if (re_exec (func_name))
10417 /* Find the first frame that contains debugging information and that is not
10418 part of the Ada run-time, starting from FI and moving upward. */
10421 ada_find_printable_frame (struct frame_info *fi)
10423 for (; fi != NULL; fi = get_prev_frame (fi))
10425 if (!is_known_support_routine (fi))
10434 /* Assuming that the inferior just triggered an unhandled exception
10435 catchpoint, return the address in inferior memory where the name
10436 of the exception is stored.
10438 Return zero if the address could not be computed. */
10441 ada_unhandled_exception_name_addr (void)
10443 return parse_and_eval_address ("e.full_name");
10446 /* Same as ada_unhandled_exception_name_addr, except that this function
10447 should be used when the inferior uses an older version of the runtime,
10448 where the exception name needs to be extracted from a specific frame
10449 several frames up in the callstack. */
10452 ada_unhandled_exception_name_addr_from_raise (void)
10455 struct frame_info *fi;
10457 /* To determine the name of this exception, we need to select
10458 the frame corresponding to RAISE_SYM_NAME. This frame is
10459 at least 3 levels up, so we simply skip the first 3 frames
10460 without checking the name of their associated function. */
10461 fi = get_current_frame ();
10462 for (frame_level = 0; frame_level < 3; frame_level += 1)
10464 fi = get_prev_frame (fi);
10469 enum language func_lang;
10471 find_frame_funname (fi, &func_name, &func_lang, NULL);
10472 if (func_name != NULL
10473 && strcmp (func_name, exception_info->catch_exception_sym) == 0)
10474 break; /* We found the frame we were looking for... */
10475 fi = get_prev_frame (fi);
10482 return parse_and_eval_address ("id.full_name");
10485 /* Assuming the inferior just triggered an Ada exception catchpoint
10486 (of any type), return the address in inferior memory where the name
10487 of the exception is stored, if applicable.
10489 Return zero if the address could not be computed, or if not relevant. */
10492 ada_exception_name_addr_1 (enum exception_catchpoint_kind ex,
10493 struct breakpoint *b)
10497 case ex_catch_exception:
10498 return (parse_and_eval_address ("e.full_name"));
10501 case ex_catch_exception_unhandled:
10502 return exception_info->unhandled_exception_name_addr ();
10505 case ex_catch_assert:
10506 return 0; /* Exception name is not relevant in this case. */
10510 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
10514 return 0; /* Should never be reached. */
10517 /* Same as ada_exception_name_addr_1, except that it intercepts and contains
10518 any error that ada_exception_name_addr_1 might cause to be thrown.
10519 When an error is intercepted, a warning with the error message is printed,
10520 and zero is returned. */
10523 ada_exception_name_addr (enum exception_catchpoint_kind ex,
10524 struct breakpoint *b)
10526 struct gdb_exception e;
10527 CORE_ADDR result = 0;
10529 TRY_CATCH (e, RETURN_MASK_ERROR)
10531 result = ada_exception_name_addr_1 (ex, b);
10536 warning (_("failed to get exception name: %s"), e.message);
10543 /* Implement the PRINT_IT method in the breakpoint_ops structure
10544 for all exception catchpoint kinds. */
10546 static enum print_stop_action
10547 print_it_exception (enum exception_catchpoint_kind ex, struct breakpoint *b)
10549 const CORE_ADDR addr = ada_exception_name_addr (ex, b);
10550 char exception_name[256];
10554 read_memory (addr, exception_name, sizeof (exception_name) - 1);
10555 exception_name [sizeof (exception_name) - 1] = '\0';
10558 ada_find_printable_frame (get_current_frame ());
10560 annotate_catchpoint (b->number);
10563 case ex_catch_exception:
10565 printf_filtered (_("\nCatchpoint %d, %s at "),
10566 b->number, exception_name);
10568 printf_filtered (_("\nCatchpoint %d, exception at "), b->number);
10570 case ex_catch_exception_unhandled:
10572 printf_filtered (_("\nCatchpoint %d, unhandled %s at "),
10573 b->number, exception_name);
10575 printf_filtered (_("\nCatchpoint %d, unhandled exception at "),
10578 case ex_catch_assert:
10579 printf_filtered (_("\nCatchpoint %d, failed assertion at "),
10584 return PRINT_SRC_AND_LOC;
10587 /* Implement the PRINT_ONE method in the breakpoint_ops structure
10588 for all exception catchpoint kinds. */
10591 print_one_exception (enum exception_catchpoint_kind ex,
10592 struct breakpoint *b, struct bp_location **last_loc)
10594 struct value_print_options opts;
10596 get_user_print_options (&opts);
10597 if (opts.addressprint)
10599 annotate_field (4);
10600 ui_out_field_core_addr (uiout, "addr", b->loc->gdbarch, b->loc->address);
10603 annotate_field (5);
10604 *last_loc = b->loc;
10607 case ex_catch_exception:
10608 if (b->exp_string != NULL)
10610 char *msg = xstrprintf (_("`%s' Ada exception"), b->exp_string);
10612 ui_out_field_string (uiout, "what", msg);
10616 ui_out_field_string (uiout, "what", "all Ada exceptions");
10620 case ex_catch_exception_unhandled:
10621 ui_out_field_string (uiout, "what", "unhandled Ada exceptions");
10624 case ex_catch_assert:
10625 ui_out_field_string (uiout, "what", "failed Ada assertions");
10629 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
10634 /* Implement the PRINT_MENTION method in the breakpoint_ops structure
10635 for all exception catchpoint kinds. */
10638 print_mention_exception (enum exception_catchpoint_kind ex,
10639 struct breakpoint *b)
10643 case ex_catch_exception:
10644 if (b->exp_string != NULL)
10645 printf_filtered (_("Catchpoint %d: `%s' Ada exception"),
10646 b->number, b->exp_string);
10648 printf_filtered (_("Catchpoint %d: all Ada exceptions"), b->number);
10652 case ex_catch_exception_unhandled:
10653 printf_filtered (_("Catchpoint %d: unhandled Ada exceptions"),
10657 case ex_catch_assert:
10658 printf_filtered (_("Catchpoint %d: failed Ada assertions"), b->number);
10662 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
10667 /* Implement the PRINT_RECREATE method in the breakpoint_ops structure
10668 for all exception catchpoint kinds. */
10671 print_recreate_exception (enum exception_catchpoint_kind ex,
10672 struct breakpoint *b, struct ui_file *fp)
10676 case ex_catch_exception:
10677 fprintf_filtered (fp, "catch exception");
10678 if (b->exp_string != NULL)
10679 fprintf_filtered (fp, " %s", b->exp_string);
10682 case ex_catch_exception_unhandled:
10683 fprintf_filtered (fp, "catch exception unhandled");
10686 case ex_catch_assert:
10687 fprintf_filtered (fp, "catch assert");
10691 internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
10695 /* Virtual table for "catch exception" breakpoints. */
10697 static enum print_stop_action
10698 print_it_catch_exception (struct breakpoint *b)
10700 return print_it_exception (ex_catch_exception, b);
10704 print_one_catch_exception (struct breakpoint *b, struct bp_location **last_loc)
10706 print_one_exception (ex_catch_exception, b, last_loc);
10710 print_mention_catch_exception (struct breakpoint *b)
10712 print_mention_exception (ex_catch_exception, b);
10716 print_recreate_catch_exception (struct breakpoint *b, struct ui_file *fp)
10718 print_recreate_exception (ex_catch_exception, b, fp);
10721 static struct breakpoint_ops catch_exception_breakpoint_ops =
10725 NULL, /* breakpoint_hit */
10726 print_it_catch_exception,
10727 print_one_catch_exception,
10728 print_mention_catch_exception,
10729 print_recreate_catch_exception
10732 /* Virtual table for "catch exception unhandled" breakpoints. */
10734 static enum print_stop_action
10735 print_it_catch_exception_unhandled (struct breakpoint *b)
10737 return print_it_exception (ex_catch_exception_unhandled, b);
10741 print_one_catch_exception_unhandled (struct breakpoint *b,
10742 struct bp_location **last_loc)
10744 print_one_exception (ex_catch_exception_unhandled, b, last_loc);
10748 print_mention_catch_exception_unhandled (struct breakpoint *b)
10750 print_mention_exception (ex_catch_exception_unhandled, b);
10754 print_recreate_catch_exception_unhandled (struct breakpoint *b,
10755 struct ui_file *fp)
10757 print_recreate_exception (ex_catch_exception_unhandled, b, fp);
10760 static struct breakpoint_ops catch_exception_unhandled_breakpoint_ops = {
10763 NULL, /* breakpoint_hit */
10764 print_it_catch_exception_unhandled,
10765 print_one_catch_exception_unhandled,
10766 print_mention_catch_exception_unhandled,
10767 print_recreate_catch_exception_unhandled
10770 /* Virtual table for "catch assert" breakpoints. */
10772 static enum print_stop_action
10773 print_it_catch_assert (struct breakpoint *b)
10775 return print_it_exception (ex_catch_assert, b);
10779 print_one_catch_assert (struct breakpoint *b, struct bp_location **last_loc)
10781 print_one_exception (ex_catch_assert, b, last_loc);
10785 print_mention_catch_assert (struct breakpoint *b)
10787 print_mention_exception (ex_catch_assert, b);
10791 print_recreate_catch_assert (struct breakpoint *b, struct ui_file *fp)
10793 print_recreate_exception (ex_catch_assert, b, fp);
10796 static struct breakpoint_ops catch_assert_breakpoint_ops = {
10799 NULL, /* breakpoint_hit */
10800 print_it_catch_assert,
10801 print_one_catch_assert,
10802 print_mention_catch_assert,
10803 print_recreate_catch_assert
10806 /* Return non-zero if B is an Ada exception catchpoint. */
10809 ada_exception_catchpoint_p (struct breakpoint *b)
10811 return (b->ops == &catch_exception_breakpoint_ops
10812 || b->ops == &catch_exception_unhandled_breakpoint_ops
10813 || b->ops == &catch_assert_breakpoint_ops);
10816 /* Return a newly allocated copy of the first space-separated token
10817 in ARGSP, and then adjust ARGSP to point immediately after that
10820 Return NULL if ARGPS does not contain any more tokens. */
10823 ada_get_next_arg (char **argsp)
10825 char *args = *argsp;
10829 /* Skip any leading white space. */
10831 while (isspace (*args))
10834 if (args[0] == '\0')
10835 return NULL; /* No more arguments. */
10837 /* Find the end of the current argument. */
10840 while (*end != '\0' && !isspace (*end))
10843 /* Adjust ARGSP to point to the start of the next argument. */
10847 /* Make a copy of the current argument and return it. */
10849 result = xmalloc (end - args + 1);
10850 strncpy (result, args, end - args);
10851 result[end - args] = '\0';
10856 /* Split the arguments specified in a "catch exception" command.
10857 Set EX to the appropriate catchpoint type.
10858 Set EXP_STRING to the name of the specific exception if
10859 specified by the user. */
10862 catch_ada_exception_command_split (char *args,
10863 enum exception_catchpoint_kind *ex,
10866 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
10867 char *exception_name;
10869 exception_name = ada_get_next_arg (&args);
10870 make_cleanup (xfree, exception_name);
10872 /* Check that we do not have any more arguments. Anything else
10875 while (isspace (*args))
10878 if (args[0] != '\0')
10879 error (_("Junk at end of expression"));
10881 discard_cleanups (old_chain);
10883 if (exception_name == NULL)
10885 /* Catch all exceptions. */
10886 *ex = ex_catch_exception;
10887 *exp_string = NULL;
10889 else if (strcmp (exception_name, "unhandled") == 0)
10891 /* Catch unhandled exceptions. */
10892 *ex = ex_catch_exception_unhandled;
10893 *exp_string = NULL;
10897 /* Catch a specific exception. */
10898 *ex = ex_catch_exception;
10899 *exp_string = exception_name;
10903 /* Return the name of the symbol on which we should break in order to
10904 implement a catchpoint of the EX kind. */
10906 static const char *
10907 ada_exception_sym_name (enum exception_catchpoint_kind ex)
10909 gdb_assert (exception_info != NULL);
10913 case ex_catch_exception:
10914 return (exception_info->catch_exception_sym);
10916 case ex_catch_exception_unhandled:
10917 return (exception_info->catch_exception_unhandled_sym);
10919 case ex_catch_assert:
10920 return (exception_info->catch_assert_sym);
10923 internal_error (__FILE__, __LINE__,
10924 _("unexpected catchpoint kind (%d)"), ex);
10928 /* Return the breakpoint ops "virtual table" used for catchpoints
10931 static struct breakpoint_ops *
10932 ada_exception_breakpoint_ops (enum exception_catchpoint_kind ex)
10936 case ex_catch_exception:
10937 return (&catch_exception_breakpoint_ops);
10939 case ex_catch_exception_unhandled:
10940 return (&catch_exception_unhandled_breakpoint_ops);
10942 case ex_catch_assert:
10943 return (&catch_assert_breakpoint_ops);
10946 internal_error (__FILE__, __LINE__,
10947 _("unexpected catchpoint kind (%d)"), ex);
10951 /* Return the condition that will be used to match the current exception
10952 being raised with the exception that the user wants to catch. This
10953 assumes that this condition is used when the inferior just triggered
10954 an exception catchpoint.
10956 The string returned is a newly allocated string that needs to be
10957 deallocated later. */
10960 ada_exception_catchpoint_cond_string (const char *exp_string)
10964 /* The standard exceptions are a special case. They are defined in
10965 runtime units that have been compiled without debugging info; if
10966 EXP_STRING is the not-fully-qualified name of a standard
10967 exception (e.g. "constraint_error") then, during the evaluation
10968 of the condition expression, the symbol lookup on this name would
10969 *not* return this standard exception. The catchpoint condition
10970 may then be set only on user-defined exceptions which have the
10971 same not-fully-qualified name (e.g. my_package.constraint_error).
10973 To avoid this unexcepted behavior, these standard exceptions are
10974 systematically prefixed by "standard". This means that "catch
10975 exception constraint_error" is rewritten into "catch exception
10976 standard.constraint_error".
10978 If an exception named contraint_error is defined in another package of
10979 the inferior program, then the only way to specify this exception as a
10980 breakpoint condition is to use its fully-qualified named:
10981 e.g. my_package.constraint_error. */
10983 for (i = 0; i < sizeof (standard_exc) / sizeof (char *); i++)
10985 if (strcmp (standard_exc [i], exp_string) == 0)
10987 return xstrprintf ("long_integer (e) = long_integer (&standard.%s)",
10991 return xstrprintf ("long_integer (e) = long_integer (&%s)", exp_string);
10994 /* Return the expression corresponding to COND_STRING evaluated at SAL. */
10996 static struct expression *
10997 ada_parse_catchpoint_condition (char *cond_string,
10998 struct symtab_and_line sal)
11000 return (parse_exp_1 (&cond_string, block_for_pc (sal.pc), 0));
11003 /* Return the symtab_and_line that should be used to insert an exception
11004 catchpoint of the TYPE kind.
11006 EX_STRING should contain the name of a specific exception
11007 that the catchpoint should catch, or NULL otherwise.
11009 The idea behind all the remaining parameters is that their names match
11010 the name of certain fields in the breakpoint structure that are used to
11011 handle exception catchpoints. This function returns the value to which
11012 these fields should be set, depending on the type of catchpoint we need
11015 If COND and COND_STRING are both non-NULL, any value they might
11016 hold will be free'ed, and then replaced by newly allocated ones.
11017 These parameters are left untouched otherwise. */
11019 static struct symtab_and_line
11020 ada_exception_sal (enum exception_catchpoint_kind ex, char *exp_string,
11021 char **addr_string, char **cond_string,
11022 struct expression **cond, struct breakpoint_ops **ops)
11024 const char *sym_name;
11025 struct symbol *sym;
11026 struct symtab_and_line sal;
11028 /* First, find out which exception support info to use. */
11029 ada_exception_support_info_sniffer ();
11031 /* Then lookup the function on which we will break in order to catch
11032 the Ada exceptions requested by the user. */
11034 sym_name = ada_exception_sym_name (ex);
11035 sym = standard_lookup (sym_name, NULL, VAR_DOMAIN);
11037 /* The symbol we're looking up is provided by a unit in the GNAT runtime
11038 that should be compiled with debugging information. As a result, we
11039 expect to find that symbol in the symtabs. If we don't find it, then
11040 the target most likely does not support Ada exceptions, or we cannot
11041 insert exception breakpoints yet, because the GNAT runtime hasn't been
11044 /* brobecker/2006-12-26: It is conceivable that the runtime was compiled
11045 in such a way that no debugging information is produced for the symbol
11046 we are looking for. In this case, we could search the minimal symbols
11047 as a fall-back mechanism. This would still be operating in degraded
11048 mode, however, as we would still be missing the debugging information
11049 that is needed in order to extract the name of the exception being
11050 raised (this name is printed in the catchpoint message, and is also
11051 used when trying to catch a specific exception). We do not handle
11052 this case for now. */
11055 error (_("Unable to break on '%s' in this configuration."), sym_name);
11057 /* Make sure that the symbol we found corresponds to a function. */
11058 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
11059 error (_("Symbol \"%s\" is not a function (class = %d)"),
11060 sym_name, SYMBOL_CLASS (sym));
11062 sal = find_function_start_sal (sym, 1);
11064 /* Set ADDR_STRING. */
11066 *addr_string = xstrdup (sym_name);
11068 /* Set the COND and COND_STRING (if not NULL). */
11070 if (cond_string != NULL && cond != NULL)
11072 if (*cond_string != NULL)
11074 xfree (*cond_string);
11075 *cond_string = NULL;
11082 if (exp_string != NULL)
11084 *cond_string = ada_exception_catchpoint_cond_string (exp_string);
11085 *cond = ada_parse_catchpoint_condition (*cond_string, sal);
11090 *ops = ada_exception_breakpoint_ops (ex);
11095 /* Parse the arguments (ARGS) of the "catch exception" command.
11097 Set TYPE to the appropriate exception catchpoint type.
11098 If the user asked the catchpoint to catch only a specific
11099 exception, then save the exception name in ADDR_STRING.
11101 See ada_exception_sal for a description of all the remaining
11102 function arguments of this function. */
11104 struct symtab_and_line
11105 ada_decode_exception_location (char *args, char **addr_string,
11106 char **exp_string, char **cond_string,
11107 struct expression **cond,
11108 struct breakpoint_ops **ops)
11110 enum exception_catchpoint_kind ex;
11112 catch_ada_exception_command_split (args, &ex, exp_string);
11113 return ada_exception_sal (ex, *exp_string, addr_string, cond_string,
11117 struct symtab_and_line
11118 ada_decode_assert_location (char *args, char **addr_string,
11119 struct breakpoint_ops **ops)
11121 /* Check that no argument where provided at the end of the command. */
11125 while (isspace (*args))
11128 error (_("Junk at end of arguments."));
11131 return ada_exception_sal (ex_catch_assert, NULL, addr_string, NULL, NULL,
11136 /* Information about operators given special treatment in functions
11138 /* Format: OP_DEFN (<operator>, <operator length>, <# args>, <binop>). */
11140 #define ADA_OPERATORS \
11141 OP_DEFN (OP_VAR_VALUE, 4, 0, 0) \
11142 OP_DEFN (BINOP_IN_BOUNDS, 3, 2, 0) \
11143 OP_DEFN (TERNOP_IN_RANGE, 1, 3, 0) \
11144 OP_DEFN (OP_ATR_FIRST, 1, 2, 0) \
11145 OP_DEFN (OP_ATR_LAST, 1, 2, 0) \
11146 OP_DEFN (OP_ATR_LENGTH, 1, 2, 0) \
11147 OP_DEFN (OP_ATR_IMAGE, 1, 2, 0) \
11148 OP_DEFN (OP_ATR_MAX, 1, 3, 0) \
11149 OP_DEFN (OP_ATR_MIN, 1, 3, 0) \
11150 OP_DEFN (OP_ATR_MODULUS, 1, 1, 0) \
11151 OP_DEFN (OP_ATR_POS, 1, 2, 0) \
11152 OP_DEFN (OP_ATR_SIZE, 1, 1, 0) \
11153 OP_DEFN (OP_ATR_TAG, 1, 1, 0) \
11154 OP_DEFN (OP_ATR_VAL, 1, 2, 0) \
11155 OP_DEFN (UNOP_QUAL, 3, 1, 0) \
11156 OP_DEFN (UNOP_IN_RANGE, 3, 1, 0) \
11157 OP_DEFN (OP_OTHERS, 1, 1, 0) \
11158 OP_DEFN (OP_POSITIONAL, 3, 1, 0) \
11159 OP_DEFN (OP_DISCRETE_RANGE, 1, 2, 0)
11162 ada_operator_length (const struct expression *exp, int pc, int *oplenp,
11165 switch (exp->elts[pc - 1].opcode)
11168 operator_length_standard (exp, pc, oplenp, argsp);
11171 #define OP_DEFN(op, len, args, binop) \
11172 case op: *oplenp = len; *argsp = args; break;
11178 *argsp = longest_to_int (exp->elts[pc - 2].longconst);
11183 *argsp = longest_to_int (exp->elts[pc - 2].longconst) + 1;
11188 /* Implementation of the exp_descriptor method operator_check. */
11191 ada_operator_check (struct expression *exp, int pos,
11192 int (*objfile_func) (struct objfile *objfile, void *data),
11195 const union exp_element *const elts = exp->elts;
11196 struct type *type = NULL;
11198 switch (elts[pos].opcode)
11200 case UNOP_IN_RANGE:
11202 type = elts[pos + 1].type;
11206 return operator_check_standard (exp, pos, objfile_func, data);
11209 /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL. */
11211 if (type && TYPE_OBJFILE (type)
11212 && (*objfile_func) (TYPE_OBJFILE (type), data))
11219 ada_op_name (enum exp_opcode opcode)
11224 return op_name_standard (opcode);
11226 #define OP_DEFN(op, len, args, binop) case op: return #op;
11231 return "OP_AGGREGATE";
11233 return "OP_CHOICES";
11239 /* As for operator_length, but assumes PC is pointing at the first
11240 element of the operator, and gives meaningful results only for the
11241 Ada-specific operators, returning 0 for *OPLENP and *ARGSP otherwise. */
11244 ada_forward_operator_length (struct expression *exp, int pc,
11245 int *oplenp, int *argsp)
11247 switch (exp->elts[pc].opcode)
11250 *oplenp = *argsp = 0;
11253 #define OP_DEFN(op, len, args, binop) \
11254 case op: *oplenp = len; *argsp = args; break;
11260 *argsp = longest_to_int (exp->elts[pc + 1].longconst);
11265 *argsp = longest_to_int (exp->elts[pc + 1].longconst) + 1;
11271 int len = longest_to_int (exp->elts[pc + 1].longconst);
11273 *oplenp = 4 + BYTES_TO_EXP_ELEM (len + 1);
11281 ada_dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
11283 enum exp_opcode op = exp->elts[elt].opcode;
11288 ada_forward_operator_length (exp, elt, &oplen, &nargs);
11292 /* Ada attributes ('Foo). */
11295 case OP_ATR_LENGTH:
11299 case OP_ATR_MODULUS:
11306 case UNOP_IN_RANGE:
11308 /* XXX: gdb_sprint_host_address, type_sprint */
11309 fprintf_filtered (stream, _("Type @"));
11310 gdb_print_host_address (exp->elts[pc + 1].type, stream);
11311 fprintf_filtered (stream, " (");
11312 type_print (exp->elts[pc + 1].type, NULL, stream, 0);
11313 fprintf_filtered (stream, ")");
11315 case BINOP_IN_BOUNDS:
11316 fprintf_filtered (stream, " (%d)",
11317 longest_to_int (exp->elts[pc + 2].longconst));
11319 case TERNOP_IN_RANGE:
11324 case OP_DISCRETE_RANGE:
11325 case OP_POSITIONAL:
11332 char *name = &exp->elts[elt + 2].string;
11333 int len = longest_to_int (exp->elts[elt + 1].longconst);
11335 fprintf_filtered (stream, "Text: `%.*s'", len, name);
11340 return dump_subexp_body_standard (exp, stream, elt);
11344 for (i = 0; i < nargs; i += 1)
11345 elt = dump_subexp (exp, stream, elt);
11350 /* The Ada extension of print_subexp (q.v.). */
11353 ada_print_subexp (struct expression *exp, int *pos,
11354 struct ui_file *stream, enum precedence prec)
11356 int oplen, nargs, i;
11358 enum exp_opcode op = exp->elts[pc].opcode;
11360 ada_forward_operator_length (exp, pc, &oplen, &nargs);
11367 print_subexp_standard (exp, pos, stream, prec);
11371 fputs_filtered (SYMBOL_NATURAL_NAME (exp->elts[pc + 2].symbol), stream);
11374 case BINOP_IN_BOUNDS:
11375 /* XXX: sprint_subexp */
11376 print_subexp (exp, pos, stream, PREC_SUFFIX);
11377 fputs_filtered (" in ", stream);
11378 print_subexp (exp, pos, stream, PREC_SUFFIX);
11379 fputs_filtered ("'range", stream);
11380 if (exp->elts[pc + 1].longconst > 1)
11381 fprintf_filtered (stream, "(%ld)",
11382 (long) exp->elts[pc + 1].longconst);
11385 case TERNOP_IN_RANGE:
11386 if (prec >= PREC_EQUAL)
11387 fputs_filtered ("(", stream);
11388 /* XXX: sprint_subexp */
11389 print_subexp (exp, pos, stream, PREC_SUFFIX);
11390 fputs_filtered (" in ", stream);
11391 print_subexp (exp, pos, stream, PREC_EQUAL);
11392 fputs_filtered (" .. ", stream);
11393 print_subexp (exp, pos, stream, PREC_EQUAL);
11394 if (prec >= PREC_EQUAL)
11395 fputs_filtered (")", stream);
11400 case OP_ATR_LENGTH:
11404 case OP_ATR_MODULUS:
11409 if (exp->elts[*pos].opcode == OP_TYPE)
11411 if (TYPE_CODE (exp->elts[*pos + 1].type) != TYPE_CODE_VOID)
11412 LA_PRINT_TYPE (exp->elts[*pos + 1].type, "", stream, 0, 0);
11416 print_subexp (exp, pos, stream, PREC_SUFFIX);
11417 fprintf_filtered (stream, "'%s", ada_attribute_name (op));
11422 for (tem = 1; tem < nargs; tem += 1)
11424 fputs_filtered ((tem == 1) ? " (" : ", ", stream);
11425 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
11427 fputs_filtered (")", stream);
11432 type_print (exp->elts[pc + 1].type, "", stream, 0);
11433 fputs_filtered ("'(", stream);
11434 print_subexp (exp, pos, stream, PREC_PREFIX);
11435 fputs_filtered (")", stream);
11438 case UNOP_IN_RANGE:
11439 /* XXX: sprint_subexp */
11440 print_subexp (exp, pos, stream, PREC_SUFFIX);
11441 fputs_filtered (" in ", stream);
11442 LA_PRINT_TYPE (exp->elts[pc + 1].type, "", stream, 1, 0);
11445 case OP_DISCRETE_RANGE:
11446 print_subexp (exp, pos, stream, PREC_SUFFIX);
11447 fputs_filtered ("..", stream);
11448 print_subexp (exp, pos, stream, PREC_SUFFIX);
11452 fputs_filtered ("others => ", stream);
11453 print_subexp (exp, pos, stream, PREC_SUFFIX);
11457 for (i = 0; i < nargs-1; i += 1)
11460 fputs_filtered ("|", stream);
11461 print_subexp (exp, pos, stream, PREC_SUFFIX);
11463 fputs_filtered (" => ", stream);
11464 print_subexp (exp, pos, stream, PREC_SUFFIX);
11467 case OP_POSITIONAL:
11468 print_subexp (exp, pos, stream, PREC_SUFFIX);
11472 fputs_filtered ("(", stream);
11473 for (i = 0; i < nargs; i += 1)
11476 fputs_filtered (", ", stream);
11477 print_subexp (exp, pos, stream, PREC_SUFFIX);
11479 fputs_filtered (")", stream);
11484 /* Table mapping opcodes into strings for printing operators
11485 and precedences of the operators. */
11487 static const struct op_print ada_op_print_tab[] = {
11488 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
11489 {"or else", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
11490 {"and then", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
11491 {"or", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
11492 {"xor", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
11493 {"and", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
11494 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
11495 {"/=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
11496 {"<=", BINOP_LEQ, PREC_ORDER, 0},
11497 {">=", BINOP_GEQ, PREC_ORDER, 0},
11498 {">", BINOP_GTR, PREC_ORDER, 0},
11499 {"<", BINOP_LESS, PREC_ORDER, 0},
11500 {">>", BINOP_RSH, PREC_SHIFT, 0},
11501 {"<<", BINOP_LSH, PREC_SHIFT, 0},
11502 {"+", BINOP_ADD, PREC_ADD, 0},
11503 {"-", BINOP_SUB, PREC_ADD, 0},
11504 {"&", BINOP_CONCAT, PREC_ADD, 0},
11505 {"*", BINOP_MUL, PREC_MUL, 0},
11506 {"/", BINOP_DIV, PREC_MUL, 0},
11507 {"rem", BINOP_REM, PREC_MUL, 0},
11508 {"mod", BINOP_MOD, PREC_MUL, 0},
11509 {"**", BINOP_EXP, PREC_REPEAT, 0},
11510 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
11511 {"-", UNOP_NEG, PREC_PREFIX, 0},
11512 {"+", UNOP_PLUS, PREC_PREFIX, 0},
11513 {"not ", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
11514 {"not ", UNOP_COMPLEMENT, PREC_PREFIX, 0},
11515 {"abs ", UNOP_ABS, PREC_PREFIX, 0},
11516 {".all", UNOP_IND, PREC_SUFFIX, 1},
11517 {"'access", UNOP_ADDR, PREC_SUFFIX, 1},
11518 {"'size", OP_ATR_SIZE, PREC_SUFFIX, 1},
11522 enum ada_primitive_types {
11523 ada_primitive_type_int,
11524 ada_primitive_type_long,
11525 ada_primitive_type_short,
11526 ada_primitive_type_char,
11527 ada_primitive_type_float,
11528 ada_primitive_type_double,
11529 ada_primitive_type_void,
11530 ada_primitive_type_long_long,
11531 ada_primitive_type_long_double,
11532 ada_primitive_type_natural,
11533 ada_primitive_type_positive,
11534 ada_primitive_type_system_address,
11535 nr_ada_primitive_types
11539 ada_language_arch_info (struct gdbarch *gdbarch,
11540 struct language_arch_info *lai)
11542 const struct builtin_type *builtin = builtin_type (gdbarch);
11544 lai->primitive_type_vector
11545 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_ada_primitive_types + 1,
11548 lai->primitive_type_vector [ada_primitive_type_int]
11549 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
11551 lai->primitive_type_vector [ada_primitive_type_long]
11552 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
11553 0, "long_integer");
11554 lai->primitive_type_vector [ada_primitive_type_short]
11555 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
11556 0, "short_integer");
11557 lai->string_char_type
11558 = lai->primitive_type_vector [ada_primitive_type_char]
11559 = arch_integer_type (gdbarch, TARGET_CHAR_BIT, 0, "character");
11560 lai->primitive_type_vector [ada_primitive_type_float]
11561 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
11563 lai->primitive_type_vector [ada_primitive_type_double]
11564 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
11565 "long_float", NULL);
11566 lai->primitive_type_vector [ada_primitive_type_long_long]
11567 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
11568 0, "long_long_integer");
11569 lai->primitive_type_vector [ada_primitive_type_long_double]
11570 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
11571 "long_long_float", NULL);
11572 lai->primitive_type_vector [ada_primitive_type_natural]
11573 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
11575 lai->primitive_type_vector [ada_primitive_type_positive]
11576 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
11578 lai->primitive_type_vector [ada_primitive_type_void]
11579 = builtin->builtin_void;
11581 lai->primitive_type_vector [ada_primitive_type_system_address]
11582 = lookup_pointer_type (arch_type (gdbarch, TYPE_CODE_VOID, 1, "void"));
11583 TYPE_NAME (lai->primitive_type_vector [ada_primitive_type_system_address])
11584 = "system__address";
11586 lai->bool_type_symbol = NULL;
11587 lai->bool_type_default = builtin->builtin_bool;
11590 /* Language vector */
11592 /* Not really used, but needed in the ada_language_defn. */
11595 emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
11597 ada_emit_char (c, type, stream, quoter, 1);
11603 warnings_issued = 0;
11604 return ada_parse ();
11607 static const struct exp_descriptor ada_exp_descriptor = {
11609 ada_operator_length,
11610 ada_operator_check,
11612 ada_dump_subexp_body,
11613 ada_evaluate_subexp
11616 const struct language_defn ada_language_defn = {
11617 "ada", /* Language name */
11621 case_sensitive_on, /* Yes, Ada is case-insensitive, but
11622 that's not quite what this means. */
11624 macro_expansion_no,
11625 &ada_exp_descriptor,
11629 ada_printchar, /* Print a character constant */
11630 ada_printstr, /* Function to print string constant */
11631 emit_char, /* Function to print single char (not used) */
11632 ada_print_type, /* Print a type using appropriate syntax */
11633 ada_print_typedef, /* Print a typedef using appropriate syntax */
11634 ada_val_print, /* Print a value using appropriate syntax */
11635 ada_value_print, /* Print a top-level value */
11636 NULL, /* Language specific skip_trampoline */
11637 NULL, /* name_of_this */
11638 ada_lookup_symbol_nonlocal, /* Looking up non-local symbols. */
11639 basic_lookup_transparent_type, /* lookup_transparent_type */
11640 ada_la_decode, /* Language specific symbol demangler */
11641 NULL, /* Language specific class_name_from_physname */
11642 ada_op_print_tab, /* expression operators for printing */
11643 0, /* c-style arrays */
11644 1, /* String lower bound */
11645 ada_get_gdb_completer_word_break_characters,
11646 ada_make_symbol_completion_list,
11647 ada_language_arch_info,
11648 ada_print_array_index,
11649 default_pass_by_reference,
11654 /* Provide a prototype to silence -Wmissing-prototypes. */
11655 extern initialize_file_ftype _initialize_ada_language;
11657 /* Command-list for the "set/show ada" prefix command. */
11658 static struct cmd_list_element *set_ada_list;
11659 static struct cmd_list_element *show_ada_list;
11661 /* Implement the "set ada" prefix command. */
11664 set_ada_command (char *arg, int from_tty)
11666 printf_unfiltered (_(\
11667 "\"set ada\" must be followed by the name of a setting.\n"));
11668 help_list (set_ada_list, "set ada ", -1, gdb_stdout);
11671 /* Implement the "show ada" prefix command. */
11674 show_ada_command (char *args, int from_tty)
11676 cmd_show_list (show_ada_list, from_tty, "");
11680 _initialize_ada_language (void)
11682 add_language (&ada_language_defn);
11684 add_prefix_cmd ("ada", no_class, set_ada_command,
11685 _("Prefix command for changing Ada-specfic settings"),
11686 &set_ada_list, "set ada ", 0, &setlist);
11688 add_prefix_cmd ("ada", no_class, show_ada_command,
11689 _("Generic command for showing Ada-specific settings."),
11690 &show_ada_list, "show ada ", 0, &showlist);
11692 add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
11693 &trust_pad_over_xvs, _("\
11694 Enable or disable an optimization trusting PAD types over XVS types"), _("\
11695 Show whether an optimization trusting PAD types over XVS types is activated"),
11697 This is related to the encoding used by the GNAT compiler. The debugger\n\
11698 should normally trust the contents of PAD types, but certain older versions\n\
11699 of GNAT have a bug that sometimes causes the information in the PAD type\n\
11700 to be incorrect. Turning this setting \"off\" allows the debugger to\n\
11701 work around this bug. It is always safe to turn this option \"off\", but\n\
11702 this incurs a slight performance penalty, so it is recommended to NOT change\n\
11703 this option to \"off\" unless necessary."),
11704 NULL, NULL, &set_ada_list, &show_ada_list);
11706 varsize_limit = 65536;
11708 obstack_init (&symbol_list_obstack);
11710 decoded_names_store = htab_create_alloc
11711 (256, htab_hash_string, (int (*)(const void *, const void *)) streq,
11712 NULL, xcalloc, xfree);
11714 observer_attach_executable_changed (ada_executable_changed_observer);
11716 /* Setup per-inferior data. */
11717 observer_attach_inferior_exit (ada_inferior_exit);
11719 = register_inferior_data_with_cleanup (ada_inferior_data_cleanup);